Commit Graph
3 Commits
Author SHA1 Message Date
2f076687b0 fix(ccr): unwrap Hermes batched tool_call so headroom_retrieve stays exempt (#3839)
## Description

Fixes #3837. `headroom_retrieve` results issued through Hermes'
deferred-tool bridge were re-compressed into `<<ccr:…>>` markers, making
retrieved content unreachable.

**Root cause.** Hermes' `tool_call` bridge advertises a **batch** shape,
`{"calls": [{"name", "arguments"}, …]}`, and only tolerates the legacy
`{"name", "arguments"}` form (Hermes
`tools/tool_search_validation.py::normalize_tool_call_entries`).
`unwrap_tool_call_name` read only the legacy form, so every batched call
resolved to the wrapper name `tool_call`:

- The `ccr_retrieve` guard never fired. SmartCrusher re-compressed the
retrieved rows into a marker whose hash matches the original.
- `--protect-tool-results` and exclude lists silently no-op'd for
**every** deferred tool. Since late August, Hermes also defers its core
tools (terminal, file reads) behind the same bridge.

Reproduced end to end through a real proxy (token mode, `--target-ratio
0.7`) against a fake Anthropic upstream. A Hermes batch-shaped retrieve
of a compressed JSON output:

| | retrieve result forwarded upstream | PERF transforms |
|---|---|---|
| main |
`{"hash":…,"original_content":"<<ccr:3c73cd7e951b,string,14.4KB>>",…}` |
`router:tool_result:smart_crusher` |
| this PR | verbatim, on every later turn too |
`router:excluded:ccr_retrieve` |

The reporter's "0.38.0 works, 0.39.1 doesn't" is most likely a
Hermes-side change to the advertised batch shape, not a Headroom
regression. v0.38.0's unwrap is identical and fails the same way on the
batch shape.

## Type of Change

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

## Changes Made

- `headroom/config.py`: new `unwrap_tool_call(name, arguments) ->
(real_name, real_arguments)`, mirroring Hermes' normalizer:
- `calls` may be a list, a bare object or a JSON string; the legacy
shape is still handled.
- A batch resolves only when every entry names the same tool. Malformed
or mixed-name batches fail open to the wrapper name, as before.
  - `unwrap_tool_call_name` now delegates to it.
- `content_router._build_tool_name_map`: uses the unwrapped
**arguments** too, so read protection (`_tool_call_commands`) and the
relevance query see the real call, not the wrapper envelope.
- `handlers/openai.py` (Responses units path): `headroom_retrieve`
detection uses the shared `is_tool_excluded` matcher instead of `== /
endswith("__headroom_retrieve")`. That also covers Hermes' `mcp_srv_x`
and OpenCode's `headroom_headroom_retrieve`. The import is hoisted to
module level.

## Testing

- [x] Unit tests pass. 270 tests across `test_hermes_tool_call_unwrap`,
`test_openai_responses_*`, `test_content_router*`,
`test_tool_name_aliases` and the exclude-tools suites.
- [x] Linting passes: CI's `ruff 0.16.8` check plus `format --check`.
- [x] Type checking passes: `mypy` on the three changed modules.
- [x] New tests added. 8 of them fail on the unfixed code, including an
end-to-end `ContentRouter.apply()` case with a non-retrieve control. The
existing Responses retrieve-protection test is parametrized over the
alias and bridge forms.
- [x] Manual testing performed (proxy + fake upstream, table above).

## Follow-up (not in this PR)

A separate hole turned up while reproducing: in token mode the session
compression cache (`CompressionCache.apply_cached`, via
`session_engine.prepare_turn`) swaps a tool result's content by
**content hash** before the router runs, without looking at the tool
name. So a retrieve result whose bytes equal an earlier compressed
output gets the cached compressed form back, even though the router logs
`excluded:ccr_retrieve`. The MCP retrieve returns a JSON envelope, so
this doesn't trigger for it in practice today. It will for any client
that returns the raw original. It is present in 0.38.0 too. It will be
filed separately.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-09-29 10:59:39 -05:00
Tejas ChopraandClaude Opus 5 954a5ffbb0 test: unstale the example name in the wrapped-exclusion sanity check
Adding `read_file` to DEFAULT_EXCLUDE_TOOLS broke this test's SECOND
assertion, which used that name only as an example of a tool absent from the
defaults. The property under test -- that the Hermes wrapper name
`tool_call` does not match the exclude whitelist before unwrapping -- is the
first assertion and was never affected.

Swapped the example to `codebase_search`, which is still outside the
defaults, so the test goes on documenting the same failure mode. The sibling
`..._exclusion_after_unwrap` still adds `read_file` explicitly and is
unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PbXdkNvPqxmrPJxxyvwtZS
2026-08-31 14:19:08 +05:30
pgjhandpgjh a97b82413b fix(proxy): unwrap Hermes tool_call bridge in tool name map (#2717)
## Description

Hermes Agent (NousResearch/hermes-agent) loads on-demand ("deferred")
tools via a `tool_search` → `tool_describe` → `tool_call` indirection.
On the wire, the emitted tool call is named **`tool_call`**, and the
REAL tool name lives inside the arguments payload:

```json
{
  "id": "call_abc123",
  "type": "function",
  "function": {
    "name": "tool_call",
    "arguments": "{\"name\": \"read_file\", \"arguments\": {\"path\": \"/etc/hostname\"}}"
  }
}
```

`ContentRouter._build_tool_name_map` only reads
`tool_calls[].function.name`, so it maps `tool_call` → `"tool_call"`
instead of the real tool name.

**Consequence**: `HEADROOM_EXCLUDE_TOOLS` /
`HEADROOM_PROTECT_TOOL_RESULTS` silently no-op for **ALL** deferred
tools (`read_file`, `write_file`, `search_files`, `mcp__*`,
`headroom_retrieve`, etc.). Their outputs get lossy-compressed even when
explicitly whitelisted, and `headroom_retrieve` falls into an endless
re-compression loop (`<<ccr:hash>>` → retrieve → re-compress →
`original_tokens: 0`).

## Type of Change

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

## Changes Made

Add an `unwrap_tool_call_name(name, arguments)` helper in `config.py`
(beside the existing `_tool_name_aliases`) and apply it at the **3
sites** where the tool_call_id → tool_name map is built:

1. **OpenAI chat path** — `content_router.py` `_build_tool_name_map`
(`tool_calls[].function`)
2. **Anthropic path** — `content_router.py` `_build_tool_name_map`
(`tool_use` blocks)
3. **Responses API path** — `openai.py`
`_compress_openai_responses_live_text_units_with_router`
(`function_call` items)

The helper:
- Passes non-wrapper names through unchanged
- Parses the arguments payload (JSON string or dict) and extracts the
inner `name`
- Fails open (returns the wrapper name) on malformed/unparseable
payloads — safe for all other clients

After unwrap, `is_tool_excluded()`/protect-list matching sees the real
tool name, so whitelists work for deferred tools exactly as they already
did for classic tools.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] New tests added for new functionality
- [x] Manual testing performed

`tests/test_hermes_tool_call_unwrap.py` — **14 tests**, all pass:

- Helper unit tests: passthrough, None/bad-JSON/missing-name fail-open,
unwrap (web_search, read_file, mcp__*, dict-args form)
- Whitelist activation: unwrapped name + `is_tool_excluded` against
`DEFAULT_EXCLUDE_TOOLS`
- Integration: `_build_tool_name_map` with OpenAI-format `tool_call`
wrapper and Anthropic-format `tool_use` wrapper
- Regression guard: documents that `tool_call` itself is NOT in
`DEFAULT_EXCLUDE_TOOLS` (the pre-fix failure mode)

### Test Output

```text
$ uv run pytest tests/test_hermes_tool_call_unwrap.py tests/test_content_router_exclude_tools.py tests/test_config.py -q
56 passed in 1.24s

$ uv run ruff check .
All checks passed!

$ uv run ruff format --check .
All checks passed!
```

## Real Behavior Proof

- Environment: Ubuntu 24.04 (kernel 7.0.0-28), Python 3.11.15, headroom
built from source at `v0.33.0-5-g6d5516dc` via `uv sync` (maturin), Rust
1.95.0 toolchain. Headroom proxy 0.34.0-dev running as a systemd user
service, `HEADROOM_PROTECT_TOOL_RESULTS=read_file,headroom_retrieve`,
mode=token. Upstream: local new-api-compatible gateway (deepseek-v4-pro
/ GLM-5.2).
- Exact command / steps: Client = Hermes Agent (fresh session via
`hermes chat -q --provider newapi2`, traffic routed through the headroom
proxy at 127.0.0.1:8788). Trigger a deferred `read_file` tool call and
observe `/stats` → `recent_requests`.
- Observed result: After fix, live traffic evidence shows request
`hr_1785683282_000016` (GLM-5.2 session) with `transforms_applied:
["router:excluded:tool", "openai:chat:tool_schema_compaction"]` — the
deferred `read_file` tool was recognized and excluded (whitelist hit),
21918 → 17218 input tokens. Before the fix this request showed no
`router:excluded:tool` for deferred tools — they were compressed. E2E
unit-level proof (`test_build_tool_name_map_exclusion_after_unwrap` +
standalone pipeline run): a `tool_call`-wrapped `read_file` output
(~9KB, 100 lines) passed through `ContentRouter.apply()` **verbatim**
(byte-identical), while a non-whitelisted tool (Bash) in the same
pipeline was compressed — proving the whitelist now works and the
pipeline still compresses normally.
- Not tested: Anthropic native clients (Claude Code) and Responses-API
clients (Codex) end-to-end — the patch sites for those paths are covered
by unit tests only. No new dependencies; no public API changes.

## Review Readiness

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

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] My changes generate no new warnings
- [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` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Additional Notes

No documentation changes needed (no public API change; behavior is
internal to the proxy tool-name map). Follow-up: end-to-end verification
with Anthropic/Responses-API clients once maintainers can run the proxy
CI on those paths.

Co-authored-by: pgjh <pgjh@users.noreply.github.com>
2026-08-04 22:16:46 -05:00