The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): cancel a prepared pass nothing can use any more
kshitijk4poor7558b1c41278
diff --git a/agent/prepared_compaction.py b/agent/prepared_compaction.pyindex 661e241af1a..2d7399099f6 100644--- a/agent/prepared_compaction.py+++ b/agent/prepared_compaction.py@@ -21,10 +21,10 @@ When no completed candidate fits but a running pass would, compaction waits forthat pass instead of starting the same summary over: the pass's streamedprogress and local prompt reading feed the waiting compaction's hooks, so thehost's idle watchdog and deadline extension see a live summary exactly as they-would inline, and the wait ends at-the host's deadline (the compression aux timeout without one) or on-cancellation. A pass that is not waited on loses the right to publish once-compaction runs.+would inline, and the wait ends at the host's deadline (the compression aux+timeout without one), on cancellation, or when the pass is discarded. A pass+that compaction or a session boundary supersedes is cancelled: its summary+stream is closed instead of being billed to completion for nothing.State is per compressor and in memory only; every session boundary (reset,end, or a rebind to another session id) discards it. Manual, focus,@@ -79,12 +79,14 @@ class PreparedSummary(_Window):class _PendingPass:- """The one running pass allowed to publish: its planned window, a completion event, and the- progress and prompt-reading hooks of a compaction waiting on it."""+ """The one running pass allowed to publish: its planned window, completion and cancel events, and+ the progress and prompt-reading hooks of a compaction waiting on it."""def __init__(self, window: _Window) -> None:self.window = windowself.done = threading.Event()+ # Set once nothing can use the pass any more; its summary stream is closed (the aux hard-cancel path).+ self.cancelled = threading.Event()self.on_progress: Optional[Callable[[], None]] = Noneself.on_reading: Optional[Callable[[float], None]] = None@@ -176,9 +178,11 @@ class PreparedCompaction:self._retry_after = 0.0 # monotonic; set by a failed passdef discard(self, reason: str) -> None:- """Drop the candidate; a running pass may finish but can no longer publish."""+ """Drop the candidate and cancel a running pass (it can no longer publish)."""with self._lock:- entry, self._entry, self._pending = self._entry, None, None+ entry, pending, self._entry, self._pending = self._entry, self._pending, None, None+ if pending is not None:+ pending.cancelled.set()if entry is not None:logger.info("prepared compaction: dropped window=%d-%d reason=%s", entry.compress_start, entry.compress_end, reason,@@ -190,8 +194,8 @@ class PreparedCompaction:) -> Optional[PreparedSummary]:"""The completed candidate when it fits ``compress()``'s planned window, else what a running passwhose window fits publishes while this compaction waits for it, else None. A window whose boundary- trails the cut by more than ``max_gap_tokens`` is stale. Either way the state is cleared: this- compaction consumes or supersedes it."""+ trails the cut by more than ``max_gap_tokens`` is stale. Either way the state is cleared and a+ running pass is cancelled: this compaction consumes or supersedes it."""with self._lock:entry, pending = self._entry, self._pendingself._entry = None@@ -201,14 +205,15 @@ class PreparedCompaction:try:if eligible and (entry is None or reason is not None):if pending is not None and self._misfit(pending.window, messages, session_id, start, end, max_gap_tokens) is None:- entry = self._await(pending)- reason = None if entry is not None else "running pass did not finish in time"+ entry, reason = self._await(pending)elif entry is None:reason = "running pass does not fit" if pending is not None else "no candidate"finally:with self._lock:if self._pending is pending:self._pending = None+ if pending is not None:+ pending.cancelled.set() # done already, or superseded: stop paying for itif reason is not None or entry is None:if entry is not None or pending is not None:logger.info("prepared compaction: splice miss reason=%s", reason)@@ -232,9 +237,10 @@ class PreparedCompaction:reason = f"stale candidate (~{gap} tokens past its boundary, budget {max_gap_tokens})"return reason- def _await(self, pending: _PendingPass) -> Optional[PreparedSummary]:- """Wait on the compacting thread for *pending* and return what it published; None at the host's- deadline (the compression aux timeout without one) or when the host cancels. Meanwhile the pass's+ def _await(self, pending: _PendingPass) -> tuple[Optional[PreparedSummary], Optional[str]]:+ """Wait on the compacting thread for *pending*: what it published, or None and why (the host's+ deadline, the compression aux timeout without one, a host cancel, a discard, a failed pass).+ Meanwhile the pass'sstreamed progress and local prompt reading feed this thread's hooks, so the host's idle watchdogand deadline extension see a live summary exactly as they would inline; the deadline is re-readon every poll because prompt reading pushes it back."""@@ -244,10 +250,11 @@ class PreparedCompaction:)from agent.local_prompt_progress import current_prompt_reading_hook- cancel_check = _capture_aux_cancel_check()- fallback = time.monotonic() + float(_effective_aux_timeout("compression", None))+ # Hooks first: the pass may stream while the rest of the setup runs (the timeout loads config).pending.on_progress = getattr(_aux_progress, "hook", None)pending.on_reading = current_prompt_reading_hook()+ cancel_check = _capture_aux_cancel_check()+ fallback = time.monotonic() + float(_effective_aux_timeout("compression", None))logger.info("prepared compaction: waiting for the running pass window=%d-%d",pending.window.compress_start, pending.window.compress_end,@@ -256,15 +263,21 @@ class PreparedCompaction:while not pending.done.wait(max(0.0, min(_WAIT_POLL_SECONDS, (_current_aux_stream_deadline() or fallback) - time.monotonic())),):- if time.monotonic() >= (_current_aux_stream_deadline() or fallback) or (- cancel_check is not None and _captured_aux_cancel_requested(cancel_check)- ):- return None+ if pending.cancelled.is_set():+ return None, "running pass discarded"+ if cancel_check is not None and _captured_aux_cancel_requested(cancel_check):+ return None, "host cancelled the wait"+ if time.monotonic() >= (_current_aux_stream_deadline() or fallback):+ return None, "running pass did not finish in time"finally:pending.on_progress = pending.on_reading = None+ if pending.cancelled.is_set():+ return None, "running pass discarded"with self._lock:entry, self._entry = self._entry, None- return entry if entry is not None and entry.fingerprint == pending.window.fingerprint else None+ if entry is None or entry.fingerprint != pending.window.fingerprint:+ return None, "running pass failed"+ return entry, Nonedef _blocked(self, agent: Any, compressor: Any) -> bool:if not getattr(agent, "compression_enabled", False) or getattr(agent, "_persist_disabled", False):@@ -354,7 +367,7 @@ class PreparedCompaction:self, worker: Any, turns: list[dict[str, Any]], focus: Optional[str], token: _PendingPass, origin: str,extend: Optional[PreparedSummary],) -> None:- from agent.auxiliary_client import aux_progress_hook+ from agent.auxiliary_client import aux_interrupt_protection, aux_progress_hookfrom agent.local_prompt_progress import prompt_reading_hookfrom agent.thread_scoped_output import thread_scoped_silence@@ -366,9 +379,14 @@ class PreparedCompaction:"prepared compaction: pass started origin=%s window=%d-%d turns=%d extends=%s",origin, start, end, len(turns), extend is not None,)- with thread_scoped_silence(), aux_progress_hook(token.touch), prompt_reading_hook(token.read):+ with (+ thread_scoped_silence(), aux_interrupt_protection(cancel_event=token.cancelled),+ aux_progress_hook(token.touch), prompt_reading_hook(token.read),+ ):summary = worker._generate_summary(turns, focus_topic=focus)- if summary:+ if token.cancelled.is_set():+ logger.info("prepared compaction: pass cancelled after %.1fs (superseded)", time.monotonic() - started)+ elif summary:entry = PreparedSummary(start, end, window_fingerprint, summary, worker._previous_summary)logger.info("prepared compaction: pass completed window=%d-%d in %.1fs",start, end, time.monotonic() - started)diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.pyindex f07b764367f..b21e1ca5202 100644--- a/hermes_cli/config_defaults.py+++ b/hermes_cli/config_defaults.py@@ -691,7 +691,7 @@ DEFAULT_CONFIG = {# front, before the first reply. Time-based complement to `threshold`; skipped when already at/below# threshold × target_ratio; honors the same cooldown/anti-thrash/lock guards. Example: 1800 = 30 min."idle_compact_after_seconds": 0,- # Opt-in: write the next summary in the background near the trigger; used only if the summarized turns are unchanged.+ # Opt-in: write the next summary in the background near the trigger; compaction uses (or waits for) it if those turns are unchanged."prepare_ahead": False,},# Anthropic prompt caching (Claude via OpenRouter or native API). cache_ttl: "5m" | "1h" | "auto"diff --git a/tests/agent/test_prepared_compaction.py b/tests/agent/test_prepared_compaction.pyindex 8ee0d2d9914..c5e3c3ca34b 100644--- a/tests/agent/test_prepared_compaction.py+++ b/tests/agent/test_prepared_compaction.py@@ -411,6 +411,46 @@ def test_prompt_reading_in_the_pass_extends_the_deadline_the_wait_honours():assert "Summary v1" in _summary_row(compressed)["content"]+def test_host_cancel_ends_the_wait_and_cancels_the_superseded_pass():+ from agent.auxiliary_client import aux_interrupt_protection, aux_progress_hook++ cc = _compressor()+ cc.gate = threading.Event()+ msgs = _messages(30)+ running = pc.maybe_prepare(_agent(cc), msgs, origin="test")+ assert running is not None+ pending, host_cancel = cc.prepared_compaction._pending, threading.Event()+ threading.Thread(target=lambda: wait_until(lambda: pending.on_progress is not None)+ and host_cancel.set()).start()+ with aux_interrupt_protection(cancel_event=host_cancel), aux_progress_hook(lambda: None):+ cc.compress(msgs, current_tokens=cc.threshold_tokens + 1)+ assert running.is_alive(), "the wait ended on the host cancel, not with the pass"+ assert [c["worker"] for c in cc.calls] == [False], "inline summary after the host cancelled the wait"+ assert pending.cancelled.is_set(), "the pass nobody can use is told to stop streaming"+ cc.gate.set()+ running.join(5.0)+++def test_session_reset_during_the_wait_wakes_it_without_waiting_for_the_pass():+ from agent.auxiliary_client import aux_progress_hook++ cc = _compressor()+ cc.gate = threading.Event()+ msgs = _messages(30)+ running = pc.maybe_prepare(_agent(cc), msgs, origin="test")+ assert running is not None+ pending = cc.prepared_compaction._pending+ threading.Thread(target=lambda: wait_until(lambda: pending.on_progress is not None)+ and cc.prepared_compaction.discard("session reset")).start()+ with aux_progress_hook(lambda: None): # a host hook marks the moment the wait starts+ cc.compress(msgs, current_tokens=cc.threshold_tokens + 1)+ assert running.is_alive(), "the wait ended on the discard, not with the pass"+ assert [c["worker"] for c in cc.calls] == [False]+ cc.gate.set()+ running.join(5.0)+ assert cc.prepared_compaction._entry is None++@pytest.mark.parametrize("boundary", ["reset", "end", "rebind"])def test_session_boundary_discards_candidate_and_running_pass(boundary):cc = _compressor()diff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.mdindex 1c5c0733480..6012e4e82f9 100644--- a/website/docs/developer-guide/context-compression-and-caching.md+++ b/website/docs/developer-guide/context-compression-and-caching.md@@ -701,8 +701,8 @@ cancellation, and compaction then summarizes inline.Everything else runs inline exactly as without the flag: no candidate, a candidate or running passthat no longer fits, manual `/compress`, focus compaction, provider-overflow recovery, andcompactions where a memory provider contributes summary context. A running pass overtaken by compaction or a session-boundary (including a rebind to another session id on `/resume` or `/branch`) cannot publish its-result. A failed pass pauses further passes but never arms the live compressor's failure cooldown. State is per compressor and in memory only, so a gateway agent+boundary (including a rebind to another session id on `/resume` or `/branch`) is cancelled: it+cannot publish, and its summary stream is closed rather than billed to completion. A failed pass pauses further passes but never arms the live compressor's failure cooldown. State is per compressor and in memory only, so a gateway agenteviction or process restart simply means the next compaction runs inline. Plugin context engines,Codex app-server sessions, native Responses compaction and micro-compaction are not prepared.