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): finalize committed null-owner transitions

DavidShen2244c0b557db
diff --git a/apps/desktop/src/renderer/__tests__/authContextRace.test.ts b/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
index e5058b71aa..0dd717fe83 100644
--- a/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
+++ b/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
@@ -75,7 +75,7 @@ describe('AuthContext auth-state races', () => {
expect(source).toContain('recentWorkdirsStore.setDataOwner(getDataOwnerGeneration());');
expect(source).toContain('invalidateProvidersSnapshot();');
expect(source).toContain('publishDataOwnerGeneration(');
- expect(source).toContain('ownerChanged ? undefined : { finalizeSessions: false }');
+ expect(source).toContain('{ finalizeSessions: ownerChanged }');
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 a719fd2f4a..25ca4e563c 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -273,6 +273,72 @@ describe('AuthContext session cache boundaries', () => {
});
});
+ it('finalizes once when a successful logout commits the null owner after the pending projection', 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();
+
+ let logout!: Promise<void>;
+ act(() => {
+ logout = view.result.current.logout();
+ });
+ act(() => mocks.emitAuth({ ...authState(null), ownerGeneration: 1 }));
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: false,
+ });
+
+ act(() => {
+ mocks.emitAuth(authState(null));
+ resolveLogout();
+ });
+ await act(async () => {
+ await logout;
+ });
+
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: true,
+ });
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledTimes(2);
+
+ act(() => mocks.emitAuth(authState(null)));
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledTimes(2);
+ });
+
+ it.each([false, true])('finalizes exitLocalMode once with a preceding commit push: %s', async (pushCommit) => {
+ mocks.service.initialize.mockResolvedValue(localAuthState());
+ const committedState = { ...authState(null), ownerGeneration: 4 };
+ mocks.service.exitLocalMode.mockImplementationOnce(async () => {
+ mocks.emitAuth({ ...authState(null), ownerGeneration: 3 });
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledTimes(1);
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: false,
+ });
+ if (pushCommit) mocks.emitAuth(committedState);
+ return committedState;
+ });
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBe('local-v1'));
+ mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary.mockClear();
+
+ await act(async () => {
+ await view.result.current.exitLocalMode();
+ });
+
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: true,
+ });
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledTimes(2);
+ expect(getDataOwnerGeneration()).toEqual({ dataOwnerId: null, generation: 4 });
+ act(() => mocks.emitAuth(committedState));
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledTimes(2);
+ });
+
it('does not finalize when a failed boundary rolls back from a pending signed-out 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 baf456e8ee..d1eb545784 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -127,8 +127,13 @@ function publishDataOwnerGeneration(
options?: { finalizeSessions?: boolean },
): void {
const previousOwnerId = getDataOwnerGeneration().dataOwnerId;
- if (previousOwnerId !== dataOwnerId) {
+ const ownerChanged = previousOwnerId !== dataOwnerId;
+ if (ownerChanged) {
resetTaskTagCatalogCache();
+ }
+ // The pre-commit fence may already publish null. A committed owner change
+ // must still close sessions even when that published owner stays null.
+ if (ownerChanged || options?.finalizeSessions === true) {
if (options) cancelRemoteOptimisticSendsForDataOwnerBoundary(options);
else cancelRemoteOptimisticSendsForDataOwnerBoundary();
}
@@ -233,7 +238,7 @@ export function AuthProvider({
publishDataOwnerGeneration(
state.dataOwnerId,
state.ownerGeneration,
- ownerChanged ? undefined : { finalizeSessions: false },
+ { finalizeSessions: ownerChanged },
);
if (ownerChanged) {
sessionsStore.reset();