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 2a0aba98c..224947099 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 @@ -168,6 +168,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, ShowTools(r)...) tools = append(tools, CancelTool(r.List, stdio.Ask), RerunTool(stdio.Ask)) tools = append(tools, VerbTools(stdio.Ask)...) tools = append(tools, TestRunsTools(stdio.Ask)...) 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 14b6c44c0..f3136b8e0 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 @@ -33,6 +33,10 @@ func TestTools(t *testing.T) { for _, tl := range FirstViewTools("the-runner", stdio.State(views.StatusState), nil, nil) { names = append(names, tl.Name) } + // design 47 H2 §789: a pipeline read in parts. + for _, tl := range ShowTools(pipeline.Reads{Pipelines: none{}, Kept: none{}}) { + names = append(names, tl.Name) + } names = append(names, CancelTool(nil, nil).Name, RerunTool(nil).Name) for _, tl := range VerbTools(nil) { names = append(names, tl.Name) @@ -49,7 +53,7 @@ func TestTools(t *testing.T) { for _, tl := range tools { 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_rerun runner_start runner_run runner_accept runner_place runner_end_again runner_unfinished_place runner_ask_again runner_health runner_waiting runner_unlogged runner_unlogged_append runner_unfinished runner_unfinished_drop" + 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_show runner_movers runner_cancel runner_rerun runner_start runner_run runner_accept runner_place runner_end_again runner_unfinished_place runner_ask_again runner_health runner_waiting runner_unlogged runner_unlogged_append runner_unfinished runner_unfinished_drop" 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/show.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/show.go new file mode 100644 index 000000000..07a332f1a --- /dev/null +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/show.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "strings" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-pipeline/internal/pipeline" +) + +// ShowTools are runner_show and runner_movers, a pipeline read in parts (design 47 H2 §789): show's filters applied while +// reading, at most 768 KB with capped, total and the next page's cursor; and one word per step of each mover. +func ShowTools(r pipeline.Reads) []stdio.Tool { + return []stdio.Tool{ + {Name: Prefix + "_show", + Description: "One pipeline in parts: its record with the steps the filters hold (a mover, a step kind, a step's " + + "outcome), at most 768 KB with capped, the filters' total and next, the cursor of the next page.", + Input: map[string]any{"type": "object", "properties": map[string]any{ + "pipeline": map[string]any{"type": "string", "description": "the pipeline's key"}, + "mover": map[string]any{"type": "string", "description": "only this mover's steps"}, + "step": map[string]any{"type": "string", "description": "only the steps of this kind (build, lint, unit, integration, validate, composition, review, approval)"}, + "state": map[string]any{"type": "string", "description": "only the steps whose outcome is this (passed, failed, running, could not run, ...)"}, + "next": map[string]any{"type": "string", "description": "the cursor the previous page answered"}, + }, "required": []string{"pipeline"}}, + Run: func(a map[string]any) (any, error) { + in, err := as[seat.ShowInput](a) + if err != nil { + return nil, err + } + return r.Show(in) + }}, + {Name: Prefix + "_movers", + Description: "Each mover of one pipeline with one word per step: passed, failed, running, waiting or not-run.", + Input: map[string]any{"type": "object", "properties": map[string]any{ + "pipeline": map[string]any{"type": "string", "description": "the pipeline's key"}, + }, "required": []string{"pipeline"}}, + Run: func(a map[string]any) (any, error) { + key, _ := a["pipeline"].(string) + if strings.TrimSpace(key) == "" { + return nil, fmt.Errorf("name the pipeline's key") + } + return r.Movers(key) + }}, + } +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/read.go b/modules/mesh-delivery-pipeline/internal/pipeline/read.go index fe5542471..a69a24595 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/read.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/read.go @@ -3,9 +3,11 @@ package pipeline import ( + "encoding/base64" "encoding/json" "fmt" "sort" + "strconv" "strings" "time" @@ -39,8 +41,12 @@ func (r Reads) fills() ([]seat.Fill, error) { return f, nil } -// Show is one pipeline whole, with its steps and the names of their outputs in step-outputs (§83), or, naming a mover, the -// pipeline's record with that mover's steps alone (H-A-187). +// MaxAnswer is the most a read answers (design 47 H2 §789, §811): 768 KB, the rest read from its cursor. +const MaxAnswer = 768 << 10 + +// Show is one pipeline, with its steps and the names of their outputs in step-outputs (§83), or, naming a mover, the +// pipeline's record with that mover's steps alone (H-A-187). Its filters mover, step and state apply while it reads, and it +// answers at most MaxAnswer with capped, the filters' total and the cursor of the next page (design 47 H2 §789, §779). func (r Reads) Show(in seat.ShowInput) (seat.ShowAnswer, error) { p, ok, err := r.pipeline(in.Pipeline) if err != nil { @@ -53,22 +59,130 @@ func (r Reads) Show(in seat.ShowInput) (seat.ShowAnswer, error) { if err != nil { return seat.ShowAnswer{}, err } - // Named, a mover is answered by its steps alone (H-A-187): one mover's of a pipeline of a hundred movers. - if m := strings.TrimSpace(in.Mover); m != "" { - if !Moves(p, m) { - return seat.ShowAnswer{}, fmt.Errorf("the pipeline %s does not move %s", in.Pipeline, m) - } - p.Steps = stepsOf(p, m) + from, err := pageFrom(in.Next) + if err != nil { + return seat.ShowAnswer{}, err } - a := seat.ShowAnswer{Pipeline: p, Fill: fills} + // Named, a mover is answered by its steps alone (H-A-187): one mover's of a pipeline of a hundred movers. + m := strings.TrimSpace(in.Mover) + if m != "" && !Moves(p, m) { + return seat.ShowAnswer{}, fmt.Errorf("the pipeline %s does not move %s", in.Pipeline, m) + } + held := []seat.StepResult{} for _, s := range p.Steps { - if s.Output != "" { - a.Outputs = append(a.Outputs, s.Output) + if (m == "" || s.Module == m) && (in.Step == "" || s.Step == in.Step) && (in.State == "" || s.Outcome == in.State) { + held = append(held, s) + } + } + p.Steps = []seat.StepResult{} + a := seat.ShowAnswer{Pipeline: p, Fill: fills, Total: int64(len(held))} + size := sizeOf(a) + for i := from; i < len(held); i++ { + n := sizeOf(held[i]) + 1 + if s := held[i]; s.Output != "" { + n += len(s.Output) + 3 + } + if size+n > MaxAnswer && len(a.Pipeline.Steps) > 0 { + a.Capped, a.Next = true, pageCursor(i) + break + } + size += n + a.Pipeline.Steps = append(a.Pipeline.Steps, held[i]) + if held[i].Output != "" { + a.Outputs = append(a.Outputs, held[i].Output) } } return a, nil } +// pageCursor is the opaque cursor of a page starting at the i-th step the filters hold (§779): the caller hands it back. +func pageCursor(i int) string { + return base64.RawURLEncoding.EncodeToString([]byte("step:" + strconv.Itoa(i))) +} + +// pageFrom is the step a cursor starts at, 0 for none; a cursor this reader did not give is refused. +func pageFrom(next string) (int, error) { + if next = strings.TrimSpace(next); next == "" { + return 0, nil + } + raw, err := base64.RawURLEncoding.DecodeString(next) + if err == nil { + if n, ok := strings.CutPrefix(string(raw), "step:"); ok { + if i, err := strconv.Atoi(n); err == nil && i >= 0 { + return i, nil + } + } + } + return 0, fmt.Errorf("the cursor %q is not one show answered", next) +} + +// sizeOf is v's size as JSON. +func sizeOf(v any) int { + b, _ := json.Marshal(v) + return len(b) +} + +// Movers is one word per step of each mover of a pipeline (design 47 H2 §789), in the record's movers' order: passed, +// failed, running, waiting or not-run. +func (r Reads) Movers(pipeline string) (MoversAnswer, error) { + p, ok, err := r.pipeline(pipeline) + if err != nil { + return MoversAnswer{}, err + } + if !ok { + return MoversAnswer{}, fmt.Errorf("no pipeline %q in %s", pipeline, seat.StatePipelines) + } + a := MoversAnswer{Pipeline: p.Pipeline, Outcome: p.Outcome, Movers: make([]MoverWords, 0, len(p.Movers))} + at := map[string]int{} + for _, m := range p.Movers { + at[m.Module] = len(a.Movers) + a.Movers = append(a.Movers, MoverWords{Module: m.Module, Steps: map[string]string{}}) + } + for _, s := range p.Steps { + i, ok := at[s.Module] + if !ok { + continue + } + name := s.Step + if s.Artifact != "" && s.Step != seat.StepResultStepValidate && s.Step != seat.StepResultStepComposition { + name += " " + s.Artifact + } + a.Movers[i].Steps[name] = StepWord(s.Outcome) + } + return a, nil +} + +// MoversAnswer is runner_movers' answer: a pipeline's movers, each with one word per step (design 47 H2 §789). +type MoversAnswer struct { + Pipeline string `json:"pipeline"` + Outcome string `json:"outcome"` + Movers []MoverWords `json:"movers"` +} + +// MoverWords is one mover's steps, each by its kind (and artifact where it has one) with one word. +type MoverWords struct { + Module string `json:"module"` + Steps map[string]string `json:"steps"` +} + +// StepWord is a step outcome's one word (design 47 H2 §789): passed for one that passed, was carried, accepted, bypassed +// or had nothing to run; failed for one that failed, could not run or did not build; running; waiting for one waiting, on +// a build node too; not-run for one not asked, declined, superseded or cancelled. +func StepWord(outcome string) string { + switch outcome { + case seat.StepResultOutcomePassed, seat.StepResultOutcomeCarried, seat.StepResultOutcomeNothingToRun, + seat.StepResultOutcomeAccepted, seat.StepResultOutcomeBypassed: + return "passed" + case seat.StepResultOutcomeFailed, seat.StepResultOutcomeCouldNotRun, seat.StepResultOutcomeDidNotBuild: + return "failed" + case seat.StepResultOutcomeRunning: + return "running" + case seat.StepResultOutcomeWaiting, seat.StepResultOutcomeWaitingForABuildNode: + return "waiting" + } + return "not-run" +} + // List is the pipelines and runs of a module, a branch, a commit or a run, newest started first, at most Limit when it // names one (§83): each one's record with its movers and steps counted, its steps empty (H-A-191). func (r Reads) List(in seat.ListInput) (seat.ListAnswer, error) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/read_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/read_test.go index 3b1ee9d71..3821a0101 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/read_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/read_test.go @@ -247,3 +247,70 @@ func TestStandsAndStepStartedNameWhatAReadingStepWaitsOn(t *testing.T) { t.Error("resume said no step-started") } } + +// TestAPipelineIsReadInParts holds show and runner_movers to design 47 H2 §789: one mover's steps of a record of 129 movers +// answer under 64 KB; the filters apply while reading; a capped answer continues from its cursor; runner_movers of 1,000 +// movers answers under 768 KB with one word per step. +func TestAPipelineIsReadInParts(t *testing.T) { + t0 := time.Date(2026, 11, 22, 1, 0, 0, 0, time.UTC) + record := func(n int) memKept { + pipes := memKept{} + p := seat.Pipeline{Pipeline: "main.9", Branch: "main", Source: 9, Outcome: seat.PipelineOutcomeRunning, Started: &t0, + Steps: []seat.StepResult{}} + for i := 0; i < n; i++ { + m := fmt.Sprintf("module-%04d", i) + p.Movers = append(p.Movers, seat.Mover{Module: m, Commit: "c9", Fingerprint: "src2:" + strings.Repeat("a", 64)}) + var steps []seat.StepResult + for _, k := range []string{seat.StepResultStepBuild, seat.StepResultStepLint, seat.StepResultStepUnit, + seat.StepResultStepIntegration, seat.StepResultStepValidate, seat.StepResultStepComposition} { + o := seat.StepResultOutcomePassed + if k == seat.StepResultStepUnit && i%10 == 3 { + o = seat.StepResultOutcomeFailed + } + steps = append(steps, seat.StepResult{Module: m, Step: k, Artifact: m + ":program", Status: "required", Outcome: o, + Ask: "ask-" + m + "-" + k, Started: &t0, Ended: &t0, Why: strings.Repeat("w", 200)}) + } + put(t, pipes, "steps.main.9."+m, seat.PipelinesByStepsPipelineMoverValue{Steps: steps}) + } + put(t, pipes, "main.9", p) + return pipes + } + r := Reads{Pipelines: record(129), Kept: memKept{}} + a, err := r.Show(seat.ShowInput{Pipeline: "main.9", Mover: "module-0042"}) + if raw, _ := json.Marshal(a); err != nil || len(raw) >= 64<<10 || len(a.Pipeline.Steps) != 6 || a.Total != 6 || a.Capped { + t.Fatalf("one mover of 129: %d bytes, %d steps, total %d, capped %v, %v", len(raw), len(a.Pipeline.Steps), a.Total, a.Capped, err) + } + a, err = r.Show(seat.ShowInput{Pipeline: "main.9", Step: seat.StepResultStepUnit, State: seat.StepResultOutcomeFailed}) + if err != nil || a.Total != 13 || len(a.Pipeline.Steps) != 13 || a.Pipeline.Steps[0].Module != "module-0003" { + t.Fatalf("the failed unit steps: total %d, %d steps, %v", a.Total, len(a.Pipeline.Steps), err) + } + // The whole record is larger than one answer: it is read in pages, each under 768 KB, every step once. + big := Reads{Pipelines: record(1000), Kept: memKept{}} + seen, pages, next := 0, 0, "" + for { + a, err := big.Show(seat.ShowInput{Pipeline: "main.9", Next: next}) + raw, _ := json.Marshal(a) + if err != nil || len(raw) > MaxAnswer || a.Total != 6000 { + t.Fatalf("page %d: %d bytes, total %d, %v", pages, len(raw), a.Total, err) + } + seen, pages = seen+len(a.Pipeline.Steps), pages+1 + if !a.Capped { + break + } + next = a.Next + } + if seen != 6000 || pages < 2 { + t.Fatalf("pages %d read %d steps, want every one of 6000 over more than one page", pages, seen) + } + if _, err := big.Show(seat.ShowInput{Pipeline: "main.9", Next: "not-a-cursor"}); err == nil { + t.Fatal("a cursor show never answered is refused") + } + m, err := big.Movers("main.9") + raw, _ := json.Marshal(m) + if err != nil || len(raw) >= MaxAnswer || len(m.Movers) != 1000 { + t.Fatalf("runner_movers of 1,000: %d bytes, %d movers, %v", len(raw), len(m.Movers), err) + } + if got := m.Movers[3].Steps["unit module-0003:program"] + " " + m.Movers[4].Steps["validate"]; got != "failed passed" { + t.Fatalf("words %q", got) + } +} diff --git a/modules/mesh-delivery-pipeline/manifest_test.go b/modules/mesh-delivery-pipeline/manifest_test.go index 49cc90e35..de97d07d5 100644 --- a/modules/mesh-delivery-pipeline/manifest_test.go +++ b/modules/mesh-delivery-pipeline/manifest_test.go @@ -119,7 +119,9 @@ func TestManifest(t *testing.T) { // RN25 (§539): the runner's tools carry the prefix runner_. {"first-view tools", equalSets(m.Tools, []string{"runner_first_views", "runner_first_view_again", "runner_health", "runner_unlogged", "runner_unlogged_append", "runner_start", "runner_run", "runner_accept", "runner_ask_again", "runner_place", "runner_end_again", "runner_waiting", - "runner_test_runs", "runner_test_runs_drop", "runner_unfinished", "runner_unfinished_drop", "runner_unfinished_place"})}, + "runner_test_runs", "runner_test_runs_drop", "runner_unfinished", "runner_unfinished_drop", "runner_unfinished_place", + // design 47 H2 §789: a pipeline read in parts. + "runner_show", "runner_movers"})}, // A-305 (ADR 0227 rule 8, ADR 0240 rule 2): the process is judged by its unit, as every process of mesh-delivery, // never by a tool of its own; runner_health stays a tool. {"health is the unit", healthTool(raw) == "unit"}, diff --git a/modules/mesh-delivery-pipeline/module.json b/modules/mesh-delivery-pipeline/module.json index fabd0a8b3..b5b28ce67 100644 --- a/modules/mesh-delivery-pipeline/module.json +++ b/modules/mesh-delivery-pipeline/module.json @@ -44,7 +44,9 @@ "runner_test_runs_drop", "runner_unfinished", "runner_unfinished_drop", - "runner_unfinished_place" + "runner_unfinished_place", + "runner_show", + "runner_movers" ], "consumes": [ "mesh-delivery-indexer.take-in.taken",