Repository navigation
refactor(desktop): break main process window and browser runtime cycles - #5568
Conversation
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
…ontracts from api (#5561) * refactor(app-components): import tool card helpers from their defining modules * refactor(app-components): remove remaining self-package barrel imports * refactor(app-ui): lower scrollbar, IME latch and PinScrollKey primitives * fix(app-components): import shared anchored menu css directly and scope IME lint suppressions * refactor(app-ui): move unified icon registry down from app-client * refactor(app-core): split composables entrypoint and decouple toast contracts from api * refactor(app-ui): lower scrollbar, IME latch and PinScrollKey primitives (#5550) * refactor(app-ui): lower scrollbar, IME latch and PinScrollKey primitives * fix(app-components): import shared anchored menu css directly and scope IME lint suppressions * refactor(desktop): break main process window and browser runtime cycles (#5568) * refactor(desktop): extract main window registry and terminal menu focus state * refactor(desktop): extract browser overlay state to break main browser cycle * build(nix): update pnpmDeps hash after removing app-client dependency
Requirement or Bug
主进程存在两处运行时循环依赖环:一是
window/main-window.ts(约 800 行)同时持有窗口单例、退出状态、sendToRenderer、关闭拦截器与窗口创建编排,与shell/menu.ts、server/connect.ts、white-screen-watchdog.ts及诊断/更新模块形成 12 文件环;二是browser-view.ts、browser-overlay.ts、browser-annotations.ts之间形成 3 文件环。本 PR 是 Desktop 分阶段重构的 PR 1.5(阶段 1 收尾),抽离无环的窗口注册表、终端菜单焦点状态与浏览器浮层状态,将主进程运行时环全部清零,业务行为保持不变。Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路
按「状态持有者与高层编排分离」拆出两个零回指的叶子模块:
window/window-registry.ts:只依赖electron、app/e2e-mode和 IPC 类型,持有mainWindow单例、退出标记、sendToRenderer、窗口消失监听与关闭拦截器。shell/terminal-menu-focus.ts:零依赖,持有终端菜单焦点布尔值与变更订阅;shell/menu.ts通过订阅触发buildMenu(),使main-window.ts不再需要反向导入shell/menu.ts。browser/browser-overlay-state.ts:仅依赖electron类型,持有当前浮层View引用、浮层外点击监听与标注浮层刷新回调;browser-view.ts改从该模块读取/触发,不再直接 importbrowser-overlay.ts,使浏览器模块变为browser-overlay → browser-annotations → browser-view的单向 DAG。flowchart TD subgraph Before[改动前:12 文件运行时环] MW0[main-window.ts] --> MENU0[shell/menu.ts] MW0 --> CONN0[server/connect.ts] --> ES0[embedded-server.ts] --> TEL0[telemetry.ts] --> SM0[system-metrics.ts] --> MW0 MW0 --> WSD0[white-screen-watchdog.ts] --> COL0[collector.ts] --> MW0 MENU0 --> TRACE0[trace.ts] --> MW0 MENU0 --> EXP0[diagnostics-export.ts] --> MW0 MENU0 --> UPD0[updater.ts] --> MW0 end subgraph After[改动后:单向 DAG] REG[window-registry.ts 叶子] FOCUS[terminal-menu-focus.ts 叶子] MW[main-window.ts] --> REG MW --> FOCUS MENU[shell/menu.ts] --> MW MENU --> REG MENU --> FOCUS COL[collector.ts] --> REG SM[system-metrics.ts] --> REG UPD[updater.ts] --> REG TRACE[trace.ts] --> REG EXP[diagnostics-export.ts] --> REG end旧路径功能清单与新归属对照
MainWindow类型、mainWindow单例、getMainWindow、windowForWebContents、focusedWindowWebContentswindow/main-window.tswindow/window-registry.ts(新增setMainWindow供createWindow/closed写入)isQuitting/quitConfirmSkipped状态、installQuitWatch、markQuitting、isMarkedQuitting、isQuitConfirmSkipped、cancelQuittingwindow/main-window.tswindow/window-registry.ts(before-quit幂等监听与 E2E 跳过逻辑不变)sendToRendererwindow/main-window.tswindow/window-registry.ts(销毁/崩溃/异常静默忽略逻辑不变)onMainWindowGone/notifyMainWindowGone、addMainWindowCloseInterceptor/shouldInterceptMainWindowClosewindow/main-window.tswindow/window-registry.ts(关闭拦截器按注册顺序短路逻辑不变)terminalFocused、getTerminalMenuFocus、setTerminalMenuFocus、onTerminalMenuFocusshell/menu.tsshell/terminal-menu-focus.ts;menu.ts模块加载时通过onTerminalMenuFocus(() => buildMenu())订阅,保持「先重建菜单、再通知快捷键监听(ipc/register.ts后注册)」的原有同步顺序../window/main-window导入../window/window-registry导入;createWindow/showMainWindow/ 标题栏等仍从main-window导入;server/connect.ts的MainWindow类型改从window-registry导入isBrowserOverlayView)、外部点击观察(observeBrowserOverlayOutsidePointer)、标注浮层刷新触发browser-view.ts直接从./browser-overlay导入browser/browser-overlay-state.ts;browser-overlay.ts在ensureEntry/destroyEntry同步浮层视图引用,并在模块加载时注册外部点击关闭与标注刷新回调(main-window.ts启动即加载browser-overlay.ts,注册时机先于任何浏览器事件)MainWindow的类型引用import type { MainWindow } from '../window/main-window'main-window.ts通过export * from './window-registry'保持原导出面测试与基线
apps/desktop/tests/main/window/window-registry.test.ts(5 例)、apps/desktop/tests/main/shell/terminal-menu-focus.test.ts(1 例)与apps/desktop/tests/main/browser/browser-overlay-state.test.ts(3 例),直接验证窗口存取、发送边界、焦点窗口解析、退出/跳过标记(含cancelQuitting不清除quitConfirmSkipped的既有语义)、关闭拦截器短路、焦点订阅顺序及浮层状态/外部点击门控/刷新分发。vi.mock目标;ipc/register.test.ts中main-window与window-registry共用mocks.getMainWindow与mocks.sendToRenderer;browser-overlay.ts继续 re-exportobserveBrowserOverlayOutsidePointer与isBrowserOverlayView,既有browser-overlay.test.ts无需改动。app-boundaries的runtime-cycle:21 → 6(主进程 12 文件环与浏览器 3 文件环全部清零,全仓仅剩app-client/stores6 文件环);oxlintimport/no-cycle同步减少 24 条,无任何新增基线。阅读指南
先看新增的
apps/desktop/src/main/window/window-registry.ts、apps/desktop/src/main/shell/terminal-menu-focus.ts及对应单测;main-window.ts主要是删除迁出代码并改用getMainWindow()/setMainWindow()/isMarkedQuitting();其余文件均为导入拆分。Behavior Changes and Affected Users
None。
证据:所有状态字段、守卫条件(
isDestroyed/isCrashed)、before-quit监听、关闭拦截顺序、终端焦点变化时的同步菜单重建与快捷键通知顺序均保持不变;未修改 IPC 通道、参数校验或浏览器逻辑。shouldHideOnClose)、Windowssession-end退出监听与自定义标题栏菜单隐藏;逻辑未变,macOS 本地类型检查与单测通过,Windows 由 CIwindows任务验证。验证记录
pnpm lint:通过(0 退出)。pnpm --filter kimi-code-app run typecheck:通过(主进程tsc --noEmit与渲染端vue-tsc --noEmit)。pnpm exec vitest run --reporter=agent <窗口/菜单/诊断/更新相关 16 个测试文件>:16 个文件、464 个用例全部通过。pnpm exec vitest run --reporter=agent apps/desktop/tests/main/browser/{browser-overlay-state,browser-overlay,browser-view,browser-annotations}.test.ts:4 个文件、371 个用例全部通过。pnpm exec vitest run --reporter=agent apps/desktop/tests/main/ipc/register.test.ts -t '^(?!.*preserves subagent controllers).*$':79/79 通过。唯一排除的preserves subagent controllers from real preferences...在本机干净基点88553c7d2上同样卡 30s 超时(本机 KAP/MCP 初始化环境既有问题,超时后残留 mock 会级联污染同文件后续用例);CI Linux 上该用例在此前 PR 均正常通过。pnpm run check:duplication 89acd6f67affcbef409af4719c30743860cb5ae5(PR 实际 base):通过,无新增重复片段。Checklist
/approve).(内部重构 PR 1.5,无关联 issue,依赖 refactor(app-core): split composables entrypoint and decouple toast contracts from api #5561)window-registry.test.ts5 例与terminal-menu-focus.test.ts1 例,并复跑全部受影响的 15 个既有测试文件)gen-changesetsskill, or this PR needs no changeset.(私有桌面端内部重构,无用户可感知变化,无需 changeset)gen-docsskill, or this PR needs no doc update.(不改变用户可见功能,无需更新用户文档)