The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): a watermark the store cannot answer for skips the pass
John Paul Soliva6614d16f3587
diff --git a/agent/micro_compaction.py b/agent/micro_compaction.pyindex 328f723f550..fe0ffc41851 100644--- a/agent/micro_compaction.py+++ b/agent/micro_compaction.py@@ -226,8 +226,10 @@ class MicroCompactionMixin:_started_at = time.monotonic()_tokens_before = estimate_messages_tokens_rough(messages)# The history this pass rewrites, and the store's watermark, before the slow summary call: the commit- # keeps what another surface or a concurrent write added instead of archiving it unseen.- _held, _start_watermark = list(messages), self._micro_start_watermark()+ # keeps what another surface or a concurrent write added instead of archiving it unseen. A watermark+ # the store could not answer for leaves the commit unbounded, so the pass does not run at all.+ _held = list(messages)+ _wm_status, _start_watermark = self._micro_start_watermark()def _telemetry(outcome: str, result: List[Dict[str, Any]], **extra: Any) -> None:self._emit_micro_compaction_telemetry(@@ -235,6 +237,10 @@ class MicroCompactionMixin:tokens_before=_tokens_before, duration_ms=int((time.monotonic() - _started_at) * 1000), **extra,)+ if _wm_status == "failed": # an unanswerable watermark would commit an unbounded archive+ _telemetry("watermark_unavailable", messages, tokens_after=_tokens_before)+ return messages+# Defrag rewrites summary text/marker in place (no splice, no cursor move) instead of# absorbing this turn.if self._needs_defrag():@@ -347,16 +353,23 @@ class MicroCompactionMixin:except Exception as exc:logger.debug("failed to emit micro-compaction telemetry: %s", exc)- def _micro_start_watermark(self) -> Optional[int]:- """The store's active watermark before a pass's slow step, or None where the store has none."""+ def _micro_start_watermark(self) -> "tuple[str, Optional[int]]":+ """``(status, watermark)`` taken before a pass's slow step.++ ``unsupported`` where the store has no watermark API: the commit keeps today's+ archive-everything behaviour, as it always has. ``failed`` when the read itself raised —+ that must NOT collapse into the same None, because None means "archive every active row",+ which is the very loss this watermark exists to prevent. The pass is skipped instead.+ """session_db, session_id = getattr(self, "_session_db", None), getattr(self, "_session_id", "")watermark_of = getattr(session_db, "get_active_message_watermark", None)if not session_id or not callable(watermark_of):- return None+ return "unsupported", Nonetry:- return watermark_of(session_id)- except Exception:- return None+ return "ok", watermark_of(session_id)+ except Exception as exc:+ logger.info("micro-compaction: watermark read failed, skipping this pass: %s", exc)+ return "failed", Nonedef _sync_micro_compact_to_db(self, compacted_messages: List[Dict[str, Any]], *, held: Optional[List[Dict[str, Any]]] = None,diff --git a/tests/agent/test_micro_compaction.py b/tests/agent/test_micro_compaction.pyindex 8be28b49443..53947e76170 100644--- a/tests/agent/test_micro_compaction.py+++ b/tests/agent/test_micro_compaction.py@@ -861,6 +861,36 @@ def test_superseding_marker_never_shows_a_user_input_twice_in_display_history(tmdb.close()+def test_micro_compaction_skips_the_pass_when_the_watermark_read_fails(tmp_path):+ """A watermark the store could not answer for must not become an unbounded archive. None is the+ store saying "I have no watermark API"; a raised read is not the same answer, and treating it as+ one archives every active row — the loss the watermark was added to prevent."""+ from agent.context_compressor import _DB_PERSISTED_MARKER+ from hermes_state import SessionDB++ db = SessionDB(db_path=tmp_path / "state.db")+ db.create_session("s", source="cli")+ messages = _conversation(exchanges=8)+ for msg in messages:+ msg["_row_id"] = db.append_message("s", role=msg["role"], content=msg["content"])+ msg[_DB_PERSISTED_MARKER] = True+ foreign = "[from Telegram] the vault code is 7741"+ db.append_message("s", "user", foreign)++ def _boom(_session_id):+ raise RuntimeError("transient store failure")+ db.get_active_message_watermark = _boom+ cc = _compressor()+ cc._session_db, cc._session_id = db, "s"++ result = cc._micro_compact(messages)++ assert _summary_markers(result) == [] # the pass did not run+ live = [str(m["content"]) for m in db.get_messages_as_conversation("s")]+ assert live[-1] == foreign # and nothing was archived+ assert sum(foreign in content for content in live) == 1++def test_micro_compaction_keeps_a_turn_another_surface_appended_after_load(tmp_path):"""The micro-compaction commit rewrites the history this process holds. A turn another surface appended tothe same session since then was archived with the rest (no watermark), as summarized away though the rolling