Skip to content

The agent that grows with you

fix(kanban): refuse a reclaimed claim before owner admission (X12, reported by @Xipong)

teknium1e2c0cd25c326
diff --git a/gateway/session_kanban.py b/gateway/session_kanban.py
index eef41e63198..8b02ec139f5 100644
--- a/gateway/session_kanban.py
+++ b/gateway/session_kanban.py
@@ -96,17 +96,22 @@ async def run_task(connection, params):
import psutil
context = json.loads(policy.kanban_json)
with connect_closing(Path(context['db'])) as conn, kb.write_txn(conn):
- task = kb.get_task(conn, params['task_id'])
- if task and task.status == 'running' and task.current_run_id == params['run_id'] and task.claim_lock == params['claim_lock']:
- marker = conn.execute("SELECT payload FROM task_events WHERE task_id=? AND run_id=? AND kind='owner_admitted' ORDER BY id DESC LIMIT 1",
- (task.id, task.current_run_id)).fetchone()
- if marker and json.loads(marker[0])['session_id'] != ref.session_id:
+ # 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')
- if not marker:
- kb._append_event(conn, task.id, 'owner_admitted',
- {'db': str(Path(authority.db.db_path).resolve()), 'session_id': ref.session_id,
- 'request_id': request_id, 'pid': os.getpid(), 'birth': psutil.Process().create_time()},
- run_id=task.current_run_id)
+ 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': str(Path(authority.db.db_path).resolve()), 'session_id': ref.session_id,
+ 'request_id': request_id, 'pid': os.getpid(), 'birth': psutil.Process().create_time()},
+ run_id=task.current_run_id)
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.py b/tests/gateway/test_session_kanban.py
index 75cbbf0e6c7..37f8e5be672 100644
--- a/tests/gateway/test_session_kanban.py
+++ b/tests/gateway/test_session_kanban.py
@@ -91,3 +91,52 @@ def test_late_launcher_cannot_stamp_replacement_claim(tmp_path, monkeypatch):
dispatch._set_worker_pid(conn, tid, 424242, run_id=old.current_run_id, claim_lock=old.claim_lock)
assert kb.get_task(conn, tid).worker_pid is None
assert conn.execute('SELECT worker_pid FROM task_runs WHERE id=?', (new.current_run_id,)).fetchone()[0] is None
+
+
+def test_claim_reclaimed_before_admission_marker_is_refused(tmp_path, monkeypatch):
+ """A claim that stops being current between policy/session creation and the marker
+ transaction is refused: no owner_admitted row, no resume, no submit."""
+ import asyncio
+ from contextlib import closing
+ from hermes_cli import kanban_db as kb
+ from hermes_cli.kanban_db_connect import connect
+ from gateway.session_contract import Principal
+ from hermes_state_runtime import RuntimeStoreError
+ 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()
+ with closing(connect(board='owned')) as conn:
+ task_id = kb.create_task(conn, title='Owned race', assignee='default', workspace_kind='dir', workspace_path=str(workspace))
+ kb.recompute_ready(conn)
+ old = kb.claim_task(conn, task_id)
+ config = {'model': {'default': 'loop-model', 'provider': 'custom'}, 'platform_toolsets': {'cli': ['terminal']}}
+ monkeypatch.setattr('gateway.run._load_gateway_config', lambda: config)
+ calls = []
+ def create_local_session(authority, actor, params, *, trusted_policy=None, trusted_secrets=None):
+ # Another process reclaims the task while the owner session is being created.
+ with closing(connect(board='owned')) as conn:
+ kb.block_task(conn, task_id, reason='replace claim', expected_run_id=old.current_run_id)
+ kb.unblock_task(conn, task_id)
+ kb.claim_task(conn, task_id)
+ return SimpleNamespace(session_id='owner-session', profile_id=str(tmp_path))
+ monkeypatch.setattr('gateway.session_local.create_local_session', create_local_session)
+ async def submit(actor, submission):
+ calls.append(('submit', submission))
+ async def resume(ref, params):
+ calls.append(('resume', ref))
+ 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=old.current_run_id, claim_lock=old.claim_lock)
+ from gateway.session_kanban import run_task
+ with pytest.raises(RuntimeStoreError, match='stale_kanban_claim'):
+ asyncio.run(run_task(connection, params))
+ assert calls == []
+ 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] == 0