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 restored turns during auth hydration

DavidShen5a699cfff8a0
diff --git a/apps/desktop/src/renderer/__tests__/authContextRace.test.ts b/apps/desktop/src/renderer/__tests__/authContextRace.test.ts
index 0dd717fe83..d6694e9ad8 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('{ finalizeSessions: ownerChanged }');
+ expect(source).toContain('{ finalizeSessions: ownerChanged && !initialOwnerHydration }');
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 4dd852dd0e..1b88f4bd51 100644
--- a/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/authContextSessionBoundary.test.tsx
@@ -213,6 +213,23 @@ describe('AuthContext session cache boundaries', () => {
expect(mocks.reset).toHaveBeenCalledTimes(4);
});
+ it('does not finalize restored turns during the first owner hydration', async () => {
+ const view = renderHook(() => useAuth(), { wrapper });
+ await waitFor(() => expect(view.result.current.dataOwnerId).toBe('account-a'));
+
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenCalledWith({
+ finalizeSessions: false,
+ });
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).not.toHaveBeenCalledWith({
+ finalizeSessions: true,
+ });
+
+ act(() => mocks.emitAuth(authState('account-b')));
+ expect(mocks.cancelRemoteOptimisticSendsForDataOwnerBoundary).toHaveBeenLastCalledWith({
+ finalizeSessions: true,
+ });
+ });
+
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 c8f13089b7..d48f65fa2a 100644
--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx
+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx
@@ -178,6 +178,7 @@ export function AuthProvider({
const activeDataOwnerIdRef = useRef<string | null>(null);
const activeDataOwnerGenerationRef = useRef(0);
const authStateVersionRef = useRef(0);
+ const hasAppliedAuthStateRef = useRef(false);
const pendingOwnerProjectionRef = useRef(false);
// Auth mutations invalidate owner-bound in-flight reads before crossing IPC. If Main rejects
@@ -239,6 +240,7 @@ export function AuthProvider({
&& !state.canEnterApp
&& activeDataOwnerIdRef.current !== null
&& state.ownerGeneration === activeDataOwnerGenerationRef.current;
+ const initialOwnerHydration = !hasAppliedAuthStateRef.current && !pendingSignedOutProjection;
const ownerChanged =
!pendingSignedOutProjection && activeDataOwnerIdRef.current !== state.dataOwnerId;
if (pendingSignedOutProjection) pendingOwnerProjectionRef.current = true;
@@ -250,7 +252,7 @@ export function AuthProvider({
publishDataOwnerGeneration(
state.dataOwnerId,
state.ownerGeneration,
- { finalizeSessions: ownerChanged },
+ { finalizeSessions: ownerChanged && !initialOwnerHydration },
);
if (ownerChanged) {
pendingOwnerProjectionRef.current = false;
@@ -260,6 +262,7 @@ export function AuthProvider({
if (!pendingSignedOutProjection) {
activeDataOwnerIdRef.current = state.dataOwnerId;
activeDataOwnerGenerationRef.current = state.ownerGeneration;
+ hasAppliedAuthStateRef.current = true;
}
setDataOwnerGenerationState(state.ownerGeneration);
setNewMakerDraftOwner(state.dataOwnerId);