Skip to content

Commit a9bf8ab

Browse files
committed
merge: 合入 origin/main(#145、#146、#148、#149),解决端口转发与已保存连接、清单校验的冲突(#2231)
七个文件冲突,都是两边各自在同一列表末尾追加条目,按并集保留: - protocol::ErrorCode 与 application-boundary.md:保留 forward_target_not_confirmed、forward_limit_reached,再加 main 的 remote_server_invalid;connection_closed 用本分支补全的说明。 - RemoteCommand、命令名映射、分发器与 rust-core-api.md 方法表:remote.startForward、remote.stopForward、remote.forwards 之后接 remote.connections、remote.editConnections。 - workspace lib.rs:connections 与 forwards 两个模块都保留。 - sessions.rs 的 import:同时引入 ArtifactError 与 ForwardSet。 端口转发经已有连接的服务端协议完成,不另起 ssh 进程,与 #148 的环境变量白名单不交叉。
2 parents 6e6aca8 + 7901a86 commit a9bf8ab

35 files changed

Lines changed: 2496 additions & 399 deletions

‎rust/contracts/application-boundary.md‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,24 +381,37 @@ or remote failures. The shape is pinned by
381381

382382
## Error Codes
383383

384-
Use stable categories rather than platform error strings:
384+
Use stable categories rather than platform error strings. Each code has one
385+
meaning; `lithe_protocol::ErrorCode` documents when it is reported:
385386

386-
- `git_branch_not_merged`
387-
- `git_branch_changed`
388-
- `git_untrusted_workspace`: the workspace is not trusted, so Git did not start
389387
- `invalid_request`
390388
- `workspace_not_found`
391389
- `permission_denied`
392390
- `not_supported`
393391
- `runtime_missing`
394392
- `process_start_failed`
395393
- `process_failed`
394+
- `git_branch_exists`
395+
- `git_branch_not_merged`
396+
- `git_branch_changed`
397+
- `git_checkout_changed`
398+
- `git_authentication_failed`
399+
- `git_worktree_not_registered`
400+
- `git_worktree_primary`
401+
- `git_worktree_current`
402+
- `git_worktree_locked`
403+
- `git_worktree_has_changes`
404+
- `git_commit_author_invalid`
405+
- `git_untrusted_workspace`: the workspace is not trusted, so Git did not start
396406
- `parse_failed`
407+
- `encoding_unmappable`
397408
- `cancelled`
398409
- `timed_out`
399410
- `connection_closed`: the remote connection the request names closed or was lost
400411
- `forward_target_not_confirmed`: a port forward reaches another machine through the remote host and the user has not confirmed it
401412
- `forward_limit_reached`: the remote connection keeps the most port forwards it allows
413+
- `remote_server_invalid`: the server build `remote.connect` would send cannot
414+
be read or is not the build its SHA-256 names
402415
- `unknown`
403416

404417
GitHub authorization is independent of a Lithe account. Device Flow is the

‎rust/contracts/remote-workspace-protocol-v1.md‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ startup deadline. The lock is released when its owning descriptor closes; its
3030
inode is retained so concurrent waiters cannot lock different files. This keeps
3131
a redundant launcher from restarting an explicitly closed session.
3232

33+
When a chain starts with an SSH hop, the reference client starts `ssh` with
34+
only the local variables OpenSSH itself needs (home, user, shell, search
35+
path, temporary directory, agent socket, Kerberos and the Windows system
36+
variables), followed by its own AskPass variables. ssh sends the remote host
37+
every variable a `SendEnv` pattern names, many systems ship
38+
`SendEnv LANG LC_*`, and no command-line option can withdraw a pattern, so
39+
this is the only way to keep local locale, proxy and credential variables
40+
off the host; the server runs with the remote user's login environment.
41+
Other clients should do the same.
42+
3343
## Stream
3444

3545
1. The server writes the preamble `\nLITHE-REMOTE-PROTOCOL/1\n` once per
@@ -108,7 +118,7 @@ subscribe before creating the resource.
108118
| `server.ack` | `seq` | `null`; events up to `seq` may be forgotten |
109119
| `server.shutdown` | — | `null`; the session ends |
110120
| `server.ping` | — | `{}` |
111-
| `server.state` | — | running `processes`, `terminals`, `watches` |
121+
| `server.state` | — | running `processes` (`processId`, `pid`, `argv`, `label?`, `runningMs`, `leaderExited`), `terminals` (`terminalId`, `pid`, `argv`, `label?`, `runningMs`, `columns`, `rows`), `watches` |
112122
| `fs.stat` | `path` | metadata of the path itself (links not followed) |
113123
| `fs.readDir` | `path` | `entries` sorted by name byte order, `skippedNonUtf8` |
114124
| `fs.readFile` | `path`, `offset`, `length` (≤ 8 MiB) | `data`, `eof`, `metadata` |
@@ -131,11 +141,11 @@ subscribe before creating the resource.
131141
| `walk.cancel` | `walkId` | `null` |
132142
| `watch.start` | `watchId`, `root`, `excludeNames` | `watchedDirectories` (per-directory backends) |
133143
| `watch.stop` | `watchId` | `null` |
134-
| `process.spawn` | `processId`, `argv`, `cwd?`, `environment` (`server` / `login`), `env`, `pipeStdin` | `pid` |
144+
| `process.spawn` | `processId`, `argv`, `cwd?`, `environment` (`server` / `login`), `env`, `pipeStdin`, `label?` | `pid` |
135145
| `process.writeStdin` | `processId`, `data` | `null`; `BUSY` when the process stops reading |
136146
| `process.closeStdin` | `processId` | `null` |
137147
| `process.kill` | `processId`, `force` | `null`; signals every process of its session, also after `process.exit` while members run; `UNKNOWN_RESOURCE` once the session is empty |
138-
| `terminal.open` | `terminalId`, `argv?`, `cwd?`, `env`, `columns`, `rows` | `pid`; default program is the login shell |
148+
| `terminal.open` | `terminalId`, `argv?`, `cwd?`, `env`, `columns`, `rows`, `label?` | `pid`; default program is the login shell |
139149
| `terminal.write` | `terminalId`, `data` | `null`; `BUSY` when not read |
140150
| `terminal.resize` | `terminalId`, `columns`, `rows` | `null` |
141151
| `terminal.close` | `terminalId` | `null`; `SIGHUP` to its session, `SIGKILL` after 2 s |
@@ -215,6 +225,16 @@ its session until no member is left, so members that outlive the leader can
215225
still be killed and are cleaned up when the session ends; `server.state`
216226
marks such processes with `leaderExited`.
217227

228+
`server.state` lets a client list what still runs before it closes a window,
229+
and lets a later client list what an earlier one left running. `label`
230+
(`kind` ≤ 64 characters, `name` ≤ 256 characters, no control characters)
231+
is what the client shows for the resource; the server stores it unchanged,
232+
never interprets it, and refuses a label that breaks a rule with
233+
`INVALID_REQUEST` before starting anything. `runningMs` counts from the
234+
start on the server's monotonic clock, so it is right after a client
235+
restart and whatever the clients' clocks say. A terminal's `argv` is the
236+
program actually started: the requested `argv`, or the login shell's path.
237+
218238
When a session ends, the server terminates everything it still owns
219239
(`SIGTERM` or `SIGHUP`, then `SIGKILL` after 2 seconds to sessions that still
220240
have members), lets running requests finish for up to 5 seconds, and exits.
@@ -322,3 +342,9 @@ belong to the remote user and are not writable by the group or other users.
322342
On a host without a hash tool, a fresh upload is accepted when its size
323343
matches and `--identity` reports the expected hash, and it is uploaded again
324344
on every connection instead of being reused.
345+
346+
The expected hash is never computed from the file about to be sent: the
347+
application ships it in a manifest signed with the builds, and the
348+
bootstrapper reads the build once, compares it with that hash and sends the
349+
same bytes. A build that cannot be read or differs is refused before anything
350+
reaches the host (`remote_server_invalid` from `remote.connect`).

‎rust/contracts/rust-core-api.md‎

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,8 @@ or removing a command fails until the table changes in the same commit.
819819
| `remote.startForward` | Forward a local port to a port as the remote host sees it |
820820
| `remote.stopForward` | Stop one port forward and close its local port |
821821
| `remote.forwards` | List the running port forwards of one connection |
822+
| `remote.connections` | Read the SSH connections the user saved |
823+
| `remote.editConnections` | Save, remove or record a folder on one saved SSH connection |
822824
| `git.historyRewritePreview` | Review undo, message edit, squash, or drop with complete messages, eligibility, and an immutable checkout expectation |
823825
| `git.remotes` | Return configured fetch remote names and URLs |
824826
| `git.tags` | Return tag names, tag object IDs, target IDs, dates, and annotations |
@@ -5664,10 +5666,28 @@ place.
56645666

56655667
`remote.probe` checks a saved SSH connection before a remote workspace is
56665668
opened. The request is `{ connection: { host, port?, user?, authentication?,
5667-
identityFile?, connectTimeoutSeconds?, keepAliveSeconds? } }`; unknown fields,
5668-
including any password, are rejected. `authentication` is `auto` (default),
5669-
`agent`, `privateKey` (requires `identityFile`), or `password`. `host` is a
5670-
name, address, or SSH configuration alias without a user.
5669+
identityFile?, proxyJump?, connectTimeoutSeconds?, keepAliveSeconds? } }`;
5670+
unknown fields, including any password, are rejected. `authentication` is
5671+
`auto` (default), `agent`, `privateKey` (requires `identityFile`), or
5672+
`password`. `host` is a name, address, or SSH configuration alias without a
5673+
user. Text fields are trimmed and empty optional fields dropped first;
5674+
`identityFile` is dropped unless `authentication` is `privateKey`. Then:
5675+
5676+
| Field | Accepted |
5677+
| --- | --- |
5678+
| `host` | 1 to 1024 bytes of ASCII letters, digits and `._-:%`, not starting with `-` |
5679+
| `port` | 1 to 65535; absent keeps the SSH configuration's port |
5680+
| `user` | at most 1024 bytes of ASCII letters, digits and `._-`, starting with a letter, digit or `_` |
5681+
| `identityFile` | at most 1024 bytes, no control characters; quoted for OpenSSH when it has spaces |
5682+
| `proxyJump` | `none`, or comma-separated `[user@]host[:port]`; absent keeps the SSH configuration's jump hosts |
5683+
| `connectTimeoutSeconds` | 1 to 600; absent is 15 |
5684+
| `keepAliveSeconds` | 1 to 3600; absent keeps the transport's default |
5685+
5686+
A refused connection is `invalid_request` whose `details` is the JSON
5687+
`{ field, reason }`, where `reason` is `required`, `invalid` or `tooLong`.
5688+
Every field becomes an allowlisted SSH option; options that run a local
5689+
command, such as `ProxyCommand`, are only read from the user's own SSH
5690+
configuration.
56715691

56725692
Core runs the system `ssh` with the probe of
56735693
[`remote-workspace-protocol-v1`](remote-workspace-protocol-v1.md)'s bootstrap
@@ -5692,7 +5712,7 @@ The result is `{ status, host, diagnostics }`:
56925712
`host` is `{ system, machine, homeDirectory, serverTarget }` from `uname -s`,
56935713
`uname -m` and `$HOME`; `serverTarget` is the server's Rust target or `null`.
56945714
`diagnostics` is the redacted end of OpenSSH's standard error (at most
5695-
4 KiB). A malformed connection is `invalid_request`, a missing `ssh` is
5715+
4 KiB). A malformed connection is `invalid_request` as above, a missing `ssh` is
56965716
`runtime_missing`, and cancellation or the five-minute bound are `cancelled`
56975717
and `timed_out`.
56985718

@@ -5701,19 +5721,75 @@ and `timed_out`.
57015721
`diskTotalBytes` and `diskAvailableBytes` (the file system holding `$HOME`),
57025722
and `uptimeSeconds`.
57035723

5724+
#### Saved connections
5725+
5726+
The connection window lists the SSH connections the user saved. Native
5727+
clients keep them in one machine-local document, read it, and publish the
5728+
`document` an edit returns atomically, as for the trust decisions; Core
5729+
validates the document and applies every edit.
5730+
5731+
`remote.connections` accepts `{ document: string|null }` and returns
5732+
`{ connections, storage: { relativePath: "remote/connections.json",
5733+
maximumBytes: 262144 } }`. `connections` is the list in the user's order;
5734+
each entry is `{ id, name, connection, recentFolders? }`, where `connection`
5735+
is the object `remote.probe` takes and `recentFolders` holds at most 10
5736+
normalized absolute host paths, most recent first.
5737+
5738+
`remote.editConnections` accepts `{ document }` with exactly one of:
5739+
5740+
- `save: { id?, name, connection }` normalizes and checks the connection,
5741+
then adds an entry with a new id or replaces the entry `id` in place.
5742+
`name` is trimmed, 1 to 128 characters without control characters, and
5743+
unique among the entries regardless of case. Replacing an entry with
5744+
another `host`, `port`, `user` or `proxyJump` clears its recent folders,
5745+
which belong to the target and not to the name.
5746+
- `remove: { id }` removes the entry; an absent id changes nothing.
5747+
- `rememberFolder: { id, path }` moves `path` to the front of the entry's
5748+
recent folders and keeps 10.
5749+
5750+
The result is `{ changed, document: string|null, connections, id? }`; `id`
5751+
names the entry a save wrote. An unchanged edit returns a `null` document and
5752+
must not create a source. Requests never carry a secret: a password field is
5753+
an unknown field and is rejected.
5754+
5755+
Bounds: 256 KiB per document and 128 entries. A document that cannot be
5756+
decoded, has duplicate keys, unknown fields, another version, a malformed
5757+
entry, or an id or name used twice is `parse_failed`; a malformed request or a
5758+
refused edit is `invalid_request`. Both carry `details` as the JSON
5759+
`{ field, reason }`, where `reason` is `required`, `invalid`, `tooLong`,
5760+
`duplicate`, `full` (128 entries) or `unknown` (no entry has the id).
5761+
5762+
Native clients keep the document below their platform user-preference
5763+
directory, never on a host, in a workspace or in an installed package. This
5764+
user-settings resource does not affect code signatures or update deltas.
5765+
57045766
#### Remote connections that stay open
57055767

5706-
`remote.connect` takes `{ connection, servers: [{ target, path }],
5768+
`remote.connect` takes `{ connection, servers: [{ target, path, sha256 }],
57075769
serverVersion, installRoot? }`. `servers` are the read-only server builds the
5708-
host ships, by Rust target; `installRoot` defaults to `$HOME/.lithe-server`.
5709-
One OpenSSH process probes the host, keeps an installed server whose
5710-
`--identity` reports the same SHA-256 or uploads the matching build through
5711-
the same stream, and starts it, so the user authenticates once. Prompts are
5712-
relayed as for `remote.probe`. The result is `{ status, session, host,
5713-
diagnostics }`; `session` is `{ id, uploaded, hostName, userName,
5770+
host ships, by Rust target; `sha256` is the build's SHA-256 as 64 hexadecimal
5771+
digits, taken from the manifest the application ships and signs with the
5772+
builds, never computed from the file Core is about to send. `installRoot`
5773+
defaults to `$HOME/.lithe-server`. One OpenSSH process probes the host, then
5774+
Core reads the build for the host's platform once and compares it with its
5775+
`sha256`; the bytes it checked are the bytes it sends. The host keeps an
5776+
installed server only when its own `sha256sum` or `shasum` computes that
5777+
SHA-256; otherwise it receives the build through the same stream, checks it
5778+
the same way (a host with neither tool checks the size and `--identity`, and
5779+
never reuses that upload), and starts it, so the user authenticates once.
5780+
Prompts are relayed as for `remote.probe`. The result is `{ status, session,
5781+
host, diagnostics }`; `session` is `{ id, uploaded, hostName, userName,
57145782
homeDirectory }` and is set only for `connected`. A host without a matching
57155783
build is `unsupportedPlatform` and nothing is uploaded.
57165784

5785+
A malformed connection, an unknown `target` or a `sha256` that is not 64
5786+
hexadecimal digits is `invalid_request`, before OpenSSH starts. A missing
5787+
`ssh` is `runtime_missing`. When the build for the host's platform cannot be
5788+
read or its SHA-256 differs, the attempt ends as `remote_server_invalid`
5789+
without sending anything; `details` says which of the two happened and, for a
5790+
mismatch, the SHA-256 Core computed, but never the path. Cancellation and the
5791+
five-minute bound are `cancelled` and `timed_out`.
5792+
57175793
Core keeps an open connection until `remote.disconnect` (`{ sessionId }`,
57185794
returning `{ closed }`) or until the host process exits; ending it terminates
57195795
the OpenSSH process tree. `remote.closeAll` (no payload, returning
@@ -5730,8 +5806,8 @@ the remote server running. Requests over it take `sessionId`:
57305806
| `remote.hostInfo` | `{ sessionId }` | the original connection's handshake `host`, including OS, architecture, home and features |
57315807
| `remote.uploadFile` | `{ sessionId, sourcePath, path, sha256, mode }` | verified `{ size, sha256 }` |
57325808
| `remote.runWatch` | `{ sessionId, params: { root, excludeNames? } }` | waits until root removal or cancellation; relays `watch.started`, `watch.changed`, `watch.error` |
5733-
| `remote.runProcess` | `{ sessionId, params: { argv, cwd?, environment?, env?, pipeStdin? } }` | leader exit; relays `process.started`, `process.output`, `process.exit` |
5734-
| `remote.runTerminal` | `{ sessionId, params: { argv?, cwd?, env?, columns, rows } }` | leader exit; relays `terminal.started`, `terminal.output`, `terminal.exit` |
5809+
| `remote.runProcess` | `{ sessionId, params: { argv, cwd?, environment?, env?, pipeStdin?, label? } }` | leader exit; relays `process.started`, `process.output`, `process.exit` |
5810+
| `remote.runTerminal` | `{ sessionId, params: { argv?, cwd?, env?, columns, rows, label? } }` | leader exit; relays `terminal.started`, `terminal.output`, `terminal.exit` |
57355811
| `remote.captureProcess` | `{ sessionId, params, timeoutMilliseconds, maxOutputBytes }` | base64 `stdout`, `stderr`, nullable `exitCode` and `signal` |
57365812

57375813
`remote.writeFile` replaces the file atomically on the host. With
@@ -5742,7 +5818,9 @@ fails with `connection_closed` and is forgotten, so the host reconnects; an
57425818
unknown or closed `sessionId` is also `connection_closed`.
57435819

57445820
`remote.hostRequest` accepts finite filesystem, environment and port queries,
5745-
plus controls of existing processes and terminals. Creating a watch, process,
5821+
`server.state` (what still runs, with the `label` given at start, so the host
5822+
can ask before closing a window), plus controls of existing processes and
5823+
terminals. Creating a watch, process,
57465824
terminal, walk or tunnel through that command is `invalid_request`; creation
57475825
requires an owner that subscribes before sending the start request. Streamed
57485826
Core events use `{ type: "remote", event, params }`. Resource IDs are allocated

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ pub enum RemoteCommand {
10161016
StopForward,
10171017
/// Lists the running port forwards of one connection (`remote.forwards`).
10181018
Forwards,
1019+
/// Reads the SSH connections the user saved (`remote.connections`).
1020+
Connections,
1021+
/// Saves, removes or records a folder on one saved SSH connection
1022+
/// (`remote.editConnections`).
1023+
EditConnections,
10191024
}
10201025

10211026
#[derive(Debug, Clone)]
@@ -1583,6 +1588,8 @@ impl CoreCommand {
15831588
"remote.startForward" => Some(Self::Remote(RemoteCommand::StartForward)),
15841589
"remote.stopForward" => Some(Self::Remote(RemoteCommand::StopForward)),
15851590
"remote.forwards" => Some(Self::Remote(RemoteCommand::Forwards)),
1591+
"remote.connections" => Some(Self::Remote(RemoteCommand::Connections)),
1592+
"remote.editConnections" => Some(Self::Remote(RemoteCommand::EditConnections)),
15861593
"git.historyRewritePreview" => Some(Self::Git(GitCommand::HistoryRewritePreview)),
15871594
"git.remotes" => Some(Self::Git(GitCommand::Remotes)),
15881595
"git.tags" => Some(Self::Git(GitCommand::Tags)),

‎rust/crates/core/src/runtime/dispatcher/remote.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,36 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
203203
Err(error) => CoreResponse::failure(id, error),
204204
}
205205
}
206+
RemoteCommand::Connections => {
207+
match serde_json::from_value(payload)
208+
.map_err(|error| {
209+
CoreError::new(
210+
ErrorCode::InvalidRequest,
211+
"Invalid remote connections request",
212+
)
213+
.with_details(error.to_string())
214+
})
215+
.and_then(lithe_workspace::connections::list)
216+
{
217+
Ok(value) => CoreResponse::success(id, value),
218+
Err(error) => CoreResponse::failure(id, error),
219+
}
220+
}
221+
RemoteCommand::EditConnections => {
222+
match serde_json::from_value(payload)
223+
.map_err(|error| {
224+
CoreError::new(
225+
ErrorCode::InvalidRequest,
226+
"Invalid remote connections request",
227+
)
228+
.with_details(error.to_string())
229+
})
230+
.and_then(lithe_workspace::connections::edit)
231+
{
232+
Ok(value) => CoreResponse::success(id, value),
233+
Err(error) => CoreResponse::failure(id, error),
234+
}
235+
}
206236
RemoteCommand::Disconnect => {
207237
match serde_json::from_value::<lithe_workspace::sessions::DisconnectRequest>(payload) {
208238
Ok(request) => CoreResponse::success(

‎rust/crates/git/src/remote_process.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub(super) fn parameters(
212212
environment: EnvironmentSource::Login,
213213
env,
214214
pipe_stdin,
215+
label: None,
215216
})
216217
}
217218

@@ -278,6 +279,7 @@ fn remote_git_executable(
278279
environment: EnvironmentSource::Login,
279280
env: BTreeMap::new(),
280281
pipe_stdin: false,
282+
label: None,
281283
},
282284
None,
283285
&mut |event| {

‎rust/crates/lsp-transport/src/remote_process.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ pub(crate) fn launch(host: Host, spec: LspProcessSpec) -> Result<LspProcessStrea
474474
environment: EnvironmentSource::Login,
475475
env: spec.environment,
476476
pipe_stdin: true,
477+
label: None,
477478
};
478479
let pending = process
479480
.connection

0 commit comments

Comments
 (0)