Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(orchestrator): Wake settled parents when delegated children finish #4499

Mergedmwolson(GitHub profile, opens in a new tab)merged 2 commits intot3code/codex-turn-mapping fromrefs/pull/4499/headmerged by juliusmarminge View on GitHub

Summary

  • Wake a settled parent thread when an app-owned delegate_task child terminalizes, so an async delegation's result is acted on instead of waiting for the user's next message.
  • Gate the wake on a per-task completionWake policy so async delegations and blocking waits do not double-deliver.
  • Deliver the wake as a real prompt. A wake that only starts a run does not help if the provider never sees the text.
  • Narrow the Claude adapter's task-notification drop, so the wake this PR introduces can actually settle its run. Carried as a second commit because it is an adapter concern rather than a wake concern, but it is a prerequisite: without it this PR ships a hang.

Problem and Fix

Problem and Why it HappenedFix
finalizeAppOwnedSubagent wrote the child result transfer but had no wake path, so the parent never ran again after an async delegation. The result card appeared and the thread simply parked.Offer a ProviderContinuationRequest for the parent when a delegated child terminalizes. The existing ProviderContinuationService worker dispatches it as a queue_after_active message.dispatch that starts a wake run.
An unconditional wake would double-deliver for blocking waits, where the tool call already returns the result, while a settled-only wake would miss async children that finish mid-turn.Record a per-task completionWake policy. "always" for async delegations offers on every child terminal and queues behind a live parent run. "settled_only" for wait-mode delegations, and for legacy records without the field, offers only when the parent has no run in preparing/starting/running.
A settled_only wait child that terminalized under a live parent lost its wake permanently once the blocking wait had timed out.New delegated_task.wake-policy command. On wait timeout the MCP service upgrades the task to "always" best-effort, and for an already-terminal task it offers the wake directly when the parent has a live run, which is exactly the state where finalize's gate skipped it.
The gate could read a stale parent snapshot, and finalize wrote the parent subagent row while holding only the child dispatch lock, racing the wake-policy command's write of the same row.Resolve the parent from the child's immutable lineage, then run finalize under the parent thread lock and startNextQueuedRun under the child lock, sequentially and never nested. Gates re-read run state fresh inside the parent lock.
The wake started a run but never prompted the model, so the agent still could not act on the result. ProviderContinuationRequests is provider-native: its dispatch only triggers ingestion of output an adapter already buffered, and both ClaudeAdapterV2 and AcpAdapterV2 discard the message text when it is marked creationSource: "provider". An app-owned child buffers nothing, so the adapter drained an empty buffer and finalized the turn immediately.Add delivery to ProviderContinuationRequest. adapter_buffered stays the default and preserves provider-native behavior; message_text dispatches creationSource: "server", which no adapter mistakes for a buffered wake. Both offer sites build the request through one delegatedTaskWakeRequest helper so they cannot diverge.
Agents polled or spawned watchers for async children because the tool description did not say the thread wakes.delegate_task description now states that async children wake the thread, so callers should end the turn rather than poll or spawn watchers.
The message_text wake above is dispatched as creationSource: "server" precisely so no adapter mistakes it for a buffered wake. But ClaudeAdapterV2 keys its task-notification guard on that same "provider" marker, and dropped every task-notification-origin terminal result on a turn without it. So the wake run's own terminal was discarded, finalizeActiveTurn never ran, and the run stayed running while every run queued behind it stalled. Observed in production for 8h27m, with three terminal success results carrying num_turns 154, 60 and 7 all dropped, and five queued runs never dispatched.fix(claude): Settle positive task-notification results, the second commit here, gates that drop on message.num_turns === 0. The stale interrupt-recovery debris the guard was written for is explicitly zero-turn and is still dropped; a result representing real model work now reaches the steering classifier and terminalization as normal. It drops strictly fewer results than before, so no previously working path can regress.

Manual test guide

Browsable scenario guide: <https://nam7nt0rbtm6.postplan.dev/>

Sections that cover this PR:

Validation

  • Focused suites: contracts decode tests for completionWake and the delegated_task.wake-policy command struct; ProviderContinuationService tests for both delivery mappings; MCP toolkit integration tests covering wait-completes, async-cancel under a live parent, wait-timeout upgrade, an already-terminal upgrade in both sub-cases, and a legacy field-omitted child. The integration probes assert delivery at every offer site.

  • Full apps/server suite passes on this base: 1674 passed, 10 skipped.

  • vp check and vp run typecheck clean.

  • Live-verified on a packaged desktop build, reading the provider session transcript rather than the projection, because a projected message does not prove delivery:

    • Settled parent: the wake prompt reached the model, it called task_status, and replied with the child's result. The wake run took 5.5s, versus an 89ms empty run before the delivery fix.
    • Mid-turn always path: the child terminalized 35s before the root turn ended, the wake was requested while the parent was still live, queued behind it, and was delivered exactly once after the root settled.
    • Provider-native regression guard: a background Bash wake still attaches its buffered CLI output and responds normally.
  • For the second commit: ClaudeAdapterV2.test.ts covers a server wake and a user turn settling from a positive task-notification result, a zero-turn result still being ignored on a normal turn, a zero-turn result still consumed by a provider continuation, debris racing an interrupt, and a positive error result. Removing only the num_turns === 0 condition fails exactly the positive-turn cases, run independently as a negative check.

  • Claude live packs on this base pass: in-turn background bash, foreground subagent, monitor, post-settle background bash wake, post-settle subagent, and resume subagent. These are the provider-native regression guard for the adapter change; the drop this commit narrows is not reachable from them, because a task-notification result only arrives on a provider continuation turn there, which the guard deliberately never applied to.

The unit and integration layers prove the offer and the delivery mapping; the packaged run is what proves the model is actually prompted end to end.

Known limitations

  • The offer queue is in-memory. A crash between the result-transfer write and the worker dispatch loses that wake on replay, since the transfer guard blocks re-offering. A durable outbox keyed by the result transfer is the proper fix.
  • One documented missed-wake window remains: finalize skips under settled_only with a live parent, the parent settles, and a late upgrade then also skips because the parent is no longer live. The MCP path awaits the upgrade inside the parent's turn, so this mostly affects direct command callers. Tracking wake delivery explicitly, rather than dual-gating on liveness, would close it.
  • Reconcile-path run updates skip finalizeAppOwnedSubagent entirely, so recovery-cancelled children produce no transfer and no wake. Pre-existing.
  • A wake can start ahead of a queued-only user run, because queue_after_active only queues behind blocking runs.

Note

Wake settled parent threads when delegated child tasks finish

  • Adds a completionWake policy field to subagent records and delegated_task.request commands: settled_only (wait mode) wakes the parent only when it has no live run; always (async mode) wakes it even mid-turn.
  • Adds a new delegated_task.wake-policy command so the orchestrator MCP service can upgrade a timed-out wait-mode task's wake policy to always after the blocking wait expires.
  • ProviderContinuationService now dispatches message_text wake requests as server-created prompts so adapters ingest the child result as a real message instead of a buffered wake signal.
  • ClaudeAdapterV2 no longer finalizes normal (non-continuation) turns on zero-turn task-notification results, preventing spurious turn endings from async child completions.
  • Lock acquisition in resumeQueuedRuns is restructured to take the parent thread lock before the child lock, avoiding nested lock inversion during subagent finalization.
Macroscope summarized 884d727.

Note

High Risk Touches orchestration locking, MCP delegation delivery, and Claude turn finalization on a path that previously caused multi-hour stuck runs; incorrect wake gating or adapter handling could duplicate wakes, miss wakes, or leave runs non-terminal.

Overview Settled parent threads now get a continuation when app-owned delegated children finish, instead of parking until the user sends another message.

The orchestrator records a per-task completionWake policy (always for async MCP delegations, settled_only for blocking wait). When a child terminalizes, finalizeAppOwnedSubagent may offer a parent wake via ProviderContinuationRequest, gated on whether the parent still has a live run. A new delegated_task.wake-policy command rewrites that policy; MCP delegate_task dispatches it best-effort after a wait timeout so a late child finish can still wake a mid-turn parent. Finalization now takes the parent thread lock (child lock only for queueing), avoiding races on the subagent row.

Wakes are delivered as real prompts: delivery: "message_text" on continuation requests makes ProviderContinuationService dispatch with creationSource: "server" so adapters do not treat app-owned completion text as an empty buffered wake.

ClaudeAdapterV2 only drops zero-turn task-notification-origin results on normal turns (stale debris); positive-turn results can settle the run so those continuations do not hang. delegate_task tool copy tells agents async children wake the thread and to avoid polling.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 884d727dc3bb909cb587c3e6bc961490a50487d0. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 53e5b720-424d-4ec0-9ac1-09ae8b7d3c4f

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit cf467bed0fa781bc5ba3ececaf9a2623363bc491. Configure [here](https://www.cursor.com/dashboard/bugbot).
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR introduces significant new orchestration behavior including a new command type, new schema fields, and changed wake logic for parent-child task delegation. Despite the 'fix' framing, this adds substantial new capability to core orchestration infrastructure that controls runtime behavior of task completion notifications.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. juliusmarminge merged this3b6251f

    6. juliusmarminge closed this

Sign in to join this conversation.