()
| 746 | let cachedReviewContent: string | null = null; |
| 747 | |
| 748 | const poll = async (): Promise<void> => { |
| 749 | if (!isRunning) return; |
| 750 | |
| 751 | try { |
| 752 | const appState = context.getAppState(); |
| 753 | const task = appState.tasks?.[taskId] as RemoteAgentTaskState | undefined; |
| 754 | if (!task || task.status !== 'running') { |
| 755 | // Task was killed externally (TaskStopTool) or already terminal. |
| 756 | // Session left alive so the claude.ai URL stays valid — the run_hunt.sh |
| 757 | // post_stage() calls land as assistant events there, and the user may |
| 758 | // want to revisit them after closing the terminal. TTL reaps it. |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | const response = await pollRemoteSessionEvents(task.sessionId, lastEventId); |
| 763 | lastEventId = response.lastEventId; |
| 764 | const logGrew = response.newEvents.length > 0; |
| 765 | if (logGrew) { |
| 766 | accumulatedLog = [...accumulatedLog, ...response.newEvents]; |
| 767 | const deltaText = response.newEvents |
| 768 | .map(msg => { |
| 769 | if (msg.type === 'assistant') { |
| 770 | const content = (msg as SDKAssistantMessage).message?.content; |
| 771 | if (!content || typeof content === 'string') return ''; |
| 772 | return (content as Array<{ type: string; text?: string }>) |
| 773 | .filter(block => block.type === 'text') |
| 774 | .map(block => ('text' in block ? block.text : '')) |
| 775 | .join('\n'); |
| 776 | } |
| 777 | return jsonStringify(msg); |
| 778 | }) |
| 779 | .join('\n'); |
| 780 | if (deltaText) { |
| 781 | appendTaskOutput(taskId, deltaText + '\n'); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (response.sessionStatus === 'archived') { |
| 786 | updateTaskState<RemoteAgentTaskState>(taskId, context.setAppState, t => |
| 787 | t.status === 'running' ? { ...t, status: 'completed', endTime: Date.now() } : t, |
| 788 | ); |
| 789 | const richContent = tryExtractRichContent(task, accumulatedLog); |
| 790 | if (richContent) { |
| 791 | enqueueRichRemoteNotification( |
| 792 | taskId, |
| 793 | task.title, |
| 794 | 'completed', |
| 795 | richContent, |
| 796 | context.setAppState, |
| 797 | task.toolUseId, |
| 798 | ); |
| 799 | } else { |
| 800 | enqueueRemoteNotification(taskId, task.title, 'completed', context.setAppState, task.toolUseId); |
| 801 | } |
| 802 | void evictTaskOutput(taskId); |
| 803 | void removeRemoteAgentMetadata(taskId); |
| 804 | runCompletionHook(taskId, task); |
| 805 | return; |
no test coverage detected