Skip to content

The agent that grows with you

fix(state): keep unseen rows active when repaired-row coverage is ambiguous

unsupportedpastels892082406f11
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 5feb9f68581..b2402a53d4e 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -1042,6 +1042,9 @@ class SessionMessagesMixin:
content, 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:
return []
+ from gateway.message_timestamps import strip_leading_message_timestamps
+
+ content, _ = strip_leading_message_timestamps(content)
bound = watermark is not None
rows = [
(int(row["id"]), row["role"], self._loaded_view_content(row["role"], self._decode_content(row["content"])))
@@ -1082,15 +1085,12 @@ class SessionMessagesMixin:
self, conn, session_id: str, covered_ids: Optional[List[int]],
unresolved_held: Optional[List[Dict[str, Any]]], watermark: Optional[int] = None,
) -> Optional[Tuple[List[int], Set[int]]]:
- """``(ids safe to archive as summarized, ids merged into another held dict)``, or None when
- a durable held row cannot be named.
-
- An unresolved dict that still carries the persist marker was loaded from the DB.
- Failing to name it means the watermark path, which archives the rows the compressor
- saw, including ones whose ids were stripped. A marker-less miss is an unpersisted
- turn: it names nothing, and it is not a reason to abandon the ids we do have.
- Several active rows with the same content are ambiguous, so that also abandons,
- and so does a merged dict whose run cannot be named.
+ """``(ids safe to archive as summarized, ids merged into another held dict)``.
+
+ Only an absent named-coverage request permits watermark archival. A durable
+ dict whose provenance cannot be resolved uniquely contributes no ids; its
+ originals stay live, along with every unseen row, rather than expanding
+ coverage to a watermark that cannot prove what the compressor held.
The second set holds only merges the caller could not count: a dict that lists its
own ``_absorbed_row_ids`` is already counted in ``tail_count``.
"""
@@ -1110,7 +1110,7 @@ class SessionMessagesMixin:
# The dict's own row, recorded before a fold rewrote its text, is its own tail slot.
matches = self._matching_retired_ids(conn, session_id, message, watermark)
if len(matches) != 1:
- return None
+ continue
proved.extend(matches)
if not message.get(OWN_ROW):
merged_away.update(matches)
@@ -1124,11 +1124,11 @@ class SessionMessagesMixin:
# resolved first, and a stamped dict that names none is still durable, never an unpersisted turn.
run = self._merged_user_run(conn, session_id, message, watermark)
if message.get(MERGED_DURABLE_ROWS) and not run:
- return None
+ continue
matches = [] if run else self._matching_active_ids(conn, session_id, message)
if len(matches) > 1 or (
message.get(_DB_PERSISTED_MARKER) and len(matches) != 1 and not run):
- return None
+ continue
proved.extend(matches or run)
merged_away.update(set(run[1:]) - set(message.get("_absorbed_row_ids") or ()))
return list(dict.fromkeys(proved)), merged_away
diff --git a/tests/hermes_state/test_dropped_row_unseen_gap.py b/tests/hermes_state/test_dropped_row_unseen_gap.py
index 9bdfd1657ac..3df1d18326d 100644
--- a/tests/hermes_state/test_dropped_row_unseen_gap.py
+++ b/tests/hermes_state/test_dropped_row_unseen_gap.py
@@ -55,6 +55,46 @@ def test_an_unseen_gap_survives_a_dropped_row(tmp_path, left, row_ids):
db.close()
[email protected]("trigger", ["ambiguous", "rendered", "orphan", "folded"])
+def test_unresolved_repair_provenance_never_archives_unseen_rows(tmp_path, trigger):
+ from gateway.run import _build_gateway_agent_history
+
+ 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=True)
+ pair = ["continue", "please continue"] if trigger == "ambiguous" else ["U2", "U3"]
+ unseen = [("user", pair[0], {}), ("user", pair[1], {}), ("assistant", "UNSEEN_REPLY", {})]
+ if trigger == "rendered":
+ unseen = [("user", "UNSEEN_USER", {}), ("assistant", "UNSEEN_REPLY", {})]
+ if trigger in {"orphan", "folded"}:
+ extra = DROPPED["tool_result" if trigger == "orphan" else "two_replies"]
+ unseen = [("user", "UNSEEN_USER", {}), *extra, ("assistant", "UNSEEN_REPLY", {})]
+ suffix = [("user", "U2", {}), ("assistant", "A2", {}), *extra,
+ ("user", "U3", {}), ("assistant", "A3", {})]
+ else:
+ suffix = [("user", pair[0], {}), ("user", pair[1], {}), ("assistant", "A3", {})]
+ for role, content, fields in [*unseen, *suffix]:
+ db.append_message("sid", role, content, **fields)
+ held.extend(db.get_messages_as_conversation("sid", include_row_ids=False)[2 + len(unseen):])
+ repair_message_sequence(None, held)
+ if trigger == "rendered":
+ held[2:], _ = _build_gateway_agent_history(held[2:], inject_timestamps=True)
+ covered, unresolved = coverage_for_commit(db, "sid", held)
+ db.archive_and_compact(
+ "sid", [{"role": "user", "content": "summary of the held rows"}],
+ watermark=db.get_active_message_watermark("sid"), covered_ids=covered, unresolved_held=unresolved)
+ active = [row["content"] for row in db.get_messages("sid")]
+ assert "UNSEEN_REPLY" in active
+ assert all(content in active for role, content, fields in unseen)
+ if trigger == "rendered":
+ assert active == ["summary of the held rows", *[content for role, content, fields in unseen]]
+ finally:
+ db.close()
+
+
def test_the_named_rows_stay_off_the_request_copy(session):
from agent.conversation_compression_archive import RETIRED_DURABLE_ROWS
from agent.turn_context import build_api_messages