2618f0d244
Track each active run's resolved workspace alongside its cancel function in a single runner.active struct map. A new request key is now refused only when an already-active run is changing the same resolved workspace; the refusal names the conflicting run id and advises waiting or using a different workspace. Workspace resolution is unchanged (default cwd, absolute request workspace, EvalSymlinks), so two spellings of one directory still compare equal. The conflict check and registration stay inside the existing r.mu critical section, so concurrent starts for one workspace cannot both pass. Idempotent repeats of an active request key and the restart-recovery guard are unchanged. Add concurrency_test.go covering concurrent different-workspace runs, same-workspace refusal, symlink/default-cwd equivalence, idempotent repeats, restart recovery and retry workspace scoping; strengthen the runner_test.go refusal assertion. Update docs/cli.md, docs/first-version-testing.md and the packaged operations skill reference to describe per-workspace concurrency. Assisted-by: DeepSeek V4.1 Flash
132 lines
4.1 KiB
Go
132 lines
4.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"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 TestRunnerRoutingPersistenceAndIdempotency(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "runs.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "test", pipeline.Template([]string{"one", "two"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
r, err := newRunner(ctx, store)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entered := make(chan struct{})
|
|
release := make(chan struct{})
|
|
r.execute = func(ctx context.Context, def stage.Definition, input stage.Values) (stage.Result, error) {
|
|
if def.Key == "one" {
|
|
if len(input) != 2 || input["prompt"] != "task" || input["pwd"] == "" {
|
|
t.Error("entry input must be the user prompt unchanged")
|
|
}
|
|
close(entered)
|
|
<-release
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"handoff": "evidence"}}, nil
|
|
}
|
|
if len(input) != 1 || input["handoff"] != "evidence" {
|
|
t.Error("handoff not propagated")
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"verified": true}}, nil
|
|
}
|
|
request := protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "same", Input: map[string]any{"prompt": "task"}}
|
|
run, err := r.start(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-entered
|
|
duplicate, err := r.start(request)
|
|
if err != nil || duplicate.ID != run.ID {
|
|
t.Fatal("active repeat was not idempotent", err)
|
|
}
|
|
other := request
|
|
other.RequestKey = "other"
|
|
if _, err := r.start(other); err == nil || !strings.Contains(err.Error(), fmt.Sprintf("run %d is already active", run.ID)) || !strings.Contains(err.Error(), "different workspace") {
|
|
t.Fatal("overlapping execution was not refused with the conflicting run", err)
|
|
}
|
|
close(release)
|
|
r.wg.Wait()
|
|
detail, err := store.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if detail.Status != "succeeded" || len(detail.Steps) != 2 || detail.Steps[1].Input["handoff"] != "evidence" || detail.Output["verified"] != true {
|
|
t.Fatalf("invalid persisted run: %+v", detail)
|
|
}
|
|
duplicate, err = r.start(request)
|
|
if err != nil || duplicate.ID != run.ID {
|
|
t.Fatal("finished repeat not idempotent")
|
|
}
|
|
request.RequestKey = "failure"
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{}, errors.New("provider failed")
|
|
}
|
|
failed, err := r.start(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
detail, _ = store.GetRun(ctx, failed.ID)
|
|
if detail.Status != "failed" || detail.Steps[0].Outcome != "error" || detail.Steps[0].Target != protocol.FailureEnd {
|
|
t.Fatal("execution error did not follow mandatory error edge")
|
|
}
|
|
request.RequestKey = "cancel"
|
|
r.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
|
|
<-ctx.Done()
|
|
return stage.Result{}, ctx.Err()
|
|
}
|
|
canceled, err := r.start(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.cancel(canceled.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
detail, _ = store.GetRun(ctx, canceled.ID)
|
|
if detail.Status != "canceled" {
|
|
t.Fatal("cancel not persisted")
|
|
}
|
|
request.RequestKey = "budget"
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
|
}
|
|
// A new version with a self-loop remains reachable to a terminal via error.
|
|
source := pipeline.Template([]string{"loop"}, true)
|
|
source = strings.Replace(source, "success -> #success", "success -> loop", 1)
|
|
if _, err := store.Submit(ctx, "loop", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ = store.List(ctx)
|
|
for _, p := range items {
|
|
if p.Name == "loop" {
|
|
request.PipelineID = p.ID
|
|
}
|
|
}
|
|
bounded, err := r.start(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
detail, _ = store.GetRun(ctx, bounded.ID)
|
|
if detail.Status != "failed" || len(detail.Steps) != 5 {
|
|
t.Fatalf("cycle not bounded: %+v", detail)
|
|
}
|
|
}
|