Skip to content

The agent that grows with you

fix(cli): list a compression tip by its own created_source

David Dudok de Witbe0085d01219
diff --git a/hermes_state_common.py b/hermes_state_common.py
index b2aaeeb125a..9439ceabb71 100644
--- a/hermes_state_common.py
+++ b/hermes_state_common.py
@@ -359,10 +359,64 @@ def _placeholders(items) -> str:
return ",".join("?" for _ in range(items if isinstance(items, int) else len(items)))
-def _source_match_clause(sources, local_created_sources=None) -> tuple[str, list]:
+# 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]:
"""``sessions s`` source predicate. With ``local_created_sources``, a ``source='local'`` row
matches 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."""
+ 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.
+ """
labels = list(sources or [])
if not labels:
return "", []
@@ -372,11 +426,17 @@ def _source_match_clause(sources, local_created_sources=None) -> tuple[str, list
non_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:
- return local_sql, list(surfaces)
- return (
- f"(s.source IN ({_placeholders(non_local)}) OR {local_sql})",
- [*non_local, *surfaces],
- )
+ 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
# Ids per ``IN (?,...)`` list: SQLite caps bound parameters at SQLITE_MAX_VARIABLE_NUMBER (999 on builds
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index 044640f59ee..38f119b94f8 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -114,6 +114,7 @@ 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
@@ -134,7 +135,8 @@ 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)
+ source_clause, source_params = _source_match_clause(
+ include_sources, local_created_sources, match_compression_tip=match_compression_tip)
for clause, values in (
(source_clause, source_params),
("s.session_key = ?", [session_key] if session_key else []),
@@ -1349,6 +1351,7 @@ 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
@@ -1433,6 +1436,7 @@ 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/tests/hermes_cli/test_sessions_source_local.py b/tests/hermes_cli/test_sessions_source_local.py
index ec3f86fbe02..e47fecb1829 100644
--- a/tests/hermes_cli/test_sessions_source_local.py
+++ b/tests/hermes_cli/test_sessions_source_local.py
@@ -55,6 +55,39 @@ def test_stats_counts_cli_segments_while_list_shows_one_conversation(tmp_path, c
assert "cli: 2 sessions" in capsys.readouterr().out
+def 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.
+ """
+ from hermes_cli.gateway_session_store import SurfaceLabelReader
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ db.create_session("chain-root", source="local")
+ with db._lock:
+ db._conn.execute("UPDATE sessions SET created_source=NULL WHERE id=?", ("chain-root",))
+ db._conn.commit()
+ db.append_message("chain-root", role="user", content="hi")
+ db.publish_compression_child(
+ parent_session_id="chain-root", child_session_id="chain-mid", source="local",
+ messages=[{"role": "user", "content": "hi"}], require_compression_lease=False)
+ db.publish_compression_child(
+ parent_session_id="chain-mid", child_session_id="chain-tip", source="local",
+ messages=[{"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"
+ 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
+ db.close()
+
+
def test_reader_session_count_includes_gateway_rows_and_a_pre_column_store(tmp_path):
"""session_count through the reader matches a local/cli row, and a read-only
store from before the column still counts its source=cli rows without migrating."""