Skip to content

refactor(gateway): core survives client exits until an explicit stop - #616

Open
h4rm00n wants to merge 3 commits into
AAswordman:mainfrom
h4rm00n:refactor/gateway-core-manual-shutdown
Open

h4rm00n wants to merge 3 commits into
AAswordman:mainfrom
h4rm00n:refactor/gateway-core-manual-shutdown

Conversation

@h4rm00n

@h4rm00n h4rm00n commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

变更说明

要解决的问题:此前网关(core)的生命周期绑定在客户端连接上——最后一个客户端断开 1 秒后自动关停,无前端的启动 60 秒后过期。但 TUI/前端可能因各种原因断开(SSH 掉线、终端关闭、应用重启),"谁拉起谁消灭"会误杀仍在使用的 core,也让"退出前端 = 丢失内核"成为隐式副作用。

采用的方案:改为显式生命周期——

  • 网关侧(apps/cli/src/gateway.rs)删除 last-client-disconnect 自动关停分支与 GatewayClients 租约全套机制(含 60 秒启动死线);网关现在运行到显式 shutdown 控制操作、前台 Ctrl+C 或宿主终止信号为止。断开连接不再保留或停止进程。
  • 共享协议层新增 gateway_shutdown_request(GatewayControlClient.rs),供非 CLI 嵌入方(flutter bridge)复用同一条显式停止路径。
  • TUI 侧:/exit、/quit 只退出 TUI,内核保持运行;新增 /exit core(/quit core 等价)退出 TUI 并向网关发显式 shutdown——owner 先落 stop 意图标记,其他附着 TUI 的自动重启 supervisor 随即暂停,不会被立刻拉回。stop 请求失败时 TUI 保持打开以便重试。Ctrl+C / Ctrl+Q 行为不变(普通退出)。
  • 桌面 App 托盘(Linux / Windows / macOS 三平台 keep-alive 插件):
    • 原有"退出 "菜单项改为显式全停:经新增的 stopGateway 运行时 Host 方法(bridge FFI 导出 operit_flutter_bridge_gateway_stop + 三平台 runner 转发)停掉网关后再通过 keep-alive 新增的 quit 方法原生退出——保持该按钮原有的"全停"用户语义(此前靠自动关停实现)。
    • 新增"仅保留内核退出"菜单项:直接原生退出前端,内核继续服务其他前端(TUI / 下次启动)。
    • Dart 侧 CoreApplicationService 订阅 keep-alive 新增的 exitActions 事件流处理 stopCore 退出(尽力停止网关后必退出);浏览器运行时 stopGateway 为空实现。
  • 文档同步更新(docs/pr603-multi-identity-validation.md 生命周期描述)。

影响范围:桌面网关进程生命周期、CLI/TUI 退出语义、桌面 App 托盘菜单、flutter bridge FFI 面(新增一个导出,未改动既有导出)。不改动控制协议既有操作(复用 shutdown op);移动端/嵌入式运行时不受影响(stopGateway 在非桌面平台返回明确错误或空实现)。

改动类型

  • Bug 修复
  • 新功能
  • 重构或性能改进
  • 文档或构建工具
  • 第三方依赖或许可证更新

影响范围

  • 平台:Linux / Windows / macOS 桌面(CLI、TUI、Flutter 桌面端);移动端不受影响
  • 模块:apps/cli(gateway 生命周期、TUI 命令与 i18n)、hosts/common/operit-host-native-common(控制协议助手)、apps/flutter/native/operit-flutter-bridge(FFI 导出与桌面网关)、三平台 runner(runtime channel 转发)、apps/flutter/thirdparty/operit_keep_alive(托盘插件协议)、apps/flutter/app(Dart Host 接口、CoreApplicationService、main)

验证

内置包解析为必需检查,覆盖 buildin 和 external,包括默认关闭的插件。本次未改动任何插件包源码或清单,仍按要求实际运行并通过:

python plugins/tools/sync_plugin_packages.py --source runtime --no-hot-reload
# Done. source=runtime, copied=18, packed=40, deleted=0, dry_run=False
# 内置 cargo test --locked -p operit-js-bridge --lib plugin_parse_gate 全部通过
# Package parsing 检查:无自动 CI(checks 显示 none 属正常),以上为本地实跑结果
1. cargo test --manifest-path apps/cli/Cargo.toml -j 4 --lib -- --test-threads=1
   → 161 passed / 0 failed / 3 ignored(删除两个随自动关停机制移除的租约生命周期测试,
     新增 stop 标记往返与 /exit 参数解析测试)
2. cargo check --manifest-path apps/cli/Cargo.toml -j 4 → exit 0
3. cargo check --manifest-path apps/flutter/native/operit-flutter-bridge/Cargo.toml -j 4 → exit 0
   (--tests 目标存在上游既有 jni 模块解析失败,干净 HEAD A/B 同为 8 个错误,与本次无关;
     顺带修复了被忽略的桌面网关进程测试中 executable move-while-borrowed 编译错误)
4. cargo test --manifest-path hosts/common/operit-host-native-common/Cargo.toml -j 4 --lib
   → 8 passed / 0 failed / 1 ignored
5. (cd apps/flutter/thirdparty/operit_keep_alive && flutter test)
   → 24 passed(新增 exitRequested 解码与 quit 透传、未知 mode 拒绝 2 个测试)
6. flutter analyze(apps/flutter/app)→ 17 errors,与干净 HEAD A/B 完全一致(既有基线),零新增
7. 行为级端到端(隔离 XDG/dbus 环境,debug 二进制):
   - `gateway start` 后路由客户端(cli chat list)连接并断开,网关持续运行(旧行为 1s 自杀)
   - TUI 启动自动拉起网关;`/exit` 退出 TUI 后网关仍 running
   - 重开 TUI 后 `/exit core`:TUI 退出、网关停止、设备级 stop 意图标记落盘
   - `gateway stop` 显式停止正常
8. Linux 托盘 UI 实机验证未做(需 GUI 会话),采用与既有菜单项完全相同的 GTK/Swift/Win32 模式修改

截图或日志

复现步骤(TUI,端到端实测记录):

$ operit2 tui                          # 无 core 时自动拉起 gateway
$ /exit                                # TUI 退出;gateway status → running(pid 不变)
$ operit2 tui                          # 再次附着同一网关
$ /exit core                           # TUI 退出;gateway status → not running;stop 标记写入

托盘菜单结构(Linux 实测占位符渲染):打开 / 仅保留内核退出 / 退出 。

提交者确认

  • 全部内置及官方扩展包通过生产解析器和注册引擎校验,PR 的 Package parsing 检查通过。
  • 我已阅读 CONTRIBUTING.md。
  • 我已按 CLA.md 的流程签署协议,或提供了覆盖本次贡献的既有签署记录;所有共同贡献者均有相应签署记录。
  • 我拥有本次提交内容的版权或必要授权。
  • 我已保留第三方代码、资源及其许可证和版权声明。
  • 我没有提交密钥、令牌、个人数据或本地构建产物。
  • 我已更新受影响的文档和测试,或在上文说明了原因。

CLA 签署评论链接:

#520 (comment)

Frontends detach for many transient reasons (SSH drops, terminal
closes, app restarts), so the gateway must not tie its lifetime to
client connections. Remove the last-client-disconnect auto shutdown
and the startup deadline along with the GatewayClients lease
machinery; the gateway now runs until the explicit shutdown control
operation, Ctrl+C, or a host termination signal. Share the shutdown
request as gateway_shutdown_request for non-CLI embedders and update
the multi-identity validation doc to describe the new lifecycle.

Signed-off-by: h4rm00n <harmoon@qq.com>
Plain /exit and /quit now leave the gateway running for whatever
attaches next; /exit core (and /quit core) additionally sends the
gateway shutdown request, whose stop intent pauses every attached
TUI's auto-restart supervisor. A failed stop request keeps the TUI
open so the user can retry or fall back to a plain quit.

Signed-off-by: h4rm00n <harmoon@qq.com>
The tray exit action now performs an explicit full stop: it asks Dart
to shut the gateway down through a new stopGateway runtime Host
method (bridge FFI export plus Linux/Windows/macOS runner forwards)
before quitting via a new keep-alive quit method, preserving the old
button's stop-everything behavior now that exits no longer stop the
core implicitly. A new tray item, 仅保留内核退出, quits the frontend
natively and leaves the gateway serving other frontends. The desktop
gateway process test expects the surviving gateway instead of the
auto shutdown and also fixes its latent executable move-while-
borrowed compile error in the ignored target.

Signed-off-by: h4rm00n <harmoon@qq.com>

This branch has not been deployed

No deployments
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.

1 participant