Skip to content

The agent that grows with you

fix(gateway): Kanban run_task's claim transaction leaves the owner loop

Tekniumc218ff023d37
diff --git a/gateway/session_kanban.py b/gateway/session_kanban.py
index 876e8dafeb5..826883f9a89 100644
--- a/gateway/session_kanban.py
+++ b/gateway/session_kanban.py
@@ -79,8 +79,9 @@ async def run_task(connection, params):
from hermes_state_local import local_receipt
from gateway.session_policy import restore_policy
authority = connection.authority
- # Resolved off-loop up front: no await may separate the saved-claim check, session
- # creation and the claim's write transaction below.
+ # Resolved off-loop up front: no await separates the saved-claim check from session
+ # creation. The Kanban claim check and its marker share one off-loop transaction; a
+ # concurrent retry of the same attempt meets that marker (same session) or a stale claim.
owner_db = str(await asyncio.to_thread(Path(authority.db.db_path).resolve))
# The dispatcher attempt is the durable request identity, including after the
# task closes. Retry checks the saved claim, never reinterprets updated cards.
@@ -104,23 +105,29 @@ async def run_task(connection, params):
import os
import psutil
context = json.loads(policy.kanban_json)
- with connect_closing(Path(context['db'])) as conn, kb.write_txn(conn):
- # Either this exact attempt was already admitted to this session (retry after a
- # crash between session creation and admission), or the claim is still current
- # now; a reclaimed/closed task never reaches resume/submit.
- marker = conn.execute("SELECT payload FROM task_events WHERE task_id=? AND run_id=? AND kind='owner_admitted' ORDER BY id DESC LIMIT 1",
- (params['task_id'], params['run_id'])).fetchone()
- if marker:
- if json.loads(marker[0])['session_id'] != ref.session_id:
- raise RuntimeStoreError('stale_kanban_claim')
- else:
- task = kb.get_task(conn, params['task_id'])
- if not (task and task.status == 'running' and task.current_run_id == params['run_id'] and task.claim_lock == params['claim_lock']):
- raise RuntimeStoreError('stale_kanban_claim')
- kb._append_event(conn, task.id, 'owner_admitted',
- {'db': owner_db, 'session_id': ref.session_id,
- 'request_id': request_id, 'pid': os.getpid(), 'birth': psutil.Process().create_time()},
- run_id=task.current_run_id)
+
+ def admit_claim():
+ # The Kanban DB is the cross-profile dispatch bus (busy_timeout up to 120 s): the claim
+ # check and its marker share one write transaction, off the owner loop.
+ with connect_closing(Path(context['db'])) as conn, kb.write_txn(conn):
+ # Either this exact attempt was already admitted to this session (retry after a
+ # crash between session creation and admission), or the claim is still current
+ # now; a reclaimed/closed task never reaches resume/submit.
+ marker = conn.execute("SELECT payload FROM task_events WHERE task_id=? AND run_id=? AND kind='owner_admitted' ORDER BY id DESC LIMIT 1",
+ (params['task_id'], params['run_id'])).fetchone()
+ if marker:
+ if json.loads(marker[0])['session_id'] != ref.session_id:
+ raise RuntimeStoreError('stale_kanban_claim')
+ else:
+ task = kb.get_task(conn, params['task_id'])
+ if not (task and task.status == 'running' and task.current_run_id == params['run_id'] and task.claim_lock == params['claim_lock']):
+ raise RuntimeStoreError('stale_kanban_claim')
+ kb._append_event(conn, task.id, 'owner_admitted',
+ {'db': owner_db, 'session_id': ref.session_id,
+ 'request_id': request_id, 'pid': os.getpid(), 'birth': psutil.Process().create_time()},
+ run_id=task.current_run_id)
+ from gateway.session_runtime_workers import tracked_write
+ await tracked_write(authority, admit_claim)
await connection.resume(ref, {})
receipt = await authority.submit(connection.actor, Submission(request_id, ref,
{'text': f'work kanban task {params["task_id"]}'}, 'queue'))
diff --git a/tests/gateway/test_session_kanban_run_off_loop.py b/tests/gateway/test_session_kanban_run_off_loop.py
new file mode 100644
index 00000000000..3c93600b13d
--- /dev/null
+++ b/tests/gateway/test_session_kanban_run_off_loop.py
@@ -0,0 +1,85 @@
+"""Kanban run_task's claim check and owner_admitted marker run off the owner loop (B1-26): the
+Kanban DB is the cross-profile dispatch bus (busy_timeout up to 120 s), so a dispatcher holding
+its writer must not freeze every session on the loop. Real Kanban SQLite, a real held writer."""
+import asyncio
+from contextlib import closing
+import sqlite3
+import threading
+import time
+from types import SimpleNamespace
+
+HOLD_S = 3.0
+
+
+def test_run_task_kanban_write_never_blocks_the_loop(tmp_path, monkeypatch):
+ from hermes_cli import kanban_db as kb
+ from hermes_cli.kanban_db_connect import connect
+ from gateway.session_contract import Principal
+ from gateway.session_kanban import run_task
+ monkeypatch.setenv('HERMES_KANBAN_HOME', str(tmp_path))
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ monkeypatch.setenv('HOME', str(tmp_path / 'home'))
+ monkeypatch.delenv('HERMES_KANBAN_DB', raising=False)
+ monkeypatch.delenv('HERMES_DELEGATED_CHILD', raising=False)
+ monkeypatch.setattr('hermes_cli.profiles.resolve_profile_env',
+ lambda name: str(tmp_path) if name == 'default' else str(tmp_path / name))
+ workspace = tmp_path / 'workspace'
+ workspace.mkdir()
+ kb.create_board('owned')
+ with closing(connect(board='owned')) as conn:
+ task_id = kb.create_task(conn, title='Owned', assignee='default', workspace_kind='dir', workspace_path=str(workspace))
+ kb.recompute_ready(conn)
+ task = kb.claim_task(conn, task_id)
+ board_db = conn.execute('PRAGMA database_list').fetchone()[2]
+ config = {'model': {'default': 'loop-model', 'provider': 'custom'}, 'platform_toolsets': {'cli': ['terminal']}}
+ monkeypatch.setattr('gateway.run._load_gateway_config', lambda: config)
+ monkeypatch.setattr('gateway.session_local.create_local_session', lambda *a, **k: SimpleNamespace(
+ session_id='owner-session', profile_id=str(tmp_path)))
+ calls = []
+
+ async def submit(actor, submission):
+ calls.append('submit')
+ return SimpleNamespace(admission_id='a')
+
+ async def resume(ref, params):
+ calls.append('resume')
+ db = SimpleNamespace(get_session=lambda sid: None, db_path=str(tmp_path / 'state.db'))
+ authority = SimpleNamespace(profile_id=str(tmp_path), db=db, submit=submit)
+ actor = Principal('owner', str(tmp_path), frozenset({'session:create'}), 'transport')
+ connection = SimpleNamespace(authority=authority, actor=actor, native_owner=True, resume=resume)
+ params = dict(board='owned', task_id=task_id, run_id=task.current_run_id, claim_lock=task.claim_lock)
+ held = threading.Event()
+
+ def dispatcher_holds_the_bus():
+ rival = sqlite3.connect(board_db, isolation_level=None)
+ rival.execute('BEGIN IMMEDIATE')
+ held.set()
+ time.sleep(HOLD_S)
+ rival.execute('ROLLBACK')
+ rival.close()
+ monkeypatch.setattr('gateway.session_kanban.asdict', lambda receipt: {})
+
+ async def scenario():
+ writer = threading.Thread(target=dispatcher_holds_the_bus, daemon=True)
+ writer.start()
+ assert await asyncio.to_thread(held.wait, 10)
+ ticks = []
+
+ async def ticker():
+ while True:
+ ticks.append(time.monotonic())
+ await asyncio.sleep(.05)
+ beat = asyncio.create_task(ticker())
+ try:
+ async with asyncio.timeout(30):
+ await run_task(connection, params)
+ finally:
+ beat.cancel()
+ ticks.append(time.monotonic()) # a loop blocked from the first tick still yields one gap
+ await asyncio.to_thread(writer.join, 10)
+ return max(b - a for a, b in zip(ticks, ticks[1:]))
+ gap = asyncio.run(scenario())
+ assert calls == ['resume', 'submit']
+ with closing(connect(board='owned')) as conn:
+ assert conn.execute("SELECT count(*) FROM task_events WHERE task_id=? AND kind='owner_admitted'", (task_id,)).fetchone()[0] == 1
+ assert gap < HOLD_S / 2, f'the owner loop stalled {gap:.2f}s behind a held Kanban writer'