diff --git a/backend/internal/handler/openai_page_context.go b/backend/internal/handler/openai_page_context.go index 784a93b97e..3aa1556943 100644 --- a/backend/internal/handler/openai_page_context.go +++ b/backend/internal/handler/openai_page_context.go @@ -13,6 +13,10 @@ import ( var codexSelectedPageName = regexp.MustCompile(`^codex_apps_selected_page_part_([1-9][0-9]*)_of_([1-9][0-9]*)$`) +// codexContextTagName is the shape of the context names Codex derives from +// its context keys; only such names are safe to embed in a tag. +var codexContextTagName = regexp.MustCompile(`^[A-Za-z0-9_]{1,128}$`) + // mayContainCodexAgentContext is the cheap gate for agent context outputs. // Names are not enumerated: Codex keeps adding context tools, and any agent // namespace output without a call_id is injected context, not a tool result. @@ -101,12 +105,18 @@ func normalizeCodexUntrustedContext(body []byte) ([]byte, bool) { // codexUntrustedContextText renders an agent context output as user text. // Known context names keep their existing tags and format checks. Any other -// name is still context when it comes from the agent namespace, so it uses a -// generic tag instead of failing the request; the name is client supplied and -// must not be interpolated into the tag unchecked. +// name is still context when it comes from the agent namespace, so it is +// rendered the way Codex renders context when it does not send it as a tool +// output: . The name is client supplied, so it is only used +// as a tag when it has the shape Codex derives names in; anything else falls +// back to a generic tag. func codexUntrustedContextText(name, output string) (string, bool) { if !knownCodexUntrustedContextName(name) { - return "" + output + "", true + tag := "agent_context" + if codexContextTagName.MatchString(name) { + tag = name + } + return "" + output + "", true } if !validCodexUntrustedContext(name, output) { return "", false @@ -120,7 +130,7 @@ func codexUntrustedContextText(name, output string) (string, bool) { func knownCodexUntrustedContextName(name string) bool { switch name { - case "codex_apps_open_page", "browser_info", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan": + case "codex_apps_open_page", "browser_info", "browser_tabs", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan": return true } return codexSelectedPageName.MatchString(name) diff --git a/backend/internal/handler/openai_page_context_test.go b/backend/internal/handler/openai_page_context_test.go index af5fbcd870..c5372f1e1e 100644 --- a/backend/internal/handler/openai_page_context_test.go +++ b/backend/internal/handler/openai_page_context_test.go @@ -3,6 +3,7 @@ package handler import ( "encoding/json" "net/http/httptest" + "strings" "testing" "github.com/Wei-Shaw/sub2api/internal/service" @@ -140,11 +141,15 @@ func TestNormalizeCodexTerminalInfoPreservesUnrelatedResults(t *testing.T) { func TestNormalizeCodexAgentContextUnknownNames(t *testing.T) { const browserTabs = `{"tabs":[{"title":"Example","url":"https://example.com"}]}` + longName := strings.Repeat("a", 129) body := pageContextRequest(t, map[string]any{"type": "function_call_output", "namespace": "agent", "name": "browser_tabs", "output": browserTabs}, map[string]any{"type": "function_call_output", "namespace": "agent", "name": "some_new_context", "output": "new context"}, map[string]any{"type": "function_call_output", "namespace": "agent", "name": "terminal_info_extra", "output": "suffix name"}, map[string]any{"type": "function_call_output", "namespace": "agent", "name": "Terminal_info", "output": "case name"}, + map[string]any{"type": "function_call_output", "namespace": "agent", "name": "bad>name"+output+"", item.Get("content.0.text").String()) + require.Equal(t, ""+want.output+"", item.Get("content.0.text").String()) require.False(t, item.Get("call_id").Exists()) require.False(t, item.Get("id").Exists()) } - require.Equal(t, "hello", gjson.GetBytes(got, "input.4.content").String()) + require.Equal(t, "hello", gjson.GetBytes(got, "input.7.content").String()) again, changed := normalizeCodexUntrustedContext(got) require.False(t, changed) require.Equal(t, got, again) @@ -181,7 +194,7 @@ func TestNormalizeCodexAgentContextEscapedAndBlankCallID(t *testing.T) { } { got, changed := normalizeCodexUntrustedContext([]byte(body)) require.True(t, changed) - require.Equal(t, "tabs", gjson.GetBytes(got, "input.0.content.0.text").String()) + require.Equal(t, "tabs", gjson.GetBytes(got, "input.0.content.0.text").String()) } } @@ -488,8 +501,6 @@ func TestNormalizeCodexBrowserTabsBoundaries(t *testing.T) { }{ {"real call id", "call_id", "call_real"}, {"other namespace", "namespace", "browser"}, - {"name prefix", "name", "browser_tabs_extra"}, - {"name case", "name", "Browser_tabs"}, {"empty output", "output", ""}, {"blank output", "output", " \n\t"}, {"object output", "output", map[string]any{"tabs": 2}}, @@ -504,3 +515,14 @@ func TestNormalizeCodexBrowserTabsBoundaries(t *testing.T) { }) } } + +func TestNormalizeCodexBrowserTabsNameVariantsUseOwnTag(t *testing.T) { + for _, name := range []string{"browser_tabs_extra", "Browser_tabs"} { + t.Run(name, func(t *testing.T) { + body := pageContextRequest(t, pageContextItem(name, "Tab 1: https://example.com/")) + got, changed := normalizeCodexUntrustedContext(body) + require.True(t, changed) + require.Equal(t, "Tab 1: https://example.com/", gjson.GetBytes(got, "input.0.content.0.text").String()) + }) + } +}