fix(proxy): resolve Codex custom_tool_call names for Responses tool exclusion (#3755)

## Description

`HEADROOM_EXCLUDE_TOOLS` does not protect Codex's shell tool, unlike
function tools such as pi's `bash` (fixed in #940).

Codex sends shell as a freeform tool: `custom_tool_call` named `exec`,
plus `custom_tool_call_output`.
The Responses adapter builds `function_name_by_call_id` from
`function_call` items only.
Custom tool outputs therefore have no name, so the exclude sets never
match them.

### Why it matters

1. Exclude `exec` for exact shell output
2. Headroom still swaps in a CCR marker
3. Codex gets `<<ccr:…,string,34.4KB>>`

Separate from #3621 and #3737, which protect `exec` reads by command,
not by tool name; #3737 still replaces the demo's excluded `cat` output.

We hit this running hosted Codex traffic through headroom since Sep 9,
on top of `aebe989`.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- **Fix:** name map also reads `custom_tool_call`
- **Test:** excluded custom tool output stays raw
- **Test:** same, beside a compressed function call

## Testing

- [x] Unit tests pass (`pytest`, touched areas)
- [x] Linting passes (`ruff check`, `ruff format --check` on touched
files)
- [x] `make ci-precheck` passes
- [x] Type checking passes (`mypy headroom --ignore-missing-imports`,
mypy 1.20.2): no new errors; `main` already fails at
`langchain/chat_model.py:394` locally
- [x] New tests added for new functionality

### Test Output

```text
main 7f2766ca, new tests:
E   assert True is False
E   AssertionError: assert 'compressed tool output' == 'line0 line1 ...ne178 line179'

branch fdc160af:
tests/test_openai_responses_compression_units.py tests/test_openai_responses_read_protection.py
tests/test_openai_responses_context_compaction.py tests/test_openai_responses_t3_replay_regression.py
65 passed in 1.95s

make ci-precheck: 176 passed, 4 skipped
```

## Real Behavior Proof

- Environment: Linux, Python 3.12.3, Codex 0.156.1, pi 0.87.1,
`gpt-6-sol` low
- Exact command / steps: see below
- Observed result: see table
- Not tested: `apply_patch`, MCP custom tools (same path)

### Steps

1. `build.json`: 260 records, 5 `ERROR`
2. Route: Codex and pi through one headroom
3. Headroom forwards to our upstream proxy
4. Proxy: `HEADROOM_EXCLUDE_TOOLS=exec,bash`
5. Proxy: `--codex-wire-debug` on
6. Same prompt to both agents (below)
7. Diff inbound vs upstream tool output

```text
run cat build.json first, then tell me how many ERROR entries there are and which modules failed
```

### Result

| | `main` 7f2766ca | branch fdc160af |
| --- | --- | --- |
| Codex `exec` `cat` upstream | `<<ccr:08e14c199475,string,34.4KB>>` |
raw, byte-identical |
| pi `bash` `cat` upstream | raw, byte-identical | raw, byte-identical |
| Codex outputs replaced | 1 of 5 | 0 of 3 |
| pi outputs replaced | 0 of 1 | 0 of 1 |
| Codex transforms | `custom_tool_call_output:mixed` |
`tool_schema_compaction` only |

### Recordings

**main: excluded `exec` compressed, excluded `bash` raw**


https://github.com/user-attachments/assets/a1119afc-ad38-4065-8312-c44bfb399972

**branch: both excluded outputs raw**


https://github.com/user-attachments/assets/bb6cb831-0b86-4e1a-9ec9-1afef881ac6e

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: Stable.
- Stable/default behavior changed: Excluded custom tools now get the
same exclusion as function tools.
- Kill switch / disable path: Unset `HEADROOM_EXCLUDE_TOOLS`.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

---------

Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
donjor
2026-09-24 17:13:17 -05:00
committed by GitHub
co-authored by JD Davis
parent a37d734352
commit 55c78deaa8
2 changed files with 110 additions and 3 deletions
+4 -3
View File
@@ -2103,13 +2103,14 @@ class OpenAIHandlerMixin:
# excluded tools (HEADROOM_EXCLUDE_TOOLS) can be protected from
# compression. The chat/Anthropic paths get this via
# ContentRouter._build_tool_name_map; the Responses payload carries the
# name on the `function_call` item and the originating call_id on the
# matching `function_call_output`, so we correlate them here.
# name on the `function_call` (or Codex `custom_tool_call`) item and
# the originating call_id on the matching output, so we correlate
# them here.
function_name_by_call_id: dict[str, str] = {}
for item in items:
if not isinstance(item, dict):
continue
if item.get("type") != "function_call":
if item.get("type") not in ("function_call", "custom_tool_call"):
continue
name = item.get("name")
call_id = item.get("call_id")
@@ -1194,3 +1194,109 @@ def test_openai_responses_adapter_floors_when_aggregate_below_threshold():
assert saved == 0
assert units_by_category == {"size_floor": len(outputs)}
assert new_payload == payload
def test_openai_responses_adapter_preserves_excluded_custom_tool_outputs():
"""Codex freeform tools pair ``custom_tool_call`` with its output by call_id.
The exclude check must resolve their names exactly like ``function_call``.
"""
router = ContentRouter()
router.config.exclude_tools = {"serena.find_symbol", "find_symbol"}
def compress(self, content: str, **_kwargs):
return RouterCompressionResult(
compressed="should not be used",
original=content,
strategy_used=CompressionStrategy.KOMPRESS,
)
router.compress = MethodType(compress, router)
handler = _handler_with_router(router)
output = " ".join(f"sym{i}" for i in range(180))
payload = {
"model": "gpt-5",
"input": [
{
"type": "custom_tool_call",
"call_id": "call_1",
"name": "serena.find_symbol",
"input": "find_symbol main",
},
{
"type": "custom_tool_call_output",
"call_id": "call_1",
"output": output,
},
],
}
new_payload, modified, saved, transforms, _units, _chain, _attempted = (
handler._compress_openai_responses_live_text_units_with_router(
payload,
model="gpt-5",
request_id="req_test",
)
)
assert modified is False
assert saved == 0
assert transforms == []
assert new_payload == payload
def test_openai_responses_adapter_excludes_custom_tool_beside_compressed_function_call():
"""An excluded custom tool stays raw while a non-excluded function call compresses."""
router = ContentRouter()
router.config.exclude_tools = {"exec"}
def compress(self, content: str, **_kwargs):
return RouterCompressionResult(
compressed="compressed tool output",
original=content,
strategy_used=CompressionStrategy.KOMPRESS,
)
router.compress = MethodType(compress, router)
handler = _handler_with_router(router)
excluded_output = " ".join(f"line{i}" for i in range(180))
other_output = " ".join(f"word{i}" for i in range(180))
payload = {
"model": "gpt-5",
"input": [
{
"type": "custom_tool_call",
"call_id": "call_exec",
"name": "exec",
"input": "cat build.log",
},
{
"type": "custom_tool_call_output",
"call_id": "call_exec",
"output": excluded_output,
},
{
"type": "function_call",
"call_id": "call_other",
"name": "some.other_tool",
"arguments": "{}",
},
{
"type": "function_call_output",
"call_id": "call_other",
"output": other_output,
},
],
}
new_payload, modified, _saved, _transforms, _units, _chain, _attempted = (
handler._compress_openai_responses_live_text_units_with_router(
payload,
model="gpt-5",
request_id="req_test",
)
)
assert modified is True
assert new_payload["input"][1]["output"] == excluded_output
assert new_payload["input"][3]["output"] == "compressed tool output"