Skip to content

feat(delegation): add background=true for fire-and-forget subagent tasks - #2570

Closed
anpicasso wants to merge 1 commit into
NousResearch:mainfrom
anpicasso:feat/background-delegation-v2
Closed

anpicasso wants to merge 1 commit into
NousResearch:mainfrom
anpicasso:feat/background-delegation-v2

Conversation

@anpicasso

@anpicasso anpicasso commented Mar 23, 2026 •

Copy link
Copy Markdown

Add background=true to delegate_task() for non-blocking subagent execution.

When set, the call returns immediately with {status: 'dispatched', background_task_id: '...'}. The subagent runs via the gateway's existing _run_background_task() — the same battle-tested flow used by the /background slash command — with full config resolution, media/image extraction, and proper error handling.

Implementation

  • tools/delegate_tool.py: new background= param. When true: validates single-task only, grabs gateway context (_gateway_runner, _gateway_source, _gateway_loop), schedules _run_background_task() via asyncio.run_coroutine_threadsafe(), returns immediately.
  • gateway/run.py: sets _gateway_source, _gateway_runner, _gateway_toolsets, and _gateway_loop on the agent before run_conversation() so delegate_tool can access them.

No new files. No duplicate infrastructure. Reuses the existing /background delivery path entirely.

gateway/run.py         |  10 ++-
tools/delegate_tool.py | 217 +++++++++++++++++++++++++---------
2 files changed, 169 insertions(+), 58 deletions(-)

Closes #1599.

Add background=true to delegate_task() for non-blocking subagent execution.
When set, the child agent runs in a daemon thread and the call returns
immediately with {status: 'dispatched', background_task_id: '...'}. Results
are delivered to the user's chat asynchronously — same delivery path as
the /background slash command.

Implementation:
- tools/background_delegation_registry.py: new thread-safe registry
  (BackgroundDelegationSession dataclass + BackgroundDelegationRegistry
  singleton) — tracks running/finished sessions, provides drain() for
  gateway pickup and wait() for watcher polling.
- delegate_task(): new background= param. When true: validates single-task
  only, checks gateway context (_gateway_source), registers session,
  spawns daemon thread, returns immediately.
- gateway/run.py: imports registry; wires _gateway_source/_gateway_toolsets
  onto agent before run_conversation(); drain_pending() after agent returns;
  new _run_background_delegation_watcher() coroutine delivers results via
  adapter.send().

Closes #1599.
@anpicasso
anpicasso force-pushed the feat/background-delegation-v2 branch from dc1b542 to bc8cd14 Compare March 23, 2026 02:32
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/tools Tool registry, model_tools, toolsets tool/delegate Subagent delegation comp/gateway Gateway runner, session dispatch, delivery labels May 3, 2026
@teknium1

teknium1 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this contribution! Closing as superseded: current main went further than this PR — background delegation is now the mandatory default for all top-level delegate_task calls, not an opt-in flag. The background parameter still exists but is documented as deprecated/ignored (tools/delegate_tool.py), single-task and batch delegations all run on a daemon executor and return immediately, with results re-entering the conversation via the async-delegation completion queue. Only orchestrator subagents (depth > 0) run synchronously.

Your PR had the right instinct months before this landed — appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery comp/tools Tool registry, model_tools, toolsets P3 Low — cosmetic, nice to have tool/delegate Subagent delegation type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(delegation): add background=true mode for fire-and-forget subagent tasks

3 participants