The ChatGPT backend answers HTTP 200 and only `event: error` "response
protection is unavailable" to any request whose input replays one of its
own web_search_call items while the request doesn't declare the web_search
tool. Probed on a real ChatGPT Pro account (gpt-6.1-sol, 2026-10-09) with
a real searched turn: refused with tools [], with function tools only and
with a compaction_trigger; answered once {"type":"web_search",
"external_web_access":false} is declared. A Responses Lite request refuses
a top-level web_search (400) but takes it in an additional_tools item. So
xjdata's case (Codex compacting a custom provider's conversation itself, a
plain /responses with tools [] that magpie's codexCompact never sees) failed
every time once the session had searched, and congee949's thread
descriptions too.
What changed:
- provider.DeclareSearch / declareSearch: when the input holds a
web_search_call and nothing declares search, add cached-only web_search to
the tools, or for Lite (header, reasoning.context all_turns, or an
additional_tools item present) to the additional_tools item (prepended
when there is none). With no tools offered, tool_choice becomes "none" so
the model can't search; a forced choice is kept. Otherwise the body is
unchanged (bytes untouched when no web_search_call is in it).
- codexBody (every ChatGPT account route: serveAgent, codexCompact, warmup,
magpie's codex/* models) calls it; codexUpstreamOn calls it for Codex's
own sign-in relayed to the backend's /responses.
- Not changed: siwc, API-key passthrough to api.openai.com, the legacy
/responses/compact endpoint (no evidence they are refused so; not probed).
- docs/subsystems/gateway-routing.md names the cause and the fix.
Tests: internal/gateway/search_history_test.go replays the backend's real
items from that probe against a fake that refuses as the backend did.
TestSearchedConversationCompactsOnAnAccount, ...FunctionTools and
...OnCodexSignIn fail on the old code with "event: error"; ...CompactionTrigger
fails with "asked 2 times, not once". TestUnsearchedConversationUnchanged
and provider TestDeclareSearch cover what's left alone.
Run: gofmt -l (clean); go vet, GOOS=linux go vet -tags nogui, GOOS=windows
go vet -tags nogui on internal/provider and internal/gateway; HOME=<temp>
go test -tags nogui ./internal/provider ./internal/gateway (ok). Real
account, sandbox magpie on 127.0.0.1:3491 under a scratch HOME, replaying
the real searched history: old binary -> /v1/responses codex/gpt-6.1-sol
text compaction with tools [] and /backend-api/codex/responses (standard
and Lite compaction_trigger) all "response protection is unavailable";
new binary -> all response.completed, Lite with a compaction item.
Codex CLI 0.161.0 end to end was not done: on a custom provider it
offered no web_search tool, so no search could get into its history.
215 lines
11 KiB
Go
215 lines
11 KiB
Go
package gateway
|
||
|
||
import (
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"sync"
|
||
"testing"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// searchedHistory is a conversation that searched the web, its items the
|
||
// ChatGPT backend's own (gpt-6.1-sol, a ChatGPT Pro account, 2026-10-09):
|
||
// a commentary message, the web_search_call, the answer citing what it
|
||
// found.
|
||
const searchedHistory = `{"type":"message","role":"user","content":[{"type":"input_text","text":"Use web search: what is the newest Go release announced on go.dev? Answer in one line."}]},` +
|
||
`{"id":"msg_0c7e789f31e92725016ac8f910d47087d0a126fed4f3afdc86","type":"message","status":"completed","content":[{"type":"output_text","annotations":[],"logprobs":[],"text":"I’ll check go.dev for the latest release announcement.\n"}],"phase":"commentary","role":"assistant"},` +
|
||
`{"id":"ws_0c7e789f31e92725016ac8f91213cc87d0ac350b2fd39a94a2","type":"web_search_call","status":"completed","action":{"type":"search","queries":["site:go.dev newest Go release October 2026 release"],"query":"site:go.dev newest Go release October 2026 release"}},` +
|
||
`{"id":"msg_0c7e789f31e92725016ac8f915990c87d0987c34e2e7b8b409","type":"message","status":"completed","content":[{"type":"output_text","annotations":[{"type":"url_citation","end_index":152,"start_index":90,"title":"Release History - The Go Programming Language","url":"https://go.dev/doc/devel/release?utm_source=openai"}],"logprobs":[],"text":"The newest Go release listed on go.dev is **Go 1.27.2**, released on **October 8, 2026**. ([go.dev](https://go.dev/doc/devel/release?utm_source=openai))"}],"phase":"final_answer","role":"assistant"}`
|
||
|
||
// protectedSearch stands in for the ChatGPT backend as it answered that
|
||
// history on 2026-10-09: replayed without the web_search tool declared,
|
||
// HTTP 200 and only this error event; declared (in the tools, or in an
|
||
// additional_tools item), a reply. A Lite request with web_search among
|
||
// its top-level tools is refused 400, as the backend refused it. It keeps
|
||
// the bodies it was sent.
|
||
func protectedSearch(t *testing.T) *[][]byte {
|
||
t.Helper()
|
||
var mu sync.Mutex
|
||
got := &[][]byte{}
|
||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
b, _ := io.ReadAll(r.Body)
|
||
mu.Lock()
|
||
*got = append(*got, b)
|
||
mu.Unlock()
|
||
var q struct {
|
||
Input []struct {
|
||
Type string `json:"type"`
|
||
Tools []map[string]any `json:"tools"`
|
||
} `json:"input"`
|
||
Tools []map[string]any `json:"tools"`
|
||
}
|
||
json.Unmarshal(b, &q)
|
||
searched, declared, compact := false, false, false
|
||
for _, it := range q.Input {
|
||
searched = searched || it.Type == "web_search_call"
|
||
compact = compact || it.Type == "compaction_trigger"
|
||
for _, tl := range it.Tools {
|
||
declared = declared || tl["type"] == "web_search"
|
||
}
|
||
}
|
||
for _, tl := range q.Tools {
|
||
if tl["type"] == "web_search" && strings.EqualFold(r.Header.Get("X-OpenAI-Internal-Codex-Responses-Lite"), "true") {
|
||
w.WriteHeader(400)
|
||
io.WriteString(w, `{"error":{"message":"X-OpenAI-Internal-Codex-Responses-Lite only supports function tools, custom tools, and client-executed tool search.","type":"invalid_request_error","param":"tools","code":null}}`)
|
||
return
|
||
}
|
||
declared = declared || tl["type"] == "web_search"
|
||
}
|
||
if searched && !declared {
|
||
io.WriteString(w, "event: error\ndata: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n")
|
||
return
|
||
}
|
||
item := `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Go 1.27.2 is the newest release."}]}`
|
||
if compact {
|
||
item = `{"type":"compaction","encrypted_content":"gAAAAABsealed-by-the-backend"}`
|
||
}
|
||
io.WriteString(w, sse(
|
||
`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-6.1-sol"}}`,
|
||
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
|
||
`data: {"type":"response.completed","response":{"id":"resp_1","usage":{"input_tokens":9,"output_tokens":2}}}`))
|
||
}))
|
||
t.Cleanup(up.Close)
|
||
was := provider.CodexBase
|
||
provider.CodexBase = up.URL + "/backend-api/codex"
|
||
t.Cleanup(func() { provider.CodexBase = was })
|
||
return got
|
||
}
|
||
|
||
// sentTools is what a request sent upstream declared: its tools' types,
|
||
// its additional_tools items' tools' types, and its tool_choice.
|
||
func sentTools(t *testing.T, b []byte) (tools, additional []string, choice any, last string) {
|
||
t.Helper()
|
||
var q struct {
|
||
Input []struct {
|
||
Type string `json:"type"`
|
||
Tools []map[string]any `json:"tools"`
|
||
} `json:"input"`
|
||
Tools []map[string]any `json:"tools"`
|
||
ToolChoice any `json:"tool_choice"`
|
||
}
|
||
if err := json.Unmarshal(b, &q); err != nil {
|
||
t.Fatalf("%v: %s", err, b)
|
||
}
|
||
for _, tl := range q.Tools {
|
||
tools = append(tools, tl["type"].(string))
|
||
}
|
||
for _, it := range q.Input {
|
||
for _, tl := range it.Tools {
|
||
additional = append(additional, tl["type"].(string))
|
||
}
|
||
}
|
||
if n := len(q.Input); n > 0 {
|
||
last = q.Input[n-1].Type
|
||
}
|
||
return tools, additional, q.ToolChoice, last
|
||
}
|
||
|
||
// #1270 (xjdata, Codex CLI 0.162.0 on a custom provider, codex/gpt-6-astra):
|
||
// Codex compacts a custom provider's conversation itself, a plain request
|
||
// with tools: [] and no compaction_trigger, so magpie's compaction never
|
||
// sees it. A conversation that searched the web failed so every time; one
|
||
// that didn't was answered.
|
||
func TestSearchedConversationCompactsOnAnAccount(t *testing.T) {
|
||
codexSignedIn(t)
|
||
got := protectedSearch(t)
|
||
code, body := post(t, "/v1/responses", `{"model":"codex/gpt-6-astra","stream":true,"store":false,"tools":[],"tool_choice":"auto","parallel_tool_calls":false,"input":[`+
|
||
searchedHistory+`,{"type":"message","role":"user","content":[{"type":"input_text","text":"You are performing a CONTEXT CHECKPOINT COMPACTION. Create a handoff summary for another LLM that will resume the task."}]}]}`)
|
||
if code != 200 || strings.Contains(body, "response protection") || !strings.Contains(body, "response.completed") {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
tools, _, choice, _ := sentTools(t, (*got)[len(*got)-1])
|
||
if len(tools) != 1 || tools[0] != "web_search" || choice != "none" {
|
||
t.Errorf("sent tools %v, tool_choice %v: want the web_search tool and none", tools, choice)
|
||
}
|
||
if !strings.Contains(string((*got)[len(*got)-1]), `"external_web_access":false`) {
|
||
t.Errorf("the declared search may fetch live: %s", (*got)[len(*got)-1])
|
||
}
|
||
}
|
||
|
||
// Codex's remote compaction of a magpie model on the ChatGPT accounts:
|
||
// the backend writes the summary from the history as it came, once,
|
||
// rather than refusing it and leaving magpie to ask again as text.
|
||
func TestSearchedConversationCompactionTrigger(t *testing.T) {
|
||
codexSignedIn(t)
|
||
got := protectedSearch(t)
|
||
code, body := codexPost(t, `{"model":"codex/gpt-6.1-sol","stream":true,"store":false,"tools":[],"input":[`+searchedHistory+`,{"type":"compaction_trigger"}]}`)
|
||
if code != 200 || !strings.Contains(body, `"type":"compaction"`) {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
if len(*got) != 1 {
|
||
t.Fatalf("asked %d times, not once", len(*got))
|
||
}
|
||
tools, _, choice, _ := sentTools(t, (*got)[0])
|
||
if len(tools) != 1 || tools[0] != "web_search" || choice != "none" {
|
||
t.Errorf("sent tools %v, tool_choice %v", tools, choice)
|
||
}
|
||
}
|
||
|
||
// A turn with Codex's function tools but not web search (search turned
|
||
// off since the conversation searched) keeps its tools callable as asked.
|
||
func TestSearchedConversationFunctionTools(t *testing.T) {
|
||
codexSignedIn(t)
|
||
got := protectedSearch(t)
|
||
code, body := post(t, "/v1/responses", `{"model":"codex/gpt-6.1-sol","stream":true,"tools":[{"type":"function","name":"shell","parameters":{"type":"object","properties":{}}}],"tool_choice":"auto","input":[`+searchedHistory+`]}`)
|
||
if code != 200 || strings.Contains(body, "response protection") {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
tools, _, choice, _ := sentTools(t, (*got)[len(*got)-1])
|
||
if strings.Join(tools, ",") != "function,web_search" || choice != "auto" {
|
||
t.Errorf("sent tools %v, tool_choice %v", tools, choice)
|
||
}
|
||
}
|
||
|
||
// Codex's own model on Codex's own sign-in, relayed as it came: a
|
||
// thread's description asked with the searched conversation and no tools
|
||
// (congee949's thread_description failures), and a Responses Lite
|
||
// compaction, whose tools go as an additional_tools item.
|
||
func TestSearchedConversationOnCodexSignIn(t *testing.T) {
|
||
setup(t, provider.Chat, &fake{t: t})
|
||
got := protectedSearch(t)
|
||
code, body := codexPost(t, `{"model":"gpt-6-luna","stream":true,"store":false,"tools":[],"tool_choice":"auto","input":[`+searchedHistory+`,{"type":"message","role":"user","content":[{"type":"input_text","text":"Describe this thread."}]}]}`)
|
||
if code != 200 || strings.Contains(body, "response protection") {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
if tools, _, choice, _ := sentTools(t, (*got)[0]); len(tools) != 1 || tools[0] != "web_search" || choice != "none" {
|
||
t.Errorf("sent tools %v, tool_choice %v", tools, choice)
|
||
}
|
||
|
||
rec := httptest.NewRecorder()
|
||
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-6.1-sol","stream":true,"store":false,"parallel_tool_calls":false,"reasoning":{"effort":"medium","context":"all_turns"},"input":[`+searchedHistory+`,{"type":"compaction_trigger"}]}`))
|
||
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
||
req.Header.Set("chatgpt-account-id", "acct-1")
|
||
req.Header.Set("X-OpenAI-Internal-Codex-Responses-Lite", "true")
|
||
New().Handler().ServeHTTP(rec, req)
|
||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"type":"compaction"`) {
|
||
t.Fatalf("Lite: status %d: %s", rec.Code, rec.Body.String())
|
||
}
|
||
tools, additional, choice, last := sentTools(t, (*got)[1])
|
||
if len(tools) != 0 || strings.Join(additional, ",") != "web_search" || choice != "none" || last != "compaction_trigger" {
|
||
t.Errorf("Lite sent tools %v, additional %v, tool_choice %v, last item %s", tools, additional, choice, last)
|
||
}
|
||
}
|
||
|
||
// Nothing changes for a conversation that didn't search, or one whose
|
||
// request declares search already.
|
||
func TestUnsearchedConversationUnchanged(t *testing.T) {
|
||
setup(t, provider.Chat, &fake{t: t})
|
||
got := protectedSearch(t)
|
||
for _, in := range []string{
|
||
`{"model":"gpt-6-luna","stream":true,"tools":[],"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}]}`,
|
||
`{"model":"gpt-6-luna","stream":true,"tools":[{"type":"web_search","external_web_access":true}],"tool_choice":"auto","input":[` + searchedHistory + `]}`,
|
||
} {
|
||
if code, body := codexPost(t, in); code != 200 {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
if sent := (*got)[len(*got)-1]; string(sent) != in {
|
||
t.Errorf("relayed changed:\n%s\n%s", in, sent)
|
||
}
|
||
}
|
||
}
|