Skip to content

The agent that grows with you

fix(state): stale-agent recovery ignores reset forks of a compression parent

teknium152a293fb07a6
diff --git a/hermes_state_compression.py b/hermes_state_compression.py
index 41dae78f9c3..2370176231e 100644
--- a/hermes_state_compression.py
+++ b/hermes_state_compression.py
@@ -102,7 +102,7 @@ class SessionCompressionMixin:
superseded = conn.execute(
"SELECT 1 FROM sessions WHERE parent_session_id = ?"
+ self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="") + " LIMIT 1",
- (session_id, session_id, session_id)).fetchone()
+ (session_id,) * 4).fetchone()
if superseded is not None:
return None
conn.execute(
@@ -141,7 +141,7 @@ class SessionCompressionMixin:
ORDER BY s.started_at ASC
LIMIT 2
""",
- (parent_session_id, parent_session_id, parent_session_id),
+ (parent_session_id,) * 4,
).fetchall()
return self._session_row_dict(rows[0]) if len(rows) == 1 else None
@@ -154,7 +154,7 @@ class SessionCompressionMixin:
def _do(conn):
if not _ended_by_compression(conn.execute(_ENDED_ROW_SQL, (session_id,)).fetchone()):
return False
- # Any non-branch/non-delegate/non-tool child is a continuation, ended or not.
+ # Any non-branch/non-delegate/non-reset/non-tool child is a continuation, ended or not.
child = conn.execute(
"""
SELECT 1
@@ -165,7 +165,7 @@ class SessionCompressionMixin:
+ """
LIMIT 1
""",
- (session_id, session_id, session_id),
+ (session_id,) * 4,
).fetchone()
if child is not None:
return False
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index d1e37bcdd3e..4208283d3d0 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -435,12 +435,14 @@ class SessionSessionsMixin:
# quiet and its unkeyed successor (incident was ~60s; 15 min without spanning conversations).
_ORPHAN_ADOPTION_MAX_GAP_S = 900.0
- # Children that are NOT compression continuations (branches, delegates, tool sessions). Markers
- # are bound to the queried parent id: continuations inherit model_config verbatim, so
- # presence-matching misclassified them as delegates.
+ # Children that are NOT compression continuations (branches, delegates, reset forks, tool
+ # sessions). Markers are bound to the queried parent id: continuations inherit model_config
+ # verbatim, so presence-matching misclassified them as delegates. Callers bind the parent id
+ # three times for this filter.
_NON_CONTINUATION_CHILD_FILTER_SQL = (
f" AND COALESCE({_sql_json_extract('{alias}model_config', '$._branched_from')}, '') != ?\n"
f" AND COALESCE({_sql_json_extract('{alias}model_config', '$._delegate_from')}, '') != ?\n"
+ f" AND COALESCE({_sql_json_extract('{alias}model_config', '$._reset_from')}, '') != ?\n"
" AND COALESCE({alias}source, '') != 'tool'\n"
)
diff --git a/tests/hermes_state/test_compression_lineage_guard.py b/tests/hermes_state/test_compression_lineage_guard.py
index eb64dbec430..3686d7cd5d4 100644
--- a/tests/hermes_state/test_compression_lineage_guard.py
+++ b/tests/hermes_state/test_compression_lineage_guard.py
@@ -231,6 +231,29 @@ def test_find_live_compression_child_ignores_non_continuation_children(
assert child["id"] == "canonical"
+def test_reset_fork_is_not_a_compression_continuation_for_recovery(db: SessionDB) -> None:
+ """A reset fork (``model_config._reset_from`` bound to the parent) is its own conversation:
+ stale-agent recovery must neither recover to it nor let it block the real continuation or a
+ reopen of a parent that has no continuation (#114271)."""
+ _compression_parent(db)
+ db.create_session("canonical", source="webui", parent_session_id="parent")
+ db.create_session(
+ "reset", source="webui", parent_session_id="parent", model_config={"_reset_from": "parent"},
+ )
+
+ child = db.find_live_compression_child("parent")
+ assert child is not None and child["id"] == "canonical"
+
+ db.end_session("canonical", "startup_orphan_reap")
+ assert db.find_live_compression_child("parent") is None
+
+ _compression_parent(db, "orphan")
+ db.create_session(
+ "orphan-reset", source="webui", parent_session_id="orphan", model_config={"_reset_from": "orphan"},
+ )
+ assert db.reopen_orphaned_compression_session("orphan") is True
+
+
def test_publish_compression_child_is_atomic_on_handoff_failure(
db: SessionDB, monkeypatch
) -> None: