mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
Closes #861 ## What First piece of compression quality measurement on **real proxied sessions** (vs the existing public-benchmark evals): an opt-in recorder captures (original, compressed) message pairs at each compression event, and a deterministic offline prober scores what survived. **Recorder** (`headroom/proxy/probe_recorder.py`) - `CompressionEventRecorder` implements the existing `PipelineExtension` protocol, subscribed to `INPUT_COMPRESSED`. Registered ONLY when `HEADROOM_PROBE_RECORD_DIR` is set — off means not even constructed, zero request-path overhead. - One JSONL line per compression event that changed tokens: `{ts, request_id, provider, model, tokens_before, tokens_after, transforms_applied, original_messages, compressed_messages}`. One file per PID (no interleaving), directory mode 0700. - Fail-open everywhere: construction failure logs a warning and disables recording; runtime exceptions are already swallowed by `PipelineExtensionManager.emit`. - Enabling handler change: the two `INPUT_COMPRESSED` emit sites (anthropic + openai) add a read-only `original_messages` reference to event metadata. No copies, no behavior change for other consumers. **Probes** (`headroom/evals/session_probes.py` + `headroom evals probes` CLI) - Probe targets extracted from ORIGINAL tool-result content across three dimensions: **exact numerics** (number + key context, incl. JSON-quoted keys), **artifact trail** (paths, URLs, hex hashes, UUIDs), **error evidence** (lines matching the existing `is_error_content` heuristic). - Each target classified as **retained** (verbatim, or surviving a legitimate format conversion — punctuation-normalized match; numerics require key AND value to survive; error lines tolerate dropped JSON key prefixes), **recoverable** (absent but a CCR retrieval marker is present), or **lost** (gone with no retrieval path). - Report: aggregate retention per dimension, bucketed by compression ratio (the quality-per-ratio curve), and grouped per transform. `--json-output` for machine-readable results. - Fully offline: no LLM, no API key. The recording format is designed to feed an LLM-judge pass later (out of scope per #861). ## Tests 33 new tests (red before, green after): `tests/test_probe_recorder.py` (11 — event filtering, JSONL shape, env activation, fail-open on unusable path, 0700 dir mode) and `tests/test_session_probes.py` (22 — extraction per dimension incl. JSON-quoted numerics, retained/recoverable/lost classification, format-change survival for numerics and error lines, ratio bucketing, transform dedup, malformed-line skipping, report rendering/serialization). Both proxy lifecycle tests additionally assert the INPUT_COMPRESSED `original_messages` metadata contract end-to-end through the real anthropic/openai handlers, so a refactor cannot silently disable the recorder. Local runs: new tests + `tests/test_proxy_pipeline_lifecycle.py` + `tests/test_canonical_pipeline.py` + `tests/test_pipeline.py` — 45 passed. `ruff check` + `uvx ruff format --check` clean. ### Self-review hardening (second commit) - Hex artifact regex now requires at least one `a-f`, so bare decimal runs (timestamps, counters) no longer inflate the artifact dimension. - Inflation events (ratio > 1, the #847 territory) get an explicit `1.00+ (inflated)` ratio bucket instead of silently dropping out of the bucketed view. - `run_probes` streams recording files line by line instead of slurping them. - Documented honestly: marker recoverability is event-scoped (comparative metric, not absolute); recorder writes synchronously on the request path (diagnostic sessions, not always-on). - `headroom/evals/README.md` gained a Session Probes usage section. ## Real behavior proof **Setup:** macOS (Darwin 25.5), Python 3.11.9, this branch, real proxy (`python -m headroom.proxy.server --port 18994 --anthropic-api-url http://127.0.0.1:18995`) with a local mock Anthropic upstream (no key), `HEADROOM_PROBE_RECORD_DIR=/tmp/headroom-probe-proof/recordings`. **Steps:** POSTed three Anthropic-format conversations whose tool results carry large JSON arrays (220-row uniform logs, 3000-row logs, 800 heterogeneous events), each containing known numerics, paths, trace hashes, and one error line. **Observed** — recorder wrote `compression-events-<pid>.jsonl` (one line per event); `headroom evals probes --recordings ...`: ``` Probed 3 compression events Aggregate retention: numerics 97.7% retained, 2.3% recoverable, 0.0% lost (527 targets) artifacts 100.0% retained, 0.0% recoverable, 0.0% lost (6555 targets) errors 100.0% retained, 0.0% recoverable, 0.0% lost (3 targets) By compression ratio (tokens_after / tokens_before): ratio 0.50-0.75: numerics 100.0% retained (CSV compaction — lossless, correctly recognized) ratio 0.75-1.00: numerics 95.7% retained, 4.3% recoverable (SmartCrusher sampling — dropped values carried a CCR marker) ``` All three classifications exercised: verbatim/format-change retention on the CSV-compacted events, **recoverable** on the heterogeneous event where SmartCrusher sampled rows out behind a `Retrieve more: hash=` marker, and the injected error lines retained in every event (the error-protection gate held). The `lost` path is covered by unit tests. The first iteration of this proof exposed two real bugs — naive verbatim matching misreported lossless JSON→CSV compaction as 100% lost, and duplicated transform markers double-counted tallies — both fixed with regression tests. **Not tested live:** Gemini path (no `INPUT_COMPRESSED` emit parity — pre-existing, same gap as #819); LLM-judge scoring (out of scope per #861). ## Security Recordings contain full conversation content in plaintext: opt-in env var only, local disk only, dir mode 0700, documented in CLI help. ## Out of scope (per #861) LLM-judge dimensions (decisions/intent, next steps), ACON-style counterfactual replay, automatic rule revision. --------- Co-authored-by: Ash Rhodes <ashley.rhodes@king.com>
280 lines
9.9 KiB
Python
280 lines
9.9 KiB
Python
"""Tests for deterministic retention probes over recorded compression events."""
|
|
|
|
import json
|
|
|
|
from headroom.evals.session_probes import (
|
|
DIMENSIONS,
|
|
DimensionTally,
|
|
extract_probe_targets,
|
|
probe_event,
|
|
render_report,
|
|
run_probes,
|
|
)
|
|
|
|
ORIGINAL_TOOL_TEXT = (
|
|
"Deploy summary\n"
|
|
"retry_limit: 3\n"
|
|
"port=8787\n"
|
|
"see headroom/proxy/server.py and https://example.com/build/42\n"
|
|
"commit d293b77ab12\n"
|
|
"ModuleNotFoundError: No module named 'left_pad'\n"
|
|
)
|
|
|
|
|
|
def _record(compressed_content, tokens_before=100, tokens_after=40, transforms=None):
|
|
return {
|
|
"request_id": "req-1",
|
|
"tokens_before": tokens_before,
|
|
"tokens_after": tokens_after,
|
|
"transforms_applied": transforms or ["smart_crusher"],
|
|
"original_messages": [
|
|
{
|
|
"role": "user",
|
|
"content": [{"type": "tool_result", "content": ORIGINAL_TOOL_TEXT}],
|
|
}
|
|
],
|
|
"compressed_messages": [
|
|
{
|
|
"role": "user",
|
|
"content": [{"type": "tool_result", "content": compressed_content}],
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
class TestExtractProbeTargets:
|
|
def test_extracts_contextual_numerics(self):
|
|
targets = extract_probe_targets("retry_limit: 3 and port=8787")
|
|
|
|
assert "retry_limit: 3" in targets["numerics"]
|
|
assert "port=8787" in targets["numerics"]
|
|
|
|
def test_extracts_json_quoted_numerics(self):
|
|
targets = extract_probe_targets('{"latency_ms": 12, "status": 200}')
|
|
|
|
assert 'latency_ms": 12' in targets["numerics"]
|
|
assert 'status": 200' in targets["numerics"]
|
|
|
|
def test_extracts_artifacts(self):
|
|
text = (
|
|
"path headroom/proxy/server.py url https://example.com/build/42 "
|
|
"hash d293b77ab12 id 123e4567-e89b-42d3-a456-426614174000"
|
|
)
|
|
targets = extract_probe_targets(text)
|
|
|
|
assert "headroom/proxy/server.py" in targets["artifacts"]
|
|
assert "https://example.com/build/42" in targets["artifacts"]
|
|
assert "d293b77ab12" in targets["artifacts"]
|
|
assert "123e4567-e89b-42d3-a456-426614174000" in targets["artifacts"]
|
|
|
|
def test_bare_decimal_runs_are_not_artifacts(self):
|
|
targets = extract_probe_targets("run id 27344471690 at ts 1765449600")
|
|
|
|
assert "27344471690" not in targets["artifacts"]
|
|
assert "1765449600" not in targets["artifacts"]
|
|
|
|
def test_extracts_error_lines(self):
|
|
targets = extract_probe_targets("all good\nModuleNotFoundError: No module named 'x'\n")
|
|
|
|
assert any("ModuleNotFoundError" in value for value in targets["errors"])
|
|
assert all("all good" not in value for value in targets["errors"])
|
|
|
|
def test_openai_role_tool_messages_supported(self):
|
|
record = _record("anything")
|
|
record["original_messages"] = [{"role": "tool", "content": ORIGINAL_TOOL_TEXT}]
|
|
|
|
result = probe_event(record)
|
|
|
|
assert result is not None
|
|
assert result.dims["numerics"].total > 0
|
|
|
|
|
|
class TestProbeEvent:
|
|
def test_everything_retained_when_content_survives(self):
|
|
result = probe_event(_record(ORIGINAL_TOOL_TEXT))
|
|
|
|
assert result is not None
|
|
for name in DIMENSIONS:
|
|
tally = result.dims[name]
|
|
assert tally.total > 0
|
|
assert tally.retained == tally.total
|
|
assert tally.lost == 0
|
|
|
|
def test_recoverable_when_ccr_marker_present(self):
|
|
result = probe_event(_record("[60 items compressed to 5. Retrieve more: hash=abc123def]"))
|
|
|
|
assert result is not None
|
|
numerics = result.dims["numerics"]
|
|
assert numerics.total > 0
|
|
assert numerics.retained == 0
|
|
assert numerics.recoverable == numerics.total
|
|
assert numerics.lost == 0
|
|
|
|
def test_lost_when_dropped_without_marker(self):
|
|
result = probe_event(_record("everything went fine"))
|
|
|
|
assert result is not None
|
|
for name in DIMENSIONS:
|
|
tally = result.dims[name]
|
|
assert tally.retained == 0
|
|
assert tally.recoverable == 0
|
|
assert tally.lost == tally.total
|
|
|
|
def test_ratio_and_transforms(self):
|
|
result = probe_event(_record("x", tokens_before=200, tokens_after=50))
|
|
|
|
assert result is not None
|
|
assert result.ratio == 0.25
|
|
assert result.transforms == ["smart_crusher"]
|
|
|
|
def test_numerics_retained_across_format_change(self):
|
|
record = _record("| latency_ms | status |\n| 12 | 200 |")
|
|
record["original_messages"] = [
|
|
{"role": "tool", "content": '{"latency_ms": 12, "status": 200}'}
|
|
]
|
|
|
|
result = probe_event(record)
|
|
|
|
assert result is not None
|
|
numerics = result.dims["numerics"]
|
|
assert numerics.total > 0
|
|
assert numerics.retained == numerics.total
|
|
|
|
def test_numerics_lost_when_value_dropped_after_format_change(self):
|
|
record = _record("| latency_ms |\n| 99 |")
|
|
record["original_messages"] = [{"role": "tool", "content": '{"latency_ms": 12}'}]
|
|
|
|
result = probe_event(record)
|
|
|
|
assert result is not None
|
|
assert result.dims["numerics"].lost == result.dims["numerics"].total
|
|
|
|
def test_error_line_survives_punctuation_rewrite(self):
|
|
record = _record("msg=ModuleNotFoundError: No module named 'left_pad'")
|
|
record["original_messages"] = [
|
|
{
|
|
"role": "tool",
|
|
"content": '{"msg": "ModuleNotFoundError: No module named \'left_pad\'"}',
|
|
}
|
|
]
|
|
|
|
result = probe_event(record)
|
|
|
|
assert result is not None
|
|
errors = result.dims["errors"]
|
|
assert errors.total > 0
|
|
assert errors.retained == errors.total
|
|
|
|
def test_error_line_survives_json_to_csv_compaction(self):
|
|
record = _record("error,ModuleNotFoundError: No module named 'left_pad',src/imports.py")
|
|
record["original_messages"] = [
|
|
{
|
|
"role": "tool",
|
|
"content": '{"msg": "ModuleNotFoundError: No module named \'left_pad\'"}',
|
|
}
|
|
]
|
|
|
|
result = probe_event(record)
|
|
|
|
assert result is not None
|
|
errors = result.dims["errors"]
|
|
assert errors.total > 0
|
|
assert errors.retained == errors.total
|
|
|
|
def test_rejects_unscorable_records(self):
|
|
assert probe_event({"tokens_before": 0, "tokens_after": 0}) is None
|
|
assert probe_event({"tokens_before": "x", "tokens_after": 5}) is None
|
|
assert probe_event({}) is None
|
|
|
|
|
|
class TestRunProbesAndReport:
|
|
def test_run_probes_reads_jsonl_and_skips_garbage(self, tmp_path):
|
|
records = [
|
|
_record(ORIGINAL_TOOL_TEXT),
|
|
_record("gone", tokens_before=100, tokens_after=80),
|
|
]
|
|
lines = [json.dumps(record) for record in records]
|
|
lines.insert(1, "{not valid json")
|
|
lines.append(json.dumps(["not", "a", "dict"]))
|
|
(tmp_path / "compression-events-1.jsonl").write_text(
|
|
"\n".join(lines) + "\n", encoding="utf-8"
|
|
)
|
|
|
|
report = run_probes(tmp_path)
|
|
|
|
assert len(report.events) == 2
|
|
assert report.skipped_lines == 2
|
|
|
|
def test_aggregate_sums_dimensions(self, tmp_path):
|
|
path = tmp_path / "compression-events-1.jsonl"
|
|
path.write_text(
|
|
json.dumps(_record(ORIGINAL_TOOL_TEXT)) + "\n" + json.dumps(_record("gone")) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
report = run_probes(tmp_path)
|
|
aggregate = report.aggregate()
|
|
|
|
for name in DIMENSIONS:
|
|
single = report.events[0].dims[name]
|
|
assert aggregate[name].total == single.total * 2
|
|
assert aggregate[name].retained == single.total
|
|
assert aggregate[name].lost == single.total
|
|
|
|
def test_bucketing_and_transform_grouping(self, tmp_path):
|
|
path = tmp_path / "compression-events-1.jsonl"
|
|
path.write_text(
|
|
json.dumps(_record("gone", tokens_before=100, tokens_after=10)) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
report = run_probes(tmp_path)
|
|
|
|
buckets = report.by_ratio_bucket()
|
|
assert buckets["0.00-0.25"]["numerics"].total > 0
|
|
assert buckets["0.75-1.00"]["numerics"].total == 0
|
|
assert "smart_crusher" in report.by_transform()
|
|
|
|
def test_inflated_events_land_in_inflation_bucket(self, tmp_path):
|
|
record = _record("gone", tokens_before=100, tokens_after=130)
|
|
path = tmp_path / "compression-events-1.jsonl"
|
|
path.write_text(json.dumps(record) + "\n", encoding="utf-8")
|
|
|
|
report = run_probes(tmp_path)
|
|
|
|
buckets = report.by_ratio_bucket()
|
|
assert buckets["1.00+ (inflated)"]["numerics"].total > 0
|
|
assert all(
|
|
dims["numerics"].total == 0
|
|
for label, dims in buckets.items()
|
|
if label != "1.00+ (inflated)"
|
|
)
|
|
|
|
def test_transform_grouping_dedupes_repeated_markers(self, tmp_path):
|
|
record = _record("gone", transforms=["smart_crusher", "smart_crusher"])
|
|
path = tmp_path / "compression-events-1.jsonl"
|
|
path.write_text(json.dumps(record) + "\n", encoding="utf-8")
|
|
|
|
report = run_probes(tmp_path)
|
|
|
|
per_dim = report.by_transform()["smart_crusher"]
|
|
assert per_dim["numerics"].total == report.events[0].dims["numerics"].total
|
|
|
|
def test_to_dict_and_render(self, tmp_path):
|
|
path = tmp_path / "compression-events-1.jsonl"
|
|
path.write_text(json.dumps(_record("gone")) + "\n", encoding="utf-8")
|
|
|
|
report = run_probes(tmp_path)
|
|
payload = report.to_dict()
|
|
rendered = render_report(report)
|
|
|
|
assert payload["aggregate"]["numerics"]["lost"] > 0
|
|
assert payload["events"][0]["ratio"] == 0.4
|
|
assert "Aggregate retention" in rendered
|
|
assert "smart_crusher" in rendered
|
|
|
|
def test_dimension_tally_lost_property(self):
|
|
tally = DimensionTally(total=5, retained=2, recoverable=1)
|
|
|
|
assert tally.lost == 2
|
|
assert tally.to_dict() == {"total": 5, "retained": 2, "recoverable": 1, "lost": 2}
|