gateway: Antigravity tool names, malformed calls and empty-reply retries (fwxkogaia on X)

Claude Desktop on antigravity/gemini-4-carbon failed 403 of 408 requests:
400 "Request contains an invalid argument", 502 empty reply after 170-230s,
and 200s marked failed with output tokens.

Changed, in internal/gateway:
- A tool name starting with a digit or a dash (MCP tools named for an id)
  goes to Google as _x_<name> and comes back without it; a property with no
  name is left out of the Antigravity schema. Both turned the whole request
  away on a live call.
- A MALFORMED_FUNCTION_CALL / UNEXPECTED_TOOL_CALL finish carries the call
  in finishMessage; it is now made the call instead of reading as an empty
  reply (tokens spent, then asked again for nothing).
- passing(): a 502 empty reply is asked again once, not lastRetries times on
  top of the in-place emptyRetries.

Run (mica): go test -run 'TestCodeAssist|TestEmptyReply' ./internal/gateway;
the new tests are red on the old code (names, malformed case).
Live (owner account, gemini-3.8-flash): names with a digit/dash first, a
space, unicode, a slash and an empty property key each 400; 64 and 100 char
names are 200.
Not tried: real gemini-4-carbon (the account lacks it, 404), so the user's
exact 400 is not reproduced; the finishMessage form is Google's documented
shape, not captured from Carbon.
This commit is contained in:
yetone
2026-11-12 23:17:51 +08:00
parent f4f104d1cc
commit 8c0c2e8b6b
4 changed files with 102 additions and 6 deletions
+43 -6
View File
@@ -436,7 +436,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
}
case ToolCall:
names[p.ID] = p.Name
call := map[string]any{"name": p.Name, "args": json.RawMessage(argsOf(p))}
call := map[string]any{"name": geminiFuncName(p.Name), "args": json.RawMessage(argsOf(p))}
if id := toolID(p.ID); id != "" {
call["id"] = id
}
@@ -452,7 +452,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
}
parts = append(parts, part)
case ToolResult:
name := names[p.CallID]
name := geminiFuncName(names[p.CallID])
if name == "" {
name = "tool"
}
@@ -575,7 +575,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
if len(r.Tools) > 0 && r.ToolChoice != "none" {
var decls []map[string]any
for _, t := range r.Tools {
d := map[string]any{"name": t.Name}
d := map[string]any{"name": geminiFuncName(t.Name)}
if t.Description != "" {
d["description"] = t.Description
}
@@ -598,7 +598,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
mode = "ANY"
case strings.HasPrefix(r.ToolChoice, "name:"):
mode = "ANY"
fc["allowedFunctionNames"] = []string{strings.TrimPrefix(r.ToolChoice, "name:")}
fc["allowedFunctionNames"] = []string{geminiFuncName(strings.TrimPrefix(r.ToolChoice, "name:"))}
case ag && claude:
// Antigravity's Claude wants its calls checked against the schema
mode = "VALIDATED"
@@ -1165,6 +1165,12 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
props := map[string]any{}
if m, ok := v.(map[string]any); ok {
for pk, pv := range m {
if pk == "" {
// Google turns a request away over a property
// with no name ("Request contains an invalid
// argument", fwxkogaia on X)
continue
}
// a property whose schema is false is one the
// call may not have: it is left out, and so is
// its name in required
@@ -1530,6 +1536,20 @@ func (d *codeAssistDecoder) decode(data string, emit func(Event)) error {
}
if cand.FinishReason != "" && !d.stopped {
d.flush(emit)
if !d.tools && (cand.FinishReason == "MALFORMED_FUNCTION_CALL" || cand.FinishReason == "UNEXPECTED_TOOL_CALL") {
// the model made a call and Google would not take it as one
// ("Malformed function call: call:default_api:Name{…}"): the
// call is in the message, whole, and the client can run it.
// Left alone the reply read as empty, though it had cost
// tokens, and was asked again for nothing (fwxkogaia on X)
if i := strings.Index(cand.FinishMessage, callMarker); i >= 0 {
if name, args, n := textCall(cand.FinishMessage[i:]); n > 0 {
d.tools = true
emit(Event{Kind: KToolStart, ID: "call_" + newID(), Name: toolOfCall(name)})
emit(Event{Kind: KToolArgs, Text: string(args)})
}
}
}
d.stopped = true
stop := stopFromGemini(cand.FinishReason)
if d.tools && stop == "stop" {
@@ -1557,14 +1577,30 @@ const callMarker = "call:" + geminiNamespace + ":"
const geminiNamespace = "default_api"
// nameLead stands before a tool name Google would turn away.
const nameLead = "_x_"
// geminiFuncName is a tool's name as Google takes it: a function name
// starts with a letter or an underscore, so one that starts with a digit or
// a dash (MCP tools named for their server's id) turned the whole request
// away as "Request contains an invalid argument" (fwxkogaia on X). The
// decoder takes nameLead off again.
func geminiFuncName(n string) string {
if n != "" && (n[0] >= '0' && n[0] <= '9' || n[0] == '-' || n[0] == '.' || n[0] == ':') {
return nameLead + n
}
return n
}
// toolOfCall is a call's tool name without Gemini's namespace.
func toolOfCall(name string) string {
for _, sep := range []string{":", "."} {
if t, ok := strings.CutPrefix(name, geminiNamespace+sep); ok && t != "" {
return t
name = t
break
}
}
return name
return strings.TrimPrefix(name, nameLead)
}
// maxHeld bounds a call written out as text: one that goes on longer is
@@ -1777,6 +1813,7 @@ type geminiChunk struct {
Parts []gPart `json:"parts"`
} `json:"content"`
FinishReason string `json:"finishReason"`
FinishMessage string `json:"finishMessage"`
GroundingMetadata *struct {
WebSearchQueries []string `json:"webSearchQueries"`
GroundingChunks []struct {
+49
View File
@@ -0,0 +1,49 @@
package gateway
import (
"encoding/json"
"strings"
"testing"
)
// Google turns the whole request away over a function name that starts with
// a digit or a dash, and over a property with no name (fwxkogaia on X).
func TestCodeAssistToolNamesGoogleTakes(t *testing.T) {
schema := json.RawMessage(`{"type":"object","properties":{"":{"type":"string"},"q":{"type":"string"}},"required":["","q"]}`)
r := &Request{Model: "gemini-4-carbon", Tools: []Tool{{Name: "1pass_get", Schema: schema}, {Name: "-dash", Schema: schema}, {Name: "ok", Schema: schema}},
Messages: []Message{
{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}},
{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: "c1", Name: "1pass_get", Args: json.RawMessage(`{}`)}}},
{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: "c1", Text: "x"}}},
}}
out := string(buildCodeAssistSent(r, "gemini-4-carbon", "antigravity"))
for _, bad := range []string{`"name":"1pass_get"`, `"name":"-dash"`, `"":`} {
if strings.Contains(out, bad) {
t.Errorf("request still has %s: %s", bad, out)
}
}
if n := strings.Count(out, `"name":"_x_1pass_get"`); n != 3 {
t.Errorf("_x_1pass_get in declaration, call and result: %d, want 3: %s", n, out)
}
if got := toolOfCall("_x_1pass_get"); got != "1pass_get" {
t.Errorf("decoded %q", got)
}
if got := toolOfCall("default_api:_x_-dash"); got != "-dash" {
t.Errorf("decoded %q", got)
}
}
// A reply that answered nothing is asked again in place, so the request
// is asked again once, not as often as an outage (fwxkogaia on X).
func TestEmptyReplyAskedAgainOnce(t *testing.T) {
body := []byte(`{"error":{"message":"antigravity: ` + emptyReply + `"}}`)
if _, ok := passing(502, nil, body, 0); !ok {
t.Error("an empty reply is asked again once")
}
if _, ok := passing(502, nil, body, 1); ok {
t.Error("an empty reply is not asked again a second time over")
}
if _, ok := passing(502, nil, []byte(`bad gateway`), 1); !ok {
t.Error("a busy vendor is still asked again")
}
}
+2
View File
@@ -297,6 +297,8 @@ func TestCodeAssistTextCalls(t *testing.T) {
"", []call{{"bash", `{"command":"ls"}`}}, "tool"},
{"unfinished", []string{chunk(text("look call:default_api:bash{command:ls"), "MAX_TOKENS")},
"look call:default_api:bash{command:ls", nil, "length"},
{"malformed", []string{`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"x","thought":true}]},"finishReason":"MALFORMED_FUNCTION_CALL","finishMessage":"Malformed function call: call:default_api:bash{command:ls -la}"}]}}`},
"", []call{{"bash", `{"command":"ls -la"}`}}, "tool"},
{"not a call", []string{chunk(text("say call:default_api: or recall"), ""), chunk(text(" ca"), "STOP")},
"say call:default_api: or recall ca", nil, "stop"},
} {
+8
View File
@@ -2301,6 +2301,14 @@ func passing(status int, header http.Header, body []byte, again int) (time.Durat
// (#2972). A group's next member still is.
return 0, false
}
if bytes.Contains(body, []byte(emptyReply)) {
// a model that answered nothing was asked again in place
// already (emptyRetries); asking the whole request again as
// often as a vendor's outage multiplied the two, nine asks of a
// minute each for one turn (fwxkogaia on X: 170 to 230s
// before the 502). Once more, for a try that was not.
return wait, again < 1
}
return wait, again < lastRetries
case status == 429:
// a relay's 429 often says nothing of when (#503: "负载已饱和,请稍