Skip to content

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.

fix(transforms): judge a Codex exec envelope read by its output (#3878)

gglucass922924ef44eb
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.py
index d672688dc..d2452c235 100644
--- a/headroom/transforms/content_router.py
+++ b/headroom/transforms/content_router.py
@@ -1157,6 +1157,41 @@ _RELEASABLE_READ_TYPES = frozenset(
)
+def _codex_exec_envelope_outputs(text: str) -> list[str] | None:
+ """The file text inside a Codex ``exec`` result printed whole, else ``None``.
+
+ Codex code mode runs ``exec`` as JavaScript. A script that prints the whole
+ ``exec_command`` result, ``text(r)`` rather than ``text(r.output)``, sends
+ the JSON envelope ``{"chunk_id", "wall_time_seconds", "exit_code", ...,
+ "output": "<file>"}`` (or a list of them), possibly behind Codex's
+ ``Script completed / Wall time / Output:`` preamble. Judging the envelope
+ would judge a line of JSON-escaped code, whose verdict flips per file
+ between source and releasable JSON; the read is its ``output`` string(s).
+ Only a body that is nothing but envelope(s) is unwrapped.
+ """
+ if '"wall_time_seconds"' not in text:
+ return None
+ body = text.strip()
+ if not body.startswith(("{", "[")):
+ _, found, body = text.partition("\nOutput:\n")
+ body = body.strip()
+ if not found or not body.startswith(("{", "[")):
+ return None
+ try:
+ parsed = json.loads(body)
+ except (ValueError, RecursionError):
+ return None
+ envelopes = parsed if isinstance(parsed, list) else [parsed]
+ outputs = [
+ env["output"]
+ for env in envelopes
+ if isinstance(env, dict)
+ and "wall_time_seconds" in env
+ and isinstance(env.get("output"), str)
+ ]
+ return outputs if outputs and len(outputs) == len(envelopes) else None
+
+
def _read_output_should_be_protected(text: Any) -> bool:
"""Finalize read-protection by CONTENT — protect by default, release only DATA.
@@ -1172,6 +1207,9 @@ def _read_output_should_be_protected(text: Any) -> bool:
"""
if not isinstance(text, str) or not text:
return False
+ outputs = _codex_exec_envelope_outputs(text)
+ if outputs is not None:
+ return any(_read_output_should_be_protected(output) for output in outputs)
try:
detection = _detect_content(text)
except Exception:
diff --git a/tests/test_openai_responses_read_protection.py b/tests/test_openai_responses_read_protection.py
index 82df1094d..b02ac8c88 100644
--- a/tests/test_openai_responses_read_protection.py
+++ b/tests/test_openai_responses_read_protection.py
@@ -17,6 +17,7 @@ exactly the bytes the model needs for line-precise edits, forcing re-reads
from __future__ import annotations
+import json
from types import MethodType, SimpleNamespace
from headroom.proxy.handlers.openai import OpenAIHandlerMixin
@@ -718,3 +719,61 @@ def test_responses_codex_exec_test_output_still_compresses(monkeypatch):
assert modified is True
assert new_payload["input"][1] != _codex_exec_output("call_pytest", _NL_OUTPUT)
+
+
+# 40 line-numbered lines of Rust: the detector calls it code on its own, but a
+# JSON envelope around it reads as a releasable JSON document.
+_RUST_NL = "".join(
+ f"{i:6d}\tpub fn f{i}(x: Option<Vec<u8>>) -> usize {{ x.map_or(0, |v| v.len()) }}\n"
+ for i in range(1, 41)
+)
+
+
+def _exec_envelope(output: str) -> str:
+ """What Codex prints for `text(r)`: the whole exec_command result."""
+ return json.dumps(
+ {
+ "chunk_id": "6c7260",
+ "wall_time_seconds": 0.0,
+ "exit_code": 0,
+ "original_token_count": 900,
+ "output": output,
+ }
+ )
+
+
+def test_read_gate_judges_codex_exec_envelope_by_its_output():
+ """Bare, listed or behind the `Output:` preamble: the verdict is the output's."""
+ from headroom.transforms.content_router import _read_output_should_be_protected as protect
+
+ data = json.dumps(
+ {
+ "name": "app",
+ "dependencies": [{"name": f"pkg{i}", "version": "1.0.0"} for i in range(30)],
+ },
+ indent=2,
+ )
+ assert protect(_RUST_NL) is True
+ assert protect(data) is False
+ for output in (_RUST_NL, data):
+ envelope = _exec_envelope(output)
+ expected = protect(output)
+ assert protect(envelope) is expected
+ assert protect(json.dumps([json.loads(envelope)] * 2)) is expected
+ assert (
+ protect("Script completed\nWall time 0.1 seconds\nOutput:\n\n" + envelope) is expected
+ )
+
+
+def test_responses_codex_exec_whole_result_read_stays_verbatim(monkeypatch):
+ """`text(r)` sends the exec envelope; the code read inside must stay verbatim."""
+ monkeypatch.setenv("HEADROOM_PROTECT_READS", "1")
+ handler = _handler_with_router(_lossy_router())
+ call = _codex_exec_call("call_exec", "nl -ba src/models.rs | sed -n '1,400p'")
+ call["input"] = call["input"].replace("text(r0.output)", "text(r0)")
+ output = _codex_exec_output("call_exec", _exec_envelope(_RUST_NL))
+ payload = {"model": "gpt-5", "input": [call, output]}
+
+ new_payload, _modified, _s, _t, _u, _c, _a = _run(handler, payload)
+
+ assert new_payload["input"][1] == output