- test_api_responses_stream_retry, test_managed_worker_stop_before_hello: poll
the state the old loop polled with async_wait_until.
- test_responses_idem_replay_record: the retry has joined once a second API
observer is registered on the in-flight admission (was sleep(.2)).
- test_managed_worker_reader_thread: the reader is parked once its thread's
stack is inside read_frame (was sleep(.3)).
- test_session_authority_cancel_settlement: wait for the drain to log two
deferred claims behind the held writer (was sleep(.6)); assertions unchanged.
- test_settlement_off_loop: other work runs once the off-loop write signals it
started (was sleep(.01)).
- test_session_bot_retry: wait until no receipt task, mailbox operation or
drain is running before asserting no extra admission; the peer waiter is
observed following the held retry admission; the mailbox-lock stall test
waits for the write to park in flock off the loop (holder release bound 10 s,
released by the test).
- test_gateway_queued_unknown: the fake worker loss waits for the view to own
its admission, and (yield_between) for session.info to be rendered.
- test_runtime_bootstrap (Windows-only): the stalled handler blocks on an Event
set at teardown; the injected client signals reaching the pipe
(CreateFile/WaitNamedPipe) and the idle accept cycle is observed via _accept.
Waiver (1):
- tests/gateway/test_runtime_bootstrap.py:148 time.sleep(2.5): the handler must
outlast the 2 s per-peer I/O budget; the delay is the behaviour under test.
(cherry picked from commit ed76f552c94c115d8c26649778b2da2eedbef929)
ManagedWorker.next_frame ran every pipe read as asyncio.to_thread(read_frame, stdout). Each
parked worker (a long tool call, a silent child, a prompt waiting on the user) pinned one
thread of the loop's shared default executor, and every other to_thread on the gateway —
settlement writes, env preparation, control queries — queued behind them (reviewer: 32
parked readers -> a control query times out). Cancelling next_frame also only cancelled the
asyncio wrapper: the thread kept blocking on readline, then consumed the child's next frame
and dropped it, so after a cancelled read the next read missed a frame (or hung).
Each worker now owns one reader thread, started on the first read. It reads exactly one
frame per demand (the pipe keeps its backpressure) and posts it to the loop with
call_soon_threadsafe into a per-worker deque; next_frame awaits arrival with the same
timeout/Stop-deadline logic, so a cancelled read leaves the frame for the next call. EOF or
an invalid frame is delivered as the exception and stays sticky. The reader thread owns
stdout and closes it on exit; close() wakes a reader parked between frames, and a reader
blocked mid-read ends at the child's EOF (close terminates/kills it first), then is joined.
Red on r6-base: tests/gateway/test_managed_worker_reader_thread.py
test_parked_worker_reads_leave_the_default_executor_free -> TimeoutError (4 parked
readers on a 4-thread default pool starve an unrelated to_thread)
test_a_frame_arriving_after_a_cancelled_read_is_not_lost -> TimeoutError (the orphaned
to_thread read swallowed the frame)
Green after (also asserts the reader thread exits and stdout is closed after close()).
tests/gateway/test_managed_*.py + test_worker_*.py + the new file: 33 files, 84 passed;
test_runtime_worker_shutdown, test_unknown_resolution_physical_worker,
test_profile_retire_stops_turns: 4 files, 10 passed.
Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5462648005