Files
hermes-agent/tools/async_delegation_worker.py
Teknium 0ce86de78b fix(delegate): a waiting child reports once on every path and blocks an exact duplicate
- Worker-ledger mode carries the silent 'delivered' state through
  delegation.complete, so a worker-run unit whose children all wait is not
  published either.
- Pruning keeps the ledger row a waiting child reports on: a silent unit is
  'delivered', the first kind the count cap deletes, and the give-up failure
  is routed through it.
- Boot recovery treats a crashed run that was its record's last chance as
  the scheduler's to report: the unit stays silent and the next tick delivers
  the one give-up failure instead of a generic "outcome unknown".
- The failed-delegations surface skips children still waiting to resume and
  lists them once they give up.
- delegate_task refuses, per task, a spawn whose goal is exactly a waiting
  child's goal, naming the child, its resume time and how to stop it first.
2026-11-13 00:03:31 -08:00

83 lines
3.6 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, delivery='pending'):
return self.store._apply('delegation.complete', {'event': event, 'result': result}
| ({'delivery': delivery} if delivery != 'pending' else {}))['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']