The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: bind continuation-marker exclusions to the queried parent (fail-open fix)
kshitija0801b878a53
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex af266e5c5f2..4eed5b44e21 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -1325,7 +1325,7 @@ def recover_rotated_compression_session(session_id,)except Exception as exc:- logger.debug(+ logger.warning("orphaned compression session reopen failed ""for %s: %s",session_id,diff --git a/hermes_state.py b/hermes_state.pyindex 87992e0d273..cecdc96d9c2 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -3648,6 +3648,24 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)).fetchone()return self._session_row_dict(row) if row else None+ # Children that carry a ``parent_session_id`` but are NOT compression+ # continuations: branches, delegate/subagent runs, and tool sessions.+ # A marker only disqualifies a child when it points at the parent being+ # queried — compression continuations inherit the rotated agent's+ # ``model_config`` verbatim (``publish_compression_child`` callers pass+ # ``agent._session_init_model_config``), so a delegate subagent's+ # continuation carries ``_delegate_from=<the delegate's own parent>``.+ # Matching markers by mere presence misclassified those real+ # continuations as delegate children (fail-open for orphan reopen,+ # fail-closed for adoption). Bind the parent id for both markers.+ _NON_CONTINUATION_CHILD_FILTER_SQL = (+ " AND COALESCE(json_extract(COALESCE({alias}model_config, '{{}}'),"+ " '$._branched_from'), '') != ?\n"+ " AND COALESCE(json_extract(COALESCE({alias}model_config, '{{}}'),"+ " '$._delegate_from'), '') != ?\n"+ " AND COALESCE({alias}source, '') != 'tool'\n"+ )+def find_live_compression_child(self, parent_session_id: str) -> Optional[Dict[str, Any]]:@@ -3681,13 +3699,13 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)LEFT JOIN system_prompts sp ON sp.hash = s.system_prompt_hashWHERE s.parent_session_id = ?AND s.ended_at IS NULL- AND json_extract(COALESCE(s.model_config, '{}'), '$._branched_from') IS NULL- AND json_extract(COALESCE(s.model_config, '{}'), '$._delegate_from') IS NULL- AND COALESCE(s.source, '') != 'tool'+ """+ + self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="s.")+ + """ORDER BY s.started_at ASCLIMIT 2""",- (parent_session_id,),+ (parent_session_id, parent_session_id, parent_session_id),).fetchall()return self._session_row_dict(rows[0]) if len(rows) == 1 else None@@ -3723,12 +3741,12 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)SELECT 1FROM sessionsWHERE parent_session_id = ?- AND json_extract(COALESCE(model_config, '{}'), '$._branched_from') IS NULL- AND json_extract(COALESCE(model_config, '{}'), '$._delegate_from') IS NULL- AND COALESCE(source, '') != 'tool'+ """+ + self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="")+ + """LIMIT 1""",- (session_id,),+ (session_id, session_id, session_id),).fetchone()if child is not None:return False@@ -3762,6 +3780,11 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)"AND end_reason = 'compression'",(session_id,),)+ # rowcount==1 is guaranteed by the parent SELECT at the top of+ # this same BEGIN IMMEDIATE transaction. If this is ever edited+ # to return False past this point, note that the lease DELETE+ # above will still COMMIT (_execute_write commits unless _do+ # raises) — raise instead of returning False to roll back.return updated.rowcount == 1return bool(self._execute_write(_do))diff --git a/tests/state/test_compression_lineage_guard.py b/tests/state/test_compression_lineage_guard.pyindex 1665c1c5243..ef071c79742 100644--- a/tests/state/test_compression_lineage_guard.py+++ b/tests/state/test_compression_lineage_guard.py@@ -94,6 +94,48 @@ def test_reopen_orphaned_compression_session_ignores_non_continuation_children() is True+def test_reopen_fails_closed_when_continuation_inherits_foreign_markers(+ db: SessionDB,+) -> None:+ """A REAL continuation can carry ``_delegate_from``/``_branched_from``+ pointing at some OTHER session: ``publish_compression_child`` callers+ pass the rotated agent's ``_session_init_model_config`` verbatim, so a+ delegate subagent's continuation inherits ``_delegate_from=<the+ delegate's own parent>``. Marker-presence matching misclassified it as+ a delegate child — reopen returned True with a live continuation+ present, forking the lineage. Markers only disqualify a child when+ they point at the queried parent."""+ _compression_parent(db, "delegate-session")+ db.create_session(+ "delegate-continuation",+ source="subagent",+ parent_session_id="delegate-session",+ model_config={"_delegate_from": "some-original-parent"},+ )++ assert db.reopen_orphaned_compression_session("delegate-session") is False+ parent = db.get_session("delegate-session")+ assert parent["end_reason"] == "compression"+++def test_find_live_child_returns_continuation_with_foreign_markers(+ db: SessionDB,+) -> None:+ """Adoption-side twin of the reopen test above: the continuation that+ inherited a foreign ``_delegate_from`` must still be adoptable."""+ _compression_parent(db, "delegate-session-2")+ db.create_session(+ "inherited-continuation",+ source="subagent",+ parent_session_id="delegate-session-2",+ model_config={"_delegate_from": "some-original-parent"},+ )++ child = db.find_live_compression_child("delegate-session-2")+ assert child is not None+ assert child["id"] == "inherited-continuation"++def test_reopen_orphaned_compression_session_fails_closed_with_active_lease(db: SessionDB,) -> None:@@ -142,8 +184,6 @@ def test_reopen_orphaned_compression_session_loses_to_expired_lease_refresh(assert db.get_session("refreshed-lease-parent")["end_reason"] == "compression"--def test_find_live_compression_child_ignores_non_continuation_children(db: SessionDB,) -> None:@@ -169,12 +209,6 @@ def test_find_live_compression_child_ignores_non_continuation_children(assert child["id"] == "canonical"------def test_publish_compression_child_is_atomic_on_handoff_failure(db: SessionDB, monkeypatch) -> None: