refactor(proxy): delete the unused memory_tool_adapter module (#4071)

## Description

This PR deletes `headroom/proxy/memory_tool_adapter.py` (1280 lines).
The module predates `MemoryHandler`, and nothing in the product imports
it.

Its only importer was `tests/test_memory_tool_adapter_null_fields.py`.
That test checked the adapter's private parse helpers on an instance
built with `object.__new__`. The live code path has its own
null-`function` test, `tests/test_memory_handler_null_function.py`,
which stays.

Evidence: I searched for `memory_tool_adapter` and `MemoryToolAdapter`
in `headroom`, `crates`, `plugins`, `sdk`, `scripts`, `docs`, `wiki`,
`tests`, `pyproject.toml` and the private consumer repos. The only hits
were:
- the module itself;
- the test above;
- one docstring line in `helpers.py`, which this PR updates.

The module is not in any lazy export map or entry-point group.

Refs #4136

## Type of Change

- [ ] 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
- [x] Code refactoring (no functional changes)

## Changes Made

- Delete `headroom/proxy/memory_tool_adapter.py`.
- Delete `tests/test_memory_tool_adapter_null_fields.py`, which tested
only the deleted module.
- `headroom/proxy/helpers.py`: drop the docstring mention of the deleted
file in `serialize_tool_definition_canonical`.

## Testing

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

### Test Output

```text
$ pytest -q -p no:cacheprovider tests/test_memory_handler_null_function.py tests/test_memory_handler_native_ops.py tests/test_proxy_memory_integration.py
======================== 35 passed, 12 skipped in 4.48s ========================
$ uvx ruff@0.16.8 check headroom/proxy/helpers.py
All checks passed!
$ python -c "import headroom.proxy.server"
import ok
$ grep -rn 'memory_tool_adapter\|MemoryToolAdapter' headroom tests
(no output)
```

## Real Behavior Proof

- Environment: macOS, Python 3.12.13, this branch at 4b1cf7d9, using the
repo .venv. Every run used a temp `HOME`, `HEADROOM_WORKSPACE_DIR` and
`CODEX_HOME`, plus `HEADROOM_OFFLINE=1`.
- Exact command / steps:
- (1) Ran the shared smoke harness with memory on: `smoke.sh <worktree>
--memory`. It starts a real `headroom proxy --memory` from this checkout
against a local fake upstream, sends one Anthropic and one OpenAI
request that each carry a compressible tool result, reads `/stats`, and
greps `proxy.log`.
- (2) A direct check that the module is gone and that the live
`MemoryHandler` still injects the native memory tool.
- Observed result:

```text
(1) branch: cleanup/proxy-core-b-memory-tool-adapter @ 4b1cf7d9
health: 200
POST /v1/messages        -> HTTP200 smoke-ok
POST /v1/chat/completions -> HTTP200 smoke-ok
stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82
proxy.log WARNING lines: only the expected air-gap ones (offline mode, Kompress/embedder not cached, memory project unresolved for a request without a project id).

(2) python -c "...find_spec('headroom.proxy.memory_tool_adapter'); MemoryHandler(MemoryConfig(enabled=True, use_native_tool=True)).inject_tools([], 'anthropic')"
INFO - Memory: Injected native memory tool (memory_20250818). Beta header required: context-management-2025-06-27
memory_tool_adapter importable: False
MemoryHandler native tool injected: True ['memory']
```

- Not tested: Live memory traffic against a real provider. The deleted
module was never on that path.

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: N/A; dead-code removal.
- Stable/default behavior changed: No. Nothing imported the module.
- Kill switch / disable path: N/A.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert this commit.

## 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
- [x] My changes generate no new warnings
- [ ] 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)

## Screenshots (if applicable)

N/A

## Additional Notes

Part of maintainer-requested cleanup #4136 (dead code, duplication,
debuggability), split into small PRs for review. This is a pure
deletion. No new tests are needed. No user-facing doc mentions the
module (only internal planning notes under REALIGNMENT/); any doc
follow-ups go in one separate docs PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
Tejas Chopra
2026-10-10 08:55:43 -07:00
committed by GitHub
parent 7d9dd55e7c
commit 7e5d97fe29
3 changed files with 1 additions and 1317 deletions
+1 -2
View File
@@ -2426,8 +2426,7 @@ def serialize_tool_definition_canonical(tool_definition: dict[str, Any]) -> byte
no ASCII escaping). Python 3.7+ dict insertion order is preserved by
``json.dumps`` so callers must construct the tool definition with a
stable key order — which the static schemas in
``headroom/proxy/memory_handler.py`` and
``headroom/proxy/memory_tool_adapter.py`` already do.
``headroom/proxy/memory_handler.py`` already do.
Returned bytes pin the golden tool definition for a session: every
follow-up turn must inject byte-equal output to keep the prefix
File diff suppressed because it is too large Load Diff
@@ -1,35 +0,0 @@
"""A tool call with a null ``function`` / ``arguments`` must not crash the
memory tool adapter's provider-format parsing.
``dict.get("function", {})`` returns ``None`` for a present-but-null key, so the
following ``.get`` raised ``AttributeError``; a null ``arguments`` makes
``json.loads(None)`` raise ``TypeError`` that the bare ``JSONDecodeError`` catch
missed. Both are reachable from the untrusted upstream response. The parse
helpers read only the ``tool_call`` argument, so we exercise them on a bare
instance via ``object.__new__``.
"""
from __future__ import annotations
from headroom.proxy.memory_tool_adapter import MemoryToolAdapter
_adapter = object.__new__(MemoryToolAdapter)
def test_get_tool_name_survives_null_function():
tc = {"id": "c1", "type": "function", "function": None}
assert _adapter._get_tool_name(tc, "openai") == ""
assert _adapter._get_tool_id(tc, "openai") == "c1"
assert _adapter._get_tool_input(tc, "openai") == {}
def test_get_tool_input_survives_null_arguments():
# json.loads(None) raises TypeError, not JSONDecodeError.
tc = {"function": {"name": "memory_save", "arguments": None}}
assert _adapter._get_tool_input(tc, "openai") == {}
def test_get_tool_helpers_still_parse_real_calls():
tc = {"function": {"name": "memory_save", "arguments": '{"content": "hi"}'}}
assert _adapter._get_tool_name(tc, "openai") == "memory_save"
assert _adapter._get_tool_input(tc, "openai") == {"content": "hi"}