package server import ( "context" "path/filepath" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) func TestRetryWaitsForExecutionCleanupEvenAfterTerminalStatus(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "cleanup.db")) if err != nil { t.Fatal(err) } defer store.Close() store.Submit(ctx, "workflow", pipeline.Template([]string{"one"}, true)) items, _ := store.List(ctx) d, _ := store.Get(ctx, items[0].ID, 1) r, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } id, _, err := store.CreateRun(ctx, d, "original", t.TempDir(), false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } if err := store.FinishRun(ctx, id, "failed", "error", "failure", nil); err != nil { t.Fatal(err) } // Simulate the interval between persisting terminal status and final cleanup. r.active[id] = activeRun{cancel: func() {}} detail, err := r.getRun(id) if err != nil || !detail.Executing { t.Fatal("terminal run not marked executing during cleanup", detail, err) } runs, err := r.listRuns() if err != nil || !runs[0].Executing { t.Fatal("list advertised stopped run during cleanup", runs, err) } if _, err := r.retry(protocol.Message{RunID: id, RequestKey: "retry"}); err == nil || !strings.Contains(err.Error(), "still executing") { t.Fatal("retry accepted during execution cleanup", err) } delete(r.active, id) r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil } if _, err := r.retry(protocol.Message{RunID: id, RequestKey: "retry"}); err != nil { t.Fatal("retry rejected after cleanup", err) } r.wg.Wait() }