Repository navigation
fix(wake): one wake per caller at a time, and a wake refused at capacity is parked (#4733, #4734) - #4745
Conversation
…ity is parked (#4733, #4734) The caller wake was keyed caller_wake:{caller}:{now // 15}, and the #4536 park ran only when that key replayed in flight. A wake turn outlives its 15 s window, so an ending in a later window claimed a new key and started a second wake beside the first (reproduced: three wake turns running at once; seen live as four wake turns for one scheduled run). - Every wake, a first ending's and a drained one's, now claims one hold per caller, caller_wake:{caller}:running, in place of the window key and the follow-on key. An ending while the caller is held folds into the running wake when it ended by that wake's claim and arrived within 15 s of ending; otherwise it is parked and delivered once when the wake ends. - The hold is read from the rows: the caller is held while a caller_wake row of it is queued, running or pending a retry, or while a claim is younger than 120 s. An older claim with no live row is reclaimed by its owner token. A clean backend stop cancels the holder and writes the wake row FAILED with no terminal fan-out, so a release at the terminal alone would have held the caller for about 65 minutes after every deploy. A wake row's terminal still frees its own hold (claimed at or before the row's admission, queued_at over a re-stamped started_at), never a later wake's. A stale hold that cannot be reclaimed wakes without it instead of looping. - At capacity (product ruling on #4733): with every slot taken and a run of the caller running, no wake row is written. The ending is parked (audit parked, reason at_capacity) and delivered when any run of the caller ends. A full meter with no running run, a pull pilot, or an unreadable meter dispatches as before. A refusal that races the check is the one FAILED row left; its ending is parked too, except on a pilot. - New read-only idempotency peek (db + facade + service, the DB half #2671 asks for) and db.has_execution_with_status. - Docs (#4734): the module text, requirements, the collaboration flow, the Agent Network section and the FAQ state the rule as built, say a wake stands as the turn that delegated, and say a wake's reads are narrowed while a shared person's or a visitor's turn runs (#4173). Tests: tests/unit/test_4733_wake_hold.py (26). Red on the base commit for the reported defect ("3 wake turns were running at once for one caller"), the later-window fold, the back-dated ending, the capacity park and its delivery, and the timed-out holder. Mutation battery, 12 of 12 red: window key restored, no wake-row release, started_at in the release rule, no live-row check, no slot check, no run-end drain, no fold skew, no stale reclaim, holder drains the refused ending, pilot refusal parked, full meter trusted, unreclaimable stale hold kept. The #4536, ent1102, 4357 and ent1216 suites are updated for the hold (the later-window test there pinned the defect). Fixes #4733 Fixes #4734 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sync-on-loop ratchet (#4674) reached dev after this branch was cut and counts a sync db read inside `async def`, directly or one helper down. Seven of the nine reads the hold added now run in `asyncio.to_thread`: the slot check, the park and drain re-checks, the wake row's release and the race-park check. Two stay on the loop and are accepted by name (baseline 4 -> 6): maybe_wake_caller's hold claim and its live-row read. Both run before the dispatch beside the claim that was already synchronous (_begin_idempotency, uncounted only because it sits one module down), so moving them alone would not take the wake path off the loop, and it would reorder the #4536 harness. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…(review of #4745) Review findings, each with a test that fails without its fix: - A push /chat writes its terminal without the fan-out, so a wake parked for a free slot behind a /chat turn was never drained when that turn ended. run_chat_turn's finally (and release_admission) now call caller_wake_service.drain_after_slot_release right after the slot is released; a test drives the real finally. - The race park (admission refused after the slot check passed) now re-checks the slot once after its SET, so a busy run that ended in between does not strand the ending. - A drained ending never folds into a claim that may yet not dispatch (a budget cap, a dispatch error); it parks. - Docs: the /chat drain in the module text, requirements, the flow and architecture/execution.md; "a slot no run holds" instead of "expired slots"; the race exception and "none for a visitor" in the user docs; the wake frame says "around the same time", not "in the same window". Stated limits: one parked audit per ending at capacity; an ending folded into a claim that dispatches nothing is not woken. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
merge-train (2026-11-24): on this train. Validated at Train decision, ruled by the operator at Gate 1 on 2026-11-24: the sync-on-loop baseline growth for Nothing was pushed to this branch. This PR and #4606 both edit Follow-ups, none blocking:
|
…lets after #4606) Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
merge-train: merged |
vybe
left a comment
There was a problem hiding this comment.
merge-train: batch validated on train/20261124-0752
|
Acknowledged, thanks. The sync-on-loop count for
They are the same kind of single-row DB call as the four already there. I'll take the five follow-ups above into #4538, which is the next change to this file. Each one gets a failing test first, and a fix only if the test reproduces it; what doesn't reproduce is written down as a stated limit. In the same change I'll look at moving the wake path's DB calls off the event loop, which would bring the count back down. |
…ts wake (#4538) (#4766) * fix(wake): a caller that waits is not woken, and a call can decline its wake (#4538) The caller wake is for a caller that got a receipt. It also fired for a caller that held the call open and already had the answer, and a caller that polled in its own turn could not decline it. A call now says so itself, and the dispatch records it for the callee row ("call terms": an idempotency row keyed on the callee execution id, so no column and no migration): - `wake: false` on POST /task and /chat, and on the `wake` parameter of `chat_with_agent` and every `chat_with_<agent>` tool, starts no wake when that run ends. The receipt answers `wake_on_ending: {effective: false, source: "call"}`, which the MCP server prints as `wake: "none"`, `wake_reason: "caller_declined"`. - `caller_wait_ms` says how long a sync caller waits (the MCP server sends its own ceiling). A run that ends 5 s or more before that deadline was answered inline and wakes nobody; one that outlives it keeps its wake. A backend handler that gives up first moves the deadline before it re-reads the row. Only positive evidence stands a wake down: a call that declares neither, and terms that are missing, unreadable, half-written or unparsable, wake as before. `wake` stays out of the MCP idempotency key. Behind a wake parked for a free slot (the #4745 follow-ups), each with a test that failed first: - every slot release now drains the parked wake, after the backlog drain, from the release callback CapacityManager registers. That covers a terminate, a cleanup release and a run whose terminal lands before its own `finally` releases the slot; - a wake refused with no run of the caller left running is parked again instead of ending as a FAILED row; - after a capacity park the ending is dispatched again only when the meter was read and shows a free slot. An unreadable meter looped refused wakes until the hourly budget ran out (120 FAILED rows for one ending in the test); - a delivery cancelled before it holds the caller puts its marker back; - `release_admission`'s release-then-drain is pinned. Co-Authored-By: Claude Fable 5.2 <[email protected]> * refactor(wake): the wake hook's database calls run off the event loop (#4538, #4674) `maybe_wake_caller` runs for every terminal, and a cleanup sweep runs it hundreds of times in one cycle. Its row read, subscription read, hold claim, live-wake read, pending-ask read and standing-depth read were written directly inside `async def`, so each one blocked the worker. They go through `asyncio.to_thread` now. The sync-on-loop baseline entry for caller_wake_service.py goes from 6 to none (regenerated with `check_sync_on_loop.py --write`). A row that cannot be read is logged at warning: it is an ending nobody is woken for. Co-Authored-By: Claude Fable 5.2 <[email protected]> * docs(wake): the per-call wake opt-out and the slot-release drain (#4538) Feature flow step 11 (call terms: carried, stamped, kept, read, re-opened, receipt), the slot-release drain in step 9, the requirement for every slot release delivering a parked wake, the execution architecture note, and the user docs: `wake: false`, `caller_declined`, no wake after an inline answer, and what a plain REST sync caller has to send. Co-Authored-By: Claude Fable 5.2 <[email protected]> * fix(wake): review round — a 10 s inline margin, drains for the removals that fire no callback, a later claim mark (#4538) From the pre-landing review, each with a test that failed first: - The inline margin was 5 s, the same as the sync waiter's database poll: a run ending on another worker could be stood down as "answered inline" while its reply left after the caller gave up. It is 10 s, and a test pins it at the poll plus 5 s or more. - The stale-slot reclaim and `force_release` remove slots without the release callback, so they drain a parked wake themselves, under the same guard. - The release drain takes the marker only when the meter shows a free slot. The callback also fires for the backlog's sentinel swap, which frees nothing, and re-ran the whole hook on every such release. - A delivery's claim is marked at the dispatch, not at the hold: a cancel during the budget check or the reads before the wake row exists puts the ending back. Tests: the time-based ones run on a frozen clock; the terms-write failure test now proves the write was attempted and that the wake fires; the real-thread tests assert what they name. Co-Authored-By: Claude Fable 5.2 <[email protected]> * docs(api): the `wake` and `caller_wait_ms` task fields, and the wake note on chat_with_agent (#4538) Co-Authored-By: Claude Fable 5.2 <[email protected]> --------- Co-authored-by: Claude Fable 5.2 <[email protected]>
Summary
caller_wake:{caller}:{now // 15}, and the bug(wake): a fast callee's ending is dropped by the 15 s wake window, so a delegate→wake loop stalls silently #4536 park ran only when that key replayed in flight. A wake turn outlives its 15 s window, so an ending in a later window claimed a new key and started a second wake beside the first (reproduced: three wake turns running at once; seen live as four wake turns for one scheduled run). Every wake, a first ending's and a drained one's, now claims one hold per caller,caller_wake:{caller}:running, in place of the window key and the follow-on key. An ending while the caller is held folds into the running wake when it ended by that wake's claim and reached the hook within 15 s of ending; any other is parked and delivered once when the wake ends.caller_wakerow of it isqueued,runningorpending_retry, or while a claim is younger than 120 s (the gap before that row exists). An older claim with no live row is reclaimed by its owner token. This is what frees the caller after a clean backend stop:execute_task's cancel branch writes the wake row FAILED without the terminal fan-out, so a release at the terminal alone would have held the caller for about 65 minutes after every deploy that interrupted a wake. A wake row's terminal still frees its own hold (claimed at or before the row's admission,queued_atover a re-stampedstarted_at), never a later wake's. A stale hold that cannot be reclaimed wakes without it instead of looping.parked,reason: "at_capacity") and delivered when any run of the caller ends. A push/chat, which writes its terminal without the fan-out, delivers it from its slot release (drain_after_slot_releaseinrun_chat_turn'sfinally). A full meter with no running run (a slot no run holds), a pull pilot, or an unreadable meter dispatches as before.requirements/core-agent.md, the collaboration feature flow, the Agent Network section and the FAQ state the rule as built, say a wake stands as the turn that delegated, and say a wake's reads are narrowed while a shared person's or a visitor's turn runs (Agent cannot read a child execution it started during a share holder's turn (404 Execution not found) #4173).Changes
src/backend/services/caller_wake_service.py— the hold (_hold_key,_claim_hold,_wake_live,_caller_busy,_release_hold_of), the fold skew in_covered, the capacity park (_has_free_slot,_park_at_capacity,_drain_if_idle,_refused_for_a_slot), module text. The window key,_bucket_for,_follow_on_keyandmaybe_wake_caller(_key=)are gone.src/backend/services/chat_execution_service.py—run_chat_turn'sfinallyandrelease_admissioncallcaller_wake_service.drain_after_slot_releaseafter the slot is released.src/backend/db/idempotency.py,src/backend/database.py,src/backend/services/idempotency_service.py— a read-onlypeekbesideclaim(the DB half refactor(reliability): read-only idempotency lookup — one deterministic answer to 'which execution is mine?', retiring the per-route timeout heuristic #2671 asks for; that issue's endpoint stays with it).src/backend/db/schedules/executions.py,src/backend/database.py—has_execution_with_status(one indexed EXISTS).scripts/ci/sync_on_loop_baseline.json—caller_wake_service.py4 → 6, accepted by name (see Test Plan).tests/unit/test_4733_wake_hold.py;test_4536_wake_window.py,test_ent1102_caller_wake.py,test_4357_wake_chain_depth.py,test_ent1216_wake_turn_reads.pyupdated for the hold;tests/registry.json.docs/user-docs/collaboration/agent-network.md,docs/user-docs/faq/collaboration.md,docs/memory/requirements/core-agent.md,docs/memory/feature-flows/agent-to-agent-collaboration.md,docs/memory/architecture/execution.md(the/chatexception), learnings fragmentdocs/memory/learnings/2026-11-23-terminal-fanout-is-not-every-release-path.md.No DB migration on either track. No frontend,
docker/or base-image change. No.env, compose orsystem_settingskey changes meaning.Journey Impact
Journey Impact: none: the caller wake's scheduling and its docs; J10's invariants (permission edge, attribution, cascade, fan-out and loop bounds, depth) are unchanged
Test Plan
cd tests && pytest unit/test_4733_wake_hold.py -v— 30 tests. Red on the base commit for the reported defect (AssertionError: 3 wake turns were running at once for one caller), the later-window fold, the back-dated ending, the capacity park and its delivery, and the timed-out holder.started_atin the release rule, no live-row check, no slot check, no run-end drain, no fold skew, no stale reclaim, the holder draining the refused ending, a pilot refusal parked, a full meter trusted, an unreclaimable stale hold kept. Review fixes, 4 of 4 red: no/chatdrain inrun_chat_turn,drain_after_slot_releasea no-op, no race re-check, a drained ending folding.4733,4536,ent1102,4357,ent1216,ent1225,4442,ent1236,2048,1920),test_chat_admission.py, the ratchet and the registry reader: 421 passed after mergingdevand the review fixes.test_4674_sync_on_loop_ratchet.py(landed ondevafter this branch was cut). Seven of the nine new blocking reads are off the loop (asyncio.to_thread): the slot check, the three park/drain re-checks, the wake row's release and the race-park check. Two are accepted by name (--allow-growth, baseline 4 → 6):maybe_wake_caller's hold claim and its live-row read. Both sit before the dispatch beside the claim that was already synchronous (_begin_idempotency, uncounted only because it lives one module down); moving them alone would not take the wake path off the loop and would reorder the bug(wake): a fast callee's ending is dropped by the 15 s wake window, so a delegate→wake loop stalls silently #4536 harness.7942a7b05; the later commits move reads to threads and add the/chatdrain, both covered by the tests above).Eyeball recipe (localhost)
This branch must be what serves
localhost:8000. Fixtures: two running agents,bossandworker, and:agent_permissionslink frombosstoworker(boss → Permissions tab), needed even when one person owns both;CLAUDE.md: "Whenever you are woken for a result, runpython3 -c "import time; time.sleep(60)"before you answer." A wake turn does not get the delegating turn's instructions, so this cannot go in the chat message. (Claude Code refuses a baresleep N; use the Python form.)/operations?tab=executions, set All agents toboss, watch thecaller_wakerows. Expect at most one runningcaller_wakeat a time; endings 2 and 3 auditedparked; exactly one follow-on, naming the newest ending.1. In boss's Chat send: "Use chat_with_agent async=true to ask worker 'Run python3 -c "import time; time.sleep(10)", then reply done'. Then run python3 -c "import time; time.sleep(90)" and reply 'finished'." Expect nocaller_wakerow when worker ends (before: a FAILED row, Agent at capacity (1/1 parallel tasks running)); an auditparkedrow with"reason": "at_capacity"; the wake starts when boss's turn ends. Reset Parallel capacity afterwards.Stated limits
wake: false).operator_response) is a separate producer and can run beside a caller wake.list_recent_executions, which is narrowed while a shared person's or a visitor's turn runs (Agent cannot read a child execution it started during a share holder's turn (404 Execution not found) #4173).caller_wakerow (its ending is still parked).parkedrow each, as behind a running wake).Notes for review
maybe_wake_callerandlines += conversationin_framed_wake(this PR edits the next line, the frame's "around the same time"), a bullet between the twoagent-network.mdbullets rewritten here, and a feature-flow item 10 after item 9. Whoever merges second keeps both edits./chatnever drained a parked wake (fixed: the slot-release drain), the race park had no re-check after its SET (fixed), a drained ending could fold into a claim that never dispatched (fixed), five doc sentences (fixed).test_4536_wake_window.py: the later-window test pinned the defect (Z's own wake started beside H and drained Y) and is rewritten to the new rule; two tests now expect an ending after a wake row's terminal to wake at once, because that terminal frees its own hold. The harness's dispatch now marks its row terminal before returning, asdispatch_and_await_terminaldoes.test_4357_wake_chain_depth.py/test_ent1216_wake_turn_reads.pyalready stub the claim out ("the storm bound has its own tests"); they now stub the hold's live-row read and the slot check the same way, because their wake rows stay RUNNING on purpose.Fixes #4733
Fixes #4734
🤖 Generated with Claude Code