InProcessTeammate

Description

One of three agent execution models (alongside LocalAgentTask and RemoteAgentTask). InProcessTeammate runs a child agent inside the same Node.js process as the leader, using Node's AsyncLocalStorage from the async_hooks module to isolate each teammate's identity context. Unlike pane-based teammates (tmux/iTerm2) which spawn separate OS processes, in-process teammates share the leader's process resources -- API client, MCP connections, file-state caches -- while maintaining logically isolated execution contexts.

The system uses two layered AsyncLocalStorage instances: TeammateContext (in teammateContext.ts) carries the teammate's identity (agentId, agentName, teamName, color, planModeRequired, parentSessionId) and lifecycle AbortController; AgentContext (in agentContext.ts) carries analytics attribution metadata (agentType, invokingRequestId, invocationKind). Both are entered via runWithTeammateContext() and runWithAgentContext() at the top of each teammate's agent loop, so all downstream async operations -- API calls, tool use, telemetry -- resolve to the correct identity without parameter drilling.

The execution lifecycle follows a continuous prompt loop: the teammate receives an initial prompt, runs the standard runAgent() loop (identical to what the AgentTool uses for subagents), transitions to idle on completion, then polls for the next message or shutdown request. This makes teammates persistent actors rather than one-shot agents. The loop only exits on AbortController signal or when the model approves a shutdown request.

In-process mode is selected automatically when no pane backend (tmux/iTerm2) is available, or can be forced via the teammateMode: 'in-process' setting. It is always used for non-interactive sessions (-p mode). When pane-backend detection fails at spawn time, the system falls back to in-process mode and records the fallback so subsequent spawns stay consistent.

Key claims

Relations

Sources