GW-15 made the stop drain wait for /bg, /btw and preview-restart side
agents, but _interrupt_running_agents never reached _side_agents. A long
/bg held the full drain, got no interrupt, and was abandoned when the
process exited. interrupt_side_agents hard-interrupts every counted side
agent's holder across served authorities, beside the managed-worker and
api_server interrupts.
Review finding by @kshitijk4poor on #140967
Refs #134889 (GW-15)
Refs #140719
GW-15 counts /btw as owner work, but its _spawn passed no holder, so
stop_authority_work could not reach its fork while join_authority_work
waited for it. A slow side question (the one-shot fallback allows 180 s)
held retirement for the whole settle window, then the profile kept its
reservation ("workers did not stop; ownership retained").
answer_side_question now publishes its cache-parity fork through
on_fork, and /btw passes a holder (_SideQuestionStop) that interrupts it.
Once stopped, the one-shot fallback is skipped; one already in flight is
an uncancellable, read-only aux call, so a stopped /btw with no fork left
stops counting as owner work and publishes no late answer.
Review finding by @kshitijk4poor on #140967
Refs #134889 (GW-15)
Refs #140719
A side agent runs on a detached thread that only a module-private set in
gateway/session_tui_side.py knew about. Every owner work reader goes
through uncounted_runtime_work (restart wait, stop drain, SessionDB close
gate, idle exit) or stop/join_authority_work (profile retirement), so a
running side agent counted as zero: a planned restart (hermes update,
service restart, SIGUSR1) or idle exit proceeded within seconds, killed
it, and the client never got background.complete / btw.complete.
_spawn now registers each side agent in an authority-owned ledger before
thread.start() and removes it in a finally around the whole thread
target, re-publishing the work count on the owner loop, so a thread that
dies on a BaseException (cancellation, exit) is never left counted.
uncounted_runtime_work counts the ledger; stop_authority_work interrupts
the built agent (/bg, preview restart) and join_authority_work waits for
the thread. Restart timeouts still cap the wait, and new side agents are
still refused with runtime_draining once a drain starts.
Refs #134889Fixes#140375
(cherry picked from commit 098ba77d706296945a1d3a0b0ee6c0963fa070b4)
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 (the cc4f8c6d7b race 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
Follow-up to 951bb6e7d2e, found by the full gateway suite:
- A drain whose claim transaction (now off-loop) read an empty FIFO exited
idle even when input committed meanwhile through a path that takes no
session mutation lock (API admit_api_turn + observe_api_turn's _schedule,
automation): that _schedule saw the drain alive and did nothing, stranding
the admission (test_api_idempotency_namespace hung, the API controls probe
timed out). _schedule now sets LiveSession.rescan; the claim clears it
before its transaction, and an idle claim with rescan set loops instead of
exiting.
- resolve_unknown's whole post-commit tail (pending_results, completion,
media release, publication, schedule) runs inside the tracked write task
(_resolved_unknown, async follow-ups supported by tracked_write), so a
cancelled caller cannot drop the media release or the successor wakeup.
- Two subprocess fixtures (native_ingress_media_peer capture,
shared_authority_recovery prepare_crash) froze "commit, not yet claimed"
by relying on admission never yielding; they now freeze the scheduling
boundary explicitly (authority._schedule no-op) before the owner is killed.
Their assertions are unchanged.
Test: test_settlement_lock_liveness.py::test_input_admitted_while_an_idle_claim_is_in_flight_still_runs
(an API-shaped admission commits while the claim's transaction reports idle):
without the rescan 1 failed (`[] == ['late']`), with it passed.
Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5475590057
7fef5d8b6c and 9dadd0be5b moved settlement and queued-cancel writes off the
owner event loop because a held state.db writer (_WRITE_PATIENCE_S = 20 s)
froze every session, timer, socket, Stop and approval. The three hottest
writes were still synchronous on the loop: admission (submit, admit_native,
cron submit, automation), the FIFO claim (_claim_next) and Discard
(resolve_unknown). Each takes SessionDB._lock and retries BEGIN IMMEDIATE with
time.sleep, so a 1.5 s foreign hold delayed a 50 ms loop callback by ~1.5 s.
session_runtime_workers.tracked_write runs the write in a worker thread with
its loop-side follow-up (publication, scheduling, the claim's execution stamp)
as ONE task tracked like track_mutation (retirement joins it) and shielded (a
cancelled caller cannot separate the commit from it). Invariants kept:
- gate: the admission gate is re-checked inside the write transaction
(_admission_gate as the admit guard, claim_session_input(_guard=...)), so a
drain or retirement that starts while the write waits still refuses it;
- epoch/generation checks are unchanged in the store;
- commit-before-ACK: the receipt, _gateway_accepted and publication follow the
commit in the same task;
- claim-to-execution-stamp continuity: the stamp and message.start are set in
the claim's task right after its commit (_stamp_claim), no loop turn between;
- admission order: admissions serialize on a per-authority lock (ordered=True),
so a later input cannot take an earlier FIFO position.
test_session_authority_cancel_settlement's hosted-preclaim case now queues
both rows before the drain starts: admission yields now, so the first
submit's drain could otherwise claim before B is admitted. Its assertions are
unchanged.
Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5475590057
Follow-up to 6156b21c5e3f (#135061). That commit made the runner's work count include owner work
the in-process agent map never sees: an executing drain, tracked mutations, bot mailbox
operations. The count reaches gateway_state.json only at agent-slot boundaries, and a messaging
turn releases its slot while its drain is still executing, so the last write saw 1 and nothing
wrote again when the drain ended. active_agents stayed 1 after every turn: the dashboard readout
never went idle, and the Telegram/Discord/Slack platform contracts (40 cases) timed out waiting
for it.
Each kind of owner work the count includes now re-persists it when it ends (drain task, tracked
mutation, mailbox operation).
Review: https://github.com/NousResearch/hermes-agent/pull/135061
Poll the owner's local receipt projection without opening a new admission RPC. Serialize delivery with an async owner lock and short off-loop file transactions, preserve legacy compare-and-set pinning, and refresh paused or adopted receipts from owner publication. Cancelling startup recovery keeps its mailbox operation tracked until physical I/O ends, including repeated cancellation.
The API peer waiter reads the locked receipt off the event loop. Fixtures follow the same public local reader and preserve isolated native daemon homes. The inherited FIFO pause regression retains its behavioral assertions after the mailbox migration.
Validation: scripts/run_tests.sh -j 3 across 11 Bot/receipt files: 112 passed, 3 Linux-only skips on native Windows, no failures or retries. Includes real daemon queued/settled receipts and single/repeated startup cancellation. Log: split-B-tests.log.
Depends-on: "fix(worker): retain persistence and ownership through physical shutdown" (physical worker lifetime)
Depends-on: "fix(gateway): retain mutation ownership through publication and retirement" (mutation ownership helpers)
Depends-on: "fix(gateway): release paused admission observers without false settlement" (shared FIFO pause)
Review-source: itsflownium, https://github.com/NousResearch/hermes-agent/pull/106742#issuecomment-6050777316
Review-source: Adolanium, https://github.com/NousResearch/hermes-agent/pull/106742#issuecomment-6055121721
Source-base: NousResearch/hermes-agent#106742 at 0b815190b2
(cherry picked from commit 819b7db20f9791660a0a2ac84771845492e7cd87)
Shield mutations from request cancellation and hold admission/claim gates until commit and live publication finish. Recheck queued submits after deletion, fence replayed projections by generation, and join mutations before profile retirement.
Depends-on: "fix(worker): retain persistence and ownership through physical shutdown" (physical worker and drain ownership)
Additional cancellation, retirement, and submit/delete regressions identified during GPT-6 Astra review.
Validation: tests/gateway/test_local_authority_transitions.py, tests/gateway/test_adversarial_mutation_lifecycle.py, tests/gateway/test_submit_after_delete_gate.py, tests/gateway/test_rewind_receipt_projection.py
Implementation and additional regression probes were AI-assisted with GPT-6 Astra.
(cherry picked from commit 564ffcb9819bb63ee34e5720b0debd1b2fbd7d6e)