Skip to content

The agent that grows with you

fix(compression): a compressor no attempt claimed is never judged stale

Eva71cca15adac7
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index df43d4101b2..34c0eac45c2 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -297,13 +297,15 @@ def _working_attempt_is_current(compressor: Any, generation: Any) -> bool:
"""True when *generation* is still the last attempt that began summary work.
Without a published marker (attribute-less compressor, or the attempt never reached
- dispatch) supersession falls back to the entry-generation ownership check."""
+ dispatch) supersession falls back to the entry-generation ownership check; a compressor
+ that no attempt has ever claimed cannot have been superseded."""
if not generation:
return True
with _COMPRESSOR_ATTEMPT_LOCK:
marker = getattr(compressor, "_compression_working_attempt_generation", None)
if marker is None:
- return int(getattr(compressor, "_compression_attempt_generation", 0) or 0) == generation
+ entry_generation = int(getattr(compressor, "_compression_attempt_generation", 0) or 0)
+ return not entry_generation or entry_generation == generation
return int(marker) == int(generation)
diff --git a/tests/agent/test_compression_attempt_ownership.py b/tests/agent/test_compression_attempt_ownership.py
index 4d6a68d04d1..ceae2803f85 100644
--- a/tests/agent/test_compression_attempt_ownership.py
+++ b/tests/agent/test_compression_attempt_ownership.py
@@ -24,10 +24,14 @@ no timing, no threads.
from types import SimpleNamespace
from agent.conversation_compression import (
+ _COMPRESSOR_ATTEMPT_GENERATION,
+ _caller_attempt_is_current,
_claim_compressor_attempt,
_clear_compression_cancelled_check_if_owner,
_compressor_attempt_is_current,
_install_compression_cancelled_check,
+ _mark_compressor_working_attempt,
+ _raise_if_stale_attempt,
_restore_compressor_attempt_state,
_snapshot_compressor_attempt_state,
)
@@ -45,6 +49,58 @@ def _compressor(**overrides):
return SimpleNamespace(**base)
+class TestCallerAttemptOwnership:
+ """The caller generation only fences a compressor that has been claimed."""
+
+ def test_never_claimed_compressor_is_current_for_outer_attempt(self):
+ compressor = _compressor()
+ token = _COMPRESSOR_ATTEMPT_GENERATION.set(7)
+ try:
+ assert _caller_attempt_is_current(compressor) is True
+ _raise_if_stale_attempt(compressor)
+ finally:
+ _COMPRESSOR_ATTEMPT_GENERATION.reset(token)
+
+ def test_newer_working_attempt_is_stale_for_claimed_compressor(self):
+ from agent.auxiliary_client import AuxiliaryExplicitCancellation
+
+ compressor = _compressor()
+ caller_gen = _claim_compressor_attempt(compressor)
+ newer_gen = _claim_compressor_attempt(compressor)
+ _mark_compressor_working_attempt(compressor, newer_gen)
+
+ token = _COMPRESSOR_ATTEMPT_GENERATION.set(caller_gen)
+ try:
+ assert _caller_attempt_is_current(compressor) is False
+ try:
+ _raise_if_stale_attempt(compressor)
+ except AuxiliaryExplicitCancellation:
+ pass
+ else:
+ raise AssertionError("stale claimed compressor was not cancelled")
+ finally:
+ _COMPRESSOR_ATTEMPT_GENERATION.reset(token)
+
+ def test_newer_entry_generation_without_marker_remains_stale(self):
+ from agent.auxiliary_client import AuxiliaryExplicitCancellation
+
+ compressor = _compressor()
+ caller_gen = _claim_compressor_attempt(compressor)
+ _claim_compressor_attempt(compressor)
+
+ token = _COMPRESSOR_ATTEMPT_GENERATION.set(caller_gen)
+ try:
+ assert _caller_attempt_is_current(compressor) is False
+ try:
+ _raise_if_stale_attempt(compressor)
+ except AuxiliaryExplicitCancellation:
+ pass
+ else:
+ raise AssertionError("stale entry generation was not cancelled")
+ finally:
+ _COMPRESSOR_ATTEMPT_GENERATION.reset(token)
+
+
class TestLatePrimaryRestoreAfterFallbackCommit:
"""Claim 1: a stale attempt's snapshot restore must no-op."""
@@ -280,6 +336,35 @@ class TestStaleAttemptEndToEnd:
resp.choices[0].message.content = content
return resp
+ def test_outer_attempt_can_use_never_claimed_inner_compressor(self):
+ from unittest.mock import patch
+
+ from agent.conversation_compression import _run_summary_dispatch
+
+ inner = self._compressor()
+
+ class DelegatingEngine:
+ def compress(self, messages, **kwargs):
+ return inner.compress(messages, **kwargs)
+
+ engine = DelegatingEngine()
+ agent = SimpleNamespace(context_compressor=engine, session_id="s1")
+ messages = self._messages()
+ generation = _claim_compressor_attempt(engine)
+
+ with patch(
+ "agent.context_compressor.call_llm",
+ return_value=self._llm_response("## Goal\ndelegated summary"),
+ ):
+ compressed = _run_summary_dispatch(
+ agent, messages, engine.compress,
+ {"current_tokens": 999999, "force": True},
+ commit_fence=None, attempt_generation=generation, hard_cancel_event=None,
+ )
+
+ assert compressed != messages
+ assert inner._previous_summary and "delegated summary" in inner._previous_summary
+
def test_detached_primary_late_success_cannot_write_after_fallback(self):
import threading
from unittest.mock import patch