The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(state): the ancestor stamp's fast path follows only compression edges
kshitijk4poore8b309f8e15e
diff --git a/hermes_state_compression.py b/hermes_state_compression.pyindex f365b918775..bb98dfed046 100644--- a/hermes_state_compression.py+++ b/hermes_state_compression.py@@ -111,17 +111,19 @@ def _walk_compression_chain(session_id: str, step) -> list[str]:_UNSET_CREATED_SOURCES = frozenset({None, "", "unknown", "local"})_UNSET_CREATED_SQL = "COALESCE({col}, '') IN ('', 'unknown', 'local')"_CREATED_ROW_SQL = "SELECT parent_session_id, created_source FROM sessions WHERE id = ?"-# Is any ancestor unset, walking up parent_session_id through ancestors on the same surface?-# One query, so a fully stamped chain is skipped without the per-hop validated walk below.+# Is any ancestor unset, walking up parent_session_id through compressed parents on the same+# surface? One query, so a fully stamped chain is skipped without the per-hop validated walk below.+# A parent that did not end by compression (a reset or branch source) is not a chain edge, so an+# unset row behind one does not send every write down that walk._UNSET_ANCESTOR_SQL = """WITH RECURSIVE up(pid, n) AS (SELECT parent_session_id, 1 FROM sessions WHERE id = ?UNION ALLSELECT s.parent_session_id, up.n + 1 FROM up JOIN sessions s ON s.id = up.pid- WHERE s.created_source = ? AND up.n < ?+ WHERE s.end_reason = 'compression' AND s.created_source = ? AND up.n < ?)SELECT 1 FROM up JOIN sessions s ON s.id = up.pid- WHERE """ + _UNSET_CREATED_SQL.format(col="s.created_source") + """+ WHERE s.end_reason = 'compression' AND """ + _UNSET_CREATED_SQL.format(col="s.created_source") + """LIMIT 1"""diff --git a/tests/hermes_state/test_compression_created_source.py b/tests/hermes_state/test_compression_created_source.pyindex 5e8c80558e1..e1f7d6552ce 100644--- a/tests/hermes_state/test_compression_created_source.py+++ b/tests/hermes_state/test_compression_created_source.py@@ -87,3 +87,34 @@ def test_compressing_a_stamped_tip_stamps_its_legacy_root(tmp_path, root_createdassert [_created(db, sid) for sid in ("parent-" + tip, tip, "new-tip")] == ["cli"] * 3finally:db.close()+++def test_unset_parent_behind_a_reset_edge_takes_the_fast_path(tmp_path):+ """A stamped chain whose root was reset from a legacy NULL row has nothing to stamp: the+ ancestor stamp returns after its two lookups instead of walking the chain on every write."""+ from hermes_state_compression import stamp_compression_ancestors++ db = SessionDB(db_path=tmp_path / "state.db")+ try:+ db.create_session("legacy", source="local")+ db.create_session("reset-root", source="cli", parent_session_id="legacy",+ model_config={"_reset_from": "legacy"})+ with db._lock:+ db._conn.execute("UPDATE sessions SET created_source=NULL, ended_at=1, "+ "end_reason='session_reset' WHERE id='legacy'")+ db._conn.commit()+ db.append_message("reset-root", role="user", content="hi")+ db.publish_compression_child(+ parent_session_id="reset-root", child_session_id="tip", source="cli",+ messages=[{"role": "user", "content": "hi"}], require_compression_lease=False)+ statements = []+ with db._lock:+ db._conn.set_trace_callback(statements.append)+ try:+ stamp_compression_ancestors(db._conn, "tip")+ finally:+ db._conn.set_trace_callback(None)+ assert len(statements) == 2, statements+ assert _created(db, "legacy") is None+ finally:+ db.close()