Add spawn_agent: multi-agent delegation with background execution and messaging - #416
Open
C-K-Loan wants to merge 9 commits into
Open
Add spawn_agent: multi-agent delegation with background execution and messaging#416C-K-Loan wants to merge 9 commits into
C-K-Loan wants to merge 9 commits into
Conversation
4 tasks
Collaborator
|
hi @C-K-Loan, this seems really nice! quick q:
|
Contributor
Author
|
Hi @Lazarus-931, glad to hear and thanks for catching these issues!
|
C-K-Loan
force-pushed
the
spawn-agent-v2
branch
from
August 28, 2026 15:58
cc28511 to
2c26c7c
Compare
Collaborator
|
nice:) conflicts as well! |
C-K-Loan
force-pushed
the
spawn-agent-v2
branch
from
August 29, 2026 13:06
2c26c7c to
9637e3a
Compare
…ncurrency Reviewer feedback: streamed deltas published on every token with no rate limit, so several concurrent sub-agents each streaming tokens could pile up unthrottled MainActor traffic. Gate publishes to ~20Hz per agent; completion always flushes unconditionally so nothing is ever lost.
Reviewer feedback: runConcurrentSpawnAgentBatch never called setTask, only startBackgroundSpawnAgent did -- so the cancel button was a silent no-op for the common (non-background) concurrent case. Split execute() into prepare()+run() like the background path already does, register a real Task via setTask, and reflect the registry's actual final status (previously always shown as succeeded regardless of the true outcome).
C-K-Loan
force-pushed
the
spawn-agent-v2
branch
from
August 29, 2026 13:14
9637e3a to
bdb031e
Compare
Contributor
Author
|
Addressed both issues in latest 2 commits should be ready to go :) @Lazarus-931 |
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.
This PR introduces the
spawn_agent()tool, along with helper toolslist_agents(),check_agent(),steer_agent().Agents can be spawned as a fork of the main agent (inheriting its context) or start with empty context. The main agent can choose to run sub-agents in the background or not (blocking vs. non-blocking).
New tools
Adds 4 new tool schemas to the model's tool list, ~606 marginal prompt tokens (measured live against the server: baseline 13 tokens, +606 with just these 4 tools added, vs. +1,700 for all 13 native tools combined).
spawn_agent(task, mode: fresh|branch, context, model, run_in_background): returns{ok, agent_id, status, stop_reason, answer, error}. Ifrun_in_backgroundisfalse, blocks until the sub-agent finishes andstatus/answerreflect its final result. Iftrue, returns immediately withstatus: "queued"and noansweryettask: the sub-agent's instructionmode:fresh(default): starts with just the task.branch: clones this conversation's history first, then adds the taskcontext: facts the sub-agent needs, without full history. Ignored inbranchmodemodel: a downloaded model ID, e.g.mlx-community/Qwen3-4B-4bit. Omit to use the current modelrun_in_background: if true, returns anagent_idimmediately instead of waiting. Check viacheck_agent/list_agents. Defaults tofalselist_agents(): returns{ok, agents: [{agent_id, task, status, stop_reason}]}, one entry per sub-agent spawned this sessioncheck_agent(agent_id, wait): returns{ok, agent_id, status, stop_reason, answer, error}for the given agentagent_id: fromspawn_agentorlist_agentswait: block until the agent reaches a terminal state instead of returning its current statussteer_agent(agent_id, message): returns{ok, error}.ok: falseif the agent is unknown or already finishedagent_id: fromspawn_agentorlist_agentsmessage: queued into the running sub-agent, delivered at its next turnSub-agents cannot call any of the 4 tools above on themselves (recursive spawning is blocked, and they'd otherwise waste rounds calling
list_agents/check_agentwith fabricated IDs).New Files:
ChatSpawnAgentTool.swiftspawn_agenttool schema + executor, two-phaseprepare()/run()design sorun_in_backgroundcan return immediatelyChatAgentRegistryTools.swiftlist_agents/check_agent/steer_agenttool schemas + executorsChatAgentRegistry.swift@MainActorin-memory registry of spawned agents (status, result, cancel), owned byChatViewModel, survives session switches but not app relaunchChatAgentLoop.swiftsubAgentToolDefinitions()which filters outswitch_model/spawn_agent/list_agents/check_agent/steer_agentUpdated Files:
ChatToolRegistry.swiftChatSessionStore.swiftChatViewModel.swiftrunChatLoopso multiple sequentialspawn_agentcalls run in parallel, not sequentially: addsrunConcurrentSpawnAgentBatch(blocking,TaskGroup-based, capped at 5 concurrent) andstartBackgroundSpawnAgent(non-blocking, fire-and-forget)ChatView.swiftChatToolRegistryTests.swiftFull quality: https://github.com/C-K-Loan/nativ/blob/demo-assets/demos/spawn-agent-multi-agent-demo.mp4
Improvements left for follow up PRs:
ChatAgentLoop.swift.check_agent(id). Instead, we should resume the coordinator's turn as soon as a background agent finishes (queuing the resume if the coordinator is mid-turn), rather than waiting on the whole batch or requiring a manual poll.