Skip to content

The agent that grows with you

fix(cache): derive the xAI fork scope only after the review fork compacts

kshitijk4poor1df043141825
diff --git a/agent/background_review.py b/agent/background_review.py
index 8f6aa4f6c41..3b741ca882a 100644
--- a/agent/background_review.py
+++ b/agent/background_review.py
@@ -998,12 +998,6 @@ def build_cache_parity_fork(
review_agent._end_session_on_close = False
review_agent._session_db = None
review_agent.session_id = agent.session_id
- # Slot-keyed caches (xAI) must not see the fork under the parent's key: the fork's divergent
- # stream evicts the parent's conversation slot. The resolver derives ``<scope>::<tag>`` for
- # slot-keyed providers only; content-addressed ones keep the shared scope below.
- review_agent._prompt_cache_fork_tag = (
- "review" if write_origin == "background_review" else str(write_origin or "fork")
- )
# Same model only: share the warm cached system prompt (~26% cost cut; a rebuilt prompt misses
# the byte-exact prefix key) and pin session_start so any re-render (compression, plugin
# hooks) stays byte-identical.
@@ -1029,6 +1023,11 @@ def build_cache_parity_fork(
inherited_scope = resolve_prompt_cache_scope_safe(agent)
if inherited_scope:
review_agent._inherited_cache_scope = inherited_scope
+ # Slot-keyed caches (xAI): once the review's OWN compaction rewrites its transcript, its
+ # divergent stream would evict the parent's server slot, so the resolver then derives
+ # ``<scope>::review``. /btw never tags: one prefix-extension call cannot diverge.
+ if write_origin == "background_review":
+ review_agent._prompt_cache_fork_tag = "review"
# Same reason for the Portal ``conversation=`` tag: with no DB the fork's own
# _conversation_root_id() falls back to the parent's PHYSICAL id, so after a compression
# rotation the review's usage was attributed to a different conversation than its parent.
diff --git a/agent/prompt_cache_scope.py b/agent/prompt_cache_scope.py
index b3c27e621f2..ce3b9a3ae66 100644
--- a/agent/prompt_cache_scope.py
+++ b/agent/prompt_cache_scope.py
@@ -181,7 +181,8 @@ def is_slot_keyed_cache_route(provider: Any, model: Any, base_url: Any = "") ->
prompt cache to the server picked by ``x-grok-conv-id`` / ``prompt_cache_key``: two divergent
request streams under one key evict each other. Anthropic, DeepSeek and Gemini caches are
content-addressed and OpenAI's ``prompt_cache_key`` only routes over a prefix match, so the
- shared scope (#109964) stays a win there.
+ shared scope (#109964) stays a win there. Only the OpenRouter profile reads ``cache_scope_id``
+ for the sticky header; other aggregators fronting Grok change only the body key.
"""
if str(provider or "").strip().lower() in {"xai", "xai-oauth"}:
return True
@@ -196,17 +197,22 @@ def is_fork_cache_scope(scope: Any) -> bool:
def _apply_fork_tag(agent: Any, scope: str) -> str:
- """Derive ``<scope>::<tag>`` for a tagged cache-parity fork on a slot-keyed provider.
-
- A same-model fork shares the parent's scope (#109964) so content-addressed caches serve it
- warm. On xAI that key makes the fork's divergent stream evict the parent's conversation slot:
- measured on grok-4.7 at ~160k context with a same-size fork, the parent's next call read
- 1,152 of 162k prompt tokens (2/2) vs 162,176 of 162k (2/2) with a derived key. Evaluated per
- call (no DB), so a mid-run provider fallback re-evaluates.
+ """Derive ``<scope>::<tag>`` for a tagged cache-parity fork on a slot-keyed provider, but only
+ after the fork's own compaction committed.
+
+ A same-model fork shares the parent's scope (#109964): until it compacts, its requests are a
+ strict prefix extension of the parent's, so request #1 is a warm read and cannot evict. Once
+ its in-place compaction rewrites the transcript, on xAI the divergent stream evicts the
+ parent's conversation slot (grok-4.7, ~160k: the parent's next call read 1,152 of 162k prompt
+ tokens, 2/2, vs 162,176 with a derived key). ``compression_count`` is the fork's own counter
+ (rolled back on aborted attempts). Evaluated per call, so a provider fallback re-evaluates.
"""
tag = getattr(agent, "_prompt_cache_fork_tag", None)
if not scope or not isinstance(tag, str) or not tag:
return scope
+ compactions = getattr(getattr(agent, "context_compressor", None), "compression_count", 0)
+ if not isinstance(compactions, int) or compactions < 1:
+ return scope
if not is_slot_keyed_cache_route(
getattr(agent, "provider", ""), getattr(agent, "model", ""), getattr(agent, "base_url", ""),
):