( taskId: string, setAppState: SetAppStateFn, )
| 226 | * @returns true if killed successfully |
| 227 | */ |
| 228 | export function killInProcessTeammate( |
| 229 | taskId: string, |
| 230 | setAppState: SetAppStateFn, |
| 231 | ): boolean { |
| 232 | let killed = false |
| 233 | let teamName: string | null = null |
| 234 | let agentId: string | null = null |
| 235 | let toolUseId: string | undefined |
| 236 | let description: string | undefined |
| 237 | let pendingAutonomyRuns: Array<{ runId: string; rootDir?: string }> = [] |
| 238 | |
| 239 | setAppState((prev: AppState) => { |
| 240 | const task = prev.tasks[taskId] |
| 241 | if (!task || task.type !== 'in_process_teammate') { |
| 242 | return prev |
| 243 | } |
| 244 | |
| 245 | const teammateTask = task as InProcessTeammateTaskState |
| 246 | |
| 247 | if (teammateTask.status !== 'running') { |
| 248 | return prev |
| 249 | } |
| 250 | |
| 251 | // Capture identity for cleanup after state update |
| 252 | teamName = teammateTask.identity.teamName |
| 253 | agentId = teammateTask.identity.agentId |
| 254 | toolUseId = teammateTask.toolUseId |
| 255 | description = teammateTask.description |
| 256 | |
| 257 | // Capture pending autonomy run IDs before clearing them |
| 258 | pendingAutonomyRuns = teammateTask.pendingUserMessages.flatMap(message => |
| 259 | message.autonomyRunId |
| 260 | ? [ |
| 261 | { |
| 262 | runId: message.autonomyRunId, |
| 263 | ...(message.autonomyRootDir |
| 264 | ? { rootDir: message.autonomyRootDir } |
| 265 | : {}), |
| 266 | }, |
| 267 | ] |
| 268 | : [], |
| 269 | ) |
| 270 | |
| 271 | // Abort the controller to stop execution |
| 272 | teammateTask.abortController?.abort() |
| 273 | |
| 274 | // Call cleanup handler |
| 275 | teammateTask.unregisterCleanup?.() |
| 276 | |
| 277 | // Update task state and remove from teamContext.teammates |
| 278 | killed = true |
| 279 | |
| 280 | // Call pending idle callbacks to unblock any waiters (e.g., engine.waitForIdle) |
| 281 | teammateTask.onIdleCallbacks?.forEach(cb => cb()) |
| 282 | |
| 283 | // Remove from teamContext.teammates using the agentId |
| 284 | let updatedTeamContext = prev.teamContext |
| 285 | if (prev.teamContext && prev.teamContext.teammates && agentId) { |
no test coverage detected