Skip to content

feat(cli): 再次启动把路径交给已在运行的 Lithe - #2548

Merged
xiaoyumuxi merged 2 commits into
mainfrom
feat/cli-single-instance
Nov 30, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
feat/cli-single-instance

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

关联 Issue / Linked issue

PR 类型 / Type

  • feat:新功能或用户可见的改进
  • fix:缺陷修复
  • docs:文档、Agent Note、Skill 等规则文件
  • release:稳定版发布准备

改动说明 / What changes

背景 / Why

Lithe 已在运行时,在终端执行 lithe <文件夹> / lithe <文件>,或从访达、Dock 再次启动,都会新开一个进程和窗口:

  • 用户想在已打开的窗口里多开一个项目标签,得到的却是一个新窗口。
  • 多个进程同时读写同一份用户数据(偏好、日志、插件状态、会话恢复),会互相覆盖。
  • 命令行也没有办法区分“复用窗口”和“确实要另开一个窗口”。

现在的做法:不带 --new-window 的启动先把路径交给同一份用户数据上已在运行的 Lithe(下称“运行副本”),由运行副本把窗口带到前台并打开;找不到运行副本时,新进程才自己启动。这与 VS Code、IDEA 命令行的默认行为一致。

改动 / Changes

启动与交接

  • desktop/cmd/lithe/main.go
    • Core planLaunch 返回 handOff 时,用 Viva 的 app/singleinstance 认领实例。没能成为运行副本的进程,在创建窗口、日志、插件和登录 shell 之前退出。
    • 认领出错时照旧自己运行,并记一条 WARN app.background.operation_failed(operation=single_instance_claim)。
    • Run() 返回后释放认领。
    • 启动器子进程现在传 --new-window 和 -- <路径>,不再只传路径。
  • 新增 desktop/internal/cli/launch.go
    • Target:取绝对路径、去掉符号链接,再观察路径类型。它由原来的 cmd/lithe 里的 commandLineTarget 搬过来。
    • Observe:观察路径类型。
    • InstanceID:由应用 id 和用户数据目录 store.Dir() 组成;开发构建再加 DevelopmentBuild(),即可执行文件的路径和修改时间。
    • HandOffArgs:生成交给运行副本的参数 -- <绝对路径>。
  • desktop/internal/core/cli.go:CLILaunch 增加 NewWindow、HandOff 两个字段,测试按 Core 共享 fixture 校验。

运行副本里的处理

  • 新增 desktop/internal/workbench/launch_handoff.go
    • LaunchInbox 在窗口就绪前最多排队 16 个启动,超出的丢弃。认领只释放一次。
    • 窗口就绪后,每个启动交给当前显示的标签处理:
      1. 交给 Core PlanCLILaunch 再校验一次,不是交接请求的参数(比如带 --new-window)会被拒绝;
      2. 用 cli.Target 重新观察路径;
      3. 调用 openLaunchTarget 打开。
    • openLaunchTarget 按路径类型打开:
      • 文件:开一个单文件标签。
      • 已打开的文件夹:切到那个标签。
      • 其他文件夹:走现有的 openProject,遵守“打开项目的位置”设置。当前显示的是单文件标签时,改用第一个项目标签打开。
  • app.go
    • Options.Launches 把收件箱接进工作台。
    • Kernel 接口增加 PlanCLILaunch。
    • 新增界面来源 surfaceLaunch。
    • RunApplication 返回后关闭收件箱。

顺带修复:交接路径会用到的既有问题

交接会让单文件标签和项目标签出现在同一个窗口,下面这些问题因此会直接暴露出来:

  • window_sessions.go
    • 单文件标签的标签栏 key 原来和项目标签可能相同,同时存在时会冲突导致崩溃,现在改为 document:<序号>。
    • showing() 原来会把单文件标签当成某个项目标签匹配,现在跳过单文件标签。
    • 新会话不继承收件箱。
  • system_document.go
    • 访达“打开方式”交来的文件夹,原来报“不是普通文件”并拒绝,现在和交接走同一个 openLaunchTarget,按项目打开。
    • 这类启动的提示原来可能落在后台标签上,现在显示在当前标签上。
  • project_open_destination.go:单文件窗口里的“以项目打开所在文件夹”改为 exe --new-window -- <路径>。它本来就要另开进程,不能被交接回当前窗口。

规则、依赖与文档

  • scripts/desktop/build-desktop-core.sh:PINNED_CORE_REVISION 固定到 Core main 的 add378d,即 feat(cli): 启动计划给出 newWindow 与 handOff,再次启动交给已在运行的副本 Lithe-IDEA/Lithe-core-rust#235 的合入提交。它的父提交是 main 原来固定的 58cc3fc([Bug] ctrl+shift+f打开全局搜索,打开搜到的文件标签之后,搜索结果消失了 #233),所以合并 main 时两边的固定版本都被包含在内。

  • desktop/go.mod / go.sum:补 github.com/Microsoft/go-winio(indirect)。这是 Viva singleinstance 在 Windows 上建命名管道用的依赖;原来缺少 go.sum 条目,Windows 编译会失败。

  • shared/i18n/features/cli-single-file.json 和生成的 shared.generated.go:lithe --help 的中英用法加入 [--new-window],并说明交接行为。

  • 新增 Agent Note .agents/notes/implemented/feature/2026-11-30-cli-launch-hands-off-to-running-copy.md,内容包括:

    • 决策、备选方案、代价与限制;
    • 运行时资源表;
    • 验证方法。

    原 CLI Note 的“后果”一节加了指向它的链接。

  • 删除 desktop/cmd/lithe/main_test.go:它测的 commandLineTarget 已搬到 cli.Target,用例改为 internal/cli 里的 TestTargetClassifiesActualFileAndFolder,覆盖范围不变。

不在本 PR 范围内 / Out of scope

  • Viva singleinstance 的两项限制,已在 singleinstance:Unix 按路径删 socket 会删掉后来者的认领,Windows 不核对管道服务端所有者 Lithe-IDEA/Lithe-viva#84 记录,按规则在 Viva fork 修复,Lithe 不在应用层绕过:
    • Unix 上 Close 和清理残留 socket 时按路径删除文件,几乎同时的两次启动可能各自成为运行副本。
    • Windows 客户端不核对命名管道服务端的所有者。
  • --new-window 进程不认领实例。 运行副本退出后,如果还有 --new-window 另开的进程在运行,下一次启动会成为新的运行副本。这一点已写进 Note。
  • 原生验收:两个真实进程之间的交接、窗口来到前台,以及 Windows 前台锁下的行为,都还没有在 macOS 和 Windows 上原生验收。功能矩阵两端都记为 pending。

类型专属:feat

多平台对齐

  • 两端都具备:交接逻辑是 macOS 与 Windows 共用的 Go 代码,跨进程通道由 Viva singleinstance 按平台实现(Unix socket / 命名管道)。Windows 只做过 singleinstance 包的交叉编译和 go mod verify,原生验收待做,功能矩阵记为 pending。
  • 只实现了 macOS:已为 Windows 提交跟进 Issue:#
  • 只实现了 Windows:已为 macOS 提交跟进 Issue:#
  • 刻意只属于某个平台(功能矩阵中另一端记为 platform-specific),理由:

范围检查 / Scope checks

功能矩阵 / Feature matrix

  • 已新增 shared/platform-feature-matrix/features/cli-launch-handoff.json:
    • 两端都是 implemented / pending;
    • 证据路径都已核对存在;
    • owner 为 Desktop;
    • 写了终端、访达和 Windows cmd/PowerShell 的验证动作。
  • 已运行 ./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh(validated 231 platform capabilities),以及 ./scripts/platform-feature-matrix/verify-platform-feature-matrix-change.sh origin/main HEAD(matrix change gate passed)。
  • 没有提交 .artifacts/ 下生成的矩阵视图。
  • [N/A] 没有更新矩阵的理由:已更新。

插件归属 / Plugin ownership

  • 已在仓库根目录运行 develop-lithe 第三节的自查命令,结果为空。
  • 新增测试只用临时目录里的真实文件和文件夹,以及 Core 契约 fixture,不涉及任何插件。
  • 没有为具体插件提供的能力新建或更新宿主功能矩阵条目。
  • 没有插件文案、插件构建步骤或资源说明进入宿主。
  • [N/A] 修改通用扩展点:没有改插件扩展点或插件 API。

UI 验证 / UI evidence

N/A:没有布局、控件、样式或菜单变化。交接打开文件夹或文件时,用的都是现有的项目标签、单文件标签和“打开项目的位置”流程。参数被拒绝或路径失效时,复用现有的打开失败通知(Errors.OpenFailed)。唯一改动的文案是终端里 lithe --help 的用法说明。窗口来到前台属于整机行为,原生验收见“不在本 PR 范围内”。

统一日志系统 / Structured logging

  • 事件都已在固定 Core 修订的日志目录中声明:
  • 每次交接只有一条终态;错误只在处理它的边界记录一次;没有高频 INFO。
  • 不记录交接参数和路径:错误先经现有的 documentLogError 去掉 PathError 里的路径,再写入 error 字段;被丢弃的启动只记一条失败,不带参数。
  • [N/A] 旧日志脚本:本 PR 没有涉及。

Windows 与 macOS 共用这份 Go 代码,用的是同一套日志。

界面文案 / i18n

  • lithe --help 用法的中英词条已在 shared/i18n/features/cli-single-file.json 更新,运行了 node scripts/i18n/generate-i18n.mjs 并提交了生成结果。
  • [N/A] 插件文字:不涉及。
  • 已运行 node scripts/i18n/verify-app-language-localization.mjs 和 node scripts/i18n/generate-i18n.mjs --check,都通过。

逻辑归属 / Logic placement

性能与内存 / Performance and memory

  • 不新增后台轮询。
    • 运行副本只持有一个监听 socket 或命名管道,有连接时才处理。
    • 收件箱最多 16 项。
    • 认领在事件循环结束时释放。
  • 主线程上没有新增文件读写或进程启动:
    • 认领发生在创建窗口之前。
    • 交接的接收在 Viva 的服务 goroutine 上,只把参数放进收件箱。
    • Core 再校验和路径观察在 a.background 后台任务里执行(有 kernelCallTimeout 上限),窗口线程只做最后的打开。
  • 启动影响:
    • 运行副本启动时多一次本地 socket 监听,没有单独测量启动基线。
    • 交接方在 Core planLaunch 和路径观察之后就退出,不再创建窗口、插件和登录 shell,比原来每次完整启动轻得多。
    • 新增的 go-winio 只在 Windows 上编译进来。

设置 / Settings

N/A:没有新增或修改设置。交接打开文件夹时遵守已有的“打开项目的位置”设置。

插件契约 / Plugin contract

N/A:不涉及插件契约、能力版本、插件函数协议或宿主方法。Core 侧只改了应用 CLI 规则 application.cli.planLaunch 和日志目录,不属于插件协议。即便如此,PINNED_CORE_REVISION 仍已升到包含该改动的 Core main 提交 add378d。

发行包与资源 / Bundle and resources

  • 运行时不向 app bundle 或安装目录写入。
    • macOS / Linux 的认领 socket 在用户配置目录:macOS 是 ~/Library/Application Support/viva/,Linux 是 /run/user/<uid>/viva/。它由 Viva 创建和删除,崩溃残留由下一次启动清理。
    • Windows 用命名管道,没有文件。
    • 生命周期、所有权和可写位置写在 Note 的“运行时资源”表里。
    • 已运行 ./scripts/desktop/verify/verify-runtime-bundle-immutability.sh,通过。
  • [N/A] docs/ci-builds.md:没有新增构建期的下载、解压、生成或缓存资源。

无用代码与重构 / Dead code and refactoring

  • 搬走 commandLineTarget 之后不再使用的 cmd/lithe/main_test.go 已删除。
  • [N/A] 既有可重构之处:本次改动涉及的既有问题都在交接路径上,已在本 PR 修复,见“顺带修复”;没有发现其他需要建单的债务。

验证 / Verification

命令或操作 平台 结果
合并 main c97790c(#2542)后(e312a9560,Core 固定 add378d,所链接的 Core 检出与它内容完全相同):desktop-go.sh vet ./...、desktop-go.sh test -count=1 ./... macOS arm64 都通过,exit=0(workbench 380s)
scripts/desktop/desktop-go.sh test -count=1 ./...(链接 Core 744d2f5,rebase 到 main 510f121 之前) macOS arm64 全部通过,exit=0(workbench 379s)
rebase 到 main 510f121 后(8374b6d3c):desktop-go.sh vet ./cmd/lithe ./internal/cli ./internal/core ./internal/workbench macOS arm64 通过
8374b6d:desktop-go.sh test ./cmd/lithe ./internal/cli ./internal/core ./internal/workbench -run 'TestTarget|TestInstanceID|TestHandOffArgs|TestCLI|TestLaunchInbox|TestHandedLaunches|TestSystemDocumentFolder|TestStandalone' -count=1 macOS arm64 通过
8374b6d:同组交接测试加 -race -v macOS arm64 通过,无数据竞争;单测试最长 1.15s(TestHandedLaunchesOpenInTheRunningWindow,竞态模式),其余 ≤0.03s
node scripts/i18n/generate-i18n.mjs --check、node scripts/i18n/verify-app-language-localization.mjs macOS 通过(中英 13399/13399)
./scripts/agent-notes/verify-agent-notes.sh macOS 通过(224 份)
./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh、verify-platform-feature-matrix-change.sh origin/main HEAD macOS 通过
./scripts/desktop/verify/verify-runtime-bundle-immutability.sh macOS(检查 macOS 与 Windows 共用源码) 通过
develop-lithe 第三节插件归属自查 rg macOS 结果为空
Viva app/singleinstance:GOOS=windows GOARCH=amd64 go build、GOOS=linux go build,以及 go mod verify macOS 交叉编译 通过(补 go-winio 的 go.sum 之前 Windows 编译失败)
Core 仓库:cargo test -p lithe-core-rules、cargo test -p lithe-diagnostics、log-events --check、cargo fmt --check、cargo clippy macOS arm64 通过(Lithe-IDEA/Lithe-core-rust#235)

未能执行的检查和原因:

  • 两个真实 Lithe 进程之间的交接:终端执行 lithe <路径> 时窗口来到前台,以及访达再次打开。这些需要 macOS 整机验收,本次没有启动 Lithe 应用;单元和工作台测试只覆盖收件箱之后的部分。
  • Windows 原生:cmd / PowerShell 里的交接、命名管道、前台激活都还没有验收,需要在本机 Parallels 虚拟机上做。桌面包依赖 cgo / Rust Core,无法在 macOS 上交叉编译,只交叉编译了 Viva singleinstance 包(windows/amd64、linux/amd64)并运行了 go mod verify。

提交前确认 / Final checks

  • 分支从最新的 main 创建,目标分支 main。
  • 以 Draft 提交。Core Merge local preview/0.3.0 updates #235 已合入,固定版本已换成 main 上的 add378d。
  • 没有无关改动,没有产物、凭据或个人路径。
  • 本次没有启动 Lithe 应用;测试进程都已结束,没有残留。

Lithe 已在运行时,lithe <路径>、访达或 Dock 再次启动不再新开进程:新进程按 Core planLaunch 的 handOff 用 Viva singleinstance 把解析后的绝对路径交给运行副本,然后在建窗口、日志和插件之前退出。运行副本把窗口带到前台,经 Core 二次校验、重新观察路径后,文件夹按“打开项目的位置”打开(已打开的切过去),文件开单文件标签。找不到运行副本才自己启动。

- 实例名包含用户数据目录;开发构建再加可执行文件路径和修改时间,不交给安装版或旧构建
- --new-window 另开进程;单文件窗口“以项目打开所在文件夹”改用它
- LaunchInbox 在窗口就绪前最多排队 16 个启动,随事件循环结束释放认领
- 访达交来的文件夹按项目打开(原来被拒绝),提示落在当前显示的标签上
- 修复单文件标签与项目标签的标签栏 key 冲突、showing() 误匹配单文件标签
- 固定 Core 744d2f5(Lithe-IDEA/Lithe-core-rust#235),合入后换 main 提交
- 新增 Agent Note 与功能矩阵条目 cli-launch-handoff

Closes #2545
# Conflicts:
#	scripts/desktop/build-desktop-core.sh
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 30, 2026 10:02
@xiaoyumuxi
xiaoyumuxi merged commit 3572abc into main Nov 30, 2026
11 of 20 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

4 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +165 to +174
if open := s.showing(path); open != nil {
s.activate(open)
return
}
target := s.active
if index := slices.IndexFunc(s.list, func(a *App) bool { return !a.documentOnly }); target.documentOnly && index >= 0 {
target = s.list[index]
s.activate(target)
}
target.openProject(path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Repeated launches duplicate project windows

With the New Window setting, openLaunchTarget opens a fresh window on every CLI launch of the same second project. It searches only the original window, so it never finds the project opened by the previous launch.

Learn more

The CLI sends normal launches to the process that owns the claim, not necessarily the process that owns the requested project; see main. The receiving process looks for the project in its own tabs. When another project is open and the user selected New Window, openProject calls launchClient to start a separate process with --new-window. That process does not claim or register its project with the receiver. Subsequent launches of the same project therefore repeat the New Window action. The same routing gap exists if the primary was started in single-file mode and has no project tab.

Example: Leave Project A open in the primary and choose New Window in settings. Run lithe ~/code/beta and let Beta finish opening. Run the same command again; it starts a second Beta window instead of activating the first one.

Recommended fix: Let the launch receiver discover and activate projects in all live windows it opens on behalf of handoffs. Register those windows and remove their registrations on close, or host them in the claiming process. Keep explicit lithe --new-window launches separate.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread desktop/cmd/lithe/main.go
Comment on lines +81 to +84
claim, primary, claimErr = singleinstance.SingleInstance(
cli.InstanceID(preferences.Dir(), build), cli.HandOffArgs(path), inbox.Receive)
if claimErr == nil && !primary {
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟥 Windows handoff trusts unverified pipe servers

On Windows, SingleInstance sends the absolute launch path without verifying who owns the pipe server. Another local program that owns the expected pipe can read the path and acknowledge it without opening anything.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: high ghfind author score; see https://ghfind.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] lithe 命令和再次启动交给已在运行的 Lithe,在当前窗口打开文件夹或文件,--new-window 才另开窗口

1 participant