From 278b0bb8e29a63a99001727574c82df47806b90c Mon Sep 17 00:00:00 2001 From: xiechen Date: Tue, 15 Sep 2026 18:06:45 +0800 Subject: [PATCH 1/2] fix: demote phantom tool_use stop when no tool block was assembled Upstream Chat/Responses turns sometimes end with finish_reason tool_calls while delivering no usable tool call (empty tool_calls array, or deltas without a name). The bridge forwarded that as stop_reason tool_use with zero tool_use content blocks. Strict clients then end the turn running nothing and reporting no error, which stalls agent loops mid-task with no visible failure. Guard both the streaming (Finish) and non-streaming (encodeBridgeResponse) paths: a tool stop with no assembled tool is demoted to a plain stop so the client treats it as text end-of-turn and continues. --- internal/protocol/response.go | 19 +++++++++++++++++++ internal/protocol/stream_emitter.go | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/internal/protocol/response.go b/internal/protocol/response.go index d95bf33..8f3c7dc 100644 --- a/internal/protocol/response.go +++ b/internal/protocol/response.go @@ -143,6 +143,14 @@ func encodeBridgeResponse(protocol Protocol, response bridgeResponse) map[string if response.ID == "" { response.ID = identity.RandomID("resp", 12) } + if isToolStop(response.Stop) && len(response.Tools) == 0 { + // The turn ended as a tool call upstream, but no usable tool block + // was ever assembled (missing name or arguments). A tool_use stop + // reason with zero tool_use blocks makes strict clients end the + // turn running nothing and reporting no error. Demote to a plain + // stop so the client treats it as text end-of-turn and continues. + response.Stop = "stop" + } switch protocol { case Chat: message := map[string]any{"role": "assistant", "content": response.Text} @@ -375,6 +383,17 @@ func canonicalAnthropicStop(stop string) string { } } +// isToolStop reports whether a canonical bridge stop reason promises tool +// calls downstream. +func isToolStop(stop string) bool { + switch stop { + case "tool_calls", "tool_use", "function_call": + return true + default: + return false + } +} + func canonicalResponsesIncomplete(reason string) string { switch reason { case "content_filter": diff --git a/internal/protocol/stream_emitter.go b/internal/protocol/stream_emitter.go index fa498d1..85b2ab4 100644 --- a/internal/protocol/stream_emitter.go +++ b/internal/protocol/stream_emitter.go @@ -200,6 +200,18 @@ func (emitter *bridgeStreamEmitter) tool(key string) *bridgeStreamTool { return tool } +// anyToolStarted reports whether at least one tool block was actually opened +// downstream. Tools stuck without a name never start; their stop promise must +// not be advertised as a tool stop. +func (emitter *bridgeStreamEmitter) anyToolStarted() bool { + for _, key := range emitter.order { + if tool := emitter.tools[key]; tool != nil && tool.Started { + return true + } + } + return false +} + func (emitter *bridgeStreamEmitter) start() error { if emitter.started { return nil @@ -474,6 +486,12 @@ func (emitter *bridgeStreamEmitter) Finish() error { emitter.stop = "stop" } } + if isToolStop(emitter.stop) && !emitter.anyToolStarted() { + // Same phantom-tool-call guard as encodeBridgeResponse, for the + // streaming path: a tool_use stop reason with zero tool_use + // content blocks ends the client turn silently. + emitter.stop = "stop" + } if err := emitter.finishReasoning(); err != nil { return err } From 82e88f2b7fa14ec193ec79310e33472aba474b09 Mon Sep 17 00:00:00 2001 From: Jason Xu Date: Sat, 19 Sep 2026 12:46:32 +0800 Subject: [PATCH 2/2] fix: filter phantom non-stream tool blocks --- internal/protocol/response.go | 36 ++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/protocol/response.go b/internal/protocol/response.go index 8f3c7dc..8976e95 100644 --- a/internal/protocol/response.go +++ b/internal/protocol/response.go @@ -3,6 +3,7 @@ package protocol import ( "encoding/json" "fmt" + "strings" "time" "opencode2api/internal/identity" @@ -143,13 +144,17 @@ func encodeBridgeResponse(protocol Protocol, response bridgeResponse) map[string if response.ID == "" { response.ID = identity.RandomID("resp", 12) } - if isToolStop(response.Stop) && len(response.Tools) == 0 { - // The turn ended as a tool call upstream, but no usable tool block - // was ever assembled (missing name or arguments). A tool_use stop - // reason with zero tool_use blocks makes strict clients end the - // turn running nothing and reporting no error. Demote to a plain - // stop so the client treats it as text end-of-turn and continues. - response.Stop = "stop" + if isToolStop(response.Stop) { + response.Tools = usableToolBlocks(response.Tools) + if len(response.Tools) == 0 { + // The turn ended as a tool call upstream, but no usable tool block + // was ever assembled (for example, the name was missing). A + // tool_use stop reason with no usable tool blocks makes strict + // clients end the turn running nothing and reporting no error. + // Demote to a plain stop so the client treats it as text + // end-of-turn and continues. + response.Stop = "stop" + } } switch protocol { case Chat: @@ -394,6 +399,23 @@ func isToolStop(stop string) bool { } } +// usableToolBlocks drops phantom tool blocks that cannot be executed by a +// downstream client. Empty arguments are valid, so the tool name is the +// minimum required signal here; the streaming emitter uses the same rule. +func usableToolBlocks(tools []bridgeBlock) []bridgeBlock { + if len(tools) == 0 { + return nil + } + usable := tools[:0] + for _, tool := range tools { + if strings.TrimSpace(tool.Name) == "" { + continue + } + usable = append(usable, tool) + } + return usable +} + func canonicalResponsesIncomplete(reason string) string { switch reason { case "content_filter":