Skip to content

The agent that grows with you

fix(agent): invalidate usage anchors after prefix rewrites

Ahmett1019f27e75a77bc
diff --git a/agent/usage_anchor.py b/agent/usage_anchor.py
index d93930c720f..d1676183123 100644
--- a/agent/usage_anchor.py
+++ b/agent/usage_anchor.py
@@ -43,6 +43,27 @@ def message_fingerprint(msg: Any) -> Optional[str]:
return hashlib.sha256(raw.encode("utf-8", "replace")).hexdigest()
+def _priced_prefix_fingerprint(messages: List[Dict[str, Any]], base_count: int) -> Optional[str]:
+ """Stable digest of the whole provider-priced prefix.
+
+ The last priced message fingerprint proves only that one row survived at
+ ``base_count - 1``. A compaction can preserve that row while rewriting the
+ earlier prefix, so the anchor must also bind to the full priced prefix it
+ represents.
+ """
+ if base_count <= 0 or len(messages) < base_count:
+ return None
+ fps = []
+ for msg in messages[:base_count]:
+ fp = message_fingerprint(msg)
+ if not fp:
+ return None
+ role = msg.get("role") if isinstance(msg, dict) else None
+ fps.append((role, fp))
+ raw = json.dumps(fps, separators=(",", ":"), ensure_ascii=True)
+ return hashlib.sha256(raw.encode("utf-8", "replace")).hexdigest()
+
+
def capture_usage_anchor(prompt_tokens: Any, completion_tokens: Any, messages: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
"""Build a usage anchor from provider-reported usage, or None when usage is unusable."""
try:
@@ -59,6 +80,7 @@ def capture_usage_anchor(prompt_tokens: Any, completion_tokens: Any, messages: L
"base_count": len(messages),
"base_last_role": last.get("role") if isinstance(last, dict) else None,
"base_last_fp": message_fingerprint(last),
+ "base_prefix_fp": _priced_prefix_fingerprint(messages, len(messages)),
}
@@ -73,7 +95,14 @@ def _anchor_matches(messages: List[Dict[str, Any]], anchor: Dict[str, Any]) -> b
if not isinstance(base_msg, dict) or base_msg.get("role") != anchor.get("base_last_role"):
return False
fp = anchor.get("base_last_fp")
- return isinstance(fp, str) and bool(fp) and message_fingerprint(base_msg) == fp
+ if not isinstance(fp, str) or not fp or message_fingerprint(base_msg) != fp:
+ return False
+ prefix_fp = anchor.get("base_prefix_fp")
+ return (
+ isinstance(prefix_fp, str)
+ and bool(prefix_fp)
+ and _priced_prefix_fingerprint(messages, base_count) == prefix_fp
+ )
def anchored_context_tokens(messages: List[Dict[str, Any]], anchor: Optional[Dict[str, Any]], *, charge_stale_thinking: bool = True) -> Optional[int]:
@@ -101,10 +130,19 @@ def _serialize(anchor: Any) -> Optional[Dict[str, Any]]:
except (TypeError, ValueError):
return None
fp, role = anchor.get("base_last_fp"), anchor.get("base_last_role")
- if pt <= 0 or base_count <= 0 or not isinstance(fp, str) or not fp:
+ prefix_fp = anchor.get("base_prefix_fp")
+ if (
+ pt <= 0
+ or base_count <= 0
+ or not isinstance(fp, str)
+ or not fp
+ or not isinstance(prefix_fp, str)
+ or not prefix_fp
+ ):
return None
return {"prompt_tokens": pt, "completion_tokens": max(0, ct), "base_count": base_count,
- "base_last_role": role if isinstance(role, str) else None, "base_last_fp": fp}
+ "base_last_role": role if isinstance(role, str) else None, "base_last_fp": fp,
+ "base_prefix_fp": prefix_fp}
def persist_usage_anchor(agent: Any, anchor: Optional[Dict[str, Any]]) -> None:
diff --git a/tests/agent/test_usage_anchor.py b/tests/agent/test_usage_anchor.py
index 12ac3585a63..4e6230ca2f2 100644
--- a/tests/agent/test_usage_anchor.py
+++ b/tests/agent/test_usage_anchor.py
@@ -132,6 +132,22 @@ class TestAnchorInvalidation:
spliced = messages[:1] + [_msg("assistant", "[marker]")] + messages[5:]
assert anchored_context_tokens(spliced, anchor) is None
+ def test_prefix_rewrite_preserving_base_message_fails_closed(self):
+ messages = _plain_history()
+ anchor = capture_usage_anchor(30_000, 50, messages)
+ # A stale anchor can otherwise survive compaction when the single
+ # checked row at base_count - 1 lands in the protected tail. The old
+ # prefix no longer matches the provider-priced request, even though the
+ # base row still has the same role/content fingerprint.
+ compacted = [
+ _msg("user", "summary handoff"),
+ _msg("assistant", "[compressed summary]"),
+ _msg("user", "new follow-up"),
+ dict(messages[-1]),
+ ]
+ compacted.append(_msg("user", "post-anchor text must not be added to the stale base"))
+ assert anchored_context_tokens(compacted, anchor) is None
+
def test_reloaded_transcript_with_same_content_still_matches(self):
"""The gateway re-reads history from the DB every turn (fresh dicts, extra
persistence keys); identity must survive that or every gateway turn falls