diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main.go index 5409cdef..eb5f47fb 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main.go @@ -151,7 +151,7 @@ func main() { tools := append(Tools(r), FirstViewTools(os.Getenv(stdio.ServedAs), stdio.State(views.StatusState), meshhandactsclient.Recorder{Ask: stdio.Ask}, time.Now)...) tools = append(tools, UnloggedTools(stdio.State(seat.StatePipelines), stdio.Log(seat.StateStepLog), meshhandactsclient.Recorder{Ask: stdio.Ask}, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-pipeline-tools] "+f+"\n", a...) })...) - tools = append(tools, CancelTool(r.List, stdio.Ask)) + tools = append(tools, CancelTool(r.List, stdio.Ask), RerunTool(stdio.Ask)) tools = append(tools, HealthTool(r.Pipelines, kept{stdio.State(pipeline.StateConsumers)}, time.Now)) if err := stdio.Serve("", tools); err != nil { fmt.Fprintf(os.Stderr, "[mesh-delivery-pipeline-tools] %v\n", err) diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go index 9283a657..6b708df0 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go @@ -31,14 +31,14 @@ func TestTools(t *testing.T) { for _, tl := range FirstViewTools("the-runner", stdio.State(views.StatusState), nil, nil) { names = append(names, tl.Name) } - names = append(names, CancelTool(nil, nil).Name) + names = append(names, CancelTool(nil, nil).Name, RerunTool(nil).Name) // RN25 (§468, §76; A-164): the health check is the bundle's runner_health. names = append(names, HealthTool(none{}, none{}, nil).Name) // §606, §549: the unlogged entries are listed and appended by hand. for _, tl := range UnloggedTools(stdio.State("pipelines"), stdio.Log("step-log"), nil, nil) { names = append(names, tl.Name) } - want := "mesh-pipeline-runner.show mesh-pipeline-runner.list mesh-pipeline-runner.stands mesh-pipeline-runner.why-carried mesh-pipeline-runner.kept runner_first_views runner_first_view_again runner_cancel runner_health runner_unlogged runner_unlogged_append" + want := "mesh-pipeline-runner.show mesh-pipeline-runner.list mesh-pipeline-runner.stands mesh-pipeline-runner.why-carried mesh-pipeline-runner.kept runner_first_views runner_first_view_again runner_cancel runner_rerun runner_health runner_unlogged runner_unlogged_append" if got := strings.Join(names, " "); got != want { t.Errorf("tools %q, want %q", got, want) } diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun.go new file mode 100644 index 00000000..4a43cb54 --- /dev/null +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun.go @@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "strings" + + "encoding/json" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// RerunTool is runner_rerun: the runner's seat verb rerun, reachable as a tool (design 47 G §482), as runner_cancel reaches +// cancel. It asks one step of one pipeline again over the bus; the verb records the hand act, refuses a running step and a +// cancelled or superseded pipeline, and runs a failed pipeline again. +func RerunTool(ask func(key string, body any) (json.RawMessage, error)) stdio.Tool { + return stdio.Tool{Name: Prefix + "_rerun", + Description: "Ask one step of a pipeline again by the seat verb rerun: the pipeline's key, the step (build, lint, unit, integration, composition, review), the artifact where the step is per artifact, and why. A failed pipeline runs again; a running step, or a cancelled or superseded pipeline, is refused by the verb, whole.", + Input: map[string]any{"type": "object", "properties": map[string]any{ + "pipeline": map[string]any{"type": "string"}, + "step": map[string]any{"type": "string"}, + "artifact": map[string]any{"type": "string"}, + "why": map[string]any{"type": "string"}, + }, "required": []string{"pipeline", "step", "why"}}, + Run: func(args map[string]any) (any, error) { + in, err := as[seat.RerunInput](args) + if err != nil { + return nil, err + } + return rerun(ask, in) + }} +} + +func rerun(ask func(string, any) (json.RawMessage, error), in seat.RerunInput) (any, error) { + if strings.TrimSpace(in.Why) == "" { + return nil, errors.New("rerun names why") + } + if in.Pipeline == "" || in.Step == "" { + return nil, errors.New("rerun names the pipeline and the step") + } + return ask("seat:"+seat.Seat+"."+seat.VerbRerun, in) +} diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun_test.go new file mode 100644 index 00000000..004596bd --- /dev/null +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/rerun_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "encoding/json" + "strings" + "testing" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" +) + +func TestRerunTool(t *testing.T) { + for _, c := range []struct { + name, refusal string + in seat.RerunInput + }{ + {"asks the verb", "", seat.RerunInput{Pipeline: "main.981.node-tools", Step: "composition", Why: "the composer was late"}}, + {"why missing", "names why", seat.RerunInput{Pipeline: "main.981.node-tools", Step: "composition"}}, + {"step missing", "names the pipeline and the step", seat.RerunInput{Pipeline: "main.981.node-tools", Why: "x"}}, + } { + t.Run(c.name, func(t *testing.T) { + var asked string + var body seat.RerunInput + ask := func(key string, b any) (json.RawMessage, error) { + asked, body = key, b.(seat.RerunInput) + return json.RawMessage(`{}`), nil + } + _, err := rerun(ask, c.in) + if c.refusal != "" { + if err == nil || !strings.Contains(err.Error(), c.refusal) || asked != "" { + t.Fatalf("want refusal %q and no ask, got %v (asked %q)", c.refusal, err, asked) + } + return + } + if err != nil || asked != "seat:mesh-pipeline-runner.rerun" || body != c.in { + t.Fatalf("asked %q with %+v, err %v", asked, body, err) + } + }) + } +}