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): fence same-owner auth pushes during boundary

DavidShend93f8e8f649d
diff --git a/apps/desktop/src/renderer/__tests__/authContextRace.test.ts b/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
index 4acd7ef598..e5058b71aa 100644
--- a/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
+++ b/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
@@ -74,9 +74,8 @@ describe('AuthContext auth-state races', () => {
expect(source).toContain('setDataOwnerGeneration(dataOwnerId, ownerGeneration);');
expect(source).toContain('recentWorkdirsStore.setDataOwner(getDataOwnerGeneration());');
expect(source).toContain('invalidateProvidersSnapshot();');
- expect(source).toContain(
- 'publishDataOwnerGeneration(state.dataOwnerId, state.ownerGeneration);',
- );
+ expect(source).toContain('publishDataOwnerGeneration(');
+ expect(source).toContain('ownerChanged ? undefined : { finalizeSessions: false }');
expect(source).toContain(
'// Invalidate in-flight remote sends before the confirmation dialog resolves.',
);
diff --git a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
index e706488cad..c63da625af 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -26,6 +26,7 @@ const mocks = vi.hoisted(() => {
return {
service,
reset: vi.fn(),
+ cancelRemoteOptimisticSendsForDataOwnerBoundary: vi.fn(),
getMe: vi.fn(async () => ({ role: 'user' })),
clearWorkersCache: vi.fn(),
setModelVisibilityOwner: vi.fn(),
@@ -50,6 +51,11 @@ const mocks = vi.hoisted(() => {
vi.mock('@/lib/authService', () => ({
createAuthService: () => mocks.service,
}));
+vi.mock('@/lib/makerChatStore', () => ({
+ cancelRemoteOptimisticSendsForDataOwnerBoundary:
+ mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary,
+ setCurrentUserName: vi.fn(),
+}));
vi.mock('@/lib/sessionsStore', () => ({
sessionsStore: { reset: mocks.reset },
}));
@@ -148,6 +154,7 @@ describe('AuthContext session cache boundaries', () => {
beforeEach(() => {
mocks.reset.mockClear();
+ mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary.mockClear();
mocks.getMe.mockClear();
mocks.clearWorkersCache.mockClear();
mocks.setModelVisibilityOwner.mockClear();
@@ -236,6 +243,36 @@ describe('AuthContext session cache boundaries', () => {
},
);
+ it('does not finalize on a same-owner push while an auth boundary is pending', async () => {
+ let rejectLogout!: (error: Error) => void;
+ mocks.service.logout.mockReturnValueOnce(
+ new Promise<void>((_resolve, reject) => {
+ rejectLogout = reject;
+ }),
+ );
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBe('account-a'));
+ mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary.mockClear();
+
+ let logout!: Promise<void>;
+ act(() => {
+ logout = view.result.current.logout();
+ });
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledWith({
+ finalizeSessions: false,
+ });
+
+ act(() => mocks.emitAuth({ ...authState('account-a'), ownerGeneration: 2 }));
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: false,
+ });
+
+ await act(async () => {
+ rejectLogout(new Error('logout failed'));
+ await expect(logout).rejects.toThrow('logout failed');
+ });
+ });
+
it('keeps a newer pushed owner when an older auth boundary later rejects', 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 87f5eaad89..631a976652 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -214,7 +214,16 @@ export function AuthProvider({
const applyIncomingState = useCallback(
(state: AuthState) => {
const ownerChanged = activeDataOwnerIdRef.current !== state.dataOwnerId;
- publishDataOwnerGeneration(state.dataOwnerId, state.ownerGeneration);
+ // A same-owner push can arrive while an auth boundary is still waiting
+ // for IPC. The pre-commit fence temporarily publishes null, so letting
+ // that push use the default finalizer would stop the current owner's
+ // task even when the boundary later rejects. Only a committed owner
+ // change may finalize the previous owner's sessions.
+ publishDataOwnerGeneration(
+ state.dataOwnerId,
+ state.ownerGeneration,
+ ownerChanged ? undefined : { finalizeSessions: false },
+ );
if (ownerChanged) {
sessionsStore.reset();
clearWorkersCache();