headroom/tests/test_image_compression_decision.py
chopratejas a7b197c6ec refactor(proxy): MemoryRanker + ImageCompressionDecision + branch-aware version-sync
Three independent contract-pattern follow-ons bundled into one PR.
Same frozen-dataclass + factory + apply_to_tags + Rust-portable
shape that PR #473 / #477 / #483 established.

## (1) MemoryRanker + RecencyBoostRanker

Pre-this-PR Headroom ranked memory candidates by pure cosine
similarity. Every other memory system we surveyed (Letta, Mem0,
Cognee, Supermemory) re-ranks beyond cosine.

* ``MemoryRanker`` Protocol — pluggable re-ranker; future PRs add
  source-weight + access-count rankers behind the same interface.
* ``RecencyBoostRanker`` — first concrete impl. Final score is
  ``cosine × exp(-age_days / decay_days)``. Default decay 30 days
  (half-life ~21 days; 60-day-old factor 0.135, 90-day-old 0.050).
* ``MemoryCandidate`` — backend-agnostic frozen value type that
  flows through the ranker. ``MemoryCandidate.from_backend_result``
  adapter converts the existing ``MemoryResult`` shape (with nested
  ``memory.created_at``) into the ranker's flatter form.
* Wired into ``memory_handler.search_and_format_context`` as an
  optional ``ranker=`` kwarg — backwards-compat: ``None`` (default)
  preserves the pure-cosine path identically.

Defensive:
* ``created_at=None`` → factor 1.0 (recency-neutral, back-compat with
  legacy rows / migrating backends)
* Negative age (clock skew) → clamped to factor 1.0 (a future-dated
  row can't outrank a real fresh memory)
* Sort is stable on ties — same input → same output every turn, so
  consecutive turns inject memories in the same order (prefix-cache
  friendly)

Performance: O(N) over candidates where N=top_k≈10. One ``math.exp``
per candidate. Sub-microsecond. Zero new I/O.

## (2) ImageCompressionDecision

Mirror of :class:`CompressionDecision` for image compression. Two
sites today (``openai.py:1203``, ``anthropic.py:868``) gate inline;
both already respect bypass (no Gemini-class drift bug like text
compression had), but consolidating into a value type:

* Locks bypass-respect via AST contract test — future sites can't
  drift on it
* Surfaces ``image_skip_reason`` in ``RequestOutcome.tags`` for
  dashboard slicing (same observability surface as
  ``passthrough_reason`` and ``memory_skip_reason``)
* Same Rust-port shape as the other decision types

Precedence: ``bypass_header`` > ``image_optimize_disabled`` >
``no_messages`` > ``should_compress=True``.

Anthropic's extra ``is_cache_mode`` check stays inline because it's
Anthropic-specific (openai/gemini don't have it). Documented in a
code comment.

## (3) Branch-aware sync-plugin-versions hook

Pre-this-fix the pre-commit ``sync-plugin-versions`` hook ran on
every commit and bumped manifests to the predicted-next-release
version. Every PR ended up carrying the prediction as collateral
("Why are we bumping ``.claude-plugin/marketplace.json`` — we
should not, right??" - user, on PR #483).

Fix: the hook is now a NO-OP unless EITHER:
* We're on the ``main`` branch, OR
* ``HEADROOM_SYNC_VERSIONS=1`` is set explicitly (release workflow)

On feature branches the hook prints a single line explaining the
skip and exits cleanly. The release workflow opts in via the env
var; behaviour on main / at release time is unchanged.

## Test coverage

* 16 new tests on ``MemoryRanker`` / ``RecencyBoostRanker``
  (frozen, equal cosine wins by recency, decay configurable, NULL
  timestamp neutral, no-mutation contract, Rust-port shape)
* 17 new tests on ``ImageCompressionDecision`` (frozen, all 3
  skip reasons, precedence, observability fields, apply_to_tags)
* 1 new AST invariant test (extends
  ``test_handler_outcome_tag_invariant.py``) — locks "no raw
  ``if self.config.image_optimize and messages and not _bypass:``
  conjunction in any handler"

All existing memory + cache-stability + handler tests pass (203 ✓).
``make ci-precheck`` clean.

## Rust portability

All three new value types port cleanly to frozen Rust structs +
pure functions. Same migration pattern as ``CompressionDecision``
(already locked in for the SmartCrusher Rust port).

## Zero-regression contract

* Default ``ranker=None`` → memory_handler behaves identically to
  pre-this-PR (pure cosine; no perf change)
* Image decision migration is identity at the bypass/optimize/messages
  gate — no behaviour change, just contract consolidation
* Hook fix is no-op on feature branches (less churn) and unchanged
  on main (release flow preserved)
2026-05-19 12:13:09 -05:00

241 lines
9.1 KiB
Python

"""Tests for :class:`headroom.proxy.image_compression_decision.ImageCompressionDecision`.
Image compression today is gated at two sites (``openai.py:1203`` +
``anthropic.py:868``) by inline conjunctions. Both already check
``_bypass`` (the drift problem CompressionDecision fixed is NOT
present here), but consolidating into a value type still pays off:
* test-lockable contract (no future site can forget bypass)
* ``apply_to_tags()`` surfaces ``image_skip_reason`` in
``RequestOutcome.tags`` — dashboards can slice image-skipped
traffic by cause (same observability surface as
``passthrough_reason`` and ``memory_skip_reason``)
* Rust-portable shape mirrors :class:`CompressionDecision` exactly
Precedence (highest first):
1. ``bypass_header`` — user opt-out
2. ``image_optimize_disabled`` — operator config off
3. ``no_messages`` — nothing to inspect
"""
from __future__ import annotations
from dataclasses import FrozenInstanceError
from types import SimpleNamespace
from typing import Any
from headroom.proxy.image_compression_decision import ImageCompressionDecision
# ── Helpers ───────────────────────────────────────────────────────────
def _config(*, image_optimize: bool = True) -> Any:
"""Minimal stand-in for ``HeadroomConfig`` — only the field the
decision reads."""
return SimpleNamespace(image_optimize=image_optimize)
def _msgs(n: int = 1) -> list[dict[str, str]]:
return [{"role": "user", "content": f"hi-{i}"} for i in range(n)]
# ── Value-type contract ───────────────────────────────────────────────
def test_decision_is_frozen() -> None:
d = ImageCompressionDecision.decide(headers={}, config=_config(), messages=_msgs())
try:
d.should_compress = False # type: ignore[misc]
except FrozenInstanceError:
pass
else:
raise AssertionError("ImageCompressionDecision must be frozen")
def test_decision_is_value_equal() -> None:
a = ImageCompressionDecision.decide(headers={}, config=_config(), messages=_msgs())
b = ImageCompressionDecision.decide(headers={}, config=_config(), messages=_msgs())
assert a == b
# ── Precedence ────────────────────────────────────────────────────────
def test_compresses_when_every_gate_open() -> None:
d = ImageCompressionDecision.decide(
headers={}, config=_config(image_optimize=True), messages=_msgs()
)
assert d.should_compress is True
assert d.passthrough_reason is None
def test_bypass_header_wins() -> None:
"""Bypass is the user's explicit "don't touch my bytes" signal —
image compression mutates bytes (tile-aligns / re-encodes), so
bypass must skip it. Mirror of CompressionDecision."""
d = ImageCompressionDecision.decide(
headers={"x-headroom-bypass": "true"},
config=_config(image_optimize=True),
messages=_msgs(),
)
assert d.should_compress is False
assert d.passthrough_reason == "bypass_header"
def test_passthrough_mode_header_also_triggers_bypass_skip() -> None:
"""``x-headroom-mode: passthrough`` alt spelling — mirrors
``_headroom_bypass_enabled`` semantics across all proxy gates."""
d = ImageCompressionDecision.decide(
headers={"x-headroom-mode": "passthrough"},
config=_config(image_optimize=True),
messages=_msgs(),
)
assert d.should_compress is False
assert d.passthrough_reason == "bypass_header"
def test_image_optimize_disabled_is_skip() -> None:
"""Operator config — ``config.image_optimize = False``. Distinct
reason from ``compression_disabled`` (text compression's gate);
operators can enable text + disable image independently."""
d = ImageCompressionDecision.decide(
headers={}, config=_config(image_optimize=False), messages=_msgs()
)
assert d.should_compress is False
assert d.passthrough_reason == "image_optimize_disabled"
def test_no_messages_is_skip() -> None:
"""Empty or missing messages — nothing to look at. Same shape as
CompressionDecision's no_messages reason."""
d = ImageCompressionDecision.decide(headers={}, config=_config(), messages=[])
assert d.should_compress is False
assert d.passthrough_reason == "no_messages"
def test_messages_none_is_skip() -> None:
"""``messages=None`` is treated identically to empty list."""
d = ImageCompressionDecision.decide(headers={}, config=_config(), messages=None)
assert d.should_compress is False
assert d.passthrough_reason == "no_messages"
# ── Precedence ordering ──────────────────────────────────────────────
def test_bypass_beats_image_optimize_disabled() -> None:
"""User signal beats operator signal — bypass is the more
informative dashboard slice."""
d = ImageCompressionDecision.decide(
headers={"x-headroom-bypass": "true"},
config=_config(image_optimize=False),
messages=_msgs(),
)
assert d.passthrough_reason == "bypass_header"
def test_bypass_beats_no_messages() -> None:
"""Bypass+no-messages surfaces bypass — user opted out, the
empty body is incidental."""
d = ImageCompressionDecision.decide(
headers={"x-headroom-bypass": "true"},
config=_config(),
messages=[],
)
assert d.passthrough_reason == "bypass_header"
def test_image_optimize_disabled_beats_no_messages() -> None:
"""When config is off AND messages empty, surface the operator
decision — more meaningful for dashboards."""
d = ImageCompressionDecision.decide(
headers={}, config=_config(image_optimize=False), messages=[]
)
assert d.passthrough_reason == "image_optimize_disabled"
# ── Observability fields ─────────────────────────────────────────────
def test_observability_booleans_populated_when_compressing() -> None:
d = ImageCompressionDecision.decide(
headers={}, config=_config(image_optimize=True), messages=_msgs(2)
)
assert d.bypass_header_set is False
assert d.image_optimize_enabled is True
assert d.has_messages is True
def test_observability_booleans_populated_when_passthrough() -> None:
d = ImageCompressionDecision.decide(
headers={"x-headroom-bypass": "true"},
config=_config(image_optimize=True),
messages=_msgs(),
)
assert d.bypass_header_set is True
assert d.image_optimize_enabled is True
assert d.has_messages is True
# ── apply_to_tags ────────────────────────────────────────────────────
def test_apply_to_tags_stamps_reason_when_passthrough() -> None:
d = ImageCompressionDecision.decide(
headers={"x-headroom-bypass": "true"}, config=_config(), messages=_msgs()
)
tags: dict[str, str] = {}
d.apply_to_tags(tags)
assert tags == {"image_skip_reason": "bypass_header"}
def test_apply_to_tags_is_a_noop_when_compressing() -> None:
d = ImageCompressionDecision.decide(headers={}, config=_config(), messages=_msgs())
tags: dict[str, str] = {"client": "codex"}
d.apply_to_tags(tags)
assert tags == {"client": "codex"}
def test_apply_to_tags_preserves_pre_existing_entries() -> None:
"""Image skip reason coexists with other slicing tags (client,
passthrough_reason, memory_skip_reason) — they all live in the
same RequestOutcome.tags dict."""
d = ImageCompressionDecision.decide(
headers={}, config=_config(image_optimize=False), messages=_msgs()
)
tags: dict[str, str] = {
"client": "claude-code",
"passthrough_reason": "compression_disabled",
"memory_skip_reason": "no_user_id",
}
d.apply_to_tags(tags)
assert tags["client"] == "claude-code"
assert tags["passthrough_reason"] == "compression_disabled"
assert tags["memory_skip_reason"] == "no_user_id"
assert tags["image_skip_reason"] == "image_optimize_disabled"
def test_apply_to_tags_for_every_skip_reason() -> None:
"""Every reason name round-trips cleanly into the tag dict."""
cases: dict[str, dict[str, Any]] = {
"bypass_header": {
"headers": {"x-headroom-bypass": "true"},
"config": _config(),
"messages": _msgs(),
},
"image_optimize_disabled": {
"headers": {},
"config": _config(image_optimize=False),
"messages": _msgs(),
},
"no_messages": {
"headers": {},
"config": _config(),
"messages": [],
},
}
for expected_reason, kwargs in cases.items():
d = ImageCompressionDecision.decide(**kwargs)
tags: dict[str, str] = {}
d.apply_to_tags(tags)
assert tags.get("image_skip_reason") == expected_reason, expected_reason