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): preserve terminal tool on Codex Responses (#2000)
Rod Boev41af39d769ef
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 846414842..dcfdd1538 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug Fixes+* **proxy/openai:** keep the exact Responses function name `terminal` resident during OpenAI tool-search deferral so cache-mode optimization stops forwarding `terminal.terminal` and triggering the reserved-namespace 400 on Codex Responses ([#1946](https://github.com/headroomlabs-ai/headroom/issues/1946)).* **proxy/openai:** thread the savings-profile kwargs into the live `/v1/chat/completions` compression path. The chat handler called `openai_pipeline.apply()` without `proxy_pipeline_kwargs(config)`, so `HEADROOM_SAVINGS_PROFILE=agent-90` (and the individual `compress_user_messages`/`target_ratio`/`min_tokens_to_compress`/... knobs) were silently dropped — OpenAI-compatible clients like OpenCode kept protecting user messages and missed the configured profile. Both the token-mode and non-token chat branches now pass the profile kwargs, matching `handlers/anthropic.py` and the dedicated OpenAI compress endpoint ([#1534](https://github.com/headroomlabs-ai/headroom/issues/1534)).* **proxy:** forward Codex Desktop `/v1/responses` posts byte-faithfully so they stop returning upstream `400 {"detail":"Bad Request"}`. `handle_openai_responses` decoded the inbound body to inspect it but always re-serialized a canonical body on the way out, and it never stripped the inbound `content-encoding` header — so a `content-encoding: zstd` Codex Desktop request was forwarded as already-decoded JSON still advertising `zstd`, and the upstream ChatGPT Codex endpoint rejected it. The handler now keeps the original decoded bytes and forwards them verbatim whenever nothing (compression or memory injection) mutated the request, and drops the stale `content-encoding` header, mirroring the byte-faithful passthrough the chat and Anthropic paths already use ([#1542](https://github.com/headroomlabs-ai/headroom/issues/1542)).* **wrap/codex:** `headroom unwrap codex` now removes the Headroom rtk instruction block from the Codex global `AGENTS.md`. `wrap codex` injects it there, but unwrap only restored `config.toml` and MCP state, so a plain `codex` launch kept following the "prefix shell commands with `rtk`" guidance and failed once the managed rtk binary was off PATH. Unwrap now strips the marker-fenced block (preserving the rest of the file), mirroring `unwrap copilot` ([#1421](https://github.com/headroomlabs-ai/headroom/issues/1421)).diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.pyindex b6313424a..cd62c22f9 100644--- a/headroom/proxy/helpers.py+++ b/headroom/proxy/helpers.py@@ -3167,6 +3167,7 @@ def inject_tool_search_deferral(_OPENAI_TOOL_SEARCH_TYPE = "tool_search"_OPENAI_TOOL_SEARCH_MIN_TOOLS = 12+_OPENAI_TOOL_SEARCH_RESIDENT_NAMES = frozenset({"terminal"})# gpt-5.4 is the first model with Responses tool_search (OpenAI docs). Version-# gated by default; overridable per deployment via a regex in# HEADROOM_OPENAI_TOOL_SEARCH_MODELS (matched against the model name) so new@@ -3233,7 +3234,11 @@ def inject_tool_search_deferral_openai(# Deferrable: a non-core function, or an MCP server (OpenAI models are# trained to search namespaces / MCP servers). Everything else — core# coding tools and other hosted tools — stays resident.- deferrable = (ttype == "function" and tool.get("name") not in core_tools) or ttype == "mcp"+ deferrable = (+ ttype == "function"+ and tool.get("name") not in core_tools+ and tool.get("name") not in _OPENAI_TOOL_SEARCH_RESIDENT_NAMES+ ) or ttype == "mcp"if deferrable and not tool.get("defer_loading"):new_tool = dict(tool)new_tool["defer_loading"] = Truediff --git a/tests/test_openai_tool_search_deferral.py b/tests/test_openai_tool_search_deferral.pyindex f73d33f1e..afde1adac 100644--- a/tests/test_openai_tool_search_deferral.py+++ b/tests/test_openai_tool_search_deferral.py@@ -70,6 +70,29 @@ def test_defers_non_core_and_injects_search_tool():assert by_name[n].get("defer_loading") is True # non-core deferred+def test_terminal_reserved_namespace_stays_resident():+ terminal = _fn("terminal")+ tools = [terminal] + [_fn(f"peer_{i}") for i in range(11)]+ snapshot = copy.deepcopy(tools)++ out = inject_tool_search_deferral_openai(tools, "gpt-5.6-terra")++ forwarded = next(t for t in out if t.get("name") == "terminal")+ assert forwarded == terminal+ assert "defer_loading" not in forwarded+ assert next(t for t in out if t.get("name") == "peer_0").get("defer_loading") is True+ assert tools == snapshot+++def test_terminal_helper_remains_deferrable():+ tools = [_fn("terminal_helper")] + [_fn(f"peer_{i}") for i in range(11)]++ out = inject_tool_search_deferral_openai(tools, "gpt-5.6-terra")++ helper = next(t for t in out if t.get("name") == "terminal_helper")+ assert helper.get("defer_loading") is True++def test_defers_mcp_server():tools = [_fn(n) for n in _CORE] + [{"type": "mcp", "server_label": "sentry"}]tools += [_fn(f"x{i}") for i in range(8)]