Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(evals): reject changed numeric probe values (#4502)

Rudy Mizrahi Celekli1fe8549f9ca7
diff --git a/headroom/evals/session_probes.py b/headroom/evals/session_probes.py
index 5574841ab..0e8bde4c1 100644
--- a/headroom/evals/session_probes.py
+++ b/headroom/evals/session_probes.py
@@ -46,7 +46,8 @@ _CCR_MARKER_RE = re.compile(r"Retrieve more: hash=|Retrieve original: hash=|<<cc
# A number with its immediate key context ("retry_limit: 3", "port=8787",
# JSON's '"latency_ms": 12'). Bare numbers are skipped: without context they
# are unverifiable noise.
-_NUMERIC_RE = re.compile(r"[A-Za-z_][\w.-]{0,24}\"?[ =:]{1,3}\d+(?:\.\d+)?")
+# Do not extract a decimal prefix from unsupported exponents or numeric suffixes.
+_NUMERIC_RE = re.compile(r"[A-Za-z_][\w.-]{0,24}\"?[ =:]{1,3}\d+(?:\.\d+)?(?!\w|\.\d)")
_URL_RE = re.compile(r"https?://[^\s\"'<>)\]]+")
_PATH_RE = re.compile(r"(?:~/|\.{1,2}/|/)?(?:[\w.-]+/){2,}[\w.@-]+")
# Requires at least one a-f so bare decimal runs (timestamps, row counts) are
@@ -233,6 +234,21 @@ def _normalize(text: str) -> str:
def _target_survives(dimension: str, value: str, haystack: str, normalized_haystack: str) -> bool:
+ if dimension == "numerics":
+ # Check numbers before substring shortcuts: 80 is not retained in
+ # 8080, 80.5 or -80. A trailing sentence period is still punctuation.
+ # Format conversions (JSON -> table) may separate key from value.
+ match = _NUMERIC_SPLIT_RE.match(value)
+ if match:
+ key, number = match.groups()
+ normalized_key = _normalize(key)
+ if (
+ normalized_key
+ and normalized_key in normalized_haystack
+ and re.search(rf"(?<![\w.-]){re.escape(number)}(?!\w|\.\d)", normalized_haystack)
+ ):
+ return True
+ return False
if value in haystack:
return True
normalized_value = _normalize(value)
@@ -244,19 +260,6 @@ def _target_survives(dimension: str, value: str, haystack: str, normalized_hayst
_, _, remainder = normalized_value.partition(" ")
if len(remainder) >= _MIN_TARGET_LEN and remainder in normalized_haystack:
return True
- if dimension == "numerics":
- # Format conversions (JSON -> table) separate key from value; count the
- # probe as retained only when both still appear.
- match = _NUMERIC_SPLIT_RE.match(value)
- if match:
- key, number = match.groups()
- normalized_key = _normalize(key)
- if (
- normalized_key
- and normalized_key in normalized_haystack
- and re.search(rf"\b{re.escape(number)}\b", normalized_haystack)
- ):
- return True
return False
diff --git a/tests/test_session_probe_numeric_boundaries.py b/tests/test_session_probe_numeric_boundaries.py
new file mode 100644
index 000000000..36dcced9c
--- /dev/null
+++ b/tests/test_session_probe_numeric_boundaries.py
@@ -0,0 +1,106 @@
+"""An unchanged numeric prefix is not evidence that the original value survived."""
+
+import pytest
+
+from headroom.evals.session_probes import probe_event
+
+
+ "compressed",
+ [
+ "port: 8080",
+ '"port": 80.5',
+ "port: -80",
+ "port: 80e3",
+ "| port |\n| 8080 |",
+ "| port |\n| 80.5 |",
+ "| port |\n| -80 |",
+ ],
+)
+def test_changed_numeric_value_is_lost(compressed: str) -> None:
+ result = probe_event(
+ {
+ "tokens_before": 100,
+ "tokens_after": 10,
+ "original_messages": [{"role": "tool", "content": "port: 80"}],
+ "compressed_messages": [{"role": "tool", "content": compressed}],
+ }
+ )
+ assert result is not None
+ assert result.dims["numerics"].to_dict() == {
+ "total": 1,
+ "retained": 0,
+ "recoverable": 0,
+ "lost": 1,
+ }
+
+
+ "compressed", ["port: 80", '"port": 80', "| port |\n| 80 |", "port is 80."]
+)
+def test_exact_numeric_value_remains_retained(compressed: str) -> None:
+ result = probe_event(
+ {
+ "tokens_before": 100,
+ "tokens_after": 10,
+ "original_messages": [{"role": "tool", "content": "port: 80"}],
+ "compressed_messages": [{"role": "tool", "content": compressed}],
+ }
+ )
+ assert result is not None
+ assert result.dims["numerics"].retained == 1
+
+
+def test_changed_value_with_marker_is_recoverable_not_retained() -> None:
+ result = probe_event(
+ {
+ "tokens_before": 100,
+ "tokens_after": 10,
+ "original_messages": [{"role": "tool", "content": "port: 80"}],
+ "compressed_messages": [{"role": "tool", "content": "port: 8080 <<ccr:abc123>>"}],
+ }
+ )
+ assert result is not None
+ assert result.dims["numerics"].retained == 0
+ assert result.dims["numerics"].recoverable == 1
+
+
[email protected]("number", ["80e3", "80e-3", "80e+3", "0.001e2", "0.001e-2", "80E3"])
+def test_unsupported_exponent_does_not_generate_partial_probe(number: str) -> None:
+ from headroom.evals.session_probes import extract_probe_targets
+
+ text = f"port: {number}"
+ assert extract_probe_targets(text)["numerics"] == set()
+ result = probe_event(
+ {
+ "tokens_before": 100,
+ "tokens_after": 10,
+ "original_messages": [{"role": "tool", "content": text}],
+ "compressed_messages": [{"role": "tool", "content": text}],
+ }
+ )
+ assert result is not None
+ assert result.dims["numerics"].total == 0
+
+
+ "original,compressed,retained",
+ [
+ ("port: 8080", "port: 8081", 0),
+ ("latency: 80.5", "latency: 80.50", 0),
+ ("latency: 80.5", "latency: 80.5", 1),
+ ("port: 8080", "port: 8080", 1),
+ ],
+)
+def test_complete_integer_and_decimal_values(original: str, compressed: str, retained: int) -> None:
+ result = probe_event(
+ {
+ "tokens_before": 100,
+ "tokens_after": 10,
+ "original_messages": [{"role": "tool", "content": original}],
+ "compressed_messages": [{"role": "tool", "content": compressed}],
+ }
+ )
+ assert result is not None
+ assert result.dims["numerics"].total == 1
+ assert result.dims["numerics"].retained == retained