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): keep rollback reconciliation resilient to renderer updates

DavidShene78a761c43a1
diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
index 74085ffa9c..90561d1fe8 100644
--- a/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreTaskUpdates.test.ts
@@ -1001,11 +1001,21 @@ describe('getRunningSnapshot 后台 subagent 折算(真 store)', () => {
applyTask(sid, { taskId: 't1', status: 'running', taskType: 'local_agent' });
makerChatStore.__applyStatusUpdateForTest(remoteSid, statusUpdate(remoteSid, true));
applyTask(remoteSid, { taskId: 'remote-t1', status: 'running', taskType: 'local_agent' });
+ let resolveListActive!: (value: unknown[]) => void;
+ const listActive = vi.fn(
+ () => new Promise<unknown[]>((resolve) => { resolveListActive = resolve; }),
+ );
globalWindow.window = {
- electronAPI: { maker: { listActive: vi.fn(async () => []) } },
+ electronAPI: { maker: { listActive } },
} as typeof globalWindow.window;
- await reconcileSessionsAfterDataOwnerRollback();
+ const reconciliation = reconcileSessionsAfterDataOwnerRollback();
+ await Promise.resolve();
+ // An unrelated renderer update replaces the state object while Main is
+ // being queried; it must not hide the still-eligible rollback candidate.
+ makerChatStore.setContextWindow(sid, 1234);
+ resolveListActive([]);
+ await reconciliation;
expect(makerChatStore.getSnapshot(sid).agentStatus.isRunning).toBe(false);
expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('stopped');
expect(makerChatStore.getSnapshot(remoteSid).agentStatus.isRunning).toBe(true);
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 44487e69cd..78ac950a7c 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -9779,6 +9779,37 @@ interface ActiveSessionSnapshot {
isTurnRunning: boolean;
}
+interface ActiveTurnBoundaryMarker {
+ startedAt: number | null;
+ streamingClientId: string | null;
+ continuationTurnClientId: string | null;
+ pendingTaskWakeGen: number;
+ isStreaming: boolean;
+}
+
+function captureActiveTurnBoundaryMarker(state: SessionChatState): ActiveTurnBoundaryMarker {
+ return {
+ startedAt: state.agentStatus.startedAt,
+ streamingClientId: state.streamingClientId,
+ continuationTurnClientId: state.continuationTurnClientId,
+ pendingTaskWakeGen: state.pendingTaskWakeGen,
+ isStreaming: state.isStreaming,
+ };
+}
+
+function sameActiveTurnBoundaryMarker(
+ state: SessionChatState,
+ marker: ActiveTurnBoundaryMarker,
+): boolean {
+ return (
+ state.agentStatus.startedAt === marker.startedAt &&
+ state.streamingClientId === marker.streamingClientId &&
+ state.continuationTurnClientId === marker.continuationTurnClientId &&
+ state.pendingTaskWakeGen === marker.pendingTaskWakeGen &&
+ state.isStreaming === marker.isStreaming
+ );
+}
+
function isActiveSessionSnapshot(value: unknown): value is ActiveSessionSnapshot {
if (!value || typeof value !== 'object') return false;
const item = value as Record<string, unknown>;
@@ -9795,9 +9826,10 @@ export async function reconcileSessionsAfterDataOwnerRollback(): Promise<void> {
if (typeof listActive !== 'function') return;
const owner = getDataOwnerGeneration();
if (owner.dataOwnerId === null) return;
- const candidates = [...sessions].filter(([id, state]) =>
- !isRemoteSessionSticky(id) && hasActiveTurnStateForOwnerBoundary(state),
- );
+ const candidates = [...sessions].flatMap(([id, state]) => {
+ if (isRemoteSessionSticky(id) || !hasActiveTurnStateForOwnerBoundary(state)) return [];
+ return [[id, captureActiveTurnBoundaryMarker(state)] as const];
+ });
if (candidates.length === 0) return;
try {
const active = await listActive();
@@ -9806,11 +9838,18 @@ export async function reconcileSessionsAfterDataOwnerRollback(): Promise<void> {
if (getDataOwnerGeneration() !== owner) return;
if (!Array.isArray(active) || !active.every(isActiveSessionSnapshot)) return;
const liveTurns = new Map(active.map((item) => [item.sessionId, item.isTurnRunning]));
- for (const [id, state] of candidates) {
+ for (const [id, marker] of candidates) {
// Keep a turn that Main still reports running, and never apply a delayed
- // absence to a new turn or changed session. A live-but-idle handle has
- // already stopped its turn and must take the same finalizer path.
- if (liveTurns.get(id) === true || sessions.get(id) !== state) continue;
+ // absence to a new turn or changed session. Ignore unrelated renderer
+ // updates while retaining the marker for the turn we actually queried.
+ // A live-but-idle handle has already stopped its turn and must take the
+ // same finalizer path.
+ const current = sessions.get(id);
+ if (
+ liveTurns.get(id) === true ||
+ !current ||
+ !sameActiveTurnBoundaryMarker(current, marker)
+ ) continue;
bumpInteractionReconcileEpoch(id);
supersedeInputProjectionRequests(id, { supersedeOperations: true });
flushPendingTextDelta(id);