This page documents the internal lifecycle of subagents — from spawn to completion — including the manager, status tracking, tool fencing, and context propagation.
Spawn Flow
When spawn_agent is called, here’s what happens internally:
- Validate input — check that the role and message are valid
- Resolve role — look up in user roles → built-in roles → fallback
- Build config — create
AgentConfigwith role overrides:- System prompt override
- Max steps limit
- Optional model override
- Apply runtime override — layer session-scoped
/subagent-configmodel overrides - Inject briefing — append context briefing from shared parent state
- Inject notepad — append latest notepad wisdom (if available)
- Compute tool filter — apply
allowed_tools/disallowed_tools - Spawn via manager — hand off to
AgentManager::spawn_agent
Agent Manager
The AgentManager handles the actual agent lifecycle:
- Generate ID — unique UUID for the agent
- Assign nickname — from a name pool (for human-readable identification)
- Enforce limits — check
max_threadsandmax_depth - Create thread — dedicated thread with status channels
- Start task — begin execution and forward events
- Track handle — maintain reference for interaction and cleanup
Status Progression
Every subagent follows this status lifecycle:
PendingInit → Running → Completed / Errored / Shutdown
| Status | Meaning |
|---|---|
PendingInit | Created but not yet running |
Running | Actively executing |
Completed(result) | Finished successfully (with optional result text) |
Errored(message) | Failed with an error |
Shutdown | Manually closed |
NotFound | Agent ID doesn’t exist |
Parent-Facing Events
The parent agent receives events about subagent activity:
| Event | When |
|---|---|
SubAgentSpawned | A new subagent is created |
SubAgentStatusChanged | Status transitions |
SubAgentCompleted | Agent reaches terminal state |
Interaction Tools
send_input
Send follow-up instructions to a running agent:
send_input(agent_id: "abc-123", message: "Also add error handling")
Used for iterative refinement without spawning a new agent.
wait
Block until one or more agents reach a terminal state:
wait(agent_ids: ["abc-123", "def-456"])
wait() # wait for all
Prevents busy-polling loops. Supports optional timeout.
close_agent
Shut down an agent and free its slot:
close_agent(agent_id: "abc-123")
The agent’s status moves to Shutdown.
resume_agent
Reopen a completed or errored agent for more work:
resume_agent(agent_id: "abc-123", message: "Continue with the test file")
The agent’s full context is preserved.
Tool Visibility
Each subagent has a scoped tool set determined at spawn time:
allowed_toolsfrom the role → whitelistdisallowed_toolsfrom the role → subtracted from whitelist- At runtime,
ToolRegistry::executeenforces the gate
A subagent can only use tools in its allowed set. This prevents, for example, an explorer role from writing files.
Context Propagation
When a subagent is spawned, its ToolContext inherits from the parent with modifications:
| Field | Value |
|---|---|
depth | Parent’s depth + 1 |
team_name | Inherited |
agent_name | Set from role |
is_team_lead | false (unless spawned as team lead) |
todo_store | Shared handle |
index | Shared handle |
sandbox_level | Inherited |
subagent_model_overrides | Inherited runtime override map |
shared_context | Shared handle (for briefing) |
Depth Limits
Subagents can spawn subagents, but depth is limited:
[agent.agents]
max_threads = 4 # max concurrent agents
max_depth = 2 # max nesting depth
At depth 0 (main agent), spawning creates depth-1 agents. Depth-1 agents can spawn depth-2 agents. Depth-2 agents cannot spawn further.
Exceeding either limit returns an explicit error.
Registration Note
Subagent lifecycle tools (spawn_agent, send_input, wait, close_agent, resume_agent) are only registered in the TUI runtime. They are not available in nyz run or nyz exec.
This means automated CLI scripts cannot directly use subagents — use teams or sequential nyz exec calls instead.
Next Steps
- Roles & Context Briefing — role resolution and briefing
- Teams — structured multi-agent collaboration
- Architecture — overall system design