From 0b5083440ffba01697fb88cbce304aa1b18324b8 Mon Sep 17 00:00:00 2001 From: Roo Thorp Date: Tue, 17 Nov 2026 11:48:52 +0000 Subject: [PATCH] fix(aibridge): correlate Responses API tool results followed by developer messages (#32053) When a user approves a command with "Yes, and don't ask again", a developer message is appended after the tool result. `correlatingToolCallID` only checked the last `input` item, so these requests weren't correlated and the session page split the agentic loop into a new thread with no prompt. `correlatingToolCallID` now walks back from the end of `input`, and skips developer/system messages and reasoning items. The request is correlated if the first remaining item is a tool result with a `call_id`. A user message or any other item means a new turn, so it isn't correlated. Fixes AIGOV-727 Generated by Coder Agents on behalf of @roothorp --- aibridge/intercept/responses/reqpayload.go | 54 ++++++++++++------- .../responses/reqpayload_internal_test.go | 30 +++++++++++ 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/aibridge/intercept/responses/reqpayload.go b/aibridge/intercept/responses/reqpayload.go index 7004591a170..d4e83f6333e 100644 --- a/aibridge/intercept/responses/reqpayload.go +++ b/aibridge/intercept/responses/reqpayload.go @@ -28,8 +28,11 @@ const ( var ( constCustomToolCallOutput = string(constant.ValueOf[constant.CustomToolCallOutput]()) + constDeveloper = string(constant.ValueOf[constant.Developer]()) constFunctionCallOutput = string(constant.ValueOf[constant.FunctionCallOutput]()) constInputText = string(constant.ValueOf[constant.InputText]()) + constReasoning = string(constant.ValueOf[constant.Reasoning]()) + constSystem = string(constant.ValueOf[constant.System]()) constUser = string(constant.ValueOf[constant.User]()) reqPathContent = string(constant.ValueOf[constant.Content]()) @@ -75,26 +78,41 @@ func (p RequestPayload) correlatingToolCallID() *string { } arr := items.Array() - if len(arr) == 0 { - return nil + for i := len(arr) - 1; i >= 0; i-- { + item := arr[i] + + // Clients can append items that don't start a new turn after a tool + // result. For example, Codex appends a developer message recording an + // approved command prefix. Look past them to find the tool result. + role := item.Get(reqPathRole).String() + itemType := item.Get(reqPathType).String() + if role == constDeveloper || role == constSystem || itemType == constReasoning { + continue + } + + // The first remaining item decides. Anything other than a tool result, + // such as a user message, means a new turn. Stopping here matters + // because the full history is resent on every request, so a new prompt + // always has older tool results earlier in the input. + // + // Both function and custom (freeform) tool results reference the + // originating tool call by call_id. Codex sends custom_tool_call_output + // for custom tools such as apply_patch. + switch itemType { + case constFunctionCallOutput, constCustomToolCallOutput: + default: + return nil + } + + callID := item.Get(reqPathCallID).String() + if callID == "" { + return nil + } + + return &callID } - // Both function and custom (freeform) tool results reference the - // originating tool call by call_id. Codex sends custom_tool_call_output - // for custom tools such as apply_patch. - last := arr[len(arr)-1] - switch last.Get(reqPathType).String() { - case constFunctionCallOutput, constCustomToolCallOutput: - default: - return nil - } - - callID := last.Get(reqPathCallID).String() - if callID == "" { - return nil - } - - return &callID + return nil } // LastUserPrompt returns input text with the "user" role from the last input diff --git a/aibridge/intercept/responses/reqpayload_internal_test.go b/aibridge/intercept/responses/reqpayload_internal_test.go index 0360e5bd253..1156da225fe 100644 --- a/aibridge/intercept/responses/reqpayload_internal_test.go +++ b/aibridge/intercept/responses/reqpayload_internal_test.go @@ -123,6 +123,36 @@ func TestCorrelatingToolCallID(t *testing.T) { name: "missing call id", payload: []byte(`{"input":[{"type":"function_call_output","output":"ok"}]}`), }, + { + name: "tool result followed by developer message", + payload: []byte(`{"model":"gpt-5","input":[{"role":"user","content":"hi"},{"type":"custom_tool_call","call_id":"call_custom","name":"exec","input":"pwd"},{"type":"custom_tool_call_output","call_id":"call_custom","output":"/home"},{"type":"message","role":"developer","content":[{"type":"input_text","text":"Approved command prefix saved"}]}]}`), + wantCall: new("call_custom"), + }, + { + name: "tool result followed by system, developer and reasoning items", + payload: []byte(`{"model":"gpt-5","input":[{"role":"user","content":"hi"},{"type":"function_call_output","call_id":"call_abc","output":"r1"},{"role":"system","content":"sys"},{"type":"reasoning","id":"rs_1","summary":[]},{"role":"developer","content":"dev"}]}`), + wantCall: new("call_abc"), + }, + { + name: "user message followed by developer message after older tool result", + payload: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","call_id":"call_first","output":"r1"},{"role":"user","content":"hi"},{"role":"developer","content":"dev"}]}`), + }, + { + name: "only developer and system messages", + payload: []byte(`{"model":"gpt-5","input":[{"role":"system","content":"sys"},{"role":"developer","content":"dev"}]}`), + }, + { + name: "assistant message after tool result stops the walk", + payload: []byte(`{"model":"gpt-5","input":[{"role":"user","content":"hi"},{"type":"function_call_output","call_id":"call_abc","output":"r1"},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]},{"role":"developer","content":"dev"}]}`), + }, + { + name: "tool call after tool result stops the walk", + payload: []byte(`{"model":"gpt-5","input":[{"role":"user","content":"hi"},{"type":"function_call_output","call_id":"call_abc","output":"r1"},{"type":"function_call","call_id":"call_next","name":"exec","arguments":"{}"},{"role":"developer","content":"dev"}]}`), + }, + { + name: "tool result missing call id followed by developer message does not fall back to older result", + payload: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","call_id":"call_first","output":"r1"},{"type":"function_call_output","output":"r2"},{"role":"developer","content":"dev"}]}`), + }, } for _, tc := range tests {