MCPcopy Create free account
hub / github.com/WJX20/claude-code / poll

Function poll

src/tasks/RemoteAgentTask/RemoteAgentTask.tsx:559–797  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

557 // at end of run; scanning only the delta (response.newEvents) is O(new).
558 let cachedReviewContent: string | null = null;
559 const poll = async (): Promise<void> => {
560 if (!isRunning) return;
561 try {
562 const appState = context.getAppState();
563 const task = appState.tasks?.[taskId] as RemoteAgentTaskState | undefined;
564 if (!task || task.status !== 'running') {
565 // Task was killed externally (TaskStopTool) or already terminal.
566 // Session left alive so the claude.ai URL stays valid — the run_hunt.sh
567 // post_stage() calls land as assistant events there, and the user may
568 // want to revisit them after closing the terminal. TTL reaps it.
569 return;
570 }
571 const response = await pollRemoteSessionEvents(task.sessionId, lastEventId);
572 lastEventId = response.lastEventId;
573 const logGrew = response.newEvents.length > 0;
574 if (logGrew) {
575 accumulatedLog = [...accumulatedLog, ...response.newEvents];
576 const deltaText = response.newEvents.map(msg => {
577 if (msg.type === 'assistant') {
578 return (msg as unknown as SDKMessageWithMessage).message.content.filter(block => block.type === 'text').map(block => 'text' in block ? block.text : '').join('\n');
579 }
580 return jsonStringify(msg);
581 }).join('\n');
582 if (deltaText) {
583 appendTaskOutput(taskId, deltaText + '\n');
584 }
585 }
586 if (response.sessionStatus === 'archived') {
587 updateTaskState<RemoteAgentTaskState>(taskId, context.setAppState, t => t.status === 'running' ? {
588 ...t,
589 status: 'completed',
590 endTime: Date.now()
591 } : t);
592 enqueueRemoteNotification(taskId, task.title, 'completed', context.setAppState, task.toolUseId);
593 void evictTaskOutput(taskId);
594 void removeRemoteAgentMetadata(taskId);
595 return;
596 }
597 const checker = completionCheckers.get(task.remoteTaskType);
598 if (checker) {
599 const completionResult = await checker(task.remoteTaskMetadata);
600 if (completionResult !== null) {
601 updateTaskState<RemoteAgentTaskState>(taskId, context.setAppState, t => t.status === 'running' ? {
602 ...t,
603 status: 'completed',
604 endTime: Date.now()
605 } : t);
606 enqueueRemoteNotification(taskId, completionResult, 'completed', context.setAppState, task.toolUseId);
607 void evictTaskOutput(taskId);
608 void removeRemoteAgentMetadata(taskId);
609 return;
610 }
611 }
612
613 // Ultraplan: result(success) fires after every CCR turn, so it must not
614 // drive completion — startDetachedPoll owns that via ExitPlanMode scan.
615 // Long-running monitors (autofix-pr) emit result per notification cycle,
616 // so the same skip applies.

Callers 1

Calls 14

pollRemoteSessionEventsFunction · 0.85
jsonStringifyFunction · 0.85
appendTaskOutputFunction · 0.85
evictTaskOutputFunction · 0.85
extractReviewTagFromLogFunction · 0.85
extractTodoListFromLogFunction · 0.85
extractReviewFromLogFunction · 0.85
updateTaskStateFunction · 0.50

Tested by

no test coverage detected