The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(state): index worker_executions(session_id,status) and probe a lineage in one query
Tekniumdcbcefc57d9b
diff --git a/hermes_state_common.py b/hermes_state_common.pyindex ef82117aed9..624fd247e1d 100644--- a/hermes_state_common.py+++ b/hermes_state_common.py@@ -537,6 +537,10 @@ CREATE TABLE IF NOT EXISTS worker_executions (adoption_digest TEXT NOT NULL,last_sequence INTEGER NOT NULL DEFAULT 0 CHECK (last_sequence >= 0));+-- Lineage-wide worker exclusion (claim/register/reset) probes every row of a conversation's lineage+-- inside the write transaction; without this each probe scanned the whole (only-growing) table.+CREATE INDEX IF NOT EXISTS idx_worker_executions_session_status+ ON worker_executions(session_id, status);CREATE TABLE IF NOT EXISTS worker_receipts (execution_id TEXT NOT NULL REFERENCES worker_executions(execution_id) ON DELETE RESTRICT,sequence INTEGER NOT NULL CHECK (sequence > 0),diff --git a/hermes_state_runtime_workers.py b/hermes_state_runtime_workers.pyindex c4dfde484ad..1d76f574a00 100644--- a/hermes_state_runtime_workers.py+++ b/hermes_state_runtime_workers.py@@ -24,8 +24,10 @@ def runtime_lineage(conn, session_id):def worker_states(conn, session_id):- return {row[0] for sid in runtime_lineage(conn, session_id) for row in conn.execute(- "SELECT status FROM worker_executions WHERE session_id=? AND status!='terminal'", (sid,))}+ # One indexed probe for the whole lineage (json_each: no bound-parameter limit on long chains).+ return {row[0] for row in conn.execute(+ "SELECT DISTINCT status FROM worker_executions WHERE session_id IN (SELECT value FROM json_each(?)) "+ "AND status IN ('registered','running','unknown')", (json.dumps(sorted(runtime_lineage(conn, session_id))),))}def discard_orphan_workers_on_reset(conn, session_ids):diff --git a/tests/hermes_state/test_worker_lineage_index.py b/tests/hermes_state/test_worker_lineage_index.pynew file mode 100644index 00000000000..470e97f6697--- /dev/null+++ b/tests/hermes_state/test_worker_lineage_index.py@@ -0,0 +1,30 @@+"""Lineage worker exclusion is one indexed probe, also on a state.db created before the index."""+import sqlite3++from hermes_state import SessionDB+++def test_lineage_worker_probe_is_indexed_on_new_and_upgraded_stores(tmp_path):+ from hermes_state_runtime_workers import worker_states+ path = tmp_path / 'state.db'+ with SessionDB(path) as db:+ db.create_session('root', source='cli')+ raw = sqlite3.connect(path)+ raw.execute('DROP INDEX IF EXISTS idx_worker_executions_session_status') # an older store+ raw.commit()+ raw.close()+ with SessionDB(path) as db: # reopening migrates it; doing it twice must be a no-op+ pass+ with SessionDB(path) as db:+ for parent, child in (('root', 'c1'), ('c1', 'c2')):+ db.create_session(child, 'cli', parent_session_id=parent)+ db.end_session(parent, 'compression')+ statements = []+ with db._read_ctx() as conn:+ conn.set_trace_callback(statements.append)+ assert worker_states(conn, 'root') == set()+ conn.set_trace_callback(None)+ probes = [s for s in statements if 'FROM worker_executions' in s]+ assert len(probes) == 1, f'one probe per lineage row: {probes}'+ plan = ' '.join(str(r[-1]) for r in conn.execute('EXPLAIN QUERY PLAN ' + probes[0]))+ assert 'idx_worker_executions_session_status' in plan, plan