Skip to content

refactor(desktop): break main process window and browser runtime cycles - #5568

Merged
wbxl2000 merged 2 commits into
refactor/app-core-entrypointsfrom
refactor/main-window-registry
Nov 27, 2026
Merged

wbxl2000 merged 2 commits into
refactor/app-core-entrypointsfrom
refactor/main-window-registry

Conversation

@liruifengv

@liruifengv liruifengv commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #5561(refactor/app-core-entrypoints)。前置 PR 合入后会将 base 切回 main。

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

核心思路

按「状态持有者与高层编排分离」拆出两个零回指的叶子模块:

  1. window/window-registry.ts:只依赖 electron、app/e2e-mode 和 IPC 类型,持有 mainWindow 单例、退出标记、sendToRenderer、窗口消失监听与关闭拦截器。
  2. shell/terminal-menu-focus.ts:零依赖,持有终端菜单焦点布尔值与变更订阅;shell/menu.ts 通过订阅触发 buildMenu(),使 main-window.ts 不再需要反向导入 shell/menu.ts。
  3. browser/browser-overlay-state.ts:仅依赖 electron 类型,持有当前浮层 View 引用、浮层外点击监听与标注浮层刷新回调;browser-view.ts 改从该模块读取/触发,不再直接 import browser-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
Loading

旧路径功能清单与新归属对照

迁移符号 / 行为 原位置 新位置 / 处理
MainWindow 类型、mainWindow 单例、getMainWindow、windowForWebContents、focusedWindowWebContents window/main-window.ts window/window-registry.ts(新增 setMainWindow 供 createWindow / closed 写入)
isQuitting / quitConfirmSkipped 状态、installQuitWatch、markQuitting、isMarkedQuitting、isQuitConfirmSkipped、cancelQuitting window/main-window.ts window/window-registry.ts(before-quit 幂等监听与 E2E 跳过逻辑不变)
sendToRenderer window/main-window.ts window/window-registry.ts(销毁/崩溃/异常静默忽略逻辑不变)
onMainWindowGone / notifyMainWindowGone、addMainWindowCloseInterceptor / shouldInterceptMainWindowClose window/main-window.ts window/window-registry.ts(关闭拦截器按注册顺序短路逻辑不变)
terminalFocused、getTerminalMenuFocus、setTerminalMenuFocus、onTerminalMenuFocus shell/menu.ts shell/terminal-menu-focus.ts;menu.ts 模块加载时通过 onTerminalMenuFocus(() => buildMenu()) 订阅,保持「先重建菜单、再通知快捷键监听(ipc/register.ts 后注册)」的原有同步顺序
非浏览器消费方(13 个模块) 统一从 ../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 的既有语义)、关闭拦截器短路、焦点订阅顺序及浮层状态/外部点击门控/刷新分发。
  • 同步拆分 16 个既有测试文件的 vi.mock 目标;ipc/register.test.ts 中 main-window 与 window-registry 共用 mocks.getMainWindow 与 mocks.sendToRenderer;browser-overlay.ts 继续 re-export observeBrowserOverlayOutsidePointer 与 isBrowserOverlayView,既有 browser-overlay.test.ts 无需改动。
  • app-boundaries 的 runtime-cycle:21 → 6(主进程 12 文件环与浏览器 3 文件环全部清零,全仓仅剩 app-client/stores 6 文件环);oxlint import/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 通道、参数校验或浏览器逻辑。

  • 受影响模块:Desktop 主窗口创建/显示/隐藏、关闭到托盘与退出确认、系统菜单与全局快捷键、自动更新退出安装、诊断采集与导出、截图窗口解析、内嵌服务连接。
  • 平台核对:涉及 macOS / Windows 关闭到托盘(shouldHideOnClose)、Windows session-end 退出监听与自定义标题栏菜单隐藏;逻辑未变,macOS 本地类型检查与单测通过,Windows 由 CI windows 任务验证。
验证记录
  • 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

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).(内部重构 PR 1.5,无关联 issue,依赖 refactor(app-core): split composables entrypoint and decouple toast contracts from api #5561)
  • I have added tests that prove my feature works.(新增 window-registry.test.ts 5 例与 terminal-menu-focus.test.ts 1 例,并复跑全部受影响的 15 个既有测试文件)
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.(私有桌面端内部重构,无用户可感知变化,无需 changeset)
  • Ran gen-docs skill, or this PR needs no doc update.(不改变用户可见功能,无需更新用户文档)

@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ec984bc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:20:40.068849Z ec984bc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@ec984bc
npx https://pkg.pr.new/@moonshot-ai/kimi-code@ec984bc

commit: ec984bc

@liruifengv liruifengv changed the title refactor(desktop): extract main window registry and terminal menu focus state refactor(desktop): break main process window and browser runtime cycles Nov 26, 2026
@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: ec984bccc2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@wbxl2000
wbxl2000 merged commit 0426f9e into refactor/app-core-entrypoints Nov 27, 2026
23 checks passed
@wbxl2000
wbxl2000 deleted the refactor/main-window-registry branch November 27, 2026 03:54
liruifengv added a commit that referenced this pull request Nov 27, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants