Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(claude): fence unconfirmed wake continuations after Stop
fmfsaisai100d2d4c3665
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/abort-stops-background-tasks.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/abort-stops-background-tasks.test.tsindex 7a1dc346aa..faa80d8198 100644--- a/packages/maker-core/src/agents/claude-code/__tests__/abort-stops-background-tasks.test.ts+++ b/packages/maker-core/src/agents/claude-code/__tests__/abort-stops-background-tasks.test.ts@@ -1624,6 +1624,87 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {await handle.close().catch(() => undefined);});+ it('stopTask reject 后为未确认 wake 安装栅栏,迟到自动续跑被丢弃', async () => {+ const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'spawn background work' });+ stream.emit(taskStarted('task-unconfirmed', 'local_agent'));+ await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');++ fakeQuery.stopTask!.mockRejectedValueOnce(new Error('remote stop rejected'));+ await handle.abort();+ stream.emit(interruptedTurnResult());+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'foreground stop terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'foreground stop settled');++ // The terminal task notification remains visible and clears local task+ // accounting; only the same-generation automatic continuation tail is fenced.+ stream.emit(taskNotification('task-unconfirmed', 'completed'));+ await waitFor(+ () => taskEvents(events).some((event) =>+ (event.data as { taskId?: unknown; status?: unknown } | null | undefined)?.taskId === 'task-unconfirmed' &&+ (event.data as { status?: unknown } | null | undefined)?.status === 'completed',+ ),+ 'unconfirmed task completion observed',+ );+ expect(handle.listBackgroundTasks?.()).toEqual([]);++ const eventCountBeforeLateTail = events.length;+ stream.emit(assistantText('late untracked continuation'));+ stream.emit(turnResult('late untracked result'));+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(events).toHaveLength(eventCountBeforeLateTail);+ expect(handle.isTurnRunning?.()).toBe(false);++ await handle.send({ type: 'user', content: 'fresh turn after fenced continuation' });+ expect(fakeQueries).toHaveLength(2);+ stream.emit(turnResult('fresh turn complete'));+ await waitFor(() => events.filter(isProductTerminal).length === 2, 'fresh turn terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'fresh turn settled');++ stream.end();+ await handle.close().catch(() => undefined);+ });++ it('老 daemon 无 stopTask 时也为未确认 wake 安装栅栏并在下一 send 换代', async () => {+ const { handle, stream, events, fakeQueries } = await startSessionWithStream({ omitStopTask: true });++ await handle.send({ type: 'user', content: 'spawn background work' });+ stream.emit(taskStarted('task-legacy', 'local_agent'));+ await waitFor(() => taskEvents(events).length >= 1, 'legacy wake task observed');++ await handle.abort();+ stream.emit(interruptedTurnResult());+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'legacy stop terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'legacy stop settled');++ stream.emit(taskNotification('task-legacy', 'completed'));+ await waitFor(+ () => taskEvents(events).some((event) =>+ (event.data as { taskId?: unknown; status?: unknown } | null | undefined)?.taskId === 'task-legacy' &&+ (event.data as { status?: unknown } | null | undefined)?.status === 'completed',+ ),+ 'legacy task completion observed',+ );+ expect(handle.listBackgroundTasks?.()).toEqual([]);++ const eventCountBeforeLateTail = events.length;+ stream.emit(assistantText('late legacy continuation'));+ stream.emit(turnResult('late legacy result'));+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(events).toHaveLength(eventCountBeforeLateTail);+ expect(handle.isTurnRunning?.()).toBe(false);++ await handle.send({ type: 'user', content: 'fresh turn after legacy fence' });+ expect(fakeQueries).toHaveLength(2);+ stream.emit(turnResult('fresh legacy turn complete'));+ await waitFor(() => events.filter(isProductTerminal).length === 2, 'fresh legacy terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'fresh legacy turn settled');++ stream.end();+ await handle.close().catch(() => undefined);+ });+it('stopTask 成功但 interrupt 失败时不伪造 continuation 收口', async () => {const { handle, stream, events, fakeQuery } = await startSessionWithStream();diff --git a/packages/maker-core/src/agents/claude-code/index.ts b/packages/maker-core/src/agents/claude-code/index.tsindex dc71198177..ccacda22ec 100644--- a/packages/maker-core/src/agents/claude-code/index.ts+++ b/packages/maker-core/src/agents/claude-code/index.ts@@ -3070,6 +3070,7 @@ export class ClaudeCodeAgent extends BaseAgent {// Query is replaced before the next explicit user turn.let continuationCancellationGeneration: number | null = null;let continuationCancellationRequiresQueryRebuild = false;+ let cancelledTailInterruptResultPending = false;const continuationClaims = new Map<number, ContinuationClaim>();const continuationListeners = new Set<(continuationId: number,@@ -3646,6 +3647,7 @@ export class ClaudeCodeAgent extends BaseAgent {// 残留的 interruptRequested 会错误抑制新 q 首个真实 is_error 终态 —— 兜底清。turnState.interruptRequested = false;continuationCancellationGeneration = null;+ cancelledTailInterruptResultPending = false;// Any successfully installed replacement Query isolates the cancelled// provider tail. This also covers rewind / invalid-resume rebuilds, so a// later send must not create a redundant intermediate Query.@@ -3670,9 +3672,15 @@ export class ClaudeCodeAgent extends BaseAgent {clearBridgeState();}const rawType = (rawMsg as { type?: string } | null)?.type;+ // Keep the foreground result that acknowledges user Stop; only+ // subsequent result/activity messages belong to the fenced tail.+ const isExpectedInterruptResult =+ cancelledTailInterruptResultPending &&+ rawType === 'result';if (continuationCancellationGeneration !== null &&- (rawType === 'assistant' || rawType === 'stream_event' || rawType === 'result')+ (rawType === 'assistant' || rawType === 'stream_event' || rawType === 'result') &&+ !isExpectedInterruptResult) {log.debug('ignoring provider activity from cancelled continuation query', {rawType,@@ -3865,6 +3873,7 @@ export class ClaudeCodeAgent extends BaseAgent {}: {}),});+ if (isExpectedInterruptResult) cancelledTailInterruptResultPending = false;if (shouldCorrelateResumeFailure) {deferResumeFailureBoundary = false;deferredResumeFailureTimer = setTimeout(@@ -4923,6 +4932,22 @@ export class ClaudeCodeAgent extends BaseAgent {terminalBackgroundTaskIds.clear();turnInFlight = false;}+ const hasUnconfirmedWakeTasks = [...runningBackgroundTasks.values()].some((info) => info.wake);+ if (+ hasUnconfirmedWakeTasks &&+ continuationCancellationGeneration !== turnState.generation+ ) {+ // A successful interrupt without a cancelled claim still leaves+ // wake tasks whose stop was rejected or unsupported. Keep the+ // product turn idle, but fence their queued auto-continuation+ // until the next send installs a fresh Query.+ continuationCancellationGeneration = turnState.generation;+ continuationCancellationRequiresQueryRebuild = true;+ cancelledTailInterruptResultPending = turnInFlight;+ log.info('installed cancelled-tail fence for unconfirmed wake tasks after user stop', {+ generation: turnState.generation,+ });+ }} catch (e) {// interrupt 失败 → 无 result 消费标记, 回收防误抑制(同 watchdog)。turnState.interruptRequested = false;