Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/widens.go
T
jschoubben a8314676c9 RN10 (widens): a pipeline's end names what its change widens, from the catalog's revisions and the grants part
The candidate (the pipeline's own revision) against the branch's newest through mesh-catalog's revision; serving parts by the holder of the runner's, catalog's, steps' and composer's seats; acting rules, credentials; grants through grants-for. A read that fails says could not run. Touches steps.go (Hearer.Widens field, say's pipeline-ended), main.go (hearer.Widens), module.json invokes.

Builds: design 47 G §71, §275, §335, §441, A-207, A-215, A-220

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 06:11:47 +01:00

180 lines
7.1 KiB
Go

package pipeline
import (
"fmt"
"sort"
"time"
"git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshgrants"
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// ComposerSeat is the seat of the composer, the node-engine's mesh-compose (design 47 G A-167, A-220): a part serving a step.
const ComposerSeat = "node-engine"
// Widener computes a pipeline's `widens` (design 47 G §71, §275, §335, §441, A-207, A-215): the pipeline's own revision, the
// candidate, against the branch's newest revision, both read through the catalog seat's `revision`, and the permissions from the
// grants part's `grants-for`; no other verb. A widening is said, never refused: a read that fails says `could not run`.
type Widener struct {
// Revision reads one catalog revision of a watched branch whole; revision 0 is the branch's newest.
Revision func(branch string, revision int64) (Half, error)
// Holder is the module that holds a seat, from the seat holders' one reading; an error when it cannot be read or none holds it.
Holder func(seat string) (string, error)
// GrantsFor asks the seat mesh-grants' verb grants-for for the candidate's source revision on the branch.
GrantsFor func(branch string, source int64) (meshgrants.GrantsFor, error)
}
// Widens names each part serving a step the change alters (the holders of the runner's seat, the catalog's, each of the pipeline's
// steps' and the composer's, whose entry's fingerprint moved), each acting rule of the pipeline's module that is gained, matches
// more or is held longer, each own secret or credential provision it names that it did not, and each grant the grants part adds.
func (w *Widener) Widens(p seat.Pipeline) []seat.Widening {
if w == nil || w.Revision == nil {
return nil
}
could := func(name, said string) []seat.Widening {
return []seat.Widening{{What: seat.WideningWhatCouldNotRun, Name: name, Said: said}}
}
cand, err := w.Revision(p.Branch, p.Revision)
if err != nil {
return could("mesh-catalog", fmt.Sprintf("the candidate's revision %d is not read: %v", p.Revision, err))
}
newest, err := w.Revision(p.Branch, 0)
if err != nil {
return could("mesh-catalog", fmt.Sprintf("the branch's newest revision is not read: %v", err))
}
var out []seat.Widening
for _, s := range servingSeats(p) {
holder, err := w.Holder(s)
if err != nil {
out = append(out, seat.Widening{What: seat.WideningWhatCouldNotRun, Name: s, Said: fmt.Sprintf("the holder of %s is not read: %v", s, err)})
continue
}
before, after := newest.Entries[holder], cand.Entries[holder]
if after.Fp != "" && after.Fp != before.Fp {
out = append(out, seat.Widening{What: seat.WideningWhatServingPart, Name: holder,
Said: fmt.Sprintf("serves %s; its fingerprint is %s, was %s", s, after.Fp, before.Fp)})
}
}
out = append(out, ruleWidens(p.Module, newest.Entries[p.Module], cand.Entries[p.Module])...)
out = append(out, credentialWidens(p.Module, newest.Entries[p.Module], cand.Entries[p.Module])...)
if w.GrantsFor != nil {
g, err := w.GrantsFor(p.Branch, p.Source)
switch {
case err != nil:
out = append(out, seat.Widening{What: seat.WideningWhatCouldNotRun, Name: "mesh-grants", Said: err.Error()})
case g.Error != "":
out = append(out, seat.Widening{What: seat.WideningWhatCouldNotRun, Name: "mesh-grants", Said: g.Error})
default:
for _, m := range g.Modules {
if m.Module != p.Module {
continue
}
for _, a := range m.Added {
out = append(out, seat.Widening{What: seat.WideningWhatGrant, Name: m.Module + " " + a.Subject + " " + a.Verb,
Said: "the grants part would compose it, and the branch's newest does not hold it"})
}
}
}
}
order := map[string]int{seat.WideningWhatServingPart: 0, seat.WideningWhatActingRule: 1, seat.WideningWhatCredential: 2,
seat.WideningWhatGrant: 3, seat.WideningWhatCouldNotRun: 4}
sort.SliceStable(out, func(i, j int) bool {
if order[out[i].What] != order[out[j].What] {
return order[out[i].What] < order[out[j].What]
}
return out[i].Name < out[j].Name
})
return out
}
// servingSeats are the seats whose holders serve a step of p (A-220): the runner's, the catalog's, the composer's and each step's.
func servingSeats(p seat.Pipeline) []string {
set := map[string]bool{seat.Seat: true, catalog.Seat: true, ComposerSeat: true}
for _, s := range p.Steps {
if s.Seat != "" {
set[s.Seat] = true
}
}
var out []string
for s := range set {
out = append(out, s)
}
sort.Strings(out)
return out
}
// ruleWidens names each acting rule of the candidate's entry that the newest entry has not, or that now names more entries
// (`every` where it named a language) or holds longer (A-215).
func ruleWidens(module string, before, after catalog.CatalogEntry) []seat.Widening {
on := func(s string) string {
if s == "" {
return "every"
}
return s
}
var out []seat.Widening
for _, r := range after.Acts {
name := module + " " + r.Seat + "." + r.Step
var was *catalog.ActingRule
for i := range before.Acts {
if before.Acts[i].Seat == r.Seat && before.Acts[i].Step == r.Step {
was = &before.Acts[i]
}
}
switch {
case was == nil:
out = append(out, seat.Widening{What: seat.WideningWhatActingRule, Name: name, Said: "the rule is gained"})
case on(r.On) == "every" && on(was.On) != "every":
out = append(out, seat.Widening{What: seat.WideningWhatActingRule, Name: name, Said: "the rule acts on every entry, it acted on " + was.On})
case longer(r.Bound, was.Bound):
out = append(out, seat.Widening{What: seat.WideningWhatActingRule, Name: name, Said: "the bound is " + r.Bound + ", it was " + was.Bound})
}
}
return out
}
func longer(a, b string) bool {
x, err1 := time.ParseDuration(a)
y, err2 := time.ParseDuration(b)
return err1 == nil && err2 == nil && x > y
}
// credentialWidens names each own secret and each provision with a credential of its own that the candidate's manifest names and
// the newest entry's did not (A-215).
func credentialWidens(module string, before, after catalog.CatalogEntry) []seat.Widening {
was := map[string]bool{}
for _, n := range credentialsOf(before.Manifest) {
was[n] = true
}
var out []seat.Widening
for _, n := range credentialsOf(after.Manifest) {
if !was[n] {
out = append(out, seat.Widening{What: seat.WideningWhatCredential, Name: module + " " + n, Said: "the manifest names it, and the branch's newest does not"})
}
}
return out
}
// credentialsOf are the names of the own secrets a manifest declares and of the provisions that carry a credential: an own secret
// of the provider, one issued by software or one only a person gives.
func credentialsOf(manifest map[string]any) []string {
var out []string
if secrets, ok := manifest["secrets"].(map[string]any); ok {
for n := range secrets {
out = append(out, n)
}
}
if provides, ok := manifest["provides"].([]any); ok {
for _, o := range provides {
if m, ok := o.(map[string]any); ok && m["credential"] != nil {
if n, ok := m["name"].(string); ok {
out = append(out, n)
}
}
}
}
sort.Strings(out)
return out
}