Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 100 additions & 6 deletions internal/gateway/toolshape.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"strings"
"sync"
"unicode"

"opencode2api/internal/jsonutil"
wire "opencode2api/internal/protocol"
Expand Down Expand Up @@ -245,7 +246,7 @@ func renameAndCompleteTools(payload map[string]any, protocol wire.Protocol, v1 b
}
}
}
items = append(items, convertLegacyFunctions(legacy, protocol, renames, used)...)
items = append(items, convertLegacyFunctions(legacy, protocol)...)
delete(payload, "functions")
}
}
Expand All @@ -264,6 +265,7 @@ func renameAndCompleteTools(payload map[string]any, protocol wire.Protocol, v1 b
if wireName := clientWireName(renames, used, name); wireName != name {
setToolName(protocol, entry, wireName)
}
sanitizeClientToolFingerprint(protocol, entry)
shaped = append(shaped, entry)
}
shaped = append(shaped, officialToolset(protocol, v1)...)
Expand Down Expand Up @@ -558,8 +560,8 @@ func setToolName(protocol wire.Protocol, entry map[string]any, name string) {
}

// convertLegacyFunctions turns the pre-tools "functions" array into entries of
// the protocol's tool shape and renames their names.
func convertLegacyFunctions(entries []any, protocol wire.Protocol, renames *wire.ToolRenames, used map[string]bool) []any {
// the protocol's tool shape; the shared shaping loop then renames them.
func convertLegacyFunctions(entries []any, protocol wire.Protocol) []any {
converted := make([]any, 0, len(entries))
for _, raw := range entries {
entry, ok := raw.(map[string]any)
Expand All @@ -570,12 +572,11 @@ func convertLegacyFunctions(entries []any, protocol wire.Protocol, renames *wire
if name == "" {
continue
}
wireName := clientWireName(renames, used, name)
description := jsonutil.StringAt(entry, "description")
switch protocol {
case wire.Chat:
function := map[string]any{
"name": wireName,
"name": name,
"description": description,
"parameters": legacyParameters(entry),
}
Expand All @@ -586,7 +587,7 @@ func convertLegacyFunctions(entries []any, protocol wire.Protocol, renames *wire
case wire.Responses:
tool := map[string]any{
"type": "function",
"name": wireName,
"name": name,
"description": description,
"parameters": legacyParameters(entry),
}
Expand Down Expand Up @@ -683,3 +684,96 @@ func rewriteToolReferences(payload map[string]any, protocol wire.Protocol, renam
}
}
}

// obfuscateToolDescription inserts zero-width spaces between prose words.
// Quoted text and Markdown code spans are kept intact. The operation is
// idempotent so repeated shaping does not accumulate invisible characters.
func obfuscateToolDescription(description string) string {
runes := []rune(description)
var result strings.Builder
var quote rune
code := false
escaped := false
for i, r := range runes {
if escaped {
result.WriteRune(r)
escaped = false
continue
}
if r == '\\' {
result.WriteRune(r)
escaped = true
continue
}
if quote == 0 && r == '`' {
code = !code
result.WriteRune(r)
continue
}
if !code && (r == '"' || r == '\'') {
if quote == r {
quote = 0
} else if quote == 0 && (r != '\'' || i == 0 || !unicode.IsLetter(runes[i-1])) {
quote = r
}
result.WriteRune(r)
continue
}
if !code && quote == 0 && r == ' ' && i > 0 && i+1 < len(runes) && unicode.IsLetter(runes[i-1]) && unicode.IsLetter(runes[i+1]) {
result.WriteRune('\u200b')
}
result.WriteRune(r)
}
return result.String()
}

// sanitizeClientToolFingerprint only changes descriptions of client-supplied
// function tools; property names, defaults, and other schema values stay intact.
func sanitizeClientToolFingerprint(protocol wire.Protocol, entry map[string]any) {
definition := entry
if protocol == wire.Chat {
function, ok := entry["function"].(map[string]any)
if !ok {
return
}
definition = function
}
if description, ok := definition["description"].(string); ok {
definition["description"] = obfuscateToolDescription(description)
}
schemaKey := "parameters"
if protocol == wire.Anthropic {
schemaKey = "input_schema"
}
sanitizeSchemaDescriptions(definition[schemaKey])
}

// Walk nested schemas, including arrays and composition keywords, without
// changing strings used as property names, examples, defaults, or enums.
func sanitizeSchemaDescriptions(value any) {
switch node := value.(type) {
case map[string]any:
for key, child := range node {
if key == "description" {
if description, ok := child.(string); ok {
node[key] = obfuscateToolDescription(description)
}
continue
}
switch key {
case "properties", "patternProperties", "$defs", "definitions":
if fields, ok := child.(map[string]any); ok {
for _, schema := range fields {
sanitizeSchemaDescriptions(schema)
}
}
case "items", "additionalProperties", "not", "if", "then", "else", "allOf", "anyOf", "oneOf", "prefixItems":
sanitizeSchemaDescriptions(child)
}
}
case []any:
for _, child := range node {
sanitizeSchemaDescriptions(child)
}
}
}
21 changes: 21 additions & 0 deletions internal/gateway/warp.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ func warpProxy(label string) *proxyTransport {
return &proxyTransport{name: label}
}

// warpRetryIDs refreshes the upstream session and project after a 429. The
// gateway request ID stays stable so attempts remain traceable in local logs.
func warpRetryIDs(ids identity.RequestIDs, previous *http.Response) identity.RequestIDs {
if upstreamStatus(previous) != http.StatusTooManyRequests {
return ids
}
newSession := func() string {
return identity.CanonicalSessionID(identity.RandomID("warp_retry", 16))
}
ids.Session = newSession()
ids.Project = identity.RandomProjectID()
if ids.ChildSession != "" {
ids.ChildSession = newSession()
}
if ids.ParentSession != "" {
ids.ParentSession = newSession()
}
return ids
}

// doWarpAnonymousUpstream is the anonymous lane with WARP as its only egress.
// It walks the same two upstream gates (v2 /inference first, v1 /zen second)
// as doAnonymousUpstream, but draws a WARP lease per attempt instead of a
Expand Down Expand Up @@ -275,6 +295,7 @@ func (g *Gateway) doWarpAnonymousUpstream(ctx context.Context, route models.Rout
meta.Attempts = attemptOffset + attempts
meta.Tier = string(config.TierZen)
}
ids = warpRetryIDs(ids, lastResponse)
if lastResponse != nil {
httpx.DrainAndClose(lastResponse.Body)
lastResponse = nil
Expand Down