fix: hold TUI tool-schema turns, skip only in-flight tool results

Grok TUI declares a tools array on every agent request, including the
first thinking turn. Skipping hold on that schema forwarded
missing-thinking streams as HTTP 200.

Keep the replay-safety skip for in-flight tool output
(function_call_output, tool_result, role=tool). Compact skip and
previous_response_id pinning are unchanged.
This commit is contained in:
lij768423-svg committed 2026-08-20 09:11:24 +08:00
1 parent 78ff3b6ac1
commit b411d041d4
2 files changed
+72 -9

No files matched your search

@@ -287,10 +287,11 @@ func shouldHoldQualityStream(input Input, ownership *inferencedomain.ResponseOwn
if route.Provider != accountdomain.ProviderBuild && route.Provider != accountdomain.ProviderConsole {
return false
}
// Retrying a tool-capable request can repeat external side effects. Tool
// requests remain outside this feature until they have their own replay
// safety contract.
if qualityRequestUsesTools(input.Body) {
// Retrying an in-flight tool *result* can repeat external side effects.
// Grok TUI always declares a tools schema even on the first thinking
// turn; skipping that schema would disable the hold for every agent
// request. Only skip when the body already contains tool output.
if qualityRequestHasInFlightToolResults(input.Body) {
return false
}
// Aliases are rewritten before this gate, so inspect the effective request
@@ -306,11 +307,43 @@ func shouldHoldQualityStream(input Input, ownership *inferencedomain.ResponseOwn
}
func qualityRequestUsesTools(body []byte) bool {
var payload map[string]json.RawMessage
return qualityRequestHasInFlightToolResults(body)
}
func qualityRequestHasInFlightToolResults(body []byte) bool {
var payload any
if json.Unmarshal(body, &payload) != nil {
return false
}
return nonEmptyJSONCollection(payload["tools"]) || nonEmptyJSONCollection(payload["functions"])
return jsonTreeHasInFlightToolResult(payload)
}
func jsonTreeHasInFlightToolResult(node any) bool {
switch typed := node.(type) {
case map[string]any:
role := jsonNodeString(typed["role"])
typ := jsonNodeString(typed["type"])
if role == "tool" || typ == "function_call_output" || typ == "tool_result" || typ == "tool_use_output" {
return true
}
for _, child := range typed {
if jsonTreeHasInFlightToolResult(child) {
return true
}
}
case []any:
for _, child := range typed {
if jsonTreeHasInFlightToolResult(child) {
return true
}
}
}
return false
}
func jsonNodeString(value any) string {
text, _ := value.(string)
return strings.ToLower(strings.TrimSpace(text))
}
func qualityRequestDisablesReasoning(body []byte) bool {
@@ -647,14 +647,44 @@ func TestShouldHoldQualityStreamGates(t *testing.T) {
{name: "responses reasoning none", body: `{"reasoning":{"effort":"none"}}`},
{name: "messages thinking disabled", body: `{"thinking":{"type":"disabled"}}`},
{name: "messages zero thinking budget", body: `{"thinking":{"type":"enabled","budget_tokens":0}}`},
{name: "client tools", body: `{"tools":[{"type":"function","function":{"name":"charge"}}]}`},
{name: "legacy functions", body: `{"functions":[{"name":"charge"}]}`},
} {
t.Run(test.name, func(t *testing.T) {
request := input
request.Body = []byte(test.body)
if shouldHoldQualityStream(request, nil, route, audit.OperationChat, cfg) {
t.Fatal("explicitly disabled reasoning and tool requests must not be held")
t.Fatal("explicitly disabled reasoning must not be held")
}
})
}
for _, test := range []struct {
name string
body string
}{
{name: "client tools schema", body: `{"tools":[{"type":"function","function":{"name":"charge"}}]}`},
{name: "legacy functions schema", body: `{"functions":[{"name":"charge"}]}`},
{name: "tui tools schema plus user input", body: `{"model":"grok-4.6","tools":[{"type":"function","name":"read_file"}],"input":[{"role":"user","content":"hello"}]}`},
} {
t.Run(test.name, func(t *testing.T) {
request := input
request.Body = []byte(test.body)
if !shouldHoldQualityStream(request, nil, route, audit.OperationChat, cfg) {
t.Fatal("tools schema alone must still hold so TUI thinking turns are classified")
}
})
}
for _, test := range []struct {
name string
body string
}{
{name: "function_call_output", body: `{"input":[{"type":"function_call_output","call_id":"call_1","output":"done"}]}`},
{name: "tool_result", body: `{"input":[{"type":"tool_result","tool_use_id":"tool_1","content":"done"}]}`},
{name: "role tool", body: `{"messages":[{"role":"tool","content":"done"}]}`},
} {
t.Run(test.name, func(t *testing.T) {
request := input
request.Body = []byte(test.body)
if shouldHoldQualityStream(request, nil, route, audit.OperationChat, cfg) {
t.Fatal("in-flight tool results must not be held")
}
})
}