Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

## 先说结论

本笔记覆盖 #2231 R1 的 #43a 和 #43b。远程工作区里,运行配置的读写、校验和启动计划由 Core 路由到主机(Core #161):配置文件经 `lithe_workspace::fs` 存在主机的项目目录,计划按主机平台(Linux、macOS)生成,从不生成批处理或 PowerShell。桌面端拿到计划后,用 `remote.runProcess` 在主机上启动内置的程序和脚本运行(`process.*`),输出进 Run 窗口,标签页带“主机”徽标。
本笔记覆盖 #2231 R1 的 #43a、#43b 和 #43c。远程工作区里,运行配置的读写、校验和启动计划由 Core 路由到主机(Core #161):配置文件经 `lithe_workspace::fs` 存在主机的项目目录,计划按主机平台(Linux、macOS)生成,从不生成批处理或 PowerShell。桌面端拿到计划后,用 `remote.runProcess` 在主机上启动内置的程序和脚本运行(`process.*`),输出进 Run 窗口,标签页带“主机”徽标。

配置“执行前”列表里引用的其他内置运行配置(#43b)同样在主机上按顺序运行,输出进同一个控制台。插件提供的执行前任务、构建工具、插件提供的运行和调试要由本机插件后端计划主机上的进程,属于 #2231 的 R3/R4,本阶段不做,各自说明原因(`desktop.run.remote.*`)。
配置“执行前”列表里引用的其他内置运行配置(#43b)同样在主机上按顺序运行,输出进同一个控制台。运行中连接断开时(#43c),主机保留会话就不结束运行:Run 窗口等窗口重新连接,接上同一个主机会话后经 `remote.attachProcess`(Core #167)从丢失的位置接着接收输出;关闭时保留在主机上的进程,下次连接可在 Run 窗口里接回。插件提供的执行前任务、构建工具、插件提供的运行和调试要由本机插件后端计划主机上的进程,属于 #2231 的 R3/R4,本阶段不做,各自说明原因(`desktop.run.remote.*`)。

## 问题

Expand All @@ -33,7 +33,7 @@
2. 工作目录是项目相对的 `/` 路径,`remoteRunDirectory` 拼到主机根下,拒绝 `..` 和绝对路径。环境用主机的登录环境,叠加计划的 `environment` 和配置的 `env`。
3. 输出经 `remoteRunOutput` 交给控制台:观察回调只追加到缓冲区,从不等待;独立的泵按顺序送出。缓冲区上限 8 MiB,超出时丢弃最旧的未送出部分,并在控制台写一行提示(`desktop.run.remote.outputDropped`)。
4. “停止”先用 `process.kill`(force 为 false)给进程组发 SIGTERM,超过停止等待时间(`buildprocess.StopGrace`)仍未结束就取消请求,由 Core 强制结束。再按一次“停止”立即强制结束。
5. 进程以非零退出码结束时运行失败,被信号结束时说明信号编号;连接断开时说明“无法确定是否仍在主机上运行”,并交给窗口重新连接(断线后接回输出是 #43c)。
5. 进程以非零退出码结束时运行失败,被信号结束时说明信号编号;连接断开时交给窗口重新连接:主机保留会话时运行继续,见下文“断线后接回(#43c)”;主机不保留会话,或断线时主机还没给出进程 id 时,说明“无法确定是否仍在主机上运行”。

主机上的进程 id 不是本机的,`Pid()` 返回 0,所以 Run 窗口不采样它的端口、CPU 和内存。主机端口由端口工具窗口列出。

Expand All @@ -45,9 +45,28 @@ Core 只允许“执行前”列表引用内置进程运行(`process.*`),

`failBeforeLaunch` 按 `ExitCode() int` 读退出码,本机的 `*exec.ExitError` 和主机的 `remoteRunExit` 都能说明“以退出码 N 结束”。确认询问(按指纹记住)与本机共用,主机步骤不另加询问。

### 断线后接回(#43c)

Core #167 在连接断开后把会话里进程的事件停放在主机上,带 `resume` 的重新连接接回同一个会话(`resumed=true`)后,`remote.attachProcess` 从这次连接还没收到的第一个事件开始转发:先发 `process.attached`(`truncated` 表示停放的事件超过上限、丢了一部分),再发之后的输出和退出。错误码一一对应:`connection_closed`(连接断开)、`remote_process_gone`(主机上没有这个进程了)、`remote_process_attached`(另一个连接正在接收它)。

`remoteRunHost.follow` 负责一次运行跨断线的接收:

1. 请求因连接断开结束、主机已给出进程 id、会话有主机会话键(主机保留会话)时,不结束运行:控制台另起一行写 `desktop.run.remote.waiting`,经 `remoteRunRejoin` 把等待交给窗口线程,等工作区的下一个会话。窗口线程在 `rejoinRemoteRuns` 里把新会话交给所有等待者;等待用带缓冲的通道,窗口线程不等运行。
2. 新会话的主机会话键相同时,`remoteProcess.move` 把停止、强制结束改走新会话,再用 `remote.attachProcess` 接收;`process.attached` 到达时先写 `desktop.run.remote.resumed`,`truncated` 时再写 `desktop.run.remote.outputLost`,然后才是之后的输出。可以断线多次,每次重复这一步。
3. 新会话的主机会话键不同(主机开了新会话),说明 `desktop.run.remote.sessionEnded` 结束运行:旧会话的进程已无法接收。工作区关闭时等待被关闭,运行按 `connectionLost` 结束,主机上的程序由关闭确认决定结束还是保留。接回被拒时分别说明 `processGone` 和 `attachedElsewhere`。
4. 等待期间用户按“停止”或项目关闭时,主机还不知道这次停止:`remoteRunAbandon` 把(主机会话键,进程 id)交给窗口,`endAbandonedRuns` 在接上同一个主机会话后立即强制结束这些进程;主机会话已变时直接忘掉,因为进程随旧会话结束。

控制台里窗口自己的说明用暗色另起一行(`remoteRunNotice`、`remoteRunLine`),不和进程没换行的输出接在一起。

### 保留进程的接回(#43c,设计 §9.2、S35)

关闭确认保留在主机上的进程,下次连接接回会话后列出(见关闭确认的笔记)。列表里有进程时,对话框除“结束这些进程”“继续运行”外还有“在运行窗口中接回”;列表里同时有终端时说明终端无法接回(终端没有可回放的输出)。选它时 `attachKeptPrograms` 为每个进程建一个运行,键为 `remote-attach:<进程 id>`(已有同键运行就跳过,所以重复选择不会开两个标签页),用 `launchRemoteAttachRun` 在 Run 窗口里接收,最后打开 Run 窗口。标签页带“主机”徽标,控制台先写 `desktop.run.remote.attachedKept`;“停止”与普通运行一样先发 SIGTERM。之后的断线同样按上一节接回。

### 输出里的路径

`flushOutputLinks` 在远程工作区用主机根作为工作区根、按 `linux` 平台匹配路径。点击时用 `remote.stat` 找第一个主机上存在的文件,经 `remoteplaceholder.LocalPath` 换成占位路径交给编辑器打开;工作区以外的路径落在占位根旁的 `.lithe-remote-host` 下,与“工作区以外的远端文件按 Zed 方式打开”一致。需要插件解析的链接拒绝,因为插件读的是本机文件。
`flushOutputLinks` 在远程工作区用主机根作为工作区根、按 `linux` 平台匹配路径。点击时用 `remote.stat` 找第一个主机上存在的文件,经 `remoteplaceholder.LocalPath` 换成占位路径交给编辑器打开;工作区以外的路径落在占位根旁的 `.lithe-remote-host` 下,与“工作区以外的远端文件按 Zed 方式打开”一致。需要插件解析的链接拒绝,因为插件读的是本机文件。接回的进程(#43c)用同一个控制台,输出里的主机路径同样可点击。

设计 §7.3 还要求测试报告和构建诊断里的主机路径映射成工作区文件。这两者都由插件提供的构建系统产生:测试结果经 `DecodeBuildTestResults` 按插件的清单和构建系统解码,构建诊断来自构建工具的输出。远程工作区在 R3 之前不探测构建系统,不运行插件任务,所以 R1 里没有测试报告和构建诊断。它们的路径映射随 R3 插件按主机路径计划进程一起做,由 Core 在报告解码时完成,桌面端不另写一份。

### 日志

Expand All @@ -60,19 +79,25 @@ Core 只允许“执行前”列表引用内置进程运行(`process.*`),
- **远程工作区也探测构建系统**:探测在本机占位目录里运行,结果永远为空,还会误导用户以为项目没有构建工具。
- **远程工作区拒绝所有执行前步骤**(#43a 的做法):配置步骤只是另一个内置运行,主机已经能运行它;一起拒绝会让常见的“先编译再运行”脚本链用不了。
- **把主机步骤的请求直接挂在准备上下文上**:停止时 Core 立即强杀,主机程序没有机会清理,与本机步骤的停止行为不一致。
- **断线时结束运行,让用户重新运行**:主机上的程序还在跑,重新运行会起第二份进程,端口和文件可能冲突;设计 §9.3 要求从上次的位置接着输出。
- **断线等待期间的“停止”只在本机结束运行**:主机上的进程会一直留在会话里,只能在下次关闭时才被发现;所以接上同一个主机会话后立即强制结束。
- **接回保留进程时自动打开所有标签页**:用户可能只想结束或让它们继续运行,接回由用户在对话框里选择。

## 后果

- 远程工作区里能直接运行项目自带的程序和脚本配置,输出、停止和退出码与本机一致;Run 窗口的“主机”徽标说明它在哪里运行。
- “执行前”列表里的内置运行配置在主机上按顺序运行,失败、停止和超时与本机一致。
- 插件提供的执行前任务、构建工具、插件运行、调试和“当前文件”暂时不可用,各自给出说明;它们依赖 #2231 的 R3(插件按主机路径计划进程)和 R4(远程调试),本阶段不做。
- 断线时运行结束为“无法确定是否仍在主机上运行”,主机上的进程可能还在;断线后接回输出由 #43c 负责。
- 主机保留会话时,断线不结束运行;重新连接到同一个主机会话后接着输出,丢了部分输出时明确说明。主机开了新会话时,旧会话里的进程无法接收,运行结束并说明原因。
- 关闭时保留的进程可以在下次连接时接回 Run 窗口,终端不能接回。
- 主机临时目录里可能残留断线或崩溃时没删掉的 `lithe-run-script-*` 目录,由主机的临时目录清理回收。

## 验证

- `desktop/internal/workbench/remote_run_test.go`:主机上的启动参数、工作目录、环境、脚本写入和删除;退出码和信号;“停止”先发 SIGTERM;执行前的配置步骤在主机上依次运行、失败时不启动配置、停止时先发 SIGTERM;插件任务步骤、工具链和调试的拒绝;输出缓冲丢弃最旧部分;输出链接的主机文件查找;不记主机和路径。
- `desktop/internal/core/remote_process_test.go`:`remote.runProcess` 事件的解析。
- `desktop/internal/workbench/remote_run_test.go`:主机上的启动参数、工作目录、环境、脚本写入和删除;退出码和信号;“停止”先发 SIGTERM;执行前的配置步骤在主机上依次运行、失败时不启动配置、停止时先发 SIGTERM;插件任务步骤、工具链和调试的拒绝;输出缓冲丢弃最旧部分;输出链接的主机文件查找;不记主机和路径;断线后等待、接上同一个主机会话后接着输出并说明丢失(`TestRemoteWorkspaceRunFollowsItsProcessAcrossAReconnection`);新会话、进程已不在、被别处接收时结束(`TestRemoteWorkspaceRunEndsWhenItsProcessCannotBeReceivedAgain`);等待期间停止的运行在重连后强制结束主机进程(`TestRemoteWorkspaceRunStoppedWhileDisconnectedEndsOnceReconnected`)。
- `desktop/internal/workbench/remote_close_test.go`:“在运行窗口中接回”为每个保留的进程开 Run 标签页并打开 Run 窗口。
- `TestVisualRemoteRun`、`TestVisualRemoteClose`:断线等待、重连后接着输出、接回保留进程的无头截图(`docs/assets/remote-run/`、`docs/assets/remote-close/`)。
- `desktop/internal/core/remote_process_test.go`:`remote.runProcess`、`remote.attachProcess` 事件的解析。
- `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:运行时不写发行目录。

## 适用范围
Expand Down
7 changes: 7 additions & 0 deletions desktop/internal/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ const (
// CodeDevcontainerUntrustedFolder means remote.devcontainerUp named a folder the user has not
// trusted; the Dev Container CLI did not run, because building runs the folder's code here.
CodeDevcontainerUntrustedFolder ErrorCode = "devcontainer_untrusted_folder"
// CodeRemoteProcessGone means remote.attachProcess found no host process to receive: the
// connection resumed no session, the process ended before what this Core received, or an
// earlier attach already received it to its end.
CodeRemoteProcessGone ErrorCode = "remote_process_gone"
// CodeRemoteProcessAttached means another remote.attachProcess request already receives the
// host process.
CodeRemoteProcessAttached ErrorCode = "remote_process_attached"
// CodeUnknown means the failure fits no more specific category.
CodeUnknown ErrorCode = "unknown"
)
Expand Down
50 changes: 40 additions & 10 deletions desktop/internal/core/remote_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ type RemoteProcessOpen struct {
// their login shell sets up, so tools installed through the profile are on the PATH.
const RemoteProcessEnvironmentLogin = "login"

// RemoteProcessEvents receive a host process's events. Both run on the request's goroutine and
// RemoteProcessEvents receive a host process's events. They run on the request's goroutine and
// must return at once: they must not wait for a UI thread. Core holds 1024 undelivered events
// per process and ends the process, and with it the request, when a subscriber stops reading.
type RemoteProcessEvents struct {
// Started is called once, before any output, with the id process.kill takes and the
// process's id on the host.
// process's id on the host. remote.attachProcess does not call it.
Started func(processID uint64, pid int)
// Attached is called once, before any output, when remote.attachProcess receives the process
// again. truncated means some of what the process wrote while nobody received it is missing.
Attached func(truncated bool)
// Output receives the bytes the program wrote to stream, "stdout" or "stderr", in order.
// Output owns data.
Output func(stream string, data []byte)
Expand All @@ -50,7 +53,7 @@ type RemoteProcessExit struct {
Signal *int `json:"signal"`
}

// remoteProcessEvent is one remote event of remote.runProcess.
// remoteProcessEvent is one remote event of remote.runProcess or remote.attachProcess.
type remoteProcessEvent struct {
Type string `json:"type"`
Event string `json:"event"`
Expand All @@ -59,10 +62,12 @@ type remoteProcessEvent struct {
PID *int `json:"pid"`
Stream string `json:"stream"`
Data string `json:"data"`
Truncated bool `json:"truncated"`
} `json:"params"`
}

// errRemoteProcessEvent is an event of remote.runProcess the client cannot read.
// errRemoteProcessEvent is an event of remote.runProcess or remote.attachProcess the client
// cannot read.
var errRemoteProcessEvent = errors.New("core: unreadable remote process event")

// RemoteRunProcess runs a process on the host of sessionID until its leader exits, and returns
Expand All @@ -73,6 +78,30 @@ var errRemoteProcessEvent = errors.New("core: unreadable remote process event")
// the client cannot read ends the process too and is returned as the error, since its output
// would be incomplete.
func (c *Client) RemoteRunProcess(ctx context.Context, sessionID string, open RemoteProcessOpen, events RemoteProcessEvents) (RemoteProcessExit, error) {
return c.receiveRemoteProcess(ctx, "remote.runProcess", struct {
SessionID string `json:"sessionId"`
Params RemoteProcessOpen `json:"params"`
}{sessionID, open}, events)
}

// RemoteAttachProcess receives again the host process processID that an earlier connection to
// the host session started, after sessionID resumed that session (design §9.3): first Attached,
// then what the process wrote meanwhile and after, until its leader exits, and returns how it
// ended. The request then owns the process as RemoteRunProcess's does: ending ctx kills it. A
// broken or unknown session fails with CodeConnectionClosed, a process nothing can receive any
// more with CodeRemoteProcessGone, and one another request receives with
// CodeRemoteProcessAttached.
func (c *Client) RemoteAttachProcess(ctx context.Context, sessionID string, processID uint64, events RemoteProcessEvents) (RemoteProcessExit, error) {
return c.receiveRemoteProcess(ctx, "remote.attachProcess", struct {
SessionID string `json:"sessionId"`
ProcessID uint64 `json:"processId"`
}{sessionID, processID}, events)
}

// receiveRemoteProcess runs command, which relays one host process's events until its leader
// exits, and returns the exit. An event the client cannot read cancels the request, which ends
// the process, and is returned as the error.
func (c *Client) receiveRemoteProcess(ctx context.Context, command string, payload any, events RemoteProcessEvents) (RemoteProcessExit, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var unreadable error
Expand All @@ -87,10 +116,7 @@ func (c *Client) RemoteRunProcess(ctx context.Context, sessionID string, open Re
}
}
var exit RemoteProcessExit
observationErr, err := c.ExecuteWithEvents(ctx, "remote.runProcess", struct {
SessionID string `json:"sessionId"`
Params RemoteProcessOpen `json:"params"`
}{sessionID, open}, &exit, observer)
observationErr, err := c.ExecuteWithEvents(ctx, command, payload, &exit, observer)
switch {
case unreadable != nil:
return RemoteProcessExit{}, unreadable
Expand All @@ -102,8 +128,8 @@ func (c *Client) RemoteRunProcess(ctx context.Context, sessionID string, open Re
return exit, nil
}

// deliverRemoteProcessEvent hands one event of remote.runProcess to events. Events of other kinds
// are not the process's and are ignored.
// deliverRemoteProcessEvent hands one event of remote.runProcess or remote.attachProcess to
// events. Events of other kinds are not the process's and are ignored.
func deliverRemoteProcessEvent(raw json.RawMessage, events RemoteProcessEvents) error {
if !bytes.Contains(raw, []byte(`"remote"`)) {
return nil
Expand All @@ -121,6 +147,10 @@ func deliverRemoteProcessEvent(raw json.RawMessage, events RemoteProcessEvents)
}
events.Started(event.Params.ProcessID, pid)
}
case "process.attached":
if events.Attached != nil {
events.Attached(event.Params.Truncated)
}
case "process.output":
if event.Params.Stream != "stdout" && event.Params.Stream != "stderr" {
return errRemoteProcessEvent
Expand Down
Loading
Loading