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): 保留跨窗口账号边界回滚标记

DavidShena13728720e16
diff --git a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
index 8c38650007..96e28f1783 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -474,6 +474,44 @@ describe('AuthContext session cache boundaries', () => {
expect(getDataOwnerGeneration().dataOwnerId).toBe('account-a');
});
+ it('keeps a newer renderer pending marker when an older boundary settles first', async () => {
+ let resolveLogout!: () => void;
+ mocks.service.logout.mockReturnValueOnce(
+ new Promise<void>((resolve) => {
+ resolveLogout = resolve;
+ }),
+ );
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBe('account-a'));
+ mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary.mockClear();
+ mocks.reconcileSessionsAfterDataOwnerRollback.mockClear();
+
+ let logout!: Promise<void>;
+ act(() => {
+ logout = view.result.current.logout();
+ });
+ act(() =>
+ mocks.emitAuth({ ...authState(null), ownerGeneration: 1, ownerBoundaryPending: true }),
+ );
+ act(() => mocks.emitAuth({ ...authState('account-b'), ownerGeneration: 2 }));
+ // A second window starts another boundary after the first commit but
+ // before the first window receives its own IPC promise settlement.
+ act(() =>
+ mocks.emitAuth({ ...authState(null), ownerGeneration: 2, ownerBoundaryPending: true }),
+ );
+
+ await act(async () => {
+ resolveLogout();
+ await logout;
+ });
+ act(() => mocks.emitAuth({ ...authState('account-b'), ownerGeneration: 2 }));
+
+ expect(mocks.reconcileSessionsAfterDataOwnerRollback).toHaveBeenCalledOnce();
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).not.toHaveBeenLastCalledWith({
+ finalizeSessions: true,
+ });
+ });
+
it('reconciles Main runtime on IPC rejection without a rollback push', async () => {
let rejectLogout!: (error: Error) => void;
mocks.service.logout.mockReturnValueOnce(
diff --git a/apps/desktop/src/renderer/contexts/AuthContext.tsx b/apps/desktop/src/renderer/contexts/AuthContext.tsx
index f4fc39cc0b..3f596fd645 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -179,11 +179,11 @@ export function AuthProvider({
const activeDataOwnerGenerationRef = useRef(0);
const authStateVersionRef = useRef(0);
const hasAppliedAuthStateRef = useRef(false);
- // Each in-flight auth boundary owns its own rollback opportunity. Main
- // serializes the mutations, but renderer promises may reject separately;
- // a single boolean would let the first rejection consume the second marker.
+ // Main broadcasts pending projections to every renderer. Keep this marker
+ // tied to the authoritative owner snapshots rather than local IPC promises:
+ // another window may have started a second boundary before this renderer's
+ // first operation settles.
const pendingOwnerProjectionRef = useRef(false);
- const pendingOwnerBoundaryCountRef = useRef(0);
// Auth mutations invalidate owner-bound in-flight reads before crossing IPC. If Main rejects
// the transition, restore the single authoritative owner ref (which successful siblings and
@@ -192,17 +192,9 @@ export function AuthProvider({
// Invalidate old-owner ingress before crossing IPC, but defer stopping the
// cached turn until the new owner has committed. On failure, reconcile
// against Main: an early rejection keeps the runtime, a late one may not.
- pendingOwnerBoundaryCountRef.current += 1;
publishDataOwnerGeneration(null, undefined, { finalizeSessions: false });
try {
const result = await operation();
- // A successful boundary owns the marker lifecycle. A same-owner auth
- // push during teardown must not consume a possible rollback marker.
- pendingOwnerBoundaryCountRef.current = Math.max(
- 0,
- pendingOwnerBoundaryCountRef.current - 1,
- );
- if (pendingOwnerBoundaryCountRef.current === 0) pendingOwnerProjectionRef.current = false;
return result;
} catch (error) {
// Restore the exact main-owned generation. Recomputing it locally would
@@ -214,11 +206,6 @@ export function AuthProvider({
{ finalizeSessions: false },
);
const rollbackNeedsReconcile = pendingOwnerProjectionRef.current;
- pendingOwnerBoundaryCountRef.current = Math.max(
- 0,
- pendingOwnerBoundaryCountRef.current - 1,
- );
- if (pendingOwnerBoundaryCountRef.current === 0) pendingOwnerProjectionRef.current = false;
if (rollbackNeedsReconcile) {
void reconcileSessionsAfterDataOwnerRollback();
}