Skip to content

fix(wake): a caller that waits is not woken, and a call can decline its wake (#4538) - #4766

Merged
vybe merged 7 commits into
devfrom
feature/4538-wake-opt-out
Nov 26, 2026
Merged

vybe merged 7 commits into
devfrom
feature/4538-wake-opt-out

Conversation

@webmixgamer

@webmixgamer webmixgamer commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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. Each such wake is a billed turn that adds nothing.

No DB migration, no new route. The call's word is kept as an idempotency_keys row keyed on the callee execution id, so it survives the backlog drain, the pull claim and every other re-dispatch from the row.

Behind a parked wake (the follow-ups from #4745)

Each has a test that failed before the fix.

Item Outcome
release_admission releases, then drains Pinned (the code was right; the test goes red when the line is deleted or moved)
A wake refused while the ended run's slot is still held Fixed: parked again, delivered at the slot release, instead of ending as a FAILED row
An unreadable slot meter Fixed: the test reproduced 120 FAILED wake rows for one ending (the hourly budget); now one, and the ending stays parked
A delivery cancelled before it holds the caller Fixed: the marker is put back (SET NX, so a newer parked ending wins; never once the wake is being dispatched)
Terminate paths do not drain Fixed for both sites, through the slot-release drain below
Wake-path database calls on the event loop Six calls moved to threads; the sync-on-loop baseline entry for caller_wake_service.py goes 6 → none

One drain at the slot release. CapacityManager._on_slot_released, the callback already registered on SlotService for the backlog, now also drains a caller wake parked for a free slot: after the backlog drain, under its own guard, and only when the meter shows a free slot. The stale-slot reclaim and force_release, which remove slots without the callback, drain too. A test makes the wake drain raise and checks that the release and the backlog drain still happen.

TARGET_ARCHITECTURE check (capacity_manager.py)

  • The change adds no terminal writer and no per-writer obligation. It hooks the one event every path already passes (the slot release) instead of adding a drain at each of the sites that skip the terminal fan-out, which is the direction of proposed target 2 (one owner for a terminal's obligations) and of the 2026-11-23 learning on release paths.
  • Principle 3 (async-first, sync as a thin edge adapter): "the caller waited" is an edge fact, recorded at the edge and read at the terminal.
  • Proposed target 1 (no blocking I/O on the loop): the hook's six counted calls now run in threads. The drain adds one synchronous Redis EXISTS per slot release, the same class as slot_service's own client (refactor: synchronous database, Docker and Redis calls run inside async request handlers #4413).
  • State lives in PostgreSQL (idempotency_keys), not Redis, apart from the existing parked-ending hint.

Stated limits

Changes

  • Backend: services/caller_wake_service.py, services/chat_execution_service.py, services/capacity_manager.py, services/idempotency_service.py, db/idempotency.py (peek returns the snapshot), models.py (wake, caller_wait_ms on ParallelTaskRequest and ChatMessageRequest).
  • MCP server: tools/chat.ts, tools/dynamic-agents.ts, client.ts (additive).
  • Docs: requirements (core-agent.md §9.1.1), the agent-to-agent collaboration flow (step 11), architecture/execution.md, user docs (Agent Network, FAQ, chat API reference, MCP server reference).
  • scripts/ci/sync_on_loop_baseline.json: regenerated with --write.

Deploy: rebuild backend and mcp-server. No migration, no base-image change.

Test Plan

  • New backend tests: cd tests && pytest unit/test_4538_wake_opt_out.py unit/test_4538_parked_wake_release.py unit/test_3127_pull_chat.py -q
  • New MCP tests: cd src/mcp-server && node --import tsx --test src/chat-wake-opt-out.test.ts src/wake.test.ts
  • Mutation proof: 64 mutations, each deleting or reverting the line that applies one rule, each turned its test red (47 backend, 15 MCP, 2 on the sync-on-loop ratchet). Among them: a sync call answered inline starts no wake (test_a_sync_call_answered_inside_the_callers_wait_starts_no_wake); wake: false on an async call starts no wake and the receipt says so (test_a_declined_async_call_starts_no_wake_and_its_receipt_says_so, and the MCP caller_declined test); a sync call that outlives the wait keeps its wake (test_a_sync_call_that_outlived_the_callers_wait_keeps_its_wake); the dedicated tool passes wake through (dropping the declaration or the pass-through line turns the dedicated chat_with_<agent> tool passes wake through red).
  • Related backend suites (wake, dispatch, capacity, slots, backlog, idempotency, pull, cleanup): pass
  • MCP server: typecheck clean, suite passes except tool-visibility.test.ts, which fails the same way without this change on the machine the tests ran on
  • Manual check on a local instance, two agents with a collaboration permission: an async call wakes the caller; the same call with wake: false shows caller_declined and starts no wake; a sync call answered inline starts no wake; a sync call past the 25 s wait gets a receipt and then its wake. The audit rows show skipped_declined with caller_declined and answered_inline.

Fixes #4538

🤖 Generated with Claude Code

webmixgamer and others added 6 commits November 24, 2026 13:16
…ts 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]>
…#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]>
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]>
…ls 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]>
…note on chat_with_agent (#4538)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Nightly unit-suite clean when this PR is merged into dev, all 3 seeds (head_sha: 7c80d0417c81eb08e972fdde63b846da99bf2662).

# Conflicts:
#	scripts/ci/sync_on_loop_baseline.json
@webmixgamer

Copy link
Copy Markdown
Contributor Author

Release note for the merge train: this should ship in the same release as #4606, or before it.

#4606 writes a wake's reply into the person's Chat thread when the delegation came from their turn there. An agent that answers every wake with a fixed one-line no-op today (the usual workaround for the wake that #4538 lets a call decline) will post that line into the person's thread as soon as #4606 ships. With this PR the caller sends wake: false and no wake runs.

For a mixed rollout: on REST /task, a backend without this PR ignores the unknown wake field (the request model does not forbid extra fields), so a caller can start sending it before the update. The MCP parameter needs the new MCP server.

@vybe

vybe commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

merge-train note (validated at 7c80d0417): no critical findings; riding the current batch.

One fit question went to the operator at Gate 1 and was ruled acceptable (recorded on #4538): a REST sync /task call that sends neither wake: false nor caller_wait_ms still gets its wake after an inline answer.

One ask, not blocking the merge: the body lists limits with no follow-up issue (the REST sync gap above; no opt-out on loop iterations and trigger_agent_schedule; fan_out and queued_timeout receipts carry no wake field). Please file the follow-up(s) and name them here.

@webmixgamer

Copy link
Copy Markdown
Contributor Author

Follow-ups filed and named in the PR body:

The remaining limits in the body are design bounds, not gaps: the 10 s margin, the 24 h life of a call's terms, and a re-send that only changes wake replaying the first call.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: validated at 7c80d04 (/validate-pr + /review, lane B); merged tree with #4782 passes the touched unit files locally. Fit ruling recorded on #4538.

@vybe
vybe merged commit 85dd5d1 into dev Nov 26, 2026
30 of 31 checks passed
@webmixgamer
webmixgamer deleted the feature/4538-wake-opt-out branch November 26, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants