Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): protect Codex exec reads whose argument is a JavaScript object literal (#3737)
gglucassecb5e5afea15
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex bdd29b82f..fac68afd2 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -2195,19 +2195,22 @@ class OpenAIHandlerMixin:command = _tool_call_command_text(item.get("action"))elif item_type == "custom_tool_call":# One script can run several commands; its single output is a- # read when any of them is (over-protecting only costs savings).+ # read when any of them is, or when one's cmd is not a string+ # literal and so might be (over-protecting only costs savings).+ commands = _custom_tool_call_commands(item.get("input"))command = next(- (- c- for c in _custom_tool_call_commands(item.get("input"))- if _is_read_command(c)- ),- "",+ (c for c in commands if c is not None and _is_read_command(c)),+ "exec_command(<cmd not a string literal>)" if None in commands else "",)else:continuecall_id = item.get("call_id")- if command and isinstance(call_id, str) and call_id and _is_read_command(command):+ if (+ command+ and isinstance(call_id, str)+ and call_id+ and (item_type == "custom_tool_call" or _is_read_command(command))+ ):read_command_by_call_id[call_id] = command# Outputs protected by read-command detection. Also unioned into the# cross-turn dedup protection set below: a [↑…] fold of a read woulddiff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex 395035623..5be6ff74f 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -572,8 +572,36 @@ def _tool_call_command_text(raw: Any) -> str:_EXEC_COMMAND_CALL_RE = re.compile(r"\bexec_command\s*\(")+# The `cmd` property of a JavaScript object literal, as Codex code-mode usually+# writes it: `{cmd: "sed -n '1,80p' f.py", workdir: "/repo"}` or+# `{cmd:"cat f","workdir":"/repo"}`. Matched from the opening brace; the key may+# be bare or quoted and the value a "...", '...' or `...` string that is the+# WHOLE property value (the next token ends it: `,` or `}`). Anything else -+# `"c" + "at f"`, `"cat f".trim()`, a template literal with `${...}` - is an+# expression whose value is not the literal and does not match.+_JS_CMD_PROPERTY_RE = re.compile(+ r"""\{[^{}]*?(?<![\w$])(?:cmd|"cmd"|'cmd')\s*:\s*"""+ r"""(?:"((?:[^"\\\n]|\\.)*)"|'((?:[^'\\\n]|\\.)*)'|`((?:[^`\\$]|\\.|\$(?!\{))*)`)"""+ r"""(?=\s*[,}])"""+)+_JS_STRING_ESCAPES = {"n": "\n", "t": "\t", "r": "\r", "b": "\b", "f": "\f", "v": "\v"}+++def _js_string_value(body: str) -> str | None:+ """Unescape the body of a JavaScript string literal.++ Returns None for an escape it does not decode (``\\x61``, ``\\u0061``, octal),+ since guessing would misread ``c\\x61t f`` as something other than ``cat f``.+ """+ escaped = re.findall(r"\\(.)", body, flags=re.S)+ if any(c.isalnum() and c not in _JS_STRING_ESCAPES for c in escaped):+ return None+ return re.sub(+ r"\\(.)", lambda m: _JS_STRING_ESCAPES.get(m.group(1), m.group(1)), body, flags=re.S+ )+-def _custom_tool_call_commands(raw: Any) -> list[str]:+def _custom_tool_call_commands(raw: Any) -> list[str | None]:"""Extract shell commands from a Codex code-mode ``exec`` custom tool call.Codex sends shell commands as a Responses ``custom_tool_call`` named ``exec``@@ -582,21 +610,34 @@ def _custom_tool_call_commands(raw: Any) -> list[str]:const r = await tools.exec_command({"cmd": "sed -n '1,80p' f.py", "workdir": "…"});text(r.output);+ The argument is usually a JavaScript object literal rather than JSON+ (``{cmd: "cat f.py"}``, unquoted key); when strict JSON decoding fails, the+ ``cmd`` property is read from the literal instead.+Returns every ``cmd`` passed to ``exec_command``, in order. Returns ``[]`` when- the input is not that shape; an argument object that is not strict JSON is- skipped, which leaves the output compressible exactly as before.+ the input is not that shape. A call whose ``cmd`` is not statically known (a+ variable, a concatenation, a template with substitution, an escape this does+ not decode) yields ``None``: the command may be a read, and callers must treat+ it as one rather than let an incomplete parse release the output."""if not isinstance(raw, str) or "exec_command" not in raw:return []decoder = json.JSONDecoder()- commands: list[str] = []+ commands: list[str | None] = []for match in _EXEC_COMMAND_CALL_RE.finditer(raw):start = raw.find("{", match.end())if start < 0 or raw[match.end() : start].strip():+ commands.append(None) # argument is not an object literal: dynamiccontinuetry:args, _end = decoder.raw_decode(raw, start)except ValueError:+ literal = _JS_CMD_PROPERTY_RE.match(raw, start)+ if literal is None:+ commands.append(None)+ continue+ body = next(group for group in literal.groups() if group is not None)+ commands.append(_js_string_value(body) or None)continuecommand = _tool_call_command_text(args)if command:diff --git a/tests/test_openai_responses_read_protection.py b/tests/test_openai_responses_read_protection.pyindex 39ce2dd15..82df1094d 100644--- a/tests/test_openai_responses_read_protection.py+++ b/tests/test_openai_responses_read_protection.py@@ -577,8 +577,91 @@ def test_custom_tool_call_commands_ignores_other_shapes():assert _custom_tool_call_commands(None) == []assert _custom_tool_call_commands({"cmd": "cat f"}) == []assert _custom_tool_call_commands("*** Begin Patch\n*** Update File: f.py\n") == []- assert _custom_tool_call_commands("tools.exec_command(notJson)") == []- assert _custom_tool_call_commands("tools.exec_command({cmd: 'cat f'})") == []+++def test_custom_tool_call_commands_marks_non_literal_cmd_unknown():+ """A cmd that is not a whole string literal may still be a read: None, not skipped."""+ from headroom.transforms.content_router import _custom_tool_call_commands++ for script in (+ "tools.exec_command(notJson)",+ "tools.exec_command({cmd: someVariable})",+ "tools.exec_command({cmd: `cat ${f}`})",+ 'tools.exec_command({cmd:"c"+"at f"})',+ "tools.exec_command({cmd: 'c' + 'at f', workdir: '/repo'})",+ 'tools.exec_command({cmd: "cat f".trim()})',+ 'tools.exec_command({cmd: "c\\x61t f"})',+ 'tools.exec_command({cmd: "c\\u0061t f"})',+ ):+ assert _custom_tool_call_commands(script) == [None], script+ # The known commands around an unknown one are still returned, in order.+ assert _custom_tool_call_commands(+ 'tools.exec_command({cmd: "ls"}); tools.exec_command({cmd: "c" + "at f"});'+ ) == ["ls", None]+++def test_custom_tool_call_commands_parses_javascript_object_literals():+ """Codex usually writes the argument as a JS literal, not JSON (bare `cmd` key)."""+ from headroom.transforms.content_router import _custom_tool_call_commands++ script = (+ 'const a = await tools.exec_command({cmd:"cat /tmp/app.js","workdir":"/tmp"});\n'+ "const b = await tools.exec_command({cmd: 'sed -n \\'1,80p\\' f.py', workdir: '/repo'});\n"+ "const c = await tools.exec_command({ workdir: '/repo', cmd: `nl -ba f.py` });\n"+ 'const d = await tools.exec_command({cmd: "rg -n \\"def f\\" src", yield_time_ms: 1000});\n'+ "const e = await tools.exec_command({'cmd': 'head -n 5 f.py'});\n"+ )+ assert _custom_tool_call_commands(script) == [+ "cat /tmp/app.js",+ "sed -n '1,80p' f.py",+ "nl -ba f.py",+ 'rg -n "def f" src',+ "head -n 5 f.py",+ ]+++def test_responses_codex_exec_javascript_literal_read_stays_verbatim(monkeypatch):+ """The same read with Codex's usual bare-key argument must also stay verbatim."""+ monkeypatch.setenv("HEADROOM_PROTECT_READS", "1")+ handler = _handler_with_router(_lossy_router())+ output = _codex_exec_output("call_exec", _NL_OUTPUT)+ call = {+ "type": "custom_tool_call",+ "call_id": "call_exec",+ "name": "exec",+ "input": (+ 'const r = await tools.exec_command({cmd: "nl -ba tenacity/wait.py | '+ 'sed -n \'20,115p\'", workdir: "/repo", yield_time_ms: 10000});\n'+ "text(r.output);\n"+ ),+ }+ payload = {"model": "gpt-5", "input": [call, output]}++ new_payload, _modified, _s, _t, _u, _c, _a = _run(handler, payload)++ assert new_payload["input"][1] == output+++def test_responses_codex_exec_concatenated_cmd_stays_verbatim(monkeypatch):+ """`"c" + "at f"` runs `cat f`: an unparsed cmd must not release the read."""+ monkeypatch.setenv("HEADROOM_PROTECT_READS", "1")+ handler = _handler_with_router(_lossy_router())+ output = _codex_exec_output("call_exec", _NL_OUTPUT)+ call = {+ "type": "custom_tool_call",+ "call_id": "call_exec",+ "name": "exec",+ "input": (+ 'const r = await tools.exec_command({cmd: "c" + "at tenacity/wait.py", '+ 'workdir: "/repo"});\n'+ "text(r.output);\n"+ ),+ }+ payload = {"model": "gpt-5", "input": [call, output]}++ new_payload, _modified, _s, _t, _u, _c, _a = _run(handler, payload)++ assert new_payload["input"][1] == outputdef test_responses_codex_exec_read_stays_verbatim(monkeypatch):