From d1d96b59ac06b88e3a277d1ff55e7b4ffc881973 Mon Sep 17 00:00:00 2001 From: Kris <2040188027@qq.com> Date: Sat, 28 Nov 2026 12:03:10 +0800 Subject: [PATCH] fix(engine): reclaim terminal runs whose workspace disappeared MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A succeeded/cancelled run whose runner workspace is removed from the host (cleaner, disk work, manual delete) could never finish cleanup: the last checkpoint export returned 404/missing, the save failed, cleanupWorkspace returned early, WorkspaceCleanedAt was never written and the admission slot stayed held — with the sweep retrying every 20s forever (#1473). Extend the existing missing-workspace checkpoint degradation from idle SOP statuses to terminal statuses too, keeping the runner probe and checkpoint status double-confirmation so real RPC failures/offline runners still report. Also fill the runner's semantic Code (WorkspaceErrorMissing) on ErrNotExist so the platform can distinguish 'already gone' from other failures. Part of #1473; does not close the issue. --- .../engine/workspace_checkpoint_stream.go | 7 +- internal/engine/workspace_cleanup_test.go | 74 +++++++++++++++++++ internal/runner/workspace.go | 4 +- internal/runner/workspace_test.go | 18 +++++ 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/internal/engine/workspace_checkpoint_stream.go b/internal/engine/workspace_checkpoint_stream.go index 0972a96f3..657ffa320 100644 --- a/internal/engine/workspace_checkpoint_stream.go +++ b/internal/engine/workspace_checkpoint_stream.go @@ -135,7 +135,12 @@ func (e *Engine) downloadConversationCheckpoint(ctx context.Context, run domain. return err } if err = workspaceCheckpointResponseError(response); err != nil { - if errors.Is(err, os.ErrNotExist) && idleSOPStatus(run) && e.defaultExecutionRunner(run) != "" { + // A terminal run's workspace may be removed from the host (tmp cleaner, + // manual cleanup) before its last checkpoint is saved. That is an + // idempotent reclaim, not a permanent failure: the sweep would otherwise + // retry every 20s and never release the run's admission slot. + terminal := run.Status == "succeeded" || run.Status == "cancelled" + if errors.Is(err, os.ErrNotExist) && (idleSOPStatus(run) || terminal) && e.defaultExecutionRunner(run) != "" { // Export may also report a missing file inside the checkout. Only // the Runner's direct probe can confirm that the root is absent. meta, _ := e.repo.WorkspaceCheckpoint(run.ID) diff --git a/internal/engine/workspace_cleanup_test.go b/internal/engine/workspace_cleanup_test.go index 35fc1a904..2429abbdc 100644 --- a/internal/engine/workspace_cleanup_test.go +++ b/internal/engine/workspace_cleanup_test.go @@ -3,6 +3,7 @@ package engine import ( "context" "encoding/json" + "errors" "testing" "time" @@ -162,3 +163,76 @@ func TestCompletedConversationSavesBeforeReclamationEvenWhenNotArchived(t *testi t.Fatal("archived workspace was never reclaimed") } } + +// Regression for #1473: the runner workspace of a terminal run can disappear +// before the sweep saves its last checkpoint. That is an idempotent reclaim, +// not a permanent failure: finish cleanup, release the admission slot and let +// the sweep settle. Real RPC failures must still abort cleanup. +func TestTerminalRunnerCleanupTreatsMissingWorkspaceAsReclaimed(t *testing.T) { + t.Parallel() + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(store.Close) + e := New(store, runtime.NewManager(t.TempDir())) + runner := domain.Runner{ID: "missing-ws-runner", Name: "runner", Status: domain.RunnerStatusOffline} + if err := store.PutRunner(runner); err != nil { + t.Fatal(err) + } + run := domain.Run{ + ID: "missing-ws-run", Status: "succeeded", AdmissionState: "held", + ExecutionContext: domain.TelemetryContext{ExecutionMode: domain.ExecutionModeRunner, ExecutionRunnerID: runner.ID}, + Nodes: map[string]*domain.NodeRun{}, + } + if err := store.PutRun(run); err != nil { + t.Fatal(err) + } + missing := domain.RunnerWorkspaceResponse{Status: 404, Code: domain.WorkspaceErrorMissing, Error: "workspace directory missing"} + e.WorkspaceCheckpointTransport = func(_ context.Context, _ domain.Run, req domain.RunnerWorkspaceRequest) (domain.RunnerWorkspaceResponse, error) { + return missing, nil + } + e.reconcileTerminalWorkspaceCleanup() + + got, _ := store.RunState(run.ID) + if got.WorkspaceCleanedAt.IsZero() { + t.Fatal("missing runner workspace was not treated as reclaimed") + } + if got.AdmissionState != "released" { + t.Fatalf("admission slot stayed held: %q", got.AdmissionState) + } + if task, ok := store.RunnerTask("rt-cleanup-" + run.ID); !ok || task.Action != domain.RunnerTaskActionCleanupRun { + t.Fatalf("runner cleanup was not queued: %+v", task) + } +} + +func TestTerminalRunnerCleanupStillReportsRealTransportFailure(t *testing.T) { + t.Parallel() + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(store.Close) + e := New(store, runtime.NewManager(t.TempDir())) + runner := domain.Runner{ID: "failing-ws-runner", Name: "runner", Status: domain.RunnerStatusOffline} + if err := store.PutRunner(runner); err != nil { + t.Fatal(err) + } + run := domain.Run{ + ID: "failing-ws-run", Status: "cancelled", AdmissionState: "held", + ExecutionContext: domain.TelemetryContext{ExecutionMode: domain.ExecutionModeRunner, ExecutionRunnerID: runner.ID}, + Nodes: map[string]*domain.NodeRun{}, + } + if err := store.PutRun(run); err != nil { + t.Fatal(err) + } + e.WorkspaceCheckpointTransport = func(_ context.Context, _ domain.Run, _ domain.RunnerWorkspaceRequest) (domain.RunnerWorkspaceResponse, error) { + return domain.RunnerWorkspaceResponse{}, errors.New("runner offline") + } + e.reconcileTerminalWorkspaceCleanup() + + got, _ := store.RunState(run.ID) + if !got.WorkspaceCleanedAt.IsZero() || got.AdmissionState != "held" { + t.Fatalf("real transport failure was swallowed: cleaned=%v admission=%q", got.WorkspaceCleanedAt, got.AdmissionState) + } +} diff --git a/internal/runner/workspace.go b/internal/runner/workspace.go index d11adaef0..7f70f942b 100644 --- a/internal/runner/workspace.go +++ b/internal/runner/workspace.go @@ -439,10 +439,12 @@ func runnerWorkspacePayload(value any) domain.RunnerWorkspaceResponse { func runnerWorkspaceError(err error) domain.RunnerWorkspaceResponse { status := http.StatusBadRequest + code := "" if errors.Is(err, os.ErrNotExist) { status = http.StatusNotFound + code = domain.WorkspaceErrorMissing } - return domain.RunnerWorkspaceResponse{Error: err.Error(), Status: status} + return domain.RunnerWorkspaceResponse{Error: err.Error(), Status: status, Code: code} } func runnerResolveWorkspacePath(root, raw string, wantDir bool) (string, string, error) { diff --git a/internal/runner/workspace_test.go b/internal/runner/workspace_test.go index 960eed809..46b1443a7 100644 --- a/internal/runner/workspace_test.go +++ b/internal/runner/workspace_test.go @@ -228,3 +228,21 @@ func TestRunnerReadWorkspaceBaseFileForIntentToAdd(t *testing.T) { } } } + +// Regression for #1473: a vanished workspace must be reported with the +// semantic missing code so the platform can treat terminal cleanup as +// already done instead of retrying it forever. +func TestReadRunnerWorkspaceMissingRootReportsSemanticCode(t *testing.T) { + d := &Daemon{cfg: Config{DefaultWorkspaceRoot: t.TempDir()}} + req := domain.RunnerWorkspaceRequest{ + RunID: "gone", WorkspaceLayout: domain.RunnerWorkspaceLayoutShared, + Operation: domain.RunnerWorkspaceOperationInfo, + } + response := d.readRunnerWorkspace(context.Background(), req) + if response.Status != 404 { + t.Fatalf("missing root status = %d, want 404: %+v", response.Status, response) + } + if response.Code != domain.WorkspaceErrorMissing { + t.Fatalf("missing root code = %q, want %q: %+v", response.Code, domain.WorkspaceErrorMissing, response) + } +}