Файли
openidea/docs/zh/contribute/architecture/remote.mdx
T
Deng Liuтаdengliu de868f1677 feat(remote): persist remote backend, terminals, runs, agents, and workbench state across window close (#4385)
Closing a remote window (SSH, WSL, Dev Containers) used to end openidea-server --stdio and tear down terminals, runs (running their stop commands), indexing and agent turns. openidea-server --attach now connects through a detached per-project daemon (private 0700 run dir, owner check, flock) that keeps them alive across disconnects; reopening reattaches terminals with bounded scrollback and OSC state, runs and agent turns, and restores remote editor tabs, tool windows and plugin views. The close dialog offers Keep Backend Running / Stop Backend; Stop Remote Backend shuts the daemon down on demand. A disconnected backend with no live work exits after an idle timeout (3 hours by default; presets, custom or Never in Settings › Tools › Remote Development, host override wins). The socket is keyed by project, so a new build replaces an idle backend of another build and reports a busy one; a window taken over by another shows that instead of a dropped connection. Clean documents are released on disconnect while dirty buffers keep the backend alive.

Also: a plugin-owned PTY keeps its exit hook across reattach (reattach only rebinds the forwarding sink), and a recent project clicked again while it is still opening joins that attempt instead of connecting twice (a double click made the second attach take over and fail the first).

Co-authored-by: dengliu <[email protected]>
2026-11-24 11:42:00 +08:00

110 рядки
10 KiB
Plaintext
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
---
title: "远程开发"
description: "桌面窗口如何处理另一台机器上的项目:openidea-server、经 SSH、WSL 或 docker exec 传输的 JSON-RPC 协议,以及前端的命令路由。"
---
远程开发时,窗口的项目位于另一台机器(SSH 主机、WSL 发行版或 dev container)。UI 留在桌面端;该项目的后端——与桌面端相同的 `ide-host` `Host`——以 `openidea-server` 的形式运行在远程机器上。使用方法见[通过 SSH 远程开发](/zh/guide/remote/ssh)。
<Note>
有两个名字里带 “remote” 的东西,它们不是一回事。**远程开发**是 `ide-host`(`rpc.rs`、`openidea-server` 二进制)加上
`src-tauri/src/remote_dev.rs` 里的客户端。**`ide-remote`** 是 SFTP 部署和运行目标(从本地项目出发,在 SSH 或 Docker
中运行某个配置)。
</Note>
## 组成
```mermaid
flowchart LR
subgraph Desktop["桌面端"]
UI["前端<br/>platform/remote.ts"] -- "remote_invoke" --> RD["src-tauri remote_dev.rs<br/>Connection"]
end
RD -- "JSON-RPC,Content-Length 分帧" --> T{"传输方式"}
T -- "SSH exec channel" --> S1["openidea-server --stdio"]
T -- "wsl.exe --exec" --> S2["openidea-server --stdio"]
T -- "docker exec -i" --> S3["openidea-server --stdio"]
S1 --> H["ide-host: Host<br/>项目、LSP、索引、git、<br/>终端、运行、调试器、插件"]
```
| 部分 | 位置 |
| ------------------------- | -------------------------------------------------------------------------- |
| 服务端二进制 | `crates/ide-host/src/bin/server.rs`(`openidea-server`) |
| 持久化守护进程与连接桥接 | `crates/ide-host/src/daemon.rs`(`openidea-server --attach` / `--daemon`) |
| 协议与 dispatcher | `crates/ide-host/src/rpc.rs` |
| 客户端:连接、安装、转发 | `src-tauri/src/remote_dev.rs` |
| WSL 与 dev container 传输 | `src-tauri/src/remote_wsl.rs`、`src-tauri/src/remote_container.rs` |
| 容器内的端口中继 | `crates/ide-host/src/relay.rs`(`openidea-server --connect`) |
| 前端路由 | `src/platform/remote.ts`,由 `src/platform/tauriCore.ts` 挂到 `invoke` 上 |
| UI(文件 › 远程开发…) | `src/features/remote-dev/` |
## 服务端
```text
openidea-server --stdio | --attach <project-path> | --mcp-stdio [--project <path>] | --connect <host> <port> | --version
```
- `--attach <project-path>` 将 stdin/stdout 桥接到独立守护进程,套接字权限为 `0600`,目录权限为 `0700`。套接字键不随构建变化;连接前会核对构建,只有在没有连接的窗口、活动任务或未保存的文档时才替换旧后端。空闲退出或主动关闭时会保留套接字,直到清理完成。
- `--stdio` 直接通过 stdin/stdout 为单个桌面客户端提供服务并在断开时退出。stdout 承载协议,所以日志写到 stderr(用 `OPENIDEA_LOG` 过滤)。
- 状态保存在 `OPENIDEA_SERVER_HOME`(默认 `~/.openidea`)下的 `config/` 和 `data/` 中。可以在 `config/languages/` 里追加语言定义。
- SSH exec channel 只给最小的环境变量,所以服务端每次启动都会读取登录 shell 的环境,并把 IDE 托管的工具目录加入 `PATH`。
- `--version` 打印应用版本和协议版本。
### 安装到远程机器
桌面端根据远程的操作系统和架构(来自 `uname -sm`)选择服务端二进制:先看是否设置了 `OPENIDEA_SERVER_BINARY`,平台相同时再用应用旁边自带的那个;否则使用该平台当前版本的 release 构建,只下载一次,放在 `<data dir>/server/<版本>/<os>-<arch>/`。离线时先用主机上已装的当前版本构建,再用不带版本的 `<data dir>/server/<os>-<arch>/openidea-server`。服务端在 `initialize` 中回报版本;版本不同的服务端缺少某个命令时,错误会写明两边的版本。之后把二进制上传到远程主机的 `~/.openidea/server/`,每个版本一个目录,按内容哈希区分,并删除同版本的其他构建。CI 会持续验证静态 Linux(musl)构建。
## 协议
基于服务端 stdio 的 JSON-RPC 2.0,使用 `Content-Length` 分帧(`ide-jsonrpc`):
| 消息 | 方向 | 内容 |
| -------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------- |
| 请求 `initialize` | 客户端 → 服务端 | `{ version }` → `ServerInfo { version, protocol, os, arch, home }` |
| 请求 `commands` | 客户端 → 服务端 | 服务端能处理的命令名列表 |
| 请求 `invoke` | 客户端 → 服务端 | `{ window, cmd, args }` → 命令结果。`args` 与桌面命令的参数相同(camelCase);流式参数写作 `{ "$channel": <id> }` |
| 通知 `event` | 服务端 → 客户端 | `{ window, event }`:发给该窗口的 `IdeEvent`,`window` 为 null 时发给所有窗口 |
| 通知 `channel` | 服务端 → 客户端 | `{ id, data }`:某个流式参数的一个值 |
| 通知 `plugin` | 服务端 → 客户端 | `{ window, event }`:远程项目中代码插件发出的事件 |
| 请求 `plugin_secret` | 服务端 → 客户端 | `{ plugin, name }` → `string` 或 `null`:从桌面端钥匙串取密钥 |
协议版本是 `rpc.rs` 中的 `PROTOCOL_VERSION`;桌面端会拒绝使用其他版本协议的服务端。已有命令的参数或结果形态改变时要修改它。二进制参数使用 `IpcBytes`(JSON 中是 base64 字符串,绑定中是 `string`),不要用 `Vec<u8>`——它会变成体积约 3.6 倍的数字数组。错误是以 JSON 形式放在 JSON-RPC 错误消息里的 `IdeError`,所以前端看到的 `kind` / `message` 与本地命令完全一样。
## 命令路由
前端并不知道窗口是不是远程的。各功能照常调用 `commands.*`,路由发生在生成的绑定之下:
1. `@tauri-apps/api/core` 通过 Vite 别名解析到 `src/platform/tauriCore.ts`,它的 `invoke` 会先询问已安装的路由。
2. 在远程窗口中,`src/platform/remote.ts` 安装一个路由:会话 `commands` 列表中的命令发给 `remote_invoke`,其余的在本地运行。
3. `remote_invoke` 把每个 `Channel` 参数替换成 `{ "$channel": id }`,以 `invoke` 请求转发,再把 `channel` 通知送回对应的 `Channel`。`event` 通知则通过普通的事件 sink 重新发给窗口。
少数命令即使服务端认识,也始终在本地运行:`open_project` 和 `close_project`(打开本地文件夹即离开远程项目;关闭会断开连接)、系统字体列表,以及客户端 UI 设置。客户端设置(keymap、外观、编辑器字体和配色等)列在 `src/platform/clientSettings.ts` 中;全局的工具类设置会同时作用于两端。
### 什么在哪里运行
| 远程机器上 | 桌面端 |
| -------------------------------------------- | ---------------------------- |
| 项目:文件、文档、设置 | 窗口、菜单、webview |
| 语言服务器、索引、搜索 | 客户端 UI 设置、keymap、主题 |
| Git、终端、运行、调试适配器、内置 MCP 服务器 | SSH 连接和端口转发 |
| 项目的代码插件及其进程和网络请求 | 插件密钥(钥匙串) |
## 端口转发
转发的端口走 SSH 连接。在 dev container 中,每个到转发端口的连接都会在容器内(通过 `docker exec`)运行 `openidea-server --connect localhost <port>`,并经由它的 stdin/stdout 转发数据,这样在任何 Docker 配置下都能以同样的方式访问容器网络。
## 信任
受限模式在远程窗口中同样生效:转发一个信任级别不是 `always` 的命令之前,桌面端会先查它自己对远程项目的信任决定,服务端还会对每个 `invoke` 用 `Host::authorize` 和命令声明的信任级别再校验一次。对于用户已经信任的文件夹所对应的 dev container,桌面端启动服务端时会把该项目目录标记为受信任(`OPENIDEA_TRUSTED_ROOT`,从不持久化)。
WSL 和容器两种传输方式都直接带参数运行程序(`wsl.exe --exec`、`docker exec`);只有固定脚本才会交给 `sh -c`,所有变量值都作为位置参数传递。
## 让命令支持远程
在 `ide-host::ops::<domain>` 的实现上用 `#[command(trust = …)]` 声明它,并把它加到该模块的 `domain!` 列表里(见 [UI 与 Rust 的通信](/zh/contribute/architecture/ipc))。服务端 `Dispatcher` 的处理函数和桌面端的路由都来自这一处声明。桌面端要路由的命令列表来自它自己的构建,而不是服务端,所以服务端无法把命令挪到桌面端执行。
用 `#[desktop_command]` 在 `src-tauri` 中声明的命令,在远程窗口中也在桌面端执行——对任何涉及项目的命令来说这都是错的。
## 测试
- `crates/ide-host/tests/rpc.rs`、`remote_env.rs`、`remote_plugins.rs`:协议和服务端行为。
- `smoke/server.ts` 启动真实的 `openidea-server --stdio` 并与之通信;`smoke/desktop.ts` 为前端模块提供一个 Tauri 运行时,其命令经远程开发的路由发给该服务端。运行和调试冒烟测试都基于它(见[冒烟测试](/zh/contribute/testing/smoke))。