Commit Graph
1 Commits
Author SHA1 Message Date
Tejas ChopraandClaude Opus 5 d17ac6a174 feat(hooks): add protect_messages, a hard per-message compression veto (#3772)
## Why

`compute_biases` is a **soft** lever. The router first decides *this
message gets compressed, with this strategy*, and only then hands the
bias down to that strategy as a multiplier. By the time bias is
consulted the decision to rewrite the message has already been made, and
strategies are free to clamp it against their own floors.

So there is no bias value — 8, 1000, anything — that reliably means
*"leave this one alone."* Bias tunes **how much** survives; it is never
asked **whether** to touch the message at all.

A hook that has concluded a specific message must survive verbatim has
no way to say so today. This adds one.

## What

A fourth hook alongside the existing three:

```python
def protect_messages(self, messages, ctx) -> set[int]:
    """Return message indices that must not be compressed at all."""
    return set()
```

`ContentRouter` checks it **before any routing decision**, so a
protected message passes through untouched whatever its content type or
role — covering both the content-block path and the string path:

```python
if i in hook_protect:
    result_slots[i] = message
    transforms_applied.append("router:protected:hook")
    continue
```

Threaded through `compress()`, the Anthropic handler (all six
`compress_messages` call sites) and the OpenAI handler.

## Inert by construction, not by a flag

There is no env var and nothing to switch off, because there is no state
to switch:

- the base implementation returns an empty set;
- `config.hooks` is `None` unless an operator installs one;
- with no hook the router's veto set is empty, so the `if i in
hook_protect` branch never fires.

An operator who installs nothing sees byte-identical output. A flag here
would gate a code path that is already unreachable.

## Tests

`tests/test_hook_protect_messages.py`, 12 tests, covering **both**
directions.

Six fail without the router change — verified by stashing
`content_router.py` and re-running:

```
FAILED test_a_protected_message_survives_verbatim[log-anthropic]
FAILED test_a_protected_message_survives_verbatim[json-openai]
FAILED test_a_protected_message_survives_verbatim[json-anthropic]
FAILED test_protection_beats_every_bias
FAILED test_the_seam_is_reachable_from_the_sdk
6 failed, 6 passed
```

`test_protection_beats_every_bias` is the one that pins the argument
above — it fails precisely because a bias cannot express a veto.

The other six assert the seam stays inert: the default hook protects
nothing, a hooks object that doesn't implement the method still works,
and an empty veto set changes nothing.

## Verification

- 12/12 new tests pass; **518 passed, 45 skipped, 0 failed** across the
hook/router/compress suites.
- `ruff check` clean, `ruff format --check` clean (1633 files), `mypy
headroom --ignore-missing-imports` — *Success: no issues found in 538
source files*. All at CI's pinned ruff 0.16.7.
- Every `biases=` call site in both handlers now has a matching
`protect=` — checked mechanically, not by eye.

## Known boundary

The Anthropic **image-only pre-pass** on the newest user turn
(`anthropic.py:603`) compresses image bytes without consulting the veto.
Left alone deliberately: it only rewrites images in the live zone, and
threading the hook context into it would widen this change with no
caller that needs it.

Gemini's native handler and Anthropic cache mode don't run the
compression-hook pipeline at all, so they're unaffected — as they
already are for `compute_biases`.

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

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-24 13:10:56 +02:00