Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(claude): retire Query after successful wake stop (#2127)
mengfanyu9410-hubeae674c9452e
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 be5b43e8d2..3a664a6ada 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@@ -1498,6 +1498,26 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {await handle.close().catch(() => undefined);});+ it('keeps an ordinary foreground Stop interrupt-only when no wake task was running', async () => {+ const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'ordinary foreground turn' });+ await handle.abort();++ // Retiring a Query is only needed once Stop touched a wake task. Normal+ // foreground cancellation stays on the provider's interrupt path.+ expect(fakeQuery.interrupt).toHaveBeenCalledTimes(1);+ expect(fakeQuery.close).not.toHaveBeenCalled();+ expect(fakeQueries).toHaveLength(1);++ stream.emit(interruptedTurnResult());+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'ordinary stopped terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'ordinary stopped turn settled');++ stream.end();+ await handle.close().catch(() => undefined);+ });+it('keeps the wake latch across task_updated patches that omit task_type', async () => {const { handle, stream, events, fakeQuery } = await startSessionWithStream();@@ -1525,14 +1545,15 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {await handle.send({ type: 'user', content: 'foreground turn with wake task' });stream.emit(taskStarted('task-unconfirmed', 'local_agent'));- await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');+ stream.emit(taskStarted('task-bash', 'local_bash'));+ await waitFor(() => taskEvents(events).length >= 2, 'wake and bash tasks observed');fakeQuery.stopTask!.mockRejectedValueOnce(new Error('remote stop rejected'));await handle.abort();- // The ACK succeeds, so the old provider process is retired even though its- // stopTask RPC was rejected. A synthetic foreground terminal replaces the- // interrupted result that close() intentionally prevents from arriving.+ // The interrupt ACK succeeds, so the old provider process is retired even+ // though its stopTask RPC was rejected. A synthetic foreground terminal+ // replaces the interrupted result that close() intentionally prevents.expect(fakeQuery.close).toHaveBeenCalledTimes(1);await waitFor(() => events.filter(isProductTerminal).length === 1, 'synthetic foreground terminal observed');expect(events.filter(isProductTerminal)).toHaveLength(1);@@ -1556,6 +1577,73 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {await handle.close().catch(() => undefined);});+ it('Stop closes a foreground Query even after every wake stopTask succeeds', async () => {+ const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'foreground turn with wake task' });+ stream.emit(taskStarted('task-stopped', 'local_agent'));+ await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');++ await handle.abort();++ // A successful stopTask can still race an already queued SDK auto-continuation,+ // so the provider Query must be replaced before another explicit user turn.+ expect(fakeQuery.stopTask).toHaveBeenCalledWith('task-stopped');+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'synthetic foreground terminal observed');++ const eventCountAfterStop = events.length;+ stream.emit(assistantText('late automatic continuation'));+ stream.emit(turnResult('late automatic result'));+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(events).toHaveLength(eventCountAfterStop);++ await handle.send({ type: 'user', content: 'fresh turn after successful wake stop' });+ expect(fakeQueries).toHaveLength(2);+ stream.emit(turnResult('fresh turn complete'));+ await waitFor(() => events.filter(isProductTerminal).length === 2, 'fresh terminal observed');++ stream.end();+ await handle.close().catch(() => undefined);+ });++ it('successful wake Stop closes a mixed wake + local_bash Query before rebuild', async () => {+ const { handle, stream, streams, events, fakeQuery, fakeQueries } = await startSessionWithStream();++ await handle.send({ type: 'user', content: 'foreground with wake and dev server' });+ stream.emit(taskStarted('task-agent', 'local_agent'));+ stream.emit(taskStarted('task-bash', 'local_bash'));+ await waitFor(() => taskEvents(events).length >= 2, 'wake and bash tasks observed');++ await handle.abort();++ expect(fakeQuery.stopTask).toHaveBeenCalledWith('task-agent');+ expect(fakeQuery.stopTask).not.toHaveBeenCalledWith('task-bash');+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);+ expect(fakeQueries).toHaveLength(1);+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'single Stop terminal observed');+ expect(events.filter(isProductTerminal)).toHaveLength(1);+ expect(handle.isTurnRunning?.()).toBe(false);++ await handle.send({ type: 'user', content: 'rebuild after mixed Stop' });+ expect(fakeQueries).toHaveLength(2);++ // A late tail from the stopped generation must not become the replacement+ // turn's terminal or leak the local_bash row into the rebuilt Query.+ streams[0]?.emit(interruptedTurnResult());+ streams[0]?.emit(taskNotification('task-bash', 'completed'));+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(events.filter(isProductTerminal)).toHaveLength(1);+ expect(handle.listBackgroundTasks?.()).toEqual([]);++ stream.emit(turnResult('rebuilt Query turn complete'));+ await waitFor(() => events.filter(isProductTerminal).length === 2, 'rebuilt Query terminal observed');+ await waitFor(() => handle.isTurnRunning?.() === false, 'rebuilt Query turn settled');+ expect(fakeQueries).toHaveLength(2);+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);+ expect(handle.listBackgroundTasks?.()).toEqual([]);+ });+it('fresh Query does not inherit a thinking-only marker from the aborted turn', async () => {const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();diff --git a/packages/maker-core/src/agents/claude-code/index.ts b/packages/maker-core/src/agents/claude-code/index.tsindex ccd9a6db5f..b8ced99234 100644--- a/packages/maker-core/src/agents/claude-code/index.ts+++ b/packages/maker-core/src/agents/claude-code/index.ts@@ -3444,7 +3444,9 @@ export class ClaudeCodeAgent extends BaseAgent {// Install the cancelled-tail fence at the same enqueue boundary as// the synthetic product terminal. stopTask can win before interrupt// resolves, so waiting for the interrupt ACK leaves a double-terminal- // window for a late provider result.+ // window for a late provider result. A live Query that owns local_bash+ // is always retired by global Stop; the fence remains as a defensive+ // guard until the replacement Query is installed.continuationCancellationGeneration = turnState.generation;continuationCancellationRequiresQueryRebuild = true;}@@ -5100,7 +5102,6 @@ export class ClaudeCodeAgent extends BaseAgent {// interrupt ACK guarantees these earlier stop requests have settled,// so allSettled intentionally has no extra timeout here.const settledStops = await Promise.allSettled(stopRequests.map(({ promise }) => promise));- const hasRejectedStops = settledStops.some((stop) => stop.status === 'rejected');const fulfilledWakeIds = stopRequests.filter((_, index) => settledStops[index]?.status === 'fulfilled').map(({ taskId }) => taskId);@@ -5118,7 +5119,18 @@ export class ClaudeCodeAgent extends BaseAgent {// to prevent a queued automatic continuation from escaping Stop.const cancelledContinuation = stoppedClaim ?? cancelActiveContinuation('user_stop');const hasUnconfirmedWakeTasks = [...runningBackgroundTasks.values()].some((info) => info.wake);- if (cancelledContinuation || hasUnconfirmedWakeTasks || hasRejectedStops) {+ // Once Stop has dispatched stopTask for any wake task, the provider+ // Query must be retired regardless of RPC outcome. A fulfilled RPC+ // only acknowledges the cancellation request; it cannot prove that+ // an automatic continuation was not already queued in the provider.+ // Closing the Query is therefore the only boundary that guarantees+ // no later model call. Mixed local_bash tasks intentionally die with+ // this Query; preserving them would reopen the unsafe same-Query path.+ const shouldRetireQuery =+ stopRequests.length > 0 ||+ cancelledContinuation ||+ hasUnconfirmedWakeTasks;+ if (shouldRetireQuery) {// All cancellation sources share one terminal state: a cancelled// continuation claim, an unconfirmed wake task, or a rejected stop// means this Query must be retired. The provider tail is fenced by