Skip to content

feat(remote): 进入 Docker/Podman 容器、列出运行中的容器,并用 Dev Container CLI 从 devcontainer.json 启动 (#2231) - #162

Merged
xiaoyumuxi merged 4 commits into
mainfrom
feat/2231-remote-containers
Nov 22, 2026
Merged

xiaoyumuxi merged 4 commits into
mainfrom
feat/2231-remote-containers

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

关联 Issue

Refs 1lck/Lithe-IDEA#2231(R2 Dev Container:容器目标,以及从 devcontainer.json 构建并启动;安全项 S32)

桌面端在 1lck/Lithe-IDEA#2295(Draft,固定到本 PR 的 05c4139;e1e0d10 只加测试,不需要换固定版本):容器连接页,以及“从 devcontainer.json 打开”页。SSH 主机和 WSL 发行版上的 devcontainer up 不在本 PR。

类型

feat

改动说明

本 PR 有三个提交,下面按功能分两部分说明;第三个提交(e1e0d10)只补测试。

  • 第一个提交:进入容器。 远程工作区可以连到正在运行的 Docker 或 Podman 容器,包括本机上的容器、SSH 主机上的容器和 WSL 发行版里的容器。Lithe 只进入正在运行的容器:不启动、不创建、不改动容器,不挂载目录,也不要求比引擎 CLI 更多的权限(S32)。
  • 第二个提交(05c4139):从 devcontainer.json 启动。 还没有容器时,Core 可以为本机上的受信任文件夹调官方 Dev Container CLI 构建并启动容器,返回进入它所需的参数,之后用第一个提交的 container 进入。

remote.connect 新增 container: { engine, container, user? }

  • 字段:
    • engine 只能是 docker 或 podman。
    • container 是名字或 id,例如 remote.listContainers 返回的值。
    • user 是容器里的用户、uid 或 user:group;没填或为空时用镜像的默认用户。
  • 跟在 connection 或 wsl 后面时,在那台主机登录后,远端命令变成 <engine> exec -i [-u <user>] <container> …。
  • 单独出现时,容器在本机上,Core 用自己的环境直接启动引擎 CLI,代替 OpenSSH。这个环境里有桌面端放在前面的登录 shell PATH,以及 DOCKER_HOST 这类选择服务的变量。不走 AskPass。
  • 探测、校验后上传到容器的 $HOME,以及之后的各个阶段,都和 SSH 主机一样,复用 launch 链。
  • 状态分类:
    • 服务器启动前引擎就放弃时,容器不存在、没在运行、已暂停,以及服务没有应答,都是 unreachable;
    • 没有权限打开服务套接字是 failed,即使经过 SSH 也不算 authenticationFailed;
    • 其他失败是 failed。diagnostics 是引擎的提示;引擎什么都没打印时是 <engine> exited with status <code>。
  • 校验在任何程序启动前完成:
    • 容器名为空,返回 { field: "container", reason: "required" };超过 128 字节或不是合法的容器名,返回 invalid。
    • 用户超过 64 字节、以 - 开头,或含字母、数字、_.:- 以外的字符,返回 { field: "user", reason: "invalid" }。
    • 未知引擎或未知字段,返回 invalid_request。
    • 本机没有引擎 CLI,返回 runtime_missing。
  • 信任目标按顺序列出各跳。容器那一跳是 { container: { engine, program, container, user? } },所以信任决定不会带到别的容器或账号。
  • 连接日志的 transport 新增 container、ssh-container、wsl-container 三个值,不记容器名、镜像或用户。

remote.listContainers(新模块 workspace/src/containers.rs)

  • 请求是 { engine, sessionId? },结果是 { available, containers: [{ id, name, image, status }], diagnostics }。
  • 不带 sessionId 时在本机运行。带上时在该连接的主机上运行,用服务器的环境,和之后进入容器用的是同一种非登录环境,所以能列出来的就能连上。
  • 运行 <engine> ps --no-trunc --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}',限制为最多 15 秒、256 个容器、256 KiB 输出。只列出正在运行的容器,因为 Lithe 不会启动容器。
  • 字段数不对的行、id 或名字不合法的行、重复的 id 都跳过。image、status 去掉控制字符,最多 256 字节。
  • 引擎 CLI 启动不了时(本机 Failure::Start,主机上 SPAWN_FAILED 或退出码 127),返回 available: false。引擎运行后失败时(例如服务没启动),containers 为空,diagnostics 是脱敏后的提示,只给界面显示,不写日志。
  • 请求只接受这两个字段,不能指定程序。未知 sessionId 返回 connection_closed;取消和超时分别返回 cancelled 和 timed_out。什么都不保存。
  • host::capture 改为自己按 session_id 查连接,把真实的会话 id 写进请求,这样断开时的 forget 不受影响。

remote.devcontainerConfigs(新模块 workspace/src/devcontainers.rs、remote/src/client/devcontainer.rs)

  • 请求是 { folder },folder 是本机上的绝对路径;结果是 { configs: [{ path, name? }] }。
  • 按规范的查找位置和顺序列出普通文件:
    1. .devcontainer/devcontainer.json
    2. .devcontainer.json
    3. .devcontainer/<子目录>/devcontainer.json:按名字顺序,最多 64 个子目录,跳过以 . 开头的子目录。
  • path 相对文件夹,用 / 分隔。
  • name 是配置里的 name,按带注释和尾逗号的 JSON 读取,去掉控制字符,最多 128 字节。以下情况不返回 name:文件超过 1 MiB、解析后在文件夹外、读不了、没有非空字符串 name。
  • 相对路径或空的 folder 返回 invalid_request,不存在的文件夹返回 workspace_not_found。只读文件,不启动任何程序。

remote.devcontainerUp

  • 请求是 { folder, config?, engine, projectCodeAllowed },结果是 { container?, diagnostics }。
  • 构建会在本机运行文件夹里的代码(initializeCommand、Dockerfile、feature 脚本),所以 projectCodeAllowed 为 false 时什么都不启动,返回新错误码 devcontainer_untrusted_folder。客户端和 Git 一样,传入文件夹的信任决定。
  • config 只能是 remote.devcontainerConfigs 为这个文件夹列出的某个 path,否则返回 invalid_request。不带 config 时,由 CLI 自己在文件夹里查找。
  • 运行 devcontainer up --workspace-folder <folder> [--config <folder>/<config>];engine: "podman" 时在 up 后面加 --docker-path podman。在本机用 Core 自己的环境运行,最长一小时,可以取消。CLI 来自 @devcontainers/cli,Windows 上是 devcontainer.cmd。
  • 输出:
    • CLI 写到 stderr 的每一行,脱敏后作为 { type: "devcontainerOutput", text } 事件发出;
    • 单个事件最多 4096 字节,总共最多 2 MiB,超出后发一次 { type: "devcontainerOutput", truncated: true },之后的行丢弃;
    • 这些输出只给界面显示,不写日志。
  • 结果取 stdout 里最后一行带 outcome 的 JSON 对象:
    • success:container 是 { engine, container, containerId, user?, workspaceFolder }。
      • containerId 是 CLI 返回的 id。
      • container 是 15 秒内 <engine> inspect --format '{{.Name}}' 给出的名字(去掉 Docker 的前导 /),没有名字时用 id。
      • user 是配置的 remoteUser。
      • workspaceFolder 是要打开的 remoteWorkspaceFolder,必须是绝对路径。
      • 以下情况都返回 parse_failed:id 不是合法的容器名、exec 不能接受这个用户、文件夹不是绝对路径,以及成功退出但没有结果行。
    • error,或者退出失败且没有结果行:不返回 container;diagnostics 是 CLI 日志的末尾加上它的 message 和 description,已脱敏,用于显示。
  • 没有 CLI 返回 runtime_missing。其他字段返回 invalid_request(“Invalid dev container start request”),所以请求不能加挂载,也不能指定程序。取消和超时分别返回 cancelled 和 timed_out。什么都不保存。
  • Lithe 不解释配置,也不额外挂载目录或加参数(S32):挂载和 features 都按 devcontainer.json 由 CLI 处理。

remote 与 workspace 以外的改动

  • core:
    • protocol/command.rs 新增 remote.listContainers、remote.devcontainerConfigs 和 remote.devcontainerUp 三个命令;
    • runtime/dispatcher/remote.rs 把它们接到 lithe_workspace::containers 和 lithe_workspace::devcontainers;
    • tests/remote_sessions.rs 增加拒绝请求和在主机上列出的测试,以及 devcontainer 的未受信任、未知字段和相对路径。
  • protocol/src/error.rs:新增 ErrorCode::DevcontainerUntrustedFolder,只表示“文件夹未受信任,所以没有构建”,不和其他错误码共用。
  • diagnostics:重新生成 catalog_generated.rs,因为 logging/events/remote.json 的 transport 新增了三个值。
  • logging/events/remote.json:同上。
  • contracts/rust-core-api.md:命令表、remote.listContainers 段、remote.connect 的 container 段和拒绝规则,以及新的“Dev Containers”段。
  • contracts/application-boundary.md:错误码列表加了 devcontainer_untrusted_folder。

和 Core #160 的重叠

#160 也改了 workspace/src/sessions.rs:它在 ConnectRequest 加了 resume 字段,改了构建传输链的那一行。两个 PR 中先合入的那个不受影响;后合入的那个要在同一处把 chain_hops(...) 和 resume 合在一起,冲突很小。

验证

  • cargo fmt --all
  • cargo clippy -p lithe-remote -p lithe-workspace -p lithe-core --all-targets --features lithe-workspace/test-support -- -D warnings
  • cargo test -p lithe-remote -p lithe-workspace --features lithe-workspace/test-support:remote 142 个、workspace 79 个,全部通过。新增的测试覆盖:
    • 容器:
      • 本机容器经引擎 exec 进入,且不带 AskPass 变量;
      • 容器不存在时是 unreachable,并带引擎提示;
      • SSH 主机上套接字没有权限时是 failed;
      • WSL 里的容器跟在 --exec 后面;
      • 各字段的拒绝规则;
      • 本机列出、引擎失败、没有引擎。
    • devcontainer:
      • 配置的查找顺序;跳过名字像配置的文件夹;name 只读文件夹内的文件,按带注释和尾逗号的 JSON 读取;
      • 未受信任时不运行;config 不在列表里时拒绝;请求只接受规定的字段;
      • up 的参数:文件夹、配置,以及 Podman 的 --docker-path 放在 up 之后;
      • 成功结果带容器名、用户和文件夹;inspect 的名字去掉前导 /;没有名字时用 id;
      • CLI 报错时带日志末尾;没有 CLI 是 runtime_missing;成功但容器不可用是 parse_failed;
      • 构建日志(e1e0d10):跨两次读取的凭据按同一行脱敏;CRLF 不进入文本;超过 4096 字节的行分段发送;超过 2 MiB 后只发一次 truncated,之后的行不再发送。
  • cargo test -p lithe-core --lib:890 个全部通过,包括在主机上列出容器、拒绝请求,以及 devcontainer 的拒绝规则。
  • 合入 Core main ac80382(b92fcaf)。冲突在三个文件:
    • sessions.rs:保留 feat(remote): keep or end host programs when a connection closes (Refs 1lck/Lithe-IDEA#2231) #160 的常驻会话恢复重试,并接上本 PR 的跳点链(chain_hops);连接日志仍按 log_kind(container) 区分容器。
    • probe/tests.rs 和 tests/remote_sessions.rs:两边新加的测试都保留。
    • 合并后,cargo clippy -p lithe-workspace -p lithe-core -p lithe-remote --all-targets --all-features -- -D warnings 没有警告,cargo test -p lithe-workspace -p lithe-core -p lithe-remote 全部通过(lithe-core 892 个)。
  • 桌面端 feat(desktop): 远程开发窗口进入本机及 SSH/WSL 主机上的 Docker/Podman 容器,并从 devcontainer.json 构建打开 Dev Container (#2231) 1lck/Lithe-IDEA#2295 现在固定在 b92fcaf 上,测试结果见该 PR。

没有用真实的 Docker、Podman 或 Dev Container CLI 运行过;测试用的是替身程序。

remote.connect 新增 container:单独出现时由本机的 docker/podman exec 进入,
跟在 connection 或 wsl 后面时在那台主机登录后再 exec 进入。只进入正在运行的容器,
不启动、不创建、不改动容器,不挂载目录,不提升权限(S32)。信任目标包含容器,
日志 transport 只记 container / ssh-container / wsl-container,不记容器名。

新增 remote.listContainers:在本机或已连接会话的主机上运行 <engine> ps,
最多 15 秒、256 个容器、256 KiB 输出;没有引擎时 available=false,
引擎报错时返回脱敏后的提示,只显示、不写日志。
- 新增 remote.devcontainerConfigs:按规范位置列出本机文件夹的
  devcontainer.json(.devcontainer/devcontainer.json、.devcontainer.json、
  .devcontainer/<子目录>/devcontainer.json),按 JSONC 读取 name;
  只读文件夹内、不超过 1 MiB 的普通文件,不启动任何程序。
- 新增 remote.devcontainerUp:运行 devcontainer up --workspace-folder
  [--config],Podman 在 up 之后加 --docker-path podman;最长一小时,可取消。
  文件夹不受信任(projectCodeAllowed=false)时什么都不运行,返回新错误码
  devcontainer_untrusted_folder;config 只能是列出的路径;没有 CLI 为
  runtime_missing。
- stderr 日志按行以 devcontainerOutput 事件发给窗口,脱敏,单行 4 KiB、
  总共 2 MiB,之后发一次 truncated;只显示,不写日志。
- 结果读 stdout 最后一行 outcome JSON:成功时返回 engine、容器名(inspect
  得到,没有时用 id)、containerId、remoteUser 与 remoteWorkspaceFolder,
  供 remote.connect 的 container 进入;CLI 报错时返回脱敏的日志尾部和
  message/description。Lithe 不解释配置,不额外挂载或加参数(S32)。
- 契约文档与错误码列表同步更新。
- 补 Log 的单元测试:跨两次读取的凭据作为同一行脱敏,CRLF 不进入文本;
  超过 4096 字节的行分段发送,剩余部分在 CLI 退出时发出。
- 超过 2 MiB 后只发一次 truncated 事件,之后的行都不再发送。
@xiaoyumuxi xiaoyumuxi changed the title feat(remote): 进入 Docker/Podman 容器与列出运行中的容器 (#2231) feat(remote): 进入 Docker/Podman 容器、列出运行中的容器,并用 Dev Container CLI 从 devcontainer.json 启动 (#2231) Nov 21, 2026
Keep the container hops of the chain and the resume retry of the persistent sessions; the
connection log still tells a container apart.
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 22, 2026 01:16
@xiaoyumuxi
xiaoyumuxi merged commit 2e9740d into main Nov 22, 2026
8 of 10 checks passed
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