Architecture Diagram
Spawn Modes
- default (in-process)
- fork (child process)
- worktree (isolated git worktree + fork)
- remote (bridge)
The agent runs in the same process as the parent. State is shared via Use this for lightweight sub-tasks that do not need file isolation.
AsyncLocalStorage context isolation. This is the lowest-overhead spawn mode — no process fork, no worktree creation.Spawning a Sub-Agent with AgentTool
Main agent calls AgentTool
The main agent invokes
AgentTool with a task description and the desired spawn mode. AgentTool is registered in the tool dispatch map and implements the standard Tool<Input, Output, Progress> interface.AgentTool creates a Task record
A new
Task with a prefixed ID (e.g., a_<8chars> for agent tasks) is created and persisted. Task IDs encode their type: b=bash, a=agent, r=remote, t=team.Sub-agent starts with fresh messages[]
For fork/worktree modes, the child process starts with an empty
messages[]. The task description becomes the first user message. The child has its own QueryEngine instance.Agent Communication
SendMessageTool
SendMessageTool provides request-response messaging between agents. One agent sends a message to another agent’s inbox by task ID; the recipient polls for new messages in its idle cycle.
TaskCreate / TaskUpdate / TaskGet / TaskList
Agents share a file-based task board. Any agent can create tasks, claim them, update their status, and list all open work.| Tool | Description |
|---|---|
TaskCreateTool | Creates a new task with description, priority, and optional dependencies |
TaskUpdateTool | Updates status (open, in_progress, done, blocked) |
TaskGetTool | Retrieves a single task by ID |
TaskListTool | Lists all tasks, optionally filtered by status or assignee |
TaskStopTool | Cancels a running task |
TaskOutputTool | Reads streamed output from a background task |
TeamCreate / TeamDelete
TeamCreateTool spawns a set of named teammates as InProcessTeammateTask instances. Each teammate gets an async mailbox and runs concurrently in the same process. TeamDeleteTool shuts down the team and cleans up resources.
Coordinator Mode (Swarm)
When enabled,coordinatorMode.ts gives each teammate an idle cycle: when a teammate has no active work, it automatically scans the shared task board and claims the highest-priority unclaimed task. This eliminates the need for a lead agent to manually assign work.
getCoordinatorUserContext() from QueryEngine.ts:115-117:
Worktree Isolation (s12)
EnterWorktreeTool creates a new git worktree for the calling agent and changes its cwd to the new directory. ExitWorktreeTool removes the worktree and restores the original cwd. Worktrees are bound to task IDs so cleanup is deterministic even if the agent crashes.
src/utils/worktree.ts and are called from bridgeMain.ts (createAgentWorktree, removeAgentWorktree).