Conversation
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
force-pushed
the
feat/background-delegation-v2
branch
from
March 23, 2026 02:32
dc1b542 to
bc8cd14
Compare
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 Your PR had the right instinct months before this landed — appreciated. |
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.
Add
background=truetodelegate_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/backgroundslash command — with full config resolution, media/image extraction, and proper error handling.Implementation
tools/delegate_tool.py: newbackground=param. When true: validates single-task only, grabs gateway context (_gateway_runner,_gateway_source,_gateway_loop), schedules_run_background_task()viaasyncio.run_coroutine_threadsafe(), returns immediately.gateway/run.py: sets_gateway_source,_gateway_runner,_gateway_toolsets, and_gateway_loopon the agent beforerun_conversation()so delegate_tool can access them.No new files. No duplicate infrastructure. Reuses the existing
/backgrounddelivery path entirely.Closes #1599.