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