Files
hermes-agent/tools/async_delegation_worker.py
Teknium df08578643 fix(worker): route the async-delegation ledger's dispatch writes off Relay's loop
External review of #141334: `_persist_dispatch` -> `WorkerDelegationLedger.dispatch`
(and the `unscheduled` mark) run inside a tool callback on Relay's loop, so the
`synchronous_rpc_on_event_loop` guard still fired and left the store failed. Both now
go through `call_off_event_loop`, like the activity and aux-usage writers.

Tests: a red/green test drives `_persist_dispatch` + `unscheduled` inside
`relay_tools.execute`; one more drives the real `tool_executor._begin_tool_execution`
frame from the reporter's ThreadPoolExecutor stack (passes at the prior head, guards
against new store calls on that frame). The label-clear test's docstring now says it
is defence in depth, not a reproduced caller.

Refs #139029
Refs #134889 (KB-2)
2026-10-18 20:50:03 -07:00

82 lines
3.5 KiB
Python

"""Process-bound worker ledger routing through the existing durable outbox.
Binding is private runtime wiring, not model tool surface or a storage fallback.
Fresh executor/helper threads retain the registered interpreter's owner boundary.
"""
import os
import threading
from agent.runtime_session_store import WorkerPersistenceError, call_off_event_loop, is_worker_process
_store = None
_lock = threading.Lock()
def bind_worker_delegation_store(store):
global _store
if not is_worker_process() or store.scope['pid'] != os.getpid():
raise WorkerPersistenceError('permission_denied')
with _lock:
if _store is not None and _store is not store:
raise WorkerPersistenceError('worker_delegation_already_bound')
_store = store
def worker_ledger():
if not is_worker_process():
return None
with _lock:
if _store is None or _store.scope['pid'] != os.getpid():
raise WorkerPersistenceError('worker_delegation_ledger_unbound')
return WorkerDelegationLedger(_store)
def require_ledger_owner():
if is_worker_process():
raise WorkerPersistenceError('worker_delegation_owner_only')
class WorkerDelegationLedger:
def __init__(self, store):
self.store = store
def dispatch(self, record):
if record.get('parent_session_id') != self.store.scope['session_id']:
raise WorkerPersistenceError('permission_denied')
from tools.async_delegation import _ROUTING_KEYS
if any(record.get(key) for key in _ROUTING_KEYS):
raise WorkerPersistenceError('unsupported_producer')
# Reached from a tool callback, on Relay's loop (#139029).
return call_off_event_loop(self.store._apply, 'delegation.dispatch', {
key: record.get(key, '') for key in ('delegation_id', 'parent_session_id', 'session_key',
'origin_ui_session_id', 'origin_session_id', 'dispatched_at')} | {'task': {
key: record[key] for key in ('goal', 'goals', 'context', 'toolsets', 'role', 'model', 'is_batch', 'task_indexes')
if key in record}})['value']
def complete(self, event, result):
return self.store._apply('delegation.complete', {'event': event, 'result': result})['value']
def child(self, delegation_id, entry):
return self.store._apply('delegation.child', {'delegation_id': delegation_id, 'entry': entry})['value']
def read(self, delegation_id):
return self.store._apply('delegation.read', {'delegation_id': delegation_id})['value']
def claim(self, delegation_id, claim_id):
return self.store._apply('delegation.claim', {'delegation_id': delegation_id, 'claim_id': claim_id})['value']
def ack(self, delegation_id, claim_id):
return self.store._apply('delegation.ack', {'delegation_id': delegation_id, 'claim_id': claim_id})['value']
def defer(self, delegation_id, claim_id):
return self.store._apply('delegation.defer', {'delegation_id': delegation_id, 'claim_id': claim_id})['value']
def release(self, delegation_id, claim_id):
return self.store._apply('delegation.release', {'delegation_id': delegation_id, 'claim_id': claim_id})['value']
def drop(self, delegation_id, claim_id):
return self.store._apply('delegation.drop', {'delegation_id': delegation_id, 'claim_id': claim_id})['value']
def unscheduled(self, delegation_id):
return call_off_event_loop(self.store._apply, 'delegation.unscheduled', {'delegation_id': delegation_id})['value']