( config: InProcessSpawnConfig, context: SpawnContext, )
| 103 | * @returns Spawn result with teammate info |
| 104 | */ |
| 105 | export async function spawnInProcessTeammate( |
| 106 | config: InProcessSpawnConfig, |
| 107 | context: SpawnContext, |
| 108 | ): Promise<InProcessSpawnOutput> { |
| 109 | const { name, teamName, prompt, color, planModeRequired, model } = config |
| 110 | const { setAppState } = context |
| 111 | |
| 112 | // Generate deterministic agent ID |
| 113 | const agentId = formatAgentId(name, teamName) |
| 114 | const taskId = generateTaskId('in_process_teammate') |
| 115 | |
| 116 | logForDebugging( |
| 117 | `[spawnInProcessTeammate] Spawning ${agentId} (taskId: ${taskId})`, |
| 118 | ) |
| 119 | |
| 120 | try { |
| 121 | // Create independent AbortController for this teammate |
| 122 | // Teammates should not be aborted when the leader's query is interrupted |
| 123 | const abortController = createAbortController() |
| 124 | |
| 125 | // Get parent session ID for transcript correlation |
| 126 | const parentSessionId = getSessionId() |
| 127 | |
| 128 | // Create teammate identity (stored as plain data in AppState) |
| 129 | const identity: TeammateIdentity = { |
| 130 | agentId, |
| 131 | agentName: name, |
| 132 | teamName, |
| 133 | color, |
| 134 | planModeRequired, |
| 135 | parentSessionId, |
| 136 | } |
| 137 | |
| 138 | // Create teammate context for AsyncLocalStorage |
| 139 | // This will be used by runWithTeammateContext() during agent execution |
| 140 | const teammateContext = createTeammateContext({ |
| 141 | agentId, |
| 142 | agentName: name, |
| 143 | teamName, |
| 144 | color, |
| 145 | planModeRequired, |
| 146 | parentSessionId, |
| 147 | abortController, |
| 148 | }) |
| 149 | |
| 150 | // Register agent in Perfetto trace for hierarchy visualization |
| 151 | if (isPerfettoTracingEnabled()) { |
| 152 | registerPerfettoAgent(agentId, name, parentSessionId) |
| 153 | } |
| 154 | |
| 155 | // Create task state |
| 156 | const description = `${name}: ${prompt.substring(0, 50)}${prompt.length > 50 ? '...' : ''}` |
| 157 | |
| 158 | const taskState: InProcessTeammateTaskState = { |
| 159 | ...createTaskStateBase( |
| 160 | taskId, |
| 161 | 'in_process_teammate', |
| 162 | description, |
no test coverage detected