The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): preserve exact coverage through composed repairs
unsupportedpastels3f04ecb789ef
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 1d01264b85a..6fce8cf786a 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -502,14 +502,17 @@ def _count_unnamed_row(survivor: Dict[str, Any], retired: Dict[str, Any]) -> Nonrecorded so the commit can name its row."""from agent.context_compressor import _DB_PERSISTED_MARKERfrom agent.conversation_compression_archive import (- RETIRED_DURABLE_ROWS, UNNAMED_DURABLE_ROWS, retired_row_payload)+ OWN_ROW, RETIRED_DURABLE_ROWS, UNNAMED_DURABLE_ROWS, retired_row_payload)def loaded(message: Dict[str, Any]) -> bool:return bool(message.get(_DB_PERSISTED_MARKER) or message.get(UNNAMED_DURABLE_ROWS))if loaded(survivor) and loaded(retired) and not isinstance(retired.get("_row_id"), int):survivor[UNNAMED_DURABLE_ROWS] = int(survivor.get(UNNAMED_DURABLE_ROWS) or 0) + 1- survivor.setdefault(RETIRED_DURABLE_ROWS, []).append(retired_row_payload(retired))+ # A previously folded dict already records its original row. The transfer in+ # _remember_absorbed_row retires that record; its synthetic text/calls never existed in storage.+ if not any(row.get(OWN_ROW) for row in retired.get(RETIRED_DURABLE_ROWS) or ()):+ survivor.setdefault(RETIRED_DURABLE_ROWS, []).append(retired_row_payload(retired))def _remember_own_row(survivor: Dict[str, Any]) -> None:diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex a1ea2aad578..f76cd908f74 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -1037,7 +1037,7 @@ class SessionMessagesMixin:Only rows at or below *watermark*: the repair ran on a load taken before the snapshot, so arun appended later that joins to the same text is another surface's, not the merged one."""- from agent.conversation_compression_archive import MERGED_DURABLE_ROWS+ from agent.conversation_compression_archive import MERGED_DURABLE_ROWS, OWN_ROW, RETIRED_DURABLE_ROWScontent, width = message.get("content"), message.get(MERGED_DURABLE_ROWS)if message.get("role") != "user" or not isinstance(content, str) or type(width) is not int or width < 2:@@ -1049,6 +1049,17 @@ class SessionMessagesMixin:f"SELECT id, role, content FROM messages WHERE session_id = ? AND active = 1"f"{' AND id <= ?' if bound else ''} ORDER BY id",(session_id, *((int(watermark),) if bound else ()))).fetchall()]+ # Dropping an orphan can make user rows adjacent only in the repaired view.+ # Skip only uniquely proved retired originals, never arbitrary intervening rows.+ retired_ids = set()+ for retired in message.get(RETIRED_DURABLE_ROWS) or ():+ if retired.get(OWN_ROW):+ continue+ matches = self._matching_retired_ids(conn, session_id, retired, watermark)+ if len(matches) != 1:+ return None+ retired_ids.update(matches)+ rows = [row for row in rows if row[0] not in retired_ids]runs: List[List[int]] = []for start in range(len(rows)):merged = ""diff --git a/tests/hermes_state/test_chained_repair_coverage.py b/tests/hermes_state/test_chained_repair_coverage.pynew file mode 100644index 00000000000..6987455083c--- /dev/null+++ b/tests/hermes_state/test_chained_repair_coverage.py@@ -0,0 +1,50 @@+"""Composed repairs must not archive context the compressor never held (#129123)."""++import pytest++from agent.agent_runtime_helpers import repair_message_sequence+from agent.conversation_compression_archive import coverage_for_commit+from hermes_state import SessionDB+++def _call(call_id):+ return {"tool_calls": [{"id": call_id, "type": "function", "function": {+ "name": "terminal", "arguments": "{}",+ }}]}++[email protected]("row_ids", [False, True])[email protected]("shape", ["chained_calls", "users_split_by_drop"])+def test_composed_repairs_preserve_unseen_context(tmp_path, row_ids, shape):+ db = SessionDB(tmp_path / "state.db")+ try:+ db.create_session("sid", source="test")+ db.append_message("sid", "user", "U1")+ db.append_message("sid", "assistant", "A1")+ held = db.get_messages_as_conversation("sid", include_row_ids=row_ids)+ # Deterministic interleaving: these rows never enter the held snapshot.+ db.append_message("sid", "user", "UNSEEN_USER")+ db.append_message("sid", "assistant", "UNSEEN_REPLY")+ db.append_message("sid", "user", "U2")+ if shape == "chained_calls":+ db.append_message("sid", "assistant", "tool setup", **_call("z"))+ db.append_message("sid", "tool", "answer z", tool_call_id="z")+ db.append_message("sid", "assistant", "", **_call("a"))+ db.append_message("sid", "assistant", "", **_call("b"))+ else:+ db.append_message("sid", "tool", "orphan", tool_call_id="orphan")+ db.append_message("sid", "user", "U3")+ db.append_message("sid", "assistant", "A3")+ held.extend(db.get_messages_as_conversation("sid", include_row_ids=row_ids)[4:])+ repair_message_sequence(None, held)+ covered, unresolved = coverage_for_commit(db, "sid", held)+ db.archive_and_compact(+ "sid", [{"role": "user", "content": "summary HELD only"}],+ watermark=db.get_active_message_watermark("sid"),+ covered_ids=covered, unresolved_held=unresolved,+ )+ assert [m["content"] for m in db.get_messages("sid")] == [+ "summary HELD only", "UNSEEN_USER", "UNSEEN_REPLY",+ ]+ finally:+ db.close()