Skip to content

The agent that grows with you

fix(state): the routing label local is never creation provenance

kshitijk4poora8ec6b933741
diff --git a/hermes_state_compression.py b/hermes_state_compression.py
index 2c7a125d9aa..ee58931ceab 100644
--- a/hermes_state_compression.py
+++ b/hermes_state_compression.py
@@ -105,8 +105,11 @@ def _walk_compression_chain(session_id: str, step) -> list[str]:
return chain
-# created_source values the creator upsert treats as unset (NULLIF(..., 'unknown')).
-_UNSET_CREATED_SOURCES = (None, "unknown")
+# created_source values that are not creation provenance: unset, the accounting placeholder
+# ``unknown`` and the routing label ``local`` (the store-side copy of session_policy._NOT_A_SURFACE).
+# The creator upsert, the managed-worker heal and the ancestor stamp may replace them, nothing else.
+_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.
@@ -118,7 +121,7 @@ _UNSET_ANCESTOR_SQL = """
WHERE s.created_source = ? AND up.n < ?
)
SELECT 1 FROM up JOIN sessions s ON s.id = up.pid
- WHERE NULLIF(s.created_source, 'unknown') IS NULL
+ WHERE """ + _UNSET_CREATED_SQL.format(col="s.created_source") + """
LIMIT 1
"""
@@ -130,8 +133,8 @@ def stamp_compression_ancestors(conn, session_id: str) -> None:
the root's ``created_source``. Automatic compression publishes the child before any writer
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. An ancestor already on the same surface is walked through
+ must be the parent's preferred continuation (``_CHAIN_STEP_SQL``); ``unknown`` and ``local``
+ count as 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.
A chain with no unset ancestor returns after one query (``_UNSET_ANCESTOR_SQL``).
"""
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 00e667993ad..838e6fc2971 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -897,13 +897,13 @@ def _worker_usage(db, conn, session_id, payload, *, auxiliary=False):
# The managed worker's platform is the policy label (gui), not the in-process
# agent surface (desktop). Stamp the same created_surface the insert uses.
from gateway.session_policy import created_surface
+ from hermes_state_compression import _UNSET_CREATED_SQL, stamp_compression_ancestors
surfaced = created_surface(source)
if surfaced:
conn.execute(
- "UPDATE sessions SET created_source=? WHERE id=? "
- "AND NULLIF(created_source, 'unknown') IS NULL",
+ "UPDATE sessions SET created_source=? WHERE id=? AND "
+ + _UNSET_CREATED_SQL.format(col="created_source"),
(surfaced, session_id))
- from hermes_state_compression import stamp_compression_ancestors
stamp_compression_ancestors(conn, session_id)
return {'value': None}
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index 5de4d7624dd..6bffd01cede 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -23,7 +23,7 @@ from hermes_state_common import (
escape_like as _escape_like, _SQL_IN_CHUNK, _id_chunks, _placeholders as _session_ids_placeholders,
_source_match_clause, emit_session_deleted, launch_stub_filter_sql,
)
-from hermes_state_compression import _CHAIN_CAP
+from hermes_state_compression import _CHAIN_CAP, _UNSET_CREATED_SQL
# caplog tests pin the "hermes_state" logger name.
logger = logging.getLogger("hermes_state")
@@ -279,10 +279,13 @@ _UPSERT_KEEP_EXISTING_SQL = ",\n".join(
"transport_profile", "git_repo_root", "origin_json", "display_name",
)
) + ",\n" + (
- # Accounting may insert before the creator: 'unknown' is not creation provenance.
- # Once a real source is stamped, later routing/constructor writes must preserve it (#56439).
- " created_source = COALESCE("
- "NULLIF(sessions.created_source, 'unknown'), excluded.created_source)"
+ # Accounting may insert before the creator ('unknown') and a routing write carries 'local':
+ # neither is creation provenance (_UNSET_CREATED_SQL), so a later creator replaces them and
+ # 'local' never replaces anything. A real source, once stamped, is preserved (#56439).
+ " created_source = CASE WHEN "
+ + _UNSET_CREATED_SQL.format(col="sessions.created_source")
+ + " THEN COALESCE(NULLIF(excluded.created_source, 'local'), sessions.created_source)"
+ " ELSE sessions.created_source END"
)
diff --git a/tests/hermes_state/test_placeholder_provenance.py b/tests/hermes_state/test_placeholder_provenance.py
index f5b6eda9b65..8ca7fa216ed 100644
--- a/tests/hermes_state/test_placeholder_provenance.py
+++ b/tests/hermes_state/test_placeholder_provenance.py
@@ -20,3 +20,20 @@ def test_placeholder_creator_remains_visible_after_gateway_routing(tmp_path):
assert db.get_session("previous")["input_tokens"] == 5
finally:
db.close()
+
+
+def test_routing_label_local_is_not_creation_provenance(tmp_path):
+ """An upsert carrying the routing label ``local`` never stamps it, and a row that already holds
+ ``local`` (or the ``unknown`` placeholder) is repaired by the real creator."""
+ db = SessionDB(db_path=tmp_path / "state.db")
+ try:
+ db.update_token_counts("placeholder", input_tokens=1)
+ db.create_session("placeholder", source="local")
+ assert db.get_session("placeholder")["created_source"] == "unknown"
+ db.create_session("frozen", source="local")
+ assert db.get_session("frozen")["created_source"] == "local"
+ for sid in ("placeholder", "frozen"):
+ db.create_session(sid, source="cli")
+ assert db.get_session(sid)["created_source"] == "cli", sid
+ finally:
+ db.close()