fix(codex): reconcile replayed web_search_call history with tool declarations

- Inject a web_search tool declaration for standard Codex requests when input replays web_search_call items without a web_search tool so upstream response protection remains available during compaction.
- Strip replayed web_search_call items from Responses Lite requests where top-level hosted tool declarations are omitted.

Closes: #6770
This commit is contained in:
Luis Pater
2026-10-19 19:00:41 +08:00
parent 8df4e42ab9
commit e7d4c7316f
6 changed files with 361 additions and 0 deletions

View File

@@ -152,3 +152,284 @@ func TestCodexExecutorCompactUseSIWCAppliesSIWCRulesBeforePayloadFinalizer(t *te
t.Fatalf("PayloadFinalizer temperature override = %v, want 0.25; body=%s", gotTemp, gotBody)
}
}
func TestCodexExecutorReplayedWebSearchCallHistoryEnsuresWebSearchTool_Issue6770(t *testing.T) {
hasWebSearchCallInInput := func(body []byte) bool {
for _, item := range gjson.GetBytes(body, "input").Array() {
if item.Get("type").String() == "web_search_call" {
return true
}
}
return false
}
countWebSearchTools := func(body []byte) int {
count := 0
for _, tool := range gjson.GetBytes(body, "tools").Array() {
if tool.Get("type").String() == "web_search" {
count++
}
}
return count
}
compactionPayloadWithEmptyTools := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"search docs"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"openai responses web search"}},{"id":"ws_2","type":"web_search_call","status":"completed","action":{"type":"open_page","url":"https://platform.openai.com/docs"}},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Found docs."}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"Summarize conversation"}]}],"tools":[],"tool_choice":"auto"}`
t.Run("ExecuteStream with empty tools injects web_search and avoids 502 response protection error", func(t *testing.T) {
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "text/event-stream")
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
_, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n"))
return
}
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"summary\"}]}],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{Codex: config.CodexConfig{StreamBootstrapBuffering: true}})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
"plan_type": "plus",
},
}
streamResult, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(compactionPayloadWithEmptyTools),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Stream: true,
})
if err != nil {
t.Fatalf("ExecuteStream() error = %v", err)
}
for chunk := range streamResult.Chunks {
if chunk.Err != nil {
t.Fatalf("stream chunk error = %v", chunk.Err)
}
}
if got := countWebSearchTools(gotBody); got != 1 {
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody)
}
})
t.Run("Execute non-stream with image generation disabled still injects web_search", func(t *testing.T) {
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "text/event-stream")
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
_, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n"))
return
}
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
},
}
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(compactionPayloadWithEmptyTools),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
})
if err != nil {
t.Fatalf("Execute() error = %v", err)
}
if got := countWebSearchTools(gotBody); got != 1 {
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody)
}
if got := len(gjson.GetBytes(gotBody, "tools").Array()); got != 1 {
t.Fatalf("tools len = %d, want 1; body=%s", got, gotBody)
}
})
t.Run("Execute compact endpoint injects web_search when input replays web_search_call", func(t *testing.T) {
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
gotBody = body
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`{"error":{"message":"response protection is unavailable","type":"internal_error","param":null,"code":null}}`))
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"resp_compact_1","object":"response.compaction","usage":{"input_tokens":10,"output_tokens":2,"total_tokens":12}}`))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
},
}
compactPayload := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":"history"},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"type":"compaction_trigger"}]}`
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(compactPayload),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Alt: "responses/compact",
})
if err != nil {
t.Fatalf("Execute compact error = %v", err)
}
if got := countWebSearchTools(gotBody); got != 1 {
t.Fatalf("compact web_search tool count = %d, want 1; body=%s", got, gotBody)
}
})
t.Run("prepareCodexWebsocketStream injects web_search when input replays web_search_call", func(t *testing.T) {
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": "http://127.0.0.1:1",
"api_key": "test",
"plan_type": "plus",
},
}
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(compactionPayloadWithEmptyTools),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Stream: true,
})
if err != nil {
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
}
if got := countWebSearchTools(prepared.clientBody); got != 1 {
t.Fatalf("websocket clientBody web_search tool count = %d, want 1; body=%s", got, prepared.clientBody)
}
})
t.Run("existing web_search tool declaration is preserved without duplication", func(t *testing.T) {
payloadWithWebSearch := `{"model":"gpt-6.1-sol","input":[{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}}],"tools":[{"type":"web_search","external_web_access":true}]}`
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": "http://127.0.0.1:1",
"api_key": "test",
},
}
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(payloadWithWebSearch),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Stream: true,
})
if err != nil {
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
}
if got := countWebSearchTools(prepared.clientBody); got != 1 {
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, prepared.clientBody)
}
if !gjson.GetBytes(prepared.clientBody, "tools.0.external_web_access").Bool() {
t.Fatalf("existing web_search options not preserved: %s", prepared.clientBody)
}
})
t.Run("Responses Lite request strips replayed web_search_call items without injecting top-level tools", func(t *testing.T) {
litePayload := `{"model":"gpt-6.1-sol","input":[{"type":"additional_tools","role":"developer","tools":[{"type":"namespace","name":"functions","description":"","tools":[{"type":"custom","name":"exec"}]}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"search"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"id":"rs_1","type":"reasoning","summary":[]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"answer"}]},{"type":"compaction_trigger"}],"client_metadata":{"ws_request_header_x_openai_internal_codex_responses_lite":"true"}}`
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{})
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": "http://127.0.0.1:1",
"api_key": "test",
},
}
headers := make(http.Header)
headers.Set(codexResponsesLiteHeader, "true")
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(litePayload),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Headers: headers,
Stream: true,
})
if err != nil {
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
}
if gjson.GetBytes(prepared.clientBody, "tools").Exists() {
t.Fatalf("Responses Lite request must not inject top-level tools: %s", prepared.clientBody)
}
if hasWebSearchCallInInput(prepared.clientBody) {
t.Fatalf("Responses Lite request must strip replayed web_search_call items: %s", prepared.clientBody)
}
inputItems := gjson.GetBytes(prepared.clientBody, "input").Array()
if len(inputItems) != 5 {
t.Fatalf("input len = %d, want 5; body=%s", len(inputItems), prepared.clientBody)
}
})
t.Run("PayloadFinalizer runs after ensureWebSearchToolForHistory", func(t *testing.T) {
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
}))
defer server.Close()
cfg := &config.Config{
SDKConfig: config.SDKConfig{
DisableImageGeneration: config.DisableImageGenerationAll,
},
Payload: config.PayloadConfig{
Filter: []config.PayloadFilterRule{
{
Models: []config.PayloadModelRule{{Name: "gpt-6.1-sol", Protocol: "codex"}},
Params: []string{"tools"},
},
},
},
}
executor := NewCodexExecutor(cfg)
auth := &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
},
}
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-6.1-sol",
Payload: []byte(compactionPayloadWithEmptyTools),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
})
if err != nil {
t.Fatalf("Execute() error = %v", err)
}
if gjson.GetBytes(gotBody, "tools").Exists() {
t.Fatalf("expected user payload filter rule to remove tools after built-in injection, got body=%s", gotBody)
}
})
}

View File

@@ -71,6 +71,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
body, _ = sjson.DeleteBytes(body, "stream_options")
useSIWC := codexUseSIWC(auth)
hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != ""
body = ensureWebSearchToolForHistory(body, opts.Headers)
if !useSIWC {
body = normalizeCodexInstructions(body, helps.IsNativeCodexRequest(req.Payload, opts))
if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff {
@@ -266,6 +267,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A
hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != ""
body = applyCodexSIWCCompactBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)})
}
body = ensureWebSearchToolForHistory(body, opts.Headers)
body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts), baseURL)
body = normalizeCodexParallelToolCalls(body, opts.Headers)
body = helps.NormalizeCodexToolSchemas(body)

View File

@@ -404,6 +404,81 @@ func normalizeCodexInstructions(body []byte, nativeRequest ...bool) []byte {
var imageGenToolJSON = []byte(`{"type":"image_generation","output_format":"png"}`)
var imageGenToolArrayJSON = []byte(`[{"type":"image_generation","output_format":"png"}]`)
var webSearchToolJSON = []byte(`{"type":"web_search"}`)
var webSearchToolArrayJSON = []byte(`[{"type":"web_search"}]`)
func isCodexWebSearchToolType(toolType string) bool {
switch strings.TrimSpace(toolType) {
case "web_search", "web_search_preview", "web_search_preview_2025_03_11":
return true
default:
return false
}
}
// ensureWebSearchToolForHistory reconciles replayed web_search_call input items
// with the request's tool declarations so upstream Codex does not fail with
// "response protection is unavailable". Standard Responses requests receive a
// {"type":"web_search"} tool declaration when missing, while Responses Lite
// requests (which omit top-level hosted tools) strip the replayed search items.
func ensureWebSearchToolForHistory(body []byte, headers http.Header) []byte {
if !bytes.Contains(body, []byte("web_search_call")) {
return body
}
input := util.GetGJSONBytesNoCopy(body, "input")
if !input.IsArray() {
return body
}
hasWebSearchCall := false
input.ForEach(func(_, item gjson.Result) bool {
if strings.TrimSpace(item.Get("type").String()) == "web_search_call" {
hasWebSearchCall = true
return false
}
return true
})
if !hasWebSearchCall {
return body
}
tools := util.GetGJSONBytesNoCopy(body, "tools")
if tools.IsArray() {
for _, t := range tools.Array() {
if isCodexWebSearchToolType(t.Get("type").String()) {
return body
}
}
}
if util.IsCodexResponsesLiteRequest(body, headers) {
items := input.Array()
rebuilt := make([]string, 0, len(items))
for _, item := range items {
if strings.TrimSpace(item.Get("type").String()) == "web_search_call" {
continue
}
rebuilt = append(rebuilt, item.Raw)
}
updated, errSet := sjson.SetRawBytes(body, "input", []byte("["+strings.Join(rebuilt, ",")+"]"))
if errSet != nil {
return body
}
return updated
}
if !tools.Exists() || !tools.IsArray() {
updated, errSet := sjson.SetRawBytes(body, "tools", webSearchToolArrayJSON)
if errSet != nil {
return body
}
return updated
}
updated, errSet := sjson.SetRawBytes(body, "tools.-1", webSearchToolJSON)
if errSet != nil {
return body
}
return updated
}
func isCodexFreePlanAuth(auth *cliproxyauth.Auth) bool {
if auth == nil || auth.Attributes == nil {

View File

@@ -78,6 +78,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
body = helps.SetStringIfDifferent(body, "model", baseModel)
useSIWC := codexUseSIWC(auth)
hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != ""
body = ensureWebSearchToolForHistory(body, opts.Headers)
if !useSIWC {
body = normalizeCodexInstructions(body, preserveNativeOutput)
if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff {

View File

@@ -72,6 +72,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut
body = helps.SetBoolIfDifferent(body, "stream", true)
body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
body, _ = sjson.DeleteBytes(body, "safety_identifier")
body = ensureWebSearchToolForHistory(body, opts.Headers)
if !useSIWC {
body = normalizeCodexInstructions(body, nativeRequest)
if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff {

View File

@@ -800,6 +800,7 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex
hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != ""
finalizePayload := helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts)
body = helps.SetStringIfDifferent(body, "model", baseModel)
body = ensureWebSearchToolForHistory(body, opts.Headers)
if !useSIWC {
body = normalizeCodexInstructions(body, preserveNativeOutput)
if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff {