Skip to content

feat(agent): run subagents in the background - #581

Open
changer-changer wants to merge 5 commits into
OpenBMB:mainfrom
changer-changer:feat/subagent-background
Open

changer-changer wants to merge 5 commits into
OpenBMB:mainfrom
changer-changer:feat/subagent-background

Conversation

@changer-changer

@changer-changer changer-changer commented Sep 13, 2026

Copy link
Copy Markdown

An agent call currently blocks the parent until its child finishes. This adds optional run_in_background: true: the call returns a task id immediately, the parent can work on independent steps, and the child's result is delivered before the parent's normal final answer. Omission preserves synchronous behavior.

The implementation reuses BackgroundTaskRuntime and the existing task_list, task_output, task_wait, and task_stop tools. Agent tasks are scoped to the owning session. Completed results are persisted once as bounded, labelled untrusted tool output; pending children are joined within the parent's turn budget and cancelled when the request ends. Cancellation status is flushed before the parent completes, and late child events cannot leak into a later turn. Queue-capacity errors fail immediately.

This is active-request background work, with the existing child timeout and permissions. It adds no detached daemon, restart recovery, or settings page. Cancellation is cooperative; arbitrary JavaScript cannot be forcibly terminated. See the background contract for limits and examples.

Validation:

  • Production TypeScript build passed; final follow-up passed typechecking and focused tests.
  • Full backend run at concurrency 4: 557 passed, 0 failed, 7 cancelled, 2 skipped. The seven networkFetch cancellations also occur on the unchanged baseline. A subsequent regression test covers a stopped task being pruned before the tool returns.
  • Web regression using the repository's CI exclusions: 190 files / 1,525 tests passed.
  • Deterministic native-gateway probes verified independent parent work, durable child-result delivery, cancellation, and no stale child events in the next turn. Independent review and focused tests covered capacity, errors, retention, session isolation, max-turn handling and non-cooperative cancellation.
  • Integration with feat(agent): configure subagent roles and model bindings #576, feat(settings): expose subagent execution timeout #579 and feat(agent): continue completed subagent tasks #580 passed backend regression (637 passed, 0 failed, the same 7 cancellations, 2 skipped), Web regression (194 files / 1,584 tests), and Web build. Additional targeted tests cover combination guards and pruning. A controlled GLM-5.3-Flash experiment queued a custom role in the background, performed independent parent work, then restored the same child after gateway recreation and deletion of the original file; the child recalled the value without rereading it. This is an interface/behavior check, not a quality or speed comparison with OpenCode.

This PR is based directly on main for independent review. Related subagent PRs touch the same fork path; the combined validation branch contains resolved integration changes. That branch supports synchronous continuation of a completed background child; combining task_id with run_in_background: true is explicitly rejected rather than silently starting fresh.


Merge sequencing (added 2026-09-14)

This PR is one of four coordinated subagent-lifecycle PRs (#579, #576, #580, #581). The features are semantically independent but overlap textually in the agent core, so an explicit merge order is proposed in #583.

Proposed order: #579#576#580#581 (this PR: 4 (last)).

This branch now includes main at ecedc5c via merge commit 3341430 (2026-09-17). The conflicts with upstream #593 in AgentLoop.ts and events.ts are resolved: upstream timeline allocation remains the public run wrapper, background child ownership/cancellation runs inside it, and child block identities are retained. Sibling PRs still overlap in the agent core; their integration needs to be rechecked as they merge.

After each sibling PR merges, I will rebase the remaining branches onto main and re-run the targeted test suites before updating the PRs.

Validation after main sync (2026-09-17)

  • Gateway production build/typecheck and Web production build passed.
  • Focused background, agent-loop, timeline and gateway regressions: 103 passed. Added live/durable identity assertions for parent and child streams and a check that returning the public iterator cancels its background child. The identity regression fails when the upstream timeline wrapper is omitted, and passes with the merged wrapper.
  • Full backend: 643 passed, 0 failed, 7 cancelled, 2 skipped. All seven cancellations were reproduced from unchanged main network-fetch source/test files.
  • Web regression with the repository's CI exclusions and four workers: 203 files / 1,734 tests passed. An initial unrestricted parallel run timed out in two files (three failed tests); the bounded rerun passed without changing code or test assertions.

These are local validation results; remote CI and maintainer review remain separate requirements.

@changer-changer

Copy link
Copy Markdown
Author

Merged current main (69ec55b). Upstream #574 removed the text-encoded tool-call fallback that neighbored the background join block; the resolution keeps upstream's removal and re-anchors the join block unchanged.

Validation: tsc --noEmit clean; background specs 28/28; upstream regression specs from #574 (stream-interruption recovery, stream retry, reasoning content, dialog model selection, checkpoint) 65/65; full tests/agent/loop 65/65.

A retained terminal agent record can be pruned by new managed work while
task_wait is still awaiting; the post-await getOutput then failed with a
raw Unknown taskId. The wait result now carries the output slice captured
from the entry the wait itself holds, mirroring the task_stop fallback.
Preserve upstream timeline ordering and child block identities alongside background ownership and cancellation. Cover live/history identity and early iterator return in background regressions.
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