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): preserve initial pending owner rollback

DavidShena42ae63ed5d6
diff --git a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
index b87bb23331..19c3a1d830 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -91,6 +91,7 @@ import { AuthProvider, useAuth } from '@/contexts/AuthContext';
import {
__testing as dataOwnerGenerationTesting,
getDataOwnerGeneration,
+ setDataOwnerGeneration,
} from '@/contexts/dataOwnerGeneration';
import {
__resetForTest as resetEnginePrefs,
@@ -230,6 +231,25 @@ describe('AuthContext session cache boundaries', () => {
});
});
+ it('preserves the owner when the first snapshot is a boundary-pending projection', async () => {
+ setDataOwnerGeneration('account-a', 1);
+ mocks.service.initialize.mockResolvedValue({ ...authState(null), ownerGeneration: 1 });
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBeNull());
+
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: false,
+ });
+
+ act(() => mocks.emitAuth(authState('account-a')));
+
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: false,
+ });
+ expect(mocks.reconcileSessionsAfterDataOwnerRollback).toHaveBeenCalledOnce();
+ expect(view.result.current.dataOwnerId).toBe('account-a');
+ });
+
it('resets sessions when authentication expires', async () => {
renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(mocks.service.initialize).toHaveBeenCalled());
diff --git a/apps/desktop/src/renderer/contexts/AuthContext.tsx b/apps/desktop/src/renderer/contexts/AuthContext.tsx
index b53314e227..bbbf72d700 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -247,12 +247,29 @@ export function AuthProvider({
// It keeps the old owner generation, so retain the authoritative owner
// refs until the commit or rollback arrives; otherwise the rollback is
// mistaken for a new owner and finalizes the task that should resume.
+ const rendererOwnerGeneration = getDataOwnerGeneration();
+ const initialBoundaryPendingProjection =
+ !hasAppliedAuthStateRef.current
+ && activeDataOwnerIdRef.current === null
+ && rendererOwnerGeneration.dataOwnerId !== null
+ && state.ownerGeneration === rendererOwnerGeneration.generation;
const pendingSignedOutProjection =
state.dataOwnerId === null
&& state.mode === 'signed-out'
&& !state.canEnterApp
- && activeDataOwnerIdRef.current !== null
- && state.ownerGeneration === activeDataOwnerGenerationRef.current;
+ && (
+ (activeDataOwnerIdRef.current !== null
+ && state.ownerGeneration === activeDataOwnerGenerationRef.current)
+ || initialBoundaryPendingProjection
+ );
+ if (initialBoundaryPendingProjection && pendingSignedOutProjection) {
+ // A newly mounted renderer may receive the transient signed-out
+ // projection before it has hydrated its refs. Seed them from the
+ // synchronous owner stamp so the following rollback remains a
+ // same-owner push and cannot finalize the running task.
+ activeDataOwnerIdRef.current = rendererOwnerGeneration.dataOwnerId;
+ activeDataOwnerGenerationRef.current = rendererOwnerGeneration.generation;
+ }
const initialOwnerHydration = !hasAppliedAuthStateRef.current && !pendingSignedOutProjection;
const ownerChanged =
!pendingSignedOutProjection && activeDataOwnerIdRef.current !== state.dataOwnerId;