The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui-gateway): keep activity heartbeats out of the sessions digest
Brooklyn Nicholsond903f3af42be
diff --git a/tests/tui_gateway/test_change_watcher.py b/tests/tui_gateway/test_change_watcher.pyindex a008fa2f5e2..75944972a64 100644--- a/tests/tui_gateway/test_change_watcher.py+++ b/tests/tui_gateway/test_change_watcher.py@@ -82,6 +82,78 @@ def test_state_db_move_broadcasts_sessions_changed(watcher_home):assert ("sessions.changed", {}) in events+def _seed_store(db_path):+ conn = sqlite3.connect(db_path)+ conn.executescript(+ "CREATE TABLE sessions (id TEXT PRIMARY KEY, title TEXT, started_at REAL, "+ "message_count INTEGER, last_activity_at REAL, last_activity_description TEXT);"+ "CREATE TABLE gateway_heartbeats (backend_id TEXT PRIMARY KEY, last_heartbeat REAL);"+ "INSERT INTO sessions VALUES ('s1', 'hello', 1, 3, 1, NULL);"+ "INSERT INTO gateway_heartbeats VALUES ('backend-1', 1);"+ )+ conn.commit()+ conn.close()+++def _write(db_path, sql, *params):+ conn = sqlite3.connect(db_path)+ conn.execute(sql, params)+ conn.commit()+ conn.close()+++def test_activity_heartbeats_do_not_broadcast_sessions_changed(watcher_home):+ """#98005: the gateway heartbeat and the session activity stamp rewrite state.db every+ minute with no session change. Each used to fire sessions.changed, so the Desktop+ Sessions panel refreshed on its own every heartbeat window."""+ home, events = watcher_home+ db = home / "state.db"+ _seed_store(db)+ server._broadcast_watched_changes(now=0.0)++ for tick in range(1, 4):+ time.sleep(0.02)+ _write(db, "UPDATE gateway_heartbeats SET last_heartbeat = ?", tick * 60.0)+ _write(db, "UPDATE sessions SET last_activity_at = ?, last_activity_description = ? "+ "WHERE id = 's1'", tick * 60.0, f"tool {tick}")+ server._broadcast_watched_changes(now=tick * 10.0)++ assert ("sessions.changed", {}) not in events++[email protected]("sql", [+ "INSERT INTO sessions VALUES ('s2', 'new', 2, 0, 2, NULL)",+ "UPDATE sessions SET title = 'renamed' WHERE id = 's1'",+ "UPDATE sessions SET message_count = 4 WHERE id = 's1'",+ "DELETE FROM sessions WHERE id = 's1'",+])+def test_session_row_changes_broadcast_sessions_changed(watcher_home, sql):+ home, events = watcher_home+ db = home / "state.db"+ _seed_store(db)+ server._broadcast_watched_changes(now=0.0)++ time.sleep(0.02)+ _write(db, sql)+ server._broadcast_watched_changes(now=10.0)++ assert ("sessions.changed", {}) in events+++def test_unreadable_store_keeps_last_digest(watcher_home):+ """A locked/unreadable moment after a good read must not flip to the mtime signature:+ digest -> mtime -> digest would broadcast twice for nothing."""+ home, events = watcher_home+ db = home / "state.db"+ _seed_store(db)+ server._broadcast_watched_changes(now=0.0)++ db.write_text("not-sqlite")+ server._broadcast_watched_changes(now=10.0)++ assert ("sessions.changed", {}) not in events++def test_projects_db_move_broadcasts_projects_changed(watcher_home):"""#53046 / #56757: the CLI and other windows write projects.db directly, inprocesses that never touch this gateway's transports. Without a watch, adiff --git a/tui_gateway/change_watcher.py b/tui_gateway/change_watcher.pyindex 872d72f8d7a..0e4e8067ce1 100644--- a/tui_gateway/change_watcher.py+++ b/tui_gateway/change_watcher.py@@ -119,12 +119,14 @@ def _pet_changed_payload() -> dict:return {"enabled": False}+# last_activity_at / _description are left out on purpose: the session activity heartbeat restamps+# them mid-turn and on idle ticks, so hashing them re-fired sessions.changed every heartbeat window+# (#98005). Liveness comes from session.active_list, and a real turn still moves message_count._SESSION_SIGNATURE_FIELDS = ("id", "source", "session_key", "display_name", "model", "parent_session_id","started_at", "ended_at", "end_reason", "message_count", "tool_call_count",- "cwd", "git_branch", "git_repo_root", "title", "title_source",- "last_activity_at", "last_activity_description", "profile_name", "archived",- "pinned", "hidden", "last_read_at", "handoff_state",+ "cwd", "git_branch", "git_repo_root", "title", "title_source", "profile_name",+ "archived", "pinned", "hidden", "last_read_at", "handoff_state",)# path -> (database/WAL mtime, session-table digest). The mtime guard keeps the# normal 0.5 s watch pass stat-only; SQLite is read only after another process@@ -155,10 +157,11 @@ def _session_db_content_sig(db_path: Path):conn = Nonetry:import hashlib- import sqlite3- from urllib.parse import quote+ from hermes_state import _connect_tracked_db+ from hermes_state_holders import read_only_db_uri- conn = sqlite3.connect(f"file:{quote(str(db_path))}?mode=ro", uri=True, timeout=0.05)+ conn = _connect_tracked_db(read_only_db_uri(db_path), tracking_path=db_path,+ uri=True, timeout=0.05)available = {row[1] for row in conn.execute("PRAGMA table_info(sessions)")}fields = tuple(field for field in _SESSION_SIGNATURE_FIELDS if field in available)if not fields:@@ -175,6 +178,10 @@ def _session_db_content_sig(db_path: Path):digest.update(b"\0")signature = (fields, digest.digest())except Exception: # noqa: BLE001 - preserve the old wake-up signal if the read probe cannot run+ # A busy/locked read after a good one keeps the last digest and leaves the cached mtime+ # stale so the next pass re-reads: digest -> mtime -> digest would broadcast twice.+ if cached is not None and cached[1] is not None and cached[1][0] != "mtime-fallback":+ return cached[1]signature = ("mtime-fallback", mtime)finally:if conn is not None: