The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: trim stale-attempt ownership tests to the end-to-end invariants
teknium121b665f42fd0
diff --git a/tests/agent/test_compression_attempt_ownership.py b/tests/agent/test_compression_attempt_ownership.pyindex f53a1bff455..b86d12819a8 100644--- a/tests/agent/test_compression_attempt_ownership.py+++ b/tests/agent/test_compression_attempt_ownership.py@@ -259,231 +259,6 @@ def _run_as_attempt(generation, fn):var.reset(token)-class TestSummarizeWindowCancelRollbackOwnership:- """Gap in the generation guard: ``_summarize_window`` restored- ``_previous_summary`` / ``_summary_has_user_turn`` unconditionally on- ``AuxiliaryExplicitCancellation``, so a detached primary's late cancel- reverted state a newer fallback attempt already advanced."""-- def _compressor_with_window(self):- from agent.context_compressor_summary import SummaryDispatchMixin- from agent.auxiliary_client import AuxiliaryExplicitCancellation-- cc = SummaryDispatchMixin.__new__(SummaryDispatchMixin)- cc._previous_summary = "fallback-advanced"- cc._summary_has_user_turn = True- cc._last_summary_error = None- cc._active_compression_telemetry = None-- def _generate_summary(turns, **kw):- raise AuxiliaryExplicitCancellation()-- cc._generate_summary = _generate_summary- return cc-- def _scan(self):- return SimpleNamespace(previous_summary_before="primary-era", has_user_turn_before=False)-- def _cancel(self, cc):- from agent.auxiliary_client import AuxiliaryExplicitCancellation-- try:- cc._summarize_window([], [], self._scan(), "focus", "", False)- raise AssertionError("expected AuxiliaryExplicitCancellation")- except AuxiliaryExplicitCancellation:- pass-- def test_stale_attempt_cancel_does_not_revert_fallback_state(self):- cc = self._compressor_with_window()- # The real stall-fallback interleaving: attempt 1 detached, fallback- # attempt 2 claimed AND published the working marker, then advanced- # summary state. Attempt 1's late cancel unwinds after all of it.- cc._compression_attempt_generation = 2- cc._compression_working_attempt_generation = 2-- _run_as_attempt(1, lambda: self._cancel(cc))-- assert cc._previous_summary == "fallback-advanced"- assert cc._summary_has_user_turn is True-- def test_current_attempt_cancel_restores_scan_mutation(self):- """Control: the owning attempt's cancel still rolls back the scan's- self-heal mutation so the cancellation stays a true no-op."""- cc = self._compressor_with_window()- cc._compression_attempt_generation = 1- cc._compression_working_attempt_generation = 1-- _run_as_attempt(1, lambda: self._cancel(cc))-- assert cc._previous_summary == "primary-era"- assert cc._summary_has_user_turn is False-- def test_noop_entry_claim_does_not_suppress_owning_rollback(self):- """A lock sit-out bumps the entry generation without working the- compressor; the working attempt's rollback must still fire."""- cc = self._compressor_with_window()- cc._compression_attempt_generation = 2- cc._compression_working_attempt_generation = 1-- _run_as_attempt(1, lambda: self._cancel(cc))-- assert cc._previous_summary == "primary-era"- assert cc._summary_has_user_turn is False-- def test_unguarded_path_restores_normally(self):- """Direct compress() calls outside the attempt machinery carry no- generation; the rollback must not be suppressed there."""- cc = self._compressor_with_window()-- self._cancel(cc)-- assert cc._previous_summary == "primary-era"---class TestStaleAttemptCompletionWrites:- """The same stale-attempt class at summary-completion time: a detached- primary's late success or failure must not publish summary state, arm a- failure cooldown, or roll back the fallback's ``_previous_summary``.- Guards read the caller's own generation from the dispatch ContextVar and- compare it against the working-attempt marker."""-- def _compressor(self):- from unittest.mock import patch-- from agent.context_compressor import ContextCompressor-- with patch(- "agent.context_compressor.get_model_context_length",- return_value=100000,- ):- cc = ContextCompressor(model="test/model", quiet_mode=True)- return cc-- def _llm_response(self, content):- from unittest.mock import MagicMock-- resp = MagicMock()- resp.choices = [MagicMock()]- resp.choices[0].message.content = content- return resp-- def _own(self, cc, entry=2, working=2):- """Shape the compressor as owned by the fallback (attempt 2)."""- cc._compression_attempt_generation = entry- cc._compression_working_attempt_generation = working- cc._previous_summary = "fallback-advanced"- return cc-- def test_stale_attempt_late_success_does_not_publish_summary(self):- from unittest.mock import patch-- import pytest-- from agent.auxiliary_client import AuxiliaryExplicitCancellation-- cc = self._own(self._compressor())-- def _call():- with patch(- "agent.context_compressor.call_llm",- return_value=self._llm_response("## Goal\nstale-era summary"),- ):- cc._generate_summary([{"role": "user", "content": "hi"}])-- with pytest.raises(AuxiliaryExplicitCancellation):- _run_as_attempt(1, _call)-- assert cc._previous_summary == "fallback-advanced"- assert cc._last_summary_error is None-- def test_current_attempt_success_publishes_summary(self):- """Control: the working attempt's identical success still publishes."""- from unittest.mock import patch-- cc = self._compressor()- cc._compression_attempt_generation = 2- cc._compression_working_attempt_generation = 2-- def _call():- with patch(- "agent.context_compressor.call_llm",- return_value=self._llm_response("## Goal\nfresh summary"),- ):- return cc._generate_summary([{"role": "user", "content": "hi"}])-- result = _run_as_attempt(2, _call)-- assert result is not None- assert "fresh summary" in cc._previous_summary-- def test_stale_attempt_failure_arms_no_cooldown(self):- from unittest.mock import patch-- import pytest-- from agent.auxiliary_client import AuxiliaryExplicitCancellation-- cc = self._own(self._compressor())- cc._last_summary_error = "fallback-recorded"-- def _call():- with patch(- "agent.context_compressor.call_llm",- side_effect=ValueError("stale-era transport failure"),- ):- cc._generate_summary([{"role": "user", "content": "hi"}])-- with pytest.raises(AuxiliaryExplicitCancellation):- _run_as_attempt(1, _call)-- assert cc._summary_failure_cooldown_until == 0.0- assert cc._last_summary_error == "fallback-recorded"-- def test_unguarded_failure_still_records_cooldown(self):- """Control: failures outside the attempt machinery keep legacy- cooldown recording (no generation means no guard)."""- from unittest.mock import patch-- cc = self._compressor()-- with patch(- "agent.context_compressor.call_llm",- side_effect=ValueError("transport failure"),- ):- cc._generate_summary([{"role": "user", "content": "hi"}])-- assert cc._summary_failure_cooldown_until > 0.0-- def test_stale_attempt_abort_does_not_restore_previous_summary(self):- """The deterministic-pin path reaches the abort rollback without a- cancellation; a stale attempt must not revert the fallback's state."""- cc = self._own(self._compressor())- cc.abort_on_summary_failure = True-- aborted = _run_as_attempt(- 1, lambda: cc._abort_on_summary_failure({}, 5, "primary-era")- )-- assert aborted is True- assert cc._previous_summary == "fallback-advanced"-- def test_current_attempt_abort_restores_previous_summary(self):- """Control: the owning attempt's abort still rolls back the scan's- self-heal rehydration (#57835)."""- cc = self._compressor()- cc.abort_on_summary_failure = True- cc._compression_attempt_generation = 1- cc._compression_working_attempt_generation = 1- cc._previous_summary = "scan-rehydrated"-- aborted = _run_as_attempt(- 1, lambda: cc._abort_on_summary_failure({}, 5, "pre-scan")- )-- assert aborted is True- assert cc._previous_summary == "pre-scan"--class TestStaleAttemptEndToEnd:"""E2E: two real attempts through ``_run_summary_dispatch`` on a sharedreal ``ContextCompressor``. The detached primary's late completion must