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(mobile): isolate account switch runtime state

xd-bobo84596efe9e4a
diff --git a/apps/mobile/app/add-account.tsx b/apps/mobile/app/add-account.tsx
index aa09354590..8e47649b77 100644
--- a/apps/mobile/app/add-account.tsx
+++ b/apps/mobile/app/add-account.tsx
@@ -1,4 +1,4 @@
-import { useEffect } from 'react';
+import { useEffect, useRef } from 'react';
import { useRouter } from 'expo-router';
import { useAuth } from '@/auth/AuthContext';
@@ -7,15 +7,30 @@ import { LoginScreen } from './(auth)/login';
export default function AddAccountScreen() {
const auth = useAuth();
const router = useRouter();
+ const flowFinishedRef = useRef(false);
+ const closeStartedRef = useRef(false);
useEffect(() => {
- if (auth.loginState?.step === 'completed') router.replace('/devices');
+ if (auth.loginState?.step !== 'completed') return;
+ flowFinishedRef.current = true;
+ router.replace('/devices');
}, [auth.loginState?.step, router]);
+ useEffect(
+ () => () => {
+ // Android 系统返回键可以直接移除路由。取消动作必须从卸载路径兜底,
+ // 让验证码校验、账号选择等迟到结果在提交前失效。
+ if (flowFinishedRef.current || closeStartedRef.current) return;
+ void auth.cancelAddAccount();
+ },
+ [auth.cancelAddAccount],
+ );
+
return (
<LoginScreen
additionalAccount
onClose={() => {
+ closeStartedRef.current = true;
void auth.cancelAddAccount().finally(() => router.replace('/devices'));
}}
/>
diff --git a/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts b/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts
index 65e1800866..04d2da7cb9 100644
--- a/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts
+++ b/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts
@@ -1,7 +1,12 @@
-import { readFileSync } from 'node:fs';
+import { readFileSync as readFileSyncRaw } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
+const readFileSync = (
+ path: Parameters<typeof readFileSyncRaw>[0],
+ encoding: BufferEncoding,
+): string => readFileSyncRaw(path, encoding).toString().replace(/\r\n/g, '\n');
+
describe('mobile auth-server login', () => {
it('uses native social credentials where available and browser PKCE for Global Android Apple', () => {
const loginSource = readFileSync(
@@ -298,7 +303,7 @@ describe('mobile auth-server login', () => {
expect(authSource).not.toContain('serializeAccountTokenMutation');
expect(authSource).not.toContain('accountRefreshInFlightRef');
expect(authSource).toMatch(
- /if \(authGenerationRef\.current !== generation\)\s+throw authCodeError\('AUTH_FLOW_SUPERSEDED'\)/,
+ /authGenerationRef\.current !== generation \|\|\s+loginFlowEpochRef\.current !== expectedLoginFlowEpoch/,
);
const refreshStart = authSource.indexOf('const refresh = useCallback');
const refreshEnd = authSource.indexOf('\n useEffect(() => {', refreshStart);
@@ -375,6 +380,75 @@ describe('mobile auth-server login', () => {
expect(switchBody).toContain("throw authCodeError('REGION_MISMATCH')");
});
+ it('invalidates an add-account login when Android removes the route', () => {
+ const screenSource = readFileSync(
+ resolve(process.cwd(), 'app/add-account.tsx'),
+ 'utf8',
+ );
+ const authSource = readFileSync(
+ resolve(process.cwd(), 'src/auth/AuthContext.tsx'),
+ 'utf8',
+ );
+ expect(screenSource).toContain('() => () => {');
+ expect(screenSource).toContain('void auth.cancelAddAccount();');
+ expect(screenSource).toContain('flowFinishedRef.current');
+
+ const beginStart = authSource.indexOf('const beginAddAccount = useCallback');
+ const cancelStart = authSource.indexOf(
+ 'const cancelAddAccount = useCallback',
+ beginStart,
+ );
+ const cancelEnd = authSource.indexOf(
+ '\n\n const clearLocalSession',
+ cancelStart,
+ );
+ expect(authSource.slice(beginStart, cancelStart)).toContain(
+ 'loginFlowEpochRef.current += 1;',
+ );
+ expect(authSource.slice(cancelStart, cancelEnd)).toContain(
+ 'loginFlowEpochRef.current += 1;',
+ );
+ expect(authSource).toContain(
+ 'assertLoginFlowCurrent(expectedLoginFlowEpoch);',
+ );
+ expect(authSource).toContain(
+ 'loginFlowEpochRef.current !== expectedLoginFlowEpoch',
+ );
+ });
+
+ it('clears every Device Link account projection when accountGeneration changes', () => {
+ const source = readFileSync(
+ resolve(process.cwd(), 'src/device-link/DeviceLinkContext.tsx'),
+ 'utf8',
+ );
+ const clearStart = source.indexOf(
+ 'const clearPerAccountDeviceLinkState = useCallback',
+ );
+ const clearEnd = source.indexOf('\n }, []);', clearStart);
+ const clearBody = source.slice(clearStart, clearEnd);
+ expect(clearBody).toContain('remoteSessionStore.clear();');
+ expect(clearBody).toContain('remoteScheduleEventStore.clearAll();');
+ expect(clearBody).toContain('revokedDevicesStore.clearAll();');
+ expect(clearBody).toContain('clearAllDeviceProviders();');
+ expect(clearBody).toContain('setLastPresenceSnapshot(null);');
+
+ const effectStart = source.indexOf(
+ 'const accountGenerationChanged =',
+ clearEnd,
+ );
+ const authenticatedStart = source.indexOf(
+ 'const client = new DeviceLinkClient',
+ effectStart,
+ );
+ const boundaryBody = source.slice(effectStart, authenticatedStart);
+ expect(boundaryBody).toContain(
+ 'accountGenerationRef.current !== auth.accountGeneration',
+ );
+ expect(boundaryBody).toContain(
+ 'if (accountGenerationChanged) clearPerAccountDeviceLinkState();',
+ );
+ });
+
it('accepts enterprise ID, organization slug, and verified domains up to the API limit', () => {
const loginSource = readFileSync(
resolve(process.cwd(), 'app/(auth)/login.tsx'),
diff --git a/apps/mobile/src/auth/AuthContext.tsx b/apps/mobile/src/auth/AuthContext.tsx
index 0a6ff0d975..ca0c35358e 100644
--- a/apps/mobile/src/auth/AuthContext.tsx
+++ b/apps/mobile/src/auth/AuthContext.tsx
@@ -342,7 +342,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const accountDeletionReceiptRealmRef = useRef<AuthRegion | null>(null);
const pendingAccountDeletionRestoredRef = useRef(false);
const loginActionInFlightRef = useRef<Promise<boolean> | null>(null);
+ const loginActionInFlightEpochRef = useRef<number | null>(null);
const browserCompletionRef = useRef<Promise<void> | null>(null);
+ const browserCompletionEpochRef = useRef<number | null>(null);
+ // 独立于 authGeneration:关闭“添加账号”只应让该登录流失效,不能让当前
+ // 已登录账号的 refresh/runtime 一并过期。
+ const loginFlowEpochRef = useRef(0);
// auth-server rotates refresh tokens, so every caller must share one request.
const refreshInFlightRef = useRef<Promise<string | null> | null>(null);
const terminalLogoutInFlightRef = useRef<Promise<void> | null>(null);
@@ -379,6 +384,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
setDeferredSessionRecovery(false);
}, []);
+ const assertLoginFlowCurrent = useCallback((expectedEpoch: number) => {
+ if (loginFlowEpochRef.current === expectedEpoch) return;
+ throw authCodeError('AUTH_FLOW_SUPERSEDED');
+ }, []);
+
const prepareBetaChannelForCurrentDevice = useCallback((): Promise<string> => {
const existing = betaChannelPreparationRef.current;
if (existing) return existing;
@@ -757,12 +767,18 @@ export function AuthProvider({ children }: { children: ReactNode }) {
);
const acceptOutcome = useCallback(
- async (outcome: LoginOutcome, did: string): Promise<void> => {
+ async (
+ outcome: LoginOutcome,
+ did: string,
+ expectedLoginFlowEpoch = loginFlowEpochRef.current,
+ ): Promise<void> => {
await deleteSecureItem(PENDING_OAUTH_KEY).catch(() => undefined);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
if (outcome.status === 'ok' || outcome.status === 'select_account') {
// 成功登录后,当前会话已明确属于本次登录的 passport。无论是否恢复了
// 注销中的账号,都不能继续保留此前其他账号留下的查询 receipt。
await persistAccountDeletionReceipt(null);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
}
pendingAccountTokenRef.current =
@@ -836,6 +852,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
? pendingAccountMembershipsRef.current
: [outcome.membership],
});
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
}
await rememberMobileResourceSession(
outcome,
@@ -843,23 +860,36 @@ export function AuthProvider({ children }: { children: ReactNode }) {
passportId || undefined,
false,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const previousRealm = activeAuthRealmRef.current;
const replacesActiveSession =
userRef.current !== null &&
(previousRealm !== committedRealm ||
userRef.current?.id !== outcome.membership.id);
- const generation = ++authGenerationRef.current;
- refreshInFlightRef.current = null;
if (replacesActiveSession) {
await clearAccountScopedRuntimeForSwitch();
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
}
+ const generation = ++authGenerationRef.current;
+ refreshInFlightRef.current = null;
const persisted = await serializeRefreshTokenMutation(async () => {
- if (authGenerationRef.current !== generation) return false;
+ if (
+ authGenerationRef.current !== generation ||
+ loginFlowEpochRef.current !== expectedLoginFlowEpoch
+ ) {
+ return false;
+ }
await writePersistedAuthSession(outcome.refreshToken, committedRealm);
- return authGenerationRef.current === generation;
+ return (
+ authGenerationRef.current === generation &&
+ loginFlowEpochRef.current === expectedLoginFlowEpoch
+ );
});
if (!persisted) throw authCodeError('AUTH_FLOW_SUPERSEDED');
- if (authGenerationRef.current !== generation)
+ if (
+ authGenerationRef.current !== generation ||
+ loginFlowEpochRef.current !== expectedLoginFlowEpoch
+ )
throw authCodeError('AUTH_FLOW_SUPERSEDED');
if (passportId) {
const nextAccountKey = accountVaultKey(
@@ -867,10 +897,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
outcome.membership.id,
);
await mutateMobileAccountVault((vault) => {
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
if (vault.resources[nextAccountKey]) {
vault.activeAccountKey = nextAccountKey;
}
});
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
}
activateMobileSessionRealm(committedRealm);
activeAuthRealmRef.current = committedRealm;
@@ -907,6 +939,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
},
[
applyUser,
+ assertLoginFlowCurrent,
clearAccountScopedRuntimeForSwitch,
loadMe,
persistAccountDeletionReceipt,
@@ -1237,8 +1270,16 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}, [user?.id]);
const completeOAuthCallback = useCallback(
- (callbackUrl: string): Promise<void> => {
- if (browserCompletionRef.current) return browserCompletionRef.current;
+ (
+ callbackUrl: string,
+ expectedLoginFlowEpoch = loginFlowEpochRef.current,
+ ): Promise<void> => {
+ if (
+ browserCompletionRef.current &&
+ browserCompletionEpochRef.current === expectedLoginFlowEpoch
+ ) {
+ return browserCompletionRef.current;
+ }
suspendSessionRecoveryForLogin();
const run = (async () => {
setIsBusy(true);
@@ -1247,6 +1288,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
throw authCodeError('INVALID_AUTH_CODE');
}
const pending = await readPendingOAuth();
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const callback = parseOAuthCallbackUrl(callbackUrl);
if (callback.state !== pending.state)
throw authCodeError('STATE_MISMATCH');
@@ -1259,16 +1301,23 @@ export function AuthProvider({ children }: { children: ReactNode }) {
'Unable to load the enterprise auth region manifest',
);
}
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
pendingAuthRealmRef.current = pending.realm;
const outcome = await authClientFor(
pending.deviceId,
pending.realm,
).exchangeAuthorizationCode(callback.code, pending.codeVerifier);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
deviceIdRef.current = pending.deviceId;
setDeviceId(pending.deviceId);
- await acceptOutcome(outcome, pending.deviceId);
+ await acceptOutcome(
+ outcome,
+ pending.deviceId,
+ expectedLoginFlowEpoch,
+ );
setAuthError(null);
} catch (error) {
+ if (loginFlowEpochRef.current !== expectedLoginFlowEpoch) throw error;
const code = authErrorCode(error);
if (code === 'INVALID_AUTH_CODE') {
await deleteSecureItem(PENDING_OAUTH_KEY).catch(() => undefined);
@@ -1279,23 +1328,35 @@ export function AuthProvider({ children }: { children: ReactNode }) {
setAuthError(code);
throw error;
} finally {
- setIsBusy(false);
+ if (loginFlowEpochRef.current === expectedLoginFlowEpoch) {
+ setIsBusy(false);
+ }
}
})();
browserCompletionRef.current = run;
+ browserCompletionEpochRef.current = expectedLoginFlowEpoch;
run.then(
() => {
- if (browserCompletionRef.current === run)
+ if (browserCompletionRef.current === run) {
browserCompletionRef.current = null;
+ browserCompletionEpochRef.current = null;
+ }
},
() => {
- if (browserCompletionRef.current === run)
+ if (browserCompletionRef.current === run) {
browserCompletionRef.current = null;
+ browserCompletionEpochRef.current = null;
+ }
},
);
return run;
},
- [acceptOutcome, suspendSessionRecoveryForLogin, updateLoginState],
+ [
+ acceptOutcome,
+ assertLoginFlowCurrent,
+ suspendSessionRecoveryForLogin,
+ updateLoginState,
+ ],
);
// SSO returns through cindycn://auth or cindy://auth. The pending PKCE verifier
@@ -1316,12 +1377,20 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const dispatchLoginAction = useCallback(
(action: MobileLoginAction): Promise<boolean> => {
+ const expectedLoginFlowEpoch = loginFlowEpochRef.current;
suspendSessionRecoveryForLogin();
- if (loginActionInFlightRef.current) return loginActionInFlightRef.current;
+ if (
+ loginActionInFlightRef.current &&
+ loginActionInFlightEpochRef.current === expectedLoginFlowEpoch
+ ) {
+ return loginActionInFlightRef.current;
+ }
let run: Promise<boolean>;
const clearIfCurrent = () => {
- if (loginActionInFlightRef.current === run)
+ if (loginActionInFlightRef.current === run) {
loginActionInFlightRef.current = null;
+ loginActionInFlightEpochRef.current = null;
+ }
};
run = (async () => {
setIsBusy(true);
@@ -1329,6 +1398,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
try {
const did =
deviceIdRef.current ?? (await prepareBetaChannelForCurrentDevice());
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
deviceIdRef.current = did;
setDeviceId(did);
const startsBuildRealmFlow =
@@ -1350,6 +1420,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
label: string;
}): Promise<boolean> => {
const { codeVerifier, codeChallenge } = await createPkcePair();
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const state = createState();
await setSecureItem(
PENDING_OAUTH_KEY,
@@ -1362,6 +1433,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
realm: loginRealm,
} satisfies PendingOAuth),
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(input.previousState, {
type: 'browser-started',
@@ -1379,11 +1451,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
authUrl,
MOBILE_REDIRECT_URL,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
if (result.type === 'success') {
- await completeOAuthCallback(result.url);
+ await completeOAuthCallback(result.url, expectedLoginFlowEpoch);
return true;
}
await deleteSecureItem(PENDING_OAUTH_KEY).catch(() => undefined);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
pendingAuthRealmRef.current = null;
updateLoginState(null);
throw authCodeError('USER_CANCELLED');
@@ -1401,10 +1475,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
pendingAccountDeletionRestoredRef.current = false;
setAccountDeletionRestored(false);
await deleteSecureItem(PENDING_OAUTH_KEY).catch(() => undefined);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const providers = await authClientFor(
did,
BUILD_AUTH_REGION,
).getProviders();
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(loginStateRef.current, {
type: 'providers-loaded',
@@ -1460,6 +1536,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
did,
BUILD_AUTH_REGION,
).discover(email);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const currentState = loginStateRef.current;
const sole = soleLoginMethod(methods);
if (sole?.type === 'sso' && currentState) {
@@ -1474,6 +1551,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
// 人机验证前置闸(覆盖 discovery→发码的自动串发路径):取消则不
// 串发,落 method-choice,用户可从个人行再次发起(会重新过闸)。
const gate = await ensureCaptchaGate('email');
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
if (!gate.proceed) {
updateLoginState(
reduceAuthFlow(currentState, {
@@ -1490,6 +1568,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
email,
gate.captchaToken,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(currentState, {
type: 'code-requested',
@@ -1526,6 +1605,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
loadMobileEndpointsForRealm('cn'),
loadMobileEndpointsForRealm('global'),
]);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
} catch {
throw new AuthApiError(
'ORG_REALM_UNAVAILABLE',
@@ -1537,6 +1617,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
cn: authClientFor(did, 'cn'),
global: authClientFor(did, 'global'),
});
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
pendingAuthRealmRef.current = selected.region;
discovery = selected.discovery;
} else {
@@ -1545,12 +1626,14 @@ export function AuthProvider({ children }: { children: ReactNode }) {
did,
BUILD_AUTH_REGION,
).discoverSsoOrg(org);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
}
const methods = ssoOrgDiscoveryToMethods(discovery);
// A sole same-region connection auto-starts browser authorization
// below. Persist the successful discovery first so cancel/timeout
// does not lose a valid organization identifier.
await rememberSsoOrgIdentifier(action.org);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
const currentState = loginStateRef.current;
if (discovery.region !== BUILD_AUTH_REGION) {
if (currentState?.step !== 'identifier') {
@@ -1587,6 +1670,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
if (action.type === 'request-code') {
const identifier = action.identifier.trim();
const gate = await ensureCaptchaGate(action.kind);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
// 取消:不发码、不改状态、不报错(用户可再点发送/重发)。
if (!gate.proceed) return false;
await requestCodeWithCaptchaFallback(
@@ -1595,6 +1679,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
identifier,
gate.captchaToken,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(loginStateRef.current, {
type: 'code-requested',
@@ -1612,6 +1697,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
action.code,
),
did,
+ expectedLoginFlowEpoch,
);
return true;
}
@@ -1619,12 +1705,14 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const credential = await acquireNativeSocialCredential(
action.provider,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
await acceptOutcome(
await authClientFor(did, BUILD_AUTH_REGION).exchangeNativeSocial(
action.provider,
credential,
),
did,
+ expectedLoginFlowEpoch,
);
return true;
}
@@ -1669,8 +1757,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
accountToken,
action.accountId,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
pendingAccountTokenRef.current = null;
- await acceptOutcome({ status: 'ok', ...pair }, did);
+ await acceptOutcome(
+ { status: 'ok', ...pair },
+ did,
+ expectedLoginFlowEpoch,
+ );
return true;
}
const ticket = pendingLoginTicketRef.current;
@@ -1678,6 +1771,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
await acceptOutcome(
await client.selectAccount(ticket, action.accountId),
did,
+ expectedLoginFlowEpoch,
);
return true;
}
@@ -1689,6 +1783,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
throw authCodeError('INVALID_SSO_VERIFICATION_TICKET');
}
await client.requestSsoVerificationCode(ticket);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(state, {
type: 'sso-verification-code-requested',
@@ -1707,6 +1802,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
await acceptOutcome(
await client.verifySsoVerification(ticket, action.code),
did,
+ expectedLoginFlowEpoch,
);
return true;
}
@@ -1722,6 +1818,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
state.bindType,
contact,
);
+ assertLoginFlowCurrent(expectedLoginFlowEpoch);
updateLoginState(
reduceAuthFlow(state, {
type: 'binding-code-requested',
@@ -1739,9 +1836,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
action.code,
),
did,
+ expectedLoginFlowEpoch,
);
return true;
} catch (error) {
+ if (loginFlowEpochRef.current !== expectedLoginFlowEpoch) return false;
const code = authErrorCode(error);
if (
code === 'INVALID_LOGIN_TICKET' ||
@@ -1766,15 +1865,19 @@ export function AuthProvider({ children }: { children: ReactNode }) {
setAuthError(code);
return false;
} finally {
- setIsBusy(false);
+ if (loginFlowEpochRef.current === expectedLoginFlowEpoch) {
+ setIsBusy(false);
+ }
}
})();
loginActionInFlightRef.current = run;
+ loginActionInFlightEpochRef.current = expectedLoginFlowEpoch;
run.then(clearIfCurrent, clearIfCurrent);
return run;
},
[
acceptOutcome,
+ assertLoginFlowCurrent,
completeOAuthCallback,
ensureCaptchaGate,
requestCodeWithCaptchaFallback,
@@ -2046,6 +2149,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
);
const beginAddAccount = useCallback(async (): Promise<void> => {
+ loginFlowEpochRef.current += 1;
additionalLoginRef.current = true;
sessionRecoverySuspendedRef.current = false;
updateLoginState(null);
@@ -2054,6 +2158,8 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}, [dispatchLoginAction, updateLoginState]);
const cancelAddAccount = useCallback(async (): Promise<void> => {
+ loginFlowEpochRef.current += 1;
+ setIsBusy(false);
pendingAccountTokenRef.current = null;
pendingAccountRefreshTokenRef.current = null;
pendingAccountMembershipsRef.current = [];
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 188e9fa7e3..d558c58dc2 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -322,6 +322,20 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
// 才会把同一代暴露给页面。这样全局补齐与页面首开使用完全相同的 single-flight key。
const connectionEpochRef = useRef(0);
const [lastPresenceSnapshot, setLastPresenceSnapshot] = useState<PresenceSnapshot | null>(null);
+ const accountGenerationRef = useRef<number | null>(null);
+
+ const clearPerAccountDeviceLinkState = useCallback(() => {
+ remoteSessionStore.clear();
+ remoteScheduleEventStore.clearAll();
+ revokedDevicesStore.clearAll();
+ resetDeviceResponsivenessTracking();
+ clearAllDeviceProviders();
+ clearAllDeviceModelMeta();
+ resetAgentCapabilitiesCache();
+ resetComposerPaletteCache();
+ setLastPresenceSnapshot(null);
+ setPresenceVersion((version) => version + 1);
+ }, []);
/**
* availability 放在 ref 里供 transport 同步读取;每次真实的三态变化也必须发布给
@@ -648,6 +662,9 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
}, [rehydrateWithClient]);
useEffect(() => {
+ const accountGenerationChanged =
+ accountGenerationRef.current !== auth.accountGeneration;
+ accountGenerationRef.current = auth.accountGeneration;
if (!auth.isAuthenticated) {
clientRef.current?.stop();
clientRef.current = null;
@@ -666,23 +683,19 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
backgroundReleaseInFlightRef.current = false;
setStatus('stopped');
setConnectionIssue(null);
- remoteSessionStore.clear();
- remoteScheduleEventStore.clearAll();
- revokedDevicesStore.clearAll();
- resetDeviceResponsivenessTracking();
// 登出 / 进程内切号:清掉所有 per-account 残留,避免下一个账号串到上一个账号的数据。
// - 供应商目录是 module 级单例缓存(useDeviceProviders 按 deviceId 命中),不随组件卸载清;
// - lastPresenceSnapshot 是本 context 的 state,home 屏据它 patch 设备列表。
// 二者若不重置,切号后会短暂看到 / 用到上一个账号的桌面端与供应商数据。
- clearAllDeviceProviders();
- clearAllDeviceModelMeta();
- resetAgentCapabilitiesCache();
- resetComposerPaletteCache();
- setLastPresenceSnapshot(null);
- setPresenceVersion((n) => n + 1);
+ clearPerAccountDeviceLinkState();
return;
}
+ // 账号切换期间 isAuthenticated 始终为 true,不能依赖上面的登出分支。
+ // effect cleanup 只负责 transport;新账号建连前必须同步清掉旧账号的任务、
+ // 调度、设备与 presence 投影,避免无设备的新账号永远保留旧快照。
+ if (accountGenerationChanged) clearPerAccountDeviceLinkState();
+
const client = new DeviceLinkClient({
getWsUrl: () => deviceLinkWsUrl(),
getToken: auth.getAccessToken,
@@ -1075,6 +1088,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
auth.accountGeneration,
auth.getAccessToken,
auth.isAuthenticated,
+ clearPerAccountDeviceLinkState,
clearRehydrateRetry,
publishPresenceAvailabilityMutation,
rehydrateWithClient,