Skip to content

The agent that grows with you

fix(compression): end cancelled passes quietly and drop stale candidates

kshitijk4poor0d37f9c2d7a9
diff --git a/agent/prepared_compaction.py b/agent/prepared_compaction.py
index 804c42399e2..439068ac9ac 100644
--- a/agent/prepared_compaction.py
+++ b/agent/prepared_compaction.py
@@ -39,6 +39,7 @@ invalidate a summary written from a superset of the same information.
from __future__ import annotations
import copy
+import dataclasses
import hashlib
import logging
import threading
@@ -57,7 +58,8 @@ PREPARE_BAND_RATIO = 0.12
MIN_EXTENSION_DELTA_TOKENS = 8_000
# Minimum pause after a failed pass (the summariser's own cooldown applies when longer).
_FAILED_PASS_BACKOFF_SECONDS = 60.0
-# Background summariser calls in flight across the process.
+# Background passes running across the process (a cancelled pass frees its slot as it unwinds; its
+# provider stream closes on the aux cancel path, normally within one chunk).
_slots = threading.BoundedSemaphore(2)
@@ -76,7 +78,7 @@ class _Window:
class PreparedSummary(_Window):
summary: str # prefixed handoff text, exactly as ``_generate_summary`` returns it
body: str # bare body the next compaction extends iteratively
- prompt_tokens: int = 0 # provider-reported prompt size when the pass was planned
+ prompt_tokens: int # provider-reported prompt size when this candidate was last planned or checked
class _PendingPass:
@@ -214,6 +216,8 @@ class PreparedCompaction:
with self._lock:
if self._pending is pending:
self._pending = None
+ # A pass that published after the snapshot above planned on the pre-compaction transcript.
+ self._entry = None
if pending is not None:
pending.cancelled.set() # done already, or superseded: stop paying for it
if reason is not None or entry is None:
@@ -241,11 +245,10 @@ class PreparedCompaction:
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's
- streamed progress and local prompt reading feed this thread's hooks, so the host's idle watchdog
- and deadline extension see a live summary exactly as they would inline; the deadline is re-read
- on every poll because prompt reading pushes it back."""
+ deadline, or the compression aux timeout without one; a host cancel; a discard; a failed pass).
+ Meanwhile the pass's streamed progress and local prompt reading feed this thread's hooks, so the
+ host's idle watchdog and deadline extension see a live summary exactly as they would inline; the
+ deadline is re-read on every poll because prompt reading pushes it back."""
from agent.auxiliary_client import (
_aux_progress, _capture_aux_cancel_check, _captured_aux_cancel_requested, _current_aux_stream_deadline,
_effective_aux_timeout,
@@ -328,6 +331,9 @@ class PreparedCompaction:
if delta < MIN_EXTENSION_DELTA_TOKENS:
logger.debug("prepared compaction: keeping window=%d-%d, delta ~%d tokens",
completed.compress_start, completed.compress_end, delta)
+ with self._lock: # measure the next skip from this check, not from the plan
+ if self._entry is completed:
+ self._entry = dataclasses.replace(completed, prompt_tokens=tokens)
return None
extend = completed
worker = _worker_copy(compressor)
@@ -374,7 +380,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_interrupt_protection, aux_progress_hook
+ from agent.auxiliary_client import AuxiliaryExplicitCancellation, aux_interrupt_protection, aux_progress_hook
from agent.local_prompt_progress import prompt_reading_hook
from agent.thread_scoped_output import thread_scoped_silence
@@ -402,6 +408,9 @@ class PreparedCompaction:
else:
logger.warning("prepared compaction: pass failed after %.1fs: %s", time.monotonic() - started,
worker._last_summary_error or "summariser returned nothing")
+ except AuxiliaryExplicitCancellation:
+ # The closed stream raises a BaseException; it must not reach the thread's excepthook (stderr).
+ logger.info("prepared compaction: pass cancelled after %.1fs (superseded)", time.monotonic() - started)
except Exception:
logger.warning("prepared compaction: pass crashed", exc_info=True)
finally:
diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py
index b21e1ca5202..5cdb2087e01 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; compaction uses (or waits for) it if those turns are unchanged.
+ # Opt-in: write the next summary in the background near the trigger; compaction uses (or waits for) it if the summarized 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.py
index 8c4cd2832cb..f028d009344 100644
--- a/tests/agent/test_prepared_compaction.py
+++ b/tests/agent/test_prepared_compaction.py
@@ -41,11 +41,15 @@ class _StubCompressor(ContextCompressor):
from agent.local_prompt_progress import current_prompt_reading_hook
pending = self.prepared_compaction._pending
- assert _wait_until(lambda: pending.on_reading is not None), "compaction never started waiting"
+ wait_until(lambda: pending.on_reading is not None, what="compaction waiting on the pass")
current_prompt_reading_hook()(self.read_seconds) # a local server reading the prompt
self.after_read()
if worker and self.gate is not None:
- assert self.gate.wait(5.0), "pass never released"
+ from agent.auxiliary_client import AuxiliaryExplicitCancellation, _aux_interrupt_cancel_requested
+
+ wait_until(lambda: self.gate.is_set() or _aux_interrupt_cancel_requested(), what="pass released or cancelled")
+ if not self.gate.is_set():
+ raise AuxiliaryExplicitCancellation() # how the aux client ends a cancelled summary stream
if worker and self.stream_progress:
from agent.auxiliary_client import _aux_progress
@@ -111,15 +115,6 @@ def _summary_row(compressed: list) -> dict:
return rows[0]
-def _wait_until(predicate, timeout: float = 5.0) -> bool:
- deadline = time.monotonic() + timeout
- while time.monotonic() < deadline:
- if predicate():
- return True
- time.sleep(0.01)
- return predicate()
-
-
# --------------------------------------------------------------------------- default off
@@ -245,10 +240,10 @@ def test_growth_past_the_candidate_beyond_the_tail_budget_summarises_inline(pass
compressed = cc.compress(later, current_tokens=cc.threshold_tokens + 1)
if pass_state == "running":
- assert running.is_alive(), "not waited on"
- cc.gate.set()
running.join(5.0)
- assert sorted(c["worker"] for c in cc.calls) == [False, True], "one pass, one inline summary"
+ assert [c["worker"] for c in cc.calls] == [False], "not waited on: cancelled, one inline summary"
+ else:
+ assert sorted(c["worker"] for c in cc.calls) == [False, True], "one pass, one inline summary"
assert "Summary v" in _summary_row(compressed)["content"] and cc.prepared_compaction._entry is None
@@ -327,7 +322,7 @@ def test_fingerprint_survives_a_session_db_round_trip(tmp_path):
# --------------------------------------------------------------------------- running passes
-def test_completed_candidate_splices_while_an_extension_pass_runs():
+def test_completed_candidate_splices_while_an_extension_pass_runs(monkeypatch):
cc = _compressor()
cc.tail_token_budget = 12_000 # room for a gap that starts an extension yet still splices
msgs = _messages(80)
@@ -339,16 +334,19 @@ def test_completed_candidate_splices_while_an_extension_pass_runs():
cc.gate = threading.Event()
running = pc.maybe_prepare(_agent(cc, _GROWN_TOKENS), grown, origin="test")
assert running is not None
+ misfit = pc.PreparedCompaction._misfit
- started = time.monotonic()
+ def publish_mid_take(*args): # the extension finishes while compaction checks the candidate
+ if not cc.gate.is_set():
+ cc.gate.set()
+ running.join(5.0)
+ return misfit(*args)
+
+ monkeypatch.setattr(pc.PreparedCompaction, "_misfit", staticmethod(publish_mid_take))
compressed = cc.compress(grown, current_tokens=cc.threshold_tokens + 1)
- assert time.monotonic() - started < 2.0, "must not wait for the running pass"
assert first.summary in _summary_row(compressed)["content"]
-
- cc.gate.set()
- running.join(5.0)
assert len(cc.calls) == 2 and cc.calls[1]["previous"] == first.body, "the pass was an extension"
- assert cc.prepared_compaction._entry is None, "an overtaken pass cannot publish"
+ assert cc.prepared_compaction._entry is None, "a pass planned before this compaction cannot outlive it"
def test_compaction_waits_for_a_running_pass_and_forwards_its_progress():
@@ -364,7 +362,7 @@ def test_compaction_waits_for_a_running_pass_and_forwards_its_progress():
host_progress = []
pending = cc.prepared_compaction._pending
# Release the pass only once the compaction is waiting on it.
- threading.Thread(target=lambda: _wait_until(lambda: pending.on_progress is not None) and cc.gate.set()).start()
+ threading.Thread(target=lambda: wait_until(lambda: pending.on_progress is not None) and cc.gate.set()).start()
with aux_progress_hook(lambda: host_progress.append(threading.current_thread().name)):
compressed = cc.compress(msgs, current_tokens=cc.threshold_tokens + 1)
running.join(5.0)
@@ -386,7 +384,6 @@ def test_wait_ends_at_the_host_deadline_then_summarises_inline_and_the_pass_cann
with aux_stream_deadline(time.monotonic() + 0.2):
cc.compress(msgs, current_tokens=cc.threshold_tokens + 1)
- assert running.is_alive(), "the wait ended at the host deadline, not with the pass"
assert [c["worker"] for c in cc.calls] == [False], "inline summary after the deadline"
cc.gate.set()
@@ -416,9 +413,11 @@ 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():
+def test_host_cancel_ends_the_wait_and_cancels_the_superseded_pass(monkeypatch):
from agent.auxiliary_client import aux_interrupt_protection, aux_progress_hook
+ escaped = []
+ monkeypatch.setattr(threading, "excepthook", escaped.append)
cc = _compressor()
cc.gate = threading.Event()
msgs = _messages(30)
@@ -429,11 +428,10 @@ def test_host_cancel_ends_the_wait_and_cancels_the_superseded_pass():
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)
+ assert not running.is_alive() and escaped == [], "the cancelled stream ends the pass quietly"
def test_session_reset_during_the_wait_wakes_it_without_waiting_for_the_pass():
@@ -449,7 +447,6 @@ def test_session_reset_during_the_wait_wakes_it_without_waiting_for_the_pass():
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)
@@ -508,6 +505,22 @@ def test_small_delta_keeps_the_candidate_without_copying_or_taking_a_slot(monkey
assert plans == [], "an unchanged prompt size skips re-planning the window on the turn thread"
+def test_grown_prompt_with_a_small_window_delta_keeps_the_candidate_and_rearms_the_skip(monkeypatch):
+ """Prompt growth that landed in the protected tail: the full check keeps the candidate, and the next
+ tool batch measures growth from that check instead of re-planning again."""
+ cc = _compressor()
+ msgs = _messages(30)
+ first = _prepare(cc, msgs)
+ plans, plan = [], cc._plan_compaction_window
+ monkeypatch.setattr(cc, "_plan_compaction_window", lambda m: plans.append(1) or plan(m))
+ grown = _grow(msgs, 4)
+ for _ in range(2):
+ assert pc.maybe_prepare(_agent(cc, _GROWN_TOKENS), grown, origin="test") is None
+ kept = cc.prepared_compaction._entry
+ assert plans == [1] and len(cc.calls) == 1
+ assert (kept.compress_end, kept.summary, kept.prompt_tokens) == (first.compress_end, first.summary, _GROWN_TOKENS)
+
+
def test_extension_pass_summarises_only_the_delta_on_top_of_the_previous_body():
cc = _compressor()
msgs = _messages(30)
@@ -616,7 +629,7 @@ def test_turn_that_crosses_the_trigger_splices_the_summary_prepared_during_its_t
pass_during_tool.append(state is not None and state._pending is not None)
tool_started.set()
if state is not None:
- assert _wait_until(lambda: state._pending is None)
+ wait_until(lambda: state._pending is None, what="the pass to finish")
return '{"ok": true}'
agent.client.chat.completions.create.side_effect = [
diff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.md
index 6012e4e82f9..ea29ab86616 100644
--- a/website/docs/developer-guide/context-compression-and-caching.md
+++ b/website/docs/developer-guide/context-compression-and-caching.md
@@ -695,8 +695,8 @@ When no completed candidate fits but the pass still running would, compaction wa
instead of writing the same summary again. Each streamed chunk of the pass touches the waiting
compaction's progress hook, so the host's idle watchdog treats the wait like an inline summary
stream, and time a llama.cpp server spends reading the pass's prompt pushes the host's deadline back
-as it would inline. The wait ends at the host's deadline (the compression aux timeout when there is none) or on
-cancellation, and compaction then summarizes inline.
+as it would inline. The wait ends at the host's deadline (the compression aux timeout when there is none), on
+cancellation, or when a session boundary discards the pass, and compaction then summarizes inline.
Everything else runs inline exactly as without the flag: no candidate, a candidate or running pass
that no longer fits, manual `/compress`, focus compaction, provider-overflow recovery, and