Skip to content

The agent that grows with you

fix(state): stamp a NULL compression root behind a same-surface segment

kshitijk4poorf4861545f5f6
diff --git a/hermes_state_compression.py b/hermes_state_compression.py
index 803db894bd5..1d330fe49bc 100644
--- a/hermes_state_compression.py
+++ b/hermes_state_compression.py
@@ -113,7 +113,8 @@ def stamp_compression_ancestors(conn, session_id: str) -> None:
knows the surface, so the turn's usage write stamps only the tip. Stamping the chain here,
in that same transaction, keeps the root matchable without a list-time chain walk. Each hop
must be the parent's preferred continuation (``_CHAIN_STEP_SQL``); ``unknown`` counts as
- unset, as in the creator upsert.
+ unset, as in the creator upsert. An ancestor already on the same surface is walked through
+ (a NULL root behind a stamped mid segment still gets stamped); another surface stops it.
"""
row = conn.execute("SELECT parent_session_id, created_source FROM sessions WHERE id = ?",
(session_id,)).fetchone()
@@ -124,12 +125,13 @@ def stamp_compression_ancestors(conn, session_id: str) -> None:
for _ in range(_CHAIN_CAP):
parent = parent_id and conn.execute(
"SELECT parent_session_id, created_source FROM sessions WHERE id = ?", (parent_id,)).fetchone()
- if not parent or parent["created_source"] not in (None, "unknown"):
+ if not parent or parent["created_source"] not in (None, "unknown", surface):
return
step = conn.execute(_CHAIN_STEP_SQL, (parent_id,)).fetchone()
if step is None or step["id"] != current:
return
- conn.execute("UPDATE sessions SET created_source = ? WHERE id = ?", (surface, parent_id))
+ if parent["created_source"] != surface:
+ conn.execute("UPDATE sessions SET created_source = ? WHERE id = ?", (surface, parent_id))
current, parent_id = parent_id, parent["parent_session_id"]
diff --git a/tests/hermes_cli/test_sessions_source_local.py b/tests/hermes_cli/test_sessions_source_local.py
index b9a167bf75e..2910ad7c915 100644
--- a/tests/hermes_cli/test_sessions_source_local.py
+++ b/tests/hermes_cli/test_sessions_source_local.py
@@ -32,6 +32,25 @@ def test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_pa
"chain-tip", input_tokens=2, output_tokens=1, model="m", api_call_count=1, source="cli")
assert [db.get_session(sid)["created_source"] for sid in ("chain-root", "chain-mid", "chain-tip")] == [
"cli", "cli", "cli"]
+ # Mixed chain: a NULL root behind a mid segment already on the same surface is still
+ # stamped; an ancestor on another surface stops the walk.
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", ("chain-root",))
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", ("chain-tip",))
+ db._conn.commit()
+ db.update_token_counts("chain-tip", input_tokens=1, model="m", api_call_count=1, source="cli")
+ assert db.get_session("chain-root")["created_source"] == "cli"
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", ("chain-root",))
+ db._conn.execute("UPDATE sessions SET created_source='tui' WHERE id=?", ("chain-mid",))
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", ("chain-tip",))
+ db._conn.commit()
+ db.update_token_counts("chain-tip", input_tokens=1, model="m", api_call_count=1, source="cli")
+ assert [db.get_session(sid)["created_source"] for sid in ("chain-root", "chain-mid", "chain-tip")] == [
+ None, "tui", "cli"]
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source='cli' WHERE id IN ('chain-root', 'chain-mid')")
+ db._conn.commit()
capsys.readouterr()
_cmd_list(db, Namespace(limit=20, source="cli", workspace=None))
listed = capsys.readouterr().out