Skip to content

feat(runtime): wait_agents, cancel_agent, and interim reports for background sub-agents (AP4.2) - #257

Merged
lg2m merged 6 commits into
mainfrom
feat/eng-1005-ap4-2-wait-cancel-agents
Oct 5, 2026
Merged

lg2m merged 6 commits into
mainfrom
feat/eng-1005-ap4-2-wait-cancel-agents

Conversation

@lg2m

@lg2m lg2m commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

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.
  • Answers arrive as notices. Neither tool returns a sub-agent's answer in its result. The answer arrives as the usual runtime notice at the next turn, the same path AP4.1 uses, so each answer still reaches the parent exactly once.
  • Interim reports. A sub-agent that is still working sends its newest finished progress report to the parent as a labelled notice, at most once per report. The parent can use partial findings or cancel the sub-agent early. Reports carry no cost and never count as progress.
  • The tools travel with spawn_agent. The two tools appear exactly when spawn_agent does. Allowing, denying or exposing spawn_agent in policy or a pack applies to all three, and the two are not tool names of their own there (see the guide's delegation section).
  • Prompt. The prompt gains one bullet about reports and the two tools.

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.

    • Each boundary sends at most one report per sub-agent, its newest. Older undelivered reports are skipped.
    • A sub-agent that has finished is answered instead.
    • Schema 42 adds 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 turn-top boundary sends reports whenever sub-agents are running;
    • the tool-free wait sends a report only in the same transaction as an answer, and an answer always ends the wait.

    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.rs and migrations.rs cover:

    • a working parent receiving interim reports end to end through the session runtime, with replay compared against the live requests;
    • wait_agents with 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;
    • a report that isn't finished yet, a newer report superseding an older one, no repeat delivery, a report finished by the final-answer turn, and a crash after a report was delivered;
    • the wait sending reports only with an answer, and steering during a wait while a report is pending;
    • reports not counting as progress;
    • the three tools following spawn_agent through every policy filter;
    • teardown waiting for a child whose answer was already delivered;
    • the schema 41 → 42 migration.
  • Mutation checks. Each test below was confirmed to fail with its fix reverted:

    Change Fails
    named waits return on any child wait_agents_returns_when_the_named_children_settle
    unfinished reports are delivered an_interim_report_is_delivered_once_and_never_as_the_answer
    already-delivered reports are sent again an_interim_report_is_delivered_once_and_never_as_the_answer
    cancel never sends the cancel signal cancel_agent_stops_the_child_and_delivers_what_it_had
    a report counts as progress an_interim_report_does_not_restart_the_stall_count
    reports disabled a_working_parent_receives_interim_reports_and_replays_them
    report replay order shifted a_working_parent_receives_interim_reports_and_replays_them
    delivered entries dropped immediately a_child_delivered_before_its_owner_finishes_is_still_drained
    the wait sends reports without an answer a_tool_free_wait_takes_reports_only_with_an_answer, a_report_only_delivery_is_not_an_answer
    the old multi-message report join a_report_split_across_messages_in_one_turn_is_joined
  • Independent review. Three rounds, recorded in the ledger:

    • first round: request changes (1 blocking, 5 should-fix, 4 nits), all fixed;
    • second round: one new blocking finding (replay order when steering arrives during a wait) and one should-fix (budget per boundary), both fixed;
    • third round: approved.
  • Benchmarks (medians against main, alternating runs on a loaded machine; full numbers in the ledger):

    • turn_overhead 10/100/1000: 40.4/39.8/41.0 ms on main against 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_write and some deadlines tests time out on main too: 0 of 4 runs pass on both main and this branch with six writers forcing fsync. They pass on a quiet machine, and with every core busy. Details are in the ledger.

Compatibility

  • Store schema 41 → 42. The migration adds an empty table. Older builds refuse a schema-42 store, as with every schema bump.
  • DESCRIPTOR_VERSION 12 → 13. Two new built-in tools.
  • Agent prompt 16 → 17.
  • No PROTOCOL_VERSION change. Reports reach clients through the child session's own events.

lg2m added 4 commits October 5, 2026 00:23
…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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T17:49:15.642056Z 8c2f88a New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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
lg2m merged commit f15a4d2 into main Oct 5, 2026
6 checks passed
@lg2m
lg2m deleted the feat/eng-1005-ap4-2-wait-cancel-agents branch October 5, 2026 18:58
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
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.

1 participant