Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(claude): close query after rejected wake stop

fmfsaisai2d1625aaf65a
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.ts
index 436c1369e8..6ceb05b418 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
@@ -1597,6 +1597,46 @@ describe('ClaudeCodeAgent abort stops background wake tasks', () => {
await handle.close().catch(() => undefined);
});
+ it('Stop closes when a rejected stop is followed by completed notification before interrupt ACK', async () => {
+ const { handle, stream, events, fakeQuery, fakeQueries } = await startSessionWithStream();
+ const interrupt = createDeferred<void>();
+
+ await handle.send({ type: 'user', content: 'foreground turn with racing completion' });
+ stream.emit(taskStarted('task-completed-during-stop', 'local_agent'));
+ await waitFor(() => taskEvents(events).length >= 1, 'wake task observed');
+ fakeQuery.stopTask!.mockRejectedValueOnce(new Error('task already completed remotely'));
+ fakeQuery.interrupt.mockImplementationOnce(() => interrupt.promise);
+
+ const abortPromise = handle.abort();
+ await waitFor(() => fakeQuery.interrupt.mock.calls.length === 1, 'interrupt dispatched');
+ stream.emit(taskNotification('task-completed-during-stop', 'completed'));
+ await waitFor(
+ () => taskEvents(events).some((event) =>
+ (event.data as { taskId?: unknown; status?: unknown } | null | undefined)?.taskId === 'task-completed-during-stop' &&
+ (event.data as { status?: unknown } | null | undefined)?.status === 'completed',
+ ),
+ 'completed notification observed before interrupt ACK',
+ );
+ expect(handle.listBackgroundTasks?.()).toEqual([]);
+
+ interrupt.resolve(undefined);
+ await abortPromise;
+ expect(fakeQuery.close).toHaveBeenCalledTimes(1);
+ await waitFor(() => events.filter(isProductTerminal).length === 1, 'synthetic terminal observed');
+ expect(events.filter(isProductTerminal)).toHaveLength(1);
+ expect(handle.isTurnRunning?.()).toBe(false);
+
+ await handle.send({ type: 'user', content: 'fresh turn after rejected stop' });
+ expect(fakeQueries).toHaveLength(2);
+ stream.emit(turnResult('fresh turn complete'));
+ await waitFor(() => events.filter(isProductTerminal).length === 2, 'fresh terminal observed');
+ expect(events.filter(isProductTerminal)).toHaveLength(2);
+ expect(handle.isTurnRunning?.()).toBe(false);
+
+ stream.end();
+ await handle.close().catch(() => undefined);
+ });
+
it('natural foreground result before Stop ACK is not duplicated when the Query closes', 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.ts
index d478cf2432..3ecb21d88d 100644
--- a/packages/maker-core/src/agents/claude-code/index.ts
+++ b/packages/maker-core/src/agents/claude-code/index.ts
@@ -4939,6 +4939,7 @@ export class ClaudeCodeAgent extends BaseAgent {
// interrupt ACK guarantees these earlier stop requests have settled,
// so allSettled intentionally has no timeout or extra watchdog.
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);
@@ -4950,15 +4951,18 @@ export class ClaudeCodeAgent extends BaseAgent {
// Stop is the authoritative cancellation boundary. An awaiting
// continuation may already have lost every task from the running
// table, so neither stopTask nor an idle interrupt is guaranteed to
- // produce another provider result. Close it explicitly after the
- // control action succeeds and append the ordered product terminal.
+ // produce another provider result. A rejected stop is likewise
+ // unconfirmed even when a completed notification already removed
+ // that task from the local table; the old Query must still be closed
+ // 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) {
- // Both cancellation sources share one terminal state: a cancelled
- // continuation claim or an unconfirmed wake task means this Query
- // must be retired. The provider tail is fenced by the per-query
- // marker, then the next send/rewind installs a fresh Query.
+ if (cancelledContinuation || hasUnconfirmedWakeTasks || hasRejectedStops) {
+ // 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
+ // the per-query marker, then the next send/rewind installs a fresh
+ // Query.
const cancelledQuery = q;
const foregroundNeedsTerminal = turnInFlight;
canceledBridgeQueries.add(cancelledQuery);