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): retain overlapping owner rollback markers

DavidShen2f09596d48b2
diff --git a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
index f1ccdafdb0..b87bb23331 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -479,6 +479,42 @@ describe('AuthContext session cache boundaries', () => {
expect(mocks.preloadLocalCatalogSnapshot).toHaveBeenCalledTimes(2);
});
+ it('keeps the rollback marker for each overlapping boundary rejection', async () => {
+ let rejectFirst!: (error: Error) => void;
+ let rejectSecond!: (error: Error) => void;
+ mocks.service.logout
+ .mockReturnValueOnce(
+ new Promise<void>((_resolve, reject) => {
+ rejectFirst = reject;
+ }),
+ )
+ .mockReturnValueOnce(
+ new Promise<void>((_resolve, reject) => {
+ rejectSecond = reject;
+ }),
+ );
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBe('account-a'));
+ mocks.reconcileSessionsAfterDataOwnerRollback.mockClear();
+
+ const first = view.result.current.logout();
+ const second = view.result.current.logout();
+ act(() => mocks.emitAuth({ ...authState(null), ownerGeneration: 1 }));
+ act(() => mocks.emitAuth({ ...authState(null), ownerGeneration: 1 }));
+
+ await act(async () => {
+ rejectFirst(new Error('first logout failed'));
+ await expect(first).rejects.toThrow('first logout failed');
+ });
+ expect(mocks.reconcileSessionsAfterDataOwnerRollback).toHaveBeenCalledTimes(1);
+
+ await act(async () => {
+ rejectSecond(new Error('second logout failed'));
+ await expect(second).rejects.toThrow('second logout failed');
+ });
+ expect(mocks.reconcileSessionsAfterDataOwnerRollback).toHaveBeenCalledTimes(2);
+ });
+
it('restores a locally committed owner when the next boundary fails before an auth push', async () => {
mocks.service.initialize.mockResolvedValue(authState(null));
mocks.service.exitLocalMode.mockRejectedValueOnce(new Error('exit failed'));
diff --git a/apps/desktop/src/renderer/contexts/AuthContext.tsx b/apps/desktop/src/renderer/contexts/AuthContext.tsx
index 4b0b2f08cb..b53314e227 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -179,7 +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.
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
@@ -188,12 +192,17 @@ 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.
- pendingOwnerProjectionRef.current = false;
+ 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
@@ -205,7 +214,11 @@ export function AuthProvider({
{ finalizeSessions: false },
);
const rollbackNeedsReconcile = pendingOwnerProjectionRef.current;
- pendingOwnerProjectionRef.current = false;
+ pendingOwnerBoundaryCountRef.current = Math.max(
+ 0,
+ pendingOwnerBoundaryCountRef.current - 1,
+ );
+ if (pendingOwnerBoundaryCountRef.current === 0) pendingOwnerProjectionRef.current = false;
if (rollbackNeedsReconcile) {
void reconcileSessionsAfterDataOwnerRollback();
}