Skip to content

The agent that grows with you

fix(gateway): list sessions compressed before created_source stamping

kshitijk4poorce28ca328d3b
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 0fb0b5b349c..98de4309dfc 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -1350,6 +1350,21 @@ def backfill_legacy_session_owners(db):
return stamped
+def backfill_compression_created_sources(db):
+ """List conversations compressed before write-time stamping under their surface again: a chain
+ root without a created_source takes its nearest stamped continuation's. Idempotent and never
+ overwrites a known value, so every start runs it, like the owner backfill above."""
+ import logging
+ try:
+ stamped = db.stamp_legacy_compression_chains()
+ except Exception:
+ logging.getLogger(__name__).warning('Compression created_source repair failed for %s', db.db_path, exc_info=True)
+ return 0
+ if stamped:
+ logging.getLogger(__name__).info('Stamped created_source on %d compressed session row(s)', stamped)
+ return stamped
+
+
async def initialize_session_authority(runner, *, profile_id, instance_id, db=None, register=True):
"""Call after exclusive profile ownership, before connecting adapters/API.
@@ -1369,6 +1384,7 @@ async def initialize_session_authority(runner, *, profile_id, instance_id, db=No
from gateway.session_unattended_unknown import settle_recovered_unattended
await asyncio.to_thread(settle_recovered_unattended, db, epoch)
await asyncio.to_thread(backfill_legacy_session_owners, db)
+ await asyncio.to_thread(backfill_compression_created_sources, db)
authority = SessionAuthority(runner, profile_id=profile_id, instance_id=instance_id, db=db, epoch=epoch)
# A receipt's compression catch-up (#140060) writes: it commits off the loop too, before the
# authority is published, so the recovery below only reads.
diff --git a/hermes_state_compression.py b/hermes_state_compression.py
index 31ad895dc58..b3e999dd66c 100644
--- a/hermes_state_compression.py
+++ b/hermes_state_compression.py
@@ -13,7 +13,8 @@ from typing import Any, Dict, List, Optional, Tuple
from hermes_state_common import (
_BOUNDARY_END_REASONS, _COMPRESSION_LOCK_ROW_SQL as _LOCK_ROW_SQL, _ENDED_ROW_SQL, _ended_by_compression,
- _RESET_CHILD_SQL, _sql_json_extract, _sql_session_last_active, is_automatic_end_reason)
+ _RESET_CHILD_SQL, _non_continuation_child_sql, _sql_json_extract, _sql_session_last_active,
+ is_automatic_end_reason)
# Log-record parity with the origin module (caplog tests pin "hermes_state").
logger = logging.getLogger("hermes_state")
@@ -161,6 +162,20 @@ def stamp_compression_ancestors(conn, session_id: str) -> None:
current, parent_id = parent_id, parent["parent_session_id"]
+# Compression edges whose parent is unset and whose continuation child carries a real surface: what
+# a store from before write-time stamping still hides from ``sessions list --source``. A child
+# forked from that parent (branch, delegate or reset marker naming it) or a tool child is no
+# continuation (as in prune) and never matches. A stamped store, or one whose unset chains have no
+# stamped segment, answers with no rows after one scan of sessions. A stamped child the stamp walk
+# does not follow (a non-preferred sibling, or one with a fork marker inherited from an earlier row)
+# still matches; the walk stops at its first hop.
+_UNSTAMPED_EDGE_CHILD_SQL = f"""
+ SELECT child.id FROM sessions parent JOIN sessions child ON child.parent_session_id = parent.id
+ WHERE parent.end_reason = 'compression' AND {_UNSET_CREATED_SQL.format(col="parent.created_source")}
+ AND NOT {_UNSET_CREATED_SQL.format(col="child.created_source")}
+{_non_continuation_child_sql("child.", "parent.id")}"""
+
+
class SessionCompressionMixin:
"""Compression lineage, cooldown/streak counters, locks and turn leases."""
@@ -207,6 +222,20 @@ class SessionCompressionMixin:
"compress and a later close is recorded (#106459)", session_id, reason, provenance)
return reason
+ def stamp_legacy_compression_chains(self) -> int:
+ """Stamp the unset ancestors of chains compressed before write-time stamping from their
+ stamped segments, through ``stamp_compression_ancestors`` (same edge checks; a known value
+ is never replaced). One write transaction and idempotent, so the owner runs it on every
+ start: a store with nothing to repair pays one scan of sessions under the write lock, plus
+ a one-hop walk per stamped child of an unset compression parent that the walk does not
+ follow (a non-preferred sibling). Returns the rows stamped."""
+ def _do(conn):
+ before = conn.total_changes
+ for row in conn.execute(_UNSTAMPED_EDGE_CHILD_SQL).fetchall():
+ stamp_compression_ancestors(conn, row["id"])
+ return conn.total_changes - before
+ return self._execute_write(_do)
+
def find_live_compression_child(self, parent_session_id: str) -> Optional[dict[str, Any]]:
"""The unique live direct child of a compression-ended session, else None. A stale
agent whose parent was rotated elsewhere may recover only when the lineage names
diff --git a/tests/hermes_state/test_compression_created_source.py b/tests/hermes_state/test_compression_created_source.py
index 1921d3929b0..7d4ef862562 100644
--- a/tests/hermes_state/test_compression_created_source.py
+++ b/tests/hermes_state/test_compression_created_source.py
@@ -120,6 +120,83 @@ def test_unset_parent_behind_a_reset_edge_takes_the_fast_path(tmp_path):
db.close()
[email protected]("root_created", [None, "unknown", "local"])
+def test_owner_start_lists_a_legacy_compressed_chain_once(tmp_path, root_created):
+ """A chain compressed before write-time stamping (unset root, stamped tip) and never written
+ since is hidden from ``--source cli``. The owner's start stamps it once; a second start has
+ nothing left to do."""
+ from gateway.session_authority import backfill_compression_created_sources
+ from hermes_cli.gateway_session_store import SurfaceLabelReader
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ try:
+ mid = _compress(db, root_created, "mid")
+ db.publish_compression_child(
+ parent_session_id=mid, child_session_id="tip", source="local",
+ messages=[{"role": "user", "content": "hi"}], require_compression_lease=False)
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source=? WHERE id=?", (root_created, mid))
+ db._conn.execute("UPDATE sessions SET created_source='cli' WHERE id='tip'")
+ db._conn.commit()
+
+ def listed():
+ return [s["id"] for s in SurfaceLabelReader(db).list_sessions_rich(source="cli")]
+
+ assert listed() == []
+ assert backfill_compression_created_sources(db) == 2
+ assert [_created(db, sid) for sid in ("parent-mid", mid, "tip")] == ["cli"] * 3
+ assert listed() == ["tip"]
+ assert backfill_compression_created_sources(db) == 0
+ finally:
+ db.close()
+
+
+def test_owner_start_keeps_known_provenance(tmp_path):
+ """The repair copies only along a compression continuation onto an unset row: a root on another
+ surface, a branch child's surface and a chain with nothing stamped are all left as they are."""
+ from gateway.session_authority import backfill_compression_created_sources
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ try:
+ _compress(db, "tui", "known")
+ _compress(db, None, "unset")
+ branched = _compress(db, None, "branched")
+ db.create_session("branch", source="cli", parent_session_id="parent-" + branched,
+ model_config={"_branched_from": "parent-" + branched})
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source='cli' WHERE id IN ('known', 'branch')")
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id IN ('unset', ?)", (branched,))
+ db._conn.commit()
+
+ assert backfill_compression_created_sources(db) == 0
+ assert [_created(db, sid) for sid in ("parent-known", "parent-unset", "parent-branched", "branch")] == [
+ "tui", None, None, "cli"]
+ finally:
+ db.close()
+
+
+def test_owner_start_skips_a_branch_child_of_an_unset_compression_parent(tmp_path):
+ """A branch child is no continuation: the stamp never follows it, so it is no repair candidate
+ either, and a later start does not walk its unset compression parent again."""
+ from gateway.session_authority import backfill_compression_created_sources
+ from hermes_state_compression import _UNSTAMPED_EDGE_CHILD_SQL
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ try:
+ branched = _compress(db, None, "branched")
+ db.create_session("branch", source="cli", parent_session_id="parent-" + branched,
+ model_config={"_branched_from": "parent-" + branched})
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source='cli' WHERE id='branch'")
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", (branched,))
+ db._conn.commit()
+
+ assert backfill_compression_created_sources(db) == 0
+ assert db._conn.execute(_UNSTAMPED_EDGE_CHILD_SQL).fetchall() == []
+ finally:
+ db.close()
+
+
def test_unset_ancestor_probe_reads_only_the_walked_rows(tmp_path):
"""The fast path runs on every usage write of a stamped compression child: its final lookup
must join the walked ids to sessions by key, never scan the table (ms per write at 30k rows)."""