Skip to content
Open
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
7 changes: 6 additions & 1 deletion internal/engine/workspace_checkpoint_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions internal/engine/workspace_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"context"
"encoding/json"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion internal/runner/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions internal/runner/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}