Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/read.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

192 lines
5.9 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
}
// 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)
}
a := seat.ShowAnswer{Pipeline: p}
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 == "" || p.Module == in.Module) && (in.Branch == "" || p.Branch == in.Branch) &&
(in.Commit == "" || p.Commit == 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 s.Outcome != seat.StepResultOutcomeRunning && s.Outcome != seat.StepResultOutcomeWaiting {
continue
}
out = append(out, seat.Wait{Pipeline: p.Pipeline, Module: p.Module, Branch: p.Branch, Commit: p.Commit,
Group: p.Group, Run: p.Run, Source: p.Source, Revision: p.Revision, Fingerprint: p.Fingerprint, Step: s.Step,
Artifact: s.Artifact, On: s.On, Since: *s.Started, V: 1})
}
}
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)
}
return seat.KeptAnswer{Kept: out}, 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.DecodePipelinesByBranchRevisionModuleValue(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
}