Skip to content

The agent that grows with you

fix(state): session counts count a local reset lineage once, like the listing

Tekniumb8ac353c07de
diff --git a/hermes_state_local.py b/hermes_state_local.py
index 10c993196f7..8b52a91cdbb 100644
--- a/hermes_state_local.py
+++ b/hermes_state_local.py
@@ -150,6 +150,17 @@ def local_lineage_index(conn):
return superseded, heads
+def exclude_superseded_segments(db, where_clauses, params):
+ """Drop earlier reset segments from a listing or count query (one conversation, one row),
+ in SQL before paging; returns ``heads`` for :func:`annotate_local_lineages`."""
+ with db._read_ctx() as conn:
+ superseded, heads = local_lineage_index(conn)
+ if superseded:
+ where_clauses.append("s.id NOT IN (SELECT value FROM json_each(?))")
+ params.append(json.dumps(sorted(superseded)))
+ return heads
+
+
def annotate_local_lineages(sessions, heads):
"""Name every segment on the conversation's representative row (``_lineage_ids``, which the
Desktop matches deletes, tombstones and stored ids against). ``_lineage_root_id`` stays the
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index 63b587704c1..47980d5b393 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -1334,15 +1334,11 @@ class SessionSessionsMixin:
if not include_hidden and not archived_only:
where_clauses.append("s.hidden = 0")
heads = {}
+ from hermes_state_local import exclude_superseded_segments
if not include_children:
# A local reset lineage is one conversation (hermes_state_local.local_lineage_index):
# earlier segments leave the page in SQL, before LIMIT/OFFSET, not after it.
- from hermes_state_local import local_lineage_index
- with self._read_ctx() as conn:
- superseded, heads = local_lineage_index(conn)
- if superseded:
- where_clauses.append("s.id NOT IN (SELECT value FROM json_each(?))")
- params.append(json.dumps(sorted(superseded)))
+ heads = exclude_superseded_segments(self, where_clauses, params)
where_sql = _where_sql(where_clauses)
# Shared projection head of the three list queries (whitespace is part of the SQL text).
select_head = (
@@ -1418,8 +1414,7 @@ class SessionSessionsMixin:
if not include_hidden and not archived_only:
pinned_clauses.append("s.hidden = 0")
if heads:
- pinned_clauses.append("s.id NOT IN (SELECT value FROM json_each(?))")
- pinned_params.append(json.dumps(sorted(superseded)))
+ exclude_superseded_segments(self, pinned_clauses, pinned_params)
pinned_clauses.append("s.pinned = 1")
pinned_where = _where_sql(pinned_clauses)
pinned_query = f"""
@@ -1560,6 +1555,9 @@ 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,
)
+ if exclude_children:
+ from hermes_state_local import exclude_superseded_segments
+ exclude_superseded_segments(self, where_clauses, params)
return self._read_one(f"SELECT COUNT(*) FROM sessions s{_where_sql(where_clauses, ' ')}", params)[0]
def session_count_ge(self, n: int = 1) -> bool:
@@ -1575,6 +1573,9 @@ class SessionSessionsMixin:
exclude_children=exclude_children, archived_only=archived_only,
include_archived=include_archived,
)
+ if exclude_children:
+ from hermes_state_local import exclude_superseded_segments
+ exclude_superseded_segments(self, where_clauses, params)
with self._read_ctx() as conn:
if self._conn is None:
raise RuntimeError("SessionDB connection is closed")
diff --git a/tests/gateway/test_reset_lineage_session_count.py b/tests/gateway/test_reset_lineage_session_count.py
new file mode 100644
index 00000000000..5e623fcc35f
--- /dev/null
+++ b/tests/gateway/test_reset_lineage_session_count.py
@@ -0,0 +1,26 @@
+"""andrexibiza 26: the session counts that page a listing count a local reset lineage once, the
+same way list_sessions_rich lists it (an earlier reset segment is not a second conversation)."""
+import pytest
+
+from tests.gateway import test_local_reset_lineage_one_conversation as one_conversation
+
+reset_lineage = one_conversation.reset_lineage # the shared fixture
+
+
+async def test_reset_lineage_counts_as_one_conversation(reset_lineage):
+ db = reset_lineage.authority.db
+ listed = db.list_sessions_rich(limit=50)
+ assert [r['id'] for r in listed] == [reset_lineage.s1]
+ assert db.session_count(exclude_children=True) == len(listed) == 1
+ assert db.session_count_by_source(exclude_children=True) == {'gui': 1}
+ # The raw row count (exclude_children=False) still sees both physical rows.
+ assert db.session_count() == 2
+
+
+async def test_pinned_backfill_lists_a_reset_lineage_once(reset_lineage):
+ db = reset_lineage.authority.db
+ db._execute_write(lambda c: c.execute('UPDATE sessions SET pinned=1 WHERE id IN (?,?)',
+ (reset_lineage.s0, reset_lineage.s1)))
+ assert [r['id'] for r in db.list_sessions_rich(limit=50, include_pinned=True)] == [reset_lineage.s1]