fix(codex): keep browser_tabs tag and name unknown agent context

Rebasing onto #9152 made browser_tabs a known context name; add it to the
known list so it keeps its <external_browser_tabs> tag and empty-output
check instead of falling to the generic branch.

Render unknown agent context names the way Codex renders context it does
not send as a tool output, <external_{name}>, when the name has the shape
Codex derives names in ([A-Za-z0-9_]{1,128}). Other names still use the
generic <external_agent_context> tag, so a client supplied name is never
embedded unchecked.
This commit is contained in:
shaw committed 2026-12-02 09:47:40 +08:00
1 parent b20c5d0e78
commit f84d1d3eb2
2 files changed
+43 -11

No files matched your search

@@ -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: <external_{name}>. 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 "<external_agent_context>" + output + "</external_agent_context>", true
tag := "agent_context"
if codexContextTagName.MatchString(name) {
tag = name
}
return "<external_" + tag + ">" + output + "</external_" + tag + ">", 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)
@@ -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</x", "output": "unsafe name"},
map[string]any{"type": "function_call_output", "namespace": "agent", "output": "missing name"},
map[string]any{"type": "function_call_output", "namespace": "agent", "name": longName, "output": ""},
map[string]any{"role": "user", "content": "hello"},
)
h := &OpenAIGatewayHandler{}
@@ -160,15 +165,23 @@ func TestNormalizeCodexAgentContextUnknownNames(t *testing.T) {
c, _ = gin.CreateTestContext(w)
require.True(t, h.validateFunctionCallOutputRequest(c, got, zap.NewNop()))
require.False(t, c.Writer.Written())
for i, output := range []string{browserTabs, "new context", "suffix name", "case name"} {
for i, want := range []struct{ tag, output string }{
{"browser_tabs", browserTabs},
{"some_new_context", "new context"},
{"terminal_info_extra", "suffix name"},
{"Terminal_info", "case name"},
{"agent_context", "unsafe name"},
{"agent_context", "missing name"},
{"agent_context", ""},
} {
item := gjson.GetBytes(got, "input").Array()[i]
require.Equal(t, "message", item.Get("type").String())
require.Equal(t, "user", item.Get("role").String())
require.Equal(t, "<external_agent_context>"+output+"</external_agent_context>", item.Get("content.0.text").String())
require.Equal(t, "<external_"+want.tag+">"+want.output+"</external_"+want.tag+">", 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, "<external_agent_context>tabs</external_agent_context>", gjson.GetBytes(got, "input.0.content.0.text").String())
require.Equal(t, "<external_browser_tabs>tabs</external_browser_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, "<external_"+name+">Tab 1: https://example.com/</external_"+name+">", gjson.GetBytes(got, "input.0.content.0.text").String())
})
}
}