fix(image): clear last_result before each compress() call (#4341)

## Description

`ImageCompressor.compress()` only assigned `self.last_result` on the
paths that actually compressed something. The per-worker instance is
reused across requests (`proxy/image_isolation.py:28-41`) and
`_compress_messages_worker` reports `last_result` whenever it is not
`None` (`:51-59`), so a request that `compress()` returned early on (no
images, URL-only images, no query) inherited the previous request's
technique and token numbers; the Anthropic handler then marked that
untouched body as `image_compression`-mutated and logged the stale
savings line (`proxy/handlers/anthropic.py:1923-1929`).

This resets `last_result` at the top of `compress()` so an early return
reports nothing. It covers the stale-result half of #4340; the
token-formula mismatch noted there is left for a separate change.

Refs #4340

## 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

- `headroom/image/compressor.py`: `self.last_result = None` as the first
statement of `compress()`, with a comment naming the singleton reuse
that makes it necessary.
- `tests/test_image_compressor_stale_result.py`: two tests, a text-only
request and a URL-only image request, each seeded with a stale
`CompressionResult`; both fail on `main` and pass with the change.

## Testing

- [x] Unit tests pass (`pytest`) — the two new tests plus
`tests/test_image_compressor.py` and
`tests/test_image_compressor_singleton_reuse.py`: 6 passed, 1 skipped, 1
failed; the one failure (`test_onnx_router_is_built_once_and_cached`,
`AttributeError: module 'headroom.image' has no attribute
'onnx_router'`) fails identically on `main` in this environment, which
has no ONNX runtime installed
- [x] Linting passes (`ruff check .`) — on the two changed files, plus
`ruff format --check`
- [ ] Type checking passes (`mypy headroom`) — not run; the change adds
one assignment to an already-typed attribute
- [x] New tests added for new functionality
- [x] Manual testing performed — reused-worker two-request proof below

### Test Output

```text
# tests added, source unchanged (fail-before), main 4c7acba5
$ pytest tests/test_image_compressor_stale_result.py -q --tb=line
tests/test_image_compressor_stale_result.py:52: AssertionError: assert CompressionResult(technique=<Technique.PRESERVE: 'preserve'>, original_tokens=765, compressed_tokens=2295, confidence=1.0) is None
FAILED tests/test_image_compressor_stale_result.py::test_compress_clears_last_result_when_there_are_no_images
FAILED tests/test_image_compressor_stale_result.py::test_compress_clears_last_result_for_url_only_images
2 failed in 1.60s

# with the fix (pass-after)
$ pytest tests/test_image_compressor_stale_result.py -q
2 passed in 0.94s

$ pytest tests/test_image_compressor_singleton_reuse.py tests/test_image_compressor.py -q
FAILED tests/test_image_compressor_singleton_reuse.py::test_onnx_router_is_built_once_and_cached   # AttributeError: module 'headroom.image' has no attribute 'onnx_router' — same on main here (no ONNX deps)
1 failed, 4 passed, 1 skipped in 1.39s

$ ruff check headroom/image/compressor.py tests/test_image_compressor_stale_result.py && ruff format --check headroom/image/compressor.py tests/test_image_compressor_stale_result.py
All checks passed!
2 files already formatted

# Worker-level proof (Devin's ask): two requests through the reused image worker, git-archive exports
$ PYTHONPATH=/var/tmp/cb-4341-main python -P pr4341_worker_proof.py main
[main] request 1 (inline PNG) -> result: {'technique': 'preserve', 'original_tokens': 765, 'compressed_tokens': 765, 'confidence': 0.0, 'savings_percent': 0.0}
[main] request 2 (URL-only)   -> result: {'technique': 'preserve', 'original_tokens': 765, 'compressed_tokens': 765, 'confidence': 0.0, 'savings_percent': 0.0}
[main] same singleton reused: True
[main] VERDICT: stale result leaked to request 2

$ PYTHONPATH=/var/tmp/cb-4341-fix python -P pr4341_worker_proof.py fix
[fix] request 1 (inline PNG) -> result: {'technique': 'preserve', 'original_tokens': 765, 'compressed_tokens': 765, 'confidence': 0.0, 'savings_percent': 0.0}
[fix] request 2 (URL-only)   -> result: None
[fix] same singleton reused: True
[fix] VERDICT: request 2 reports None (correct)
```

## Real Behavior Proof

- Environment: WSL2 Ubuntu, Python 3.12.15, pytest 9.1.1, ruff 0.17.1; a
clone of this fork at `4c7acba5` with the core runtime dependencies only
(no PIL, no ONNX runtime)
- Exact command / steps: add the test file alone and run it (both fail:
`last_result` still holds the seeded stale result after `compress()`
returned early), then apply the source change and run again (both pass);
then the two neighbouring image test files and ruff; then the
worker-level driver (`pr4341_worker_proof.py`, pasted above) calling
`headroom.proxy.image_isolation._compress_messages_worker` twice on the
same worker singleton against `git archive` exports of `main` and of
this branch: request 1 an inline base64 PNG, request 2 a URL-only image
- Observed result: before the fix, `compress()` on a text-only request
and on a URL-only image request leaves the previous `CompressionResult`
on `last_result`; after the fix both leave it `None`. Worker-level: on
`main` the second (URL-only) request through the reused worker returns
the first request's result dict verbatim (`technique preserve, 765 ->
765`); on this branch it returns `None`, so the handler does not mark
the body mutated
- Not tested: the full proxy handler path through
`run_image_compression_isolated` (needs the worker process); a request
that does compress (PIL/ONNX absent here, so the router falls back to
`preserve`), which still sets `last_result` on the same lines as before;
the token-formula half of #4340

## Runtime Rollout Safety

- Rollout-managed feature(s): none (image compression is not behind a
rollout flag)
- Minimum rollout channel: n/a (not rollout-managed)
- Stable/default behavior changed: yes, only for requests `compress()`
returns early on: they now report no image result instead of the
previous request's, so no `image_compression` mutation mark and no stale
savings log line for them; compressed requests are unchanged
- Kill switch / disable path: none added; disabling image compression
(`image_optimize=False`) bypasses the whole path as before
- Unsafe override required: no
- Qualification impact: none known; no `CHANGELOG.md` edit
(release-please)
- 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
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation (none
needed)
- [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 (with the
environment caveat above)
- [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

Produced with AI assistance (Claude Code) operated by me (HetCreep); the
test runs above were executed on my machine on a clone of this fork.
Related: #2513 / #2536 (the per-worker singleton), #4296 (open; alpha
flattening in the same file, a different bug).

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

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
HetCreep
2026-10-24 08:44:21 -07:00
committed by GitHub
co-authored by Claude Fable 5.1 JerrettDavis
parent a25623109d
commit 19c91a870f
2 changed files with 57 additions and 0 deletions
+5
View File
@@ -701,6 +701,11 @@ class ImageCompressor:
Returns:
Messages with compressed images
"""
# Reset first: the per-worker singleton (proxy/image_isolation.py) keeps
# one ImageCompressor for every request and reports last_result whenever
# it is not None, so a request we return early on below must not inherit
# the previous request's technique and token numbers.
self.last_result = None
if not self.has_images(messages) or not pillow_is_safe():
return messages
+52
View File
@@ -0,0 +1,52 @@
"""compress() must not report a previous request's result for a request it did not touch (#4340).
The per-worker ImageCompressor is a singleton (proxy/image_isolation.py reuses one
instance across requests) and _compress_messages_worker reports last_result whenever
it is not None. Before the fix, compress() left last_result set from the previous call
on its early-return paths, so a request whose images were never touched inherited the
previous request's technique and savings and was marked image_compression-mutated.
"""
from __future__ import annotations
from headroom.image.compressor import CompressionResult, ImageCompressor, Technique
def _stale() -> CompressionResult:
return CompressionResult(
technique=Technique.PRESERVE,
original_tokens=765,
compressed_tokens=2295,
confidence=1.0,
)
def test_compress_clears_last_result_when_there_are_no_images() -> None:
compressor = ImageCompressor()
compressor.last_result = _stale()
messages = [{"role": "user", "content": "plain text, no images"}]
out = compressor.compress(messages)
assert out == messages
assert compressor.last_result is None
def test_compress_clears_last_result_for_url_only_images() -> None:
# A URL image (no inline base64 data) reaches the "no image_data" early return;
# it must not report the previous request's result either.
compressor = ImageCompressor()
compressor.last_result = _stale()
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "describe this"},
{"type": "image_url", "image_url": {"url": "https://example.test/a.png"}},
],
}
]
compressor.compress(messages)
assert compressor.last_result is None