Skip to content

feat(remote): keep or end host programs when a connection closes (Refs 1lck/Lithe-IDEA#2231) - #160

Merged
xiaoyumuxi merged 3 commits into
mainfrom
feat/2231-close-remote-processes
Nov 22, 2026
Merged

xiaoyumuxi merged 3 commits into
mainfrom
feat/2231-close-remote-processes

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

关联 Issue

Refs 1lck/Lithe-IDEA#2231(§9.1、§9.2、§10 关闭确认与保留主机进程;安全项 S33、S35;决策 D5)

桌面端:1lck/Lithe-IDEA#2291(固定到本 PR 的 a81ad08)

类型

feat

改动说明

关闭远程工作区窗口时,主机上还在跑的进程和终端由用户决定结束还是保留。客户端每次都问,可以记住选择,也可以在设置里改回来。没人决定的进程不会一直留在主机上。

持久会话与重新连接

  • remote.connect 改为启动持久服务器会话:--connect <sessionKey>,提供 installRoot 时加上 --state-dir <installRoot>。守护进程的 run/、log/ 和安装目录 bin/ 在同一个根目录下,互不冲突。
  • 连接结果的 session 新增四个字段:sessionKey、sessionToken、resumed、keptPrograms。resumed 为真时,Core 立即列出会话里仍在运行的程序;列表失败时记 remote.session.state_failed 日志,并返回空列表。
  • 新增 resume: { key, token },用于接回之前保留的会话。key 和 token 在 OpenSSH 或 wsl.exe 启动前校验:key 为空、超过 64 个字符或含字母、数字、.、_、- 以外的字符,token 为空、超过 128 个字符或不是十六进制,都返回 invalid_request,{ field: "resume", reason: "invalid" }。key 会进入远端命令行,所以必须先校验。
  • 主机拒绝 token(INVALID_SESSION_TOKEN,会话已结束、key 被别的会话占用)时,Core 不带 resume 再连一次。代价是用户多认证一次,但不会被旧 token 锁在外面。

server.keep

  • 新增协议方法 server.keep:客户端离开前告诉主机保留仍在运行的程序。只有被保留的会话能超过空闲超时(十分钟)继续存在,程序全部结束后会话照常结束。
  • 新客户端接入会清除之前的保留标记,所以客户端崩溃或断网后,它的程序会在空闲超时后结束,不会无限期留在主机上。
  • --stdio 会话活不过客户端,收到 server.keep 时返回 UNSUPPORTED。

remote.disconnect

  • 请求新增 programs: "end" | "keep",默认 end。结果从 { closed } 改为 { closed, kept }。
  • end 先关闭转发,再发 server.shutdown,然后结束传输。keep 先关闭转发,再发 server.keep,然后结束传输,不结束会话。两种请求都用 EXIT_GRACE 限时,关窗口不会卡住。通知主机失败时 kept 为 false,程序按崩溃客户端的规则在超时后结束;除 Disconnected 外的失败记 remote.session.cleanup_failed。
  • 连接不存在或已经关闭时返回 { closed: false, kept: false }。

remote.runningPrograms / remote.endPrograms(新模块 workspace/src/programs.rs)

  • runningPrograms 返回 { programs: [{ kind, id, argv, label, runningMs }] },进程在前、终端在后。客户端在关闭确认和重连后的列表(S35)里用它。
  • endPrograms 的请求是 { sessionId, programs? }。不带 programs 时结束全部;带上 [{ kind, id }](runningPrograms 列出的类型和编号)时只结束这些。接回被保留的会话后,客户端只结束上次列出的程序,不会挂断这次新开的终端。kind 不是 process 或 terminal 时返回 invalid_request。
  • 结束方式:进程组收到 SIGTERM(process.kill,force: false),终端挂断(terminal.close)。每个请求限时 30 秒,返回 { ended }。请求期间已经结束的程序(UNKNOWN_RESOURCE)和会话里没有的编号不计数;不理会信号的程序仍会留在列表里。
  • 两个命令都加进请求追踪的非热点列表。

设置

  • remote/preferences.json 新增 closePrograms,取值 ask、end、keep。自动值是 ask,即每次都问。
  • remote.preferences 新增读取字段 closing: { source, programs, idleTimeoutSeconds }。remote.editPreferences 支持 closePrograms: { programs: ...|null },null 改回自动。
  • idleTimeoutSeconds(600)是主机等多久才结束断线且没人保留程序的会话。它由服务器决定,没有文档能改;桌面端设置页只读显示它,不再自己抄一份常量。常量 DEFAULT_IDLE_TIMEOUT_SECONDS 从服务器移到 protocol::methods,客户端和服务器共用。

日志

  • 新增事件 remote.session.state_failed:warn 级,remote 通道,每 60 秒限一次,字段为 stage、error。
  • remote.session.cleanup_failed 的 stage 说明加入 keep。
  • 资源清理(workspace/src/host.rs 的 Resource::drop)发现连接已经关闭(ClientError::Disconnected)时,不再记 remote.resource.cleanup_failed WARN。这时主机已经结束会话、按用户的选择保留了它,或者会在空闲超时后结束它,这一侧已无事可做;会话自身的清理本来就这样处理。其他清理失败照旧记 WARN。
  • 目录重新生成,共 149 个事件。日志里不出现 session key、token、命令行和标签。

契约文档

  • contracts/remote-workspace-protocol-v1.md:方法表加 server.keep。空闲超时一段写明:没有客户端接入时开始计时,除非最后一个客户端发了 keep 且仍有程序在运行。“无客户端接入时”一段写明 keep 语义:接入清除保留,崩溃客户端的程序在超时后结束。
  • contracts/rust-core-api.md:
    • 命令表更新 remote.disconnect、remote.editPreferences,新增两行。
    • 设置一节加 closePrograms 和 closing(含 idleTimeoutSeconds)。
    • 命令表和会话命令表的 remote.endPrograms 写明可选的 programs 列表。
    • 连接一节加 resume?、新字段和持久会话说明(重新连接、拒绝后重连、校验规则)。
    • 断开写明 { sessionId, programs? } 返回 { closed, kept },以及十分钟空闲超时。
    • 会话命令表加两行。

验证

  • cargo fmt --all
  • cargo clippy -p lithe-remote -p lithe-workspace -p lithe-core --all-features --all-targets -- -D warnings:无警告
  • cargo test -p lithe-remote --all-features:全部通过。persistence.rs 14 个,覆盖:keep 后无客户端仍保留程序;未 keep 的会话超时结束;接入清除保留;程序结束后被保留的会话结束;stdio 会话拒绝 keep。
  • cargo test -p lithe-workspace --all-features:61 个通过。新增 a_malformed_resume_is_refused_before_the_client_runs,覆盖 7 组非法 key/token,确认都在 OpenSSH 启动前被拒。另有 closePrograms 编辑往返测试。
  • cargo test -p lithe-core --lib:890 个通过。新增 remote_programs_are_listed_ended_and_kept_only_by_a_persistent_session,通过分发器验证:
    • 列出进程和终端(种类、编号、参数、标签);
    • 全部结束后列表为空,再次结束返回 0;
    • programs: "later" 被拒;
    • stdio 会话 keep 返回 kept: false;
    • 再次断开返回 closed: false。
  • node scripts/logging/generate-log-events.mjs --check:149 个事件,生成结果一致。
  • 本机 plugin_sdk_build 的端到端用例因缺少 Node 环境失败("native workflow checks require Node")。这些用例由 shared/plugins/scripts/sdk-e2e.mjs 运行,与本改动无关。

…1lck/Lithe-IDEA#2231)

- remote.connect starts the persistent server session (--connect <key>,
  --state-dir <installRoot>) and returns sessionKey, sessionToken, resumed
  and keptPrograms; resume { key, token } attaches to a kept session, and a
  refused token connects once more with a new session.
- server.keep: only programs the user chose to keep outlast the idle
  timeout; attaching clears an earlier keep, so a crashed client's programs
  end after ten minutes.
- remote.disconnect takes programs: end|keep and returns { closed, kept }.
- remote.runningPrograms and remote.endPrograms list and end what a host
  session still runs.
- closePrograms preference (ask|end|keep, automatic asks) with closing in
  remote.preferences.
- remote.session.state_failed log event; protocol and Core API docs.
…anup is expected (Refs 1lck/Lithe-IDEA#2231)

remote.endPrograms takes an optional programs list ([{kind, id}] as
remote.runningPrograms listed them), so a client that reconnected to a
kept session ends what it listed without hanging up a terminal it
started meanwhile. Resource cleanup that finds the connection already
closed (after server.keep or a loss) no longer logs WARN, as the
session's own cleanup already treats it.
… choice (Refs 1lck/Lithe-IDEA#2231)

remote.preferences now reports closing.idleTimeoutSeconds, the server's default idle timeout, so the Remote Workspace settings page shows the value the host applies instead of a copy. The constant moves to protocol::methods so the client and the server share it.
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