Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/read.go
T
jschoubben a755925024 R4, R5, R6, R8: one take-in is one pipeline, its movers placed, ended and said per source revision
A take-in.taken with entries starts one pipeline keyed <branch>.<source> by compare-and-set, its
movers the entries; each mover's steps are placed from its own registration and entry, each step
and ask id naming its mover; a step end is matched to its mover's commit and fingerprint; the
pipeline passes when every mover's required steps passed, a required "nothing to run" counting
as passed. The movers get tiers from the delivery edges (kinds 2, 4, 5, 6; a cycle one tier,
said). pipeline-started and pipeline-ended are said on <branch>.<source>, the end once with per
mover its fingerprint, version, steps and tier, in tier order; waits on <branch>.<source>; step
events on <module>.<branch>.<commit>; no group-ended. Approval is one step per mover. A run on
request is one pipeline keyed <branch>.run.<id>, its source the branch's newest, members of one
branch only. A running pipeline is superseded no more. A composition step names the runner as
its asker. The seat's verb place is served, refusing every pipeline until the queue (R11, R14).
The seat list part takes no callers (H-A-66). Unit tests 77 to 80, integration 3.

Builds: design 47 H §746, §747 (run, start, a publish's key), §748 (a running pipeline runs to its end), §764 (group-ended, the key per module); lead answers H-A-3, H-A-35, H-A-51, H-A-52, H-A-53, H-A-66, H-A-70, H-A-72, H-A-83 (1), H-A-85, H-A-88

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:31 +01:00

249 lines
7.6 KiB
Go

// Package pipeline is the pipeline runner's own logic, holder of the seat mesh-pipeline-runner (design 47 G §21). It names
// no module, branch or repository (§7): every name comes from what it reads.
package pipeline
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// Kept is one of the runner's old state sections as it reads them: every key, and one key's value (nil when absent).
type Kept interface {
Keys() ([]string, error)
Value(key string) (json.RawMessage, error)
}
// Reads answers the runner's read verbs (§83, §306) from its own states pipelines and kept, never another part's.
type Reads struct {
Pipelines Kept
Kept Kept
// Fill is each of the runner's stores' fill now, pipelines, step-outputs, kept and step-log, read through the bundle's calls
// (the state's `mesh/state.info`, the object store's and the log's fill; design 47 G §366, §623, A-214); nil says none.
Fill func() ([]seat.Fill, error)
}
// fills is the stores' fill, or an error saying it could not be read: an answer never leaves it out unsaid.
func (r Reads) fills() ([]seat.Fill, error) {
if r.Fill == nil {
return nil, nil
}
f, err := r.Fill()
if err != nil {
return nil, fmt.Errorf("the stores' fill cannot be read: %w", err)
}
return f, nil
}
// Show is one pipeline whole, with its steps and the names of their outputs in step-outputs (§83).
func (r Reads) Show(in seat.ShowInput) (seat.ShowAnswer, error) {
p, ok, err := r.pipeline(in.Pipeline)
if err != nil {
return seat.ShowAnswer{}, err
}
if !ok {
return seat.ShowAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
}
fills, err := r.fills()
if err != nil {
return seat.ShowAnswer{}, err
}
a := seat.ShowAnswer{Pipeline: p, Fill: fills}
for _, s := range p.Steps {
if s.Output != "" {
a.Outputs = append(a.Outputs, s.Output)
}
}
return a, nil
}
// 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).
func (r Reads) List(in seat.ListInput) (seat.ListAnswer, error) {
all, err := r.pipelines()
if err != nil {
return seat.ListAnswer{}, err
}
out := []seat.Pipeline{}
for _, p := range all {
if (in.Module == "" || Moves(p, in.Module)) && (in.Branch == "" || p.Branch == in.Branch) &&
(in.Commit == "" || movesCommit(p, in.Commit)) && (in.Run == "" || p.Run == in.Run) {
out = append(out, p)
}
}
sort.SliceStable(out, func(i, j int) bool { return startedOf(out[i]).After(startedOf(out[j])) })
if in.Limit > 0 && int64(len(out)) > in.Limit {
out = out[:in.Limit]
}
return seat.ListAnswer{Pipelines: out}, nil
}
// Stands is every step that has started and not ended, with what it waits on and since when (§75, §83): the watchman
// seeds its open waits from it.
func (r Reads) Stands() (seat.StandsAnswer, error) {
all, err := r.pipelines()
if err != nil {
return seat.StandsAnswer{}, err
}
out := []seat.Wait{}
for _, p := range all {
for _, s := range p.Steps {
if s.Ended != nil || s.Started == nil {
continue
}
if !open(s) && s.Outcome != seat.StepResultOutcomeWaiting {
continue
}
m, _ := MoverOf(p, s.Module)
out = append(out, seat.Wait{Pipeline: p.Pipeline, Module: m.Module, Branch: p.Branch, Commit: m.Commit,
Run: p.Run, Source: p.Source, Revision: p.Revision, Fingerprint: m.Fingerprint, Step: s.Step,
Artifact: s.Artifact, On: s.On, Since: *s.Started, V: 1, Ask: askOfWait(s), Shared: sharedOfWait(s)})
}
}
sort.SliceStable(out, func(i, j int) bool { return out[i].Since.Before(out[j].Since) })
return seat.StandsAnswer{Stands: out}, nil
}
// WhyCarried is where a step's result was carried from (§25, §83): the pipeline it names.
func (r Reads) WhyCarried(in seat.WhyCarriedInput) (seat.WhyCarriedAnswer, error) {
p, ok, err := r.pipeline(in.Pipeline)
if err != nil {
return seat.WhyCarriedAnswer{}, err
}
if !ok {
return seat.WhyCarriedAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
}
for _, s := range p.Steps {
if s.Step != in.Step || (in.Artifact != "" && s.Artifact != in.Artifact) {
continue
}
if s.Outcome != seat.StepResultOutcomeCarried {
return seat.WhyCarriedAnswer{}, fmt.Errorf("step %s of %s %s was not carried: it ended %q", s.Step,
in.Pipeline, s.Artifact, s.Outcome)
}
return seat.WhyCarriedAnswer{CarriedFrom: s.CarriedFrom}, nil
}
return seat.WhyCarriedAnswer{}, fmt.Errorf("pipeline %q has no step %s %s", in.Pipeline, in.Step, in.Artifact)
}
// KeptResults is the kept passing results, by level, digest or package (§83).
func (r Reads) KeptResults(in seat.KeptInput) (seat.KeptAnswer, error) {
keys, err := r.Kept.Keys()
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s cannot be read: %w", seat.StateKept, err)
}
sort.Strings(keys)
out := []seat.KeptResult{}
for _, k := range keys {
level, digest, ok := seat.ParseKeptByLevelDigestKey(k)
if !ok || (in.Level != "" && level != in.Level) || (in.Digest != "" && digest != in.Digest) {
continue
}
raw, err := r.Kept.Value(k)
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s %s cannot be read: %w", seat.StateKept, k, err)
}
if raw == nil {
continue
}
v, err := seat.DecodeKeptByLevelDigestValue(raw)
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s %s: %w", seat.StateKept, k, err)
}
if in.Package != "" && v.Package != in.Package {
continue
}
out = append(out, v)
}
fills, err := r.fills()
if err != nil {
return seat.KeptAnswer{}, err
}
a := seat.KeptAnswer{Kept: out}
for _, f := range fills {
if f.Store == seat.StateKept {
f := f
a.Fill = &f
}
}
return a, nil
}
func (r Reads) pipeline(key string) (seat.Pipeline, bool, error) {
key = strings.TrimSpace(key)
if key == "" {
return seat.Pipeline{}, false, fmt.Errorf("name the pipeline's key, <branch>.<revision>.<module>")
}
raw, err := r.Pipelines.Value(key)
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s %s cannot be read: %w", seat.StatePipelines, key, err)
}
if raw == nil {
return seat.Pipeline{}, false, nil
}
p, err := seat.DecodePipelinesByBranchSourceValue(raw)
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s %s: %w", seat.StatePipelines, key, err)
}
if p.Pipeline == "" {
p.Pipeline = key
}
return p, true, nil
}
func (r Reads) pipelines() ([]seat.Pipeline, error) {
keys, err := r.Pipelines.Keys()
if err != nil {
return nil, fmt.Errorf("%s cannot be read: %w", seat.StatePipelines, err)
}
sort.Strings(keys)
var out []seat.Pipeline
for _, k := range keys {
p, ok, err := r.pipeline(k)
if err != nil {
return nil, err
}
if ok {
out = append(out, p)
}
}
return out, nil
}
func startedOf(p seat.Pipeline) time.Time {
if p.Started == nil {
return time.Time{}
}
return *p.Started
}
// askOfWait is a waiting step's own ask (design 47 G §731, §496): a step reading another's test run keeps none (A-469m).
func askOfWait(s seat.StepResult) string { return s.Ask }
// sharedOfWait is the run a waiting step waits on, where the runner knows it (§731; A-469r, A-469v): a test step's run key
// and ask, as one entry with no machine and no asker.
func sharedOfWait(s seat.StepResult) []seat.Shared {
if len(s.Shared) == 0 {
return nil
}
out := make([]seat.Shared, 0, len(s.Shared))
for _, x := range s.Shared {
out = append(out, seat.Shared{Run: x.Run, Ask: x.Ask, Machine: x.Machine})
}
return out
}
// movesCommit says whether a mover of p is at commit.
func movesCommit(p seat.Pipeline, commit string) bool {
for _, m := range p.Movers {
if m.Commit == commit {
return true
}
}
return false
}