({
agentId,
description,
prompt,
selectedAgent,
setAppState,
parentAbortController,
toolUseId
}: {
agentId: string;
description: string;
prompt: string;
selectedAgent: AgentDefinition;
setAppState: SetAppState;
parentAbortController?: AbortController;
toolUseId?: string;
})
| 465 | * This ensures subagents are aborted when their parent (e.g., in-process teammate) aborts. |
| 466 | */ |
| 467 | export function registerAsyncAgent({ |
| 468 | agentId, |
| 469 | description, |
| 470 | prompt, |
| 471 | selectedAgent, |
| 472 | setAppState, |
| 473 | parentAbortController, |
| 474 | toolUseId |
| 475 | }: { |
| 476 | agentId: string; |
| 477 | description: string; |
| 478 | prompt: string; |
| 479 | selectedAgent: AgentDefinition; |
| 480 | setAppState: SetAppState; |
| 481 | parentAbortController?: AbortController; |
| 482 | toolUseId?: string; |
| 483 | }): LocalAgentTaskState { |
| 484 | void initTaskOutputAsSymlink(agentId, getAgentTranscriptPath(asAgentId(agentId))); |
| 485 | |
| 486 | // Create abort controller - if parent provided, create child that auto-aborts with parent |
| 487 | const abortController = parentAbortController ? createChildAbortController(parentAbortController) : createAbortController(); |
| 488 | const taskState: LocalAgentTaskState = { |
| 489 | ...createTaskStateBase(agentId, 'local_agent', description, toolUseId), |
| 490 | type: 'local_agent', |
| 491 | status: 'running', |
| 492 | agentId, |
| 493 | prompt, |
| 494 | selectedAgent, |
| 495 | agentType: selectedAgent.agentType ?? 'general-purpose', |
| 496 | abortController, |
| 497 | retrieved: false, |
| 498 | lastReportedToolCount: 0, |
| 499 | lastReportedTokenCount: 0, |
| 500 | isBackgrounded: true, |
| 501 | // registerAsyncAgent immediately backgrounds |
| 502 | pendingMessages: [], |
| 503 | retain: false, |
| 504 | diskLoaded: false |
| 505 | }; |
| 506 | |
| 507 | // Register cleanup handler |
| 508 | const unregisterCleanup = registerCleanup(async () => { |
| 509 | killAsyncAgent(agentId, setAppState); |
| 510 | }); |
| 511 | taskState.unregisterCleanup = unregisterCleanup; |
| 512 | |
| 513 | // Register task in AppState |
| 514 | registerTask(taskState, setAppState); |
| 515 | return taskState; |
| 516 | } |
| 517 | |
| 518 | // Map of taskId -> resolve function for background signals |
| 519 | // When backgroundAgentTask is called, it resolves the corresponding promise |
no test coverage detected