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(desktop): clear continuation state on owner finalization

DavidShen3159ad0d4d11
diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
index e0d0a66b7d..5397fbdfb6 100644
--- a/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
@@ -976,6 +976,8 @@ describe('getRunningSnapshot 后台 subagent 折算(真 store)', () => {
recovery: null,
errorRetryText: null,
credentialSwitchWait: null,
+ continuationInFlightClientId: 'manual-continuation',
+ continuationTurnClientId: 'manual-continuation-turn',
});
makerChatStore.__applyStatusUpdateForTest(remoteSid, statusUpdate(remoteSid, true));
applyTask(remoteSid, { taskId: 'remote-t1', status: 'running', taskType: 'local_agent' });
@@ -1005,6 +1007,9 @@ describe('getRunningSnapshot 后台 subagent 折算(真 store)', () => {
expect(state.pendingQueue.map((item) => item.clientId)).toEqual([
'user-queued-owner-finalize',
]);
+ expect(state.continuationInFlightClientId).toBeNull();
+ expect(state.continuationTurnClientId).toBeNull();
+ expect(state.continuationInFlightProjectionCapability).toBe('unknown');
expect(makerChatStore.getSnapshot(remoteSid).agentStatus.isRunning).toBe(true);
// Re-entering account A must observe the stopped snapshot, with no
@@ -1394,4 +1399,50 @@ describe('getRunningSnapshot 后台 subagent 折算(真 store)', () => {
globalWindow.window = previousWindow;
}
});
+
+ it('preserves idle background work found by the second Main reconciliation read', async () => {
+ const sid = 'rollback-second-read-idle-' + Math.random().toString(36).slice(2, 8);
+ const globalWindow = globalThis as typeof globalThis & {
+ window?: { electronAPI?: unknown };
+ };
+ const previousWindow = globalWindow.window;
+ try {
+ setDataOwnerGeneration('account-a', 1);
+ makerChatStore.__applyStatusUpdateForTest(sid, statusUpdate(sid, true));
+ applyTask(sid, {
+ taskId: 'second-read-background',
+ status: 'running',
+ taskType: 'local_bash',
+ });
+ makerChatStore.__applyStatusUpdateForTest(sid, statusUpdate(sid, false));
+ let resolveFirst!: (value: unknown[]) => void;
+ let listActiveCalls = 0;
+ const listActive = vi.fn(() => {
+ listActiveCalls += 1;
+ if (listActiveCalls === 1) {
+ return new Promise<unknown[]>((resolve) => { resolveFirst = resolve; });
+ }
+ return Promise.resolve([
+ { sessionId: sid, agentKind: 'codex', isTurnRunning: false },
+ ]);
+ });
+ globalWindow.window = {
+ electronAPI: { maker: { listActive } },
+ } as typeof globalWindow.window;
+
+ const reconciliation = reconcileSessionsAfterDataOwnerRollback();
+ await Promise.resolve();
+ resolveFirst([]);
+ await reconciliation;
+
+ expect(listActive).toHaveBeenCalledTimes(2);
+ expect(
+ makerChatStore.getSnapshot(sid).taskUpdates?.get('second-read-background')?.status,
+ ).toBe('running');
+ } finally {
+ makerChatStore.purgeSession(sid);
+ dataOwnerGenerationTesting.reset();
+ globalWindow.window = previousWindow;
+ }
+ });
});
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 930e32217b..fc60b19981 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -2176,6 +2176,7 @@ function hasActiveTurnStateForOwnerBoundary(state: SessionChatState): boolean {
state.pendingRemoteDesktopConfirmationQueue.length > 0 ||
state.queueAbortPending ||
state.steeringQueueClientIds.length > 0 ||
+ state.continuationInFlightClientId !== null ||
state.continuationTurnClientId !== null ||
state.pendingTaskWake > 0 ||
state.messages.some(
@@ -6760,6 +6761,7 @@ function forceFinalizeOnSessionClosed(state: SessionChatState): SessionChatState
!state.messages.some((m) => m.isStreaming) &&
!state.queueAbortPending &&
state.steeringQueueClientIds.length === 0 &&
+ state.continuationInFlightClientId === null &&
state.continuationTurnClientId === null &&
state.pendingTaskWake === 0 &&
!state.messages.some(
@@ -6824,7 +6826,9 @@ function forceFinalizeOnSessionClosed(state: SessionChatState): SessionChatState
pendingRemoteDesktopConfirmationQueue: [],
queueAbortPending: false,
steeringQueueClientIds: [],
+ continuationInFlightClientId: null,
continuationTurnClientId: null,
+ continuationInFlightProjectionCapability: 'unknown',
// session 都关了,后台任务事件流已断:running 残留任务标 stopped、唤醒桥接
// 清零,否则 running 快照(折算了后台任务)会让 spinner 永久转下去。
taskUpdates: stoppedTasks,
@@ -9926,7 +9930,7 @@ export async function reconcileSessionsAfterDataOwnerRollback(): Promise<void> {
// A live-but-idle handle has already stopped its turn and must take the
// same finalizer path.
const current = sessions.get(id);
- const mainTurnRunning = liveTurns.get(id);
+ let mainTurnRunning = liveTurns.get(id);
if (mainTurnRunning === true || !current || !sameActiveTurnBoundaryMarker(id, current, marker))
continue;
// The first query can legitimately race a replacement turn created by
@@ -9936,7 +9940,11 @@ export async function reconcileSessionsAfterDataOwnerRollback(): Promise<void> {
const latest = await listActive();
if (getDataOwnerGeneration() !== owner) return;
if (!Array.isArray(latest) || !latest.every(isActiveSessionSnapshot)) return;
- if (latest.some((item) => item.sessionId === id && item.isTurnRunning === true)) {
+ const latestSession = latest.find((item) => item.sessionId === id);
+ if (latestSession) {
+ mainTurnRunning = latestSession.isTurnRunning;
+ }
+ if (mainTurnRunning === true) {
continue;
}
}