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 {