Repository navigation
feat(runtime): wait_agents, cancel_agent, and interim reports for background sub-agents (AP4.2) - #257
Merged
Merged
Conversation
…kground sub-agents (AP4.2)
A parent now controls the read sub-agents it started in the background:
- wait_agents { ids?, timeout_seconds } blocks the call until every named
child settles (any one, with no ids) or the timeout passes, and reports
each child as finished, still working, or unknown;
- cancel_agent { id } cancels one child and returns once it settled;
- a running child's newest closed report reaches the parent at a turn
boundary as a labelled interim notice, once, superseding older
undelivered reports; it carries no spend and is never progress.
Answers never ride in the tool results: they follow at the next boundary
through the one stamped delivery row, so exactly-once holds across tool
result, boundary, settlement, and recovery. Answers and reports share one
delivery ordinal per parent run, and assembly replays them in live order.
The two tools are declared exactly when spawn_agent is and run in request
order after the spawns before them. report_spans unifies the report walk
for blocking answers, delivered answers, and interim reports, and joins a
report split across messages in one turn (it previously kept the last
message only).
Store schema 41 -> 42 (child_reports); DESCRIPTOR_VERSION 12 -> 13; agent
prompt 16 -> 17 (one bullet). No protocol change.
Linear: ENG-1005
…w spawn_agent, drains keep owners - An interim report that arrives during the tool-free wait no longer ends it: deliver_children reports answers and reports separately, and only an answer (or steering) wakes the next turn. - wait_agents and cancel_agent follow spawn_agent through exposed_tools and pack policy instead of being policy names, so the three delegation tools are always present together and the prompt never names a missing tool. - A child the store delivered before its owner task finished stays in the registry, no longer outstanding, so teardown still awaits the task. - report_spans collects message ids and newest_report loads only the report it returns. - End-to-end interim-report test through the session runtime with replay identity, plus tests for no-id waits, AlreadyFinished, a report closed by the final-answer notice, and a report delivered before a crash. Linear: ENG-1005
…with an answer A report-only wake inside the tool-free wait pushed a report and kept waiting, so steering applied later in the same wait landed after it live but before it in replay, and one boundary could stamp several report batches with a full budget each. Delivery now takes a ReportDelivery: Always at the turn top, WithAnswers in the wait, where reports come only in the same transaction as an answer. Every wait delivery therefore ends the wait: one delivery and one budget per boundary, and nothing it delivers can precede steering. The turn ordinal and policy travel together as a Boundary. Tests: a_tool_free_wait_takes_reports_only_with_an_answer (fails if the wait asks Always), steering_during_a_wait_with_a_pending_report_replays_as_live, and a_report_only_delivery_is_not_an_answer extended for WithAnswers. Linear: ENG-1005
…paragraph Linear: ENG-1005
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Linear: ENG-1005
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7a0be84092
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…s finished A child's run can settle durably while its owner task is still reading the answer. A cancel in that window waited for the owner and then always said "was cancelled", although the child had completed and its real answer followed. The result now comes from the run's durable outcome: Cancelled stays Cancelled, anything else is AlreadyFinished. Codex review on #257. Linear: ENG-1005
lg2m
added a commit
that referenced
this pull request
Oct 7, 2026
main's AP4.2 (#257) took schema 42 (child_reports). The prune watermark is now step 43; step 42 accepts 43, every version list gains it, and the migration test upgrades from 42. The live-seam test's window grows with the larger tool block so its second prompt is not itself a seam, and it now asserts the watermark held still between the runs. Refs: ENG-992, ENG-996
lg2m
added a commit
that referenced
this pull request
Oct 7, 2026
…X3) (#254) * perf(runtime): prune at seams so each run extends the last request (CX3) Assembly used to stub every stale read-only result between runs while a live run stubbed only on overflow, so a run's first request rewrote the middle of the previous run's last request and missed the provider cache. - Store schema 41 -> 42: sessions.prune_through_ordinal/_turn, a durable prune watermark. Assembly stubs as if the context ended at that turn; between seams each request extends the last one byte for byte. - Seams: the live overflow prune (RuntimeEvent::ContextPruned, committed before the stubbed request is sent) and the proactive threshold, which now moves the watermark and reassembles once before it compacts; if the stubbed context fits, no summarizer runs. - Fix: a live run re-stubbed its own stubs on later overflowing turns, so the stub named the previous stub's size and disagreed with replay. - Upgraded sessions start at their newest turn and assemble as before. context_assembly is within noise against the CX4 tip. Refs: ENG-992, ENG-996 * docs(plans): link CX3 ledger row to #254 * fix(runtime): treat only the exact generated stub as already pruned The re-stub guard matched any result whose last line began with "[pruned: ", so a large read-only result ending that way was never pruned. Now content is skipped only when it is exactly the stub this call would produce (an optional header line, then the stub line with this call's name, arguments and hint). Addresses Codex review on #254. Refs: ENG-992, ENG-996 * test(runtime): pin storage-backstop stubbing for a session without a window Codex flagged that a new session's unset watermark would leave a windowless model unstubbed until the 4 MiB backstop refused it. The backstop plans Compact, which runs the threshold seam; this end-to-end test pins that (it fails at run 156 with the seam disabled). tools.md names the backstop as part of the seam. Refs: ENG-992, ENG-996 * fix(runtime): the live prune classifies inherited results by stored effect The live overflow prune knew effects only for this run's calls, so an earlier run's read-only result outside the built-in names (load_skill, spawn_agent, select_tools) stayed verbatim live but was stubbed on replay, breaking the cross-run extension at that seam. Assembly now also returns stored effects in result order; the claim and every reload carry them into the run, and the live prune classifies inherited results by them exactly as assembly does. Addresses the third Codex review on #254. Refs: ENG-992, ENG-996 * fix(runtime): bound the stub guard's header; no watermark seam without a prompt - The re-stub guard's optional header must pass the same bounded header_line check the stub is built with, and the size must be a u64; a large first line before a stub-shaped line no longer escapes pruning. - A summarizer's live prune is not a seam (its request is not a transcript), and advance_prune_watermark fails instead of silently succeeding for a run with no prompt row. Addresses the fourth Codex review on #254. Refs: ENG-992, ENG-996 * chore(runtime): move CX3 to store schema 43 after AP4.2 took 42 main's AP4.2 (#257) took schema 42 (child_reports). The prune watermark is now step 43; step 42 accepts 43, every version list gains it, and the migration test upgrades from 42. The live-seam test's window grows with the larger tool block so its second prompt is not itself a seam, and it now asserts the watermark held still between the runs. Refs: ENG-992, ENG-996
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linear: ENG-1005. ADR-0054 § 4, slice AP4.2. This finishes decision 4: AP4.1 (#244) made read sub-agents run in the background, and this slice gives the parent control over them. Based on
main; nothing is stacked.What changes for users
The agent can now manage the read sub-agents it started:
wait_agents { ids?, timeout_seconds }. Blocks the call until every named sub-agent has finished, or until any one has when no ids are given, or until the timeout passes (1–600 s, at most 8 ids). The result lists each sub-agent as finished, still working, or unknown.cancel_agent { id }. Cancels one sub-agent and returns once it has stopped.spawn_agent. The two tools appear exactly whenspawn_agentdoes. Allowing, denying or exposingspawn_agentin policy or a pack applies to all three, and the two are not tool names of their own there (see the guide'sdelegationsection).Design
One delivery path. All answers go through AP4.1's single delivery row, which is marked delivered in the same transaction as the notice. That keeps exactly-once across tool results, turn boundaries, settlement and recovery. Putting answers in the tool results would have needed a second path that could double or drop them on a crash.
Only finished reports are sent. A report counts as finished once a later turn of the sub-agent has started. Until then a continuation (after an output cut, a fault or an interrupt) could still extend it, and the parent would see text that later changes.
child_reports, one row per delivered report. Answers and reports share one ordering per parent run, so a restarted session sees them in the order the live run did.When reports are sent. Two kinds of boundary deliver notices:
The wait therefore delivers at most once per boundary, with one tool-output budget. Nothing it delivers can land between the waiting reply and a steering message, which is the order replay rebuilds.
Teardown still waits for every child task. The store can deliver a child's answer before the child's own task has finished. The registry keeps that task, no longer counted as outstanding, until it ends.
Report text is loaded lazily. Reports are found by message id and their text is loaded newest-first, so a boundary normally reads one report. The same walk now serves blocking answers, delivered answers and interim reports. It also fixes a bug where a report stored as several messages in one turn kept only its last message.
Verification
Tests. 2103 workspace tests pass; fmt and clippy are clean; the soak passes; the nonblocking, delegation and progress suites passed 5 runs in a row. New tests in
sessions/tests/nonblocking.rs,lib.rs,plan.rsandmigrations.rscover:wait_agentswith named ids, with no ids, with a timeout, and with unknown, malformed or out-of-range arguments;cancel_agent, including a sub-agent that had already finished;spawn_agentthrough every policy filter;Mutation checks. Each test below was confirmed to fail with its fix reverted:
wait_agents_returns_when_the_named_children_settlean_interim_report_is_delivered_once_and_never_as_the_answeran_interim_report_is_delivered_once_and_never_as_the_answercancel_agent_stops_the_child_and_delivers_what_it_hadan_interim_report_does_not_restart_the_stall_counta_working_parent_receives_interim_reports_and_replays_thema_working_parent_receives_interim_reports_and_replays_thema_child_delivered_before_its_owner_finishes_is_still_draineda_tool_free_wait_takes_reports_only_with_an_answer,a_report_only_delivery_is_not_an_answera_report_split_across_messages_in_one_turn_is_joinedIndependent review. Three rounds, recorded in the ledger:
Benchmarks (medians against
main, alternating runs on a loaded machine; full numbers in the ledger):turn_overhead10/100/1000: 40.4/39.8/41.0 ms onmainagainst 42.1/39.6/40.8 ms;child_admission, all four cases: within noise, with identical peak concurrency and spend.The turn-top delivery now runs one store transaction per parent turn while sub-agents are running. A run without background sub-agents makes no store call.
A known flaky test, not from this PR. Under heavy fsync I/O,
child_mutation_drains_before_steering_or_a_replacement_run_can_writeand somedeadlinestests time out onmaintoo: 0 of 4 runs pass on bothmainand this branch with six writers forcing fsync. They pass on a quiet machine, and with every core busy. Details are in the ledger.Compatibility
DESCRIPTOR_VERSION12 → 13. Two new built-in tools.PROTOCOL_VERSIONchange. Reports reach clients through the child session's own events.