The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(state): stamp compression ancestors at write time, drop list-time walk
kshitijk4poore3cc0ca0cb22
diff --git a/hermes_state_common.py b/hermes_state_common.pyindex 9439ceabb71..b2aaeeb125a 100644--- a/hermes_state_common.py+++ b/hermes_state_common.py@@ -359,64 +359,10 @@ def _placeholders(items) -> str:return ",".join("?" for _ in range(items if isinstance(items, int) else len(items)))-# Same preferred-child edge as hermes_state_compression._CHAIN_STEP_SQL. A list page-# hides compression continuations and then shows the root as its tip, so the source-# filter has to admit the root when that tip's created_source matches.-_COMPRESSION_STEP_EDGE_SQL = (- "parent.end_reason = 'compression'\n"- f" AND {_sql_json_extract('child.model_config', '$._branched_from')} IS NULL\n"- f" AND {_sql_json_extract('child.model_config', '$._delegate_from')} IS NULL\n"- f" AND NOT ({_RESET_CHILD_SQL.format(a='child')})\n"- " AND COALESCE(child.source, '') != 'tool'"-)-_COMPRESSION_STEP_ORDER_SQL = (- "ORDER BY\n"- " CASE\n"- " WHEN child.end_reason = 'compression' THEN 0\n"- " WHEN child.ended_at IS NULL THEN 1\n"- " ELSE 2\n"- " END,\n"- f" {_sql_session_last_active('child')} DESC,\n"- " child.started_at DESC,\n"- " child.id DESC\n"- " LIMIT 1"-)---def _compression_tip_surface_sql(surfaces) -> tuple[str, list]:- """``s`` (a list root) whose compression-chain tip is ``source='local'`` with- ``created_source`` in *surfaces*. One preferred child per hop, same walk as- ``get_compression_chain``."""- step = (- "SELECT child.id FROM sessions parent "- "JOIN sessions child ON child.parent_session_id = parent.id "- "WHERE parent.id = {parent} AND "- + _COMPRESSION_STEP_EDGE_SQL + "\n "- + _COMPRESSION_STEP_ORDER_SQL- )- sql = (- "EXISTS (SELECT 1 FROM sessions tip WHERE tip.id = ("- "SELECT id FROM ("- "WITH RECURSIVE walk(id, n) AS ("- f"SELECT ({step.format(parent='s.id')}), 1 "- "UNION ALL "- f"SELECT ({step.format(parent='walk.id')}), walk.n + 1 "- "FROM walk WHERE walk.id IS NOT NULL AND walk.n < 1000"- ") SELECT id FROM walk WHERE id IS NOT NULL ORDER BY n DESC LIMIT 1"- ")) AND tip.source = 'local' "- f"AND tip.created_source IN ({_placeholders(surfaces)}))"- )- return sql, list(surfaces)---def _source_match_clause(sources, local_created_sources=None, *, match_compression_tip=False) -> tuple[str, list]:+def _source_match_clause(sources, local_created_sources=None) -> tuple[str, list]:"""``sessions s`` source predicate. With ``local_created_sources``, a ``source='local'`` rowmatches only when ``created_source`` is one of those surfaces; the placeholder ``'unknown'``- is not a surface and stays unmatched until an upsert stamps the real creator.-- ``match_compression_tip`` also admits a list root whose chain tip matches. List hides the- tip and paints the root as that tip, so a root still NULL must not hide a healed tip.- """+ is not a surface and stays unmatched until an upsert stamps the real creator."""labels = list(sources or [])if not labels:return "", []@@ -426,17 +372,11 @@ def _source_match_clause(sources, local_created_sources=None, *, match_compressinon_local = [item for item in labels if item != "local"]local_sql = f"(s.source = 'local' AND s.created_source IN ({_placeholders(surfaces)}))"if not non_local:- clause, params = local_sql, list(surfaces)- else:- clause, params = (- f"(s.source IN ({_placeholders(non_local)}) OR {local_sql})",- [*non_local, *surfaces],- )- if match_compression_tip:- tip_sql, tip_params = _compression_tip_surface_sql(surfaces)- clause = f"({clause} OR {tip_sql})"- params = [*params, *tip_params]- return clause, params+ return local_sql, list(surfaces)+ return (+ f"(s.source IN ({_placeholders(non_local)}) OR {local_sql})",+ [*non_local, *surfaces],+ )# Ids per ``IN (?,...)`` list: SQLite caps bound parameters at SQLITE_MAX_VARIABLE_NUMBER (999 on buildsdiff --git a/hermes_state_compression.py b/hermes_state_compression.pyindex f7055852e95..c4a43ba5dcf 100644--- a/hermes_state_compression.py+++ b/hermes_state_compression.py@@ -105,6 +105,34 @@ def _walk_compression_chain(session_id: str, step) -> list[str]:return chain+def stamp_compression_ancestors(conn, session_id: str) -> None:+ """Copy *session_id*'s ``created_source`` onto compression ancestors that have none.++ A list page shows a compressed conversation as its root, painted as the tip, and filters on+ 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.+ """+ row = conn.execute("SELECT parent_session_id, created_source FROM sessions WHERE id = ?",+ (session_id,)).fetchone()+ surface = row["created_source"] if row else None+ if surface in (None, "unknown"):+ return+ current, parent_id = session_id, row["parent_session_id"]+ 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"):+ 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))+ current, parent_id = parent_id, parent["parent_session_id"]++class SessionCompressionMixin:"""Compression lineage, cooldown/streak counters, locks and turn leases."""diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex f1814bdd189..ea141db2c84 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -901,6 +901,8 @@ def _worker_usage(db, conn, session_id, payload, *, auxiliary=False):conn.execute("UPDATE sessions SET created_source=? WHERE id=? AND created_source IS NULL",(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.pyindex c93e9ca42de..46b855a0312 100644--- a/hermes_state_sessions.py+++ b/hermes_state_sessions.py@@ -114,7 +114,6 @@ def _session_filter_where(session_key: str | None = None, exclude_sources: list[str] | None = None, cwd_prefix: str | None = None,min_message_count: int = 0, archived_only: bool = False, include_archived: bool = False,include_subagents: bool = False, local_created_sources: Sequence[str] | None = None,- match_compression_tip: bool = False,) -> tuple[list[str], list[Any]]:"""Shared ``sessions s`` WHERE builder so counts line up with listed rows. ``exclude_children``hides sub-agent runs and compression continuations but keeps branch/reset children@@ -135,8 +134,7 @@ def _session_filter_where(# legacy heuristic (parent ended with 'branched' before the child started), covering branch sessions# created before the marker existed.include_sources = [source] if source else list(sources or [])- source_clause, source_params = _source_match_clause(- include_sources, local_created_sources, match_compression_tip=match_compression_tip)+ source_clause, source_params = _source_match_clause(include_sources, local_created_sources)for clause, values in ((source_clause, source_params),("s.session_key = ?", [session_key] if session_key else []),@@ -1353,7 +1351,6 @@ class SessionSessionsMixin:exclude_sources=exclude_sources, cwd_prefix=cwd_prefix, min_message_count=min_message_count,archived_only=archived_only, include_archived=include_archived, include_subagents=include_subagents,local_created_sources=local_created_sources,- match_compression_tip=bool(local_created_sources) and not include_children,)# The archived-only view is the recovery surface for rows that dropped out of every# default list: a session that is archived AND hidden (Bot Mode marks its sessions@@ -1438,7 +1435,6 @@ class SessionSessionsMixin:session_key=session_key, exclude_sources=exclude_sources, cwd_prefix=cwd_prefix,min_message_count=min_message_count, archived_only=False, include_archived=True,include_subagents=include_subagents, local_created_sources=local_created_sources,- match_compression_tip=bool(local_created_sources) and not include_children,)if not include_hidden and not archived_only:pinned_clauses.append("s.hidden = 0")diff --git a/hermes_state_worker_lifecycle.py b/hermes_state_worker_lifecycle.pyindex 31df60e2d0a..86b12b2c24e 100644--- a/hermes_state_worker_lifecycle.py+++ b/hermes_state_worker_lifecycle.py@@ -272,3 +272,6 @@ def insert_session_row_in_transaction(self._delete_unreferenced_system_prompts(conn)if parent_session_id:self._inherit_parent_session_metadata(conn, session_id)+ # The upsert may have just filled a compression tip's created_source (update_token_counts).+ from hermes_state_compression import stamp_compression_ancestors+ stamp_compression_ancestors(conn, session_id)diff --git a/tests/hermes_cli/test_sessions_source_local.py b/tests/hermes_cli/test_sessions_source_local.pyindex e47fecb1829..6c8cae04ce1 100644--- a/tests/hermes_cli/test_sessions_source_local.py+++ b/tests/hermes_cli/test_sessions_source_local.py@@ -58,8 +58,8 @@ def test_stats_counts_cli_segments_while_list_shows_one_conversation(tmp_path, cdef test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_path):"""Search and stats see a tip healed to cli. List must show that conversation too.- The page hides compression children and paints the root as the tip, so a root- whose created_source is still NULL used to drop the tip from the page.+ The page hides compression children and paints the root as the tip, so the usage+ write that heals the tip also stamps the NULL ancestors of that chain."""from hermes_cli.gateway_session_store import SurfaceLabelReader@@ -77,14 +77,13 @@ def test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_pamessages=[{"role": "user", "content": "hi"}], require_compression_lease=False)db.update_token_counts("chain-tip", input_tokens=2, output_tokens=1, model="m", api_call_count=1, source="cli")- assert db.get_session("chain-root")["created_source"] is None- assert db.get_session("chain-mid")["created_source"] is None- assert db.get_session("chain-tip")["created_source"] == "cli"+ assert [db.get_session(sid)["created_source"] for sid in ("chain-root", "chain-mid", "chain-tip")] == [+ "cli", "cli", "cli"]reader = SurfaceLabelReader(db)listed = [row["id"] for row in reader.list_sessions_rich(source="cli", limit=10)]assert listed == ["chain-tip"]assert "chain-tip" in [row["id"] for row in reader.search_sessions(source="cli", limit=10)]- assert reader.session_count(source="cli") == 1+ assert reader.session_count(source="cli") == 3 # stats counts every segmentdb.close()