diff --git a/.agents/notes/implemented/bug-fix/2026-11-17-run-process-outlives-preparation-timeout.md b/.agents/notes/implemented/bug-fix/2026-11-17-run-process-outlives-preparation-timeout.md index 31c25486b3..985c78e4fa 100644 --- a/.agents/notes/implemented/bug-fix/2026-11-17-run-process-outlives-preparation-timeout.md +++ b/.agents/notes/implemented/bug-fix/2026-11-17-run-process-outlives-preparation-timeout.md @@ -47,8 +47,10 @@ a.background(p, 0, func(ctx context.Context) func(*App) { ## 验证 - `./scripts/desktop/desktop-go.sh test ./internal/workbench/ -run TestServiceRunOutlivesItsPreparationBound -count=1`:把准备时限缩短到 5 秒,服务运行 6 秒后写出标记文件。修复前服务在第 5 秒被结束(`signal: killed`),修复后通过。 +- #2154 的构建工具版本预检属于准备阶段:`probeBuildTool` 每次派生不超过 10 秒的子上下文,超时或用户停止时由 `buildprocess` 结束进程树;不得因工具已打印文本而忽略超时或非零退出状态。这不缩短运行中服务的生命周期。跨平台回归:`./scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestProbeBuildTool' -count=1`;真实 macOS/Windows 结果在 PR 中单独记录。 ## 适用范围 - `desktop/internal/workbench/build.go` +- `desktop/internal/workbench/build_probe_test.go` - `desktop/internal/workbench/build_connection_debug_test.go` diff --git a/desktop/internal/workbench/build.go b/desktop/internal/workbench/build.go index fd5fb53f41..04108c9149 100644 --- a/desktop/internal/workbench/build.go +++ b/desktop/internal/workbench/build.go @@ -1489,10 +1489,21 @@ func buildRequiresPresent(path string, required []string, root string) bool { return err == nil } +// A version probe is a short-lived preflight, not the build itself. It must not hold a +// model sync for the full buildRequestTimeout if the executable stalls. +const buildToolProbeTimeout = 10 * time.Second + func probeBuildTool(ctx context.Context, executable string, arguments []string, workingDirectory string, environment map[string]string) (string, error) { - output, waitErr := captureBuildProcess(ctx, executable, arguments, workingDirectory, environment, false, nil) - if output.combined == "" { - return "", waitErr + probeCtx, cancel := context.WithTimeout(ctx, buildToolProbeTimeout) + defer cancel() + output, waitErr := captureBuildProcess(probeCtx, executable, arguments, workingDirectory, environment, false, nil) + // The tool may have printed a version before hanging or failing. Output does not + // make a cancelled or nonzero-exit probe successful. + if err := probeCtx.Err(); err != nil { + return "", err + } + if waitErr != nil { + return "", withBuildOutput(waitErr, output.combined) } return output.combined, nil } diff --git a/desktop/internal/workbench/build_probe_test.go b/desktop/internal/workbench/build_probe_test.go new file mode 100644 index 0000000000..e5b90aaefe --- /dev/null +++ b/desktop/internal/workbench/build_probe_test.go @@ -0,0 +1,73 @@ +package workbench + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +const ( + buildProbeFixtureMode = "LITHE_BUILD_PROBE_FIXTURE_MODE" + buildProbeFixtureReady = "LITHE_BUILD_PROBE_FIXTURE_READY" +) + +// TestBuildToolProbeFixture is invoked by probeBuildTool in a child test binary. +func TestBuildToolProbeFixture(t *testing.T) { + switch os.Getenv(buildProbeFixtureMode) { + case "success": + fmt.Fprintln(os.Stdout, "synthetic tool 1.2.3") + case "failure": + fmt.Fprintln(os.Stderr, "invalid runtime root") + os.Exit(7) + case "hang": + if err := os.WriteFile(os.Getenv(buildProbeFixtureReady), []byte("started"), 0o600); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + fmt.Fprintln(os.Stdout, "version output before hanging") + <-time.After(30 * time.Second) + } +} + +func runBuildProbeFixture(t *testing.T, ctx context.Context, mode, readyFile string) (string, error) { + t.Helper() + executable, err := os.Executable() + if err != nil { + t.Fatal(err) + } + return probeBuildTool(ctx, executable, []string{"-test.run=^TestBuildToolProbeFixture$"}, t.TempDir(), map[string]string{ + buildProbeFixtureMode: mode, buildProbeFixtureReady: readyFile, + }) +} + +func TestProbeBuildToolReturnsVersionOnSuccess(t *testing.T) { + output, err := runBuildProbeFixture(t, context.Background(), "success", "") + if err != nil || !strings.Contains(output, "synthetic tool 1.2.3") { + t.Fatalf("success probe = %q, %v", output, err) + } +} + +func TestProbeBuildToolReportsNonzeroExitWithOutput(t *testing.T) { + output, err := runBuildProbeFixture(t, context.Background(), "failure", "") + if err == nil || !strings.Contains(err.Error(), "exit status 7") || !strings.Contains(err.Error(), "invalid runtime root") || output != "" { + t.Fatalf("failed probe = %q, %v; want the exit status and output", output, err) + } +} + +func TestProbeBuildToolTimeoutDoesNotAcceptPartialOutput(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + readyFile := filepath.Join(t.TempDir(), "ready") + output, err := runBuildProbeFixture(t, ctx, "hang", readyFile) + if _, statErr := os.Stat(readyFile); statErr != nil { + t.Fatalf("the probe fixture never started: %v", statErr) + } + if output != "" || !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("timed-out probe = %q, %v; want a deadline error, not partial version output", output, err) + } +} diff --git a/shared/platform-feature-matrix/features/build-model-sync-session.json b/shared/platform-feature-matrix/features/build-model-sync-session.json index be872bea31..e0ba55faad 100644 --- a/shared/platform-feature-matrix/features/build-model-sync-session.json +++ b/shared/platform-feature-matrix/features/build-model-sync-session.json @@ -5,13 +5,14 @@ "capability": "加载构建模型时,Run 窗口为每次加载开一个“同步 <构建系统>”执行标签,实时显示构建工具下载、解析依赖时打印的日志,带状态、开始时间、耗时、停止和再次运行;Build 窗口里正在加载的那一行显示最新一行输出,点击打开对应标签", "owner": "Run", "notes": "Viva 共享实现。命令来源的模型从 stdout 读取时只解码 stdout,stderr(如 go: downloading …)只进同步标签的控制台,不再混进模型文档;模型写到输出文件时两路输出都进控制台。Provider 来源把 lithe/build/progress 的 output 写进控制台、message 作为最新一行。同步标签不抢占正在运行的执行标签,也不主动把 Run 窗口调到前台;同名的已结束同步标签被新一次加载替换,标签数超出上限时先移除已结束的同步标签。用户停止后结果为“构建模型加载已停止”,Build 窗口不显示失败警告,也不会因文件变化自动重新加载,直到用户点 Reload 或再次运行。文案走 shared/i18n 的 desktop.build.syncSession*、desktop.build.modelStopped、desktop.build.showSyncOutput。", - "verification": "打开一个首次加载需要下载依赖的 Go 模块项目(清空模块缓存),在 Build 窗口点加载或 Reload:Run 窗口出现“同步 Go Modules”标签并显示 go: downloading 行,Build 窗口加载行显示最新一行输出,点击后 Run 窗口打开并选中该标签;加载结束后标签显示成功、开始时间和耗时,Build 树正常展开。再次加载时点停止:标签结果为已停止,Build 窗口不显示警告也不自动重试,点再次运行在同一个标签重新加载。正在运行一个任务时触发加载,确认 Run 窗口仍停在那个任务上。自动化:build_sync_test.go 的 TestBuildModelLoadShowsTheToolsLogInARunWindowSession、TestBuildModelLoadTheUserStopsEndsStoppedAndRerunsInItsTab,build_command_record_test.go 的 download 场景。", + "verification": "打开一个首次加载需要下载依赖的 Go 模块项目(清空模块缓存),在 Build 窗口点加载或 Reload:Run 窗口出现“同步 Go Modules”标签并显示 go: downloading 行,Build 窗口加载行显示最新一行输出,点击后 Run 窗口打开并选中该标签;加载结束后标签显示成功、开始时间和耗时,Build 树正常展开。再次加载时点停止:标签结果为已停止,Build 窗口不显示警告也不自动重试,点再次运行在同一个标签重新加载。正在运行一个任务时触发加载,确认 Run 窗口仍停在那个任务上。自动化:build_sync_test.go 的 TestBuildModelLoadShowsTheToolsLogInARunWindowSession、TestBuildModelLoadTheUserStopsEndsStoppedAndRerunsInItsTab,build_command_record_test.go 的 download 场景。 通用版本探测补充检查:scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestProbeBuildTool' -count=1;用会输出版本后不退出的合成可执行文件验证单次探测不超过 10 秒加进程停止缓冲、失败不会作为有效版本,并用退出码非零但带错误输出的合成工具核对错误尾部;macOS/Windows 原生待执行。", "macos": { "implementationStatus": "implemented", "verificationStatus": "pending", "evidence": [ "desktop/internal/workbench/build_sync.go", "desktop/internal/workbench/build.go", + "desktop/internal/workbench/build_probe_test.go", "desktop/internal/workbench/build_tree.go", "desktop/internal/workbench/build_tree_view.go", "desktop/internal/workbench/runs.go", @@ -25,6 +26,7 @@ "evidence": [ "desktop/internal/workbench/build_sync.go", "desktop/internal/workbench/build.go", + "desktop/internal/workbench/build_probe_test.go", "desktop/internal/workbench/build_tree.go", "desktop/internal/workbench/build_tree_view.go", "desktop/internal/workbench/runs.go",