({
taskId,
abortController,
makeStream,
metadata,
description,
toolUseContext,
rootSetAppState,
agentIdForCleanup,
enableSummarization,
getWorktreeResult,
}: {
taskId: string
abortController: AbortController
makeStream: (
onCacheSafeParams: ((p: CacheSafeParams) => void) | undefined,
) => AsyncGenerator<MessageType, void>
metadata: Parameters<typeof finalizeAgentTool>[2]
description: string
toolUseContext: ToolUseContext
rootSetAppState: SetAppState
agentIdForCleanup: string
enableSummarization: boolean
getWorktreeResult: () => Promise<{
worktreePath?: string
worktreeBranch?: string
}>
})
| 506 | * Shared between AgentTool's async-from-start path and resumeAgentBackground. |
| 507 | */ |
| 508 | export async function runAsyncAgentLifecycle({ |
| 509 | taskId, |
| 510 | abortController, |
| 511 | makeStream, |
| 512 | metadata, |
| 513 | description, |
| 514 | toolUseContext, |
| 515 | rootSetAppState, |
| 516 | agentIdForCleanup, |
| 517 | enableSummarization, |
| 518 | getWorktreeResult, |
| 519 | }: { |
| 520 | taskId: string |
| 521 | abortController: AbortController |
| 522 | makeStream: ( |
| 523 | onCacheSafeParams: ((p: CacheSafeParams) => void) | undefined, |
| 524 | ) => AsyncGenerator<MessageType, void> |
| 525 | metadata: Parameters<typeof finalizeAgentTool>[2] |
| 526 | description: string |
| 527 | toolUseContext: ToolUseContext |
| 528 | rootSetAppState: SetAppState |
| 529 | agentIdForCleanup: string |
| 530 | enableSummarization: boolean |
| 531 | getWorktreeResult: () => Promise<{ |
| 532 | worktreePath?: string |
| 533 | worktreeBranch?: string |
| 534 | }> |
| 535 | }): Promise<void> { |
| 536 | let stopSummarization: (() => void) | undefined |
| 537 | const agentMessages: MessageType[] = [] |
| 538 | try { |
| 539 | const tracker = createProgressTracker() |
| 540 | const resolveActivity = createActivityDescriptionResolver( |
| 541 | toolUseContext.options.tools, |
| 542 | ) |
| 543 | const onCacheSafeParams = enableSummarization |
| 544 | ? (params: CacheSafeParams) => { |
| 545 | const { stop } = startAgentSummarization( |
| 546 | taskId, |
| 547 | asAgentId(taskId), |
| 548 | params, |
| 549 | rootSetAppState, |
| 550 | ) |
| 551 | stopSummarization = stop |
| 552 | } |
| 553 | : undefined |
| 554 | for await (const message of makeStream(onCacheSafeParams)) { |
| 555 | agentMessages.push(message) |
| 556 | // Append immediately when UI holds the task (retain). Bootstrap reads |
| 557 | // disk in parallel and UUID-merges the prefix — disk-write-before-yield |
| 558 | // means live is always a suffix of disk, so merge is order-correct. |
| 559 | rootSetAppState(prev => { |
| 560 | const t = prev.tasks[taskId] |
| 561 | if (!isLocalAgentTask(t) || !t.retain) return prev |
| 562 | const base = t.messages ?? [] |
| 563 | return { |
| 564 | ...prev, |
| 565 | tasks: { |
no test coverage detected