fix(proxy): preserve OpenCode Headroom retrieve results (#3782)

## Description

When OpenCode reports the retrieve tool as `headroom_headroom_retrieve`,
its result could miss the existing retrieve exclusion and be compressed
again. This adds that exact name to the shared alias mapping so the
existing ContentRouter pass-through guard can preserve the retrieved
content.

Fixes #3780

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Map `headroom_headroom_retrieve` to the existing `headroom_retrieve`
exclusion.
- Add alias coverage and a ContentRouter regression test for the
OpenCode name.

## Testing

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

### Test Output

```text
python -m pytest -q tests/test_tool_name_aliases.py: 5 passed
Changed-file Ruff checks, format check, compileall, and git diff --check: passed
python -m pytest -q tests/test_transforms/test_content_router_ccr_retrieve_exemption.py: collection blocked by ModuleNotFoundError: headroom._core
Full pytest, repository-wide Ruff, mypy, and live OpenCode/proxy verification were not run.
```

## Real Behavior Proof

- Environment: local submitter checkout; the native `headroom._core`
extension is unavailable.
- Exact command / steps: `python -m pytest -q
tests/test_tool_name_aliases.py` after the alias change.
- Observed result: 5 tests passed, including the exact OpenCode tool
name and negative alias checks.
- Not tested: the ContentRouter regression could not be collected
locally; the issue-reported cache/token OpenCode runtime was not
independently reproduced after this change.

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: the matching OpenCode retrieve name
now follows the existing exclusion behavior.
- Kill switch / disable path: None.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert this PR.

## 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
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] 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)

## Screenshots (if applicable)

Not applicable.

## Additional Notes

The focused alias test passes, but the end-to-end ContentRouter test and
live runtime behavior remain unverified in this environment.
This commit is contained in:
lorenzozane
2026-09-24 22:26:30 -05:00
committed by GitHub
parent 0c19244e38
commit c7ebaedd74
3 changed files with 42 additions and 1 deletions
+3 -1
View File
@@ -360,7 +360,9 @@ def _tool_name_aliases(name: str) -> tuple[str, ...]:
aliases = [name]
lname = name.lower()
if lname.startswith("mcp__"):
if lname == "headroom_headroom_retrieve":
aliases.append("headroom_retrieve")
elif lname.startswith("mcp__"):
# OpenAI-style MCP wrappers use mcp__server__tool. Custom agents that
# speak Anthropic sometimes emit the same wrapper as mcp_Server_tool.
parts = name.split("__", 2)
+25
View File
@@ -0,0 +1,25 @@
"""Tests for normalizing client-specific tool names."""
from __future__ import annotations
import pytest
from headroom.config import DEFAULT_EXCLUDE_TOOLS, is_tool_excluded
@pytest.mark.parametrize(
"name",
[
"headroom_retrieve",
"mcp__headroom__headroom_retrieve",
"mcp_headroom_headroom_retrieve",
"headroom_headroom_retrieve",
],
)
def test_retrieve_tool_aliases_are_excluded(name: str) -> None:
assert is_tool_excluded(name, DEFAULT_EXCLUDE_TOOLS)
def test_unrelated_underscored_tool_names_are_not_excluded() -> None:
assert not is_tool_excluded("my_headroom_retrieve", DEFAULT_EXCLUDE_TOOLS)
assert not is_tool_excluded("other_Read", DEFAULT_EXCLUDE_TOOLS)
@@ -181,6 +181,20 @@ class TestContentRouterCcrRetrieveExemption:
assert "<<ccr:" not in tool_msg["content"]
assert "router:excluded:ccr_retrieve" in result.transforms_applied
def test_server_tool_mcp_name_not_recompressed(self):
"""OpenCode's <server>_<tool> name must resolve to the excluded tool."""
content = _big_json()
router = ContentRouter(ContentRouterConfig(min_section_tokens=10))
tokenizer = _get_tokenizer()
messages = _openai_messages("headroom_headroom_retrieve", content)
result = router.apply(messages, tokenizer)
tool_msg = next(m for m in result.messages if m.get("tool_call_id") == "call_ccr_1")
assert tool_msg["content"] == content
assert "<<ccr:" not in tool_msg["content"]
assert "router:excluded:ccr_retrieve" in result.transforms_applied
def test_bare_tool_name_also_protected(self):
"""The proxy's own internally-injected retrieval tool uses the bare
name (not MCP-qualified) — must be protected too, matching the