f2a717d8c6(N14) moved admission writes off the owner loop and scheduled the FIFO drain inside the tracked write task. The drain could then claim, run or pause the input before the caller had its receipt, i.e. before messaging's admit_message registered the delivery waiter. Two real-SDK regressions on the messaging-gated e2e lane: - test_native_pause_notice::test_crash_mid_turn_then_platform_message_gets_one_pause_notice_and_reset_unpauses: the pause released no waiter, so the "paused, use /reset" notice was never sent (CI run 38057119007, e2e job). - test_native_role_reauthorization::test_role_preflight_fences_await_and_restart_without_consuming_work: that drain's off-loop claim transaction was open while another drain awaited Discord's fetch_member, which the probe forbids (no SQLite transaction may span a remote authorization await). Intermittent: 3 of 12 local runs. tracked_write gains `after(result)`, run in the caller's own step once the write returns: all four admission paths (native, submit, cron, automation) schedule their drain there, the order synchronous admission had. A caller cancelled while its commit is in flight hands `after` to the commit's completion, so a committed row is never stranded without a drain (thecc4f8c6d7brace stays closed). The writes themselves stay off the loop. Test: test_settlement_lock_liveness.py::test_admission_schedules_its_drain_in_the_admitting_callers_step red before (drain ran before the caller: ['schedule', 'drain', 'caller']), green after 3/3. Real-SDK lane after the fix: the role fence test 0/12 failures and the crash pause-notice test passes. Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5475590057
150 lines
6.5 KiB
Python
150 lines
6.5 KiB
Python
"""Physical worker liveness and cooperative Stop across every served authority."""
|
|
import asyncio
|
|
|
|
from gateway.session_authorities import all_authorities
|
|
|
|
|
|
def executing_sessions(authority):
|
|
for sid, live in getattr(authority, 'sessions', {}).items():
|
|
execution = getattr(getattr(live, 'event_stream', None), 'execution', None) or {}
|
|
task = live.task
|
|
if execution.get('execution_generation') is not None and task is not None and not task.done():
|
|
yield sid, live, execution['execution_generation']
|
|
|
|
|
|
def uncounted_runtime_work(runner):
|
|
"""Count claims absent from the ordinary agent map, including pre-bootstrap managed turns."""
|
|
running = getattr(runner, '_running_agents', {})
|
|
authorities = all_authorities(runner)
|
|
managed = sum(not worker.closed.is_set() for authority in authorities
|
|
for worker in getattr(authority, '_managed_workers', {}).values())
|
|
return (managed + sum(live.route not in running and sid not in getattr(authority, '_managed_workers', {})
|
|
for authority in authorities for sid, live, _ in executing_sessions(authority))
|
|
+ sum(len(mutation_tasks(authority)) for authority in authorities))
|
|
|
|
|
|
def persist_work_count(authority, _task=None):
|
|
"""Re-publish the runner's in-flight count when owner work it counts (a drain, a tracked
|
|
mutation, a mailbox operation) ends: the agent-slot release that last wrote
|
|
``gateway_state.json`` ran while that work was still live, so without this write the
|
|
persisted count never returns to zero."""
|
|
persist = getattr(getattr(authority, 'runner', None), '_persist_active_agents', None)
|
|
if persist is not None:
|
|
persist()
|
|
|
|
|
|
def mutation_tasks(authority):
|
|
tasks = {*getattr(authority, '_mutation_tasks', ()), *getattr(authority, '_bot_mailbox_operations', ())}
|
|
return [task for task in tasks if not task.done()]
|
|
|
|
|
|
def track_mutation(authority, operation):
|
|
tasks = getattr(authority, '_mutation_tasks', None)
|
|
if tasks is None:
|
|
tasks = authority._mutation_tasks = set()
|
|
task = asyncio.create_task(operation)
|
|
tasks.add(task)
|
|
def finished(done):
|
|
tasks.discard(done)
|
|
if not done.cancelled():
|
|
done.exception() # the observer may already have disconnected
|
|
persist_work_count(authority)
|
|
task.add_done_callback(finished)
|
|
return task
|
|
|
|
|
|
async def tracked_write(authority, write, then=None, *, ordered=False, after=None):
|
|
"""Run a synchronous ledger write (``write()``) in a worker thread, off the owner loop.
|
|
|
|
A held SQLite writer (``_WRITE_PATIENCE_S``) must not freeze every session, timer, socket,
|
|
Stop and approval. The write and its loop-side follow-up (``then(result)``, sync or a
|
|
coroutine: publication, scheduling, the claim's execution stamp) are one task tracked like ``track_mutation``, so
|
|
retirement joins it, and shielded, so a cancelled caller cannot separate a commit from it.
|
|
``ordered`` writes (admissions) commit in call order, as they did on the loop: worker threads
|
|
alone would let a later input take an earlier FIFO position.
|
|
|
|
``after(result)`` (an admission's drain scheduling) runs in the CALLER's own step once the
|
|
write returns, as it did when admission was synchronous: the caller registers its delivery
|
|
waiter before any drain it scheduled can claim, settle or pause that admission. A caller
|
|
cancelled while the commit is in flight hands ``after`` to the commit's completion instead,
|
|
so a committed row is never stranded without a drain."""
|
|
order = getattr(authority, '_ordered_writes', None)
|
|
if ordered and order is None:
|
|
order = authority._ordered_writes = asyncio.Lock()
|
|
|
|
async def run():
|
|
if ordered:
|
|
async with order:
|
|
result = await asyncio.to_thread(write)
|
|
else:
|
|
result = await asyncio.to_thread(write)
|
|
if then is None:
|
|
return result
|
|
followed = then(result)
|
|
return await followed if asyncio.iscoroutine(followed) else followed
|
|
task = track_mutation(authority, run())
|
|
if after is None:
|
|
return await asyncio.shield(task)
|
|
try:
|
|
result = await asyncio.shield(task)
|
|
except asyncio.CancelledError:
|
|
def late(done):
|
|
if not done.cancelled() and done.exception() is None:
|
|
after(done.result())
|
|
task.add_done_callback(late)
|
|
raise
|
|
after(result)
|
|
return result
|
|
|
|
|
|
def start_turn_worker(runner, worker, agent_holder, run_sync):
|
|
authority = track_turn_worker(runner, worker, agent_holder)
|
|
worker.executor_task = asyncio.ensure_future(runner._run_in_executor_with_context(run_sync))
|
|
if authority is not None:
|
|
# Normal settlement releases the retained agent promptly. A cancelled asyncio waiter is
|
|
# not physical completion; leave that entry until worker_done proves the thread exited.
|
|
worker.executor_task.add_done_callback(lambda _: list(physical_workers(authority)))
|
|
|
|
|
|
def track_turn_worker(runner, worker, agent_holder):
|
|
from gateway.session_authorities import active_authority
|
|
authority = active_authority(runner)
|
|
if authority is not None:
|
|
workers = getattr(authority, '_turn_workers', None)
|
|
if workers is None:
|
|
workers = authority._turn_workers = {}
|
|
list(physical_workers(authority))
|
|
workers[id(worker)] = (worker, agent_holder)
|
|
return authority
|
|
|
|
|
|
def physical_workers(authority):
|
|
workers = getattr(authority, '_turn_workers', {})
|
|
for key, (worker, holder) in list(workers.items()):
|
|
if worker.worker_done.is_set():
|
|
del workers[key]
|
|
else:
|
|
yield worker, holder
|
|
|
|
|
|
def stop_authority_work(authority):
|
|
from gateway.run_runtime import stop_authority_turns
|
|
from agent.interrupt_compat import request_hard_interrupt
|
|
stop_authority_turns(authority, in_process=True)
|
|
for _, holder in physical_workers(authority):
|
|
if holder and holder[0] is not None:
|
|
request_hard_interrupt(holder[0], 'Profile runtime is stopping')
|
|
for cancel in getattr(authority, '_cron_cancellations', {}).values():
|
|
cancel.set()
|
|
|
|
|
|
async def join_authority_work(authority, timeout):
|
|
deadline = asyncio.get_running_loop().time() + timeout
|
|
while True:
|
|
stop_authority_work(authority)
|
|
if not list(physical_workers(authority)) and not list(executing_sessions(authority)) and not mutation_tasks(authority):
|
|
return
|
|
if asyncio.get_running_loop().time() >= deadline:
|
|
raise TimeoutError('Profile workers did not stop; ownership retained')
|
|
await asyncio.sleep(.05)
|