fix(login): 会话切换窗口内不接新的过门动作,补上第二条上报窗口

codex 审查 P1 第二条(#907):上一轮把同意记录挪到 enter-local 之后,只堵住了顺序,
没堵住并发。`auth:enter-local` 的 handler 要 await waitForSessionInvalidation() 与
teardownAuthAccountBoundary(),这是一段真实可观测的窗口;窗口里 isLoading 仍为 false
(enter-local 不走 loginFlow),而协议弹窗已关、consentAccepted 已为 true,于是邮箱 /
社交等入口仍可点——它们走 deferConsentPersist=false 分支,会在 main 还没转成 local 时
立刻 persist,再次把 allowed:true 广播给 TapDB。

修法:
- `requireConsent` 顶部单点收口 `if (localModePendingRef.current) return;`——会话都在切了,
  任何登录动作本来就该作废,persist 与派发一起挡掉
- 绕过 requireConsent 的三处自挡:企业 SSO 圆钮(豁免协议门)、组织标识提交(会真的
  dispatch)、error 步 reset(重试/返回会把状态机拨回去)
- guard 读 ref 不读 state:点击可能落在 setState 与 re-render 之间,那时闭包里的
  state 还是旧的 false。`localModePending` state 只留给按钮 disabled 视觉
- 取舍:用行为层 guard 而不是给每个控件补 disabled。窗口通常只有几十毫秒,全量
  disabled 会换来一次可见闪变(规则 7:无视觉跳变),而行为 guard 已杜绝上报窗口

测试:新增并发窗口回归——让 authEnterLocal 悬挂在窗口里,断言此时点社交圆钮不派发、
不落同意,点 SSO 圆钮也不把视图拨到 sso-org;放开后只有 skip 自己那一次同意记录,
且仍落在 enter-local 之后。反向验证过:摘掉 guard 后这条转红,恢复即绿。
DESIGN.md 与 design-decision-log 的合规约束段补上并发窗口与 ref/state 的理由。

验证:pnpm test:unit 全量 0 FAIL / 0 not-ok(apps/desktop + apps/mobile 均 PASS);
pnpm --filter desktop typecheck 绿;check:i18n-glossary 绿;prettier 干净;
login 定向 15 文件 152 例通过(consent 25 例)。

Signed-off-by: Dash <dashhuang@gmail.com>
This commit is contained in:
Dash
2026-07-29 16:43:11 +08:00
parent 4a11a03807
commit 04b5cd7cea
4 changed files with 75 additions and 6 deletions

View File

@@ -119,6 +119,14 @@ export function LoginPage() {
}, []);
const isMac = window.electronAPI?.platform === 'darwin';
const [localModePending, setLocalModePending] = useState(false);
/* 会话切换进行中的**真值来源**。`localModePending` state 只用于按钮 disabled 视觉;
guard 一律读这个 ref —— 点击可能落在 setState 与 re-render 之间,那时事件处理器
闭包里的 state 还是旧的 false,ref 不受渲染时机影响。 */
const localModePendingRef = useRef(false);
const markLocalModeTransition = (pending: boolean) => {
localModePendingRef.current = pending;
setLocalModePending(pending);
};
/**
* 进入未登录状态(「跳过登录」入口 + error 步逃生入口共用)。
@@ -130,8 +138,8 @@ export function LoginPage() {
* 在会话切过去之后落,不能由 requireConsent 提前落(竞态原因见那里的注释)。
*/
const openLocalMode = async () => {
if (isLoading || localModePending || !window.electronAPI?.authEnterLocal) return;
setLocalModePending(true);
if (isLoading || localModePendingRef.current || !window.electronAPI?.authEnterLocal) return;
markLocalModeTransition(true);
try {
await window.electronAPI.authEnterLocal();
// 顺序是硬要求:先 enter-local(main 侧 isLocalMode() 转真)再落同意,这样
@@ -148,7 +156,7 @@ export function LoginPage() {
// (登录页的 errorCode 归 main 的 loginFlowState 所有,renderer 不旁路写)。
log.error('enter not-signed-in session failed', error);
} finally {
setLocalModePending(false);
markLocalModeTransition(false);
}
};
@@ -203,6 +211,16 @@ export function LoginPage() {
* 先切会话、再落同意即可关掉这个窗口(此时 allowed 恒为 false)。
*/
const requireConsent = (action: () => void, options?: { deferConsentPersist?: boolean }) => {
/* 会话切换进行中一律不接新的过门动作(codex 审查 P1 第二条,#907)。
`auth:enter-local` 的 handler 要 await waitForSessionInvalidation() 与
teardownAuthAccountBoundary(),这是一个真实可观测的窗口;窗口里 isLoading
仍为 false(enter-local 不走 loginFlow),而弹窗已关、consentAccepted 已为 true,
于是邮箱 / 社交等入口仍可点 —— 它们走的是 deferConsentPersist=false 分支,会在
main 还没转成 local 时立刻 persist,再次把 allowed:true 广播给 TapDB。
在这里单点收口而不是给每个控件补 disabled:窗口通常只有几十毫秒,给全部控件
加 disabled 视觉会换来一次可见闪变(§规则 7:无视觉跳变),而行为层 guard 已经
杜绝了 persist 与派发 —— 会话都在切了,任何登录动作本来就该作废。 */
if (localModePendingRef.current) return;
const deferConsentPersist = options?.deferConsentPersist === true;
if (consentAccepted) {
if (!deferConsentPersist) persistPrivacyConsent();
@@ -387,6 +405,9 @@ export function LoginPage() {
}, [errorCode, t]);
const reset = () => {
// error 步的「重试」与 back 都走这里,而 error 步同时挂着跳过登录逃生入口:
// 会话已经在切了就别再把状态机拨回去(同 requireConsent 里那道 guard 的理由)。
if (localModePendingRef.current) return;
clearError();
setIdentifierFormatError(null);
void dispatch({ type: 'reset' });
@@ -425,6 +446,9 @@ export function LoginPage() {
const submitSsoOrg = (event: FormEvent) => {
event.preventDefault();
// 企业 SSO 豁免协议门,所以拿不到 requireConsent 里那道会话切换 guard;它会真的
// 派发,切换期间必须自己挡住(否则 main 会同时处理 enter-local 与 SSO 登录)。
if (localModePendingRef.current) return;
const value = ssoOrg.trim();
if (!value) return;
// 组织区域先静默发现;仅当结果与安装包区域不一致时,main 状态机进入
@@ -536,8 +560,9 @@ export function LoginPage() {
isLoading={isLoading}
onClick={() => {
// SC-SOC-7: in-flight 期间 no-op(行为层 guard,无 disabled 视觉回填)。
// 企业 SSO 豁免协议门(产品拍板),不过 requireConsent。
if (isLoading) return;
// 企业 SSO 豁免协议门(产品拍板),不过 requireConsent —— 所以会话切换
// 期间要自己挡:那道 guard 在 requireConsent 里,这条路径绕过了它。
if (isLoading || localModePendingRef.current) return;
clearError();
setSsoOrgMode(true);
}}

View File

@@ -308,6 +308,44 @@ describe('consent guard(未勾选拦截个人登录链路)', () => {
expectConsentPersistedAfterEnterLocal();
});
/* 并发窗口回归(codex 审查 P1 第二条,#907):auth:enter-local 的 handler 要 await
waitForSessionInvalidation() + teardownAuthAccountBoundary(),这段窗口里 isLoading
仍是 false、弹窗已关、consentAccepted 已为 true,于是邮箱 / 社交等入口仍可点。
它们走 deferConsentPersist=false 分支,会在 main 还没转成 local 时立刻 persist,
把 allowed:true 广播给 TapDB —— 必须一个都不放过。 */
it('会话切换窗口内点社交圆钮 → 不派发、不落同意;切换完成后只落 skip 自己那一次', async () => {
let releaseEnterLocal: () => void = () => undefined;
authEnterLocal.mockImplementationOnce(
() =>
new Promise((resolve) => {
releaseEnterLocal = () => resolve({ mode: 'local' });
}),
);
mount(await identifierState('providers:cn-social'));
fireEvent.click(screen.getByTestId('login-consent-radio'));
// 不 await:enter-local 故意悬挂在窗口里
fireEvent.click(screen.getByTestId('login-skip-entry'));
expect(authEnterLocal).toHaveBeenCalledTimes(1);
expect(acceptPrivacyConsent).not.toHaveBeenCalled();
// 窗口内点其它过门入口:一律作废
fireEvent.click(screen.getByTestId('login-social-apple'));
expect(loginHook.value.dispatch).not.toHaveBeenCalled();
expect(acceptPrivacyConsent).not.toHaveBeenCalled();
// 豁免协议门的企业 SSO 圆钮同样不该在切换中把视图拨走
fireEvent.click(screen.getByTestId('login-social-sso'));
expect(screen.queryByTestId('login-panel-sso-org')).toBeNull();
await act(async () => {
releaseEnterLocal();
});
// 只有 skip 自己那一次同意记录,且它落在 enter-local 之后
expect(acceptPrivacyConsent).toHaveBeenCalledTimes(1);
expect(authEnterLocal.mock.invocationCallOrder[0]).toBeLessThan(
acceptPrivacyConsent.mock.invocationCallOrder[0],
);
});
it('其它个人链路不受影响:社交圆钮仍在派发前先落同意记录', async () => {
mount(await identifierState('providers:cn-social'));
await act(async () => {

View File

@@ -978,6 +978,8 @@ The execution rulebook for subsequent desktop / mobile UI updates. Sources: the
「跳过登录」过门的口径(2026-07-29 拍板,**推翻 07-27 的免门结论**):不登录账号也是在使用 Cindy 客户端,因此与个人账号登录同口径——未勾选时先弹协议弹窗,同意后才进未登录状态,并写入统计采集同意(是否真的上报另由 main 侧闸决定:`analyticsSettingsService` 对本地会话一律 `!isLocalMode()` 不放行)。协议 UI(radio 四态、弹窗小按钮)与企业 SSO 豁免均不变。
> **实现约束(合规,勿改顺序)**:这条链路上「同意记录」必须落在 `auth:enter-local` **之后**,与其它过门点(放行时刻即落)不同。原因是 `acceptPrivacyConsent` 的 IPC handler 会同步广播 `allowed = isAnalyticsAllowed() && !isLocalMode()`——提前落同意时 `isLocalMode()` 尚为 false,广播出的 `allowed:true` 会让 renderer 的 TapDB 当场 `initSdk()` 并发出 `device_login`,等 `enter-local` 完成再广播 `allowed:false` 已经晚了,「未登录态不上报」在正式包上就被破了一个窗口。落码为 `requireConsent(action, { deferConsentPersist: true })` + `openLocalMode` 自己在会话切换后落同意;顺序由 `LoginPage.consent.test.tsx` 的 `invocationCallOrder` 断言锁住(面板内入口与 `error` 步逃生口、勾选与弹窗两条路径都覆盖)。
>
> 同一条合规约束还要求**切换窗口内不接任何新动作**:`auth:enter-local` 的 handler 要 await `waitForSessionInvalidation()` 与 `teardownAuthAccountBoundary()`,这段窗口里 `isLoading` 仍为 false、协议弹窗已关且 radio 已勾选,邮箱 / 社交等入口仍可点——它们走「放行即落同意」分支,会在 main 还没转成 local 时再次广播 `allowed:true`。收口点是 `requireConsent` 顶部的 `localModePendingRef` guard(豁免协议门的企业 SSO 入口与 `error` 步 `reset` 另行自挡,因为它们绕过 `requireConsent`)。用**行为层 guard 而不是给每个控件加 disabled**:窗口通常只有几十毫秒,全量 disabled 会换来一次可见闪变(规则 7:无视觉跳变),而行为 guard 已杜绝 persist 与派发。guard 读 ref 而非 state——点击可能落在 `setState` 与 re-render 之间,那时闭包里的 state 还是旧值。
**协议同意行整行热区(2026-07-29 拍板)**:命中区由 radio 的 24px 圈体扩到**整行 680×40**——点声明文字或行内空白都等于点 radio。例外两类:行内「服务条款」/「隐私协议」链接各自 `stopPropagation`(只开链接、不切勾选态),radio 自身也 `stopPropagation`(否则冒泡到行容器二次 toggle,净效果为点不动)。行容器不加 `role` / `tabIndex`:radio 仍是唯一无障碍交互点(`role="checkbox"` + `aria-labelledby` 指向声明文字),整行点击只是鼠标增强;整行 `select-none`,避免连点选中文字盖住勾选反馈。

View File

@@ -23,7 +23,11 @@
`auth:enter-local` **之后**——`acceptPrivacyConsent` 会同步广播 `allowed`,提前落时
`isLocalMode()` 还是 false,`allowed:true` 会让 TapDB 当场 init 并发 `device_login`,
把「未登录态不上报」破一个窗口。落码 = `deferConsentPersist` 选项 + `openLocalMode`
自己落同意,顺序由 consent 测试的 `invocationCallOrder` 断言锁住。
自己落同意,顺序由 consent 测试的 `invocationCallOrder` 断言锁住。同批还要求切换窗口内
不接任何新动作(`requireConsent` 顶部 `localModePendingRef` guard;企业 SSO 入口与
`error` 步 `reset` 因绕过 requireConsent 另行自挡),否则窗口里点邮箱 / 社交会走
「放行即落同意」分支再次广播 `allowed:true`。取舍:用行为层 guard 而非给全部控件加
disabled(窗口只有几十毫秒,全量 disabled 会换来可见闪变)。
(2) **协议同意行整行可点**:热区由 radio 的 24px 圈体扩到整行 680×40(原来要瞄准一个小圆点);
两个内联链接与 radio 自身 `stopPropagation`(前者只开链接,后者防冒泡二次 toggle),
行容器不加 role / tabIndex(radio 仍是唯一无障碍交互点),整行 `select-none`。