Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(claude): close cancelled continuation query immediately
fmfsaisai6227db413a92
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 2b906d40ee..3a0fdce259 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@@ -650,6 +650,68 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {await handle.close().catch(() => undefined);});+ it('user Stop 取消 awaiting claim 后立即关闭旧 Query,并在下一次 send 使用 fresh Query', async () => {+ const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'spawn background work' });+ stream.emit(taskStarted('task-agent', 'local_agent'));+ await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');+ stream.emit(turnResult('waiting for background task'));+ await waitFor(() => events.some((event) => event.type === 'done'), 'parent done observed');+ stream.emit(taskNotification('task-agent', 'completed'));+ await waitFor(() => taskEvents(events).length >= 2, 'task completion observed');+ expect(handle.isTurnRunning?.()).toBe(true);++ await handle.abort();+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);+ // 下一条 send 的 rebuildCancelledContinuationQuery 会对已关闭 stale Query+ // 再做一次幂等 close;这里锁定的是 send 之前立即发生的第一次 close。+ const closeCallsBeforeSend = fakeQuery.close.mock.calls.length;+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'synthetic terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'synthetic terminal acknowledged');++ await handle.send({ type: 'user', content: 'fresh turn after cancellation' });+ expect(fakeQueries).toHaveLength(2);+ expect(closeCallsBeforeSend).toBe(1);+ expect(fakeQueries[1]?.close).not.toHaveBeenCalled();+ 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('立即关闭后旧 Query 的迟到 assistant/result 不会产生新事件或终态', async () => {+ const { handle, stream, streams, events, fakeQuery } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'spawn background work' });+ stream.emit(taskStarted('task-agent', 'local_agent'));+ await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');+ stream.emit(turnResult('waiting for background task'));+ await waitFor(() => events.some((event) => event.type === 'done'), 'parent done observed');+ stream.emit(taskNotification('task-agent', 'completed'));+ await waitFor(() => taskEvents(events).length >= 2, 'task completion observed');++ await handle.abort();+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'synthetic terminal observed');+ const eventCountAfterCancellation = events.length;++ streams[0]?.emit(assistantText('late old-query assistant'));+ streams[0]?.emit(turnResult('late old-query result'));+ streams[0]?.emit(taskProgress('task-agent'));+ await new Promise((resolve) => setTimeout(resolve, 30));++ expect(events).toHaveLength(eventCountAfterCancellation);+ expect(events.filter(isProductTerminal)).toHaveLength(1);+ expect(handle.beginTurnContinuationWait?.(1)).toBeNull();+ expect(handle.isTurnRunning?.()).toBe(false);++ stream.end();+ await handle.close().catch(() => undefined);+ });+it('interrupt ACK 前真实 continuation done 先到时不再追加 synthetic 终态', 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 ee9e948991..adde162a2a 100644--- a/packages/maker-core/src/agents/claude-code/index.ts+++ b/packages/maker-core/src/agents/claude-code/index.ts@@ -3016,6 +3016,10 @@ export class ClaudeCodeAgent extends BaseAgent {}const canceledBridgeQueries = new WeakSet<Query>();+ // Despite the historical name, this per-query fence also marks an old+ // Query closed after user Stop cancels an awaiting continuation. Its+ // forward loop must silently discard any buffered tail until the next+ // fresh Query is installed.// Rewind commit 会 close 当前 Query,但它的 forward loop 可能晚于下一次 send 的// rebuild 完成才退出。pendingRewindTo 是共享状态,会在新 q 接管后清掉;旧 q 自身仍// 需要一个 per-query 标记,否则迟到的 stream_end/abort 会被误判为当前新 q 的崩溃。@@ -4874,6 +4878,26 @@ export class ClaudeCodeAgent extends BaseAgent {// the provider tasks themselves.retireContinuationTasks(cancelledContinuation);emitCancelledContinuationBoundary(cancelledContinuation, 'user_stop');+ // The provider may already have queued the automatic continuation+ // on this Query. Close it now so that cancellation fences process+ // termination and canUseTool callbacks as well as event output;+ // the next send will perform the existing fresh-query rebuild.+ const cancelledQuery = q;+ canceledBridgeQueries.add(cancelledQuery);+ inputQueue.clear();+ try {+ inputQueue.end();+ } catch (e) {+ log.warn('user_stop continuation cancellation: inputQueue.end threw', { error: String(e) });+ }+ try {+ cancelledQuery.close();+ } catch (e) {+ log.warn('user_stop continuation cancellation: q.close threw', { error: String(e) });+ }+ runningBackgroundTasks.clear();+ terminalBackgroundTaskIds.clear();+ turnInFlight = false;}} catch (e) {// interrupt 失败 → 无 result 消费标记, 回收防误抑制(同 watchdog)。