Skip to content

The agent that grows with you

fix(compression): preserve terminal lifecycle for lock skips

Uttkarsh Tiwari36ae32524c72
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index f2a77bbe249..d8b4920ed87 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -2496,15 +2496,18 @@ def compress_context(
_compaction_done_emitted = False
_compaction_succeeded = False
- def _complete_compaction_lifecycle() -> None:
+ def _complete_compaction_lifecycle(*, force_terminal: bool = False) -> None:
nonlocal _compaction_done_emitted
if _compaction_done_emitted:
return
_compaction_done_emitted = True
# A suppressed start (quiet context engine) opened no visible
# compaction phase — emit no terminal edge either. Failure warnings
- # go through agent._emit_warning and are never suppressed here.
- if _compaction_status_emitted and _compaction_succeeded:
+ # go through agent._emit_warning and are never suppressed here. A lock
+ # contender is the one exception: it did not compact, but still needs
+ # the structured terminal edge so clients can retire their compaction
+ # phase. Chat surfaces filter this routine notice independently.
+ if _compaction_status_emitted and (_compaction_succeeded or force_terminal):
_emit_compaction_done(agent)
# ── Compression lock ────────────────────────────────────────────────
@@ -2725,7 +2728,7 @@ def compress_context(
split_status="aborted",
failure_class="lock_contended",
)
- _complete_compaction_lifecycle()
+ _complete_compaction_lifecycle(force_terminal=True)
return messages, _existing_sp
_lock_released = False
_lock_release_guard = threading.Lock()
@@ -4112,7 +4115,7 @@ def compress_context(
else None
),
)
- _compaction_succeeded = True
+ _compaction_succeeded = _commit_status == "committed"
return compressed, new_system_prompt
finally:
# Release the lock on the OLD session_id only AFTER rotation completed
diff --git a/tests/run_agent/test_413_compression.py b/tests/run_agent/test_413_compression.py
index fcedeca0e17..ad9bfcfca48 100644
--- a/tests/run_agent/test_413_compression.py
+++ b/tests/run_agent/test_413_compression.py
@@ -508,6 +508,25 @@ class TestPreflightCompression:
assert [event for event, _ in events] == ["lifecycle", "warn", "compacted"]
assert events[-1] == ("compacted", COMPACTION_DONE_STATUS)
+ def test_compress_context_does_not_emit_completion_after_an_abort(self, agent):
+ """An aborted summary must not claim that compaction completed."""
+ agent.compression_enabled = False
+ events = []
+ agent.status_callback = lambda event, message: events.append((event, message))
+ messages = [{"role": "user", "content": "hello"}]
+
+ def _abort_compression(current_messages, **_kwargs):
+ agent.context_compressor._last_compress_aborted = True
+ agent.context_compressor._last_summary_error = "auxiliary model unavailable"
+ return current_messages
+
+ with patch.object(agent.context_compressor, "compress", side_effect=_abort_compression):
+ compressed, prompt = agent._compress_context(messages, "system prompt", force=True)
+
+ assert compressed is messages
+ assert prompt == "You are helpful."
+ assert [event for event, _ in events] == ["lifecycle", "warn"]
+ assert ("compacted", COMPACTION_DONE_STATUS) not in events
def test_compression_reuses_cached_prompt_when_memory_snapshot_is_unchanged(self, agent):
"""A memory reload without new injected text must keep the cache prefix."""