Skip to content

The agent that grows with you

refactor(compression): fold review follow-ups on #71488 salvage

kshitijk4poorc693c772b55b
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index d8b4920ed87..5a0d191069f 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -2494,7 +2494,11 @@ def compress_context(
if _compaction_status:
agent._emit_status(_compaction_status)
_compaction_done_emitted = False
- _compaction_succeeded = False
+ # Commit outcome of this attempt; rebound to "committed" on the success
+ # path just before returning the compressed history. The lifecycle
+ # closure reads it at call time, so any abort/exception path that skips
+ # that rebind keeps the terminal edge suppressed.
+ _commit_status = "aborted"
def _complete_compaction_lifecycle(*, force_terminal: bool = False) -> None:
nonlocal _compaction_done_emitted
@@ -2503,11 +2507,14 @@ def compress_context(
_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. 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):
+ # go through agent._emit_warning and are never suppressed here.
+ # Aborts that never compacted (lock contender, cancelled commit
+ # fence) opt in via force_terminal: they still need the structured
+ # terminal edge so clients can retire their compaction phase. Chat
+ # surfaces filter this routine notice independently.
+ if _compaction_status_emitted and (
+ _commit_status == "committed" or force_terminal
+ ):
_emit_compaction_done(agent)
# ── Compression lock ────────────────────────────────────────────────
@@ -2636,7 +2643,7 @@ def compress_context(
split_status="aborted",
failure_class="commit_fence_cancelled",
)
- _complete_compaction_lifecycle()
+ _complete_compaction_lifecycle(force_terminal=True)
return messages, _existing_sp
try:
_lock_acquired = _try_acquire_lock(
@@ -4115,7 +4122,6 @@ def compress_context(
else None
),
)
- _compaction_succeeded = _commit_status == "committed"
return compressed, new_system_prompt
finally:
# Release the lock on the OLD session_id only AFTER rotation completed
@@ -4190,17 +4196,6 @@ def _compress_context_via_codex_app_server(
except Exception:
pass
- _compaction_done_emitted = False
- _compaction_succeeded = False
-
- def _complete_compaction_lifecycle() -> None:
- nonlocal _compaction_done_emitted
- if _compaction_done_emitted:
- return
- _compaction_done_emitted = True
- if _compaction_succeeded:
- _emit_compaction_done(agent)
-
_activity_heartbeat: Optional[_CompressionActivityHeartbeat] = None
try:
_activity_heartbeat = _CompressionActivityHeartbeat(agent).start()
@@ -4208,7 +4203,6 @@ def _compress_context_via_codex_app_server(
except BaseException:
if _activity_heartbeat is not None:
_activity_heartbeat.stop("context compression failed")
- _complete_compaction_lifecycle()
raise
if getattr(result, "interrupted", False) or getattr(result, "error", None):
@@ -4233,7 +4227,6 @@ def _compress_context_via_codex_app_server(
existing_prompt = getattr(agent, "_cached_system_prompt", None)
if not existing_prompt:
existing_prompt = agent._build_system_prompt(system_message)
- _complete_compaction_lifecycle()
return messages, existing_prompt
try:
@@ -4273,8 +4266,9 @@ def _compress_context_via_codex_app_server(
existing_prompt = getattr(agent, "_cached_system_prompt", None)
if not existing_prompt:
existing_prompt = agent._build_system_prompt(system_message)
- _compaction_succeeded = True
- _complete_compaction_lifecycle()
+ # Terminal edge only on success — failure/interrupt paths above return
+ # without it, matching the main compress_context() gating.
+ _emit_compaction_done(agent)
return messages, existing_prompt