diff --git a/contracts/go/clients/meshdeliverycheckerclient/client.go b/contracts/go/clients/meshdeliverycheckerclient/client.go deleted file mode 100644 index 7e2acbf3..00000000 --- a/contracts/go/clients/meshdeliverycheckerclient/client.go +++ /dev/null @@ -1,341 +0,0 @@ -// Package meshdeliverycheckerclient holds the logic the holders and callers of the seat mesh-delivery-checker share over its -// contract (novox/hq ADR 0625 §4, ADR 0634 §2): the rules a verdict and a kept verdict must keep to be written or read. The -// checker writes them, and the view, the gate and the indexer read them; the contract's decoding is strict by schema only, so -// these rules are written once here and no caller copies them. -package meshdeliverycheckerclient - -import ( - "fmt" - pathpkg "path" - "strings" - - checker "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The states of a verdict, the levels of a check and the compositions' states. -const ( - VerdictRunning = "running" - VerdictPass = "pass" - VerdictFail = "fail" - - LevelUnit = "unit" - LevelIntegration = "integration" - - CompositionNotRun = "not-run" - CompositionPass = "pass" - CompositionFail = "fail" -) - -// MaxFailures is the most failures a verdict names; it names the count of the rest beside them. -const MaxFailures = 50 - -func checkLevel(level string) error { - if level != LevelUnit && level != LevelIntegration { - return fmt.Errorf("meshdeliverycheckerclient: the level %q is not %s or %s", level, LevelUnit, LevelIntegration) - } - return nil -} - -func isHex12(s string) bool { return len(s) == 12 && strings.Trim(s, "0123456789abcdef") == "" } - -// CheckRepository refuses a repository that is not / with exactly one slash, each part a key part. -func CheckRepository(repository string) error { - owner, name, ok := strings.Cut(repository, "/") - if !ok || strings.Contains(name, "/") { - return fmt.Errorf("meshdeliverycheckerclient: the repository %q is not /, with exactly one slash", repository) - } - if err := keys.CheckName("repository owner", owner); err != nil { - return fmt.Errorf("meshdeliverycheckerclient: the repository %q: %w", repository, err) - } - if err := keys.CheckName("repository name", name); err != nil { - return fmt.Errorf("meshdeliverycheckerclient: the repository %q: %w", repository, err) - } - return nil -} - -func checkComposition(c *checker.VerdictsByGroupHeadsValueComposition) error { - if c == nil { - return nil - } - switch c.State { - case CompositionNotRun: - if c.Sdk != "" || len(c.Machines) > 0 { - return fmt.Errorf("meshdeliverycheckerclient: the verdict's composition is not run and names no mesh-sdk commit or machines") - } - case CompositionPass: - if c.Sdk == "" { - return fmt.Errorf("meshdeliverycheckerclient: the verdict's composition passed and names no mesh-sdk commit") - } - case CompositionFail: - if c.Words == "" { - return fmt.Errorf("meshdeliverycheckerclient: the verdict's composition failed and gives no words") - } - default: - return fmt.Errorf("meshdeliverycheckerclient: the verdict's composition state %q is not %s, %s or %s", c.State, CompositionNotRun, CompositionPass, CompositionFail) - } - return nil -} - -// checkJudgedBy refuses a checking program named at no repository, full commit and package, kept under no program key, -// or over no head or a head at no full commit (novox/hq ADR 0644, ADR 0656; issue 1921). -func checkJudgedBy(j *checker.VerdictsByGroupHeadsValueJudgedBy) error { - const who = "meshdeliverycheckerclient: " - if j == nil { - return nil - } - if CheckRepository(j.Program.Repository) != nil || !isCommit(j.Program.Commit) || j.Program.Package == "" { - return fmt.Errorf(who+"the verdict is judged by a checking program built at no repository, full commit and package (%+v)", j.Program) - } - if !strings.HasPrefix(j.Key, "program-") { - return fmt.Errorf(who+"the verdict's checking program is kept under %q, which is no program key", j.Key) - } - if len(j.Heads) == 0 { - return fmt.Errorf(who + "the verdict's checking program read no head") - } - for _, h := range j.Heads { - if CheckRepository(h.Repository) != nil || !isCommit(h.Commit) { - return fmt.Errorf(who+"the verdict's checking program read %s at %q, which is no full commit", h.Repository, h.Commit) - } - } - return nil -} - -func isCommit(s string) bool { return len(s) == 40 && strings.Trim(s, "0123456789abcdef") == "" } - -// CheckVerdict refuses a verdict a reader could not place: no group, heads that are not 12 lowercase hex digits, a repository -// both merged and read beside, a carried-from that carries nothing, a series change from a series to itself or named twice, a -// level that is neither unit nor integration or counts a negative number, a time below zero, a pass that says nothing of a -// level, a fail that names neither a test nor a machine, and failures on a verdict that did not fail. -func CheckVerdict(v checker.VerdictsByGroupHeadsValue) error { - const who = "meshdeliverycheckerclient: " - if v.Group == "" { - return fmt.Errorf(who + "a verdict names no group") - } - if !isHex12(v.Heads) { - return fmt.Errorf(who+"the verdict's heads hash %q is not 12 lowercase hex digits", v.Heads) - } - for repository, head := range v.Trunk { - if head == "" { - return fmt.Errorf(who+"the verdict's trunk head of %s is empty", repository) - } - } - for repository, commit := range v.Beside { - if commit == "" { - return fmt.Errorf(who+"the verdict's sibling commit of %s is empty", repository) - } - if _, ok := v.Trunk[repository]; ok { - return fmt.Errorf(who+"the verdict names %s as a trunk it merged and as a sibling it read", repository) - } - } - if len(v.CarriedFrom) > 0 { - if v.State == VerdictRunning { - return fmt.Errorf(who + "a running verdict is carried from nothing: carried-from is written with its end") - } - moved := false - for repository, commit := range v.CarriedFrom { - now, inTrunk := v.Trunk[repository] - if !inTrunk { - now = v.Beside[repository] - } - if commit == "" || now == "" { - return fmt.Errorf(who+"the verdict's carried-from names %s, which it judged on no commit of", repository) - } - moved = moved || commit != now - } - if !moved { - return fmt.Errorf(who + "the verdict's carried-from names the commits it is judged on: nothing moved, so nothing was carried") - } - } - changed := map[string]bool{} - for _, c := range v.Series { - if c.Module == "" || c.Artifact == "" || c.From == "" || c.To == "" { - return fmt.Errorf(who+"the verdict's series change %+v names no module, artifact, from or to", c) - } - if c.From == c.To { - return fmt.Errorf(who+"the verdict's series change of %s/%s is from %s to the same series", c.Module, c.Artifact, c.From) - } - if changed[c.Module+"/"+c.Artifact] { - return fmt.Errorf(who+"the verdict names the series change of %s/%s twice", c.Module, c.Artifact) - } - changed[c.Module+"/"+c.Artifact] = true - } - for level, r := range v.Levels { - if err := checkLevel(level); err != nil { - return err - } - if r.Run < 0 || r.Kept < 0 || r.Skipped < 0 { - return fmt.Errorf(who+"the verdict's %s level counts a negative number of tests", level) - } - } - if err := checkComposition(v.Composition); err != nil { - return err - } - if err := checkJudgedBy(v.JudgedBy); err != nil { - return err - } - seen := map[string]bool{} - for _, level := range v.OverBound { - if err := checkLevel(level); err != nil { - return fmt.Errorf(who+"the verdict's over-bound names: %w", err) - } - if seen[level] { - return fmt.Errorf(who+"the verdict's over-bound names the level %s twice", level) - } - seen[level] = true - } - if t := v.Took; t != nil { - for part, ms := range map[string]int64{"queue": t.Queue, "clone": t.Clone, "unit": t.Unit, "lint": t.Lint, - "integration-setup": t.IntegrationSetup, "integration-run": t.IntegrationRun, "composition": t.Composition} { - if ms < 0 { - return fmt.Errorf(who+"the verdict's time of the part %s is negative", part) - } - } - } - named := v.Failed != nil && (v.Failed.Test != "" || v.Failed.Machine != "") - if len(v.Failures) > 0 && v.State != VerdictFail { - return fmt.Errorf(who+"a %s verdict names no failures", v.State) - } - if len(v.Failures) > MaxFailures+1 { - return fmt.Errorf(who+"the verdict names %d failures, more than %d and the count of the rest", len(v.Failures), MaxFailures) - } - switch v.State { - case VerdictRunning: - if v.Failed != nil && *v.Failed != (checker.VerdictsByGroupHeadsValueFailed{}) { - return fmt.Errorf(who + "a running verdict names no failure") - } - case VerdictPass: - if v.Failed != nil && *v.Failed != (checker.VerdictsByGroupHeadsValueFailed{}) { - return fmt.Errorf(who + "a pass names no failure") - } - for _, level := range []string{LevelUnit, LevelIntegration} { - if _, ok := v.Levels[level]; !ok { - return fmt.Errorf(who+"a pass says nothing of the %s level", level) - } - } - case VerdictFail: - if !named { - return fmt.Errorf(who + "a fail names neither a test nor a machine") - } - default: - return fmt.Errorf(who+"the verdict state %q is not running, pass or fail", v.State) - } - return nil -} - -// CheckKept refuses a kept verdict that is not a pass of a repository's package at a level, with its toolchain and its time: -// a failure is never kept. -func CheckKept(k checker.KeptByLevelDigestValue) error { - const who = "meshdeliverycheckerclient: " - if k.State != VerdictPass { - return fmt.Errorf(who+"a kept verdict is a pass, never %q: a failure is never kept", k.State) - } - if err := CheckRepository(k.Repository); err != nil { - return err - } - if k.Package == "" { - return fmt.Errorf(who + "a kept verdict names no package") - } - if err := checkLevel(k.Level); err != nil { - return err - } - if k.Tests < 0 || k.Took < 0 { - return fmt.Errorf(who + "a kept verdict counts a negative number of tests or a negative time") - } - if k.Toolchain == "" { - return fmt.Errorf(who + "a kept verdict names no toolchain version") - } - if k.At == "" { - return fmt.Errorf(who + "a kept verdict says no time") - } - return nil -} - -// CheckPreview refuses a preview a reader could not place (the checker writes it, the indexer reads it for the candidates' -// fingerprints): a repository, head or trunk commit that does not key, a head in conflict that names no file or has a merge -// commit or modules, a head that merged and names no merge commit, and a module with no directory, no tree, a manifest -// digest that is not 64 lowercase hex digits, a lock without path or hash, or a named path that is not clean and inside the -// module's directory or names no git object. -func CheckPreview(p checker.PreviewsByRepositoryHeadTrunkValue) error { - const who = "meshdeliverycheckerclient: " - if err := CheckRepository(p.Repository); err != nil { - return err - } - if err := keys.CheckName("head", p.Head); err != nil { - return fmt.Errorf(who+"the preview: %w", err) - } - if err := keys.CheckName("trunk commit", p.Trunk); err != nil { - return fmt.Errorf(who+"the preview: %w", err) - } - for _, f := range p.Conflicts { - if f == "" { - return fmt.Errorf(who + "a preview names a conflicting file with an empty path") - } - } - if len(p.Conflicts) > 0 { - if p.Merge != "" || len(p.Modules) > 0 { - return fmt.Errorf(who + "a preview of a head in conflict has no merge commit and no modules") - } - return nil - } - if p.Merge == "" { - return fmt.Errorf(who + "a preview names no merge commit") - } - for dir, m := range p.Modules { - if dir == "" { - return fmt.Errorf(who + "a preview names a module with an empty directory") - } - if m.Tree == "" { - return fmt.Errorf(who+"the preview's module %q has no tree", dir) - } - if !keys.IsHex64(m.Manifest) { - return fmt.Errorf(who+"the preview's module %q: %q is not a manifest digest, 64 lowercase hex digits", dir, m.Manifest) - } - for path, hash := range m.Locks { - if path == "" || hash == "" { - return fmt.Errorf(who+"the lock %q of the preview's module %q has an empty path or hash", path, dir) - } - } - for p, object := range m.Paths { - if p == "" || p != pathpkg.Clean(p) || strings.HasPrefix(p, "/") || p == ".." || strings.HasPrefix(p, "../") { - return fmt.Errorf(who+"the preview's module %q: the named path %q is not a clean path inside the module's directory", dir, p) - } - if object == "" { - return fmt.Errorf(who+"the preview's module %q: the named path %q names no git object", dir, p) - } - } - } - return nil -} - -// VerdictKey is the key, in the state verdicts, of a group's check at its heads: `.`, the -// group escaped so a branch such as `fix/1.2` keys, the heads hash as the mesh-delivery client's HeadsHash gives it -// (12 lowercase hex digits). -func VerdictKey(group, headsHash string) (string, error) { - if group == "" { - return "", fmt.Errorf("meshdeliverycheckerclient: the group name is empty") - } - if !isHex12(headsHash) { - return "", fmt.Errorf("meshdeliverycheckerclient: %q is not a heads hash, 12 lowercase hex digits", headsHash) - } - return keys.EscapeName(group) + "." + headsHash, nil -} - -// ParseVerdictKey is the inverse of VerdictKey. -func ParseVerdictKey(key string) (group, headsHash string, err error) { - i := strings.LastIndex(key, ".") - if i < 0 { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a verdict key, .", key) - } - part, headsHash := key[:i], key[i+1:] - if !isHex12(headsHash) { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a verdict key: %q is not a heads hash, 12 lowercase hex digits", key, headsHash) - } - if part == "" || strings.Contains(part, ".") { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a verdict key: the group part is empty or holds a dot", key) - } - if group, err = keys.UnescapeName(part); err != nil { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a verdict key: %w", key, err) - } - return group, headsHash, nil -} diff --git a/contracts/go/clients/meshdeliverycheckerclient/client_test.go b/contracts/go/clients/meshdeliverycheckerclient/client_test.go deleted file mode 100644 index 38c651c0..00000000 --- a/contracts/go/clients/meshdeliverycheckerclient/client_test.go +++ /dev/null @@ -1,269 +0,0 @@ -package meshdeliverycheckerclient - -import ( - "encoding/json" - "strings" - "testing" - - checker "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker" -) - -type verdict = checker.VerdictsByGroupHeadsValue - -func passing() verdict { - return verdict{Group: "g", Heads: "0123456789ab", State: VerdictPass, Trunk: map[string]string{"novox/a": "c1"}, - Levels: map[string]checker.VerdictsByGroupHeadsValueLevelsValue{LevelUnit: {Run: 3}, LevelIntegration: {Run: 1}}} -} - -// The rules a verdict keeps to be written or read: each case edits a passing verdict and names what the refusal says. -func TestAVerdictIsRefusedForWhatAReaderCouldNotPlace(t *testing.T) { - if err := CheckVerdict(passing()); err != nil { - t.Fatalf("a passing verdict is refused: %v", err) - } - for name, c := range map[string]struct { - edit func(*verdict) - says string - }{ - "no group": {func(v *verdict) { v.Group = "" }, "no group"}, - "heads of 11 digits": {func(v *verdict) { v.Heads = "0123456789a" }, "12 lowercase hex"}, - "an empty trunk head": {func(v *verdict) { v.Trunk["novox/a"] = "" }, "trunk head"}, - "merged and read": {func(v *verdict) { v.Beside = map[string]string{"novox/a": "c2"} }, "trunk it merged and as a sibling"}, - "nothing carried": {func(v *verdict) { v.CarriedFrom = map[string]string{"novox/a": "c1"} }, "nothing moved"}, - "a running carry": {func(v *verdict) { v.State, v.CarriedFrom = VerdictRunning, map[string]string{"novox/a": "c0"} }, "carried from nothing"}, - "a series to itself": {func(v *verdict) { - v.Series = []checker.VerdictsByGroupHeadsValueSeriesItem{{Module: "m", Artifact: "a", From: "0.1", To: "0.1"}} - }, "same series"}, - "an unknown level": {func(v *verdict) { v.Levels["smoke"] = checker.VerdictsByGroupHeadsValueLevelsValue{} }, "smoke"}, - "a negative count": {func(v *verdict) { v.Levels[LevelUnit] = checker.VerdictsByGroupHeadsValueLevelsValue{Run: -1} }, "negative"}, - "a negative time": {func(v *verdict) { v.Took = &checker.VerdictsByGroupHeadsValueTook{Unit: -1} }, "negative"}, - "a pass without a level": {func(v *verdict) { delete(v.Levels, LevelIntegration) }, "integration level"}, - "a pass that failed": {func(v *verdict) { v.Failed = &checker.VerdictsByGroupHeadsValueFailed{Test: "T"} }, "names no failure"}, - "failures of a pass": {func(v *verdict) { v.Failures = []checker.VerdictsByGroupHeadsValueFailuresItem{{Test: "T"}} }, "names no failures"}, - "a fail naming nothing": {func(v *verdict) { v.State = VerdictFail }, "neither a test nor a machine"}, - "an unknown state": {func(v *verdict) { v.State = "maybe" }, "maybe"}, - "a composition passed without a commit": {func(v *verdict) { - v.Composition = &checker.VerdictsByGroupHeadsValueComposition{State: CompositionPass} - }, "mesh-sdk commit"}, - "the same level over bound twice": {func(v *verdict) { v.OverBound = []string{LevelUnit, LevelUnit} }, "twice"}, - // 1921 (B0, G): the checking program that judged it. - "judged at no full commit": {func(v *verdict) { j := judgedBy(); j.Program.Commit = "0123456"; v.JudgedBy = &j }, "checking program built at no repository, full commit"}, - "judged under no key": {func(v *verdict) { j := judgedBy(); j.Key = "composer-1"; v.JudgedBy = &j }, "composer-1"}, - "judged over no head": {func(v *verdict) { j := judgedBy(); j.Heads = nil; v.JudgedBy = &j }, "no head"}, - "judged over a branch": {func(v *verdict) { j := judgedBy(); j.Heads[0].Commit = "main"; v.JudgedBy = &j }, "novox/mesh-delivery"}, - } { - v := passing() - c.edit(&v) - if err := CheckVerdict(v); err == nil || !strings.Contains(err.Error(), c.says) { - t.Errorf("%s: %v, want a refusal saying %q", name, err, c.says) - } - } - judged := passing() - j := judgedBy() - judged.JudgedBy = &j - if err := CheckVerdict(judged); err != nil { - t.Errorf("a pass judged by a checking program is refused: %v", err) - } - failed := passing() - failed.State, failed.Failed = VerdictFail, &checker.VerdictsByGroupHeadsValueFailed{Machine: "ace"} - if err := CheckVerdict(failed); err != nil { - t.Errorf("a fail naming a machine is refused: %v", err) - } -} - -func TestAKeptVerdictIsAPassOfAPackageAtALevel(t *testing.T) { - t.Run("a preview is refused for what a reader of candidates could not place", previewIsRefused) - ok := checker.KeptByLevelDigestValue{State: VerdictPass, Repository: "novox/mesh-sdk", Package: "./go/keys", Level: LevelUnit, Toolchain: "1.26.0", At: "2026-11-05T10:00:00Z"} - if err := CheckKept(ok); err != nil { - t.Fatalf("a kept pass is refused: %v", err) - } - for name, edit := range map[string]func(*checker.KeptByLevelDigestValue){ - "a fail": func(k *checker.KeptByLevelDigestValue) { k.State = VerdictFail }, - "no owner": func(k *checker.KeptByLevelDigestValue) { k.Repository = "mesh-sdk" }, - "no package": func(k *checker.KeptByLevelDigestValue) { k.Package = "" }, - "an unknown level": func(k *checker.KeptByLevelDigestValue) { k.Level = "smoke" }, - "negative tests": func(k *checker.KeptByLevelDigestValue) { k.Tests = -1 }, - "no toolchain": func(k *checker.KeptByLevelDigestValue) { k.Toolchain = "" }, - "no time": func(k *checker.KeptByLevelDigestValue) { k.At = "" }, - } { - k := ok - edit(&k) - if CheckKept(k) == nil { - t.Errorf("%s was taken", name) - } - } -} - -// A preview is refused for what a reader of candidates could not place. -func previewIsRefused(t *testing.T) { - good := func() checker.PreviewsByRepositoryHeadTrunkValue { - return checker.PreviewsByRepositoryHeadTrunkValue{Repository: "novox/a", Head: "h1", Trunk: "t1", Merge: "m1", - Modules: map[string]checker.PreviewsByRepositoryHeadTrunkValueModulesValue{"mod": {Tree: "tr", Manifest: strings.Repeat("a", 64), - Locks: map[string]string{"go.sum": "x"}, Paths: map[string]string{"src": "o1"}}}} - } - if err := CheckPreview(good()); err != nil { - t.Fatalf("a good preview is refused: %v", err) - } - for name, c := range map[string]struct { - edit func(*checker.PreviewsByRepositoryHeadTrunkValue) - says string - }{ - "a repository with no owner": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { p.Repository = "a" }, "/"}, - "a head with a dot": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { p.Head = "h.1" }, "head"}, - "a conflict with a merge": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { p.Conflicts = []string{"f"} }, "in conflict"}, - "no merge commit": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { p.Merge = "" }, "no merge commit"}, - "a module with no tree": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { - m := p.Modules["mod"] - m.Tree = "" - p.Modules["mod"] = m - }, "no tree"}, - "a short manifest digest": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { - m := p.Modules["mod"] - m.Manifest = "abc" - p.Modules["mod"] = m - }, "manifest digest"}, - "a path outside the module": {func(p *checker.PreviewsByRepositoryHeadTrunkValue) { - m := p.Modules["mod"] - m.Paths = map[string]string{"../x": "o"} - p.Modules["mod"] = m - }, "clean path"}, - } { - p := good() - c.edit(&p) - if err := CheckPreview(p); err == nil || !strings.Contains(err.Error(), c.says) { - t.Errorf("%s: CheckPreview = %v, want it to say %q", name, err, c.says) - } - } - if err := CheckPreview(checker.PreviewsByRepositoryHeadTrunkValue{Repository: "novox/a", Head: "h", Trunk: "t", Conflicts: []string{"f"}}); err != nil { - t.Errorf("a head in conflict is refused: %v", err) - } -} - -// A verdict's key as go/delivery's CheckerVerdictKey wrote it (golden): the group escaped, the heads hash after the -// last dot. -func TestVerdictKeyAsGoDeliveryWroteIt(t *testing.T) { - key, err := VerdictKey("fix/1.2", "0123456789ab") - if err != nil || key != "fix/1=2e2.0123456789ab" { - t.Fatalf("%q %v", key, err) - } - group, heads, err := ParseVerdictKey(key) - if err != nil || group != "fix/1.2" || heads != "0123456789ab" { - t.Fatalf("%q %q %v", group, heads, err) - } - for _, bad := range []string{"nodot", "g.XYZ", ".0123456789ab", "a.b.0123456789ab", "g=zz.0123456789ab"} { - if _, _, err := ParseVerdictKey(bad); err == nil { - t.Errorf("%q parses", bad) - } - } - if _, err := VerdictKey("", "0123456789ab"); err == nil { - t.Error("an empty group keys") - } - if _, err := VerdictKey("g", "0123"); err == nil { - t.Error("a short hash keys") - } -} - -// The codecs write a value at the kept values' version after its rules and read it back through them. -func TestTheCodecsWriteAtTheVersionAndReadBackThroughTheRules(t *testing.T) { - raw, err := EncodeVerdict(passing()) - if err != nil { - t.Fatal(err) - } - if got, err := DecodeVerdict(raw); err != nil || got.V == 0 || got.Group != "g" { - t.Errorf("verdict read back = %+v, %v", got, err) - } - bad := passing() - bad.Heads = "x" - if _, err := EncodeVerdict(bad); err == nil { - t.Error("a verdict whose heads are no hash is written") - } - if _, err := DecodeVerdict([]byte(`{"v":99,"group":"g","heads":"0123456789ab","state":"running"}`)); err == nil { - t.Error("a verdict of an unknown version is read") - } - k := checker.KeptByLevelDigestValue{State: VerdictPass, Repository: "novox/mesh-sdk", Package: "./go/keys", Level: LevelUnit, Toolchain: "1.26.0", At: "2026-11-05T10:00:00Z"} - raw, err = EncodeKept(k) - if err != nil { - t.Fatal(err) - } - if got, err := DecodeKept(raw); err != nil || got.Package != k.Package { - t.Errorf("kept read back = %+v, %v", got, err) - } - k.State = VerdictFail - if _, err := EncodeKept(k); err == nil { - t.Error("a failure is kept") - } - p := checker.PreviewsByRepositoryHeadTrunkValue{Repository: "novox/a", Head: "h1", Trunk: "t1", Conflicts: []string{"f.go"}} - raw, err = EncodePreview(p) - if err != nil { - t.Fatal(err) - } - if got, err := DecodePreview(raw); err != nil || got.Head != "h1" { - t.Errorf("preview read back = %+v, %v", got, err) - } - p.Conflicts = nil - if _, err := EncodePreview(p); err == nil { - t.Error("a preview with no merge commit and no conflict is written") - } - // 1921 (B0, G): a checking program's input is asked of the seat's verb input by its digest, and an answer for - // another digest, or bytes that are not the digest, is refused. - body := []byte(`{"v":1}`) - digest := DigestOf(body) - for name, c := range map[string]struct { - answer any - says string - }{ - "its own bytes": {InputAnswerOf(body), ""}, - "another digest": {checker.InputAnswer{Digest: DigestOf([]byte("x")), Input: InputAnswerOf(body).Input}, "answered the input"}, - "bytes that are not it": {checker.InputAnswer{Digest: digest, Input: InputAnswerOf([]byte(`{"v":2}`)).Input}, "whose digest is"}, - "bytes in no base64": {checker.InputAnswer{Digest: digest, Input: "%%"}, "no base64"}, - "no answer of the layout": {"nothing", "answered no input"}, - } { - got, err := ReadInput(func(key string, b any) (json.RawMessage, error) { - if key != "seat:mesh-delivery-checker.input" || b != (checker.InputInput{Digest: digest}) { - t.Errorf("%s: asked %s %+v", name, key, b) - } - return json.Marshal(c.answer) - }, digest) - switch { - case c.says == "" && (err != nil || string(got) != string(body)): - t.Errorf("%s: %q, %v", name, got, err) - case c.says != "" && (err == nil || !strings.Contains(err.Error(), c.says)): - t.Errorf("%s: %v, want a refusal saying %q", name, err, c.says) - } - } -} - -// The kept and preview keys: a key reads back to its parts, and a part that does not key is refused. -func TestKeptAndPreviewKeysReadBack(t *testing.T) { - digest := strings.Repeat("ab", 32) - key, err := KeptKey(LevelUnit, digest) - if err != nil || key != "unit."+digest { - t.Fatalf("kept key = %q, %v", key, err) - } - if level, d, err := ParseKeptKey(key); err != nil || level != LevelUnit || d != digest { - t.Errorf("kept key read back = %q %q, %v", level, d, err) - } - for _, bad := range []string{"fast." + digest, "unit.XY", "unit"} { - if _, _, err := ParseKeptKey(bad); err == nil { - t.Errorf("kept key %q is read", bad) - } - } - key, err = PreviewKey("novox/mesh-sdk", "h1", "t1") - if err != nil { - t.Fatal(err) - } - if repo, h, tr, err := ParsePreviewKey(key); err != nil || repo != "novox/mesh-sdk" || h != "h1" || tr != "t1" { - t.Errorf("preview key %q read back = %q %q %q, %v", key, repo, h, tr, err) - } - for _, bad := range []string{"novox.h1.t1", "a/b.h1", "a/b.h1.t1.x"} { - if _, _, _, err := ParsePreviewKey(bad); err == nil { - t.Errorf("preview key %q is read", bad) - } - } -} - -func judgedBy() checker.VerdictsByGroupHeadsValueJudgedBy { - const c = "0123456789abcdef0123456789abcdef01234567" - return checker.VerdictsByGroupHeadsValueJudgedBy{ - Program: checker.VerdictsByGroupHeadsValueJudgedByProgram{Repository: "novox/mesh-delivery", Commit: c, Dir: "modules/mesh-delivery-checker", Package: "p"}, - Key: "program-0123456789abcdef", Built: true, Heads: []checker.VerdictsByGroupHeadsValueJudgedByHeadsItem{{Repository: "novox/mesh-delivery", Commit: c}}} -} diff --git a/contracts/go/clients/meshdeliverycheckerclient/codec.go b/contracts/go/clients/meshdeliverycheckerclient/codec.go deleted file mode 100644 index c684e73d..00000000 --- a/contracts/go/clients/meshdeliverycheckerclient/codec.go +++ /dev/null @@ -1,150 +0,0 @@ -package meshdeliverycheckerclient - -import ( - "encoding/json" - "fmt" - "strings" - - checker "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// The codecs of the three states the checker writes and others read (the view and the gate read verdicts, the indexer reads -// previews, the checker and the tools read kept verdicts): a value is written at the version of the kept values after its rules -// (CheckVerdict, CheckKept, CheckPreview) and read back through the same rules. The checker's owed state is read by no other -// part and keeps its own codec. - -// EncodeVerdict is v as JSON at the kept values' version, refused by CheckVerdict's rules. -func EncodeVerdict(v checker.VerdictsByGroupHeadsValue) ([]byte, error) { - if err := CheckVerdict(v); err != nil { - return nil, err - } - v.V = keys.Version - return json.Marshal(v) -} - -// DecodeVerdict reads a `verdicts` value; it refuses an unknown version (keys.ErrUnknownVersion) and what EncodeVerdict refuses. -func DecodeVerdict(data []byte) (checker.VerdictsByGroupHeadsValue, error) { - var v checker.VerdictsByGroupHeadsValue - if err := decode(data, &v); err != nil { - return v, err - } - if err := CheckVerdict(v); err != nil { - return checker.VerdictsByGroupHeadsValue{}, err - } - return v, nil -} - -// EncodeKept is k as JSON at the kept values' version, refused by CheckKept's rules. -func EncodeKept(k checker.KeptByLevelDigestValue) ([]byte, error) { - if err := CheckKept(k); err != nil { - return nil, err - } - k.V = keys.Version - return json.Marshal(k) -} - -// DecodeKept reads a `kept` value; it refuses an unknown version and what EncodeKept refuses. -func DecodeKept(data []byte) (checker.KeptByLevelDigestValue, error) { - var k checker.KeptByLevelDigestValue - if err := decode(data, &k); err != nil { - return k, err - } - if err := CheckKept(k); err != nil { - return checker.KeptByLevelDigestValue{}, err - } - return k, nil -} - -// EncodePreview is p as JSON at the kept values' version, refused by CheckPreview's rules. -func EncodePreview(p checker.PreviewsByRepositoryHeadTrunkValue) ([]byte, error) { - if err := CheckPreview(p); err != nil { - return nil, err - } - p.V = keys.Version - return json.Marshal(p) -} - -// DecodePreview reads a `previews` value; it refuses an unknown version and what EncodePreview refuses. -func DecodePreview(data []byte) (checker.PreviewsByRepositoryHeadTrunkValue, error) { - var p checker.PreviewsByRepositoryHeadTrunkValue - if err := decode(data, &p); err != nil { - return p, err - } - if err := CheckPreview(p); err != nil { - return checker.PreviewsByRepositoryHeadTrunkValue{}, err - } - return p, nil -} - -func decode(data []byte, into any) error { - if err := keys.CheckVersion(data); err != nil { - return err - } - if err := json.Unmarshal(data, into); err != nil { - return fmt.Errorf("meshdeliverycheckerclient: value does not fit its shape: %w", err) - } - return nil -} - -// KeptKey is the key, in the state kept, of a kept verdict: `.`, the level unit or integration and the digest, -// lowercase hex, of everything that package's tests read at that level as the language's build module names it. -func KeptKey(level, digest string) (string, error) { - if err := checkLevel(level); err != nil { - return "", err - } - if digest == "" || strings.Trim(digest, "0123456789abcdef") != "" { - return "", fmt.Errorf("meshdeliverycheckerclient: %q is not a digest, lowercase hex", digest) - } - return checker.KeptByLevelDigestKey(level, digest), nil -} - -// ParseKeptKey is the inverse of KeptKey. -func ParseKeptKey(key string) (level, digest string, err error) { - level, digest, ok := checker.ParseKeptByLevelDigestKey(key) - if !ok { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a kept key, .", key) - } - if _, err = KeptKey(level, digest); err != nil { - return "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a kept key: %w", key, err) - } - return level, digest, nil -} - -// PreviewKey is the key, in the state previews, of a head merged onto a trunk commit: `..`, the -// repository's owner and name each escaped (keys.EscapeName). -func PreviewKey(repository, head, trunk string) (string, error) { - if err := CheckRepository(repository); err != nil { - return "", err - } - if err := keys.CheckName("head", head); err != nil { - return "", err - } - if err := keys.CheckName("trunk commit", trunk); err != nil { - return "", err - } - owner, name, _ := strings.Cut(repository, "/") - return checker.PreviewsByRepositoryHeadTrunkKey(keys.EscapeName(owner)+"/"+keys.EscapeName(name), head, trunk), nil -} - -// ParsePreviewKey is the inverse of PreviewKey. -func ParsePreviewKey(key string) (repository, head, trunk string, err error) { - repo, head, trunk, ok := checker.ParsePreviewsByRepositoryHeadTrunkKey(key) - if !ok || strings.Contains(trunk, ".") { - return "", "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a preview key, ..", key) - } - owner, name, found := strings.Cut(repo, "/") - if !found { - return "", "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a preview key: the repository has no slash", key) - } - owner, err1 := keys.UnescapeName(owner) - name, err2 := keys.UnescapeName(name) - if err1 != nil || err2 != nil { - return "", "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a preview key: the repository does not unescape", key) - } - repository = owner + "/" + name - if _, err := PreviewKey(repository, head, trunk); err != nil { - return "", "", "", fmt.Errorf("meshdeliverycheckerclient: %q is not a preview key: %w", key, err) - } - return repository, head, trunk, nil -} diff --git a/contracts/go/clients/meshdeliverycheckerclient/go.mod b/contracts/go/clients/meshdeliverycheckerclient/go.mod deleted file mode 100644 index ba6062bf..00000000 --- a/contracts/go/clients/meshdeliverycheckerclient/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliverycheckerclient - -go 1.26.0 - -require ( - git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 -) diff --git a/contracts/go/clients/meshdeliverycheckerclient/input.go b/contracts/go/clients/meshdeliverycheckerclient/input.go deleted file mode 100644 index 1594df80..00000000 --- a/contracts/go/clients/meshdeliverycheckerclient/input.go +++ /dev/null @@ -1,53 +0,0 @@ -package meshdeliverycheckerclient - -import ( - "crypto/sha256" - "encoding/base64" - "encoding/hex" - "encoding/json" - "fmt" - - checker "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker" -) - -// InputVerb is the seat's verb input as a manifest's `invokes` names it and a run ask's input names it: asked of the -// seat, on no one machine (novox/hq ADR 0644, ADR 0656; issue 1921). A checking program's input is kept by the checker -// by the sha256 of its exact bytes, and asked of it by the build module that runs the program (ADR 0596 §2). -const InputVerb = "seat:" + checker.Seat + "." + checker.VerbInput - -// InputAnswerOf is the verb input's answer for body: its digest and its exact bytes, base64. -func InputAnswerOf(body []byte) checker.InputAnswer { - return checker.InputAnswer{Digest: DigestOf(body), Input: base64.StdEncoding.EncodeToString(body)} -} - -// DigestOf is the digest a checking program's input is kept and asked by: the sha256 of its bytes, 64 lowercase hex -// digits. -func DigestOf(body []byte) string { - sum := sha256.Sum256(body) - return hex.EncodeToString(sum[:]) -} - -// ReadInput asks the verb input, with ask (a tools bundle's stdio.Ask, a process's Conn.Call), for the input kept by -// digest and answers its bytes, refusing an answer for another digest and bytes whose sha256 is not the digest. -func ReadInput(ask func(key string, body any) (json.RawMessage, error), digest string) ([]byte, error) { - const who = "meshdeliverycheckerclient: " - raw, err := ask(InputVerb, checker.InputInput{Digest: digest}) - if err != nil { - return nil, fmt.Errorf(who+"asking %s for the input %s: %w", InputVerb, digest, err) - } - var a checker.InputAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return nil, fmt.Errorf(who+"%s answered no input for %s: %w", InputVerb, digest, err) - } - if a.Digest != digest { - return nil, fmt.Errorf(who+"%s answered the input %q, asked %s", InputVerb, a.Digest, digest) - } - body, err := base64.StdEncoding.DecodeString(a.Input) - if err != nil { - return nil, fmt.Errorf(who+"%s answered the input %s in no base64: %w", InputVerb, digest, err) - } - if got := DigestOf(body); got != digest { - return nil, fmt.Errorf(who+"the input %s answered %d bytes whose digest is %s", digest, len(body), got) - } - return body, nil -} diff --git a/contracts/go/seats/meshcatalog/contract.go b/contracts/go/seats/meshcatalog/contract.go index 32870c7c..4c98ac35 100644 --- a/contracts/go/seats/meshcatalog/contract.go +++ b/contracts/go/seats/meshcatalog/contract.go @@ -58,13 +58,12 @@ const SubjectManifestAccepted = "mesh.seat.mesh-catalog.event.manifest-accepted" const SubjectManifestRefused = "mesh.seat.mesh-catalog.event.manifest-refused" const ( - StateManifests = "manifests" - StateModules = "modules" - StateCandidates = "candidates" - StateVerdicts = "verdicts" - StatePredefined = "predefined" - StateConfigCandidates = "config-candidates" - StatePackages = "packages" + StateManifests = "manifests" + StateModules = "modules" + StateCandidates = "candidates" + StateVerdicts = "verdicts" + StatePredefined = "predefined" + StatePackages = "packages" ) // ManifestsByDigestKey is the key of one value of the state manifests under the form "". @@ -221,37 +220,6 @@ func DecodePredefinedByListValue(raw []byte) (PredefinedByListValue, error) { return v, nil } -// ConfigCandidatesByPullKey is the key of one value of the state config-candidates under the form "". -func ConfigCandidatesByPullKey(pull string) string { return pull } - -// ParseConfigCandidatesByPullKey reads the parts of a key of the form "", in the order of the form; the last result is false for a key of another form. -func ParseConfigCandidatesByPullKey(key string) (string, bool) { - rest := key - if rest == "" { - return "", false - } - v0 := rest - rest = "" - if rest != "" { - return "", false - } - return v0, true -} - -// DecodeConfigCandidatesByPullValue reads one value of the state config-candidates under the form "", refusing a field ConfigCandidatesByPullValue does not know and any text after the value. -func DecodeConfigCandidatesByPullValue(raw []byte) (ConfigCandidatesByPullValue, error) { - var v ConfigCandidatesByPullValue - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&v); err != nil { - return v, err - } - if dec.More() { - return v, errors.New("text after the value") - } - return v, nil -} - // PackagesByModuleKey is the key of one value of the state packages under the form "". func PackagesByModuleKey(module string) string { return module } @@ -584,39 +552,6 @@ type PredefinedByListValue struct { V int64 `json:"v,omitempty"` } -// ConfigCandidatesByPullValueDeclaresValueValue is a type of the contract. -type ConfigCandidatesByPullValueDeclaresValueValue struct { - // the default - Default any `json:"default"` - Description string `json:"description,omitempty"` - // the allowed values - Enum any `json:"enum,omitempty"` - Kind string `json:"kind"` - // a declared bound; 0 is a bound, absent is none - Maximum *float64 `json:"maximum,omitempty"` - // a declared bound; 0 is a bound, absent is none - Minimum *float64 `json:"minimum,omitempty"` - Pattern string `json:"pattern,omitempty"` - Predefined string `json:"predefined,omitempty"` - Required bool `json:"required,omitempty"` - Runtime bool `json:"runtime,omitempty"` - Trusted bool `json:"trusted,omitempty"` - Type string `json:"type,omitempty"` - Why string `json:"why"` -} - -// ConfigCandidatesByPullValue is one value of the state config-candidates under the key . -type ConfigCandidatesByPullValue struct { - // when (RFC 3339 time) - At *time.Time `json:"at,omitempty"` - // module to key to declaration - Declares map[string]map[string]ConfigCandidatesByPullValueDeclaresValueValue `json:"declares"` - Head string `json:"head"` - Pull string `json:"pull"` - // the layout version - V int64 `json:"v,omitempty"` -} - // PackagesByModuleValuePackagesItem is a type of the contract. type PackagesByModuleValuePackagesItem struct { Digest string `json:"digest"` diff --git a/contracts/go/seats/meshcatalog/contract_test.go b/contracts/go/seats/meshcatalog/contract_test.go index b6d95b15..8c962d92 100644 --- a/contracts/go/seats/meshcatalog/contract_test.go +++ b/contracts/go/seats/meshcatalog/contract_test.go @@ -27,18 +27,6 @@ func TestFixtures(t *testing.T) { decode: func(b []byte) (any, error) { var v VerdictsByDigestValue; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, {name: "the predefined list", subject: "", given: "{\"commit\":\"cccccccccccccccccccccccccccccccccccccccc\",\"list\":{\"streams\":[]},\"v\":1}", wire: "{\"commit\":\"cccccccccccccccccccccccccccccccccccccccc\",\"list\":{\"streams\":[]},\"v\":1}", decode: func(b []byte) (any, error) { var v PredefinedByListValue; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, - {name: "a pull request's configuration", subject: "", given: "{\"declares\":{\"nats\":{\"limit\":{\"default\":1,\"kind\":\"setting\",\"why\":\"a bound\"}}},\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"pull\":\"novox/mesh-catalog#1\",\"v\":1}", wire: "{\"declares\":{\"nats\":{\"limit\":{\"default\":1,\"kind\":\"setting\",\"why\":\"a bound\"}}},\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"pull\":\"novox/mesh-catalog#1\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v ConfigCandidatesByPullValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, - {name: "a pull request's configuration state whose bounds are declared 0 (a zero is a bound, not none)", subject: "", given: "{\"declares\":{\"nats\":{\"limit\":{\"default\":0,\"kind\":\"setting\",\"maximum\":0,\"minimum\":0,\"why\":\"a bound\"}}},\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"pull\":\"novox/mesh-catalog#1\",\"v\":1}", wire: "{\"declares\":{\"nats\":{\"limit\":{\"default\":0,\"kind\":\"setting\",\"maximum\":0,\"minimum\":0,\"why\":\"a bound\"}}},\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"pull\":\"novox/mesh-catalog#1\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v ConfigCandidatesByPullValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, {name: "a published Go module", subject: "", given: "{\"commit\":\"cccccccccccccccccccccccccccccccccccccccc\",\"module\":\"git.novox.be/novox/mesh-sdk/go\",\"packages\":[{\"digest\":\"ab\",\"package\":\"git.novox.be/novox/mesh-sdk/go/keys\"}],\"repository\":\"novox/mesh-sdk\",\"v\":1}", wire: "{\"commit\":\"cccccccccccccccccccccccccccccccccccccccc\",\"module\":\"git.novox.be/novox/mesh-sdk/go\",\"packages\":[{\"digest\":\"ab\",\"package\":\"git.novox.be/novox/mesh-sdk/go/keys\"}],\"repository\":\"novox/mesh-sdk\",\"v\":1}", decode: func(b []byte) (any, error) { var v PackagesByModuleValue; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, {name: "a package series", subject: "", given: "{\"name\":\"go-keys\",\"series\":\"0.1\",\"v\":1}", wire: "{\"name\":\"go-keys\",\"series\":\"0.1\",\"v\":1}", diff --git a/contracts/go/seats/meshdeliverychecker/contract.go b/contracts/go/seats/meshdeliverychecker/contract.go deleted file mode 100644 index 00a8c8d8..00000000 --- a/contracts/go/seats/meshdeliverychecker/contract.go +++ /dev/null @@ -1,490 +0,0 @@ -// Code generated by seatgen from the seat definitions; DO NOT EDIT. - -// Package meshdeliverychecker is the contract of the seat "mesh-delivery-checker", defined whole (seats/mesh-delivery-checker/seat.json): The merge check's tries: start one, read one, end one. The checker's own events stay under its own subjects (ADR 0620 §2). -package meshdeliverychecker - -import ( - "bytes" - "encoding/json" - "errors" - "strings" -) - -// Seat is the seat's name. -const Seat = "mesh-delivery-checker" - -const ( - VerbTry = "try" - VerbTryRead = "try-read" - VerbTryEnd = "try-end" - VerbInput = "input" -) - -// Verbs is every verb the seat serves, in its definition's order: a new copy each call. -func Verbs() []string { - return []string{VerbTry, VerbTryRead, VerbTryEnd, VerbInput} -} - -// The holder's own events, listed beside the seat and published under the holder's subjects (ADR 0620 §2). -const ( - HolderEventVerdict = "verdict" - HolderEventTagWanted = "tag-wanted" - HolderEventPreviewWritten = "preview.written" -) - -// HolderSubjectVerdict is where the holder publishes verdict, kept in checker.asks. -func HolderSubjectVerdict(holder string) string { return "mesh.stream." + holder + ".event.verdict" } - -// HolderSubjectTagWanted is where the holder publishes tag-wanted, kept in checker.asks. -func HolderSubjectTagWanted(holder string) string { - return "mesh.stream." + holder + ".event.tag-wanted" -} - -// HolderSubjectPreviewWritten is where the holder publishes preview.written, kept in checks. -func HolderSubjectPreviewWritten(holder string) string { - return "mesh.stream." + holder + ".event.preview.written" -} - -const ( - StateVerdicts = "verdicts" - StateKept = "kept" - StateOwed = "owed" - StatePreviews = "previews" -) - -// VerdictsByGroupHeadsKey is the key of one value of the state verdicts under the form ".". -func VerdictsByGroupHeadsKey(group, heads string) string { return group + "." + heads } - -// ParseVerdictsByGroupHeadsKey reads the parts of a key of the form ".", in the order of the form; the last result is false for a key of another form. -func ParseVerdictsByGroupHeadsKey(key string) (string, string, bool) { - rest := key - v0, after0, found0 := strings.Cut(rest, ".") - if !found0 || v0 == "" { - return "", "", false - } - rest = after0 - if rest == "" { - return "", "", false - } - v2 := rest - rest = "" - if rest != "" { - return "", "", false - } - return v0, v2, true -} - -// DecodeVerdictsByGroupHeadsValue reads one value of the state verdicts under the form ".", refusing a field VerdictsByGroupHeadsValue does not know and any text after the value. -func DecodeVerdictsByGroupHeadsValue(raw []byte) (VerdictsByGroupHeadsValue, error) { - var v VerdictsByGroupHeadsValue - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&v); err != nil { - return v, err - } - if dec.More() { - return v, errors.New("text after the value") - } - return v, nil -} - -// KeptByLevelDigestKey is the key of one value of the state kept under the form ".". -func KeptByLevelDigestKey(level, digest string) string { return level + "." + digest } - -// ParseKeptByLevelDigestKey reads the parts of a key of the form ".", in the order of the form; the last result is false for a key of another form. -func ParseKeptByLevelDigestKey(key string) (string, string, bool) { - rest := key - v0, after0, found0 := strings.Cut(rest, ".") - if !found0 || v0 == "" { - return "", "", false - } - rest = after0 - if rest == "" { - return "", "", false - } - v2 := rest - rest = "" - if rest != "" { - return "", "", false - } - return v0, v2, true -} - -// DecodeKeptByLevelDigestValue reads one value of the state kept under the form ".", refusing a field KeptByLevelDigestValue does not know and any text after the value. -func DecodeKeptByLevelDigestValue(raw []byte) (KeptByLevelDigestValue, error) { - var v KeptByLevelDigestValue - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&v); err != nil { - return v, err - } - if dec.More() { - return v, errors.New("text after the value") - } - return v, nil -} - -// OwedByRepositoryNumberHeadActKey is the key of one value of the state owed under the form "...". -func OwedByRepositoryNumberHeadActKey(repository, number, head, act string) string { - return repository + "." + number + "." + head + "." + act -} - -// ParseOwedByRepositoryNumberHeadActKey reads the parts of a key of the form "...", in the order of the form; the last result is false for a key of another form. -func ParseOwedByRepositoryNumberHeadActKey(key string) (string, string, string, string, bool) { - rest := key - v0, after0, found0 := strings.Cut(rest, ".") - if !found0 || v0 == "" { - return "", "", "", "", false - } - rest = after0 - v2, after2, found2 := strings.Cut(rest, ".") - if !found2 || v2 == "" { - return "", "", "", "", false - } - rest = after2 - v4, after4, found4 := strings.Cut(rest, ".") - if !found4 || v4 == "" { - return "", "", "", "", false - } - rest = after4 - if rest == "" { - return "", "", "", "", false - } - v6 := rest - rest = "" - if rest != "" { - return "", "", "", "", false - } - return v0, v2, v4, v6, true -} - -// DecodeOwedByRepositoryNumberHeadActValue reads one value of the state owed under the form "...", refusing a field OwedByRepositoryNumberHeadActValue does not know and any text after the value. -func DecodeOwedByRepositoryNumberHeadActValue(raw []byte) (OwedByRepositoryNumberHeadActValue, error) { - var v OwedByRepositoryNumberHeadActValue - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&v); err != nil { - return v, err - } - if dec.More() { - return v, errors.New("text after the value") - } - return v, nil -} - -// PreviewsByRepositoryHeadTrunkKey is the key of one value of the state previews under the form "..". -func PreviewsByRepositoryHeadTrunkKey(repository, head, trunk string) string { - return repository + "." + head + "." + trunk -} - -// ParsePreviewsByRepositoryHeadTrunkKey reads the parts of a key of the form "..", in the order of the form; the last result is false for a key of another form. -func ParsePreviewsByRepositoryHeadTrunkKey(key string) (string, string, string, bool) { - rest := key - v0, after0, found0 := strings.Cut(rest, ".") - if !found0 || v0 == "" { - return "", "", "", false - } - rest = after0 - v2, after2, found2 := strings.Cut(rest, ".") - if !found2 || v2 == "" { - return "", "", "", false - } - rest = after2 - if rest == "" { - return "", "", "", false - } - v4 := rest - rest = "" - if rest != "" { - return "", "", "", false - } - return v0, v2, v4, true -} - -// DecodePreviewsByRepositoryHeadTrunkValue reads one value of the state previews under the form "..", refusing a field PreviewsByRepositoryHeadTrunkValue does not know and any text after the value. -func DecodePreviewsByRepositoryHeadTrunkValue(raw []byte) (PreviewsByRepositoryHeadTrunkValue, error) { - var v PreviewsByRepositoryHeadTrunkValue - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&v); err != nil { - return v, err - } - if dec.More() { - return v, errors.New("text after the value") - } - return v, nil -} - -// TryInputMembersItem is a type of the contract. -type TryInputMembersItem struct { - // the full commit to try, 40 or 64 hex digits - Head string `json:"head"` - // / - Repository string `json:"repository"` - // the trunk commit to merge it onto, or the trunk's branch for its head now; the trunk's head when absent - Trunk string `json:"trunk,omitempty"` -} - -// TryInput is what try takes. -type TryInput struct { - // the full commit to try, 40 or 64 hex digits, for a try of one member (instead of members) - Head string `json:"head,omitempty"` - // unit or integration: only this level; both when absent - Level string `json:"level,omitempty"` - // the group: one repository at most once, each at the exact commit asked and the trunk commit it is merged onto; instead of repository and head - Members []TryInputMembersItem `json:"members,omitempty"` - // /, for a try of one member (instead of members) - Repository string `json:"repository,omitempty"` - // a go test -run pattern: only those tests run, and nothing is kept - Tests string `json:"tests,omitempty"` - // the trunk commit to merge it onto, or the trunk's branch for its head now; the trunk's head when absent - Trunk string `json:"trunk,omitempty"` -} - -// TryAnswer is what try answers. -type TryAnswer struct { - // the try's id - ID string `json:"id"` - // its state - State string `json:"state,omitempty"` -} - -// TryReadInput is what try-read takes. -type TryReadInput struct { - // the try's id, try- - ID string `json:"id"` -} - -// TryReadAnswer is what try-read answers. -type TryReadAnswer = map[string]any - -// TryEndInput is what try-end takes. -type TryEndInput struct { - // the try's id, try- - ID string `json:"id"` - // why it is ended, in words - Why string `json:"why"` -} - -// TryEndAnswer is what try-end answers. -type TryEndAnswer struct { - // it is ended - Ended bool `json:"ended"` - // the try - ID string `json:"id"` -} - -// InputInput is what input takes. -type InputInput struct { - // the input's sha256, 64 lowercase hex digits, as the run ask names it - Digest string `json:"digest"` -} - -// InputAnswer is what input answers. -type InputAnswer struct { - // the digest asked - Digest string `json:"digest"` - // the input's exact bytes, base64, so no encoding on the way changes a byte the digest covers - Input string `json:"input"` -} - -// VerdictHolderEventSeriesItem is a type of the contract. -type VerdictHolderEventSeriesItem struct { - // its artifact, or the package's ecosystem name as the module's packages name it - Artifact string `json:"artifact"` - // the series on trunk - From string `json:"from"` - // the module whose artifact's series changes - Module string `json:"module"` - // the series the checked head gives it - To string `json:"to"` -} - -// VerdictHolderEvent is the body of the holder's event verdict. -type VerdictHolderEvent struct { - // the commit checked - Head string `json:"head"` - // the generated files out of step with their seat definitions, each path (design 26) - OutOfStep []string `json:"out-of-step,omitempty"` - // the pull request's number, for a pull request's check - Pull int64 `json:"pull,omitempty"` - // owner/repo - Repository string `json:"repository"` - // each change of an artifact's series the checked head makes against trunk's manifests, once each (design 32); absent when it changes none - Series []VerdictHolderEventSeriesItem `json:"series,omitempty"` - // pass or fail - Verdict string `json:"verdict"` -} - -// TagWantedHolderEvent is the body of the holder's event tag-wanted. -type TagWantedHolderEvent struct { - // the pull request's branch: its tag is removed when the branch ends - Branch string `json:"branch"` - // its head - Head string `json:"head"` - // the group member - Module string `json:"module"` - // the package it declares - Package string `json:"package"` - // the pull request - Pull int64 `json:"pull,omitempty"` - // owner/repo - Repository string `json:"repository"` - // the tag wanted - Tag string `json:"tag,omitempty"` -} - -// VerdictsByGroupHeadsValueComposition is a type of the contract. -type VerdictsByGroupHeadsValueComposition struct { - Fpset string `json:"fpset,omitempty"` - Machines []string `json:"machines,omitempty"` - Sdk string `json:"sdk,omitempty"` - // not-run, pass or fail - State string `json:"state"` - Words string `json:"words,omitempty"` -} - -// VerdictsByGroupHeadsValueFailed is a type of the contract. -type VerdictsByGroupHeadsValueFailed struct { - Machine string `json:"machine,omitempty"` - Test string `json:"test,omitempty"` - Words string `json:"words,omitempty"` -} - -// VerdictsByGroupHeadsValueFailuresItem is a type of the contract. -type VerdictsByGroupHeadsValueFailuresItem struct { - Machine string `json:"machine,omitempty"` - Test string `json:"test,omitempty"` - Words string `json:"words,omitempty"` -} - -// VerdictsByGroupHeadsValueJudgedByHeadsItem is a type of the contract. -type VerdictsByGroupHeadsValueJudgedByHeadsItem struct { - Commit string `json:"commit"` - Repository string `json:"repository"` -} - -// VerdictsByGroupHeadsValueJudgedByProgram is a type of the contract. -type VerdictsByGroupHeadsValueJudgedByProgram struct { - Commit string `json:"commit"` - Dir string `json:"dir"` - Package string `json:"package"` - Repository string `json:"repository"` -} - -// VerdictsByGroupHeadsValueJudgedBy is the checking program that judged the check: where it was built, the key its build is kept under, whether this check's run built it, and the heads it read (ADR 0644, ADR 0656). Absent in a value written before it. -type VerdictsByGroupHeadsValueJudgedBy struct { - Built bool `json:"built,omitempty"` - Heads []VerdictsByGroupHeadsValueJudgedByHeadsItem `json:"heads"` - // program- and 16 hex digits - Key string `json:"key"` - Program VerdictsByGroupHeadsValueJudgedByProgram `json:"program"` -} - -// VerdictsByGroupHeadsValueLevelsValue is a type of the contract. -type VerdictsByGroupHeadsValueLevelsValue struct { - Kept int64 `json:"kept"` - Run int64 `json:"run"` - Skipped int64 `json:"skipped,omitempty"` -} - -// VerdictsByGroupHeadsValueSeriesItem is a type of the contract. -type VerdictsByGroupHeadsValueSeriesItem struct { - Artifact string `json:"artifact"` - From string `json:"from"` - Module string `json:"module"` - To string `json:"to"` -} - -// VerdictsByGroupHeadsValueTook is milliseconds. -type VerdictsByGroupHeadsValueTook struct { - Clone int64 `json:"clone,omitempty"` - Composition int64 `json:"composition,omitempty"` - IntegrationRun int64 `json:"integration-run,omitempty"` - IntegrationSetup int64 `json:"integration-setup,omitempty"` - Lint int64 `json:"lint,omitempty"` - Queue int64 `json:"queue,omitempty"` - Unit int64 `json:"unit,omitempty"` -} - -// VerdictsByGroupHeadsValue is one value of the state verdicts under the key .. -type VerdictsByGroupHeadsValue struct { - Beside map[string]string `json:"beside,omitempty"` - CarriedFrom map[string]string `json:"carried-from,omitempty"` - Composition *VerdictsByGroupHeadsValueComposition `json:"composition,omitempty"` - Failed *VerdictsByGroupHeadsValueFailed `json:"failed,omitempty"` - Failures []VerdictsByGroupHeadsValueFailuresItem `json:"failures,omitempty"` - Group string `json:"group"` - // 12 hex digits - Heads string `json:"heads"` - // the checking program that judged the check: where it was built, the key its build is kept under, whether this check's run built it, and the heads it read (ADR 0644, ADR 0656). Absent in a value written before it - JudgedBy *VerdictsByGroupHeadsValueJudgedBy `json:"judged-by,omitempty"` - // by level, unit or integration - Levels map[string]VerdictsByGroupHeadsValueLevelsValue `json:"levels,omitempty"` - Machines []string `json:"machines,omitempty"` - OverBound []string `json:"over-bound,omitempty"` - Sdk string `json:"sdk,omitempty"` - Series []VerdictsByGroupHeadsValueSeriesItem `json:"series,omitempty"` - // running, pass or fail - State string `json:"state"` - // milliseconds - Took *VerdictsByGroupHeadsValueTook `json:"took,omitempty"` - // repository to the trunk head merged onto - Trunk map[string]string `json:"trunk,omitempty"` - // the layout version - V int64 `json:"v,omitempty"` -} - -// KeptByLevelDigestValue is one value of the state kept under the key .. -type KeptByLevelDigestValue struct { - // when (RFC 3339 time) - At string `json:"at,omitempty"` - Level string `json:"level"` - Package string `json:"package"` - Repository string `json:"repository"` - // pass: a failure is never kept - State string `json:"state"` - Tests int64 `json:"tests,omitempty"` - Took int64 `json:"took,omitempty"` - Toolchain string `json:"toolchain,omitempty"` - // the layout version - V int64 `json:"v,omitempty"` -} - -// OwedByRepositoryNumberHeadActValue is one value of the state owed under the key .... -type OwedByRepositoryNumberHeadActValue struct { - // status=, view, note or comment. - Act string `json:"act"` - Context string `json:"context,omitempty"` - Ref string `json:"ref,omitempty"` - State string `json:"state,omitempty"` - Target string `json:"target,omitempty"` - Text string `json:"text,omitempty"` - // the layout version - V int64 `json:"v,omitempty"` -} - -// PreviewsByRepositoryHeadTrunkValueModulesValue is a type of the contract. -type PreviewsByRepositoryHeadTrunkValueModulesValue struct { - Locks map[string]string `json:"locks,omitempty"` - // manifest digest - Manifest string `json:"manifest"` - Packages []string `json:"packages,omitempty"` - Paths map[string]string `json:"paths,omitempty"` - Tree string `json:"tree"` -} - -// PreviewsByRepositoryHeadTrunkValue is one value of the state previews under the key ... -type PreviewsByRepositoryHeadTrunkValue struct { - // files in conflict - Conflicts []string `json:"conflicts,omitempty"` - Head string `json:"head"` - // the merge commit - Merge string `json:"merge,omitempty"` - // by directory - Modules map[string]PreviewsByRepositoryHeadTrunkValueModulesValue `json:"modules,omitempty"` - Repository string `json:"repository"` - Trunk string `json:"trunk"` - // the layout version - V int64 `json:"v,omitempty"` -} diff --git a/contracts/go/seats/meshdeliverychecker/contract_test.go b/contracts/go/seats/meshdeliverychecker/contract_test.go deleted file mode 100644 index 65d9e0c4..00000000 --- a/contracts/go/seats/meshdeliverychecker/contract_test.go +++ /dev/null @@ -1,82 +0,0 @@ -// Code generated by seatgen from the seat definitions; DO NOT EDIT. - -package meshdeliverychecker - -import ( - "encoding/json" - "reflect" - "regexp" - "testing" -) - -// TestFixtures reads every fixture of seats/mesh-delivery-checker/fixtures: each decodes into its type, a field the type does not -// know is dropped (ADR 0608 §6), and it reads back as its wire says; each subject named is the contract's. -func TestFixtures(t *testing.T) { - cases := []struct { - name, subject, given, wire string - decode func([]byte) (any, error) - subjectIs *regexp.Regexp - }{ - {name: "verdict on the checker's own family", subject: "mesh.stream.mesh-delivery-checker.event.verdict", given: "{\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"out-of-step\":[\"go/seats/meshbroker/contract.go\"],\"pull\":3,\"repository\":\"novox/mesh-contracts\",\"series\":[{\"artifact\":\"go\",\"from\":\"0.1\",\"module\":\"mesh-contracts\",\"to\":\"0.2\"}],\"verdict\":\"fail\"}", wire: "{\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"out-of-step\":[\"go/seats/meshbroker/contract.go\"],\"pull\":3,\"repository\":\"novox/mesh-contracts\",\"series\":[{\"artifact\":\"go\",\"from\":\"0.1\",\"module\":\"mesh-contracts\",\"to\":\"0.2\"}],\"verdict\":\"fail\"}", - decode: func(b []byte) (any, error) { var v VerdictHolderEvent; err := json.Unmarshal(b, &v); return v, err }, subjectIs: regexp.MustCompile("^mesh\\.stream\\.[a-z0-9-]+\\.event\\.verdict$")}, - {name: "tag-wanted on the checker's own family", subject: "mesh.stream.mesh-delivery-checker.event.tag-wanted", given: "{\"branch\":\"feat/registry\",\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"module\":\"mesh-sdk\",\"package\":\"go\",\"pull\":170,\"repository\":\"novox/mesh-sdk\",\"tag\":\"registry-1966\"}", wire: "{\"branch\":\"feat/registry\",\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"module\":\"mesh-sdk\",\"package\":\"go\",\"pull\":170,\"repository\":\"novox/mesh-sdk\",\"tag\":\"registry-1966\"}", - decode: func(b []byte) (any, error) { var v TagWantedHolderEvent; err := json.Unmarshal(b, &v); return v, err }, subjectIs: regexp.MustCompile("^mesh\\.stream\\.[a-z0-9-]+\\.event\\.tag-wanted$")}, - {name: "preview.written on the checker's stream", subject: "mesh.stream.mesh-delivery-checker.event.preview.written", given: "{}", wire: "{}", - decode: func(b []byte) (any, error) { var v map[string]any; err := json.Unmarshal(b, &v); return v, err }, subjectIs: regexp.MustCompile("^mesh\\.stream\\.[a-z0-9-]+\\.event\\.preview\\.written$")}, - {name: "a running verdict", subject: "", given: "{\"group\":\"feat/registry\",\"heads\":\"0123456789ab\",\"state\":\"running\",\"v\":1}", wire: "{\"group\":\"feat/registry\",\"heads\":\"0123456789ab\",\"state\":\"running\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v VerdictsByGroupHeadsValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, - {name: "a pass judged by the checking program built at a mesh-delivery head", subject: "", given: "{\"group\":\"feat/registry\",\"heads\":\"0123456789ab\",\"judged-by\":{\"built\":true,\"heads\":[{\"commit\":\"0123456789abcdef0123456789abcdef01234567\",\"repository\":\"novox/mesh-delivery\"}],\"key\":\"program-0123456789abcdef\",\"program\":{\"commit\":\"0123456789abcdef0123456789abcdef01234567\",\"dir\":\"modules/mesh-delivery-checker\",\"package\":\"./cmd/mesh-checking-program\",\"repository\":\"novox/mesh-delivery\"}},\"levels\":{\"integration\":{\"kept\":0,\"run\":3},\"unit\":{\"kept\":0,\"run\":12}},\"state\":\"pass\",\"v\":1}", wire: "{\"group\":\"feat/registry\",\"heads\":\"0123456789ab\",\"judged-by\":{\"built\":true,\"heads\":[{\"commit\":\"0123456789abcdef0123456789abcdef01234567\",\"repository\":\"novox/mesh-delivery\"}],\"key\":\"program-0123456789abcdef\",\"program\":{\"commit\":\"0123456789abcdef0123456789abcdef01234567\",\"dir\":\"modules/mesh-delivery-checker\",\"package\":\"./cmd/mesh-checking-program\",\"repository\":\"novox/mesh-delivery\"}},\"levels\":{\"integration\":{\"kept\":0,\"run\":3},\"unit\":{\"kept\":0,\"run\":12}},\"state\":\"pass\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v VerdictsByGroupHeadsValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, - {name: "a kept pass", subject: "", given: "{\"at\":\"2026-11-05T10:00:00Z\",\"level\":\"unit\",\"package\":\"go/keys\",\"repository\":\"novox/mesh-sdk\",\"state\":\"pass\",\"tests\":4,\"took\":120,\"toolchain\":\"go1.26\",\"v\":1}", wire: "{\"at\":\"2026-11-05T10:00:00Z\",\"level\":\"unit\",\"package\":\"go/keys\",\"repository\":\"novox/mesh-sdk\",\"state\":\"pass\",\"tests\":4,\"took\":120,\"toolchain\":\"go1.26\",\"v\":1}", - decode: func(b []byte) (any, error) { var v KeptByLevelDigestValue; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, - {name: "the status owed for a head", subject: "", given: "{\"act\":\"status=mesh/check\",\"context\":\"mesh/check\",\"state\":\"success\",\"v\":1}", wire: "{\"act\":\"status=mesh/check\",\"context\":\"mesh/check\",\"state\":\"success\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v OwedByRepositoryNumberHeadActValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, - {name: "a preview", subject: "", given: "{\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"merge\":\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\",\"repository\":\"novox/mesh-sdk\",\"trunk\":\"dddddddddddddddddddddddddddddddddddddddd\",\"v\":1}", wire: "{\"head\":\"cccccccccccccccccccccccccccccccccccccccc\",\"merge\":\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\",\"repository\":\"novox/mesh-sdk\",\"trunk\":\"dddddddddddddddddddddddddddddddddddddddd\",\"v\":1}", - decode: func(b []byte) (any, error) { - var v PreviewsByRepositoryHeadTrunkValue - err := json.Unmarshal(b, &v) - return v, err - }, subjectIs: nil}, - {name: "a checking program asks its input by its digest", subject: "", given: "{\"digest\":\"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\"}", wire: "{\"digest\":\"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\"}", - decode: func(b []byte) (any, error) { var v InputInput; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, - {name: "the input answered as its exact bytes, base64", subject: "", given: "{\"digest\":\"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\",\"input\":\"eyJ2IjoxfQ==\"}", wire: "{\"digest\":\"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\",\"input\":\"eyJ2IjoxfQ==\"}", - decode: func(b []byte) (any, error) { var v InputAnswer; err := json.Unmarshal(b, &v); return v, err }, subjectIs: nil}, - } - for _, c := range cases { - v, err := c.decode([]byte(c.given)) - if err != nil { - t.Errorf("%s: does not decode: %v", c.name, err) - continue - } - back, err := json.Marshal(v) - if err != nil { - t.Errorf("%s: does not encode: %v", c.name, err) - continue - } - var got, want any - if err := json.Unmarshal(back, &got); err != nil { - t.Fatal(err) - } - if err := json.Unmarshal([]byte(c.wire), &want); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(got, want) { - t.Errorf("%s: reads back as %s, want %s", c.name, back, c.wire) - } - if c.subjectIs != nil && !c.subjectIs.MatchString(c.subject) { - t.Errorf("%s: subject %q is not the contract's %s", c.name, c.subject, c.subjectIs) - } - } -} diff --git a/contracts/go/seats/meshdeliverychecker/go.mod b/contracts/go/seats/meshdeliverychecker/go.mod deleted file mode 100644 index d50dcda1..00000000 --- a/contracts/go/seats/meshdeliverychecker/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker - -go 1.26.0 diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits.go deleted file mode 100644 index 5ff6f835..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits.go +++ /dev/null @@ -1,237 +0,0 @@ -package main - -import ( - "context" - "maps" - "slices" - "strings" - - meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - - "mesh-delivery-assigner/internal/acting" - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/assigngraph" - "mesh-delivery-assigner/internal/assignment" - "mesh-delivery-assigner/internal/buckets" -) - -// The waits for an acting part's new version in the steps the assigner holds (design 47 G §266, §268, §276): an entry of the assignment -// graph whose acting edge names composing or placing waits, its want held, until the part's version runs on the edge's node. The wait is -// kept in the state `waits` under `acting...`, said as `wait-held` when it starts and `wait-ended` (ran, released, -// replaced) when it ends; `assigner_waits` reads it and `assigner_release` with how `now` lets the entry move with the part's running -// version. A node away starts no wait: its clock starts when the node is present (§268). - -// actingOf is the Module.Acting of module: the held acting edges of its entry on a node, those whose wait was not released left out. -func (p *passer) actingOf(module string, released map[string]bool) func(node string) []assign.ActingEdge { - return func(node string) []assign.ActingEdge { - var out []assign.ActingEdge - if p.engineOn(node) == module { - return nil // the node-engine's own want is held by no acting edge, so it is written first (§268, A-323, A-330) - } - for _, e := range p.entryEdges(node, module) { - if acting.Held(e.Step) && !released[acting.Key(node, module, e.Module)+"@"+e.Fp] { - out = append(out, assign.ActingEdge{Node: e.Node, Part: e.Module, Fp: e.Fp, Step: e.Step, Seat: e.Seat}) - } - } - return out - } -} - -// engineOn is the module that holds the seat node-engine on node in the revision the pass computed, read from the acting edges' -// seat (A-330); "" when none is read. -func (p *passer) engineOn(node string) string { - if p.graph == nil { - return "" - } - return assigngraph.Engines(slices.Collect(maps.Values(p.graph.computed.Entries)))[node] -} - -// entryEdges are the acting edges of the entry of module on node in the revision the pass computed. -func (p *passer) entryEdges(node, module string) []seat.Edge { - g := p.graph - if g == nil { - return nil - } - for _, ref := range g.computed.Revision.Entries { - if ref.Node == node && ref.Module == module { - var out []seat.Edge - for _, e := range g.computed.Entries[ref.Digest].Edges { - if e.Kind == "acting" { - out = append(out, e) - } - } - return out - } - } - return nil -} - -// actingReleased is the waits released by hand and not yet run, by `@`. -func (p *passer) actingReleased() map[string]bool { - out := map[string]bool{} - for key, raw := range p.w.read(buckets.Waits()) { - if w, err := acting.Decode(raw); err == nil && w.Released && (acting.Is(key) || acting.IsOrder(key)) { - out[key+"@"+w.Fp] = true - } - } - return out -} - -// actingWaits keeps the acting waits after the pass's wants: it starts the wait of each entry held for a part's version, ends the -// wait of one that ran, was replaced or was released, and says each. -func (p *passer) actingWaits(ctx context.Context, n assign.Nodes, wanted map[string]map[string]meshwants.Want) { - g := p.graph - if g == nil || p.kvs[buckets.Waits()] == nil { - return - } - now := p.clock() - kept := p.w.read(buckets.Waits()) - standing := map[string]bool{} - engines := assigngraph.Engines(slices.Collect(maps.Values(g.computed.Entries))) - held := map[string]bool{} // `/` of each entry held for a part's version - for _, ref := range g.computed.Revision.Entries { - entry := g.computed.Entries[ref.Digest] - if w, ok := wanted[ref.Module][ref.Node]; ok && w.Fp == entry.Fp { - continue // the entry's version is wanted already: nothing moves, so nothing waits - } - if engines[ref.Node] == ref.Module { - continue // the node-engine's own want waits for no acting part (§268, A-323, A-330) - } - for _, e := range entry.Edges { - if e.Kind != "acting" || !acting.Held(e.Step) { - continue - } - key := acting.Key(ref.Node, ref.Module, e.Module) - if took, runs := n.ActingRuns(assign.ActingEdge{Node: e.Node, Part: e.Module, Fp: e.Fp, Step: e.Step, Seat: e.Seat}); runs { - if took != e.Fp && kept[key] != nil { - p.logf("the wait of %s on %s for %s ends: %s runs %s, which serves the seat %s, though its newest is %s (design 47 G A-426)", ref.Module, ref.Node, e.Module, e.Node, took, e.Seat, e.Fp) - } - continue - } - standing[key] = true - held[ref.Node+"/"+ref.Module] = true - if raw := kept[key]; raw != nil { - if w, err := acting.Decode(raw); err == nil && w.Entry == entry.Fp && w.Fp == e.Fp { - p.actingReleaseSaid(ctx, key, w) - continue - } - } - if !n.Online[ref.Node] { - continue // a node away holds only itself, and its clock has not started (§268) - } - w := acting.Wait{Step: e.Step, Node: ref.Node, Module: ref.Module, Part: e.Module, PartNode: e.Node, Fp: e.Fp, Entry: entry.Fp, - Since: now, ClockFrom: now, Bound: acting.BoundOf(e).String()} - if err := p.wantsSay(seat.EventWaitHeld, acting.Body(w, assignment.Module()), key+"."+e.Fp+"."+entry.Fp); err != nil { - p.logf("the wait of %s on %s for %s is not said, and is said at the next pass: %v", ref.Module, ref.Node, e.Module, err) - continue - } - p.put(ctx, buckets.Waits(), key, func() ([]byte, error) { return acting.Encode(w) }) - p.logf("the want of %s on %s is held: its acting part %s (step %s) does not run the version %s on %s; the wait %s ends when it does, or by assigner_release", ref.Module, ref.Node, e.Module, e.Step, e.Fp, e.Node, key) - } - } - p.actingCycles(g.computed.Entries, engines, held, n) - for key, raw := range kept { - if !acting.Is(key) || standing[key] { - continue - } - w, err := acting.Decode(raw) - if err != nil { - continue - } - how := acting.HowRan - if w.Released { - how = acting.HowReleased - } else if e, ok := p.edgeOf(w); !ok { - how = acting.HowReplaced - } else if _, runs := n.ActingRuns(e); !runs { - how = acting.HowReplaced - } - if !w.EndSaid { - body := acting.Body(w, assignment.Module()) - body.How = how - if err := p.wantsSay(seat.EventWaitEnded, body, key+"."+w.Fp+"."+w.Entry+"."+how); err != nil { - p.logf("the end of the wait of %s on %s for %s is not said, and is said at the next pass: %v", w.Module, w.Node, w.Part, err) - continue - } - } - p.remove(ctx, buckets.Waits(), key) - } -} - -// actingReleaseSaid says wait-ended of a wait released by hand, once, and keeps its record so the entry is not held again. -func (p *passer) actingReleaseSaid(ctx context.Context, key string, w acting.Wait) { - if !w.Released || w.EndSaid { - return - } - body := acting.Body(w, assignment.Module()) - body.How = acting.HowReleased - body.By = w.By - if err := p.wantsSay(seat.EventWaitEnded, body, key+"."+w.Fp+"."+w.Entry+"."+acting.HowReleased); err != nil { - p.logf("the release of the wait of %s on %s for %s is not said, and is said at the next pass: %v", w.Module, w.Node, w.Part, err) - return - } - w.EndSaid = true - p.put(ctx, buckets.Waits(), key, func() ([]byte, error) { return acting.Encode(w) }) -} - -// edgeOf is the edge a kept wait stands on, as an acting edge. -func (p *passer) edgeOf(w acting.Wait) (assign.ActingEdge, bool) { - return assign.ActingEdge{Node: w.PartNode, Part: w.Part, Fp: w.Fp, Step: w.Step}, true -} - -// actingCycles writes the finding acting-cycle of each held want whose holding edges lie on a cycle no rule breaks, and clears it of -// every other want, a finding a pass before or another process wrote included (design 47 G A-323, A-330). An edge holds while its -// step is the assigner's, its part's version does not run and its wait was not released; the node-engine's own want is held by none. -func (p *passer) actingCycles(entries map[string]seat.AssignmentEntry, engines map[string]string, held map[string]bool, n assign.Nodes) { - if p.findings.Put == nil { - return - } - released := p.actingReleased() - cycles := assigngraph.HeldCycles(slices.Collect(maps.Values(entries)), func(e seat.AssignmentEntry, edge seat.Edge) bool { - return held[e.Node+"/"+e.Module] && engines[e.Node] != e.Module && edge.Kind == "acting" && acting.Held(edge.Step) && - !released[acting.Key(e.Node, e.Module, edge.Module)+"@"+edge.Fp] && !actingRunsOf(n, edge) - }) - raised := map[string]bool{} - for _, at := range slices.Sorted(maps.Keys(cycles)) { - node, module, _ := strings.Cut(at, "/") - o := assign.ActingCycleFinding(node, module, cycles[at]) - key := mcseatKey(o) - raised[key] = true - if p.said[key+"="+o.Summary] { - continue // written already as it stands - } - if err := p.findings.Put(o.Finding(p.clock())); err != nil { - p.logf("the finding %s is not written: %v", key, err) - continue - } - p.said[key+"="+o.Summary] = true - } - if p.findings.Keys == nil || p.findings.Delete == nil { - return - } - keys, err := p.findings.Keys() - if err != nil { - p.logf("the findings %s are not read, so none is cleared this pass: %v", assign.KindActingCycle, err) - return - } - for _, key := range keys { - if strings.HasSuffix(key, "."+assign.KindActingCycle) && !raised[key] { - if err := p.findings.Delete(key); err != nil { - p.logf("the finding %s is not cleared: %v", key, err) - continue - } - for said := range p.said { - if strings.HasPrefix(said, key+"=") { - delete(p.said, said) - } - } - } - } -} - -// actingRunsOf says whether an assignment edge's acting part runs a build serving it (A-426). -func actingRunsOf(n assign.Nodes, edge seat.Edge) bool { - _, runs := n.ActingRuns(assign.ActingEdge{Node: edge.Node, Part: edge.Module, Fp: edge.Fp, Step: edge.Step, Seat: edge.Seat}) - return runs -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits_test.go deleted file mode 100644 index 8c746b80..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/actingwaits_test.go +++ /dev/null @@ -1,186 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - - "mesh-delivery-assigner/internal/acting" - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/buckets" -) - -// Design 47 G §266, §268, §276 (AS9): an entry with an acting edge in composing waits for the part's version to run on the edge's node; -// the wait is kept, said as wait-held with its bound and clock-from, read by assigner_waits, released by assigner_release with how -// `now` and ended when the version runs. -func TestAnEntryWaitsForItsActingPartAndTheWaitIsKeptSaidAndEnded(t *testing.T) { - now := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) - p := newPasser(kvSet{buckets.Waits(): &memKV{values: map[string][]byte{}}}, now) - var events []string - var bodies []meshwants.Wait - p.publishWants = func(event string, body any, id string) error { - events = append(events, event) - bodies = append(bodies, body.(meshwants.Wait)) - return nil - } - entry := meshwants.AssignmentEntry{Node: "ace", Module: "nats", Fp: fpTest, Edges: []meshwants.Edge{ - {Kind: "acting", Node: "ace", Module: "mesh-host", Fp: "src2:new", Seat: "node-engine", Step: "compose", Bound: "15m"}, - {Kind: "acting", Node: "ace", Module: "indexer", Fp: "src2:idx", Seat: "mesh-catalog", Step: "take-in"}}} - p.graph = &graphKeeper{} - p.graph.computed.Revision.Entries = []meshwants.NodeEntryRef{{Node: "ace", Module: "nats", Digest: "d1"}} - p.graph.computed.Entries = map[string]meshwants.AssignmentEntry{"d1": entry} - // The old build of mesh-host serves no seat of the edge here, so the entry waits (A-426: one serving it ends the wait). - notServing := func(nodeengine.Running, string) (bool, bool) { return false, true } - n := assign.Nodes{Online: map[string]bool{"ace": true}, Running: map[string]map[string]nodeengine.Running{"ace": {"mesh-host": {Fingerprint: "src2:old"}}}, Serves: notServing} - wanted := map[string]map[string]mwc.Want{"nats": {"ace": {Fp: "src2:older"}}} - ctx := context.Background() - - if got := p.actingOf("nats", nil)("ace"); len(got) != 1 || got[0].Part != "mesh-host" { - t.Fatalf("the assigner holds composing only: %+v", got) - } - p.actingWaits(ctx, n, wanted) - p.actingWaits(ctx, n, wanted) // said once - if len(events) != 1 || events[0] != meshwants.EventWaitHeld { - t.Fatalf("events %v", events) - } - b := bodies[0] - if b.Step != "compose" || b.Part != "mesh-host" || b.Fp != "src2:new" || b.Bound != "15m0s" || b.ClockFrom == nil || !b.ClockFrom.Equal(now) || b.Owner == "" { - t.Errorf("wait-held %+v", b) - } - key := acting.Key("ace", "nats", "mesh-host") - if _, err := acting.Decode(p.w.read(buckets.Waits())[key]); err != nil { - t.Fatalf("the wait is kept: %v", err) - } - // a release by hand lets the entry move, and ends the wait once - w, _ := acting.Decode(p.w.read(buckets.Waits())[key]) - w.Released = true - raw, _ := acting.Encode(w) - p.w.put(buckets.Waits(), key, raw) - if got := p.actingOf("nats", p.actingReleased())("ace"); len(got) != 0 { - t.Errorf("a released wait still holds: %+v", got) - } - p.actingWaits(ctx, n, wanted) - p.actingWaits(ctx, n, wanted) - if len(events) != 2 || events[1] != meshwants.EventWaitEnded || bodies[1].How != acting.HowReleased { - t.Fatalf("events %v bodies %+v", events, bodies) - } - // the version runs: the record goes, said nothing more - n.Running["ace"]["mesh-host"] = nodeengine.Running{Fingerprint: "src2:new"} - p.actingWaits(ctx, n, wanted) - if len(p.w.read(buckets.Waits())) != 0 || len(events) != 2 { - t.Errorf("kept %v events %v", p.w.read(buckets.Waits()), events) - } -} - -func TestAnActingWaitOfANodeAwayStartsNoClockAndAnEntryWantedAlreadyWaitsNot(t *testing.T) { - p := newPasser(kvSet{buckets.Waits(): &memKV{values: map[string][]byte{}}}, time.Now()) - p.publishWants = func(string, any, string) error { t.Fatal("said"); return nil } - p.graph = &graphKeeper{} - p.graph.computed.Revision.Entries = []meshwants.NodeEntryRef{{Node: "ace", Module: "nats", Digest: "d1"}} - p.graph.computed.Entries = map[string]meshwants.AssignmentEntry{"d1": {Node: "ace", Module: "nats", Fp: "src2:a", Edges: []meshwants.Edge{ - {Kind: "acting", Node: "ace", Module: "mesh-host", Fp: "src2:new", Step: "compose"}}}} - n := assign.Nodes{Online: map[string]bool{}} - p.actingWaits(context.Background(), n, map[string]map[string]mwc.Want{}) - n.Online["ace"] = true - p.actingWaits(context.Background(), n, map[string]map[string]mwc.Want{"nats": {"ace": {Fp: "src2:a"}}}) - if len(p.w.read(buckets.Waits())) != 0 || strings.Contains(p.graph.computed.Entries["d1"].Fp, "x") { - t.Errorf("kept %v", p.w.read(buckets.Waits())) - } -} - -func TestTheToolsReadAndReleaseAnActingWait(t *testing.T) { - var acts []string - s := newPutTools(memStore{waitsState: &memKV{values: map[string][]byte{}}, buckets.DeliveryWaitsState: &memKV{values: map[string][]byte{}}, buckets.DeliveredAgainState: &memKV{values: map[string][]byte{}}}, func(key string, body any) (json.RawMessage, error) { - acts = append(acts, key) - return json.RawMessage(`{"ok":true,"log":"logged"}`), nil - }) - w := acting.Wait{Step: "compose", Node: "ace", Module: "nats", Part: "mesh-host", PartNode: "ace", Fp: "src2:new", Entry: "src2:a", Since: time.Now(), ClockFrom: time.Now(), Bound: "30m0s"} - raw, _ := acting.Encode(w) - s.st.put(waitsState, acting.Key("ace", "nats", "mesh-host"), raw) - got, err := s.doWaits(map[string]any{"module": "nats"}) - if list := got.(map[string]any)["waits"].([]any); err != nil || len(list) != 1 || list[0].(actingSaid).BoundEnds == "" { - t.Fatalf("assigner_waits = %+v %v", got, err) - } - if _, err := s.doRelease(map[string]any{"node": "ace", "module": "nats", "how": "now"}, "x"); err == nil { - t.Error("a release with no why is taken") - } - if _, err := s.doRelease(map[string]any{"node": "ace", "module": "nats", "how": "now", "why": "the new engine is known good"}, "shanks.node-tools"); err != nil || len(acts) == 0 { - t.Fatalf("%v %v", err, acts) - } - raw, _, _, _ = s.st.get(waitsState, acting.Key("ace", "nats", "mesh-host")) - if r, _ := acting.Decode(raw); !r.Released || !strings.Contains(r.By, "known good") { - t.Errorf("wait = %+v", r) - } -} - -// Design 47 G §268, A-323, A-330: the assigner and the node-engine on a node are one step: the node-engine's own want waits for no -// acting part, so it moves first, and the node's other wants, the assigner's own included, wait for the new node-engine; a want held -// on a cycle no rule breaks is the finding acting-cycle (scope module, id .) naming each edge's step, cleared by the -// next pass that finds it on none. -func TestTheNodeEngineIsWantedFirstAndAWantHeldOnACycleIsAFinding(t *testing.T) { - p := newPasser(kvSet{buckets.Waits(): &memKV{values: map[string][]byte{}}}, time.Date(2026, 11, 13, 2, 0, 0, 0, time.UTC)) - p.publishWants = func(string, any, string) error { return nil } - findings := map[string]mcseat.Finding{} - p.findings = assign.Findings{ - Put: func(f mcseat.Finding) error { findings[f.Scope+"."+f.ID+"."+f.Kind] = f; return nil }, - Delete: func(key string) error { delete(findings, key); return nil }, - Keys: func() ([]string, error) { - var keys []string - for k := range findings { - keys = append(keys, k) - } - return keys, nil - }} - compose := meshwants.Edge{Kind: "acting", Node: "ace", Module: "mesh-host", Fp: "src2:engine", Seat: "node-engine", Step: "compose"} - place := meshwants.Edge{Kind: "acting", Node: "ace", Module: "assigner", Fp: "src2:assigner", Seat: "mesh-wants", Step: "place"} - entries := map[string]meshwants.AssignmentEntry{ - "d1": {Node: "ace", Module: "mesh-host", Fp: "src2:engine", Edges: []meshwants.Edge{place}}, - "d2": {Node: "ace", Module: "assigner", Fp: "src2:assigner", Edges: []meshwants.Edge{compose}}, - "d3": {Node: "ace", Module: "app", Fp: "src2:app", Edges: []meshwants.Edge{compose, place}}, - } - p.graph = &graphKeeper{} - for d, e := range entries { - p.graph.computed.Revision.Entries = append(p.graph.computed.Revision.Entries, meshwants.NodeEntryRef{Node: e.Node, Module: e.Module, Digest: d}) - } - p.graph.computed.Entries = entries - n := assign.Nodes{Online: map[string]bool{"ace": true}, Running: map[string]map[string]nodeengine.Running{"ace": { - "mesh-host": {Fingerprint: "src2:old"}, "assigner": {Fingerprint: "src2:old"}}}, Serves: func(nodeengine.Running, string) (bool, bool) { return false, true }} - wanted := map[string]map[string]mwc.Want{} - if got := p.actingOf("mesh-host", nil)("ace"); len(got) != 0 { - t.Errorf("the node-engine's want is held by %+v, want by no acting edge", got) - } - if got := p.actingOf("assigner", nil)("ace"); len(got) != 1 || got[0].Part != "mesh-host" { - t.Errorf("the assigner's own want is held by %+v, want by the new node-engine", got) - } - p.actingWaits(context.Background(), n, wanted) - kept := p.w.read(buckets.Waits()) - if kept[acting.Key("ace", "mesh-host", "assigner")] != nil || kept[acting.Key("ace", "assigner", "mesh-host")] == nil || kept[acting.Key("ace", "app", "mesh-host")] == nil { - t.Errorf("the waits kept: %v", kept) - } - if len(findings) != 0 { - t.Errorf("the cycle the node-engine's want breaks is a finding: %v", findings) - } - // Two parts holding each other across compose and place, neither the node-engine: no rule breaks it. - entries["d4"] = meshwants.AssignmentEntry{Node: "ace", Module: "x", Fp: "src2:x", Edges: []meshwants.Edge{{Kind: "acting", Node: "ace", Module: "y", Fp: "src2:y", Seat: "s-y", Step: "place"}}} - entries["d5"] = meshwants.AssignmentEntry{Node: "ace", Module: "y", Fp: "src2:y", Edges: []meshwants.Edge{{Kind: "acting", Node: "ace", Module: "x", Fp: "src2:x", Seat: "s-x", Step: "compose"}}} - p.graph.computed.Revision.Entries = append(p.graph.computed.Revision.Entries, meshwants.NodeEntryRef{Node: "ace", Module: "x", Digest: "d4"}, - meshwants.NodeEntryRef{Node: "ace", Module: "y", Digest: "d5"}) - p.actingWaits(context.Background(), n, wanted) - f, ok := findings["module.ace.x.acting-cycle"] - if len(findings) != 2 || !ok || !strings.Contains(f.Summary, "ace/x -[place]-> ace/y -[compose]-> ace/x") { - t.Fatalf("the findings of a cycle no rule breaks: %v", findings) - } - // x's new version runs: y no longer waits, so neither is held on a cycle, and both findings clear. - n.Running["ace"]["x"] = nodeengine.Running{Fingerprint: "src2:x"} - p.actingWaits(context.Background(), n, wanted) - if len(findings) != 0 { - t.Errorf("the findings after the cycle broke: %v", findings) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended.go index 43d2af42..6b02c229 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended.go @@ -193,20 +193,6 @@ func hearBuilds(ctx context.Context, js jetstream.JetStream, ends *builtEnds, wa }() } -// hearTaken registers the hub's taker of the indexer's take-in.taken, each waking the pass (orderwaits.go, §298, A-260); before -// the hub starts. -func hearTaken(hub *process.EventHub, taken *takenSources, wake func(), logf func(string, ...any)) error { - return hub.Handle(takenSubject, func(m jetstream.Msg) { - if err := taken.put(m.Data()); err != nil { - logf("%s is not kept: %v", m.Subject(), err) - } - if err := m.Ack(); err != nil { - logf("acknowledging %s: %v", m.Subject(), err) - } - wake() - }) -} - // compareVersion orders two MAJOR.MINOR.PATCH versions by their numbers; a part that is no number counts as 0. func compareVersion(a, b string) int { pa, pb := strings.Split(a, "."), strings.Split(b, ".") diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended_test.go index 8d50c068..efc70ae9 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/buildended_test.go @@ -56,10 +56,10 @@ func TestAVersionReleasedHeardNamesItsVersionAndPartsBySource(t *testing.T) { } } -// Design 47 G §379, A-96, A-419: the trunk's entry takes its version and parts from the version-released of the source its -// fingerprint was built at (its fp-src), whatever source the kept assignment graph revision is at; a kept build from its end; no -// revision kept, another fingerprint, an fp-src with no release or none names none, and an unheard release is said once. -func TestATrunkEntryTakesItsVersionFromTheReleaseOfItsFingerprintsSource(t *testing.T) { +// Design 47 G §379, A-96, A-421; H §764, H-A-48: the trunk's entry, which the catalog stamps with no source, takes its version and +// parts from the version-released whose parts are what its build.ended made; a kept build from its end; no revision kept, another +// fingerprint and a build whose parts no release names name none, and an unheard release is said once. +func TestATrunkEntryTakesItsVersionFromTheReleaseOfItsBuildsParts(t *testing.T) { w := newWorld() raw, _ := json.Marshal(meshcatalog.VersionReleasedEvent{Module: "mail", Source: 7, Version: "1.2.3", Artifacts: []meshcatalog.VersionArtifact{{Artifact: "mail:image", Digest: "sha256:9"}}}) if err := w.released.put(raw); err != nil { @@ -71,10 +71,11 @@ func TestATrunkEntryTakesItsVersionFromTheReleaseOfItsFingerprintsSource(t *test } var logs []string p := &passer{w: w, graph: &graphKeeper{ok: true, source: 7}, logf: func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }} - named := p.namedBuild("mail", "t", 7) - if k, ok := named("t"); !ok || k.Version != "1.2.3" || k.Parts["mail:image"] != "sha256:9" || k.KeptOn != "" { - t.Errorf("trunk entry = %+v, %v", k, ok) + named := p.namedBuild("mail", "t") + if _, ok := named("t"); ok { + t.Error("a trunk entry whose build.ended is not heard names a version") } + _, _ = named("t") if k, ok := named("k"); !ok || k.KeptOn != "g14" || k.Version != "" { t.Errorf("kept build = %+v, %v", k, ok) } @@ -89,66 +90,23 @@ func TestATrunkEntryTakesItsVersionFromTheReleaseOfItsFingerprintsSource(t *test if k, ok := named("old"); !ok || k.Version != "1.2.3" { t.Errorf("a held build whose parts were released = %+v, %v; want 1.2.3", k, ok) } - p.graph.source = 8 // a later merge that did not rebuild mail - if k, ok := named("t"); !ok || k.Version != "1.2.3" { - t.Errorf("after a later merge the trunk entry = %+v, %v; want the release of its fingerprint's source", k, ok) + // The entry's module value carries no source stamp: the catalog writes none (H-A-48). + if v, _, _ := fromEntry(meshcatalog.CatalogEntry{Module: "mail", Fp: "t", Inputs: &meshcatalog.EntryInputs{}}); v.Fp2 != "t" { + t.Errorf("the entry's module value is %+v", v) } - if _, ok := p.namedBuild("mail", "t", 8)("t"); ok { - t.Error("an fp-src with no release names a version") - } - _, _ = p.namedBuild("mail", "t", 8)("t") - if _, ok := p.namedBuild("mail", "t", 0)("t"); ok { - t.Error("an entry naming no fp-src, whose build.ended is not heard, names a version") - } - // The release names each artifact by its whole reference, as the catalog says it, and the parts are keyed so (A-96); the - // assigner reads the entry's fp-src from its inputs (A-419). - // A-429: a released version's build counts as done for rule 1; a kept build (no version) and an unknown one do not. - if !releasedBuild(p.namedBuild("mail", "t", 7), "t") || releasedBuild(p.namedBuild("mail", "t", 7), "k") || releasedBuild(p.namedBuild("mail", "t", 8), "t") || releasedBuild(nil, "t") { - t.Error("rule 1's released build: the release of the entry's fingerprint counts, a kept build, another source or none does not") - } - if v, _, _ := fromEntry(meshcatalog.CatalogEntry{Module: "mail", Fp: "t", Inputs: &meshcatalog.EntryInputs{Src: 9, FpSrc: 7}}); v.FpSrc != 7 || v.Src != 9 { - t.Errorf("the entry's module value carries fp-src %d and src %d, want 7 and 9", v.FpSrc, v.Src) - } - // A-421: an entry naming no fp-src takes the release whose parts are what its build.ended made. made, _ := json.Marshal(builds.BuildEnded{Module: "mail", Fingerprint: "t", Made: map[string]string{"image": "sha256:9"}}) if err := w.ends.put(made); err != nil { t.Fatal(err) } - if k, ok := p.namedBuild("mail", "t", 0)("t"); !ok || k.Version != "1.2.3" { - t.Errorf("an entry naming no fp-src, its build made the released digests: %+v, %v", k, ok) + p.graph.source = 8 // a later merge that did not rebuild mail + if k, ok := named("t"); !ok || k.Version != "1.2.3" || k.Parts["mail:image"] != "sha256:9" { + t.Errorf("the trunk entry whose build made the released digests: %+v, %v", k, ok) } - // A release from an earlier source than the entry's fp-src (novox 2026-11-16, 962 against 963) is still this build's when - // its parts are exactly what the build made. - if k, ok := p.namedBuild("mail", "t", 8)("t"); !ok || k.Version != "1.2.3" || k.Parts["mail:image"] != "sha256:9" { - t.Errorf("an fp-src with no release, its build made the released digests: %+v, %v", k, ok) - } - if len(logs) != 2 || !strings.Contains(logs[0], "the release of mail at fingerprint t is not heard: no version-released of it from source 8") || !strings.Contains(logs[1], "names no fp-src, and no version-released of it names the parts its build.ended made") { - t.Errorf("logs = %q, want each unheard release said once, naming the module, the fingerprint and the source", logs) + if len(logs) != 1 || !strings.Contains(logs[0], "the release of mail at fingerprint t is not heard: no version-released of it names the parts its build.ended made") { + t.Errorf("logs = %q, want the unheard release said once, naming the module and the fingerprint", logs) } p.graph.ok = false - if _, ok := p.namedBuild("mail", "t", 7)("t"); ok { + if _, ok := p.namedBuild("mail", "t")("t"); ok { t.Error("no revision kept names a release") } } - -// A-162: the assigner knows a failed build from its `build.ended`: the error whole; a done end or none heard is not failed. -func TestTheStandingOfABuildIsHeardFromItsEnd(t *testing.T) { - w := newWorld() - say := func(e builds.BuildEnded) { - raw, _ := json.Marshal(e) - if err := w.ends.put(raw); err != nil { - t.Fatal(err) - } - } - if failed, _ := w.buildStanding("web", "fp1"); failed { - t.Error("a build nothing was heard of failed") - } - say(builds.BuildEnded{Module: "web", Fingerprint: "fp1", Outcome: builds.BuildFailed, Error: "transport fault: connection"}) - if failed, why := w.buildStanding("web", "fp1"); !failed || why != "transport fault: connection" { - t.Errorf("failed end: failed %v why %q", failed, why) - } - say(builds.BuildEnded{Module: "web", Fingerprint: "fp1", Outcome: builds.BuildDone}) - if failed, _ := w.buildStanding("web", "fp1"); failed { - t.Error("a later done end leaves the build failed") - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded.go index 61748b49..af775a3f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded.go @@ -34,7 +34,7 @@ func (p *passer) addedOf(node string, laid *mma.Assignments, current map[string] p.skippedAdd(node, module, "", "the branch "+branch+" names no fingerprint of it") continue } - k, ok := p.namedBuild(module, fp, entry.FpSrc)(fp) + k, ok := p.namedBuild(module, fp)(fp) if !ok { p.skippedAdd(node, module, fp, "no version-released of it is heard for that fingerprint, so the candidate carries no version and no parts") continue diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded_test.go index e2f49be4..240488a5 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/candidateadded_test.go @@ -17,19 +17,23 @@ import ( func TestACandidateThatAddsAModuleCarriesItsParts(t *testing.T) { w := newWorld() for _, m := range []string{"mail", "web", "db"} { - w.putModule("main", m, meshcatalog.ModulesByModuleValue{Fp2: "fp-" + m, FpSrc: 7}, graph.Node{}, nil) + w.putModule("main", m, meshcatalog.ModulesByModuleValue{Fp2: "fp-" + m}, graph.Node{}, nil) } w.first = "main" rel, _ := json.Marshal(meshcatalog.VersionReleasedEvent{Module: "mail", Source: 7, Version: "1.2.3", Artifacts: []meshcatalog.VersionArtifact{{Artifact: "mail:image", Digest: "sha256:9"}}}) if err := w.released.put(rel); err != nil { t.Fatal(err) } + built, _ := json.Marshal(builds.BuildEnded{Module: "mail", Fingerprint: "fp-mail", Made: map[string]string{"image": "sha256:9"}}) + if err := w.ends.put(built); err != nil { + t.Fatal(err) + } end, _ := json.Marshal(builds.BuildEnded{Module: "db", Fingerprint: "fp-db", Made: map[string]string{"program": "sha256:3"}, KeptOn: "g14"}) if err := w.ends.put(end); err != nil { t.Fatal(err) } w.put(buckets.Wants(), "want.ace.web", []byte(`{}`)) - p := &passer{w: w, graph: &graphKeeper{ok: true, source: 9}} // a later merge: the release is matched by fp-src (A-419) + p := &passer{w: w, graph: &graphKeeper{ok: true, source: 9}} // a later merge: the release is matched by the parts its build made (A-421, H-A-48) laid := &mma.Assignments{Node: "ace", Modules: []mma.PlacedModule{{Module: "mail"}, {Module: "web"}, {Module: "db"}, {Module: "old"}}} current := map[string]mma.Assignments{"ace": {Node: "ace", Modules: []mma.PlacedModule{{Module: "old"}}}} c := p.candidateOf("p1", "ace", laid, current) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionbounds.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionbounds.go index 5c1b5902..1fb929de 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionbounds.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionbounds.go @@ -370,6 +370,11 @@ func (c *compositionSteps) takeUp(ctx context.Context, r *compositionRuns) { if reader.Ended != nil { again = reader.ask } - go c.say1(ctx, reader.ID, stepRunOf(reader.CompositionReader), c.resume, again) + how := c.howOf(reader.Step, true) + if how == nil { + c.logf("the step %s of %s is not taken up again: it is no composition or validate step (%q)", reader.ID, reader.Module, reader.Step) + continue + } + go c.say1(ctx, reader.ID, stepRunOf(reader.CompositionReader), how, again) } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go index c2aad0c6..2482ebc0 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go @@ -23,8 +23,8 @@ import ( // The run of a composition step (design 47 G §354, §63, §64, §718; A-226, A-238, A-240, A-252, A-254, A-261): the step reads one // composition run per machine of its candidate (compositionruns.go), and the step that starts a machine's run composes it: one -// compose-candidate ask of the node, one composer.Request, depth compose, the one machine and one case `change`, its manifests, -// verdicts and seats from the step's catalog revision read through mesh-catalog's `revision`, kept once in `inputs`; one run ask +// compose-candidate ask of the node, one composer.Request, depth compose, the one machine and one case `change`, its manifests +// and seats from the step's catalog revision read through mesh-catalog's `revision`, kept once in `inputs`; one run ask // of the seat node-build-go, naming no head, its program the node-engine build's catalog entry with the composer's package, // chosen once and recorded in the run. The run's end is heard on the build seat's step-ended of that ask, its answer (a // composer.Verdict) read from `output`, and written in the run's key, whose change every reading step hears. A machine the @@ -188,7 +188,8 @@ func couldNotRun(said string) stepOutcome { } // runPrep is what the step that starts a run reads of its catalog revision, once for every machine it starts: the seats, the -// case's manifests and verdicts, and the composer program per machine. +// case's manifests, and the composer program per machine. The case carries no verdicts: the step validate judged the +// manifests before the composition step (design 47 H-A-112, H-A-108). type runPrep struct { r *compositionRun step meshwants.StepRun @@ -196,7 +197,6 @@ type runPrep struct { // read once seats []json.RawMessage manifests map[string]composer.Manifest - verdicts map[string]json.RawMessage none map[string]string programOf func(node string) (buildstep.RunProgram, string, error) err error @@ -212,7 +212,7 @@ func (p *runPrep) get(ctx context.Context) error { if p.seats, p.err = p.r.seats(ctx, half.Revision.Definitions); p.err != nil { return } - if p.manifests, p.verdicts, p.none, p.err = caseOf(half); p.err != nil { + if p.manifests, p.none, p.err = caseOf(half); p.err != nil { return } p.programOf, p.err = p.r.programs(ctx, half) @@ -244,7 +244,7 @@ func (r *compositionRun) start(ctx context.Context, key string, run meshwants.Co return } req := composer.Request{V: composer.Version, Input: in.Body, Depth: composer.DepthCompose, Machines: []string{node}, - Cases: []composer.Case{{Name: changeCase, Manifests: prep.manifests, Verdicts: prep.verdicts, None: prep.none}}, Seats: prep.seats, + Cases: []composer.Case{{Name: changeCase, Manifests: prep.manifests, None: prep.none}}, Seats: prep.seats, BlameTries: r.runs.set.blameTries} // the composer's bound of its search for culprits (§721, A-466r) raw, err := json.Marshal(req) if err != nil { @@ -479,12 +479,10 @@ func (r *compositionRun) seatAt(ctx context.Context, seat, digest string) (json. return def, nil } -// caseOf is the case's manifests, verdicts and none from the revision's entries: each manifest whole, the verdict of each one not -// accepted (a module without one is accepted), and the revision's none words of each module it gives no fingerprint, whose -// build can never be made (design 47 G A-470j, A-470k). -func caseOf(half mcc.Half) (map[string]composer.Manifest, map[string]json.RawMessage, map[string]string, error) { +// caseOf is the case's manifests and none from the revision's entries: each manifest whole, and the revision's none words of each +// module it gives no fingerprint, whose build can never be made (design 47 G A-470j, A-470k). It sets no verdicts (H-A-112). +func caseOf(half mcc.Half) (map[string]composer.Manifest, map[string]string, error) { manifests := map[string]composer.Manifest{} - verdicts := map[string]json.RawMessage{} var none map[string]string for name, e := range half.Entries { if len(e.Manifest) == 0 { @@ -492,7 +490,7 @@ func caseOf(half mcc.Half) (map[string]composer.Manifest, map[string]json.RawMes } raw, err := json.Marshal(e.Manifest) if err != nil { - return nil, nil, nil, fmt.Errorf("the manifest of %s cannot be encoded: %w", name, err) + return nil, nil, fmt.Errorf("the manifest of %s cannot be encoded: %w", name, err) } manifests[name] = composer.Manifest{Raw: raw} if e.Fp == "" && e.None != "" { @@ -501,23 +499,8 @@ func caseOf(half mcc.Half) (map[string]composer.Manifest, map[string]json.RawMes } none[name] = e.None } - if e.Verdict == "" || e.Verdict == mcc.VerdictAccepted { - continue - } - v := meshcatalog.VerdictsByDigestValue{V: 1, Module: name, State: e.Verdict} - if e.Verdict != mcc.VerdictRefused { - v.State = mcc.VerdictManifestWaits - } - if e.Inputs != nil { - v.Problems, v.Unknown, v.Warnings = e.Inputs.Problems, e.Inputs.Unknown, e.Inputs.Warnings - } - raw, err = json.Marshal(v) - if err != nil { - return nil, nil, nil, err - } - verdicts[name] = raw } - return manifests, verdicts, none, nil + return manifests, none, nil } // nodeEngineEntry is the entry of the module that claims the seat node-engine, or false. diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go index 30dec563..ed48790f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go @@ -157,7 +157,7 @@ func newRunStand(t *testing.T, revisionEngine, trunkEngine, seatDigest string) * } // Design 47 G §354, §63, §64, A-226, A-238, A-240, A-243, A-252, A-254, A-261: one request and one run ask per machine, the request -// depth compose with the one machine and one case `change`, its manifests, verdicts and seats from the step's revision; the +// depth compose with the one machine and one case `change`, its manifests and seats from the step's revision, no verdicts (H-A-112); the // composer is the node-engine build each machine runs, its entry read through `entry`, or the revision's where it differs from the // trunk's, or the revision's for a machine whose node-engine is not heard running (A-451); the run's verdict is read from its step-ended; a machine away passes with a warning, one not composing fails the step. func TestTheCompositionStepRunsOneAskPerMachine(t *testing.T) { @@ -185,9 +185,8 @@ func TestTheCompositionStepRunsOneAskPerMachine(t *testing.T) { t.Errorf("the request of ace %+v", req) } c := req.Cases[0] - if string(c.Manifests["m"].Raw) != `{"module":"m"}` || len(c.Manifests) != 4 || len(c.None) != 1 || c.None["lidarr"] != "builders disagree: node" || len(c.Verdicts) != 1 || !strings.Contains(string(c.Verdicts["bad"]), `"refused"`) || - !strings.Contains(string(c.Verdicts["bad"]), "no seat x") { - t.Errorf("the case %+v %s", c.Manifests, c.Verdicts) + if string(c.Manifests["m"].Raw) != `{"module":"m"}` || len(c.Manifests) != 4 || len(c.None) != 1 || c.None["lidarr"] != "builders disagree: node" { + t.Errorf("the case %+v", c.Manifests) } if strings.Join(s.entries, " ") == "" { t.Error("no node-engine entry is read through entry") diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go index 62e59090..37f2a774 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go @@ -374,7 +374,11 @@ type joined struct { // written create-only (or replacing the ended key by compare-and-set), the reader its first, ask its run ask's id. At the // cap no run is started, an error naming the fill. func (r *compositionRuns) join(reader meshwants.CompositionReader, candidate, machine, ask string) (joined, error) { - key := runKeyOf(candidate, machine) + return r.joinAt(runKeyOf(candidate, machine), reader, candidate, machine, ask) +} + +// joinAt is join of the run under key: a machine's composition run, or a revision's validate run with no machine (H-A-108). +func (r *compositionRuns) joinAt(key string, reader meshwants.CompositionReader, candidate, machine, ask string) (joined, error) { for { run, rev, ok, err := r.read(key) if err != nil { @@ -539,12 +543,7 @@ func (r *compositionRuns) clearBounds(key, ask string, prefixes ...string) error // stepEnded writes at as the `ended` of the reader asked under id in each run of its step's candidate naming it, by // compare-and-set (A-470m): the time its step said its end, carried with it into a rerun's run. func (r *compositionRuns) stepEnded(id string, step meshwants.StepRun, at time.Time) { - candidate, err := candidateKeyOf(step) - if err != nil { - r.logf("the end of the composition step %s is not written on its runs: %v", id, err) - return - } - keys, err := r.keysOf(candidate, id) + keys, err := r.keysOfStep(step, id) if err != nil { r.logf("the end of the composition step %s is not written on its runs: %v", id, err) return @@ -564,6 +563,23 @@ func (r *compositionRuns) stepEnded(id string, step meshwants.StepRun, at time.T } } +// keysOfStep are the keys of the runs the step asked under id reads: its revision's validate run for a validate step, its +// candidate's runs naming it for a composition step. +func (r *compositionRuns) keysOfStep(step meshwants.StepRun, id string) ([]string, error) { + if step.Step == meshwants.StepRunStepValidate { + key, err := validateKeyOf(step) + if err != nil { + return nil, err + } + return []string{key}, nil + } + candidate, err := candidateKeyOf(step) + if err != nil { + return nil, err + } + return r.keysOf(candidate, id) +} + // hasReader is whether readers holds the step asked under id. func hasReader(readers []meshwants.CompositionReader, id string) bool { return slices.ContainsFunc(readers, func(c meshwants.CompositionReader) bool { return c.ID == id }) @@ -672,6 +688,10 @@ func (r *compositionRuns) heardEnd(e buildstep.StepEnded, read func(buildstep.St said = fmt.Sprintf("%s: its run ask %s ended %s on %s: %s", node, e.Ask, e.Outcome, e.Node, e.Said) } else if answer, err := read(e); err != nil { // the program's last line of standard output (A-254) said = node + ": " + err.Error() + } else if isValidateRun(run) { + if verdict, said = validateRunVerdict(answer); said == "" { + outcome = meshwants.CompositionRunOutcomePassed + } } else if verdict, said = runVerdict(node, answer); said == "" { outcome = meshwants.CompositionRunOutcomePassed } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns_integration_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns_integration_test.go index 7d32c033..7f6e6ace 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns_integration_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns_integration_test.go @@ -346,7 +346,7 @@ func TestOneCompositionRunPerCandidateAndMachine(t *testing.T) { sup := asks(2) readers(runKeyOf("run-r3", "n1"), 2) readers(runKeyOf("run-r3", "n2"), 2) - pe, _ := json.Marshal(mpr.PipelineEnded{V: 1, Pipeline: "run-r3.a", Module: "a", Branch: trunkBranch, Commit: commit, Outcome: mpr.PipelineEndedOutcomeSuperseded, Run: "r3", Steps: []mpr.StepResult{}}) + pe, _ := json.Marshal(mpr.PipelineEnded{V: 1, Pipeline: "run-r3.a", Branch: trunkBranch, Source: 1, Outcome: mpr.PipelineEndedOutcomeSuperseded, Run: "r3", Movers: []mpr.MoverEnded{{Module: "a", Steps: []mpr.StepResult{}}}}) if _, err := b.js.Publish(b.ctx, "mesh.seat."+mpr.Seat+".event."+mpr.EventPipelineEnded+".n1", pe); err != nil { t.Fatal(err) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep.go index 3f91b28d..58786168 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "strings" "sync" "time" @@ -11,7 +10,6 @@ import ( meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-sdk/go/process" - "git.novox.be/novox/mesh-sdk/go/seatdef" ) // The composition step (design 47 G §22, §354, §63): the assigner takes the seat mesh-wants' accepted kind `step`, the @@ -35,13 +33,15 @@ type compositionSteps struct { // run runs one step and answers what it came to; resume judges a step taken up at the start from its runs (takeUp). run func(ctx context.Context, id string, run meshwants.StepRun) stepOutcome resume func(ctx context.Context, id string, run meshwants.StepRun) stepOutcome + // validate runs one validate step and resumeValidate judges one taken up at the start (validatestep.go, design 47 H §745); + // nil takes no validate step. + validate func(ctx context.Context, id string, run meshwants.StepRun) stepOutcome + resumeValidate func(ctx context.Context, id string, run meshwants.StepRun) stepOutcome // ended writes, once the step asked under id said its end, that time on its reader in each run naming it (A-470m); nil // writes none. ended func(id string, run meshwants.StepRun, at time.Time) now func() time.Time logf func(string, ...any) - // asker is the name the step's progress names as its asker, read when it is said (stepAskerOf). - asker func() string // ticks is a ticker of every d, its channel and its stop (time.NewTicker); nil is the clock's. A test hands its own. ticks func(d time.Duration) (<-chan time.Time, func()) mu sync.Mutex @@ -49,32 +49,6 @@ type compositionSteps struct { stops map[string]context.CancelFunc } -// stepAskerOf is the asker a composition step's progress names: the one caller the seat mesh-wants' definition lists for its -// accepted kind step, read from the seat list when it is said, since that subject names no asker (design 47 G A-294, which -// corrects A-267). Empty while the seat list holds no definition naming exactly one. -func stepAskerOf(definition func() (seatdef.Definition, bool)) func() string { - return func() string { - d, ok := definition() - if !ok { - return "" - } - var one string - for _, m := range d.Accepts { - if m.Name != meshwants.AcceptStep || m.Callers == nil { - continue - } - for _, e := range m.Callers.List { - name, isSeat := strings.CutPrefix(e, seatdef.CallerSeatPrefix) - if !isSeat || (one != "" && one != name) { - return "" - } - one = name - } - } - return one - } -} - // progressEvery is how often a running composition step says its progress (design 47 G §496, A-262). const progressEvery = time.Minute @@ -118,18 +92,35 @@ func (p *stepPhase) get() string { return p.phase } -// take takes one ask of the accepted kind `step` by its id and body: a step that is no composition step, or an ask with no -// id, is left with a log line; one taken is run in its own goroutine and its end said. +// take takes one ask of the accepted kind `step` by its id and body: a step that is no composition or validate step, or an +// ask with no id, is left with a log line; one taken is run in its own goroutine and its end said. func (c *compositionSteps) take(ctx context.Context, id string, data []byte) bool { var run meshwants.StepRun - if err := json.Unmarshal(data, &run); err != nil || id == "" || run.Step != meshwants.StepRunStepComposition { - c.logf("a step ask that is no composition step with an id is left: %v", err) + err := json.Unmarshal(data, &run) + how := c.howOf(run.Step, false) + if err != nil || id == "" || how == nil { + c.logf("a step ask that is no composition or validate step with an id is left: %v", err) return false } - go c.say1(ctx, id, run, c.run, "") + go c.say1(ctx, id, run, how, "") return true } +// howOf is what runs a step, or judges it once taken up at the start: by its step, composition or validate; nil for another. +func (c *compositionSteps) howOf(step string, resumed bool) func(context.Context, string, meshwants.StepRun) stepOutcome { + switch { + case step == meshwants.StepRunStepComposition && resumed: + return c.resume + case step == meshwants.StepRunStepComposition: + return c.run + case step == meshwants.StepRunStepValidate && resumed: + return c.resumeValidate + case step == meshwants.StepRunStepValidate: + return c.validate + } + return nil +} + // say1 runs one step with how and says its end, naming in `shared` the runs it read (§723); again is, for an end said again, the // ask of the run whose end makes it say again (sayEnd). A step whose wait was ended by its pipeline's supersede or cancel (leave) // says nothing: the runner ended it already. @@ -154,7 +145,7 @@ func (c *compositionSteps) say1(ctx context.Context, id string, run meshwants.St stopWait() }() phase := &stepPhase{phase: meshwants.StepProgressEventPhaseFetch} - stop := c.sayProgress(id, phase) + stop := c.sayProgress(id, run.Asker, phase) o := how(context.WithValue(ctx, phaseKey{}, phase), id, run) stop() if ctx.Err() != nil { @@ -172,7 +163,11 @@ func (c *compositionSteps) sayEnd(id string, run meshwants.StepRun, started *tim if started == nil { started = &ended } - end := meshwants.StepEnded{V: 1, ID: id, Pipeline: run.Pipeline, Step: meshwants.StepEndedStepComposition, Module: run.Module, + step := meshwants.StepEndedStepComposition + if run.Step == meshwants.StepRunStepValidate { + step = meshwants.StepEndedStepValidate + } + end := meshwants.StepEnded{V: 1, ID: id, Pipeline: run.Pipeline, Step: step, Module: run.Module, Commit: run.Commit, Fingerprint: run.Fingerprint, Node: c.node, Outcome: o.Outcome, Said: o.Said, Warnings: o.Warnings, Rerun: run.Rerun, By: run.By, Started: started, Ended: &ended, Shared: o.Shared} msgID := id @@ -222,7 +217,7 @@ func (c *compositionSteps) leave(ids []string) { // sayProgress says the step's progress on the seat's step-progress. once a minute until the returned stop is called (design // 47 G §496, A-262): a ticker inside the running step, as the build seats' step runner says its own beat. -func (c *compositionSteps) sayProgress(id string, phase *stepPhase) (stop func()) { +func (c *compositionSteps) sayProgress(id, asker string, phase *stepPhase) (stop func()) { ticks := c.ticks if ticks == nil { ticks = func(d time.Duration) (<-chan time.Time, func()) { t := time.NewTicker(d); return t.C, t.Stop } @@ -239,7 +234,7 @@ func (c *compositionSteps) sayProgress(id string, phase *stepPhase) (stop func() return case <-tick: at := c.now().UTC() - p := meshwants.StepProgressEvent{Ask: id, Asker: c.asker(), Phase: phase.get(), At: at, Shared: phase.waitsOn()} + p := meshwants.StepProgressEvent{Ask: id, Asker: asker, Phase: phase.get(), At: at, Shared: phase.waitsOn()} if err := c.say(meshwants.EventStepProgress+"."+c.node, p, id+".progress."+at.Format(time.RFC3339Nano)); err != nil { c.logf("saying the progress of the composition step %s: %v", id, err) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_integration_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_integration_test.go index 230d16f7..42615f58 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_integration_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_integration_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" "encoding/json" + "fmt" "strings" "testing" "time" @@ -241,3 +242,133 @@ func readInput(t *testing.T, nc *nats.Conn, in buildstep.RunInput) []byte { } return out.Bytes() } + +// Design 47 H §745 (acceptance test 117, the assigner's half; H-A-108), started whole on the bus: the assigner serves the step +// validate on the seat mesh-wants. A take-in whose manifest names an unknown field fails it `manifest-refused` naming it, the +// validator being the revision's own entry of the module holding the seat mesh-catalog, run on the Go build seat over the +// revision's manifests and the previous revision's; a take-in that changes a manifest with no problem passes it; a take-in whose +// entries make a cycle fails it `cycle` naming the cycle, with no validator asked. +func TestTheValidateStepServedWholeRefusesAManifestProblemAndACycle(t *testing.T) { + b := startBed(t, "") + nc, err := nats.Connect(b.srv.ClientURL()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(nc.Close) + const commit = "2222222222222222222222222222222222222222" + if _, err := b.js.CreateOrUpdateConsumer(b.ctx, "EVENTS", jetstream.ConsumerConfig{Durable: "n1_mesh-delivery-assigner", + FilterSubjects: []string{enginereads.SubjectPrefix + "*", "mesh.mod." + buckets.IndexerModule + ".event.views.>", meshcatalog.SubjectCatalogRevisionAll, + nodebuildgo.SubjectStepEndedAll}, AckPolicy: jetstream.AckExplicitPolicy}); err != nil { + t.Fatal(err) + } + indexer := meshcatalog.CatalogEntry{Module: "indexer", Repository: "novox/mesh-delivery", Commit: commit, Path: "modules/mesh-delivery-indexer", + Fp: "src2:ix", Serves: []string{"seat:" + meshcatalog.Seat + ".revision"}, Manifest: map[string]any{"module": "indexer"}, ManifestDigest: "d-ix"} + entry := func(module, digest string, manifest map[string]any, edges ...meshcatalog.Edge) meshcatalog.CatalogEntry { + manifest["module"] = module + return meshcatalog.CatalogEntry{Module: module, Manifest: manifest, ManifestDigest: digest, Edges: edges} + } + b.cat.mu.Lock() + b.cat.rev[trunkBranch] = 9 + b.cat.at = map[string]map[int64][]meshcatalog.CatalogEntry{trunkBranch: { + 6: {indexer, entry("web", "d-web-1", map[string]any{"version": "1"})}, + 7: {indexer, entry("web", "d-web-2", map[string]any{"version": "2", "colour": "red"})}, + 8: {indexer, entry("web", "d-web-3", map[string]any{"version": "3"})}, + 9: {indexer, entry("a", "d-a", map[string]any{}, meshcatalog.Edge{From: "a:x", To: "b:y", Kind: 3}, meshcatalog.Edge{From: "b:y", To: "a:x", Kind: 3}), + entry("b", "d-b", map[string]any{})}, + }} + b.cat.mu.Unlock() + ended := make(chan meshwants.StepEnded, 4) + ends, err := nc.Subscribe("mesh.seat.mesh-wants.event.step-ended.n1", func(m *nats.Msg) { + var e meshwants.StepEnded + _ = json.Unmarshal(m.Data, &e) + ended <- e + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = ends.Unsubscribe() }) + subscribed(t, nc) + from := b.journal.mark() + b.start(t) + b.journal.await(t, from, "the composition step is heard on", "the steps served", 30*time.Second) + select { + case <-b.seatListRead: + case <-time.After(30 * time.Second): + t.Fatal("the assigner did not read the seat list within 30 s") + } + queue, _ := buildstep.AskStream(buildstep.AskGo) + worker, err := b.js.CreateOrUpdateConsumer(b.ctx, queue, jetstream.ConsumerConfig{Durable: "stand-in", FilterSubject: "mesh.seat.node-build-go.accept.check.mesh-wants", AckPolicy: jetstream.AckExplicitPolicy}) + if err != nil { + t.Fatal(err) + } + ask := func(revision int64, module string, run func()) meshwants.StepEnded { + t.Helper() + id := fmt.Sprintf("validate-%d", revision) + step, _ := json.Marshal(meshwants.StepRun{Pipeline: fmt.Sprintf("%s.%d", trunkBranch, revision), Step: meshwants.StepRunStepValidate, Module: module, + Branch: trunkBranch, Revision: revision, Commit: commit}) + msg := nats.NewMsg(meshwants.AcceptSubjectStep) + msg.Header.Set("x-event-id", id) + msg.Data = step + if err := nc.PublishMsg(msg); err != nil { + t.Fatal(err) + } + if run != nil { + run() + } + for { + select { + case e := <-ended: + if e.ID == id { + return e + } + case <-time.After(30 * time.Second): + t.Fatalf("the validate step of revision %d said no end", revision) + } + } + } + // The stand-in Go build seat runs the validator: it refuses a manifest naming the field colour, and accepts every other. + validator := func() { + t.Helper() + got, err := worker.Next(jetstream.FetchMaxWait(30 * time.Second)) + if err != nil { + t.Fatalf("no validator run is asked: %v", err) + } + _ = got.Ack() + a, err := buildstep.DecodeStepAsk(got.Data()) + if err != nil || a.Run == nil || a.Run.Program.Repository != "novox/mesh-delivery" || a.Run.Program.Commit != commit { + t.Fatalf("the validator's run ask %s: %v", got.Data(), err) + } + req, err := composer.Decode(readInput(t, nc, a.Run.Input)) + if err != nil || len(req.Cases) != 2 || req.Cases[0].Name != changeCase || req.Cases[1].Name != beforeCase { + t.Fatalf("the validator's request: %+v %v", req, err) + } + v := composer.Verdict{V: composer.Version} + for _, c := range req.Cases { + reads := map[string]composer.ManifestRead{} + for module, m := range c.Manifests { + reads[module] = composer.ManifestRead{Accepted: true} + if strings.Contains(string(m.Raw), "colour") { + reads[module] = composer.ManifestRead{Refused: `unknown field "colour"`} + } + } + v.Cases = append(v.Cases, composer.CaseVerdict{Name: c.Name, Manifests: reads}) + } + out, _ := json.Marshal(v) + end, _ := json.Marshal(buildstep.StepEnded{V: 1, ID: a.ID, Ask: a.ID, Asker: runAsker, Node: "b1", Outcome: buildstep.OutcomePassed, Output: string(out), At: time.Now().UTC()}) + if _, err := b.js.Publish(b.ctx, nodebuildgo.SubjectStepEnded("b1"), end); err != nil { + t.Fatal(err) + } + } + if e := ask(7, "web", validator); e.Outcome != meshwants.StepEndedOutcomeFailed || !strings.HasPrefix(e.Said, reasonManifestRefused+": ") || !strings.Contains(e.Said, `web: unknown field "colour"`) { + t.Errorf("the validate step of a manifest naming an unknown field ended %+v", e) + } + if e := ask(8, "web", validator); e.Outcome != meshwants.StepEndedOutcomePassed { + t.Errorf("the validate step of a manifest with no problem ended %+v", e) + } + if e := ask(9, "a", nil); e.Outcome != meshwants.StepEndedOutcomeFailed || e.Said != reasonCycle+": a:x → b:y → a:x" { + t.Errorf("the validate step of a take-in making a cycle ended %+v", e) + } + if info, err := worker.Info(b.ctx); err != nil || info.NumPending != 0 { + t.Errorf("a validator is asked for the cycle: %+v %v", info, err) + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_test.go index 07e25cb8..ee2541c1 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionstep_test.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "git.novox.be/novox/mesh-sdk/go/seatdef" "slices" "strings" "sync" @@ -84,23 +83,7 @@ func TestARunningCompositionStepSaysItsProgress(t *testing.T) { stopped := make(chan struct{}) at := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) release := make(chan struct{}) - // A-294: the asker is the one caller mesh-wants' definition lists for step, read from the seat list; none while unread - // or while it lists more than one. - runner := seatdef.Definition{Seat: meshwants.Seat, Accepts: []seatdef.Message{{Name: meshwants.AcceptStep, - Callers: &seatdef.Callers{List: []string{"seat:mesh-pipeline-runner"}}}}} - two := seatdef.Definition{Seat: meshwants.Seat, Accepts: []seatdef.Message{{Name: meshwants.AcceptStep, - Callers: &seatdef.Callers{List: []string{"seat:mesh-pipeline-runner", "seat:mesh-catalog"}}}}} - for _, d := range []struct { - def seatdef.Definition - read bool - want string - }{{runner, true, "mesh-pipeline-runner"}, {runner, false, ""}, {two, true, ""}} { - if got := stepAskerOf(func() (seatdef.Definition, bool) { return d.def, d.read })(); got != d.want { - t.Errorf("the asker of %+v (read %v) = %q, want %q", d.def.Accepts, d.read, got, d.want) - } - } c := &compositionSteps{node: "n1", say: said.say, now: func() time.Time { return at }, logf: t.Logf, - asker: stepAskerOf(func() (seatdef.Definition, bool) { return runner, true }), ticks: func(d time.Duration) (<-chan time.Time, func()) { if d != time.Minute { t.Errorf("the progress ticks every %s, want a minute", d) @@ -113,7 +96,8 @@ func TestARunningCompositionStepSaysItsProgress(t *testing.T) { <-release return stepOutcome{Outcome: meshwants.StepEndedOutcomePassed} }} - body, _ := json.Marshal(meshwants.StepRun{Pipeline: "main.4.m", Step: meshwants.StepRunStepComposition, Module: "m", Branch: "main"}) + // A11/AC1 (design 47 H §752, H-A-88): with no callers in mesh-wants, the progress names the asker the step run carries. + body, _ := json.Marshal(meshwants.StepRun{Pipeline: "main.4", Step: meshwants.StepRunStepComposition, Module: "m", Branch: "main", Asker: "mesh-pipeline-runner"}) if !c.take(context.Background(), "ask-1", body) { t.Fatal("the composition step is not taken") } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered.go deleted file mode 100644 index 8a2bf3ee..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered.go +++ /dev/null @@ -1,454 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "slices" - "sort" - "strings" - "sync" - "time" - - "github.com/nats-io/nats.go/jetstream" - - wd "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/process" - - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/assignment" - "mesh-delivery-assigner/internal/buckets" -) - -// A delivery is waited for mesh-wide (design 47 G §376, §389, A-189, A-246). At its start the assigner reads whole, from their first -// message, the streams of the catalog seat's version-released and of its own version-delivered (mesh-sdk process.FromFirst, A-233), -// and the watchman's machine-away and machine-back, never its state; then it hears them. A released version of a module is said -// version-delivered on the seat mesh-wants once, when every present machine placing it on the branch it is released from runs, healthy, -// the want the assigner wrote naming that version; a version no present machine places is not delivered. A dependent whose want -// changes fingerprint waits, by an edge of kind 2, 4 or 6, for its dependency's newest released version not yet said delivered, on -// the branch it is released from; on another branch no version is released and nothing waits. The wait is said wait-held, step -// delivery, bound 60 minutes from the version's release, kept in the state delivery-waits under the dependent's name, and said -// wait-ended when the version is delivered (ran) or released by hand (assigner_release, released). - -// deliveryWaitBound is a dependent's delivery wait's bound, from its dependency's version-released. -const deliveryWaitBound = 60 * time.Minute - -// Wait-ended's how of a delivery wait. -const ( - deliveryWaitRan = "ran" - deliveryWaitReleased = "released" -) - -// releaseHeard is one version-released heard: the version, each part's digest, the source revision and when it was said. -type releaseHeard struct { - Version string - Parts map[string]string - Source int64 - At time.Time -} - -// newestOf is the newest version of module released, the last heard of it in the stream's order. -func (r *releasedVersions) newestOf(module string) (releaseHeard, bool) { - r.mu.Lock() - defer r.mu.Unlock() - h, ok := r.newest[module] - return h, ok -} - -// deliveredVersions are the version-delivered said, the assigner's own, by module and version: each is said once. -type deliveredVersions struct { - mu sync.Mutex - said map[string]bool -} - -func (d *deliveredVersions) key(module, version string) string { return module + "@" + version } - -// put takes one version-delivered in. -func (d *deliveredVersions) put(data []byte) error { - var e meshwants.VersionDeliveredEvent - if err := json.Unmarshal(data, &e); err != nil { - return fmt.Errorf("a version-delivered that is none: %w", err) - } - if e.Module == "" || e.Version == "" { - return fmt.Errorf("a version-delivered naming no module or version: %s", data) - } - d.mark(e.Module, e.Version) - return nil -} - -func (d *deliveredVersions) mark(module, version string) { - d.mu.Lock() - defer d.mu.Unlock() - if d.said == nil { - d.said = map[string]bool{} - } - d.said[d.key(module, version)] = true -} - -// has says whether version-delivered of module's version was said. -func (d *deliveredVersions) has(module, version string) bool { - d.mu.Lock() - defer d.mu.Unlock() - return d.said[d.key(module, version)] -} - -// presenceOf reads one machine-away or machine-back: the machine and when it was said. -func presenceOf(data []byte) (string, time.Time, error) { - var e wd.MachineAwayEvent // machine-back has the same fields - if err := json.Unmarshal(data, &e); err != nil { - return "", time.Time{}, err - } - if e.Machine == "" { - return "", time.Time{}, fmt.Errorf("it names no machine: %s", data) - } - return e.Machine, e.At, nil -} - -// hearDelivery reads whole, then follows, the four streams a delivery is judged from: version-released, version-delivered, -// machine-away and machine-back. A stream not heard is said as the assigner's finding, and what it carries is not known until -// the assigner starts again. -func hearDelivery(ctx context.Context, js jetstream.JetStream, w *world, wake func(), findings assign.Findings, logf func(string, ...any)) { - acct := process.Account{Node: thisNode, Module: assignment.Module()} - nh := notHeardWriter(findings, logf) - for _, s := range []struct { - stream, words string - take func([]byte) error - }{ - {meshcatalog.StreamVersionReleased, "no want names a released version and no delivery is judged", w.released.put}, - {meshwants.StreamVersionDelivered, "a version said delivered before is said again once it is judged", w.delivered.put}, - {wd.StreamMachineAway, "no machine is known away, so a delivery waits for a machine that is away", func(data []byte) error { - m, at, err := presenceOf(data) - if err == nil { - w.heardPresence(m, true, at) - logf("%s is away (machine-away): a composition step does not compose it until its machine-back", m) - } - return err - }}, - {wd.StreamMachineBack, "a machine once away is not known back", func(data []byte) error { - m, at, err := presenceOf(data) - if err == nil { - w.heardPresence(m, false, at) - logf("%s is back (machine-back): a composition step composes it again", m) - } - return err - }}, - } { - take := s.take - cc, err := acct.HearSeatEvents(ctx, js, s.stream, func(m jetstream.Msg) { - if err := take(m.Data()); err != nil { - logf("%s is not kept: %v", m.Subject(), err) - } - if err := m.Ack(); err != nil { - logf("acknowledging %s: %v", m.Subject(), err) - } - wake() - }, logf, process.FromFirst()) - if err != nil { - nh.NotHeard(s.stream, s.words+" until the assigner starts again", err) - continue - } - nh.Heard(s.stream) - go func() { - <-ctx.Done() - cc.Stop() - }() - } -} - -// releaseBranch is the branch module's versions are released from: the first watched branch of its registration (§371). -func (p *passer) releaseBranch(module string) string { - if p.ends.following() { - return p.ends.branchOf(module, "") - } - return p.w.trunkName() -} - -// placing is every machine placing module on the branch its versions are released from, sorted. -func (p *passer) placing(module string) []string { - branch := p.releaseBranch(module) - var out []string - for node, placed := range p.w.placedBranches()[module] { - if p.ends.following() { - placed = p.ends.branchOf(module, placed) - } else { - placed = p.w.trunkName() // the trunk-only form: every placement runs the trunk's - } - if placed == branch { - out = append(out, node) - } - } - sort.Strings(out) - return out -} - -// deliveryStanding is where module's version stands: the present placing machines not yet running it healthy as its want names -// it, and the placing machines away. -func (p *passer) deliveryStanding(module, version string) (notRunning, away []string, placing int) { - gone := p.w.awayNow() - n := assign.Nodes{Running: p.w.runningNow()} - wanted := p.w.wantsOf()[module] - nodes := p.placing(module) - for _, node := range nodes { - if gone[node] { - away = append(away, node) - continue - } - want, ok := wanted[node] - if !ok || want.Removed || want.Version != version || !n.RunsHealthy(node, module, want.Fp) { - notRunning = append(notRunning, node) - } - } - return notRunning, away, len(nodes) -} - -// judgeDelivered says version-delivered of each module's newest released version not said yet, once every present machine placing -// it runs it healthy; judged again at each pass, which each running, machine-away, machine-back and placement change wakes. -func (p *passer) judgeDelivered() { - for _, module := range p.releasedModules() { - rel, _ := p.w.released.newestOf(module) - if p.w.delivered.has(module, rel.Version) { - continue - } - notRunning, away, placing := p.deliveryStanding(module, rel.Version) - if placing == 0 || len(away) == placing || len(notRunning) > 0 { - continue - } - if err := p.sayDelivered(module, rel, away); err != nil { - p.logf("version-delivered of %s %s is not said, and is judged again at the next pass: %v", module, rel.Version, err) - } - } -} - -// sayDelivered says version-delivered of module's release rel, with the machines away, and keeps it said. -func (p *passer) sayDelivered(module string, rel releaseHeard, away []string) error { - at := p.clock().UTC() - body := meshwants.VersionDeliveredEvent{V: keys.Version, Module: module, Version: rel.Version, Parts: rel.Parts, Away: away, At: &at} - if err := p.wantsSay(meshwants.EventVersionDelivered, body, fmt.Sprintf("%s.%s.%s.%s", meshwants.EventVersionDelivered, module, rel.Version, at.Format(time.RFC3339Nano))); err != nil { - return err - } - p.w.delivered.mark(module, rel.Version) - return nil -} - -// releasedModules is every module a version-released was heard of, sorted. -func (p *passer) releasedModules() []string { - r := &p.w.released - r.mu.Lock() - defer r.mu.Unlock() - out := make([]string, 0, len(r.newest)) - for m := range r.newest { - out = append(out, m) - } - sort.Strings(out) - return out -} - -// deliveryWait is what a dependent waits for: its dependency, the version and the machines not yet running it. -type deliveryWait struct { - dependency string - rel releaseHeard - notRunning []string - away []string -} - -// waitsForDelivery says whether module, whose want on its nodes changes to fp on branch, waits for a dependency's delivery: by -// each edge of kind 2, 4 or 6 into it in branch's catalog half, the dependency's newest released version on branch not yet said -// delivered. A want that changes no fingerprint waits for nothing; on a branch no version is released from, nothing waits. -func (p *passer) waitsForDelivery(module, fp, branch string, nodes []string) (deliveryWait, bool) { - wanted := p.w.wantsOf()[module] - changes := false - for _, node := range nodes { - if w, ok := wanted[node]; !ok || w.Removed || w.Fp != fp { - changes = true - } - } - if !changes || fp == "" { - return deliveryWait{}, false - } - var deps []string - for _, e := range p.w.edgesOf(branch, module) { - if e.Kind != 2 && e.Kind != 4 && e.Kind != 6 { - continue - } - dep, _, _ := strings.Cut(e.From, ":") - if dep != "" && dep != module && !slices.Contains(deps, dep) { - deps = append(deps, dep) - } - } - sort.Strings(deps) - for _, dep := range deps { - if p.releaseBranch(dep) != branch { - continue - } - rel, ok := p.w.released.newestOf(dep) - if !ok || p.w.delivered.has(dep, rel.Version) { - continue - } - notRunning, away, _ := p.deliveryStanding(dep, rel.Version) - return deliveryWait{dependency: dep, rel: rel, notRunning: notRunning, away: away}, true - } - return deliveryWait{}, false -} - -// words is a delivery wait in plain words, as assigner_why_not and the status view say it. -func (d deliveryWait) words() string { - if len(d.notRunning) == 0 && len(d.away) > 0 { - return fmt.Sprintf("waiting for %s to be back", strings.Join(d.away, ", ")) - } - return fmt.Sprintf("waiting for delivery of %s %s on %s", d.dependency, d.rel.Version, strings.Join(d.notRunning, ", ")) -} - -// deliveryWaitBody is the wait-held of module's delivery wait d. -func deliveryWaitBody(module, fp string, d deliveryWait, since time.Time) meshwants.Wait { - from := d.rel.At.UTC() - if from.IsZero() { - from = since.UTC() - } - return meshwants.Wait{Step: meshwants.WaitStepDelivery, Owner: meshwants.Seat, Module: module, Version: fp, Part: d.dependency, - On: d.dependency + "@" + d.rel.Version, Machines: d.notRunning, Since: since.UTC(), ClockFrom: &from, Bound: deliveryWaitBound.String(), - Words: d.words()} -} - -// deliveryWaiting is one delivery wait the state delivery-waits keeps, by its dependent's name: the wait-held said, and a release -// by hand written by assigner_release. -type deliveryWaiting struct { - meshwants.Wait - // Released is set by assigner_release: the wait ends released and the dependent's want is written. - Released bool `json:"released,omitempty"` -} - -// saidDeliveryWaits is every delivery wait the state delivery-waits holds, by module; a value that does not decode is said and left. -func (p *passer) saidDeliveryWaits() map[string]deliveryWaiting { - out := map[string]deliveryWaiting{} - for key, value := range p.w.read(buckets.DeliveryWaits()) { - var w deliveryWaiting - if err := json.Unmarshal(value, &w); err != nil || w.Module == "" { - p.logf("%s %s holds no delivery wait, left: %v", buckets.DeliveryWaits(), key, err) - continue - } - out[w.Module] = w - } - return out -} - -// holdForDelivery says whether module's want, changing to fp on branch, is held for a dependency's delivery this pass: it says -// wait-held once and keeps it; a wait released by hand holds nothing; a wait that no longer holds is ended ran. -func (p *passer) holdForDelivery(ctx context.Context, module, fp, branch string, nodes []string) bool { - if p.kvs[buckets.DeliveryWaits()] == nil { - return false - } - key := keys.EscapeName(module) - said, was := p.saidDeliveryWaits()[module] - d, waits := p.waitsForDelivery(module, fp, branch, nodes) - switch { - case was && said.Released: - p.endDeliveryWait(ctx, key, said, deliveryWaitReleased) // the want is written now, so it changes nothing at the next pass - return false - case !waits: - if was { - p.endDeliveryWait(ctx, key, said, deliveryWaitRan) - } - return false - } - body := deliveryWaitBody(module, fp, d, p.clock()) - if was && said.On == body.On && said.Version == fp && slices.Equal(said.Machines, body.Machines) { - return true - } - if was { - body.Since = said.Since - } - id := fmt.Sprintf("%s.delivery.%s.%s.%s", key, fp, body.On, strings.Join(body.Machines, ",")) - if err := p.wantsSay(meshwants.EventWaitHeld, body, id); err != nil { - p.logf("the delivery wait of %s is not said, and is said at the next pass: %v", module, err) - return true - } - kept := deliveryWaiting{Wait: body} - p.put(ctx, buckets.DeliveryWaits(), key, func() ([]byte, error) { return json.Marshal(kept) }) - p.logf("%s at %s on %s waits for the delivery of its dependency %s on %s: the rules write no want of it until then (design 47 G §376)", module, shortHex(fp), branch, body.On, strings.Join(body.Machines, ", ")) - return true -} - -// endDeliveryWait says wait-ended of a delivery wait with how and removes it from the state. -func (p *passer) endDeliveryWait(ctx context.Context, key string, w deliveryWaiting, how string) { - body := w.Wait - body.How = how - if err := p.wantsSay(meshwants.EventWaitEnded, body, key+".delivery."+body.Version+"."+how); err != nil { - p.logf("the end of the delivery wait of %s is not said, and is said at the next pass: %v", w.Module, err) - return - } - p.remove(ctx, buckets.DeliveryWaits(), key) -} - -// againAsk is one ask of assigner_delivered_again, kept in the state delivered-again under its module (A-250): the version to say -// delivered again, who asked and why, and, while it does not hold, why not. -type againAsk struct { - Module string `json:"module"` - Version string `json:"version"` - By string `json:"by"` - Why string `json:"why"` - Waits string `json:"waits,omitempty"` -} - -// releaseOf is the release heard of module's version. -func (r *releasedVersions) releaseOf(module, version string) (releaseHeard, bool) { - r.mu.Lock() - defer r.mu.Unlock() - if h, ok := r.newest[module]; ok && h.Version == version { - return h, true - } - for key, k := range r.seen { - if strings.HasPrefix(key, module+"@") && k.Version == version { - return releaseHeard{Version: version, Parts: k.Parts}, true - } - } - return releaseHeard{}, false -} - -// deliveredAgain takes each ask of assigner_delivered_again, heard at once through the watch of the state delivered-again: it says -// version-delivered again when every present machine placing the module runs that version healthy and removes the ask, else keeps -// it with why it does not hold, which assigner_waits reads. -func (p *passer) deliveredAgain(ctx context.Context) { - if p.kvs[buckets.DeliveredAgain()] == nil { - return - } - for key, value := range p.w.read(buckets.DeliveredAgain()) { - var a againAsk - if err := json.Unmarshal(value, &a); err != nil || a.Version == "" { - p.logf("%s %s holds no ask, left: %v", buckets.DeliveredAgain(), key, err) - continue - } - module := a.Module - if module == "" { - module = key - } - waits := "" - rel, ok := p.w.released.releaseOf(module, a.Version) - notRunning, away, placing := p.deliveryStanding(module, a.Version) - switch { - case !ok: - waits = fmt.Sprintf("no version-released of %s %s is heard", module, a.Version) - case placing == 0: - waits = fmt.Sprintf("no machine places %s", module) - case len(away) == placing: - waits = fmt.Sprintf("every machine placing %s is away: %s", module, strings.Join(away, ", ")) - case len(notRunning) > 0: - waits = fmt.Sprintf("%s %s does not run healthy on %s", module, a.Version, strings.Join(notRunning, ", ")) - } - if waits == "" { - if err := p.sayDelivered(module, rel, away); err != nil { - p.logf("version-delivered of %s %s asked again is not said, and is asked at the next pass: %v", module, a.Version, err) - continue - } - p.logf("version-delivered of %s %s said again, asked by %s: %s", module, a.Version, a.By, a.Why) - p.remove(ctx, buckets.DeliveredAgain(), key) - continue - } - if a.Waits != waits { - a.Waits = waits - p.put(ctx, buckets.DeliveredAgain(), key, func() ([]byte, error) { return json.Marshal(a) }) - } - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered_test.go deleted file mode 100644 index a8d3827f..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/delivered_test.go +++ /dev/null @@ -1,188 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - - "mesh-delivery-assigner/internal/buckets" -) - -// Design 47 G §376, §389, §498, A-189, A-246: a released version is said version-delivered once, when every present machine placing -// it runs healthy the want naming it, a machine away named and not waited for, none when every placing machine is away; a dependent -// whose want changes fingerprint waits by an edge of kind 2, 4 or 6 for its dependency's newest released version not yet said -// delivered, said wait-held (step delivery, 60 minutes from the release) once and kept in delivery-waits, and wait-ended ran once -// the version is delivered; a kind-1 edge, or a dependency with no version released, waits for nothing; one released by hand ends -// released and its want is written. -func TestADependentWaitsForItsDependencysDelivery(t *testing.T) { - ctx := context.Background() - at := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) - waits := &memKV{values: map[string][]byte{}} - again := &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.DeliveryWaits(): waits, buckets.DeliveredAgain(): again}, at) - p.logf = t.Logf - var events []said - p.publishWants = func(event string, body any, id string) error { - raw, _ := json.Marshal(body) - events = append(events, said{event, id, raw}) - return nil - } - p.w.runningHeard = true - placeOn(t, p.w, "ace", "db", "app") - placeOn(t, p.w, "g14", "db") - fp := func(c string) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(c, 64) } - want := func(node, module, fp, version string) { - key, _ := mwc.WantKey(node, module) - raw, err := mwc.EncodeWant(mwc.Want{Fp: fp, Version: version, Self: true}) - if err != nil { - t.Fatal(err) - } - p.w.put(buckets.Wants(), key, raw) - } - runs := func(node, module, fp string) { - raw, _ := json.Marshal(map[string]any{"v": 1, "node": node, "module": module, "fingerprint": fp, "at": at}) - if err := p.w.hearRunning("running", raw); err != nil { - t.Fatal(err) - } - } - source := int64(3) - release := func(module, version string) { - source++ - raw, _ := json.Marshal(meshcatalog.VersionReleasedEvent{V: 1, Module: module, Version: version, Source: source, At: &at, - Artifacts: []meshcatalog.VersionArtifact{{Artifact: "program", Digest: "sha256:d"}}}) - if err := p.w.released.put(raw); err != nil { - t.Fatal(err) - } - } - saidOf := func(event string) []said { - var out []said - for _, e := range events { - if e.event == event { - out = append(out, e) - } - } - return out - } - want("ace", "app", fp("1"), "") - - // No version of db is released: app's new fingerprint waits for nothing. - p.w.putEdges(trunkBranch, "app", []meshcatalog.Edge{{Kind: 4, From: "db:program", To: "app:program"}}) - if p.holdForDelivery(ctx, "app", fp("2"), trunkBranch, []string{"ace"}) { - t.Fatal("app waits with no version of db released") - } - release("db", "1.1.0") - want("ace", "db", fp("d"), "1.1.0") - want("g14", "db", fp("d"), "1.1.0") - runs("ace", "db", fp("d")) - // A kind-1 edge orders nothing. - p.w.putEdges(trunkBranch, "app", []meshcatalog.Edge{{Kind: 1, From: "db:program", To: "app:program"}}) - if p.holdForDelivery(ctx, "app", fp("2"), trunkBranch, []string{"ace"}) { - t.Fatal("a kind-1 edge holds app") - } - p.w.putEdges(trunkBranch, "app", []meshcatalog.Edge{{Kind: 4, From: "db:program", To: "app:program"}}) - // app's want unchanged waits for nothing. - if p.holdForDelivery(ctx, "app", fp("1"), trunkBranch, []string{"ace"}) { - t.Fatal("app's unchanged want waits") - } - // g14 places db and does not run 1.1.0 yet: not delivered, and app's new version waits, said once. - p.judgeDelivered() - if !p.holdForDelivery(ctx, "app", fp("2"), trunkBranch, []string{"ace"}) || !p.holdForDelivery(ctx, "app", fp("2"), trunkBranch, []string{"ace"}) { - t.Fatal("app does not wait for db 1.1.0") - } - if len(saidOf(meshwants.EventVersionDelivered)) != 0 { - t.Fatalf("db is said delivered while g14 does not run it: %+v", events) - } - held := saidOf(meshwants.EventWaitHeld) - if len(held) != 1 { - t.Fatalf("wait-held said %d times, want once", len(held)) - } - var w meshwants.Wait - _ = json.Unmarshal(held[0].body, &w) - if w.Step != meshwants.WaitStepDelivery || w.Part != "db" || w.Module != "app" || w.Version != fp("2") || w.Bound != "1h0m0s" || - w.ClockFrom == nil || !w.ClockFrom.Equal(at) || len(w.Machines) != 1 || w.Machines[0] != "g14" || w.Words != "waiting for delivery of db 1.1.0 on g14" { - t.Errorf("the wait said is %+v", w) - } - if _, ok := waits.values["app"]; !ok { - t.Errorf("the wait is not kept in delivery-waits: %v", waits.values) - } - // g14 away: every present machine runs it, so it is delivered, g14 named away; once only. - p.w.heardPresence("g14", true, at) - p.judgeDelivered() - p.judgeDelivered() - delivered := saidOf(meshwants.EventVersionDelivered) - var d meshwants.VersionDeliveredEvent - if len(delivered) != 1 || json.Unmarshal(delivered[0].body, &d) != nil || d.Module != "db" || d.Version != "1.1.0" || - len(d.Away) != 1 || d.Away[0] != "g14" || d.Parts["db:program"] != "sha256:d" { - t.Fatalf("version-delivered said %+v", delivered) - } - // The wait ends ran, and app's want goes on. - if p.holdForDelivery(ctx, "app", fp("2"), trunkBranch, []string{"ace"}) { - t.Fatal("app still waits once db 1.1.0 is delivered") - } - ended := saidOf(meshwants.EventWaitEnded) - if len(ended) != 1 || json.Unmarshal(ended[0].body, &w) != nil || w.How != deliveryWaitRan { - t.Errorf("wait-ended said %+v", ended) - } - if _, ok := waits.values["app"]; ok { - t.Error("the ended wait is kept") - } - // A newer release with every placing machine away is not delivered, and its wait says the machines it waits to be back. - release("db", "1.2.0") - p.w.heardPresence("ace", true, at) - p.judgeDelivered() - if len(saidOf(meshwants.EventVersionDelivered)) != 1 { - t.Error("a version is said delivered while every placing machine is away") - } - if !p.holdForDelivery(ctx, "app", fp("3"), trunkBranch, []string{"ace"}) { - t.Fatal("app does not wait for db 1.2.0") - } - held = saidOf(meshwants.EventWaitHeld) - if json.Unmarshal(held[len(held)-1].body, &w) != nil || w.Words != "waiting for ace, g14 to be back" { - t.Errorf("the wait while every machine is away says %q", w.Words) - } - // Released by hand (assigner_release writes it): the wait ends released and app's want is written. - var kept deliveryWaiting - _ = json.Unmarshal(waits.values["app"], &kept) - kept.Released = true - raw, _ := json.Marshal(kept) - p.w.put(buckets.DeliveryWaits(), "app", raw) - if p.holdForDelivery(ctx, "app", fp("3"), trunkBranch, []string{"ace"}) { - t.Fatal("a released wait holds app") - } - ended = saidOf(meshwants.EventWaitEnded) - if json.Unmarshal(ended[len(ended)-1].body, &w) != nil || w.How != deliveryWaitReleased { - t.Errorf("the released wait ended %+v", w) - } - // Presence: the later of machine-away and machine-back holds, whichever stream is read first. - p.w.heardPresence("ace", false, at.Add(time.Minute)) - p.w.heardPresence("ace", true, at) - if p.w.awayNow()["ace"] || !p.w.online()["ace"] { - t.Error("an older machine-away read after a newer machine-back leaves ace away") - } - // A-250: an ask of assigner_delivered_again of 1.1.0, which ace (present) runs and g14 (away) does not, is said again and taken - // away; one of 1.2.0, which no machine runs, is kept with why. - ask := func(version string) { - raw, _ := json.Marshal(againAsk{Module: "db", Version: version, By: "op", Why: "the catalog missed it"}) - p.w.put(buckets.DeliveredAgain(), "db", raw) - } - before := len(saidOf(meshwants.EventVersionDelivered)) - ask("1.1.0") - p.deliveredAgain(ctx) - if len(saidOf(meshwants.EventVersionDelivered)) != before+1 || p.w.has(buckets.DeliveredAgain(), "db") { - t.Errorf("the ask of 1.1.0 said %d version-delivered and is kept %v", len(saidOf(meshwants.EventVersionDelivered))-before, p.w.has(buckets.DeliveredAgain(), "db")) - } - ask("1.2.0") - p.deliveredAgain(ctx) - var a againAsk - if json.Unmarshal(p.w.value(buckets.DeliveredAgain(), "db"), &a) != nil || a.Waits != "db 1.2.0 does not run healthy on ace" || - len(saidOf(meshwants.EventVersionDelivered)) != before+1 { - t.Errorf("the ask of 1.2.0 = %+v", a) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/enginewords.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/enginewords.go new file mode 100644 index 00000000..10cf687f --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/enginewords.go @@ -0,0 +1,153 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "maps" + "slices" + "sync" + "time" + + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// engineWords is one node's key of the assigner's state engine-reads (design 47 H-A-104): the words its node-engine said it +// reads in the last manifest-reads-changed the assigner heard of it, and when the engine said them. +type engineWords struct { + Words []string `json:"words"` + At time.Time `json:"at"` +} + +// engineReadsKept is what the assigner holds of its state engine-reads (design 47 H-A-104, §772): written when it hears the +// node-engine seat's manifest-reads-changed, and read back from the state's watch at the start, so a restart loses no node's +// words; no verb is asked and no node's record is read in its place. +type engineReadsKept struct { + mu sync.Mutex + by map[string]engineWords + // put writes one node's key; nil keeps the words in memory only (the state could not be opened). + put func(node string, value []byte) error + // changed wakes a pass when a node's words changed; nil wakes nothing. + changed func() +} + +// heard keeps what one manifest-reads-changed says of its node: written into the state first, then held. +func (k *engineReadsKept) heard(e nodeengine.ManifestReadsChangedEvent) error { + at, err := time.Parse(time.RFC3339Nano, e.At) + if err != nil { + return fmt.Errorf("at %q is no RFC 3339 time", e.At) + } + v := engineWords{Words: slices.Clone(e.ManifestReads), At: at} + if k.put != nil { + raw, err := json.Marshal(v) + if err != nil { + return err + } + if err := k.put(e.Node, raw); err != nil { + return fmt.Errorf("the words of %s are not written into the state engine-reads: %w", e.Node, err) + } + } + k.hold(e.Node, v, false) + return nil +} + +// take takes in one key of the state as its watch hands it: at the start every node's words, later the assigner's own writes +// (which change nothing) and a key dropped. +func (k *engineReadsKept) take(node string, raw []byte, deleted bool) error { + if deleted { + k.hold(node, engineWords{}, true) + return nil + } + var v engineWords + if err := json.Unmarshal(raw, &v); err != nil { + return fmt.Errorf("the key %s of the state engine-reads does not read: %w", node, err) + } + k.hold(node, v, false) + return nil +} + +// hold sets one node's words, waking a pass when they changed. +func (k *engineReadsKept) hold(node string, v engineWords, deleted bool) { + k.mu.Lock() + old, had := k.by[node] + switch { + case deleted: + delete(k.by, node) + default: + if k.by == nil { + k.by = map[string]engineWords{} + } + k.by[node] = v + } + changed := (had && deleted) || (!deleted && (!had || !old.At.Equal(v.At) || !slices.Equal(old.Words, v.Words))) + k.mu.Unlock() + if changed && k.changed != nil { + k.changed() + } +} + +// words is what node's engine said it reads, and whether the assigner holds its key; nil k holds none. +func (k *engineReadsKept) words(node string) ([]string, bool) { + if k == nil { + return nil, false + } + k.mu.Lock() + defer k.mu.Unlock() + v, ok := k.by[node] + return slices.Clone(v.Words), ok +} + +// nodes is every node whose key the assigner holds, sorted. +func (k *engineReadsKept) nodes() []string { + if k == nil { + return nil + } + k.mu.Lock() + defer k.mu.Unlock() + return slices.Sorted(maps.Keys(k.by)) +} + +// engineReadsTool lists the assigner's state engine-reads: what each node's node-engine said it reads, and when. +const engineReadsTool = "assigner_engine_reads" + +// readEngineReads is the state engine-reads, every node's key or node's alone. +func readEngineReads(st store, node string) (any, error) { + keys := []string{node} + if node == "" { + var err error + if keys, err = st.keys(engineReadsState); err != nil { + return nil, err + } + slices.Sort(keys) + } + out := map[string]engineWords{} + for _, key := range keys { + raw, _, found, err := st.get(engineReadsState, key) + if err != nil { + return nil, err + } + if !found { + if node != "" { + return nil, fmt.Errorf("the assigner holds no words of %s: its node-engine said no %s it heard", node, nodeengine.EventManifestReadsChanged) + } + continue + } + var v engineWords + if err := json.Unmarshal(raw, &v); err != nil { + return nil, errors.Join(fmt.Errorf("the key %s does not read", key), err) + } + out[key] = v + } + return map[string]any{"nodes": out}, nil +} + +// engineReadsTools is the tool over the runtime's state. +func engineReadsTools(st store) []stdio.Tool { + return []stdio.Tool{{Name: engineReadsTool, Description: "What each node's node-engine says it reads of a manifest, as the " + + "assigner keeps it in its state engine-reads (design 47 H-A-104): per node the words of the last manifest-reads-changed " + + "it heard and when the engine said them. A node with no key states nothing the assigner heard, so its wants are held " + + "awaiting its hand placement (§772). With node, that node's key alone.", + Input: schema(map[string]string{"node": "one node; every node when left out"}, nil), + Run: func(a map[string]any) (any, error) { return readEngineReads(st, arg(a, "node")) }}} +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go index 4afa0d44..44385d0c 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "maps" "slices" "strings" "time" @@ -35,13 +34,11 @@ type graphKeeper struct { source int64 ok bool said string - // cycled is the cycles last said (assigngraph.Cycles). - cycled string // full says the next revision did not fit the store, so no want is written (graphfill.go); started says the first pass judged the fill. full, started bool - // pins removes a revision that ceased to be current once nothing pins it (pins.go); nil keeps every revision. - pins *pinKeeper - // computed is the revision computed by the last pass, with its entries: the edges the wants wait on (actingwaits.go). + // revisions removes a revision that ceased to be current (revisions.go, H-A-86); nil keeps every revision. + revisions *revisionRemover + // computed is the revision computed by the last pass, with its entries: its edges. computed assigngraph.Computed } @@ -53,21 +50,12 @@ func (p *passer) keepGraph(ctx context.Context) { } c := assigngraph.Compute(p.graphInputs()) g.computed = c - if p.ends != nil { - p.ends.setRunner(assigngraph.Runner(slices.Collect(maps.Values(c.Entries)))) // the runner's member of a group goes first (A-334) - } for _, u := range c.Uncomputed { if g.said != u { g.said = u p.logf("the assignment graph leaves out %s", u) } } - if cycles := strings.Join(c.Cycles, "; "); cycles != g.cycled { - g.cycled = cycles - if cycles != "" { - p.logf("the assignment graph holds a cycle among the acting waits, within one step or across steps: %s", cycles) - } - } if !g.started { g.started = true p.fillAtStart(ctx) @@ -95,8 +83,8 @@ func (p *passer) keepGraph(ctx context.Context) { return } p.fillingAfter(ctx, used) - if g.pins != nil { - g.pins.judge() // the revision before ceased to be current + if g.revisions != nil { + g.revisions.judge() // the revision before ceased to be current } id := fmt.Sprintf("%s-%s-%d", meshwants.EventAssignmentRevision, rev.Kind, rev.Revision) if err := p.wantsSay(meshwants.EventAssignmentRevision, assigngraph.Event(rev), id); err != nil { @@ -131,16 +119,12 @@ func (p *passer) capabilityDigests() []meshwants.AssignmentRevisionCapabilitiesI slices.Sort(known) var out []meshwants.AssignmentRevisionCapabilitiesItem for _, node := range known { - read := p.reads != nil && p.reads.Known(node) + reads, read := p.reads.words(node) rec, recorded := records[node] if !read && !recorded { continue } - var reads []string - if read { - reads = slices.Clone(p.reads.Reads([]string{node})[node]) - slices.Sort(reads) - } + slices.Sort(reads) item := meshwants.AssignmentRevisionCapabilitiesItem{Node: node, Readings: slices.Clone(rec.RunnerCapabilities), Fingerprint: rec.RunnerFingerprint} slices.Sort(item.Readings) @@ -158,7 +142,7 @@ func (p *passer) capabilityDigests() []meshwants.AssignmentRevisionCapabilitiesI // sayWanted says `wanted` for a want written or removed now, from the revision the pass keeps (A-61, A-67); with none kept, it is // said in the log that it was not said. fp is the want's, and for a removal the fingerprint last wanted there. -func (p *passer) sayWanted(node, module, fp string, removed bool) { +func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool) { if p.publishWants == nil { return } @@ -168,7 +152,7 @@ func (p *passer) sayWanted(node, module, fp string, removed bool) { return } at := p.clock().UTC() - body := meshwants.WantedEvent{V: keys.Version, Node: node, Module: module, Fp: fp, Removed: removed, Revision: g.kept.Revision, Source: g.source, At: &at} + body := meshwants.WantedEvent{V: keys.Version, Node: node, Module: module, Fp: fp, Pipeline: pipeline, Removed: removed, Revision: g.kept.Revision, Source: g.source, At: &at} event := strings.TrimPrefix(meshwants.SubjectWanted(keys.EscapeName(node), keys.EscapeName(module)), "mesh.seat."+meshwants.Seat+".event.") id := fmt.Sprintf("wanted.%s.%s.%s.%t.%d.%s", keys.EscapeName(node), keys.EscapeName(module), fp, removed, g.kept.Revision, at.Format(time.RFC3339Nano)) if err := p.wantsSay(event, body, id); err != nil { @@ -178,10 +162,9 @@ func (p *passer) sayWanted(node, module, fp string, removed bool) { // namedBuild answers what a want of module at fp names of its build (design 47 G §379, §526, A-96): a kept build's or another watched // branch's parts and keeping node from the builder's `build.ended`; for the trunk's entry (fp is the module's, trunkFP), the released -// version and the parts of the `version-released` of the module whose source is the one its fingerprint was built at, the entry's -// `fp-src` (A-419: a merge that does not rebuild the module leaves it so), or for an entry naming none the one whose parts are the -// digests its build.ended made (A-421). A trunk entry whose release is not heard is said, once, naming which was tried. -func (p *passer) namedBuild(module, trunkFP string, fpSrc int64, unreleased ...bool) func(fp string) (assign.KeptBuild, bool) { +// version and the parts of the `version-released` whose parts are the digests its build.ended made (A-421): the catalog stamps no +// source on an entry (design 47 H §764, H-A-48). A trunk entry whose release is not heard is said, once, naming which was tried. +func (p *passer) namedBuild(module, trunkFP string, unreleased ...bool) func(fp string) (assign.KeptBuild, bool) { return func(fp string) (assign.KeptBuild, bool) { if k, ok := p.w.ends.partsOf(module, fp); ok { return k, true @@ -206,25 +189,11 @@ func (p *passer) namedBuild(module, trunkFP string, fpSrc int64, unreleased ...b p.unheardRelease(module, fp, "no assignment graph revision is kept yet, so no release is looked up") return assign.KeptBuild{}, false } - if fpSrc == 0 { - // No fp-src (its fingerprint moved with no merge): the release whose parts are what its build made (A-421). - if k, ok := p.w.released.withParts(module, p.w.ends.madeOf(module, fp)); ok { - return k, true - } - p.unheardRelease(module, fp, "its catalog entry names no fp-src, and no version-released of it names the parts its build.ended made") - return assign.KeptBuild{}, false + if k, ok := p.w.released.withParts(module, p.w.ends.madeOf(module, fp)); ok { + return k, true } - k, ok := p.w.released.of(module, fpSrc) - if !ok { - // The release names the source its reservation was made at, which can be an earlier revision than the entry's - // fp-src (novox 2026-11-16: mesh-hand-acts 0.1.13 released from 962, its entry's fp-src 963): the release whose parts - // are exactly the digests this fingerprint's build made is this build's (A-421). - if k, ok = p.w.released.withParts(module, p.w.ends.madeOf(module, fp)); ok { - return k, true - } - p.unheardRelease(module, fp, fmt.Sprintf("no version-released of it from source %d, the one its fingerprint was built at, nor one naming the parts its build.ended made, is heard", fpSrc)) - } - return k, ok + p.unheardRelease(module, fp, "no version-released of it names the parts its build.ended made") + return assign.KeptBuild{}, false } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/halves.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/halves.go index 49dad157..49f8a57c 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/halves.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/halves.go @@ -148,11 +148,6 @@ func (h *halves) changed(branch, module string, raw json.RawMessage, deleted boo func fromEntry(e meshcatalog.CatalogEntry) (meshcatalog.ModulesByModuleValue, graph.Node, *parsed) { v := meshcatalog.ModulesByModuleValue{Commit: e.Commit, Fp2: e.Fp, Needs: e.Needs, None2: e.None, Packages: e.Packages, Path: e.Path, Repository: e.Repository, Verdict: e.Verdict} - // The source revisions it was taken in at and its fingerprint was built at: the release of a trunk build is matched by the - // latter (design 47 G A-419). - if e.Inputs != nil { - v.Src, v.FpSrc = e.Inputs.Src, e.Inputs.FpSrc - } n := graph.Node{FP: e.Fp, None: e.None, Repository: e.Repository, Path: e.Path, Bases: e.Bases, Provides: e.Provides, Requires: e.Requires, Awaits: e.Awaits, Invokes: e.Invokes, Serves: e.Serves, LeftOut: e.LeftOut} for p := range e.Packages { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go index 74648708..93781e6d 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go @@ -9,7 +9,6 @@ import ( "fmt" mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" wd "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-sdk/go/keys" "io" "maps" @@ -25,13 +24,15 @@ import ( "time" "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + mconds "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - mdcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" @@ -45,7 +46,6 @@ import ( mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" mdseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/announce" @@ -78,6 +78,8 @@ type bed struct { buckets map[string]jetstream.KeyValue // placed is, by node, the modules the bed's assignments names on it (place). placed map[string][]string + // builds is, by node and module, the build a placement names (placeBuild, design 47 H §750). + builds map[string]map[string]string // journal is what the programs the bed starts write in their journal. journal *journal // cat is the catalog half the seat mesh-catalog serves the assigner (serveCatalog). @@ -91,6 +93,9 @@ type catalogBed struct { entries map[string]map[string]meshcatalog.CatalogEntry rev map[string]int64 bodies map[string]string + // at is, by branch and revision, a revision's entries a test keeps whole: the verb revision answers a revision asked by its + // number from here, and the current one otherwise. + at map[string]map[int64][]meshcatalog.CatalogEntry } // journal is a program's journal as the bed hears it: a test awaits a line in it (await) where the line is the only sign @@ -202,10 +207,9 @@ func startBed(t *testing.T, leaveOut string) *bed { if _, err := js.CreateOrUpdateConsumer(ctx, nodeengine.StreamRunning, jetstream.ConsumerConfig{Durable: "n1_mesh-delivery-assigner", AckPolicy: jetstream.AckExplicitPolicy}); err != nil { t.Fatal(err) } - // The watchman's machine-away and machine-back and the assigner's own version-delivered, kept by age, which it reads whole from their - // first message at its start and then follows through its durable (design 47 G §376, §389, A-189, A-233). - for _, k := range []struct{ stream, subject string }{{wd.StreamMachineAway, wd.SubjectMachineAwayAll}, {wd.StreamMachineBack, wd.SubjectMachineBackAll}, - {meshwants.StreamVersionDelivered, meshwants.SubjectVersionDelivered}} { + // The watchman's machine-away and machine-back, kept by age, which it reads whole from their first message at its start and + // then follows through its durable (design 47 G §389, A-233). + for _, k := range []struct{ stream, subject string }{{wd.StreamMachineAway, wd.SubjectMachineAwayAll}, {wd.StreamMachineBack, wd.SubjectMachineBackAll}} { if _, err := js.CreateStream(ctx, keptStream(k.stream, k.subject, jetstream.StreamConfig{MaxAge: 90 * 24 * time.Hour})); err != nil { t.Fatal(err) } @@ -213,6 +217,14 @@ func startBed(t *testing.T, leaveOut string) *bed { t.Fatal(err) } } + // The seat mesh-catalog's version-released, kept by age, which the assigner reads whole and follows: a placement's build is + // named by the version released at it (design 47 H §750, H-A-99). + if _, err := js.CreateStream(ctx, keptStream(meshcatalog.StreamVersionReleased, meshcatalog.SubjectVersionReleased, jetstream.StreamConfig{MaxAge: 90 * 24 * time.Hour})); err != nil { + t.Fatal(err) + } + if _, err := js.CreateOrUpdateConsumer(ctx, meshcatalog.StreamVersionReleased, jetstream.ConsumerConfig{Durable: "n1_mesh-delivery-assigner", AckPolicy: jetstream.AckExplicitPolicy}); err != nil { + t.Fatal(err) + } // The builder's stream of build ends, kept last per subject (its manifest's `last-per-subject`, design 47 G A-181), which the assigner // reads whole at its start and follows through its durable, as the grants part makes it (from the last of each subject). if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: buildsStreamName, Subjects: []string{process.ModuleStreamSubject(builds.BuilderModule, builds.BuildEndedEvent+".>")}, MaxAge: 168 * time.Hour}); err != nil { // kept by age, as the bus keeps it live (2026-11-15): read whole from its first message all the same (A-233) @@ -231,7 +243,7 @@ func startBed(t *testing.T, leaveOut string) *bed { // machineEvent; the grants part's own test holds the derivation). if _, err := js.CreateOrUpdateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{Durable: "n1_mesh-delivery-assigner", FilterSubjects: []string{enginereads.SubjectPrefix + "*", "mesh.mod." + buckets.IndexerModule + ".event.views.>", - "mesh.mod." + buckets.TellerModule + ".event.views.>", meshcatalog.SubjectCatalogRevisionAll}, AckPolicy: jetstream.AckExplicitPolicy}); err != nil { + "mesh.mod." + buckets.TellerModule + ".event.views.>", meshcatalog.SubjectCatalogRevisionAll, pipelineEndedSubject}, AckPolicy: jetstream.AckExplicitPolicy}); err != nil { t.Fatal(err) } // The operator-channel seat's stream of conditions, which the assigner says its conditions on. @@ -508,7 +520,7 @@ func (b *bed) place(t *testing.T, node, module string) { } pl := mma.Assignments{Node: node} for _, m := range b.placed[node] { - pl.Modules = append(pl.Modules, mma.PlacedModule{Module: m}) + pl.Modules = append(pl.Modules, mma.PlacedModule{Module: m, Build: b.builds[node][m]}) } values, err := mmac.AssignmentsValues(pl) if err != nil { @@ -562,61 +574,131 @@ func (b *bed) get(t *testing.T, bucket, key string) []byte { return nil } -// 1297/B4 on the bus, 1906/D5: the controller sends ace, g14 and shanks builds while the canary ace does not run F healthy yet. -// Started whole, the assigner wants F on the canary and writes no difference at all: there is no send to compare with (ADR 0539 -// §8). 1945/AC19, AC21: shanks, which runs the older fingerprint (composed and granted) with no want, gets its first want at that -// fingerprint, never at F; g14, which runs a build of the old form, gets none, so web is on neither list there and the assigner -// raises the condition naming g14, web and the canary rule; once ace runs F healthy, g14's first want names F and the condition is -// cleared. +// moverAt is one mover of a pipeline the bed's runner says ended: its module, the fingerprint and version it released, its tier. +type moverAt struct { + module, fp, version string + tier int64 +} + +// ended says the runner's pipeline-ended of the trunk's pipeline at source, with outcome, each mover's required steps passed when +// the pipeline passed and its validate step failed when it did not (design 47 H §746, §750). +func (b *bed) ended(t *testing.T, source int64, outcome string, movers ...moverAt) string { + t.Helper() + pipeline := fmt.Sprintf("%s.%d", trunkBranch, source) + step := mpr.StepResult{Step: mpr.StepResultStepValidate, Status: mpr.StepResultStatusRequired, Outcome: mpr.StepResultOutcomePassed} + if outcome != mpr.PipelineEndedOutcomePassed { + step.Outcome = mpr.StepResultOutcomeFailed + } + e := mpr.PipelineEnded{V: 1, Pipeline: pipeline, Branch: trunkBranch, Source: source, Outcome: outcome} + for _, m := range movers { + e.Movers = append(e.Movers, mpr.MoverEnded{Module: m.module, Fingerprint: m.fp, Version: m.version, Tier: m.tier, Steps: []mpr.StepResult{step}}) + } + raw, _ := json.Marshal(e) + if _, err := b.js.Publish(b.ctx, mpr.SubjectPipelineEnded(trunkBranch, fmt.Sprint(source)), raw); err != nil { + t.Fatal(err) + } + return pipeline +} + +// readsVersion says node's node-engine reads a want by version (the node-engine seat's manifest-reads-changed, design 47 H §772). +func (b *bed) readsVersion(t *testing.T, node string) { + t.Helper() + body, _ := json.Marshal(nodeengine.ManifestReadsChangedEvent{V: 1, Node: node, ManifestReads: []string{nodeengine.ManifestReadWordWantVersion}, + At: time.Now().UTC().Format(time.RFC3339Nano), Why: enginereads.ReadsStarted}) + if _, err := b.js.Publish(b.ctx, enginereads.SubjectPrefix+node, body); err != nil { + t.Fatal(err) + } +} + +// runs says node runs module at fp, healthy, as its node-engine writes its have and says `running`. +func (b *bed) runs(t *testing.T, node, module, fp string) { + t.Helper() + have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fp, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + b.putJSON(t, buckets.Have, node+"."+module, have) +} + +// placeBuild names build in the placement of module on node (design 47 H §750, H-A-99). +func (b *bed) placeBuild(t *testing.T, node, module, build string) { + t.Helper() + if b.builds == nil { + b.builds = map[string]map[string]string{} + } + if b.builds[node] == nil { + b.builds[node] = map[string]string{} + } + b.builds[node][module] = build + b.place(t, node, module) +} + +// versionsOf are the versions the want of module on node named, in the order written, each once. +func (b *bed) versionsOf(t *testing.T, node, module string) []string { + t.Helper() + key, _ := mwc.WantKey(node, module) + history, err := b.buckets[buckets.Wants()].History(b.ctx, key) + if err != nil { + t.Fatalf("the want of %s on %s has no history: %v", module, node, err) + } + var out []string + for _, e := range history { + if w, err := mwc.DecodeWant(e.Value()); err == nil && (len(out) == 0 || out[len(out)-1] != w.Version) { + out = append(out, w.Version) + } + } + return out +} + +// Design 47 H §750, §751, §772 (acceptance test 113, the assigner's half), started whole on the bus: a want is the newest +// released version whose pipeline passed, written tier by tier. A provider (db) and its consumer (app) passed in one pipeline: +// db is wanted on ace and g14 naming its version, the pipeline and rank 1, whatever build the acting part (the node-engine +// module, whose pipeline never passed) runs, with no grant, no canary and no healthy provider; app's want waits until db runs +// on both, then names rank 2, and the pipeline says pipeline-wanted running. A failed pipeline's version is not wanted, and an +// older end heard after a newer one writes nothing. g14's placement naming db's first build reverts its want to the version +// released at it. A pipeline whose first tier never runs raises tier-late within tier-bound and writes no later tier. shanks, +// whose node-engine does not say it reads a want by version, has its want held, awaiting its hand placement. DLV-F5r (ADR 0621 +// §3, ADR 0631 §2): the assigner's first views wait on the indexer, whose late answer clears the condition naming it. func TestWantsAreWrittenAndNoDifferenceIs(t *testing.T) { - // DLV-F5r (ADR 0621 §3): the indexer's runtime starts together with the assigner and answers its first views only - // after the assigner asked; within the bound the assigner writes the wants from what they answer. up := make(chan struct{}) catalogLate = up - // ADR 0631 §2: while the indexer does not answer, past the first-view bound the assigner raises the condition naming - // the indexer and graph, and reads nothing of the indexer's state; the answer clears it. firstViewBound, firstViewWait = 300*time.Millisecond, 50*time.Millisecond - t.Cleanup(func() { catalogLate, firstViewBound, firstViewWait = nil, 0, 0 }) + tierBoundUnit = time.Second + t.Cleanup(func() { catalogLate, firstViewBound, firstViewWait, tierBoundUnit = nil, 0, 0, time.Minute }) for _, name := range append(append([]string{}, readBuckets...), watched()...) { if strings.HasPrefix(name, buckets.IndexerModule+"_") { t.Errorf("the assigner reads the indexer's state %s itself", name) } } b := startBed(t, "") - fNew, fOld := meshcatalogclient.SourcePrefix2+strings.Repeat("b", 64), meshcatalogclient.SourcePrefix2+strings.Repeat("a", 64) - digest := b.manifest(t, "web", `{"module":"web","version":"1"}`) - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fNew, Manifest: digest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - b.putModule(t, "web", mod) - built, err := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, By: "builder.novox.1", Step: "pushed", Manifest: digest}) - if err != nil { - t.Fatal(err) + t.Cleanup(func() { + if t.Failed() { + b.journal.mu.Lock() + t.Log(b.journal.text.String()) + b.journal.mu.Unlock() + } + }) + b.settings = map[string]string{"MESH_ASSIGNER_TIER_BOUND_FILE": "3"} // tier-bound 3 s at the test's unit + fpOf := func(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(string(c), 64) } + // The trunk's catalog half names every module at its newest fingerprint; none is built, granted or provided as the old rules + // asked. The acting part engine runs an older build on ace, and no pipeline of its newer one passed. + for module, fp := range map[string]string{"web": fpOf('b'), "db": fpOf('2'), "app": fpOf('3'), "cache": fpOf('5'), "api": fpOf('6'), "engine": fpOf('e')} { + digest := b.manifest(t, module, fmt.Sprintf(`{"module":%q,"version":"1"}`, module)) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: digest, Repository: "novox/" + module, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) + b.putModule(t, module, mod) } - bkey, _ := builds.BuilderBuildKey("web", fNew) - b.putBuild(t, bkey, built) - for _, fp := range []string{fNew, fOld} { - gkey, _ := grantsstate.PartGrantKey("web", fp) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - b.putJSON(t, buckets.Grants, gkey, grant) - } - runs := map[string]string{"ace": fOld, "g14": "build-9", "shanks": fOld} for _, node := range []string{"ace", "g14", "shanks"} { b.compose(t, node, 'c') - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: runs[node], Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, node+".web", have) b.place(t, node, "web") } - b.compose(t, "shanks", 'a') + for _, node := range []string{"ace", "g14"} { + b.place(t, node, "db") + b.place(t, node, "app") + b.readsVersion(t, node) + } + b.place(t, "ace", "engine") + b.runs(t, "ace", "engine", fpOf('d')) + b.place(t, "ace", "cache") + b.place(t, "ace", "api") b.start(t) - // DLV-F5r (ADR 0621 §1-§3, design 25 § 12 on a fresh mesh): the assigner asks nothing of the bus module (its manifest - // has no bus-state entry, the bed runs no bus module and holds no binding of one), and while it still waits on the - // indexer its runtime of its own answers each first-view tool to the readers its manifest's callers lists name, at - // its mark, and refuses every other account, the assigner's own included. - firstViewsBeforeAnyNewFormState(t, b, map[string][]string{ - "assignments": {"n1.mesh-grants", "n1.node-tools", "n1.mesh-delivery-indexer", "n1.mesh-delivery-builder", "n1.mesh-delivery-checker"}, - "seat-holders": {"n1.node-tools", "n1.mesh-delivery-builder", "n1.mesh-delivery-view", "n1.mesh-delivery-teller", "n1.mesh-delivery-checker", "n1.mesh-delivery-indexer"}, // A-298, A-302: the parts following it - "wants": {"n1.mesh-grants"}, - }) - // The reader raises the finding of the first view through the module's finding writer (A-26), its id escaped as a key's name. + firstViewsBeforeAnyNewFormState(t, b, []string{"assignments", "seat-holders", "wants"}) graphKey := views.FindingKey(views.FindingScope, keys.EscapeName(assignment.Module()+"."+meshcatalog.Seat+"."+views.BranchState(meshcatalog.StateCatalogRevisions, trunkBranch)), views.UnansweredKind) graphRaised := func() bool { e, err := b.buckets[assign.FindingsBucket()].Get(b.ctx, graphKey) @@ -633,66 +715,128 @@ func TestWantsAreWrittenAndNoDifferenceIs(t *testing.T) { t.Fatalf("the seat answered, and the condition naming its revision verb stands (finding %s)", graphKey) } } - - wkey, _ := mwc.WantKey("ace", "web") - want, err := mwc.DecodeWant(b.get(t, buckets.Wants(), wkey)) - if err != nil || want.Fp != fNew { - t.Fatalf("the canary ace's want = %+v, %v", want, err) + nc, err := nats.Connect(b.srv.ClientURL()) + if err != nil { + t.Fatal(err) } - if w := b.wantNow(t, "shanks", "web", "shanks' first want at what it runs", func(w mwc.Want) bool { return w.Fp != "" }); w.Fp != fOld || w.Composed != "" || !w.Self || w.Newest != fNew { - t.Errorf("shanks' want = %+v, want the fingerprint it runs, the node composing it, held back from F", w) + t.Cleanup(nc.Close) + wanted := make(chan meshwants.PipelineWanted, 8) + sub, err := nc.Subscribe("mesh.seat."+meshwants.Seat+".event."+meshwants.EventPipelineWanted+".>", func(m *nats.Msg) { + var e meshwants.PipelineWanted + _ = json.Unmarshal(m.Data, &e) + wanted <- e + }) + if err != nil { + t.Fatal(err) } - // DLV-O5 (ADR 0615 §5, ADR 0619 §2): each want is said on the assigner's views family, its value whole at its revision, - // and the first view at the mark followed by those events holds it as the bucket does; a waiter on put never wakes. - b.viewsHold(t, "wants", wkey) - var neither oc.Condition - for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline) && neither.Key == ""; time.Sleep(100 * time.Millisecond) { - for k, c := range b.conditionsSaid(t) { - if strings.HasSuffix(k, ".neither") && c.State == occ.StateRaised { - neither = c + t.Cleanup(func() { _ = sub.Unsubscribe() }) + subscribed(t, nc) + pipelineWanted := func(pipeline, outcome string) meshwants.PipelineWanted { + t.Helper() + for { + select { + case e := <-wanted: + if e.Pipeline == pipeline { + if e.Outcome != outcome { + t.Fatalf("pipeline-wanted of %s said %+v, want %s", pipeline, e, outcome) + } + return e + } + case <-time.After(15 * time.Second): + t.Fatalf("no pipeline-wanted of %s", pipeline) } } } - if neither.Subject.Machine != "g14" || !strings.HasPrefix(neither.Subject.ID, "g14.web") || neither.Severity != occ.SeverityUrgent || !strings.Contains(neither.Explanation, "canary") { - t.Errorf("the condition of web on neither list on g14 = %+v", neither) + + // §772: web passed; ace and g14 are wanted at its version, shanks' want is held awaiting its hand placement. + web := b.ended(t, 4, mpr.PipelineEndedOutcomePassed, moverAt{"web", fpOf('b'), "1.0.0", 1}) + if w := b.wantNow(t, "ace", "web", "web's want at its passed version", func(w mwc.Want) bool { return w.Version == "1.0.0" }); w.Fp != fpOf('b') || w.Pipeline != web || w.Rank != 1 || w.Hold { + t.Errorf("ace's want of web = %+v", w) } + if w := b.wantNow(t, "shanks", "web", "shanks' want held", func(w mwc.Want) bool { return w.Hold }); w.Words != mwc.HoldAwaitsHandPlacement || w.Version != "1.0.0" { + t.Errorf("shanks' want of web = %+v, want held awaiting its hand placement", w) + } + + // §750, §751: tier 1 (db) is wanted at once on every placing machine, tier 2 (app) only once db runs on both. + first := b.ended(t, 5, mpr.PipelineEndedOutcomePassed, moverAt{"db", fpOf('2'), "1.0.0", 1}, moverAt{"app", fpOf('3'), "1.0.0", 2}) + for _, node := range []string{"ace", "g14"} { + if w := b.wantNow(t, node, "db", "db's want at 1.0.0", func(w mwc.Want) bool { return w.Version == "1.0.0" }); w.Fp != fpOf('2') || w.Pipeline != first || w.Rank != 1 { + t.Errorf("%s's want of db = %+v", node, w) + } + } + if b.hasKey(buckets.Wants(), "want.ace.app") || b.hasKey(buckets.Wants(), "want.g14.app") { + t.Error("app, the consumer in tier 2, is wanted before its provider runs") + } + if b.hasKey(buckets.Wants(), "want.ace.engine") { + t.Error("the acting part is wanted with no pipeline of it passed") + } + b.runs(t, "ace", "db", fpOf('2')) + b.runs(t, "g14", "db", fpOf('2')) + for _, node := range []string{"ace", "g14"} { + if w := b.wantNow(t, node, "app", "app's want once db runs", func(w mwc.Want) bool { return w.Version == "1.0.0" }); w.Fp != fpOf('3') || w.Rank != 2 { + t.Errorf("%s's want of app = %+v", node, w) + } + b.runs(t, node, "app", fpOf('3')) + } + pipelineWanted(first, meshwants.PipelineWantedOutcomeRunning) + + // §750: a failed pipeline's version is not wanted; a newer passed one is, and an older end heard after it writes nothing. + b.ended(t, 6, mpr.PipelineEndedOutcomeFailed, moverAt{"db", fpOf('7'), "1.1.0", 1}) + b.ended(t, 8, mpr.PipelineEndedOutcomePassed, moverAt{"db", fpOf('9'), "1.2.0", 1}) + b.wantNow(t, "ace", "db", "db's want at 1.2.0", func(w mwc.Want) bool { return w.Version == "1.2.0" }) + b.ended(t, 7, mpr.PipelineEndedOutcomePassed, moverAt{"db", fpOf('8'), "1.1.5", 1}) + // §751: cache never runs, so its tier is late within tier-bound and api, tier 2, is never wanted; the late end is also the + // barrier after which the older end of db was heard. + late := b.ended(t, 10, mpr.PipelineEndedOutcomePassed, moverAt{"cache", fpOf('5'), "1.0.0", 1}, moverAt{"api", fpOf('6'), "1.0.0", 2}) + b.wantNow(t, "ace", "cache", "cache's want", func(w mwc.Want) bool { return w.Version == "1.0.0" }) + if e := pipelineWanted(late, meshwants.PipelineWantedOutcomeTierLate); len(e.NotRunning) != 1 || e.NotRunning[0].Module != "cache" || !slices.Equal(e.NotRunning[0].Machines, []string{"ace"}) { + t.Errorf("pipeline-wanted tier-late = %+v, want cache on ace not running", e) + } + lateKey := views.FindingKey(mconds.ScopeDelivery, assign.KeyID(late), kindTierLate) + if raw := b.get(t, assign.FindingsBucket(), lateKey); !strings.Contains(string(raw), "cache on ace") { + t.Errorf("the finding tier-late = %s", raw) + } + if b.hasKey(buckets.Wants(), "want.ace.api") { + t.Error("api, tier 2 of a late pipeline, is wanted") + } + for _, node := range []string{"ace", "g14"} { + if got := b.versionsOf(t, node, "db"); !slices.Equal(got, []string{"1.0.0", "1.2.0"}) { + t.Errorf("%s's want of db named %v, want 1.0.0 then 1.2.0: no failed and no older version", node, got) + } + } + + // §750, H-A-99: g14's placement names db's first build, which the builder made and the catalog released as 1.0.0: its want + // names that build and version, with no pipeline and no rank, while ace stays at 1.2.0. + made, _ := json.Marshal(builds.BuildEnded{Module: "db", Fingerprint: fpOf('2'), Outcome: builds.BuildDone, Made: map[string]string{"image": "sha256:d1"}}) + if _, err := b.js.Publish(b.ctx, process.ModuleStreamSubject(builds.BuilderModule, builds.BuildEndedEventName("db", fpOf('2'))), made); err != nil { + t.Fatal(err) + } + rel, _ := json.Marshal(meshcatalog.VersionReleasedEvent{Module: "db", Source: 5, Version: "1.0.0", Artifacts: []meshcatalog.VersionArtifact{{Artifact: "db:image", Digest: "sha256:d1"}}}) + if _, err := b.js.Publish(b.ctx, meshcatalog.SubjectVersionReleased, rel); err != nil { + t.Fatal(err) + } + b.placeBuild(t, "g14", "db", fpOf('2')) + if w := b.wantNow(t, "g14", "db", "g14's want back at db's first build", func(w mwc.Want) bool { return w.Fp == fpOf('2') }); w.Version != "1.0.0" || w.Pipeline != "" || w.Rank != 0 { + t.Errorf("g14's want of db at its placement's build = %+v", w) + } + if w := b.wantNow(t, "ace", "db", "ace's want of db", func(mwc.Want) bool { return true }); w.Version != "1.2.0" { + t.Errorf("ace's want of db = %+v, want 1.2.0", w) + } + + wkey, _ := mwc.WantKey("ace", "web") + // DLV-O5 (ADR 0615 §5, ADR 0619 §2): each want is said on the assigner's views family, its value whole at its revision. + b.viewsHold(t, "wants", wkey) if keys, err := b.buckets[buckets.Differences()].Keys(b.ctx); err == nil && len(keys) != 0 { t.Errorf("differences were written: %v", keys) } - if e, err := b.buckets[buckets.Wants()].Get(b.ctx, "want.g14.web"); err == nil { - t.Errorf("g14, which runs an old-form build and is held back by the canary, was given a want: %s", e.Value()) - } - // 1945/AC21: once the canary ace runs F healthy, g14's first want names F by the rules as usual, and web leaves neither list - // there by itself: the condition is cleared. - healthyF, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.web", healthyF) - if w := b.wantNow(t, "g14", "web", "g14's first want at F once the canary runs it healthy", func(w mwc.Want) bool { return w.Fp != "" }); w.Fp != fNew || w.Composed != "" || !w.Self || w.Newest != "" { - t.Errorf("g14's want = %+v, want F, the node composing it", w) + // Issue 1126 (ADR 0547 §2): placed through the catalogue's assignments, ace composes its own: its own want says so. + values, err := mmac.AssignmentsValues(mma.Assignments{Node: "ace", Modules: []mma.PlacedModule{{Module: "web"}}}) + if err != nil { + t.Fatal(err) } - cleared := false - for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline) && !cleared; time.Sleep(100 * time.Millisecond) { - c, ok := b.conditionsSaid(t)[neither.Key] - cleared = ok && c.State == occ.StateCleared - } - if !cleared { - t.Errorf("the condition of web on neither list on g14 was not cleared once g14 got its want") - } - - // Issue 1126 (ADR 0547 §2): the catalogue's assignments reaches the indexer's state naming web on every node the - // assignments name it on. Each node then composes its own: its want names the build alone (Self, no composition), and - // its own want says it composes its own, which the node agent reads (mesh-host's TestNats...ComposesAndAppliesItsOwn). - for _, node := range []string{"ace", "g14", "shanks"} { - values, err := mmac.AssignmentsValues(mma.Assignments{Node: node, Modules: []mma.PlacedModule{{Module: "web"}}}) - if err != nil { - t.Fatal(err) - } - for _, k := range slices.Sorted(maps.Keys(values)) { - b.putJSON(t, plc.AssignmentsBucket(), k, values[k]) - } - } - if w := b.wantNow(t, "ace", "web", "ace's want once placed: the build alone, composed by the node", func(w mwc.Want) bool { return w.Self }); w.Composed != "" || w.Fp != fNew { - t.Errorf("ace's want once placed = %+v", w) + for _, k := range slices.Sorted(maps.Keys(values)) { + b.putJSON(t, plc.AssignmentsBucket(), k, values[k]) } nkey, _ := mwc.WantNodeKey("ace") var self bool @@ -706,178 +850,29 @@ func TestWantsAreWrittenAndNoDifferenceIs(t *testing.T) { t.Error("ace's own want does not say it composes its own once placed") } - // ADR 0603 (L3, D15): one source revision changes a provider (db), its new consumer (app) and a build module (tool, whose - // recipe moved: ace runs its first build and the second, under the agreed toolchain, is the one the source revision names). - // The keys are written as their writers write them: the indexer's modules and manifests, the builder's builds, the - // controller's grants, the assignments seat's assignments and the provisioner's provisions. The build module's want moves at - // once and the provider's with it; the consumer's waits for its provision, with the words naming it, and moves once the - // provisioner provides it (rule 8, ADR 0570 §4): nothing waits for a turn or an order, only for a key. - offer := `"provides":[{"name":"database","scope":"mesh","answered-by":"helper"}]` - dbDigest := b.manifest(t, "db", `{"module":"db","version":"1",`+offer+`}`) - appDigest := b.manifest(t, "app", `{"module":"app","version":"1","requires":["database"]}`) - toolDigest := b.manifest(t, "tool", `{"module":"tool","version":"1"}`) - asked := func(state, why string) { - raw, err := meshprovisionerclient.Encode(meshprovisionerclient.Provision{V: meshprovisionerclient.Version, Provision: "database", Consumer: meshprovisionerclient.End{Module: "app", Node: "ace"}, - Provider: meshprovisionerclient.End{Module: "db", Node: "ace"}, State: state, Why: why, At: time.Now()}) - if err != nil { - t.Fatal(err) - } - b.putJSON(t, buckets.Provisions, "ace.app.database", raw) - } - // The provisioner keys it from the manifests, as soon as they are known and whether the provider is placed yet or not. - asked(meshprovisionerclient.Asked, "the provider's build is not done") - fpOf := func(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(string(c), 64) } - toolOld := fpOf('1') - for module, row := range map[string]struct { - fp, digest string - }{"db": {fpOf('2'), dbDigest}, "app": {fpOf('3'), appDigest}, "tool": {fpOf('4'), toolDigest}} { - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: row.fp, Manifest: row.digest, Repository: "novox/" + module, Commit: strings.Repeat("2", 40), Path: meshcatalogclient.RootPath}) - b.putModule(t, module, mod) - fps := []string{row.fp} - if module == "tool" { - fps = append(fps, toolOld) - } - for _, fp := range fps { - built, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, By: "builder.novox.1", Step: "pushed", Manifest: row.digest}) - bkey, _ := builds.BuilderBuildKey(module, fp) - b.putBuild(t, bkey, built) - gkey, _ := grantsstate.PartGrantKey(module, fp) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - b.putJSON(t, buckets.Grants, gkey, grant) - } - b.place(t, "ace", module) - } - runsOld, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: toolOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.tool", runsOld) - if w := b.wantNow(t, "ace", "tool", "the build module's want at the second build", func(w mwc.Want) bool { return w.Fp == fpOf('4') }); w.Fp != fpOf('4') { - t.Errorf("tool's want = %+v", w) - } - b.wantNow(t, "ace", "db", "the provider's want at its fingerprint", func(w mwc.Want) bool { return w.Fp == fpOf('2') }) - if e, err := b.buckets[buckets.Wants()].Get(b.ctx, "want.ace.app"); err == nil { - t.Errorf("app was wanted while its provision was asked: %s", e.Value()) - } - asked(meshprovisionerclient.Provided, "") - if w := b.wantNow(t, "ace", "app", "the consumer's want once its provision is provided", func(w mwc.Want) bool { return w.Fp == fpOf('3') }); w.Fp != fpOf('3') { - t.Errorf("app's want = %+v", w) - } - - // ADR 0618 §1, §3, §4, ADR 0624 §2: ace's engine answers the node-engine seat's manifest-reads; a build written only in a - // form it does not state is held on ace, naming the form, and moves once the engine says manifest-reads-changed on its - // subject and states the form; no timer moves it. g14's engine answers nothing and has no record: read as the baseline. - stated := catalogue.ManifestReads() - var statedMu sync.Mutex - // hold, when set, holds the next ask of ace's engine, answered with what it stated when asked, until it is closed; - // held is closed once that ask is held (issue 1966 note 170). - var hold, held chan struct{} - nc, err := nats.Connect(b.srv.ClientURL()) + // Design 47 H-A-104: ace's engine says manifest-reads-changed again; the assigner writes its words and when it said them into + // its state engine-reads, asking no verb; shanks' engine said nothing, so the state holds no key of it. + watch, err := b.buckets[engineReadsBucket()].Watch(b.ctx, "ace", jetstream.UpdatesOnly()) if err != nil { t.Fatal(err) } - defer nc.Close() - if _, err := nc.Subscribe("mesh.seat.node-engine.tool.manifest-reads.ace", func(m *nats.Msg) { - statedMu.Lock() - answer, _ := json.Marshal(nodeengine.ManifestReadsAnswer{V: 1, Node: "ace", ManifestReads: stated, StatedAt: time.Now().Format(time.RFC3339Nano)}) - release, isHeld := hold, held - hold = nil - statedMu.Unlock() - if release != nil { - close(isHeld) - <-release - } - reply, _ := json.Marshal(map[string]any{"result": json.RawMessage(answer), "node": "ace"}) - _ = m.Respond(reply) - }); err != nil { + defer watch.Stop() + at := time.Now().UTC().Truncate(time.Millisecond) + body, _ := json.Marshal(nodeengine.ManifestReadsChangedEvent{V: 1, Node: "ace", ManifestReads: []string{nodeengine.ManifestReadWordWantVersion}, At: at.Format(time.RFC3339Nano), Why: enginereads.ReadsStarted}) + if _, err := b.js.Publish(b.ctx, enginereads.SubjectPrefix+"ace", body); err != nil { t.Fatal(err) } - // The responder's interest is on the bus before the event that asks it is said: the assigner asks a node once per - // manifest-reads-changed and never on a timer, so an ask that lands before this subscription reached the server is - // answered by nobody and is never asked again (the group check of 616c42434ac2 on a loaded build node). - subscribed(t, nc) - say := func(why string) { - body, _ := json.Marshal(nodeengine.ManifestReadsChangedEvent{V: 1, Node: "ace", ManifestReads: stated, At: time.Now().Format(time.RFC3339Nano), Why: why}) - if _, err := b.js.Publish(b.ctx, enginereads.SubjectPrefix+"ace", body); err != nil { - t.Fatal(err) - } - } - say(enginereads.ReadsStarted) - formsOld, formsNew := fpOf('5'), fpOf('6') - formsDigest := b.manifest(t, "forms", `{"module":"forms","version":"1"}`) - formsAt := func(fp string) { - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: formsDigest, Repository: "novox/forms", Commit: strings.Repeat("3", 40), Path: meshcatalogclient.RootPath}) - b.putModule(t, "forms", mod) - } - for _, fp := range []string{formsOld, formsNew} { - build := builds.BuilderBuild{State: builds.BuildDone, By: "builder.novox.1", Step: "pushed", Manifest: formsDigest} - if fp == formsNew { - build.Needs = []string{"form:new"} - } - built, err := builds.EncodeBuilderBuild(build) - if err != nil { - t.Fatal(err) - } - bkey, _ := builds.BuilderBuildKey("forms", fp) - b.putBuild(t, bkey, built) - gkey, _ := grantsstate.PartGrantKey("forms", fp) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - b.putJSON(t, buckets.Grants, gkey, grant) - } - runsFormsOld, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: formsOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.forms", runsFormsOld) - b.place(t, "ace", "forms") - formsAt(formsOld) - b.wantNow(t, "ace", "forms", "the want at what ace runs, a build in the first form", func(w mwc.Want) bool { return w.Fp == formsOld }) - formsAt(formsNew) - statement := func(node string, ok func(enginereads.Statement) bool) enginereads.Statement { - var last enginereads.Statement - for deadline := time.Now().Add(15 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - if e, err := b.buckets[engineReadsBucket()].Get(b.ctx, node); err == nil { - if last, err = enginereads.Decode(e.Value()); err == nil && ok(last) { - return last - } - } - } - t.Fatalf("%s's engine reads are %+v", node, last) - return last - } - statement("ace", func(s enginereads.Statement) bool { return s.From == enginereads.FromVerb && s.Answered }) - if s := statement("g14", func(s enginereads.Statement) bool { return !s.Answered }); s.From != enginereads.FromNone { - t.Errorf("g14, whose engine answers nothing and which has no record, reads %+v", s) - } - if w := b.wantNow(t, "ace", "forms", "the want held on ace, naming the form", func(w mwc.Want) bool { - return w.Newest == formsNew && strings.Contains(w.Words, "form:new") - }); w.Fp != formsOld || w.Rule != mwc.RuleComposed { - t.Errorf("forms' want = %+v, want it held at what ace runs under rule 7", w) - } - // Issue 1966 note 170, 1966/AC170-4: ace's engine says its statement, and while the assigner's ask of it is held, - // restates it with the form and says that too; the ask held answers the first statement. The restatement's event, - // heard while that ask ran, is asked once more after it, said in the journal, so the state ends at the restatement - // and the held want moves. - release := make(chan struct{}) - statedMu.Lock() - hold, held = release, make(chan struct{}) - isHeld := held - statedMu.Unlock() - say(enginereads.ReadsStarted) select { - case <-isHeld: + case e := <-watch.Updates(): + var v engineWords + if err := json.Unmarshal(e.Value(), &v); err != nil || !slices.Equal(v.Words, []string{nodeengine.ManifestReadWordWantVersion}) || !v.At.Equal(at) { + t.Errorf("the state engine-reads holds %s for ace (%v)", e.Value(), err) + } case <-time.After(15 * time.Second): - t.Fatal("the assigner did not ask ace's engine on its manifest-reads-changed") + t.Fatal("the assigner did not write ace's words into its state engine-reads") } - from := b.journal.mark() - statedMu.Lock() - stated = append(slices.Clone(stated), "form:new") - statedMu.Unlock() - say(enginereads.ReadsStarted) - b.journal.await(t, from, "manifest-reads on ace: an ask runs, so it is asked once more after it", "the restatement heard while ace was asked", 15*time.Second) - close(release) - if s := statement("ace", func(s enginereads.Statement) bool { return slices.Contains(s.ManifestReads, "form:new") }); s.EventAt.IsZero() { - t.Errorf("ace's statement does not show when its event was heard: %+v", s) - } - b.wantNow(t, "ace", "forms", "the want moved once ace's engine said it reads the form", func(w mwc.Want) bool { return w.Fp == formsNew }) - // assigner_engine_reads asks ace's engine again through the state: the assigner answers there with what it states now. - again, err := enginereads.AskAgainOf(bedState{b, engineReadsBucket()}, "ace", "n1.ada", "a person asks", time.Now(), 15*time.Second) - if err != nil || again.From != enginereads.FromVerb || !slices.Contains(again.ManifestReads, "form:new") || again.AskAgain != nil { - t.Errorf("the ask again of ace answered %+v, %v", again, err) + if _, err := b.buckets[engineReadsBucket()].Get(b.ctx, "shanks"); err == nil { + t.Error("the state engine-reads holds a key of shanks, whose engine said nothing") } } @@ -891,59 +886,6 @@ func subscribed(t *testing.T, nc *nats.Conn) { } } -// bedState is one of the bed's buckets as the engine reads' tool reads it. -type bedState struct { - b *bed - bucket string -} - -func (s bedState) Keys() ([]string, error) { return s.b.buckets[s.bucket].Keys(s.b.ctx) } -func (s bedState) Get(key string) ([]byte, uint64, bool, error) { - e, err := s.b.buckets[s.bucket].Get(s.b.ctx, key) - if err != nil { - return nil, 0, false, nil - } - return e.Value(), e.Revision(), true, nil -} -func (s bedState) Put(key string, value []byte) (uint64, error) { - return s.b.buckets[s.bucket].Put(s.b.ctx, key, value) -} -func (s bedState) Delete(key string) error { return s.b.buckets[s.bucket].Delete(s.b.ctx, key) } -func (s bedState) Await(key string, after uint64, done func([]byte) bool, bound time.Duration) ([]byte, error) { - for deadline := time.Now().Add(bound); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - if v, rev, found, _ := s.Get(key); found && rev > after && done(v) { - return v, nil - } - } - return nil, fmt.Errorf("no answer at %s within %s", key, bound) -} - -// 1297/B9 (the assigner's half, ADR 0528 clause 6): started whole, the assigner holds a fingerprint whose health check -// failed on a node and writes the ask about it in its own asks, which the view and the teller read; its asks bucket is -// the one its manifest declares (ownBuckets, held equal to the manifest by TestOpensWhatItsManifestNames). -func TestAHeldFingerprintIsAskedAbout(t *testing.T) { - b := startBed(t, "") - fNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64) - digest := b.manifest(t, "web", `{"module":"web","version":"1"}`) - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fNew, Manifest: digest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - b.putModule(t, "web", mod) - failed, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"}) - b.putJSON(t, buckets.Have, "ace.web", failed) - b.place(t, "ace", "web") - b.start(t) - hkey, _ := mwc.HeldKey("web", fNew) - // 1906/H3: the held record names the machine and keeps the health, when and the node's words. - if h, err := mwc.DecodeHeld(b.get(t, buckets.Wants(), hkey)); err != nil || h.Node != "ace" || - !strings.HasPrefix(h.Words, "health failed, phase running, seen at ") || !strings.HasSuffix(h.Words, ": connection refused") { - t.Fatalf("held = %+v, %v", h, err) - } - akey, _ := asks.HoldAskKey("web", fNew) - a, err := decodeAsk(akey, b.get(t, buckets.Asks(), akey)) - if err != nil || a.Asking <= 0 || a.Ask.ID != meshdeliveryclient.AskingID(akey, a.Asking) || len(a.Ask.Options) != 2 { - t.Fatalf("the ask = %+v, %v", a, err) - } -} - // conditionsSaid reads what the assigner said of its conditions, by finding key: each a finding in its own bucket, which // the condition keeper carries (novox/hq ADR 0590 §3, issue 1126), raised while it stands and cleared once deleted, each // with the words its last finding said and the subject the finding names (`[.]`). @@ -974,54 +916,16 @@ func (b *bed) conditionsSaid(t *testing.T) map[string]oc.Condition { return out } -// 1906/D3 (ADR 0539 §19), whole on the bus: a module holding data whose new fingerprint failed its health check on the -// canary is held, not set back, and asked about (restore it, or leave it stopped); the condition names the module, the node -// and the check's words. -func TestAFailedDataModuleIsAskedToRestore(t *testing.T) { - b := startBed(t, "") - fNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("d", 64) - digest := b.manifest(t, "mail", `{"module":"mail","version":"1","resources":[{"id":"data","type":"directory","path":"/var/lib/mail"}],"data":{"own":[{"id":"mail","path":"${dir:data}","class":"irreplaceable","backup":"copy"}]}}`) - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fNew, Manifest: digest, Repository: "novox/mail", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - b.putModule(t, "mail", mod) - failed, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"}) - b.putJSON(t, buckets.Have, "ace.mail", failed) - wantKey, _ := mwc.WantKey("ace", "mail") - want, _ := mwc.EncodeWant(mwc.Want{Fp: fNew, Composed: "sha256:" + strings.Repeat("c", 64)}) - b.putJSON(t, buckets.Wants(), wantKey, want) - b.place(t, "ace", "mail") - b.start(t) - - akey, _ := asks.RestoreAskKey("ace", "mail", fNew) - a, err := decodeAsk(akey, b.get(t, buckets.Asks(), akey)) - if err != nil || len(a.Ask.Options) != 2 || a.Ask.Options[0].ID != "restore" || a.Ask.Options[1].ID != "leave-stopped" { - t.Fatalf("the restore ask = %+v, %v", a, err) - } - if w, err := mwc.DecodeWant(b.get(t, buckets.Wants(), wantKey)); err != nil || w.Fp != fNew { - t.Errorf("the data module was set back: want = %+v, %v", w, err) - } - key := "" - for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline) && key == ""; time.Sleep(100 * time.Millisecond) { - for k, c := range b.conditionsSaid(t) { - if c.State == occ.StateRaised && strings.HasPrefix(c.Subject.ID, "mail.") && strings.HasSuffix(k, ".held") { - if c.Subject.Machine != "ace" || !strings.Contains(c.Explanation, "connection refused") { - t.Fatalf("the condition = %+v, want it to name ace and the check's words", c) - } - key = k - } - } - } - if key == "" { - t.Fatal("no condition for the held fingerprint was said") - } -} - // 1906/D8 (scenario D of acceptance test 18, ADR 0539 §19), whole on the bus: the assigner is stopped while it writes the wants // of forty modules and another is started; every module's want is written, and each exactly once, by revision: nothing lost -// and nothing written again. +// and nothing written again. The wants follow one passed pipeline of the forty, all in tier 1 (design 47 H §750, §751). func TestTheAssignerReplacedResumesWithNothingLostOrRepeated(t *testing.T) { b := startBed(t, "") const modules = 40 names := make([]string, modules) + var movers []moverAt + b.compose(t, "ace", 'c') + b.readsVersion(t, "ace") for i := range names { name := fmt.Sprintf("mod%02d", i) names[i] = name @@ -1029,18 +933,10 @@ func TestTheAssignerReplacedResumesWithNothingLostOrRepeated(t *testing.T) { digest := b.manifest(t, name, fmt.Sprintf(`{"module":%q,"version":"1"}`, name)) mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: digest, Repository: "novox/" + name, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) b.putModule(t, name, mod) - built, err := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, By: "builder.novox.1", Step: "pushed", Manifest: digest}) - if err != nil { - t.Fatal(err) - } - bkey, _ := builds.BuilderBuildKey(name, fp) - b.putBuild(t, bkey, built) - gkey, _ := grantsstate.PartGrantKey(name, fp) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - b.putJSON(t, buckets.Grants, gkey, grant) - b.compose(t, "ace", 'c') b.place(t, "ace", name) + movers = append(movers, moverAt{name, fp, "1.0.0", 1}) } + b.ended(t, 3, mpr.PipelineEndedOutcomePassed, movers...) count := func() int { keys, err := b.buckets[buckets.Wants()].Keys(b.ctx) if err != nil { @@ -1138,43 +1034,6 @@ func (b *bed) hasKey(bucket, key string) bool { return err == nil } -// 1906/D4 (ADR 0539 §19), whole on the bus: the operator's release of a held fingerprint writes its want again, by the rules, -// and clears the condition. -func TestAReleaseAnswerWritesTheWantAgain(t *testing.T) { - b := startBed(t, "") - fNew, fOld := meshcatalogclient.SourcePrefix2+strings.Repeat("b", 64), meshcatalogclient.SourcePrefix2+strings.Repeat("a", 64) - b.fixture(t, "web", fNew, `{"module":"web","version":"1"}`) - b.compose(t, "ace", 'b') - b.compose(t, "ace", 'a') - healthy, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.web", healthy) // it ran the older fingerprint healthy before - failed, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"}) - b.putJSON(t, buckets.Have, "ace.web", failed) - b.putWant(t, "ace", "web", mwc.Want{Fp: fNew, Composed: "sha256:" + strings.Repeat("b", 64)}) - b.start(t) - w := b.wantNow(t, "ace", "web", "set back to the older fingerprint", func(w mwc.Want) bool { return w.Fp == fOld }) - if w.Newest != fNew { - t.Fatalf("the set-back want = %+v", w) - } - hold, _ := asks.HoldAskKey("web", fNew) - b.get(t, buckets.Asks(), hold) - b.answer(t, hold, "release") - w = b.wantNow(t, "ace", "web", "the held fingerprint's want written again", func(w mwc.Want) bool { return w.Fp == fNew }) - if w.Composed != "" || !w.Self || w.Rule != 0 { - t.Errorf("the want = %+v, want F by the rules, the node composing it", w) - } - for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) { - cleared := false - for k, c := range b.conditionsSaid(t) { - cleared = cleared || (strings.HasPrefix(k, "module.web.") && c.State == occ.StateCleared && strings.HasSuffix(k, ".held")) - } - if cleared { - return - } - } - t.Error("the condition of the held fingerprint was not cleared") -} - // askNow waits for the backup ask at key to be the one numbered asking and answers it. func (b *bed) askNow(t *testing.T, key string, asking int) decodedAsk { t.Helper() @@ -1198,6 +1057,9 @@ func TestABackupAnswerIsWrittenIntoTheNodesWant(t *testing.T) { b.fixture(t, "web", fNew, `{"module":"web","version":"1"}`) fRun := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) // a held have names the build it keeps running b.compose(t, "ace", 'b') + b.readsVersion(t, "ace") + // The want follows the passed version (design 47 H §750). + b.ended(t, 3, mpr.PipelineEndedOutcomePassed, moverAt{"web", fNew, "1.0.0", 1}) b.putWant(t, "ace", "web", mwc.Want{Fp: fNew, Self: true}) // a placed node's want names the build alone held, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fRun, Phase: nodeengineclient.PhaseHeld, Health: nodeengineclient.HealthWaits, Detail: "no space left"}) b.putJSON(t, buckets.Have, "ace.web", held) @@ -1215,7 +1077,7 @@ func TestABackupAnswerIsWrittenIntoTheNodesWant(t *testing.T) { } b.answerTo(t, akey, mwc.BackupAgain, a.Ask.ID) w1 := b.wantNow(t, "ace", "web", "the answer in the want", func(w mwc.Want) bool { return w.Backup == mwc.BackupAgain }) - if w1.Fp != fNew || w1.Composed != "" || !w1.Self { + if w1.Fp != fNew || !w1.Self { t.Errorf("the want = %+v", w1) } // the node retries: its have is backing up, not held. The ask stands and its condition is not cleared (A-453a). @@ -1242,10 +1104,11 @@ func TestABackupAnswerIsWrittenIntoTheNodesWant(t *testing.T) { if w2.Words == w1.Words { t.Errorf("the second answer left the want as it was: %+v", w2) } - // Issue 1966 note 165 (AC165-1): the move is superseded, the trunk and then the want naming another build; the pass takes + // Issue 1966 note 165 (AC165-1): the move is superseded, a newer passed version and then the want naming another build; the pass takes // the backup ask away and clears the condition of the failed backup. fOther := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64) - b.fixture(t, "web", fOther, `{"module":"web","version":"2"}`) // the trunk moves on: the want follows + b.fixture(t, "web", fOther, `{"module":"web","version":"2"}`) + b.ended(t, 4, mpr.PipelineEndedOutcomePassed, moverAt{"web", fOther, "2.0.0", 1}) // a newer version passed: the want follows for deadline := time.Now().Add(15 * time.Second); ; time.Sleep(50 * time.Millisecond) { cleared := false for k, c := range b.conditionsSaid(t) { @@ -1262,112 +1125,6 @@ func TestABackupAnswerIsWrittenIntoTheNodesWant(t *testing.T) { const recorded = `{"module":"web","version":"1","upgrade":{"policy":"record","why":"a person takes each build"}}` -// 1906/H2, whole on the bus: given a fingerprint held after one failed check, when every node that runs the module then runs -// it healthy, the assigner lifts the hold by itself: the held record and its ask are taken away, the held condition is -// cleared saying why, and the want stays at the fingerprint. -func TestAHoldIsLiftedOnceEveryNodeRunsItHealthy(t *testing.T) { - b := startBed(t, "") - fNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64) - b.fixture(t, "web", fNew, `{"module":"web","version":"1"}`) - b.compose(t, "ace", 'b') - failed, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"}) - b.putJSON(t, buckets.Have, "ace.web", failed) - b.putWant(t, "ace", "web", mwc.Want{Fp: fNew, Composed: "sha256:" + strings.Repeat("b", 64)}) - b.start(t) - hkey, _ := mwc.HeldKey("web", fNew) - akey, _ := asks.HoldAskKey("web", fNew) - b.get(t, buckets.Wants(), hkey) - b.get(t, buckets.Asks(), akey) - healthy, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.web", healthy) - for deadline := time.Now().Add(15 * time.Second); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) { - if b.hasKey(buckets.Wants(), hkey) || b.hasKey(buckets.Asks(), akey) { - continue - } - for k, c := range b.conditionsSaid(t) { - if strings.HasPrefix(k, "module.web.") && c.State == occ.StateCleared && strings.HasSuffix(k, ".held") { - if w := b.wantNow(t, "ace", "web", "the want at the lifted fingerprint", func(w mwc.Want) bool { return w.Fp == fNew }); w.Rule != 0 { - t.Errorf("the want = %+v", w) - } - return - } - } - } - t.Fatalf("the hold was not lifted: held %v, ask %v", b.hasKey(buckets.Wants(), hkey), b.hasKey(buckets.Asks(), akey)) -} - -// 1906/H2, whole on the bus: a hold lifted by itself and written again after the assigner was started again is asked under a -// new id, so the teller raises it; the answer to the first hold does not carry over (1906/H4). -func TestAHoldWrittenAgainAfterALiftAndARestartIsAskedAgain(t *testing.T) { - b := startBed(t, "") - fNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64) - b.fixture(t, "web", fNew, `{"module":"web","version":"1"}`) - b.compose(t, "ace", 'b') - failed, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"}) - healthy, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - b.putJSON(t, buckets.Have, "ace.web", failed) - b.putWant(t, "ace", "web", mwc.Want{Fp: fNew, Composed: "sha256:" + strings.Repeat("b", 64)}) - _, stop := b.startStoppable(t) - hkey, _ := mwc.HeldKey("web", fNew) - akey, _ := asks.HoldAskKey("web", fNew) - first, err := decodeAsk(akey, b.get(t, buckets.Asks(), akey)) - if err != nil { - t.Fatal(err) - } - b.putJSON(t, buckets.Have, "ace.web", healthy) - for deadline := time.Now().Add(15 * time.Second); b.hasKey(buckets.Wants(), hkey); time.Sleep(100 * time.Millisecond) { - if time.Now().After(deadline) { - t.Fatal("the hold was not lifted") - } - } - stop() - time.Sleep(1100 * time.Millisecond) // the next hold is written in another second - b.answerTo(t, akey, "keep", first.Ask.ID) // an answer to the first hold, which the later hold must not take - b.putJSON(t, buckets.Have, "ace.web", failed) - _, stop = b.startStoppable(t) - again, err := decodeAsk(akey, b.get(t, buckets.Asks(), akey)) - if err != nil { - t.Fatal(err) - } - if again.Ask.ID == first.Ask.ID { - t.Fatalf("the hold written again is asked under the first id %s", first.Ask.ID) - } - // Issue 1966 note 155, ADR 0668 §4: that asking lapses at the operator channel while no assigner runs, so the teller's - // ask-ended is said before the next one's consumer binds; at its bind the next reads the operator-channel seat's asks and - // writes the hold again under a new asking. - stop() - nc, err := nats.Connect(b.srv.ClientURL()) - if err != nil { - t.Fatal(err) - } - defer nc.Close() - for _, subject := range []string{"mesh.seat.operator-channel.tool.asks", "mesh.seat.operator-channel.call.asks.*"} { - if _, err := nc.Subscribe(subject, func(m *nats.Msg) { - answer, _ := json.Marshal(map[string]any{"asks": []map[string]string{{"key": "mesh-delivery-teller." + again.Ask.ID, "state": "expired"}}, "matched": 1}) - reply, _ := json.Marshal(map[string]any{"result": json.RawMessage(answer)}) - _ = m.Respond(reply) - }); err != nil { - t.Fatal(err) - } - } - subscribed(t, nc) - b.start(t) - for deadline := time.Now().Add(15 * time.Second); ; time.Sleep(50 * time.Millisecond) { - if third, err := decodeAsk(akey, b.get(t, buckets.Asks(), akey)); err == nil && third.Ask.ID != again.Ask.ID { - break - } - if time.Now().After(deadline) { - t.Fatalf("the hold whose asking %s lapsed before the assigner's bind was not asked again", again.Ask.ID) - } - } - b.putJSON(t, buckets.Have, "ace.web", healthy) - for deadline := time.Now().Add(15 * time.Second); b.hasKey(buckets.Wants(), hkey); time.Sleep(100 * time.Millisecond) { - if time.Now().After(deadline) { - t.Fatal("the stale \"keep\" of the first hold kept the second one held") - } - } -} - // bedStore is the runtime's states over the bed's buckets, as the seat's verbs read and write them: the assigner's own by // their local names, another module's `.`. type bedStore struct{ b *bed } @@ -1606,7 +1363,7 @@ func TestAProposalIsAnsweredApprovedAndWritten(t *testing.T) { } silent := q.(proposed).Proposal decided := proposalNow(silent.ID, "decided without its silent nodes", func(p mma.Proposal) bool { return p.State == mmac.ProposalAsked }) - if decided.Verdict == nil || decided.Verdict.State != mdcc.VerdictPass || + if decided.Verdict == nil || decided.Verdict.State != plc.VerdictPass || !strings.Contains(strings.Join(decided.Verdict.Notes, "; "), "no answer from") || !strings.Contains(strings.Join(decided.Verdict.Notes, "; "), "laptop (offline)") { t.Errorf("the silent node's verdict = %+v", decided.Verdict) } @@ -1821,8 +1578,9 @@ func serveOwner(t *testing.T, b *bed, nc *nats.Conn, owner string, tools map[str // firstViewsBeforeAnyNewFormState serves the assigner's first-view tools as its runtime of its own does (the tools bundle's // viewTools over the bed's buckets, the env its manifest's bundle writes, the caller the runtime proves from the subject's -// last token), before any new-form state exists, and asks each from every reader granted and from the others. -func firstViewsBeforeAnyNewFormState(t *testing.T, b *bed, granted map[string][]string) { +// last token), before any new-form state exists, and asks each from every reader: every account is answered (design 47 H §752, +// H-A-41: no seat or tool names its callers). +func firstViewsBeforeAnyNewFormState(t *testing.T, b *bed, tools []string) { t.Helper() raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { @@ -1925,20 +1683,17 @@ func firstViewsBeforeAnyNewFormState(t *testing.T, b *bed, granted map[string][] return msg.Data, nil }, tool)(b.ctx, "") } - readers := []string{"n1.mesh-grants", "n1.node-tools", "n1.mesh-delivery-indexer", "n1.mesh-delivery-assigner", "n1.mesh-delivery-builder", "n1.mesh-delivery-checker", "n1.mesh-delivery-teller"} - for tool, allowed := range granted { + readers := []string{"n1.mesh-grants", "n1.node-tools", "n1.mesh-delivery-indexer", "n1.mesh-delivery-assigner", "n1.mesh-delivery-builder", "n1.mesh-delivery-teller"} + for _, tool := range tools { for _, user := range readers { a, err := ask(user, tool) - switch { - case slices.Contains(allowed, user) && err != nil: + if err != nil { t.Errorf("%s's first view of %s before any new-form state: %v", user, tool, err) - case slices.Contains(allowed, user): - keys, _ := b.buckets[viewedStates()[tool]].Keys(b.ctx) - if a.Owner != assignment.Module() || a.State != tool || len(a.Values) != len(keys) { - t.Errorf("%s's first view of %s = %+v, the state holds %v", user, tool, a, keys) - } - case err == nil || !strings.Contains(err.Error(), "refused"): - t.Errorf("%s, granted no first view of %s, was answered %+v, %v", user, tool, a, err) + continue + } + keys, _ := b.buckets[viewedStates()[tool]].Keys(b.ctx) + if a.Owner != assignment.Module() || a.State != tool || len(a.Values) != len(keys) { + t.Errorf("%s's first view of %s = %+v, the state holds %v", user, tool, a, keys) } } } @@ -2012,6 +1767,15 @@ func serveCatalog(t *testing.T, b *bed, nc *nats.Conn) { _ = m.Respond([]byte(`{"result":{"none-yet":true},"node":"n0"}`)) return } + if kept, ok := b.cat.at[branch][in.Revision]; ok { + rev := meshcatalog.CatalogRevision{Kind: "trunk", Branch: in.Branch, Revision: in.Revision} + for _, e := range kept { + rev.Entries = append(rev.Entries, meshcatalog.EntryRef{Module: e.Module, Digest: e.ManifestDigest}) + } + raw, _ := json.Marshal(map[string]any{"result": meshcatalog.RevisionAnswer{Revision: &rev, Entries: kept}, "node": "n0"}) + _ = m.Respond(raw) + return + } rev := meshcatalog.CatalogRevision{Kind: "trunk", Branch: in.Branch, Revision: b.cat.rev[branch]} var entries []meshcatalog.CatalogEntry for _, name := range slices.Sorted(maps.Keys(b.cat.entries[branch])) { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index 55ec36cb..745d4e0a 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -16,7 +16,6 @@ import ( "git.novox.be/novox/mesh-sdk/go/buscalls" "io" "log" - "maps" "mesh-delivery-assigner/internal/assigngraph" "mesh-delivery-assigner/internal/assignment" "mesh-delivery-assigner/internal/buckets" @@ -41,6 +40,7 @@ import ( "git.novox.be/novox/mesh-sdk/go/seatlist" "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient/enginereads" + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" "git.novox.be/novox/mesh-sdk/go/views" "mesh-delivery-assigner/internal/assign" @@ -77,7 +77,6 @@ func main() { var readBuckets = []string{ buckets.Grants, buckets.Have, - buckets.Provisions, } // readFamilies are the states kept one bucket per node the assigner reads, each named in its manifest's reads: every @@ -90,11 +89,11 @@ func ownBuckets() []string { return []string{buckets.Asks(), buckets.Differences(), buckets.Puts(), buckets.Wants(), plc.AssignmentsBucket(), plc.ProposalsBucket(), plc.CandidatesBucket(), plc.PulsesBucket(), assign.FindingsBucket(), assign.PoliciesBucket(), plc.SeatHoldersBucket(), - engineReadsBucket(), retiredBucket(), copiesBucket(), writtenBucket(), buckets.Waits(), buckets.PutWaits(), buckets.DeliveryWaits(), buckets.DeliveredAgain(), buckets.Inputs(), buckets.CompositionRuns(), pinsBucket()} + engineReadsBucket(), retiredBucket(), copiesBucket(), writtenBucket(), buckets.Waits(), buckets.PutWaits(), buckets.Inputs(), buckets.CompositionRuns()} } -// engineReadsState is the assigner's state of what each node's engine states it reads (ADR 0618 §3), one key ``, as -// the verb manifest-reads last answered it or the fallback read it: what assigner_engine_reads lists and asks again. +// engineReadsState is the assigner's state of what each node's engine says it reads (design 47 H-A-104), one key `` +// whose value is the words and the time of the last manifest-reads-changed heard of it: what assigner_engine_reads lists. const engineReadsState = "engine-reads" // engineReadsBucket is that state's bucket. @@ -135,11 +134,8 @@ var keptStates = []announce.State{ {Name: writtenState, Readable: true}, {Name: buckets.WaitsState, Readable: true}, {Name: buckets.PutWaitsState, Readable: true}, // the put waits said (putwaits.go, design 47 G A-211) - {Name: buckets.DeliveryWaitsState, Readable: true}, // the delivery waits said (delivered.go, design 47 G A-246) - {Name: buckets.DeliveredAgainState, Readable: true}, // the asks of assigner_delivered_again (delivered.go, design 47 G A-250) {Name: buckets.InputsState, Readable: true}, // read by the tool process alone: the verb input and assigner_inputs (design 47 G §354) {Name: buckets.CompositionRunsState, Readable: true}, // the composition runs, read by the process and its tools (design 47 G §719, A-468c) - {Name: pinsState, Readable: true}, // the records pinning each assignment graph revision (design 47 G §579) } // kvSet is every bucket the assigner opened, by name. @@ -168,12 +164,6 @@ func open(ctx context.Context, js process.BucketOpener, logf func(string, ...any logf("the bucket %s cannot be opened (%v): it is bound once the bus says it was made", name, err) continue } - if name == buckets.Provisions && err != nil { - // The seat mesh-provisioner's state, with its own delivery (ADR 0570 §4): bound once the bus says it was made; - // until then no want waits for a provision, as before. - logf("the bucket %s cannot be opened (%v): it is bound once the bus says it was made", name, err) - continue - } if name == assign.PoliciesBucket() && err != nil { // The seat mesh-wants' state, new with it (ADR 0565 §7): bound once the bus says it was made; until then no // person's policy is read, so the controller's beside each composition holds, as before. @@ -187,8 +177,8 @@ func open(ctx context.Context, js process.BucketOpener, logf func(string, ...any continue } if name == engineReadsBucket() && err != nil { - // New with ADR 0618 §3: without it the statements the verb answers are kept in memory only, and - // assigner_engine_reads lists nothing. + // Without it the words each manifest-reads-changed says are kept in memory only, and assigner_engine_reads + // lists nothing. logf("the bucket %s cannot be opened (%v): what each node's engine reads is kept in memory only until it starts again", name, err) continue } @@ -197,27 +187,12 @@ func open(ctx context.Context, js process.BucketOpener, logf func(string, ...any logf("the bucket %s cannot be opened (%v): it is read by the tool process alone", name, err) continue } - if name == pinsBucket() && err != nil { - // New with the pins (design 47 G §579): without it no pin is kept and no revision is removed. - logf("the bucket %s cannot be opened (%v): no pin is kept and no revision removed until it starts again", name, err) - continue - } if name == buckets.Waits() && err != nil { // New with the wait on an outside cause (design 47 G §706): without it no wait is made, and a failing check is judged // as before. logf("the bucket %s cannot be opened (%v): no outside wait is kept until the assigner starts again", name, err) continue } - if name == buckets.DeliveredAgain() && err != nil { - // New with A-250: without it assigner_delivered_again is not taken until the assigner starts again. - logf("the bucket %s cannot be opened (%v): no version is said delivered again until the assigner starts again", name, err) - continue - } - if name == buckets.DeliveryWaits() && err != nil { - // New with A-246: without it no dependent waits for its dependency's delivery until the assigner starts again. - logf("the bucket %s cannot be opened (%v): no delivery wait is kept until the assigner starts again", name, err) - continue - } if name == buckets.PutWaits() && err != nil { // New with A-211: without it no put's wait is said until the assigner starts again. logf("the bucket %s cannot be opened (%v): no put's wait is said until the assigner starts again", name, err) @@ -246,8 +221,8 @@ func open(ctx context.Context, js process.BucketOpener, logf func(string, ...any // states and those new with a delivery of their own, bound once made, and the two that choose the grants' source. func startsWithout(name string) bool { switch name { - case plc.SeatHoldersBucket(), buckets.Provisions, assign.PoliciesBucket(), assign.FindingsBucket(), - buckets.Puts(), engineReadsBucket(), retiredBucket(), writtenBucket(), buckets.Waits(), buckets.PutWaits(), buckets.DeliveryWaits(), buckets.DeliveredAgain(), buckets.Inputs(), pinsBucket(): + case plc.SeatHoldersBucket(), assign.PoliciesBucket(), assign.FindingsBucket(), + buckets.Puts(), engineReadsBucket(), retiredBucket(), writtenBucket(), buckets.Waits(), buckets.PutWaits(), buckets.Inputs(): return true } return slices.Contains(seatBuckets(), name) || name == buckets.Grants @@ -303,9 +278,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // The inputs of the composition steps it asks a build seat to run: the seat mesh-wants' verb input is answered here in chunks // of the core connection, of any size (design 47 G §354, §712, A-237). inputs := inputsOver(kvs[buckets.Inputs()], js, account.Module, logger.Printf) - // The seat list (seatlist.go), followed in follow over the connection and the hub: the verb input's callers are checked - // against the seat's definition it holds (A-186). - gate := seatlist.NewPart("assigner", seatListAgainCallers) + // The seat list (seatlist.go), followed in follow over the connection and the hub. + gate := seatlist.NewPart("assigner") if inputs != nil { inputs.dropAtStart() // the objects of keys another part deleted (A-465) stopInput, err := said.AnswerSeat(meshwants.Seat, seatlist.Definition(gate.Reader, meshwants.Seat), map[string]process.SeatHandler{meshwants.VerbInput: inputs.verb(conn, buscalls.SeatAckWithin)}) @@ -388,14 +362,11 @@ func watched() []string { buckets.Differences(), buckets.Wants(), assign.PoliciesBucket(), - buckets.Provisions, plc.SeatHoldersBucket(), engineReadsBucket(), writtenBucket(), buckets.Waits(), buckets.PutWaits(), - buckets.DeliveryWaits(), - buckets.DeliveredAgain(), // its watch wakes a pass at once on an ask of assigner_delivered_again (A-250) } } @@ -403,8 +374,6 @@ func watched() []string { // each change; changes that come during a pass make one more. It returns when ctx ends or a watch ends. func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, kvs kvSet, say func(oc.Condition) error, findings assign.Findings, bus caller, iv *catalogViews, inputs *inputKeeper, gate *seatlist.Part, logger *log.Logger, getenv func(string) string) error { w := newWorld() - // The callers' seat entries are resolved from the assigner's own seat-holders as it last read them (A-298). - gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return w.read(plc.SeatHoldersBucket()), nil })) dirty := make(chan struct{}, 1) wake := func() { select { @@ -414,16 +383,11 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, } errs := make(chan error, len(watched())+2) settled := make(chan struct{}, len(watched())+2) - // What each node's engine reads (ADR 0618 §3, §4): asked through the seat's verb, kept in the state engine-reads, read - // from the node's record where the engine does not answer (the fallback, said in the journal). - reads := &enginereads.Keeper{Fallback: w.recordReads, Changed: wake, Logf: logger.Printf} - if bus != nil { - reads.Call = bus.CallKeptOn // a kept call, called again through a transport fault (ADR 0668 §1) - reads.Once = bus.CallOn // its fault keeps the fallback at once while the kept call asks on (ADR 0668 §3) - } + // What each node's engine reads (design 47 H-A-104): the words of its manifest-reads-changed, kept in the state + // engine-reads and read back from its watch, so a restart loses none; no verb is asked. + reads := &engineReadsKept{changed: wake} if kv := kvs[engineReadsBucket()]; kv != nil { - reads.Put = func(node string, v []byte) error { _, err := kv.Put(ctx, node, v); return err } - reads.Delete = func(node string) error { return kv.Delete(ctx, node) } + reads.put = func(node string, v []byte) error { _, err := kv.Put(ctx, node, v); return err } } watch := func(name string, opts ...jetstream.WatchOpt) error { kw, err := kvs[name].WatchAll(ctx, opts...) @@ -459,7 +423,9 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, value = e.Value() } if name == engineReadsBucket() { - reads.Heard(e.Key(), value, value == nil) // it wakes a pass when a statement changed + if err := reads.take(e.Key(), value, value == nil); err != nil { // it wakes a pass when the words changed + logger.Printf("%v", err) + } continue } w.putEntry(name, e.Key(), value, e.Revision(), e.Created()) @@ -586,7 +552,7 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, } } // And the two that choose where grants are read from, with the same delivery as the grants part that writes one. - for _, name := range []string{buckets.Grants, assign.PoliciesBucket(), buckets.Provisions, plc.SeatHoldersBucket()} { + for _, name := range []string{buckets.Grants, assign.PoliciesBucket(), plc.SeatHoldersBucket()} { if kvs[name] == nil && js != nil { name := name process.AwaitBucket(ctx, js, nc, name, "until then the bucket is not read", @@ -617,6 +583,12 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, p := &passer{kvs: kvs, w: w, logf: logger.Printf, say: say, findings: findings, said: map[string]bool{}, answeredAt: map[string]uint64{}, reads: reads, seats: gate, askForFile: getenv("MESH_ASSIGNER_ASK_OPERATOR_FOR_FILE")} p.askOperatorFor() // the setting ask-operator-for said once at the start (design 47 G A-425a) + // The setting tier-bound (design 47 H §751) and the one timer that wakes the pass at a tier's deadline (H-A-56). + tierBound, err := tierBoundSetting(getenv) + if err != nil { + return err + } + p.tierBound, p.tierWake = tierBound, wake // The wait on an outside cause (outside.go, design 47 G §706): the keeper's three events say when to judge again, and the // seat mesh-wants' wait-held and wait-ended are said; registered before the hub starts. if bus != nil && kvs[buckets.Waits()] != nil { @@ -636,7 +608,6 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, logger.Printf("%s is not heard, so a wait on an outside cause is judged again only when its check changes: %v", subject, err) } } - p.outside.readStart() } if js != nil { p.publishWants = wantsPublisher(ctx, js, thisNode) @@ -648,27 +619,20 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, } iv.candidates.Store(&keep) // A held revert's running entry and its want's, asked of the seat mesh-catalog's verb entry (design 47 G §710, A-217). - p.entries = meshcatalogclient.NewEntries(iv.h.call) - p.entries.Changed = func(meshcatalogclient.EntryRow) { wake() } - // The pins of the revisions (pins.go, design 47 G §579): the gate's pinned, freed, heads-replaced and pins-stated - // on the hub, registered before it starts. - if kv := kvs[pinsBucket()]; kv != nil { - k := &pinKeeper{ctx: ctx, st: kvEnds{ctx, kv}, objects: objects, inUse: inUseOf(w), findings: findings, now: time.Now, logf: logger.Printf} - if err := k.made(); err != nil { - logger.Printf("the state %s is not marked made, so no revision is removed until it is: %v", pinsState, err) - } - if err := hearPins(hub, k); err != nil { - logger.Printf("a pin is not heard, so no revision is removed until the assigner starts again: %v", err) - } else { - p.graph.pins = k - k.watchUnpins(kv) - } + entryAsks, err := settingFile(getenv("MESH_ASSIGNER_ENTRY_ASKS_FILE")) + if err != nil { + return err } + p.entries = entriesOf(iv.h.branches, iv.h.call, entryAsks, iv.writer, wake) + askAgainOnReconnect(ctx, nc, p.entries) + // The revisions that are not current are removed when one ceases to be current and once now (revisions.go, + // design 47 H §764, H-A-86). + p.graph.revisions = &revisionRemover{ctx: ctx, objects: objects, inUse: inUseOf(w), logf: logger.Printf} + p.graph.revisions.judge() } } - // The node-engine seat's manifest-reads-changed, for one node (ADR 0618 §4): that node is asked again, with no timer. - // Heard on the assigner's durable consumer on EVENTS, which the grants part makes from the manifest's consumes; where it - // is not there yet, a node is asked again only when it comes back online. + // The node-engine seat's manifest-reads-changed, for one node (design 47 H-A-104): its words are written into the state + // engine-reads. Heard on the assigner's durable consumer on EVENTS, which the grants part makes from the manifest's consumes. if js != nil { // The teller's ask-ended (issue 1966): an ask ended at the channel without an answer is written again; registered // before hearReadsChanged starts the hub. @@ -690,11 +654,8 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, logger.Printf("a pipeline's end or a registration is not heard, so the wants keep the trunk-only form until the assigner starts again: %v", err) } } - hearDelivery(ctx, js, w, wake, findings, logger.Printf) // version-released, version-delivered, machine-away and machine-back + hearReleases(ctx, js, w, wake, findings, logger.Printf) // version-released, machine-away and machine-back hearBuilds(ctx, js, &w.ends, wake, findings, logger.Printf) - if err := hearTaken(hub, &w.taken, wake, logger.Printf); err != nil { - logger.Printf("the indexer's take-ins are not heard, so an entry held by an order edge waits until the assigner starts again: %v", err) - } hearReadsChanged(ctx, js, hub, reads, findings, gate.EventsLive, logger.Printf) hearRunning(ctx, js, w, wake, findings, logger.Printf) } @@ -705,8 +666,8 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, if bus != nil && js != nil && inputs != nil && p.publishWants != nil && runs != nil { run := &compositionRun{runs: runs, inputs: &compositionInputs{machines: w.machinesOf, callFile: bus.CallFile}, keeper: inputs, reads: &compositionReads{}, call: bus.CallKept, trunk: trunkOf(iv, w.trunkName), running: w.runningOf, away: func(machine string) bool { return w.awayNow()[machine] }, put: runAskPublisher(js), ask: bus.CallFile, now: time.Now, logf: logger.Printf} - steps := &compositionSteps{node: thisNode, say: p.publishWants, run: run.run, resume: run.resume, ended: runs.stepEnded, now: time.Now, logf: logger.Printf, - asker: stepAskerOf(seatlist.Definition(gate.Reader, meshwants.Seat))} + steps := &compositionSteps{node: thisNode, say: p.publishWants, run: run.run, resume: run.resume, + validate: run.validate, resumeValidate: run.resumeValidate, ended: runs.stepEnded, now: time.Now, logf: logger.Printf} move := steps.move(ctx) // the readers a rerun carried (§727, A-470d) runs.moved.Store(&move) // The state's watch wakes the steps waiting on a run and hears a bound's expiry; the runs standing at the start are taken up @@ -804,45 +765,45 @@ func sayVia(ctx context.Context, js jetstream.JetStream) func(oc.Condition) erro } } -// readsChangedHandler is what the assigner does with each event manifest-reads-changed: acknowledges it, keeps what it says -// of the node (a statement older than it does not stand) and asks the node again. -func readsChangedHandler(reads *enginereads.Keeper, logf func(string, ...any)) func(jetstream.Msg) { +// readsChangedHandler is what the assigner does with each event manifest-reads-changed: writes the words it says of its node +// into the state engine-reads, then acknowledges it; an event not written is not acknowledged, so the bus delivers it again. +func readsChangedHandler(reads *engineReadsKept, logf func(string, ...any)) func(jetstream.Msg) { return func(m jetstream.Msg) { + node, ours, err := enginereads.NodeOf(m.Subject(), m.Data()) + if ours && err == nil { + var e nodeengine.ManifestReadsChangedEvent + if e, err = enginereads.DecodeChanged(m.Data()); err == nil { + if err = reads.heard(e); err != nil { + logf("%s of %s: %v", enginereads.Event, node, err) + if nerr := m.Nak(); nerr != nil { + logf("not acknowledging %s: %v", m.Subject(), nerr) + } + return + } + } + } + if ours && err != nil { + logf("%s holds what is not %s, and is not kept: %v", m.Subject(), enginereads.Event, err) + } if err := m.Ack(); err != nil { logf("acknowledging %s: %v", m.Subject(), err) } - node, ours, err := enginereads.NodeOf(m.Subject(), m.Data()) - switch { - case !ours: - case err != nil: - logf("%s holds what is not %s, and %s is asked again all the same: %v", m.Subject(), enginereads.Event, node, err) - reads.AskNode(node, enginereads.Event+" that does not read") - default: - // What the event says is kept first: a statement older than it does not stand while the node is asked again. - if e, derr := enginereads.DecodeChanged(m.Data()); derr == nil { - reads.HeardChanged(e) - } - reads.AskNode(node, enginereads.Event) - } } } // hearReadsChanged hears the node-engine seat's event manifest-reads-changed on the assigner's one hub of its durable -// consumer on EVENTS, and asks the node it names again; the hub also hands the indexer's views to the followers of +// consumer on EVENTS, and keeps the words of the node it names; the hub also hands the indexer's views to the followers of // indexerViews, and starts once both are registered. -func hearReadsChanged(ctx context.Context, js jetstream.JetStream, hub *process.EventHub, reads *enginereads.Keeper, findings assign.Findings, live func(), logf func(string, ...any)) { +func hearReadsChanged(ctx context.Context, js jetstream.JetStream, hub *process.EventHub, reads *engineReadsKept, findings assign.Findings, live func(), logf func(string, ...any)) { account := process.Account{Node: thisNode, Module: assignment.Module()} w := notHeardWriter(findings, logf) handle := readsChangedHandler(reads, logf) err := hub.Handle(enginereads.SubjectPrefix+"*", handle) - // At each bind every node the assigner holds a statement of is asked again: an event said before its consumer existed - // is not delivered to it (issue 1966 note 155: ace's bootstrapped statement stood; ADR 0668 §4). - hub.OnBound(reads.AtBind(func() []string { return slices.Sorted(maps.Keys(reads.Statements())) })) if err == nil { err = hub.Start(ctx, js, account) } if err != nil { - w.NotHeard(process.EventsStream, enginereads.Event+" is not heard until the assigner starts again (a node is asked again when it comes back online)", err) + w.NotHeard(process.EventsStream, enginereads.Event+" is not heard until the assigner starts again, so no node's new words are kept", err) return } w.Heard(process.EventsStream) @@ -857,3 +818,35 @@ func hearReadsChanged(ctx context.Context, js jetstream.JetStream, hub *process. hub.Stop() }() } + +// entriesOf are the catalog entries the passer asks by fingerprint: the followed branches' own, so each catalog-revision asks +// again an entry whose ask failed to reach the holder, at most asks times (the setting entry-asks, 0 its default), and the +// assigner's finding writer writes entry-not-found and entry-unanswered of them (design 47 H, H-A-60). Each change wakes a pass. +func entriesOf(b *meshcatalogclient.Branches, call meshcatalogclient.Call, asks int, w views.WireWriter, wake func()) *meshcatalogclient.Entries { + if b == nil || b.Entries == nil { + b = &meshcatalogclient.Branches{Entries: meshcatalogclient.NewEntries(call)} + } + b.Entries.Asks = asks + if w == nil { + b.Entries.Changed = func(meshcatalogclient.EntryRow) { wake() } + return b.Entries + } + b.WriteEntryFindings(w, func(meshcatalogclient.EntryRow) { wake() }) + return b.Entries +} + +// askAgainOnReconnect asks the entries again whose ask failed each time the assigner's link to the bus opens again (design 47 +// H, H-A-60), after whatever the connection already does then. +func askAgainOnReconnect(ctx context.Context, nc process.Subscriber, e *meshcatalogclient.Entries) { + conn, ok := nc.(*nats.Conn) + if !ok || conn == nil { + return + } + before := conn.Opts.ReconnectedCB + conn.SetReconnectHandler(func(c *nats.Conn) { + if before != nil { + before(c) + } + e.AskAgain(ctx) + }) +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main_test.go index 5cfe19f8..cd1af9b7 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main_test.go @@ -16,6 +16,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "slices" "strings" "testing" @@ -26,6 +27,7 @@ import ( mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/process" "git.novox.be/novox/mesh-sdk/go/seatlist" @@ -125,7 +127,7 @@ func TestOpensWhatItsManifestNames(t *testing.T) { } kvs[buckets.Puts()].(*watchKV).closed = true kvs[buckets.Wants()].(*watchKV).wrote = wrote - go follow(ctx, nil, nil, kvs, func(oc.Condition) error { return nil }, assign.Findings{}, nil, nil, nil, seatlist.NewPart("assigner", seatListAgainCallers), log.New(io.Discard, "", 0), func(string) string { return "" }) + go follow(ctx, nil, nil, kvs, func(oc.Condition) error { return nil }, assign.Findings{}, nil, nil, nil, seatlist.NewPart("assigner"), log.New(io.Discard, "", 0), func(string) string { return "" }) select { case key := <-wrote: if key != "want.ace.node" { @@ -209,3 +211,43 @@ func putInto(into map[string][]byte) func(mcseat.Finding) error { return err } } + +// fakeWire is a views part's finding writer that hands each written finding's kind on. +type fakeWire struct{ written chan string } + +func (f fakeWire) WriteWire(_ context.Context, finding map[string]any) error { + k, _ := finding["kind"].(string) + f.written <- k + return nil +} +func (fakeWire) Clear(context.Context, string, string, string, string) error { return nil } +func (fakeWire) ModuleNode() (string, string) { return "mesh-delivery-assigner", "n1" } + +// H-A-60: the assigner's entries are asked again to the setting entry-asks, then written through its finding writer as the +// finding entry-unanswered. +func TestAnUnreachedEntryEndsInTheAssignersFindingEntryUnanswered(t *testing.T) { + ctx := context.Background() + unreached := func(string, any) (json.RawMessage, error) { return nil, errors.New("no responders") } + w := fakeWire{written: make(chan string, 4)} + e := entriesOf(nil, unreached, 1, w, func() {}) + if e.Asks != 1 { + t.Fatalf("entry-asks %d, want 1 from the setting", e.Asks) + } + e.Need(ctx, "src2:u") + deadline := time.Now().Add(5 * time.Second) + for { + select { + case kind := <-w.written: + if kind != meshcatalogclient.EntryUnansweredKind { + t.Fatalf("the finding written is %q, want %s", kind, meshcatalogclient.EntryUnansweredKind) + } + return + default: + } + if time.Now().After(deadline) { + t.Fatal("no finding entry-unanswered is written past the bound") + } + e.AskAgain(ctx) + runtime.Gosched() + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits.go deleted file mode 100644 index dcdfdb3e..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits.go +++ /dev/null @@ -1,296 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "strconv" - "sync" - - "git.novox.be/novox/mesh-contracts/go/seats/meshbroker" - mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/process" - "git.novox.be/novox/mesh-sdk/go/seatdef" - - "mesh-delivery-assigner/internal/acting" - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/assigngraph" - "mesh-delivery-assigner/internal/assignment" - "mesh-delivery-assigner/internal/buckets" -) - -// The three order edges of a delivery that crosses a form change (design 47 G §298, A-245, A-258, A-259, A-260). Each is an acting wait of the -// assigner, kept in the state `waits` under `order...` beside the acting waits, in the step placing, said as wait-held -// when a want is held for it and wait-ended (ran, released, replaced) when it moves; assigner_waits reads them and assigner_release with -// how `now` releases them, as it does an acting wait. The holders of the seats are read from the seat holders the assignments compose -// (assignment.SeatHoldersOf), never named: -// - the node-backup holder's and the bus module's (the holder of the seat mesh-broker) new versions run before a module whose kept data's -// form changes is moved, since the restore point needs both (OrderBackup); -// - at a revert a node's node-engine goes back last on that node, after every module it restores there runs the reverted build, which it -// starts only once it has said `restored` (OrderEngineLast); -// - the reverted assigner and gate are wanted only once the indexer's take-in.taken of the trunk branch names a source revision at least -// the trunk's head, the highest `src` of the trunk branch's module values (OrderTakenIn, A-260). - -// takenSubject is the indexer's event take-in.taken on its own stream, as the manifest's consumes names it. -var takenSubject = process.ModuleStreamSubject(buckets.IndexerModule, "take-in.taken") - -// takenSources are the newest source revisions a take-in.taken named, by watched branch, as heard (hearBuildsAndReleases). -type takenSources struct { - mu sync.Mutex - own map[string]int64 -} - -// put takes one take-in.taken in. A hand drop, and a taken with no source revision (one said before G), take nothing in. -func (t *takenSources) put(data []byte) error { - var e meshcatalog.TakeInTaken - if err := json.Unmarshal(data, &e); err != nil { - return fmt.Errorf("a take-in.taken that is none: %w", err) - } - if e.Dropped || e.Source == 0 || e.Branch == "" { - return nil - } - t.mu.Lock() - defer t.mu.Unlock() - if t.own == nil { - t.own = map[string]int64{} - } - t.own[e.Branch] = max(t.own[e.Branch], e.Source) - return nil -} - -// of is the newest source revision of branch taken in; 0 before one is heard. -func (t *takenSources) of(branch string) int64 { - t.mu.Lock() - defer t.mu.Unlock() - return t.own[branch] -} - -// orderEdge is one order edge held on module's want on node: part's version fp must run on partNode (OrderBackup, OrderEngineLast), or the -// trunk's source revision source be taken in (OrderTakenIn). -type orderEdge struct { - node, module, part, partNode, fp, entry, order string - source int64 -} - -func (e orderEdge) key() string { return acting.OrderKey(e.node, e.module, e.part) } - -func (e orderEdge) acting() assign.ActingEdge { - return assign.ActingEdge{Node: e.partNode, Part: e.part, Fp: e.fp, Step: seatdef.StepPlace} -} - -// orderRuns says an order edge is met: its part runs the version, or the source revision is taken in. -func (p *passer) orderRuns(n assign.Nodes, e orderEdge) bool { - if e.order == acting.OrderTakenIn { - return p.w.taken.of(p.w.trunkName()) >= e.source - } - return n.Runs(e.acting()) -} - -// seatHolders are the holders of each seat as the assignments compose them, by seat name. -func (p *passer) holdersBySeat() map[string][]mma.SeatHoldersHoldersItem { - out := map[string][]mma.SeatHoldersHoldersItem{} - for _, sh := range assignment.SeatHoldersOf(p.w.assignments()) { - out[sh.Seat] = append(out[sh.Seat], sh.Holders...) - } - return out -} - -// orderEdgesOf computes §298's edges on the wants of module m on each of its nodes, into p.orders. wanted is m's wants by node; trunk the -// trunk's module values. A node that has no running build heard, runs m at its trunk version already, or is wanted that version already -// has none: nothing moves. It answers, by node, whether an edge still stands that cannot be recorded as a wait because the catalog entry -// it reads is not answered yet (asked, and its answer wakes a pass): the want is held meanwhile, never written across a form change unread. -func (p *passer) orderEdgesOf(ctx context.Context, m assign.Module, wanted map[string]mwc.Want, trunk map[string]meshcatalog.ModulesByModuleValue) map[string]bool { - unread := map[string]bool{} - p.orderVisited[m.Name] = true - if m.FP == "" || p.graph == nil || p.entries == nil { - return unread - } - var holders map[string][]mma.SeatHoldersHoldersItem - for _, node := range m.Nodes { - if w, ok := wanted[node]; ok && !w.Removed && w.Fp == m.FP { - continue // the version is wanted already: nothing moves, so nothing waits - } - run, heard := p.w.runningOf(node, m.Name) - if !heard || run.Stopped || run.Fingerprint == "" || run.Fingerprint == m.FP { - continue - } - if holders == nil { - holders = p.holdersBySeat() - } - held := func(seatName string) bool { - for _, h := range holders[seatName] { - if h.Module == m.Name && h.Node == node { - return true - } - } - return false - } - add := func(e orderEdge) { - e.node, e.module, e.entry = node, m.Name, m.FP - p.orders[e.key()] = e - } - // 1. a kept data's form changes: the restore point needs the new node-backup holder and bus module. - changes, known := p.formChange(ctx, m.Name, run.Fingerprint, m.FP) - if !known { - unread[node] = true - } - if len(changes) > 0 { - for seatName, onNode := range map[string]bool{catalogue.BackupSeat: true, meshbroker.Seat: false} { - for _, h := range holders[seatName] { - if h.Module == m.Name || (onNode && h.Node != node) || trunk[h.Module].Fp2 == "" { - continue - } - add(orderEdge{part: h.Module, partNode: h.Node, fp: trunk[h.Module].Fp2, order: acting.OrderBackup}) - } - } - } - if !held(nodeengine.Seat) && !held(mma.Seat) && !held(meshdelivery.Seat) { - continue - } - reverted, err := assigngraph.Reverted(ctx, p.graph.objects, node, m.Name, run.Fingerprint, m.FP) - if err != nil { - p.logf("whether the want of %s on %s at %s is a revert is not read: %v", m.Name, node, m.FP, err) - continue - } - if !reverted { - continue - } - // 2. at a revert the node-engine goes back last on its node: after every module that is set back there across a form change runs. - if held(nodeengine.Seat) { - for module, byNode := range p.w.wantsOf() { - // What it is set back to is its trunk version, which the rules write as its want in this pass. - target := trunk[module].Fp2 - if w, ok := byNode[node]; module == m.Name || !ok || w.Removed || target == "" { - continue - } - xrun, heard := p.w.runningOf(node, module) - if !heard || xrun.Stopped || xrun.Fingerprint == "" || xrun.Fingerprint == target { - continue - } - if back, err := assigngraph.Reverted(ctx, p.graph.objects, node, module, xrun.Fingerprint, target); err != nil || !back { - continue - } - xchanges, known := p.formChange(ctx, module, xrun.Fingerprint, target) - if !known { - unread[node] = true - } - if len(xchanges) > 0 { - add(orderEdge{part: module, partNode: node, fp: target, order: acting.OrderEngineLast}) - } - } - } - // 3. the reverted assigner and gate wait for the indexer to take in the trunk's head. - if held(mma.Seat) || held(meshdelivery.Seat) { - var head int64 - for _, v := range p.w.trunkOf(p.w.trunkName()) { - head = max(head, v.Src) - } - if head > 0 { - add(orderEdge{part: buckets.IndexerModule, fp: strconv.FormatInt(head, 10), source: head, order: acting.OrderTakenIn}) - } - } - } - return unread -} - -// orderActing is the order edges of module on node that still hold its want, as acting edges: those not met, and not released by hand. A -// node whose catalog entry is not read yet holds its want too, on a stand-in edge no part runs. -func (p *passer) orderActing(module string, released map[string]bool, unread map[string]bool, n func() assign.Nodes) func(node string) []assign.ActingEdge { - return func(node string) []assign.ActingEdge { - var out []assign.ActingEdge - nodes := n() - for _, e := range p.orders { - if e.module == module && e.node == node && !released[e.key()+"@"+e.fp] && !p.orderRuns(nodes, e) { - out = append(out, assign.ActingEdge{Node: e.partNode, Part: e.part, Fp: e.fp, Step: seatdef.StepPlace}) - } - } - if unread[node] { - out = append(out, assign.ActingEdge{Node: node, Part: "the catalog entry", Step: seatdef.StepPlace}) - } - return out - } -} - -// orderWaits keeps the order waits after the pass's wants, as actingWaits keeps the acting waits: it starts the wait of each edge not met, -// ends the wait of one that ran, was replaced or was released, and says each. -func (p *passer) orderWaits(ctx context.Context, n assign.Nodes, wanted map[string]map[string]mwc.Want) { - if p.kvs[buckets.Waits()] == nil { - return - } - now := p.clock() - kept := p.w.read(buckets.Waits()) - trunk := p.w.trunk() - if p.orderSaid == nil { - p.orderSaid = map[string]bool{} - } - standing := map[string]bool{} - for key, e := range p.orders { - if w, ok := wanted[e.module][e.node]; ok && !w.Removed && w.Fp == e.entry { - continue - } - if p.orderRuns(n, e) { - continue - } - standing[key] = true - if raw := kept[key]; raw != nil { - if w, err := acting.Decode(raw); err == nil && w.Entry == e.entry && w.Fp == e.fp { - if !w.Released && !p.orderSaid[key] { - // A wait still held after a restart is said again, as wait-held (A-260). - if err := p.wantsSay(seat.EventWaitHeld, acting.Body(w, assignment.Module()), key+"."+e.fp+"."+e.entry+".again"); err != nil { - p.logf("the wait of %s on %s for %s is not said again, and is said at the next pass: %v", e.module, e.node, e.part, err) - } else { - p.orderSaid[key] = true - } - } - p.actingReleaseSaid(ctx, key, w) - continue - } - } - if !n.Online[e.node] { - continue // a node away holds only itself, and its clock has not started (§268) - } - w := acting.Wait{Step: seatdef.StepPlace, Node: e.node, Module: e.module, Part: e.part, PartNode: e.partNode, Fp: e.fp, Entry: e.entry, Order: e.order, - Since: now, ClockFrom: now, Bound: acting.BoundOf(seat.Edge{Step: seatdef.StepPlace}).String()} - if err := p.wantsSay(seat.EventWaitHeld, acting.Body(w, assignment.Module()), key+"."+e.fp+"."+e.entry); err != nil { - p.logf("the wait of %s on %s for %s is not said, and is said at the next pass: %v", e.module, e.node, e.part, err) - continue - } - p.put(ctx, buckets.Waits(), key, func() ([]byte, error) { return acting.Encode(w) }) - p.orderSaid[key] = true - } - for key, raw := range kept { - if !acting.IsOrder(key) || standing[key] { - continue - } - w, err := acting.Decode(raw) - if err != nil { - continue - } - if _, there := trunk[w.Module]; there && !p.orderVisited[w.Module] { - continue // the module was not looked at in this pass (no pipeline passed for it yet): its wait stands as kept - } - how := acting.HowRan - switch e, there := p.orders[key]; { - case w.Released: - how = acting.HowReleased - case there && e.fp != w.Fp, !there && wanted[w.Module][w.Node].Fp != w.Entry: - how = acting.HowReplaced - } - if !w.EndSaid { - body := acting.Body(w, assignment.Module()) - body.How = how - if err := p.wantsSay(seat.EventWaitEnded, body, key+"."+w.Fp+"."+w.Entry+"."+how); err != nil { - p.logf("the end of the wait of %s on %s for %s is not said, and is said at the next pass: %v", w.Module, w.Node, w.Part, err) - continue - } - } - p.remove(ctx, buckets.Waits(), key) - delete(p.orderSaid, key) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits_test.go deleted file mode 100644 index 6a2a9ee4..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/orderwaits_test.go +++ /dev/null @@ -1,351 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "strings" - "testing" - "time" - - mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-contracts/go/seats/meshbroker" - mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/seatdef" - - "mesh-delivery-assigner/internal/acting" - "mesh-delivery-assigner/internal/assigngraph" - plc "mesh-delivery-assigner/internal/assignment" - "mesh-delivery-assigner/internal/buckets" -) - -// orderFP is a fingerprint of the form the trunk keeps, made of one letter. -func orderFP(c string) string { - return meshcatalogclient.SourcePrefix2 + strings.Repeat(fmt.Sprintf("%x", c[0]&15), 64) -} - -// orderMod is one module of an order test: the fingerprint it runs on ace and the one the trunk holds, the form of its kept data at each -// fingerprint (0: it keeps none), the seats it holds and the source revision of its trunk value. -type orderMod struct { - name string - run, trunk string - forms map[string]int - holds []string - src int64 -} - -type orderFixture struct { - t *testing.T - p *passer - wants *memKV - events []string - bodies []meshwants.Wait - answers chan struct{} -} - -// newOrderFixture is a pass on node ace, which composes its own modules, with each module running its fingerprint healthy, built and -// granted at the trunk's, an older want, the catalog's entries answered and each held seat as the assignments say it. history lists the -// fingerprints of each module by assignment graph revision, oldest first (the objects Reverted reads). -func newOrderFixture(t *testing.T, mods []orderMod, history ...map[string]string) *orderFixture { - t.Helper() - ctx := context.Background() - f := &orderFixture{t: t, wants: &memKV{values: map[string][]byte{}}, answers: make(chan struct{}, 64)} - f.p = newPasser(kvSet{buckets.Wants(): f.wants, buckets.Asks(): &memKV{values: map[string][]byte{}}, buckets.Waits(): &memKV{values: map[string][]byte{}}}, time.Now()) - p := f.p - p.logf = t.Logf - p.say = func(oc.Condition) error { return nil } - p.publishWants = func(event string, body any, id string) error { - f.events = append(f.events, event) - f.bodies = append(f.bodies, body.(meshwants.Wait)) - return nil - } - digest := "sha256:" + strings.Repeat("c", 64) - pl := mma.Assignments{Node: "ace"} - forms := map[string]int{} // by fingerprint: each module's own - modules := []string{} - for _, m := range mods { - modules = append(modules, m.name) - pl.Modules = append(pl.Modules, mma.PlacedModule{Module: m.name}) - for _, seat := range m.holds { - scope := "node" - if seat != catalogue.BackupSeat && seat != nodeengine.Seat { - scope = "mesh" - } - pl.Holds = append(pl.Holds, mma.Holding{Claim: seat, Module: m.name, Scope: scope}) - } - for fp, form := range m.forms { - forms[fp] = form - } - mdigest := fmt.Sprintf("%064x", len(modules)) - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: m.trunk, Manifest: mdigest, Repository: "novox/" + m.name, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath, Src: m.src}) - putModuleRaw(p.w, trunkBranch, m.name, mod) - p.w.manifests[mdigest] = parsed{module: m.name, manifest: catalogue.Manifest{Module: m.name}} - endDone(t, p.w, m.name, m.trunk, mdigest) - gkey, _ := grantsstate.PartGrantKey(m.name, m.trunk) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - p.w.put(buckets.Grants, gkey, grant) - f.runs(m.name, m.run) - wkey, _ := mwc.WantKey("ace", m.name) - old, _ := mwc.EncodeWant(mwc.Want{Fp: m.run, Self: true}) - p.w.put(buckets.Wants(), wkey, old) - f.wants.values[wkey] = old - } - composes(t, p.w, "ace", digest) - values, err := mmac.AssignmentsValues(pl) - if err != nil { - t.Fatal(err) - } - for key, raw := range values { - p.w.put(plc.AssignmentsBucket(), key, raw) - } - p.w.heardPresence("ace", false, time.Now()) // ace is online: its last presence event heard is machine-back - objects := memObjects{} - for i, at := range history { - half := meshcatalogclient.Half{Revision: meshcatalog.CatalogRevision{Branch: trunkBranch, Kind: "trunk", Revision: int64(i + 1), Source: int64(i + 1)}, - Entries: map[string]meshcatalog.CatalogEntry{}} - for _, module := range modules { - digest := fmt.Sprintf("sha256:%064x", (i+1)*1000+len(half.Entries)) - half.Revision.Entries = append(half.Revision.Entries, meshcatalog.EntryRef{Module: module, Digest: digest}) - half.Entries[module] = meshcatalog.CatalogEntry{Module: module, Fp: at[module]} - } - placed := mma.Assignments{Node: "ace"} - for _, module := range modules { - placed.Modules = append(placed.Modules, mma.PlacedModule{Module: module}) - } - in := assigngraph.Inputs{Halves: map[string]meshcatalogclient.Half{trunkBranch: half}, FirstBranch: func(string) string { return trunkBranch }, - Placements: map[string]mma.Assignments{"ace": placed}} - if _, _, err := assigngraph.Keep(ctx, objects, assigngraph.Compute(in), "test", time.Now()); err != nil { - t.Fatal(err) - } - } - p.graph = &graphKeeper{objects: objects} - p.entries = meshcatalogclient.NewEntries(func(_ string, body any) (json.RawMessage, error) { - in := body.(meshcatalog.EntryInput) - entry := meshcatalog.CatalogEntry{Module: "m", Fp: in.Fingerprint, Manifest: map[string]any{"module": "m"}} - if form := forms[in.Fingerprint]; form > 0 { - entry.Manifest = map[string]any{"module": "m", "state": []any{map[string]any{"name": "sessions", "form": form}}} - } - return json.Marshal(meshcatalog.EntryAnswer{Entry: entry}) - }) - p.entries.Changed = func(meshcatalogclient.EntryRow) { f.answers <- struct{}{} } - return f -} - -// runs says module runs fp healthy on ace, heard as the node-engine seat's running and in the have. -func (f *orderFixture) runs(module, fp string) { - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: "sha256:" + strings.Repeat("c", 64), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - f.p.w.put(buckets.Have, "ace."+module, have) - f.p.w.running["ace."+module] = nodeengine.Running{Node: "ace", Module: module, Fingerprint: fp} -} - -// pass makes a pass, and after it every catalog entry it asked is answered. -func (f *orderFixture) pass() { - f.t.Helper() - f.p.wants(context.Background()) - asked := 0 - for _, row := range f.p.entries.Rows() { - if row.State == meshcatalogclient.EntryAsked { - asked++ - } - } - for range asked { - select { - case <-f.answers: - case <-time.After(10 * time.Second): // a bound on a failure only: the answers come at once - f.t.Fatal("an entry asked was not answered") - } - } -} - -// wantAt is the fingerprint module is wanted at on ace. -func (f *orderFixture) wantAt(module string) string { - f.t.Helper() - key, _ := mwc.WantKey("ace", module) - w, err := mwc.DecodeWant(f.wants.values[key]) - if err != nil { - f.t.Fatal(err) - } - return w.Fp -} - -func (f *orderFixture) waits() []string { - var out []string - for key := range f.p.w.read(buckets.Waits()) { - if acting.IsOrder(key) { - out = append(out, key) - } - } - return out -} - -// Design 47 G §298 first edge, A-258, A-259: a module whose kept data's form changes is not moved until the new version of the node-backup -// holder and of the holder of the seat mesh-broker run; the wait is kept under `order.` in the state waits, said as wait-held, and ends -// ran (wait-ended) when the holders run; a module whose kept data keeps its form is moved at once. -func TestAModuleWhoseKeptDataChangesFormWaitsForTheNewBackupHolderAndTheBusModule(t *testing.T) { - f := newOrderFixture(t, []orderMod{ - {name: "web", run: orderFP("a"), trunk: orderFP("b"), forms: map[string]int{orderFP("a"): 1, orderFP("b"): 2}}, - {name: "plain", run: orderFP("c"), trunk: orderFP("d"), forms: map[string]int{orderFP("c"): 1, orderFP("d"): 1}}, - {name: "backup", run: orderFP("e"), trunk: orderFP("f"), holds: []string{catalogue.BackupSeat}}, - {name: "bus", run: orderFP("g"), trunk: orderFP("h"), holds: []string{meshbroker.Seat}}, - }) - f.pass() // the entries are asked: no want crosses a form change unread - if got := f.wantAt("web"); got != orderFP("a") { - t.Fatalf("web is wanted at %s before the entries are read", got) - } - f.pass() - if got := f.wantAt("web"); got != orderFP("a") { - t.Fatalf("web moved across a form change at %s while the new node-backup holder and bus module do not run", got) - } - if got := f.wantAt("plain"); got != orderFP("d") { - t.Errorf("a module whose kept data keeps its form is wanted at %s, not moved", got) - } - if len(f.waits()) != 2 || len(f.events) != 2 || f.events[0] != meshwants.EventWaitHeld || f.bodies[0].Step != seatdef.StepPlace || f.bodies[0].Module != "web" { - t.Fatalf("waits %v events %v bodies %+v: want two wait-held in the step place, `order.ace.web.backup` and `order.ace.web.bus`", f.waits(), f.events, f.bodies) - } - if _, err := acting.Decode(f.p.w.read(buckets.Waits())[acting.OrderKey("ace", "web", "backup")]); err != nil { - t.Fatalf("the wait is not kept under its key: %v", err) - } - f.pass() - if len(f.events) != 2 { - t.Fatalf("a pass that changed nothing said %v", f.events) - } - f.runs("backup", orderFP("f")) - f.pass() - if got := f.wantAt("web"); got != orderFP("a") || len(f.events) != 3 || f.events[2] != meshwants.EventWaitEnded || f.bodies[2].How != acting.HowRan { - t.Fatalf("with the bus module still old: web at %s, events %v", got, f.events) - } - f.runs("bus", orderFP("h")) - f.pass() - f.pass() - if got := f.wantAt("web"); got != orderFP("b") || len(f.waits()) != 0 || len(f.events) != 4 { - t.Fatalf("with both running: web at %s, waits %v, events %v", got, f.waits(), f.events) - } -} - -// §298 first edge, the hand: assigner_waits reads an order wait and assigner_release with how `now` lets the module move. -func TestAnOrderWaitIsReadAndReleasedByTheExistingTools(t *testing.T) { - f := newOrderFixture(t, []orderMod{ - {name: "web", run: orderFP("a"), trunk: orderFP("b"), forms: map[string]int{orderFP("a"): 1, orderFP("b"): 2}}, - {name: "backup", run: orderFP("e"), trunk: orderFP("f"), holds: []string{catalogue.BackupSeat}}, - }) - f.pass() - f.pass() - var acts []string - s := newPutTools(memStore{waitsState: f.p.kvs[buckets.Waits()].(*memKV), buckets.DeliveryWaitsState: &memKV{values: map[string][]byte{}}, buckets.DeliveredAgainState: &memKV{values: map[string][]byte{}}}, func(key string, body any) (json.RawMessage, error) { - acts = append(acts, key) - return json.RawMessage(`{"ok":true,"log":"logged"}`), nil - }) - got, err := s.doWaits(map[string]any{"module": "web"}) - if list := got.(map[string]any)["waits"].([]any); err != nil || len(list) != 1 || list[0].(actingSaid).Key != "order.ace.web.backup" { - t.Fatalf("assigner_waits = %+v %v", got, err) - } - if _, err := s.doRelease(map[string]any{"node": "ace", "module": "web", "how": "now", "why": "the restore point is taken by hand"}, "shanks.node-tools"); err != nil || len(acts) == 0 { - t.Fatalf("%v %v", err, acts) - } - f.p.w.put(buckets.Waits(), "order.ace.web.backup", f.p.kvs[buckets.Waits()].(*memKV).values["order.ace.web.backup"]) - f.pass() - f.pass() - if got := f.wantAt("web"); got != orderFP("b") || f.bodies[len(f.bodies)-1].How != acting.HowReleased { - t.Fatalf("after the release: web at %s, last %+v", got, f.bodies[len(f.bodies)-1]) - } -} - -// §298 second edge, A-259: at a revert the node-engine goes back last on its node, after every module that is set back there across a -// form change runs the reverted build. -func TestAtARevertTheNodeEngineGoesBackLast(t *testing.T) { - old, now := map[string]string{"web": orderFP("a"), "engine": orderFP("c")}, map[string]string{"web": orderFP("b"), "engine": orderFP("d")} - f := newOrderFixture(t, []orderMod{ - {name: "web", run: orderFP("b"), trunk: orderFP("a"), forms: map[string]int{orderFP("a"): 1, orderFP("b"): 2}}, - {name: "engine", run: orderFP("d"), trunk: orderFP("c"), holds: []string{nodeengine.Seat}}, - }, old, now) - f.pass() - f.pass() - if f.wantAt("web") != orderFP("a") { - t.Fatalf("web is wanted at %s: the module set back is not held", f.wantAt("web")) - } - if got := f.wantAt("engine"); got != orderFP("d") { - t.Fatalf("the node-engine is set back to %s before web runs its reverted build", got) - } - if w := f.waits(); len(w) != 1 || w[0] != "order.ace.engine.web" || f.bodies[len(f.bodies)-1].Module != "engine" { - t.Fatalf("waits %v bodies %+v", w, f.bodies) - } - f.runs("web", orderFP("a")) - f.pass() - f.pass() - if got := f.wantAt("engine"); got != orderFP("c") || len(f.waits()) != 0 { - t.Fatalf("with web back: the node-engine is wanted at %s, waits %v", got, f.waits()) - } -} - -// §298 third edge, A-259, A-260: the reverted assigner and gate are wanted only once the indexer's take-in.taken of the trunk branch names a -// source revision at least the trunk's head (the highest src of the trunk's module values). -func TestTheRevertedAssignerAndGateWaitForTheIndexerToTakeInTheTrunksHead(t *testing.T) { - old, now := map[string]string{"asg": orderFP("a"), "gate": orderFP("c"), "idx": orderFP("e")}, map[string]string{"asg": orderFP("b"), "gate": orderFP("d"), "idx": orderFP("f")} - f := newOrderFixture(t, []orderMod{ - {name: "asg", run: orderFP("b"), trunk: orderFP("a"), holds: []string{mma.Seat}, src: 6}, - {name: "gate", run: orderFP("d"), trunk: orderFP("c"), holds: []string{meshdelivery.Seat}, src: 7}, - {name: "idx", run: orderFP("e"), trunk: orderFP("e"), src: 7}, - }, old, now) - f.pass() - f.pass() - if f.wantAt("asg") != orderFP("b") || f.wantAt("gate") != orderFP("d") { - t.Fatalf("the reverted assigner (%s) and gate (%s) are wanted before the head is taken in", f.wantAt("asg"), f.wantAt("gate")) - } - if len(f.waits()) != 2 || f.bodies[0].Part != "mesh-delivery-indexer" { - t.Fatalf("waits %v bodies %+v", f.waits(), f.bodies) - } - taken := func(branch string, source int64) { - raw, _ := json.Marshal(meshcatalog.TakeInTaken{Kind: "taken", Key: "m.1", Branch: branch, Source: source}) - if err := f.p.w.taken.put(raw); err != nil { - t.Fatal(err) - } - } - taken("beta", 9) // another branch, and - taken(trunkBranch, 6) - f.pass() - if f.wantAt("asg") != orderFP("b") { - t.Fatal("a source revision before the trunk's head was taken in, and the assigner was wanted") - } - taken(trunkBranch, 7) - f.pass() - f.pass() - if f.wantAt("asg") != orderFP("a") || f.wantAt("gate") != orderFP("c") || len(f.waits()) != 0 { - t.Fatalf("after the head is taken in: asg %s gate %s waits %v", f.wantAt("asg"), f.wantAt("gate"), f.waits()) - } -} - -// A-260: an order wait still held after a restart is said again as wait-held, by a pass that remembers nothing, and ends at the next -// take-in.taken that names the trunk's head. -func TestAnOrderWaitHeldAfterARestartIsSaidAgain(t *testing.T) { - old, now := map[string]string{"asg": orderFP("a")}, map[string]string{"asg": orderFP("b")} - f := newOrderFixture(t, []orderMod{{name: "asg", run: orderFP("b"), trunk: orderFP("a"), holds: []string{mma.Seat}, src: 7}}, old, now) - f.pass() - f.pass() - if len(f.events) != 1 { - t.Fatalf("events %v", f.events) - } - f.p.orderSaid = nil // a process started again - f.pass() - if len(f.events) != 2 || f.events[1] != meshwants.EventWaitHeld || len(f.waits()) != 1 { - t.Fatalf("events %v waits %v", f.events, f.waits()) - } - raw, _ := json.Marshal(meshcatalog.TakeInTaken{Kind: "taken", Key: "m.1", Branch: trunkBranch, Source: 8}) - if err := f.p.w.taken.put(raw); err != nil { - t.Fatal(err) - } - f.pass() - f.pass() - if f.wantAt("asg") != orderFP("a") || len(f.waits()) != 0 || f.events[len(f.events)-1] != meshwants.EventWaitEnded { - t.Fatalf("asg at %s, waits %v, events %v", f.wantAt("asg"), f.waits(), f.events) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go index ad477697..721f9df3 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go @@ -76,9 +76,6 @@ type outsideActs struct { cleared map[string]bool // sigs is each check's last signature, by node.module. sigs map[string]string - // own is the machines whose own network is open; ownRead says the read at the start was answered. - own map[string]bool - ownRead bool // causes is, by condition key, the nodes whose waits stand on it, as the last pass kept them. causes map[string][]string // started says the first pass judged every wait again. @@ -93,7 +90,7 @@ func newOutsideActs(call func(string, any) (json.RawMessage, error), wake func() settle = outside.DefaultSettle } return &outsideActs{call: call, wake: wake, logf: logf, bound: bound, settle: settle, reading: map[string]bool{}, answered: map[string][]outside.Cond{}, - dirty: map[string]bool{}, cleared: map[string]bool{}, sigs: map[string]string{}, own: map[string]bool{}, causes: map[string][]string{}} + dirty: map[string]bool{}, cleared: map[string]bool{}, sigs: map[string]string{}, causes: map[string][]string{}} } // outsideSettings are the bounds of the wait, the settings outside-wait-minutes and outside-settle-seconds (design 47 G §633, §706), @@ -135,31 +132,8 @@ func settingFile(path string) (int, error) { return n, nil } -// readStart reads the open conditions of every machine once at the start, for the machines whose own network is held. -func (o *outsideActs) readStart() { - go func() { - answer, err := o.call(conditionsVerb, map[string]any{"scope": "machine"}) - if err != nil { - o.logf("the open conditions are not read at the start (%v): no machine is held for its own network until its event is heard", err) - return - } - conds, err := outside.ParseConditions(answer) - if err != nil { - o.logf("%v", err) - return - } - o.mu.Lock() - for m := range outside.OwnNetwork(conds) { - o.own[m] = true - } - o.ownRead = true - o.mu.Unlock() - o.wake() - }() -} - -// Heard is one of the seat's events condition-raised, -changed or -cleared: it says which machines to judge again and keeps the -// machines whose own network is open; nothing is kept of the condition. +// Heard is one of the seat's events condition-raised, -changed or -cleared: it says which machines to judge again; nothing is kept +// of the condition (the network hold ends, design 47 H §750, H-A-55). func (o *outsideActs) Heard(subject string, body []byte) { var e mcseat.ConditionEvent if err := json.Unmarshal(body, &e); err != nil || e.Key == "" { @@ -168,9 +142,6 @@ func (o *outsideActs) Heard(subject string, body []byte) { } cleared := strings.HasSuffix(subject, "."+mcseat.EventConditionCleared) o.mu.Lock() - if p := strings.Split(e.Key, "."); len(p) == 3 && p[0] == "machine" && p[2] == "network" { - o.own[p[1]] = !cleared - } if cleared { for _, node := range o.causes[e.Key] { o.dirty[node] = true @@ -186,16 +157,6 @@ func (o *outsideActs) Heard(subject string, body []byte) { o.wake() } -// Held says whether machine's own network is open: no want moves to it. -func (o *outsideActs) Held(machine string) bool { - if o == nil { - return false - } - o.mu.Lock() - defer o.mu.Unlock() - return o.own[machine] -} - // take is the conditions read for machine, consumed (one answer is one judging), or false while the kept call is unanswered, // which it starts when none is in flight. func (o *outsideActs) take(machine string) ([]outside.Cond, bool) { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go index 04cf6c25..7c8c383e 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go @@ -237,29 +237,12 @@ func TestTheWaitOnAnOutsideCause(t *testing.T) { }) } -func TestAMachineWhoseOwnNetworkIsOpenGetsNoWant(t *testing.T) { - m := assign.Module{Name: "nats", FP: fpTest, Built: true, Nodes: []string{"ace"}, MachineHeld: func(node string) bool { return node == "ace" }} - ok, rule, why := assign.Holds(m, "ace", assign.Nodes{Online: map[string]bool{"ace": true}}) - if ok || rule == 0 || !strings.Contains(why, "machine.ace.network") { - t.Errorf("holds = %v %d %q", ok, rule, why) - } - o := newOutsideActs(nil, func() {}, func(string, ...any) {}, 0, 0) - o.Heard("mesh.seat.mesh-conditions.event.condition-raised", []byte(`{"key":"machine.ace.network","subject":{"id":"ace","scope":"machine","machine":"ace"}}`)) - if !o.Held("ace") || o.Held("g14") { - t.Error("the own network condition's event does not hold the machine") - } - o.Heard("mesh.seat.mesh-conditions.event.condition-cleared", []byte(`{"key":"machine.ace.network","subject":{"id":"ace","scope":"machine","machine":"ace"}}`)) - if o.Held("ace") { - t.Error("the machine is held after its condition cleared") - } -} - func TestTheToolsReadAndReleaseAnOutsideWait(t *testing.T) { r := newOutsideRig(t) r.cond("machine.hub.unreachable", "ace") r.review(false, nil) var acts []string - s := newPutTools(memStore{waitsState: &memKV{values: map[string][]byte{}}, buckets.DeliveryWaitsState: &memKV{values: map[string][]byte{}}, buckets.DeliveredAgainState: &memKV{values: map[string][]byte{}}}, func(key string, body any) (json.RawMessage, error) { + s := newPutTools(memStore{waitsState: &memKV{values: map[string][]byte{}}}, func(key string, body any) (json.RawMessage, error) { acts = append(acts, key) return json.RawMessage(`{"ok":true,"log":"logged"}`), nil }) @@ -296,38 +279,4 @@ func TestTheToolsReadAndReleaseAnOutsideWait(t *testing.T) { if r.bodies[len(r.bodies)-1].How != "go-on" || n.Haves["ace"]["nats"].Health != nodeengineclient.HealthOK { t.Errorf("last %+v health %s", r.bodies[len(r.bodies)-1], n.Haves["ace"]["nats"].Health) } - // A-246: a dependent's delivery wait is listed beside them, and released by its module and step delivery, its hand act first. - dw, _ := json.Marshal(deliveryWaiting{Wait: meshwants.Wait{Step: meshwants.WaitStepDelivery, Module: "app", Part: "db", On: "db@1.1.0", Machines: []string{"g14"}}}) - s.st.put(buckets.DeliveryWaitsState, "app", dw) - got, err = s.doWaits(map[string]any{"node": "g14"}) - if d := got.(map[string]any)["delivery"].([]deliveryWaiting); err != nil || len(d) != 1 || d[0].Module != "app" { - t.Errorf("the delivery waits listed = %+v %v", got, err) - } - if _, err := s.doRelease(map[string]any{"module": "app", "step": "build", "why": "x"}, "shanks.node-tools"); err == nil { - t.Error("a release of another step is taken") - } - if _, err := s.doRelease(map[string]any{"module": "web", "step": "delivery", "why": "x"}, "shanks.node-tools"); err == nil { - t.Error("a release of a module waiting for no delivery is taken") - } - before := len(acts) - if _, err := s.doRelease(map[string]any{"module": "app", "step": "delivery", "why": "db is fine as it runs"}, "shanks.node-tools"); err != nil { - t.Fatal(err) - } - raw, _, _, _ = s.st.get(buckets.DeliveryWaitsState, "app") - var released deliveryWaiting - if json.Unmarshal(raw, &released) != nil || !released.Released || !strings.Contains(released.By, "db is fine") || len(acts) != before+1 { - t.Errorf("the delivery wait after its release = %+v, hand acts %v", released, acts[before:]) - } - // A-250: assigner_delivered_again records its hand act, writes the ask and answers asked; assigner_waits lists it. - if _, err := s.doDeliveredAgain(map[string]any{"module": "db", "version": "1.1.0"}, "op"); err == nil { - t.Error("an ask with no why is taken") - } - ans, err := s.doDeliveredAgain(map[string]any{"module": "db", "version": "1.1.0", "why": "the catalog missed it"}, "op") - if err != nil || ans.(map[string]any)["asked"] != "db" || len(acts) != before+2 { - t.Fatalf("assigner_delivered_again = %+v %v", ans, err) - } - got, _ = s.doWaits(map[string]any{}) - if a := got.(map[string]any)["delivered-again"].([]againAsk); len(a) != 1 || a[0].Version != "1.1.0" || a[0].By != "op" { - t.Errorf("the asks listed = %+v", a) - } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_tools.go index 3140d230..2209af6c 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_tools.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "errors" "fmt" "slices" @@ -10,11 +9,8 @@ import ( "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/keys" - "mesh-delivery-assigner/internal/acting" "mesh-delivery-assigner/internal/buckets" "mesh-delivery-assigner/internal/outside" ) @@ -37,13 +33,6 @@ type waitSaid struct { BoundEnds string `json:"bound-ends,omitempty"` } -// actingSaid is one wait for an acting part's new version as assigner_waits answers it (actingwaits.go). -type actingSaid struct { - Key string `json:"key"` - acting.Wait - BoundEnds string `json:"bound-ends,omitempty"` -} - // doWaits is the tool assigner_waits. func (s *putTools) doWaits(args map[string]any) (any, error) { keys, err := s.st.keys(waitsState) @@ -57,16 +46,6 @@ func (s *putTools) doWaits(args map[string]any) (any, error) { if err != nil || !found { continue } - if acting.Is(key) || acting.IsOrder(key) { - if a, err := acting.Decode(raw); err == nil && (arg(args, "node") == "" || a.Node == arg(args, "node")) && (arg(args, "module") == "" || a.Module == arg(args, "module")) { - said := actingSaid{Key: key, Wait: a} - if d, err := time.ParseDuration(a.Bound); err == nil && !a.Released { - said.BoundEnds = a.ClockFrom.Add(d).UTC().Format("2006-01-02T15:04:05Z") - } - out = append(out, said) - } - continue - } w, err := outside.Decode(raw) if err != nil || (arg(args, "node") != "" && w.Node != arg(args, "node")) || (arg(args, "module") != "" && w.Module != arg(args, "module")) { continue @@ -79,120 +58,13 @@ func (s *putTools) doWaits(args map[string]any) (any, error) { } out = append(out, said) } - // And each dependent's wait for its dependency's delivery (design 47 G §376, A-246), from the state delivery-waits. - delivery := []deliveryWaiting{} - dkeys, err := s.st.keys(buckets.DeliveryWaitsState) - if err != nil { - return nil, fmt.Errorf("the delivery waits cannot be listed: %w", err) - } - sort.Strings(dkeys) - for _, key := range dkeys { - raw, _, found, err := s.st.get(buckets.DeliveryWaitsState, key) - if err != nil || !found { - continue - } - var w deliveryWaiting - if json.Unmarshal(raw, &w) != nil || (arg(args, "module") != "" && w.Module != arg(args, "module")) || - (arg(args, "node") != "" && !slices.Contains(w.Machines, arg(args, "node"))) { - continue - } - delivery = append(delivery, w) - } - // And each ask of assigner_delivered_again not yet holding, with why (A-250). - again := []againAsk{} - akeys, err := s.st.keys(buckets.DeliveredAgainState) - if err != nil { - return nil, fmt.Errorf("the asks to say a version delivered again cannot be listed: %w", err) - } - sort.Strings(akeys) - for _, key := range akeys { - raw, _, found, err := s.st.get(buckets.DeliveredAgainState, key) - var a againAsk - if err != nil || !found || json.Unmarshal(raw, &a) != nil || (arg(args, "module") != "" && a.Module != arg(args, "module")) { - continue - } - again = append(again, a) - } - return map[string]any{"waits": out, "delivery": delivery, "delivered-again": again}, nil -} - -// toolDeliveredAgain is the tool that asks version-delivered said again for one version (design 47 G §376, A-250). -const toolDeliveredAgain = "assigner_delivered_again" - -// doDeliveredAgain is assigner_delivered_again: the hand act is recorded first, then the ask is written into the state -// delivered-again, whose watch the assigner's process hears at once; it answers that it is asked. -func (s *putTools) doDeliveredAgain(args map[string]any, by string) (any, error) { - module, version, why := arg(args, "module"), arg(args, "version"), arg(args, "why") - if module == "" || version == "" || why == "" { - return nil, errors.New("assigner_delivered_again names the module, its version and why: it is kept in the hand-act log") - } - rec := meshhandactsclient.Recorder{Ask: s.ask} - taken, err := rec.Record(meshhandactsclient.Request{What: fmt.Sprintf("assigner_delivered_again %s %s", module, version), Why: why, - Cause: "version-delivered", Verb: "mesh-wants.assigner_delivered_again"}) - if err != nil { - return nil, fmt.Errorf("the hand act could not be recorded, so nothing was asked: %w", err) - } - data, err := json.Marshal(againAsk{Module: module, Version: version, By: by, Why: why}) - if err != nil { - return nil, err - } - if err := s.st.put(buckets.DeliveredAgainState, keys.EscapeName(module), data); err != nil { - return nil, fmt.Errorf("the ask of %s %s is not written: %w", module, version, err) - } - return map[string]any{"asked": module, "version": version, "hand-act": taken.Log, - "follow": "the assigner says version-delivered again once every present machine placing it runs it healthy; assigner_waits says why it does not yet"}, nil -} - -// deliveredAgainTool is assigner_delivered_again as the runtime serves it. -func (s *putTools) deliveredAgainTool() stdio.Tool { - return stdio.Tool{Name: toolDeliveredAgain, Description: "Say version-delivered again for one version of a module, for the operator and the healers " + - "(design 47 G §376): written in the hand-act log first, then asked of the assigner, which says it once every present machine placing " + - "the module runs that version healthy; until then assigner_waits lists the ask with why it does not hold. Answers that it is asked.", - Input: schema(map[string]string{"module": "the module", "version": "its MAJOR.MINOR.PATCH", "why": "why, kept in the hand-act log"}, - []string{"module", "version", "why"}), - Run: func(a map[string]any) (any, error) { return s.doDeliveredAgain(a, stdio.CallerOf(a)) }} -} - -// doReleaseDelivery is assigner_release with step delivery: the hand act is recorded first, then the release is written on the -// module's delivery wait, which the assigner's process ends released at its next pass, writing the module's want. -func (s *putTools) doReleaseDelivery(args map[string]any, by string) (any, error) { - module, why := arg(args, "module"), arg(args, "why") - if why == "" { - return nil, errors.New("release says why: it is kept in the hand-act log") - } - key := keys.EscapeName(module) - raw, _, found, err := s.st.get(buckets.DeliveryWaitsState, key) - if err != nil { - return nil, fmt.Errorf("the delivery wait of %s is not read: %w", module, err) - } - var w deliveryWaiting - if !found || json.Unmarshal(raw, &w) != nil || w.Released { - return nil, fmt.Errorf("%s waits for no delivery: assigner_waits lists the delivery waits", module) - } - rec := meshhandactsclient.Recorder{Ask: s.ask} - taken, err := rec.Record(meshhandactsclient.Request{What: fmt.Sprintf("assigner_release %s step delivery (waits on %s)", module, w.On), Why: why, - Cause: "delivery-wait", Verb: "mesh-wants.assigner_release"}) - if err != nil { - return nil, fmt.Errorf("the hand act could not be recorded, so nothing was released: %w", err) - } - w.Released, w.By = true, by+": "+why - data, err := json.Marshal(w) - if err != nil { - return nil, err - } - if err := s.st.put(buckets.DeliveryWaitsState, key, data); err != nil { - return nil, fmt.Errorf("the release of %s's delivery wait is not written: %w", module, err) - } - return map[string]any{"released": module, "step": meshwants.WaitStepDelivery, "by": by, "hand-act": taken.Log}, nil + return map[string]any{"waits": out}, nil } // doReleaseWait is assigner_release with how: the hand act is recorded first, then the release is written on the wait of node and // module, which the assigner's process takes at its next pass. func (s *putTools) doReleaseWait(args map[string]any, by string) (any, error) { node, module, how, why := arg(args, "node"), arg(args, "module"), arg(args, "how"), arg(args, "why") - if how == acting.HowNow { - return s.doReleaseActing(node, module, why, by) - } if !slices.Contains([]string{outside.HowGoOn, outside.HowFailed, outside.HowRejudge}, how) { return nil, fmt.Errorf("how is %s, %s or %s, not %q", outside.HowGoOn, outside.HowFailed, outside.HowRejudge, how) } @@ -238,61 +110,9 @@ func (s *putTools) doReleaseWait(args map[string]any, by string) (any, error) { return map[string]any{"released": key, "how": how, "by": by, "hand-act": taken.Log}, nil } -// doReleaseActing is assigner_release with how `now`: the entry of module on node moves with its acting parts' running versions. The -// hand act is recorded first; the release is written on each standing wait of the entry, and the process says wait-ended at its next pass. -func (s *putTools) doReleaseActing(node, module, why, by string) (any, error) { - if why == "" { - return nil, errors.New("release says why: it is kept in the hand-act log") - } - keys, err := s.st.keys(waitsState) - if err != nil { - return nil, fmt.Errorf("the waits cannot be listed: %w", err) - } - type one struct { - key string - w acting.Wait - } - var held []one - prefix, orderPrefix := acting.Prefix+node+"."+module+".", acting.OrderPrefix+node+"."+module+"." - for _, k := range keys { - if !strings.HasPrefix(k, prefix) && !strings.HasPrefix(k, orderPrefix) { - continue - } - raw, _, found, err := s.st.get(waitsState, k) - if err != nil || !found { - continue - } - if w, err := acting.Decode(raw); err == nil && !w.Released { - held = append(held, one{k, w}) - } - } - if len(held) == 0 { - return nil, fmt.Errorf("%s holds no wait of %s for an acting part: assigner_waits lists them", node, module) - } - rec := meshhandactsclient.Recorder{Ask: s.ask} - taken, err := rec.Record(meshhandactsclient.Request{What: fmt.Sprintf("assigner_release %s %s %s", node, module, acting.HowNow), Why: why, - Cause: "acting-wait", Verb: "mesh-wants.assigner_release"}) - if err != nil { - return nil, fmt.Errorf("the hand act could not be recorded, so nothing was released: %w", err) - } - released := []string{} - for _, h := range held { - h.w.Released, h.w.By, h.w.Why = true, by+": "+why, why - data, err := acting.Encode(h.w) - if err != nil { - return nil, err - } - if err := s.st.put(waitsState, h.key, data); err != nil { - return nil, fmt.Errorf("the release of %s on %s is not written: %w", module, node, err) - } - released = append(released, h.key) - } - return map[string]any{"released": released, "how": acting.HowNow, "by": by, "hand-act": taken.Log}, nil -} - // waitsTool is assigner_waits as the runtime serves it. func (s *putTools) waitsTool() stdio.Tool { - return stdio.Tool{Name: toolWaits, Description: "Every wait of the delivery and where it stands: an entry waiting for an acting part's new version in composing or placing (§266, §268), with its part, version, step, clock-from and bound; and a moved module's failing health check on a " + + return stdio.Tool{Name: toolWaits, Description: "Every wait of the delivery and where it stands: a moved module's failing health check on a " + "machine whose open conditions name a cause outside the delivery (design 47 G §706), with its module, machine, fingerprint, the " + "condition's key, what it stands `on`, its clock-from, bound, its ask and its state; a settle shows as settling with its end. Narrowed by " + "machine or module. Read-only.", diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 092877ec..4a5afd27 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -5,8 +5,7 @@ import ( "context" "encoding/json" "errors" - "fmt" - "maps" + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" "mesh-delivery-assigner/internal/buckets" "slices" "sort" @@ -14,25 +13,18 @@ import ( "time" "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" - "git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay" - "git.novox.be/novox/mesh-sdk/go/motion" "git.novox.be/novox/mesh-sdk/go/seatlist" "sync" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" - mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient/enginereads" - mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "mesh-delivery-assigner/internal/assign" @@ -62,21 +54,12 @@ type passer struct { whys map[string]string // findings is the assigner's findings bucket (its said conditions, ADR 0564 §2). findings assign.Findings - // calls is the wait of the kept manifest-reads calls (enginereads.ReadsCalls, ADR 0668 §3), made at the first pass. - calls *enginereads.ReadsCalls - // failedProvisions are the failed provisions whose finding this process wrote (ADR 0570 §4). - failedProvisions map[string]bool - // missing are the required provisions with no key this process pulsed, and missingSaid those whose finding it wrote. - missing, missingSaid map[string]bool // unplaced are the nodes whose unplaced finding this process wrote. unplaced map[string]bool // answeredAt is, by the id of a backup ask, how many values the have had when its answer was written into the want. answeredAt map[string]uint64 // now is the clock; nil is time.Now. now func() time.Time - // neither is, by node and module, each module on neither list whose condition this process raised (issue 1945). Nil until the - // first pass after a start, which clears what a previous process may have raised (startClears). - neither map[string]neitherOne // ends are the pipeline ends the wants follow (pipelines.go), once the first registrations are heard; until then, and nil, the trunk-only want. ends *pipelineEnds // acts are the puts' acts (ADR 0546); nil: the puts are not taken further (no bus to call, or no puts bucket). @@ -110,24 +93,23 @@ type passer struct { intended map[string]map[string]map[string]bool // laidHere are the proposals whose candidates this process wrote (design 47 G A-317), by id. laidHere map[string]bool - // reads keeps what each node's engine states it reads, asked through the node-engine seat's verb (ADR 0618 §3); nil reads - // each node's record, as the rules did before the verb. online is, by node, whether the last pass saw it online. - reads *enginereads.Keeper - online map[string]bool + // reads is the assigner's state engine-reads, what each node's engine said it reads (enginewords.go, design 47 H-A-104); + // nil reads none. + reads *engineReadsKept // outside is the judging of waits on an outside cause (outside.go, design 47 G §706); publishWants says the seat mesh-wants' // wait-held and wait-ended. Nil: no wait is made. outside *outsideActs publishWants publisher // graph keeps the assignment graph revision the wants follow (graphkept.go, design 47 G §292, A-67); nil: none is kept. graph *graphKeeper - // orders are §298's order edges computed in this pass, by their wait's key, and orderVisited the modules they were computed for (orderwaits.go). - orders map[string]orderEdge - orderVisited map[string]bool - // orderSaid are the order waits whose wait-held this process said. - orderSaid map[string]bool // entries are the catalog entries by fingerprint asked of the seat mesh-catalog's verb `entry` (design 47 G §614, A-217): a // held revert's running entry and its want's (revert.go); nil: no revert is asked about. entries *meshcatalogclient.Entries + // tierBound is the setting tier-bound (design 47 H §751; zero: its default), and tierTimer the one timer that wakes the pass at + // the earliest tier's deadline (tiers.go), made with tierWake; nil tierWake sets no timer. + tierBound time.Duration + tierWake func() + tierTimer *time.Timer } // wantsSay says one event of the seat mesh-wants; with no bus to say it on, nothing is said. @@ -138,86 +120,15 @@ func (p *passer) wantsSay(event string, body any, id string) error { return p.publishWants(event, body, id) } -// engineReads sets what the rules read of each node's engine (ADR 0618 §3, §4): the statement the verb manifest-reads -// answered, or its record where the verb was not answered (the fallback) or not asked yet. A node is asked when the -// assigner first hears of it, and again when it comes back online, since an engine built before the event says none. +// engineReads sets what the rules read of each node's engine: the words its node-engine said in the last +// manifest-reads-changed the assigner heard of it, as its state engine-reads keeps them (design 47 H-A-104). A node with no +// key states nothing, so its wants are held awaiting its hand placement (§772). func (p *passer) engineReads(n assign.Nodes) { - if p.reads == nil { - return - } - known := p.w.knownNodes() - first := p.online == nil - for _, node := range known { - switch { - case first: - p.reads.AskNode(node, "the assigner started") - case !p.reads.Known(node): - p.reads.AskNode(node, "first heard of") - case !first && n.Online[node] && !p.online[node]: - p.reads.AskNode(node, "back online") + for _, node := range p.reads.nodes() { + if words, ok := p.reads.words(node); ok { + n.Reads[node] = words } } - p.online = maps.Clone(n.Online) - if p.online == nil { - p.online = map[string]bool{} - } - for node, reads := range p.reads.Reads(known) { - if p.reads.Known(node) { - n.Reads[node] = reads - } - } - p.unansweredReads(known, n.Online) -} - -// unansweredReads keeps the wait of every node whose engine has not answered manifest-reads while its machine is heard -// (online), through the node-engine seat's client (enginereads.ReadsCalls, shared with the builder and the indexer): it -// is asked again once its last ask ended, so the kept call goes on until answered; past -// meshconditionsclient.CallUnansweredAfter it is the finding call-unanswered in the assigner's findings, cleared once -// answered (novox/hq ADR 0668 §3). Each pass is the event, and the assigner's own presence says heard: no timer, no bound. -func (p *passer) unansweredReads(known []string, online map[string]bool) { - if p.reads.Call == nil { - return - } - if p.findings.Put == nil || p.findings.Delete == nil { - for _, node := range known { - if s, ok := p.reads.Statements()[node]; ok && !s.Answered && online[node] && !p.reads.Asking(node) { - p.reads.AskNode(node, "unanswered since "+s.UnansweredSince.UTC().Format(time.RFC3339)+", its machine heard") - } - } - return - } - if p.calls == nil || p.calls.Keeper != p.reads { - p.calls = &enginereads.ReadsCalls{Keeper: p.reads, Bound: mcc.CallUnansweredAfter, Now: p.clock, Logf: p.logf, - Raise: func(_ context.Context, u enginereads.UnansweredCall) error { - o := assign.CallUnanswered(enginereads.CallID(u.Node), enginereads.Invoke, u.Node, u.Words, "assigner."+mcc.KindCallUnanswered, u.Since) - o.Needs = "assigner_engine_reads with ask_again asks the node again now and shows its statement." - return p.findings.Put(o.Finding(u.At)) - }, - Clear: func(_ context.Context, node string) error { - return p.findings.Delete(mcc.Key(mcc.ScopeCall, enginereads.CallID(node), mcc.KindCallUnanswered)) - }} - if p.findings.Keys != nil { - // What a pass before or another process wrote: every such finding of the assigner's is weighed to be cleared. - prefix, suffix := mcc.ScopeCall+"."+enginereads.CallID(""), "."+mcc.KindCallUnanswered - p.calls.Held = func(context.Context) ([]string, error) { - keys, err := p.findings.Keys() - var nodes []string - for _, key := range keys { - if rest, ok := strings.CutPrefix(key, prefix); ok { - if node, ok := strings.CutSuffix(rest, suffix); ok { - nodes = append(nodes, node) - } - } - } - return nodes, err - } - } else { - p.calls.Held = func(context.Context) ([]string, error) { return nil, nil } - } - } - p.calls.Nodes = func() []string { return known } - p.calls.Heard = func(_ context.Context, node string) (bool, error) { return online[node], nil } - p.calls.Step(context.Background()) } func (p *passer) clock() time.Time { @@ -230,8 +141,6 @@ func (p *passer) clock() time.Time { func (p *passer) pass(ctx context.Context) { p.assignmentsSeat(ctx) p.keepGraph(ctx) - p.judgeDelivered() - p.deliveredAgain(ctx) if p.graph == nil || !p.graph.full { // a full assignment graph store keeps the last revision and writes no new want (design 47 G §181) p.wants(ctx) p.sayPass(ctx, "judged") @@ -261,14 +170,13 @@ func (p *passer) nodeWants(ctx context.Context) { } } -// wants writes, for every module the trunk holds a fingerprint of, each node's want by the six rules (ADR 0429 §6), and -// the hold of a fingerprint whose health check failed. The assigner compares its wants with nothing the controller -// sends: the differences bucket is kept read-only with its history (ADR 0539 §8). +// wants writes, for every module assigned or wanted, each node's want naming the newest version released on the branch its +// placement names whose pipeline passed, written tier by tier (design 47 H §750, §751). The want is held for nothing else: the +// rules of ADR 0429 §6, the network hold and A-428's exceptions end (H-A-55); a put owns its node's want, and the operator's yes +// on a module of policy record is the node-engine's have and the asker's (yeses). func (p *passer) wants(ctx context.Context) { assigned := p.w.assigned() assignedBranches := p.w.placedBranches() - // Every module of every branch's catalog half, for the parts that ask which exist; a node's entries come from the half of the - // branch its placement names (design 47 G §14, §184). trunk := p.w.trunk() views := map[string]*branchView{} viewOf := func(branch string) *branchView { @@ -288,15 +196,23 @@ func (p *passer) wants(ctx context.Context) { } } wanted := p.w.wantsOf() + // The tier progress moves before the wants are written, so a tier that runs has the next tier's wants written in this pass + // (design 47 H §751). + p.tierDeadline(p.tiers(nodes, func(module, branch string) []string { + for _, g := range p.groups(module, nodesOf[module], assignedBranches[module]) { + if g.branch == branch { + return g.nodes + } + } + return nil + })) chosen, _ := assign.DecodePolicies(p.w.read(assign.PoliciesBucket())) - provisionsOf := p.provisionsOf() - missing := map[string]bool{} - defer func() { p.provisionsMissing(ctx, missing) }() defer p.unplacedFindings() defer func() { p.seatHolders(ctx, viewOf(p.w.trunkName()).manifests) }() p.clearStaleCalls() placed := p.w.placedNodes() puts := p.putsByModule() + builds := placementBuilds(p.w.assignments()) p.staleAsks(ctx, trunk) names := map[string]bool{} for name := range trunk { @@ -305,410 +221,42 @@ func (p *passer) wants(ctx context.Context) { for name := range wanted { names[name] = true } - neither := map[string]neitherOne{} - // The first pass after a start remembers nothing a previous process said: a neither or held condition it raised would stay - // raised for ever once the module has its want or is no longer held. That pass clears, once, the neither condition of every - // assigned module not on neither list and the held condition of every module not held; a clear of a condition that is not open is - // quiet at the router. - first := p.neither == nil - stale := map[string]neitherOne{} - left := map[string]string{} - defer func() { p.sayNeither(neither, stale, left) }() - // Where grants are read from, once for the pass: the grants part's (issue 774, ADR 0571 clause 9). - grants := p.w.grantsBucket() - released := p.actingReleased() - p.orders, p.orderVisited = map[string]orderEdge{}, map[string]bool{} - defer func() { p.actingWaits(ctx, viewOf(p.w.trunkName()).nodes, wanted) }() - defer func() { p.orderWaits(ctx, viewOf(p.w.trunkName()).nodes, wanted) }() for name := range names { groups := p.groups(name, nodesOf[name], assignedBranches[name]) for gi, g := range groups { - func() { - bv := viewOf(g.branch) - trunk, manifests, nodes, moduleGraph, served, offering := bv.trunk, bv.manifests, bv.nodes, bv.graph, bv.served, bv.offering - fp := trunk[name].Fp2 - name := name - nodesOf := map[string][]string{name: g.nodes} - wantedOf := map[string]map[string]mwc.Want{name: wantedHere(wanted[name], g.nodes, groups, gi == 0)} - // A want follows a passed pipeline: a module whose fingerprint no pipeline passed at on the branch its placements - // chose, and whose wants do not name it already, gets none (design 47 G §29, §35). - var branches []string - if p.ends.following() { - branches = []string{g.branch} - } - if fp != "" && !p.ends.mayWant(name, fp, branches, wantedOf[name]) { - if !runnerFirstStart(name, manifests, p.w.runningNow()) && !manifests[name].ClaimsSeat(meshcatalog.Seat) { - none := assign.Outcome{Rules: noPipelineRules(g.nodes, name, fp, branches)} - p.sayNoWant(name, g.nodes, wantedOf[name], none) - p.sayWhy(ctx, name, g.nodes, wantWhy{Branch: g.branch, Fp: fp, Step: whyNoPipeline}, wantedOf[name], &none, nil) - p.nameHeldBuilds(ctx, name, fp, g.nodes, nodes, wantedOf[name], p.namedBuild(name, fp, trunk[name].FpSrc)) - return + bv := viewOf(g.branch) + wantedOf := wantedHere(wanted[name], g.nodes, groups, gi == 0) + target, ok := p.ends.target(name, g.branch) + m := assign.Module{Name: name, Manifest: bv.manifests[name], Chosen: chosenOf(chosen, name), Nodes: g.nodes, Puts: puts[name], + Self: func(node string) bool { return placed[node] }, + // §772, H-A-45: a node-engine that does not state want:version gets its want held, awaiting its hand placement. + ReadsVersion: func(node string) bool { + return slices.Contains(nodes.Reads[node], nodeengine.ManifestReadWordWantVersion) + }, + // H §750, H-A-5, H-A-99: a placement's build is read before the rule and names the want's version. + Target: func(node string) (assign.Target, bool) { + if build := builds[node][name]; build != "" { + return p.buildTarget(name, g.branch, node, build) } - // The first start of the pipeline runner (design 47 G A-428): no runner runs to pass its pipeline, so its want is - // written from the trunk's entry by the other rules, and said once. So is the holder of mesh-catalog's: every - // pipeline runs on a revision it took in, and a revision naming a newer one is taken in only by that one (§16, - // G-5's whole test of indexer and assigner: the new indexer is wanted and granted, starts, and takes it in). - if key := "no-pipeline-start " + name + "@" + fp; !p.skipped[key] { - if p.skipped == nil { - p.skipped = map[string]bool{} - } - p.skipped[key] = true - if manifests[name].ClaimsSeat(meshcatalog.Seat) { - p.logf("%s at %s is wanted with no passed pipeline: it claims the seat %s, every pipeline runs on a revision it took in, and a revision naming it is taken in only by it (design 47 G §16)", name, shortHex(fp), meshcatalog.Seat) - } else { - p.logf("%s at %s is wanted with no passed pipeline: it claims the seat %s and no module holding that seat runs on any node, so none can pass one (design 47 G A-428)", name, shortHex(fp), mpr.Seat) - } - } - } - // A dependent's new version waits for its dependency's newest released version to be delivered (§376, A-189). - if p.holdForDelivery(ctx, name, fp, g.branch, g.nodes) { - p.sayWhy(ctx, name, g.nodes, wantWhy{Branch: g.branch, Fp: fp, Step: whyDelivery, Words: p.saidDeliveryWaits()[name].Wait.On}, wantedOf[name], nil, nil) - return - } - m := assign.Module{Name: name, FP: fp, Manifest: manifests[name], Needs: trunk[name].Needs, Chosen: chosenOf(chosen, name), Provisions: provisionsOf(name), Answering: answering(manifests, nodes), Offered: offered(manifests), Nodes: nodesOf[name], Now: time.Now(), Puts: puts[name], FormNeeds: p.w.buildNeeds(name, fp), - Self: func(node string) bool { return placed[node] }, - Composed: func(node, fp string) (assign.ComposedDigest, bool) { - return p.w.composed(node, name, fp) // a placed node's own: the digest its own have names (ADR 0571 §6) - }, - GrantedAt: func(fp string) bool { - key, err := grantsstate.PartGrantKey(name, fp) - return err == nil && p.w.has(grants, key) - }, - Restored: func(node string) bool { - key, err := asks.RestoreAskKey(node, name, fp) - return err == nil && p.answeredWith(key, assign.OptionRestore) - }, - Allowed: func(node, digest string) bool { - key, err := asks.RecordAskKey(node, name, digest) - return err == nil && p.answeredWith(key, assign.OptionYes) - }} - if fp != "" { - held, _ := mwc.HeldKey(name, fp) - grant, _ := grantsstate.PartGrantKey(name, fp) - hold, _ := asks.HoldAskKey(name, fp) - noPipeline := runnerFirstStart(name, manifests, p.w.runningNow()) || manifests[name].ClaimsSeat(meshcatalog.Seat) - m.KeptBuilds = p.namedBuild(name, m.FP, trunk[name].FpSrc, noPipeline) - m.Released = p.answeredWith(hold, "release") - m.Kept = p.answeredWith(hold, "keep") - m.Built, m.Held, m.Granted = p.w.built(name, fp), p.w.has(buckets.Wants(), held) && !m.Released, p.w.has(grants, grant) - m.BuildFailed, m.BuildError = p.w.buildStanding(name, fp) - // A build whose version is released counts as done for rule 1; its build.ended counts only for a build not - // released (design 47 G A-429), so a restart that lost the end loses nothing of a released build. - if releasedBuild(m.KeptBuilds, fp) { - m.Built, m.BuildFailed, m.BuildError = true, false, "" - } - if raw := p.w.value(grants, grant); raw != nil { - if g, err := grantsstate.DecodeGrant(raw); err == nil { - m.GrantedOn = func(node string) bool { return assign.GrantNamesNode(g, node, name) } - } - } - if m.Released { - p.clearHeld(name, fp) - } - } - if served != nil { - m.Invokes, m.Served = keptInvokes(moduleGraph, name), served - } - m.Offering = offering - last := map[string]string{} - for _, node := range m.Nodes { - last[node] = p.w.lastHealthy(node, name, fp) - } - if p.outside != nil { - m.MachineHeld = p.outside.Held - nodes = p.outsideReview(ctx, name, fp, m.Nodes, nodes, manifests[name].ClaimsSeat(mcseat.Seat), p.movedTo(wanted, nodes)) - } - unread := p.orderEdgesOf(ctx, m, wantedOf[name], trunk) - actingOf, orderOf := p.actingOf(name, released), p.orderActing(name, released, unread, func() assign.Nodes { return nodes }) - m.Acting = func(node string) []assign.ActingEdge { return append(actingOf(node), orderOf(node)...) } - out := assign.Wants(m, nodes, wantedOf[name], last) - p.sayWhy(ctx, name, m.Nodes, wantWhy{Branch: g.branch, Fp: fp, Step: whyRules}, wantedOf[name], &out, &m) - p.sayParts(name, m, nodes, out, wantedOf[name]) - for _, node := range m.Nodes { - for _, prov := range m.MissingProvisions(node) { - missing[node+"."+name+"."+prov] = true - } - } - p.yeses(ctx, name, m, wantedOf[name], &out) - gatherNeither(neither, name, out) - leftNeither(left, m, wantedOf[name], out) - p.sayNoWant(name, m.Nodes, wantedOf[name], out) - p.sayUnreached(name, m.Nodes, out) - p.sayNodeFailed(name, fp, m.Nodes, nodes, out, first) - if first { - startClears(stale, m, out) - if fp != "" && !m.Held && out.Held == nil && !out.Lifted { - p.clearHeld(name, fp) - } - } - if out.Held != nil { - key, err := mwc.HeldKey(name, fp) - if err == nil { - held := *out.Held - p.put(ctx, buckets.Wants(), key, func() ([]byte, error) { return mwc.EncodeHeld(held) }) - } - } - if out.Lifted { - p.lift(ctx, name, fp) - } - if out.Held != nil || (m.Held && !out.Lifted) { - p.holdAsk(ctx, name, fp, out.Held) - p.heldCondition(ctx, name, fp, out.Held, len(out.Restore) > 0) - } - for _, node := range out.Restore { - p.restoreAsk(ctx, name, node, fp) - } - for _, need := range out.Record { - p.recordAsk(ctx, name, fp, need) - } - for node, want := range out.Wants { - p.putWant(ctx, node, name, want) - } - if len(out.Wants) > 0 { - p.ends.wrote(name, fp, branches) - } - }() - } - } -} - -// movedTo says whether the delivery moved owner to node: owner's want there names a fingerprint its have there does not yet run -// healthy (design 47 G §706, *What counts as an outside cause*: a rewritten file of a module the delivery moved is that module's). -func (p *passer) movedTo(wanted map[string]map[string]mwc.Want, n assign.Nodes) func(owner, node string) bool { - return func(owner, node string) bool { - w, ok := wanted[owner][node] - return ok && !w.Removed && !n.RunsHealthy(node, owner, w.Fp) - } -} - -// sayNoWant says in the journal why a node of module gets no want after this pass, from the rule the outcome names for it -// (issue 1945: a new assignment whose trunk build was not done was held back with nothing said), once per reason, and says -// when the node has a want again. A node with a live want, written before and kept or written in this pass, has one. -func (p *passer) sayNoWant(module string, nodes []string, before map[string]mwc.Want, out assign.Outcome) { - if p.noWant == nil { - p.noWant = map[string]string{} - } - assigned := map[string]bool{} - for _, node := range nodes { - assigned[node] = true - key := node + "." + module - w, written := out.Wants[node] - if !written { - w, written = before[node] - } - reason := "" - if h, held := out.Rules[node]; held && (!written || w.Removed) { - reason = h.Reason() - } - last, said := p.noWant[key] - switch { - case reason != "" && reason != last: - p.logf("%s on %s gets no want: %s", module, node, reason) - p.noWant[key] = reason - case reason == "" && said && written && !w.Removed: - p.logf("%s on %s has a want", module, node) - delete(p.noWant, key) - } - } - for key := range p.noWant { - if node, m, _ := strings.Cut(key, "."); m == module && !assigned[node] { - delete(p.noWant, key) // no longer assigned there: its removed want says so - } - } -} - -// sayUnreached says in the journal, once per words, that a module is wanted on a node and may not be reached there, since -// no provider of what it requires runs healthy (rule 5, which holds no want: issue 1960), and once when it is reached again. -func (p *passer) sayUnreached(module string, nodes []string, out assign.Outcome) { - if p.unreached == nil { - p.unreached = map[string]string{} - } - for _, node := range nodes { - key := node + "." + module - why, last := out.Unreached[node], p.unreached[key] - switch { - case why != "" && why != last: - p.logf("%s on %s is wanted and may not be reached: %s", module, node, why) - p.unreached[key] = why - case why == "" && last != "": - p.logf("%s on %s is reached: its providers run healthy", module, node) - delete(p.unreached, key) - } - } -} - -// sayNodeFailed raises, once per node and words, the condition of a failed health check of a module's newest fingerprint fp -// that is the node's own, since fp runs healthy elsewhere and is not held (ADR 0557 §1, issue 1496), and says it in the journal; -// it clears the condition once when the node no longer fails alone, or is no longer one of the module's nodes. The first pass -// after a start remembers nothing a previous process raised: it clears, for every module, the condition of every node the -// presence bucket knows that does not fail alone, since a node that recovered while no process watched leaves no failed have to -// find it by (a clear of a condition not open is quiet at the router). A condition the router did not take is said again at the next pass. -func (p *passer) sayNodeFailed(module, fp string, nodes []string, known assign.Nodes, out assign.Outcome, first bool) { - if p.nodeFailed == nil { - p.nodeFailed = map[string]string{} - } - look := map[string]bool{} - for _, node := range nodes { - look[node] = true - } - for key := range p.nodeFailed { - if node, m, _ := strings.Cut(key, "."); m == module { - look[node] = true - } - } - if first { - for node := range known.Online { - look[node] = true - } - } - for _, node := range slices.Sorted(maps.Keys(look)) { - key := node + "." + module - f, failing := out.NodeFailed[node] - last, said := p.nodeFailed[key] - switch { - case failing && f.Words+"|"+strings.Join(f.Others, ",") != last: - p.logf("%s on %s: the health check at %s failed here (%s), while it runs healthy on %s: the failure is %s's, so nothing "+ - "is held or set back", module, node, fp, f.Words, strings.Join(f.Others, ", "), node) - if p.say != nil { - if err := p.say(assign.NodeFailedCondition(node, module, fp, f, true)); err != nil { - p.logf("the condition of %s failing on %s alone could not be said, and is said at the next pass: %v", module, node, err) - continue - } + return target, ok + }, + Allowed: func(node, digest string) bool { + key, err := asks.RecordAskKey(node, name, digest) + return err == nil && p.answeredWith(key, assign.OptionYes) + }} + if p.outside != nil && ok { + p.outsideReview(ctx, name, target.Fp, m.Nodes, bv.nodes, bv.manifests[name].ClaimsSeat(mcseat.Seat), p.movedTo(wanted, bv.nodes)) } - p.nodeFailed[key] = f.Words + "|" + strings.Join(f.Others, ",") - case !failing && (said || first): - if said { - p.logf("%s on %s no longer fails its health check alone", module, node) + out := assign.Wants(m, wantedOf) + p.sayWhy(ctx, name, m.Nodes, wantWhy{Branch: g.branch, Fp: target.Fp, Version: target.Version, Pipeline: target.Pipeline, Step: whyStep(ok)}, wantedOf, &out) + if ok { + p.yeses(ctx, name, m, wantedOf, &out) } - if p.say != nil { - if err := p.say(assign.NodeFailedCondition(node, module, fp, assign.NodeFailure{}, false)); err != nil && said { - p.logf("the condition of %s failing on %s alone could not be cleared, and is cleared at the next pass: %v", module, node, err) - continue - } - } - delete(p.nodeFailed, key) - } - } -} - -// neitherOne is a module assigned to a node that holds no want for it after a pass, and the rule holding its first want back. -type neitherOne struct { - node, module string - holding assign.Holding -} - -// gatherNeither adds the nodes on which module is on neither list in its outcome, keyed by node and module. -func gatherNeither(into map[string]neitherOne, module string, out assign.Outcome) { - for node, h := range out.Neither { - into[node+"."+module] = neitherOne{node: node, module: module, holding: h} - } -} - -// leftNeither says, by node and module, for every node of m not on neither list after the pass, why: "" when the node has a live -// want for it (written before and kept, or written in this pass), else why it has none. sayNeither clears a neither condition in -// these words, so a clear never says a want the node does not hold (issue 1945: "nats on novox has a want again" while the bus bar -// left it with none). -func leftNeither(into map[string]string, m assign.Module, before map[string]mwc.Want, out assign.Outcome) { - for _, node := range m.Nodes { - if _, on := out.Neither[node]; on { - continue - } - w, written := out.Wants[node] - if !written { - w, written = before[node] - } - why := "" - h, ruled := out.Rules[node] - switch { - case written && !w.Removed: - case (m.Held && !out.Lifted) || out.Held != nil: - why = "it is held, and its held condition says so" - case slices.ContainsFunc(out.Record, func(r assign.RecordNeed) bool { return r.Node == node }): - why = "its move waits for the operator's yes on its record ask" - case putOn(m, node): - why = "its put waits on its own steps and the operator's answer" - case m.FP == "": - why = "the trunk has no build of it" - case ruled: - why = h.Reason() - default: - why = "the node does not run it" - } - into[node+"."+m.Name] = why - } -} - -// putOn says whether an operator's put on node owns m's want there: one still on its steps (ADR 0546), so the rules write none -// meanwhile. An ended put (done, refused, released) stays in the bucket and owns nothing. -func putOn(m assign.Module, node string) bool { - p, put := m.Puts[node] - _, owns := assign.PutOwns(p) - return put && owns -} - -// startClears adds, for the first pass after a start, every node of m that is not on neither list after the pass: a neither -// condition a previous process raised for it is cleared. That covers a node with a want and one with none that is not on neither -// list, such as a held module (issue 1945: the hold is said on its own, so a neither raised for it before the restart would stay up -// for ever) or a move waiting for its record ask. Clearing a condition that is not open is quiet at the router. -func startClears(into map[string]neitherOne, m assign.Module, out assign.Outcome) { - for _, node := range m.Nodes { - if _, on := out.Neither[node]; !on { - into[node+"."+m.Name] = neitherOne{node: node, module: m.Name} - } - } -} - -// sayNeither raises, once, the condition of every module on neither list in this pass and writes it to the journal, and clears, -// once, the condition of each one said before that has a want again (issue 1945). A say that fails is made again at the next pass. -// stale is, on the first pass after a start, the modules whose condition a previous process may have raised and that have a want: -// each is cleared once. -// left is, by node and module, why each assigned node not on neither list after this pass has no want ("" when it has one); a key -// it does not hold is no longer assigned. -func (p *passer) sayNeither(now, stale map[string]neitherOne, left map[string]string) { - if p.neither == nil { - p.neither = map[string]neitherOne{} - for key, one := range stale { - if _, on := now[key]; !on { - p.neither[key] = one + for node, want := range out.Wants { + p.putWant(ctx, node, name, want) } } } - for key, one := range now { - if _, said := p.neither[key]; said { - continue - } - c := assign.NeitherCondition(one.node, one.module, one.holding, true) - if p.say != nil { - if err := p.say(c); err != nil { - p.logf("the condition of %s on neither list on %s could not be said, and is said at the next pass: %v", one.module, one.node, err) - continue - } - } - p.logf("%s is assigned to %s and on neither list: no want, and not in the node's own composition; what holds its first want back: %s", - one.module, one.node, one.holding.Reason()) - p.neither[key] = one - } - for key, one := range p.neither { - if _, still := now[key]; still { - continue - } - why, assigned := left[key] - if !assigned { - why = "it is no longer assigned there" - } - if p.say != nil { - if err := p.say(assign.NeitherLeftCondition(one.node, one.module, why)); err != nil { - p.logf("the condition of %s on neither list on %s could not be cleared, and is cleared at the next pass: %v", one.module, one.node, err) - continue - } - } - if why == "" { - p.logf("%s on %s has a want again", one.module, one.node) - } else { - p.logf("%s on %s is no longer on neither list and has no want: %s", one.module, one.node, why) - } - delete(p.neither, key) - } } // put writes one key of the assigner's own bucket; a write that fails is said and made again at the next pass. @@ -752,60 +300,7 @@ func (p *passer) putWant(ctx context.Context, node, module string, want mwc.Want } } if p.written(ctx, buckets.Wants(), key, func() ([]byte, error) { return mwc.EncodeWant(want) }) { - p.sayWanted(node, module, fp, want.Removed) - } -} - -// holdAsk writes, once, the ask about a held fingerprint (ADR 0528 clause 6, ADR 0437 clause 10): the first ask that needs -// no proof of who answered, which the view may answer and the teller fills. In release 5 an answer changes nothing on a -// node (wants are only compared, ADR 0429 §7), and the options say so. -func (p *passer) holdAsk(ctx context.Context, module, fp string, fresh *mwc.Held) { - key, err := asks.HoldAskKey(module, fp) - if err != nil || p.w.has(buckets.Asks(), key) { - return - } - // The asking is the hold's time in seconds: a hold of the same fingerprint written again after a lift is another asking, under - // an id of its own, also after a restart, since the router drops a second ask under an id it keeps (issue 1906). - now := p.clock() - asking := int(now.Unix()) - h := p.heldHold(module, fp, fresh) - a := assign.HoldAsk(key, module, fp, h.Node, h.Words, asking, now) - p.put(ctx, buckets.Asks(), key, func() ([]byte, error) { return assign.EncodeAsk(key, a) }) -} - -// lift takes away a hold the assigner lifted by itself, since every node that runs the module runs the held fingerprint healthy -// (issue 1906): the held record and its ask (the teller then takes the ask back from the operator), and the held condition is -// cleared saying why, once. A hold of the same fingerprint written later is said and asked again. -func (p *passer) lift(ctx context.Context, module, fp string) { - held, err := mwc.HeldKey(module, fp) - if err != nil { - return - } - ask, err := asks.HoldAskKey(module, fp) - if err != nil { - return - } - c := assign.LiftedCondition(module, fp) - if !p.said[c.Key+".lifted"] { - if p.say != nil { - if err := p.say(c); err != nil { - // The hold stays written, so the next pass lifts it again and says so then. - p.logf("the condition of the lifted hold of %s could not be cleared, and is cleared at the next pass: %v", module, err) - return - } - } - p.logf("%s", assign.LiftedWords(module, fp)) - p.said[c.Key+".lifted"] = true - delete(p.said, c.Key+".raised") // a hold written again is said again - } - p.remove(ctx, buckets.Wants(), held) - p.remove(ctx, buckets.Asks(), ask) - // The restore asks of that fingerprint, on every node, go with the hold: every node runs it healthy, so no node waits to be - // set back from it (issue 1906, note 28). - for key := range p.w.read(buckets.Asks()) { - if k, err := asks.ParseAnyAskKey(key); err == nil && k.Kind == asks.AskKindRestore && k.Module == module && k.FP == fp { - p.remove(ctx, buckets.Asks(), key) - } + p.sayWanted(node, module, fp, want.Pipeline, want.Removed) } } @@ -914,29 +409,6 @@ func (p *passer) yesWritten(k asks.AskKey) bool { return err == nil && w.Yes == k.Composed } -// fingerprintsOf are the fingerprints a node may still want of module: the trunk's, the one its want names and the one its have -// names, each once. -func (p *passer) fingerprintsOf(node, module, trunkFP string) []string { - var out []string - add := func(fp string) { - if fp != "" && !slices.Contains(out, fp) { - out = append(out, fp) - } - } - add(trunkFP) - if key, err := mwc.WantKey(node, module); err == nil { - if w, err := mwc.DecodeWant(p.w.value(buckets.Wants(), key)); err == nil { - add(w.Fp) - } - } - if key, err := nodeengineclient.HaveKey(node, module); err == nil { - if h, err := nodeengineclient.DecodeHave(p.w.value(buckets.Have, key)); err == nil { - add(h.Fp) - } - } - return out -} - // staleAsks takes away, at every pass, each hold, restore, record and backup ask that staleAsk names (issue 1906, note 28): an ask // outlived its fingerprint, and the teller takes its router ask back from the operator when the key goes. With the trunk not // read at all (no module), nothing is taken away. @@ -949,10 +421,6 @@ func (p *passer) staleAsks(ctx context.Context, trunk map[string]meshcatalog.Mod // The condition is cleared first and the ask goes only once that was said: the ask is what brings this key back at the // next pass, so a clear that failed once is made again, with no state of its own (issue 1906, note 29). k, kerr := asks.ParseAnyAskKey(key) - held := kerr == nil && (k.Kind == asks.AskKindHold || k.Kind == asks.AskKindRestore) - if held && !p.clearHeld(k.Module, k.FP) { - continue - } // A backup ask's condition is cleared with it, raised again if a move to that fingerprint is backed up again. if kerr == nil && k.Kind == asks.AskKindBackup && p.say != nil { if err := p.say(assign.BackupCondition(k.Node, k.Module, k.FP, "", false)); err != nil { @@ -967,12 +435,6 @@ func (p *passer) staleAsks(ctx context.Context, trunk map[string]meshcatalog.Mod } p.logf("the ask %s is about a fingerprint or composition the trunk has left behind and is taken away: %s", key, why) p.remove(ctx, buckets.Asks(), key) - // The held record stays: it is the memory that this fingerprint failed (ADR 0408 §4), and a revert returns the trunk to - // exactly it. Only the condition, which would else ask for an answer that can no longer be given, is cleared, and it is - // raised again if the trunk returns to the fingerprint. - if held { - delete(p.said, assign.HeldCondition(k.Module, k.FP, "", "", false, false).Key+".raised") - } } } } @@ -990,48 +452,6 @@ func (p *passer) remove(ctx context.Context, bucket, key string) { p.w.put(bucket, key, nil) } -// heldHold is the hold of fp as written: the node whose health check failed and its words. -func (p *passer) heldHold(module, fp string, fresh *mwc.Held) mwc.Held { - if fresh != nil { - return *fresh - } - key, err := mwc.HeldKey(module, fp) - if err != nil { - return mwc.Held{} - } - h, _ := mwc.DecodeHeld(p.w.read(buckets.Wants())[key]) - return h -} - -// heldCondition raises, once, the condition for a fingerprint the assigner holds (ADR 0429 §6, ADR 0539 §19): the module, the -// node whose health check failed and the check's words. A module holding data needs the operator's answer to its restore ask. -func (p *passer) heldCondition(ctx context.Context, module, fp string, fresh *mwc.Held, data bool) { - h := p.heldHold(module, fp, fresh) - c := assign.HeldCondition(module, fp, h.Node, h.Words, true, data) - if p.say == nil || p.said[c.Key+".raised"] { - return - } - if err := p.say(c); err != nil { - p.logf("the condition of the held %s could not be said, and is said at the next pass: %v", module, err) - return - } - p.said[c.Key+".raised"] = true - delete(p.said, c.Key+".lifted") // a hold raised again and lifted again is said lifted again - delete(p.said, c.Key+".cleared") // and released again is cleared again -} - -// restoreAsk writes, once, the ask about a module holding data whose held fingerprint failed its health check on node. -func (p *passer) restoreAsk(ctx context.Context, module, node, fp string) { - key, err := asks.RestoreAskKey(node, module, fp) - if err != nil || p.w.has(buckets.Asks(), key) { - return - } - words := p.heldHold(module, fp, nil).Words - now := p.clock() - a := assign.RestoreAsk(key, node, module, fp, words, assign.AskingSeconds(now), now) - p.put(ctx, buckets.Asks(), key, func() ([]byte, error) { return assign.EncodeAsk(key, a) }) -} - // answeredWith says whether the teller wrote an answer to the ask at key as it stands now and the option chosen is option. The // answer is the operator's, given in the view or on the phone, and stays under its key (ADR 0539 §14, §16, §19); it counts only // for the asking it answered (its warrant names that asking's id), so an answer to an ask taken away, or to an earlier hold of @@ -1049,21 +469,6 @@ func (p *passer) answeredWith(key, option string) bool { return id == ask.ID } -// clearHeld clears, once, the condition of a hold the operator released. -// It says whether nothing is left to say: the clear was said now or before, or there is no router to say it to. -func (p *passer) clearHeld(module, fp string) bool { - c := assign.HeldCondition(module, fp, "", "", false, false) - if p.say == nil || p.said[c.Key+".cleared"] { - return true - } - if err := p.say(c); err != nil { - p.logf("the condition of the released %s could not be cleared, and is cleared at the next pass: %v", module, err) - return false - } - p.said[c.Key+".cleared"] = true - return true -} - // recordAsk writes, once, the ask about a move of a module of policy record that waits for the operator's yes. func (p *passer) recordAsk(ctx context.Context, module, fp string, need assign.RecordNeed) { key, err := asks.RecordAskKey(need.Node, module, need.Digest) @@ -1111,7 +516,7 @@ func (p *passer) backups(ctx context.Context, reverting map[string]bool) { continue } current, currentAsk, there := assign.DecodeAsk(akey, p.w.read(buckets.Asks())[akey]) - if there != nil || assign.BackupAskDigest(current) != assign.ShortDigest(want.Composed) { + if there != nil { after := -1 if there == nil { after = current.Asking @@ -1146,7 +551,7 @@ func (p *passer) backups(ctx context.Context, reverting map[string]bool) { // backupAsk writes the backup ask at its asking and raises the condition of the failed backup, once. func (p *passer) backupAsk(ctx context.Context, key, node, module string, want mwc.Want, words string, asking int) { - a := assign.BackupAsk(key, node, module, want.Fp, want.Composed, words, asking, time.Now()) + a := assign.BackupAsk(key, node, module, want.Fp, words, asking, time.Now()) p.put(ctx, buckets.Asks(), key, func() ([]byte, error) { return assign.EncodeAsk(key, a) }) c := assign.BackupCondition(node, module, want.Fp, words, true) if p.say == nil || p.said[c.Key] { @@ -1196,13 +601,11 @@ func (p *passer) yeses(ctx context.Context, module string, m assign.Module, befo p.recordAsk(ctx, module, w.Fp, assign.RecordNeed{Node: node, Digest: h.Waits, Judgement: h.Judgement, Words: h.Detail}) } } - // A want carried over from before the node composed its own may still name the controller's composition; a placed - // node's want names the build alone (ADR 0547 §2). if yes == "" { release = "" } - if yes != w.Yes || release != w.Release || w.Composed != "" || !w.Self { - w.Yes, w.Release, w.Composed, w.Self = yes, release, "", true + if yes != w.Yes || release != w.Release || !w.Self { + w.Yes, w.Release, w.Self = yes, release, true out.Wants[node] = w } } @@ -1216,139 +619,6 @@ func chosenOf(chosen map[string]catalogue.PersonPolicy, module string) *catalogu return nil } -// provisionsOf answers, per module, the provisions it consumes on a node, by key (ADR 0570 §4), from the seat -// mesh-provisioner's state; nil while the state is not read (the seat not delivered yet), so no want waits on it. A failed -// provision is also said as the assigner's finding `provider..provision-failed` (a waiting one is the watchman's -// needs-operator), deleted once it is no longer failed. -func (p *passer) provisionsOf() func(module string) func(node string) map[string]meshprovisionerclient.Provision { - if p.kvs[buckets.Provisions] == nil { - return func(string) func(string) map[string]meshprovisionerclient.Provision { return nil } - } - by := map[string]map[string]map[string]meshprovisionerclient.Provision{} // module, node, key - failed := map[string]meshprovisionerclient.Provision{} - for key, raw := range p.w.read(buckets.Provisions) { - pr, err := meshprovisionerclient.Decode(raw) - if err != nil { - continue - } - node, module := pr.Consumer.Node, pr.Consumer.Module - if by[module] == nil { - by[module] = map[string]map[string]meshprovisionerclient.Provision{} - } - if by[module][node] == nil { - by[module][node] = map[string]meshprovisionerclient.Provision{} - } - by[module][node][key] = pr - if pr.State == meshprovisionerclient.Failed { - failed[key] = pr - } - } - p.provisionFindings(failed) - return func(module string) func(string) map[string]meshprovisionerclient.Provision { - return func(node string) map[string]meshprovisionerclient.Provision { return by[module][node] } - } -} - -// provisionFindings writes the finding of each failed provision and deletes the ones no longer failed. -func (p *passer) provisionFindings(failed map[string]meshprovisionerclient.Provision) { - if p.findings.Put == nil { - return - } - now := time.Now - if p.now != nil { - now = p.now - } - for key, pr := range failed { - if p.failedProvisions[key] { - continue - } - f, err := assign.ProvisionFailedFinding(key, pr, now()) - if err == nil { - err = p.findings.Put(f) - } - if err != nil { - p.logf("the failed provision %s is not said: %v", key, err) - continue - } - if p.failedProvisions == nil { - p.failedProvisions = map[string]bool{} - } - p.failedProvisions[key] = true - } - for key := range p.failedProvisions { - if _, still := failed[key]; still { - continue - } - if err := p.findings.Delete(assign.ProvisionFailedKey(key)); err != nil { - p.logf("the failed provision %s is not cleared: %v", key, err) - continue - } - delete(p.failedProvisions, key) - } -} - -// MissingBound is how long a required provision may have no key before it is said: a module's delivery bound, the default -// when its manifest names none (ADR 0564 clause 13 (d)). -var MissingBound = motion.DefaultDeliveryBound - -// missingPulse is the pulse key of a provision with no key, `provision-missing...`. -func missingPulse(key string) string { return "provision-missing." + key } - -// provisionsMissing is the loud end of a required provision with no key (the lead, 2026-10-29, ADR 0570 §4): each one is -// pulsed with the bound when it is first seen; once its pulse expired and it still has no key, the finding -// `provider....provision-missing` is written; one keyed again ends both. The pulse's expiry, heard on -// the watch, is the pass that writes it: no timer. -func (p *passer) provisionsMissing(ctx context.Context, missing map[string]bool) { - pulses := p.kvs[plc.PulsesBucket()] - if p.missing == nil { - p.missing = map[string]bool{} - } - for key := range missing { - switch { - case !p.missing[key]: - p.missing[key] = true - if pulses != nil { - p.pulse(ctx, missingPulse(key), MissingBound) - } - case pulses == nil || !p.pulsing(missingPulse(key)): - if p.missingSaid[key] || p.findings.Put == nil { - continue - } - f, err := assign.ProvisionMissingFinding(key, MissingBound, p.clock()) - if err == nil { - err = p.findings.Put(f) - } - if err != nil { - p.logf("the missing provision %s is not said: %v", key, err) - continue - } - if p.missingSaid == nil { - p.missingSaid = map[string]bool{} - } - p.missingSaid[key] = true - } - } - for key := range p.missing { - if missing[key] { - continue - } - delete(p.missing, key) - if pulses != nil && p.pulsing(missingPulse(key)) { - if err := pulses.Delete(ctx, missingPulse(key)); err == nil { - p.w.put(plc.PulsesBucket(), missingPulse(key), nil) - } - } - if p.missingSaid[key] && p.findings.Delete != nil { - if err := p.findings.Delete(assign.ProvisionMissingKey(key)); err != nil { - p.logf("the missing provision %s is not cleared: %v", key, err) - p.missing[key] = true - continue - } - delete(p.missingSaid, key) - } - } -} - // unplacedFindings writes, from the lease write, the finding `machine..unplaced` for each node that runs but no // assignments names, which gets no want (ADR 0571 §6), and deletes it once the node is placed. func (p *passer) unplacedFindings() { @@ -1443,40 +713,6 @@ func (p *passer) seatHolders(ctx context.Context, manifests map[string]catalogue } } -// offered says whether some manifest of the trunk, or a module the mesh provides itself, offers a provision under its name -// (a module offers its own name too, catalogue.Manifest.Offers): what a requirement nobody offers is told from. -func offered(manifests map[string]catalogue.Manifest) func(string) bool { - names := map[string]bool{} - add := func(m catalogue.Manifest) { - for _, o := range m.Offers() { - names[o] = true - } - } - for _, m := range manifests { - add(m) - } - for _, module := range overlay.ProvidedModules() { - if m, _, err := overlay.ProvidedManifest(module); err == nil { - add(m) - } - } - return func(provision string) bool { return names[provision] } -} - -// answering is, by provision, the manifests of the modules placed on some node that provide it: what a want's -// provisions are judged by (catalogue.Awaited, issue 774). A provider in the catalogue placed nowhere answers nobody. -func answering(manifests map[string]catalogue.Manifest, n assign.Nodes) func(string) []catalogue.Manifest { - return func(provision string) []catalogue.Manifest { - var out []catalogue.Manifest - for _, name := range slices.Sorted(maps.Keys(n.Providers[provision])) { - if m, ok := manifests[name]; ok && len(n.Providers[provision][name]) > 0 { - out = append(out, m) - } - } - return out - } -} - // branchView is what one pass reads of the catalog half of one watched branch (design 47 G §184, §195): its modules, their // manifests, the nodes the rules read through them, the module graph and the two readings of what runs. type branchView struct { @@ -1556,91 +792,49 @@ func wantedHere(wanted map[string]mwc.Want, own []string, groups []group, first return out } -// noPipelineRules is why each node of module gets no want while no pipeline passed at fp on its branches (design 47 G §29): said as -// any rule's reason (sayNoWant), once. -func noPipelineRules(nodes []string, module, fp string, branches []string) map[string]assign.Holding { - out := map[string]assign.Holding{} - for _, node := range nodes { - out[node] = assign.Holding{Rule: mwc.RuleNotHeld, Words: fmt.Sprintf("no pipeline of %s passed at %s on %s, and a want follows a passed pipeline (design 47 G §29)", - module, shortHex(fp), strings.Join(branches, ", "))} +// movedTo says whether the delivery moved owner to node: owner's want there names a fingerprint its have there does not yet run +// healthy (design 47 G §706, *What counts as an outside cause*: a rewritten file of a module the delivery moved is that module's). +func (p *passer) movedTo(wanted map[string]map[string]mwc.Want, n assign.Nodes) func(owner, node string) bool { + return func(owner, node string) bool { + w, ok := wanted[owner][node] + return ok && !w.Removed && !n.RunsHealthy(node, owner, w.Fp) + } +} + +// placementBuilds is, by node and module, the build a placement names (the placement's value `build`, design 47 H §750, H-A-39): +// a return to an earlier build. +func placementBuilds(assignments map[string]mma.Assignments) map[string]map[string]string { + out := map[string]map[string]string{} + for node, a := range assignments { + for _, m := range a.Modules { + if m.Build == "" { + continue + } + if out[node] == nil { + out[node] = map[string]string{} + } + out[node][m.Module] = m.Build + } } return out } -// runnerFirstStart says whether module claims the seat mesh-pipeline-runner while no module claiming it runs on any node (design 47 -// G A-428): read from the node-engine seat's running statements the assigner hears, each module's claims from its manifest. With -// no running heard yet it says false: the gate stands until the assigner knows. -func runnerFirstStart(module string, manifests map[string]catalogue.Manifest, running map[string]map[string]nodeengine.Running) bool { - if !manifests[module].ClaimsSeat(mpr.Seat) || running == nil { - return false +// buildTarget is the want of module on node whose placement names build (design 47 H §750, H-A-5, H-A-99): that build, named by +// the version released at it, with no pipeline and no rank (H-A-37). The version is the kept end's when it is that build, else +// the version whose release names the parts the build made; with none known no want is written, said once per build. +func (p *passer) buildTarget(module, branch, node, build string) (assign.Target, bool) { + version := "" + if end, ok := p.ends.endOf(module, branch); p.ends != nil && ok && end.Fp == build { + version = end.Version + } else if k, ok := p.w.released.withParts(module, p.w.ends.madeOf(module, build)); ok { + version = k.Version } - for _, mods := range running { - for m, r := range mods { - if !r.Stopped && r.Fingerprint != "" && manifests[m].ClaimsSeat(mpr.Seat) { - return false - } + if version == "" { + if key := "build-unreleased." + node + "." + module + "." + build; !p.said[key] { + p.said[key] = true + p.logf("the placement of %s on %s names build %s, at which no released version is known; its want is written once one is", module, node, shortFP(build)) } + return assign.Target{}, false } - return true -} - -// releasedBuild says whether the build at fp is a released version's, as a want of it names it (namedBuild): such a build counts as -// done for rule 1 whatever the builder's build.ended said or the assigner kept of it (design 47 G A-429). -func releasedBuild(named func(fp string) (assign.KeptBuild, bool), fp string) bool { - if named == nil { - return false - } - k, ok := named(fp) - return ok && k.Version != "" -} - -// sayParts says once per node, module and reason why a want the pass writes or keeps at the module's fingerprint names no parts -// (design 47 G §379, §525, §526): whether the node's engine reads a want's parts (its record's capabilities, its manifest reads) -// and whether the build has a release or a kept build the want could name. -func (p *passer) sayParts(name string, m assign.Module, n assign.Nodes, out assign.Outcome, before map[string]mwc.Want) { - for _, node := range m.Nodes { - w, ok := out.Wants[node] - if !ok { - w, ok = before[node] // kept as it stands this pass - } - if !ok || w.Removed || w.Fp == "" || len(w.Parts) > 0 || w.Version != "" { - continue - } - reads := n.ReadsParts(node) - var k assign.KeptBuild - found := false - if m.KeptBuilds != nil { - k, found = m.KeptBuilds(w.Fp) - } - why := fmt.Sprintf("its engine reads parts: %v (capabilities %v); a release or kept build of %s found: %v", reads, n.Capable[node], shortHex(w.Fp), found) - if found && len(k.Parts) == 0 { - why += ", naming no parts" - } - key := "parts " + node + "." + name + "@" + w.Fp + ": " + why - if p.skipped[key] { - continue - } - if p.skipped == nil { - p.skipped = map[string]bool{} - } - p.skipped[key] = true - p.logf("the want of %s on %s names no version and no parts: %s", name, node, why) - } -} - -// nameHeldBuilds rewrites, for a module the trunk's newer fingerprint of which passed no pipeline, each want held at an older build -// that names no version: with the release whose parts are exactly what that build made, and its parts where the node's engine reads -// them, everything else of the want as it stands (novox 2026-11-16: mesh-hand-acts held at 4eb3cf48, released 0.1.13, after the -// mesh-sdk merge moved its fingerprint). No want is written for the unpassed fingerprint, nor for a build with no release. -func (p *passer) nameHeldBuilds(ctx context.Context, name, trunkFP string, group []string, n assign.Nodes, held map[string]mwc.Want, named func(fp string) (assign.KeptBuild, bool)) { - for _, node := range group { - w, ok := held[node] - if !ok || w.Removed || w.Fp == "" || w.Fp == trunkFP || w.Version != "" { - continue - } - if k, found := named(w.Fp); !found || k.Version == "" { - continue - } - p.putWant(ctx, node, name, n.NameKept(assign.Module{KeptBuilds: named}, node, w)) - } + return assign.Target{Fp: build, Version: version}, true } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go index 76e97147..cfed17a9 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go @@ -6,8 +6,7 @@ import ( "encoding/json" "errors" "fmt" - stdio "git.novox.be/novox/mesh-sdk/go" - "maps" + mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" "mesh-delivery-assigner/internal/buckets" "mesh-delivery-assigner/internal/puts" "os" @@ -19,11 +18,12 @@ import ( "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-teller/tellerevents" + mwseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" "github.com/nats-io/nats.go/jetstream" @@ -32,13 +32,10 @@ import ( mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "git.novox.be/novox/mesh-sdk/go/busretry" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient/enginereads" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" "mesh-delivery-assigner/internal/assign" plc "mesh-delivery-assigner/internal/assignment" @@ -105,166 +102,13 @@ func composes(t *testing.T, w *world, node, digest string) { var fpTest = meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64) -// 1906/H2: given a hold lifted by itself, when the assigner is started again and the same fingerprint is held again, then the -// hold ask is written under another asking, with an id of its own, so the teller raises it (the router drops a second ask under -// an id it keeps). -func TestAHoldAskedAgainAfterALiftAndARestartHasANewID(t *testing.T) { - asksKV, wantsKV := &memKV{values: map[string][]byte{}}, &memKV{values: map[string][]byte{}} - kvs := kvSet{buckets.Asks(): asksKV, buckets.Wants(): wantsKV} - key, _ := asks.HoldAskKey("web", fpTest) - held := &mwc.Held{Node: "ace", Words: "health failed"} - first := newPasser(kvs, time.Date(2026, 10, 25, 1, 0, 0, 0, time.UTC)) - first.holdAsk(context.Background(), "web", fpTest, held) - a1, err := decodeAsk(key, asksKV.values[key]) - if err != nil { - t.Fatal(err) - } - first.lift(context.Background(), "web", fpTest) - if _, there := asksKV.values[key]; there { - t.Fatal("the lift left the hold ask") - } - again := newPasser(kvs, time.Date(2026, 10, 25, 2, 0, 0, 0, time.UTC)) // started again: it remembers nothing - again.holdAsk(context.Background(), "web", fpTest, held) - a2, err := decodeAsk(key, asksKV.values[key]) - if err != nil { - t.Fatal(err) - } - if a2.Ask.ID == a1.Ask.ID { - t.Errorf("the ask written again after a lift and a restart has the id %s of the first", a1.Ask.ID) - } - // Issue 1966, ADR 0668 §5: assigner_ask_again writes the hold again under a new asking and records the hand act; the - // asking the operator answered is refused, naming the answer; a proposal's authorising ask is refused by its rule. - answersKV := &memKV{values: map[string][]byte{}} - st := memStore{asksState: asksKV, answersState: answersKV} - var recorded []string - record := func(key string, body any) (json.RawMessage, error) { - raw, _ := json.Marshal(body) - recorded = append(recorded, key+" "+string(raw)) - return json.RawMessage(`{}`), nil - } - now := time.Date(2026, 10, 25, 3, 0, 0, 0, time.UTC) - out, err := askAgainByHand(st, record, key, "the channel lost it", "lead@shanks", now) - a3, derr := decodeAsk(key, asksKV.values[key]) - if err != nil || derr != nil || a3.Ask.ID == a2.Ask.ID || len(recorded) != 1 || !strings.Contains(recorded[0], askAgainTool+" "+key) { - t.Fatalf("ask again: %v, %v; asked %s (was %s), recorded %v", out, err, a3.Ask.ID, a2.Ask.ID, recorded) - } - answersKV.values[key], _ = mdc.EncodeAnswer(mdc.Answer{Option: "keep", Warrant: a3.Ask.ID + "@2026-10-25T03:01:00Z", Words: "the operator"}) - if _, err := askAgainByHand(st, record, key, "again", "lead@shanks", now); err == nil || !strings.Contains(err.Error(), "keep") { - t.Errorf("an answered asking is asked again: %v", err) - } - // Issue 1966 note 165 (AC165-2, AC165-3): the teller's ask-ended for a backup ask whose want names another fingerprint writes - // nothing and the journal says it is stale; the same ask with its want at the same fingerprint in another composition, or no - // want yet, is written again. - backKey, _ := asks.BackupAskKey("ace", "web", fpTest) - fpOther := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64) - for _, c := range []struct { - name string - want *mwc.Want - have *nodeengine.HaveByNodeModuleValue // A-453: the module's have on the node - stale bool - }{ - {"its want names another fingerprint", &mwc.Want{Fp: fpOther, Self: true}, nil, true}, - {"its want is removed", &mwc.Want{Removed: true}, nil, true}, - {"its want names the fingerprint in another composition", &mwc.Want{Fp: fpTest, Composed: "sha256:" + strings.Repeat("e", 64)}, nil, false}, - {"no want yet", nil, nil, false}, - // A-453: only a held have with no wait is a failed backup; a hold that carries its wait, or a have no longer held, takes the ask away. - {"its have is held with no wait", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseHeld, Health: nodeengineclient.HealthWaits}, false}, - {"its have is held with a wait", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseHeld, Health: nodeengineclient.HealthWaits, - Wait: &nodeengine.Wait{Step: nodeengine.WaitStepCompose, Reason: "entry-missing", Since: now}}, true}, - {"its have runs", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}, true}, - {"its have backs up again (A-453a)", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseBackingUp, Health: nodeengineclient.HealthWaits}, false}, - {"its have is held, naming the build it keeps running", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpOther, Phase: nodeengineclient.PhaseHeld, Health: nodeengineclient.HealthWaits}, false}, - {"its have applies at another fingerprint", &mwc.Want{Fp: fpTest, Self: true}, &nodeengine.HaveByNodeModuleValue{Fp: fpOther, Phase: nodeengineclient.PhaseApplying, Health: nodeengineclient.HealthWaits}, true}, - } { - t.Run("ended backup ask: "+c.name, func(t *testing.T) { - bkv := &memKV{values: map[string][]byte{}} - pb := newPasser(kvSet{buckets.Asks(): bkv, buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.Have: &memKV{values: map[string][]byte{}}}, now) - ba := assign.BackupAsk(backKey, "ace", "web", fpTest, "sha256:"+strings.Repeat("d", 64), "w", 2, now) - bv, err := assign.EncodeAsk(backKey, ba) - if err != nil { - t.Fatal(err) - } - bkv.values[backKey] = bv - if c.want != nil { - wkey, _ := mwc.WantKey("ace", "web") - raw, _ := mwc.EncodeWant(*c.want) - pb.w.put(buckets.Wants(), wkey, raw) - } - if c.have != nil { - hkey, _ := nodeengineclient.HaveKey("ace", "web") - raw, err := nodeengineclient.EncodeHave(*c.have) - if err != nil { - t.Fatal(err) - } - pb.w.put(buckets.Have, hkey, raw) - } - _, bask, _ := assign.DecodeAsk(backKey, bv) - body, err := tellerevents.EncodeEnded(tellerevents.Ended{Key: backKey, Ask: bask.ID, Outcome: occ.OutcomeExpired}) - if err != nil { - t.Fatal(err) - } - var said []string - stale := func(key string) string { - return pb.staleAsk(key, map[string]meshcatalog.ModulesByModuleValue{"web": {Fp2: fpOther}}) - } - if err := askAgain(context.Background(), bkv, nil, stale, body, now.Add(time.Hour), func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }); err != nil { - t.Fatal(err) - } - written := !bytes.Equal(bkv.values[backKey], bv) - if written == c.stale || (c.stale && !strings.Contains(strings.Join(said, "\n"), "stale")) { - t.Errorf("written again = %v (stale %v), journal %q", written, c.stale, said) - } - }) - } - for _, c := range []struct{ key, by, why string }{{key, stdio.Unproven, "a caller"}, {"hold.none", "lead@shanks", "no ask"}} { - if _, err := askAgainByHand(st, record, c.key, "w", c.by, now); err == nil { - t.Errorf("%s: asked again", c.why) - } - } - // ADR 0668 §3 (review round 1): the bind's read of the operator channel's asks keeps its wait through transport faults; - // past five minutes with the channel's machine heard it is the finding call-unanswered, cleared once answered; with - // the machine away none is written. A refusal is an answer and ends the wait. - for _, heard := range []bool{false, true} { - clock, findings := now, map[string][]byte{} - cw := callWait{heard: func() bool { return heard }, now: func() time.Time { return clock }, - findings: assign.Findings{Put: putInto(findings), - Delete: func(k string) error { delete(findings, k); return nil }}} - tries, during := 0, 0 - answer, err := cw.keep(context.Background(), asksCallID, channelAsksVerb, func() (json.RawMessage, error) { - tries++ - clock = clock.Add(2 * time.Minute) - if tries == 5 { - during = len(findings) - return json.RawMessage(`{"asks":[]}`), nil - } - return nil, errors.New("nats: no responders available for request") - }, t.Logf) - if want := map[bool]int{false: 0, true: 1}[heard]; err != nil || string(answer) != `{"asks":[]}` || tries != 5 || during != want || len(findings) != 0 { - t.Errorf("heard %v: %s %v after %d tries; findings during %d, after %d", heard, answer, err, tries, during, len(findings)) - } - } - if _, err := (callWait{}).keep(context.Background(), asksCallID, channelAsksVerb, func() (json.RawMessage, error) { - return nil, &busretry.Answered{Words: "refused"} - }, t.Logf); err == nil { - t.Error("a refusal was asked again") - } -} - -// 1906/H4: an answer counts only for the asking it answered: an answer to an earlier hold of the same fingerprint, or to an ask -// taken away, is not taken for the hold that stands now. +// 1906/H4: an answer counts only for the asking it answered: an answer to an earlier asking of the same ask, or to an ask +// taken away, is not taken for the asking that stands now. func TestAnAnswerCountsOnlyForTheAskingItAnswered(t *testing.T) { - // ADR 0603 §5: a provision is offered by a manifest's provides, by a module's own name, and by a module the mesh provides - // itself (mesh-addressing, mesh-wireguard's); nothing else. - off := offered(map[string]catalogue.Manifest{"node-tools": {Module: "node-tools"}, "db": {Module: "db", Provides: []catalogue.Offer{{Name: "database"}}}}) - for provision, want := range map[string]bool{"node-tools": true, "database": true, "mesh-addressing": true, "queue": false} { - if off(provision) != want { - t.Errorf("offered(%s) = %v, want %v", provision, !want, want) - } - } - key, _ := asks.HoldAskKey("web", fpTest) + key, _ := asks.BackupAskKey("ace", "web", fpTest) p := newPasser(kvSet{}, time.Now()) put := func(asking int) string { - a := assign.HoldAsk(key, "web", fpTest, "ace", "health failed", asking, time.Now()) + a := assign.BackupAsk(key, "ace", "web", fpTest, "disk full", asking, time.Now()) v, err := assign.EncodeAsk(key, a) if err != nil { t.Fatal(err) @@ -273,148 +117,102 @@ func TestAnAnswerCountsOnlyForTheAskingItAnswered(t *testing.T) { return mustAskID(a) } first := put(100) - v, _ := mdc.EncodeAnswer(mdc.Answer{Option: "release", Warrant: first + "@2026-10-25T01:00:00Z", Words: "the operator"}) + v, _ := mdc.EncodeAnswer(mdc.Answer{Option: assign.OptionBackupSkip, Warrant: first + "@2026-10-25T01:00:00Z", Words: "the operator"}) p.w.put(buckets.Answers, key, v) - if !p.answeredWith(key, "release") { + if !p.answeredWith(key, assign.OptionBackupSkip) { t.Fatal("the answer to the ask that stands is not taken") } - if p.answeredWith(key, "keep") { + if p.answeredWith(key, assign.OptionBackupAgain) { t.Error("an answer was taken for an option not chosen") } - put(200) // the same fingerprint held again, after a lift - if p.answeredWith(key, "release") { - t.Error("the answer to an earlier hold carried over to a later one") + put(200) // the same backup asked again + if p.answeredWith(key, assign.OptionBackupSkip) { + t.Error("the answer to an earlier asking carried over to a later one") } p.w.put(buckets.Asks(), key, nil) // the ask taken away - if p.answeredWith(key, "release") { + if p.answeredWith(key, assign.OptionBackupSkip) { t.Error("an answer to an ask taken away was taken") } t.Run("a put of the bus acts on the operator's warrant alone", putOfTheBus) - t.Run("a placement's build is put held, and released when the placement names it no more", aPlacementsBuildIsPut) - // ADR 0614 §4: the rules read what each node's engine states it reads from its record in `.node`; a node with no - // record, or one that states nothing, is read as stating nothing (the rules then read the baseline). - t.Run("the rules read what each node's engine reads", func(t *testing.T) { + t.Run("a placement's build names the want's version", aPlacementsBuildNamesTheWantsVersion) + // Design 47 H-A-104: what each node's engine reads is the assigner's own state engine-reads, key the node, value its + // words and when it said them, written when it hears manifest-reads-changed: no verb is asked, no node's record is read + // in its place, and a restart reads the state back with nothing lost. + t.Run("the rules read the state engine-reads that manifest-reads-changed writes", func(t *testing.T) { w := newWorld() - for node, reads := range map[string][]string{"ace": {"word:module", "word:tool-runner"}, "g14": nil} { - raw, err := nodeengineclient.EncodeHaveNode(nodeengine.HaveByNodeNodeValue{Record: &nodeengine.NodeRecord{Node: node, ManifestReads: reads}}) - if err != nil { + raw, err := nodeengineclient.EncodeHaveNode(nodeengine.HaveByNodeNodeValue{Record: &nodeengine.NodeRecord{Node: "g14", ManifestReads: []string{nodeengine.ManifestReadWordWantVersion}}}) + if err != nil { + t.Fatal(err) + } + key, _ := nodeengineclient.HaveNodeKey("g14") + w.put(buckets.Have, key, raw) + state := map[string][]byte{} + failing, woken := false, 0 + kept := &engineReadsKept{changed: func() { woken++ }, put: func(node string, v []byte) error { + if failing { + return errors.New("the bucket is full") + } + state[node] = v + return nil + }} + say := func(node, at string, words ...string) *readsMsg { + body, _ := json.Marshal(nodeengine.ManifestReadsChangedEvent{V: 1, Node: node, Why: enginereads.ReadsStarted, At: at, ManifestReads: words}) + m := &readsMsg{subject: enginereads.SubjectPrefix + node, data: body} + readsChangedHandler(kept, t.Logf)(m) + return m + } + if m := say("ace", "2026-11-20T03:00:00Z", nodeengine.ManifestReadWordWantVersion, "seat:a"); !m.acked || m.naked { + t.Errorf("the event written was acked %v, naked %v", m.acked, m.naked) + } + var v engineWords + if err := json.Unmarshal(state["ace"], &v); err != nil || !slices.Equal(v.Words, []string{nodeengine.ManifestReadWordWantVersion, "seat:a"}) || + !v.At.Equal(time.Date(2026, 11, 20, 3, 0, 0, 0, time.UTC)) || woken != 1 { + t.Errorf("the state holds %s (%v), woken %d", state["ace"], err, woken) + } + p := &passer{w: w, reads: kept} + n := w.nodes(nil, nil) + p.engineReads(n) + if !slices.Equal(n.Reads["ace"], []string{nodeengine.ManifestReadWordWantVersion, "seat:a"}) || n.Reads["g14"] != nil { + t.Errorf("the rules read %v: g14's record is no statement of what its engine reads", n.Reads) + } + // A restart: the state's watch hands every key back; the rules read the same, and the assigner's own write changes nothing. + again := &engineReadsKept{} + for node, value := range state { + if err := again.take(node, value, false); err != nil { t.Fatal(err) } - key, _ := nodeengineclient.HaveNodeKey(node) - w.put(buckets.Have, key, raw) } - n := w.nodes(nil, nil) - if !slices.Equal(n.Reads["ace"], []string{"word:module", "word:tool-runner"}) || len(n.Reads["g14"]) != 0 || len(n.Reads["novox"]) != 0 { - t.Errorf("the nodes' reads are %v", n.Reads) - } - // ADR 0618 §3: with the verb asked, a node's reads are what its engine answered; where the engine answers it does not - // serve the verb, its record is read once in its place (the fallback, issue 774); a node asked again when it comes - // back online, since an engine built before the event says none. - asked := map[string]int{} - call := func(node, key string, _ any) (json.RawMessage, error) { - asked[node]++ - if node == "g14" || key != enginereads.Invoke { - return nil, &busretry.Answered{Words: "the seat node-engine serves no verb manifest-reads"} - } - return json.Marshal(nodeengine.ManifestReadsAnswer{V: 1, Node: node, ManifestReads: []string{"form:new"}, StatedAt: time.Now().Format(time.RFC3339Nano)}) - } - p := &passer{w: w, reads: &enginereads.Keeper{Call: call, Fallback: w.recordReads, Sync: true}} - on := false - w.heardPresence("ace", !on, time.Now()) - n = w.nodes(nil, nil) - p.engineReads(n) - if !slices.Equal(n.Reads["ace"], []string{"form:new"}) || len(n.Reads["g14"]) != 0 || asked["ace"] != 1 { - t.Errorf("with the verb the nodes' reads are %v (asked %v)", n.Reads, asked) - } - if s := p.reads.Statements(); s["ace"].From != enginereads.FromVerb || s["g14"].From != enginereads.FromRecord { - t.Errorf("the statements are %+v", s) - } - p.engineReads(w.nodes(nil, nil)) - on = true - w.heardPresence("ace", !on, time.Now()) - p.engineReads(w.nodes(nil, nil)) - if asked["ace"] != 2 { - t.Errorf("ace, back online, was asked %d times, want 2", asked["ace"]) - } - // Issue 1975: ace's engine answered its bootstrapped seats, then stated the full list and said so; an answer older - // than that event does not stand: the rules read what the event says, and the node is asked until it is as new. - // The engine's verb keeps answering the bootstrapped statement (its stated-at is older than the event); the event is - // handed to the assigner's own handler (readsChangedHandler, what hearReadsChanged registers) as the bus delivers it. - bootstrapped := func(node, _ string, _ any) (json.RawMessage, error) { - return json.Marshal(nodeengine.ManifestReadsAnswer{V: 1, Node: node, ManifestReads: []string{"seat:a"}, StatedAt: "2026-11-12T01:14:36.498Z"}) - } - rk := &enginereads.Keeper{Call: bootstrapped, Sync: true} - rk.AskNode("ace", "the assigner started") - body, _ := json.Marshal(nodeengine.ManifestReadsChangedEvent{V: 1, Node: "ace", Why: enginereads.ReadsStarted, At: "2026-11-12T01:14:39.002Z", ManifestReads: []string{"seat:a", "seat:b"}}) - readsChangedHandler(rk, t.Logf)(readsMsg{subject: enginereads.SubjectPrefix + "ace", data: body}) - rp := &passer{w: w, reads: rk, online: map[string]bool{"ace": true}} rn := w.nodes(nil, nil) - rp.engineReads(rn) - if !slices.Equal(rn.Reads["ace"], []string{"seat:a", "seat:b"}) { - t.Errorf("the rules read the statement older than the event heard: %v", rn.Reads["ace"]) + (&passer{w: w, reads: again}).engineReads(rn) + if !slices.Equal(rn.Reads["ace"], n.Reads["ace"]) { + t.Errorf("after a restart the rules read %v", rn.Reads) } - // ADR 0668 §3 (review round 1): a node whose engine answers nothing while its machine is heard is asked again at each - // pass once its ask ended, and past five minutes is the finding call-unanswered, cleared once it answers; none is - // written while its machine is away. - clock := time.Date(2026, 11, 10, 3, 0, 0, 0, time.UTC) - held := map[string][]byte{} - answers := false - silent := func(node, _ string, _ any) (json.RawMessage, error) { - asked[node]++ - if answers { - return json.Marshal(nodeengine.ManifestReadsAnswer{V: 1, Node: node, ManifestReads: []string{"form:new"}, StatedAt: clock.Format(time.RFC3339Nano)}) - } - return nil, context.DeadlineExceeded + if err := kept.take("ace", state["ace"], false); err != nil || woken != 1 { + t.Errorf("the assigner's own write handed back woke a pass (%d, %v)", woken, err) } - q := &passer{w: w, logf: t.Logf, now: func() time.Time { return clock }, findings: assign.Findings{ - Put: putInto(held), - Delete: func(k string) error { delete(held, k); return nil }, - Keys: func() ([]string, error) { return slices.Collect(maps.Keys(held)), nil }}, - reads: &enginereads.Keeper{Call: silent, Sync: true, Now: func() time.Time { return clock }}} - key := mcc.Key(mcc.ScopeCall, enginereads.CallID("ace"), mcc.KindCallUnanswered) - w.heardPresence("g14", true, time.Now()) // g14 away throughout: a machine is present unless the watchman said it away (§389) - before := asked["ace"] - q.engineReads(w.nodes(nil, nil)) - clock = clock.Add(6 * time.Minute) - off := false - w.heardPresence("ace", !off, time.Now()) - q.engineReads(w.nodes(nil, nil)) - if len(held) != 0 { - t.Errorf("a finding was written while ace is away: %v", slices.Collect(maps.Keys(held))) + // An event whose words are not written is not acknowledged, so the bus delivers it again; one that does not read is + // acknowledged and not kept. + failing = true + if m := say("ace", "2026-11-20T03:05:00Z"); m.acked || !m.naked { + t.Errorf("an event not written was acked %v, naked %v", m.acked, m.naked) } - w.heardPresence("ace", !on, time.Now()) - q.engineReads(w.nodes(nil, nil)) - if _, ok := held[key]; !ok || asked["ace"]-before < 3 { - t.Errorf("ace unanswered six minutes, heard: findings %v, asked %d times", slices.Collect(maps.Keys(held)), asked["ace"]-before) + failing = false + if m := say("ace", "yesterday"); !m.acked { + t.Error("an event that does not read was not acknowledged") } - answers = true - q.engineReads(w.nodes(nil, nil)) - q.engineReads(w.nodes(nil, nil)) - if _, ok := held[key]; ok { - t.Errorf("the finding stays after ace answered") + if words, _ := kept.words("ace"); !slices.Contains(words, "seat:a") { + t.Errorf("an event not written or not read changed the words to %v", words) } - // The contract carries no checks, so its reader judges what the engine says (mesh-contracts seats/nodeengine): an - // answer or an event that does not hold is no statement, and an event's node is its subject's last token. + // assigner_engine_reads lists the state. + got, err := readEngineReads(memStore{engineReadsState: &memKV{values: state}}, "") + if listed, _ := json.Marshal(got); err != nil || !strings.Contains(string(listed), `"ace":{"words":["want:version","seat:a"]`) { + t.Errorf("assigner_engine_reads answers %s, %v", listed, err) + } + if _, err := readEngineReads(memStore{engineReadsState: &memKV{values: state}}, "g14"); err == nil { + t.Error("assigner_engine_reads answers a node it holds no key of") + } + // The event's node is its subject's last token, and what does not hold is no event. now := time.Now().Format(time.RFC3339Nano) - for _, row := range []struct { - name string - raw string - stays bool - }{ - {"a whole answer", `{"v":1,"node":"ace","manifest-reads":["form:new"],"stated-at":"` + now + `"}`, true}, - {"an engine stating nothing", `{"v":1,"node":"ace","stated-at":"` + now + `"}`, true}, - {"another layout", `{"v":2,"node":"ace","stated-at":"` + now + `"}`, false}, - {"no node", `{"v":1,"node":"","stated-at":"` + now + `"}`, false}, - {"a node that is no token", `{"v":1,"node":"a.ce","stated-at":"` + now + `"}`, false}, - {"another node's answer", `{"v":1,"node":"g14","stated-at":"` + now + `"}`, false}, - {"an empty capability", `{"v":1,"node":"ace","manifest-reads":[""],"stated-at":"` + now + `"}`, false}, - {"a time that is no time", `{"v":1,"node":"ace","stated-at":"yesterday"}`, false}, - } { - s := enginereads.Ask(func(string, string, any) (json.RawMessage, error) { return json.RawMessage(row.raw), nil }, "ace", time.Now()) - if (s.From == enginereads.FromVerb) != row.stays || !s.Answered { - t.Errorf("answer %s: %+v", row.name, s) - } - } for _, row := range []struct { name, subject, raw string ours, fails bool @@ -496,69 +294,77 @@ func (b *fakeBus) CallOn(node, key string, body any) (json.RawMessage, error) { return b.Call(node+"/"+key, body) } func (b *fakeBus) CallKept(key string, body any) (json.RawMessage, error) { return b.Call(key, body) } -func (b *fakeBus) CallKeptOn(node, key string, body any) (json.RawMessage, error) { - return b.CallOn(node, key, body) -} func (b *fakeBus) CallFile(key string, body any) (json.RawMessage, error) { return b.Call(key, body) } func (b *fakeBus) Emit(eventType string, body any) error { b.events = append(b.events, fmt.Sprintf("%s %+v", eventType, body)) return nil } -// Design 47 line 912 and line 914 ("a placement's `build` delivers the earlier build"; ADR 0666 §24, B-K1): a placed -// module's build becomes a held put of that build, which then goes a put's steps; a placement naming no build any more -// releases the put it made, never a person's. -func aPlacementsBuildIsPut(t *testing.T) { +// A8/AC1 (design 47 H §750, H-A-5, H-A-37, H-A-99): a placement's build is read before the rule and names the want's version, +// the version released at that build, with no pipeline and no rank and no put; the node whose placement names none follows the +// newest passed version; a placement naming no build any more gives the node back to the rule. +func aPlacementsBuildNamesTheWantsVersion(t *testing.T) { ctx := context.Background() - putsKV := &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Puts(): putsKV}, time.Date(2026, 11, 10, 18, 0, 0, 0, time.UTC)) - earlier := meshcatalogclient.SourcePrefix2 + strings.Repeat("e", 64) + wantsKV, putsKV := &memKV{values: map[string][]byte{}}, &memKV{values: map[string][]byte{}} + p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}, buckets.Puts(): putsKV}, time.Now()) + p.say = func(oc.Condition) error { return nil } + earlier, newer := meshcatalogclient.SourcePrefix2+strings.Repeat("e", 64), meshcatalogclient.SourcePrefix2+strings.Repeat("f", 64) + mdigest := strings.Repeat("a", 64) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: newer, Manifest: mdigest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) + putModuleRaw(p.w, trunkBranch, "web", mod) + p.w.manifests[mdigest] = parsed{module: "web", manifest: catalogue.Manifest{Module: "web"}} + // The earlier build's release: its parts are what the build made. + rel, _ := json.Marshal(meshcatalog.VersionReleasedEvent{Module: "web", Source: 3, Version: "0.1.1", Artifacts: []meshcatalog.VersionArtifact{{Artifact: "web:image", Digest: "sha256:1"}}}) + if err := p.w.released.put(rel); err != nil { + t.Fatal(err) + } + end, _ := json.Marshal(builds.BuildEnded{Module: "web", Fingerprint: earlier, Outcome: builds.BuildDone, Made: map[string]string{"image": "sha256:1"}}) + if err := p.w.ends.put(end); err != nil { + t.Fatal(err) + } place := func(build string) { - values, err := mmac.AssignmentsValues(mma.Assignments{Node: "g14", Modules: []mma.PlacedModule{{Module: "web", Build: build}, {Module: "db"}}}) - if err != nil { - t.Fatal(err) - } - for key, raw := range values { - p.w.putEntry(plc.AssignmentsBucket(), key, raw, uint64(len(raw)), time.Now()) + for _, node := range []string{"g14", "ace"} { + pm := mma.PlacedModule{Module: "web"} + if node == "g14" { + pm.Build = build + } + values, err := mmac.AssignmentsValues(mma.Assignments{Node: node, Modules: []mma.PlacedModule{pm}}) + if err != nil { + t.Fatal(err) + } + for key, raw := range values { + p.w.putEntry(plc.AssignmentsBucket(), key, raw, uint64(len(raw)), time.Now()) + } } } - read := func() (puts.Put, bool) { - raw, ok := putsKV.values["g14.web"] - if !ok { - return puts.Put{}, false - } - got, err := puts.Decode(raw) + p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) + p.ends.trunk = p.w.trunkName + if _, err := p.ends.take(ended(trunkBranch, 5, mpr.PipelineEndedOutcomePassed, mover("web", newer, "0.1.2", 1, mpr.StepResultOutcomePassed))); err != nil { + t.Fatal(err) + } + read := func(node string) mwc.Want { + key, _ := mwc.WantKey(node, "web") + w, err := mwc.DecodeWant(wantsKV.values[key]) if err != nil { - t.Fatal(err) + t.Fatalf("no want of web on %s: %v", node, err) } - return got, true + return w } place(earlier) - p.placementPuts(ctx) - got, ok := read() - if !ok || got.FP != earlier || !got.Hold || got.By != placementBy || got.Step != puts.Asked { - t.Fatalf("the placement's build made the put %+v (there %v)", got, ok) + p.wants(ctx) + if w := read("g14"); w.Fp != earlier || w.Version != "0.1.1" || w.Pipeline != "" || w.Rank != 0 || w.Put { + t.Errorf("the placement's build names the want %+v, want %s at 0.1.1, no pipeline, rank or put", w, earlier) } - if _, there := putsKV.values["g14.db"]; there { - t.Error("a placed module naming no build was put") + if w := read("ace"); w.Fp != newer || w.Version != "0.1.2" { + t.Errorf("the node naming no build wants %+v", w) } - p.placementPuts(ctx) // again: the same build makes nothing new - if again, _ := read(); !again.At.Equal(got.At) || again.Step != puts.Asked { - t.Errorf("a second pass made the put again: %+v", again) + if len(putsKV.values) != 0 { + t.Errorf("a placement's build made a put: %v", putsKV.values) } place("") - p.placementPuts(ctx) - if got, _ := read(); got.Step != puts.Released || !strings.Contains(got.Words, placementBy) { - t.Errorf("the placement naming no build left the put %+v", got) - } - // A person's put stands while the placement names no build. - person := puts.Put{FP: earlier, Hold: true, Why: "a try", By: "lead@shanks", At: time.Now()}.Said(time.Now(), puts.Holding, "held") - raw, _ := puts.Encode(person) - putsKV.values["g14.web"] = raw - p.w.put(buckets.Puts(), "g14.web", raw) - p.placementPuts(ctx) - if got, _ := read(); got.Step != puts.Holding || got.By != "lead@shanks" { - t.Errorf("a person's put was released by the placement: %+v", got) + p.wants(ctx) + if w := read("g14"); w.Fp != newer || w.Version != "0.1.2" { + t.Errorf("a placement naming no build any more left the want %+v", w) } } @@ -677,7 +483,7 @@ func putOfTheBus(t *testing.T) { <-woke got = step() w, err := mwc.DecodeWant(wantsKV.values["want.novox.nats"]) - if got.Step != puts.Wanted || err != nil || w.Fp != fp || w.Composed != digest || !w.Put || !w.Hold { + if got.Step != puts.Wanted || err != nil || w.Fp != fp || !w.Put || !w.Hold { t.Fatalf("snapshotted, the put stands at %s (%s); the want %+v, %v", got.Step, got.Words, w, err) } if call := <-bus.calls; !strings.HasPrefix(call, "seat:mesh-hand-acts.warranted ") || !strings.Contains(call, a.Ask.ID) { // ADR 0565 §11: into the seat @@ -754,266 +560,6 @@ func putOfTheBus(t *testing.T) { } } -// 1945/AC21: given a module on neither list on a node, when passes follow, then the assigner raises its condition once, says it -// again after a say that failed, and clears it once when the module has a want again; a module on neither list is also written to -// the journal, so it is loud with no operator channel. -func TestAModuleOnNeitherListIsRaisedOnceAndClearedOnce(t *testing.T) { - // A pair only one source names is a condition of the same kind (issue 1126): folded here to hold the count. - aAssignmentsAloneNamesWhereModulesRun(t) - // The verb edges a want waits on come from the graph and what runs (ADR 0614 §2, §3): folded here to hold the count. - aWantWaitsOnTheVerbEdgesTheGraphKeeps(t) - t.Run("gathers", testThePassGathersTheModulesOnNeitherList) - p := newPasser(kvSet{}, time.Date(2026, 10, 25, 15, 0, 0, 0, time.UTC)) - var said []oc.Condition - failing := true - p.say = func(c oc.Condition) error { - if failing { - failing = false - return errors.New("the channel is away") - } - said = append(said, c) - return nil - } - var logged []string - p.logf = func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) } - one := map[string]neitherOne{"g14.screen-lock": {node: "g14", module: "screen-lock", - holding: assign.Holding{Rule: mwc.RuleProviders, Words: "no provider of x11-display runs healthy on g14 or at its canary"}}} - p.sayNeither(one, nil, nil) // the say fails - p.sayNeither(one, nil, nil) - p.sayNeither(one, nil, nil) - if len(said) != 1 || said[0].State != occ.StateRaised || said[0].Subject.Machine != "g14" || said[0].Subject.ID != "screen-lock" { - t.Fatalf("said = %+v, want one raised condition of screen-lock on g14", said) - } - if !strings.Contains(strings.Join(logged, "\n"), "screen-lock") { - t.Errorf("the journal does not name the module on neither list: %q", logged) - } - wants := map[string]string{"g14.screen-lock": ""} - p.sayNeither(map[string]neitherOne{}, nil, wants) - p.sayNeither(map[string]neitherOne{}, nil, wants) - if len(said) != 2 || said[1].State != occ.StateCleared || said[1].Key != said[0].Key { - t.Errorf("said = %+v, want the condition cleared once under its key", said) - } - if last := logged[len(logged)-1]; last != "screen-lock on g14 has a want again" { - t.Errorf("the clear said %q", last) - } - // 1945: a module that leaves neither list with no want is cleared in words saying so, never "has a want again". The bus is - // on neither list by a rule as any module (ADR 0601 §1), nothing for the operator to do. - bus := map[string]neitherOne{"novox.nats": {node: "novox", module: "nats", holding: assign.Holding{Rule: mwc.RuleGrant, Words: "no credential"}}} - p.sayNeither(bus, nil, nil) - if c := said[len(said)-1]; c.State != occ.StateRaised || !strings.HasPrefix(c.Explanation, occ.NothingToDo) || !strings.Contains(c.Explanation, "rule 6 holds its first want back: no credential") { - t.Errorf("the bus's neither condition = %+v", c) - } - p.sayNeither(map[string]neitherOne{}, nil, map[string]string{"novox.nats": "it is held, and its held condition says so"}) - if c := said[len(said)-1]; c.State != occ.StateCleared || strings.Contains(c.Resolved, "has a want") { - t.Errorf("a module that left neither list with no want was cleared as %+v", c) - } - if last := logged[len(logged)-1]; last != "nats on novox is no longer on neither list and has no want: it is held, and its held condition says so" { - t.Errorf("the clear without a want said %q", last) - } -} - -// 1945/AC21: the pass gathers the modules on neither list of every module's outcome, keyed by node and module. -func testThePassGathersTheModulesOnNeitherList(t *testing.T) { - got := map[string]neitherOne{} - gatherNeither(got, "screen-lock", assign.Outcome{Neither: map[string]assign.Holding{"g14": {Rule: mwc.RuleGrant, Words: "w"}}}) - gatherNeither(got, "rofi", assign.Outcome{}) - if len(got) != 1 || got["g14.screen-lock"].node != "g14" || got["g14.screen-lock"].module != "screen-lock" || got["g14.screen-lock"].holding.Rule != mwc.RuleGrant { - t.Errorf("gathered = %+v", got) - } - // 1945 (the bus bar): a node off neither list is cleared in words that say why it has no want; an operator's put running - // through its steps owns the want, so it is named, never "the node does not run it". - for _, c := range []struct { - name string - puts map[string]puts.Put - before map[string]mwc.Want - why string - }{ - {"a live want", nil, map[string]mwc.Want{"novox": {Fp: fpTest}}, ""}, - {"a put on its steps", map[string]puts.Put{"novox": {FP: fpTest, Step: puts.Person}}, nil, "its put waits on its own steps and the operator's answer"}, - {"nothing", nil, nil, "the node does not run it"}, - {"an ended put", map[string]puts.Put{"novox": {FP: fpTest, Step: puts.Refused}}, nil, "the node does not run it"}, - } { - left := map[string]string{} - leftNeither(left, assign.Module{Name: "nats", FP: fpTest, Nodes: []string{"novox"}, Puts: c.puts}, c.before, assign.Outcome{}) - if why, there := left["novox.nats"]; !there || why != c.why { - t.Errorf("%s: left %q (%v), want %q", c.name, why, there, c.why) - } - } - // 1945 (a first want held back silently): the journal says why a node gets no want when the reason appears or changes, and - // when it has a want again: one line per change, not per pass. - p := newPasser(kvSet{}, time.Date(2026, 10, 25, 21, 30, 0, 0, time.UTC)) - var logged []string - p.logf = func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) } - notDone := assign.Outcome{Rules: map[string]assign.Holding{"shanks": {Rule: mwc.RuleBuildDone, Words: "the build of rofi at 4e7b118e is being built"}}} - wanted := assign.Outcome{Wants: map[string]mwc.Want{"shanks": {Fp: fpTest, Composed: "sha256:" + strings.Repeat("c", 64)}}} - for i, c := range []struct { - before map[string]mwc.Want - out assign.Outcome - line string // the line said in this pass, "" for none - }{ - {nil, notDone, "rofi on shanks gets no want: rule 1: the build of rofi at 4e7b118e is being built"}, - {nil, notDone, ""}, - {map[string]mwc.Want{"shanks": {Removed: true}}, notDone, ""}, - {nil, assign.Outcome{Rules: map[string]assign.Holding{"shanks": {Rule: mwc.RuleGrant, Words: "no credential"}}}, "rofi on shanks gets no want: rule 6: no credential"}, - {nil, wanted, "rofi on shanks has a want"}, - {map[string]mwc.Want{"shanks": wanted.Wants["shanks"]}, assign.Outcome{}, ""}, - } { - logged = nil - p.sayNoWant("rofi", []string{"shanks"}, c.before, c.out) - if got := strings.Join(logged, "\n"); got != c.line { - t.Errorf("pass %d said %q, want %q", i, got, c.line) - } - } - // 1496, ADR 0557 §1: a failure that is the node's own (its fingerprint healthy elsewhere, not held) raises the node's condition - // once, naming the node, the module, F and the have's words, and clears it once when it ends. - var conds []oc.Condition - p.say = func(c oc.Condition) error { conds = append(conds, c); return nil } - failing := map[string]assign.NodeFailure{"shanks": {Others: []string{"ace"}, Words: "health failed, phase running: w"}} - for i, c := range []struct { - failed map[string]assign.NodeFailure - state string // the condition said in this pass, "" for none - }{ - {failing, occ.StateRaised}, - {failing, ""}, - {nil, occ.StateCleared}, - {nil, ""}, - } { - conds = nil - p.sayNodeFailed("rofi", fpTest, []string{"shanks"}, assign.Nodes{}, assign.Outcome{NodeFailed: c.failed}, false) - switch { - case c.state == "" && len(conds) != 0, c.state != "" && (len(conds) != 1 || conds[0].State != c.state): - t.Errorf("node failed, pass %d said %+v, want %q", i, conds, c.state) - case c.state == occ.StateRaised && (conds[0].Key != "mesh-delivery-assigner.failed-alone.shanks.rofi" || - !strings.Contains(conds[0].Explanation, "failed on shanks (health failed, phase running: w), while it runs healthy on ace")): - t.Errorf("node failed, raised %+v", conds[0]) - } - } - // D2: a node that leaves the module's nodes while it failed alone is cleared once; the first pass after a start clears every - // node the presence bucket knows, for every module, failed have or not (a node that recovered unwatched leaves none). - conds = nil - p.sayNodeFailed("rofi", fpTest, []string{"shanks"}, assign.Nodes{}, assign.Outcome{NodeFailed: failing}, false) - p.sayNodeFailed("rofi", fpTest, nil, assign.Nodes{}, assign.Outcome{}, false) - p.sayNodeFailed("rofi", fpTest, nil, assign.Nodes{}, assign.Outcome{}, false) - if len(conds) != 2 || conds[1].State != occ.StateCleared || conds[1].Key != "mesh-delivery-assigner.failed-alone.shanks.rofi" { - t.Errorf("a node leaving the module said %+v, want one raise and one clear", conds) - } - known := assign.Nodes{Online: map[string]bool{"ace": true, "g14": false}, - Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"g14": {"rofi": {Fp: fpTest, Health: nodeengineclient.HealthFailed}}}} - for _, c := range []struct { - name string - known assign.Nodes - keys []string - }{ - {"a failed have", known, []string{"mesh-delivery-assigner.failed-alone.ace.rofi", "mesh-delivery-assigner.failed-alone.g14.rofi"}}, - {"no failed have", assign.Nodes{Online: known.Online}, []string{"mesh-delivery-assigner.failed-alone.ace.rofi", "mesh-delivery-assigner.failed-alone.g14.rofi"}}, - } { - conds = nil - fresh := newPasser(kvSet{}, time.Date(2026, 10, 26, 22, 0, 0, 0, time.UTC)) - fresh.logf = func(string, ...any) {} - fresh.say = func(c oc.Condition) error { conds = append(conds, c); return nil } - fresh.sayNodeFailed("rofi", fpTest, nil, c.known, assign.Outcome{}, true) - var keys []string - for _, cond := range conds { - if cond.State == occ.StateCleared { - keys = append(keys, cond.Key) - } - } - if !slices.Equal(keys, c.keys) || len(conds) != len(c.keys) { - t.Errorf("first pass, %s: said %+v, want clears of %q", c.name, conds, c.keys) - } - } -} - -// A neither or held condition a previous process raised is cleared by the first pass after a start: a fresh pass remembers -// nothing it said, so it clears, once, the neither condition of every assigned module not on neither list and the held condition of -// every module that is not held. Clearing a condition that is not open is quiet at the router. -func TestTheFirstPassAfterAStartClearsWhatAPreviousProcessRaised(t *testing.T) { - t.Run("a held module", testAHeldModuleRaisesNoNeitherAndClearsAStandingOne) - wantsKV := &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) - var said []oc.Condition - p.say = func(c oc.Condition) error { said = append(said, c); return nil } - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - putModuleRaw(p.w, trunkBranch, "web", mod) - placeOn(t, p.w, "g14", "web") - on := true - p.w.heardPresence("g14", !on, time.Now()) - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - p.w.put(buckets.Have, "g14.web", have) - key, _ := mwc.WantKey("g14", "web") - want, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest, Composed: "sha256:" + strings.Repeat("c", 64)}) - p.w.put(buckets.Wants(), key, want) - p.wants(context.Background()) - cleared := map[string]bool{} - for _, c := range said { - if c.State != occ.StateCleared { - t.Errorf("the first pass raised %+v", c) - } - cleared[c.Key] = true - } - neither := assign.NeitherCondition("g14", "web", assign.Holding{}, false).Key - held := assign.HeldCondition("web", fpTest, "", "", false, false).Key - if !cleared[neither] || !cleared[held] { - t.Errorf("cleared = %v, want %s and %s", cleared, neither, held) - } - n := len(said) - p.wants(context.Background()) - if len(said) != n { - t.Errorf("a second pass said again: %+v", said[n:]) - } -} - -// 1945/AC24: given a module held at F under rule 2 on a node that runs it with no want, when the assigner passes, then it raises no -// neither condition for it, a neither condition this process raised for it before is cleared, and so is one a previous process -// raised (the first pass after a start), each once. -func testAHeldModuleRaisesNoNeitherAndClearsAStandingOne(t *testing.T) { - setup := func() (*passer, *[]oc.Condition) { - p := newPasser(kvSet{buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) - said := &[]oc.Condition{} - p.say = func(c oc.Condition) error { *said = append(*said, c); return nil } - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/resolved", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - putModuleRaw(p.w, trunkBranch, "systemd-resolved", mod) - placeOn(t, p.w, "shanks", "systemd-resolved") - on := true - p.w.heardPresence("shanks", !on, time.Now()) - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed}) - p.w.put(buckets.Have, "shanks.systemd-resolved", have) - hkey, _ := mwc.HeldKey("systemd-resolved", fpTest) - held, _ := mwc.EncodeHeld(mwc.Held{Node: "shanks", Words: "health failed"}) - p.w.put(buckets.Wants(), hkey, held) - return p, said - } - key := assign.NeitherCondition("shanks", "systemd-resolved", assign.Holding{}, false).Key - neithers := func(said []oc.Condition) (raised, cleared int) { - for _, c := range said { - if c.Key != key { - continue - } - if c.State == occ.StateRaised { - raised++ - } else { - cleared++ - } - } - return - } - // a standing neither this process raised - p, said := setup() - p.neither = map[string]neitherOne{"shanks.systemd-resolved": {node: "shanks", module: "systemd-resolved", holding: assign.Holding{Rule: mwc.RuleNotHeld}}} - p.wants(context.Background()) - p.wants(context.Background()) - if r, c := neithers(*said); r != 0 || c != 1 { - t.Errorf("standing in this process: %d raised, %d cleared, want 0 and 1: %+v", r, c, *said) - } - // one a previous process raised: the first pass after a start clears it - p, said = setup() - p.wants(context.Background()) - p.wants(context.Background()) - if r, c := neithers(*said); r != 0 || c != 1 { - t.Errorf("after a start: %d raised, %d cleared, want 0 and 1: %+v", r, c, *said) - } -} - // 1906 note 28: an ask outlives its fingerprint. Each pass takes away the hold, restore and record asks of a module whose // fingerprint (or composition) is not the trunk's now, or whose module left the trunk; a trunk not read takes nothing away; a // backup ask is taken away when its module's want on the node names another fingerprint or is removed, and left when the want @@ -1025,15 +571,10 @@ func TestAsksOfAFingerprintTheTrunkLeftBehindAreTakenAway(t *testing.T) { return map[string]meshcatalog.ModulesByModuleValue{"web": {Fp2: fp, Manifest: "sha256:" + strings.Repeat("1", 64), Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}} } type ask struct{ name, key string } - holdOld, _ := asks.HoldAskKey("web", fpOld) - holdNew, _ := asks.HoldAskKey("web", fpNew) - holdGone, _ := asks.HoldAskKey("gone", fpOld) - restOld, _ := asks.RestoreAskKey("novox", "web", fpOld) - restNew, _ := asks.RestoreAskKey("novox", "web", fpNew) recOld, _ := asks.RecordAskKey("novox", "web", digOld) recNew, _ := asks.RecordAskKey("novox", "web", digNew) backOld, _ := asks.BackupAskKey("novox", "web", fpOld) - all := []string{holdOld, holdNew, holdGone, restOld, restNew, recOld, recNew, backOld} + all := []string{recOld, recNew, backOld} for _, c := range []struct { name string trunk map[string]meshcatalog.ModulesByModuleValue @@ -1041,13 +582,12 @@ func TestAsksOfAFingerprintTheTrunkLeftBehindAreTakenAway(t *testing.T) { noDigit bool // the node's own composition is not read want *mwc.Want // the want of web on novox, nil when none is written }{ - {"the trunk moved on", trunkOf(fpNew), []string{holdOld, holdGone, restOld, recOld}, false, nil}, - {"the backup's want names another fingerprint", trunkOf(fpNew), []string{holdOld, holdGone, restOld, recOld, backOld}, false, &mwc.Want{Fp: fpNew, Self: true}}, - {"the backup's want is removed", trunkOf(fpNew), []string{holdOld, holdGone, restOld, recOld, backOld}, false, &mwc.Want{Removed: true}}, - {"the backup's want names the fingerprint in another composition (its have waits for a yes: a mid-move have is not past the hold, A-453a)", trunkOf(fpNew), []string{holdOld, holdGone, restOld, recOld}, false, &mwc.Want{Fp: fpOld, Composed: digNew}}, - {"the trunk is at the old fingerprint", trunkOf(fpOld), []string{holdNew, holdGone, restNew, recOld}, false, nil}, - {"the trunk's fingerprint is not read yet", trunkOf(""), []string{holdGone}, false, nil}, - {"the composition is not read yet", trunkOf(fpNew), []string{holdOld, holdGone, restOld}, true, nil}, + {"the trunk moved on", trunkOf(fpNew), []string{recOld}, false, nil}, + {"the backup's want names another fingerprint", trunkOf(fpNew), []string{recOld, backOld}, false, &mwc.Want{Fp: fpNew, Self: true}}, + {"the backup's want is removed", trunkOf(fpNew), []string{recOld, backOld}, false, &mwc.Want{Removed: true}}, + {"the trunk is at the old fingerprint", trunkOf(fpOld), []string{recOld}, false, nil}, + {"the trunk's fingerprint is not read yet", trunkOf(""), nil, false, nil}, + {"the composition is not read yet", trunkOf(fpNew), nil, true, nil}, {"the trunk is not read at all", nil, nil, false, nil}, {"the trunk is not read at all, the backup's want moved", nil, nil, false, &mwc.Want{Fp: fpNew, Self: true}}, } { @@ -1060,14 +600,10 @@ func TestAsksOfAFingerprintTheTrunkLeftBehindAreTakenAway(t *testing.T) { k, _ := asks.ParseAnyAskKey(key) var a asks.Ask switch k.Kind { - case asks.AskKindHold: - a = assign.HoldAsk(key, k.Module, k.FP, "novox", "w", 1, time.Now()) - case asks.AskKindRestore: - a = assign.RestoreAsk(key, k.Node, k.Module, k.FP, "w", 2, time.Now()) case asks.AskKindRecord: a = assign.RecordAsk(key, k.Node, k.Module, fpNew, assign.RecordNeed{Node: k.Node, Digest: k.Composed}, 2, time.Now()) default: - a = assign.BackupAsk(key, k.Node, k.Module, k.FP, digOld, "w", 2, time.Now()) + a = assign.BackupAsk(key, k.Node, k.Module, k.FP, "w", 2, time.Now()) } v, err := assign.EncodeAsk(key, a) if err != nil { @@ -1094,20 +630,7 @@ func TestAsksOfAFingerprintTheTrunkLeftBehindAreTakenAway(t *testing.T) { } p.w.put(buckets.Wants(), wkey, raw) } - heldOld, _ := mwc.HeldKey("web", fpOld) - wantsKV.values[heldOld] = []byte("x") - p.w.put(buckets.Wants(), heldOld, []byte("x")) - firstAsk, _ := decodeAsk(holdOld, asksKV.values[holdOld]) p.staleAsks(context.Background(), c.trunk) - // the held record and condition of a fingerprint whose hold ask went are taken away with it - heldOld, _ = mwc.HeldKey("web", fpOld) - _, heldThere := wantsKV.values[heldOld] - cleared := slices.ContainsFunc(said, func(c oc.Condition) bool { - return c.Key == assign.HeldCondition("web", fpOld, "", "", false, false).Key && c.State == occ.StateCleared - }) - if gone := slices.Contains(c.gone, holdOld); !heldThere || cleared != gone { - t.Errorf("hold ask of the old fingerprint gone = %v: its held record stays = %v (must), its condition cleared = %v", gone, heldThere, cleared) - } // the failed backup's condition is cleared with its ask, and with no other (AC165-1) backCleared := slices.ContainsFunc(said, func(c oc.Condition) bool { return c.Key == assign.BackupCondition("novox", "web", fpOld, "", false).Key && c.State == occ.StateCleared @@ -1122,66 +645,9 @@ func TestAsksOfAFingerprintTheTrunkLeftBehindAreTakenAway(t *testing.T) { t.Errorf("%s: left in the bucket = %v, want %v", key, there, want) } } - if slices.Contains(c.gone, holdOld) { - // the trunk returns to the fingerprint (a revert): the held record still holds it, and it is asked and said again - p.now = func() time.Time { return time.Now().Add(time.Hour) } - p.holdAsk(context.Background(), "web", fpOld, nil) - p.heldCondition(context.Background(), "web", fpOld, nil, false) - again, err := decodeAsk(holdOld, asksKV.values[holdOld]) - raised := slices.ContainsFunc(said, func(c oc.Condition) bool { return c.State == occ.StateRaised }) - if err != nil || again.Ask.ID == firstAsk.Ask.ID || !raised { - t.Errorf("the trunk returned to the held fingerprint: ask %q (first %q, %v), condition raised again = %v", again.Ask.ID, firstAsk.Ask.ID, err, raised) - } - } }) } - t.Run("a clear that failed to be said is said at the next pass, and the ask goes after it", func(t *testing.T) { - asksKV, wantsKV := &memKV{values: map[string][]byte{}}, &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Asks(): asksKV, buckets.Wants(): wantsKV}, time.Now()) - var said []oc.Condition - fail := true - p.say = func(c oc.Condition) error { - if fail { - fail = false - return errors.New("router unreachable") - } - said = append(said, c) - return nil - } - a := assign.HoldAsk(holdOld, "web", fpOld, "novox", "w", 1, time.Now()) - v, err := assign.EncodeAsk(holdOld, a) - if err != nil { - t.Fatal(err) - } - asksKV.values[holdOld] = v - p.w.put(buckets.Asks(), holdOld, v) - p.staleAsks(context.Background(), trunkOf(fpNew)) - if _, there := asksKV.values[holdOld]; !there || len(said) != 0 { - t.Fatalf("after a failed clear: ask left = %v, conditions said = %d; want the ask kept and nothing said", there, len(said)) - } - p.staleAsks(context.Background(), trunkOf(fpNew)) - if _, there := asksKV.values[holdOld]; there || len(said) != 1 || said[0].State != occ.StateCleared { - t.Errorf("at the next pass: ask left = %v, conditions said = %v; want the ask gone and the clear said", there, said) - } - }) - - t.Run("a lift takes the restore asks of its fingerprint on every node", func(t *testing.T) { - asksKV, wantsKV := &memKV{values: map[string][]byte{}}, &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Asks(): asksKV, buckets.Wants(): wantsKV}, time.Now()) - keep, _ := asks.RestoreAskKey("ace", "web", fpNew) - keepOther, _ := asks.RestoreAskKey("ace", "other", fpOld) - for _, key := range []string{restOld, mustKey(asks.RestoreAskKey("ace", "web", fpOld)), keep, keepOther} { - asksKV.values[key] = []byte("x") - p.w.put(buckets.Asks(), key, []byte("x")) - } - p.lift(context.Background(), "web", fpOld) - for key, gone := range map[string]bool{restOld: true, mustKey(asks.RestoreAskKey("ace", "web", fpOld)): true, keep: false, keepOther: false} { - if _, there := asksKV.values[key]; there == gone { - t.Errorf("%s: left in the bucket = %v after the lift", key, there) - } - } - }) } func mustKey(key string, err error) string { @@ -1210,12 +676,12 @@ func aReleasesApproveAnswersTheMoveOfItsBuildAlone(t *testing.T) { asksKV := &memKV{values: map[string][]byte{}} p := newPasser(kvSet{buckets.Asks(): asksKV}, time.Now()) p.logf = t.Logf - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/web", Commit: commit, Src: 7, FpSrc: 7, Path: meshcatalogclient.RootPath}) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/web", Commit: commit, Path: meshcatalogclient.RootPath}) putModuleRaw(p.w, trunkBranch, "web", mod) have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fpOld, Composed: was, Phase: nodeengineclient.PhaseWaitingYes, Health: nodeengineclient.HealthOK, Judgement: assign.JudgedRecreates, Waits: waiting, Detail: "it recreates web.server", BuildAlone: true}) p.w.put(buckets.Have, "g14.web", have) - m := assign.Module{Name: "web", FP: fpTest, Nodes: []string{"g14"}, Self: func(string) bool { return true }} + m := assign.Module{Name: "web", Nodes: []string{"g14"}, Self: func(string) bool { return true }} out := assign.Outcome{Wants: map[string]mwc.Want{"g14": {Fp: fpTest, Self: true}}} p.yeses(context.Background(), "web", m, map[string]mwc.Want{"g14": {Fp: fpOld, Self: true}}, &out) if w := out.Wants["g14"]; w.Yes != "" || w.Release != "" { @@ -1253,7 +719,11 @@ func aPlacedNodesWantsNameTheBuildAloneAndItsYesComesFromItsHave(t *testing.T) { p.logf = t.Logf put := func(bucket, key string, v []byte) { p.w.put(bucket, key, v) } mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - putModuleRaw(p.w, trunkBranch, "web", mod) // the trunk's build is not done: rule 1 holds g14 at fpOld + putModuleRaw(p.w, trunkBranch, "web", mod) + // The newest version passed is fpOld's (design 47 H §750): the trunk's newer build passed no pipeline. + p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) + p.ends.trunk = p.w.trunkName + _, _ = p.ends.take(ended(trunkBranch, 1, mpr.PipelineEndedOutcomePassed, mover("web", fpOld, "0.1.1", 1, mpr.StepResultOutcomePassed))) placeOn(t, p.w, "g14", "web") pl, _ := mmac.EncodeAssignments(mma.Assignments{Node: "g14", Modules: []mma.PlacedModule{{Module: "web"}}}) put(plc.AssignmentsBucket(), "assignments.g14", pl) @@ -1262,7 +732,7 @@ func aPlacedNodesWantsNameTheBuildAloneAndItsYesComesFromItsHave(t *testing.T) { have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fpOld, Composed: was, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) put(buckets.Have, "g14.web", have) wkey, _ := mwc.WantKey("g14", "web") - want, _ := mwc.EncodeWant(mwc.Want{Fp: fpOld, Composed: was}) + want, _ := mwc.EncodeWant(mwc.Want{Fp: fpOld, Version: "0.1.1", Pipeline: trunkBranch + ".1", Rank: 1}) wantsKV.values[wkey] = want put(buckets.Wants(), wkey, want) sync := func() { @@ -1275,7 +745,7 @@ func aPlacedNodesWantsNameTheBuildAloneAndItsYesComesFromItsHave(t *testing.T) { p.nodeWants(context.Background()) sync() got, err := mwc.DecodeWant(wantsKV.values[wkey]) - if err != nil || got.Fp != fpOld || got.Composed != "" || !got.Self { + if err != nil || got.Fp != fpOld || !got.Self { t.Fatalf("the placed node's want is %+v (%v), want it at %s, the node composing it", got, err, fpOld) } nkey, _ := mwc.WantNodeKey("g14") @@ -1391,92 +861,6 @@ func TestAnApprovedAssignmentsCallsNoControllerVerb(t *testing.T) { } } -// ADR 0614 §2, §3: of what a module invokes, the pass keeps as edges the verbs the graph does not leave out and another -// module serves; a verb is served once a running healthy build's manifest (the builder's build, the indexer's manifest) -// serves it, and a build whose manifest cannot be read counts what the graph says its module serves. No graph read: no wait. -func aWantWaitsOnTheVerbEdgesTheGraphKeeps(t *testing.T) { - p := newPasser(kvSet{}, time.Now()) - fp := func(c string) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(c, 64) } - if p.served(graph.Graph{}, assign.Nodes{}) != nil { - t.Error("with no graph read, a want waits on a verb") - } - for module, n := range map[string]graph.Node{ - "caller": {FP: fp("c"), Repository: "novox/caller", Invokes: []string{"seat:x.v", "loop.l", "none.t", "caller.own"}, LeftOut: []string{"loop.l"}, - Serves: []string{"caller.own"}}, - "server": {FP: fp("e"), Repository: "novox/server", Serves: []string{"seat:x.v", "seat:x.w"}}, - "loop": {FP: fp("1"), Repository: "novox/loop", Serves: []string{"loop.l"}}, - "late": {FP: fp("2"), Repository: "novox/late", Serves: []string{"late.t"}}, - } { - raw, err := graph.Encode(n) - if err != nil { - t.Fatal(err) - } - _ = raw - p.w.putModule(trunkBranch, module, meshcatalog.ModulesByModuleValue{}, n, nil) - } - g := p.moduleGraph(trunkBranch) - if got := keptInvokes(g, "caller"); !slices.Equal(got, []string{"seat:x.v"}) || keptInvokes(g, "unknown") != nil { - t.Errorf("the kept verb edges of caller = %v", got) - } - digest := strings.Repeat("d", 64) - parsedServer, err := catalogue.Decode([]byte(`{"module":"server","claims":[{"name":"x","scope":"mesh","serves":["w"]}]}`)) - if err != nil { - t.Fatal(err) - } - p.w.manifests[digest] = parsed{module: "server", manifest: parsedServer} - endDone(t, p.w, "server", fp("f"), digest) - running := nodeengine.HaveByNodeModuleValue{Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - at := func(f string) nodeengine.HaveByNodeModuleValue { h := running; h.Fp = f; return h } - served := p.served(g, assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "g14": {"server": at(fp("f")), "late": at(fp("2"))}}}) - for v, want := range map[string]bool{"seat:x.w": true, "seat:x.v": false, "late.t": true} { - if served(v) != want { - t.Errorf("served(%s) = %v, want %v: the running server's manifest serves w alone, late's is not read", v, served(v), want) - } - } - // The modules whose running build's manifest is not read are said once per change of their set (G-23). - var said []string - p.logf = func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) } - lateOnly := assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"g14": {"server": at(fp("f")), "late": at(fp("2"))}}} - p.served(g, lateOnly) - p.served(g, lateOnly) - p.served(g, assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"g14": {"late": at(fp("2")), "loop": at(fp("1"))}}}) - p.served(g, lateOnly) - if len(said) != 2 || !strings.Contains(said[0], "[loop late]") && !strings.Contains(said[0], "[late loop]") || !strings.Contains(said[1], "[late]") { - t.Errorf("the unread manifests were said %q, want once per change of the set", said) - } - // A-426a: a healthy build held (no catalog entry of it) or waiting for a yes serves what it serves, said once; one held unhealthy, - // or whose running statement says it stopped, does not. - held := at(fp("2")) - held.Phase = nodeengineclient.PhaseHeld - served = p.served(g, assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"novox": {"late": held}}}) - if !served("late.t") { - t.Error("a healthy held build of late does not serve late.t") - } - stopped := assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"novox": {"late": held}}, - Running: map[string]map[string]nodeengine.Running{"novox": {"late": {Stopped: true}}}} - sick := held - sick.Health = "failed" - if p.served(g, stopped)("late.t") || p.served(g, assign.Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"novox": {"late": sick}}})("late.t") { - t.Error("a held build stopped, or not healthy, serves late.t") - } - // ADR 0614 §5: bus-state counts as offered only from a healthy build on a node present; the bus module's build on an - // offline node offers nothing, and one whose manifest is not read counts its trunk manifest's offer. A-352: a have waiting - // for a person's yes on a newer build names the build that still runs, healthy, and it offers bus-state too. - bus := map[string]catalogue.Manifest{"bus": {Module: "bus", Provides: []catalogue.Offer{{Name: graph.BusState, Scope: catalogue.ScopeMesh}}}} - for _, online := range []bool{false, true} { - for _, phase := range []string{nodeengineclient.PhaseRunning, nodeengineclient.PhaseWaitingYes} { - h := at(fp("2")) - h.Phase = phase - offering := p.offering(bus, assign.Nodes{Online: map[string]bool{"n2": online}, - Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"n2": {"bus": h}}}) - if offering(graph.BusState) != online { - t.Errorf("with the bus module's node online %v in phase %s, bus-state offered %v", online, phase, offering(graph.BusState)) - } - } - } -} - // decodedAsk is an asks-state value with the router's ask it carries decoded (mesh-sdk keeps it as raw JSON). type decodedAsk struct { Asking int @@ -1566,20 +950,10 @@ func aRecordAskIsNotTakenAwayBeforeItsYesIsWrittenAndIsAskedAgainWhenWrittenAgai p.recordAsk(context.Background(), "web", fp, assign.RecordNeed{Node: "novox", Digest: dig}) return readAskID(t, p, rkey) } - rest, _ := asks.RestoreAskKey("novox", "web", fp) - restore := func(p *passer) string { - p.restoreAsk(context.Background(), "web", "novox", fp) - return readAskID(t, p, rest) - } - hold, _ := asks.HoldAskKey("web", fp) - holdIt := func(p *passer) string { - p.holdAsk(context.Background(), "web", fp, &mwc.Held{Node: "novox", Words: "w"}) - return readAskID(t, p, hold) - } for name, c := range map[string]struct { write func(*passer) string key string - }{"record": {rec, rkey}, "restore": {restore, rest}, "hold": {holdIt, hold}} { + }{"record": {rec, rkey}} { at := time.Now() asksKV := &memKV{values: map[string][]byte{}} p := newPasser(kvSet{buckets.Asks(): asksKV, buckets.Wants(): &memKV{values: map[string][]byte{}}}, at) @@ -1606,10 +980,30 @@ func readAskID(t *testing.T, p *passer, key string) string { // readsMsg is a message of the events stream as the assigner's handler reads it. type readsMsg struct { jetstream.Msg - subject string - data []byte + subject string + data []byte + acked, naked bool } -func (m readsMsg) Ack() error { return nil } -func (m readsMsg) Subject() string { return m.subject } -func (m readsMsg) Data() []byte { return m.data } +func (m *readsMsg) Ack() error { m.acked = true; return nil } +func (m *readsMsg) Nak() error { m.naked = true; return nil } +func (m *readsMsg) Subject() string { return m.subject } +func (m *readsMsg) Data() []byte { return m.data } + +// A2/AC1 (design 47 H §750, §764; H-A-70): given an entry of the assignment graph whose acting part's new version does not run on +// its node, when the assigner passes, then it keeps no wait for the part and says no wait-held: no want is held for an acting part. +func TestAnActingPartsNewVersionHoldsNoWant(t *testing.T) { + waits := &memKV{values: map[string][]byte{}} + p := newPasser(kvSet{buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.Asks(): &memKV{values: map[string][]byte{}}, buckets.Waits(): waits}, time.Now()) + var said []string + p.publishWants = func(event string, _ any, _ string) error { said = append(said, event); return nil } + p.w.heardPresence("ace", false, time.Now()) + p.graph = &graphKeeper{} + p.graph.computed.Revision.Entries = []mwseat.NodeEntryRef{{Node: "ace", Module: "nats", Digest: "d1"}} + p.graph.computed.Entries = map[string]mwseat.AssignmentEntry{"d1": {Node: "ace", Module: "nats", Fp: fpTest, Edges: []mwseat.Edge{ + {Kind: "acting", Node: "ace", Module: "mesh-host", Fp: "src2:new", Seat: "node-engine", Step: "compose"}}}} + p.wants(context.Background()) + if len(waits.values) != 0 || len(said) != 0 { + t.Errorf("an acting part's version held the want: kept %v, said %v", waits.values, said) + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins.go deleted file mode 100644 index 7b437b80..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins.go +++ /dev/null @@ -1,539 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "slices" - "sort" - "strconv" - "strings" - "sync" - "time" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - - mdseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/process" - - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/assigngraph" - "mesh-delivery-assigner/internal/assignment" -) - -// The pins of the assignment graph revisions (design 47 G §153, §178, §216, §579; A-169): the gate says on the seat -// mesh-delivery which revisions a delivery record starts (`pinned`) and stops (`freed`) pointing at, and once at each of its -// starts `pins-stated`. The assigner hears them on its durable consumer of the events stream and keeps, in its old `state` -// section `pins` (4 MiB), one key `trunk.` per assignment graph revision whose value is the list of the records pinning -// it, added or removed by compare-and-set before the event is acknowledged; a record heard twice pins once. Two more keys: -// `made`, written once when the state is first used, and `pins-stated`, the `at` of the last pins-stated applied. A revision -// is removed, its object and its key and the entry objects no kept revision lists and no want or running build names -// (§179), when its list is empty and it is not current (the newest the assigner wrote), only while the consumer is caught up -// and once a pins-stated said after `made` was applied; judged on a freed, on a revision ceasing to be current, on being -// caught up, and at start once both hold. Never on a timer, and never by reading the gate's state. - -const ( - pinsState = "pins" - pinsTool = "assigner_pins" - unpinTool = "assigner_unpin" - pinsMadeKey = "made" - pinsStatedKey = "pins-stated" - pinsCapBytes = 4 << 20 - pinMissingKind = "pin-missing" -) - -// PinsValue is one value of the state pins: the records that pin the revision, and whether the revision was no longer held -// when a record pinned it. -type PinsValue struct { - Records []string `json:"records"` - Missing bool `json:"missing,omitempty"` -} - -// pinKey is the key of an assignment graph revision, its object's own name (A-169). -func pinKey(revision int64) string { return assigngraph.TrunkObject(revision) } - -// pinRevision is the revision a key of the state names, and whether it names one. -func pinRevision(key string) (int64, bool) { - n, ok := strings.CutPrefix(key, "trunk.") - if !ok { - return 0, false - } - r, err := strconv.ParseInt(n, 10, 64) - return r, err == nil && r > 0 -} - -// pinObjects is the object bucket of the revisions as the pins remove from it. -type pinObjects interface { - assigngraph.Objects - Delete(ctx context.Context, name string) error -} - -// pinKeeper keeps the pins and removes what nothing pins. -type pinKeeper struct { - ctx context.Context - st endStore - objects pinObjects - // inUse says a want or a running build names the module's fingerprint (§179). - inUse func(module, fp string) bool - findings assign.Findings - now func() time.Time - logf func(string, ...any) - mu sync.Mutex - // caught is whether the last event applied left none of the consumer's messages pending. - caught bool -} - -// edit applies change to the value at key by compare-and-set, again while another write comes between. -func (k *pinKeeper) edit(key string, change func(v PinsValue, found bool) (PinsValue, bool)) error { - for { - raw, rev, err := k.st.get(key) - if err != nil { - return err - } - var v PinsValue - if raw != nil { - if err := json.Unmarshal(raw, &v); err != nil { - return fmt.Errorf("the pins of %s do not read: %w", key, err) - } - } - next, write := change(v, raw != nil) - if !write { - return nil - } - value, _ := json.Marshal(next) - if err := k.st.swap(key, value, rev); errors.Is(err, errSwapped) { - continue - } else { - return err - } - } -} - -// held says the revision's object is kept. -func (k *pinKeeper) held(revision int64) (bool, error) { - _, err := k.objects.Get(k.ctx, assigngraph.TrunkObject(revision)) - if errors.Is(err, assigngraph.ErrNoObject) { - return false, nil - } - return err == nil, err -} - -// pin adds record to each assignment graph revision of p; a revision no longer held is written missing, with the finding -// pin-missing naming the record. -func (k *pinKeeper) pin(p mdseat.Pins) error { - for _, r := range p.Assignment { - held, err := k.held(r) - if err != nil { - return err - } - err = k.edit(pinKey(r), func(v PinsValue, _ bool) (PinsValue, bool) { - if slices.Contains(v.Records, p.Record) && (held || v.Missing) { - return v, false - } - if !slices.Contains(v.Records, p.Record) { - v.Records = append(v.Records, p.Record) - slices.Sort(v.Records) - } - v.Missing = v.Missing || !held - return v, true - }) - if err != nil { - return err - } - if !held { - k.missing(r, p.Record) - } - } - return nil -} - -// free removes record from each assignment graph revision of p; a freed applied twice changes nothing. -func (k *pinKeeper) free(p mdseat.Pins) error { - for _, r := range p.Assignment { - err := k.edit(pinKey(r), func(v PinsValue, found bool) (PinsValue, bool) { - i := slices.Index(v.Records, p.Record) - if !found || i < 0 { - return v, false - } - v.Records = slices.Delete(v.Records, i, i+1) - return v, true - }) - if err != nil { - return err - } - } - return nil -} - -// missing writes the finding pin-missing, scope the assigner, id the revision. -func (k *pinKeeper) missing(revision int64, record string) { - if k.findings.Put == nil { - k.logf("the revision %d the record %s pins is not held, and no finding writer is wired to say so", revision, record) - return - } - o := assign.Observed{Scope: assignment.Module(), ID: strconv.FormatInt(revision, 10), Kind: pinMissingKind, - Summary: fmt.Sprintf("the delivery record %s pins the assignment graph revision %d, which the assigner no longer holds", record, revision), - Headline: "A delivery record pins a revision the assigner no longer holds", - Explanation: fmt.Sprintf("The record %s pinned revision %d after it was removed; %s clears it.", record, revision, unpinTool), - Source: assignment.Module()} - if err := k.findings.Put(o.Finding(k.now())); err != nil { - k.logf("the finding %s of revision %d is not written: %v", pinMissingKind, revision, err) - } -} - -// made writes the key made once, create-only; one there already stays. -func (k *pinKeeper) made() error { - raw, _, err := k.st.get(pinsMadeKey) - if err != nil || raw != nil { - return err - } - value, _ := json.Marshal(k.now().UTC()) - if err := k.st.swap(pinsMadeKey, value, 0); err != nil && !errors.Is(err, errSwapped) { - return err - } - return nil -} - -// timeAt is the time a key holds; false when it holds none. -func (k *pinKeeper) timeAt(key string) (time.Time, bool, error) { - raw, _, err := k.st.get(key) - if err != nil || raw == nil { - return time.Time{}, false, err - } - var t time.Time - if err := json.Unmarshal(raw, &t); err != nil { - return t, false, fmt.Errorf("the key %s does not read: %w", key, err) - } - return t, true, nil -} - -// stated keeps the at of a pins-stated applied. -func (k *pinKeeper) stated(at time.Time) error { - for { - raw, rev, err := k.st.get(pinsStatedKey) - if err != nil { - return err - } - var was time.Time - if raw != nil && json.Unmarshal(raw, &was) == nil && !at.After(was) { - return nil - } - value, _ := json.Marshal(at.UTC()) - if err := k.st.swap(pinsStatedKey, value, rev); errors.Is(err, errSwapped) { - continue - } else { - return err - } - } -} - -// statedSinceMade says a pins-stated said after the state was made was applied. -func (k *pinKeeper) statedSinceMade() (bool, error) { - made, ok, err := k.timeAt(pinsMadeKey) - if err != nil || !ok { - return false, err - } - at, ok, err := k.timeAt(pinsStatedKey) - if err != nil || !ok { - return false, err - } - return at.After(made), nil -} - -// hear applies one of the seat mesh-delivery's events and acknowledges it only once applied. -func (k *pinKeeper) hear(m jetstream.Msg) { - err := k.apply(m.Subject(), m.Data()) - if err != nil { - k.logf("%s: %v; it is heard again", m.Subject(), err) - _ = m.Nak() - return - } - _ = m.Ack() - caught := false - if meta, merr := m.Metadata(); merr == nil { - caught = meta.NumPending == 0 - } - k.mu.Lock() - was := k.caught - k.caught = caught - k.mu.Unlock() - if caught && (!was || m.Subject() != mdseat.SubjectPinned) { - k.judge() // on a freed, on a candidate's heads replaced, on pins-stated, and on being caught up - } -} - -// apply takes one event's body. -func (k *pinKeeper) apply(subject string, data []byte) error { - switch subject { - case mdseat.SubjectPinned, mdseat.SubjectFreed: - var p mdseat.Pins - if err := json.Unmarshal(data, &p); err != nil || p.Record == "" { - k.logf("%s holds no pins naming a record, and is not taken: %v", subject, err) - return nil - } - if subject == mdseat.SubjectPinned { - return k.pin(p) - } - return k.free(p) - case mdseat.SubjectPinsStated: - var s mdseat.PinsStatedEvent - if err := json.Unmarshal(data, &s); err != nil || s.At.IsZero() { - k.logf("%s holds no at, and is not taken: %v", subject, err) - return nil - } - return k.stated(s.At) - } - return nil // heads-replaced: a candidate's revision stops being current, judged after -} - -// judge removes every revision neither pinned nor current, when the rule allows a removal at all. -func (k *pinKeeper) judge() { - k.mu.Lock() - caught := k.caught - k.mu.Unlock() - if !caught { - return - } - ok, err := k.statedSinceMade() - if err != nil { - k.logf("the pins are not judged: %v", err) - return - } - if !ok { - return // no pins-stated applied since the state was made: a pin said before may not be heard yet - } - if err := k.remove(); err != nil { - k.logf("the revisions nothing pins are not removed: %v", err) - } -} - -// remove removes each kept revision that is not current and that no record pins, and then the entries left unlisted. -func (k *pinKeeper) remove() error { - names, err := k.objects.Names(k.ctx) - if err != nil { - return err - } - var kept []int64 - for _, n := range names { - if r, ok := pinRevision(n); ok { - kept = append(kept, r) - } - } - if len(kept) == 0 { - return nil - } - current := slices.Max(kept) - listed := map[string]bool{} - removed := 0 - for _, r := range kept { - raw, _, err := k.st.get(pinKey(r)) - if err != nil { - return err - } - var v PinsValue - if raw != nil { - if err := json.Unmarshal(raw, &v); err != nil { - return fmt.Errorf("the pins of %d do not read: %w", r, err) - } - } - if r == current || len(v.Records) > 0 { - rev, err := assigngraph.ReadRevision(k.ctx, k.objects, r) - if err != nil { - return err - } - for _, e := range rev.Entries { - listed[e.Digest] = true - } - continue - } - if err := k.objects.Delete(k.ctx, assigngraph.TrunkObject(r)); err != nil { - return fmt.Errorf("revision %d: %w", r, err) - } - if raw != nil { - if err := k.st.drop(pinKey(r)); err != nil { - return fmt.Errorf("the pins of %d: %w", r, err) - } - } - removed++ - } - if removed == 0 { - return nil - } - // A run's candidate kept now lists its entries too, until its group-ended drops it (A-201, A-235). - for _, n := range names { - group, heads, ok := meshwants.ParseAssignmentRevisionsByCandidateGroupHeadsKey(n) - if !ok { - continue - } - rev, err := assigngraph.ReadCandidate(k.ctx, k.objects, group, heads) - if err != nil { - return err - } - for _, e := range rev.Entries { - listed[e.Digest] = true - } - } - for _, n := range names { - digest, ok := strings.CutPrefix(n, "entry.") - if !ok || listed[digest] { - continue - } - e, err := assigngraph.ReadEntry(k.ctx, k.objects, digest) - if err != nil { - return err - } - if k.inUse != nil && k.inUse(e.Module, e.Fp) { - continue - } - if err := k.objects.Delete(k.ctx, n); err != nil { - return fmt.Errorf("the entry %s: %w", digest, err) - } - } - k.logf("%d assignment graph revisions no record pins and not current are removed", removed) - return nil -} - -// hearPins registers the seat mesh-delivery's four events on the assigner's hub. -func hearPins(hub *process.EventHub, k *pinKeeper) error { - for _, s := range []string{mdseat.SubjectPinned, mdseat.SubjectFreed, mdseat.SubjectHeadsReplaced, mdseat.SubjectPinsStated} { - if err := hub.Handle(s, k.hear); err != nil { - return fmt.Errorf("%s: %w", s, err) - } - } - return nil -} - -// pinsTools are assigner_pins and assigner_unpin over the runtime's states. -func pinsTools(st store, ask func(string, any) (json.RawMessage, error)) []stdio.Tool { - return []stdio.Tool{{Name: pinsTool, Description: "the assignment graph revisions the delivery records pin (design 47 G §579): per revision " + - "the records that pin it and whether it was missing when pinned, the newest revision kept (current, never removed), when the state " + - "was made, the last pins-stated applied, and the state's fill against its cap of 4 MiB.", - Run: func(map[string]any) (any, error) { return readPins(st) }}, - {Name: unpinTool, Description: "Remove one record from a revision's pins, for a freed that was lost (design 47 G §579); written in the " + - "hand-act log first. The removal rule then applies.", - Input: schema(map[string]string{"revision": "the assignment graph revision's number", "record": "the delivery record's id", - "why": "why it is removed by hand"}, []string{"revision", "record", "why"}), - Run: func(a map[string]any) (any, error) { - record, _ := a["record"].(string) - why, _ := a["why"].(string) - return unpin(st, ask, fmt.Sprint(a["revision"]), record, why, stdio.CallerOf(a)) - }}} -} - -// readPins is what assigner_pins answers. -func readPins(st store) (any, error) { - ks, err := st.keys(pinsState) - if err != nil { - return nil, err - } - sort.Strings(ks) - revisions := map[string]PinsValue{} - out := map[string]any{"revisions": revisions} - var newest int64 - bytes := 0 - for _, key := range ks { - raw, _, found, err := st.get(pinsState, key) - if err != nil || !found { - continue - } - bytes += len(key) + len(raw) - if r, ok := pinRevision(key); ok { - var v PinsValue - if json.Unmarshal(raw, &v) == nil { - revisions[key] = v - } - newest = max(newest, r) - continue - } - out[key] = json.RawMessage(raw) - } - out["fill"] = meshwants.Fill{Store: pinsState, Bytes: int64(bytes), Cap: pinsCapBytes} - if newest > 0 { - out["newest-pinned"] = newest - } - return out, nil -} - -// unpin removes one record from one revision's list, the hand act recorded first. -func unpin(st store, ask func(string, any) (json.RawMessage, error), revision, record, why, by string) (any, error) { - r, err := strconv.ParseInt(strings.TrimPrefix(revision, "trunk."), 10, 64) - switch { - case err != nil || r <= 0: - return nil, fmt.Errorf("revision %q is no assignment graph revision's number", revision) - case record == "" || why == "": - return nil, errors.New("name the record and say why") - case by == "" || by == stdio.Unproven: - return nil, errors.New("an unpin names who asks it, and the runtime proved no caller") - } - key := pinKey(r) - raw, _, found, err := st.get(pinsState, key) - if err != nil { - return nil, err - } - var v PinsValue - if found { - if err := json.Unmarshal(raw, &v); err != nil { - return nil, err - } - } - i := slices.Index(v.Records, record) - if i < 0 { - return nil, fmt.Errorf("the record %s does not pin revision %d: %s lists its pins", record, r, pinsTool) - } - taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: unpinTool + " " + key + " " + record, - Why: why + " (" + by + ")", Cause: "unpin"}) - if err != nil { - return nil, fmt.Errorf("the hand act is not recorded, so nothing is removed: %w", err) - } - v.Records = slices.Delete(v.Records, i, i+1) - value, _ := json.Marshal(v) - if err := st.put(pinsState, key, value); err != nil { - return nil, err - } - return map[string]any{"revision": r, "unpinned": record, "records": v.Records, "hand-act": taken.Log}, nil -} - -// watchUnpins judges again when a revision's pins change in the state itself, as assigner_unpin writes them from the tool -// process: the assigner's own state, heard as it changes, never on a timer. -func (k *pinKeeper) watchUnpins(kv jetstream.KeyValue) { - w, err := kv.WatchAll(k.ctx, jetstream.UpdatesOnly()) - if err != nil { - k.logf("the state %s is not watched, so a hand unpin is judged at the next freed or revision: %v", pinsState, err) - return - } - go func() { - defer func() { _ = w.Stop() }() - for e := range w.Updates() { - if e == nil { - continue - } - if _, ok := pinRevision(e.Key()); ok && e.Operation() == jetstream.KeyValuePut { - k.judge() - } - } - }() -} - -// inUseOf says a want or a running build names the module's fingerprint, as the world holds them now. -func inUseOf(w *world) func(module, fp string) bool { - return func(module, fp string) bool { - for _, want := range w.wantsOf()[module] { - if want.Fp == fp { - return true - } - } - for _, mods := range w.runningNow() { - if r, ok := mods[module]; ok && r.Fingerprint == fp { - return true - } - } - return false - } -} - -func pinsBucket() string { return assignment.Module() + "_" + pinsState } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins_test.go deleted file mode 100644 index de2a17d8..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pins_test.go +++ /dev/null @@ -1,119 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "testing" - "time" - - mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - mdseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - - "mesh-delivery-assigner/internal/assign" - "mesh-delivery-assigner/internal/assigngraph" -) - -func (m memObjects) Delete(_ context.Context, name string) error { delete(m, name); return nil } - -// The pins of the assignment graph revisions (design 47 G §153, §178, §579; A-169): a revision two records pin stays until -// both free it; a record pinning twice pins once; the current revision is never removed; nothing is removed before a -// pins-stated said after the state was made, nor while the consumer is behind; a revision no longer held is pinned missing, -// with the finding pin-missing; an entry another kept revision lists, or a want or a running build names, stays. -func TestPinsKeepARevisionWhileARecordPinsIt(t *testing.T) { - objects := memObjects{} - entry := func(module, fp string) string { - e := meshwants.AssignmentEntry{Node: "n1", Module: module, Fp: fp} - d := assigngraph.EntryDigest(e) - raw, _ := json.Marshal(e) - objects[assigngraph.EntryObject(d)] = raw - return d - } - shared, old, running := entry("a", "fp-a"), entry("b", "fp-b1"), entry("c", "fp-c1") - revision := func(n int64, digests ...string) { - rev := meshwants.AssignmentRevision{Revision: n} - for _, d := range digests { - rev.Entries = append(rev.Entries, meshwants.NodeEntryRef{Node: "n1", Digest: d}) - } - raw, _ := json.Marshal(rev) - objects[assigngraph.TrunkObject(n)] = raw - } - // A run's candidate lists an entry no trunk revision lists (A-201): it stays while the candidate is kept. - cand := entry("d", "fp-d2") - candRaw, _ := json.Marshal(meshwants.AssignmentRevision{Revision: 1, Kind: "candidate", Group: "run-7", Heads: "h1", - Entries: []meshwants.NodeEntryRef{{Node: "n1", Module: "d", Digest: cand}}}) - objects[assigngraph.CandidateObject("run-7", "h1")] = candRaw - revision(1, shared, old, running) - revision(2, shared, old) - revision(3, shared) - var found []mcseat.Finding - clock := time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC) - k := &pinKeeper{ctx: context.Background(), st: newMemEnds(), objects: objects, now: func() time.Time { return clock }, - logf: t.Logf, - inUse: func(module, fp string) bool { return module == "c" && fp == "fp-c1" }, - findings: assign.Findings{Put: func(f mcseat.Finding) error { found = append(found, f); return nil }}} - if err := k.made(); err != nil { - t.Fatal(err) - } - say := func(subject string, body any) { - t.Helper() - raw, _ := json.Marshal(body) - if err := k.apply(subject, raw); err != nil { - t.Fatal(err) - } - k.judge() - } - pins := func(record string, revs ...int64) mdseat.Pins { return mdseat.Pins{Record: record, Assignment: revs} } - has := func(n int64) bool { _, ok := objects[assigngraph.TrunkObject(n)]; return ok } - - k.caught = true - say(mdseat.SubjectPinned, pins("r1", 1)) - say(mdseat.SubjectPinned, pins("r1", 1)) - say(mdseat.SubjectPinned, pins("r2", 1)) - say(mdseat.SubjectFreed, pins("r9", 2)) // nothing pins 2, but no pins-stated since made: nothing removed - if !has(2) { - t.Fatal("revision 2 was removed before a pins-stated said after the state was made") - } - k.caught = false - say(mdseat.SubjectPinsStated, mdseat.PinsStatedEvent{At: clock.Add(time.Minute)}) - if !has(2) { - t.Fatal("revision 2 was removed while the consumer was behind") - } - k.caught = true - say(mdseat.SubjectFreed, pins("r1", 1)) - if has(2) { - t.Error("revision 2, neither pinned nor current, is kept") - } - if !has(1) || !has(3) { - t.Error("revision 1 (pinned by r2) or 3 (current) was removed") - } - raw, _, _ := k.st.get(pinKey(1)) - if string(raw) != `{"records":["r2"]}` { - t.Errorf("the pins of 1 = %s, want r2 alone (r1 pinned twice pins once, and freed once)", raw) - } - say(mdseat.SubjectFreed, pins("r2", 1)) - if has(1) { - t.Error("revision 1 is kept once both records freed it") - } - if _, ok := objects[assigngraph.EntryObject(old)]; ok { - t.Error("the entry no kept revision lists and nothing names is kept") - } - if _, ok := objects[assigngraph.EntryObject(running)]; !ok { - t.Error("the entry of a running build is removed") - } - if _, ok := objects[assigngraph.EntryObject(shared)]; !ok { - t.Error("the entry the current revision lists is removed") - } - if _, ok := objects[assigngraph.EntryObject(cand)]; !ok { - t.Error("the entry a run's kept candidate lists is removed") - } - say(mdseat.SubjectHeadsReplaced, mdseat.HeadsReplacedEvent{}) - if _, ok := objects[assigngraph.CandidateObject("run-7", "h1")]; !ok { - t.Error("the gate's heads-replaced dropped a run's candidate") - } - say(mdseat.SubjectPinned, pins("r3", 2)) - raw, _, _ = k.st.get(pinKey(2)) - if string(raw) != `{"records":["r3"],"missing":true}` || len(found) != 1 || found[0].Kind != pinMissingKind || found[0].ID != "2" { - t.Errorf("a pin of a revision no longer held = %s, findings %+v", raw, found) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines.go index 5baf09ba..0ca00286 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines.go @@ -16,18 +16,20 @@ import ( mcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mprc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient" - mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" mpcat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/keys" "git.novox.be/novox/mesh-sdk/go/process" + "mesh-delivery-assigner/internal/assign" ) -// The wants follow a passed pipeline (design 47 G §29, §35, §325, §352): the runner says each pipeline's end as -// pipeline-ended, and a group's as group-ended. The assigner keeps, in its old state `written`, one key per module and -// watched branch: the source revision and end count of the newest end it heard for it, and whether the wants were written -// from it. A newer end replaces it by compare-and-set; an older one, or the same one again, writes nothing. +// The wants follow a passed pipeline (design 47 H §750): the runner says each pipeline's end once, per source revision, as +// pipeline-ended on `pipeline-ended..`, naming per mover its fingerprint, its released version and its tier +// (§746). The assigner keeps, in its state `written` (G §352), one key per module and watched branch: the newest released +// version of the module on that branch whose pipeline passed, with the pipeline and the mover's rank in it. A newer version +// replaces it by compare-and-set; an older one, the same one again, a failed pipeline and a run on request write nothing. // writtenState is the assigner's state of the pipeline ends its wants follow; writtenTool reads it. const ( @@ -35,23 +37,17 @@ const ( writtenTool = "assigner_written" ) -// pipelineEndedSubject and groupEndedSubject are the runner's events the assigner consumes. -const ( - pipelineEndedSubject = "mesh.seat." + mpr.Seat + ".event." + mpr.EventPipelineEnded + ".>" - groupEndedSubject = "mesh.seat." + mpr.Seat + ".event." + mpr.EventGroupEnded + ".>" -) +// pipelineEndedSubject is the runner's event the assigner consumes. +const pipelineEndedSubject = "mesh.seat." + mpr.Seat + ".event." + mpr.EventPipelineEnded + ".>" -// End is one value of the state written: the newest end of a module's pipeline on a watched branch. +// End is one value of the state written: the newest version of a module released on a watched branch whose pipeline passed. type End struct { + Pipeline string `json:"pipeline"` Source int64 `json:"source"` - EndCount int64 `json:"end-count"` Fp string `json:"fp"` - Commit string `json:"commit,omitempty"` - // Passed: the pipeline ended passed, every required step passed (and, for a group, the group ended passed). - Passed bool `json:"passed"` - // Written: the wants were written from this end. - Written bool `json:"written,omitempty"` - Why string `json:"why,omitempty"` + Version string `json:"version"` + // Rank is the mover's tier in the pipeline's delivery order, from 1 (§746, §750). + Rank int64 `json:"rank"` } // endKey is the key of a module on a watched branch, each name escaped as a key's name is. @@ -59,15 +55,10 @@ func endKey(module, branch string) string { return keys.EscapeName(module) + "." + keys.EscapeName(branch) } -// newer says whether an end of (source, count) replaces the one kept: a later source revision, or the same one ended again. -func (e End) newer(source, count int64) bool { - return source > e.Source || (source == e.Source && count > e.EndCount) -} - -// requiredPassed says whether no required step of a pipeline stands failed, unasked or unended (design 47 G §30: a failed -// optional step holds nothing). -func requiredPassed(p mpr.PipelineEnded) bool { - for _, s := range p.Steps { +// moverPassed says whether no required step of a mover stands failed, unasked or unended: a required step ending "nothing to +// run" counts as passed, as a bypassed one does (design 47 H §746, H-A-3); a failed optional step holds nothing (G §30). +func moverPassed(m mpr.MoverEnded) bool { + for _, s := range m.Steps { if s.Status != mpr.StepResultStatusRequired { continue } @@ -78,7 +69,7 @@ func requiredPassed(p mpr.PipelineEnded) bool { return false } } - return p.Outcome == mpr.PipelineEndedOutcomePassed + return true } // endStore is the kept state written: the bus bucket, or a test's. @@ -139,19 +130,19 @@ func isWrongSequence(err error) bool { return errors.As(err, &ae) && ae.ErrorCode == jetstream.JSErrCodeStreamWrongLastSequence } -// pipelineEnds follows the runner's ends. A nil *pipelineEnds is the trunk-only want, kept until the first registrations -// (design 47 G §95, §353). +// pipelineEnds follows the runner's ends; a nil *pipelineEnds keeps none, and no want is written from it. type pipelineEnds struct { st endStore wake func() logf func(string, ...any) - // on is whether the wants follow the ends: set by the first registration heard, never before (design 47 G §95, §353). + // on says the first registration was heard (design 47 G §353), said once in the journal. on atomic.Bool mu sync.Mutex // firsts is, by module, the first watched branch of its registration, the branch of a placement that names none (design // 47 G §14). firsts map[string]string - // runEnded is told a run on request's group-ended, its group `run-`, to drop the run's candidate (A-235); nil drops nothing. + // runEnded is told a run on request's pipeline-ended, its group `run-`, to drop the run's candidate (A-235, H-A-58); nil + // drops nothing. runEnded func(group string) error // left is told the key of a pipeline ended superseded or cancelled, whose composition steps leave the runs they read // (design 47 G §723, A-469n); none tells nothing. Set once the composition steps are served, after the hub starts. @@ -160,110 +151,131 @@ type pipelineEnds struct { onBranches func([]string) // trunk names the first watched branch, the trunk's (design 47 G §507), the branch of a module registered with none. trunk func() string - // runner is the module holding the seat mesh-pipeline-runner, as the last pass read it from the acting edges (A-334); nil - // until a pass read the graph. - runner atomic.Pointer[string] - // held are the group members' pipeline-ended heard before a pass read the graph, by their body: unacknowledged, so the - // bus keeps them, and taken once the runner is read (A-334), never dropped for the runner not being known yet. - held map[string]jetstream.Msg -} - -// setRunner keeps the module holding the seat mesh-pipeline-runner, read by the pass from the acting edges (A-334); the -// first one takes the group members' ends held until the graph was read. -func (e *pipelineEnds) setRunner(module string) { - e.mu.Lock() - first := e.runner.Swap(&module) == nil - held := e.held - e.held = nil - e.mu.Unlock() - if !first { - return - } - for _, m := range held { - e.hearPipeline(m) - } -} - -// holdUntilRead keeps m, a group member's pipeline-ended, until a pass reads the runner from the graph; false when the -// runner is read already, so m is taken now. -func (e *pipelineEnds) holdUntilRead(m jetstream.Msg) bool { - e.mu.Lock() - defer e.mu.Unlock() - if e.runner.Load() != nil { - return false - } - if e.held == nil { - e.held = map[string]jetstream.Msg{} - } - e.held[string(m.Data())] = m - e.logf("%s is a group member's end heard before the assignment graph is read: held until a pass reads which module is the runner (A-334)", m.Subject()) - return true -} - -// isRunner says whether module holds the seat mesh-pipeline-runner, as the last pass read it. -func (e *pipelineEnds) isRunner(module string) bool { - r := e.runner.Load() - return r != nil && *r != "" && *r == module + // now is the clock of the tier progress (tiers.go); nil is time.Now. + now func() time.Time } func newPipelineEnds(st endStore, wake func(), logf func(string, ...any)) *pipelineEnds { return &pipelineEnds{st: st, wake: wake, logf: logf, firsts: map[string]string{}} } -// take keeps one pipeline's end if it is newer than the one kept for its module and branch, by compare-and-set. -func (e *pipelineEnds) take(p mpr.PipelineEnded, passed bool) error { - key := endKey(p.Module, p.Branch) - for { - raw, rev, err := e.st.get(key) - if err != nil { - return err +// take keeps, for each mover of a passed pipeline that released a version, that version as the newest of its module on the +// pipeline's branch, by compare-and-set on a newer version (design 47 H §750): an older version, or the same one again, writes +// nothing. It says whether any key changed. +func (e *pipelineEnds) take(p mpr.PipelineEnded) (bool, error) { + if p.Outcome != mpr.PipelineEndedOutcomePassed || p.Run != "" { + return false, nil + } + changed := false + var kept []tierMover + defer func() { + if err := e.startTiers(p.Pipeline, kept); err != nil { + e.logf("the tier progress of %s is not written: %v", p.Pipeline, err) } - if raw != nil { - var kept End - if err := json.Unmarshal(raw, &kept); err == nil && !kept.newer(p.Source, p.EndCount) { - return nil // an older end, or this one again + }() + for _, m := range p.Movers { + if m.Version == "" || m.Fingerprint == "" || !moverPassed(m) { + continue + } + key := endKey(m.Module, p.Branch) + for { + raw, rev, err := e.st.get(key) + if err != nil { + return changed, err } + if raw != nil { + var kept End + if err := json.Unmarshal(raw, &kept); err == nil && compareVersion(m.Version, kept.Version) <= 0 { + break // an older version, or this one again + } + } + value, err := json.Marshal(End{Pipeline: p.Pipeline, Source: p.Source, Fp: m.Fingerprint, Version: m.Version, Rank: max(m.Tier, 1)}) + if err != nil { + return changed, err + } + if err := e.st.swap(key, value, rev); errors.Is(err, errSwapped) { + continue + } else if err != nil { + return changed, err + } + changed = true + kept = append(kept, tierMover{Module: m.Module, Fp: m.Fingerprint, Tier: max(m.Tier, 1)}) + break } - end := End{Source: p.Source, EndCount: p.EndCount, Fp: p.Fingerprint, Commit: p.Commit, Passed: passed, Why: p.Why} - value, err := json.Marshal(end) - if err != nil { - return err - } - if err := e.st.swap(key, value, rev); errors.Is(err, errSwapped) { - continue - } else if err != nil { - return err - } - e.wake() - return nil } + return changed, nil } -// markWritten says the wants of module on branch were written from the end it keeps, if that is still the end of fp. -func (e *pipelineEnds) markWritten(module, branch, fp string) { - key := endKey(module, branch) - for { - raw, rev, err := e.st.get(key) - var kept End - if err != nil || raw == nil || json.Unmarshal(raw, &kept) != nil || kept.Fp != fp || kept.Written { - return - } - kept.Written = true - value, _ := json.Marshal(kept) - if err := e.st.swap(key, value, rev); errors.Is(err, errSwapped) { - continue - } else if err != nil { - e.logf("the end of %s on %s is not marked written: %v", module, branch, err) - } - return +// endOf is the end kept for module on branch: the newest version released there whose pipeline passed. +func (e *pipelineEnds) endOf(module, branch string) (End, bool) { + if e == nil { + return End{}, false } -} - -// passedAt says whether the newest end kept for module on branch passed at fp. -func (e *pipelineEnds) passedAt(module, branch, fp string) bool { raw, _, err := e.st.get(endKey(module, branch)) var kept End - return err == nil && raw != nil && json.Unmarshal(raw, &kept) == nil && kept.Passed && kept.Fp == fp + if err != nil || raw == nil || json.Unmarshal(raw, &kept) != nil || kept.Fp == "" { + return End{}, false + } + return kept, true +} + +// target is the build module's wants on branch name (design 47 H §750): the newest version released there whose pipeline +// passed, with the pipeline and the mover's rank; false when none is kept, or its pipeline's tier progress has not reached its +// rank (§751). +func (e *pipelineEnds) target(module, branch string) (assign.Target, bool) { + end, ok := e.endOf(module, e.branchOf(module, branch)) + if !ok || !e.tierWritten(end) { + return assign.Target{}, false + } + return assign.Target{Fp: end.Fp, Version: end.Version, Pipeline: end.Pipeline, Rank: end.Rank}, true +} + +// hearPipeline takes a pipeline-ended: a passed one's released versions are kept (take), and a run on request's drops the +// run's candidate assignment graph (A-235, H-A-58); a run delivers nothing (design 47 G §29, §64). A pipeline ended superseded or +// cancelled has its composition steps leave the runs they read (G §723). +func (e *pipelineEnds) hearPipeline(m jetstream.Msg) { + p, err := mprc.PipelineEnded(m.Data()) + if err != nil { + e.logf("%s is not taken: %v", m.Subject(), err) + _ = m.Ack() + return + } + if left := e.left.Load(); left != nil && (p.Outcome == mpr.PipelineEndedOutcomeSuperseded || p.Outcome == mpr.PipelineEndedOutcomeCancelled) { + if err := (*left)(p.Pipeline); err != nil { + e.logf("%s: the composition steps of %s do not all leave their runs: %v; it is heard again", m.Subject(), p.Pipeline, err) + _ = m.Nak() + return + } + } + if p.Run != "" { + if e.runEnded != nil { + if err := e.runEnded(runGroupPrefix + p.Run); err != nil { + e.logf("%s: %v; it is heard again", m.Subject(), err) + _ = m.Nak() + return + } + } + _ = m.Ack() + return + } + changed, err := e.take(p) + if err != nil { + e.logf("%s: %v; it is heard again", m.Subject(), err) + _ = m.Nak() + return + } + if changed { + e.wake() + } + _ = m.Ack() +} + +// hearEnds registers pipeline-ended on the assigner's hub. +func hearEnds(hub *process.EventHub, e *pipelineEnds) error { + if err := hub.Handle(pipelineEndedSubject, e.hearPipeline); err != nil { + return fmt.Errorf("%s: %w", pipelineEndedSubject, err) + } + return nil } // branchOf is the watched branch a placement names, else the first of the module's registration, else the trunk's. @@ -282,79 +294,9 @@ func (e *pipelineEnds) branchOf(module, placed string) string { return "" } -// hearPipeline takes a pipeline-ended. A pipeline of a group writes nothing on its own end, but the runner's: in a group -// take-in that changes the runner it is the acting part the other members wait on, so it goes first, wanted on its own end -// once that passed, ahead of the group (A-334, as the node-engine does, §268). A run on request delivers nothing (design 47 G -// §29, §64). -func (e *pipelineEnds) hearPipeline(m jetstream.Msg) { - p, err := mprc.PipelineEnded(m.Data()) - if left := e.left.Load(); err == nil && left != nil && (p.Outcome == mpr.PipelineEndedOutcomeSuperseded || p.Outcome == mpr.PipelineEndedOutcomeCancelled) { - if err := (*left)(p.Pipeline); err != nil { - e.logf("%s: the composition steps of %s do not all leave their runs: %v; it is heard again", m.Subject(), p.Pipeline, err) - _ = m.Nak() - return - } - } - switch { - case err != nil: - e.logf("%s is not taken: %v", m.Subject(), err) - case p.Run == "" && p.Group != "" && e.holdUntilRead(m): - return - case p.Run != "" || (p.Group != "" && !e.isRunner(p.Module)): - default: - if err := e.take(p, requiredPassed(p)); err != nil { - e.logf("%s: %v; it is heard again", m.Subject(), err) - _ = m.Nak() - return - } - } - _ = m.Ack() -} - -// hearGroup takes a group-ended: each member's end, passed only if the group passed. A run on request's (group `run-`) delivers -// nothing: it drops the run's candidate assignment graph (A-235). -func (e *pipelineEnds) hearGroup(m jetstream.Msg) { - g, err := mprc.GroupEnded(m.Data()) - if err != nil { - e.logf("%s is not taken: %v", m.Subject(), err) - _ = m.Ack() - return - } - if strings.HasPrefix(g.Group, runGroupPrefix) { - if e.runEnded != nil { - if err := e.runEnded(g.Group); err != nil { - e.logf("%s: %v; it is heard again", m.Subject(), err) - _ = m.Nak() - return - } - } - _ = m.Ack() - return - } - for _, p := range g.Pipelines { - if err := e.take(p, g.Outcome == mpr.GroupEndedOutcomePassed && requiredPassed(p)); err != nil { - e.logf("%s: %v; it is heard again", m.Subject(), err) - _ = m.Nak() - return - } - } - _ = m.Ack() -} - -// hearEnds registers the two events on the assigner's hub. -func hearEnds(hub *process.EventHub, e *pipelineEnds) error { - if err := hub.Handle(pipelineEndedSubject, e.hearPipeline); err != nil { - return fmt.Errorf("%s: %w", pipelineEndedSubject, err) - } - if err := hub.Handle(groupEndedSubject, e.hearGroup); err != nil { - return fmt.Errorf("%s: %w", groupEndedSubject, err) - } - return nil -} - // writtenTools is the tool assigner_written: every key of the state written, with its end. func writtenTools(st store) []stdio.Tool { - return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the source revision and end count of the newest end heard, whether it passed and whether the wants were written from it; with why, why each want is what it is (as assigner_why).", + return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the newest version released whose pipeline passed, with its pipeline and rank; per pipeline whose wants are written tier by tier, its tier progress (tier, since, movers); with why, why each want is what it is (as assigner_why).", Input: schema(map[string]string{"why": "true: why each want is what it is", "module": "with why: a module's name", "node": "with why: a node's name"}, nil, "why"), Run: func(args map[string]any) (any, error) { if args["why"] == true || args["why"] == "true" { @@ -365,7 +307,7 @@ func writtenTools(st store) []stdio.Tool { return nil, err } sort.Strings(ks) - out := map[string]End{} + out := map[string]any{} for _, k := range ks { if strings.HasPrefix(k, whyPrefix) { continue // a want's why (why.go), read by assigner_why @@ -374,6 +316,13 @@ func writtenTools(st store) []stdio.Tool { if err != nil || !ok { continue } + if isTierKey(k) { // a pipeline's tier progress (tiers.go) + var t tierProgress + if json.Unmarshal(raw, &t) == nil { + out[k] = t + } + continue + } var e End if json.Unmarshal(raw, &e) == nil { out[k] = e @@ -383,56 +332,6 @@ func writtenTools(st store) []stdio.Tool { }}} } -// branchesOf is the watched branches the placements of module chose, each once; the default branch when none names one. -func (e *pipelineEnds) branchesOf(module string, placed map[string]map[string]string) []string { - if !e.following() { - return nil - } - seen := map[string]bool{} - var out []string - for _, b := range placed[module] { - b = e.branchOf(module, b) - if !seen[b] { - seen[b] = true - out = append(out, b) - } - } - if len(out) == 0 { - out = append(out, e.branchOf(module, "")) - } - sort.Strings(out) - return out -} - -// mayWant says whether module's wants may name fp: a pipeline passed at fp on every branch, or the wants name fp already -// (a node placed later, a module taken away). -func (e *pipelineEnds) mayWant(module, fp string, branches []string, wanted map[string]mwc.Want) bool { - if !e.following() { - return true - } - for _, w := range wanted { - if w.Fp == fp { - return true - } - } - for _, b := range branches { - if !e.passedAt(module, b, fp) { - return false - } - } - return true -} - -// wrote marks the end the wants of module were written from. -func (e *pipelineEnds) wrote(module, fp string, branches []string) { - if !e.following() { - return - } - for _, b := range branches { - e.markWritten(module, b, fp) - } -} - // following says whether the wants follow the pipeline ends yet; a nil follower never does. func (e *pipelineEnds) following() bool { return e != nil && e.on.Load() } @@ -454,7 +353,7 @@ func (e *pipelineEnds) registered(reg mpcat.Registration, gone bool) { prefix := keys.EscapeName(reg.Module) + "." for _, k := range kept { rest, ok := strings.CutPrefix(k, prefix) - if !ok { + if !ok || isTierKey(k) || strings.HasPrefix(k, whyPrefix) { continue } branch, err := keys.UnescapeName(rest) @@ -476,7 +375,7 @@ func (e *pipelineEnds) registered(reg mpcat.Registration, gone bool) { // followRegistrations follows the registrations through the seat mesh-catalog (design 47 G §353): a first view of its verb // and each registration-changed on the hub. It returns when the hub's handler is registered; the first view is asked on its own -// goroutine, the wants keep the trunk-only form until a registration is heard. +// goroutine. func followRegistrations(ctx context.Context, hub *process.EventHub, call mcc.Call, e *pipelineEnds) error { regs := mcc.NewRegistrations(call) regs.Follower.Logf = e.logf diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines_test.go index 37533c01..53de5242 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pipelines_test.go @@ -4,35 +4,24 @@ import ( "context" "encoding/json" "fmt" - mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - plc "mesh-delivery-assigner/internal/assignment" "slices" "strings" "testing" "time" + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" mpcat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" - - oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" - "git.novox.be/novox/mesh-sdk/go/catalogue" "github.com/nats-io/nats.go/jetstream" - "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "mesh-delivery-assigner/internal/assigngraph" + plc "mesh-delivery-assigner/internal/assignment" "mesh-delivery-assigner/internal/buckets" - - mws "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" ) // memEnds is endStore over a map, with revisions. @@ -72,250 +61,156 @@ func (m *heardMsg) Subject() string { return m.subject } func (m *heardMsg) Ack() error { m.acked = true; return nil } func (m *heardMsg) Nak() error { m.naked = true; return nil } -func pipe(module, branch string, source, count int64, fp, outcome string, steps ...mpr.StepResult) mpr.PipelineEnded { - return mpr.PipelineEnded{Module: module, Branch: branch, Source: source, EndCount: count, Fingerprint: fp, Outcome: outcome, Steps: steps, - Pipeline: branch + "." + module, Commit: "c"} +// mover is one mover at its pipeline's end, its required unit step ending outcome. +func mover(module, fp, version string, tier int64, outcome string) mpr.MoverEnded { + return mpr.MoverEnded{Module: module, Fingerprint: fp, Version: version, Tier: tier, + Steps: []mpr.StepResult{{Step: "unit", Status: mpr.StepResultStatusRequired, Outcome: outcome, Module: module}}} } -func step(status, outcome string) mpr.StepResult { - return mpr.StepResult{Step: "unit", Status: status, Outcome: outcome} +// ended is the pipeline-ended of the take-in of source on branch, its movers given. +func ended(branch string, source int64, outcome string, movers ...mpr.MoverEnded) mpr.PipelineEnded { + return mpr.PipelineEnded{V: 1, Branch: branch, Source: source, EndCount: 1, Outcome: outcome, Movers: movers, + Pipeline: fmt.Sprintf("%s.%d", branch, source)} } -// Design 47 G §29, §35, §325, §352 (tests 66, 67, 89): a want follows a passed pipeline, by compare-and-set per module and branch. -func TestAWantFollowsAPassedPipeline(t *testing.T) { - const fp1, fp2 = "fp1", "fp2" +// A6/AC1 to AC3 (design 47 H §750, H-A-3): a passed pipeline keeps each mover's released version as the newest of its module on +// the branch, with the pipeline and the mover's tier; a failed pipeline, a mover whose required step failed, one that released +// no version, an older version, the same one again and a run on request keep nothing. +func TestThePassedVersionIsKept(t *testing.T) { pass, fail := mpr.PipelineEndedOutcomePassed, mpr.PipelineEndedOutcomeFailed - req, opt := mpr.StepResultStatusRequired, mpr.StepResultStatusOptional + ok, failed, nothing := mpr.StepResultOutcomePassed, mpr.StepResultOutcomeFailed, mpr.StepResultOutcomeNothingToRun for _, c := range []struct { - name string - ends []mpr.PipelineEnded - group *mpr.GroupEnded - module string - branch string - fp string - may bool + name string + ends []mpr.PipelineEnded + want *End // nil: none kept for web on main }{ - {"a failed required step writes no want", []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, fail, step(req, "failed"))}, nil, "web", "main", fp1, false}, - {"a pipeline passed writes", []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, pass, step(req, "passed"))}, nil, "web", "main", fp1, true}, - {"a failed optional step holds nothing", []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, pass, step(req, "passed"), step(opt, "failed"))}, nil, "web", "main", fp1, true}, - {"a pipeline passed at another fingerprint", []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, pass)}, nil, "web", "main", fp2, false}, - {"an older revision ending after a newer one writes nothing", []mpr.PipelineEnded{pipe("web", "main", 6, 1, fp2, fail), pipe("web", "main", 5, 1, fp1, pass)}, nil, "web", "main", fp1, false}, - {"another branch writes its own", []mpr.PipelineEnded{pipe("web", "main", 6, 1, fp2, fail), pipe("web", "beta", 5, 1, fp1, pass)}, nil, "web", "beta", fp1, true}, - {"a later end of the same revision writes", []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, fail, step(req, "failed")), pipe("web", "main", 5, 2, fp1, pass, step(req, "passed"))}, nil, "web", "main", fp1, true}, - {"the same end again after a later one keeps the later", []mpr.PipelineEnded{pipe("web", "main", 5, 2, fp1, pass), pipe("web", "main", 5, 1, fp1, fail)}, nil, "web", "main", fp1, true}, - {"a member's own end writes nothing", []mpr.PipelineEnded{func() mpr.PipelineEnded { p := pipe("web", "main", 5, 1, fp1, pass); p.Group = "g"; return p }()}, nil, "web", "main", fp1, false}, - {"a group ended passed writes its members", nil, &mpr.GroupEnded{Branch: "main", Group: "g", Outcome: mpr.GroupEndedOutcomePassed, Pipelines: []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, pass)}}, "web", "main", fp1, true}, - {"a group ended failed writes none", nil, &mpr.GroupEnded{Branch: "main", Group: "g", Outcome: mpr.GroupEndedOutcomeFailed, Pipelines: []mpr.PipelineEnded{pipe("web", "main", 5, 1, fp1, pass)}}, "web", "main", fp1, false}, + {"a passed pipeline keeps its version", []mpr.PipelineEnded{ended("main", 5, pass, mover("web", "fp1", "0.1.2", 2, ok))}, + &End{Pipeline: "main.5", Source: 5, Fp: "fp1", Version: "0.1.2", Rank: 2}}, + {"nothing to run counts as passed", []mpr.PipelineEnded{ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, nothing))}, + &End{Pipeline: "main.5", Source: 5, Fp: "fp1", Version: "0.1.2", Rank: 1}}, + {"a failed pipeline keeps nothing, a passed mover in it neither", []mpr.PipelineEnded{ended("main", 5, fail, mover("web", "fp1", "0.1.2", 1, ok), + mover("db", "fp9", "0.3.0", 1, failed))}, nil}, + {"a mover whose required step failed keeps nothing", []mpr.PipelineEnded{ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, failed))}, nil}, + {"no version released keeps nothing", []mpr.PipelineEnded{ended("main", 5, pass, mover("web", "fp1", "", 1, ok))}, nil}, + {"an older end heard after a newer one keeps the newer", []mpr.PipelineEnded{ended("main", 6, pass, mover("web", "fp2", "0.1.3", 1, ok)), + ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, ok))}, &End{Pipeline: "main.6", Source: 6, Fp: "fp2", Version: "0.1.3", Rank: 1}}, + {"a newer version replaces", []mpr.PipelineEnded{ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, ok)), + ended("main", 6, pass, mover("web", "fp2", "0.1.10", 1, ok))}, &End{Pipeline: "main.6", Source: 6, Fp: "fp2", Version: "0.1.10", Rank: 1}}, + {"a run on request keeps nothing", []mpr.PipelineEnded{func() mpr.PipelineEnded { + p := ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, ok)) + p.Run = "r1" + return p + }()}, nil}, } { - woken := 0 - e := newPipelineEnds(newMemEnds(), func() { woken++ }, t.Logf) - e.on.Store(true) + e := newPipelineEnds(newMemEnds(), func() {}, t.Logf) for _, p := range c.ends { - if p.Group != "" { - continue // heard and ignored, as hearPipeline does - } - if err := e.take(p, requiredPassed(p)); err != nil { - t.Fatal(err) + if _, err := e.take(p); err != nil { + t.Fatalf("%s: %v", c.name, err) } } - if g := c.group; g != nil { - for _, p := range g.Pipelines { - if err := e.take(p, g.Outcome == mpr.GroupEndedOutcomePassed && requiredPassed(p)); err != nil { - t.Fatal(err) - } - } - } - if got := e.mayWant(c.module, c.fp, []string{c.branch}, nil); got != c.may { - t.Errorf("%s: may want = %v, want %v", c.name, got, c.may) + got, kept := e.endOf("web", "main") + switch { + case c.want == nil && kept: + t.Errorf("%s: kept %+v", c.name, got) + case c.want != nil && (!kept || got != *c.want): + t.Errorf("%s: kept %+v (%v), want %+v", c.name, got, kept, *c.want) } } - // A run on request's group-ended (group run-, A-235) writes no want and drops the run's candidate; a drop that fails is - // heard again. - { - e := newPipelineEnds(newMemEnds(), func() {}, t.Logf) - e.on.Store(true) - var dropped []string - fail := false - e.runEnded = func(group string) error { - dropped = append(dropped, group) - if fail { - return fmt.Errorf("the bucket is not open") - } - return nil - } - raw, _ := json.Marshal(mpr.GroupEnded{V: 1, Branch: "main", Group: "run-7", Outcome: mpr.GroupEndedOutcomePassed, - Pipelines: []mpr.PipelineEnded{func() mpr.PipelineEnded { p := pipe("web", "main", 5, 1, fp1, pass); p.Run = "7"; return p }()}}) - m := &heardMsg{data: raw, subject: "mesh.seat.mesh-pipeline-runner.event.group-ended.x"} - e.hearGroup(m) - if e.mayWant("web", fp1, []string{"main"}, nil) || !slices.Equal(dropped, []string{"run-7"}) || !m.acked { - t.Errorf("a run's group-ended: may want %v, dropped %v, acked %v; want no want, run-7 dropped, acked", - e.mayWant("web", fp1, []string{"main"}, nil), dropped, m.acked) - } - fail = true - m = &heardMsg{data: raw, subject: m.subject} - e.hearGroup(m) - if !m.naked { - t.Error("a run's candidate not dropped is not heard again") + // The same end heard again changes nothing and wakes nothing; a run on request's end drops its candidate. + woken := 0 + e := newPipelineEnds(newMemEnds(), func() { woken++ }, t.Logf) + var dropped []string + e.runEnded = func(group string) error { dropped = append(dropped, group); return nil } + p := ended("main", 5, pass, mover("web", "fp1", "0.1.2", 1, ok)) + for range 2 { + raw, _ := json.Marshal(p) + m := &heardMsg{data: raw, subject: "mesh.seat.mesh-pipeline-runner.event.pipeline-ended.main.5"} + e.hearPipeline(m) + if !m.acked { + t.Error("the end was not acknowledged") } } - // A-334 (§29, §65, §268): in a group take-in that changes the runner, the runner's member (the holder of the seat - // mesh-pipeline-runner, read from the acting edges) is wanted on its own pipeline-ended once it passed, ahead of the group; - // the other members wait for group-ended. - { - e := newPipelineEnds(newMemEnds(), func() {}, t.Logf) - e.on.Store(true) - heard := func(p mpr.PipelineEnded) *heardMsg { - p.Group = "g" - raw, _ := json.Marshal(p) - m := &heardMsg{data: raw, subject: "mesh.seat.mesh-pipeline-runner.event.pipeline-ended.x"} - e.hearPipeline(m) - return m - } - hear := func(p mpr.PipelineEnded) { - if m := heard(p); !m.acked { - t.Errorf("the pipeline-ended of %s is not acknowledged", p.Module) - } - } - // The runner's end heard before a pass read the graph is held, unacknowledged, and taken once the runner is read. - early := heard(pipe("runner", "main", 5, 1, "fp-runner", pass, step(req, "passed"))) - if early.acked || early.naked || e.mayWant("runner", "fp-runner", []string{"main"}, nil) { - t.Fatalf("the runner's end before the graph is read: acked %v, naked %v, wanted %v; want it held", early.acked, early.naked, - e.mayWant("runner", "fp-runner", []string{"main"}, nil)) - } - e.setRunner(assigngraph.Runner([]mws.AssignmentEntry{{Node: "ace", Module: "web", Edges: []mws.Edge{ - {Kind: assigngraph.EdgeActing, Node: "ace", Module: "runner", Seat: mpr.Seat, Step: "pipeline"}}}})) - if !early.acked { - t.Error("the runner's end held until the graph was read is not taken once it is") - } - hear(pipe("web", "main", 5, 1, fp1, pass, step(req, "passed"))) - if !e.mayWant("runner", "fp-runner", []string{"main"}, nil) || e.mayWant("web", fp1, []string{"main"}, nil) { - t.Errorf("a group changing the runner: may want the runner %v, web %v; want the runner ahead of the group and web not yet", - e.mayWant("runner", "fp-runner", []string{"main"}, nil), e.mayWant("web", fp1, []string{"main"}, nil)) - } - hear(pipe("runner", "main", 6, 1, "fp-runner-2", fail, step(req, "failed"))) - if e.mayWant("runner", "fp-runner-2", []string{"main"}, nil) { - t.Error("the runner's member that failed is wanted") - } - } - // Design 47 G A-428: the runner's first start skips the gate while no module claiming the seat runs; another module never - // does, and a runner running anywhere (stopped ones aside) closes it; with no running heard the gate stands. - manifests := map[string]catalogue.Manifest{"runner": {Claims: []catalogue.Claim{{Name: mpr.Seat}}}, "web": {}} - none := map[string]map[string]nodeengine.Running{"ace": {"web": {Fingerprint: fp1}}} - if !runnerFirstStart("runner", manifests, none) || runnerFirstStart("web", manifests, none) || runnerFirstStart("runner", manifests, nil) { - t.Error("the runner's first start: the runner skips the gate, another module or an unknown world does not") - } - if runnerFirstStart("runner", manifests, map[string]map[string]nodeengine.Running{"novox": {"runner": {Fingerprint: "fp-runner"}}}) || - !runnerFirstStart("runner", manifests, map[string]map[string]nodeengine.Running{"novox": {"runner": {Stopped: true}}}) { - t.Error("a runner running closes the gate's opening; one stopped does not") - } - // A want that names the fingerprint already is not held by a later failure (a node placed afterwards gets it). - e := newPipelineEnds(newMemEnds(), func() {}, t.Logf) - e.on.Store(true) - if !e.mayWant("web", fp1, []string{"main"}, map[string]mwc.Want{"g14": {Fp: fp1}}) { - t.Error("a want naming the fingerprint was held") - } - // markWritten keeps the end and says it was written from; a newer end replaces it unwritten. - st := newMemEnds() - e = newPipelineEnds(st, func() {}, t.Logf) - e.on.Store(true) - _ = e.take(pipe("web", "main", 5, 1, fp1, pass), true) - e.wrote("web", fp1, []string{"main"}) - var end End - _ = json.Unmarshal(st.vals[endKey("web", "main")], &end) - if !end.Written || end.Source != 5 { - t.Errorf("end = %+v, want written at source 5", end) - } - _ = e.take(pipe("web", "main", 6, 1, fp2, pass), true) - end = End{} - _ = json.Unmarshal(st.vals[endKey("web", "main")], &end) - if end.Written || end.Source != 6 { - t.Errorf("a newer end = %+v, want unwritten at source 6", end) + run := p + run.Run = "r1" + raw, _ := json.Marshal(run) + e.hearPipeline(&heardMsg{data: raw, subject: "mesh.seat.mesh-pipeline-runner.event.pipeline-ended.main.5"}) + if woken != 1 || !slices.Equal(dropped, []string{runGroupPrefix + "r1"}) { + t.Errorf("woken %d (want 1), dropped %v (want the run's candidate)", woken, dropped) } } -// Design 47 G §29: the wants of a pass follow a pipeline: with the follower on, a module whose fingerprint no pipeline passed at -// gets no want and one a pipeline passed at gets it; with it off (nil, the trunk-only want) the pass is as before. -func TestAPassWritesNoWantWithoutAPassedPipeline(t *testing.T) { +// A6/AC1, A5/AC1 (design 47 H §750): a pass writes, on every node the module is assigned to, the want naming the newest version +// released on its branch whose pipeline passed, with the pipeline and the rank; with none passed it writes none. No grant, +// canary, provider or composition is asked for. +func TestAPassWritesTheWantOfThePassedVersion(t *testing.T) { fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) - for _, c := range []struct { - name string - follow bool - passed bool - want bool - }{{"the trunk-only want", false, false, true}, {"no pipeline passed", true, false, false}, {"a pipeline passed", true, true, true}} { + for _, passed := range []bool{false, true} { wantsKV := &memKV{values: map[string][]byte{}} p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) p.say = func(oc.Condition) error { return nil } mdigest := strings.Repeat("e", 64) mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: mdigest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) putModuleRaw(p.w, trunkBranch, "web", mod) - p.w.manifests[mdigest] = parsed{module: "web", manifest: catalogue.Manifest{Module: "web"}} - endDone(t, p.w, "web", fp, mdigest) - gkey, _ := grantsstate.PartGrantKey("web", fp) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - p.w.put(buckets.Grants, gkey, grant) - digest := "sha256:" + strings.Repeat("c", 64) - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: digest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - p.w.put(buckets.Have, "g14.web", have) - composes(t, p.w, "g14", digest) + p.w.manifests[mdigest] = parsed{module: "web", manifest: catalogue.Manifest{Module: "web", Requires: []string{"database"}}} placeOn(t, p.w, "g14", "web") - on := true - p.w.heardPresence("g14", !on, time.Now()) - if c.follow { - p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) - p.ends.trunk = p.w.trunkName - p.ends.on.Store(true) - if c.passed { - _ = p.ends.take(pipe("web", trunkBranch, 5, 1, fp, mpr.PipelineEndedOutcomePassed), true) - } + placeOn(t, p.w, "ace", "web") + p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) + p.ends.trunk = p.w.trunkName + if passed { + _, _ = p.ends.take(ended(trunkBranch, 5, mpr.PipelineEndedOutcomePassed, mover("web", fp, "0.1.2", 2, mpr.StepResultOutcomePassed))) } p.wants(context.Background()) - key, _ := mwc.WantKey("g14", "web") - if _, got := wantsKV.values[key]; got != c.want { - t.Errorf("%s: a want written = %v, want %v", c.name, got, c.want) - } - if c.follow && c.passed { - raw, _, _ := p.ends.st.get(endKey("web", trunkBranch)) - var end End - _ = json.Unmarshal(raw, &end) - if !end.Written { - t.Errorf("%s: the end was not marked written", c.name) + for _, node := range []string{"g14", "ace"} { + key, _ := mwc.WantKey(node, "web") + raw, got := wantsKV.values[key] + if got != passed { + t.Errorf("passed %v: a want on %s written = %v", passed, node, got) + continue + } + if !passed { + continue + } + if w, err := mwc.DecodeWant(raw); err != nil || w.Fp != fp || w.Version != "0.1.2" || w.Pipeline != trunkBranch+".5" || w.Rank != 2 { + t.Errorf("the want on %s = %+v (%v)", node, w, err) } } } } -// Design 47 G §95, §130, §353, §352 (test 100): the wants keep the trunk-only form until the first registration is heard and -// switch on it; a branch a registration no longer watches loses its end, and its first branch is a placement's default. -func TestTheWantsSwitchOnTheFirstRegistration(t *testing.T) { +// Design 47 G §353, §352: a registration's first branch is a placement's default, and a branch it no longer watches loses its +// end; a registration gone loses them all. +func TestARegistrationNamesTheDefaultBranch(t *testing.T) { st := newMemEnds() woken := 0 e := newPipelineEnds(st, func() { woken++ }, t.Logf) e.trunk = func() string { return trunkBranch } - _ = e.take(pipe("web", "main", 5, 1, "fp1", mpr.PipelineEndedOutcomeFailed), false) - _ = e.take(pipe("web", "beta", 5, 1, "fp1", mpr.PipelineEndedOutcomePassed), true) - if e.following() || !e.mayWant("web", "fp1", []string{"main"}, nil) { - t.Fatal("the follower held a want before any registration") - } + pass := mpr.PipelineEndedOutcomePassed + _, _ = e.take(ended("main", 5, pass, mover("web", "fp1", "0.1.1", 1, mpr.StepResultOutcomePassed))) + _, _ = e.take(ended("beta", 5, pass, mover("web", "fp2", "0.1.2", 1, mpr.StepResultOutcomePassed))) e.registered(mpcat.Registration{Module: "web", Branches: []string{"beta"}}, false) - if !e.following() || woken == 0 { - t.Error("the first registration did not switch the wants on") + endsKept := func() (n int) { // a pipeline's tier progress is not a module's end + for k := range st.vals { + if !isTierKey(k) { + n++ + } + } + return n } - if e.mayWant("web", "fp1", []string{"main"}, nil) || !e.mayWant("web", "fp1", []string{"beta"}, nil) { - t.Error("after the switch the wants did not follow the ends") + if woken == 0 || endsKept() != 1 || e.branchOf("web", "") != "beta" || e.branchOf("other", "") != trunkBranch { + t.Errorf("ends kept %d, default branch %q: an unwatched branch's end stayed, or the default is wrong", endsKept(), e.branchOf("web", "")) } - if len(st.vals) != 1 || e.branchOf("web", "") != "beta" || e.branchOf("other", "") != trunkBranch { - t.Errorf("ends kept %d, default branch %q: an unwatched branch's end stayed, or the default is wrong", len(st.vals), e.branchOf("web", "")) + if tg, ok := e.target("web", ""); !ok || tg.Fp != "fp2" { + t.Errorf("the default branch's target = %+v %v", tg, ok) } e.registered(mpcat.Registration{Module: "web"}, true) - if len(st.vals) != 0 { - t.Errorf("a registration gone left %d ends", len(st.vals)) + if endsKept() != 0 { + t.Errorf("a registration gone left %d ends", endsKept()) } } -// Design 47 G §14, §184 (test 68): a node's entry is computed from the catalog half of the branch its placement names, so a -// change on `beta` moves only the want of the node whose placement chose `beta`. -func TestACommitOnBetaMovesOnlyTheNodeThatChoseBeta(t *testing.T) { +// Design 47 G §14, §184 (test 68): a node's want follows the branch its placement names, so a version passed on `beta` moves only +// the want of the node whose placement chose `beta`. +func TestAVersionOnBetaMovesOnlyTheNodeThatChoseBeta(t *testing.T) { fp := func(c string) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(c, 64) } wantsKV := &memKV{values: map[string][]byte{}} p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) @@ -323,15 +218,8 @@ func TestACommitOnBetaMovesOnlyTheNodeThatChoseBeta(t *testing.T) { p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) p.ends.trunk = p.w.trunkName p.ends.registered(mpcat.Registration{Module: "web", Branches: []string{"main", "beta"}}, false) - digest := "sha256:" + strings.Repeat("c", 64) - on := true - for _, node := range []string{"g14", "ace"} { - composes(t, p.w, node, digest) - p.w.heardPresence(node, !on, time.Now()) - } for node, branch := range map[string]string{"g14": "", "ace": "beta"} { - pl := mma.Assignments{Node: node, Modules: []mma.PlacedModule{{Module: "web", Branch: branch}}} - values, err := mmac.AssignmentsValues(pl) + values, err := mmac.AssignmentsValues(mma.Assignments{Node: node, Modules: []mma.PlacedModule{{Module: "web", Branch: branch}}}) if err != nil { t.Fatal(err) } @@ -339,24 +227,10 @@ func TestACommitOnBetaMovesOnlyTheNodeThatChoseBeta(t *testing.T) { p.w.put(plc.AssignmentsBucket(), key, raw) } } - // publish makes the half of branch name web at fingerprint f: built, granted and passed by its pipeline. - publish := func(branch, f string, source int64) { - mdigest := strings.Repeat("e", 62) + fmt.Sprintf("%02d", source) - if branch == "beta" { - mdigest = strings.Repeat("f", 62) + fmt.Sprintf("%02d", source) - } - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: f, Manifest: mdigest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - putModuleRaw(p.w, branch, "web", mod) - p.w.manifests[mdigest] = parsed{module: "web", manifest: catalogue.Manifest{Module: "web"}} - endDone(t, p.w, "web", f, mdigest) - gkey, _ := grantsstate.PartGrantKey("web", f) - grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) - p.w.put(buckets.Grants, gkey, grant) - _ = p.ends.take(pipe("web", branch, source, 1, f, mpr.PipelineEndedOutcomePassed), true) - } - run := func(node, f string) { - have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: f, Composed: digest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - p.w.put(buckets.Have, node+".web", have) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp("a"), Manifest: strings.Repeat("e", 64), Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) + putModuleRaw(p.w, "main", "web", mod) + passed := func(branch, f, version string, source int64) { + _, _ = p.ends.take(ended(branch, source, mpr.PipelineEndedOutcomePassed, mover("web", f, version, 1, mpr.StepResultOutcomePassed))) } fpWant := func(node string) string { key, _ := mwc.WantKey(node, "web") @@ -366,18 +240,16 @@ func TestACommitOnBetaMovesOnlyTheNodeThatChoseBeta(t *testing.T) { } return w.Fp } - publish("main", fp("a"), 5) - publish("beta", fp("b"), 5) - run("g14", fp("a")) - run("ace", fp("b")) + passed("main", fp("a"), "0.1.1", 5) + passed("beta", fp("b"), "0.2.1", 5) p.wants(context.Background()) if fpWant("g14") != fp("a") || fpWant("ace") != fp("b") { t.Fatalf("each node is wanted at its branch's build: g14 %s, ace %s", fpWant("g14"), fpWant("ace")) } - publish("beta", fp("d"), 6) + passed("beta", fp("d"), "0.2.2", 6) p.wants(context.Background()) if fpWant("g14") != fp("a") || fpWant("ace") != fp("d") { - t.Errorf("a commit on beta moves only ace: g14 %s (want main's), ace %s (want beta's new)", fpWant("g14"), fpWant("ace")) + t.Errorf("a version on beta moves only ace: g14 %s (want main's), ace %s (want beta's new)", fpWant("g14"), fpWant("ace")) } } @@ -411,40 +283,3 @@ func TestTheCatalogHalfIsTakenInPerBranch(t *testing.T) { t.Error("a module taken out of beta's half leaves main's") } } - -// Design 47 G §379, §526: a want held at an older build, the trunk's newer fingerprint having passed no pipeline, is rewritten -// with that build's released version (and parts for an engine that reads them), the rest of the want kept; no want names the -// unpassed fingerprint (novox 2026-11-16, mesh-hand-acts 0.1.13 at 4eb3cf48). -func TestAWantHeldAtAnOlderBuildNamesItsReleaseWithoutAPassedPipeline(t *testing.T) { - old := meshcatalogclient.SourcePrefix2 + strings.Repeat("4", 64) - fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("9", 64) - wantsKV := &memKV{values: map[string][]byte{}} - p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) - p.say = func(oc.Condition) error { return nil } - mdigest := strings.Repeat("e", 64) - mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: mdigest, Repository: "novox/web", Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) - putModuleRaw(p.w, trunkBranch, "web", mod) - p.w.manifests[mdigest] = parsed{module: "web", manifest: catalogue.Manifest{Module: "web"}} - endRaw, _ := json.Marshal(builds.BuildEnded{Module: "web", Fingerprint: old, Outcome: builds.BuildDone, Manifest: mdigest, Made: map[string]string{"tools": "sha256:1c"}}) - if err := p.w.ends.put(endRaw); err != nil { - t.Fatal(err) - } - rel, _ := json.Marshal(meshcatalog.VersionReleasedEvent{Module: "web", Source: 962, Version: "0.1.13", Artifacts: []meshcatalog.VersionArtifact{{Artifact: "tools", Digest: "sha256:1c"}}}) - if err := p.w.released.put(rel); err != nil { - t.Fatal(err) - } - placeOn(t, p.w, "g14", "web") - key, _ := mwc.WantKey("g14", "web") - held, _ := mwc.EncodeWant(mwc.Want{Fp: old, Self: true}) - wantsKV.values[key] = held - p.w.put(buckets.Wants(), key, held) - p.w.heardPresence("g14", false, time.Now()) - p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) - p.ends.trunk = p.w.trunkName - p.ends.on.Store(true) - p.wants(context.Background()) - got, err := mwc.DecodeWant(wantsKV.values[key]) - if err != nil || got.Fp != old || got.Version != "0.1.13" || !got.Self { - t.Errorf("the held want = %+v, %v; want %s at 0.1.13, self kept", got, err, old) - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placementbuild.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placementbuild.go deleted file mode 100644 index 2db32d76..00000000 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placementbuild.go +++ /dev/null @@ -1,95 +0,0 @@ -package main - -import ( - "context" - "fmt" - "sort" - - "mesh-delivery-assigner/internal/buckets" - "mesh-delivery-assigner/internal/puts" -) - -// placementBy is who a put the placement's build makes names as its asker: the placement, whose proposal the operator -// approved, is the warrant (design 47 *Going back is one act*; ADR 0666 §24). -const placementBy = "the placement" - -// placementPuts makes each placed module's `build` a held put of that build, and releases a put the placement made once its -// placement names that build no more (design 47 line 912: "a return to an earlier build is one value in the placement, the -// module's `build` in its placement key, set by a placement proposal"; ADR 0666 §24; B-K1). The put then goes its own steps -// (the builder's build, the composition, an ask where the module's policy needs one, the want, holding) and is read with -// `assigner_puts` and released with `assigner_release` like any other; a placement's build the builder never built is refused -// by its build step, said in the put's words. A put asked by a person stands until the placement names another build. -func (p *passer) placementPuts(ctx context.Context) { - kv := p.kvs[buckets.Puts()] - if kv == nil { - return - } - now := p.clock().UTC() - named := map[string]string{} // put key -> the build its placement names - for node, a := range p.w.assignments() { - for _, m := range a.Modules { - if m.Build == "" { - continue - } - if key, err := puts.Key(node, m.Module); err == nil { - named[key] = m.Build - } - } - } - held := p.w.read(buckets.Puts()) - keys := make([]string, 0, len(named)+len(held)) - for k := range named { - keys = append(keys, k) - } - for k := range held { - if _, there := named[k]; !there { - keys = append(keys, k) - } - } - sort.Strings(keys) - for _, key := range keys { - build, names := named[key] - var before *puts.Put - if raw, ok := held[key]; ok { - if got, err := puts.Decode(raw); err == nil { - before = &got - } - } - var next puts.Put - switch { - case names && before != nil && before.FP == build: - continue // made already, or a person's put of the same build: it goes its own steps, ended or not - case names: - words := fmt.Sprintf("asked by %s: it names build %s of %s", placementBy, shortFP(build), key) - if before != nil && !puts.Ended(before.Step) { - words += fmt.Sprintf("; it replaces the put of %s, which stood at %s", shortFP(before.FP), before.Step) - } - next = puts.Put{FP: build, Hold: true, Why: "the placement names this build (ADR 0666 §24)", By: placementBy, At: now}.Said(now, puts.Asked, words) - case before != nil && before.By == placementBy && !puts.Ended(before.Step): - next = before.Said(now, puts.Released, fmt.Sprintf("released by %s: it names no build of its own any more; the assigner's rules write the want again", placementBy)) - default: - continue - } - data, err := puts.Encode(next) - if err != nil { - p.logf("the placement's put %s is not written: %v", key, err) - continue - } - if before == nil { - _, err = kv.Create(ctx, key, data) - } else if e, gerr := kv.Get(ctx, key); gerr != nil { - err = gerr - } else { - _, err = kv.Update(ctx, key, data, e.Revision()) - } - if err != nil { - p.logf("the placement's put %s could not be written, and is written at the next pass: %v", key, err) - continue - } - p.w.put(buckets.Puts(), key, data) - p.logf("put %s: %s: %s", key, next.Step, next.Words) - if p.acts != nil { - p.acts.say(key, next) - } - } -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placeorder_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placeorder_test.go new file mode 100644 index 00000000..ccbc91a2 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/placeorder_test.go @@ -0,0 +1,204 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + "strings" + "testing" + "time" + + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + "git.novox.be/novox/mesh-contracts/go/seats/meshbroker" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" + mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" + "git.novox.be/novox/mesh-sdk/go/catalogue" + + "mesh-delivery-assigner/internal/assigngraph" + plc "mesh-delivery-assigner/internal/assignment" + "mesh-delivery-assigner/internal/buckets" +) + +// orderFP is a fingerprint of the form the trunk keeps, made of one letter. +func orderFP(c string) string { + return meshcatalogclient.SourcePrefix2 + strings.Repeat(fmt.Sprintf("%x", c[0]&15), 64) +} + +// orderMod is one module of an order test: the fingerprint it runs on ace and the one the trunk holds, the form of its kept data at each +// fingerprint (0: it keeps none), the seats it holds and the source revision of its trunk value. +type orderMod struct { + name string + run, trunk string + forms map[string]int + holds []string +} + +type orderFixture struct { + t *testing.T + p *passer + wants *memKV + events []string + bodies []meshwants.Wait + answers chan struct{} +} + +// newOrderFixture is a pass on node ace, which composes its own modules, with each module running its fingerprint healthy, built and +// granted at the trunk's, an older want, the catalog's entries answered and each held seat as the assignments say it. history lists the +// fingerprints of each module by assignment graph revision, oldest first (the objects Reverted reads). +func newOrderFixture(t *testing.T, mods []orderMod, history ...map[string]string) *orderFixture { + t.Helper() + ctx := context.Background() + f := &orderFixture{t: t, wants: &memKV{values: map[string][]byte{}}, answers: make(chan struct{}, 64)} + f.p = newPasser(kvSet{buckets.Wants(): f.wants, buckets.Asks(): &memKV{values: map[string][]byte{}}, buckets.Waits(): &memKV{values: map[string][]byte{}}}, time.Now()) + p := f.p + p.logf = t.Logf + p.say = func(oc.Condition) error { return nil } + p.publishWants = func(event string, body any, id string) error { + f.events = append(f.events, event) + f.bodies = append(f.bodies, body.(meshwants.Wait)) + return nil + } + digest := "sha256:" + strings.Repeat("c", 64) + p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) + p.ends.trunk = p.w.trunkName + pl := mma.Assignments{Node: "ace"} + forms := map[string]int{} // by fingerprint: each module's own + modules := []string{} + for _, m := range mods { + modules = append(modules, m.name) + pl.Modules = append(pl.Modules, mma.PlacedModule{Module: m.name}) + for _, seat := range m.holds { + scope := "node" + if seat != catalogue.BackupSeat && seat != nodeengine.Seat { + scope = "mesh" + } + pl.Holds = append(pl.Holds, mma.Holding{Claim: seat, Module: m.name, Scope: scope}) + } + for fp, form := range m.forms { + forms[fp] = form + } + mdigest := fmt.Sprintf("%064x", len(modules)) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: m.trunk, Manifest: mdigest, Repository: "novox/" + m.name, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) + putModuleRaw(p.w, trunkBranch, m.name, mod) + p.w.manifests[mdigest] = parsed{module: m.name, manifest: catalogue.Manifest{Module: m.name}} + endDone(t, p.w, m.name, m.trunk, mdigest) + gkey, _ := grantsstate.PartGrantKey(m.name, m.trunk) + grant, _ := grantsstate.EncodeGrant(grantsstate.Grant{}) + p.w.put(buckets.Grants, gkey, grant) + f.runs(m.name, m.run) + // The trunk's build is released with a passed pipeline (design 47 H §750). + _, _ = p.ends.take(ended(trunkBranch, 1, mpr.PipelineEndedOutcomePassed, mover(m.name, m.trunk, "0.1.1", 1, mpr.StepResultOutcomePassed))) + wkey, _ := mwc.WantKey("ace", m.name) + old, _ := mwc.EncodeWant(mwc.Want{Fp: m.run, Self: true}) + p.w.put(buckets.Wants(), wkey, old) + f.wants.values[wkey] = old + } + composes(t, p.w, "ace", digest) + values, err := mmac.AssignmentsValues(pl) + if err != nil { + t.Fatal(err) + } + for key, raw := range values { + p.w.put(plc.AssignmentsBucket(), key, raw) + } + p.w.heardPresence("ace", false, time.Now()) // ace is online: its last presence event heard is machine-back + objects := memObjects{} + for i, at := range history { + half := meshcatalogclient.Half{Revision: meshcatalog.CatalogRevision{Branch: trunkBranch, Kind: "trunk", Revision: int64(i + 1), Source: int64(i + 1)}, + Entries: map[string]meshcatalog.CatalogEntry{}} + for _, module := range modules { + digest := fmt.Sprintf("sha256:%064x", (i+1)*1000+len(half.Entries)) + half.Revision.Entries = append(half.Revision.Entries, meshcatalog.EntryRef{Module: module, Digest: digest}) + half.Entries[module] = meshcatalog.CatalogEntry{Module: module, Fp: at[module]} + } + placed := mma.Assignments{Node: "ace"} + for _, module := range modules { + placed.Modules = append(placed.Modules, mma.PlacedModule{Module: module}) + } + in := assigngraph.Inputs{Halves: map[string]meshcatalogclient.Half{trunkBranch: half}, FirstBranch: func(string) string { return trunkBranch }, + Placements: map[string]mma.Assignments{"ace": placed}} + if _, _, err := assigngraph.Keep(ctx, objects, assigngraph.Compute(in), "test", time.Now()); err != nil { + t.Fatal(err) + } + } + p.graph = &graphKeeper{objects: objects} + p.entries = meshcatalogclient.NewEntries(func(_ string, body any) (json.RawMessage, error) { + in := body.(meshcatalog.EntryInput) + entry := meshcatalog.CatalogEntry{Module: "m", Fp: in.Fingerprint, Manifest: map[string]any{"module": "m"}} + if form := forms[in.Fingerprint]; form > 0 { + entry.Manifest = map[string]any{"module": "m", "state": []any{map[string]any{"name": "sessions", "form": form}}} + } + return json.Marshal(meshcatalog.EntryAnswer{Entry: entry}) + }) + p.entries.Changed = func(meshcatalogclient.EntryRow) { f.answers <- struct{}{} } + return f +} + +// runs says module runs fp healthy on ace, heard as the node-engine seat's running and in the have. +func (f *orderFixture) runs(module, fp string) { + have, _ := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: "sha256:" + strings.Repeat("c", 64), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + f.p.w.put(buckets.Have, "ace."+module, have) + f.p.w.running["ace."+module] = nodeengine.Running{Node: "ace", Module: module, Fingerprint: fp} +} + +// pass makes a pass, and after it every catalog entry it asked is answered. +func (f *orderFixture) pass() { + f.t.Helper() + f.p.wants(context.Background()) + asked := 0 + for _, row := range f.p.entries.Rows() { + if row.State == meshcatalogclient.EntryAsked { + asked++ + } + } + for range asked { + select { + case <-f.answers: + case <-time.After(10 * time.Second): // a bound on a failure only: the answers come at once + f.t.Fatal("an entry asked was not answered") + } + } +} + +// wantAt is the fingerprint module is wanted at on ace. +func (f *orderFixture) wantAt(module string) string { + f.t.Helper() + key, _ := mwc.WantKey("ace", module) + w, err := mwc.DecodeWant(f.wants.values[key]) + if err != nil { + f.t.Fatal(err) + } + return w.Fp +} + +func (f *orderFixture) waits() []string { + var out []string + for key := range f.p.w.read(buckets.Waits()) { + out = append(out, key) + } + return out +} + +// A3/AC1 (design 47 H §771, §764): given a module whose kept data's form changes while the new versions of the node-backup holder and +// of the bus module do not run, when the assigner passes, then the module is wanted at its new version, no `order.` key is kept and no +// wait is said: the order on a machine is its node-engine's apply order, by rank. +func TestNoWantWaitsForTheOrderOfADelivery(t *testing.T) { + f := newOrderFixture(t, []orderMod{ + {name: "web", run: orderFP("a"), trunk: orderFP("b"), forms: map[string]int{orderFP("a"): 1, orderFP("b"): 2}}, + {name: "backup", run: orderFP("e"), trunk: orderFP("f"), holds: []string{catalogue.BackupSeat}}, + {name: "bus", run: orderFP("g"), trunk: orderFP("h"), holds: []string{meshbroker.Seat}}, + }) + f.pass() + f.pass() + if got := f.wantAt("web"); got != orderFP("b") || len(f.waits()) != 0 || len(f.events) != 0 { + t.Fatalf("web at %s, waits %v, events %v: want web at its new version, no wait kept or said", got, f.waits(), f.events) + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/puts.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/puts.go index dc4b3db9..b42fa859 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/puts.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/puts.go @@ -42,8 +42,6 @@ type caller interface { CallOn(node, key string, body any) (json.RawMessage, error) // CallKept is Call for a kept call, safe to call again (process.Conn.CallKept; ADR 0668). CallKept(key string, body any) (json.RawMessage, error) - // CallKeptOn is CallOn for a kept call, safe to call again (process.Conn.CallKeptOn; ADR 0668). - CallKeptOn(node, key string, body any) (json.RawMessage, error) // CallFile is Call of a verb marked chunks, its answer read whole (process.Conn.CallFile; design 47 G A-291). CallFile(key string, body any) (json.RawMessage, error) Emit(eventType string, body any) error @@ -106,7 +104,6 @@ func (p *passer) puts(ctx context.Context) { if p.acts == nil { return } - p.placementPuts(ctx) all := p.putsByModule() defer func() { p.putWaits(ctx, p.putsByModule()) }() if len(all) == 0 { @@ -136,14 +133,6 @@ func (p *passer) puts(ctx context.Context) { world := p.putWorld(node, module, put, manifest, assigned[node]) world.Snapshot, world.Snapshotting, world.SnapshotFailed = p.acts.state(id) next := assign.StepPut(world) - // A held bus whose user list the rules moved on (ADR 0539 §7): the put follows the composition its want names. - if w, ok := wanted[module][node]; ok && w.Put && w.Fp == next.Put.FP && w.Composed != "" && w.Composed != next.Put.Composed && - (next.Put.Step == puts.Wanted || next.Put.Step == puts.Holding) { - if d, _ := p.composedFor(node, module, w.Fp); d != nil && d.UserListOnly && d.Digest == w.Composed { - next.Put = next.Put.Said(world.Now, next.Put.Step, fmt.Sprintf("the bus's user list changed: the want names composition %s", shortHex(w.Composed))) - next.Put.Composed = w.Composed - } - } // The bus's want is written only for the act the operator's yes bound (ADR 0546 clause 8). if next.Put.Step == puts.Wanted && put.Step != puts.Wanted && world.Bus && next.Put.Ask != "" { if err := p.performs(next.Put.Ask, assign.PutAct(node, module, next.Put.FP, next.Put.Composed)); err != nil { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/releases.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/releases.go new file mode 100644 index 00000000..6fc65cbd --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/releases.go @@ -0,0 +1,87 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "time" + + "github.com/nats-io/nats.go/jetstream" + + wd "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-sdk/go/process" + + "mesh-delivery-assigner/internal/assign" + "mesh-delivery-assigner/internal/assignment" +) + +// releaseHeard is one version-released heard: the version, each part's digest, the source revision and when it was said. +type releaseHeard struct { + Version string + Parts map[string]string + Source int64 + At time.Time +} + +// presenceOf reads one machine-away or machine-back: the machine and when it was said. +func presenceOf(data []byte) (string, time.Time, error) { + var e wd.MachineAwayEvent // machine-back has the same fields + if err := json.Unmarshal(data, &e); err != nil { + return "", time.Time{}, err + } + if e.Machine == "" { + return "", time.Time{}, fmt.Errorf("it names no machine: %s", data) + } + return e.Machine, e.At, nil +} + +// hearReleases reads whole, from their first message, then follows, the catalog seat's version-released and the watchman's +// machine-away and machine-back (mesh-sdk process.FromFirst, A-233), never their state. A stream not heard is said as the assigner's finding, and what it carries is not known until +// the assigner starts again. +func hearReleases(ctx context.Context, js jetstream.JetStream, w *world, wake func(), findings assign.Findings, logf func(string, ...any)) { + acct := process.Account{Node: thisNode, Module: assignment.Module()} + nh := notHeardWriter(findings, logf) + for _, s := range []struct { + stream, words string + take func([]byte) error + }{ + {meshcatalog.StreamVersionReleased, "no want names a released version", w.released.put}, + {wd.StreamMachineAway, "no machine is known away, so a delivery waits for a machine that is away", func(data []byte) error { + m, at, err := presenceOf(data) + if err == nil { + w.heardPresence(m, true, at) + logf("%s is away (machine-away): a composition step does not compose it until its machine-back", m) + } + return err + }}, + {wd.StreamMachineBack, "a machine once away is not known back", func(data []byte) error { + m, at, err := presenceOf(data) + if err == nil { + w.heardPresence(m, false, at) + logf("%s is back (machine-back): a composition step composes it again", m) + } + return err + }}, + } { + take := s.take + cc, err := acct.HearSeatEvents(ctx, js, s.stream, func(m jetstream.Msg) { + if err := take(m.Data()); err != nil { + logf("%s is not kept: %v", m.Subject(), err) + } + if err := m.Ack(); err != nil { + logf("acknowledging %s: %v", m.Subject(), err) + } + wake() + }, logf, process.FromFirst()) + if err != nil { + nh.NotHeard(s.stream, s.words+" until the assigner starts again", err) + continue + } + nh.Heard(s.stream) + go func() { + <-ctx.Done() + cc.Stop() + }() + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions.go new file mode 100644 index 00000000..57c2d085 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions.go @@ -0,0 +1,140 @@ +package main + +import ( + "context" + "fmt" + "slices" + "strconv" + "strings" + + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + + "mesh-delivery-assigner/internal/assigngraph" +) + +// The assignment graph revisions are removed when they are no longer current (design 47 H §764, H-A-86): the gate's pins of +// them went with its pinned, freed, heads-replaced and pins-stated (H-A-48), and with them the assigner's state pins and its +// tools assigner_pins and assigner_unpin. A revision that is not current (the newest the assigner wrote) is removed, its object +// and the entry objects no kept revision or candidate lists and no want or running build names (G §179), when a revision ceases +// to be current and once at the start. Never on a timer. + +// revisionObjects is the object bucket of the revisions as the remover removes from it. +type revisionObjects interface { + assigngraph.Objects + Delete(ctx context.Context, name string) error +} + +// revisionRemover removes the revisions that are not current. +type revisionRemover struct { + ctx context.Context + objects revisionObjects + // inUse says a want or a running build names the module's fingerprint (§179). + inUse func(module, fp string) bool + logf func(string, ...any) +} + +// judge removes every revision that is not current; a removal that fails is said and made at the next judging. +func (k *revisionRemover) judge() { + if err := k.remove(); err != nil { + k.logf("the revisions that are not current are not removed: %v", err) + } +} + +// trunkRevision is the revision an object of a trunk revision names, and whether it names one. +func trunkRevision(name string) (int64, bool) { + n, ok := strings.CutPrefix(name, "trunk.") + if !ok { + return 0, false + } + r, err := strconv.ParseInt(n, 10, 64) + return r, err == nil && r > 0 +} + +// remove removes each kept revision that is not current, and then the entries no kept revision or candidate lists and no want or +// running build names (design 47 G §179). +func (k *revisionRemover) remove() error { + names, err := k.objects.Names(k.ctx) + if err != nil { + return err + } + var kept []int64 + for _, n := range names { + if r, ok := trunkRevision(n); ok { + kept = append(kept, r) + } + } + if len(kept) == 0 { + return nil + } + current := slices.Max(kept) + listed := map[string]bool{} + removed := 0 + for _, r := range kept { + if r == current { + rev, err := assigngraph.ReadRevision(k.ctx, k.objects, r) + if err != nil { + return err + } + for _, e := range rev.Entries { + listed[e.Digest] = true + } + continue + } + if err := k.objects.Delete(k.ctx, assigngraph.TrunkObject(r)); err != nil { + return fmt.Errorf("revision %d: %w", r, err) + } + removed++ + } + if removed == 0 { + return nil + } + // A run's candidate kept now lists its entries too, until its run's pipeline-ended drops it (A-201, A-235). + for _, n := range names { + group, heads, ok := meshwants.ParseAssignmentRevisionsByCandidateGroupHeadsKey(n) + if !ok { + continue + } + rev, err := assigngraph.ReadCandidate(k.ctx, k.objects, group, heads) + if err != nil { + return err + } + for _, e := range rev.Entries { + listed[e.Digest] = true + } + } + for _, n := range names { + digest, ok := strings.CutPrefix(n, "entry.") + if !ok || listed[digest] { + continue + } + e, err := assigngraph.ReadEntry(k.ctx, k.objects, digest) + if err != nil { + return err + } + if k.inUse != nil && k.inUse(e.Module, e.Fp) { + continue + } + if err := k.objects.Delete(k.ctx, n); err != nil { + return fmt.Errorf("the entry %s: %w", digest, err) + } + } + k.logf("%d assignment graph revisions not current are removed", removed) + return nil +} + +// inUseOf says a want or a running build names the module's fingerprint, as the world holds them now. +func inUseOf(w *world) func(module, fp string) bool { + return func(module, fp string) bool { + for _, want := range w.wantsOf()[module] { + if want.Fp == fp { + return true + } + } + for _, mods := range w.runningNow() { + if r, ok := mods[module]; ok && r.Fingerprint == fp { + return true + } + } + return false + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions_test.go new file mode 100644 index 00000000..5b38752d --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/revisions_test.go @@ -0,0 +1,53 @@ +package main + +import ( + "context" + "encoding/json" + "testing" + + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + + "mesh-delivery-assigner/internal/assigngraph" +) + +func (m memObjects) Delete(_ context.Context, name string) error { delete(m, name); return nil } + +// Design 47 H §764, H-A-86: with the gate's pins gone, an assignment graph revision that is not current is removed, with the +// entries no kept revision or candidate lists and no want or running build names; the current revision, the entries it lists, +// a running build's entry and a run's kept candidate with its entries stay. +func TestARevisionNotCurrentIsRemoved(t *testing.T) { + objects := memObjects{} + entry := func(module, fp string) string { + e := meshwants.AssignmentEntry{Node: "n1", Module: module, Fp: fp} + d := assigngraph.EntryDigest(e) + raw, _ := json.Marshal(e) + objects[assigngraph.EntryObject(d)] = raw + return d + } + shared, old, running, cand := entry("a", "fp-a"), entry("b", "fp-b1"), entry("c", "fp-c1"), entry("d", "fp-d2") + revision := func(n int64, digests ...string) { + rev := meshwants.AssignmentRevision{Revision: n} + for _, d := range digests { + rev.Entries = append(rev.Entries, meshwants.NodeEntryRef{Node: "n1", Digest: d}) + } + raw, _ := json.Marshal(rev) + objects[assigngraph.TrunkObject(n)] = raw + } + candRaw, _ := json.Marshal(meshwants.AssignmentRevision{Revision: 1, Kind: "candidate", Group: "run-7", Heads: "h1", + Entries: []meshwants.NodeEntryRef{{Node: "n1", Module: "d", Digest: cand}}}) + objects[assigngraph.CandidateObject("run-7", "h1")] = candRaw + revision(1, shared, old, running) + revision(2, shared, old) + revision(3, shared) + k := &revisionRemover{ctx: context.Background(), objects: objects, logf: t.Logf, + inUse: func(module, fp string) bool { return module == "c" && fp == "fp-c1" }} + k.judge() + has := func(name string) bool { _, ok := objects[name]; return ok } + for name, want := range map[string]bool{assigngraph.TrunkObject(1): false, assigngraph.TrunkObject(2): false, assigngraph.TrunkObject(3): true, + assigngraph.EntryObject(old): false, assigngraph.EntryObject(running): true, assigngraph.EntryObject(shared): true, + assigngraph.EntryObject(cand): true, assigngraph.CandidateObject("run-7", "h1"): true} { + if has(name) != want { + t.Errorf("%s kept = %v, want %v", name, has(name), want) + } + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go index d406ece6..ab335fe2 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go @@ -17,7 +17,6 @@ import ( "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" - mdcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" "github.com/nats-io/nats.go" @@ -341,11 +340,11 @@ func (p *passer) stepProposal(ctx context.Context, prop mma.Proposal, rev uint64 } prop.Verdict = &v switch v.State { - case mdcc.VerdictFail: + case plc.VerdictFail: prop.State, prop.Ask = mmac.ProposalFailed, "" case plc.VerdictWaits: prop.State, prop.Ask = mmac.ProposalAnswering, "" - case mdcc.VerdictPass: + case plc.VerdictPass: if prop.State != mmac.ProposalAsked || again { // A proposal none of whose changes the setting ask-operator-for names is applied at once, with no ask (A-425a). if kinds := proposalKinds(prop, v); !p.asksFor(kinds) { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go index 8d4b894f..f037ba4f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go @@ -312,10 +312,10 @@ func warrantOf(v any) (*mma.Warrant, error) { // mesh-node-enroller when the proven caller is that module's (its bus user `.`, never a person's // `person.`), else the caller as proven, which the refusal names. func moduleOfCaller(by string, enrollers []string) string { - for _, module := range enrollers { - if (stdio.Callers{module}).Allows(by) { - return module - } + // The enrolment's caller stays (design 47 H-A-41): a module's bus user is `.`, a person's `person.`. + _, module, ok := strings.Cut(by, ".") + if ok && !strings.HasPrefix(by, "person.") && slices.Contains(enrollers, module) { + return module } return by } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go index fbe233ca..8b18c9f0 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go @@ -20,11 +20,9 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/catalogkey" "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/seatlist" mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/views" @@ -270,19 +268,6 @@ func TestTheSeatsVerbs(t *testing.T) { }) } s.seatList = nil - t.Run("1966: the seat list's again tool answers an operator's session through the mesh MCP server", func(t *testing.T) { - // Such a session is proved as its machine's tool runner (issue 627); the healers too, no other module. The list names - // the seats, resolved from the assigner's own seat-holders (A-295, A-298): a module holding neither is refused. - held := map[string][]byte{"node-tool-runner.shanks": []byte(`{"holders":[{"module":"node-tools","node":"shanks"}]}`), - "mesh-healers": []byte(`{"holders":[{"module":"mesh-delivery-watchman","node":"ace"}]}`)} - holders := seatlist.HoldersIn(func() (map[string][]byte, error) { return held, nil }) - for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "ace.mesh-delivery-watchman": false, - "ace.mesh-healers": true, "ace.messenger": true} { - if err := seatlist.RefusalWith("assigner_seat_list_again", seatListAgainCallers, caller, holders); (err != nil) != refused { - t.Errorf("%s: refused=%v, err %v", caller, refused, err) - } - } - }) t.Run("1953/D4-D6: the seat mesh-wants' upgrade and bus", wantsVerbs) t.Run("1955/L7 (ADR 0571 §6): rule 7 reads the node's own composition; an unplaced node is said", func(t *testing.T) { w := newWorld() @@ -533,51 +518,6 @@ func TestTheSeatsVerbs(t *testing.T) { }) } }) - t.Run("1954/D1: a failed provision is the assigner's finding until it is provided", func(t *testing.T) { - findings := map[string][]byte{} - p := &passer{w: newWorld(), logf: func(string, ...any) {}, kvs: kvSet{buckets.Provisions: nil}, findings: assign.Findings{ - Put: putInto(findings), - Delete: func(k string) error { delete(findings, k); return nil }}} - failed, _ := meshprovisionerclient.Encode(meshprovisionerclient.Provision{Provision: "db", Consumer: meshprovisionerclient.End{Module: "photos", Node: "ace"}, - Provider: meshprovisionerclient.End{Module: "postgres", Node: "novox"}, State: meshprovisionerclient.Failed, Why: "the role exists already"}) - p.w.putEntry(buckets.Provisions, "ace.photos.db", failed, 1, time.Now()) - p.kvs[buckets.Provisions] = &memKV{} - of := p.provisionsOf() - if got := of("photos")("ace"); got["ace.photos.db"].State != meshprovisionerclient.Failed { - t.Fatalf("provisions = %+v", got) - } - if _, ok := findings[assign.ProvisionFailedKey("ace.photos.db")]; !ok || len(findings) != 1 { - t.Fatalf("findings = %v", findings) - } - provided, _ := meshprovisionerclient.Encode(meshprovisionerclient.Provision{Provision: "db", Consumer: meshprovisionerclient.End{Module: "photos", Node: "ace"}, - Provider: meshprovisionerclient.End{Module: "postgres", Node: "novox"}, State: meshprovisionerclient.Provided}) - p.w.putEntry(buckets.Provisions, "ace.photos.db", provided, 2, time.Now()) - p.provisionsOf() - if len(findings) != 0 { - t.Errorf("findings after provided = %v", findings) - } - // A required provision with no key: pulsed with the bound first, said once the pulse expired, ended once keyed. - ctx := context.Background() - p.kvs[plc.PulsesBucket()] = &memKV{values: map[string][]byte{}} - gone := map[string]bool{"ace.photos.s3": true} - p.provisionsMissing(ctx, gone) - if !p.pulsing("provision-missing.ace.photos.s3") || len(findings) != 0 { - t.Fatalf("first seen: pulsing %v, findings %v", p.pulsing("provision-missing.ace.photos.s3"), findings) - } - p.provisionsMissing(ctx, gone) - if len(findings) != 0 { - t.Fatal("said before its bound") - } - p.w.put(plc.PulsesBucket(), "provision-missing.ace.photos.s3", nil) // the server expired it - p.provisionsMissing(ctx, gone) - if _, ok := findings[assign.ProvisionMissingKey("ace.photos.s3")]; !ok { - t.Fatalf("past its bound: findings %v", findings) - } - p.provisionsMissing(ctx, map[string]bool{}) - if len(findings) != 0 { - t.Errorf("keyed again: findings %v", findings) - } - }) // A whole state is read in pages where the store has them, never a key at a time (27 s for the assignments live, // 2026-11-14): the paged read answers what the key-by-key read answers, and asks no key alone. direct, err := readAll(st, haveState) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go index cf2da054..1b78b133 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go @@ -1,16 +1,7 @@ package main -import ( - stdio "git.novox.be/novox/mesh-sdk/go" -) - // The assigner reads the seat list as every part does, through mesh-sdk's one wiring (go/seatlist.Part; design 47 G §204): its // reader over the seat mesh-catalog's verb seat-definitions and its two events, its tools `assigner_seat_list` and // `assigner_seat_list_again`. What the reader holds is given to catalogue at every change, which the check of a proposal and the // seat-holders state read; until the list is read, a mesh seat the bootstrapped definitions do not name is never judged held once // (plc.CheckAssignments), and seat-holders is not written. - -// A call through the mesh MCP server is proved only as its machine's tool runner (issue 627), so the holders of the seat -// node-tool-runner stand for an operator's session until a session is proved (issue 1966). The list names seats, never a -// module, resolved from the assigner's own seat-holders (design 47 G A-210, A-295, A-298). -var seatListAgainCallers = []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go index 992599c2..64c3ecc4 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go @@ -60,7 +60,7 @@ func gateOver(t *testing.T, silent bool) *seatlist.Part { // that waits for it to stay unread keeps short. t.Cleanup(busretry.Waits(20*time.Millisecond, time.Millisecond, 5*time.Millisecond)) } - g := seatlist.NewPart("assigner", seatListAgainCallers) + g := seatlist.NewPart("assigner") r := g.Follow(standInCatalog{silent: silent}, nil, t.Logf) if r == nil { t.Fatal("the reader did not start") @@ -98,7 +98,7 @@ func TestTheSeatListIsWiredByTheSDKAlone(t *testing.T) { // A-179, A-186: the definition the verb input's callers are checked against is the seat list's, none before the reader starts // or while the seat mesh-catalog has not answered, and none for a seat the list does not hold. func TestTheAnsweredSeatsDefinitionIsTheSeatListsOnceRead(t *testing.T) { - if _, ok := seatlist.Definition(seatlist.NewPart("assigner", seatListAgainCallers).Reader, "mesh-wants")(); ok { + if _, ok := seatlist.Definition(seatlist.NewPart("assigner").Reader, "mesh-wants")(); ok { t.Error("a definition is answered before the seat list's reader started") } if _, ok := seatlist.Definition(gateOver(t, true).Reader, "mesh-wants")(); ok { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go new file mode 100644 index 00000000..5211d564 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go @@ -0,0 +1,308 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "sort" + "strings" + "time" + + mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" + mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + "git.novox.be/novox/mesh-sdk/go/keys" + "git.novox.be/novox/mesh-sdk/go/views" + + "mesh-delivery-assigner/internal/assign" +) + +// One pipeline's wants are written tier by tier (design 47 H §751, H-A-10, H-A-56): on a passed pipeline-ended the assigner +// writes the wants of its first tier's movers; once each of them runs at the mover's fingerprint (the node-engine seat's +// `running`, H-A-40) on every present placing machine, it writes the next tier's, and so on. The tier progress is the key +// `pipeline..` of the state written, `{tier, since, movers}`; a tier counts placed movers only, and a machine away +// is not waited for. A tier whose movers do not all run within `tier-bound` ends the delivery loudly: no later tier is written, +// the finding `tier-late` (scope delivery, id the pipeline) is raised naming the movers and machines not running, and +// `pipeline-wanted` says outcome tier-late; once every tier runs it says outcome running and the key goes (H-A-75). tier-late +// clears when the late movers run or a later passed pipeline moves them (H-A-56). + +// tierPrefix starts the key of a pipeline's tier progress in the state written. +const tierPrefix = "pipeline." + +// kindTierLate is the finding of a tier not running within tier-bound (design 47 H §751). +const kindTierLate = "tier-late" + +// defaultTierBound is tier-bound's default, 30 minutes (design 47 H §751). +const defaultTierBound = 30 * time.Minute + +// tierMover is one mover of a passed pipeline whose version the state written keeps: its module, fingerprint and tier. +type tierMover struct { + Module string `json:"module"` + Fp string `json:"fp"` + Tier int64 `json:"tier"` +} + +// tierProgress is a pipeline's tier progress (H-A-56): the tier whose wants are written, since when, and the movers. +type tierProgress struct { + Tier int64 `json:"tier"` + Since time.Time `json:"since"` + Movers []tierMover `json:"movers"` +} + +// tierKey is the key of pipeline's tier progress; pipeline is the runner's key `.`, its branch escaped. +func tierKey(pipeline string) string { return tierPrefix + pipeline } + +// isTierKey says whether a key of the state written is a pipeline's tier progress: three parts, the first `pipeline` (a module's +// end has two). +func isTierKey(key string) bool { + return strings.HasPrefix(key, tierPrefix) && strings.Count(key, ".") == 2 +} + +// startTiers writes the tier progress of a passed pipeline whose movers' versions are now kept, its first tier the lowest of +// theirs, since now. A pipeline heard again writes nothing. +func (e *pipelineEnds) startTiers(pipeline string, movers []tierMover) error { + if len(movers) == 0 { + return nil + } + first := movers[0].Tier + for _, m := range movers { + first = min(first, m.Tier) + } + value, err := json.Marshal(tierProgress{Tier: first, Since: e.clock().UTC(), Movers: movers}) + if err != nil { + return err + } + if err := e.st.swap(tierKey(pipeline), value, 0); err != nil && !errors.Is(err, errSwapped) { + return err + } + return nil +} + +func (e *pipelineEnds) clock() time.Time { + if e.now != nil { + return e.now() + } + return time.Now() +} + +// tiersOf is the tier progress of pipeline, with its revision; false when none stands. +func (e *pipelineEnds) tiersOf(pipeline string) (tierProgress, uint64, bool) { + raw, rev, err := e.st.get(tierKey(pipeline)) + var t tierProgress + if err != nil || raw == nil || json.Unmarshal(raw, &t) != nil { + return tierProgress{}, 0, false + } + return t, rev, true +} + +// tierWritten says whether the want of an end may be written: its pipeline's tier progress reached the end's rank, or no +// progress stands for its pipeline (every tier ran). +func (e *pipelineEnds) tierWritten(end End) bool { + t, _, ok := e.tiersOf(end.Pipeline) + return !ok || end.Rank <= t.Tier +} + +// tierBound is the setting tier-bound; zero or less is its default. +func (p *passer) tierBoundOf() time.Duration { + if p.tierBound > 0 { + return p.tierBound + } + return defaultTierBound +} + +// tiers moves each pipeline's tier progress (design 47 H §751): a tier whose placed movers all run on every present placing machine +// gives way to the next, the last to the end said `running`; a tier past tier-bound is late. placedOn is, by module, the nodes +// placing it on branch. It returns the earliest deadline still to come (zero: none), for one timer to wake the pass at. +func (p *passer) tiers(n assign.Nodes, placedOn func(module, branch string) []string) time.Time { + if p.ends == nil { + return time.Time{} + } + ks, err := p.ends.st.keys() + if err != nil { + p.logf("the tier progress cannot be listed: %v", err) + return time.Time{} + } + sort.Strings(ks) + now := p.clock().UTC() + bound := p.tierBoundOf() + var next time.Time + for _, key := range ks { + if !isTierKey(key) { + continue + } + pipeline := strings.TrimPrefix(key, tierPrefix) + branchEsc, source, _ := strings.Cut(pipeline, ".") + branch, err := keys.UnescapeName(branchEsc) + if err != nil { + continue + } + for { + t, rev, ok := p.ends.tiersOf(pipeline) + if !ok { + break + } + notRunning := p.notRunning(n, placedOn, pipeline, branch, t, t.Tier) + late := !now.Before(t.Since.Add(bound)) + if late { + p.tierLate(pipeline, branch, source, t, notRunning) + break + } + if len(notRunning) > 0 { + if d := t.Since.Add(bound); next.IsZero() || d.Before(next) { + next = d + } + break + } + // The tier runs: the next tier's wants are written, or every tier ran. + later := int64(0) + for _, m := range t.Movers { + if m.Tier > t.Tier && (later == 0 || m.Tier < later) { + later = m.Tier + } + } + if later == 0 { + if err := p.ends.st.drop(key); err != nil { + p.logf("the tier progress %s is not deleted: %v", key, err) + break + } + p.sayPipelineWanted(pipeline, branch, source, meshwants.PipelineWantedOutcomeRunning, nil) + break + } + value, err := json.Marshal(tierProgress{Tier: later, Since: now, Movers: t.Movers}) + if err == nil { + err = p.ends.st.swap(key, value, rev) + } + if errors.Is(err, errSwapped) { + continue + } + if err != nil { + p.logf("the tier progress %s is not moved to tier %d: %v", key, later, err) + break + } + p.logf("pipeline %s: tier %d runs on every present placing machine, so tier %d's wants are written", pipeline, t.Tier, later) + } + } + return next +} + +// notRunning is, of tier's movers in t whose kept end still names pipeline, those not running at their fingerprint on a present +// placing machine, with those machines. A mover a later pipeline moved counts as run; one placed nowhere counts as run. +func (p *passer) notRunning(n assign.Nodes, placedOn func(module, branch string) []string, pipeline, branch string, t tierProgress, tier int64) []meshwants.NotRunning { + var out []meshwants.NotRunning + for _, m := range t.Movers { + if m.Tier != tier { + continue + } + if end, ok := p.ends.endOf(m.Module, branch); !ok || end.Pipeline != pipeline { + continue // a later pipeline moved it + } + var machines []string + for _, node := range placedOn(m.Module, branch) { + if online, known := n.Online[node]; known && !online { + continue // a machine away is not waited for + } + r, ok := n.Running[node][m.Module] + if !ok || r.Stopped || r.Fingerprint != m.Fp { + machines = append(machines, node) + } + } + if len(machines) > 0 { + sort.Strings(machines) + out = append(out, meshwants.NotRunning{Module: m.Module, Machines: machines}) + } + } + return out +} + +// tierLate ends a pipeline's delivery at a tier past tier-bound: the finding tier-late is raised and pipeline-wanted says tier-late, +// once each; the finding clears once the late movers run or a later pipeline moved them. The progress stays, so no later tier is +// written, until no later tier's mover is kept at this pipeline any more. +func (p *passer) tierLate(pipeline, branch, source string, t tierProgress, notRunning []meshwants.NotRunning) { + o := tierLateFinding(pipeline, t.Tier, notRunning) + key := views.FindingKey(o.Scope, assign.KeyID(o.ID), o.Kind) + if len(notRunning) > 0 { + if !p.said[key+".raised"] { + if p.findings.Put != nil { + if err := p.findings.Put(o.Finding(p.clock())); err != nil { + p.logf("the finding %s is not written: %v", key, err) + return + } + } + p.said[key+".raised"] = true + delete(p.said, key+".cleared") + p.logf("pipeline %s: tier %d is late (tier-bound %s): %s; no later tier is written", pipeline, t.Tier, p.tierBoundOf(), o.Summary) + p.sayPipelineWanted(pipeline, branch, source, meshwants.PipelineWantedOutcomeTierLate, notRunning) + } + } else if !p.said[key+".cleared"] { + if p.findings.Delete != nil { + if err := p.findings.Delete(key); err != nil { + p.logf("the finding %s is not deleted: %v", key, err) + return + } + } + p.said[key+".cleared"] = true + delete(p.said, key+".raised") + } + for _, m := range t.Movers { + if m.Tier <= t.Tier { + continue + } + if end, ok := p.ends.endOf(m.Module, branch); ok && end.Pipeline == pipeline { + return // a later tier's mover still kept at this pipeline: its want stays unwritten + } + } + if len(notRunning) == 0 { + if err := p.ends.st.drop(tierKey(pipeline)); err != nil { + p.logf("the tier progress of %s is not deleted: %v", pipeline, err) + } + } +} + +// tierLateFinding is the finding tier-late of pipeline's tier, naming the movers and machines not running (design 47 H §751). +func tierLateFinding(pipeline string, tier int64, notRunning []meshwants.NotRunning) assign.Observed { + var named []string + for _, nr := range notRunning { + named = append(named, fmt.Sprintf("%s on %s", nr.Module, strings.Join(nr.Machines, ", "))) + } + summary := fmt.Sprintf("pipeline %s: tier %d does not run within tier-bound: %s", pipeline, tier, strings.Join(named, "; ")) + return assign.Observed{Scope: mcc.ScopeDelivery, ID: pipeline, Kind: kindTierLate, Severity: mcseat.FindingSeverityWarning, + Summary: summary, Headline: "A delivery's tier does not run in time", + Explanation: summary + ". The assigner writes no later tier of this pipeline; the delivery ends here.", + Source: "assigner." + kindTierLate} +} + +// sayPipelineWanted says the seat mesh-wants' pipeline-wanted of pipeline, once (its id the pipeline and the outcome). +func (p *passer) sayPipelineWanted(pipeline, branch, source, outcome string, notRunning []meshwants.NotRunning) { + var src int64 + _, _ = fmt.Sscan(source, &src) + body := meshwants.PipelineWantedEvent{V: keys.Version, Pipeline: pipeline, Branch: branch, Source: src, Outcome: outcome, NotRunning: notRunning} + event := meshwants.EventPipelineWanted + "." + pipeline + if err := p.wantsSay(event, body, "pipeline-wanted."+pipeline+"."+outcome); err != nil { + p.logf("pipeline-wanted of %s is not said: %v", pipeline, err) + } +} + +// tierDeadline sets the one timer that wakes the pass at the earliest tier's deadline (H-A-56); zero stops it. +func (p *passer) tierDeadline(at time.Time) { + if p.tierWake == nil { + return + } + if p.tierTimer != nil { + p.tierTimer.Stop() + } + if at.IsZero() { + return + } + p.tierTimer = time.AfterFunc(max(at.Sub(p.clock()), 0), p.tierWake) +} + +// tierBoundSetting is the setting tier-bound in minutes (design 47 H §751), read at the start from the file the manifest declares +// with the content ${config:tier-bound}; zero, its default, for a file missing or empty. +func tierBoundSetting(getenv func(string) string) (time.Duration, error) { + minutes, err := settingFile(getenv("MESH_ASSIGNER_TIER_BOUND_FILE")) + return time.Duration(minutes) * tierBoundUnit, err +} + +// tierBoundUnit is the unit of the setting tier-bound, a minute; the whole test sets a shorter one, so a tier passes its bound +// without a test waiting minutes. +var tierBoundUnit = time.Minute diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers_test.go new file mode 100644 index 00000000..39d9bb39 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers_test.go @@ -0,0 +1,118 @@ +package main + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" + mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + "git.novox.be/novox/mesh-sdk/go/catalogue" + "git.novox.be/novox/mesh-sdk/go/views" + + "mesh-delivery-assigner/internal/assign" + "mesh-delivery-assigner/internal/buckets" +) + +// A7/AC1, A7/AC2 (design 47 H §751, H-A-56): a pipeline of a provider (tier 1) and its consumer (tier 2) writes the provider's +// wants first and the consumer's only once the provider runs at its fingerprint on every present placing machine (one machine +// away is not waited for), then says pipeline-wanted running and drops its progress; a provider that never runs is late at +// tier-bound: the finding tier-late names it and its machine, pipeline-wanted says tier-late, and no consumer want is written. +func TestAPipelinesWantsAreWrittenTierByTier(t *testing.T) { + db, web := meshcatalogclient.SourcePrefix2+strings.Repeat("d", 64), meshcatalogclient.SourcePrefix2+strings.Repeat("c", 64) + for _, runs := range []bool{true, false} { + start := time.Date(2026, 11, 20, 4, 0, 0, 0, time.UTC) + now := start + wantsKV := &memKV{values: map[string][]byte{}} + p := newPasser(kvSet{buckets.Wants(): wantsKV, buckets.Asks(): &memKV{values: map[string][]byte{}}}, start) + p.now = func() time.Time { return now } + p.say = func(oc.Condition) error { return nil } + findings := map[string]mcseat.Finding{} + p.findings = assign.Findings{Put: func(f mcseat.Finding) error { findings[views.FindingKey(f.Scope, f.ID, f.Kind)] = f; return nil }, + Delete: func(key string) error { delete(findings, key); return nil }} + var said []meshwants.PipelineWanted + p.publishWants = func(event string, body any, _ string) error { + if b, ok := body.(meshwants.PipelineWantedEvent); ok { + if event != "pipeline-wanted."+trunkBranch+".5" { + t.Errorf("pipeline-wanted said as %s", event) + } + said = append(said, b) + } + return nil + } + for i, m := range []struct{ name, fp string }{{"db", db}, {"web", web}} { + digest := strings.Repeat(string(rune('e'+i)), 64) + mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: m.fp, Manifest: digest, Repository: "novox/" + m.name, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath}) + putModuleRaw(p.w, trunkBranch, m.name, mod) + p.w.manifests[digest] = parsed{module: m.name, manifest: catalogue.Manifest{Module: m.name}} + } + placeOn(t, p.w, "g14", "db", "web") + placeOn(t, p.w, "ace", "db", "web") + p.w.heardPresence("ace", true, start) // away: not waited for + p.w.mu.Lock() + p.w.runningHeard = true + p.w.mu.Unlock() + p.ends = newPipelineEnds(newMemEnds(), func() {}, t.Logf) + p.ends.trunk, p.ends.now = p.w.trunkName, func() time.Time { return now } + if _, err := p.ends.take(ended(trunkBranch, 5, mpr.PipelineEndedOutcomePassed, mover("db", db, "0.2.0", 1, mpr.StepResultOutcomePassed), + mover("web", web, "0.1.2", 2, mpr.StepResultOutcomePassed))); err != nil { + t.Fatal(err) + } + wanted := func(node, module string) bool { + key, _ := mwc.WantKey(node, module) + _, ok := wantsKV.values[key] + return ok + } + p.wants(context.Background()) + if !wanted("g14", "db") || !wanted("ace", "db") || wanted("g14", "web") || wanted("ace", "web") { + t.Fatalf("runs %v: tier 1 alone is not what is wanted first: %v", runs, wantsKV.values) + } + if runs { + raw, _ := json.Marshal(nodeengine.Running{Node: "g14", Module: "db", Fingerprint: db}) + if err := p.w.hearRunning("running.g14.db", raw); err != nil { + t.Fatal(err) + } + now = start.Add(time.Minute) + p.wants(context.Background()) + if !wanted("g14", "web") || !wanted("ace", "web") { + t.Fatalf("the consumer's want is not written once its provider runs: %v", wantsKV.values) + } + raw, _ = json.Marshal(nodeengine.Running{Node: "g14", Module: "web", Fingerprint: web}) + _ = p.w.hearRunning("running.g14.web", raw) + p.wants(context.Background()) + if len(said) != 1 || said[0].Outcome != meshwants.PipelineWantedOutcomeRunning || len(findings) != 0 { + t.Errorf("every tier runs: said %+v, findings %v", said, findings) + } + if _, _, ok := p.ends.tiersOf(trunkBranch + ".5"); ok { + t.Error("the tier progress stands after every tier ran") + } + continue + } + now = start.Add(29 * time.Minute) + p.wants(context.Background()) + if len(findings) != 0 || len(said) != 0 { + t.Fatalf("late before tier-bound: %v, %+v", findings, said) + } + now = start.Add(30 * time.Minute) + p.wants(context.Background()) + p.wants(context.Background()) + f, ok := findings["delivery."+assign.KeyID(trunkBranch+".5")+".tier-late"] + if !ok || !strings.Contains(f.Summary, "db on g14") || strings.Contains(f.Summary, "ace") { + t.Errorf("tier-late is not raised naming db on g14 alone: %v", findings) + } + if len(said) != 1 || said[0].Outcome != meshwants.PipelineWantedOutcomeTierLate || len(said[0].NotRunning) != 1 || said[0].NotRunning[0].Module != "db" { + t.Errorf("pipeline-wanted tier-late is not said once: %+v", said) + } + if wanted("g14", "web") || wanted("ace", "web") { + t.Error("a consumer's want is written after its provider's tier was late") + } + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go index e1d31602..c71e7dbd 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -1,7 +1,6 @@ package main import ( - mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" plc "mesh-delivery-assigner/internal/assignment" "encoding/json" @@ -22,13 +21,10 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/catalogkey" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/seatlist" "git.novox.be/novox/mesh-sdk/go/stateobjects" "mesh-delivery-assigner/internal/buckets" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient/enginereads" ) // The assigner's tools (novox/hq ADR 0546), served through the tool runner from its bundle: a process never serves (ADR @@ -36,9 +32,6 @@ import ( // every step as the event put. They are the module's own tools; the seat mesh-module-assigner's verbs are served beside them // (seat_tools.go, ADR 0567 §10). -// engineReadsTool lists what each node's engine reads, as the assigner uses it, and asks one node's engine again. -const engineReadsTool = "assigner_engine_reads" - // putsState is the assigner's state of puts, as the runtime names a module's own state. const putsState = "puts" @@ -215,12 +208,6 @@ func (s *putTools) doRelease(args map[string]any, by string) (any, error) { if arg(args, "how") != "" { return s.doReleaseWait(args, by) // a wait on an outside cause (outside_tools.go, design 47 G §706) } - if step := arg(args, "step"); step != "" { - if step != meshwants.WaitStepDelivery { - return nil, fmt.Errorf("step is %s, the one wait released by its step, not %q", meshwants.WaitStepDelivery, step) - } - return s.doReleaseDelivery(args, by) // a dependent's delivery wait (delivered.go, design 47 G §376, A-246) - } node, module, why := arg(args, "node"), arg(args, "module"), arg(args, "why") key, err := puts.Key(node, module) if err != nil { @@ -353,12 +340,10 @@ func (s *putTools) tools() []stdio.Tool { "rules write the machine's want again from their next pass. Nothing changes on the machine until they move it.", Input: schema(map[string]string{"node": "the machine", "module": "the module", "why": "why, kept with the put", "how": "with how, the wait of the delivery on an outside cause (assigner_waits) is released, not a put: go-on (the module counts healthy there), " + - "failed (the failing check is the delivery's), rejudge (judge it again now), or now (the entry waiting for an acting part's new version moves with the running one); written in the hand-act log first", - "step": "delivery: the module's wait for its dependency's delivery (assigner_waits) is released, its want written now; no machine is named; written in the hand-act log first"}, + "failed (the failing check is the delivery's), or rejudge (judge it again now); written in the hand-act log first"}, []string{"module", "why"}), Run: func(a map[string]any) (any, error) { return s.doRelease(a, stdio.CallerOf(a)) }}, s.waitsTool(), - s.deliveredAgainTool(), {Name: toolPuts, Description: "Every put and where it stands (novox/hq ADR 0546): its version, hold, why, each step with " + "its time and words. Narrowed by machine or module. Read-only.", Input: schema(map[string]string{"node": "one machine", "module": "one module"}, nil), @@ -403,7 +388,7 @@ func serveTools() error { } }() // The seat list (seatlist.go), read through the runtime once Serve has its channel: the seat's verbs judge by it. - gate := seatlist.NewPart("assigner", seatListAgainCallers) + gate := seatlist.NewPart("assigner") go func() { <-stdio.Served() gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-delivery-assigner: "+f+"\n", a...) }) @@ -417,8 +402,6 @@ func serveTools() error { return stdio.SeatPublish(wantsEventSubject(e), b, id) }) wants.watched = stdio.NewWatchedStates() // the large states read whole from one watched copy (ADR 0580 clause 3) - // The callers' seat entries are resolved from the assigner's own seat-holders, its watched copy (A-298). - gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return wants.watched.Values(mma.StateSeatHolders) })) // The verb assignments with seats answers from the same watched copy, so every caller of it is fast too (issue 1982). seatVerbs.watched = wants.watched wants.revisions = runtimeObjects{busstate.ObjectsOf(assigngraph.RevisionsObjects)} @@ -427,12 +410,12 @@ func serveTools() error { tools = append(tools, inputsRead(keeper), inputsDrop(keeper, stdio.Ask)) tools = append(tools, compositionsTools(stdio.Ask)...) // the composition runs' read and drop, through their verbs (§728) tools = append(tools, viewTools(os.Getenv, keptSource, time.Now)...) - // What each node's engine reads, as the assigner uses it (ADR 0618 §3): its own state engine-reads, asked again through it. - tools = append(tools, enginereads.Tool(engineReadsTool, "the assigner", enginereads.KeptState(stdio.State(engineReadsState)), false, time.Now)) + // What each node's engine reads, as the assigner uses it: its own state engine-reads (design 47 H-A-104). + tools = append(tools, engineReadsTools(runtimeStore{})...) tools = append(tools, writtenTools(runtimeStore{})...) tools = append(tools, whyTools(runtimeStore{})...) - tools = append(tools, pinsTools(runtimeStore{}, stdio.Ask)...) tools = append(tools, askAgainTools(runtimeStore{}, stdio.Ask, time.Now)...) + tools = append(tools, wantDropTools(runtimeStore{}, stdio.Ask)...) return stdio.Serve("", tools) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep.go new file mode 100644 index 00000000..2b2a4dbb --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep.go @@ -0,0 +1,367 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "maps" + "slices" + "strings" + + mcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" +) + +// The step validate (design 47 H §745; H-A-34, H-A-51, H-A-108): the assigner serves it as an accepted kind `step` of the seat +// mesh-wants, asked by the runner for each mover before its composition step. One run per take-in revision, a key +// `.validate` of the state composition-runs in the CompositionRun form with no machine, which every mover's step +// reads: the cycle the revision's entries make (graph.RefusedCycle) ends it at once; else the manifest validator built from +// the revision's own entry of the module holding the seat mesh-catalog, on the Go build seat as the composer is, runs in the +// sandbox over the revision's manifests and the branch's previous revision's, its input kept and served on mesh-wants' input. +// Every mover's step ends the same: failed `cycle` naming the cycle, failed `manifest-refused` naming every problem of a +// manifest the revision alters or between manifests one it alters takes part in, or passed. + +const ( + // validatorPackage is the manifest validator's import path in the module holding the seat mesh-catalog (the indexer's + // cmd/mesh-judge, design 47 H-A-34). + validatorPackage = "mesh-delivery-indexer/cmd/mesh-judge" + // validateKeySuffix ends the key of a revision's validate run, `.validate` (H-A-108). + validateKeySuffix = "." + meshwants.StepRunStepValidate + // beforeCase is the validator's case of the branch's previous revision, beside changeCase (H-A-108 (3)). + beforeCase = "before" + // The reasons a validate step fails with, the first word of its said (§745). + reasonManifestRefused = "manifest-refused" + reasonCycle = "cycle" + // betweenManifests is the validator's name for what it judges between manifests (the catalogue as a whole). + betweenManifests = "(between manifests)" +) + +// validateKeyOf is the key of the validate run of the step's take-in revision. +func validateKeyOf(step meshwants.StepRun) (string, error) { + if step.Pipeline == "" { + return "", errors.New("the step names no pipeline, so no validate run is named") + } + return step.Pipeline + validateKeySuffix, nil +} + +// isValidateRun says whether a run of the state is a revision's validate run: one with no machine. +func isValidateRun(run meshwants.CompositionRun) bool { return run.Machine == "" } + +// validate runs one mover's validate step: it joins or starts the revision's validate run and judges the step from its end. +func (r *compositionRun) validate(ctx context.Context, id string, step meshwants.StepRun) stepOutcome { + key, err := validateKeyOf(step) + if err != nil { + return couldNotRun(err.Error()) + } + candidate, err := candidateKeyOf(step) + if err != nil { + return couldNotRun(err.Error()) + } + j, err := r.runs.joinAt(key, readerOf(id, step), candidate, "", buildstep.NewID()) + if err != nil { + return couldNotRun("the validate run " + key + " is not joined: " + err.Error()) + } + if j.started { + go r.startValidate(ctx, key, j.run, step) + } + return r.readValidate(ctx, key, step) +} + +// resumeValidate judges a validate step taken up at the assigner's start from its revision's run. +func (r *compositionRun) resumeValidate(ctx context.Context, _ string, step meshwants.StepRun) stepOutcome { + key, err := validateKeyOf(step) + if err != nil { + return couldNotRun(err.Error()) + } + return r.readValidate(ctx, key, step) +} + +// readValidate waits for the end of the validate run of key and judges the step from it. +func (r *compositionRun) readValidate(ctx context.Context, key string, step meshwants.StepRun) stepOutcome { + setShared(ctx, r.runs.sharedOf([]string{key}, nil)) + setPhase(ctx, meshwants.StepProgressEventPhaseRun) + run := r.runs.await(ctx, []string{key})[key] + setPhase(ctx, meshwants.StepProgressEventPhaseReport) + out := r.judgeValidate(ctx, step, run) + out.Shared = r.runs.sharedOf([]string{key}, map[string]meshwants.CompositionRun{key: run}) + return out +} + +// judgeValidate is a step's end from its revision's validate run: could not run with the run's words; failed `cycle` naming +// the cycle; failed `manifest-refused` naming every problem the revision causes; else passed. +func (r *compositionRun) judgeValidate(ctx context.Context, step meshwants.StepRun, run meshwants.CompositionRun) stepOutcome { + if run.Ended == nil { + return couldNotRun("the step stopped before its validate run ended") + } + if run.Outcome != meshwants.CompositionRunOutcomePassed { + return couldNotRun(run.Said) + } + var v validateVerdict + if raw, err := json.Marshal(run.Verdict); err != nil || json.Unmarshal(raw, &v) != nil { + return couldNotRun("the validate run's verdict does not read") + } + if len(v.Cycle) > 0 { + return stepOutcome{Outcome: meshwants.StepEndedOutcomeFailed, Said: reasonCycle + ": " + cycleWords(v.Cycle)} + } + half, err := r.revision(ctx, step) + if err != nil { + return couldNotRun("the step's catalog revision is not read: " + err.Error()) + } + before, err := r.before(ctx, step) + if err != nil { + return couldNotRun("the branch's previous catalog revision is not read: " + err.Error()) + } + if problems := causedProblems(v.Change, v.Before, altered(half, before)); len(problems) > 0 { + return stepOutcome{Outcome: meshwants.StepEndedOutcomeFailed, Said: reasonManifestRefused + ": " + strings.Join(problems, "; ")} + } + return stepOutcome{Outcome: meshwants.StepEndedOutcomePassed} +} + +// validateVerdict is a validate run's verdict as the run keeps it: the cycle the revision makes, or the validator's two cases. +type validateVerdict struct { + Cycle []meshcatalog.ArtifactRef `json:"cycle,omitempty"` + Change composer.CaseVerdict `json:"change"` + Before composer.CaseVerdict `json:"before"` +} + +// asVerdict is v as a run's verdict. +func (v validateVerdict) asVerdict() (map[string]any, error) { + raw, err := json.Marshal(v) + if err != nil { + return nil, err + } + var out map[string]any + return out, json.Unmarshal(raw, &out) +} + +// cycleWords names a cycle as the artifacts it passes through. +func cycleWords(cycle []meshcatalog.ArtifactRef) string { + words := make([]string, len(cycle)) + for i, a := range cycle { + words[i] = string(a) + } + return strings.Join(words, " → ") +} + +// before is the branch's catalog revision before the step's, read through the seat mesh-catalog's verb revision (H-A-108 (3)); +// none before the first. +func (r *compositionRun) before(ctx context.Context, step meshwants.StepRun) (mcc.Half, error) { + if step.Revision <= 1 { + return mcc.Half{Entries: map[string]meshcatalog.CatalogEntry{}}, nil + } + prev := step + prev.Revision-- + return r.revision(ctx, prev) +} + +// altered are the modules whose manifest the revision adds or changes against the one before it. +func altered(half, before mcc.Half) []string { + var out []string + for name, e := range half.Entries { + if len(e.Manifest) == 0 { + continue + } + b, ok := before.Entries[name] + switch { + case !ok || len(b.Manifest) == 0: + out = append(out, name) + case e.ManifestDigest != "" && b.ManifestDigest != "": + if e.ManifestDigest != b.ManifestDigest { + out = append(out, name) + } + default: + x, _ := json.Marshal(e.Manifest) + y, _ := json.Marshal(b.Manifest) + if string(x) != string(y) { + out = append(out, name) + } + } + } + slices.Sort(out) + return out +} + +// causedProblems are the problems the revision causes (ADR 0578 §2): every problem of a manifest it alters, and of any other +// manifest, or between manifests, each problem the case before did not have. +func causedProblems(change, before composer.CaseVerdict, touched []string) []string { + var out []string + for _, module := range slices.Sorted(maps.Keys(change.Manifests)) { + read := change.Manifests[module] + if read.Accepted || read.Refused == "" { + continue + } + had := map[string]bool{} + for _, p := range strings.Split(before.Manifests[module].Refused, "\n") { + had[p] = true + } + var caused []string + for _, p := range strings.Split(read.Refused, "\n") { + if p != "" && (slices.Contains(touched, module) || !had[p]) { + caused = append(caused, p) + } + } + if len(caused) == 0 { + continue + } + what := module + if module == betweenManifests { + what = "between manifests" + } + out = append(out, what+": "+strings.Join(caused, "; ")) + } + return out +} + +// catalogEntry is the entry of the module that holds the seat mesh-catalog, or false. +func catalogEntry(entries map[string]meshcatalog.CatalogEntry) (meshcatalog.CatalogEntry, bool) { + for _, name := range slices.Sorted(maps.Keys(entries)) { + for _, s := range entries[name].Serves { + if strings.HasPrefix(strings.TrimPrefix(s, "seat:"), meshcatalog.Seat+".") { + return entries[name], true + } + } + } + return meshcatalog.CatalogEntry{}, false +} + +// manifestsOf are the half's manifests whole, as the validator's case holds them. +func manifestsOf(half mcc.Half) (map[string]composer.Manifest, error) { + out := map[string]composer.Manifest{} + for name, e := range half.Entries { + if len(e.Manifest) == 0 { + continue + } + raw, err := json.Marshal(e.Manifest) + if err != nil { + return nil, fmt.Errorf("the manifest of %s cannot be encoded: %w", name, err) + } + out[name] = composer.Manifest{Raw: raw} + } + return out, nil +} + +// startValidate runs the validate run the step started: the revision's cycle ends it at once; else the validator's request +// is kept once in `inputs`, its program and input recorded on the run, and one run ask put on the Go build seat. A fault ends +// the run could not run. +func (r *compositionRun) startValidate(ctx context.Context, key string, run meshwants.CompositionRun, step meshwants.StepRun) { + ask := run.Ask + end := func(outcome, said string, verdict map[string]any) { + if _, err := r.runs.endRun(key, ask, outcome, said, verdict); err != nil { + r.logf("the validate run %s is not ended: %v", key, err) + } + } + couldNot := func(said string) { end(meshwants.CompositionRunOutcomeCouldNotRun, said, nil) } + half, err := r.revision(ctx, step) + if err != nil { + couldNot("the step's catalog revision is not read: " + err.Error()) + return + } + if cycle := graph.RefusedCycle(half.Entries); cycle != nil { + verdict, err := validateVerdict{Cycle: cycle}.asVerdict() + if err != nil { + couldNot(err.Error()) + return + } + end(meshwants.CompositionRunOutcomePassed, "", verdict) + return + } + before, err := r.before(ctx, step) + if err != nil { + couldNot("the branch's previous catalog revision is not read: " + err.Error()) + return + } + entry, ok := catalogEntry(half.Entries) + if !ok { + couldNot(fmt.Sprintf("the catalog revision %d holds no module claiming the seat %s, so no manifest validator is built", half.Revision.Revision, meshcatalog.Seat)) + return + } + program, err := programOfEntry(entry) + if err != nil { + couldNot(err.Error()) + return + } + program.Package = validatorPackage + seats, err := r.seats(ctx, half.Revision.Definitions) + if err != nil { + couldNot(err.Error()) + return + } + change, err := manifestsOf(half) + if err != nil { + couldNot(err.Error()) + return + } + prior, err := manifestsOf(before) + if err != nil { + couldNot(err.Error()) + return + } + // No predefined key list and no known package names until a design line names the verb that gives them (H-A-108 (2)). + req := composer.Request{V: composer.Version, Cases: []composer.Case{{Name: changeCase, Manifests: change}, {Name: beforeCase, Manifests: prior}}, + Seats: seats} + raw, err := json.Marshal(req) + if err != nil { + couldNot("its request cannot be encoded: " + err.Error()) + return + } + digest, err := r.keeper.keep(ask, raw) + if err != nil { + couldNot("its request is not kept: " + err.Error()) + return + } + ref, err := buildstep.RunInputOf(inputVerbKey, raw) + if err != nil { + couldNot(err.Error()) + return + } + if _, _, err := r.runs.change(key, func(c *meshwants.CompositionRun) bool { + if c.Ask != ask || c.Ended != nil { + return false + } + c.Program = &meshwants.CompositionRunProgram{Repository: program.Repository, Commit: program.Commit, Directory: program.Dir, Fingerprint: entry.Fp} + c.Input = digest + return true + }); err != nil { + couldNot("the run's program and input are not written: " + err.Error()) + return + } + a := buildstep.StepAsk{ID: ask, Kind: buildstep.AskCheck, Language: buildstep.AskGo, Asker: runAsker, At: r.now().UTC(), + Run: &buildstep.RunJob{Program: program, Heads: []buildstep.RunHead{}, Input: ref, Reader: "the manifest validator of " + step.Pipeline + " at " + program.Commit}} + if err := r.put(ctx, a); err != nil { + couldNot("its run ask " + ask + " is not put: " + err.Error()) + } +} + +// validateRunVerdict reads the validator's answer: its two cases as the run keeps them, or why it is none. +func validateRunVerdict(answer []byte) (map[string]any, string) { + var v composer.Verdict + if err := json.Unmarshal(answer, &v); err != nil { + return nil, "the manifest validator answered no verdict: " + err.Error() + } + if v.V != composer.Version { + return nil, fmt.Sprintf("the manifest validator answered a verdict of version %d, the assigner reads %d", v.V, composer.Version) + } + var out validateVerdict + found := 0 + for _, c := range v.Cases { + switch c.Name { + case changeCase: + out.Change, found = c, found+1 + case beforeCase: + out.Before, found = c, found+1 + } + } + if found != 2 { + return nil, "the manifest validator's verdict does not hold both the cases " + changeCase + " and " + beforeCase + } + verdict, err := out.asVerdict() + if err != nil { + return nil, "the manifest validator's verdict cannot be kept: " + err.Error() + } + return verdict, "" +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep_test.go new file mode 100644 index 00000000..13e6d409 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep_test.go @@ -0,0 +1,120 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "strings" + "sync" + "testing" + "time" + + mcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" +) + +// A10/AC1, A10/AC2 (design 47 H §745; H-A-34, H-A-51, H-A-108): every mover's validate step of one take-in reads one validate +// run, `.validate` of the state composition-runs; a manifest the revision alters that names an unknown field fails +// it `manifest-refused` naming the problem, a problem of a manifest it leaves as it was does not, and the validator is the +// revision's own entry of the module holding the seat mesh-catalog, on the Go build seat; a revision whose entries make a cycle +// fails it `cycle` naming the cycle, with no validator asked. +func TestTheValidateStepRefusesAManifestProblemAndACycle(t *testing.T) { + indexer := meshcatalog.CatalogEntry{Module: "indexer", Repository: "novox/mesh-delivery", Commit: commitE3, Path: "modules/mesh-delivery-indexer", + Fp: "src2:ix", Serves: []string{"seat:" + meshcatalog.Seat + ".revision"}, Manifest: map[string]any{"module": "indexer"}, ManifestDigest: "d-ix"} + entry := func(module, digest string, edges ...meshcatalog.Edge) meshcatalog.CatalogEntry { + return meshcatalog.CatalogEntry{Module: module, Manifest: map[string]any{"module": module, "d": digest}, ManifestDigest: digest, Edges: edges} + } + revisions := map[int64][]meshcatalog.CatalogEntry{ + 6: {indexer, entry("web", "d-web-1"), entry("db", "d-db")}, + 7: {indexer, entry("web", "d-web-2"), entry("db", "d-db")}, + 9: {indexer, entry("a", "d-a", meshcatalog.Edge{From: "a:x", To: "b:y", Kind: 3}, meshcatalog.Edge{From: "b:y", To: "a:x", Kind: 3}), entry("b", "d-b")}, + } + call := func(key string, body any) (json.RawMessage, error) { + raw, _ := json.Marshal(body) + switch key { + case mcc.RevisionVerb: + var in meshcatalog.RevisionInput + _ = json.Unmarshal(raw, &in) + entries, ok := revisions[in.Revision] + if !ok || in.Branch != "main" { + return nil, errors.New("asked another revision") + } + return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Revision: in.Revision, + Definitions: []meshcatalog.CatalogRevisionDefinitionsItem{{Seat: "s1", Digest: "sd"}}}, Entries: entries}) + case seatDefinitionsKey: + return json.Marshal(meshcatalog.SeatDefinitionsAnswer{Found: true, Entry: &meshcatalog.SeatDefinitionsAnswerEntry{Seat: "s1", Digest: "sd", Definition: map[string]any{"seat": "s1"}}}) + } + return nil, errors.New("no such verb " + key) + } + var mu sync.Mutex + var asks []buildstep.StepAsk + var requests []composer.Request + keeper := testKeeper(&objectsMem{objs: map[string][]byte{}}, time.Now) + runs := newCompositionRuns(newRunsMem(), defaultRunSettings, time.Now, t.Logf) + r := &compositionRun{runs: runs, keeper: keeper, reads: &compositionReads{}, call: call, now: time.Now, logf: t.Logf, + put: func(_ context.Context, a buildstep.StepAsk) error { + body, err := keeper.body(a.Run.Input.Digest) + if err != nil { + return err + } + req, err := composer.Decode(body) + if err != nil { + return err + } + mu.Lock() + asks, requests = append(asks, a), append(requests, req) + mu.Unlock() + answer, _ := json.Marshal(composer.Verdict{V: composer.Version, Cases: []composer.CaseVerdict{ + {Name: changeCase, Manifests: map[string]composer.ManifestRead{"web": {Refused: `unknown field "colour"`}, "db": {Refused: "an old problem"}, "indexer": {Accepted: true}}}, + {Name: beforeCase, Manifests: map[string]composer.ManifestRead{"db": {Refused: "an old problem"}}}}}) + go func() { + if err := runs.heardEnd(buildstep.StepEnded{Asker: runAsker, Ask: a.ID, Outcome: buildstep.OutcomePassed, Node: "novox"}, + func(buildstep.StepEnded) ([]byte, error) { return answer, nil }); err != nil { + t.Error(err) + } + }() + return nil + }} + steps := &compositionSteps{validate: r.validate, run: r.run} + if steps.howOf(meshwants.StepRunStepValidate, false) == nil || steps.howOf("check", false) != nil { + t.Error("the validate step is not taken, or another step is") + } + step := func(module string, revision int64) meshwants.StepRun { + return meshwants.StepRun{Step: meshwants.StepRunStepValidate, Pipeline: "main." + string(rune('0'+revision)), Branch: "main", Module: module, + Revision: revision, Source: revision} + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + outs := make([]stepOutcome, 2) + var wg sync.WaitGroup + for i, module := range []string{"web", "db"} { + wg.Add(1) + go func() { defer wg.Done(); outs[i] = r.validate(ctx, "ask-"+module, step(module, 7)) }() + } + wg.Wait() + for i, o := range outs { + if o.Outcome != meshwants.StepEndedOutcomeFailed || !strings.HasPrefix(o.Said, reasonManifestRefused+": ") || !strings.Contains(o.Said, `web: unknown field "colour"`) || + strings.Contains(o.Said, "an old problem") { + t.Errorf("mover %d's validate step ended %+v", i, o) + } + } + if len(asks) != 1 { + t.Fatalf("%d validator asks were put for one take-in, want 1", len(asks)) + } + if p := asks[0].Run.Program; p.Commit != commitE3 || p.Dir != "modules/mesh-delivery-indexer" || p.Package != validatorPackage || len(asks[0].Run.Heads) != 0 { + t.Errorf("the validator is built from %+v", asks[0].Run) + } + if c := requests[0].Cases; len(c) != 2 || c[0].Name != changeCase || c[1].Name != beforeCase || len(c[1].Manifests) != 3 || requests[0].Predefined != nil || requests[0].Packages != nil { + t.Errorf("the validator's request is %+v", requests[0]) + } + if _, _, ok, _ := runs.read("main.7" + validateKeySuffix); !ok { + t.Error("the validate run is not the key .validate of composition-runs") + } + o := r.validate(ctx, "ask-a", step("a", 9)) + if o.Outcome != meshwants.StepEndedOutcomeFailed || o.Said != reasonCycle+": a:x → b:y → a:x" || len(asks) != 1 { + t.Errorf("a revision making a cycle ended %+v (%d validator asks)", o, len(asks)) + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/verbs.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/verbs.go index 9a4dec05..9f867b0c 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/verbs.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/verbs.go @@ -24,26 +24,6 @@ import ( // moduleGraph is the module graph of the catalog half of branch, from the entries' invokes and serves (design 47 G §184, §195). func (p *passer) moduleGraph(branch string) graph.Graph { return p.w.graphOf(branch) } -// keptInvokes are the verbs of module the graph keeps as edges: each it invokes and does not leave out, that a module -// other than itself serves. A verb no module of the graph serves is no edge, so it holds nothing; nil when the graph holds -// no node of module. -func keptInvokes(g graph.Graph, module string) []string { - n, ok := g[module] - if !ok { - return nil - } - var out []string - for _, v := range n.Invokes { - if slices.Contains(n.LeftOut, v) { - continue - } - if slices.ContainsFunc(g.Servers(v), func(s string) bool { return s != module }) { - out = append(out, v) - } - } - return out -} - // served is, for this pass, whether a build serving a verb runs healthy on some node (graph.Served): every running healthy // have's build (the builder's builds key of its module and fingerprint) names its manifest, and the verbs that manifest // serves are served. A healthy build whose manifest cannot be read counts what the graph says its module serves, said in diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/views.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/views.go index 92eeecb7..2aec6e8d 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/views.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/views.go @@ -73,7 +73,7 @@ func viewTools(getenv func(string) string, state func(string) views.Source, now marks := state(views.MarksState) var out []stdio.Tool for _, t := range firstViewTools { - out = append(out, views.FirstViewTool(assignment.Module(), t.tool, t.state, t.words, views.ParseCallers(getenv(views.CallersEnv(t.tool))), state(t.state), marks, now)) + out = append(out, views.FirstViewTool(assignment.Module(), t.tool, t.state, t.words, state(t.state), marks, now)) } for _, t := range readTools { out = append(out, views.ReadTool(assignment.Module(), t.tool, t.state, t.words, state(t.state), marks, now)) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go new file mode 100644 index 00000000..44bf1a93 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go @@ -0,0 +1,146 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "slices" + "strings" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// wantDropTool drops one want of a node for a module by hand (design 47 H §765, H-A-58): a held or failed want of the earlier +// model goes at the clean state, its key deleted and the act written in the hand-act log; the next pass writes the want of the +// new form. +const wantDropTool = "assigner_want_drop" + +// wantsState is the state the wants are kept in. +const wantsState = "wants" + +// dropWant deletes the want of module on node, the act first recorded through the seat mesh-hand-acts. +func dropWant(st seatStore, ask func(string, any) (json.RawMessage, error), node, module, why, by string) (any, error) { + switch { + case node == "" || module == "": + return nil, errors.New("name the node and the module whose want is dropped") + case why == "": + return nil, errors.New("a want dropped by hand says why") + case by == "" || by == stdio.Unproven: + return nil, errors.New("a want dropped by hand names who drops it, and the runtime proved no caller") + } + key, err := mwc.WantKey(node, module) + if err != nil { + return nil, err + } + raw, _, found, err := st.get(wantsState, key) + if err != nil { + return nil, err + } + if !found { + return nil, fmt.Errorf("the assigner holds no want under %s", key) + } + taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: wantDropTool + " " + key, Why: why + " (" + by + ")", Cause: "want-drop"}) + if err != nil { + return nil, fmt.Errorf("the drop of %s is not recorded, so the want stays: %w", key, err) + } + if err := st.del(wantsState, key); err != nil { + return nil, fmt.Errorf("the want %s is not dropped: %w", key, err) + } + return map[string]any{"key": key, "dropped": json.RawMessage(raw), "by": by, "hand-act": taken.Log}, nil +} + +// The kinds the all form counts what it dropped by (design 47 H-A-123). +const ( + dropHeld = "held" + dropCurrent = "current" + dropUndecodable = "undecodable" +) + +// dropAllWants deletes every want key of the state wants, held or not, and every value of an earlier form the decoder refuses +// (the all form, design 47 H-A-121 (2), H-A-123): each act recorded through the seat mesh-hand-acts first, as the one-item form +// does; it answers what it dropped, counted per kind. A drop not recorded stops it, saying what went before. +func dropAllWants(st seatStore, ask func(string, any) (json.RawMessage, error), why, by string) (any, error) { + switch { + case why == "": + return nil, errors.New("a want dropped by hand says why") + case by == "" || by == stdio.Unproven: + return nil, errors.New("a want dropped by hand names who drops it, and the runtime proved no caller") + } + all, err := st.keys(wantsState) + if err != nil { + return nil, err + } + slices.Sort(all) + counted := map[string]int{dropHeld: 0, dropCurrent: 0, dropUndecodable: 0} + dropped := map[string][]string{} + answer := func() map[string]any { + return map[string]any{"dropped": dropped, "counted": counted, "by": by} + } + for _, key := range all { + if !strings.HasPrefix(key, "want.") { + continue + } + raw, _, found, err := st.get(wantsState, key) + if err != nil { + return answer(), fmt.Errorf("the want %s cannot be read, so the drop stops there: %w", key, err) + } + if !found { + continue + } + kind := wantKind(key, raw) + taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: wantDropTool + " " + key, Why: why + " (" + by + ")", Cause: "want-drop"}) + if err != nil { + return answer(), fmt.Errorf("the drop of %s is not recorded, so it and the wants after it stay: %w", key, err) + } + if err := st.del(wantsState, key); err != nil { + return answer(), fmt.Errorf("the want %s is not dropped: %w", key, err) + } + counted[kind]++ + dropped[kind] = append(dropped[kind], key+" (hand act "+taken.Log+")") + } + return answer(), nil +} + +// wantKind says how the all form counts a want: undecodable when its key or value is of no form the decoder takes, held when it +// holds, current otherwise (design 47 H-A-123). +func wantKind(key string, raw []byte) string { + if _, err := mwc.ParseWantNodeKey(key); err == nil { + if _, err := mwc.DecodeNodeWant(raw); err != nil { + return dropUndecodable + } + return dropCurrent + } + if _, _, err := mwc.ParseWantKey(key); err != nil { + return dropUndecodable + } + w, err := mwc.DecodeWant(raw) + switch { + case err != nil: + return dropUndecodable + case w.Hold: + return dropHeld + } + return dropCurrent +} + +// wantDropTools is the tool over the runtime's states. +func wantDropTools(st seatStore, ask func(string, any) (json.RawMessage, error)) []stdio.Tool { + return []stdio.Tool{{Name: wantDropTool, Description: "Drop one want by hand (design 47 H §765): the want of module on node is " + + "deleted from the state wants, as the clean state drops a held or failed want of the earlier model; the next pass writes the " + + "want of the new form. Recorded as a hand act first; refused with no want under the key. With all true and no node or module, " + + "every want key goes, held or not, and every value of an earlier form the decoder refuses, each recorded as a hand act; it " + + "answers what it dropped, counted per kind (held, current, undecodable).", + Input: schema(map[string]string{"node": "the node's name", "module": "the module's name", "why": "why the want is dropped", + "all": "true drops every want of the state wants, naming no node or module"}, []string{"why"}, "all"), + Run: func(a map[string]any) (any, error) { + if arg(a, "all") == "true" { + if arg(a, "node") != "" || arg(a, "module") != "" { + return nil, errors.New("the all form drops every want: name no node or module with it") + } + return dropAllWants(st, ask, arg(a, "why"), stdio.CallerOf(a)) + } + return dropWant(st, ask, arg(a, "node"), arg(a, "module"), arg(a, "why"), stdio.CallerOf(a)) + }}} +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go new file mode 100644 index 00000000..fefdab08 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go @@ -0,0 +1,99 @@ +package main + +import ( + "encoding/json" + "errors" + "strings" + "testing" + + mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" +) + +// A12/AC1 (design 47 H §765, H-A-58): assigner_want_drop with node, module and why records the act in the hand-act log first +// and then deletes that want; with no want under the key, no why or no proven caller, and when the act is not recorded, nothing is +// dropped. +func TestAWantIsDroppedByHandAndRecorded(t *testing.T) { + key, _ := mwc.WantKey("ace", "web") + held, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest, Put: true, Hold: true, Words: "put: x"}) + for _, c := range []struct { + name, node, module, why, by string + refuse error + dropped bool + }{ + {"dropped", "ace", "web", "the clean state", "person.jochen", nil, true}, + {"no want there", "ace", "db", "the clean state", "person.jochen", nil, false}, + {"no why", "ace", "web", "", "person.jochen", nil, false}, + {"no caller", "ace", "web", "the clean state", "", nil, false}, + {"not recorded", "ace", "web", "the clean state", "person.jochen", errors.New("no responders"), false}, + } { + st := memStore{wantsState: &memKV{values: map[string][]byte{key: held}}} + var asked []string + ask := func(verb string, body any) (json.RawMessage, error) { + raw, _ := json.Marshal(body) + asked = append(asked, verb+" "+string(raw)) + return json.RawMessage(`{}`), c.refuse + } + out, err := dropWant(st, ask, c.node, c.module, c.why, c.by) + _, there := st[wantsState].values[key] + switch { + case c.dropped && (err != nil || there || len(asked) != 1 || !strings.Contains(asked[0], "seat:mesh-hand-acts.hand-act ") || !strings.Contains(asked[0], wantDropTool+" "+key)): + t.Errorf("%s: %v, %v; the want left %v, asked %v", c.name, out, err, there, asked) + case !c.dropped && (err == nil || !there): + t.Errorf("%s: %v, %v; the want left %v, want it refused and kept", c.name, out, err, there) + } + } +} + +// H-A-121 (2), H-A-123: assigner_want_drop's all form drops every want key, held or not, and every value of an earlier form, +// each recorded as a hand act first, answering the count per kind; a held fingerprint's key stays; a drop not recorded stops it; +// the all form names no node or module. +func TestEveryWantIsDroppedByHandAndCounted(t *testing.T) { + heldKey, _ := mwc.WantKey("ace", "web") + curKey, _ := mwc.WantKey("ace", "db") + nodeKey, _ := mwc.WantNodeKey("ace") + oldKey, _ := mwc.WantKey("g14", "web") + fpKey, _ := mwc.HeldKey("web", fpTest) + held, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest, Put: true, Hold: true, Words: "put: x"}) + cur, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest}) + node, _ := mwc.EncodeNodeWant(mwc.NodeWant{Self: true}) + values := func() map[string][]byte { + return map[string][]byte{heldKey: held, curKey: cur, nodeKey: node, oldKey: []byte(`{"fingerprint":"old"}`), fpKey: []byte(`{}`)} + } + for _, c := range []struct { + name string + refuse error + left int + }{ + {"dropped", nil, 1}, + {"not recorded", errors.New("no responders"), 5}, + } { + st := memStore{wantsState: &memKV{values: values()}} + var asked []string + ask := func(verb string, body any) (json.RawMessage, error) { + raw, _ := json.Marshal(body) + asked = append(asked, string(raw)) + return json.RawMessage(`{}`), c.refuse + } + out, err := dropAllWants(st, ask, "the clean state", "person.jochen") + if left := len(st[wantsState].values); left != c.left || (c.refuse == nil) != (err == nil) { + t.Errorf("%s: %v, %v; %d keys left, want %d", c.name, out, err, left, c.left) + } + if c.refuse != nil { + continue + } + if _, ok := st[wantsState].values[fpKey]; !ok { + t.Errorf("the held fingerprint's key %s is dropped: it is no want", fpKey) + } + counted := out.(map[string]any)["counted"].(map[string]int) + if counted[dropHeld] != 1 || counted[dropCurrent] != 2 || counted[dropUndecodable] != 1 || len(asked) != 4 { + t.Errorf("counted %v, asked %d hand acts; want held 1, current 2, undecodable 1 and 4 acts", counted, len(asked)) + } + } + if _, err := dropAllWants(memStore{wantsState: &memKV{values: values()}}, nil, "", "person.jochen"); err == nil { + t.Error("the all form dropped with no why") + } + run := wantDropTools(memStore{wantsState: &memKV{values: values()}}, nil)[0].Run + if _, err := run(map[string]any{"all": "true", "node": "ace", "why": "x"}); err == nil || !strings.Contains(err.Error(), "name no node") { + t.Errorf("the all form with a node: %v, want it refused", err) + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/why.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/why.go index 54b585f7..94d73776 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/why.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/why.go @@ -26,34 +26,36 @@ const whyTool = "assigner_why" // The steps of the pass that decide a want. const ( - whyNoPipeline = "no-passed-pipeline" // the trunk's fingerprint passed no pipeline on the branch, and no want names it - whyDelivery = "waits-for-delivery" // a dependency's newest released version is not delivered yet - whyRules = "rules" // the six rules judged it + whyNoPipeline = "no-passed-pipeline" // no version of the module released on the branch passed its pipeline: the want written stays + whyPassed = "passed-pipeline" // the want names the newest version released on the branch whose pipeline passed (design 47 H §750) ) +// whyStep is the step that decided a want: a passed pipeline's version, or none yet. +func whyStep(passed bool) string { + if passed { + return whyPassed + } + return whyNoPipeline +} + // wantWhy is one key's value. type wantWhy struct { At time.Time `json:"at"` Branch string `json:"branch,omitempty"` - // Fp is the fingerprint the trunk's entry names, the one the pass judged. - Fp string `json:"fp,omitempty"` - Step string `json:"step"` + // Fp, Version and Pipeline are the build the want names and the pipeline whose passed end released it (design 47 H §750). + Fp string `json:"fp,omitempty"` + Version string `json:"version,omitempty"` + Pipeline string `json:"pipeline,omitempty"` + Step string `json:"step"` // Before is the want that stood before the pass, After the one the pass wrote (none: it kept Before). Before *mwc.Want `json:"before,omitempty"` After *mwc.Want `json:"after,omitempty"` - // Rule is the rule holding the node back from Fp, with its words. - Rule string `json:"rule,omitempty"` - // What the rules read of the module. - Built, BuildFailed, Held, Granted, Released, Kept bool `json:",omitempty"` - BuildError string `json:"build-error,omitempty"` - Acting []string `json:"acting,omitempty"` - Words string `json:"words,omitempty"` } func whyKey(node, module string) string { return whyPrefix + node + "." + module } // sayWhy writes the why of module on each of nodes, once per change (the time aside): every put is a change its watchers hear. -func (p *passer) sayWhy(ctx context.Context, module string, nodes []string, w wantWhy, before map[string]mwc.Want, out *assign.Outcome, m *assign.Module) { +func (p *passer) sayWhy(ctx context.Context, module string, nodes []string, w wantWhy, before map[string]mwc.Want, out *assign.Outcome) { kv := p.kvs[writtenBucket()] if kv == nil { return @@ -71,17 +73,6 @@ func (p *passer) sayWhy(ctx context.Context, module string, nodes []string, w wa if a, ok := out.Wants[node]; ok { one.After = &a } - if h, ok := out.Rules[node]; ok { - one.Rule = h.Reason() - } - } - if m != nil { - one.Built, one.BuildFailed, one.BuildError, one.Held, one.Granted, one.Released, one.Kept = m.Built, m.BuildFailed, m.BuildError, m.Held, m.Granted, m.Released, m.Kept - if m.Acting != nil { - for _, e := range m.Acting(node) { - one.Acting = append(one.Acting, e.Step+" "+e.Node+"/"+e.Part+"@"+shortHex(e.Fp)+" (seat "+e.Seat+")") - } - } } same := one same.At = time.Time{} @@ -104,7 +95,7 @@ func (p *passer) sayWhy(ctx context.Context, module string, nodes []string, w wa // whyTools is the tool assigner_why: every want's why, or those of one module or one node. func whyTools(st store) []stdio.Tool { - return []stdio.Tool{{Name: whyTool, Description: "why a want is what it is: per node and module, the step of the assigner's last pass that decided the want (no passed pipeline, waits for a dependency's delivery, or the six rules with the rule holding it back), the fingerprint the trunk names, the want before and the one written, and what the rules read (built, held, granted, acting waits). A time that stands still says the pass no longer judges that module. Arguments: module and node narrow it.", + return []stdio.Tool{{Name: whyTool, Description: "why a want is what it is: per node and module, the step of the assigner's last pass that decided the want (no passed pipeline yet, or a passed pipeline's version), the build, version and pipeline it names, the want before and the one written. A time that stands still says the pass no longer judges that module. Arguments: module and node narrow it.", Input: schema(map[string]string{"module": "a module's name", "node": "a node's name"}, nil), Run: func(args map[string]any) (any, error) { ks, err := st.keys(writtenState) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/world.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/world.go index ff932523..9c0cf124 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/world.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/world.go @@ -12,7 +12,6 @@ import ( "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" @@ -43,12 +42,8 @@ type world struct { ends builtEnds // released are the catalog's version-released heard, by module and source revision (buildended.go). released releasedVersions - // delivered are the assigner's own version-delivered heard and said (delivered.go). - delivered deliveredVersions // edges are, by watched branch and module, the edges into the module's artifacts its catalog entry names (§374's kinds). edges map[string]map[string][]meshcatalog.Edge - // taken is the newest source revision of each watched branch the indexer's take-in.taken named, as heard (orderwaits.go, §298, A-260). - taken takenSources // assignments is the seat's state assignments assembled node by node from its keys in either form (mesh-sdk // mmac.Keys; research 072 row 37): the one reader of its keys in the assigner. assignmentKeys *mmac.Keys @@ -139,23 +134,6 @@ func (w *world) put(bucket, key string, value []byte) { } } -// lastHealthy is the newest fingerprint other than fp the node ran the module healthy at, or empty. -func (w *world) lastHealthy(node, module, fp string) string { - key, err := nodeengineclient.HaveKey(node, module) - if err != nil { - return "" - } - w.mu.Lock() - defer w.mu.Unlock() - list := w.healthy[key] - for i := len(list) - 1; i >= 0; i-- { - if list[i] != fp { - return list[i] - } - } - return "" -} - // read is a copy of what a bucket holds now. func (w *world) read(bucket string) map[string][]byte { w.mu.Lock() @@ -407,30 +385,14 @@ func (w *world) wantsOf() map[string]map[string]mwc.Want { // nodes is what the six rules read of the nodes: presence, haves, and every provision's providers with their nodes. func (w *world) nodes(manifests map[string]catalogue.Manifest, assigned map[string][]string) assign.Nodes { n := assign.Nodes{Online: map[string]bool{}, Running: w.runningNow(), Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{}, Providers: map[string]map[string][]string{}, - Reads: map[string][]string{}, Capable: map[string][]string{}} + Reads: map[string][]string{}} n.Online = w.online() - // The build a running statement names serves an acting edge's seat when its manifest claims it (design 47 G A-426). - n.Serves = func(r nodeengine.Running, seat string) (bool, bool) { - if r.Manifest == "" { - return false, false - } - got := w.manifest(r.Manifest) - if got.err != "" { - return false, false - } - return got.manifest.ClaimsSeat(seat), true - } for key, value := range w.read(buckets.Have) { node, module, isNode, err := nodeengineclient.ParseHaveKey(key) if err != nil { continue } - if isNode { - // What the node's engine reads (ADR 0614 §4); a record that states none is read as the baseline. - if h, err := nodeengineclient.DecodeHaveNode(value); err == nil && h.Record != nil { - n.Reads[node] = h.Record.ManifestReads - n.Capable[node] = h.Record.Capabilities // where a node-engine of G states want-parts (§525) - } + if isNode { // what the node's engine reads is the state engine-reads' alone (design 47 H-A-104) continue } if h, err := nodeengineclient.DecodeHave(value); err == nil { @@ -476,37 +438,6 @@ func (w *world) has(bucket, key string) bool { return ok } -// built says whether the builder's build of a module at a fingerprint is done, from its `build.ended` heard (design 47 G A-181). -func (w *world) built(module, fp string) bool { - e, ok := w.ends.endOf(module, fp) - return ok && e.Outcome == builds.BuildDone -} - -// buildStanding says whether the builder's `build.ended` of module at fp said the build failed, and its error whole (design 47 G -// §575, A-162): the words of a hold under rule 1. -func (w *world) buildStanding(module, fp string) (failed bool, words string) { - if e, ok := w.ends.endOf(module, fp); ok && e.Outcome == builds.BuildFailed { - return true, e.Error - } - return false, "" -} - -// buildNeeds are the reference forms the builder's done build of module at fp is written in (the build's end says them, ADR -// 0618 §1, A-181): nil when it is not done or names none. -func (w *world) buildNeeds(module, fp string) []string { - if e, ok := w.ends.endOf(module, fp); ok && e.Outcome == builds.BuildDone { - return e.Needs - } - return nil -} - -// recordReads is what node's record `.node` states its engine reads (ADR 0614 §4), and whether the assigner holds -// a record of it: the fallback where the engine does not answer the verb manifest-reads (enginereads.FromRecord). -func (w *world) recordReads(node string) ([]string, bool) { - r, ok := w.records()[node] - return r.ManifestReads, ok -} - // knownNodes is every node the assigner hears of: placed, with a record, or named by machine-away or machine-back. func (w *world) knownNodes() []string { set := map[string]bool{} diff --git a/modules/mesh-delivery-assigner/go.mod b/modules/mesh-delivery-assigner/go.mod index 1eea0779..f28e78db 100644 --- a/modules/mesh-delivery-assigner/go.mod +++ b/modules/mesh-delivery-assigner/go.mod @@ -89,16 +89,13 @@ replace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view/viewstate => require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient v0.0.0-00010101000000-000000000000 -require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient v0.0.0-00010101000000-000000000000 -replace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient => ../mesh-delivery-contracts/go/clients/meshdeliverycheckerclient require git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go v0.0.0-00010101000000-000000000000 require ( git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/theprivatenetwork v0.0.0-00010101000000-000000000000 // indirect ) diff --git a/modules/mesh-delivery-assigner/internal/acting/acting.go b/modules/mesh-delivery-assigner/internal/acting/acting.go deleted file mode 100644 index abc217c1..00000000 --- a/modules/mesh-delivery-assigner/internal/acting/acting.go +++ /dev/null @@ -1,140 +0,0 @@ -// Package acting is the assigner's wait for an acting part's new version (design 47 G §266, §268, §276): an entry of the assignment -// graph whose edge of kind acting names a step the assigner holds (composing, placing) waits until the acting part's version runs -// on the node of the edge, with the bound of the rule that gave the edge, or the step's default. The package holds the rules and -// the kept record; the assigner's process reads what runs, says the events and keeps the records in its state `waits`. -package acting - -import ( - "encoding/json" - "fmt" - "strings" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-sdk/go/seatdef" -) - -// HowNow is the `how` of assigner_release for a wait for an acting part: the entry is wanted now, with the version running. -const HowNow = "now" - -// The ways a wait ends, as `wait-ended` says them. -const ( - HowRan = "ran" - HowReleased = "released" - HowReplaced = "replaced" -) - -// Prefix opens the key of an acting wait in the state, apart from the outside waits' `..`. -const Prefix = "acting." - -// OrderPrefix opens the key of an order wait (design 47 G §298, A-259): a want held for the order of a delivery that crosses a form change, -// kept beside the acting waits in the state `waits`, as `order...`. -const OrderPrefix = "order." - -// The three order edges of §298, as a kept wait's Order names them. -const ( - // OrderBackup: the new versions of the node-backup holder and of the holder of the bus module's seat run before a module whose kept - // data's form changes is moved. - OrderBackup = "backup-and-broker-first" - // OrderEngineLast: at a revert a node's node-engine goes back last on that node. - OrderEngineLast = "node-engine-last" - // OrderTakenIn: the reverted assigner and gate are wanted only once the reverted indexer took in the trunk's head. - OrderTakenIn = "taken-in-first" -) - -// Held says whether the assigner holds the entries of step: composing and placing (§276's table). -func Held(step string) bool { return step == seatdef.StepCompose || step == seatdef.StepPlace } - -// Wait is one wait as kept: Module's entry on Node waits for Part's version Fp to run on PartNode, in Step. -type Wait struct { - V int `json:"v"` - Step string `json:"step"` - Node string `json:"node"` - Module string `json:"module"` - Part string `json:"part"` - PartNode string `json:"part-node"` - Fp string `json:"fp"` - Entry string `json:"entry"` - Since time.Time `json:"since"` - ClockFrom time.Time `json:"clock-from"` - Bound string `json:"bound"` - // Released is a release by hand: the entry moves on with the part's old version; EndSaid says wait-ended was said for it. - // Order is which of §298's edges the wait stands on; empty for an acting wait. - Order string `json:"order,omitempty"` - Released bool `json:"released,omitempty"` - EndSaid bool `json:"end-said,omitempty"` - By string `json:"by,omitempty"` - Why string `json:"why,omitempty"` -} - -// Key is the key of a wait in the state. -func Key(node, module, part string) string { return Prefix + node + "." + module + "." + part } - -// OrderKey is the key of an order wait in the state. -func OrderKey(node, module, part string) string { - return OrderPrefix + node + "." + module + "." + part -} - -// IsOrder says a kept value is an order wait's record. -func IsOrder(key string) bool { return strings.HasPrefix(key, OrderPrefix) } - -// Is says a kept value is an acting wait's record. -func Is(key string) bool { return strings.HasPrefix(key, Prefix) } - -// Encode is w as JSON. -func Encode(w Wait) ([]byte, error) { w.V = 1; return json.Marshal(w) } - -// Decode reads a kept wait. -func Decode(raw []byte) (Wait, error) { - var w Wait - if err := json.Unmarshal(raw, &w); err != nil { - return w, err - } - if w.V != 1 || w.Step == "" || w.Node == "" || w.Module == "" || w.Part == "" { - return w, fmt.Errorf("acting: %q is not a kept acting wait", raw) - } - return w, nil -} - -// BoundOf is the bound of the edge's wait: its rule's, else its step's default (seatdef.ActingRule.Wait). -func BoundOf(e meshwants.Edge) time.Duration { - return seatdef.ActingRule{Step: e.Step, Bound: e.Bound}.Wait() -} - -// Pending are the acting edges of the assigner's steps whose part's version does not run yet: runs says the edge's part runs -// its version on the edge's node. -func Pending(edges []meshwants.Edge, runs func(e meshwants.Edge) bool) []meshwants.Edge { - var out []meshwants.Edge - for _, e := range edges { - if e.Kind == "acting" && Held(e.Step) && !runs(e) { - out = append(out, e) - } - } - return out -} - -// Body is the wait as the seat mesh-wants says it. -func Body(w Wait, owner string) meshwants.Wait { - since, from := w.Since.UTC(), w.ClockFrom.UTC() - return meshwants.Wait{Step: w.Step, Owner: owner, Node: w.Node, Module: w.Module, Part: w.Part, Fp: w.Fp, Since: since, ClockFrom: &from, Bound: w.Bound, - Words: words(w)} -} - -func words(w Wait) string { - switch w.Order { - case OrderBackup: - return fmt.Sprintf("%s on %s moves its kept data across a form change: it waits in %s for %s at %s to run on %s, which the restore point needs", w.Module, w.Node, w.Step, w.Part, short(w.Fp), w.PartNode) - case OrderEngineLast: - return fmt.Sprintf("%s on %s goes back last on its node: it waits in %s for %s at %s to run on %s, restored", w.Module, w.Node, w.Step, w.Part, short(w.Fp), w.PartNode) - case OrderTakenIn: - return fmt.Sprintf("%s on %s goes back only once the indexer took in the trunk's head: it waits in %s for source revision %s to be taken in", w.Module, w.Node, w.Step, w.Fp) - } - return fmt.Sprintf("%s on %s waits in %s for %s at %s to run on %s", w.Module, w.Node, w.Step, w.Part, short(w.Fp), w.PartNode) -} - -func short(fp string) string { - if len(fp) > 19 { - return fp[:19] - } - return fp -} diff --git a/modules/mesh-delivery-assigner/internal/assign/asks.go b/modules/mesh-delivery-assigner/internal/assign/asks.go index f88290fe..77ac64f9 100644 --- a/modules/mesh-delivery-assigner/internal/assign/asks.go +++ b/modules/mesh-delivery-assigner/internal/assign/asks.go @@ -3,7 +3,6 @@ package assign import ( "fmt" "mesh-delivery-assigner/internal/assignment" - "regexp" "strings" "time" @@ -16,56 +15,12 @@ import ( oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" ) -// HoldAsk is the ask about a module's fingerprint the assigner holds, at its key (asks.HoldAskKey): keep it held or -// release it. Releasing writes the held fingerprint's want again by the rules (ADR 0539 §19). node and words are the held -// record's: the machine whose health check failed and the hold's words (issue 1906). asking numbers the writing: 0 the first, a -// larger number an ask written again after a hold lifted by itself, so the router takes it as new. -func HoldAsk(key, module, fp, node, words string, asking int, now time.Time) asks.Ask { - return assignerAsk(asking, oc.Ask{ - ID: meshdeliveryclient.AskingID(key, asking), - Headline: fmt.Sprintf("%s is held at %s", module, short(fp)), - Explanation: fmt.Sprintf("The health check of %s failed on %s at %s (%s), so the assigner holds that version and wants "+ - "no node to move to it. It lifts the hold by itself once every node that runs %s runs it healthy; releasing it "+ - "lets the wants move to it now.", module, node, short(fp), words, module), - Options: []oc.Option{ - {ID: "keep", Label: "Keep it held", Does: "the version stays held", Level: occ.Acknowledge}, - {ID: "release", Label: "Release it", Does: "the version is released and the wants move to it by the rules again", Level: occ.Acknowledge}, - }, - Who: occ.Operator, - Expires: occ.FormatTime(now.Add(AskLasts)), - OnExpiry: "the version stays held", - About: key, - }) -} - // The options of the restore ask (ADR 0537 §3, ADR 0539 §19), and what each does on the node. const ( OptionRestore = "restore" OptionLeaveStopped = "leave-stopped" ) -// RestoreAsk is the ask about a module holding data whose fingerprint failed its health check on a node (ADR 0429 §6): it is -// not set back by itself, since a set-back would lose the writes made since. words is the check's words. asking is the writing's -// number (AskingSeconds): an ask taken away as stale and written again is another asking under an id of its own, since the -// router drops a second ask under an id it keeps and the teller took the first back (issue 1966). Restore sets the -// node back to the last healthy fingerprint and loses the newer writes; leaving it stopped keeps the data as it is. -func RestoreAsk(key, node, module, fp, words string, asking int, now time.Time) asks.Ask { - return assignerAsk(asking, oc.Ask{ - ID: meshdeliveryclient.AskingID(key, asking), - Headline: fmt.Sprintf("%s failed its health check on %s", module, node), - Explanation: fmt.Sprintf("%s holds data, and its health check failed on %s at %s: %s. Setting it back to the version that ran healthy "+ - "would lose the writes made since; leaving it stopped keeps the data as it is.", module, node, short(fp), words), - Options: []oc.Option{ - {ID: OptionRestore, Label: "Restore it", Does: "the node is set back to the last healthy version and the newer writes are lost", Level: occ.Acknowledge}, - {ID: OptionLeaveStopped, Label: "Leave it stopped", Does: "the module stays as it is on the node and the data is kept", Level: occ.Acknowledge}, - }, - Who: occ.Operator, - Expires: occ.FormatTime(now.Add(AskLasts)), - OnExpiry: "the module stays as it is on the node", - About: key, - }) -} - // The options of the outside ask (design 47 G §706). const ( OptionGoOn = "go-on" @@ -106,92 +61,6 @@ func WaitsOutsideFinding(node, module, causeKey, on string, at time.Time) Observ Needs: "Nothing for you to do.", Source: "assigner.delivery-waits-outside"} } -// HeldCondition is the condition the assigner raises when it holds a fingerprint of a module (ADR 0429 §6, ADR 0539 §19), or -// clears (raised false) when the hold is released. It names the module, the node whose health check failed and the check's -// words, under the assigner's own name (ADR 0437 clause 9), keyed by the held fingerprint. needsYou is true for a module holding -// data, whose restore ask waits for the operator. -func HeldCondition(module, fp, node, words string, raised, needsYou bool) oc.Condition { - key := assignment.Module() + ".held." + module + "." + strings.TrimPrefix(fp, meshcatalogclient.SourcePrefix2) - subject := oc.ConditionSubject{Scope: "module", ID: module, Machine: node} - if !raised { - return oc.Condition{Key: key, Subject: subject, State: occ.StateCleared} - } - c := oc.Condition{Key: key, Severity: occ.SeverityWarning, Subject: subject, State: occ.StateRaised, - Summary: fmt.Sprintf("%s is held at %s", module, short(fp)), - Headline: fmt.Sprintf("%s is held at %s", module, short(fp))} - what := fmt.Sprintf("The health check of %s failed on %s at %s: %s. The assigner holds that version and wants no node to move to it.", module, node, short(fp), words) - if needsYou { - c.Needs = "answer whether to restore " + module + " on " + node - c.Explanation = occ.NeedsYou + c.Needs + ". " + what - } else { - c.Explanation = occ.NothingToDo + " " + what + " Nodes that wanted it are set back to the version that ran healthy." - } - return c -} - -// NodeFailedCondition is the condition the assigner raises for a module whose newest fingerprint fp failed its health check on -// node while other nodes run it healthy (ADR 0557 §1, issue 1496): the failure is the node's, so nothing is held or set back and -// the node keeps its want. It names the node, the module, fp and the have's words, keyed by node and module, and is cleared -// (raised false) once the node no longer fails alone. -func NodeFailedCondition(node, module, fp string, f NodeFailure, raised bool) oc.Condition { - key := assignment.Module() + ".failed-alone." + node + "." + module - subject := oc.ConditionSubject{Scope: "module", ID: module, Machine: node} - if !raised { - return oc.Condition{Key: key, Subject: subject, State: occ.StateCleared, - Resolved: fmt.Sprintf("%s on %s no longer fails its health check alone.", module, node)} - } - headline := fmt.Sprintf("%s fails on %s alone", module, node) - if n := len([]rune(headline)); n > occ.HeadlineLength { - headline = string([]rune(headline)[:occ.HeadlineLength]) - } - return oc.Condition{Key: key, Severity: occ.SeverityWarning, Subject: subject, State: occ.StateRaised, - Summary: headline, Headline: headline, - Explanation: fmt.Sprintf("%s The health check of %s at %s failed on %s (%s), while it runs healthy on %s. The failure is %s's, "+ - "so the assigner holds nothing and sets no node back, and %s keeps its want. This clears once %s runs it healthy or "+ - "another version is the newest.", occ.NothingToDo, module, short(fp), node, f.Words, strings.Join(f.Others, ", "), node, - node, node)} -} - -// NeitherCondition is the condition of a module assigned to node that holds no want for it (issue 1945): the node's own composition -// gives up every module with a fingerprint of the new form, so nothing applies it there and its files are gone. It is raised (urgent) -// naming the module, the node and the rule holding its first want back with the rule's words, under the assigner's own name keyed by -// node and module, and cleared (raised false) when the node has a want for it again. -func NeitherCondition(node, module string, h Holding, raised bool) oc.Condition { - key := assignment.Module() + ".neither." + node + "." + module - subject := oc.ConditionSubject{Scope: "module", ID: module, Machine: node} - if !raised { - return oc.Condition{Key: key, Subject: subject, State: occ.StateCleared, - Resolved: fmt.Sprintf("%s on %s has a want again, so its own composition is applied there.", module, node)} - } - headline := fmt.Sprintf("%s on %s is on no list", module, node) - if n := len([]rune(headline)); n > occ.HeadlineLength { - headline = string([]rune(headline)[:occ.HeadlineLength]) - } - return oc.Condition{Key: key, Severity: occ.SeverityUrgent, Subject: subject, State: occ.StateRaised, - Summary: headline, Headline: headline, - Explanation: fmt.Sprintf("%s %s is assigned to %s, but the node holds no want for it and the node's own composition does not "+ - "hold it either, so nothing applies it there. The assigner's rule %d holds its first want back: %s. The assigner writes the want once the "+ - "rule holds, and clears this.", occ.NothingToDo, module, node, h.Rule, h.Words)} -} - -// NeitherLeftCondition clears the neither condition of module on node when it is no longer on neither list: why is "" when the -// node has a live want for it again, else why it has none (it is held, its move waits for an ask, the node no longer runs it), so -// the clear never says a want the node does not hold (issue 1945: "nats on novox has a want again" while it had none). -func NeitherLeftCondition(node, module, why string) oc.Condition { - c := NeitherCondition(node, module, Holding{}, false) - if why != "" { - c.Resolved = fmt.Sprintf("%s on %s is no longer on neither list and has no want: %s.", module, node, why) - } - return c -} - -// LiftedCondition clears the condition of a held fingerprint the assigner lifted by itself, saying why (issue 1906). -func LiftedCondition(module, fp string) oc.Condition { - c := HeldCondition(module, fp, "", "", false, false) - c.Resolved = LiftedWords(module, fp) + "." - return c -} - // The options of the record ask (ADR 0539 §14) and of the backup ask (§16). const ( OptionYes = "yes" @@ -288,27 +157,6 @@ func NextBackupAsking(after int) int { return max(after+1, 2) } -var backupDigest = regexp.MustCompile(`\(composition (sha256:[0-9a-f]{12})\)`) - -// BackupAskDigest is the digest of the composition a backup ask was made about, from its explanation, cut to its first 12 digits as -// the ask says it (ShortDigest); empty for an ask that names -// none. An answer is taken for the composition of the ask it answers, and a new revision at the same fingerprint is asked again -// under a new number (ADR 0539 §16). -func BackupAskDigest(a asks.Ask) string { - ask, err := AskOf(a) - if err != nil { - return "" - } - if m := backupDigest.FindStringSubmatch(ask.Explanation); m != nil { - return m[1] - } - return "" -} - -// ShortDigest is a composition's digest as a backup ask names it, its first 12 digits (said): the ask stays bound to its -// composition, and a new composition is asked again. -func ShortDigest(digest string) string { return said(digest) } - // BackupAnswerWords are the words of a want carrying the answer to the asking-th ask: each asking's answer is another want // value, so a backup that fails again after "back up again" and is answered "again" again changes the key and the node retries. func BackupAnswerWords(option string, asking int) string { @@ -316,14 +164,14 @@ func BackupAnswerWords(option string, asking int) string { } // BackupAsk is the ask about a backup of a module on node that failed before an apply (ADR 0539 §16): back up again, or go on -// without a backup. digest is the composition about to be applied and asking the number of the asking, 0 the first; words are the +// without a backup, at the build fp the want names. asking is the number of the asking, 0 the first; words are the // backup's own words, from the node's have. -func BackupAsk(key, node, module, fp, digest, words string, asking int, now time.Time) asks.Ask { +func BackupAsk(key, node, module, fp, words string, asking int, now time.Time) asks.Ask { return assignerAsk(asking, oc.Ask{ ID: meshdeliveryclient.AskingID(key, asking), Headline: fmt.Sprintf("The backup of %s failed on %s", module, node), - Explanation: fmt.Sprintf("The backup before applying %s at %s on %s failed: %s. %s keeps running what it has until you answer. (composition %s)", - module, short(fp), node, words, module, digest), + Explanation: fmt.Sprintf("The backup before applying %s at %s on %s failed: %s. %s keeps running what it has until you answer.", + module, short(fp), node, words, module), Options: []oc.Option{ {ID: OptionBackupAgain, Label: "Back up again", Does: "the node tries the backup again before it applies", Level: occ.Acknowledge}, {ID: OptionBackupSkip, Label: "Go on without a backup", Does: "the node applies the new version without a backup", Level: occ.Acknowledge}, diff --git a/modules/mesh-delivery-assigner/internal/assign/asks_test.go b/modules/mesh-delivery-assigner/internal/assign/asks_test.go index d6ff0f23..78a9d0f6 100644 --- a/modules/mesh-delivery-assigner/internal/assign/asks_test.go +++ b/modules/mesh-delivery-assigner/internal/assign/asks_test.go @@ -23,11 +23,10 @@ import ( "git.novox.be/novox/mesh-sdk/go/views" ) -// 1297/B9 (the assigner's half): the ask about a held fingerprint is a valid ask of the router and of mesh-sdk's layout at -// its key, of a kind the view may answer. -func TestTheHoldAskIsValid(t *testing.T) { - t.Run("hold", theHoldAskIsValid) - t.Run("restore", theRestoreAskIsValid) +// 1297/B9 (the assigner's half): the backup ask is a valid ask of the router and of mesh-sdk's layout at its key, of a kind the +// view may answer; the hold and restore asks went with the hold of a fingerprint (design 47 H §750, H-A-55). +func TestTheAssignersAsks(t *testing.T) { + t.Run("backup", theBackupAskIsValid) t.Run("ended at the channel", anAskEndedAtTheChannelIsWrittenAgain) t.Run("ended at the channel, read at bind", anAskEndedAtTheChannelIsReadAtBind) } @@ -40,8 +39,6 @@ func anAskEndedAtTheChannelIsWrittenAgain(t *testing.T) { asked := time.Date(2026, 11, 9, 22, 0, 0, 0, time.UTC) now := asked.Add(8 * 24 * time.Hour) digest := "sha256:" + strings.Repeat("e", 64) - holdKey, _ := asks.HoldAskKey("nats", fNew) - restoreKey, _ := asks.RestoreAskKey("novox", "nats", fNew) backupKey, _ := asks.BackupAskKey("novox", "nats", fNew) revertKey, _ := asks.RevertAskKey("novox", "nats", fNew) recordKey, _ := asks.RecordAskKey("novox", "nats", digest) @@ -63,9 +60,7 @@ func anAskEndedAtTheChannelIsWrittenAgain(t *testing.T) { put *puts.Put why string // "" when it is written again }{ - {name: "hold", key: holdKey, a: ptr(HoldAsk(holdKey, "nats", fNew, "novox", "w", AskingSeconds(asked), asked)), outcome: occ.OutcomeExpired}, - {name: "restore", key: restoreKey, a: ptr(RestoreAsk(restoreKey, "novox", "nats", fNew, "w", AskingSeconds(asked), asked)), outcome: occ.OutcomeCancelled}, - {name: "backup", key: backupKey, a: ptr(BackupAsk(backupKey, "novox", "nats", fNew, digest, "w", 0, asked)), outcome: occ.OutcomeCancelled}, + {name: "backup", key: backupKey, a: ptr(BackupAsk(backupKey, "novox", "nats", fNew, "w", 0, asked)), outcome: occ.OutcomeCancelled}, {name: "revert", key: revertKey, a: ptr(assignerAsk(AskingSeconds(asked), oc.Ask{ID: meshdeliveryclient.AskingID(revertKey, AskingSeconds(asked)), Headline: "h", Explanation: "e", Who: occ.Operator, Expires: occ.FormatTime(asked.Add(AskLasts)), OnExpiry: "o", Options: []oc.Option{{ID: "hold", Label: "Hold", Does: "d", Level: occ.Acknowledge}}})), outcome: occ.OutcomeExpired}, @@ -73,11 +68,11 @@ func anAskEndedAtTheChannelIsWrittenAgain(t *testing.T) { {name: "record of another put", key: recordKey, a: &record, outcome: occ.OutcomeExpired, put: &other}, {name: "record a put asked", key: recordKey, a: &byPut, outcome: occ.OutcomeExpired, put: &put, why: "put"}, {name: "assignments", key: assignmentsKey, a: &assignments, outcome: occ.OutcomeExpired, why: "ADR 0567"}, - {name: "another asking stands", key: holdKey, a: ptr(HoldAsk(holdKey, "nats", fNew, "novox", "w", AskingSeconds(now), now)), - ended: meshdeliveryclient.AskingID(holdKey, AskingSeconds(asked)), outcome: occ.OutcomeExpired, why: "holds the asking"}, - {name: "taken away", key: holdKey, ended: "hold-x", outcome: occ.OutcomeExpired, why: "taken away"}, - {name: "refused", key: holdKey, a: ptr(HoldAsk(holdKey, "nats", fNew, "novox", "w", 0, asked)), outcome: occ.OutcomeRefused, why: "refused"}, - {name: "replaced", key: holdKey, a: ptr(HoldAsk(holdKey, "nats", fNew, "novox", "w", 0, asked)), outcome: occ.OutcomeReplaced, why: "replaced"}, + {name: "another asking stands", key: backupKey, a: ptr(BackupAsk(backupKey, "novox", "nats", fNew, "w", AskingSeconds(now), now)), + ended: meshdeliveryclient.AskingID(backupKey, AskingSeconds(asked)), outcome: occ.OutcomeExpired, why: "holds the asking"}, + {name: "taken away", key: backupKey, ended: "backup-x", outcome: occ.OutcomeExpired, why: "taken away"}, + {name: "refused", key: backupKey, a: ptr(BackupAsk(backupKey, "novox", "nats", fNew, "w", 0, asked)), outcome: occ.OutcomeRefused, why: "refused"}, + {name: "replaced", key: backupKey, a: ptr(BackupAsk(backupKey, "novox", "nats", fNew, "w", 0, asked)), outcome: occ.OutcomeReplaced, why: "replaced"}, } { t.Run(c.name, func(t *testing.T) { var current []byte @@ -134,12 +129,12 @@ func anAskEndedAtTheChannelIsWrittenAgain(t *testing.T) { // answered or another asker's ask, and one the key no longer holds, are not. func anAskEndedAtTheChannelIsReadAtBind(t *testing.T) { asked := time.Date(2026, 11, 9, 22, 0, 0, 0, time.UTC) - holdKey, _ := asks.HoldAskKey("nats", fNew) - restoreKey, _ := asks.RestoreAskKey("novox", "nats", fNew) backupKey, _ := asks.BackupAskKey("novox", "nats", fNew) - hold := HoldAsk(holdKey, "nats", fNew, "novox", "w", AskingSeconds(asked), asked) - restore := RestoreAsk(restoreKey, "novox", "nats", fNew, "w", AskingSeconds(asked.Add(time.Hour)), asked.Add(time.Hour)) - backup := BackupAsk(backupKey, "novox", "nats", fNew, "sha256:"+strings.Repeat("e", 64), "w", 0, asked.Add(-time.Hour)) // no asking + holdKey, _ := asks.BackupAskKey("g14", "nats", fNew) + restoreKey, _ := asks.BackupAskKey("ace", "nats", fNew) + hold := BackupAsk(holdKey, "g14", "nats", fNew, "w", AskingSeconds(asked), asked) + restore := BackupAsk(restoreKey, "ace", "nats", fNew, "w", AskingSeconds(asked.Add(time.Hour)), asked.Add(time.Hour)) + backup := BackupAsk(backupKey, "novox", "nats", fNew, "w", 0, asked.Add(-time.Hour)) // no asking held := map[string][]byte{} for key, a := range map[string]asks.Ask{holdKey: hold, restoreKey: restore, backupKey: backup} { v, err := EncodeAsk(key, a) @@ -174,27 +169,27 @@ func anAskEndedAtTheChannelIsReadAtBind(t *testing.T) { func ptr(a asks.Ask) *asks.Ask { return &a } -func theHoldAskIsValid(t *testing.T) { - key, err := asks.HoldAskKey("mail", fNew) +func theBackupAskIsValid(t *testing.T) { + key, err := asks.BackupAskKey("ace", "mail", fNew) if err != nil { t.Fatal(err) } now := time.Now() - a := HoldAsk(key, "mail", fNew, "ace", "health failed, phase running: connection refused", 0, now) + a := BackupAsk(key, "ace", "mail", fNew, "health failed, phase running: connection refused", 0, now) if err := occ.Check(mustAsk(t, a), now); err != nil { t.Errorf("the router refuses the ask: %v", err) } if _, err := EncodeAsk(key, a); err != nil { t.Errorf("the ask cannot be written: %v", err) } - // 1906/H3: the ask names the machine whose check failed and the hold's words. + // The ask names the machine whose backup failed and the backup's words. if !strings.Contains(mustAsk(t, a).Explanation, "on ace") || !strings.Contains(mustAsk(t, a).Explanation, "connection refused") { t.Errorf("the ask does not name the node and the hold's words: %q", mustAsk(t, a).Explanation) } - // 1906/H2: an ask written again after a lift is another asking, under an id of its own. - again := HoldAsk(key, "mail", fNew, "ace", "x", 7, now) + // An ask written again is another asking, under an id of its own. + again := BackupAsk(key, "ace", "mail", fNew, "x", 7, now) if again.Asking != 7 || mustAsk(t, again).ID != meshdeliveryclient.AskingID(key, 7) || mustAsk(t, again).ID == mustAsk(t, a).ID { - t.Errorf("the asking after a lift = %d %s", again.Asking, mustAsk(t, again).ID) + t.Errorf("the asking again = %d %s", again.Asking, mustAsk(t, again).ID) } if _, err := EncodeAsk(key, again); err != nil { t.Errorf("the ask asked again cannot be written: %v", err) @@ -204,7 +199,7 @@ func theHoldAskIsValid(t *testing.T) { } // The join (A1d ruling 1): mesh-sdk no longer refuses another id, no option or no ask in an assigner's ask; EncodeAsk does. t.Run("EncodeAsk refuses what mesh-sdk no longer does", func(t *testing.T) { - good := HoldAsk(key, "mail", fNew, "ace", "x", 0, time.Unix(100, 0)) + good := BackupAsk(key, "ace", "mail", fNew, "x", 0, time.Unix(100, 0)) other := good other.Asking = 3 // its id is the asking 0's empty := good @@ -219,68 +214,11 @@ func theHoldAskIsValid(t *testing.T) { }) } -// 1906/D3 (ADR 0539 §19): the ask about a data module held on a node is a valid ask of the router and of mesh-sdk's layout at -// its restore key, of a kind the view may answer; it offers restoring (losing the newer writes) and leaving it stopped. -func theRestoreAskIsValid(t *testing.T) { - key, err := asks.RestoreAskKey("ace", "mail", fNew) - if err != nil { - t.Fatal(err) - } - now := time.Now() - a := RestoreAsk(key, "ace", "mail", fNew, "connection refused", 2, now) - if err := occ.Check(mustAsk(t, a), now); err != nil { - t.Errorf("the router refuses the ask: %v", err) - } - if _, err := EncodeAsk(key, a); err != nil { - t.Errorf("the ask cannot be written: %v", err) - } - if _, err := viewstate.ViewAnswerKey(key); err != nil { - t.Errorf("the view may not answer it: %v", err) - } - var ids []string - for _, o := range mustAsk(t, a).Options { - ids = append(ids, o.ID) - } - if len(ids) != 2 || ids[0] != OptionRestore || ids[1] != OptionLeaveStopped { - t.Errorf("options = %v", ids) - } - if !strings.Contains(mustAsk(t, a).Explanation, "connection refused") || !strings.Contains(mustAsk(t, a).Explanation, "ace") { - t.Errorf("the ask does not name the node and the check's words: %q", mustAsk(t, a).Explanation) - } -} - -// 1906/D3: the condition for a held fingerprint names the module, the node and the check's words, under the assigner's own -// name (ADR 0437 clause 9), and is cleared under the same key. +// 1952/WM6: the assigner's conditions are findings. func TestTheAssignersConditions(t *testing.T) { - t.Run("the held condition names module, node and words", heldConditionNamesModuleNodeAndWords) - t.Run("a lifted hold clears its condition saying why", liftedHoldClearsItsConditionSayingWhy) t.Run("1952/WM6: each said condition is a finding, its clear deletes it, said straight only with no findings bucket", saidConditionsAreFindings) } -func heldConditionNamesModuleNodeAndWords(t *testing.T) { - c := HeldCondition("mail", fNew, "ace", "connection refused", true, true) - if _, err := occ.EncodeCondition(c); err != nil { - t.Fatalf("the condition is refused: %v", err) - } - if c.State != occ.StateRaised || c.Subject.ID != "mail" || c.Subject.Machine != "ace" || !strings.Contains(c.Explanation, "connection refused") || !strings.Contains(c.Key, "mail") { - t.Errorf("condition = %+v", c) - } - if got := HeldCondition("mail", fNew, "", "", false, false); got.State != occ.StateCleared || got.Key != c.Key { - t.Errorf("cleared = %+v", got) - } -} - -// 1906/H2: a hold lifted by itself clears the held condition under its key, saying why. -func liftedHoldClearsItsConditionSayingWhy(t *testing.T) { - c := LiftedCondition("mail", fNew) - if _, err := occ.EncodeCondition(c); err != nil { - t.Fatalf("the condition is refused: %v", err) - } - if c.State != occ.StateCleared || c.Key != HeldCondition("mail", fNew, "ace", "x", true, false).Key || !strings.Contains(c.Resolved, "lifted its hold by itself") { - t.Errorf("condition = %+v", c) - } -} - // 1952/WM6 (ADR 0564 §2, issue 1952 note 16): every condition the assigner says is a finding in its own bucket, under // `..`, written at the raise and deleted at the clear, and only as a finding: the keeper is the one // source since the controller's end (novox/hq ADR 0590 §3, issue 1126 B8). Said straight to the router only where the @@ -291,9 +229,6 @@ func saidConditionsAreFindings(t *testing.T) { raised, cleared oc.Condition key string }{ - {HeldCondition("mail", fNew, "ace", "refused", true, true), LiftedCondition("mail", fNew), "module.mail." + strings.TrimPrefix(fNew, meshcatalogclient.SourcePrefix2) + ".held"}, - {NodeFailedCondition("ace", "mail", fNew, NodeFailure{Words: "x", Others: []string{"g14"}}, true), NodeFailedCondition("ace", "mail", fNew, NodeFailure{}, false), "module.ace.mail.failed-alone"}, - {NeitherCondition("ace", "mail", Holding{Rule: 3, Words: "w"}, true), NeitherCondition("ace", "mail", Holding{}, false), "module.ace.mail.neither"}, {SeatWaitsCondition([]string{"assignments"}), SeatWaitsCondition(nil), "module.mesh-delivery-assigner.seat-states"}, {BackupCondition("ace", "mail", fNew, "disk full", true), BackupCondition("ace", "mail", fNew, "", false), "module.ace.mail." + strings.TrimPrefix(fNew, meshcatalogclient.SourcePrefix2) + ".backup"}, } { @@ -336,8 +271,7 @@ func TestTheRecordAndBackupAsksAreValid(t *testing.T) { if err != nil { t.Fatal(err) } - dgst := "sha256:" + strings.Repeat("b", 64) - b := BackupAsk(bkey, "ace", "mail", fNew, dgst, "no space left", 0, now) + b := BackupAsk(bkey, "ace", "mail", fNew, "no space left", 0, now) vkey, err := asks.RevertAskKey("ace", "mail", fNew) if err != nil { t.Fatal(err) @@ -360,18 +294,15 @@ func TestTheRecordAndBackupAsksAreValid(t *testing.T) { if !strings.Contains(mustAsk(t, b).Explanation, "no space left") || mustAsk(t, b).Options[0].ID != mwc.BackupAgain || mustAsk(t, b).Options[1].ID != mwc.BackupSkip { t.Errorf("the backup ask = %+v", mustAsk(t, b)) } - // the id binds the ask to the composition and to its number (review round 1, items 3 and 4) - if again := BackupAsk(bkey, "ace", "mail", fNew, dgst, "x", NextBackupAsking(0), now); mustAsk(t, again).ID == mustAsk(t, b).ID { + // the id binds the ask to its number (review round 1, item 4); its key binds it to the build (design 47 H §750: a want names no composition) + if again := BackupAsk(bkey, "ace", "mail", fNew, "x", NextBackupAsking(0), now); mustAsk(t, again).ID == mustAsk(t, b).ID { t.Errorf("asked again under the same id") } if NextBackupAsking(-1) != 0 || NextBackupAsking(0) != 2 || NextBackupAsking(2) != 3 { t.Errorf("the numbers of the askings: %d %d %d", NextBackupAsking(-1), NextBackupAsking(0), NextBackupAsking(2)) } - if BackupAskDigest(b) != ShortDigest(dgst) || ShortDigest(dgst) == dgst { - t.Errorf("the ask names composition %q, want %q", BackupAskDigest(b), ShortDigest(dgst)) - } // novox 2026-11-16: the router refuses a long hexadecimal string, so no word of an ask carries a digest or fingerprint whole. - held := BackupAsk(bkey, "novox", "mail", fNew, dgst, "the kept data between "+fNew+" and "+fNew+" is not known", 0, now) + held := BackupAsk(bkey, "novox", "mail", fNew, "the kept data between "+fNew+" and "+fNew+" is not known", 0, now) if a := mustAsk(t, held); regexp.MustCompile(`[0-9a-f]{20,}`).MatchString(a.Headline + a.Explanation + a.Details) { t.Errorf("the ask carries a long hexadecimal string: %q", a.Explanation) } @@ -401,6 +332,7 @@ func TestTheRecordAndBackupAsksAreValid(t *testing.T) { // ADR 0546 clauses 7 and 8: a put's ask is valid at the record ask's key; for the bus its yes approves and binds the act, // so the view cannot answer it, and it lasts no longer than an approving ask may. put := puts.Put{FP: fNew, Why: "the bus's user list", Hold: true} + dgst := "sha256:" + strings.Repeat("b", 64) d := ComposedDigest{Digest: dgst, Judgement: JudgedCannot, Words: "the have names no composition"} for _, bus := range []bool{false, true} { a, err := PutAsk(rkey, "ace", "mail", put, d, bus, now) @@ -445,22 +377,6 @@ func TestTheRecordAndBackupAsksAreValid(t *testing.T) { } } -// 1945/AC21: the condition of a module on neither list is urgent, names the module, the node and the rule holding its want back, -// under the assigner's own name keyed by node and module, and is cleared under the same key. -func TestTheNeitherConditionNamesModuleNodeAndRule(t *testing.T) { - c := NeitherCondition("g14", "i3status-rust", Holding{Rule: mwc.RuleProviders, Words: "no provider of x11-display runs healthy on g14 or at its canary"}, true) - if _, err := occ.EncodeCondition(c); err != nil { - t.Fatalf("the condition is refused: %v", err) - } - if c.State != occ.StateRaised || c.Severity != occ.SeverityUrgent || c.Subject.ID != "i3status-rust" || c.Subject.Machine != "g14" || - !strings.Contains(c.Explanation, "x11-display") || !strings.Contains(c.Explanation, "rule 5") || c.Key != "mesh-delivery-assigner.neither.g14.i3status-rust" { - t.Errorf("condition = %+v", c) - } - if got := NeitherCondition("g14", "i3status-rust", Holding{}, false); got.State != occ.StateCleared || got.Key != c.Key { - t.Errorf("cleared = %+v", got) - } -} - // mustAsk is the router's ask an asks-state value carries. func mustAsk(t *testing.T, a asks.Ask) oc.Ask { t.Helper() diff --git a/modules/mesh-delivery-assigner/internal/assign/findings.go b/modules/mesh-delivery-assigner/internal/assign/findings.go index c69387d9..260ba6ea 100644 --- a/modules/mesh-delivery-assigner/internal/assign/findings.go +++ b/modules/mesh-delivery-assigner/internal/assign/findings.go @@ -154,18 +154,3 @@ func (f Findings) Say(say func(oc.Condition) error) func(oc.Condition) error { return errors.Join(errs...) } } - -// KindActingCycle is the finding of a want held on a cycle of acting waits no rule breaks (design 47 G A-323, A-330): scope module, -// id `.` of the held want, naming the cycle with each edge's step; cleared by the next pass that finds it held on no -// cycle. -const KindActingCycle = "acting-cycle" - -// ActingCycleFinding is the finding acting-cycle of module's want on node, held on cycle. -func ActingCycleFinding(node, module, cycle string) Observed { - return Observed{Scope: mcc.ScopeModule, ID: node + "." + module, Kind: KindActingCycle, Machine: node, Severity: "warning", - Summary: fmt.Sprintf("the want of %s on %s is held on a cycle of acting waits: %s", module, node, cycle), - Headline: fmt.Sprintf("%s on %s waits on a cycle of acting parts", module, node), - Explanation: fmt.Sprintf("Each acting part on the cycle %s waits for another's new version to run, so none moves by itself. "+ - "assigner_release lets one move with the version running.", cycle), - Source: "assigner." + KindActingCycle} -} diff --git a/modules/mesh-delivery-assigner/internal/assign/policy.go b/modules/mesh-delivery-assigner/internal/assign/policy.go index 248346ae..38911e1a 100644 --- a/modules/mesh-delivery-assigner/internal/assign/policy.go +++ b/modules/mesh-delivery-assigner/internal/assign/policy.go @@ -6,13 +6,11 @@ import ( "mesh-delivery-assigner/internal/assignment" "mesh-delivery-assigner/internal/buckets" "sort" - "strings" "time" mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" "git.novox.be/novox/mesh-sdk/go/catalogue" ) @@ -48,66 +46,15 @@ func PolicyOf(m catalogue.Manifest, chosen map[string]catalogue.PersonPolicy) (p return catalogue.Upgrade(m, c) } -func contains(list []string, s string) bool { - for _, v := range list { - if v == s { - return true - } - } - return false -} - // KindProvisionFailed is the assigner's finding of a provision its module's want waits for that failed (ADR 0570 §4). const KindProvisionFailed = "provision-failed" -// ProvisionFailedKey is a failed provision's finding's key, `provider...[.].provision-failed`. -func ProvisionFailedKey(key string) string { - return mcc.Key(mcc.ScopeProvider, key, KindProvisionFailed) -} - -// ProvisionFailedFinding is the finding of a failed provision, naming its consumer, its provider and why. -func ProvisionFailedFinding(key string, p meshprovisionerclient.Provision, at time.Time) (mcseat.Finding, error) { - summary := fmt.Sprintf("%s for %s on %s failed at %s on %s: %s, so %s is not moved there until it is provided", p.Provision, - p.Consumer.Module, p.Consumer.Node, p.Provider.Module, p.Provider.Node, p.Why, p.Consumer.Module) - return encode(Observed{Scope: mcc.ScopeProvider, ID: key, Kind: KindProvisionFailed, Machine: p.Consumer.Node, Also: also(p.Provider.Node, p.Consumer.Node), - Summary: summary, Said: p.Why, Headline: clipHeadline(p.Consumer.Module + " waits for a provision that failed"), - Explanation: occ.NothingToDo + " " + summary + ". It clears once the provider provides it.", - Source: "assigner." + KindProvisionFailed}, at) -} - // encode is the observation as the assigner's finding at at. func encode(o Observed, at time.Time) (mcseat.Finding, error) { return o.Finding(at), nil } -func also(provider, consumer string) []string { - if provider == "" || provider == consumer { - return nil - } - return []string{provider} -} - // KindProvisionMissing is the assigner's finding of a required provision that has had no key for longer than its bound. const KindProvisionMissing = "provision-missing" -// ProvisionMissingKey is its key, `provider....provision-missing`. -func ProvisionMissingKey(key string) string { - return mcc.Key(mcc.ScopeProvider, key, KindProvisionMissing) -} - -// ProvisionMissingFinding is the finding of a required provision with no key past bound (key `..`). -func ProvisionMissingFinding(key string, bound time.Duration, at time.Time) (mcseat.Finding, error) { - parts := strings.SplitN(key, ".", 3) - if len(parts) != 3 { - return mcseat.Finding{}, fmt.Errorf("%q is not ..", key) - } - node, module, provision := parts[0], parts[1], parts[2] - summary := fmt.Sprintf("%s on %s requires %s and mesh-provisioner has composed no key for it in %s, so %s is not moved there: "+ - "no provider of it may be placed", module, node, provision, bound, module) - return encode(Observed{Scope: mcc.ScopeProvider, ID: key, Kind: KindProvisionMissing, Machine: node, - Summary: summary, Headline: clipHeadline(module + " on " + node + " lacks " + provision), - Explanation: occ.NothingToDo + " " + summary + ". It clears once the provision has its key.", - Source: "assigner." + KindProvisionMissing}, at) -} - // KindUnplaced is, from the lease write, a node that runs but no assignments names (ADR 0571 §6). const KindUnplaced = "unplaced" diff --git a/modules/mesh-delivery-assigner/internal/assign/put.go b/modules/mesh-delivery-assigner/internal/assign/put.go index da2515f2..7a9f9635 100644 --- a/modules/mesh-delivery-assigner/internal/assign/put.go +++ b/modules/mesh-delivery-assigner/internal/assign/put.go @@ -212,9 +212,9 @@ func needsPerson(w PutWorld, d ComposedDigest) bool { return d.Judgement == JudgedRecreates || d.Judgement == JudgedMovesData || d.Judgement == JudgedCannot } -// PutWant is the want a put writes once it is wanted: the fingerprint and composition, marked put, and held when it holds. +// PutWant is the want a put writes once it is wanted: the fingerprint, marked put, and held when it holds. func PutWant(p puts.Put) mwc.Want { - return mwc.Want{Fp: p.FP, Composed: p.Composed, Put: true, Hold: p.Hold, Words: "put: " + p.Why} + return mwc.Want{Fp: p.FP, Put: true, Hold: p.Hold, Words: "put: " + p.Why} } // PutOwns says what a put does to the node's want: owns says the rules pass over it, want the want it writes (nil: what diff --git a/modules/mesh-delivery-assigner/internal/assign/verbs.go b/modules/mesh-delivery-assigner/internal/assign/verbs.go deleted file mode 100644 index a6d806f5..00000000 --- a/modules/mesh-delivery-assigner/internal/assign/verbs.go +++ /dev/null @@ -1,38 +0,0 @@ -package assign - -import ( - "slices" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" -) - -// verbWaiting says why the module's want on node waits for a verb it invokes (novox/hq ADR 0614 §2): the first seat verb or -// module tool of Invokes (the module graph's verb edges, those it leaves out on a cycle not among them, §3) that no -// running healthy build serves. Nil Served (nothing read of what runs) waits for none, and a verb a running build serves -// already adds no wait. It holds under the provisions' rule (RuleProvisions): no reader knows a rule number of its own -// yet, so the words name the edge (graph.VerbWaitWords, which the delivery view reads). -func (m Module) verbWaiting(node string) (string, bool) { - if m.Served == nil { - return "", false - } - for _, v := range slices.Sorted(slices.Values(m.Invokes)) { - if !m.Served(v) { - return graph.VerbWaitWords(v, m.Name, node), true - } - } - return "", false -} - -// busStateWaiting says why the module's want on node waits for bus state (ADR 0614 §5): its manifest asks bus state (a -// bus-state entry, as the module graph reads it) and no build whose manifest offers bus-state runs healthy on a node -// present. The provision bus-state is answered with no credential of a consumer's own, so ADR 0603 §5 counts the -// provider's own have and no provisioner key is waited for, handed or not. Nil Offering (nothing read of what runs) waits -// for none, and neither does a module that offers bus-state itself. It holds under the provisions' rule, its words naming bus state (graph.BusStateWaitWords). -func (m Module) busStateWaiting(node string) (string, bool) { - if len(m.Manifest.BusState) == 0 || m.Offering == nil || m.Offering(graph.BusState) || - slices.Contains(m.Manifest.Offers(), graph.BusState) { // the bus module offering it waits on nobody for it - - return "", false - } - return graph.BusStateWaitWords(m.Name, node), true -} diff --git a/modules/mesh-delivery-assigner/internal/assign/wants.go b/modules/mesh-delivery-assigner/internal/assign/wants.go index c8091c54..62a13f9d 100644 --- a/modules/mesh-delivery-assigner/internal/assign/wants.go +++ b/modules/mesh-delivery-assigner/internal/assign/wants.go @@ -2,430 +2,79 @@ package assign import ( "fmt" + "slices" + "strings" + "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/catalogkey" - "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" - "maps" - "mesh-delivery-assigner/internal/puts" - "slices" - "sort" - "strings" - "time" - - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds/forms" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" + "mesh-delivery-assigner/internal/puts" ) -// Module is what the six rules read of one module (ADR 0429 §6, ADR 0528 clause 3): its newest fingerprint F at the -// trunk (the indexer's modules), whether the builder's build of F is done, whether F is held, whether the controller -// issued its credential for F, its manifest at F, and the nodes it is assigned to. +// A want names the newest released version whose pipeline passed (design 47 H §750): for every node whose assignment places +// a module on a watched branch, the want names the version, the pipeline and the mover's rank in it, written tier by tier +// (§751). It is held for nothing else: the rules of ADR 0429 §6 (the build done, not held, the canary and step, every +// provider healthy, the grant, the composition and the reader capabilities, the provisions, the served verbs and the bus +// state), the network hold and A-428's exceptions end (H-A-55). A put owns its node's want as before (ADR 0546), and the +// operator's yes on a module of policy record is the node-engine's have and the asker's (the pass's yeses). + +// Target is the build a node's want names: its fingerprint and released version, with the pipeline whose passed end released +// it and the mover's rank in that pipeline; a placement's build names no pipeline and no rank (design 47 H §750, H-A-37). +type Target struct { + Fp string + Version string + Pipeline string + Rank int64 +} + +// Module is what the want rule reads of one module: the nodes it is assigned to, the build each node's want names, the puts +// that own a node's want and the nodes that compose it themselves. type Module struct { - Name string - FP string - Built bool - // BuildFailed and BuildError say a build that ended failed, with the error as the builder's `build.ended` carries it whole - // (design 47 G §575, A-162): the words of a hold under rule 1. - BuildFailed bool - BuildError string - Held bool - Granted bool - // GrantedAt says whether the controller issued the module's credential for another fingerprint than FP: the first want of a node - // that runs an older one names it (issue 1945). Nil: none. - GrantedAt func(fp string) bool - // GrantedOn says whether the controller's grant of FP names the module's accounts on a node, when the grant names any: - // a grant written for the module's other nodes says nothing of a node whose account is not issued and sealed (issue - // 1945, live on 2026-10-25 at 20:04: ace's mesh-build-go was wanted with no account on the bus). Nil: the grant alone. - GrantedOn func(node string) bool - Manifest catalogue.Manifest - // Provisions is the seat mesh-provisioner's provisions this module consumes on a node, by key (ADR 0570 §4): the want - // waits until each is provided. Nil: the seat's state is not read (not delivered yet), and nothing waits on it. - Provisions func(node string) map[string]meshprovisionerclient.Provision - // Answering is the manifests of the modules placed somewhere that provide a provision: what catalogue.Awaited - // judges a want's provisions by (issue 774). Nil: none known, and no key is waited for that is not there. - Answering func(provision string) []catalogue.Manifest - // Offered says whether any module offers a provision by name or by its own name (catalogue.Manifest.Offers) among every - // manifest the trunk holds and the modules the mesh provides itself, placed or not. Nil: not known, and no requirement - // is waited on for want of an offer. - Offered func(provision string) bool - // Invokes are the seat verbs and module tools the module graph says the module invokes and keeps as edges to the - // modules serving them, those it leaves out on a cycle not among them (ADR 0614 §2, §3): the want waits until each is - // served (Served). Nil: no graph node is read, and no want waits on a verb. - Invokes []string - // Served says whether a build serving a seat verb or module tool runs healthy, counted from its holder's have (ADR 0614 - // §2, as ADR 0603 §5 counts a provider's). Nil: nothing is read of what runs, and no want waits on a verb. - Served func(invoke string) bool - // Offering says whether a build whose manifest offers a provision runs healthy on a node present, counted from the - // provider's own have (ADR 0603 §5): what a want asking bus state waits for (ADR 0614 §5). Nil: nothing is read of what - // runs, and no want waits on bus state. - Offering func(provision string) bool + Name string + Manifest catalogue.Manifest // Chosen is a person's choice of the module's upgrade policy (the assigner's `policies`, ADR 0565 §7); nil: none. Chosen *catalogue.PersonPolicy Nodes []string - // Composed answers the composition of the module for a node at a fingerprint: the digest the node's own have names (a - // placed node composes its own, ADR 0571 §6), and whether it names one. Nil: none exists (ADR 0539 §1, §6). - Composed func(node, fp string) (ComposedDigest, bool) + // Target is the build node's want names: the newest version of the module released on the branch the node's placement + // names whose pipeline passed and whose tier is written (§750, §751), or the placement's build (H-A-5); false: none yet, + // and the want written stays. + Target func(node string) (Target, bool) // Allowed says whether the operator answered yes to the record ask about the move of this module on node to the composition // with this digest (ADR 0539 §14). Nil: none did. Allowed func(node, digest string) bool - // Released is a hold on FP the operator released (ADR 0539 §19): FP is not held again by a health check that failed. - Released bool - // Restored says whether the operator answered "restore" to the restore ask of this data module on node: it is then set back - // as any module is, losing the writes made since (ADR 0539 §19). Nil: none did. - Restored func(node string) bool - // Kept says the operator answered "keep" to the hold ask of FP: a hold kept so is not lifted by itself. - Kept bool - // Now is when this pass reads the haves: the held record names it as when the health check was seen to fail. Zero: not said. - Now time.Time - // Puts are the puts of the module, by node (ADR 0546): while one owns a node's want (PutOwns), the rules pass over it. + // Puts are the puts of the module, by node (ADR 0546): while one owns a node's want (PutOwns), the rule passes over it. Puts map[string]puts.Put - // Needs are the reader capabilities the manifest at FP needs (the indexer's modules value, catalogue.ManifestNeeds, ADR - // 0614 §4): a node's want moves only while that node's engine states each of them. Nil: none. - Needs []string - // FormNeeds are the reference forms the module's build at FP is written in (`form:`, the builder's build key's - // needs, ADR 0618 §1): a node's want is held only while that node's engine reads none of them (ADR 0624 §2). Nil: the - // build names none, and holds nothing. - FormNeeds []string - // Self says whether node composes the module itself: the catalogue's assignments names the node (ADR 0547 §2). It is the one - // source of a want's Self: every want the rules write or keep is derived from it (selfDerived), so a want written Self while - // the node was placed does not outlive the assignments (issue 1126). Nil: no node does. + // Self says whether node composes the module itself: the catalogue's assignments names the node (ADR 0547 §2). Nil: no + // node does. Self func(node string) bool - // KeptBuilds answers what a want at a fingerprint names of its build: its parts and its keeping node as the builder's `build.ended` - // names them, or the released version and parts of the catalog's `version-released` (design 47 G §379, §526, A-96), for a node that states the capability WantParts. Nil: none is read. - KeptBuilds func(fp string) (KeptBuild, bool) - // MachineHeld says whether the node's own network condition is open, which holds the machine as a whole: no want moves to it - // until that clears (design 47 G §706 *M's own network*). Nil: none is held. - MachineHeld func(node string) bool - // Acting are the acting edges of the assignment graph entry of this module on node that the assigner holds (composing, placing) - // and whose wait was not released by hand (design 47 G §266, §268): the want moves only once each acting part's version runs - // on the edge's node, as Nodes.Running says it. Nil: none. - Acting func(node string) []ActingEdge + // ReadsVersion says whether node's node-engine states that it reads a want by version (the word want:version of the seat + // node-engine, design 47 H §772, H-A-45); a node whose engine does not is written its want held, awaiting its hand placement + // (H-A-73). Nil: every node does. + ReadsVersion func(node string) bool } -// ActingEdge is one acting edge: Part's version Fp acts in Step on Node, before the entry moves. -type ActingEdge struct{ Node, Part, Fp, Step, Seat string } - -// Runs says the part's version of the edge runs on its node, as the node-engine seat's `running` says it. -func (n Nodes) Runs(e ActingEdge) bool { - if n.Running != nil { - r, ok := n.Running[e.Node][e.Part] - return ok && !r.Stopped && r.Fingerprint == e.Fp - } - h, ok := n.Haves[e.Node][e.Part] - return ok && healthy(h, e.Fp) -} - -// actingWaiting says why the module's want on node waits for an acting part's new version (§266): the first edge not run. -func (m Module) actingWaiting(node string, n Nodes) (string, bool) { - if m.Acting == nil { - return "", false - } - for _, e := range m.Acting(node) { - if _, runs := n.ActingRuns(e); !runs { - return fmt.Sprintf("%s on %s waits in %s for %s at %s to run on %s", m.Name, node, e.Step, e.Part, short(e.Fp), e.Node), true - } - } - return "", false -} - -// ReadsParts says whether node's node-engine reads a want's version, parts and kept-on: it states WantParts among its record's -// capabilities, where a node-engine of G states it (design 47 G §525), or, as read before, among its manifest reads. -func (n Nodes) ReadsParts(node string) bool { - return slices.Contains(n.Capable[node], WantParts) || slices.Contains(n.Reads[node], WantParts) -} - -// WantParts is the capability a node-engine of G states (ADR 0614 §4, design 47 G §525): it reads a want's `parts` and `kept-on`. -const WantParts = "want-parts" - -// KeptBuild is what a want names of a build: the digest of each of its parts by `:`, and, as the builder's -// `build.ended` says it, the build node that keeps a kept build; for a trunk build the released version the catalog's -// `version-released` says (design 47 G §379, §526, A-96). -type KeptBuild struct { - Parts map[string]string - KeptOn string - Version string -} - -// withKept is w naming the kept build at its fingerprint, the parts by digest and the keeping node, only while the node's -// engine states WantParts (design 47 G §525, §526); a want of a build that is not kept, or of a node that does not state it, -// is w. -func (n Nodes) withKept(m Module, node string, w mwc.Want) mwc.Want { - if m.KeptBuilds == nil { - return w - } - k, ok := m.KeptBuilds(w.Fp) - if !ok { - return w - } - if k.Version != "" { - // A want names the module and its version, never its artifacts' digests (design 47 G A-430, A-430a): the node-engine - // resolves each artifact at that version through its kind's registry seat. - w.Version, w.Parts = k.Version, nil - if k.KeptOn != "" { - w.KeptOn = k.KeptOn - } - // Bootstrap, until G is delivered (lead-47, 2026-11-15): a node whose node-engine reads parts gets them beside the - // version, so it composes without asking a registry seat its grant does not yet admit (the grant follows the - // node-engine build wanted there, which needs a pipeline, which needs the pipeline runner this composes). - if n.ReadsParts(node) { - w.Parts = k.Parts - } - return w - } - // A build with no version known to the assigner (another watched branch's, a kept one: build.ended names none) is named by its - // parts and keeping node as before, for a node that reads them, until the builder says the version it numbered it under. - if n.ReadsParts(node) { - w.Parts, w.KeptOn = k.Parts, k.KeptOn - } - return w -} - -// NameKept is w naming the build at its fingerprint as the want of a module does (withKept), for a want held as it stands. -func (n Nodes) NameKept(m Module, node string, w mwc.Want) mwc.Want { return n.withKept(m, node, w) } - func (m Module) selfOn(node string) bool { return m.Self != nil && m.Self(node) } -func (m Module) composition(node, fp string) (ComposedDigest, bool) { - if m.Composed == nil { - return ComposedDigest{}, false - } - return m.Composed(node, fp) -} - -// Node is what the rules read of the nodes: whether each is online (the watchman's presence) and each module's have. +// Nodes is what the pass reads of the nodes: whether each is online (the watchman's presence), each module's have and the +// node-engine seat's last `running`. type Nodes struct { Online map[string]bool Haves map[string]map[string]nodeengine.HaveByNodeModuleValue - // Running is, by node and module, the last `running` the node-engine seat said (design 47 G §309): a provider runs healthy - // while the build it runs does, as this says it, whatever a newer want of it waits for (rule 5, main file *A provider - // before its consumers*). Nil: the seat's events are not heard, and the have says it. + // Running is, by node and module, the last `running` the node-engine seat said (design 47 G §309). Nil: the seat's events + // are not heard. Running map[string]map[string]nodeengine.Running // Providers is, by provision, the modules that provide it, with the nodes each is assigned to. Providers map[string]map[string][]string - // Reads is, by node, the reader capabilities its node-engine states in its node record (nodeengine.NodeRecord.ManifestReads, - // ADR 0614 §4). A node with none, or none read, is read as stating the baseline (catalogue.StatedManifestReads). + // Reads is, by node, the reader capabilities its node-engine states (nodeengine.NodeRecord.ManifestReads, ADR 0614 §4). Reads map[string][]string - // Capable is, by node, the capabilities its node-engine states in its node record (nodeengine.NodeRecord.Capabilities): a - // node-engine of G states WantParts there (design 47 G §525). - Capable map[string][]string - // Serves says whether the build of a running statement serves an acting edge's seat, from that build's manifest: known false - // when its manifest cannot be read (design 47 G A-426). Nil: none is read. - Serves func(r nodeengine.Running, seat string) (serves, known bool) -} - -// ActingRuns says whether an acting edge's part runs a build serving the edge's seat on its node, and which build (design 47 G -// A-426): any running build of the part that serves the seat ends the wait, whatever version its newest is, so a wait on a version -// not yet built is never held for that alone. A build whose manifest cannot be read counts as serving it when it is the part's -// (the part holds the seat by its claim). Without the running statements the have is read as before: the edge's version only. -func (n Nodes) ActingRuns(e ActingEdge) (string, bool) { - if n.Running == nil { - return e.Fp, n.Runs(e) - } - r, ok := n.Running[e.Node][e.Part] - if !ok { - // No running statement names the part on its node: its node-engine states running only of what it applied itself (a - // part placed before, the node-engine itself, or an engine from before G states none), so the part is read from its - // have: a build running there, its health check not failed (such an engine may write no health), ends the wait, - // whatever its version, as a statement would (A-426). Read by the edge's version only, a part placed there waited for - // ever (lead-47 2026-11-16: the indexer on novox for the assigner on ace and novox's own node-engine). - if h, ok := n.Haves[e.Node][e.Part]; ok && h.Fp != "" && h.Phase == nodeengineclient.PhaseRunning && !failed(h) { - return h.Fp, true - } - // The node's own node-engine writes that node's haves and keeps it heard: a node online runs its node-engine, whatever - // phase or health its have of itself says (novox's said held, health unknown, while it composed every other module). - if h, ok := n.Haves[e.Node][e.Part]; ok && e.Seat == nodeengine.Seat && n.Online[e.Node] { - return h.Fp, true - } - return e.Fp, n.Runs(e) - } - if r.Stopped || r.Fingerprint == "" { - return "", false - } - if r.Fingerprint == e.Fp || e.Seat == "" || n.Serves == nil { - return r.Fingerprint, true - } - if serves, known := n.Serves(r, e.Seat); known && !serves { - return "", false - } - return r.Fingerprint, true -} - -// healthy says whether a have runs fp healthy. -func healthy(h nodeengine.HaveByNodeModuleValue, fp string) bool { - return h.Fp == fp && h.Phase == nodeengineclient.PhaseRunning && h.Health == nodeengineclient.HealthOK } // Failed says whether a have's health check failed once the node is done moving. func Failed(h nodeengine.HaveByNodeModuleValue) bool { return failed(h) } -// Canary is the module's canary: the first of its nodes, by name, that is online (ADR 0435 §3; one ring, ordered by -// name, ADR 0528 clause 3). Empty when none is online. -func Canary(nodes []string, online map[string]bool) string { - sorted := slices.Sorted(slices.Values(nodes)) - for _, n := range sorted { - if online[n] { - return n - } - } - return "" -} - -// Holds is whether every rule holds for moving node's want for m to m.FP, and if not, the first rule that does not, -// with why (ADR 0429 §6). Rule 4, at most `step` others ahead, always holds: the mesh keeps no step, and step 0 is no -// limit (ADR 0528 clause 3). Rule 5, every provider healthy, holds no want (issue 1960): a module whose provider is down -// is wanted all the same, and said as not reached (Unreached), since a site runs while the proxy in front of it is -// down and one module never blocks another. -func Holds(m Module, node string, n Nodes) (bool, int, string) { - return holds(m, node, n, true) -} - -// holds is Holds; with ordering false it leaves out the canary (3), which orders a move among nodes, and keeps those -// without which there is nothing to want: the build done, not held, the grant and the composition. -func holds(m Module, node string, n Nodes, ordering bool) (bool, int, string) { - switch { - case !m.Built: - if m.BuildFailed { - return false, mwc.RuleBuildDone, fmt.Sprintf("the build of %s at %s failed: %s", m.Name, short(m.FP), m.BuildError) - } - return false, mwc.RuleBuildDone, fmt.Sprintf("the build of %s at %s is being built", m.Name, short(m.FP)) - case m.Held: - return false, mwc.RuleNotHeld, fmt.Sprintf("%s is held at %s", m.Name, short(m.FP)) - case m.MachineHeld != nil && m.MachineHeld(node): - return false, mwc.RuleNotHeld, fmt.Sprintf("the condition machine.%s.network is open: no want moves to %s until its own network is healthy", node, node) - } - if ordering { - canary := Canary(m.Nodes, n.Online) - switch { - case canary == "": - return false, mwc.RuleCanary, fmt.Sprintf("none of the nodes of %s is online, so it has no canary", m.Name) - case node != canary && !n.RunsHealthy(canary, m.Name, m.FP): - return false, mwc.RuleCanary, fmt.Sprintf("the canary %s does not run %s healthy yet", canary, short(m.FP)) - } - } - if !m.Granted { - return false, mwc.RuleGrant, fmt.Sprintf("the controller has not issued the credential of %s for %s", m.Name, short(m.FP)) - } - if m.GrantedOn != nil && !m.GrantedOn(node) { - return false, mwc.RuleGrant, fmt.Sprintf("the controller has not issued the credential of %s on %s for %s", m.Name, node, - short(m.FP)) - } - if words, lacking := m.readerLacking(node, n); lacking { - return false, mwc.RuleComposed, words - } - if _, ok := m.composition(node, m.FP); !ok { - return false, mwc.RuleComposed, fmt.Sprintf("%s has named no composition of its own for %s at %s yet", node, m.Name, short(m.FP)) - } - if words, waiting := m.actingWaiting(node, n); waiting { - return false, mwc.RuleComposed, words - } - if words, waiting := m.provisionWaiting(node); waiting { - return false, mwc.RuleProvisions, words - } - if words, waiting := m.verbWaiting(node); waiting { - return false, mwc.RuleProvisions, words - } - if words, waiting := m.busStateWaiting(node); waiting { - return false, mwc.RuleProvisions, words - } - return true, 0, "" -} - -// readerLacking says why the module's want on node is held for the node's engine (ADR 0614 §4): a reader capability its -// manifest needs that the engine does not state, read as the baseline when it states none, a bootstrapped seat counting as -// read by every engine (graph.MissingReads, ADR 0636 §2). It is held under rule 7 (the -// composition), since no rule of its own is numbered in this release, with words naming the capability; it moves once the -// engine states it. The node-engine's own new build is held only by a word of its own manifest no engine states. -// -// A build's reference forms hold the want only when the engine reads none of them (ADR 0624 §2): every engine reads a -// first form, so a build written in one is never held by its forms; the words name the forms it reads none of. -func (m Module) readerLacking(node string, n Nodes) (string, bool) { - missing := graph.MissingReads(m.Needs, n.Reads[node]) - if len(missing) == 0 && forms.Current.Held(m.FormNeeds, n.Reads[node]) { - missing = slices.Clone(m.FormNeeds) - } - if len(missing) == 0 { - return "", false - } - return graph.ReaderWaitWords(node, missing, m.Name, short(m.FP)), true // the words the delivery view names the edge by -} - -// provisionWaiting says why the module's want on node waits for a provision (ADR 0570 §4): a provision it requires that has -// no key yet, or the first of its keys that is not provided. Nil Provisions (the seat's state not on the bus) waits for none. -func (m Module) provisionWaiting(node string) (string, bool) { - if m.Provisions == nil { - return "", false - } - if missing := m.MissingProvisions(node); len(missing) > 0 { - return fmt.Sprintf("provision %s has no key yet for %s on %s", missing[0], m.Name, node), true - } - keyed := m.Provisions(node) - // Only a provision the newest manifest still requires is waited on (ADR 0603 §9): a key of an older revision's edge, - // asked or retiring for the operator's answer (ADR 0570 §5), holds nothing. A requirement catalogue.Awaited leaves out is - // not waited on when something offers it (a module's own name, a module the mesh provides itself, a provision the - // composition does not key: optional, a toolchain on the module's own machine, a login the controller makes). One that - // nothing offers is waited on (ADR 0603 §5: an unknown provider has provided nothing), so a consumer never moves ahead of - // a provider whose module the indexer has not yet taken in; it holds loudly, since the watchman raises the hold past its - // bound (ADR 0603 §8). - needed := m.Manifest.Requires - if m.Answering != nil { - needed = catalogue.Awaited(m.Manifest, m.Answering) - have := map[string]bool{} - for _, p := range keyed { - have[p.Provision] = true - } - for _, r := range slices.Sorted(slices.Values(m.Manifest.Requires)) { - if have[r] && !m.Manifest.OptionalRequires[r] && !slices.Contains(needed, r) { - needed = append(needed, r) // the provisioner keys it from the manifests, whoever is placed yet - continue - } - if m.Offered == nil || m.Manifest.OptionalRequires[r] || len(m.Answering(r)) > 0 || m.Offered(r) || slices.Contains(needed, r) { - continue - } - needed = append(needed, r) - if !have[r] { - return fmt.Sprintf("no module offers %s yet for %s on %s: an unknown provider has provided nothing, so the want waits for one, and the watchman raises it past its bound", r, m.Name, node), true - } - } - } - for _, key := range slices.Sorted(maps.Keys(keyed)) { - if p := keyed[key]; p.State != meshprovisionerclient.Provided && slices.Contains(needed, p.Provision) { - words := fmt.Sprintf("the provision %s of %s on %s is %s, not provided", key, m.Name, node, p.State) - if p.Why != "" { - words += ": " + p.Why - } - return words, true - } - } - return "", false -} - -// MissingProvisions are the provisions a want of the module waits for (catalogue.Awaited, the one rule the plan -// composition and the seat mesh-provisioner key by, issue 774) that have no key on node yet, sorted; none while the -// seat's state is not read. A requirement the composition keys nothing for (a toolchain on the module's own machine, a -// login the controller still makes for a module not handed) is not waited for: on 2026-10-30 every new build of -// grafana, dnsmasq, mesh-knowledge, mesh-mail and the builders waited here for a key nobody would make. -func (m Module) MissingProvisions(node string) []string { - if m.Provisions == nil || m.Answering == nil { - return nil - } - have := map[string]bool{} - for _, p := range m.Provisions(node) { - have[p.Provision] = true - } - var out []string - for _, r := range catalogue.Awaited(m.Manifest, m.Answering) { - if !have[r] { - out = append(out, r) - } - } - return out -} - // runsHealthy says whether module runs healthy on node: the node-engine seat's last `running` of it names a build, does not say it // stopped and does not say `healthy` false (design 47 G §520); nothing heard of it counts as not healthy. func (n Nodes) runsHealthy(node, module string) bool { @@ -434,317 +83,53 @@ func (n Nodes) runsHealthy(node, module string) bool { } // RunsHealthy says whether the build of module at fp runs healthy on node, from the node-engine seat's last `running` of it alone -// (design 47 G §520): a `running` with no `healthy`, said by a node-engine of before G, reads as healthy while it stands, and a -// module of which nothing was heard counts as not healthy. The have is never read for it. +// (design 47 G §520). The have is never read for it. func (n Nodes) RunsHealthy(node, module, fp string) bool { r, ok := n.Running[node][module] return ok && n.runsHealthy(node, module) && r.Fingerprint == fp } -// providedHealthy is rule 5 for one provision: some module providing it runs healthy on the node, or, assigned -// elsewhere only, at its canary. A provision no module provides is not a module's to judge (the mesh or the node -// offers it) and holds. -func providedHealthy(provision, node string, n Nodes) (bool, string) { - providers := n.Providers[provision] - if len(providers) == 0 { - return true, "" - } - names := make([]string, 0, len(providers)) - for p := range providers { - names = append(names, p) - } - sort.Strings(names) - for _, p := range names { - nodes := providers[p] - at := node - if !slices.Contains(nodes, node) { - at = Canary(nodes, n.Online) - } - if at != "" && n.runsHealthy(at, p) { - return true, "" - } - } - return false, fmt.Sprintf("no provider of %s runs healthy on %s or at its canary", provision, node) -} - -// unreached is why m on node may be wanted and not reached: the first provision it requires that no provider runs -// healthy on node or at its canary (rule 5, said and never holding a want; issue 1960); empty when all do. -func unreached(m Module, node string, n Nodes) string { - for _, provision := range m.Manifest.Requires { - if ok, why := providedHealthy(provision, node, n); !ok { - return why - } - } - return "" -} - -// keepsData says whether a module holds data a set-back would lose: an own item of a class that keeps (irreplaceable, -// valuable or rebuildable; cache and none do not). -func keepsData(m catalogue.Manifest) bool { - for _, it := range m.DataItems() { - if it.Class == catalogue.ClassIrreplaceable || it.Class == catalogue.ClassValuable || it.Class == catalogue.ClassRebuildable { - return true - } - } - return false -} - -// Outcome is what one module's pass decides: each node's want to write (only those that change), and a hold to write. +// Outcome is what one module's pass decides: each node's want to write (only those that change). type Outcome struct { Wants map[string]mwc.Want - // Held is the hold of m.FP to write, when a node's health check failed on it and it is not held yet. - Held *mwc.Held - // Lifted is the hold of m.FP lifted by itself: every node that runs the module runs m.FP healthy (issue 1906). The held - // record and its ask are taken away, and the wants move by the rules again in this pass. - Lifted bool - // NodeFailed is, by node, a failed health check of m.FP on a node while another node runs m.FP healthy: the failure is - // that node's, so m.FP is not held and nothing is set back (ADR 0557 §1, issue 1496). The pass raises its condition once. - NodeFailed map[string]NodeFailure - // Record is the moves a person must allow before the want names them: each is asked once (ADR 0539 §14). - Record []RecordNeed - // Restore is the nodes of a module holding data whose held fingerprint failed its health check there and that are not set - // back: the operator is asked whether to restore each (ADR 0539 §19). - Restore []string - // Rules is, by node, the rule holding the node back from m.FP and why. - Rules map[string]Holding - // Unreached is, by node, why the module may be wanted there and not reached: no provider of a provision it requires runs - // healthy (rule 5, issue 1960). It holds no want back; the pass says it. - Unreached map[string]string - // Neither is, by node, the nodes the module is assigned to that run it (or whose have names it), hold no want for it after this - // pass and are not held (a held module is on the held list), with the rule holding the first want back: the node's own composition does not hold a module with a fingerprint of the new form, so such a module is on - // neither list and nothing applies it on that node (issue 1945). A module with no fingerprint of the new form goes with the node's - // own composition and is never here. - Neither map[string]Holding } -// RecordNeed is a move of a module of policy record on a node that waits for the operator's yes: the composition's digest, the -// controller's judgement of the move and its words. -type RecordNeed struct { - Node string - Digest string - Judgement Judgement - Words string -} - -// Holding is a rule holding a node back, and why. The bus module is held back by the rules alone, as every module (ADR 0601 §1, -// §2): no bar of its own. -type Holding struct { - Rule int - Words string -} - -// Reason is the holding in words: the rule and its words. -func (h Holding) Reason() string { - return fmt.Sprintf("rule %d: %s", h.Rule, h.Words) -} - -// Wants is the pass of one module m: for each of its nodes, the want by the six rules, starting from the want written -// before (wants, by node); lastHealthy is, by node, the newest fingerprint other than m.FP the node ran healthy. A failed -// health check of m.FP on a node holds it: every node wanting m.FP is set back to its last healthy fingerprint, but a -// module holding data is not set back but asked about (Restore). A -// node that wants m but is no longer among its nodes gets its want written removed, never deleted (ADR 0408 §4). -func Wants(m Module, n Nodes, wants map[string]mwc.Want, lastHealthy map[string]string) Outcome { - out := Outcome{Wants: map[string]mwc.Want{}, Rules: map[string]Holding{}, Neither: map[string]Holding{}} - held := m.Held - if held && !m.Kept && m.FP != "" && allRunHealthy(m, n) { - out.Lifted, held = true, false - } - if !held && !out.Lifted && !m.Released && m.FP != "" { - for _, node := range slices.Sorted(slices.Values(m.Nodes)) { - h, ok := n.Haves[node][m.Name] - if !ok || h.Fp != m.FP || !failed(h) { - continue - } - if others := healthyElsewhere(m, n, node); len(others) > 0 && !failedElsewhere(m, n, node) { - // F runs healthy on another node, so its failure here is this node's, not F's: F is not held, no node is set back, - // and the failing node keeps its want (ADR 0557 §1; issue 1496: on ace a newly assigned mesh-build-node failed its - // first check and the hold moved shanks, g14 and novox, healthy on F for an hour, back). The pass raises the node's - // condition. With no online healthy witness, or with F failed on a second node, the hold and the set-back stand (ADR 0557 - // §2, ADR 0429 §6). - if out.NodeFailed == nil { - out.NodeFailed = map[string]NodeFailure{} - } - out.NodeFailed[node] = NodeFailure{Others: others, Words: HeldWords(h, time.Time{})} // said once: no clock in it - continue - } - out.Held = &mwc.Held{Node: node, Words: HeldWords(h, m.Now)} - held = true - break - } - } - mm := m - mm.Held = held +// Wants is the pass of one module m: for each of its nodes, the want naming its Target, starting from the want written before +// (wants, by node). A put that owns a node's want writes the put's; a node with no target keeps the want written. A node that +// wants m but is no longer among its nodes gets its want written removed, never deleted (ADR 0408 §4). +func Wants(m Module, wants map[string]mwc.Want) Outcome { + out := Outcome{Wants: map[string]mwc.Want{}} for _, node := range m.Nodes { - if m.FP == "" { - break - } before, had := wants[node] - var next mwc.Want p, put := m.Puts[node] if !put && had && before.Put && before.Hold { - // The want is the truth (ADR 0546 clause 5): a want marked put and held stands while its put is not heard (the - // puts not open, or not yet handed after a start); only a put heard released gives it back to the rules. A put - // without hold is not kept this way: its put takes it further once heard. - p, put = puts.Put{FP: before.Fp, Composed: before.Composed, Hold: true, Why: strings.TrimPrefix(before.Words, "put: "), - Step: puts.Holding}, true + // The want is the truth (ADR 0546 clause 5): a want marked put and held stands while its put is not heard; only a put + // heard released gives it back to the rule. + p, put = puts.Put{FP: before.Fp, Hold: true, Why: strings.TrimPrefix(before.Words, "put: "), Step: puts.Holding}, true } if put { if want, owns := PutOwns(p); owns { - if want != nil { - w := *want - // A held bus still takes a revision of its composition that changes only its user list (ADR 0539 §7). - if ul, ok := m.userListRevision(node, before, had, n); ok && ul.Fp == w.Fp { - w.Composed = ul.Composed - } - if !had || !mwc.SameWant(normal(before), normal(w)) { - out.Wants[node] = w - } + if want != nil && (!had || !mwc.SameWant(normal(before), normal(*want))) { + out.Wants[node] = *want } continue } } - // A revision of the bus module's composition that differs only in its user list is written at once, at the fingerprint the - // node runs, outside the rules, the record hold and the bar on a new fingerprint: a new module's credential must reach the - // bus before the module can connect (ADR 0539 §7). The controller marks it; whatever it judges of the file's mount stands. - if w, ok := m.userListRevision(node, before, had, n); ok { - if !had || !mwc.SameWant(normal(before), normal(w)) { - out.Wants[node] = w - } + t, ok := Target{}, false + if m.Target != nil { + t, ok = m.Target(node) + } + if !ok || t.Fp == "" { continue } - ok, rule, words := Holds(mm, node, n) - target := m.FP - if have, ran := n.Haves[node][m.Name]; !ok && (!had || before.Removed) && rule == mwc.RuleCanary { - // The canary orders a move, never the first want of a node that does not run the module (issue 1960: de-spiegel, - // link2pay and photos on novox were left with no want). A node that runs an older fingerprint of the new form, - // composed and granted, gets its first want at what it runs, and rule 3 then orders its move to F as from any want. - // A node that runs an old-form build, which no want can name, stays under the canary (issue 1945, a design limit): - // its first want names F once the canary runs F healthy. Any other node that runs it (F itself, or an older - // fingerprint with no composition or grant) gets F by the rules without which there is nothing to want. - older := ran && runs(have) && have.Fp != m.FP && strings.HasPrefix(have.Fp, meshcatalogclient.SourcePrefix2) - if older { - _, composed := m.composition(node, have.Fp) - older = composed && m.GrantedAt != nil && m.GrantedAt(have.Fp) - } - oldForm := ran && runs(have) && have.Fp != m.FP && !strings.HasPrefix(have.Fp, meshcatalogclient.SourcePrefix2) - switch { - case older: - ok, target = true, have.Fp - case oldForm: - // held back by rule 3 as written: on neither list until the canary runs F healthy - default: - ok, rule, words = holds(mm, node, n, false) - } + next := mwc.Want{Fp: t.Fp, Version: t.Version, Pipeline: t.Pipeline, Rank: t.Rank, Self: m.selfOn(node)} + if had && !before.Removed && !before.Put && before.Fp == t.Fp { + // The answers the want carries stay while its build does: a failed backup's (ADR 0539 §16), a held revert's, the yes. + next.Backup, next.Words, next.Revert, next.Yes, next.Release = before.Backup, before.Words, before.Revert, before.Yes, before.Release } - if why := unreached(m, node, n); why != "" { - if out.Unreached == nil { - out.Unreached = map[string]string{} - } - out.Unreached[node] = why - } - if ok && target == m.FP && len(out.NodeFailed) > 0 && !(had && !before.Removed && before.Fp == m.FP) && - n.Haves[node][m.Name].Fp != m.FP { - // While a node fails F alone, F reaches no further node: a node that neither wants nor runs F gets no new want - // naming it, and wants already written stay (ADR 0557 §2). A node that runs F with no live want gets its want at F, or - // it would be on neither list and its node-engine would give the module up (issue 1945). - ok, rule, words = false, mwc.RuleNotHeld, FailedAloneWords(m.Name, m.FP, slices.Sorted(maps.Keys(out.NodeFailed))) - } - switch { - case ok: - d, _ := m.composition(node, target) - next = n.withKept(m, node, mwc.Want{Fp: target, Composed: d.Digest}) - at := m - if target != m.FP { - // the first want at the older fingerprint the node runs: held back from F by the rule, and judged as a want at it - next.Newest, next.Rule, next.Words = m.FP, int64(rule), words - at.FP = target - } - // A placed node's want names the build alone: it is the same target while the build is. - if had && !before.Removed && !before.Put && before.Fp == target && (before.Composed == d.Digest || (before.Self && m.selfOn(node))) { - next.Backup, next.Words = before.Backup, before.Words // the answer to a failed backup stays while its target stays (ADR 0539 §16) - } - running, ran := n.Haves[node][m.Name] - // A removed want is no want: a first want after it goes through the record ask as a first want does. - asked, hadLive := before, had - if before.Removed { - asked, hadLive = mwc.Want{}, false - } - if need, wait := at.mustAsk(node, asked, hadLive, ran, running.Fp, running.Composed == "", d); wait { - if need != nil { - out.Record = append(out.Record, *need) - } - continue - } - case held && had && before.Fp == m.FP: - out.Rules[node] = Holding{Rule: rule, Words: words} - last := lastHealthy[node] - switch { - case keepsData(m.Manifest) && (m.Restored == nil || !m.Restored(node)): - next = before - next.Words = fmt.Sprintf("%s holds data, so it is not set back from %s: the operator is asked whether to restore it", m.Name, short(m.FP)) - out.Restore = append(out.Restore, node) - case last == "": - next = before - next.Words = fmt.Sprintf("%s is held at %s and %s ran no other fingerprint healthy to set it back to", m.Name, short(m.FP), node) - default: - d, there := m.composition(node, last) - if !there { - next = before - next.Words = fmt.Sprintf("%s is held at %s, and the composition of %s at %s is gone, so %s is not set back", m.Name, short(m.FP), m.Name, short(last), node) - break - } - next = mwc.Want{Fp: last, Composed: d.Digest, Newest: m.FP, Rule: int64(rule), Words: words} - } - default: - out.Rules[node] = Holding{Rule: rule, Words: words} - if !had || before.Removed { - // The node runs the module, or its have names it, and gets no want for it: the module is on neither list (issue 1945), and - // the pass says so. A new assignment, with no have, waits for the rules and is on no list yet by design. A held module (rule 2, - // held before or by this pass) is on the held list instead, whose hold is said and answered on its own: it is never on neither - // list (issue 1945: systemd-resolved, held on shanks by the operator, was said as urgent every hour). - h, there := n.Haves[node][m.Name] - switch { - case there && h.Phase != nodeengineclient.PhaseRemoved && !held: - out.Neither[node] = Holding{Rule: rule, Words: words} - case had && (!there || h.Phase == nodeengineclient.PhaseRemoved): - // A removed want of a node that does not run the module says why no want follows it (issue 1945), so the view's - // assigner_want answers it; it stays removed. A node with no key gets none: a removed want written where none was - // would have its node agent remove the module and stop holding its composition's resources back. - next = mwc.Want{Removed: true, Words: NoWantWords(rule, words)} - if !mwc.SameWant(normal(before), normal(next)) { - out.Wants[node] = next - } - } - continue - } - next = mwc.Want{Fp: before.Fp, Composed: before.Composed, Newest: m.FP, Rule: int64(rule), Words: words} - if before.Fp == m.FP { - // Held at F by the rules: a new revision of F's composition waits for them, as a new fingerprint does (ADR 0539 §5). - next = mwc.Want{Fp: m.FP, Composed: before.Composed} - } else if d, there := m.composition(node, before.Fp); there && d.Digest != "" && d.Digest != before.Composed { - // Held back at an earlier fingerprint whose kept composition the controller composed again with today's config - // (novox/hq ADR 0545): the want follows its new revision, so the node does not run that build with the config of - // the moment it was made (on g14 a capacity of 0 after a rollback). Asked as any move of a record module is. - at := m - at.FP = before.Fp - running, ran := n.Haves[node][m.Name] - need, wait := at.mustAsk(node, before, true, ran, running.Fp, running.Composed == "", d) - if need != nil { - out.Record = append(out.Record, *need) - } - if !wait { - next.Composed = d.Digest - } - } - } - next, write := m.selfDerived(node, before, had, next, n, &out) - if !write { - continue - } - // Every want that names a build names its version (A-430a), whichever rule made or kept it: a want held at its fingerprint - // by the rules, a node's own (self) want too. - if !next.Removed && next.Fp != "" && next.Version == "" { - next = n.withKept(m, node, next) + if m.ReadsVersion != nil && !m.ReadsVersion(node) { + // A node-engine that cannot read a want by version is placed by hand when its machine is back (design 47 H §772). + next.Hold, next.Words, next.Backup, next.Revert = true, mwc.HoldAwaitsHandPlacement, "", "" } if !had || !mwc.SameWant(normal(before), normal(next)) { out.Wants[node] = next @@ -758,143 +143,17 @@ func Wants(m Module, n Nodes, wants map[string]mwc.Want, lastHealthy map[string] return out } -// selfDerived is next with its Self derived from the assignments now (Module.Self), whichever rule made it, and whether it is -// written. A placed node's want names the build alone (ADR 0547 §2). An unplaced node's want that a rule kept from the want -// written while the node was placed (held at F, held back by rule 3 or 5, a data module asked about) names the controller's -// composition of the same fingerprint, asked as any move of a record module is (ADR 0539 §14): on 2026-10-27, after the -// assignments seed's revert, g14's systemd-resolved (held) and forticlient (rule 5), and shanks's forticlient (rule 3), kept -// their Self wants, which their node agents, no longer placed, could not compose (issue 1126). With no such composition, or -// while its ask waits, the want written stays and the node's holding says why. -func (m Module) selfDerived(node string, before mwc.Want, had bool, next mwc.Want, n Nodes, out *Outcome) (mwc.Want, bool) { - switch { - case next.Removed: - return next, true - case m.selfOn(node): - next.Self, next.Composed = true, "" - return next, true - case !next.Self && next.Composed != "": - return next, true - } - next.Self, next.Yes = false, "" - d, there := m.composition(node, next.Fp) - if !there || d.Digest == "" { - out.Rules[node] = Holding{Rule: mwc.RuleComposed, Words: fmt.Sprintf("%s on %s was composed by the node while it was placed, "+ - "and no composition of its own is named at %s now, so its want stays", m.Name, node, short(next.Fp))} - return next, false - } - at := m - at.FP = next.Fp - running, ran := n.Haves[node][m.Name] - if need, wait := at.mustAsk(node, before, had, ran, running.Fp, running.Composed == "", d); wait { - if need != nil && !slices.Contains(out.Record, *need) { // the branch held back at an older fingerprint may have asked it - out.Record = append(out.Record, *need) - } - return next, false - } - next.Composed = d.Digest - return next, true -} - -// NoWantWords say why a node gets no want for a module: the rule that holds its first want back and why (issue 1945). -func NoWantWords(rule int, words string) string { - return fmt.Sprintf("no want: rule %d: %s", rule, words) -} - // failed says whether a have's health check failed once the node is done moving: phase running or failed (never removed, held, or -// moving). A failed health while it -// backs up, applies or checks is a step of the move, not its outcome (on novox at 15:27:36 on 2026-10-25, "health failed, phase -// applying" held the controller's canary two seconds before the new build was healthy). Every healthy read (healthy, providedHealthy, -// the world's lastHealthy) already asks for phase running. +// moving). func failed(h nodeengine.HaveByNodeModuleValue) bool { return h.Health == nodeengineclient.HealthFailed && (h.Phase == nodeengineclient.PhaseRunning || h.Phase == nodeengineclient.PhaseFailed) } -// healthyElsewhere is every online node of m but node that runs m.FP healthy, by name. -func healthyElsewhere(m Module, n Nodes, node string) []string { - var out []string - for _, other := range slices.Sorted(slices.Values(m.Nodes)) { - // A witness is online: a node that sleeps keeps the have it last wrote, and a stale have is no evidence that F runs - // healthy now. - if other != node && n.Online[other] && n.RunsHealthy(other, m.Name, m.FP) { - out = append(out, other) - } - } - return out -} - -// failedElsewhere says whether another node of m has a have naming m.FP failed: then the failure is not one node's alone, and -// the hold and the set-back stand at the second failure (ADR 0557 §2). -func failedElsewhere(m Module, n Nodes, node string) bool { - for _, other := range m.Nodes { - if h, ok := n.Haves[other][m.Name]; ok && other != node && h.Fp == m.FP && failed(h) { - return true - } - } - return false -} - -// FailedAloneWords say why a node gets no new want naming fp: fp fails its health check on the nodes named, alone. -func FailedAloneWords(module, fp string, failing []string) string { - return fmt.Sprintf("%s at %s fails its health check on %s, so no further node moves to it until that is over", module, short(fp), - strings.Join(failing, ", ")) -} - -// NodeFailure is a failed health check of a module's newest fingerprint on one node while it runs healthy on Others: the have's -// health, phase and words (HeldWords without a time), and the healthy witnesses by name. -type NodeFailure struct { - Others []string - Words string -} - -// runs says whether a have is of a module the node runs: running, whatever its health. -func runs(h nodeengine.HaveByNodeModuleValue) bool { - return h.Phase == nodeengineclient.PhaseRunning -} - -// allRunHealthy says whether every node of m that runs the module (its have's phase is running, or failed) runs m.FP healthy, and -// at least one does: a hold whose fingerprint runs healthy wherever the module runs holds nothing back any more (issue 1906: seven -// modules stayed held on every machine after one transient failed check). A node set back to another fingerprint does not run -// m.FP, and a failed one does not run it healthy, so the hold stays; a have that is removed, held for its backup or still moving -// (backing up, applying, checking) is not counted, or it would keep the hold up for ever. -func allRunHealthy(m Module, n Nodes) bool { - ran := 0 - for _, node := range m.Nodes { - h, ok := n.Haves[node][m.Name] - if !ok || (h.Phase != nodeengineclient.PhaseRunning && h.Phase != nodeengineclient.PhaseFailed) { - continue - } - if !n.RunsHealthy(node, m.Name, m.FP) { - return false - } - ran++ - } - return ran > 0 -} - -// HeldWords are the words a hold keeps of its cause (issue 1906): the health and phase the node reported, when the assigner saw -// it, and the node's own words. The node is the held record's own field. -func HeldWords(h nodeengine.HaveByNodeModuleValue, at time.Time) string { - words := fmt.Sprintf("health %s, phase %s", h.Health, h.Phase) - if !at.IsZero() { - words += ", seen at " + at.UTC().Format(time.RFC3339) - } - if d := strings.TrimSpace(h.Detail); d != "" { - return words + ": " + d - } - return words + ": the node gave no words" -} - -// LiftedWords say why a hold was lifted by itself. -func LiftedWords(module, fp string) string { - return fmt.Sprintf("every node that runs %s runs %s healthy, so the assigner lifted its hold by itself", module, short(fp)) -} - // normal is a want without its version, as a decoded and a composed one are compared. func normal(w mwc.Want) mwc.Want { w.V = 0 return w } - func short(fp string) string { part, err := catalogkey.KeyPart(fp) if err != nil || len(part) < 12 { @@ -903,69 +162,20 @@ func short(fp string) string { return part[:12] } -// mustAsk says whether the move to the composition d on node waits for the operator's yes (need names the ask, ADR 0539 §14). A -// module the node never ran (it has no want and no have), a move judged neither, a module that is not of policy record and a want -// that stays are written as any want. The bus module is no exception: it moves by the rules (ADR 0601 §1), its node backing up its -// streams before it applies (§2); the assigner asks no snapshot for it. -func (m Module) mustAsk(node string, before mwc.Want, had, ran bool, running string, haveNamesNoComposition bool, d ComposedDigest) (*RecordNeed, bool) { - if (!had && !ran) || before.Removed || (before.Fp == m.FP && before.Composed == d.Digest) { - return nil, false - } - // Right after the switch no node has a readable want, and a have from the old node-engine names a fingerprint and no composition - // (ADR 0539 §9, §11): a node running this fingerprint is given its want at it with no hold when the controller judges the composition - // unchanged, equal to the declaration the node last applied (ADR 0540 §9), since the node agent then applies nothing. A composition that - // differs from it (a changed setting or secret), or that the controller could not judge, waits for the ask. A new fingerprint still waits. - if !had && ran && running == m.FP && haveNamesNoComposition && d.Judgement == JudgedUnchanged { - return nil, false - } - if !m.recorded(d) || d.Judgement == JudgedNeither || d.Judgement == JudgedUnchanged { - return nil, false - } - if m.Allowed != nil && m.Allowed(node, d.Digest) { - return nil, false - } - return &RecordNeed{Node: node, Digest: d.Digest, Judgement: d.Judgement, Words: d.Words}, true +// RecordNeed is a move of a module of policy record on a node that waits for the operator's yes: the digest the node-engine's have +// waits on, its judgement of the move and its words (ADR 0539 §14). +type RecordNeed struct { + Node string + Digest string + Judgement Judgement + Words string } -// recorded says whether the module is of policy record: a person's choice at the assigner over the manifest's word (ADR -// 0565 §7, catalogue.Upgrade); for a module with none, as the controller holds it beside the composition's digest until the -// lease write (ADR 0539 §14); on a composition made by hand, which carries none, as the manifest derives it. -func (m Module) recorded(d ComposedDigest) bool { - if m.Chosen != nil { - policy, _, _, _ := catalogue.Upgrade(m.Manifest, m.Chosen) - return policy == catalogue.PolicyRecord - } - if d.Policy != "" { - return d.Policy == catalogue.PolicyRecord - } - policy, _, _ := catalogue.DerivedUpgrade(m.Manifest) - return policy == catalogue.PolicyRecord -} - -// userListRevision is the want that names the bus module's composition at the fingerprint the node runs when that composition -// differs from the one the node runs only in the bus's user list (ComposedDigest.UserListOnly), and whether there is one. A node -// whose want names another fingerprint than the one it runs, or a module that does not provide the bus, has none. -func (m Module) userListRevision(node string, before mwc.Want, had bool, n Nodes) (mwc.Want, bool) { - if !catalogue.ProvidesBus(m.Manifest) { - return mwc.Want{}, false - } - running, ran := n.Haves[node][m.Name] - if !ran || running.Fp == "" || (had && (before.Removed || before.Fp != running.Fp)) { - return mwc.Want{}, false - } - d, ok := m.composition(node, running.Fp) - if !ok || !d.UserListOnly { - return mwc.Want{}, false - } - w := mwc.Want{Fp: running.Fp, Composed: d.Digest} - if had && before.Composed == d.Digest { - w.Backup, w.Words = before.Backup, before.Words - } - return w, true -} - -// GrantNamesNode says whether a grant of module names its accounts on node (mesh-sdk's Grant.NamesNode, which a -// put reads by too). -func GrantNamesNode(g grantsstate.Grant, node, module string) bool { - return g.NamesNode(node, module) +// KeptBuild is what the assigner knows of a build: the digest of each of its parts by `:` and the build node that +// keeps it, as the builder's `build.ended` says them, and its released version as the catalog's `version-released` says it (design +// 47 G §379, A-96). A want names the version alone (design 47 H §750). +type KeptBuild struct { + Parts map[string]string + KeptOn string + Version string } diff --git a/modules/mesh-delivery-assigner/internal/assign/wants_test.go b/modules/mesh-delivery-assigner/internal/assign/wants_test.go index 92b98146..d41135b0 100644 --- a/modules/mesh-delivery-assigner/internal/assign/wants_test.go +++ b/modules/mesh-delivery-assigner/internal/assign/wants_test.go @@ -1,24 +1,18 @@ package assign import ( + "strings" + "testing" + "time" + "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" + "mesh-delivery-assigner/internal/puts" - "reflect" - "slices" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" - "git.novox.be/novox/mesh-sdk/go/catalogue" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" ) func fpOf(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(string(c), 64) } @@ -28,804 +22,76 @@ var ( fOld = fpOf('a') ) -// ready is a module mail at fNew on ace and g14 for which every rule holds: built, not held, granted, its provider -// store healthy on ace and the canary ace running fNew healthy. -func ready() (Module, Nodes) { - m := Module{Name: "mail", FP: fNew, Built: true, Granted: true, Composed: composedEverywhere, Nodes: []string{"g14", "ace"}, - Manifest: catalogue.Manifest{Module: "mail", Requires: []string{"database"}}} - n := Nodes{ - Online: map[string]bool{"ace": true, "g14": true}, - Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "ace": {"mail": {Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}, "store": {Fp: fpOf('s'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}}, - "g14": {"mail": {Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}}, - }, - Providers: map[string]map[string][]string{"database": {"store": {"ace"}}}, - } - return m, n -} - // dig is the digest of the composition at a fingerprint in these tests: the fingerprint's last character repeated. func dig(fp string) string { return "sha256:" + strings.Repeat(fp[len(fp)-1:], 64) } -// composedEverywhere is a Module.Composed with a composition for every node at every fingerprint, judged neither. -func composedEverywhere(_, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedNeither}, true +// released is a module mail on ace and g14 whose newest version released on its branch with a passed pipeline is 1.2.3 at fNew, +// the second tier of the pipeline main.12. +func released() Module { + t := Target{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2} + return Module{Name: "mail", Nodes: []string{"g14", "ace"}, Target: func(string) (Target, bool) { return t, true }} } func wantedOld() map[string]mwc.Want { - return map[string]mwc.Want{"ace": {Fp: fOld, Composed: dig(fOld)}, "g14": {Fp: fOld, Composed: dig(fOld)}} + return map[string]mwc.Want{"ace": {Fp: fOld, Version: "1.2.2", Pipeline: "main.11", Rank: 1}, "g14": {Fp: fOld, Version: "1.2.2", Pipeline: "main.11", Rank: 1}} } -// 1297/B4: with every rule holding, both nodes' wants move to the new fingerprint (the canary ace runs it healthy, so -// g14 follows at once: step 0, ADR 0528 clause 3). -func TestEveryRuleHoldsSoTheWantsMove(t *testing.T) { - m, n := ready() - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - for _, node := range []string{"ace", "g14"} { - if out.Wants[node].Fp != fNew || out.Wants[node].Rule != 0 || out.Wants[node].Composed != dig(fNew) { - t.Errorf("%s's want = %+v, want %s", node, out.Wants[node], fNew) - } - } -} - -// 1297/B4, one test per rule: given every rule but N holds, no want moves to F, and the rule N is named. -func TestEachRuleHoldsTheWantBack(t *testing.T) { - for rule, breakIt := range map[int]func(*Module, *Nodes){ - mwc.RuleBuildDone: func(m *Module, _ *Nodes) { m.Built = false }, - mwc.RuleNotHeld: func(m *Module, _ *Nodes) { m.Held = true }, - mwc.RuleCanary: func(_ *Module, n *Nodes) { - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - }, - mwc.RuleGrant: func(m *Module, _ *Nodes) { m.Granted = false }, - mwc.RuleComposed: func(m *Module, _ *Nodes) { - m.Composed = func(string, string) (ComposedDigest, bool) { return ComposedDigest{}, false } - }, - // 1954/D1 (ADR 0570 §4): a provision of the module on that node still asked holds its want. - mwc.RuleProvisions: func(m *Module, _ *Nodes) { - m.Provisions = func(node string) map[string]meshprovisionerclient.Provision { - return map[string]meshprovisionerclient.Provision{node + ".mail.database": {Provision: "database", State: meshprovisionerclient.Asked}, node + ".mail.oidc": {Provision: "oidc", State: meshprovisionerclient.Provided}} - } - }, - } { - m, n := ready() - breakIt(&m, &n) - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if w, ok := out.Wants["g14"]; ok && w.Fp == fNew { - t.Errorf("rule %d broken: g14's want moved to F", rule) - } - if got := out.Rules["g14"].Rule; got != rule { - t.Errorf("rule %d broken: g14 is held back by rule %d (%s)", rule, got, out.Rules["g14"].Words) - } - if w := out.Wants["g14"]; w.Fp != "" && (w.Fp != fOld || w.Newest != fNew || w.Rule != int64(rule)) { - t.Errorf("rule %d broken: g14's want is %+v", rule, w) - } - if rule == mwc.RuleProvisions && (!strings.Contains(out.Rules["g14"].Words, "g14.mail.database") || !strings.Contains(out.Rules["g14"].Words, "asked")) { - t.Errorf("the provision's hold does not name its key and state: %s", out.Rules["g14"].Words) - } - } - // 1954/D1: every provision provided, the want moves; with the seat's state not read, nothing waits on it. - for _, all := range []func(string) map[string]meshprovisionerclient.Provision{ - func(node string) map[string]meshprovisionerclient.Provision { - return map[string]meshprovisionerclient.Provision{node + ".mail.database": {Provision: "database", State: meshprovisionerclient.Provided}} - }, nil} { - m, n := ready() - m.Provisions = all - if out := Wants(m, heardOfHaves(n), wantedOld(), nil); out.Wants["g14"].Fp != fNew { - t.Errorf("provided provisions held g14's want: %+v (%s)", out.Wants["g14"], out.Rules["g14"].Words) - } - } - // ADR 0614 §4: a want is held on a node whose engine does not state a reader capability the manifest needs, under rule 7 - // with the capability as its words, and moves once the engine states it; an engine stating nothing reads the baseline, - // so a manifest of the baseline's words, the node-engine's own new build among them, is not held. - hostNeeds, err := catalogue.ManifestNeeds([]byte(`{"module":"mesh-host","version":"1","slug":"host","build":{"artifacts":[` + - `{"name":"host-arch","kind":"bundle","language":"go","system":"arch","from":"cmd/mesh-host"}]},"consumes":["restic.backed-up"],` + - `"state":[{"name":"health"}],"config":{"x":{"type":"number","minimum":1,"default":50,"runtime":true}},` + - `"resources":[{"id":"k","type":"file","path":"/var/lib/k","content":"${config:x}\n"}]}`)) - if err != nil { - t.Fatal(err) - } +// A6/AC1, A5/AC1 (design 47 H §750): a want names the newest released version whose pipeline passed, with the pipeline and the +// mover's rank, on every node the module is assigned to; nothing else holds it: no provider, canary, grant, composition, reader +// capability, provision, served verb or bus state is read. A node with no target keeps the want written; a node no longer +// assigned is removed; a want that stands is not written again and keeps the answers it carries. +func TestAWantNamesTheNewestPassedVersion(t *testing.T) { + want := mwc.Want{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2} for _, c := range []struct { - why string - name string - needs []string - reads map[string][]string - held bool - // forms are the build's reference forms (ADR 0624 §2), and names what the hold's words name. - forms []string - names string + name string + module func() Module + before map[string]mwc.Want + wrote map[string]mwc.Want }{ - {"a word the baseline lacks, on engines stating nothing", "mail", []string{"word:module", "word:tool-runner"}, nil, true, nil, "word:tool-runner"}, - {"the same, once both engines state it", "mail", []string{"word:module", "word:tool-runner"}, - map[string][]string{"ace": catalogue.ManifestReads(), "g14": catalogue.ManifestReads()}, false, nil, ""}, - {"the baseline's words on engines stating nothing", "mail", []string{"word:claims.serves", "seat:mesh-catalog", "form:config"}, nil, false, nil, ""}, - {"the node-engine's new build, whose contract has more words than its manifest uses", "mesh-host", hostNeeds, nil, false, nil, ""}, - // Issue 1966: g14's engine states its words and forms but no seat (its statement made before its seat list was in - // use); needs of the bootstrapped seats mesh-catalog and node-backup move all the same (ADR 0636 §2), one of a seat - // not bootstrapped is held. - {"mesh-catalog on an engine stating no seat", "mesh-host", []string{"seat:mesh-catalog", "word:module"}, - map[string][]string{"g14": {"word:module"}}, false, nil, ""}, - {"node-backup on an engine stating no seat", "restic", []string{"seat:node-backup", "word:module"}, - map[string][]string{"g14": {"word:module"}}, false, nil, ""}, - {"a seat not bootstrapped on an engine stating no seat: held, naming it", "mail", []string{"seat:mesh-vault", "word:module"}, - map[string][]string{"g14": {"word:module"}}, true, nil, "seat:mesh-vault"}, - // ADR 0624 §2, with form:new a form no engine reads unless it states it and artifact-store the first form. - {"g14 back stating {old}, a build carrying {old,new}: moves", "mail", nil, map[string][]string{"g14": {"word:module"}}, false, - []string{"form:artifact-store", "form:new"}, ""}, - {"an engine stating nothing, a build carrying the baseline's form: moves", "mail", nil, nil, false, []string{"form:artifact-store"}, ""}, - {"a build carrying {new} only: held, naming the form", "mail", nil, map[string][]string{"g14": {"word:module"}}, true, - []string{"form:new"}, "form:new"}, - {"the same, once g14's engine states it", "mail", nil, map[string][]string{"g14": {"form:new"}}, false, []string{"form:new"}, ""}, - {"a manifest needing an unstated word, its build in the first form: held, naming the word", "mail", []string{"word:tool-runner"}, nil, true, - []string{"form:artifact-store"}, "word:tool-runner"}, + {"both nodes move to the passed version", released, wantedOld(), map[string]mwc.Want{"ace": want, "g14": want}}, + {"a first want", released, nil, map[string]mwc.Want{"ace": want, "g14": want}}, + {"a placed node composes its own", func() Module { + m := released() + m.Self = func(node string) bool { return node == "ace" } + return m + }, nil, map[string]mwc.Want{"ace": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Self: true}, "g14": want}}, + {"no passed version yet: the want written stays", func() Module { + m := released() + m.Target = func(string) (Target, bool) { return Target{}, false } + return m + }, wantedOld(), map[string]mwc.Want{}}, + {"a want that stands is not written again", released, map[string]mwc.Want{"ace": want, "g14": want}, map[string]mwc.Want{}}, + {"the backup answer stays while the build does", released, + map[string]mwc.Want{"ace": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Backup: mwc.BackupSkip, Words: BackupAnswerWords("skip", 0)}, "g14": want}, + map[string]mwc.Want{}}, + {"A9/AC1 (§772, H-A-73): a node whose engine reads no want by version is held, awaiting its hand placement", func() Module { + m := released() + m.ReadsVersion = func(node string) bool { return node == "ace" } + return m + }, nil, map[string]mwc.Want{"ace": want, "g14": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Hold: true, Words: mwc.HoldAwaitsHandPlacement}}}, + {"a node no longer assigned is removed", func() Module { + m := released() + m.Nodes = []string{"ace"} + return m + }, map[string]mwc.Want{"ace": want, "g14": want}, map[string]mwc.Want{"g14": {Removed: true, Words: "mail is no longer assigned to g14"}}}, } { - m, n := ready() - m.Name, m.Needs, n.Reads, m.FormNeeds = c.name, c.needs, c.reads, c.forms - n.Haves["ace"][c.name], n.Haves["g14"][c.name] = n.Haves["ace"]["mail"], n.Haves["g14"]["mail"] - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if moved := out.Wants["g14"].Fp == fNew; moved == c.held { - t.Errorf("%s: g14's want %+v, held by %+v", c.why, out.Wants["g14"], out.Rules["g14"]) - } - if r := out.Rules["g14"]; c.held && (r.Rule != mwc.RuleComposed || !strings.Contains(r.Words, c.names) || - !strings.Contains(r.Words, "node-engine of g14")) { - t.Errorf("%s: held by %+v, want rule 7 naming the capability", c.why, r) - } - if w := out.Wants["g14"]; c.held && (w.Rule != mwc.RuleComposed || w.Newest != fNew) { - t.Errorf("%s: g14's want %+v, want it kept at what it runs and naming rule 7", c.why, w) - } - } - // ADR 0614 §2, §3: a want waits, under the provisions' rule, until each verb it invokes is served by a running build, - // and moves once one is; a verb a running build serves already adds no wait, and an edge the graph leaves out on a cycle - // is not among Invokes, so it holds nothing; nothing read of what runs waits for none. - for _, c := range []struct { - name string - invokes []string - served []string - nilRead bool - holds string - }{ - {"a verb no running build serves", []string{"store.store_put", "seat:mesh-catalog.modules"}, []string{"store.store_put"}, false, "seat:mesh-catalog.modules"}, - {"once a build serving it runs", []string{"seat:mesh-catalog.modules"}, []string{"seat:mesh-catalog.modules"}, false, ""}, - {"an edge left out on a cycle", nil, nil, false, ""}, - {"nothing read of what runs", []string{"seat:mesh-catalog.modules"}, nil, true, ""}, - // ADR 0614 §5: a module asking bus state is held while no build offering bus-state runs, and moves once one does. - {"bus state no running build offers", nil, nil, false, graph.BusState}, - {"bus state a running build offers", nil, []string{graph.BusState}, false, ""}, - {"bus state its own module offers", nil, nil, false, ""}, - } { - m, n := ready() - m.Invokes = c.invokes - if !c.nilRead { - m.Served = func(v string) bool { return slices.Contains(c.served, v) } - } - if strings.HasPrefix(c.name, "bus state") { - m.Manifest.BusState = []catalogue.BusStateEntry{{Name: "cache", Storage: "key-value"}} - m.Offering = func(p string) bool { return slices.Contains(c.served, p) } - if c.name == "bus state its own module offers" { - m.Manifest.Provides = []catalogue.Offer{{Name: graph.BusState, Scope: catalogue.ScopeMesh}} - } - } - words := graph.VerbWaitWords(c.holds, "mail", "g14") - if c.holds == graph.BusState { - words = graph.BusStateWaitWords("mail", "g14") - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if c.holds == "" { - if out.Wants["g14"].Fp != fNew { - t.Errorf("%s: held %+v", c.name, out.Rules["g14"]) - } + out := Wants(c.module(), c.before) + if len(out.Wants) != len(c.wrote) { + t.Errorf("%s: wrote %+v, want %+v", c.name, out.Wants, c.wrote) continue } - if out.Wants["g14"].Fp == fNew || out.Rules["g14"].Rule != mwc.RuleProvisions || out.Rules["g14"].Words != words { - t.Errorf("%s: want %+v, held by %+v", c.name, out.Wants["g14"], out.Rules["g14"]) - } - } - // ADR 0603 §9: the newest source revision governs. The module now at fNew no longer requires the database an older revision - // asked for and requires oidc instead; the seat's keys follow the newest manifest, so the older edge is no wait: a provided - // oidc moves the want, an asked one holds it and names oidc and never database. - for _, c := range []struct { - name string - state, older string - moves bool - }{{"the replaced edge provided, the older edge retiring", meshprovisionerclient.Provided, meshprovisionerclient.Retiring, true}, - {"the replaced edge provided, the older edge asked", meshprovisionerclient.Provided, meshprovisionerclient.Asked, true}, - {"the replaced edge asked, the older edge retiring", meshprovisionerclient.Asked, meshprovisionerclient.Retiring, false}} { - m, n := ready() - m.Manifest.Requires = []string{"oidc"} - m.Provisions = func(node string) map[string]meshprovisionerclient.Provision { - return map[string]meshprovisionerclient.Provision{node + ".mail.oidc": {Provision: "oidc", State: c.state}, - node + ".mail.database": {Provision: "database", State: c.older, Why: "the operator's answer"}} - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if got := out.Wants["g14"].Fp == fNew; got != c.moves || (!c.moves && (!strings.Contains(out.Rules["g14"].Words, "oidc") || strings.Contains(out.Rules["g14"].Words, "database"))) { - t.Errorf("%s: want %+v, held by %q", c.name, out.Wants["g14"], out.Rules["g14"].Words) - } - } - // 1954/D1 (the lead, 2026-10-29): a required provision with no key yet holds the want, named; and only one the - // composition keys (catalogue.Awaited, issue 774), replaying 2026-10-30: grafana (not handed) waited on ace for - // oidc-client, which the controller still makes, and mesh-build-go (handed) for go-toolchain on its own machine. - { - handed := &catalogue.Handed{Source: "s", Digest: "d", Templates: map[string]string{"c": "c"}} - keycloak := catalogue.Manifest{Module: "keycloak", Provides: []catalogue.Offer{{Name: "oidc-client", Scope: catalogue.ScopeMesh}}} - proxy := catalogue.Manifest{Module: "route-proxy", Provides: []catalogue.Offer{{Name: "route", Scope: catalogue.ScopeMesh, - AnsweredBy: catalogue.AnsweredByHelper}}} - store := catalogue.Manifest{Module: "store", Provides: []catalogue.Offer{{Name: "database", Scope: catalogue.ScopeMesh}}} - goTools := catalogue.Manifest{Module: "go", Provides: []catalogue.Offer{{Name: "go-toolchain", Scope: catalogue.ScopeNode}}} - gitea := catalogue.Manifest{Module: "gitea", Provides: []catalogue.Offer{{Name: "forge-account", Scope: catalogue.ScopeMesh}}} - answering := func(p string) []catalogue.Manifest { - for _, m := range []catalogue.Manifest{keycloak, proxy, store, goTools, gitea} { - if m.Provides[0].Name == p { - return []catalogue.Manifest{m} - } - } - return nil - } - for _, c := range []struct { - name string - manifest catalogue.Manifest - missing string // the provision named, "" for none: the want moves - }{ - {"a handed module's database with no key", catalogue.Manifest{Module: "mail", Moved: true, Handed: handed, - Requires: []string{"database"}}, "database"}, - {"a module not handed waits for no login the controller makes (grafana on ace, oidc-client)", - catalogue.Manifest{Module: "mail", Requires: []string{"oidc-client"}}, ""}, - {"a module not handed waits for its route, which the helper answers (grafana's route)", - catalogue.Manifest{Module: "mail", Requires: []string{"oidc-client", "route"}}, "route"}, - {"a handed builder waits for no toolchain on its own machine (mesh-build-go, go-toolchain)", - catalogue.Manifest{Module: "mail", Moved: true, Handed: handed, Requires: []string{"go-toolchain"}}, ""}, - {"and does wait for its forge account", catalogue.Manifest{Module: "mail", Moved: true, Handed: handed, - Requires: []string{"go-toolchain", "forge-account"}}, "forge-account"}, - } { - m, n := ready() - m.Manifest, m.Answering = c.manifest, answering - m.Provisions = func(string) map[string]meshprovisionerclient.Provision { - return map[string]meshprovisionerclient.Provision{} - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if c.missing == "" { - if out.Wants["g14"].Fp != fNew || len(m.MissingProvisions("g14")) != 0 { - t.Errorf("%s: held %+v", c.name, out.Rules["g14"]) - } - continue - } - if out.Rules["g14"].Rule != mwc.RuleProvisions || !strings.Contains(out.Rules["g14"].Words, - "provision "+c.missing+" has no key yet") || len(m.MissingProvisions("g14")) != 1 { - t.Errorf("%s: %+v", c.name, out.Rules["g14"]) + for node, w := range c.wrote { + if got, ok := out.Wants[node]; !ok || !mwc.SameWant(normal(got), w) { + t.Errorf("%s: %s's want = %+v (written %v), want %+v", c.name, node, got, ok, w) } } } - // ADR 0603 §5: a requirement nobody offers is an unknown provider and waits, loudly; one something offers (a module's own - // name, a module the mesh provides itself) is left to catalogue.Awaited as before and does not. - for _, c := range []struct { - name, requires string - offered bool - }{{"a module's own name (node-tools)", "node-tools", true}, {"a module the mesh provides itself (mesh-addressing)", "mesh-addressing", true}, - {"a provision nobody offers", "database", false}} { - m, n := ready() - m.Manifest.Requires = []string{c.requires} - m.Answering = func(string) []catalogue.Manifest { return nil } - m.Offered = func(p string) bool { return c.offered && p == c.requires } - m.Provisions = func(string) map[string]meshprovisionerclient.Provision { - return map[string]meshprovisionerclient.Provision{} - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if got := out.Wants["g14"].Fp == fNew; got != c.offered || (!c.offered && (out.Rules["g14"].Rule != mwc.RuleProvisions || - !strings.Contains(out.Rules["g14"].Words, "no module offers database yet") || !strings.Contains(out.Rules["g14"].Words, "past its bound"))) { - t.Errorf("%s: want %+v, held by %+v", c.name, out.Wants["g14"], out.Rules["g14"]) - } - } - // 1960: rule 5 holds no want, and no module is left without a first want for a provider down or a node offline; the - // provider down is said (Unreached). novox on 2026-10-29: route-proxy down held de-spiegel and link2pay, minio not - // healthy held photos, and novox offline left a module with no canary. - for _, c := range []struct { - name, module, provision, provider string - have *nodeengine.HaveByNodeModuleValue // what novox runs of the module; nil: a new assignment - before map[string]mwc.Want - online bool - want string - }{ - {"de-spiegel, route-proxy down, old form", "de-spiegel", "route", "route-proxy", - &nodeengine.HaveByNodeModuleValue{Fp: "build-1", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown}, nil, true, fNew}, - {"link2pay, route-proxy down, new assignment", "link2pay", "route", "route-proxy", nil, nil, true, fNew}, - {"photos, minio not healthy, moving its want", "photos", "s3-bucket", "minio", - &nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}, - map[string]mwc.Want{"novox": {Fp: fOld, Composed: dig(fOld)}}, true, fNew}, - {"novox offline, no canary, new assignment", "link2pay", "route", "route-proxy", nil, nil, false, fNew}, - {"novox offline, no canary, moving its want", "photos", "s3-bucket", "minio", - &nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}, - map[string]mwc.Want{"novox": {Fp: fOld, Composed: dig(fOld)}}, false, fOld}, - } { - m := Module{Name: c.module, FP: fNew, Built: true, Granted: true, Composed: composedEverywhere, Nodes: []string{"novox"}, - Manifest: catalogue.Manifest{Module: c.module, Requires: []string{c.provision}}} - n := Nodes{Online: map[string]bool{"novox": c.online}, Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"novox": { - c.provider: {Fp: fpOf('p'), Phase: nodeengineclient.PhaseFailed, Health: nodeengineclient.HealthFailed}}}, - Providers: map[string]map[string][]string{c.provision: {c.provider: {"novox"}}}} - if c.have != nil { - n.Haves["novox"][c.module] = *c.have - } - out := Wants(m, heardOfHaves(n), c.before, nil) - w, written := out.Wants["novox"] - if !written { - w = c.before["novox"] - } - if w.Fp != c.want || w.Removed { - t.Errorf("%s: novox's want = %+v, want %s", c.name, w, short(c.want)) - } - if _, on := out.Neither["novox"]; on { - t.Errorf("%s: on neither list: %+v", c.name, out.Neither) - } - if c.online && !strings.Contains(out.Unreached["novox"], c.provision) { - t.Errorf("%s: the provider down is not said: %q", c.name, out.Unreached["novox"]) - } - } - - // 1126/AC-SW1, AC-SW2, AC-SW3: a want written Self while g14 was placed, kept by a rule after the assignments is gone, names - // the controller's composition of the fingerprint the rule keeps (on 2026-10-27 g14's systemd-resolved, held, and forticlient, - // rule 5, kept their Self wants, which g14 could no longer compose); with no such composition, or while a record ask waits, it - // stays and is said; a placed node's want is Self with no composition. - selfAt := func(fp string) map[string]mwc.Want { - return map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}, "g14": {Fp: fp, Self: true, Yes: dig(fp)}} - } - atF := func(_ *Module, n *Nodes) { - n.Haves["g14"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: dig(fNew), Phase: nodeengineclient.PhaseFailed, Health: nodeengineclient.HealthOK} - } - for _, c := range []struct { - name string - set func(*Module, *Nodes) - before string - want mwc.Want // the want of g14 written; zero: none written - rule int // the rule g14's holding names - asked bool - }{ - {name: "rule 5 at F", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - n.Haves["ace"]["store"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf('s'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthWaits} - }, want: mwc.Want{Fp: fNew, Composed: dig(fNew)}}, - {name: "rule 3 at F", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - }, want: mwc.Want{Fp: fNew, Composed: dig(fNew)}, rule: mwc.RuleCanary}, - {name: "rule 3 held back at an older fingerprint", before: fOld, set: func(_ *Module, n *Nodes) { - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - }, want: mwc.Want{Fp: fOld, Composed: dig(fOld), Newest: fNew, Rule: mwc.RuleCanary, - Words: "the canary ace does not run bbbbbbbbbbbb healthy yet"}, rule: mwc.RuleCanary}, - {name: "held at F with nothing to set back to", before: fNew, set: func(m *Module, n *Nodes) { atF(m, n); m.Held = true }, - want: mwc.Want{Fp: fNew, Composed: dig(fNew), Words: "mail is held at bbbbbbbbbbbb and g14 ran no other fingerprint healthy to set it back to"}, - rule: mwc.RuleNotHeld}, - {name: "held data module", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - m.Held = true - m.Manifest.Data = &catalogue.Data{Own: []catalogue.DataItem{{ID: "mail", Class: catalogue.ClassIrreplaceable}}} - }, want: mwc.Want{Fp: fNew, Composed: dig(fNew), - Words: "mail holds data, so it is not set back from bbbbbbbbbbbb: the operator is asked whether to restore it"}, rule: mwc.RuleNotHeld}, - {name: "a record ask waits", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - recording(m) - m.Held = true - m.Composed = func(_, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedRecreates, Against: dig(fOld)}, true - } - }, rule: mwc.RuleNotHeld, asked: true}, - {name: "no composition of F", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - m.Composed = func(node, fp string) (ComposedDigest, bool) { - if node == "g14" { - return ComposedDigest{}, false - } - return composedEverywhere(node, fp) - } - }, rule: mwc.RuleComposed}, - {name: "placed", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - m.Self = func(node string) bool { return node == "g14" } - m.Composed = func(node, fp string) (ComposedDigest, bool) { - if node == "g14" { - return ComposedDigest{Judgement: JudgedNeither}, true - } - return composedEverywhere(node, fp) - } - }, want: mwc.Want{Fp: fNew, Self: true}}, - // A-430a: a placed node's want held at F by the rules names F's release, as a moved want does (novox's want of - // mesh-delivery-pipeline stood as {fp, self}, the hold having rebuilt it without its version). - {name: "placed, held at F, names its release", before: fNew, set: func(m *Module, n *Nodes) { - atF(m, n) - m.Held = true - m.Self = func(node string) bool { return node == "g14" } - m.KeptBuilds = func(fp string) (KeptBuild, bool) { - return KeptBuild{Parts: map[string]string{"mail:image": "sha256:1"}, KeptOn: "g14", Version: "1.2.3"}, fp == fNew - } - }, rule: mwc.RuleNotHeld, want: mwc.Want{Fp: fNew, Self: true, Version: "1.2.3", KeptOn: "g14", Yes: dig(fNew), - Words: "mail is held at " + short(fNew) + " and g14 ran no other fingerprint healthy to set it back to"}}, - } { - m, n := ready() - c.set(&m, &n) - before := selfAt(c.before) - if c.name == "placed" { - before["g14"] = mwc.Want{Fp: fNew, Composed: dig(fNew)} - } - out := Wants(m, heardOfHaves(n), before, nil) - if got := out.Wants["g14"]; !reflect.DeepEqual(got, c.want) { - t.Errorf("%s: g14's want = %+v, want %+v", c.name, got, c.want) - } - if got := out.Rules["g14"].Rule; got != c.rule { - t.Errorf("%s: g14 is held by rule %d (%s), want %d", c.name, got, out.Rules["g14"].Words, c.rule) - } - if asked := len(out.Record) == 1 && out.Record[0].Node == "g14"; asked != c.asked { - t.Errorf("%s: record asks %+v", c.name, out.Record) - } - if w, ok := out.Wants["g14"]; ok { - if _, err := mwc.EncodeWant(w); err != nil { - t.Errorf("%s: g14's want does not encode: %v", c.name, err) - } - } - } - - // Rule 6 per node: a grant written for the module's other nodes does not grant a node whose account it does not name - // (issue 1945, live on 2026-10-25 at 20:04: ace's mesh-build-go was wanted with no account on the bus). - m, n := ready() - grant := grantsstate.Grant{Accounts: []string{"ace.mail", "ace.mail.send"}} - m.GrantedOn = func(node string) bool { return GrantNamesNode(grant, node, "mail") } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if w := out.Wants["ace"]; w.Fp != fNew { - t.Errorf("ace, whose account the grant names, wants %+v", w) - } - if got := out.Rules["g14"]; got.Rule != mwc.RuleGrant || !strings.Contains(got.Words, "on g14") { - t.Errorf("g14, whose account the grant does not name, is held by %+v", got) - } - for _, c := range []struct { - accounts []string - node, module string - named bool - }{ - {nil, "g14", "mail", true}, - {[]string{"g14.mail.send"}, "g14", "mail", true}, - {[]string{"g14.mailer"}, "g14", "mail", false}, - {[]string{"ace.mail"}, "g14", "mail", false}, - } { - if got := GrantNamesNode(grantsstate.Grant{Accounts: c.accounts}, c.node, c.module); got != c.named { - t.Errorf("%v names %s of %s: %v, want %v", c.accounts, c.module, c.node, got, c.named) - } - } } -// 1297/B4, ADR 0435 §3: a canary that is offline is passed over: the next node by name is the canary, and moves first. -func TestAnOfflineCanaryIsPassedOver(t *testing.T) { - m, n := ready() - n.Online["ace"] = false - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - n.Providers["database"]["store"] = []string{"ace", "g14"} - n.Haves["g14"]["store"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf('s'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - if c := Canary(m.Nodes, n.Online); c != "g14" { - t.Fatalf("canary = %q, want g14", c) - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - if out.Wants["g14"].Fp != fNew { - t.Errorf("the canary g14's want = %+v", out.Wants["g14"]) - } - if out.Rules["ace"].Rule != mwc.RuleCanary { - t.Errorf("ace, behind the canary g14, is held back by %+v", out.Rules["ace"]) - } -} - -// 1297/B4: a failed health check of F holds F and sets the nodes that want it back to their last healthy fingerprint. -func TestAFailedHealthHoldsAndSetsBack(t *testing.T) { - m, n := ready() - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "connection refused"} - m.Now = time.Date(2026, 10, 25, 1, 0, 0, 0, time.UTC) - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}, "g14": {Fp: fOld, Composed: dig(fOld)}}, map[string]string{"ace": fOld}) - // 1906/H3: the held record keeps its cause in words: which machine (its node), which health, when, and the node's words. - if out.Held == nil || out.Held.Node != "ace" || out.Held.Words != "health failed, phase running, seen at 2026-10-25T01:00:00Z: connection refused" { - t.Fatalf("held = %+v", out.Held) - } - if w := out.Wants["ace"]; w.Fp != fOld || w.Composed != dig(fOld) || w.Newest != fNew || w.Rule != mwc.RuleNotHeld { - t.Errorf("ace's want = %+v, want set back to the last healthy fingerprint", w) - } - if w, ok := out.Wants["g14"]; ok && w.Fp == fNew { - t.Errorf("g14 moved to a held fingerprint: %+v", w) - } - - // 1496, ADR 0557 §1, §2: the same failure while F runs healthy on another node is that node's: F is not held, nothing is set - // back, the failing node keeps its want, and the outcome names it (on ace a newly assigned module's first check held a version healthy for an hour on three nodes). - n.Haves["g14"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - before := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}, "g14": {Fp: fNew, Composed: dig(fNew)}} - out = Wants(m, heardOfHaves(n), before, map[string]string{"ace": fOld, "g14": fOld}) - if out.Held != nil || len(out.Wants) != 0 { - t.Fatalf("held %+v, wants %+v: want no hold and no want changed", out.Held, out.Wants) - } - if f := out.NodeFailed["ace"]; !slices.Equal(f.Others, []string{"g14"}) || f.Words != "health failed, phase running: connection refused" || - len(out.NodeFailed) != 1 { - t.Errorf("node failed = %+v", out.NodeFailed) - } - - // ADR 0557 §2 (version 9): while one node fails F alone, novox, whose want names fOld, gets no new want naming F; the wants - // at F stay. The canary ace runs F healthy here and g14 fails it, so the canary rule (3) would let novox move: only §2 holds it. - three := m - three.Nodes = []string{"ace", "g14", "novox"} - failedHave := n.Haves["ace"]["mail"] - n.Haves["ace"]["mail"], n.Haves["g14"]["mail"] = n.Haves["g14"]["mail"], failedHave - n.Online["novox"] = true - n.Haves["novox"] = map[string]nodeengine.HaveByNodeModuleValue{"mail": {Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}} - before["novox"] = mwc.Want{Fp: fOld, Composed: dig(fOld)} - out = Wants(three, heardOfHaves(n), before, map[string]string{"ace": fOld, "g14": fOld, "novox": fOld}) - if _, alone := out.NodeFailed["g14"]; !alone { - t.Errorf("g14 does not fail alone: %+v", out.NodeFailed) - } - if w, written := out.Wants["novox"]; out.Held != nil || len(out.NodeFailed) != 1 || (written && w.Fp == fNew) || out.Rules["novox"].Rule != mwc.RuleNotHeld { - t.Errorf("held %+v, node failed %+v, novox's want %+v (%+v): want novox kept off F", out.Held, out.NodeFailed, out.Wants["novox"], out.Rules["novox"]) - } - if _, written := out.Wants["g14"]; written { - t.Errorf("g14's want at F was rewritten: %+v", out.Wants["g14"]) - } - // A node that runs F healthy with no want, while g14 fails it alone, gets its want at F and is on no neither list (issue 1945). - n.Haves["novox"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - delete(before, "novox") - out = Wants(three, heardOfHaves(n), before, map[string]string{"ace": fOld, "g14": fOld, "novox": fOld}) - if w := out.Wants["novox"]; w.Fp != fNew || len(out.Neither) != 0 { - t.Errorf("novox running F with no want: want %+v, neither %+v; want its want at F and no neither", w, out.Neither) - } - n.Haves["ace"]["mail"], n.Haves["g14"]["mail"] = failedHave, n.Haves["ace"]["mail"] - - // ADR 0557 §2: a witness that is offline keeps the have it last wrote and witnesses nothing (D1); F failing on a second node is - // not one node's failure (D3). Either way F is held and ace is set back. - for _, c := range []struct { - name string - online map[string]bool - novox string - }{ - {"the witness sleeps", map[string]bool{"ace": true, "g14": false, "novox": true}, nodeengineclient.HealthOK}, - {"F fails on a second node", map[string]bool{"ace": true, "g14": true, "novox": true}, nodeengineclient.HealthFailed}, - } { - n.Online = c.online - n.Haves["novox"] = map[string]nodeengine.HaveByNodeModuleValue{"mail": {Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: c.novox}} - if c.novox == nodeengineclient.HealthOK { - n.Haves["novox"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - } - before["novox"] = mwc.Want{Fp: fNew, Composed: dig(fNew)} - out = Wants(three, heardOfHaves(n), before, map[string]string{"ace": fOld, "g14": fOld, "novox": fOld}) - if out.Held == nil || out.Held.Node != "ace" || len(out.NodeFailed) != 0 { - t.Errorf("%s: held %+v, node failed %+v: want F held at ace", c.name, out.Held, out.NodeFailed) - } - if w := out.Wants["ace"]; w.Fp != fOld { - t.Errorf("%s: ace's want = %+v, want set back", c.name, w) - } - } -} - -// 1297/B4: a module holding data is not set back; its want stays and says why. -func TestADataModuleIsNotSetBack(t *testing.T) { - m, n := ready() - m.Manifest.Data = &catalogue.Data{Own: []catalogue.DataItem{{ID: "mail", Class: catalogue.ClassIrreplaceable}}} - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fNew}}, map[string]string{"ace": fOld}) - if out.Held == nil { - t.Fatal("F was not held") - } - if w := out.Wants["ace"]; w.Fp != fNew || !strings.Contains(w.Words, "holds data") { - t.Errorf("ace's want = %+v, want it left at F, saying it holds data", w) - } - // 1906/D3: and the node is to be asked whether to restore it - if !slices.Equal(out.Restore, []string{"ace"}) { - t.Errorf("restore asks = %v, want ace", out.Restore) - } -} - -// 1906/D3: a module without data is set back and asked about nothing. -func TestAModuleWithoutDataIsNotAskedToRestore(t *testing.T) { - m, n := ready() - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fNew}}, map[string]string{"ace": fOld}) - if len(out.Restore) != 0 { - t.Errorf("restore asks = %v", out.Restore) - } -} - -// 1297/B4, ADR 0408 §4: a node that leaves the module's nodes gets its want written removed, never deleted; a want that -// is already as the rules give it is not written again. -func TestALeavingNodeIsRemovedAndNothingIsWrittenTwice(t *testing.T) { - m, n := ready() - m.Nodes = []string{"ace"} - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {V: 1, Fp: fNew, Composed: dig(fNew)}, "g14": {Fp: fOld, Composed: dig(fOld)}}, nil) - if w := out.Wants["g14"]; !w.Removed { - t.Errorf("g14's want = %+v, want removed", w) - } - if _, ok := out.Wants["ace"]; ok { - t.Errorf("ace's want, unchanged, was written again: %+v", out.Wants["ace"]) - } -} - -// 1906/D2 (ADR 0539 §6): the seventh rule. A want waits for the composition of the module for that node at F, and names its -// digest once it exists; a node with no want before still gets none until then. -func TestAWantWaitsForItsCompositionAndNamesItsDigest(t *testing.T) { - m, n := ready() - have := map[string]bool{} - m.Composed = func(node, fp string) (ComposedDigest, bool) { - if !have[node+"|"+fp] { - return ComposedDigest{}, false - } - return composedEverywhere(node, fp) - } - out := Wants(m, heardOfHaves(n), wantedOld(), nil) - for node, w := range out.Wants { - if w.Fp == fNew { - t.Errorf("%s's want moved to F before its composition exists: %+v", node, w) - } - } - if r := out.Rules["ace"]; r.Rule != mwc.RuleComposed || !strings.Contains(r.Words, "ace") { - t.Errorf("ace is held back by %+v, want rule 7 naming the node", r) - } - // and a node that never had a want gets none - if out := Wants(m, heardOfHaves(n), nil, nil); len(out.Wants) != 0 { - t.Errorf("wants without a composition: %+v", out.Wants) - } - have["ace|"+fNew] = true - out = Wants(m, heardOfHaves(n), wantedOld(), nil) - if w := out.Wants["ace"]; w.Fp != fNew || w.Composed != dig(fNew) { - t.Errorf("ace's want = %+v, want F naming the composition's digest", w) - } - if w, ok := out.Wants["g14"]; ok && w.Fp == fNew { - t.Errorf("g14, whose composition does not exist, moved: %+v", w) - } -} - -// 1906/D2 (ADR 0539 §5): a new revision of the composition at the same fingerprint reaches the node only through a new want, -// by the same rules: written when they hold, and not while one does not. -func TestARevisionAtTheSameFingerprintIsAWantByTheSameRules(t *testing.T) { - m, n := ready() - revised := "sha256:" + strings.Repeat("9", 64) - m.Composed = func(node, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: revised, Judgement: JudgedNeither}, true - } - running := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}, "g14": {Fp: fNew, Composed: dig(fNew)}} - out := Wants(m, heardOfHaves(n), running, nil) - for _, node := range []string{"ace", "g14"} { - if w := out.Wants[node]; w.Fp != fNew || w.Composed != revised { - t.Errorf("%s's want = %+v, want F naming the revised digest", node, w) - } - } - m.Granted = false - out = Wants(m, heardOfHaves(n), running, nil) - if w, ok := out.Wants["ace"]; ok && w.Composed == revised { - t.Errorf("a revision was written while a rule holds: %+v", w) - } - - // A want held back at an earlier fingerprint follows the new revision of that fingerprint's kept composition, which the - // controller composed again with today's config (novox/hq ADR 0545: on g14 a capacity of 0 after a rollback), while - // the rules still hold F; for a record module whose revision recreates a container, only after the ask. - out = Wants(m, heardOfHaves(n), wantedOld(), nil) - if w := out.Wants["g14"]; w.Fp != fOld || w.Composed != revised || w.Newest != fNew { - t.Errorf("g14, held back at its earlier fingerprint, wants %+v; want it at that fingerprint naming the revised digest", w) - } - m.Composed = func(node, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: revised, Judgement: JudgedRecreates, Policy: catalogue.PolicyRecord, - Against: dig(fOld), Words: "recreates"}, true - } - out = Wants(m, heardOfHaves(n), wantedOld(), nil) - if w := out.Wants["g14"]; w.Composed == revised { - t.Errorf("a recorded module's revision that recreates a container was written with no ask: %+v", w) - } - if !slices.ContainsFunc(out.Record, func(r RecordNeed) bool { return r.Node == "g14" && r.Digest == revised }) { - t.Errorf("the revision was not asked: %+v", out.Record) - } -} - -// 1906/D2: a set-back names the composition of the fingerprint it sets back to, and when that is gone the want stays and says so. -func TestASetBackNamesTheOlderCompositionOrStays(t *testing.T) { - m, n := ready() - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "x"} - running := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}} - m.Composed = func(node, fp string) (ComposedDigest, bool) { - if fp == fOld { - return ComposedDigest{}, false - } - return composedEverywhere(node, fp) - } - out := Wants(m, heardOfHaves(n), running, map[string]string{"ace": fOld}) - if w := out.Wants["ace"]; w.Fp == fOld { - t.Errorf("set back to a fingerprint whose composition is gone: %+v", w) - } - m.Composed = composedEverywhere - out = Wants(m, heardOfHaves(n), running, map[string]string{"ace": fOld}) - if w := out.Wants["ace"]; w.Fp != fOld || w.Composed != dig(fOld) { - t.Errorf("set-back want = %+v", w) - } -} - -// movingTo is a module ready to move, whose composition at F is judged j against the one it runs, and whose digest is dig(F). -func movingTo(j Judgement) (Module, Nodes, map[string]mwc.Want) { - m, n := ready() - m.Composed = func(node, fp string) (ComposedDigest, bool) { - d := ComposedDigest{Digest: dig(fp), Judgement: j} - if j != JudgedNeither { - d.Against, d.Words = dig(fOld), "judged" - } - return d, true - } - return m, n, wantedOld() -} - -func recording(m *Module) { - m.Manifest.Upgrade = &catalogue.UpgradePolicy{Policy: catalogue.PolicyRecord, Why: "a person takes each build"} -} - -// 1906/D4 (ADR 0539 §14): a module of policy record whose move is judged to recreate a container, to move data, or cannot be -// judged, is not written until its ask is answered yes; the same at a new revision of the same fingerprint. -func TestARecordModuleWaitsForItsAskWhenTheMoveIsNotJudgedNeither(t *testing.T) { - for _, j := range []Judgement{JudgedRecreates, JudgedMovesData, JudgedCannot} { - m, n, wanted := movingTo(j) - recording(&m) - out := Wants(m, heardOfHaves(n), wanted, nil) - if w, ok := out.Wants["ace"]; ok { - t.Errorf("%s: ace's want was written without the ask's yes: %+v", j, w) - } - var ace *RecordNeed - for i := range out.Record { - if out.Record[i].Node == "ace" { - ace = &out.Record[i] - } - } - if ace == nil || ace.Digest != dig(fNew) || ace.Judgement != j { - t.Errorf("%s: the asks = %+v, want one for ace naming the digest and the judgement", j, out.Record) - } - // a new revision at the same fingerprint is asked too - same := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fOld)}} - if out := Wants(m, heardOfHaves(n), same, nil); len(out.Record) == 0 || len(out.Wants) != 0 && out.Wants["ace"].Composed == dig(fNew) { - t.Errorf("%s: a new revision at the same fingerprint: %+v", j, out) - } - m.Allowed = func(node, digest string) bool { return node == "ace" && digest == dig(fNew) } - out = Wants(m, heardOfHaves(n), wanted, nil) - if w := out.Wants["ace"]; w.Fp != fNew || w.Composed != dig(fNew) { - t.Errorf("%s: after the yes ace's want = %+v", j, w) - } - } -} - -// 1906/D4: a move judged neither is written at once, whatever the policy; so is a module the node never ran; and a module -// that rolls (not record) is never asked. -func TestAMoveJudgedNeitherOrNeverRunOrNotRecordedIsWrittenAtOnce(t *testing.T) { - m, n, wanted := movingTo(JudgedNeither) - recording(&m) - if out := Wants(m, heardOfHaves(n), wanted, nil); out.Wants["ace"].Fp != fNew || len(out.Record) != 0 { - t.Errorf("neither: %+v", out) - } - m, n, _ = movingTo(JudgedRecreates) - recording(&m) - m.Nodes = []string{"ace"} // g14 runs it with no want, so its first want is asked (1945/AC19): this case is ace's alone - delete(n.Haves["ace"], "mail") - if out := Wants(m, heardOfHaves(n), nil, nil); out.Wants["ace"].Fp != fNew || len(out.Record) != 0 { - t.Errorf("a node that never ran it: %+v", out) - } - // but one that runs an older fingerprint with no want yet is asked like any other (one running F is not: N1 below) - m, n, _ = movingTo(JudgedCannot) - recording(&m) - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - if out := Wants(m, heardOfHaves(n), nil, nil); out.Wants["ace"].Fp == fNew || len(out.Record) == 0 { - t.Errorf("a node that runs it with no want: %+v", out) - } - m, n, wanted = movingTo(JudgedRecreates) - if out := Wants(m, heardOfHaves(n), wanted, nil); out.Wants["ace"].Fp != fNew || len(out.Record) != 0 { - t.Errorf("a rolled module: %+v", out) - } -} - -// 1906/D4 (ADR 0539 §7, §15): the bus module's new fingerprint is never written by the assigner, but a revision of its -// composition at the same fingerprint that only changes the user list (judged neither) is written at once. -func TestTheBusMovesByTheRulesAndItsUserListRevisionIsWrittenAtOnce(t *testing.T) { - m, n, _ := movingTo(JudgedNeither) - m.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}} - // ADR 0601 §1, §2: the bus module's want is written by the rules, as any module's, and no snapshot is asked for it: its node - // backs its streams up before it applies. - if out := Wants(m, heardOfHaves(n), wantedOld(), nil); out.Wants["ace"].Fp != fNew || len(out.Record) != 0 || len(out.Neither) != 0 { - t.Errorf("the bus did not move by the rules: %+v", out) - } - running := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fOld)}, "g14": {Fp: fNew, Composed: dig(fOld)}} - out := Wants(m, heardOfHaves(n), running, nil) - if w := out.Wants["ace"]; w.Fp != fNew || w.Composed != dig(fNew) { - t.Errorf("a user-list revision of the bus: ace's want = %+v", w) - } - if len(out.Record) != 0 { - t.Errorf("a user-list revision was asked: %+v", out.Record) - } - // ADR 0546: a put owns the node's want from its asking until it ends. While it waits the rules write nothing there; once - // wanted its want stands, marked put and held, and a held bus still takes its user-list revision (ADR 0539 §7). +// ADR 0546: a put owns the node's want from its asking until it ends; while it waits the rule writes nothing there, once wanted its +// want stands, marked put and held, and once released the rule writes the passed version again. +func TestAPutOwnsItsNodesWant(t *testing.T) { + m := released() put := puts.Put{FP: fNew, Hold: true, Why: "the bus's user list", Composed: dig(fNew)} for _, c := range []struct { name string @@ -834,11 +100,11 @@ func TestTheBusMovesByTheRulesAndItsUserListRevisionIsWrittenAtOnce(t *testing.T want *mwc.Want // nil: nothing written on ace }{ {"waiting for the operator", puts.Person, wantedOld(), nil}, - {"wanted", puts.Wanted, wantedOld(), &mwc.Want{Fp: fNew, Composed: dig(fNew), Put: true, Hold: true, Words: "put: the bus's user list"}}, + {"wanted", puts.Wanted, wantedOld(), &mwc.Want{Fp: fNew, Put: true, Hold: true, Words: "put: the bus's user list"}}, {"holding, written already", puts.Holding, map[string]mwc.Want{"ace": PutWant(put)}, nil}, - {"released: the rules again", puts.Released, map[string]mwc.Want{"ace": PutWant(put)}, &mwc.Want{Fp: fNew, Composed: dig(fNew)}}, - {"not heard: a held want at an older fingerprint stands", "", map[string]mwc.Want{"ace": {Fp: fOld, Composed: dig(fOld), Put: true, Hold: true, Words: "put: back"}}, nil}, - {"not heard: a held want the rules would rewrite stands", "", map[string]mwc.Want{"ace": PutWant(put)}, nil}, + {"released: the rule again", puts.Released, map[string]mwc.Want{"ace": PutWant(put)}, &mwc.Want{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2}}, + {"not heard: a held want at an older fingerprint stands", "", map[string]mwc.Want{"ace": {Fp: fOld, Put: true, Hold: true, Words: "put: back"}}, nil}, + {"not heard: a held want the rule would rewrite stands", "", map[string]mwc.Want{"ace": PutWant(put)}, nil}, } { p := put p.Step = c.step @@ -846,30 +112,28 @@ func TestTheBusMovesByTheRulesAndItsUserListRevisionIsWrittenAtOnce(t *testing.T if c.step == "" { m.Puts = nil } - out := Wants(m, heardOfHaves(n), c.wanted, nil) - got, wrote := out.Wants["ace"] + got, wrote := Wants(m, c.wanted).Wants["ace"] switch { case c.want == nil && wrote: - t.Errorf("put %s: the rules wrote ace's want %+v", c.name, got) + t.Errorf("put %s: the rule wrote ace's want %+v", c.name, got) case c.want != nil && (!wrote || !mwc.SameWant(normal(got), *c.want)): t.Errorf("put %s: ace's want = %+v (written %v), want %+v", c.name, got, wrote, *c.want) } } - userList := "sha256:" + strings.Repeat("7", 64) - m.Composed = func(node, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: userList, Judgement: JudgedNeither, UserListOnly: true}, true - } - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: dig(fNew), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - held := put - held.Step = puts.Holding - m.Puts = map[string]puts.Put{"ace": held} - if w := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": PutWant(held)}, nil).Wants["ace"]; w.Composed != userList || !w.Put || !w.Hold { - t.Errorf("a held bus did not take its user-list revision as a put: %+v", w) - } - m.Puts = nil stepsOfAPut(t) } +// §520: health is read from the node-engine seat's `running` alone, and a build of which nothing was heard is not healthy. +func TestHealthIsReadFromRunningAlone(t *testing.T) { + yes, no := true, false + n := Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"ace": {"mail": {Fp: fNew, Phase: nodeengineclient.PhaseFailed, Health: nodeengineclient.HealthFailed}}}, + Running: map[string]map[string]nodeengine.Running{"ace": {"mail": {Fingerprint: fNew, Healthy: &yes}}, "g14": {"mail": {Fingerprint: fNew, Healthy: &no}}}} + if !n.RunsHealthy("ace", "mail", fNew) || n.RunsHealthy("g14", "mail", fNew) || n.RunsHealthy("ace", "mail", fOld) || n.RunsHealthy("novox", "mail", fNew) { + t.Errorf("health read from running: ace %v, g14 %v, ace at fOld %v, novox %v", n.RunsHealthy("ace", "mail", fNew), + n.RunsHealthy("g14", "mail", fNew), n.RunsHealthy("ace", "mail", fOld), n.RunsHealthy("novox", "mail", fNew)) + } +} + // stepsOfAPut takes a put through its steps (ADR 0546 clauses 3 to 8): the build, the controller's composition, the // operator's yes, the bus's snapshot, the want, and the have. func stepsOfAPut(t *testing.T) { @@ -1004,745 +268,3 @@ func stepsOfAPut(t *testing.T) { } } } - -// 1906/D4 (ADR 0539 §16): the answer to a failed backup stays in the want while its target stays. -func TestTheBackupAnswerStaysInTheWantWhileItsTargetStays(t *testing.T) { - m, n := ready() - running := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew), Backup: mwc.BackupSkip, Words: BackupAnswerWords("skip", 0)}} - if w, ok := Wants(m, heardOfHaves(n), running, nil).Wants["ace"]; ok { - t.Errorf("the answer was erased: %+v", w) - } - // A placed node composes its own: its want names the build alone, and the answer stays in it all the same (the digest its - // have names is not in the want, so a want kept for its digest would erase the answer at every pass). - m.Self = func(string) bool { return true } - placed := map[string]mwc.Want{"ace": {Fp: fNew, Self: true, Backup: mwc.BackupSkip, Words: BackupAnswerWords("skip", 0)}} - if w, ok := Wants(m, heardOfHaves(n), placed, nil).Wants["ace"]; ok { - t.Errorf("the answer in a placed node's want was erased: %+v", w) - } -} - -// 1906/D4 (ADR 0539 §19): a hold released by the operator is not held again by a health check that failed before, and the -// wants move by the rules again. -func TestAReleasedHoldIsNotHeldAgain(t *testing.T) { - m, n := ready() - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed, Detail: "x"} - m.Released = true - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fOld, Composed: dig(fOld), Newest: fNew, Rule: mwc.RuleNotHeld}}, map[string]string{"ace": fOld}) - if out.Held != nil { - t.Errorf("a released fingerprint was held again: %+v", out.Held) - } - if w := out.Wants["ace"]; w.Fp != fNew || w.Composed != dig(fNew) || w.Rule != 0 { - t.Errorf("ace's want = %+v, want the held fingerprint written again", w) - } -} - -// 1906/D4: a data module whose restore ask the operator answered "restore" is set back like any other, losing the newer writes; -// answered "leave stopped" (or not answered) it stays. -func TestARestoreAnswerSetsADataModuleBack(t *testing.T) { - m, n := ready() - m.Manifest.Data = &catalogue.Data{Own: []catalogue.DataItem{{ID: "mail", Class: catalogue.ClassIrreplaceable}}} - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - running := map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}} - m.Restored = func(node string) bool { return false } - if w, ok := Wants(m, heardOfHaves(n), running, map[string]string{"ace": fOld}).Wants["ace"]; ok && w.Fp == fOld { - t.Errorf("set back without the operator's restore: %+v", w) - } - m.Restored = func(node string) bool { return node == "ace" } - if w := Wants(m, heardOfHaves(n), running, map[string]string{"ace": fOld}).Wants["ace"]; w.Fp != fOld || w.Composed != dig(fOld) { - t.Errorf("after restore ace's want = %+v", w) - } -} - -// 1906/D4 (ADR 0539 §14): the policy the controller holds beside the composition decides, the operator's own choice included; -// a composition with none (made by hand) falls back to what the manifest derives. -func TestTheRecordPolicyIsTheOneTheControllerHolds(t *testing.T) { - m, n, wanted := movingTo(JudgedMovesData) - with := func(policy, from string) { - inner := m.Composed - m.Composed = func(node, fp string) (ComposedDigest, bool) { - d, ok := inner(node, fp) - d.Policy, d.PolicyFrom = policy, from - return d, ok - } - } - with(catalogue.PolicyRecord, "person") // a roll module the operator set to record - if out := Wants(m, heardOfHaves(n), wanted, nil); len(out.Record) == 0 || out.Wants["ace"].Fp == fNew { - t.Errorf("the operator's record policy was not honoured: %+v", out) - } - recording(&m) // the manifest says record, the operator chose roll - with("roll", "person") - if out := Wants(m, heardOfHaves(n), wanted, nil); len(out.Record) != 0 || out.Wants["ace"].Fp != fNew { - t.Errorf("the operator's roll policy was not honoured: %+v", out) - } - with("", "") // made by hand: the manifest decides - if out := Wants(m, heardOfHaves(n), wanted, nil); len(out.Record) == 0 { - t.Errorf("a hand-made composition fell through the manifest's record policy: %+v", out) - } - // 1953/D4 (ADR 0565 §7): a person's choice at the assigner is over the controller's beside the composition. - with(catalogue.PolicyRecord, "person") - m.Chosen = &catalogue.PersonPolicy{V: catalogue.PersonPolicyVersion, Module: m.Name, Policy: catalogue.PolicyRoll, Why: "w", By: "op"} - if out := Wants(m, heardOfHaves(n), wanted, nil); len(out.Record) != 0 || out.Wants["ace"].Fp != fNew { - t.Errorf("a person's roll at the assigner was not honoured over the controller's record: %+v", out) - } -} - -// A node that runs the bus with no readable want gets its want by the rules, at the fingerprint it runs or at a new one alike: the -// bus has no bar of its own (ADR 0601 §1). -func TestTheBusWithNoWantIsComparedWithWhatItRuns(t *testing.T) { - m, n, _ := movingTo(JudgedNeither) - m.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}} - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - if w, ok := Wants(m, heardOfHaves(n), nil, nil).Wants["ace"]; !ok || w.Fp != fNew { - t.Errorf("the bus runs F with no want and was not given one: %+v %v", w, ok) - } - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - if w, ok := Wants(m, heardOfHaves(n), nil, nil).Wants["ace"]; !ok || w.Fp != fNew { - t.Errorf("the bus running an older fingerprint with no want was not given its want by the rules: %+v %v", w, ok) - } -} - -// Review round 1, item 1 (ADR 0539 §7): a revision of the bus module's composition that differs only in its user list is written -// at once, at the fingerprint the node runs, before the record hold, whatever the controller judges of the file's mount (it -// says recreates) and whatever policy the bus has (record, from the bus). The shape is the controller's: judged recreates against -// the composition the node runs, policy record, UserListOnly set. -func TestAUserListOnlyRevisionOfTheBusIsWrittenAtOnceBeforeTheRecordHold(t *testing.T) { - m, n, _ := movingTo(JudgedRecreates) - m.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}} - userList := "sha256:" + strings.Repeat("7", 64) - m.Composed = func(node, fp string) (ComposedDigest, bool) { - d := ComposedDigest{Digest: dig(fp), Judgement: JudgedRecreates, Against: dig(fOld), Words: "the user list file is mounted", - Policy: catalogue.PolicyRecord, PolicyFrom: "the bus"} - if fp == fOld { - d.Digest, d.UserListOnly = userList, true // the composition at the fingerprint the node runs, with the new user list - } - return d, true - } - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - for name, wanted := range map[string]map[string]mwc.Want{ - "a want at the fingerprint it runs": {"ace": {Fp: fOld, Composed: dig(fOld)}}, - "no readable want": nil, - } { - out := Wants(m, heardOfHaves(n), wanted, nil) - if w := out.Wants["ace"]; w.Fp != fOld || w.Composed != userList { - t.Errorf("%s: ace's want = %+v, want the running fingerprint naming the user-list revision", name, w) - } - if len(out.Record) != 0 { - t.Errorf("%s: the user-list revision was asked: %+v", name, out.Record) - } - } - // the same flag on a module that is not the bus changes nothing - m.Manifest.Provides = nil - if out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fOld, Composed: dig(fOld)}}, nil); out.Wants["ace"].Composed == userList { - t.Errorf("a module that is not the bus was written at once: %+v", out.Wants["ace"]) - } -} - -// Review round 2, N1 (ADR 0539 §9, §11): right after the switch no node has a readable want and every have comes from the old node-engine -// with a fingerprint and no composition, so every move is judged cannot-judge. A node running F with such a have gets its want at F -// with no record hold, for a record module and for the bus alike (the node agent applies nothing when the composition equals the -// declaration it kept); a new fingerprint still waits for its ask. -func TestAHaveFromBeforeTheSwitchGetsItsWantWithNoRecordHold(t *testing.T) { - for name, bus := range map[string]bool{"a record module": false, "the bus": true} { - // only a composition the controller judged unchanged (it equals the declaration the node last applied, ADR 0540 §9) skips the hold - unchanged := func(node, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedUnchanged}, true - } - m, n, _ := movingTo(JudgedCannot) - recording(&m) - m.Nodes = []string{"ace"} - if bus { - m.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}} - } - m.Composed = unchanged - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} // no composition named - out := Wants(m, heardOfHaves(n), nil, nil) - if w := out.Wants["ace"]; w.Fp != fNew || w.Composed != dig(fNew) { - t.Errorf("%s: ace's want = %+v, want F naming its composition", name, w) - } - if len(out.Record) != 0 { - t.Errorf("%s: asked at the switch: %+v", name, out.Record) - } - // a composition that differs from the declaration the node kept (a changed setting or secret), and one the controller did not judge, - // still wait for the ask - for what, j := range map[string]Judgement{"differing": JudgedRecreates, "unjudged": JudgedCannot, "judged neither": JudgedNeither} { - m2, n2, _ := movingTo(j) - recording(&m2) - m2.Nodes = []string{"ace"} - if bus { - m2.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}} - } - n2.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - out := Wants(m2, heardOfHaves(n2), nil, nil) - if j == JudgedNeither { - if out.Wants["ace"].Fp != fNew || len(out.Record) != 0 { - t.Errorf("%s, %s: %+v", name, what, out) - } - continue - } - if w, ok := out.Wants["ace"]; ok || len(out.Record) == 0 { - t.Errorf("%s, %s: written at the switch with no yes: %+v %+v", name, what, w, out.Record) - } - } - // a have that names a composition is judged as before - m.Composed = func(node, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedRecreates, Against: dig(fOld), Words: "judged"}, true - } - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: dig(fOld), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - if out := Wants(m, heardOfHaves(n), nil, nil); out.Wants["ace"].Fp == fNew && len(out.Record) == 0 { - t.Errorf("%s: a have naming a composition skipped the hold: %+v", name, out) - } - // a new fingerprint still asks (the bus is barred instead) - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - out = Wants(m, heardOfHaves(n), nil, nil) - if w, ok := out.Wants["ace"]; ok { - t.Errorf("%s: a new fingerprint was written with no yes: %+v", name, w) - } - if !bus && len(out.Record) == 0 { - t.Errorf("%s: a new fingerprint was not asked", name) - } - } -} - -// 1906/H3: a failed check whose node gave no words is held saying so, never with empty words. -func TestAHoldOfAFailureWithoutWordsSaysSo(t *testing.T) { - m, n := ready() - n.Haves["ace"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"ace": {Fp: fNew, Composed: dig(fNew)}}, map[string]string{"ace": fOld}) - if out.Held == nil || out.Held.Words != "health failed, phase running: the node gave no words" { - t.Fatalf("held = %+v", out.Held) - } -} - -// 1906/H2: given a held fingerprint that every node running the module now runs healthy, when the assigner passes, then the -// hold is lifted by itself and the wants move to it by the rules again. A node set back to another fingerprint, a node whose -// check fails, or a hold the operator answered "keep" leaves it held; a node that does not run the module at all does not count. -func TestAHoldIsLiftedOnceEveryNodeRunsItHealthy(t *testing.T) { - everywhere := func() (Module, Nodes) { - m, n := ready() - m.Held = true - n.Haves["g14"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - return m, n - } - m, n := everywhere() - out := Wants(m, heardOfHaves(n), wantedOld(), map[string]string{"ace": fOld, "g14": fOld}) - if !out.Lifted || out.Held != nil { - t.Fatalf("lifted = %v, held = %+v: want the hold lifted", out.Lifted, out.Held) - } - for _, node := range []string{"ace", "g14"} { - if w := out.Wants[node]; w.Fp != fNew || w.Rule != 0 { - t.Errorf("%s's want = %+v, want it moved to the lifted fingerprint", node, w) - } - } - - m, n = everywhere() - delete(n.Haves["g14"], "mail") - if out := Wants(m, heardOfHaves(n), wantedOld(), nil); !out.Lifted { - t.Error("a node that does not run the module kept the hold") - } - - for name, change := range map[string]func(*Module, *Nodes){ - "set back": func(_ *Module, n *Nodes) { - n.Haves["g14"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - }, - "failing": func(_ *Module, n *Nodes) { - n.Haves["g14"]["mail"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - }, - "kept": func(m *Module, _ *Nodes) { m.Kept = true }, - "no haves": func(_ *Module, n *Nodes) { delete(n.Haves["g14"], "mail"); delete(n.Haves["ace"], "mail") }, - "not held": func(m *Module, _ *Nodes) { m.Held = false }, - } { - m, n := everywhere() - change(&m, &n) - if out := Wants(m, heardOfHaves(n), wantedOld(), map[string]string{"ace": fOld, "g14": fOld}); out.Lifted { - t.Errorf("%s: the hold was lifted", name) - } - } -} - -// x11 is screen-lock as on g14 and shanks on 2026-10-25 (issue 1945): built, granted and composed at F and at the older fOld -// on both, requiring x11-display, whose provider xorg runs on each node with its health unknown; each node runs screen-lock at fOld -// (health unknown) and holds no want for it. The canary g14 does not run F. -func x11() (Module, Nodes) { - m := Module{Name: "screen-lock", FP: fNew, Built: true, Granted: true, Composed: composedEverywhere, Nodes: []string{"g14", "shanks"}, - GrantedAt: func(fp string) bool { return fp == fNew || fp == fOld }, - Manifest: catalogue.Manifest{Module: "screen-lock", Requires: []string{"x11-display"}}} - n := Nodes{ - Online: map[string]bool{"g14": true, "shanks": true}, - Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "g14": {"screen-lock": {Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown}, - "xorg": {Fp: fpOf('x'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown}}, - "shanks": {"screen-lock": {Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown}, - "xorg": {Fp: fpOf('x'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown}}, - }, - Providers: map[string]map[string][]string{"x11-display": {"xorg": {"g14", "shanks"}}}, - } - return m, n -} - -// 1945/AC19: given a node that runs an older fingerprint of the new form, composed and granted, with no want, held back from F by -// the providers or the canary rule, when the assigner passes, then its first want names the fingerprint it runs with that -// composition, never the newest, and says the newest it is held back from; the canary is never passed over. -func TestANodeRunningAModuleWithNoWantGetsItsFirstWantAtWhatItRuns(t *testing.T) { - m, n := x11() - out := Wants(m, heardOfHaves(n), nil, nil) - // 1960: the providers rule holds no want: the canary g14 gets F, its provider's health unknown said - if w := out.Wants["g14"]; w.Fp != fNew || out.Unreached["g14"] == "" { - t.Errorf("g14's want = %+v (unreached %q), want F, the provider said", w, out.Unreached["g14"]) - } - if w := out.Wants["shanks"]; w.Fp != fOld || w.Composed != dig(fOld) || w.Newest != fNew || w.Rule != mwc.RuleCanary { - t.Errorf("shanks's want = %+v, want its first want at the fingerprint it runs, held back from F by the canary", w) - } - if len(out.Neither) != 0 { - t.Errorf("left on neither list: %+v", out.Neither) - } - // with the provider healthy, only the canary holds shanks back: still at what it runs - m, n = x11() - n.Haves["g14"]["xorg"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf('x'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - n.Haves["shanks"]["xorg"] = n.Haves["g14"]["xorg"] - if w := Wants(m, heardOfHaves(n), nil, nil).Wants["shanks"]; w.Fp != fOld || w.Rule != mwc.RuleCanary { - t.Errorf("shanks held back by the canary with no want: %+v, want at fOld by rule 3", w) - } - // a node already running F gets its want at F; a removed want is no want either - m, n = x11() - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - out = Wants(m, heardOfHaves(n), map[string]mwc.Want{"g14": {Removed: true}}, nil) - if w := out.Wants["g14"]; w.Fp != fNew || w.Composed != dig(fNew) || w.Removed { - t.Errorf("g14 running F with a removed want = %+v, want F", w) - } -} - -// 1945/AC19: a node running a fingerprint that is not of the new form, or one whose composition or grant does not exist, gets no -// want past the rules, and is named on neither list; the ordering rules still order every move from a want. -func TestNoFirstWantWithoutTheCompositionAndGrantOfWhatItRuns(t *testing.T) { - for what, change := range map[string]func(*Module, *Nodes){ - "an old-form build": func(_ *Module, n *Nodes) { - n.Haves["shanks"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: "build-2", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - }, - "no grant": func(m *Module, _ *Nodes) { m.GrantedAt = func(fp string) bool { return fp == fNew } }, - "no composition": func(m *Module, _ *Nodes) { - m.Composed = func(_, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedNeither}, fp == fNew - } - }, - } { - m, n := x11() - change(&m, &n) - out := Wants(m, heardOfHaves(n), nil, nil) - if what == "an old-form build" { - // the canary g14 does not run F healthy: no want, on neither list by rule 3 (issue 1945) - if w, ok := out.Wants["shanks"]; ok { - t.Errorf("%s: shanks got a want: %+v", what, w) - } - if h, ok := out.Neither["shanks"]; !ok || h.Rule != mwc.RuleCanary { - t.Errorf("%s: shanks on neither list = %+v (named %v), want rule 3", what, h, ok) - } - continue - } - // 1960: no want can name what it runs, so its first want names F rather than none: never on neither list - if w := out.Wants["shanks"]; w.Fp != fNew { - t.Errorf("%s: shanks's want = %+v, want F", what, w) - } - if h, ok := out.Neither["shanks"]; ok { - t.Errorf("%s: shanks on neither list = %+v", what, h) - } - } - m, n := x11() - before := map[string]mwc.Want{"g14": {Fp: fOld, Composed: dig(fOld)}, "shanks": {Fp: fOld, Composed: dig(fOld)}} - out := Wants(m, heardOfHaves(n), before, nil) - if len(out.Neither) != 0 { - t.Errorf("nodes holding a want named on neither list: %+v", out.Neither) - } - // the canary g14 moves to F (its provider's health no rule); shanks waits for it - if w := out.Wants["shanks"]; w.Fp != "" && w.Fp != fOld { - t.Errorf("shanks's want moved from its want past the canary: %+v", w) - } - if w := out.Wants["g14"]; w.Fp != fNew { - t.Errorf("the canary g14's want = %+v, want F", w) - } -} - -// 1945/AC20: a new assignment (a node with no have of the module) waiting for the canary, the build or the grant gets no want and is -// never named on neither list: only a node that runs the module, or whose have names it, is; a module with no fingerprint of the new -// form goes with the node's own composition and is never named. -func TestANewAssignmentWaitingIsNotOnNeitherList(t *testing.T) { - m, n := x11() - delete(n.Haves["shanks"], "screen-lock") - out := Wants(m, heardOfHaves(n), nil, nil) - // 1960: the canary orders moves, never a first want: the new assignment is wanted at F - if w := out.Wants["shanks"]; w.Fp != fNew { - t.Errorf("a new assignment while the canary does not run F: %+v, want F", w) - } - if _, ok := out.Neither["shanks"]; ok { - t.Errorf("a new assignment is named on neither list: %+v", out.Neither) - } - m.Built = false - if out := Wants(m, heardOfHaves(n), nil, nil); len(out.Neither) != 1 || out.Neither["g14"].Rule != mwc.RuleBuildDone { - t.Errorf("with no build: neither = %+v, want g14 alone (it runs the module), by rule 1", out.Neither) - } - // 1945 (the assigner holds a first want back silently): with no build, a node whose want was removed and that does not run - // the module has the reason written into its removed want, once; a node with no key gets no want written, and a node that - // runs the module is on neither list, its removed want left as it is. - gone := mwc.Want{Removed: true, Words: "screen-lock is no longer assigned to shanks"} - for _, c := range []struct { - name string - before map[string]mwc.Want - want map[string]mwc.Want - }{ - {"no key: nothing written", nil, map[string]mwc.Want{}}, - {"a removed want: it says why", map[string]mwc.Want{"shanks": gone}, - map[string]mwc.Want{"shanks": {Removed: true, Words: "no want: rule 1: the build of screen-lock at " + short(m.FP) + " is being built"}}}, - {"a removed want that says why already: nothing written", map[string]mwc.Want{"shanks": {Removed: true, Words: "no want: rule 1: the build of screen-lock at " + short(m.FP) + " is being built"}}, - map[string]mwc.Want{}}, - {"a node that runs it: on neither list, its removed want left", map[string]mwc.Want{"g14": gone}, map[string]mwc.Want{}}, - } { - out := Wants(m, heardOfHaves(n), c.before, nil) - if len(out.Wants) != len(c.want) { - t.Errorf("%s: wants = %+v, want %+v", c.name, out.Wants, c.want) - continue - } - for node, w := range c.want { - if !mwc.SameWant(normal(out.Wants[node]), w) { - t.Errorf("%s: want of %s = %+v, want %+v", c.name, node, out.Wants[node], w) - } - } - } - m.FP = "" - if out := Wants(m, heardOfHaves(n), nil, nil); len(out.Neither) != 0 { - t.Errorf("a module of the node's own composition named on neither list: %+v", out.Neither) - } -} - -// 1945/AC21: a node whose have names an old-form build (no fingerprint of the new form, so no composition of it) can never be -// named in a want at what it runs; its first want names F, under the canary rule as usual and with everything F needs and the -// providers rule. Given the canary runs F healthy, shanks's first want names F; given it does not yet, shanks gets no want and -// stays on neither list; the providers hold no want (issue 1960). The old-form node that is itself the canary gets F once its providers run healthy. -func TestAnOldFormNodeGetsItsFirstWantAtFOnlyUnderTheCanary(t *testing.T) { - oldForm := func(n Nodes, node string) { - n.Haves[node]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: "build-1792260876956849831", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - } - providersHealthy := func(n Nodes) { - for _, node := range []string{"g14", "shanks"} { - n.Haves[node]["xorg"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf('x'), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - } - } - // the canary g14 runs F healthy: shanks's first want names F - m, n := x11() - providersHealthy(n) - oldForm(n, "shanks") - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - out := Wants(m, heardOfHaves(n), nil, nil) - if w := out.Wants["shanks"]; w.Fp != fNew || w.Composed != dig(fNew) || w.Newest != "" || w.Rule != 0 { - t.Errorf("canary healthy on F: shanks's want = %+v, want F", w) - } - if _, ok := out.Neither["shanks"]; ok { - t.Errorf("canary healthy on F: shanks still on neither list: %+v", out.Neither) - } - // the canary does not run F healthy yet: no want, and shanks stays on neither list by rule 3 - m, n = x11() - providersHealthy(n) - oldForm(n, "shanks") - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - out = Wants(m, heardOfHaves(n), nil, nil) - if w, ok := out.Wants["shanks"]; ok { - t.Errorf("canary not healthy on F: shanks got a want: %+v", w) - } - if h, ok := out.Neither["shanks"]; !ok || h.Rule != mwc.RuleCanary { - t.Errorf("canary not healthy on F: shanks on neither list = %+v (named %v), want rule 3", h, ok) - } - // 1960: the canary healthy on F but the providers not healthy on shanks: F all the same, the provider down said - m, n = x11() - oldForm(n, "shanks") - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - out = Wants(m, heardOfHaves(n), nil, nil) - if w := out.Wants["shanks"]; w.Fp != fNew || out.Unreached["shanks"] == "" { - t.Errorf("providers not healthy: shanks's want = %+v (unreached %q), want F, the provider said", w, out.Unreached["shanks"]) - } - if _, on := out.Neither["shanks"]; on { - t.Errorf("providers not healthy: shanks on neither list: %+v", out.Neither) - } - // F not granted: nothing, even with the canary healthy - m, n = x11() - providersHealthy(n) - oldForm(n, "shanks") - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK} - m.Granted = false - if w, ok := Wants(m, heardOfHaves(n), nil, nil).Wants["shanks"]; ok { - t.Errorf("F not granted: shanks got a want: %+v", w) - } - // the old-form node is the canary itself: F once its providers run healthy - m, n = x11() - providersHealthy(n) - oldForm(n, "g14") - if w := Wants(m, heardOfHaves(n), nil, nil).Wants["g14"]; w.Fp != fNew || w.Composed != dig(fNew) { - t.Errorf("old-form canary: g14's want = %+v, want F", w) - } -} - -// A failed health counts only once the node is done moving: phase running or failed, never backing-up, applying or checking. -// The live row on novox at 15:27:36 on 2026-10-25 ("health failed, phase applying" at the controller's new fingerprint) held the -// canary two seconds before the new build was healthy. -func TestAFailedHealthWhileTheNodeMovesHoldsNothing(t *testing.T) { - for _, phase := range []string{nodeengineclient.PhaseBackingUp, nodeengineclient.PhaseApplying, nodeengineclient.PhaseChecking} { - m := Module{Name: "mesh-controller", FP: fNew, Built: true, Granted: true, Composed: composedEverywhere, Nodes: []string{"novox"}, - Manifest: catalogue.Manifest{Module: "mesh-controller"}} - n := Nodes{Online: map[string]bool{"novox": true}, Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "novox": {"mesh-controller": {Fp: fNew, Phase: phase, Health: nodeengineclient.HealthFailed}}}} - before := map[string]mwc.Want{"novox": {Fp: fNew, Composed: dig(fNew)}} - if out := Wants(m, heardOfHaves(n), before, map[string]string{"novox": fOld}); out.Held != nil { - t.Errorf("phase %s, health failed: held %+v, want no hold while the node moves", phase, out.Held) - } - } - for _, phase := range []string{nodeengineclient.PhaseRunning, nodeengineclient.PhaseFailed} { - m := Module{Name: "mesh-controller", FP: fNew, Built: true, Granted: true, Composed: composedEverywhere, Nodes: []string{"novox"}, - Manifest: catalogue.Manifest{Module: "mesh-controller"}} - n := Nodes{Online: map[string]bool{"novox": true}, Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "novox": {"mesh-controller": {Fp: fNew, Phase: phase, Health: nodeengineclient.HealthFailed}}}} - before := map[string]mwc.Want{"novox": {Fp: fNew, Composed: dig(fNew)}} - if out := Wants(m, heardOfHaves(n), before, map[string]string{"novox": fOld}); out.Held == nil || out.Held.Node != "novox" { - t.Errorf("phase %s, health failed: held %+v, want a hold on novox", phase, out.Held) - } - } -} - -// A hold is lifted by itself once every node that runs the module runs F healthy: a have of a node that is not running the module -// (removed, held for its backup, or still moving) is not counted, so it does not keep the hold up for ever; a node running another -// fingerprint, or failed, still does. -func TestAHoldIsLiftedOverHavesThatAreNotRunning(t *testing.T) { - setup := func(phase string, fp string, health string) (Module, Nodes) { - m := Module{Name: "web", FP: fNew, Built: true, Granted: true, Held: true, Composed: composedEverywhere, Nodes: []string{"ace", "g14"}} - n := Nodes{Online: map[string]bool{"ace": true, "g14": true}, Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{ - "ace": {"web": {Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}}, - "g14": {"web": {Fp: fp, Phase: phase, Health: health}}}} - return m, n - } - for _, phase := range []string{nodeengineclient.PhaseRemoved, nodeengineclient.PhaseHeld, nodeengineclient.PhaseApplying, nodeengineclient.PhaseBackingUp, nodeengineclient.PhaseChecking} { - m, n := setup(phase, fOld, nodeengineclient.HealthUnknown) - if out := Wants(m, heardOfHaves(n), nil, nil); !out.Lifted { - t.Errorf("g14's have in phase %s kept the hold up", phase) - } - } - for what, h := range map[string]nodeengine.HaveByNodeModuleValue{ - "running another fingerprint": {Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}, - "failed": {Fp: fNew, Phase: nodeengineclient.PhaseFailed, Health: nodeengineclient.HealthFailed}, - } { - m, n := setup(h.Phase, h.Fp, h.Health) - if out := Wants(m, heardOfHaves(n), nil, nil); out.Lifted { - t.Errorf("g14 %s: the hold was lifted", what) - } - } - // a failed health on a removed have holds nothing - m, n := setup(nodeengineclient.PhaseRemoved, fNew, nodeengineclient.HealthFailed) - m.Held = false - if out := Wants(m, heardOfHaves(n), nil, nil); out.Held != nil { - t.Errorf("a removed have with a failed health held F: %+v", out.Held) - } -} - -// A module of policy record whose want was removed but which the node still runs gets its first want through the record ask: a -// removed want is no want, so a move the controller judges to recreate or move data waits for the operator's yes. -func TestAFirstWantAfterARemovedWantGoesThroughTheRecordAsk(t *testing.T) { - m, n := x11() - recording(&m) - m.Composed = func(_, fp string) (ComposedDigest, bool) { - return ComposedDigest{Digest: dig(fp), Judgement: JudgedRecreates, Policy: catalogue.PolicyRecord}, true - } - out := Wants(m, heardOfHaves(n), map[string]mwc.Want{"shanks": {Removed: true}}, nil) - if w, ok := out.Wants["shanks"]; ok { - t.Errorf("shanks's first want after a removed want was written with no record ask: %+v", w) - } - asked := false - for _, r := range out.Record { - asked = asked || (r.Node == "shanks" && r.Digest == dig(fOld) && r.Judgement == JudgedRecreates) - } - if !asked { - t.Errorf("the record asks = %+v, want one for shanks at the composition it runs", out.Record) - } -} - -// 1945/AC23: given a module held at F under rule 2 (the operator holds systemd-resolved on shanks, 2026-10-25), or held by the -// assigner in this pass after a failed health check of F, when a node that runs it holds no want for it, then the node is not on -// neither list: a held module is on the held list, whose hold is said and answered on its own. -func TestAHeldModuleWithNoWantIsNotOnNeitherList(t *testing.T) { - for what, change := range map[string]func(*Module, *Nodes){ - "held at F": func(m *Module, _ *Nodes) { m.Held, m.Kept = true, true }, - "held at F, running an old-form build": func(m *Module, n *Nodes) { - m.Held, m.Kept = true, true - n.Haves["shanks"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: "build-2", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - }, - "held by this pass": func(_ *Module, n *Nodes) { - n.Haves["g14"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: fNew, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} - n.Haves["shanks"]["screen-lock"] = nodeengine.HaveByNodeModuleValue{Fp: "build-2", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthUnknown} - }, - } { - m, n := x11() - change(&m, &n) - out := Wants(m, heardOfHaves(n), nil, nil) - if len(out.Neither) != 0 { - t.Errorf("%s: a held module is on neither list: %+v", what, out.Neither) - } - if out.Rules["shanks"].Rule != mwc.RuleNotHeld { - t.Errorf("%s: shanks is held back by %+v, want rule 2", what, out.Rules["shanks"]) - } - } -} - -// Main file *A provider before its consumers* (design 47 G §309): from G a provider runs healthy while the build it runs is -// healthy, as the node-engine seat's `running` says it, whatever a newer want of it waits for (its have's phase waiting-yes). -func TestAProviderRunsHealthyByItsRunning(t *testing.T) { - waiting := nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseWaitingYes, Health: nodeengineclient.HealthOK} - for _, c := range []struct { - name string - running map[string]map[string]nodeengine.Running - ok bool - }{ - {"its new want waits for a yes, its old build runs", map[string]map[string]nodeengine.Running{"novox": {"nats": {Node: "novox", Module: "nats", Fingerprint: fOld}}}, true}, - {"its running build is stopped", map[string]map[string]nodeengine.Running{"novox": {"nats": {Node: "novox", Module: "nats", Stopped: true}}}, false}, - {"it said no running", map[string]map[string]nodeengine.Running{"novox": {}}, false}, - {"its running names no build", map[string]map[string]nodeengine.Running{"novox": {"nats": {Node: "novox", Module: "nats"}}}, false}, - {"the seat's events are not heard: the have says it, and waiting-yes is not running", nil, false}, - } { - n := Nodes{Online: map[string]bool{"novox": true}, Running: c.running, - Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"novox": {"nats": waiting}}, - Providers: map[string]map[string][]string{"bus-state": {"nats": {"novox"}}}} - if ok, words := providedHealthy("bus-state", "novox", n); ok != c.ok { - t.Errorf("%s: healthy = %v (%s), want %v", c.name, ok, words, c.ok) - } - } -} - -// Design 47 G A-430, A-430a (over §525, §526): a want names the module's version, never its artifacts' digests, whichever node it -// is for; a build the assigner knows no version of (another branch's, a kept one) is named by its parts and keeping node, as -// before, for a node whose engine reads them (its record's capabilities or its manifest reads). -func TestAWantNamesAKeptBuildsPartsAndNodeForANodeThatReadsThem(t *testing.T) { - released := KeptBuild{Parts: map[string]string{"mail:image": "sha256:1"}, KeptOn: "g14", Version: "1.2.3"} - unversioned := KeptBuild{Parts: map[string]string{"mail:image": "sha256:1"}, KeptOn: "g14"} - for name, row := range map[string]struct { - reads, capable []string - kept func(string) (KeptBuild, bool) - version string - parts bool - }{ - "released, reads parts": {[]string{WantParts}, nil, func(fp string) (KeptBuild, bool) { return released, fp == fNew }, "1.2.3", true}, - "released, reads none": {[]string{"form:oci"}, nil, func(fp string) (KeptBuild, bool) { return released, fp == fNew }, "1.2.3", false}, - "unversioned, capability": {[]string{"form:oci"}, []string{WantParts}, func(fp string) (KeptBuild, bool) { return unversioned, fp == fNew }, "", true}, - "unversioned, reads none": {[]string{"form:oci"}, nil, func(fp string) (KeptBuild, bool) { return unversioned, fp == fNew }, "", false}, - "build not kept": {[]string{WantParts}, nil, func(string) (KeptBuild, bool) { return KeptBuild{}, false }, "", false}, - "no kept builds": {[]string{WantParts}, nil, nil, "", false}, - "another fingerprint's release": {[]string{WantParts}, nil, func(fp string) (KeptBuild, bool) { return released, fp == fOld }, "", false}, - } { - m, n := ready() - n.Reads = map[string][]string{"ace": row.reads, "g14": row.reads} - n.Capable = map[string][]string{"ace": row.capable} - m.KeptBuilds = row.kept - w := Wants(m, heardOfHaves(n), wantedOld(), nil).Wants["ace"] - if w.Fp != fNew || w.Version != row.version || (len(w.Parts) == 1) != row.parts { - t.Errorf("%s: ace's want = %+v", name, w) - } - } -} - -// heardOfHaves gives each have that runs the `running` the node-engine seat says of it (design 47 G §520: health is read from -// `running` alone), for a fixture written as haves; a fixture that names its own `running` is left as it is. -func heardOfHaves(n Nodes) Nodes { - if n.Running != nil { - return n - } - n.Running = map[string]map[string]nodeengine.Running{} - for node, byModule := range n.Haves { - n.Running[node] = map[string]nodeengine.Running{} - for module, h := range byModule { - if h.Phase == nodeengineclient.PhaseRunning && h.Fp != "" { - ok := h.Health == nodeengineclient.HealthOK - n.Running[node][module] = nodeengine.Running{Node: node, Module: module, Fingerprint: h.Fp, Healthy: &ok} - } - } - } - return n -} - -// §520: health is read from the node-engine seat's `running` alone, whatever the have says, and a build of which nothing was heard -// is not healthy. -func TestHealthIsReadFromRunningAlone(t *testing.T) { - yes, no := true, false - say := func(node, module, fp string, healthy *bool) nodeengine.Running { - return nodeengine.Running{Node: node, Module: module, Fingerprint: fp, Healthy: healthy} - } - for _, c := range []struct { - name string - running map[string]map[string]nodeengine.Running - rule int - }{ - {"all healthy by running", map[string]map[string]nodeengine.Running{"ace": {"mail": say("ace", "mail", fNew, &yes), "store": say("ace", "store", fpOf('s'), &yes)}}, 0}, - {"a running with no healthy reads healthy", map[string]map[string]nodeengine.Running{"ace": {"mail": say("ace", "mail", fNew, nil), "store": say("ace", "store", fpOf('s'), nil)}}, 0}, - {"the canary says unhealthy, its have says healthy", map[string]map[string]nodeengine.Running{"ace": {"mail": say("ace", "mail", fNew, &no), "store": say("ace", "store", fpOf('s'), &yes)}}, mwc.RuleCanary}, - {"nothing heard of the canary", map[string]map[string]nodeengine.Running{"ace": {"store": say("ace", "store", fpOf('s'), &yes)}}, mwc.RuleCanary}, - } { - m, n := ready() - n.Running = c.running - if got := Wants(m, n, wantedOld(), nil).Rules["g14"].Rule; got != c.rule { - t.Errorf("%s: g14 held by rule %d, want %d", c.name, got, c.rule) - } - } - // rule 5: a provider that says unhealthy, or that nothing was heard of, holds no want but leaves the consumer unreached - m, n := ready() - n.Running = map[string]map[string]nodeengine.Running{"ace": {"mail": say("ace", "mail", fNew, &yes), "store": say("ace", "store", fpOf('s'), &no)}} - if out := Wants(m, n, wantedOld(), nil); len(out.Unreached) == 0 { - t.Errorf("a provider saying unhealthy: no module unreached, %+v", out) - } -} - -// §575: a hold under rule 1 says how the build stands: failed with the error whole, or being built. -func TestRuleOneSaysHowTheBuildStands(t *testing.T) { - for _, c := range []struct { - name string - failed bool - err string - want string - }{ - {"a build that runs", false, "", "the build of mail at " + short(fNew) + " is being built"}, - {"a failed build", true, "transport fault: no responders at mesh-build-go", "the build of mail at " + short(fNew) + " failed: transport fault: no responders at mesh-build-go"}, - } { - m, n := ready() - m.Built, m.BuildFailed, m.BuildError = false, c.failed, c.err - held, rule, words := Holds(m, "g14", n) - if held || rule != mwc.RuleBuildDone || words != c.want { - t.Errorf("%s: held %v rule %d words %q, want %q", c.name, held, rule, words, c.want) - } - } -} - -// Design 47 G §266, §268 (AS9), A-426: an entry whose acting edge names a step the assigner holds waits, its want held, only while -// the acting part runs nothing that serves the edge's seat on the edge's node: any build of it serving the seat (an older one, a -// hand start) ends the wait at once, a build not serving it or a stopped part holds it, and a released wait is no edge. -func TestAWantWaitsForItsActingPartsNewVersion(t *testing.T) { - edge := ActingEdge{Node: "g14", Part: "mesh-host", Fp: fNew, Step: "compose", Seat: "node-engine"} - run := func(fp string, stopped bool, serves bool) Nodes { - _, n := ready() - n.Running = map[string]map[string]nodeengine.Running{"g14": {"mesh-host": {Fingerprint: fp, Stopped: stopped, Manifest: "m"}}} - n.Serves = func(nodeengine.Running, string) (bool, bool) { return serves, true } - return n - } - m, _ := ready() - m.Acting = func(string) []ActingEdge { return []ActingEdge{edge} } - m.Nodes = []string{"g14"} - out := Wants(m, run("", true, true), wantedOld(), nil) - if out.Wants["g14"].Fp == fNew || out.Rules["g14"].Rule != mwc.RuleComposed || !strings.Contains(out.Rules["g14"].Words, "waits in compose for mesh-host") { - t.Fatalf("the part stopped: want %+v rule %+v", out.Wants["g14"], out.Rules["g14"]) - } - if out := Wants(m, run(fOld, false, false), wantedOld(), nil); out.Wants["g14"].Fp == fNew { - t.Errorf("an old build not serving the seat runs: want %+v", out.Wants["g14"]) - } - if out := Wants(m, run(fOld, false, true), wantedOld(), nil); out.Wants["g14"].Fp != fNew { - t.Errorf("an old build serving the seat runs: want %+v, want the new version moved", out.Wants["g14"]) - } - if took, runs := run(fOld, false, true).ActingRuns(edge); !runs || took != fOld { - t.Errorf("ActingRuns took %q, %v: want the old build it runs", took, runs) - } - if out := Wants(m, run(fNew, false, true), wantedOld(), nil); out.Wants["g14"].Fp != fNew { - t.Errorf("the new version runs: want %+v", out.Wants["g14"]) - } - m.Acting = func(string) []ActingEdge { return nil } // a released wait is no edge - if out := Wants(m, run("", true, true), wantedOld(), nil); out.Wants["g14"].Fp != fNew { - t.Errorf("released: want %+v", out.Wants["g14"]) - } -} diff --git a/modules/mesh-delivery-assigner/internal/assigngraph/compute.go b/modules/mesh-delivery-assigner/internal/assigngraph/compute.go index b58a63ee..87bc9729 100644 --- a/modules/mesh-delivery-assigner/internal/assigngraph/compute.go +++ b/modules/mesh-delivery-assigner/internal/assigngraph/compute.go @@ -53,9 +53,6 @@ type Computed struct { // Uncomputed is each placed module whose entry could not be computed, as "/: why", sorted: it is said, // never left out in silence. Uncomputed []string - // Cycles are the cycles among the acting waits, within one step or across steps (Cycles, design 47 G §272, A-323): said, never - // left in silence. - Cycles []string } // placedAt is one placed module with what its entry is computed from. @@ -74,7 +71,7 @@ type placedAt struct { optional func(string) bool } -type actingRule struct{ seat, step, on, bound string } +type actingRule struct{ seat, step, on string } // Compute is the assignment graph revision of in: one entry per module placed on a node whose branch's catalog graph // holds it, its version the build the placement names or else the catalog entry's fingerprint (design 47 lines 909-914), @@ -128,7 +125,7 @@ func Compute(in Inputs) Computed { p.langs[lang] = true } for _, r := range ce.Acts { - p.acts = append(p.acts, actingRule{seat: r.Seat, step: r.Step, on: r.On, bound: r.Bound}) + p.acts = append(p.acts, actingRule{seat: r.Seat, step: r.Step, on: r.On}) } p.provides, p.contributes = ce.Provides, ce.Contributes p.delivering = map[string]seatdef.Definition{} @@ -199,7 +196,7 @@ func Compute(in Inputs) Computed { } for _, h := range from { t.entry.Edges = append(t.entry.Edges, meshwants.Edge{Kind: EdgeActing, Node: h.node, Module: part, Fp: h.entry.Fp, - Seat: rule.seat, Step: rule.step, Bound: rule.bound}) + Seat: rule.seat, Step: rule.step}) } } } @@ -217,7 +214,6 @@ func Compute(in Inputs) Computed { rev.Entries = append(rev.Entries, meshwants.NodeEntryRef{Node: p.node, Module: p.placed.Module, Digest: d}) } out.Revision = rev - out.Cycles = Cycles(slices.Collect(maps.Values(out.Entries))) slices.Sort(out.Uncomputed) return out } @@ -229,7 +225,7 @@ func sortEdges(edges []meshwants.Edge) { } func edgeKey(e meshwants.Edge) string { - return strings.Join([]string{e.Kind, e.Node, e.Module, e.Provision, e.Seat, e.Step, e.Fp, e.Bound}, "\x00") + return strings.Join([]string{e.Kind, e.Node, e.Module, e.Provision, e.Seat, e.Step, e.Fp}, "\x00") } // EntryDigest is the digest an entry is stored under: the same entry, the same digest. diff --git a/modules/mesh-delivery-assigner/internal/assigngraph/cycle.go b/modules/mesh-delivery-assigner/internal/assigngraph/cycle.go deleted file mode 100644 index d95014f9..00000000 --- a/modules/mesh-delivery-assigner/internal/assigngraph/cycle.go +++ /dev/null @@ -1,248 +0,0 @@ -package assigngraph - -import ( - "fmt" - "maps" - "slices" - "strings" - - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" -) - -// Cycles are the cycles among the acting waits (design 47 G §272, ADR 0656, A-323): an entry waits for its acting parts in the step -// an edge names, so acting parts that wait for each other, in one step or across steps, wait for ever unless a rule breaks the -// cycle. A cycle within one step is `: a -> b -> a`; one across steps names each edge's step, `a -[compose]-> b -[place]-> a`; -// each by node and module, said once from its smallest member, sorted. A holder's own inputs are no edge here (the graph gives an -// acting edge only from a part to the entries it acts on, never to what the part is built from). -func Cycles(entries []meshwants.AssignmentEntry) []string { - type key struct{ step, at string } - next := map[key][]string{} - steps := map[string]bool{} - for _, e := range entries { - for _, edge := range e.Edges { - if edge.Kind != EdgeActing { - continue - } - k := key{edge.Step, e.Node + "/" + e.Module} - next[k] = append(next[k], edge.Node+"/"+edge.Module) - steps[edge.Step] = true - } - } - var out []string - for _, step := range slices.Sorted(maps.Keys(steps)) { - const ( - open = iota + 1 - done - ) - state := map[string]int{} - var path []string - var walk func(at string) - walk = func(at string) { - state[at] = open - path = append(path, at) - for _, to := range next[key{step, at}] { - switch state[to] { - case 0: - walk(to) - case open: - from := slices.Index(path, to) - cycle := slices.Clone(path[from:]) - // start at the smallest member so one cycle is said once, whichever member is met first - lowest := slices.Index(cycle, slices.Min(cycle)) - cycle = append(cycle[lowest:], cycle[:lowest]...) - out = append(out, fmt.Sprintf("%s: %s -> %s", step, strings.Join(cycle, " -> "), cycle[0])) - } - } - path = path[:len(path)-1] - state[at] = done - } - starts := map[string]bool{} - for k := range next { - if k.step == step { - starts[k.at] = true - } - } - for _, at := range slices.Sorted(maps.Keys(starts)) { - if state[at] == 0 { - walk(at) - } - } - } - // Across steps (A-323): the shortest cycle through each entry over every acting edge, said when its edges name two steps or more. - for _, hops := range cyclesThrough(entries, func(_ meshwants.AssignmentEntry, e meshwants.Edge) bool { return e.Kind == EdgeActing }) { - if crossesSteps(hops) { - out = append(out, hopWords(rotated(hops))) - } - } - slices.Sort(out) - return slices.Compact(out) -} - -// HeldCycles is, by `/`, the cycle each entry lies on among the edges holds says hold it (A-323, A-330): a want held -// on such a cycle is held for ever, since no rule breaks it. Each cycle starts at the entry and names each edge's step. -func HeldCycles(entries []meshwants.AssignmentEntry, holds func(e meshwants.AssignmentEntry, edge meshwants.Edge) bool) map[string]string { - out := map[string]string{} - for at, hops := range cyclesThrough(entries, holds) { - out[at] = hopWords(hops) - } - return out -} - -// Engines is, by node, the module that holds the seat node-engine there, read from the acting edges' seat (A-330). -func Engines(entries []meshwants.AssignmentEntry) map[string]string { - out := map[string]string{} - for _, e := range entries { - for _, edge := range e.Edges { - if edge.Kind == EdgeActing && edge.Seat == nodeengine.Seat { - out[edge.Node] = edge.Module - } - } - } - return out -} - -// Runner is the module that holds the seat mesh-pipeline-runner, read from the acting edges' seat as Engines reads the node-engine -// (A-334); "" when none is read. -func Runner(entries []meshwants.AssignmentEntry) string { - for _, e := range entries { - for _, edge := range e.Edges { - if edge.Kind == EdgeActing && edge.Seat == meshpipelinerunner.Seat { - return edge.Module - } - } - } - return "" -} - -// hop is one edge of a cycle: from the entry at, in step, to the next hop's entry. -type hop struct{ at, step string } - -// cyclesThrough is, by `/`, the shortest cycle through each entry over the edges keep takes, found within the -// entry's strongly connected part, each starting at the entry. -func cyclesThrough(entries []meshwants.AssignmentEntry, keep func(meshwants.AssignmentEntry, meshwants.Edge) bool) map[string][]hop { - type out struct{ to, step string } - next := map[string][]out{} - for _, e := range entries { - at := e.Node + "/" + e.Module - for _, edge := range e.Edges { - if keep(e, edge) { - next[at] = append(next[at], out{edge.Node + "/" + edge.Module, edge.Step}) - } - } - } - for at := range next { - slices.SortFunc(next[at], func(a, b out) int { return strings.Compare(a.to+"\x00"+a.step, b.to+"\x00"+b.step) }) - } - part := strongParts(slices.Sorted(maps.Keys(next)), func(at string) []string { - var to []string - for _, o := range next[at] { - to = append(to, o.to) - } - return to - }) - found := map[string][]hop{} - for _, start := range slices.Sorted(maps.Keys(next)) { - // breadth first from start back to it, within its part - prev := map[string]hop{} - queue := []string{start} - seen := map[string]bool{} - for len(queue) > 0 && found[start] == nil { - at := queue[0] - queue = queue[1:] - for _, o := range next[at] { - if part[o.to] != part[start] { - continue - } - if o.to == start { - hops := []hop{{at, o.step}} - for cur := at; cur != start; { - h := prev[cur] - hops = append(hops, h) - cur = h.at - } - slices.Reverse(hops) - found[start] = hops - break - } - if !seen[o.to] { - seen[o.to] = true - prev[o.to] = hop{at, o.step} - queue = append(queue, o.to) - } - } - } - } - return found -} - -// strongParts numbers each vertex by its strongly connected part (Tarjan). -func strongParts(vertices []string, succ func(string) []string) map[string]int { - index, low, part := map[string]int{}, map[string]int{}, map[string]int{} - on := map[string]bool{} - var stack []string - n, parts := 0, 0 - var visit func(v string) - visit = func(v string) { - index[v], low[v] = n, n - n++ - stack = append(stack, v) - on[v] = true - for _, w := range succ(v) { - if _, ok := index[w]; !ok { - visit(w) - low[v] = min(low[v], low[w]) - } else if on[w] { - low[v] = min(low[v], index[w]) - } - } - if low[v] == index[v] { - parts++ - for { - w := stack[len(stack)-1] - stack = stack[:len(stack)-1] - on[w] = false - part[w] = parts - if w == v { - break - } - } - } - } - for _, v := range vertices { - if _, ok := index[v]; !ok { - visit(v) - } - } - return part -} - -func crossesSteps(hops []hop) bool { - for _, h := range hops[1:] { - if h.step != hops[0].step { - return true - } - } - return false -} - -// rotated is the cycle started at its smallest member, so one cycle is said once. -func rotated(hops []hop) []hop { - lowest := 0 - for i, h := range hops { - if h.at < hops[lowest].at { - lowest = i - } - } - return append(slices.Clone(hops[lowest:]), hops[:lowest]...) -} - -// hopWords is a cycle with each edge's step: `a -[compose]-> b -[place]-> a`. -func hopWords(hops []hop) string { - var b strings.Builder - for _, h := range hops { - fmt.Fprintf(&b, "%s -[%s]-> ", h.at, h.step) - } - return b.String() + hops[0].at -} diff --git a/modules/mesh-delivery-assigner/internal/assigngraph/cycle_test.go b/modules/mesh-delivery-assigner/internal/assigngraph/cycle_test.go deleted file mode 100644 index 522dfec1..00000000 --- a/modules/mesh-delivery-assigner/internal/assigngraph/cycle_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package assigngraph - -import ( - "slices" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" -) - -// Design 47 G §272 (AS10), A-323: a cycle among the waits is found within one step and across steps, each edge's step named across -// steps; a chain and provisioning edges are none. -func TestACycleAmongTheWaitsWithinOrAcrossStepsIsFound(t *testing.T) { - act := func(node, module, step string) meshwants.Edge { - return meshwants.Edge{Kind: EdgeActing, Node: node, Module: module, Step: step} - } - entry := func(module string, edges ...meshwants.Edge) meshwants.AssignmentEntry { - return meshwants.AssignmentEntry{Node: "ace", Module: module, Edges: edges} - } - for _, c := range []struct { - name string - entries []meshwants.AssignmentEntry - want []string - }{ - {"two parts on each other in one step", []meshwants.AssignmentEntry{entry("a", act("ace", "b", "compose")), entry("b", act("ace", "a", "compose"))}, - []string{"compose: ace/a -> ace/b -> ace/a"}}, - {"three in a ring, said once", []meshwants.AssignmentEntry{entry("c", act("ace", "a", "build")), entry("a", act("ace", "b", "build")), entry("b", act("ace", "c", "build"))}, - []string{"build: ace/a -> ace/b -> ace/c -> ace/a"}}, - {"the indexer taking in the checker, the checker checking the indexer", []meshwants.AssignmentEntry{entry("checker", act("ace", "indexer", "take-in")), entry("indexer", act("ace", "checker", "check"))}, - []string{"ace/checker -[take-in]-> ace/indexer -[check]-> ace/checker"}}, - {"the assigner and the node-engine, across compose and place", []meshwants.AssignmentEntry{entry("assigner", act("ace", "engine", "compose")), - entry("engine", act("ace", "assigner", "place")), entry("app", act("ace", "engine", "compose"), act("ace", "assigner", "place"))}, - []string{"ace/assigner -[compose]-> ace/engine -[place]-> ace/assigner"}}, - {"a chain", []meshwants.AssignmentEntry{entry("a", act("ace", "b", "compose")), entry("b", act("ace", "c", "compose")), entry("c")}, nil}, - {"provisioning edges are no wait of a step", []meshwants.AssignmentEntry{entry("a", meshwants.Edge{Kind: EdgeProvisioning, Node: "ace", Module: "b"}), entry("b", meshwants.Edge{Kind: EdgeProvisioning, Node: "ace", Module: "a"})}, nil}, - } { - if got := Cycles(c.entries); !slices.Equal(got, c.want) { - t.Errorf("%s: cycles %v, want %v", c.name, got, c.want) - } - } -} - -// A-323, A-330: the node-engine on a node is read from the acting edges' seat, and the cycles that hold a want are those of the -// edges said to hold it, each from the entry, naming each edge's step. -func TestTheHeldCyclesAreThoseOfTheHoldingEdges(t *testing.T) { - entries := []meshwants.AssignmentEntry{ - {Node: "ace", Module: "assigner", Edges: []meshwants.Edge{{Kind: EdgeActing, Node: "ace", Module: "engine", Step: "compose", Seat: "node-engine"}}}, - {Node: "ace", Module: "engine", Edges: []meshwants.Edge{{Kind: EdgeActing, Node: "ace", Module: "assigner", Step: "place", Seat: "mesh-wants"}}}, - } - if got := Engines(entries); len(got) != 1 || got["ace"] != "engine" { - t.Errorf("the node-engines %v, want engine on ace", got) - } - all := func(meshwants.AssignmentEntry, meshwants.Edge) bool { return true } - if got := HeldCycles(entries, all); len(got) != 2 || got["ace/engine"] != "ace/engine -[place]-> ace/assigner -[compose]-> ace/engine" { - t.Errorf("held cycles %v", got) - } - // the node-engine's want held by no acting edge: no cycle holds anything - if got := HeldCycles(entries, func(e meshwants.AssignmentEntry, _ meshwants.Edge) bool { return e.Module != "engine" }); len(got) != 0 { - t.Errorf("held cycles with the node-engine's edges holding nothing: %v", got) - } -} diff --git a/modules/mesh-delivery-assigner/internal/assigngraph/graph_test.go b/modules/mesh-delivery-assigner/internal/assigngraph/graph_test.go index 5a207e22..88c41dfb 100644 --- a/modules/mesh-delivery-assigner/internal/assigngraph/graph_test.go +++ b/modules/mesh-delivery-assigner/internal/assigngraph/graph_test.go @@ -86,7 +86,7 @@ func TestTheAssignmentGraphIsComputedFromItsInputsAndKept(t *testing.T) { } want := []meshwants.Edge{ {Kind: EdgeActing, Node: "anchor", Module: "build-go", Fp: "src2:b1", Seat: "node-build-go", Step: "build"}, - {Kind: EdgeActing, Node: "anchor", Module: "node-engine", Fp: "src2:e1", Seat: "node-engine", Step: "compose", Bound: "30m"}, + {Kind: EdgeActing, Node: "anchor", Module: "node-engine", Fp: "src2:e1", Seat: "node-engine", Step: "compose"}, // the edge stays; the wait's bound goes (H-A-70) {Kind: EdgeProvisioning, Node: "anchor", Module: "postgres", Fp: "src2:p1", Provision: "database"}, } if fmt.Sprint(webA.Edges) != fmt.Sprint(want) { diff --git a/modules/mesh-delivery-assigner/internal/buckets/buckets.go b/modules/mesh-delivery-assigner/internal/buckets/buckets.go index 96712348..a894baaf 100644 --- a/modules/mesh-delivery-assigner/internal/buckets/buckets.go +++ b/modules/mesh-delivery-assigner/internal/buckets/buckets.go @@ -18,12 +18,6 @@ import ( const ( // WaitsState holds the waits on an outside cause, one key `..` (design 47 G §706). WaitsState = "waits" - // DeliveredAgainState holds each ask of assigner_delivered_again, one key ``, its value the version, who asked and why, and - // why it does not hold yet (design 47 G §376, A-250). - DeliveredAgainState = "delivered-again" - // DeliveryWaitsState holds each dependent's delivery wait the assigner said, one key ``, its value the wait-held said - // (design 47 G §376, §498, A-246). - DeliveryWaitsState = "delivery-waits" // PutWaitsState holds each put wait the assigner said, one key `.`, its value the wait's id and the put as said // (design 47 G A-204, A-211), so a put that ends while the assigner is stopped gets its wait-ended at the next start. PutWaitsState = "put-waits" @@ -45,8 +39,6 @@ func Differences() string { return own(wants.StateDifferences) } func Asks() string { return own("asks") } func Puts() string { return own("puts") } func Waits() string { return own(WaitsState) } -func DeliveredAgain() string { return own(DeliveredAgainState) } -func DeliveryWaits() string { return own(DeliveryWaitsState) } func PutWaits() string { return own(PutWaitsState) } func Inputs() string { return own(InputsState) } func CompositionRuns() string { return own(CompositionRunsState) } @@ -71,10 +63,6 @@ const ( Graph = IndexerModule + "_graph" ModulesRead = IndexerModule + "." + catalog.StateModules ManifestsRead = IndexerModule + "." + catalog.StateManifests - // ProvisionerModule is the holder of the seat mesh-provisioner, named from this module's manifest (reads its provisions); Provisions - // is its state provisions. - ProvisionerModule = "mesh-provisioner" - Provisions = ProvisionerModule + "_provisions" // TellerModule is the module whose hand the operator's answers pass: the one that holds each ask's answer, and the // name the hand-act log records a warrant under. Held equal to the manifest's reads by its test. TellerModule = "mesh-delivery-teller" diff --git a/modules/mesh-delivery-assigner/internal/buckets/buckets_test.go b/modules/mesh-delivery-assigner/internal/buckets/buckets_test.go index 2b9dd207..fa5bcb64 100644 --- a/modules/mesh-delivery-assigner/internal/buckets/buckets_test.go +++ b/modules/mesh-delivery-assigner/internal/buckets/buckets_test.go @@ -12,9 +12,9 @@ func TestBucketsAreTheEarlierNames(t *testing.T) { for name, c := range map[string][2]string{ "Have": {Have, "node-engine_have"}, "ComposedRead": {ComposedRead, "mesh-host.composed"}, "Modules": {Modules, "mesh-delivery-indexer_modules"}, "Manifests": {Manifests, "mesh-delivery-indexer_manifests"}, - "Answers": {Answers, "mesh-delivery-teller_answers"}, - "Grants": {Grants, "mesh-grants_grants"}, - "Provisions": {Provisions, "mesh-provisioner_provisions"}, "TellerModule": {TellerModule, "mesh-delivery-teller"}, + "Answers": {Answers, "mesh-delivery-teller_answers"}, + "Grants": {Grants, "mesh-grants_grants"}, + "TellerModule": {TellerModule, "mesh-delivery-teller"}, } { if c[0] != c[1] { t.Errorf("%s = %q, want %q", name, c[0], c[1]) diff --git a/modules/mesh-delivery-assigner/internal/judge/judge.go b/modules/mesh-delivery-assigner/internal/judge/judge.go index 13510df0..bd81b797 100644 --- a/modules/mesh-delivery-assigner/internal/judge/judge.go +++ b/modules/mesh-delivery-assigner/internal/judge/judge.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - mdcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" "reflect" "slices" "sort" @@ -170,11 +169,11 @@ func JudgeAnswers(p mma.Proposal, intended map[string]map[string]bool, answers m } switch { case len(v.Fails) > 0: - v.State = mdcc.VerdictFail + v.State = plc.VerdictFail case len(waiting) > 0: v.State = plc.VerdictWaits default: - v.State = mdcc.VerdictPass + v.State = plc.VerdictPass } return v } diff --git a/modules/mesh-delivery-assigner/internal/judge/judge_test.go b/modules/mesh-delivery-assigner/internal/judge/judge_test.go index 4d2ed8cc..f4b6ddb3 100644 --- a/modules/mesh-delivery-assigner/internal/judge/judge_test.go +++ b/modules/mesh-delivery-assigner/internal/judge/judge_test.go @@ -2,7 +2,6 @@ package judge import ( "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - mdcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" "slices" "strings" "testing" @@ -49,22 +48,22 @@ func TestJudge(t *testing.T) { words string marked string }{ - {"all the same", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": same(8)}, nil, mdcc.VerdictPass, "", ""}, + {"all the same", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": same(8)}, nil, plc.VerdictPass, "", ""}, {"a module added where intended", false, map[string]*nodeengine.CandidateAnswer{"anchor": {Proposal: "p-one", Revision: 7, Verdict: nodeengine.CandidateAnswerVerdictDiffers, - Added: []nodeengine.CandidateAdded{{Module: "web", Unissued: []string{"its own secret"}}}}, "laptop": same(8)}, nil, mdcc.VerdictPass, "", ""}, - {"a difference not computed passes, marked", false, map[string]*nodeengine.CandidateAnswer{"anchor": differs(7, "mail"), "laptop": same(8)}, nil, mdcc.VerdictPass, "", "mail"}, + Added: []nodeengine.CandidateAdded{{Module: "web", Unissued: []string{"its own secret"}}}}, "laptop": same(8)}, nil, plc.VerdictPass, "", ""}, + {"a difference not computed passes, marked", false, map[string]*nodeengine.CandidateAnswer{"anchor": differs(7, "mail"), "laptop": same(8)}, nil, plc.VerdictPass, "", "mail"}, {"the seed carries a difference, named", true, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": {Proposal: "p-one", Revision: 8, Verdict: nodeengine.CandidateAnswerVerdictDiffers, Differs: []nodeengine.CandidateDiffers{{Module: "node-tools", Resource: "node-tools.runtime", Why: "the resource differs", Bodies: []nodeengine.CandidateBodies{{ID: "node-tools.runtime", Paths: []string{"env.MESH_OWNED_PATHS./etc/resolv.conf", "env.MESH_TOOL_MODULES"}, Running: `{"env":{"MESH_OWNED_PATHS":"{\"/etc/hosts\":\"hostname\",\"/etc/resolv.conf\":\"networkmanager (node-uplink)\"}"}}`, - Composed: `{"env":{"MESH_OWNED_PATHS":"{\"/etc/hosts\":\"hostname\",\"/etc/resolv.conf\":\"systemd-resolved (node-resolver)\"}","MESH_TOOL_MODULES":"… (cut at 8192 bytes)`}}}}}}, nil, mdcc.VerdictPass, "", ""}, + Composed: `{"env":{"MESH_OWNED_PATHS":"{\"/etc/hosts\":\"hostname\",\"/etc/resolv.conf\":\"systemd-resolved (node-resolver)\"}","MESH_TOOL_MODULES":"… (cut at 8192 bytes)`}}}}}}, nil, plc.VerdictPass, "", ""}, {"a refusal", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": {Proposal: "p-one", Revision: 8, Verdict: nodeengine.CandidateAnswerVerdictRefused, - Refusals: []nodeengine.CandidateRefusal{{Module: "web", Why: "no own secret"}}}}, nil, mdcc.VerdictFail, "laptop refuses", ""}, + Refusals: []nodeengine.CandidateRefusal{{Module: "web", Why: "no own secret"}}}}, nil, plc.VerdictFail, "laptop refuses", ""}, {"a node not answering, within its bound", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7)}, map[string]bool{"laptop": true}, plc.VerdictWaits, "laptop (offline)", ""}, - {"an answer of an earlier revision, past its bound, is no answer and decided without it", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(6), "laptop": same(8)}, nil, mdcc.VerdictPass, "no answer from anchor", ""}, + {"an answer of an earlier revision, past its bound, is no answer and decided without it", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(6), "laptop": same(8)}, nil, plc.VerdictPass, "no answer from anchor", ""}, {"a module held as today passes, shown in the brief", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": {Proposal: "p-one", Revision: 8, - Verdict: nodeengine.CandidateAnswerVerdictSame, Held: []nodeengine.CandidateRefusal{{Module: "spiegel", Why: "a required value is unset"}}}}, nil, mdcc.VerdictPass, "", ""}, - {"its later answer", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": same(8)}, nil, mdcc.VerdictPass, "", ""}, + Verdict: nodeengine.CandidateAnswerVerdictSame, Held: []nodeengine.CandidateRefusal{{Module: "spiegel", Why: "a required value is unset"}}}}, nil, plc.VerdictPass, "", ""}, + {"its later answer", false, map[string]*nodeengine.CandidateAnswer{"anchor": same(7), "laptop": same(8)}, nil, plc.VerdictPass, "", ""}, } { q := p q.Seed = row.seed @@ -73,10 +72,10 @@ func TestJudge(t *testing.T) { if v.State != row.state || !strings.Contains(all, row.words) { t.Errorf("%s: %s, %q", row.name, v.State, all) } - if row.seed && row.state == mdcc.VerdictPass && !strings.Contains(Brief(q, v), "the seed carries: node-tools: MESH_OWNED_PATHS[/etc/resolv.conf] networkmanager (node-uplink) → (value cut) (and 1 more fields)") { + if row.seed && row.state == plc.VerdictPass && !strings.Contains(Brief(q, v), "the seed carries: node-tools: MESH_OWNED_PATHS[/etc/resolv.conf] networkmanager (node-uplink) → (value cut) (and 1 more fields)") { t.Errorf("%s: the brief does not name it: %s", row.name, Brief(q, v)) } - if row.seed && row.state == mdcc.VerdictPass { + if row.seed && row.state == plc.VerdictPass { whole := row.answers["laptop"].Differs[0] whole.Bodies = []nodeengine.CandidateBodies{{ID: "r", Paths: []string{"env.MESH_OWNED_PATHS./etc/resolv.conf"}, Running: `{"env":{"MESH_OWNED_PATHS":"{\"/etc/resolv.conf\":\"networkmanager (node-uplink)\"}"}}`, diff --git a/modules/mesh-delivery-assigner/internal/outside/outside.go b/modules/mesh-delivery-assigner/internal/outside/outside.go index 64d81ebc..47235075 100644 --- a/modules/mesh-delivery-assigner/internal/outside/outside.go +++ b/modules/mesh-delivery-assigner/internal/outside/outside.go @@ -87,21 +87,6 @@ func Causes(conds []Cond, m string, moved func(owner string) bool) []Cause { return out } -// OwnNetworkKey is the key of a machine's own open network condition, which holds the machine as a whole (§706 *M's own -// network*). -func OwnNetworkKey(m string) string { return "machine." + m + ".network" } - -// OwnNetwork are the machines whose own network condition is open among conds. -func OwnNetwork(conds []Cond) map[string]bool { - out := map[string]bool{} - for _, c := range conds { - if p := strings.Split(c.Key, "."); len(p) == 3 && p[0] == "machine" && p[2] == "network" && c.Key == OwnNetworkKey(p[1]) { - out[p[1]] = true - } - } - return out -} - // Wait is one outside wait as the assigner keeps it: the move of Module at FP on Node, or its settle (Settling). Decided is // what ended it for the rest of that fingerprint on that node: `go-on` (counted healthy by the operator's word) or `failed` // (the delivery's failure), so a check that goes on failing is not waited on again. diff --git a/modules/mesh-delivery-assigner/internal/outside/outside_test.go b/modules/mesh-delivery-assigner/internal/outside/outside_test.go index eb39c173..541d4dd2 100644 --- a/modules/mesh-delivery-assigner/internal/outside/outside_test.go +++ b/modules/mesh-delivery-assigner/internal/outside/outside_test.go @@ -31,9 +31,6 @@ func TestCauses(t *testing.T) { if !slices.Equal(got, want) { t.Errorf("causes = %v, want %v (a rewritten naming a moved module, an unreachable not listing ace and ace's own network are none)", got, want) } - if own := OwnNetwork(conds); !own["ace"] || !own["hub2"] || own["hub"] { - t.Errorf("own network = %v", own) - } } func facts(now time.Time, conds ...Cond) Facts { diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index cc79899d..16bf094b 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -50,14 +50,14 @@ func TestManifest(t *testing.T) { // §431, §507: the gate's source is read through its verb `source` and its event `source-moved`, and no `merges` is read. // A-175, A-178: the teller's answers are followed through its first view and its views family, never its bucket. // §389: the watchman's machine-away and machine-back are consumed, never its presence. - want := []string{"mesh-grants.grants", "mesh-host.composed", - "mesh-provisioner.provisions", "node-engine.have"} + // A5/AC1 (design 47 H §750): no want waits for a provision, so the provisioner's provisions are no read; the grants stay + // for a put's own judgement (ADR 0546 clause 4). + want := []string{"mesh-grants.grants", "mesh-host.composed", "node-engine.have"} if !reflect.DeepEqual(reads, want) { t.Errorf("reads = %v, want exactly %v", reads, want) } // the buckets the program names are the manifest's reads, `.` as `_` - named := []string{buckets.Grants, - buckets.ComposedRead, "mesh-provisioner_provisions", buckets.Have} + named := []string{buckets.Grants, buckets.ComposedRead, buckets.Have} slices.Sort(named) for i, r := range want { if got := strings.Replace(named[i], "_", ".", 1); got != r { @@ -70,7 +70,7 @@ func TestManifest(t *testing.T) { } if want := map[string]int{"asks": 5, "differences": 64, "puts": 20, "wants": 20, "assignments": 64, "placement": 64, "copies": 1, "proposals": 5, "candidates": 5, "pulses": 0, "findings": 5, "policies": 20, "seat-holders": 5, views.MarksState: 1, views.StatusState: 1, - "engine-reads": 1, "written": 5, "waits": 5, "put-waits": 1, "delivery-waits": 1, "delivered-again": 1, "inputs": 1, "composition-runs": 1, "pins": 5}; !reflect.DeepEqual(states, want) { + "engine-reads": 1, "written": 5, "waits": 5, "put-waits": 1, "inputs": 1, "composition-runs": 1}; !reflect.DeepEqual(states, want) { t.Errorf("state = %v, want %v", states, want) } if _, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 { @@ -105,21 +105,18 @@ func TestManifest(t *testing.T) { // which lists and asks again what each node's engine reads. if !slices.Equal(m.Tools, []string{"assigner_put", "assigner_release", "assigner_puts", "assigner_waits", "assigner_inputs", "assigner_inputs_drop", "assigner_compositions", "assigner_compositions_drop", "assignments", "seat-holders", "wants", "asks", "assigner_read_assignments", "assigner_read_seat_holders", "assigner_read_wants", "assigner_read_findings", "assigner_read_differences", "assigner_read_copies", - "assigner_first_views", "assigner_first_view_again", "assigner_engine_reads", "assigner_seat_list", "assigner_seat_list_again", "assigner_ask_again", "assigner_written", "assigner_why", "assigner_pins", "assigner_unpin", "assigner_delivered_again"}) { + "assigner_first_views", "assigner_first_view_again", "assigner_engine_reads", "assigner_seat_list", "assigner_seat_list_again", "assigner_ask_again", "assigner_written", "assigner_why", "assigner_want_drop"}) { t.Errorf("the assigner serves tools %v, want its three put tools, three first views, six read tools, assigner_engine_reads, the seat list's two (ADR 0636 §7) assigner_ask_again (ADR 0668 §5) and assigner_why (design 47 G A-439)", m.Tools) } if m.RunsAs != m.Module || m.SecretsOwner != m.RunsAs { t.Errorf("runs-as = %q: the bundle runs on the assigner's own account (ADR 0621 §1)", m.RunsAs) } - // ADR 0621 §2: each first-view tool answers exactly its readers, never the assigner. - // Design 47 G §717: the teller and the status view take their first views of the seat mesh-wants' asks and wants, and of - // the assignments, from the tools named for the states. - callers := map[string]string{"assignments": "mesh-grants,node-tools,mesh-delivery-indexer,mesh-delivery-builder,mesh-delivery-checker,mesh-delivery-view", - "seat-holders": "node-tools,mesh-delivery-builder,mesh-delivery-view,mesh-delivery-teller,mesh-delivery-checker,mesh-delivery-indexer", "wants": "mesh-grants,mesh-delivery-view", "asks": "mesh-delivery-teller,mesh-delivery-view"} - env := m.Build.Artifacts[0].Env - for tool, list := range callers { - if env[views.CallersEnv(tool)] != list || views.ParseCallers(env[views.CallersEnv(tool)]).Allows("n1."+m.Module) { - t.Errorf("%s callers = %q, want %q", tool, env[views.CallersEnv(tool)], list) + // design 47 H §752, H-A-66: every first-view tool answers every caller, so the manifest names no callers list for any. + for _, a := range m.Build.Artifacts { + for k := range a.Env { + if strings.HasPrefix(k, "MESH_VIEW_CALLERS_") { + t.Errorf("the artifact %s names the callers list %s: no seat or tool keeps one (design 47 H §752)", a.Name, k) + } } } // ADR 0635 §5: its views family is its ordinary events, `views.` among its emits, on no stream of its own. @@ -135,9 +132,10 @@ func TestManifest(t *testing.T) { } } // ADR 0618 §4: and it hears each node's engine say its reads changed; ADR 0636 §4 (issue 1966): and the seat list's two events; - // issue 1966 note 151: and the teller's ask-ended, its subject the one the assigner hears; §376, §389: its own version-delivered and - // the watchman's machine-away and machine-back, read whole at its start. - if !slices.Equal(m.Consumes, []string{"mesh-conditions.findings-asked", "mesh-conditions.condition-raised", "mesh-conditions.condition-changed", "mesh-conditions.condition-cleared", "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-catalog.version-released", "mesh-wants.version-delivered", "mesh-watchdogs.machine-away", "mesh-watchdogs.machine-back", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", builds.BuilderModule + "." + builds.BuildsStreamEvents, buckets.IndexerModule + ".take-in.taken", buckets.TellerModule + "." + tellerevents.AskEnded, buckets.TellerModule + ".views." + buckets.AnswersState, "mesh-pipeline-runner.pipeline-ended", "mesh-pipeline-runner.group-ended", "node-build-go.step-ended", "node-build-go.ask-taken", "node-build-go.step-progress", "node-engine.manifest-reads-changed", "node-engine.running", "mesh-delivery.pinned", "mesh-delivery.freed", "mesh-delivery.heads-replaced", "mesh-delivery.pins-stated"}) { + // issue 1966 note 151: and the teller's ask-ended, its subject the one the assigner hears; §389: the watchman's machine-away + // and machine-back, read whole at its start. A1/AC1 (design 47 H §750, §764): no version-delivered, delivery-waits, + // delivered-again or assigner_delivered_again: a want waits on no dependency's delivery. + if !slices.Equal(m.Consumes, []string{"mesh-conditions.findings-asked", "mesh-conditions.condition-raised", "mesh-conditions.condition-changed", "mesh-conditions.condition-cleared", "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-catalog.version-released", "mesh-watchdogs.machine-away", "mesh-watchdogs.machine-back", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", builds.BuilderModule + "." + builds.BuildsStreamEvents, buckets.TellerModule + "." + tellerevents.AskEnded, buckets.TellerModule + ".views." + buckets.AnswersState, "mesh-pipeline-runner.pipeline-ended", "node-build-go.step-ended", "node-build-go.ask-taken", "node-build-go.step-progress", "node-engine.manifest-reads-changed", "node-engine.running"}) { t.Errorf("consumes = %v, want the catalog half's revisions (design 47 G §195), the teller's ask-ended, the engines' reads changed and what they run", m.Consumes) } if want := []string{"mesh-delivery-indexer.manifests", "mesh-delivery-indexer.modules", @@ -151,7 +149,6 @@ func TestManifest(t *testing.T) { "seat:node-build-go.drop", // §720, A-469s: a composition run past its bound drops its ask: the composition run's ask on the Go build seat's queue check (accept.check.) "seat:node-build-go.output", // A-254: a composition run's answer kept as the build seat's object "seat:node-engine.compose-candidate", // §354, A-226: each machine's composition input - "seat:node-engine.manifest-reads", // ADR 0618 §3 "seat:operator-channel.asks"}; !slices.Equal(slices.Sorted(slices.Values(m.Invokes)), want) { // ADR 0668 §4: the ended asks at bind t.Errorf("invokes = %v, want %v", m.Invokes, want) } @@ -168,7 +165,7 @@ func TestManifest(t *testing.T) { } file, _ := os.ReadFile("module.json") if err := json.Unmarshal(file, &raw); err != nil || raw.Config["outside-wait-minutes"].Default != float64(30) || raw.Config["outside-settle-seconds"].Default != float64(90) || - raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 8 || + raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 10 || raw.Config["tier-bound"].Default != float64(30) || raw.Config["entry-asks"].Default != float64(5) || raw.Config["composition-runs-cap-mib"].Default != float64(16) || raw.Config["composition-runs-kept"].Default != "24h" || raw.Config["composition-step-bound"].Default != "30m" || raw.Config["run-untaken-bound"].Default != "24h" || raw.Config["blame-tries"].Default != float64(20) { t.Errorf("config = %+v (%v), want outside-wait-minutes of 30, outside-settle-seconds of 90 and ask-operator-for of take-off,remove-node,stops (A-425a)", raw.Config, err) @@ -183,6 +180,8 @@ func TestManifest(t *testing.T) { {"MESH_ASSIGNER_COMPOSITION_STEP_BOUND_FILE", "${dir:state}/composition-step-bound", "composition-step-bound"}, {"MESH_ASSIGNER_RUN_UNTAKEN_BOUND_FILE", "${dir:state}/run-untaken-bound", "run-untaken-bound"}, {"MESH_ASSIGNER_BLAME_TRIES_FILE", "${dir:state}/blame-tries", "blame-tries"}, + {"MESH_ASSIGNER_TIER_BOUND_FILE", "${dir:state}/tier-bound", "tier-bound"}, // design 47 H §751 + {"MESH_ASSIGNER_ENTRY_ASKS_FILE", "${dir:state}/entry-asks", "entry-asks"}, // design 47 H H-A-60 } { if !strings.Contains(string(file), `"`+f.env+`": "`+f.path+`"`) || !strings.Contains(string(file), `"path": "`+f.path+`"`) || !strings.Contains(string(file), `"content": "${config:`+f.key+`}"`) { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index e33fc83b..5dfdd07d 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -34,7 +34,6 @@ "reads": [ "mesh-grants.grants", "mesh-host.composed", - "mesh-provisioner.provisions", "node-engine.have" ], "uses": [ @@ -49,26 +48,19 @@ "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-catalog.version-released", - "mesh-wants.version-delivered", "mesh-watchdogs.machine-away", "mesh-watchdogs.machine-back", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-delivery-builder.build.ended.**", - "mesh-delivery-indexer.take-in.taken", "mesh-delivery-teller.ask-ended", "mesh-delivery-teller.views.answers", "mesh-pipeline-runner.pipeline-ended", - "mesh-pipeline-runner.group-ended", "node-build-go.step-ended", "node-build-go.ask-taken", "node-build-go.step-progress", "node-engine.manifest-reads-changed", - "node-engine.running", - "mesh-delivery.pinned", - "mesh-delivery.freed", - "mesh-delivery.heads-replaced", - "mesh-delivery.pins-stated" + "node-engine.running" ], "invokes": [ "mesh-delivery-indexer.manifests", @@ -88,7 +80,6 @@ "seat:node-build-go.output", "seat:node-build-go.drop", "seat:node-engine.compose-candidate", - "seat:node-engine.manifest-reads", "seat:operator-channel.asks", "seat:node-build-go.accept.check" ], @@ -138,9 +129,7 @@ "assigner_ask_again", "assigner_written", "assigner_why", - "assigner_pins", - "assigner_unpin", - "assigner_delivered_again" + "assigner_want_drop" ], "state": [ { "name": "assignments", "history": 64 }, @@ -162,11 +151,8 @@ { "name": "written", "history": 5 }, { "name": "waits", "history": 5 }, { "name": "put-waits", "history": 1 }, - { "name": "delivery-waits", "history": 1 }, - { "name": "delivered-again", "history": 1 }, { "name": "inputs", "history": 1, "max-mib": 64 }, - { "name": "composition-runs", "history": 1, "key-ttl": true, "max-mib": 64 }, - { "name": "pins", "history": 5, "max-mib": 4 } + { "name": "composition-runs", "history": 1, "key-ttl": true, "max-mib": 64 } ], "config": { "outside-wait-minutes": { @@ -191,6 +177,22 @@ "trusted": false, "default": "take-off,remove-node,stops" }, + "tier-bound": { + "type": "number", + "description": "minutes each tier of a pipeline's movers has to run on every present placing machine before the assigner writes the next tier's wants; a tier that does not run within it ends the delivery with the finding tier-late and writes no later tier (design 47 H section 751).", + "minimum": 1, + "maximum": 10080, + "trusted": false, + "default": 30 + }, + "entry-asks": { + "type": "number", + "description": "how often the assigner asks a catalog entry again, on each catalog-revision and when its link to the bus opens again, after its ask failed to reach the holder of the seat mesh-catalog; one more failure leaves it unanswered, with the finding entry-unanswered (design 47 H, H-A-60).", + "minimum": 1, + "maximum": 100, + "trusted": false, + "default": 5 + }, "composition-runs-cap-mib": { "type": "number", "description": "the cap of the state composition-runs in MiB, the process refusing a new composition run at it with its steps ending could not run naming the fill (design 47 G section 719, A-469d); its maximum is the manifest's max-mib.", @@ -273,6 +275,22 @@ "mode": "0600", "content": "${config:ask-operator-for}" }, + { + "id": "tier-bound", + "type": "file", + "path": "${dir:state}/tier-bound", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:tier-bound}" + }, + { + "id": "entry-asks", + "type": "file", + "path": "${dir:state}/entry-asks", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:entry-asks}" + }, { "id": "composition-runs-cap-mib", "type": "file", @@ -326,6 +344,8 @@ "MESH_ASSIGNER_OUTSIDE_WAIT_FILE": "${dir:state}/outside-wait-minutes", "MESH_ASSIGNER_OUTSIDE_SETTLE_FILE": "${dir:state}/outside-settle-seconds", "MESH_ASSIGNER_ASK_OPERATOR_FOR_FILE": "${dir:state}/ask-operator-for", + "MESH_ASSIGNER_TIER_BOUND_FILE": "${dir:state}/tier-bound", + "MESH_ASSIGNER_ENTRY_ASKS_FILE": "${dir:state}/entry-asks", "MESH_ASSIGNER_COMPOSITION_RUNS_CAP_MIB_FILE": "${dir:state}/composition-runs-cap-mib", "MESH_ASSIGNER_COMPOSITION_RUNS_KEPT_FILE": "${dir:state}/composition-runs-kept", "MESH_ASSIGNER_COMPOSITION_STEP_BOUND_FILE": "${dir:state}/composition-step-bound", @@ -349,13 +369,7 @@ "binary": "mesh-delivery-assigner", "loads": [ "mesh-delivery-assigner" - ], - "env": { - "MESH_VIEW_CALLERS_ASKS": "mesh-delivery-teller,mesh-delivery-view", - "MESH_VIEW_CALLERS_ASSIGNMENTS": "mesh-grants,node-tools,mesh-delivery-indexer,mesh-delivery-builder,mesh-delivery-checker,mesh-delivery-view", - "MESH_VIEW_CALLERS_SEAT_HOLDERS": "node-tools,mesh-delivery-builder,mesh-delivery-view,mesh-delivery-teller,mesh-delivery-checker,mesh-delivery-indexer", - "MESH_VIEW_CALLERS_WANTS": "mesh-grants,mesh-delivery-view" - } + ] } ] } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go index bfc658dc..37a94c66 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go @@ -408,9 +408,8 @@ func main() { pk.Start(context.Background()) // The build modules are no names (design 47 G A-82): the build seats on the seat list, each held by the module the catalog // half's manifests say; the seat list and its two tools wired once by mesh-sdk (§204). - // Its callers name seats, never a module, resolved from the seat-holders view the catalog half's claimant serves (A-295, A-298). - gate := seatlist.NewPart("builder", []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"}) - gate.Holders(branches.SeatHolders(context.Background())) + // Its tools are open to every caller, so the part resolves no caller's seat to its holder (design 47 H H-A-66). + gate := seatlist.NewPart("builder") go func() { <-stdio.Served() gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-builder-tools] "+f+"\n", a...) }) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/integration_test.go index d7760233..58afc2be 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/integration_test.go @@ -414,7 +414,7 @@ func (b *bed) manifestOf(t *testing.T, fixture string) map[string]any { // entry is the catalog graph entry of a module as the holder answers it. func (b *bed) entry(name, path, at, fp2, digest string, manifest map[string]any, toolchains map[string]builds.ToolchainUsed, src uint64) meshcatalog.CatalogEntry { return meshcatalog.CatalogEntry{Module: name, Repository: "novox/mesh-catalog", Commit: at, Fp: fp2, Path: path, ManifestDigest: digest, Manifest: manifest, - Inputs: &meshcatalog.EntryInputs{Toolchains: build.CatalogToolchains(toolchains), Src: int64(src)}} + Inputs: &meshcatalog.EntryInputs{Toolchains: build.CatalogToolchains(toolchains)}} } // archive writes modules. as the real indexer does for a module of an archive only: its fingerprint names no diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index d813037b..1435795a 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -121,9 +121,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // The part's own buckets are `_` of the credential's module (design 47 G A-296). own := ownBucketsOf(cred.Module) seatGate := newSeatGate() - // The part's one holder function (A-298): the seat list's callers and the views found by their seat (§717) read it. + // The part's one holder function (A-298): the views found by their seat (§717) read it; the seat list names no callers + // (design 47 H H-A-66). holders := branches.SeatHolders(ctx) - seatGate.Holders(holders) seats := buildrun.NewBuildSeats(seatGate.definitions, versions.HolderOf(branches)) bound := defaultStepBound if v := strings.TrimSpace(getenv("MESH_BUILDER_STEP_BOUND")); v != "" { @@ -399,7 +399,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // The seat mesh-artifact-sweep says artifacts-gone when it found a key's blob gone (issue 1937, ADR 0571 §2): the // builder's durable consumer on EVENTS, made by the grants part from the manifest's consumes. Where it is not there yet the builder says // so; it still asks held at its start and at every module value. - // The same consumer carries what wakes a floor (design 51 *The floor*): the indexer's manifest-accepted and the + // The same consumer carries what wakes a floor (design 51 *The floor*): the indexer's take-in.taken (design 47 H H-A-116) and the // condition keeper's condition-cleared. floor := floorWake{b: b, waits: floorWaits, logf: logger.Printf} // And the four registry seats' published, refused and removed (DLV-B4), which move a numbered version. @@ -416,7 +416,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro } heardGone(ctx, b, m, logger.Printf) } - // The seats' events (artifacts-gone, the registry seats' words, the build seats' toolchain-reported, manifest-accepted and + // The seats' events (artifacts-gone, the registry seats' words, the build seats' toolchain-reported, the indexer's take-in.taken and // manifest-reads-changed and the seat mesh-conditions' condition-cleared: seatEventPatterns); the owners' views events and the // catalog half's catalog-revision and registration-changed and the gate's source-moved are the views reader's (a message has // one taker, so no pattern here covers them). diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names.go index 69d32b26..e54fac5e 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names.go @@ -9,7 +9,7 @@ const namedTreePrefix = "named:" // seatEventPatterns are the seats' events the builder's own handler takes off its durable consumer (its manifest's `consumes` of // them): the registry seats' words, artifacts-gone, the build seats' toolchain-reported, condition-cleared, the node-engine -// seat's events and, of the seat mesh-catalog, manifest-accepted, version-released and versions-filled. The seat mesh-catalog's +// seat's events, of the seat mesh-catalog, version-released and versions-filled, and the indexer's take-in.taken. The seat mesh-catalog's // catalog-revision and registration-changed and the seat mesh-delivery's source-moved are the views reader's (the branch // follower's), so no pattern here covers them: a message has one taker. var seatEventPatterns = func() []string { @@ -18,8 +18,9 @@ var seatEventPatterns = func() []string { "mesh-artifact-sweep", "mesh-conditions", "node-engine", "node-build-go", "node-build-image", "node-build-node"} { out = append(out, "mesh.seat."+seat+".event.>") } - for _, event := range []string{meshcatalog.EventManifestAccepted, meshcatalog.EventVersionReleased, meshcatalog.EventVersionsFilled} { + for _, event := range []string{meshcatalog.EventVersionReleased, meshcatalog.EventVersionsFilled} { out = append(out, "mesh.seat."+meshcatalog.Seat+".event."+event, "mesh.seat."+meshcatalog.Seat+".event."+event+".>") } - return out + // And the indexer's take-in.taken, which wakes the floor (design 47 H H-A-116). + return append(out, takenInSubject) }() diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names_test.go index e37af399..c22673e3 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/names_test.go @@ -32,9 +32,13 @@ func TestTheNamesAreTheManifests(t *testing.T) { t.Errorf("the builder reads %s, its manifest's reads are %v", read, m.Reads) } } - // The catalog half (design 47 G §348, §614) is followed through the seat, and no state of the indexer is. + // The catalog half (design 47 G §348, §614) is followed through the seat, and no state of the indexer is; the indexer's + // event take-in.taken wakes the floor (design 47 H H-A-116). + if !slices.Contains(m.Consumes, "mesh-delivery-indexer.take-in.taken") { + t.Errorf("the builder's floor wakes on no take-in.taken: its manifest consumes %v", m.Consumes) + } for _, c := range m.Consumes { - if strings.HasPrefix(c, "mesh-delivery-indexer.") { + if strings.HasPrefix(c, "mesh-delivery-indexer.") && c != "mesh-delivery-indexer.take-in.taken" { t.Errorf("the builder follows %s, an indexer state, its manifest consumes it", c) } } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go index 04fc96f9..7d0d4f77 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go @@ -257,9 +257,9 @@ func aFloorsWakeIsHeard(t *testing.T) { waits.Wait("m/lib", versions.SeatGoPackages, "", versions.Floor{Silent: []string{versions.SourceRequirements}}, nil, time.Unix(0, 0)) waits.Wait("m/img", versions.SeatImages, "", versions.Floor{Silent: []string{versions.SourceTags}}, nil, time.Unix(0, 0)) f := &floorWake{b: &build.Builder{}, waits: waits, logf: log.New(&logs, "", 0).Printf} - accepted := &wakeMsg{subject: manifestAcceptedSubject} - if !f.heard(context.Background(), accepted) || !accepted.acked || !strings.Contains(logs.String(), "the floor of m/lib is asked again: manifest-accepted") { - t.Fatalf("manifest-accepted: acked %v, log %q", accepted.acked, logs.String()) + accepted := &wakeMsg{subject: takenInSubject} + if !f.heard(context.Background(), accepted) || !accepted.acked || !strings.Contains(logs.String(), "the floor of m/lib is asked again: take-in.taken") { + t.Fatalf("take-in.taken: acked %v, log %q", accepted.acked, logs.String()) } logs.Reset() cleared := &wakeMsg{subject: conditionClearedSubject, data: []byte(`{"subject":{"scope":"probe","id":"ace/mesh-delivery-forge-bridge"}}`)} @@ -299,8 +299,8 @@ func aFloorsWakeIsHeard(t *testing.T) { if !h.heard(context.Background(), published) || !published.acked || published.naked || len(saidPub) != 1 || saidPub[0].Artifact != "m:program" { t.Fatalf("a published of a reserved version: acked %v, naked %v, said %+v, log %q", published.acked, published.naked, saidPub, logs.String()) } - if h.heard(context.Background(), &wakeMsg{subject: manifestAcceptedSubject}) { - t.Fatal("manifest-accepted was taken as a seat's outcome") + if h.heard(context.Background(), &wakeMsg{subject: takenInSubject}) { + t.Fatal("take-in.taken was taken as a seat's outcome") } // Design 47 G A-420: a message left for the bus is said given up on the last delivery its consumer allows, never before. for _, n := range []uint64{4, EventDeliveries} { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go index 73afe11a..8c4f963b 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go @@ -3,20 +3,17 @@ package main import ( "git.novox.be/novox/mesh-sdk/go/seatdef" "git.novox.be/novox/mesh-sdk/go/seatlist" - - stdio "git.novox.be/novox/mesh-sdk/go" ) // The builder reads the seat list as every part does, through mesh-sdk's one wiring (go/seatlist.Part; design 47 G §204): its // definitions name the build seats (the seats whose acting rule is the step build), its tools `builder_seat_list` and -// `builder_seat_list_again`. A call through the mesh MCP server is proved only as its machine's tool runner (issue 627). The -// list names seats, never a module, resolved by the holder function meshcatalogclient's SeatHolders answers (A-295, A-298). -var seatListAgainCallers = []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"} +// `builder_seat_list_again`, open to every caller (design 47 H H-A-66). The list names seats, never a module, resolved by the +// holder function meshcatalogclient's SeatHolders answers (A-295, A-298). // seatGate is the builder's wiring of the seat list. type seatGate struct{ *seatlist.Part } -func newSeatGate() *seatGate { return &seatGate{seatlist.NewPart("builder", seatListAgainCallers)} } +func newSeatGate() *seatGate { return &seatGate{seatlist.NewPart("builder")} } // definitions are the seat definitions the list holds, none before it is read. func (g *seatGate) definitions() map[string]seatdef.Definition { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go index 2d30aacb..1dfae047 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go @@ -13,7 +13,6 @@ import ( "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/process" "mesh-delivery-builder/internal/build" @@ -189,7 +188,8 @@ func emitSeatEvent(ctx context.Context, js jetstream.JetStream, cred process.Acc // The events on the builder's consumer of EVENTS that wake a floor (design 51 *The floor*, DLV-B3b). var ( - manifestAcceptedSubject = meshcatalog.SubjectManifestAccepted + // The indexer's take-in.taken, which the builder consumes in place of the retired manifest-accepted (design 47 H H-A-116). + takenInSubject = "mesh.mod.mesh-delivery-indexer.event.take-in.taken" conditionClearedSubject = meshconditions.SubjectConditionCleared ) @@ -204,9 +204,9 @@ type floorWake struct { // heard takes a message of the consumer in when it is one that wakes a floor, acknowledged, and answers whether it was. func (f *floorWake) heard(ctx context.Context, m jetstream.Msg) bool { switch m.Subject() { - case manifestAcceptedSubject: + case takenInSubject: f.ack(m) - f.wake(ctx, versions.WakeAccepted, "", "") + f.wake(ctx, versions.WakeTakenIn, "", "") case conditionClearedSubject: f.ack(m) var e meshconditions.ConditionEvent diff --git a/modules/mesh-delivery-builder/internal/build/entry_test.go b/modules/mesh-delivery-builder/internal/build/entry_test.go index dbf0432e..5ccbf04f 100644 --- a/modules/mesh-delivery-builder/internal/build/entry_test.go +++ b/modules/mesh-delivery-builder/internal/build/entry_test.go @@ -13,15 +13,20 @@ import ( func TestAnEntryOfTheCatalogHalfIsTheFormTheBuilderBuildsBy(t *testing.T) { e := meshcatalog.CatalogEntry{Module: "netcheck", Repository: "novox/mesh-catalog", Path: "modules/netcheck", Commit: "abc", Fp: "src2:f", ManifestDigest: "sha256:m", Bases: []string{"base"}, Packages: map[string]string{"p": "1"}, Needs: []string{"n"}, - Inputs: &meshcatalog.EntryInputs{Src: 7, Toolchains: map[string]meshcatalog.ToolchainUsed{"go": {Version: "go1", Recipe: "1"}}, + Inputs: &meshcatalog.EntryInputs{Toolchains: map[string]meshcatalog.ToolchainUsed{"go": {Version: "go1", Recipe: "1"}}, Paths: map[string][]string{"lib": {"a/b", "c"}}}} got := ModuleOf(e) - if got.Fp2 != "src2:f" || got.Manifest != "m" || got.Path != "modules/netcheck" || got.Commit != "abc" || got.Src != 7 || + if got.Fp2 != "src2:f" || got.Manifest != "m" || got.Path != "modules/netcheck" || got.Commit != "abc" || got.Toolchains["go"].Version != "go1" || got.Packages["p"] != "1" || !reflect.DeepEqual(got.Needs, []string{"n"}) || got.Inputs2 == nil || !reflect.DeepEqual(got.Inputs2.Bases, []string{"base"}) || !reflect.DeepEqual(got.Inputs2.Paths, map[string]string{"a/b": "lib", "c": "lib"}) { t.Errorf("ModuleOf = %+v", got) } + // A module value carries no source stamp (design 47 H H-A-48): its claim is numbered per fingerprint, naming no source + // revision and no merge. + if c := ClaimOf("netcheck", got, Plan{}); c.Revision != "" || c.Merge != 0 || c.Fingerprint != "src2:f" { + t.Errorf("ClaimOf = %+v", c) + } if bare := ModuleOf(meshcatalog.CatalogEntry{Module: "m"}); bare.Inputs2 != nil || bare.Fp2 != "" { t.Errorf("ModuleOf of an entry with none = %+v, want nothing set", bare) } diff --git a/modules/mesh-delivery-builder/internal/build/numbers.go b/modules/mesh-delivery-builder/internal/build/numbers.go index 9200129a..2aeefc0e 100644 --- a/modules/mesh-delivery-builder/internal/build/numbers.go +++ b/modules/mesh-delivery-builder/internal/build/numbers.go @@ -6,7 +6,6 @@ import ( "fmt" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-sdk/go/catalogue" - "strconv" "strings" "time" @@ -30,14 +29,11 @@ type VersionNumbers struct { N *versions.Numberer } -// ClaimOf is the claim a module value's trunk build is numbered by: a package per source revision (Src), the rest per -// fingerprint, in merge order (Src). +// ClaimOf is the claim a module value's trunk build is numbered by, per fingerprint: a module value carries no source stamp +// (design 47 H H-A-48), so the claim names no source revision and no merge. func ClaimOf(module string, m meshcatalog.ModulesByModuleValue, plan Plan) versions.BuildClaim { c := versions.BuildClaim{Module: module, Path: m.Path, Repository: trunk.RepositoryName(m.Repository), Fingerprint: m.Fp2, - Commit: m.Commit, Merge: uint64(m.Src)} - if uint64(m.Src) != 0 { - c.Revision = strconv.FormatUint(uint64(m.Src), 10) - } + Commit: m.Commit} for _, t := range plan.Tasks { c.Artifacts = append(c.Artifacts, versions.ClaimArtifact{Artifact: t.Artifact, Kind: t.Kind, Language: t.Language}) } diff --git a/modules/mesh-delivery-builder/internal/build/run.go b/modules/mesh-delivery-builder/internal/build/run.go index 9b923a6a..fcabe73a 100644 --- a/modules/mesh-delivery-builder/internal/build/run.go +++ b/modules/mesh-delivery-builder/internal/build/run.go @@ -9,7 +9,6 @@ import ( "maps" "slices" "sort" - "strconv" "strings" "sync" "time" @@ -266,7 +265,7 @@ func (b *Builder) take(ctx context.Context, o Origin, key string, m meshcatalog. } // ModuleOf is a catalog entry in the form the builder builds by: its fingerprint, manifest digest, source (repository, -// directory, commit, the source revision it was taken in at), the toolchains its fingerprint was computed with, the bases it is +// directory, commit; no source stamp, design 47 H H-A-48), the toolchains its fingerprint was computed with, the bases it is // built against, its packages and needs, and the named paths of its artifacts. func ModuleOf(e meshcatalog.CatalogEntry) meshcatalog.ModulesByModuleValue { m := meshcatalog.ModulesByModuleValue{Repository: e.Repository, Path: e.Path, Commit: e.Commit, Fp2: e.Fp, None2: e.None, @@ -275,7 +274,7 @@ func ModuleOf(e meshcatalog.CatalogEntry) meshcatalog.ModulesByModuleValue { m.Inputs2 = &meshcatalog.ModulesByModuleValueInputs2{Bases: e.Bases} } if in := e.Inputs; in != nil { - m.Src, m.Toolchains, m.Problems = in.Src, in.Toolchains, in.Problems + m.Toolchains, m.Problems = in.Toolchains, in.Problems if len(in.Paths) > 0 { if m.Inputs2 == nil { m.Inputs2 = &meshcatalog.ModulesByModuleValueInputs2{} @@ -1217,15 +1216,15 @@ func (b *Builder) logBuild(e builds.BuildLogEntry) { b.Log(e) } -// LogStarted is the log's entry of a build of module's fingerprint starting (ADR 0571 §1): the module, the fingerprint, the -// source revision, where it is built from (repository, directory, commit, the candidate's ref), the node of the builder +// LogStarted is the log's entry of a build of module's fingerprint starting (ADR 0571 §1): the module, the fingerprint (no +// source stamp, design 47 H H-A-48), where it is built from (repository, directory, commit, the candidate's ref), the node of the builder // that asked it and the time. func LogStarted(module string, m meshcatalog.ModulesByModuleValue, ref, by string, at time.Time) builds.BuildLogEntry { dir := m.Path if dir == "" { dir = meshcatalogclient.RootPath } - return builds.BuildLogEntry{Event: builds.BuildStartedEvent, Module: module, FP: m.Fp2, Source: strconv.FormatUint(uint64(m.Src), 10), + return builds.BuildLogEntry{Event: builds.BuildStartedEvent, Module: module, FP: m.Fp2, Repository: m.Repository, Directory: dir, Commit: m.Commit, Ref: ref, Node: by, At: at.UTC()} } diff --git a/modules/mesh-delivery-builder/internal/versions/floor.go b/modules/mesh-delivery-builder/internal/versions/floor.go index 2ef45300..a186bcb3 100644 --- a/modules/mesh-delivery-builder/internal/versions/floor.go +++ b/modules/mesh-delivery-builder/internal/versions/floor.go @@ -66,8 +66,9 @@ const ( WakePublished = "published" // WakeRevision is a catalog revision the builder hears (the catalog half's entry changed): tags and requirements are asked again. WakeRevision = "catalog-revision" - // WakeAccepted is the next `manifest-accepted` of the seat mesh-catalog. - WakeAccepted = "manifest-accepted" + // WakeTakenIn is the indexer's next `take-in.taken`, in place of the retired manifest-accepted: the floor looks at every + // module again (design 47 H H-A-116). + WakeTakenIn = "take-in.taken" // WakeCleared is the condition keeper's clear of a silent module's health condition on `mesh-conditions`. WakeCleared = "condition-cleared" // WakeByHand is builder_floor_again, a hand act. @@ -75,7 +76,7 @@ const ( ) // sourceOfWake is the floor source each event says something of; a clear names the source by its module. -var sourceOfWake = map[string]string{WakePublished: SourceSeat, WakeAccepted: SourceRequirements} +var sourceOfWake = map[string]string{WakePublished: SourceSeat, WakeTakenIn: SourceRequirements} // SourceSeats are the seats whose holders are the floor's tag and requirement sources (design 47 G A-82): the seat git (tags) // and the seat mesh-catalog (requirements), the holders read from the seat list and the catalog half. diff --git a/modules/mesh-delivery-builder/internal/versions/versions_test.go b/modules/mesh-delivery-builder/internal/versions/versions_test.go index da2ec6d5..6b04ce65 100644 --- a/modules/mesh-delivery-builder/internal/versions/versions_test.go +++ b/modules/mesh-delivery-builder/internal/versions/versions_test.go @@ -269,7 +269,7 @@ func TestTheBuildersVersions(t *testing.T) { want []string }{ {WakePublished, "oci-artifact-registry", "", "", nil}, // the seat answered already: a publish wakes nothing - {WakeAccepted, "", "", "", nil}, + {WakeTakenIn, "", "", "", nil}, {WakeRevision, "", "", "", []string{"m/program"}}, {WakeCleared, "", "mesh-delivery-forge-bridge", "", []string{"m/program"}}, // recovered with nothing to say {WakeCleared, "", "mesh-delivery-indexer", "", nil}, diff --git a/modules/mesh-delivery-builder/manifest_test.go b/modules/mesh-delivery-builder/manifest_test.go index f833d98a..638ea6aa 100644 --- a/modules/mesh-delivery-builder/manifest_test.go +++ b/modules/mesh-delivery-builder/manifest_test.go @@ -154,13 +154,14 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 // The builder hears the build seats' toolchain-reported and the node-engine seat's running (design 47 G §492, §546) and reads no // build module's toolchains; and by the seat mesh-artifact-sweep's artifacts-gone, a blob the sweep found gone (1937/AC4, 1955/L7), the // controller's no longer (1126/B9, ADR 0590). - // Design 51 *The floor*: the indexer's manifest-accepted and the condition keeper's condition-cleared wake a silent floor. + // Design 51 *The floor*: the indexer's take-in.taken (design 47 H H-A-116) and the condition keeper's condition-cleared wake + // a silent floor. // DLV-B4: the four registry seats' outcomes, each a seat event `.` on the shared events stream. // DLV-M1: the indexer's and the assigner's views families of the states it follows. if want := []string{"container-image-registry.kept", "container-image-registry.published", "container-image-registry.refused", "container-image-registry.removed", "go-package-registry.kept", "go-package-registry.published", "go-package-registry.refused", "go-package-registry.removed", - "mesh-artifact-sweep.artifacts-gone", "mesh-catalog.manifest-accepted", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-catalog.version-released", "mesh-catalog.versions-filled", - "mesh-conditions.condition-cleared", "mesh-conditions.findings-asked", "seat:mesh-module-assigner.views.assignments", + "mesh-artifact-sweep.artifacts-gone", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-catalog.version-released", "mesh-catalog.versions-filled", + "mesh-conditions.condition-cleared", "mesh-conditions.findings-asked", "mesh-delivery-indexer.take-in.taken", "seat:mesh-module-assigner.views.assignments", "seat:mesh-module-assigner.views.seat-holders", // A-298: the claimant's seat-holders view resolves the callers' seat entries "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "node-build-go.toolchain-reported", "node-build-node.toolchain-reported", "node-engine.manifest-reads-changed", "node-engine.running", "npm-package-registry.kept", "npm-package-registry.published", "npm-package-registry.refused", "npm-package-registry.removed", diff --git a/modules/mesh-delivery-builder/module.json b/modules/mesh-delivery-builder/module.json index f98edc9c..10a21eae 100644 --- a/modules/mesh-delivery-builder/module.json +++ b/modules/mesh-delivery-builder/module.json @@ -54,12 +54,12 @@ "go-package-registry.refused", "go-package-registry.removed", "mesh-artifact-sweep.artifacts-gone", - "mesh-catalog.manifest-accepted", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-catalog.version-released", "mesh-catalog.versions-filled", "mesh-conditions.condition-cleared", + "mesh-delivery-indexer.take-in.taken", "node-build-go.toolchain-reported", "node-build-node.toolchain-reported", "node-engine.manifest-reads-changed", diff --git a/modules/mesh-delivery-checker/checkermsg/checkermsg.go b/modules/mesh-delivery-checker/checkermsg/checkermsg.go deleted file mode 100644 index 978f52f2..00000000 --- a/modules/mesh-delivery-checker/checkermsg/checkermsg.go +++ /dev/null @@ -1,56 +0,0 @@ -// Package checkermsg is the checker's own published layouts (as forgemsg is the forge bridge's): the bodies of its events on the -// stream checks and the answer of its tool checker_tries. It is what the checker says on its stream checks when a whole try (a try of a delivery group, not narrowed -// to a level or to some tests) starts and ends (issue 1921), and what the indexer, which hears it, reads: a try has no pull -// request, so the indexer writes the candidate of its group from the members at their commits, and deletes it at the end. -// The events carry the members, so no part reads the checker's state. -package checkermsg - -import ( - "encoding/json" - "fmt" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" -) - -// The events of the checker's stream checks. -const ( - Started = "try.started" - Ended = "try.ended" -) - -// Member is one repository of the try: the exact commit and the trunk commit it is merged onto. -type Member struct { - Repository string `json:"repository"` - Head string `json:"head"` - Trunk string `json:"trunk"` -} - -// Event is the body of both events: the try, and for Started its members; for Ended the state it ended in. -type Event struct { - ID string `json:"id"` - Members []Member `json:"members,omitempty"` - State string `json:"state,omitempty"` -} - -// Decode reads an event body; a started event names its members. -func Decode(event string, body []byte) (Event, error) { - var e Event - if err := json.Unmarshal(body, &e); err != nil { - return e, err - } - if !meshcatalogclient.IsTryGroup(e.ID) { - return e, fmt.Errorf("%q is not a try's id, try-", e.ID) - } - if event == Started && len(e.Members) == 0 { - return e, fmt.Errorf("the try %s starts with no member", e.ID) - } - return e, nil -} - -// Open is one try that waits or runs, as the checker's tool checker_tries answers it. -type Open struct { - ID string `json:"id"` - State string `json:"state"` - Whole bool `json:"whole"` - Members []Member `json:"members"` -} diff --git a/modules/mesh-delivery-checker/checkermsg/go.mod b/modules/mesh-delivery-checker/checkermsg/go.mod deleted file mode 100644 index 48e0db10..00000000 --- a/modules/mesh-delivery-checker/checkermsg/go.mod +++ /dev/null @@ -1,22 +0,0 @@ -module git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg - -go 1.26.0 - -require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 - -require ( - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 // indirect -) - -require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 // indirect - -require ( - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 // indirect - github.com/klauspost/compress v1.20.1 // indirect - github.com/nats-io/nats.go v1.54.0 // indirect - github.com/nats-io/nkeys v0.4.16 // indirect - github.com/nats-io/nuid v1.0.1 // indirect - golang.org/x/crypto v0.57.0 // indirect - golang.org/x/sys v0.48.0 // indirect -) diff --git a/modules/mesh-delivery-checker/checkermsg/go.sum b/modules/mesh-delivery-checker/checkermsg/go.sum deleted file mode 100644 index 0daef11f..00000000 --- a/modules/mesh-delivery-checker/checkermsg/go.sum +++ /dev/null @@ -1,12 +0,0 @@ -github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ= -github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= -github.com/nats-io/nats.go v1.54.0 h1:vsXoOxjHp/GmPUN+EcI7uOf/uB+iAP+kEsAFNQN0yzA= -github.com/nats-io/nats.go v1.54.0/go.mod h1:y+DZoD1oBOYfZTU681eTUiUjI0vbqYGixNVFHcjHJ0k= -github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg= -github.com/nats-io/nkeys v0.4.16/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs= -github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= -github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= -golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= -golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= diff --git a/modules/mesh-delivery-checker/cmd/mesh-checking-program/main.go b/modules/mesh-delivery-checker/cmd/mesh-checking-program/main.go deleted file mode 100644 index fc829bc8..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-checking-program/main.go +++ /dev/null @@ -1,52 +0,0 @@ -// Command mesh-checking-program is the checker's checking program (novox/hq ADR 0644, ADR 0656; design 47's bootstrap -// delivery): built from a check's heads and run in the build seat's sandbox through the seat node-build-go's ask run, -// with no network and no bus account, it reads the check's facts from the file MESH_RUN_INPUT names and answers the -// verdict the judging rules give them, as one JSON line on its standard output (rules.Answer). -// -// mesh-checking-program -verdict -// mesh-checking-program -seats -// -// Asked -seats, it answers the seat list of the heads laid in the directory MESH_RUN_HEADS names (rules.SeatsAnswer). -// It refuses facts of a version it does not read, naming it, with exit 2. -package main - -import ( - "fmt" - "io" - "io/fs" - "os" - "slices" - - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -func main() { - os.Exit(run(os.Args[1:], os.Getenv, os.Stdout, os.Stderr)) -} - -// run is the command over its arguments, environment and outputs, answering its exit. -func run(args []string, getenv func(string) string, stdout, stderr io.Writer) int { - var heads fs.FS - if dir := getenv(buildstep.RunHeadsEnv); dir != "" { - heads = os.DirFS(dir) - } - input := func() ([]byte, error) { - file := getenv(buildstep.RunInputEnv) - if file == "" { - return nil, fmt.Errorf("%s names no file", buildstep.RunInputEnv) - } - return os.ReadFile(file) - } - line, err := rules.Program(args, input, heads) - if err != nil { - if heads == nil && slices.Contains(args, "-seats") { - err = fmt.Errorf("no heads: %s names no directory", buildstep.RunHeadsEnv) - } - fmt.Fprintf(stderr, "mesh-checking-program: %v\n", err) - return 2 - } - fmt.Fprintf(stdout, "%s\n", line) - return 0 -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-checking-program/main_test.go b/modules/mesh-delivery-checker/cmd/mesh-checking-program/main_test.go deleted file mode 100644 index 972fa7d8..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-checking-program/main_test.go +++ /dev/null @@ -1,342 +0,0 @@ -package main - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "io/fs" - "os" - "path" - "path/filepath" - "reflect" - "strings" - "testing" - - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/seatdefs" - "git.novox.be/novox/mesh-sdk/go/seatdef" -) - -// 1921 (B0, D2): the checking program answers, on its last line, the verdict the rules give the facts in its input, and -// refuses facts of a version it does not read, an input it is not given, and a question it is not asked, naming each. -// 1921 (B0, D3): asked -seats, it answers the seat list of the heads laid for it: a definition a head adds, resolved with -// the parts it includes; what it left out, and why; and an empty list for heads with no definition. -func TestTheCheckingProgramAnswersTheVerdictOfItsFacts(t *testing.T) { - failing, _ := json.Marshal(rules.Tested{Level: buildstep.LevelUnit, Tests: 3, Failed: []rules.Failure{{Package: "x/a", Test: "TestA", Cause: "a_test.go:9: boom"}}}) - facts := rules.Facts{V: rules.FactsVersion, Group: "feat/x", Heads: "0123456789ab", Members: []rules.Member{{Repository: "novox/x", Number: 3, Head: "h"}}, - Steps: []rules.Step{ - {Repository: "novox/x", Number: 3, Module: "mesh-build-go", Node: "laptop", Level: &buildstep.LevelAnswer{Level: buildstep.LevelUnit, Tests: []json.RawMessage{failing}}}, - {Repository: "novox/x", Number: 3, Failed: &rules.FailedFirst{Machine: "laptop", Words: "novox/x#3 has files in conflict: go.mod"}}, - }} - dir := t.TempDir() - write := func(name string, v any) string { - data, _ := json.Marshal(v) - p := filepath.Join(dir, name) - if err := os.WriteFile(p, data, 0o600); err != nil { - t.Fatal(err) - } - return p - } - v1 := write("v1.json", facts) - later := facts - later.V = rules.FactsVersion + 1 - v2 := write("v2.json", later) - // The heads as the build node lays them, each at its repository's path: a contracts module of its own defining a seat - // with a part, and the mesh's own contracts module defining a seat of a domain that is not its (ADR 0636 §5). - lay := func(name string, files map[string]string) string { - root := filepath.Join(dir, name) - for p, body := range files { - if err := os.MkdirAll(filepath.Dir(filepath.Join(root, p)), 0o755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(root, p), []byte(body), 0o600); err != nil { - t.Fatal(err) - } - } - return root - } - contracts := func(module string) string { - return `{"module": "` + module + `", "build": {"artifacts": [{"name": "go", "kind": "package", "language": "go", "generated-from": "."}]}}` - } - heads := lay("heads", map[string]string{ - "novox/x/x-contracts/module.json": contracts("x-contracts"), - "novox/x/x-contracts/seats/node-build-x/seat.json": `{"format":1,"seat":"node-build-x","scope":"node","grain":"whole","domain":"Delivery","decision":"ADR 1","description":"kept","includes":["build"],"verbs":[{"name":"own","input":{"type":"object","properties":{"credentials":{"$ref":"#/defs/Sealed"}}},"answer":{"type":"object"}}],"defs":{"Sealed":{"type":"object"}}}`, - "novox/x/x-contracts/parts/build.json": `{"format":1,"part":"build","verbs":[{"name":"current","input":{"type":"object"},"answer":{"type":"object"}}],"events":[{"name":"ended","body":{"type":"object"}}]}`, - "novox/x/contracts/module.json": contracts("mesh-contracts"), - "novox/x/contracts/seats/mesh-catalog/seat.json": `{"format":1,"seat":"mesh-catalog","scope":"mesh","grain":"whole","domain":"Delivery","decision":"ADR 1"}`, - }) - broken := lay("broken", map[string]string{ - "novox/x/x-contracts/module.json": contracts("x-contracts"), - "novox/x/x-contracts/seats/broken/seat.json": `{"format":1,"seat":"broken","scope":"nowhere"}`, - }) - none := filepath.Join(dir, "no-heads") - if err := os.MkdirAll(filepath.Join(none, "novox/x"), 0o755); err != nil { - t.Fatal(err) - } - for name, c := range map[string]struct { - args []string - input, heads string - exit int - said string - }{ - "the seats": {args: []string{"-seats"}, input: v1, heads: heads}, - "no seats": {args: []string{"-seats"}, input: v1, heads: none}, - "a seat unparsed": {args: []string{"-seats"}, input: v1, heads: broken, exit: 2, said: "the seat definition of broken (module x-contracts) at the head of novox/x is not taken"}, - "no heads laid": {args: []string{"-seats"}, exit: 2, said: buildstep.RunHeadsEnv + " names no directory"}, - "the facts": {args: []string{"-verdict"}, input: v1}, - "a later version": {args: []string{"-verdict"}, input: v2, exit: 2, said: "facts of version 2, this checking program reads version 1"}, - "no input": {args: []string{"-verdict"}, exit: 2, said: buildstep.RunInputEnv + " names no file"}, - "nothing asked": {input: v1, exit: 2, said: "nothing asked"}, - "an unknown asking": {args: []string{"-judge"}, input: v1, exit: 2, said: "-judge"}, - } { - var out, errs bytes.Buffer - exit := run(c.args, func(k string) string { - switch k { - case buildstep.RunInputEnv: - return c.input - case buildstep.RunHeadsEnv: - return c.heads - } - return "" - }, &out, &errs) - if exit != c.exit || !strings.Contains(errs.String(), c.said) { - t.Errorf("%s: exit %d, said %q; want %d and %q", name, exit, errs.String(), c.exit, c.said) - continue - } - if c.exit != 0 { - continue - } - lines := strings.Split(strings.TrimSpace(out.String()), "\n") - if c.heads != "" { - var sa rules.SeatsAnswer - if err := json.Unmarshal([]byte(lines[len(lines)-1]), &sa); err != nil || sa.V != rules.SeatsVersion || sa.Seats == nil { - t.Fatalf("%s: the last line %q: %v", name, lines[len(lines)-1], err) - } - if c.heads == none { - if len(sa.Seats) != 0 || len(sa.Left) != 0 { - t.Errorf("%s: %+v, want an empty list", name, sa) - } - continue - } - var d struct { - Seat string `json:"seat"` - Verbs []struct { - Name string `json:"name"` - } `json:"verbs"` - } - if len(sa.Seats) == 1 { - _ = json.Unmarshal(sa.Seats[0], &d) - } - if d.Seat != "node-build-x" || len(d.Verbs) != 2 || len(sa.Left) != 1 || !strings.Contains(sa.Left[0].Why, "whose seats mesh-delivery-contracts defines") { - t.Errorf("%s: seats %s, left %+v; want node-build-x with its part's verb, and mesh-catalog left out", name, sa.Seats, sa.Left) - } - // The group check of 616c42434ac2: a checker from before G (no digests in these facts) writes the answer into the - // build seat's step state, whose writer refuses a value with a field named like a credential, here a schema's - // property name; the answer carries no verb's or message's schema and no type only they referred to. - if len(sa.Seats) == 1 && (bytes.Contains(sa.Seats[0], []byte("credentials")) || bytes.Contains(sa.Seats[0], []byte("Sealed"))) { - t.Errorf("%s: %s carries a verb's schema to a checker that hands no digests", name, sa.Seats[0]) - } - continue - } - var a rules.Answer - if err := json.Unmarshal([]byte(lines[len(lines)-1]), &a); err != nil || a.V != rules.AnswerVersion { - t.Fatalf("%s: the last line %q: %v", name, lines[len(lines)-1], err) - } - if want := rules.Judge(facts); !reflect.DeepEqual(a.Verdict, want) || a.Verdict.State != meshdeliverycheckerclient.VerdictFail || len(a.Verdict.Failures) != 2 || - a.Verdict.Failures[1].Words != "novox/x#3 has files in conflict: go.mod" || a.Verdict.Machines[0] != "laptop" || a.Verdict.Levels[buildstep.LevelUnit].Run != 3 { - t.Errorf("%s: the verdict %+v, want %+v", name, a.Verdict, want) - } - } - // Review finding 3 of milestone 47/G: the heads of a real group carry every seat definition the reader holds, unchanged - // (this repository's own contracts, laid as its head): the answer leaves them all to the checker's list and stays - // far below the whole list. - t.Run("heads carrying every definition the reader holds unchanged answer none of them", func(t *testing.T) { - real := under{prefix: "novox/mesh-delivery", fs: os.DirFS("../../../..")} - group := rules.Facts{V: rules.FactsVersion, Members: []rules.Member{{Repository: "novox/mesh-delivery", Head: "h"}}} - // An empty list of digests answers every definition whole (a checker that hands its list's digests, holding none). - whole, err := rules.SeatsOf(real, rules.Facts{V: rules.FactsVersion, Members: group.Members, SeatDigests: map[string]string{}}) - if err != nil || len(whole.Seats) < 10 { - t.Fatalf("the repository's own definitions: %d, %v", len(whole.Seats), err) - } - // The reader's digests are of each definition whole, as the seat mesh-catalog answers it; the answer is its judging view. - group.SeatDigests = wholeDigests(t, real, "novox/mesh-delivery") - same, err := rules.SeatsOf(real, group) - answer, _ := json.Marshal(same) - if err != nil || len(same.Seats) != 0 || same.Same != len(whole.Seats) { - t.Errorf("unchanged heads answer %d definitions (%d the same), %d bytes; want none, all %d the same: %v", - len(same.Seats), same.Same, len(answer), len(whole.Seats), err) - } - // Review round 2 of G: a checker from before G (B0's, which makes G's own check) hands no digests, and the answer - // then keeps each seat's own description only, so a group's whole list stays within the bound; each still parses. - b0, err := rules.SeatsOf(real, rules.Facts{V: rules.FactsVersion, Members: group.Members}) - if err != nil || len(b0.Seats) != len(whole.Seats) { - t.Fatalf("B0-shaped facts: %d definitions, %v", len(b0.Seats), err) - } - for i, raw := range b0.Seats { - var own struct { - Description string `json:"description"` - } - _ = json.Unmarshal(whole.Seats[i], &own) - var d struct { - Description string `json:"description"` - Verbs []json.RawMessage `json:"verbs"` - Events []json.RawMessage `json:"events"` - } - _ = json.Unmarshal(raw, &d) - inner := 0 - for _, m := range append(d.Verbs, d.Events...) { - inner += strings.Count(string(m), `"description":"`) - } - parsed, perr := seatdef.Parse(raw) - for _, v := range parsed.Verbs { - if len(v.Input) != 0 || len(v.Answer) != 0 { - t.Errorf("B0-shaped facts answer %s's verb %s with its schemas; want none", parsed.Seat, v.Name) - } - } - for name := range parsed.Defs { - if !bytes.Contains(raw, []byte(`"#/defs/`+name+`"`)) { - t.Errorf("B0-shaped facts answer %s with the type %s, which nothing it keeps refers to", parsed.Seat, name) - } - } - if inner > 0 || d.Description != own.Description || perr != nil { - t.Errorf("B0-shaped facts answer %s with %d inner descriptions, its own %q: %v; want only its own", raw[:min(len(raw), 80)], inner, d.Description, perr) - } - } - if answer, _ := json.Marshal(b0); len(answer) >= len(mustJSON(t, whole))*3/4 { - t.Errorf("B0-shaped facts answer %d bytes (whole %d), want far below the whole", len(answer), len(mustJSON(t, whole))) - } - // One definition the heads change is answered alone. - for seat := range group.SeatDigests { - group.SeatDigests[seat] = "changed" - break - } - if one, err := rules.SeatsOf(real, group); err != nil || len(one.Seats) != 1 { - t.Errorf("one changed definition: answered %d, %v", len(one.Seats), err) - } - }) - // The lead's ruling after G's group check of 616c42434ac2: with digests given too, a changed definition whose verb input - // has a property named credentials is answered as the validator and the composer read it, with no such field. - changed, err := rules.SeatsOf(os.DirFS(heads), rules.Facts{V: rules.FactsVersion, Members: facts.Members, - SeatDigests: map[string]string{"node-build-x": "changed"}}) - if err != nil || len(changed.Seats) != 1 || bytes.Contains(changed.Seats[0], []byte("credentials")) || bytes.Contains(changed.Seats[0], []byte("Sealed")) { - t.Errorf("a changed definition with digests given: %d answered, %s, %v; want it alone, with no verb's schema", len(changed.Seats), changed.Seats, err) - } - // Review round 1 of milestone 47/B0: the module graph's cycle rule is the program's (ADR 0603 §2, ADR 0656 §5), over the - // edges the running checker gathers. A cycle the change makes fails; one the trunk has among the same modules is noted. - web, mail := rules.Edge{From: "web", To: "mail", Kind: rules.EdgeAwaits}, rules.Edge{From: "mail", To: "web", Kind: rules.EdgeAwaits} - only := func(e rules.Edge, where string) rules.Edge { e.Only = where; return e } - for _, row := range []struct { - name string - graph rules.Graph - fails string - note string - }{ - {name: "no cycle", graph: rules.Graph{Trunk: true, Edges: []rules.Edge{web}}, note: "has no cycle"}, - {name: "a cycle the change makes", graph: rules.Graph{Trunk: true, Edges: []rules.Edge{mail, only(web, rules.EdgeChange)}}, fails: "has the cycle mail -> web -> mail"}, - {name: "a cycle the trunk has", graph: rules.Graph{Trunk: true, Edges: []rules.Edge{mail, web}}, note: "which the trunk already has"}, - {name: "the trunk unread", graph: rules.Graph{Edges: []rules.Edge{mail, web}}, fails: "has the cycle mail -> web -> mail"}, - {name: "a base on itself", graph: rules.Graph{Trunk: true, Edges: []rules.Edge{only(rules.Edge{From: "web", To: "web", Kind: rules.EdgeBase}, rules.EdgeChange)}}, fails: "has the cycle web -> web"}, - {name: "an edge only the trunk has", graph: rules.Graph{Trunk: true, Edges: []rules.Edge{mail, only(web, rules.EdgeTrunk)}}, note: "has no cycle"}, - } { - g := row.graph - a := rules.Judged(rules.Facts{V: rules.FactsVersion, Steps: []rules.Step{{Graph: &g}}}) - if row.fails != "" && (a.Verdict.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(a.Verdict.Failed.Words, row.fails)) { - t.Errorf("%s: verdict %+v, want a fail saying %q", row.name, a.Verdict, row.fails) - } - if row.note != "" && (a.Verdict.State != meshdeliverycheckerclient.VerdictPass || !strings.Contains(strings.Join(a.Notes, "\n"), row.note)) { - t.Errorf("%s: verdict %s, notes %q, want a pass noting %q", row.name, a.Verdict.State, a.Notes, row.note) - } - } -} - -// under is a directory laid at a repository's path of the heads, as the build node lays a head. -type under struct { - prefix string - fs fs.FS -} - -func (u under) Open(name string) (fs.File, error) { - if name == u.prefix { - return u.fs.Open(".") - } - if rest, ok := strings.CutPrefix(name, u.prefix+"/"); ok { - return u.fs.Open(rest) - } - return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} -} - -func mustJSON(t *testing.T, v any) []byte { - t.Helper() - out, err := json.Marshal(v) - if err != nil { - t.Fatal(err) - } - return out -} - -// wholeDigests are the digests of a head's seat definitions whole (rules.SeatDigest), read by the domain SDK as the -// indexer reads them: what a checker's reader holds for heads that change none. -func wholeDigests(t *testing.T, root fs.FS, repository string) map[string]string { - t.Helper() - head, err := fs.Sub(root, repository) - if err != nil { - t.Fatal(err) - } - manifests := map[string]string{} - _ = fs.WalkDir(head, ".", func(p string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() && (d.Name() == ".git" || d.Name() == "testdata" || d.Name() == "node_modules") { - return fs.SkipDir - } - if !d.IsDir() && d.Name() == "module.json" { - raw, _ := fs.ReadFile(head, p) - manifests[path.Dir(p)] = string(raw) - } - return nil - }) - forge := seatdefs.Forge{ - Files: func(_ context.Context, _, _ string, dirs []string) (map[string]map[string]string, error) { - out := map[string]map[string]string{} - for _, dir := range dirs { - files := map[string]string{} - if err := fs.WalkDir(head, dir, func(p string, d fs.DirEntry, err error) error { - if err == nil && !d.IsDir() { - files[strings.TrimPrefix(p, dir+"/")] = "" - } - return err - }); err == nil { - out[dir] = files - } - } - return out, nil - }, - File: func(_ context.Context, _, _, file string) ([]byte, error) { - raw, err := fs.ReadFile(head, file) - if errors.Is(err, fs.ErrNotExist) { - return nil, nil - } - return raw, err - }, - } - out := map[string]string{} - if len(manifests) == 0 { - t.Fatalf("no manifest at the head of %s", repository) - } - for _, c := range seatdefs.ContractsModules(manifests) { - defs, _, err := forge.Read(context.Background(), repository, "h", c) - if err != nil { - t.Fatal(err) - } - for seat, d := range defs { - raw, _ := json.Marshal(d) - out[seat] = rules.SeatDigest(raw) - } - } - return out -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main.go deleted file mode 100644 index eceea22b..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main.go +++ /dev/null @@ -1,575 +0,0 @@ -// Command mesh-delivery-checker-tools serves the checker's read tools (novox/hq ADR 0484 clause 13, issue 1686): the runtime -// launches it for a call, it reads the checker's verdicts and kept states through the checker's state and reads, and the -// indexer's candidates and modules through the indexer's read tools. It writes only the tries (the try's tools) and, asking a check again by hand, one verdict. The checker is the other program: it serves no tool (ADR 0437 §4). -package main - -import ( - "encoding/json" - "fmt" - "os" - "strings" - "sync/atomic" - "time" - - "mesh-delivery-checker/internal/check" - - "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/buscalls" - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" - "git.novox.be/novox/mesh-sdk/go/views" -) - -// reads counts the values this call read from the bus: the runtime launches the tools for one call, so the count is the call's. -var reads atomic.Int64 - -// cutAnswer says in the answer that it was cut, and how to narrow it. -func cutAnswer(answer map[string]any, cut string) map[string]any { - if cut != "" { - answer["cut"], answer["note"] = true, cut - } - return answer -} - -// logged runs every tool with one line to stderr per call: the tool, the values it read and the time it took. -func logged(tools []stdio.Tool) []stdio.Tool { - for i := range tools { - name, run := tools[i].Name, tools[i].Run - tools[i].Run = func(args map[string]any) (any, error) { - reads.Store(0) - start := time.Now() - out, err := run(args) - fmt.Fprintf(os.Stderr, "[mesh-delivery-checker-tools] %s read %d values in %s (error: %v)\n", name, reads.Load(), time.Since(start).Round(time.Millisecond), err) - return out, err - } - } - return tools -} - -func read(s stdio.KeptState) check.StateRead { - return check.StateRead{Keys: s.Keys, Get: func(key string) (json.RawMessage, error) { - reads.Add(1) - e, err := s.Get(key) - if err != nil || e == nil { - return nil, err - } - return e.Value, nil - }} -} - -// ownerRead is a state of another module read through that module's ordinary read tool (views.ReadTool), asked through ask -// (stdio.Ask, or a test's): its keys with no argument, one key's value with key. Another module's data is asked through its -// tools, never read from its bus state (novox/hq ADR 0596); the manifest invokes the tool, and the owner answers every reader -// that invokes it. Each read is asked when it is read: the owner's state is the truth, and the tools keep no copy. -func ownerRead(ask func(string, any) (json.RawMessage, error), tool string) check.StateRead { - call := func(args map[string]any, into any) error { - reads.Add(1) - raw, err := ask(tool, args) - if err != nil { - return fmt.Errorf("%s does not answer: %w", tool, err) - } - answer, said, ok := buscalls.Unwrap(raw) - if !ok { - return fmt.Errorf("%s answered: %s", tool, said) - } - if err := json.Unmarshal(answer, into); err != nil { - return fmt.Errorf("%s answered what does not decode: %w", tool, err) - } - return nil - } - return check.StateRead{ - Keys: func() ([]string, error) { - var a views.ReadAnswer - if err := call(map[string]any{}, &a); err != nil { - return nil, err - } - return a.Keys, nil - }, - Get: func(key string) (json.RawMessage, error) { - var a views.KeyAnswer - if err := call(map[string]any{"key": key}, &a); err != nil { - return nil, err - } - if !a.Found { - return nil, nil - } - return a.Value, nil - }} -} - -// The indexer's read tools the tools ask for its candidates and modules (the manifest's invokes name them under the indexer's -// module; the call goes to the holder of the seat mesh-catalog, atHolder). -const ( - indexerCandidatesTool = "indexer_read_candidates" - indexerModulesTool = "indexer_read_modules" -) - -// atHolder asks a tool of the module that holds the seat when it is asked (design 47 G A-210, §428), as the view's tools do: -// the holder is found by the seat list for each call, and a seat with no holder is an error that says so. -func atHolder(ask func(string, any) (json.RawMessage, error), holder func(seat string) (string, bool), seat string) func(string, any) (json.RawMessage, error) { - return func(tool string, body any) (json.RawMessage, error) { - module, ok := holder(seat) - if !ok { - return nil, fmt.Errorf("the holder of %s is not found, so %s is not asked", seat, tool) - } - return ask(module+"."+tool, body) - } -} - -// runsOf says which commit the mesh runs of a module from the indexer's modules state, as the checker's own record of it does -// (the commit the module's record was computed at); "" when the record is not readable. -type runsOf struct{ modules check.StateRead } - -func (r runsOf) CommitOf(module string) string { - if r.modules.Get == nil { - return "" - } - raw, err := r.modules.Get(module) - if err != nil || len(raw) == 0 { - return "" - } - m, err := meshcatalogclient.DecodeModule(raw) - if err != nil { - return "" - } - return m.Commit -} - -// stateStore is a state the tools write: the tries. -type stateStore struct { - check.StateRead - create func(key string, value []byte) error - entry func(key string) (json.RawMessage, uint64, error) - update func(key string, value []byte, revision uint64) error -} - -func (s stateStore) Entry(key string) (json.RawMessage, uint64, error) { return s.entry(key) } -func (s stateStore) Update(key string, value []byte, revision uint64) error { - return s.update(key, value, revision) -} - -func (s stateStore) Keys() ([]string, error) { return s.StateRead.Keys() } -func (s stateStore) Get(key string) (json.RawMessage, error) { return s.StateRead.Get(key) } -func (s stateStore) Create(key string, value []byte) error { return s.create(key, value) } - -// Trying is what the try's tools work on: the tries, and the gate's trunk heads. -type Trying struct { - Tries check.TryStore - Ender check.TryEnder - Trunks func() (map[string]string, error) - Now func() time.Time -} - -// TryTools are the try's tools (ADR 0501 §5): ask one, read one, and end one by hand (issue 734). mesh-controller's try calls them. -func TryTools(tr Trying) []stdio.Tool { - str := func(args map[string]any, k string) string { s, _ := args[k].(string); return s } - return []stdio.Tool{ - {Name: check.TryTool, - Description: "Try a delivery group, or one pushed head, before its pull requests are opened (try a group, group try, try several repositories): " + - "name the members, each a repository and the exact commit asked (never the branch's head now, so a push after the ask changes nothing of what is tried), " + - "or one repository and its head for a try of one member (the seat verb try of mesh-delivery-checker takes the same members, ADR 0637 §6). The checker judges it by the code a group's pull-request check runs, so the two cannot drift: " + - "each member merged onto its trunk's head, the unit and the integration level of every member through the build modules, the manifest validator over " + - "the members' manifests, the module graph, and the composition on every placed node. Every failure is named (each member, level, test, manifest and node), not " + - "the first. A try owes the forge nothing (no status, no Approve asked) and writes no verdict of a pull request; what its passing levels prove is kept, so a pull request's " + - "check of the same heads on the same trunk runs only what the try did not pass. A try narrowed to a level or to some tests judges only those, no manifest and no composition, and keeps nothing of tests. " + - "Answers the try's id at once; " + check.TryReadTool + " reads where it stands, " + check.TryEndTool + " ends it by hand. A try of the same commits that waits or runs is the answer to a second ask " + - "(whatever the order of the members); at most 4 run on a build node, and 8 wait for one, mesh-wide. A repository the checker checks no level of (one whose trunk the gate does not name) " + - "is refused. A try whose ask no build node took within the step bound ends as error; its ask stays in the build seat's stream and may still run later, judged by nobody (issue 734).", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "members": map[string]any{"type": "array", "description": "the group: one repository at most once, each at the exact commit asked and the trunk commit it is merged onto", - "items": map[string]any{"type": "object", "required": []string{"repository", "head"}, "properties": map[string]any{ - "repository": map[string]any{"type": "string", "description": "/"}, - "head": map[string]any{"type": "string", "description": "the full commit to try, 40 or 64 hex digits"}, - "trunk": map[string]any{"type": "string", "description": "the trunk commit to merge it onto, or the trunk's branch for its head now; the trunk's head when absent"}}}}, - "repository": map[string]any{"type": "string", "description": "/, for a try of one member (instead of members)"}, - "head": map[string]any{"type": "string", "description": "the full commit to try, 40 or 64 hex digits (with repository)"}, - "trunk": map[string]any{"type": "string", "description": "the trunk commit to merge it onto, or the trunk's branch for its head now; the trunk's head when absent (with repository)"}, - "tests": map[string]any{"type": "string", "description": "a go test -run pattern: only those tests run, and nothing is kept"}, - "level": map[string]any{"type": "string", "enum": []string{"unit", "integration"}, "description": "only this level; both when absent"}}}, - Run: func(args map[string]any) (any, error) { - if tr.Tries == nil || tr.Trunks == nil { - return nil, fmt.Errorf("the tries state cannot be written here") - } - trunks, err := tr.Trunks() - if err != nil { - return nil, fmt.Errorf("the trunk heads cannot be read: %w", err) - } - now := time.Now - if tr.Now != nil { - now = tr.Now - } - var members []check.TryAskMember - if raw, ok := args["members"]; ok { - list, isList := raw.([]any) - if !isList { - return nil, fmt.Errorf("members is a list of {repository, head, trunk}") - } - for _, item := range list { - m, isMap := item.(map[string]any) - if !isMap { - return nil, fmt.Errorf("a member is {repository, head, trunk}") - } - members = append(members, check.TryAskMember{Repository: str(m, "repository"), Head: str(m, "head"), Trunk: str(m, "trunk")}) - } - } - id, err := check.AskTry(tr.Tries, trunks, check.TryAsk{Repository: str(args, "repository"), Head: str(args, "head"), - Trunk: str(args, "trunk"), Tests: str(args, "tests"), Level: str(args, "level"), Members: members}, now()) - if err != nil { - return nil, err - } - return map[string]any{"id": id}, nil - }}, - {Name: check.TryReadTool, - Description: "Where a try (of a group or of one head) stands, read whole, as stored: queued, running, or its end (pass, fail, or error when it could not run, or " + - "when its manifests and composition could not be judged), with what was said of it, every member's commit and the trunk commit it was merged onto, the time it took, " + - "every failure one line each (members, levels, tests, manifests, the composition per node), the levels' tests run and kept, the composition on every placed node, " + - "the notes of the check (manifests judged, module graph, kept verdicts); while it runs, each ask it put as soon as it is put, " + - "with the node that took it, its state and the phase its step says (fetch and merge, a level's digest, lint, tests) and since when. Read-only.", - Input: map[string]any{"type": "object", "required": []string{"id"}, "properties": map[string]any{ - "id": map[string]any{"type": "string", "description": "the try's id, try-"}}}, - Run: func(args map[string]any) (any, error) { - if tr.Tries == nil { - return nil, fmt.Errorf("the tries state cannot be read here") - } - return check.ReadTry(tr.Tries, str(args, "id")) - }}, - {Name: check.TryEndTool, - Description: "End a try of a group (or of one head) that waits or runs, as error, by hand: say why (kept in what it says). Its place among the 4 is free at once, " + - "the checker stops waiting for its asks, and the next " + check.TryTool + " of its head asks anew with a new id. For a try whose ask " + - "was lost (no step runs for it in checker_status); the checker ends one by itself when the build seat gives its ask up, or no node " + - "takes it within the step bound. A try that ended already is refused.", - Input: map[string]any{"type": "object", "required": []string{"id", "why"}, "properties": map[string]any{ - "id": map[string]any{"type": "string", "description": "the try's id, try-"}, - "why": map[string]any{"type": "string", "description": "why it is ended, in words"}}}, - Run: func(args map[string]any) (any, error) { - if tr.Ender == nil { - return nil, fmt.Errorf("the tries state cannot be written here") - } - now := time.Now - if tr.Now != nil { - now = tr.Now - } - t, err := check.EndTry(tr.Ender, str(args, "id"), str(args, "why"), now()) - if err != nil { - return nil, err - } - return map[string]any{"id": t.ID, "state": t.State, "said": t.Said}, nil - }}, - } -} - -// OpenTriesTools is the tool that lists the tries that wait or run, each with its members at their commits, and whether it is -// a whole try (the indexer writes the candidate of a whole try's group): the read a part uses at its start to find the tries -// whose start event it missed, instead of reading the checker's state. -func OpenTriesTools(tr Trying) []stdio.Tool { - return []stdio.Tool{{Name: check.TriesTool, - Description: "The tries that wait or run now (a try of a group or of one head; try.started and try.ended on the checker's stream say the same), each with its id, state, its members at their exact commits and " + - "trunk commits, and whether it is a whole try (not narrowed to a level or to tests, so it is composed and its manifests judged). For a part that " + - "missed the checker's try.started or try.ended event (the indexer at its start); checker_try_read reads one try whole. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{}}, - Run: func(map[string]any) (any, error) { - if tr.Tries == nil { - return nil, fmt.Errorf("the tries state cannot be read here") - } - open, err := check.OpenTries(tr.Tries) - return map[string]any{"tries": open}, err - }}} -} - -// Seat is the seat the checker holds (novox/hq ADR 0547 §10, issue 774); its verbs are served as tools named .. -const Seat = "mesh-delivery-checker" - -// SeatTryTools are the try's tools as the seat's verbs: try, try-read and try-end, run by the same code as checker_try, -// checker_try_read and checker_try_end, so the answer is the same. The module tools stay beside them. -func SeatTryTools(tr Trying) []stdio.Tool { - verbs := map[string]string{check.TryTool: "try", check.TryReadTool: "try-read", check.TryEndTool: "try-end"} - var out []stdio.Tool - for _, t := range TryTools(tr) { - t.Name = Seat + "." + verbs[t.Name] - out = append(out, t) - } - return out -} - -// SeatInputTools are the seat's verb input (check.InputVerb): a checking program's input as the checker keeps it, asked by -// the Go build module that runs the program (buildstep.RunInput.Read), never read from the checker's state by it. -func SeatInputTools(r check.Reader) []stdio.Tool { - return []stdio.Tool{{Name: Seat + "." + meshdeliverychecker.VerbInput, - Description: "A checking program's input the checker keeps, by the sha256 of its exact bytes, as a run ask names it: the digest " + - "and the bytes, base64. A digest it keeps no input of, or bytes that read back as another digest, is an error. Read-only.", - Input: map[string]any{"type": "object", "required": []string{"digest"}, "properties": map[string]any{ - "digest": map[string]any{"type": "string", "description": "the input's sha256, 64 lowercase hex digits"}}}, - Run: func(args map[string]any) (any, error) { - digest, _ := args["digest"].(string) - return r.Input(digest) - }}} -} - -// Tools are the read tools over the checker's states. -func Tools(r check.Reader) []stdio.Tool { - return []stdio.Tool{ - {Name: check.VerdictsReadTool, - Description: "The verdicts the checker holds, each as stored under verdicts..: its state, every failure it names one line each (every member's failed tests, refused steps and conflicts, the manifests' refusals and the composition), the tests run, kept and skipped per level, the time of each part, the trunk heads it judged against, the composition and which checking program judged it (judged: its build and the heads it read), with the full " + - "report of the status each pull request carries (the line the forge shows is cut at 140 characters, this is not). With a group, that group's only. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "group": map[string]any{"type": "string", "description": "the delivery group's branch name"}}}, - Run: func(args map[string]any) (any, error) { - group, _ := args["group"].(string) - vs, cut, err := r.Verdicts(group) - return cutAnswer(map[string]any{"verdicts": vs}, cut), err - }}, - {Name: check.PullReadTool, - Description: "The verdict that judges one pull request, every failure it names one line each (all of them, not only the first the status line shows), which checking program judged it (judged: its build, its key and the heads it read), and the full report of its status mesh/check. Read-only.", - Input: map[string]any{"type": "object", "required": []string{"repository", "number"}, "properties": map[string]any{ - "repository": map[string]any{"type": "string", "description": "/"}, - "number": map[string]any{"type": "integer", "description": "the pull request's number"}}}, - Run: func(args map[string]any) (any, error) { - repository, _ := args["repository"].(string) - n, _ := args["number"].(float64) - return r.Pull(repository, int(n)) - }}, - {Name: check.KeptReadTool, - Description: "The kept verdicts of passing packages, each as stored under kept..: the package, its tests, the time they took, the toolchain " + - "and when it was kept. With a level, that level's; with a level and a digest, that one. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "level": map[string]any{"type": "string", "enum": []string{"unit", "integration"}, "description": "the level"}, - "digest": map[string]any{"type": "string", "description": "the package's digest, with a level"}}}, - Run: func(args map[string]any) (any, error) { - level, _ := args["level"].(string) - digest, _ := args["digest"].(string) - kept, cut, err := r.Kept(level, digest) - return cutAnswer(map[string]any{"kept": kept}, cut), err - }}, - {Name: check.InputsReadTool, - Description: "The checking program's inputs the checker keeps, each under the sha256 of its exact bytes (inputs., kept a day): " + - "the facts of a check its verdict is asked of, or the heads its seat list is asked of. With a digest, that input as kept; " + - "without, every digest and its size. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "digest": map[string]any{"type": "string", "description": "the input's sha256, as a run ask names it"}}}, - Run: func(args map[string]any) (any, error) { - digest, _ := args["digest"].(string) - inputs, cut, err := r.Inputs(digest) - return cutAnswer(map[string]any{"inputs": inputs}, cut), err - }}, - {Name: check.PreviewsReadTool, - Description: "The merge previews the checker holds, each as stored under previews...: the merge commit, per module directory " + - "its tree, manifest digest and lock hashes, or the files in conflict. With a repository, that repository's; with a head (7 or more characters) " + - "only that head's. Keys that are no preview key are listed as unreadable. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "repository": map[string]any{"type": "string", "description": "/"}, - "head": map[string]any{"type": "string", "description": "a pull request head commit, or a prefix of at least 7 characters"}}}, - Run: func(args map[string]any) (any, error) { - repository, _ := args["repository"].(string) - head, _ := args["head"].(string) - if head != "" && len(head) < 7 { - return nil, fmt.Errorf("the head %q is too short: give at least 7 characters", head) - } - ps, bad, cut, err := r.Previews(repository, head) - return cutAnswer(map[string]any{"previews": ps, "unreadable": bad}, cut), err - }}, - {Name: check.StatusReadTool, - Description: "What runs: the steps in flight on the build modules by node, the candidates without a verdict for their heads, " + - "and the candidates whose check is owed by kind (waiting_by_kind: new, nodes, and trunk, a re-judge pulled after the other two). A key a " + - "build module left running (started past the step bound, or its build module not running on its node) is listed apart under " + - "left_running with why, and not counted. Nothing holds the checker and it has no capacity of its own: a check is an ask on the " + - "build seats' streams, whose backlog the delivery view shows (novox/hq ADR 0525). No merge holds a check off (novox/hq ADR 0661). could_not_run names each check " + - "that could not run because a part of the mesh was not heard: the part, since when, the running that asked it again, and whether it " + - "is the finding check-could-not-run (an hour unasked); it is asked again when that part runs. " + - "dead_letters says whether the checker hears the asks the build seats give up on (issue 734), and why not: " + - "deaf, a try or check whose ask is given up waits until its bound. builds_not_ended names, of each running check, the builder's builds of " + - "its candidate's changed modules that are neither done nor failed: the check waits on them before it composes, within its build-wait-bound " + - "(novox/hq ADR 0603 §10); a failed one fails the check: rebuild it with the builder's builder_build_again, and the check asks itself again when the build is done. Read-only.", - Input: map[string]any{"type": "object", "properties": map[string]any{}}, - Run: func(map[string]any) (any, error) { return r.Status() }}, - } -} - -// RequestTools is the tool that reads the composition requests the checker sent to nodes (issue 1921). -func RequestTools(r check.StateRead) []stdio.Tool { - return []stdio.Tool{{Name: check.RequestsReadTool, - Description: "The composition request a check sent to a node: the composer's whole input for that node (the node-engine's composition input, the case's " + - "manifests and verdicts, the seat list), kept exactly as sent so a session reproduces the check's composition (the composer run by hand over the same " + - "request gives the check's answer). Kept in the checker's state compositions, per check key (group.heads, the 12-digit heads hash) and node, " + - "0.7 to 0.8 MB each (kept compressed, in pieces that each fit the state's 256 KiB value), for 3 days (the state's ttl-seconds), the state holding at most 256 MiB (max-mib, which mesh-grants raises a held bucket to). With a check and a node, that request whole; with a check, " + - "the nodes it kept one for; with neither, every kept request, each with its size, digest and when it was kept. Read-only; " + check.RequestsDropTool + " drops a check's.", - Input: map[string]any{"type": "object", "properties": map[string]any{ - "check": map[string]any{"type": "string", "description": "the check's key, ., as the verdict and composition name it"}, - "node": map[string]any{"type": "string", "description": "the node the request was sent for, with a check"}}}, - Run: func(args map[string]any) (any, error) { - key, _ := args["check"].(string) - node, _ := args["node"].(string) - return check.ReadRequests(r, key, node) - }}} -} - -// RequestDropTools is the tool that drops a check's kept composition requests by hand, which frees the state when it is -// full (tools-are-how-a-module-is-spoken-to): it records the hand act through the seat mesh-hand-acts first. -func RequestDropTools(s check.StateDrop, acts views.HandActs) []stdio.Tool { - return []stdio.Tool{{Name: check.RequestsDropTool, - Description: "Drop the composition requests the checker kept for one check (" + check.RequestsReadTool + " lists them): every node's, and the pieces " + - "no other kept request names. The hand act is recorded through the seat mesh-hand-acts first, and a drop whose act could not be recorded is not made. " + - "Answers the nodes and pieces dropped. Use it when a check's words say its request could not be kept in the state compositions because it is full; " + - "the state itself drops a request 3 days after it was kept.", - Input: map[string]any{"type": "object", "required": []string{"check", "why"}, "properties": map[string]any{ - "check": map[string]any{"type": "string", "description": "the check's key, ., whose kept requests are dropped"}, - "why": map[string]any{"type": "string", "description": "why they are dropped, in words; recorded as the hand act"}}}, - Run: func(args map[string]any) (any, error) { - key, _ := args["check"].(string) - why, _ := args["why"].(string) - return check.DropRequests(s, key, why, func(what string) error { - return acts.RecordHandAct(views.HandAct{What: what, Why: why, Cause: "requests-drop", Verb: check.RequestsDropTool}) - }) - }}} -} - -// RecoveryTools are the tools that steer a check by hand (tools-are-how-a-module-is-spoken-to): ask a check again whose -// wait judged nothing (the lost check of 2026-10-28). They write the checker's verdicts through w. -func RecoveryTools(r check.Reader, w check.VerdictEntry) []stdio.Tool { - str := func(args map[string]any, k string) string { s, _ := args[k].(string); return s } - return []stdio.Tool{{Name: check.AskAgainTool, - Description: "Ask a candidate's check again by hand, whose verdict stays running while nothing runs for it (" + check.StatusReadTool + " names it " + - "under lost): its verdict is ended as asked again, saying why, and the checker, which hears its verdicts, stops the wait that judged nothing " + - "and asks the check anew, whole. Refused for a group with no candidate, a verdict that is not running or a failure whose failing steps did not all end on a transport fault (a pass, or any other failure, stands until a push or " + - "a trunk move), and while a step of its pull requests can still run on a build module (its end judges it). The checker reads a step that " + - "ended while its watch was deaf by itself at the next step end of its build module; this is for what that does not reach.", - Input: map[string]any{"type": "object", "required": []string{"group", "why"}, "properties": map[string]any{ - "group": map[string]any{"type": "string", "description": "the delivery group's branch name"}, - "why": map[string]any{"type": "string", "description": "why it is asked again, in words; kept in the verdict it ends"}}}, - Run: func(args map[string]any) (any, error) { - return r.AskAgain(w, str(args, "group"), str(args, "why")) - }}} -} - -// stepBound is the checker's step bound as the checker reads it (MESH_CHECKER_STEP_BOUND), its default when unset or not a -// duration. -func stepBound() time.Duration { - if d, err := time.ParseDuration(os.Getenv("MESH_CHECKER_STEP_BOUND")); err == nil && d > 0 { - return d - } - return check.DefaultStepBound -} - -// seatListAgainCallers are who may ask the seat list again by hand (design 47 G §204). -var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"} - -// holderOf is a seat's holder as the seat that places modules answers it (seatlist.HolderOf, design 47 G A-126): false while the -// seat has none or the answer cannot be read. -func holderOf(seat string) (string, bool) { - module, err := seatlist.HolderOf(stdio.Ask, seat) - return module, err == nil -} - -func main() { - // The build modules are no names (design 47 G A-82): the build seats on the seat list, each held by the module the seat - // that places modules names for it; the seat list and its two tools are wired once by mesh-sdk (§204). - gate := seatlist.NewPart("checker", seatListAgainCallers) - go func() { - <-stdio.Served() - gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-checker-tools] "+f+"\n", a...) }) - }() - seats := buildrun.NewBuildSeats(func() map[string]seatdef.Definition { - if rd := gate.Reader(); rd != nil { - return rd.Definitions() - } - return nil - }, holderOf) - r := check.Reader{VerdictState: read(stdio.State("verdicts")), KeptState: read(stdio.State("kept")), CandidateState: ownerRead(atHolder(stdio.Ask, holderOf, meshcatalog.Seat), indexerCandidatesTool), - PreviewState: read(stdio.State("previews")), InputState: read(stdio.State(check.InputsStateName)), - StepStates: func() map[string]check.StateRead { - out := map[string]check.StateRead{} - for _, m := range seats.All() { - if m.Module != "" && m.Toolchain != "" { - out[m.Module] = read(stdio.State(m.Module + ".steps")) - } - } - return out - }, - HaveState: read(stdio.State("node-engine.have")), StepBound: stepBound(), - CouldNotRunState: read(stdio.State(check.CouldNotRunState)), HearingState: read(stdio.State(buildrun.HearingState)), - AskState: read(stdio.State(check.AsksState)), BuildState: read(stdio.State("mesh-delivery-builder.builds"))} - r.Runs = runsOf{ownerRead(atHolder(stdio.Ask, holderOf, meshcatalog.Seat), indexerModulesTool)} // the node-engine at the commit the mesh runs, as the checker reads it - tries := stdio.State(check.TriesState) - trunks := func() (map[string]string, error) { - raw, err := stdio.Ask(check.TrunkVerb, meshcatalog.RevisionInput{}) - if err != nil { - return nil, err - } - return check.TrunkHeads(raw) - } - r.Trunks = trunks - store := stateStore{StateRead: read(tries), create: func(key string, value []byte) error { - _, err := tries.Create(key, json.RawMessage(value)) - return err - }, entry: func(key string) (json.RawMessage, uint64, error) { - reads.Add(1) - e, err := tries.Get(key) - if err != nil || e == nil { - return nil, 0, err - } - return e.Value, e.Revision, nil - }, update: func(key string, value []byte, revision uint64) error { - _, err := tries.Update(key, json.RawMessage(value), revision) - return err - }} - trying := Trying{Tries: store, Ender: store, Trunks: trunks} - r.Backlog = backlogOf(stdio.Ask) // the build seats' streams, for checker_ask_again (issue 1373) - verdicts := stdio.State("verdicts") - written := stateStore{entry: func(key string) (json.RawMessage, uint64, error) { - reads.Add(1) - e, err := verdicts.Get(key) - if err != nil || e == nil { - return nil, 0, err - } - return e.Value, e.Revision, nil - }, update: func(key string, value []byte, revision uint64) error { - _, err := verdicts.Update(key, json.RawMessage(value), revision) - return err - }} - tools := append(append(append(append(Tools(r), TryTools(trying)...), OpenTriesTools(trying)...), SeatTryTools(trying)...), RecoveryTools(r, written)...) - // checker_first_views and checker_first_view_again: where the checker's first views of the indexer's and the assigner's - // states stand, and asking one again by hand (ADR 0615, DLV-M2). - tools = append(tools, views.StatusTools(os.Getenv(stdio.ServedAs), "checker_first_views", "checker_first_view_again", stdio.State(views.StatusState), meshhandactsclient.Recorder{Ask: stdio.Ask}, time.Now)...) - tools = append(tools, gate.Tools()...) - tools = append(tools, SeatInputTools(r)...) - tools = append(tools, RequestTools(read(stdio.State(check.RequestsStateName)))...) - requests := stdio.State(check.RequestsStateName) - tools = append(tools, RequestDropTools(check.StateDrop{StateRead: read(requests), Delete: requests.Delete}, meshhandactsclient.Recorder{Ask: stdio.Ask})...) - if err := stdio.Serve("", logged(tools)); err != nil { - fmt.Fprintf(os.Stderr, "[mesh-delivery-checker-tools] %v\n", err) - os.Exit(1) - } -} - -// buildUsageTool is the delivery view's tool of the build nodes' use, whose backlog is the build seats' streams (issue 1373). -const buildUsageTool = "mesh-delivery-view.build_usage" - -// backlogOf reads the build seats' backlog through the view's tool, asked through ask (stdio.Ask, or a test's). -func backlogOf(ask func(string, any) (json.RawMessage, error)) func() ([]check.BacklogRow, error) { - return func() ([]check.BacklogRow, error) { - reads.Add(1) - raw, err := ask(buildUsageTool, map[string]any{}) - if err != nil { - return nil, fmt.Errorf("%s does not answer: %w", buildUsageTool, err) - } - answer, said, ok := buscalls.Unwrap(raw) - if !ok { - return nil, fmt.Errorf("%s answered: %s", buildUsageTool, said) - } - var a struct { - Backlog []check.BacklogRow `json:"backlog"` - Notes []string `json:"notes"` - } - if err := json.Unmarshal(answer, &a); err != nil { - return nil, fmt.Errorf("%s answered what does not decode: %w", buildUsageTool, err) - } - if len(a.Notes) > 0 { // the backlog was not read: unread is never empty - return nil, fmt.Errorf("%s did not read the backlog: %s", buildUsageTool, strings.Join(a.Notes, "; ")) - } - return a.Backlog, nil - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main_test.go deleted file mode 100644 index b42ad7e3..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main_test.go +++ /dev/null @@ -1,366 +0,0 @@ -package main - -import ( - "context" - "encoding/base64" - "encoding/json" - "errors" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "math/rand" - "os" - "regexp" - "slices" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - - "mesh-delivery-checker/internal/check" -) - -func TestTheToolsServedAreTheFiveReadTools(t *testing.T) { - var names []string - for _, tl := range Tools(check.Reader{}) { - names = append(names, tl.Name) - if tl.Description == "" || tl.Run == nil { - t.Errorf("%s has no description or no run", tl.Name) - } - } - want := []string{check.VerdictsReadTool, check.PullReadTool, check.KeptReadTool, check.InputsReadTool, check.PreviewsReadTool, check.StatusReadTool} - if !slices.Equal(names, want) { - t.Errorf("tools = %v, want %v", names, want) - } - // The recovery tool beside them asks a lost check again; with nothing to write it refuses. - recovery := RecoveryTools(check.Reader{}, nil) - if len(recovery) != 1 || recovery[0].Name != check.AskAgainTool || recovery[0].Description == "" { - t.Errorf("recovery tools = %+v", recovery) - } - if _, err := recovery[0].Run(map[string]any{"group": "feat/x", "why": "lost"}); err == nil { - t.Errorf("%s answered with no state to write", check.AskAgainTool) - } - // Issue 1921: the composition requests kept by the checker are read by check and node; the tool refuses a node with no check - // and a state it cannot read, and lists a check's nodes without their bodies. - store := &memStore{vals: map[string]json.RawMessage{}} - keeper := check.RequestKeeper{Store: memPut{store}} - for _, k := range [][2]string{{"g.0123456789ab", "anchor"}, {"g.0123456789ab", "ace"}, {"h.0123456789ab", "ace"}} { - if err := keeper.Keep(context.Background(), k[0], k[1], []byte(`{"input":{"nodes":[]}}`)); err != nil { - t.Fatal(err) - } - } - // Issue 1966: a request whose compressed bytes pass the state's value limit is kept in pieces and read back whole; the - // pieces are no requests in the list. - rnd := make([]byte, 600<<10) - _, _ = rand.New(rand.NewSource(1)).Read(rnd) - big := []byte(`{"input":"` + base64.StdEncoding.EncodeToString(rnd) + `"}`) - if err := keeper.Keep(context.Background(), "big.0123456789ab", "anchor", big); err != nil { - t.Fatalf("a request of %d bytes: %v", len(big), err) - } - rt := RequestTools(check.StateRead{Keys: store.Keys, Get: store.Get}) - if one, err := rt[0].Run(map[string]any{"check": "big.0123456789ab", "node": "anchor"}); err != nil || !strings.Contains(func() string { b, _ := json.Marshal(one); return string(b) }(), base64.StdEncoding.EncodeToString(rnd)[:64]) { - t.Errorf("the big request read back: %v", err) - } - // Listing reads no piece, and a request of another version is refused by name. - store.vals["v1.0123456789ab/anchor"] = json.RawMessage(`{"v":1,"check":"v1.0123456789ab","node":"anchor","gzip":"x"}`) - noPieces := check.StateRead{Keys: store.Keys, Get: func(k string) (json.RawMessage, error) { - if strings.HasPrefix(k, "pieces/") { - return nil, errors.New("a piece was read") - } - return store.Get(k) - }} - if _, err := RequestTools(noPieces)[0].Run(map[string]any{}); err != nil { - t.Errorf("listing: %v", err) - } - if _, err := rt[0].Run(map[string]any{"check": "v1.0123456789ab", "node": "anchor"}); err == nil || !strings.Contains(err.Error(), "version 1") { - t.Errorf("a version 1 request: %v", err) - } - delete(store.vals, "v1.0123456789ab/anchor") - if all, _ := rt[0].Run(map[string]any{}); strings.Count(func() string { b, _ := json.Marshal(all); return string(b) }(), `"node"`) != 4 { - t.Errorf("the list holds a piece as a request: %v", all) - } - if len(rt) != 1 || rt[0].Name != check.RequestsReadTool || !strings.Contains(rt[0].Description, "composition request a check sent to a node") { - t.Fatalf("request tools = %+v", rt) - } - one, err := rt[0].Run(map[string]any{"check": "g.0123456789ab", "node": "anchor"}) - if b, _ := json.Marshal(one); err != nil || !strings.Contains(string(b), `"request":{"input":{"nodes":[]}}`) { - t.Errorf("one request = %s, %v", b, err) - } - list, _ := rt[0].Run(map[string]any{"check": "g.0123456789ab"}) - if b, _ := json.Marshal(list); strings.Count(string(b), `"node"`) != 2 || strings.Contains(string(b), "h.0123") || strings.Contains(string(b), `"request"`) { - t.Errorf("list of a check = %s", b) - } - for _, args := range []map[string]any{{"node": "anchor"}, {"check": "g.0123456789ab", "node": "gone"}} { - if _, err := rt[0].Run(args); err == nil { - t.Errorf("%v answered", args) - } - } - if _, err := RequestTools(check.StateRead{})[0].Run(map[string]any{}); err == nil { - t.Errorf("answered with no state to read") - } - // Issue 1966: another module's state is asked of its read tool, never read from its bus state (ADR 0596). Every - // stdio.State this program names of another module is one its manifest declares under reads, which the runtime grants; - // the indexer's candidates and modules are asked of indexer_read_candidates and indexer_read_modules, which it invokes. - t.Run("1966: another module's state is read only where the manifest declares it", func(t *testing.T) { - raw, err := os.ReadFile("../../module.json") - if err != nil { - t.Fatal(err) - } - var man struct { - Reads []string `json:"reads"` - Invokes []string `json:"invokes"` - } - if err := json.Unmarshal(raw, &man); err != nil { - t.Fatal(err) - } - src, err := os.ReadFile("main.go") - if err != nil { - t.Fatal(err) - } - for _, m := range regexp.MustCompile(`stdio\.State\("([^"]+\.[^"]+)"\)`).FindAllStringSubmatch(string(src), -1) { - if !slices.Contains(man.Reads, m[1]) { - t.Errorf("main.go reads %s, which the manifest does not declare under reads: the runtime refuses it", m[1]) - } - } - for _, tool := range []string{indexerCandidatesTool, indexerModulesTool} { - if !slices.Contains(man.Invokes, check.IndexerModule+"."+tool) { - t.Errorf("the manifest does not invoke %s", tool) - } - } - }) - for _, c := range []struct { - name, key, answer, want string - failed bool - }{ - {name: "the keys", answer: `{"owner":"mesh-delivery-indexer","state":"candidates","keys":["a","b"]}`, want: "[a b]"}, - {name: "one key, wrapped as the tool runner hands it", key: "a", - answer: `{"content":[{"type":"text","text":"{\"key\":\"a\",\"found\":true,\"value\":{\"commit\":\"c\"}}"}]}`, want: `{"commit":"c"}`}, - {name: "a key the indexer does not hold", key: "gone", answer: `{"key":"gone","found":false}`, want: ""}, - {name: "the tool refused", key: "a", answer: `{"content":[{"type":"text","text":"refused"}],"isError":true}`, failed: true}, - } { - t.Run("1966: the indexer's read tool, "+c.name, func(t *testing.T) { - var asked []string - holder := func(seat string) (string, bool) { return "acme-indexer", seat == meshcatalog.Seat } // found at the seat list's holder - r := ownerRead(atHolder(func(key string, body any) (json.RawMessage, error) { - b, _ := json.Marshal(body) - asked = append(asked, key+" "+string(b)) - return json.RawMessage(c.answer), nil - }, holder, meshcatalog.Seat), indexerCandidatesTool) - var got string - var err error - if c.key == "" { - var keys []string - keys, err = r.Keys() - got = strings.Trim(strings.Join([]string{"[", strings.Join(keys, " "), "]"}, ""), " ") - } else { - var raw json.RawMessage - raw, err = r.Get(c.key) - got = string(raw) - } - want := []string{"acme-indexer." + indexerCandidatesTool + " {}"} - if c.key != "" { - want = []string{"acme-indexer." + indexerCandidatesTool + ` {"key":"` + c.key + `"}`} - } - if (err != nil) != c.failed || (!c.failed && got != c.want) || !slices.Equal(asked, want) { - t.Errorf("read %q, %v, asking %v; want %q, failed %v, asking %v", got, err, asked, c.want, c.failed, want) - } - }) - } - // 1373: the build seats' backlog is read through build_usage; one it says it did not read is never taken for empty. - for _, c := range []struct { - name, answer string - rows int - failed bool - }{ - {name: "a backlog read", answer: `{"backlog":[{"language":"go","kind":"check","waiting":2}]}`, rows: 1}, - {name: "a backlog with a note says it was not read", answer: `{"backlog":[],"notes":["the seats' streams cannot be read here"]}`, failed: true}, - } { - rows, err := backlogOf(func(string, any) (json.RawMessage, error) { return json.RawMessage(c.answer), nil })() - if (err != nil) != c.failed || len(rows) != c.rows || (c.failed && !strings.Contains(err.Error(), "cannot be read here")) { - t.Errorf("1373 backlog, %s: %v, %v", c.name, rows, err) - } - } - // With nothing to read from a tool says so and changes nothing. - for _, tl := range Tools(check.Reader{}) { - if tl.Name == check.StatusReadTool { - continue // it answers from what it can read, and says what it could not - } - if _, err := tl.Run(map[string]any{"repository": "novox/x", "number": float64(1)}); err == nil { - t.Errorf("%s answered with no state to read", tl.Name) - } - } -} - -// memStore is a tries state in memory. -type memStore struct{ vals map[string]json.RawMessage } - -func (m *memStore) Keys() ([]string, error) { - var out []string - for k := range m.vals { - out = append(out, k) - } - return out, nil -} -func (m *memStore) Get(key string) (json.RawMessage, error) { return m.vals[key], nil } -func (m *memStore) Create(key string, value []byte) error { - if _, ok := m.vals[key]; ok { - return errors.New("exists") - } - m.vals[key] = value - return nil -} - -func (m *memStore) Entry(key string) (json.RawMessage, uint64, error) { return m.vals[key], 1, nil } -func (m *memStore) Update(key string, value []byte, revision uint64) error { - m.vals[key] = value - return nil -} - -func TestTheTryToolsAskAndReadATry(t *testing.T) { // ADR 0501 §5, the agreed interface with mesh-controller's try - store := &memStore{vals: map[string]json.RawMessage{}} - tools := TryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(1, 5) }, - Trunks: func() (map[string]string, error) { - return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil - }}) - if len(tools) != 3 || tools[0].Name != "checker_try" || tools[1].Name != "checker_try_read" || tools[2].Name != "checker_try_end" { - t.Fatalf("tools = %+v", tools) - } - out, err := tools[0].Run(map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40), "trunk": "main"}) - if err != nil { - t.Fatal(err) - } - id := out.(map[string]any)["id"].(string) - if id != "try-1000000005" { - t.Errorf("id = %q", id) - } - raw, err := tools[1].Run(map[string]any{"id": id}) - if err != nil || !strings.Contains(string(raw.(json.RawMessage)), `"state":"queued"`) { - t.Errorf("read = %s, %v", raw, err) - } - // The seat's verbs answer as the tools do (ADR 0547 §10, issue 774): the same ask is the same try, and the read is alike. - verbs := SeatTryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(1, 5) }, - Trunks: func() (map[string]string, error) { - return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil - }}) - if len(verbs) != 3 || verbs[0].Name != Seat+".try" || verbs[1].Name != Seat+".try-read" || verbs[2].Name != Seat+".try-end" { - t.Fatalf("verbs = %+v", verbs) - } - again, err := verbs[0].Run(map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40), "trunk": "main"}) - if err != nil || again.(map[string]any)["id"] != id { - t.Errorf("verb try = %v, %v, want the waiting try %s", again, err, id) - } - if viaVerb, err := verbs[1].Run(map[string]any{"id": id}); err != nil || string(viaVerb.(json.RawMessage)) != string(raw.(json.RawMessage)) { - t.Errorf("verb try-read = %s, %v; tool = %s", viaVerb, err, raw) - } - if _, err := verbs[0].Run(map[string]any{"repository": "novox/hq", "head": strings.Repeat("1", 40)}); err == nil { - t.Error("the verb tried a repository nothing checks") - } - // The seat's definition of the verb try gives the members the verb serves (ADR 0637 §6), as the tool takes them, and says so. - def, err := os.ReadFile("../../../mesh-delivery-contracts/seats/mesh-delivery-checker/seat.json") - if err != nil { - t.Fatal(err) - } - var seat struct { - Verbs []struct { - Name string `json:"name"` - Description string `json:"description"` - Input struct { - Properties map[string]any `json:"properties"` - Required []string `json:"required"` - } `json:"input"` - } `json:"verbs"` - } - if err := json.Unmarshal(def, &seat); err != nil { - t.Fatal(err) - } - for _, v := range seat.Verbs { - if v.Name != "try" { - continue - } - toolProps := verbs[0].Input["properties"].(map[string]any) - for name := range toolProps { - if _, ok := v.Input.Properties[name]; !ok { - t.Errorf("the verb try serves no %q, which the tool takes", name) - } - } - const limit = "at most 4 run on a build node, and 8 wait for one, mesh-wide" - if !strings.Contains(v.Description, limit) || !strings.Contains(verbs[0].Description, limit) { - t.Errorf("the verb and the tool state one limit %q", limit) - } - if len(v.Input.Required) != 0 || !strings.Contains(v.Description, "members") { - t.Errorf("the verb try requires %v and says %q: want members as an alternative to one repository and head", v.Input.Required, v.Description) - } - } - // The seat's verb input (issue 1921, ADR 0596 §2): a checking program's input as kept, which the Go build module asks. - body := []byte(`{"v":1}`) - in, _ := buildstep.RunInputOf(check.InputVerb, body) - inputs := SeatInputTools(check.Reader{InputState: check.StateRead{Get: func(key string) (json.RawMessage, error) { - if key == in.Digest { - return body, nil - } - return nil, nil - }}}) - if len(inputs) != 1 || inputs[0].Name != Seat+".input" { - t.Fatalf("input verbs = %+v", inputs) - } - if a, err := inputs[0].Run(map[string]any{"digest": in.Digest}); err != nil || a.(meshdeliverychecker.InputAnswer).Input != base64.StdEncoding.EncodeToString(body) { - t.Errorf("verb input = %+v, %v", a, err) - } - // A group try (issue 1921): the members, each an exact commit; members and one repository together are refused; the - // description says what it does in the words a search for "try a group" finds. - later := TryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(2, 5) }, - Trunks: func() (map[string]string, error) { - return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil - }}) - g, err := later[0].Run(map[string]any{"members": []any{map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("2", 40)}}}) - if err != nil || g.(map[string]any)["id"] == id { - t.Errorf("a group try = %v, %v, want a try of its own", g, err) - } - if _, err := tools[0].Run(map[string]any{"members": []any{map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("2", 40)}}, - "repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40)}); err == nil { - t.Error("members and a repository were both taken") - } - if _, err := tools[0].Run(map[string]any{"members": "novox/mesh-delivery"}); err == nil { - t.Error("members that are no list were taken") - } - for _, tl := range tools { - for _, words := range []string{"group"} { - if !strings.Contains(strings.ToLower(tl.Description), words) { - t.Errorf("%s says nothing of a %s: %q", tl.Name, words, tl.Description) - } - } - } - if d := strings.ToLower(tools[0].Description); !strings.Contains(d, "try a group") || !strings.Contains(d, "exact commit") || !strings.Contains(d, "every failure") || - !strings.Contains(d, "composition on every placed node") || !strings.Contains(d, "owes the forge nothing") || !strings.Contains(d, "kept") { - t.Errorf("the try's description does not say what a group try does: %q", d) - } - if open := OpenTriesTools(Trying{Tries: store}); len(open) != 1 || open[0].Name != check.TriesTool || !strings.Contains(open[0].Description, "try.started") { - t.Errorf("open tries tool = %+v", open) - } else if out, err := open[0].Run(nil); err != nil || len(out.(map[string]any)["tries"].([]checkermsg.Open)) != 2 { - t.Errorf("open tries = %v, %v, want the two tries asked", out, err) - } - // Issue 734: a try whose ask was lost is ended by hand with why, and its head is asked anew. - ended, err := tools[2].Run(map[string]any{"id": id, "why": "its ask was lost"}) - if err != nil || ended.(map[string]any)["state"] != "error" { - t.Errorf("end = %v, %v", ended, err) - } - if _, err := tools[2].Run(map[string]any{"id": id, "why": "again"}); err == nil { - t.Error("an ended try was ended again") - } - if _, err := tools[0].Run(map[string]any{"repository": "novox/hq", "head": strings.Repeat("1", 40)}); err == nil || - err.Error() != "no level of novox/hq is checked by the checker" { - t.Errorf("hq: %v", err) - } -} - -// memPut writes a memStore as the checker's kept-request state is written. -type memPut struct{ m *memStore } - -func (p memPut) Put(_ context.Context, key string, value []byte) (uint64, error) { - if len(value) > 262144 { // the state's value limit, headers included (issue 1966) - return 0, errors.New("the value is over the 262144 bytes the bucket takes") - } - p.m.vals[key] = value - return 1, nil -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/bus.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/bus.go deleted file mode 100644 index 4342f010..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/bus.go +++ /dev/null @@ -1,197 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/process" - - "mesh-delivery-checker/internal/check" -) - -// bucketOpener is the part of the bus the watches use. -type bucketOpener interface { - KeyValue(ctx context.Context, bucket string) (jetstream.KeyValue, error) -} - -// kvStore is the builds bucket as the builder's store: create and compare-and-set. -type kvStore struct{ kv jetstream.KeyValue } - -func conflict(err error) error { - if errors.Is(err, jetstream.ErrKeyExists) || errors.Is(err, jetstream.ErrKeyRevisionMismatch) { - return check.ErrConflict - } - return err -} - -func (s kvStore) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) { - e, err := s.kv.Get(ctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, 0, false, nil - } - if err != nil { - return nil, 0, false, err - } - return e.Value(), e.Revision(), true, nil -} - -func (s kvStore) Create(ctx context.Context, key string, value []byte) (uint64, error) { - rev, err := s.kv.Create(ctx, key, value) - return rev, conflict(err) -} - -func (s kvStore) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) { - rev, err := s.kv.Update(ctx, key, value, revision) - return rev, conflict(err) -} - -func (s kvStore) Keys(ctx context.Context) ([]string, error) { - keys, err := s.kv.Keys(ctx) - if errors.Is(err, jetstream.ErrNoKeysFound) { - return nil, nil - } - return keys, err -} - -// Delete removes a key (an ended try past its keeping). -func (s kvStore) Delete(ctx context.Context, key string) error { return s.kv.Delete(ctx, key) } - -func opOf(e jetstream.KeyValueEntry) bool { return e.Operation() == jetstream.KeyValuePut } - -// settle reads the entries a watcher holds now, up to its mark of the last values, and returns the watcher for the -// changes that follow. -func settle(ctx context.Context, kv jetstream.KeyValue, each func(jetstream.KeyValueEntry)) (jetstream.KeyWatcher, error) { - w, err := kv.WatchAll(ctx) - if err != nil { - return nil, err - } - for { - select { - case <-ctx.Done(): - w.Stop() - return nil, ctx.Err() - case e, ok := <-w.Updates(): - if !ok { - return nil, errors.New("the watch ended") - } - if e == nil { - return w, nil - } - each(e) - } - } -} - -// follow hands each change of a watcher to each until ctx ends; a watch that ends is an error, and the process manager -// starts the program again. -func follow(ctx context.Context, w jetstream.KeyWatcher, each func(jetstream.KeyValueEntry)) error { - defer w.Stop() - for { - select { - case <-ctx.Done(): - return nil - case e, ok := <-w.Updates(): - if !ok { - return errors.New("a watch ended") - } - if e != nil { - each(e) - } - } - } -} - -// manifestReader reads manifests. of the indexer and parses the manifest it holds. -type manifestReader struct{ kv jetstream.KeyValue } - -func (m manifestReader) Manifest(ctx context.Context, digest string) (catalogue.Manifest, error) { - key, err := meshcatalogclient.ManifestsKey(digest) - if err != nil { - return catalogue.Manifest{}, err - } - e, err := m.kv.Get(ctx, key) - if err != nil { - return catalogue.Manifest{}, fmt.Errorf("manifests.%s: %w", key, err) - } - kept, err := meshcatalogclient.DecodeManifest(e.Value()) - if err != nil { - return catalogue.Manifest{}, err - } - return parseManifest(kept.Manifest) -} - -// parseManifest decodes a manifest the indexer kept, as every reader does (novox/hq ADR 0578 §3): judged by nothing here, -// a field it does not know set aside. The checker judges a change's manifests with the indexer's judge, run as a step -// (judge.go). -func parseManifest(raw string) (catalogue.Manifest, error) { - return catalogue.Decode([]byte(raw)) -} - -// awaitedKV is a bucket that may appear after the start (process.AwaitBucket) as a check.KV: a call while it is not on the -// bus fails saying so. -type awaitedKV struct{ b *process.AwaitedBucket } - -func (a awaitedKV) kv() (kvStore, error) { - kv := a.b.KV() - if kv == nil { - return kvStore{}, errors.New("the bucket is not on the bus yet") - } - return kvStore{kv}, nil -} - -func (a awaitedKV) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) { - s, err := a.kv() - if err != nil { - return nil, 0, false, err - } - return s.Get(ctx, key) -} - -func (a awaitedKV) Create(ctx context.Context, key string, value []byte) (uint64, error) { - s, err := a.kv() - if err != nil { - return 0, err - } - return s.Create(ctx, key, value) -} - -func (a awaitedKV) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) { - s, err := a.kv() - if err != nil { - return 0, err - } - return s.Update(ctx, key, value, revision) -} - -func (a awaitedKV) Keys(ctx context.Context) ([]string, error) { - s, err := a.kv() - if err != nil { - return nil, err - } - return s.Keys(ctx) -} - -func (a awaitedKV) Delete(ctx context.Context, key string) error { - s, err := a.kv() - if err != nil { - return err - } - return s.Delete(ctx, key) -} - -// runInputs keeps the checking program's inputs in the checker's state inputs (check.InputsBucket), each under the -// sha256 of its exact bytes: an input already kept under its digest is the same bytes, so a second keep is no error. -type runInputs struct{ a awaitedKV } - -func (r runInputs) Keep(ctx context.Context, digest string, body []byte) error { - _, err := r.a.Create(ctx, digest, body) - if errors.Is(err, check.ErrConflict) { - return nil - } - return err -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/couldnotrun.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/couldnotrun.go deleted file mode 100644 index e747f8dd..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/couldnotrun.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "time" - - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - - "mesh-delivery-checker/internal/check" -) - -// waitCouldNotRun raises each check that could not run as the finding check-could-not-run once its bound passed -// (check.CouldNotRunBound): it waits for the next one due, read from the checker's state could-not-run, and reads again -// each time one is kept. The bound is the only wait: a check is asked again on its part's running, never here. -func waitCouldNotRun(ctx context.Context, ch *check.Checker, kept <-chan struct{}) { - for ctx.Err() == nil { - // The wait is a context's bound (ADR 0437 §8): none while no check waits for its bound. - wait, done := ctx, func() {} - if next := ch.CouldNotRunDue(ctx, time.Now()); !next.IsZero() { - wait, done = context.WithDeadline(ctx, next) - } - select { - case <-wait.Done(): - case <-kept: - } - done() - } -} - -// partRunner is what a `running` is handed to: check.Checker's PartRunning. -type partRunner interface { - PartRunning(ctx context.Context, node, module, fingerprint string) -} - -// heardRunning takes one node-engine seat's `running..` in, as the checker's durable consumer of its stream -// hands it (module.json consumes node-engine.running): the part named runs at its fingerprint, so every check that could not -// run for want of it is asked again once (check.Checker.PartRunning). A body that names no node, module or fingerprint asks -// nothing, said in the log. -func heardRunning(ctx context.Context, ch partRunner, body []byte, logf func(string, ...any)) { - var r nodeengine.RunningEvent - if err := json.Unmarshal(body, &r); err != nil || r.Node == "" || r.Module == "" || r.Fingerprint == "" { - logf("a running of the node-engine seat that names no node, module or fingerprint asks no check again: %s (%v)", body, err) - return - } - ch.PartRunning(ctx, r.Node, r.Module, r.Fingerprint) -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/engines.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/engines.go deleted file mode 100644 index 64932370..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/engines.go +++ /dev/null @@ -1,167 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "sort" - "sync" - "sync/atomic" - "time" - - nodeengineseat "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" - "git.novox.be/novox/mesh-sdk/go/views" -) - -// The checker's composition of a check on the bus (novox/hq ADR 0593 §7, §9; check.EngineCompositions): each placed -// node's engine asked `node-engine.compose-candidate` on that node, and, for a group that changes the node-engine's -// composition code, the composer built from its head run on the Go build seat. - -// composeCandidateKey is the seat verb's call key, asked of one node (process.Conn.CallOn). -var composeCandidateKey = "seat:" + nodeengineseat.Seat + "." + ne.VerbComposeCandidate - -// engineAsks asks one node's engine over the bus, its answer waited for at most ctx's deadline (the answer bound). -type engineAsks struct { - call func(node, key string, body any) (json.RawMessage, error) -} - -func (e engineAsks) ComposeCandidate(ctx context.Context, node string, ask ne.ComposeCandidateInput) (ne.ComposeCandidateAnswer, error) { - type answer struct { - raw json.RawMessage - err error - } - got := make(chan answer, 1) - go func() { - raw, err := e.call(node, composeCandidateKey, ask) - got <- answer{raw, err} - }() - select { - case <-ctx.Done(): - return ne.ComposeCandidateAnswer{}, fmt.Errorf("no answer within its bound: %w", ctx.Err()) - case a := <-got: - if a.err != nil { - return ne.ComposeCandidateAnswer{}, a.err - } - return nec.DecodeAnswer(a.raw) - } -} - -// assignedNodes is the placedNodes' read of the assignments' picture, which is followed once the holder of the seat mesh-module-assigner -// is known: an error says so until then, so a check never passes with no node asked. -func assignedNodes(p *atomic.Pointer[views.Picture]) func(context.Context) (jetstream.KeyValue, error) { - return func(context.Context) (jetstream.KeyValue, error) { - pic := p.Load() - if pic == nil { - return nil, errors.New("the seat that places modules has no holder known yet") - } - return pic, nil - } -} - -// placedNodes names every node the seat mesh-module-assigner places now, read from its state when a check composes (no -// watch: a check reads it once). A state that cannot be read is an error, so the check says its composition was not -// made rather than passing with no node asked (review finding 4). -type placedNodes struct { - kv func(ctx context.Context) (jetstream.KeyValue, error) -} - -func (p placedNodes) Placed() ([]string, error) { - ctx, cancel := context.WithTimeout(context.Background(), nec.AnswerBound) - defer cancel() - kv, err := p.kv(ctx) - if err != nil { - return nil, fmt.Errorf("the assignments cannot be read: %w", err) - } - lister, err := kv.ListKeys(ctx) - if errors.Is(err, jetstream.ErrNoKeysFound) { - return nil, nil // assignments that places nothing - } - if err != nil { - return nil, fmt.Errorf("the assignments cannot be listed: %w", err) - } - seen := map[string]bool{} - for key := range lister.Keys() { - if node, _, _, err := meshmoduleassignerclient.ParseKey(key); err == nil { - seen[node] = true - } - } - out := make([]string, 0, len(seen)) - for n := range seen { - out = append(out, n) - } - sort.Strings(out) - return out, nil -} - -// seatSandbox runs a compose job on the Go build seat's check stream, as the checker's own ask, and answers the -// composer's verdict from the step's result (buildstep.ComposeAnswer). -type seatSandbox struct { - // module is the checker's own, the credential's: the asker of the compose ask. - module string - asker buildrun.Asker -} - -func (s seatSandbox) Compose(ctx context.Context, job buildstep.ComposeJob) (composer.Verdict, error) { - ask := buildstep.StepAsk{ID: buildstep.NewID(), Kind: buildstep.AskCheck, Language: buildstep.AskGo, Asker: s.module, - At: time.Now().UTC(), Compose: &job} - step, err := s.asker.Ask(ctx, ask) - if err != nil { - return composer.Verdict{}, err - } - if step.State != buildstep.StepPassed { - return composer.Verdict{}, fmt.Errorf("the compose ask %s ended %s: %s", ask.ID, step.State, step.Reason) - } - var a buildstep.ComposeAnswer - if err := json.Unmarshal(step.Result, &a); err != nil { - return composer.Verdict{}, fmt.Errorf("the compose ask %s answered no compose answer: %w", ask.ID, err) - } - var v composer.Verdict - if err := json.Unmarshal(a.Verdict, &v); err != nil || v.V != composer.Version { - return composer.Verdict{}, fmt.Errorf("the compose ask %s answered no composer verdict of version %d (%v)", ask.ID, composer.Version, err) - } - return v, nil -} - -// manifestVerdicts are the indexer's refusals of the trunk's manifests, by module, from its modules' values -// (meshcatalog.ModulesByModuleValue.VerdictOf, ADR 0578 §3): what the sandbox's composer holds a refused module by. -type manifestVerdicts struct { - mu sync.Mutex - by map[string]meshcatalog.VerdictsByDigestValue -} - -func (m *manifestVerdicts) put(module string, v *meshcatalog.VerdictsByDigestValue) { - m.mu.Lock() - defer m.mu.Unlock() - if m.by == nil { - m.by = map[string]meshcatalog.VerdictsByDigestValue{} - } - if v == nil { - delete(m.by, module) - return - } - // The sandbox's composer may be the node-engine of an older delivery, which does not know the verdict waits: it is handed a - // wait as the refusal it is held as. - m.by[module] = meshcatalogclient.VerdictAsRefused(*v) -} - -func (m *manifestVerdicts) all() map[string]meshcatalog.VerdictsByDigestValue { - m.mu.Lock() - defer m.mu.Unlock() - out := make(map[string]meshcatalog.VerdictsByDigestValue, len(m.by)) - for k, v := range m.by { - out[k] = v - } - return out -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/findingwriter.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/findingwriter.go deleted file mode 100644 index 996b94c8..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/findingwriter.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "context" - - "github.com/nats-io/nats.go" - "github.com/nats-io/nats.go/jetstream" - - condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" - "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/process" - "git.novox.be/novox/mesh-sdk/go/views" - - "mesh-delivery-checker/internal/check" -) - -type findingWriterOf = views.Writer[condseat.Finding, condseat.FindingCleared, condseat.FindingsStated, condseat.FindingsAskedEvent] - -// checkerFindings is the checker's finding writer (design 47 G §461, §689): it owns the finding check-could-not-run beside -// its views reader's, observes the raised ones of its kept checks, and writes both as the generated contract type. The -// writer forms its subjects itself (A-13). -type checkerFindings struct { - w *findingWriterOf - node string -} - -// newCheckerFindings makes and starts the writer; observe says the checks that could not run and whose finding is -// written (check.Checker.RaisedCouldNotRun). -func newCheckerFindings(ctx context.Context, cred process.Account, conn *nats.Conn, js jetstream.JetStream, state process.State, - observe func(context.Context) ([]check.Finding, error), logf func(string, ...any)) (*checkerFindings, error) { - c := &checkerFindings{node: cred.Node} - w, err := views.NewWriter(views.WriterConfig[condseat.Finding, condseat.FindingCleared, condseat.FindingsStated, condseat.FindingsAskedEvent]{ - Module: cred.Module, State: state, Node: cred.Node, Asked: condseat.SubjectFindingsAsked, JS: js, Conn: conn, Logf: logf, - Kinds: []string{check.CouldNotRunKind}, - Observe: func(ctx context.Context) (views.Observed[condseat.Finding], error) { - found, err := observe(ctx) - if err != nil { - return views.Observed[condseat.Finding]{}, err - } - var out views.Observed[condseat.Finding] - for _, f := range found { - out.Findings = append(out.Findings, c.body(f)) - } - return out, nil - }, - }) - if err != nil { - return nil, err - } - c.w = w - // A writer that cannot start (the stream of its asks is not there yet) is said; its findings are then kept in the - // state alone and said by the next start (as the node-engine's is). - if err := w.Start(ctx); err != nil { - logf("the finding writer cannot start, its findings are kept in the state alone: %v", err) - } - return c, nil -} - -// body lays the checker's finding out as the seat's contract type: a finding of the module, about the module on its node. -func (c *checkerFindings) body(f check.Finding) condseat.Finding { - module, _ := c.w.ModuleNode() - return condseat.Finding{Kind: f.Kind, Scope: views.FindingScope, ID: keys.EscapeName(f.ID), At: f.At, Since: f.At, - Severity: condseat.FindingSeverityWarning, Source: f.Kind, Summary: f.Summary, Headline: f.Headline, Explanation: f.Explanation, - Subject: condseat.Subject{Scope: views.FindingScope, ID: module, Machine: c.node}} -} - -// Put writes one finding; its `since` stays where its key exists. -func (c *checkerFindings) Put(f check.Finding) error { - return c.w.Write(context.Background(), c.body(f)) -} - -// Delete clears one finding. -func (c *checkerFindings) Delete(f check.Finding) error { - return c.w.Clear(context.Background(), views.FindingScope, keys.EscapeName(f.ID), f.Kind, "the checker no longer observes it") -} - -var _ check.FindingWriter = (*checkerFindings)(nil) diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge.go deleted file mode 100644 index e00b1958..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - - "github.com/nats-io/nats.go" - - gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - - "mesh-delivery-checker/internal/check" -) - -// The checker reads the forge through the seat git's read verbs (design 47 *The seat git*; milestone 47/G W6.14), as its -// manifest's invokes names them, never by a module's tool: whichever module holds the seat answers. It writes nothing to -// the forge: what the checker owes the forge is the forge bridge's to perform (ADR 0484 clause 2). - -// fileVerb is the seat git's verb file, as invokes names it. -var fileVerb = "seat:" + gitseat.Seat + "." + gitseat.VerbFile - -// forgeReads reads the forge through call (mesh-sdk process.Conn.Call as the checker's own account). -type forgeReads struct { - call func(key string, body any) (json.RawMessage, error) -} - -// ask calls a verb of the seat git; a call that reached nobody (no responders: the holder's tool runner re-serving, or the -// forge down) is marked Unheard, so the check says it could not run and is asked again when the part it could not hear -// runs (issue 1373, issue 1624). -func (f forgeReads) ask(key string, body any) (json.RawMessage, error) { - raw, err := f.call(key, body) - if errors.Is(err, nats.ErrNoResponders) || errors.Is(err, nats.ErrTimeout) || errors.Is(err, context.DeadlineExceeded) { - return nil, &check.Unheard{Err: err} - } - return raw, err -} - -// File is the text of a file of a repository at a ref. A file the forge says the ref does not hold wraps -// check.ErrNotFound (issue 1845); the seat's error is a failure, never an absent file. -func (f forgeReads) File(_ context.Context, repository, path, ref string) (string, error) { - raw, err := f.ask(fileVerb, gitseat.FileInput{Repository: repository, Path: path, Ref: ref}) - if err != nil { - return "", err - } - var a gitseat.FileAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return "", fmt.Errorf("the answer of %s is not its answer: %v", fileVerb, err) - } - switch { - case a.Error != "": - return "", fmt.Errorf("%s of %s at %s: %s", fileVerb, path, ref, a.Error) - case a.Absent: - return "", fmt.Errorf("%w: %s holds no %s at %s", check.ErrNotFound, repository, path, ref) - case a.Path == "" && a.Blob == "" && a.Content == "": - return "", fmt.Errorf("the answer of %s names no file and no error", fileVerb) - } - return a.Content, nil -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge_test.go deleted file mode 100644 index 1803122e..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/forge_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/nats-io/nats.go" - - "mesh-delivery-checker/internal/check" -) - -// seatAnswer is an answer of the seat git's verb file as its fixtures give it (modules/mesh-delivery-contracts/seats/git/fixtures/verbs.json). -func seatAnswer(t *testing.T, name string) json.RawMessage { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "..", "mesh-delivery-contracts", "seats", "git", "fixtures", "verbs.json")) - if err != nil { - t.Fatal(err) - } - var fixtures []struct { - Name string `json:"name"` - Of string `json:"of"` - Given json.RawMessage `json:"given"` - } - if err := json.Unmarshal(raw, &fixtures); err != nil { - t.Fatal(err) - } - for _, f := range fixtures { - if f.Of == "verb:file.answer" && strings.Contains(string(f.Given), name) { - return f.Given - } - } - t.Fatalf("no file answer of the seat git's fixtures holds %q", name) - return nil -} - -func TestTheForgeReadsParseWhatTheForgeAnswered(t *testing.T) { // 1624/AC4; milestone 47/G W6.14: through the seat git - var asked []string - f := forgeReads{call: func(key string, body any) (json.RawMessage, error) { - b, _ := json.Marshal(body) - asked = append(asked, key+" "+string(b)) - return seatAnswer(t, "seats/index.json"), nil - }} - text, err := f.File(context.Background(), "novox/mesh-contracts", "seats/index.json", "main") - if err != nil || text != `{"format":1,"seats":["mesh-broker"]}` { - t.Errorf("file = %q, %v", text, err) - } - if want := `seat:git.file {"path":"seats/index.json","ref":"main","repository":"novox/mesh-contracts"}`; len(asked) != 1 || asked[0] != want { - t.Errorf("asked %v, want %s", asked, want) - } - bad := forgeReads{call: func(string, any) (json.RawMessage, error) { return json.RawMessage(`{}`), nil }} - if _, err := bad.File(context.Background(), "novox/x", "go.mod", "main"); err == nil { - t.Error("an answer with no file was taken") - } -} - -func TestAFileTheRefDoesNotHoldIsNotFoundAndOtherFailuresAreNot(t *testing.T) { // 1845/AC1, AC4 - f := forgeReads{call: func(string, any) (json.RawMessage, error) { return seatAnswer(t, "seats/gone.json"), nil }} - if _, err := f.File(context.Background(), "novox/mesh-contracts", "seats/gone.json", "aaaa"); !errors.Is(err, check.ErrNotFound) { - t.Errorf("an absent file is %v, want ErrNotFound", err) - } - f = forgeReads{call: func(string, any) (json.RawMessage, error) { return seatAnswer(t, "over 512 KiB"), nil }} - if _, err := f.File(context.Background(), "novox/mesh-contracts", "seats/big.json", "main"); err == nil || errors.Is(err, check.ErrNotFound) { - t.Errorf("the seat's error is %v, want a failure that is not ErrNotFound", err) - } - f = forgeReads{call: func(string, any) (json.RawMessage, error) { return nil, errors.New("the holder failed") }} - if _, err := f.File(context.Background(), "novox/mesh-catalog", "go.mod", "main"); err == nil || errors.Is(err, check.ErrNotFound) { - t.Errorf("a failed call is %v, want a failure that is not ErrNotFound", err) - } -} - -func TestAForgeReadThatReachesNobodyIsUnheard(t *testing.T) { // 1624 C7: "nats: no responders" on mesh-catalog #293 - f := forgeReads{call: func(string, any) (json.RawMessage, error) { return nil, nats.ErrNoResponders }} - _, err := f.File(context.Background(), "novox/mesh-catalog", "checks/agenthome/go.mod", "950e37a46e8d") - var u *check.Unheard - if !errors.As(err, &u) { - t.Errorf("err = %v, want it marked as reaching nobody", err) - } - // Issue 1373, milestone 47/G W6.9: the check that could not run is asked again when the part runs, heard as the - // node-engine seat's running.. on the checker's consumer; a running that names no part asks nothing. - t.Run("asked again on running", func(t *testing.T) { - for _, c := range []struct { - body string - want []string - }{ - {`{"v":1,"node":"novox","module":"gitea","fingerprint":"src2:ab","manifest":"sha256:cd","at":"2026-11-09T20:00:00Z"}`, []string{"novox gitea src2:ab"}}, - {`{"v":1,"node":"novox","module":"gitea"}`, nil}, - {`not json`, nil}, - } { - var got []string - heardRunning(context.Background(), runnerFunc(func(_ context.Context, node, module, fp string) { - got = append(got, node+" "+module+" "+fp) - }), []byte(c.body), t.Logf) - if strings.Join(got, ",") != strings.Join(c.want, ",") { - t.Errorf("%s asked %v, want %v", c.body, got, c.want) - } - } - }) -} - -type runnerFunc func(ctx context.Context, node, module, fingerprint string) - -func (f runnerFunc) PartRunning(ctx context.Context, node, module, fingerprint string) { - f(ctx, node, module, fingerprint) -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/integration_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/integration_test.go deleted file mode 100644 index 546fef66..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/integration_test.go +++ /dev/null @@ -1,1373 +0,0 @@ -//go:build integration - -package main - -import ( - "bytes" - "context" - "crypto/sha256" - "encoding/json" - "errors" - "fmt" - "maps" - "os" - "path/filepath" - "slices" - "sort" - "strings" - "sync" - "sync/atomic" - "testing" - "testing/fstest" - "time" - - "github.com/nats-io/nats-server/v2/server" - "github.com/nats-io/nats.go" - "github.com/nats-io/nats.go/jetstream" - - "mesh-delivery-checker/internal/check" - - "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - gobuild "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" - "git.novox.be/novox/mesh-sdk/go/announce" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/process" - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" - "git.novox.be/novox/mesh-sdk/go/seatlist/seatlisttest" - "git.novox.be/novox/mesh-sdk/go/views/viewstest" - - "mesh-delivery-checker/rules" -) - -// busConf is the shape of the composed accounts file (mesh-controller internal/broker): one account with JetStream, the -// controller's user and the checker's own principal .. It carries no per-principal permissions: the grants -// the controller composes from the manifest's state and reads (only the checker writes its buckets) are the controller's, -// and are proved by its own module check, not here. -const busConf = ` -listen: 127.0.0.1:-1 -jetstream { store_dir: %q } -accounts { - MESH { - jetstream: enabled - users = [ - { user: "ctl", password: "pw" } - { user: "n1.mesh-delivery-checker", password: "pw" } - ] - } -} -` - -const ( - repo = "novox/mesh-delivery" - head1 = "1111111111111111111111111111111111111111" - head2 = "2222222222222222222222222222222222222222" - trunk = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - merged = "fedcba9876543210fedcba9876543210fedcba98" // the merge commit of the captured fetch answer - digU = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11" - digI = "bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22" -) - -var captured = filepath.Join("..", "..", "internal", "check", "testdata", "captured") - -func readCaptured(t *testing.T, name string) map[string]any { - t.Helper() - raw, err := os.ReadFile(filepath.Join(captured, name+".json")) - if err != nil { - t.Fatal(err) - } - var v map[string]any - if err := json.Unmarshal(raw, &v); err != nil { - t.Fatal(err) - } - return v -} - -// fakeBuildModules serve the tools of mesh-build-go and mesh-build-node as the real ones do (ADR 0469 clause 2): a call on -// mesh.mod..call... answers at once with the step and its key, and the step's end is written to -// the key . of the module's steps bucket. What the steps end with are the results the build modules' own code -// marshalled (internal/check/testdata/captured, README). -type fakeBuildModules struct { - js jetstream.JetStream - ctx context.Context - conflict atomic.Bool - // hold is a commit whose fetch never answers: its ask stays taken, as one lost in a bus reload (issue 734). - hold atomic.Value - mu sync.Mutex - calls map[string]int // by `.` - testTool *testing.T - // beside is, by workspace, the siblings the fetch placed in it as ` `, sorted: a digest echoes them - // (1929/AC2; mesh-build-go buildstep.besideOf), or the level runs whole and keeps nothing. - beside map[string][]string - // names are the nodes' names the last check ask carried, for the names check (ADR 0567 §12). - names []string - // packages are, by repository, the package versions the last Go check ask of it carried (StepAsk.Packages). - packages map[string]buildstep.PackageVersions - // runs counts the run asks answered, and broken, when set, is why the checking program does not build (issue 1921). - runs atomic.Int32 - broken atomic.Value - // drop, when set, drops every run ask unanswered and counts it in dropped, as a build module that does not know the job - // run (the lead's ruling on milestone 47/B0). - drop atomic.Bool - dropped atomic.Int32 -} - -// droppingAsks is the seat's asks as a build module that does not know the job run takes them: a run ask is acknowledged and -// never begun while the fake drops them. -type droppingAsks struct { - gobuild.AskSource - f *fakeBuildModules -} - -func (d droppingAsks) Next(ctx context.Context, kind string, wait time.Duration) (*gobuild.Pulled, error) { - msg, err := d.AskSource.Next(ctx, kind, wait) - if err != nil || msg == nil || !d.f.drop.Load() { - return msg, err - } - var a buildstep.StepAsk - if json.Unmarshal(msg.Data, &a) == nil && a.Run != nil { - d.f.dropped.Add(1) - return nil, msg.Ack() - } - return msg, nil -} - -// run answers a run ask as mesh-build-go does: the checking program's work (rules.Program) over the input the ask names, -// asked of the seat verb it names on the bus and checked against its digest (buildstep.RunInput.Read, as mesh-build-go -// reads it), with no heads of its own laid. -func (f *fakeBuildModules) run(ctx context.Context, a buildstep.StepAsk) (any, error) { - f.runs.Add(1) - if why, _ := f.broken.Load().(string); why != "" { - return nil, errors.New(why) - } - answer := buildstep.RunAnswer{Built: true, Key: "program-0123456789abcdef"} - // As mesh-build-go does, the input is asked before the program runs, and a step whose input cannot be had fails. - body, err := meshdeliverycheckerclient.ReadInput(func(key string, body any) (json.RawMessage, error) { return askVerb(f.js.Conn(), key, body) }, a.Run.Input.Digest) - if err == nil { - err = a.Run.Input.Verify(body) - } - if err != nil { - return nil, err - } - out, err := rules.Program(a.Run.Args, func() ([]byte, error) { return body, nil }, fstest.MapFS{}) - if err != nil { - answer.Exit, answer.Said = 2, err.Error() - } - answer.Output = out - return answer, nil -} - -// askVerb asks a seat verb on the subject that names no caller and reads the runtime's reply, {result, error}. -func askVerb(nc *nats.Conn, key string, body any) (json.RawMessage, error) { - seat, verb, ok := strings.Cut(strings.TrimPrefix(key, "seat:"), ".") - if !ok || !strings.HasPrefix(key, "seat:") { - return nil, fmt.Errorf("%q names no seat verb", key) - } - data, err := json.Marshal(body) - if err != nil { - return nil, err - } - msg, err := nc.Request("mesh.seat."+seat+".tool."+verb, data, 5*time.Second) - if err != nil { - return nil, err - } - var r struct { - Result json.RawMessage `json:"result"` - Error string `json:"error"` - } - if err := json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Result, nil -} - -// serveInputVerb answers the checker seat's verb input on the bus as the checker's tools do (check.Reader.Input over the -// checker's state inputs), and counts what it answered (issue 1921, ADR 0596 §2). -func (b *bed) serveInputVerb(t *testing.T, nc *nats.Conn) { - t.Helper() - kv := b.buckets[testBucket(check.InputsStateName)] - r := check.Reader{InputState: check.StateRead{Get: func(key string) (json.RawMessage, error) { - e, err := kv.Get(b.ctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil - } - if err != nil { - return nil, err - } - return e.Value(), nil - }}} - if _, err := nc.Subscribe("mesh.seat.mesh-delivery-checker.tool."+meshdeliverychecker.VerbInput, func(m *nats.Msg) { - var ask meshdeliverychecker.InputInput - reply := map[string]any{} - if err := json.Unmarshal(m.Data, &ask); err != nil { - reply["error"] = err.Error() - } else if a, err := r.Input(ask.Digest); err != nil { - reply["error"] = err.Error() - } else { - reply["result"] = a - b.inputsAnswered.Add(1) - } - data, _ := json.Marshal(reply) - _ = m.Respond(data) - }); err != nil { - t.Fatal(err) - } -} - -func (f *fakeBuildModules) count(tool string) int { - f.mu.Lock() - defer f.mu.Unlock() - return f.calls[tool] -} - -func (f *fakeBuildModules) result(t *testing.T, tool string, args map[string]any) json.RawMessage { - level, _ := args["level"].(string) - var v any - switch { - case strings.HasSuffix(tool, "_fetch"): - if h, _ := f.hold.Load().(string); h != "" && args["commit"] == h { - <-f.ctx.Done() - } - name := "go-fetch-clean" - if f.conflict.Load() { - name = "go-fetch-conflict" - } - fetched := readCaptured(t, name) - fetched["commit"], fetched["base"] = args["commit"], args["base"] - var refs []struct { - Repository string `json:"repository"` - Commit string `json:"commit"` - } - raw, _ := json.Marshal(args["beside"]) - _ = json.Unmarshal(raw, &refs) - var lines []string - for _, r := range refs { - lines = append(lines, r.Repository+" "+r.Commit) - } - sort.Strings(lines) - f.mu.Lock() - if f.beside == nil { - f.beside = map[string][]string{} - } - f.beside[fmt.Sprint(fetched["workspace"])] = lines - f.mu.Unlock() - v = fetched - case strings.HasSuffix(tool, "_digest"): - d := map[string]string{"unit": digU, "integration": digI}[level] - f.mu.Lock() - beside := f.beside[fmt.Sprint(args["workspace"])] - f.mu.Unlock() - v = map[string]any{"level": level, "packages": map[string]string{"fixture/slow": d}, "beside": beside} - case strings.HasSuffix(tool, "_test"): - r := readCaptured(t, "go-test-slow") - r["level"] = level - v = r - } - data, _ := json.Marshal(v) - return data -} - -// serve makes module's steps bucket and its build seat's stream and workers, and pulls its asks on n1 as the real module -// does since R4g (ADR 0525; issue 1887 A5): each ask's job runs with step bodies answering the captured results, and a -// check takes the kept verdicts of the checker's kept bucket by their digests. -func (f *fakeBuildModules) serve(t *testing.T, module string, kept jetstream.KeyValue) { - kv, err := f.js.CreateKeyValue(f.ctx, jetstream.KeyValueConfig{Bucket: buildrun.StepsBucket(module), History: 1}) - if err != nil { - t.Fatal(err) - } - language := strings.TrimPrefix(module, "mesh-build-") - if err := gobuild.DeclareAskWorkers(f.ctx, f.js, language, 30*time.Second); err != nil { - t.Fatal(err) - } - prefix := map[string]string{goBuildModule: "build_go_", nodeBuildModule: "build_node_"}[module] - call := func(name string) gobuild.StepFunc { - return func(ctx context.Context, args map[string]any) (any, error) { - tool := prefix + name - f.mu.Lock() - f.calls[module+"."+tool]++ - f.mu.Unlock() - return f.result(t, tool, args), nil - } - } - steps := gobuild.AskSteps{Fetch: call("fetch"), Test: call("test"), - Kept: func(ctx context.Context, level, digest string) (int, bool, error) { - key, err := meshdeliverycheckerclient.KeptKey(level, digest) - if err != nil { - return 0, false, err - } - e, err := kept.Get(ctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return 0, false, nil - } - if err != nil { - return 0, false, err - } - v, err := meshdeliverycheckerclient.DecodeKept(e.Value()) - return int(v.Tests), err == nil, err - }} - if module == goBuildModule { - steps.Digest = call("digest") - } - put := func(key string, value any) (uint64, error) { - raw, _ := value.(json.RawMessage) - return kv.Put(f.ctx, key, raw) - } - p := &gobuild.Puller{Source: droppingAsks{&gobuild.JetStreamAsks{JS: f.js, Language: language}, f}, Language: language, Node: "n1", Put: put, - Capacity: func() int { return 2 }, PullWait: 200 * time.Millisecond, - Run: func(ctx context.Context, a buildstep.StepAsk) (any, error) { - if a.Check != nil { - f.mu.Lock() - f.names = a.Check.NodeNames - if repository := a.Check.Fetch.Repository; a.Language == buildstep.AskGo { - if f.packages == nil { - f.packages = map[string]buildstep.PackageVersions{} - } - f.packages[repository] = a.Packages - } - f.mu.Unlock() - } - if a.Run != nil { - return f.run(ctx, a) - } - return gobuild.RunAsk(ctx, steps, a) - }} - go func() { _ = p.Pull(f.ctx) }() -} - -// serveForge answers the seat git's verb file as its holder does (milestone 47/G W6.14), with the go.mod the forge gave -// (internal/check/testdata/captured), whose mesh-sdk pin is cb54756c3865 unless the test set it as stale, and a member's -// manifest at its head as the bed holds it, else absent. -func (b *bed) serveForge(t *testing.T, nc *nats.Conn) { - var captured struct { - File struct { - Content string `json:"content"` - } `json:"file"` - } - if err := json.Unmarshal(mustRead(t, "forge-get-file-go-mod"), &captured); err != nil { - t.Fatal(err) - } - respond := func(m *nats.Msg, a gitseat.FileAnswer) { - answer, _ := json.Marshal(map[string]any{"result": a, "node": "n0"}) - _ = m.Respond(answer) - } - for _, subject := range []string{"mesh.seat.git.call.file", "mesh.seat.git.call.file.>"} { - sub, err := nc.Subscribe(subject, func(m *nats.Msg) { - var in gitseat.FileInput - _ = json.Unmarshal(m.Data, &in) - switch { - case strings.HasSuffix(in.Path, "module.json"): - // A manifest at a member's head (design 51 *Tagged versions*): the bed's own, else absent. - if content, ok := b.heads.Load(in.Repository + " " + in.Path + " " + in.Ref); ok { - respond(m, gitseat.FileAnswer{Path: in.Path, Ref: in.Ref, Content: content.(string)}) - return - } - respond(m, gitseat.FileAnswer{Path: in.Path, Ref: in.Ref, Absent: true}) - case b.stalePin.Load(): - respond(m, gitseat.FileAnswer{Path: in.Path, Ref: in.Ref, Content: strings.Replace(captured.File.Content, "cb54756c3865", "9aaa5c80ac5c", 1)}) - default: - respond(m, gitseat.FileAnswer{Path: in.Path, Ref: in.Ref, Content: captured.File.Content}) - } - }) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = sub.Unsubscribe() }) - } -} - -func mustRead(t *testing.T, name string) []byte { - t.Helper() - raw, err := os.ReadFile(filepath.Join(captured, name+".json")) - if err != nil { - t.Fatal(err) - } - return raw -} - -type bed struct { - stalePin atomic.Bool - // heads are the manifests the forge holds at a member's head, by " ". - heads sync.Map - ctx context.Context - js jetstream.JetStream - buckets map[string]jetstream.KeyValue - tools map[string]jetstream.KeyValue - // inputsAnswered counts the inputs the seat's verb input answered (issue 1921). - inputsAnswered atomic.Int32 - fake *fakeBuildModules - srvURL string - // trunk is the catalog half's trunk revision the bed's seat answers `revision` with (design 47 G section 197). - trunkNow *atomic.Value - announce []string - mu sync.Mutex - logs saidLog -} - -// saidLog is what the program said, safe for its goroutines and the test's. -type saidLog struct { - mu sync.Mutex - b strings.Builder -} - -func (l *saidLog) Write(p []byte) (int, error) { l.mu.Lock(); defer l.mu.Unlock(); return l.b.Write(p) } -func (l *saidLog) String() string { l.mu.Lock(); defer l.mu.Unlock(); return l.b.String() } - -// said waits until the program said words, within 20 s: an event to wait on, not a pause a loaded machine can outlast. -func (b *bed) said(t *testing.T, words string) { - t.Helper() - for deadline := time.Now().Add(20 * time.Second); !strings.Contains(b.logs.String(), words); time.Sleep(10 * time.Millisecond) { - if time.Now().After(deadline) { - t.Fatalf("the checker did not say %q within 20 s:\n%s", words, b.logs.String()) - } - } -} - -// engine places each node and serves compose-candidate for the first one, as its node-engine does on the bus (answered on -// the seat verb's caller-named and plain subjects); the others never answer. It answers what it was asked. -func (b *bed) engine(t *testing.T, nodes ...string) func() []ne.ComposeCandidateInput { - t.Helper() - kv, err := b.js.CreateKeyValue(b.ctx, jetstream.KeyValueConfig{Bucket: testAssigner + "_assignments", History: 1}) - if err != nil { - t.Fatal(err) - } - for _, n := range nodes { - pk, _ := meshmoduleassignerclient.NodeKey(n) - raw, _ := meshmoduleassignerclient.EncodeNode(meshmoduleassigner.AssignmentsByNodeNodeValue{Node: n}) - if _, err := kv.Put(b.ctx, pk, raw); err != nil { - t.Fatal(err) - } - } - nc, err := nats.Connect(b.srvURL, nats.UserInfo("ctl", "pw")) - if err != nil { - t.Fatal(err) - } - t.Cleanup(nc.Close) - var mu sync.Mutex - var asked []ne.ComposeCandidateInput - answer := func(m *nats.Msg) { - ask, err := nec.DecodeAsk(m.Data) - if err != nil { - _ = m.Respond([]byte(`{"error":"` + err.Error() + `"}`)) - return - } - mu.Lock() - asked = append(asked, ask) - mu.Unlock() - a := ne.ComposeCandidateAnswer{Node: nodes[0], Candidate: ask.Candidate, Verdict: nec.Composed, - Own: ne.Module{Module: nec.Own, State: nec.ModuleComposed, Digest: "sha256:" + strings.Repeat("b", 64)}} - raw, err := nec.EncodeAnswer(a) - if err != nil { - t.Error(err) - } - out, _ := json.Marshal(map[string]any{"result": json.RawMessage(raw), "node": nodes[0]}) - _ = m.Respond(out) - } - for _, subject := range []string{"mesh.seat.node-engine.call.compose-candidate." + nodes[0] + ".>", "mesh.seat.node-engine.tool.compose-candidate." + nodes[0]} { - if _, err := nc.Subscribe(subject, answer); err != nil { - t.Fatal(err) - } - } - return func() []ne.ComposeCandidateInput { - mu.Lock() - defer mu.Unlock() - return slices.Clone(asked) - } -} - -// startBed starts the bus, the buckets the controller makes from the checker's and the other parts' manifests, the build -// modules' streams and the checker's durable consumers on them, one node's toolchains, the gate's source naming the trunk, and -// the fake build modules. It runs no checker. -func startBed(t *testing.T) *bed { - dir, err := os.MkdirTemp(os.Getenv("TMPDIR"), "c") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { os.RemoveAll(dir) }) - conf := filepath.Join(dir, "bus.conf") - if err := os.WriteFile(conf, []byte(fmt.Sprintf(busConf, filepath.Join(dir, "js"))), 0o600); err != nil { - t.Fatal(err) - } - opts, err := server.ProcessConfigFile(conf) - if err != nil { - t.Fatal(err) - } - opts.NoLog, opts.NoSigs = true, true - srv, err := server.NewServer(opts) - if err != nil { - t.Fatal(err) - } - go srv.Start() - if !srv.ReadyForConnections(10 * time.Second) { - t.Fatal("the bus did not come up") - } - t.Cleanup(srv.Shutdown) - ctl, err := nats.Connect(srv.ClientURL(), nats.UserInfo("ctl", "pw")) - if err != nil { - t.Fatal(err) - } - t.Cleanup(ctl.Close) - js, err := jetstream.New(ctl) - if err != nil { - t.Fatal(err) - } - // The seat mesh-catalog's holder answers the seat list with no trunk seat named: the checker's reader has then heard the seat - // and holds the bootstrapped definitions, which the judge is handed (ADR 0636 §1). - // It lists the build seats (design 47 G A-82): the seat whose acting rule builds Go and the one that builds TypeScript, from - // which the checker finds its build modules. - for _, subject := range []string{"mesh.seat.mesh-catalog.call.seat-definitions", "mesh.seat.mesh-catalog.call.seat-definitions.>"} { - if _, err := ctl.Subscribe(subject, func(m *nats.Msg) { _ = m.Respond(seatDefinitionsAnswer(m.Data)) }); err != nil { - t.Fatal(err) - } - } - trunkView := &atomic.Value{} - trunkView.Store(map[string]string{}) - for _, subject := range []string{"mesh.seat.mesh-catalog.call.revision", "mesh.seat.mesh-catalog.call.revision.>"} { - if _, err := ctl.Subscribe(subject, func(m *nats.Msg) { - var heads []meshcatalog.Head - for repo, commit := range trunkView.Load().(map[string]string) { - heads = append(heads, meshcatalog.Head{Repository: repo, Commit: commit}) - } - // Design 47 G §717, A-298: the trunk's entry of testAssigner claims mesh-module-assigner, so the catalog half names it - // the claimant whose seat-holders view the checker follows. - body, _ := json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Kind: "trunk", Branch: "main", Revision: 1, Trunk: heads}, - Entries: []meshcatalog.CatalogEntry{{Module: testAssigner, ManifestDigest: "sha256:" + strings.Repeat("a", 64), - Manifest: map[string]any{"module": testAssigner, "claims": []any{map[string]any{"name": meshmoduleassigner.Seat}}}}}}) - reply, _ := json.Marshal(map[string]any{"result": json.RawMessage(body), "node": "ctl"}) - _ = m.Respond(reply) - }); err != nil { - t.Fatal(err) - } - } - ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) - t.Cleanup(cancel) - b := &bed{trunkNow: trunkView, ctx: ctx, js: js, buckets: map[string]jetstream.KeyValue{}, tools: map[string]jetstream.KeyValue{}, srvURL: srv.ClientURL()} - for _, module := range []string{goBuildModule, nodeBuildModule} { - kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: buildrun.ToolchainsBucket(module), History: 1}) - if err != nil { - t.Fatal(err) - } - b.tools[module] = kv - } - // What the controller keeps of the messages a consumer gave up on (issue 734). - if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: buildrun.DeadLettersStream, Subjects: []string{"mesh.events.dead.>"}}); err != nil { - t.Fatal(err) - } - // The checker's streams as its manifest declares them (ADR 0619, ADR 0620), read from it so the bed holds every event - // it declares: checks, which the indexer and the forge bridge hear (preview, owed and a whole try's start and end, whose - // say would otherwise wait out its retries on a subject no stream holds), and asks, with verdict and tag-wanted. - raw, err := os.ReadFile("../../module.json") - if err != nil { - t.Fatal(err) - } - m, err := catalogue.Decode(raw) - if err != nil { - t.Fatal(err) - } - for _, s := range m.Streams { - var subjects []string - for _, e := range s.Events { - subjects = append(subjects, process.ModuleStreamSubject(testChecker, strings.ReplaceAll(e, "**", ">"))) - } - if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: s.BusName(), Subjects: subjects}); err != nil { - t.Fatal(err) - } - } - if !slices.ContainsFunc(m.Streams, func(s catalogue.StreamDeclaration) bool { return s.Name == "checks" }) || - meshdeliverychecker.HolderSubjectVerdict(testChecker) != process.ModuleStreamSubject(testChecker, "verdict") { - t.Fatalf("the checker's manifest declares the streams %+v, want checks, and asks at the definition's subjects", m.Streams) - } - // The shared events stream with the seats' and modules' events (the package registry seats' published among them), and the checker's durable consumer of it. - if _, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{Name: process.EventsStream, Subjects: []string{"mesh.mod.*.event.>", "mesh.seat.*.event.>"}}); err != nil { - t.Fatal(err) - } - if _, err := js.CreateOrUpdateConsumer(ctx, process.EventsStream, jetstream.ConsumerConfig{Durable: "n1_mesh-delivery-checker", AckPolicy: jetstream.AckExplicitPolicy}); err != nil { - t.Fatal(err) - } - for name, history := range map[string]int{testBucket(meshdeliverychecker.StateVerdicts): 5, testBucket(meshdeliverychecker.StateKept): 1, - testBucket(meshdeliverychecker.StateOwed): 5, testBucket(meshdeliverychecker.StatePreviews): 5, check.CandidatesBucket: 10, - check.ModulesBucket: 20, check.ManifestsBucket: 1, - check.PresenceBucket: 20, check.HaveBucket: 20, testBucket(check.TriesState): 1, - builds.BuildsBucket(builds.BuilderModule): 1, testBucket(check.InputsStateName): 1} { - kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: uint8(history)}) - if err != nil { - t.Fatal(err) - } - b.buckets[name] = kv - } - // The indexer and the assigner as the owners of the states the checker follows: their read tools and views families - // (ADR 0615, DLV-M2). - for _, st := range []string{candidatesState, modulesState, manifestsState, meshcatalog.StatePackages, check.IndexerPredefinedState, seatDefinitionsState} { - viewstest.Serve(t, ctx, js, ctl, check.IndexerModule, st, check.IndexerModule+".indexer_read_"+strings.ReplaceAll(st, "-", "_"), "n1_"+testChecker) - } - // The catalog half's registrations and the gate's source branch (design 47 G §614), and the claimant's seat-holders view naming - // testAssigner the holder of mesh-module-assigner (§717): nothing answers the seat's verb assignments. - for subject, result := range map[string]any{"mesh.seat.mesh-catalog.call.registrations": meshcatalog.RegistrationsAnswer{Registrations: []meshcatalog.Registration{}}, - "mesh.seat.mesh-delivery.call.source": map[string]any{"branches": []string{"main"}}} { - reply, _ := json.Marshal(map[string]any{"result": result, "node": "ctl"}) - for _, s := range []string{subject, subject + ".>"} { - if _, err := ctl.Subscribe(s, func(m *nats.Msg) { _ = m.Respond(reply) }); err != nil { - t.Fatal(err) - } - } - } - seatHolders := viewstest.Serve(t, ctx, js, ctl, testAssigner, meshmoduleassigner.StateSeatHolders, testAssigner+"."+meshmoduleassigner.StateSeatHolders, "n1_"+testChecker) - if _, err := seatHolders.Put(ctx, meshmoduleassigner.Seat, []byte(`{"seat":"`+meshmoduleassigner.Seat+`","holders":[{"module":"`+testAssigner+`","node":"n1"}]}`)); err != nil { - t.Fatal(err) - } - viewstest.Serve(t, ctx, js, ctl, testAssigner, meshmoduleassigner.StateAssignments, testAssigner+"."+meshmoduleassigner.StateAssignments, "n1_"+testChecker) - // 1929/AC1: the indexer's value of the repository's module, with no TypeScript manifest, so the Go build module alone - // judges the repository (an unknown repository is judged by both, issue 1887 A12). - known, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: repo, Commit: trunk, Path: "modules/m", None2: "the fingerprint is computed with its chain"}) - if err != nil { - t.Fatal(err) - } - if _, err := b.buckets[check.ModulesBucket].Put(ctx, "m", known); err != nil { - t.Fatal(err) - } - b.setOnline(t, "n1", true) - b.setNode(t, "n1") - // The indexer's modules name the holders of the build seats, by the seats their manifests claim. - b.putBuildModule(t, goBuildModule, "node-build-go") - b.putBuildModule(t, nodeBuildModule, "node-build-node") - for _, module := range []string{goBuildModule, nodeBuildModule} { - b.setHealth(t, "n1", module) - } - b.serveForge(t, ctl) - b.serveInputVerb(t, ctl) - b.fake = &fakeBuildModules{js: js, ctx: ctx, calls: map[string]int{}} - b.fake.serve(t, goBuildModule, b.buckets[testBucket(meshdeliverychecker.StateKept)]) - b.fake.serve(t, nodeBuildModule, b.buckets[testBucket(meshdeliverychecker.StateKept)]) - b.report(t, goBuildModule, "n1", "go", "go1.27.1", "1") - b.report(t, nodeBuildModule, "n1", "typescript", "tsc5", "1") - b.setTrunk(t, map[string]string{repo: trunk}) - return b -} - -func (b *bed) setTrunk(t *testing.T, heads map[string]string) { - t.Helper() - b.trunkNow.Store(heads) - body, _ := json.Marshal(meshcatalog.CatalogRevisionEvent{Kind: "trunk", Branch: "main", Revision: 1, V: 1}) - if _, err := b.js.Publish(b.ctx, meshcatalog.SubjectCatalogRevision("main"), body); err != nil { - t.Fatal(err) - } -} - -func (b *bed) setOnline(t *testing.T, node string, online bool) { - t.Helper() - data, _ := json.Marshal(meshwatchdogs.PresenceByNodeValue{V: keys.Version, Online: online}) - if _, err := b.buckets[check.PresenceBucket].Put(b.ctx, node, data); err != nil { - t.Fatal(err) - } -} - -// setNode writes the node's record in the node-engine's have, as the node-engine does, so the checker names it in each -// check ask (ADR 0567 §12); with no record the checker asks no check. -func (b *bed) setNode(t *testing.T, node string) { - t.Helper() - data, err := nodeengineclient.EncodeHaveNode(nodeengine.HaveByNodeNodeValue{Record: &nodeengine.NodeRecord{Node: node}}) - if err != nil { - t.Fatal(err) - } - key, _ := nodeengineclient.HaveNodeKey(node) - if _, err := b.buckets[check.HaveBucket].Put(b.ctx, key, data); err != nil { - t.Fatal(err) - } -} - -func (b *bed) setHealth(t *testing.T, node, module string) { - t.Helper() - data, err := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - if err != nil { - t.Fatal(err) - } - key, _ := nodeengineclient.HaveKey(node, module) - if _, err := b.buckets[check.HaveBucket].Put(b.ctx, key, data); err != nil { - t.Fatal(err) - } -} - -func (b *bed) report(t *testing.T, module, node, language, version, recipe string) { - t.Helper() - data, err := buildstep.EncodeToolchain(buildstep.Toolchain{Language: language, Version: version, Recipe: recipe, At: time.Now().UTC()}) - if err != nil { - t.Fatal(err) - } - key, _ := buildstep.ToolchainKey(node, language) - if _, err := b.tools[module].Put(b.ctx, key, data); err != nil { - t.Fatal(err) - } - seat := "node-build-go" - if module == nodeBuildModule { - seat = "node-build-node" - } - if _, err := b.js.Publish(b.ctx, "mesh.seat."+seat+".event.toolchain-reported."+node, []byte(`{}`)); err != nil { - t.Fatal(err) - } -} - -// candidate writes candidates. as the indexer does (the SDK's EncodeCandidate is the indexer's own encoder). -func (b *bed) candidate(t *testing.T, group string, pulls ...meshcatalog.CandidatesByGroupValuePullsItem) string { - t.Helper() - heads := make([]meshdeliveryclient.Head, len(pulls)) - for i, p := range pulls { - heads[i] = meshdeliveryclient.Head{Repository: p.Repository, Number: int(p.Number), Head: p.Head} - } - hash, err := meshdeliveryclient.HeadsHash(heads) - if err != nil { - t.Fatal(err) - } - data, err := json.Marshal(meshcatalog.CandidatesByGroupValue{V: keys.Version, Group: group, Heads: hash, Pulls: pulls, Fpset: meshcatalogclient.FingerprintSet(pulls)}) - if err != nil { - t.Fatal(err) - } - key, _ := meshcatalogclient.CandidatesKey(group) - if _, err := b.buckets[check.CandidatesBucket].Put(b.ctx, key, data); err != nil { - t.Fatal(err) - } - vk, _ := meshdeliverycheckerclient.VerdictKey(group, hash) - return vk -} - -func pull(repository string, number int, head string) meshcatalog.CandidatesByGroupValuePullsItem { - return meshcatalog.CandidatesByGroupValuePullsItem{Repository: repository, Number: int64(number), Head: head, Modules: []string{}} -} - -// start runs the program as the checker of a node. -func (b *bed) start(t *testing.T, env map[string]string) { - t.Helper() - broker := filepath.Join(t.TempDir(), "broker") - cred, _ := json.Marshal(process.Account{URL: b.srvURL, Node: "n1", Module: "mesh-delivery-checker", User: "n1.mesh-delivery-checker", Password: "pw"}) - if err := os.WriteFile(broker, cred, 0o600); err != nil { - t.Fatal(err) - } - // The settle window of a group's heads (check.DefaultSettle) is cut short: each bed's candidate is written whole. - e := map[string]string{"MESH_BROKER_FILE": broker, "MESH_CHECKER_SETTLE": "10ms"} - for k, v := range env { - e[k] = v - } - runCtx, stop := context.WithCancel(b.ctx) - done := make(chan error, 1) - go func() { done <- run(runCtx, func(k string) string { return e[k] }, &b.logs) }() - t.Cleanup(func() { stop(); <-done }) -} - -// verdict reads the verdict key until pred holds of it, within 20 s. -func (b *bed) verdict(t *testing.T, key string, pred func(meshdeliverychecker.VerdictsByGroupHeadsValue) bool) meshdeliverychecker.VerdictsByGroupHeadsValue { - t.Helper() - var last meshdeliverychecker.VerdictsByGroupHeadsValue - for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - e, err := b.buckets[testBucket(meshdeliverychecker.StateVerdicts)].Get(b.ctx, key) - if err != nil { - continue - } - if last, err = meshdeliverycheckerclient.DecodeVerdict(e.Value()); err == nil && pred(last) { - return last - } - } - t.Fatalf("verdicts.%s = %+v within 20 s", key, last) - return last -} - -func ended(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State != meshdeliverycheckerclient.VerdictRunning -} - -// owed reads the status the checker owes on a head. -func (b *bed) owed(t *testing.T, number int, head, state string) meshdeliverychecker.OwedByRepositoryNumberHeadActValue { - t.Helper() - key, err := check.CheckerOwedKey(repo, number, head) - if err != nil { - t.Fatal(err) - } - var last meshdeliverychecker.OwedByRepositoryNumberHeadActValue - for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - e, err := b.buckets[testBucket(meshdeliverychecker.StateOwed)].Get(b.ctx, key) - if err != nil { - continue - } - if last, err = check.DecodeCheckerOwed(e.Value()); err == nil && last.State == state { - return last - } - } - t.Fatalf("owed.%s = %+v within 20 s, want the state %s", key, last, state) - return last -} - -// TestCheckerJudgesACandidateAndOwesItsStatus: a candidate the indexer wrote, one pull request of mesh-delivery, is fetched -// merged onto the trunk the gate names, digested and tested at both levels on the node, kept, previewed; the verdict passes -// with the counts of both levels and the composition not run, and the status mesh/check is owed on the head as success. -func TestCheckerJudgesACandidateAndOwesItsStatus(t *testing.T) { // N6/AC5, 1300/AC5, 1445/AC1, 1929/AC1, ADR 0603 §10 - b := startBed(t) - // ADR 0603 §10: the candidate changes the module m, whose manifest the trunk holds; the builder has not built m's - // candidate fingerprint yet, so the check waits on the builder's key before it composes. - manifest := `{"module":"m"}` - digest := meshcatalogclient.ManifestDigest([]byte(manifest)) - stored, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "m", Manifest: manifest}) - if err != nil { - t.Fatal(err) - } - mkey, _ := meshcatalogclient.ManifestsKey(digest) - if _, err := b.buckets[check.ManifestsBucket].Put(b.ctx, mkey, stored); err != nil { - t.Fatal(err) - } - known, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: repo, Commit: trunk, Path: "modules/m", Manifest: digest, None2: "the fingerprint is computed with its chain"}) - if err != nil { - t.Fatal(err) - } - if _, err := b.buckets[check.ModulesBucket].Put(b.ctx, "m", known); err != nil { - t.Fatal(err) - } - fp := "src2:" + strings.Repeat("f", 64) - p := pull(repo, 7, head1) - p.Modules, p.Fps = []string{"m"}, map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"m": {Fp2: fp, Manifest: digest}} - key := b.candidate(t, "feat/x", p) - // ADR 0593 §7, §9 (D12): n1 and n2 are placed; n1's engine answers compose-candidate on the bus, n2's never does. - asked := b.engine(t, "n1", "n2") - b.start(t, nil) - b.said(t, "waits on the builder's builds of m before it composes") - if got := asked(); len(got) != 0 { - t.Fatalf("the engines were asked %+v before m's build ended", got) - } - bkey, _ := builds.BuilderBuildKey("m", fp) - building, _ := json.Marshal(builds.BuilderBuild{V: 1, State: builds.BuildBuilding}) - if _, err := b.buckets[builds.BuildsBucket(builds.BuilderModule)].Put(b.ctx, bkey, building); err != nil { - t.Fatal(err) - } - if v := b.verdict(t, key, func(meshdeliverychecker.VerdictsByGroupHeadsValue) bool { return true }); v.State != meshdeliverycheckerclient.VerdictRunning || len(asked()) != 0 { - t.Fatalf("verdict %s and %d asks while m builds: the check composes only once its build ended", v.State, len(asked())) - } - // m's build fails: the check fails naming it. Built again (builder_build_again writes the key done), the check asks - // itself again for the same heads, with no recheck, and passes. - failedBuild, _ := json.Marshal(builds.BuilderBuild{V: 1, State: builds.BuildFailed, Error: "go build: undefined: x"}) - if _, err := b.buckets[builds.BuildsBucket(builds.BuilderModule)].Put(b.ctx, bkey, failedBuild); err != nil { - t.Fatal(err) - } - if v := b.verdict(t, key, ended); v.State != meshdeliverycheckerclient.VerdictFail || v.Failed.Test != check.BuildFailedTest+"m" { - t.Fatalf("verdict %+v, want it failed on m's build", v) - } - built, _ := json.Marshal(builds.BuilderBuild{V: 1, State: builds.BuildDone, Manifest: digest}) - if _, err := b.buckets[builds.BuildsBucket(builds.BuilderModule)].Put(b.ctx, bkey, built); err != nil { - t.Fatal(err) - } - b.said(t, "the builds of m it failed on are done again, so it is checked again") - v := b.verdict(t, key, func(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictPass - }) - // The check asked again takes the packages the failed check ran from their kept verdicts. - if v.State != meshdeliverycheckerclient.VerdictPass || v.Levels[buildstep.LevelUnit].Kept != 2 || v.Levels[buildstep.LevelIntegration].Kept != 2 || v.Trunk[repo] != trunk { - t.Fatalf("verdict = %+v", v) - } - if c := v.Composition; c == nil || c.State != meshdeliverycheckerclient.CompositionPass || !slices.Equal(c.Machines, []string{"n1"}) || - !strings.Contains(c.Words, "n2: "+nec.NoAnswer) || len(v.Machines) != 1 || v.Machines[0] != "n1" { - t.Errorf("composition %+v machines %v", v.Composition, v.Machines) - } - if v.Composition != nil && strings.Contains(v.Composition.Words, "did not end") { - t.Errorf("the composition says %q: m's build ended within the bound", v.Composition.Words) - } - if got := asked(); len(got) == 0 || got[0].Candidate != key { - t.Errorf("n1's engine was asked %+v, want the check's candidate %s", got, key) - } - // Issue 1921 (B0): the verdict is the checking program's, built at the group's mesh-delivery head over its input kept in - // the state inputs and asked of the seat's verb input, two run asks a check (the seat list, the verdict); the verdict names the program's build. - if by := v.JudgedBy; by == nil || by.Program.Commit != head1 || by.Program.Package != check.CheckingProgramPackage || - by.Key != "program-0123456789abcdef" || len(by.Heads) != 1 || by.Heads[0].Commit != head1 { - t.Errorf("judged by %+v, want the checking program at the head %s", v.JudgedBy, head1) - } - if n := b.fake.runs.Load(); n != 4 { - t.Errorf("%d run asks, want two for each of the two checks", n) - } - // The operator's answer on B0: each run's input reached the build module through the seat's verb input on the bus. - if n := b.inputsAnswered.Load(); n != 4 { - t.Errorf("the verb input answered %d inputs, want one for each run ask", n) - } - b.fake.mu.Lock() - names := b.fake.names - b.fake.mu.Unlock() - if len(names) != 1 || names[0] != "n1" { - t.Errorf("the check ask named the nodes %v, want [n1] from the have's node record", names) - } - o := b.owed(t, 7, head1, "success") - if o.Context != meshdeliveryclient.StatusContextCheck || !strings.HasPrefix(o.Text, "pass 111111111111 on aaaaaaaaaaaa") || !strings.Contains(o.Text, "unit 0 run, 2 kept") { - t.Errorf("owed = %+v", o) - } - // Two checks of the same heads: the one failed on m's build and the one it asked itself again; the second runs no test. - for tool, want := range map[string]int{goBuildModule + ".build_go_fetch": 2, goBuildModule + ".build_go_digest": 4, goBuildModule + ".build_go_test": 2} { - if got := b.fake.count(tool); got != want { - t.Errorf("%s called %d times, want %d", tool, got, want) - } - } - // What passed is kept once under each level's digest. - for _, k := range []string{"unit." + digU, "integration." + digI} { - if _, err := b.buckets[testBucket(meshdeliverychecker.StateKept)].Get(b.ctx, k); err != nil { - t.Errorf("kept.%s: %v", k, err) - } - } - // The preview of the head merged onto the trunk, with the tree and the manifest digest the fetch named. - pk, _ := meshdeliverycheckerclient.PreviewKey(repo, head1, trunk) - e, err := b.buckets[testBucket(meshdeliverychecker.StatePreviews)].Get(b.ctx, pk) - if err != nil { - t.Fatalf("previews.%s: %v", pk, err) - } - pv, err := meshdeliverycheckerclient.DecodePreview(e.Value()) - if err != nil || pv.Merge != merged || pv.Modules["modules/m"].Tree != "1111111111111111111111111111111111111111" { - t.Errorf("preview = %+v, %v", pv, err) - } -} - -// TestACheckOrACarryOfEqualInputsRunsNothing: a second candidate (another head, the same digests) is checked with every -// package taken from the first's kept verdicts: no test step runs and no second kept key appears. Then, through the real bus -// (novox/hq ADR 0548 §3, §5): a third candidate is not claimed while the gate's merge turn on its repository stands, and is -// claimed when the turn's key is deleted; and a move of the trunk carries the first verdict on the recheck stream by its -// unchanged digests, running no test, the verdict naming the trunk it was carried from. -func TestACheckOrACarryOfEqualInputsRunsNothing(t *testing.T) { // 1287/AC5, 1288/AC6, 1929/AC2, ADR 0548 §3, §5 - b := startBed(t) - first := b.candidate(t, "feat/x", pull(repo, 7, head1)) - b.start(t, nil) - b.verdict(t, first, ended) - tests, kept := b.fake.count(goBuildModule+".build_go_test"), keptCount(t, b) - second := b.candidate(t, "feat/y", pull(repo, 8, head2)) - v := b.verdict(t, second, ended) - if v.State != meshdeliverycheckerclient.VerdictPass || v.Levels[buildstep.LevelUnit].Run != 0 || v.Levels[buildstep.LevelUnit].Kept != 2 || v.Levels[buildstep.LevelIntegration].Run != 0 { - t.Fatalf("verdict = %+v, want everything taken from the kept verdicts", v) - } - if got := b.fake.count(goBuildModule + ".build_go_test"); got != tests { - t.Errorf("build_go_test called %d times, was %d: a test ran for equal inputs", got, tests) - } - if got := keptCount(t, b); got != kept { - t.Errorf("%d kept keys, was %d", got, kept) - } - - // Design 47 G §278 (test 52): a new check is claimed at once, with no state of the gate granted to the checker. - third := b.candidate(t, "feat/z", pull(repo, 9, "3333333333333333333333333333333333333333")) - b.verdict(t, third, ended) - - // §5: the trunk moves; the first verdict is carried by its digests, nothing runs. - tests = b.fake.count(goBuildModule + ".build_go_test") - const moved = "abababababababababababababababababababab" - b.setTrunk(t, map[string]string{repo: moved}) - c := b.verdict(t, first, func(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { return ended(v) && v.Trunk[repo] == moved }) - if c.State != meshdeliverycheckerclient.VerdictPass || c.CarriedFrom[repo] != trunk || c.Levels[buildstep.LevelUnit].Run != 0 || c.Levels[buildstep.LevelIntegration].Run != 0 { - t.Fatalf("the carried verdict = %+v, want a pass carried from %s with no test run", c, trunk[:12]) - } - if got := b.fake.count(goBuildModule + ".build_go_test"); got != tests { - t.Errorf("build_go_test called %d times on the carry, was %d: a carry of unchanged digests runs nothing", got, tests) - } - if o := b.owed(t, 7, head1, "success"); !strings.HasPrefix(o.Text, "pass 111111111111 on abababababab | carried from aaaaaaaaaaaa") { - t.Errorf("the carried status says %q", o.Text) - } -} - -func keptCount(t *testing.T, b *bed) int { - t.Helper() - keys, err := b.buckets[testBucket(meshdeliverychecker.StateKept)].Keys(b.ctx) - if err != nil { - return 0 - } - return len(keys) -} - -// TestACheckThatCannotBeJudgedFailsNamingWhy: a head that does not merge fails naming the files in conflict and runs no -// test (1289/AC4); a checking program whose run asks no build node begins, and one that does not build, fail the check -// naming the checking program, never judged by the running rules (issue 1921, the lead's ruling on milestone 47/B0: the -// begin bound is the checker's setting, shortened here). -func TestACheckThatCannotBeJudgedFailsNamingWhy(t *testing.T) { // 1289/AC4, 1921 - for _, row := range []struct { - name string - set func(f *fakeBuildModules) - env map[string]string - words string - tests bool // the digest and test steps ran - dropped int32 // the run asks dropped: the seat list's, put twice, and no verdict asked after it - program bool // the checking program judged it - }{ - {name: "a head that does not merge", set: func(f *fakeBuildModules) { f.conflict.Store(true) }, words: "files in conflict: a.go, sub/b.go", program: true}, - {name: "a run ask no build node begins", set: func(f *fakeBuildModules) { f.drop.Store(true) }, env: map[string]string{"MESH_CHECKER_BEGIN_BOUND": "500ms"}, - words: "no build node began the job run of the asks", tests: true, dropped: 2}, - {name: "a program that does not build", set: func(f *fakeBuildModules) { f.broken.Store("go build: undefined: rules.Nothing") }, - words: "by the checking program: its run ask", tests: true}, - } { - t.Run(row.name, func(t *testing.T) { - b := startBed(t) - row.set(b.fake) - key := b.candidate(t, "feat/x", pull(repo, 7, head1)) - b.start(t, row.env) - v := b.verdict(t, key, ended) - if v.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(v.Failed.Words, row.words) || (v.JudgedBy != nil) != row.program { - t.Fatalf("verdict = %+v, want a fail saying %q, judged by the program %v", v, row.words, row.program) - } - if o := b.owed(t, 7, head1, "failure"); !strings.HasPrefix(o.Text, "failed") || - !strings.Contains(o.Text, map[bool]string{true: row.words, false: "the checking program"}[row.program]) { - t.Errorf("owed = %+v", o) - } - if n := b.fake.count(goBuildModule+".build_go_digest") + b.fake.count(goBuildModule+".build_go_test"); (n != 0) != row.tests { - t.Errorf("%d digest and test steps ran, want them run %v", n, row.tests) - } - if n := b.fake.dropped.Load(); n != row.dropped { - t.Errorf("%d run asks dropped, want %d", n, row.dropped) - } - if !row.tests { - pk, _ := meshdeliverycheckerclient.PreviewKey(repo, head1, trunk) - if e, err := b.buckets[testBucket(meshdeliverychecker.StatePreviews)].Get(b.ctx, pk); err != nil { - t.Errorf("previews.%s: %v", pk, err) - } else if pv, err := meshdeliverycheckerclient.DecodePreview(e.Value()); err != nil || strings.Join(pv.Conflicts, ",") != "a.go,sub/b.go" || pv.Merge != "" { - t.Errorf("preview = %+v, %v, want the files in conflict alone", pv, err) - } - } - }) - } -} - -// TestAnHqPullRequestGetsNoVerdictAndAnotherDoes: hq's pull request is judged by no one (ADR 0484 clause 8), the other one in -// the same run is. -// 1887/A10, ADR 0527 §2: a pull request into hq gets a verdict and a mesh/check from the checker, as any repository's. -func TestAnHqPullRequestGetsAVerdictAndAStatus(t *testing.T) { - b := startBed(t) - b.setTrunk(t, map[string]string{repo: trunk, "novox/hq": trunk}) - hq := b.candidate(t, "feat/hq", pull("novox/hq", 3, head1)) - b.start(t, nil) - if v := b.verdict(t, hq, ended); v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("hq's verdict = %+v", v) - } - key, _ := check.CheckerOwedKey("novox/hq", 3, head1) - for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(50 * time.Millisecond) { - if e, err := b.buckets[testBucket(meshdeliverychecker.StateOwed)].Get(b.ctx, key); err == nil { - if o, err := check.DecodeCheckerOwed(e.Value()); err == nil && o.State == "success" { - return - } - } - if time.Now().After(deadline) { - t.Fatalf("hq#3 owes no success within 20 s") - } - } -} - -// TestACheckLeftRunningIsTakenOverWhenTheCheckerStarts: the verdict key holds running, left by a checker that stopped; the -// started checker judges it again and ends it (ADR 0478 clause 6, as the builder's). -func TestACheckLeftRunningIsTakenOverWhenTheCheckerStarts(t *testing.T) { // N6/AC2 - b := startBed(t) - key := b.candidate(t, "feat/x", pull(repo, 7, head1)) - group, heads, _ := meshdeliverycheckerclient.ParseVerdictKey(key) - left, err := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: group, Heads: heads, Trunk: map[string]string{repo: trunk}, State: meshdeliverycheckerclient.VerdictRunning}) - if err != nil { - t.Fatal(err) - } - if _, err := b.buckets[testBucket(meshdeliverychecker.StateVerdicts)].Create(b.ctx, key, left); err != nil { - t.Fatal(err) - } - b.start(t, nil) - if v := b.verdict(t, key, ended); v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("verdict = %+v", v) - } -} - -// TestAMemberNamingAMeshSDKCommitOffTheTrunkIsJudgedAndNotRefused: the go.mod the forge answers names a commit that main does -// not hold; the check is not refused for it, since the build takes the latest published mesh-sdk whatever go.mod names -// (novox/hq ADR 0573 §2, §4; issue 1585 S14, which retires issue 1624's pin check). -func TestAMemberNamingAMeshSDKCommitOffTheTrunkIsJudgedAndNotRefused(t *testing.T) { // 1624/AC2 as ADR 0573 §4 changes it - b := startBed(t) - b.stalePin.Store(true) - key := b.candidate(t, "feat/x", pull(repo, 7, head1)) - b.start(t, nil) - if v := b.verdict(t, key, ended); v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", v) - } - - // Design 47 G §528: a group whose mesh-sdk member declares a Go package is judged at once, with no tagged version asked - // or waited for, and its other member is built against the group's head. - const sdkRepo = "novox/mesh-sdk" - b.setTrunk(t, map[string]string{repo: trunk, sdkRepo: trunk}) - b.heads.Store(sdkRepo+" module.json "+head2, `{"module":"mesh-sdk","build":{"artifacts":[{"name":"go","kind":"package","language":"go","paths":["go"]}]}}`) - sdk := pull(sdkRepo, 12, head2) - sdk.Modules, sdk.Fps = []string{"mesh-sdk"}, map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"mesh-sdk": {Fp2: "src2:" + strings.Repeat("e", 64)}} - key = b.candidate(t, "feat/x", pull(repo, 8, head1), sdk) - if v := b.verdict(t, key, ended); v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("the group's verdict = %+v", v) - } - b.fake.mu.Lock() - got := b.fake.packages[repo] - b.fake.mu.Unlock() - if got[check.SDKModulePath] != head2 { - t.Errorf("mesh-delivery was built against %v, want mesh-sdk's Go module at the group's head %s", got, head2) - } - - // DLV-C2, DLV-C6 (design 26 *Out of step*, ADR 0620): each check's end says one verdict per pull request on the stream - // asks, at the definition's subject and in its body (decoded strictly): the lone pull request's, then the group's two. - verdicts, err := b.js.OrderedConsumer(b.ctx, "ASKS", jetstream.OrderedConsumerConfig{FilterSubjects: []string{meshdeliverychecker.HolderSubjectVerdict(testChecker)}}) - if err != nil { - t.Fatal(err) - } - said := map[int64]meshdeliverychecker.VerdictHolderEvent{} - for len(said) < 3 { - msg, err := verdicts.Next(jetstream.FetchMaxWait(20 * time.Second)) - if err != nil { - t.Fatalf("verdicts said %+v, then none: %v", said, err) - } - var e meshdeliverychecker.VerdictHolderEvent - dec := json.NewDecoder(bytes.NewReader(msg.Data())) - dec.DisallowUnknownFields() - if err := dec.Decode(&e); err != nil { - t.Fatalf("verdict %s is not the definition's body: %v", msg.Data(), err) - } - if _, twice := said[e.Pull]; twice { - t.Errorf("a second verdict of pull %d: %+v", e.Pull, e) - } - said[e.Pull] = e - } - for pull, want := range map[int64]meshdeliverychecker.VerdictHolderEvent{7: {Repository: repo, Pull: 7, Head: head1, Verdict: meshdeliverycheckerclient.VerdictPass}, - 8: {Repository: repo, Pull: 8, Head: head1, Verdict: meshdeliverycheckerclient.VerdictPass}, 12: {Repository: sdkRepo, Pull: 12, Head: head2, Verdict: meshdeliverycheckerclient.VerdictPass}} { - if got := said[pull]; got.Repository != want.Repository || got.Head != want.Head || got.Verdict != want.Verdict || len(got.OutOfStep) != 0 { - t.Errorf("verdict of pull %d = %+v, want %+v", pull, got, want) - } - } -} - -// triesStore is the tries bucket as the tools write it. -type triesStore struct { - kv jetstream.KeyValue - ctx context.Context -} - -func (s triesStore) Keys() ([]string, error) { - keys, err := s.kv.Keys(s.ctx) - if err == jetstream.ErrNoKeysFound { - return nil, nil - } - return keys, err -} -func (s triesStore) Get(key string) (json.RawMessage, error) { - e, err := s.kv.Get(s.ctx, key) - if err == jetstream.ErrKeyNotFound { - return nil, nil - } - if err != nil { - return nil, err - } - return e.Value(), nil -} -func (s triesStore) Create(key string, value []byte) error { - _, err := s.kv.Create(s.ctx, key, value) - return err -} - -// TestATryIsJudgedAndItsEndWrittenWithNoStatusOwed: a try written as checker_try writes it is taken by the running checker, -// judged through the build modules as a pull request's head merged onto its trunk, and ended pass in its key, with no -// verdict and no status owed (ADR 0501 §5). A whole try waits for the indexer's candidate of its group, which it reads on the -// key's watch, and writes the merge previews the indexer makes it from (issue 1921). -func TestATryIsJudgedAndItsEndWrittenWithNoStatusOwed(t *testing.T) { - b := startBed(t) - asked := b.engine(t, "n1") // a whole try is composed on every placed node, as a group's check is - b.start(t, nil) - store := triesStore{b.buckets[testBucket(check.TriesState)], b.ctx} - id, err := check.AskTry(store, map[string]string{repo: trunk}, check.TryAsk{Repository: repo, Head: head1}, time.Now()) - if err != nil { - t.Fatal(err) - } - var last check.Try - // The indexer's candidate of the try's group (here written by the test), at the heads of its members. - b.candidate(t, id, pull(repo, 0, head1)) - for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - raw, _ := store.Get(id) - if last, err = check.DecodeTry(raw); err == nil && last.Over() { - break - } - } - if last.State != check.TryPass || last.Trunk != trunk || last.Took == "" || last.Composition == nil { - t.Fatalf("try = %+v, want passed on the trunk with its composition made", last) - } - if got := asked(); len(got) != 1 || !strings.HasPrefix(got[0].Candidate, id+".") { - t.Errorf("the placed node's engine was asked %+v, want the try's candidate %s.", got, id) - } - if b.fake.count(goBuildModule+".build_go_fetch") == 0 { - t.Errorf("the try fetched nothing") - } - // A whole try's start and end are said on the stream checks, which the indexer hears to write and delete the - // candidate of its group: said once each, never waited out on a subject no stream holds. - checks, err := b.js.Stream(b.ctx, "CHECKS") - if err != nil { - t.Fatal(err) - } - for _, event := range []string{checkermsg.Started, checkermsg.Ended} { // the end is said once the try's end is written - said, err := b.js.OrderedConsumer(b.ctx, checks.CachedInfo().Config.Name, jetstream.OrderedConsumerConfig{ - FilterSubjects: []string{process.ModuleStreamSubject(testChecker, event)}}) - if err != nil { - t.Fatal(err) - } - if m, err := said.Next(jetstream.FetchMaxWait(5 * time.Second)); err != nil || !strings.Contains(string(m.Data()), id) { - t.Errorf("the stream checks holds no %s of the try %s: %v", event, id, err) - } - } - for _, name := range []string{testBucket(meshdeliverychecker.StateOwed), testBucket(meshdeliverychecker.StateVerdicts)} { - if keys, _ := b.buckets[name].Keys(b.ctx); len(keys) != 0 { - t.Errorf("a try wrote %s: %v", name, keys) - } - } - // Issue 734: a try whose ask the build seat gives up on (the controller keeps its copy in DEAD_LETTERS with the ask's - // message id) ends as error saying so, and its head asked again is a new try. - b.fake.hold.Store(head2) - lost, err := check.AskTry(store, map[string]string{repo: trunk}, check.TryAsk{Repository: repo, Head: head2}, time.Now()) - if err != nil { - t.Fatal(err) - } - steps, err := b.js.KeyValue(b.ctx, buildrun.StepsBucket(goBuildModule)) - if err != nil { - t.Fatal(err) - } - askID := "" - for deadline := time.Now().Add(20 * time.Second); askID == "" && time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - keys, _ := steps.Keys(b.ctx) - for _, k := range keys { - if e, err := steps.Get(b.ctx, k); err == nil && strings.Contains(string(e.Value()), `"running"`) { - askID = strings.TrimPrefix(k, "n1.") - } - } - } - if askID == "" { - t.Fatal("the held try's ask was never taken") - } - dead := &nats.Msg{Subject: "mesh.events.dead.SEAT_NODE_BUILD_GO.SEAT_NODE_BUILD_GO_CHECK_worker", Header: nats.Header{}} - dead.Header.Set(buildrun.DeadHeaderMsgID, askID) - dead.Header.Set(buildrun.DeadHeaderDeliveries, "5") - dead.Header.Set(buildrun.DeadHeaderStream, "SEAT_NODE_BUILD_GO") - dead.Header.Set(buildrun.DeadHeaderGaveUp, time.Now().UTC().Format(time.RFC3339Nano)) - if _, err := b.js.PublishMsg(b.ctx, dead); err != nil { - t.Fatal(err) - } - for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - raw, _ := store.Get(lost) - if last, err = check.DecodeTry(raw); err == nil && last.Over() { - break - } - } - if last.State != check.TryError || !strings.Contains(last.Said, buildrun.LostWords+" after 5 deliveries") { - t.Fatalf("the try whose ask was given up = %+v, want error saying so", last) - } - if again, err := check.AskTry(store, map[string]string{repo: trunk}, check.TryAsk{Repository: repo, Head: head2}, time.Now()); err != nil || again == lost { - t.Errorf("its head asked again answered %q, %v, want a new try", again, err) - } -} - -// TestTheCheckerSaysOwedReadableOnTheBus: started, the checker says on its own states subject what it keeps, owed among -// the readable ones, which is what grants the forge module its read of owed (ADR 0495 §2-§3, ADR 0521; R4e finding R2-1). -func TestTheCheckerSaysOwedReadableOnTheBus(t *testing.T) { // R2-1 - b := startBed(t) - b.start(t, nil) - events, err := b.js.Stream(b.ctx, "EVENTS") - if err != nil { - t.Fatal(err) - } - var said announce.States - for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { - m, err := events.GetLastMsgForSubject(b.ctx, announce.Subject("mesh-delivery-checker")) - if err != nil { - continue - } - if err := json.Unmarshal(m.Data, &said); err != nil { - t.Fatal(err) - } - break - } - if said.Module != "mesh-delivery-checker" || said.Node != "n1" || !said.Keeps("owed") || !said.Readable("owed") || - !said.Readable("previews") || !said.Readable("verdicts") || !said.Readable("kept") || !said.Readable("tries") || said.Readable("hearing") || - !said.Keeps("asks") || said.Readable("asks") || !said.Keeps("first-views") || said.Readable("first-views") || !said.Keeps("findings") || !said.Keeps("inputs") || said.Readable("inputs") || !said.Keeps("compositions") || said.Readable("compositions") || !said.Keeps("could-not-run") || said.Readable("could-not-run") || len(said.States) != 12 { - t.Fatalf("the checker said %+v, want its twelve states, asks, hearing, first-views, inputs, compositions and could-not-run its own (DLV-M2; W6.9), with kept, owed, previews, tries and verdicts readable, tries by the delivery view (1929/AC4; 1887 A5b; build_usage)", said) - } - // Design 47 G §717, A-298: the assignments are followed at the holder the seat-holders view of the catalog half's claimant - // names, no seat verb asked for it. - b.said(t, "holds "+testAssigner+"'s assignments, the holder of the seat mesh-module-assigner") -} - -// testChecker is the module the bed's credential names; the checker's own buckets and events are named from it (design 47 G A-296). -const testChecker = "mesh-delivery-checker" - -func testBucket(state string) string { return check.Bucket(testChecker, state) } - -const testAssigner = "acme-assigner" // a holder of the seat mesh-module-assigner no code names - -// buildSeatDefinitions are the seat definitions the bed's seat mesh-catalog lists: the build seats, as the mesh's own definitions of -// them, whose acting rules say which language each builds. -var buildSeatDefinitions = func() map[string]seatdef.Definition { - out := map[string]seatdef.Definition{} - for _, d := range seatlisttest.Definitions() { - if d.Seat == "node-build-go" || d.Seat == "node-build-node" { - out[d.Seat] = d - } - } - return out -}() - -// seatDefinitionsAnswer is the seat mesh-catalog's answer to the seat list's asks: every trunk seat with its digest when no -// seat is named, one seat's definition when it is. -func seatDefinitionsAnswer(body []byte) []byte { - var ask struct { - Seat string `json:"seat"` - } - _ = json.Unmarshal(body, &ask) - entry := func(seat string) seatlist.Entry { - raw, _ := json.Marshal(buildSeatDefinitions[seat]) - return seatlist.Entry{Seat: seat, Source: seatlist.SourceTrunk, Digest: fmt.Sprintf("%x", sha256.Sum256(raw)), Definition: raw} - } - var out any - if ask.Seat == "" { - l := seatlist.Listing{Revision: 1} - for _, seat := range slices.Sorted(maps.Keys(buildSeatDefinitions)) { - e := entry(seat) - e.Definition = nil - l.Seats = append(l.Seats, e) - } - out = l - } else if _, ok := buildSeatDefinitions[ask.Seat]; ok { - e := entry(ask.Seat) - out = seatlist.Answer{Seat: ask.Seat, Found: true, Entry: &e, Revision: 1} - } else { - out = seatlist.Answer{Seat: ask.Seat, Revision: 1} - } - raw, _ := json.Marshal(map[string]any{"result": out, "node": "ctl"}) - return raw -} - -// putBuildModule writes the indexer's module and manifest of a module that claims a build seat, so the checker finds the seat's -// holder (design 47 G A-82). Its repository is its own, so no repository the tests judge changes the build modules it names. -func (b *bed) putBuildModule(t *testing.T, module, seat string) { - t.Helper() - manifest := fmt.Sprintf(`{"module":%q,"version":"0.1","slug":"b","claims":[{"name":%q,"scope":"node"}]}`, module, seat) - digest := fmt.Sprintf("%x", sha256.Sum256([]byte(manifest))) - mkey, err := meshcatalogclient.ManifestsKey(digest) - if err != nil { - t.Fatal(err) - } - stored, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Manifest: manifest, Module: module}) - if err != nil { - t.Fatal(err) - } - if _, err := b.buckets[check.ManifestsBucket].Put(b.ctx, mkey, stored); err != nil { - t.Fatal(err) - } - value, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/build-modules", Commit: strings.Repeat("b", 40), Path: "modules/" + module, Manifest: digest, - None2: "the fingerprint is computed with its chain"}) - if err != nil { - t.Fatal(err) - } - if _, err := b.buckets[check.ModulesBucket].Put(b.ctx, module, value); err != nil { - t.Fatal(err) - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge.go deleted file mode 100644 index 3e3ec09f..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge.go +++ /dev/null @@ -1,604 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "path" - "slices" - "sort" - "strconv" - "strings" - "sync" - "time" - - "github.com/nats-io/nats.go/jetstream" - - "mesh-delivery-checker/internal/check" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/keys" - "git.novox.be/novox/mesh-sdk/go/process" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" -) - -// judgeDeps are the parts the judge of a check is made of. -type judgeDeps struct { - // module is the checker's own, the credential's (check.Examiner.Module). - module string - // inputs keeps the checking program's inputs (check.InputsStateName, served on check.InputVerb). - inputs check.Inputs - // beginBound is how long a run ask may wait for a build node to begin it; check.DefaultBeginBound when 0. - beginBound time.Duration - asks check.Asks - renames check.Renames - seatDefs check.SeatDefinitions - // seats are the seat list the checker's reader holds, the base the checking program lays the heads over (seatlist.go). - seats check.SeatList - // buildSeats finds the build modules from the seat list and the holders of seats (design 47 G A-82). - buildSeats *buildrun.BuildSeats - tools *buildrun.ToolchainSet - trunks *check.Trunks - kept check.KV - previews check.KV - // candidates answers the indexer's candidate of a try's group (try_candidates.go); nil leaves a whole try with no - // manifest or composition judged, said. - candidates check.TryCandidates - repos *repoModules - // published is the indexer's packages state as it is followed (packages.go). - published *publishedPackages - forge check.Forge - announce check.Announcer - builds check.BuildStates - // ends waits on the builder's builds of a candidate before the check composes (ADR 0603 §10); nil composes without waiting. - ends check.BuildEnds - // buildWait is the setting build-wait-bound; check.DefaultBuildWait when 0. - buildWait time.Duration - // compositions makes a check's composition from the engines' answers (ADR 0593 §7); nil leaves it not run. - compositions check.CompositionReader - // nodes names every node with a record; nil hands no names. - nodes *nodeNames - // predefined is the indexer's kept list of predefined keys; nil reads none. - predefined check.PredefinedList - logf func(format string, args ...any) -} - -// keptPredefined reads the indexer's key predefined.list (the seat mesh-catalog's state predefined), bound once its state appears. -type keptPredefined struct{ kv jetstream.KeyValue } - -func (k keptPredefined) PredefinedList(ctx context.Context) (json.RawMessage, error) { - e, err := k.kv.Get(ctx, meshcatalog.PredefinedByListKey()) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil - } - if err != nil { - return nil, err - } - kept, err := meshcatalog.DecodePredefinedByListValue(e.Value()) - if err != nil { - return nil, err - } - return json.Marshal(kept.List) -} - -// keptSeatDefinitions reads a pull request's entries of the indexer's seat definitions from its first view: each whose -// `unparsed` says why its head's definition does not parse (the entry's shape is the indexer's read tool's answer). -type keptSeatDefinitions struct { - keys func(ctx context.Context) ([]string, error) - get func(ctx context.Context, key string) ([]byte, error) -} - -// seatDefinitionsOf reads the state's picture (a key gone between the keys and its get is none). -func seatDefinitionsOf(kv jetstream.KeyValue) keptSeatDefinitions { - return keptSeatDefinitions{keys: func(ctx context.Context) ([]string, error) { - keys, err := kv.Keys(ctx) - if errors.Is(err, jetstream.ErrNoKeysFound) { - return nil, nil - } - return keys, err - }, get: func(ctx context.Context, key string) ([]byte, error) { - e, err := kv.Get(ctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil - } - if err != nil { - return nil, err - } - return e.Value(), nil - }} -} - -func (k keptSeatDefinitions) Unparsed(ctx context.Context, repository string, pull int) ([]check.UnparsedSeat, error) { - suffix := ".pull." + keys.EscapeName(repository) + "." + strconv.Itoa(pull) - keys, err := k.keys(ctx) - if err != nil { - return nil, err - } - sort.Strings(keys) - var out []check.UnparsedSeat - for _, key := range keys { - if !strings.HasSuffix(key, suffix) { - continue - } - raw, err := k.get(ctx, key) - if err != nil { - return nil, err - } - if raw == nil { - continue - } - var v struct { - Seat string `json:"seat"` - Module string `json:"module"` - Directory string `json:"directory"` - Unparsed string `json:"unparsed"` - } - if err := json.Unmarshal(raw, &v); err != nil { - return nil, fmt.Errorf("%s is no seat definition entry: %w", key, err) - } - if v.Unparsed != "" { - out = append(out, check.UnparsedSeat{Seat: v.Seat, Module: v.Module, Directory: v.Directory, Why: v.Unparsed}) - } - } - return out, nil -} - -// nodeNames reads every node's name from the node-engine's have, one key `.node` per node with a record (novox/hq -// ADR 0567 §12), at each ask: a node enrolled since is named in the next check. -type nodeNames struct{ kv jetstream.KeyValue } - -// openNodeNames opens the node-engine's have for the nodes' names alone: it is not watched, so no check waits on a node. -func openNodeNames(ctx context.Context, js process.BucketOpener, logf func(string, ...any)) *nodeNames { - return &nodeNames{kv: process.OptionalBucket(ctx, js, check.HaveBucket, "no check is asked until it is: the names check needs the nodes' names", logf)} -} - -func (n nodeNames) Names(ctx context.Context) ([]string, error) { - if n.kv == nil { - return nil, fmt.Errorf("the bucket %s could not be opened at the checker's start", check.HaveBucket) - } - lister, err := n.kv.ListKeysFiltered(ctx, "*."+check.HaveNodeWord) - if err != nil { - return nil, err - } - var out []string - for key := range lister.Keys() { - if node, _, isNode, err := nodeengineclient.ParseHaveKey(key); err == nil && isNode { - out = append(out, node) - } - } - sort.Strings(out) - return out, nil -} - -// runningServes says what the running builds serve (novox/hq ADR 0614 §7), read when a check asks it: every module's have -// in the node-engine's have that runs healthy, the builder's build of its fingerprint, and the verbs its manifest serves -// (graph.Served). A build whose manifest cannot be read serves nothing here, said in the log. -type runningServes struct { - have *nodeNames - builds check.BuildStates - read check.ManifestReader - logf func(format string, args ...any) -} - -func (r runningServes) Served(ctx context.Context) (func(string) bool, error) { - if r.have == nil || r.have.kv == nil || r.builds == nil || r.read == nil { - return nil, fmt.Errorf("the node-engine's have, the builder's builds or the indexer's manifests cannot be read") - } - lister, err := r.have.kv.ListKeys(ctx) - if err != nil { - return nil, err - } - haves := map[string]map[string]nodeengine.HaveByNodeModuleValue{} - for key := range lister.Keys() { - node, module, isNode, err := nodeengineclient.ParseHaveKey(key) - if err != nil || isNode { - continue - } - e, err := r.have.kv.Get(ctx, key) - if err != nil { - continue - } - if h, err := nodeengineclient.DecodeHave(e.Value()); err == nil { - if haves[node] == nil { - haves[node] = map[string]nodeengine.HaveByNodeModuleValue{} - } - haves[node][module] = h - } - } - served, unread := graph.Served(haves, func(module, fp string) ([]string, bool) { - b, found, err := r.builds.Build(ctx, module, fp) - if err != nil || !found || b.Manifest == "" { - return nil, false - } - m, err := r.read.Manifest(ctx, b.Manifest) - if err != nil { - return nil, false - } - return graph.ServesOf(m), true - }, nil) - if len(unread) > 0 && r.logf != nil { - r.logf("the manifest of the running build of %v cannot be read, so it serves nothing the check counts (ADR 0614 §7)", unread) - } - return served, nil -} - -func newJudge(d judgeDeps) check.Judge { - var nodes interface { - Names(ctx context.Context) ([]string, error) - } - if d.nodes != nil { - nodes = d.nodes - } - ex := &check.Examiner{Module: d.module, Nodes: nodes, Compositions: d.compositions, Asks: d.asks, Toolchains: d.tools, - Kept: check.Kept{Store: d.kept}, Previews: d.previews, Candidates: d.candidates, Modules: d.repos, Packages: d.repos, Published: d.published, KnownPackages: d.published, Forge: d.forge, Runs: d.repos, Builds: d.builds, BuildEnds: d.ends, BuildWait: d.buildWait, Announce: d.announce, Trunks: d.trunks, Manifests: d.repos, Read: d.repos, Dirs: d.repos, Predefined: d.predefined, Renames: d.renames, SeatDefinitions: d.seatDefs, SeatList: d.seats, Seats: d.buildSeats, Inputs: d.inputs, BeginBound: d.beginBound, Logf: d.logf} - if d.nodes != nil && d.builds != nil && d.repos != nil { - ex.Serving = runningServes{have: d.nodes, builds: d.builds, read: d.repos, logf: d.logf} - } - return ex -} - -// manifestKV reads a manifest by its digest. -type manifestKV interface { - Get(ctx context.Context, key string) (value []byte, revision uint64, found bool, err error) -} - -// repoModules knows which build modules judge a repository, from the indexer's modules and their manifests: the Go build -// module always (a repository's go.mod files are its own), the Node one when a module of the repository has a TypeScript -// artifact (ADR 0484 clause 10). Safe for use from the watch and from a check at once. -type repoModules struct { - manifests manifestKV - logf func(format string, args ...any) - // verdicts keeps the indexer's verdict on each module's manifest, for the sandbox's composer (engines.go); nil keeps none. - verdicts *manifestVerdicts - // seats finds the build modules (design 47 G A-82); set before the first check. - seats *buildrun.BuildSeats - - mu sync.Mutex - claims map[string][]string // by manifest digest: the seats its module claims - langs map[string]map[string]bool // by manifest digest: the languages of its artifacts - tsPkgs map[string][]string // by manifest digest: the names of its TypeScript package artifacts - path map[string]string // by module: its directory in its repository - named map[string]map[string][]string // by module: each artifact's named paths, as the indexer keeps them - repoOf map[string]string // by module: its repository - fp map[string]string // by module: its entry's fingerprint, whose graph entry holds the manifest - commit map[string]string // by module: the commit its record was computed at (the commit the mesh runs) - typed map[string]string // by module: its manifest digest - parsed map[string]catalogue.Manifest // by manifest digest: the manifest as read, once (a digest's manifest never changes) -} - -// Manifest is the kept manifest of a digest, read from the indexer's manifests state once and then from memory. -func (r *repoModules) Manifest(ctx context.Context, digest string) (catalogue.Manifest, error) { - r.mu.Lock() - m, known := r.parsed[digest] - r.mu.Unlock() - if known { - return m, nil - } - key, err := meshcatalogclient.ManifestsKey(digest) - if err != nil { - return catalogue.Manifest{}, err - } - raw, _, found, err := r.manifests.Get(ctx, key) - if err != nil { - return catalogue.Manifest{}, err - } - if !found { - return catalogue.Manifest{}, fmt.Errorf("the indexer keeps no manifest %s", digest) - } - kept, err := meshcatalogclient.DecodeManifest(raw) - if err != nil { - return catalogue.Manifest{}, err - } - if m, err = parseManifest(kept.Manifest); err != nil { - return catalogue.Manifest{}, err - } - r.mu.Lock() - if r.parsed == nil { - r.parsed = map[string]catalogue.Manifest{} - } - r.parsed[digest] = m - r.mu.Unlock() - return m, nil -} - -// Put takes a change of the indexer's modules bucket in: a module's repository and the languages its manifest names. -func (r *repoModules) Put(ctx context.Context, key string, value []byte, deleted bool) { - r.mu.Lock() - if r.repoOf == nil { - r.repoOf, r.typed, r.langs, r.commit = map[string]string{}, map[string]string{}, map[string]map[string]bool{}, map[string]string{} - r.fp = map[string]string{} - r.tsPkgs, r.path, r.named = map[string][]string{}, map[string]string{}, map[string]map[string][]string{} - } - if deleted { - if r.verdicts != nil { - r.verdicts.put(key, nil) - } - delete(r.repoOf, key) - delete(r.typed, key) - delete(r.fp, key) - delete(r.commit, key) - delete(r.path, key) - delete(r.named, key) - r.mu.Unlock() - return - } - r.mu.Unlock() - m, err := meshcatalogclient.DecodeModule(value) - if err != nil { - return - } - if r.verdicts != nil { - r.verdicts.put(key, meshcatalogclient.ModuleVerdict(key, m)) - } - r.mu.Lock() - r.repoOf[key] = m.Repository - r.typed[key] = m.Manifest - r.fp[key] = m.Fp2 - r.commit[key] = m.Commit - r.path[key] = m.Path - r.named[key] = m.Paths - _, known := r.langs[m.Manifest] - r.mu.Unlock() - if m.Manifest == "" || known { - return - } - set := map[string]bool{} - var pkgs, claimed []string - if mk, err := meshcatalogclient.ManifestsKey(m.Manifest); err == nil { - if raw, _, found, err := r.manifests.Get(ctx, mk); err == nil && found { - if kept, err := meshcatalogclient.DecodeManifest(raw); err == nil { - if man, err := parseManifest(kept.Manifest); err == nil { - for _, c := range man.Claims { - claimed = append(claimed, c.Name) - } - } - if man, err := parseManifest(kept.Manifest); err == nil && man.Build != nil { - for _, a := range man.Build.Artifacts { - if a.Language != "" { - set[a.Language] = true - } - if a.Kind == "package" && a.Language == "typescript" { - pkgs = append(pkgs, a.Name) - } - } - } else if err != nil && r.logf != nil { - r.logf("the manifest %s cannot be read, so its module counts as no TypeScript: %v", m.Manifest, err) - } - } - } - } - r.mu.Lock() - r.langs[m.Manifest] = set - r.tsPkgs[m.Manifest] = pkgs - if r.claims == nil { - r.claims = map[string][]string{} - } - r.claims[m.Manifest] = claimed - r.mu.Unlock() -} - -// ModuleDir is a module's directory in its repository as the indexer's modules hold it (check.Examiner.Dirs); false for a -// module the indexer holds no directory of. -func (r *repoModules) ModuleDir(module string) (string, bool) { - r.mu.Lock() - defer r.mu.Unlock() - d, ok := r.path[module] - return d, ok && d != "" -} - -// TypeScriptPackages are the TypeScript package artifacts of a repository's modules, sorted by directory: each by its -// directory in the repository and the paths, relative to the repository, it is built from: the named paths the indexer -// keeps in each module's value (meshcatalog.ModulesByModuleValue.ArtifactPaths; only the indexer reads them from a -// manifest, the lead's decision L8), under the module's directory. A package's directory is the one path it names when -// that names a directory (the contracts folders, ts/seats/, each with its own package.json), else the module's -// directory (the SDK's lib names its package.json beside its sources), as the Node build module reads it -// (mesh-build-node artifact.packageDir). None for a module whose value holds no paths. -func (r *repoModules) TypeScriptPackages(repository string) []check.TypeScriptPackage { - r.mu.Lock() - defer r.mu.Unlock() - var out []check.TypeScriptPackage - for module, repo := range r.repoOf { - if repo != repository { - continue - } - dir := strings.Trim(r.path[module], "/") - if dir == meshcatalogclient.RootPath || dir == "." { - dir = "" - } - under := func(p string) string { - p = strings.Trim(strings.TrimPrefix(p, "./"), "/") - if dir != "" { - p = dir + "/" + p - } - return p - } - for _, name := range r.tsPkgs[r.typed[module]] { - named := r.named[module][name] - if len(named) == 0 { - continue - } - pkg := check.TypeScriptPackage{Dir: dir} - if one := strings.Trim(strings.TrimPrefix(named[0], "./"), "/"); len(named) == 1 && one != "" && !strings.Contains(path.Base(one), ".") { - pkg.Dir = under(one) - } - for _, p := range named { - pkg.Paths = append(pkg.Paths, under(p)) - } - sort.Strings(pkg.Paths) - out = append(out, pkg) - } - } - sort.Slice(out, func(i, j int) bool { return out[i].Dir < out[j].Dir }) - return out -} - -// HolderOf is the module whose manifest claims the seat, as the indexer's modules hold them: none while no module of the mesh -// claims it, and the first by name where several do (design 47 G A-82, as the catalog half's holder is read by the builder). -func (r *repoModules) HolderOf(seat string) (string, bool) { - r.mu.Lock() - defer r.mu.Unlock() - found := "" - for module, digest := range r.typed { - if slices.Contains(r.claims[digest], seat) && (found == "" || module < found) { - found = module - } - } - return found, found != "" -} - -// BuildModules are the build modules that judge a repository, sorted: the build module of Go always, and that of TypeScript -// when a module of the repository builds TypeScript. A repository of which the mesh knows no module (mesh-lab, hq) -// is judged by both, each finding its own directories in the tree (go.mod files; package.json files), so a repository -// with tests in either language and no manifest is judged whole (issue 1887 A12). A language no build seat on the seat list -// builds has none. -func (r *repoModules) BuildModules(repository string) []string { - var out []string - add := func(language string) { - if m, err := r.seats.For(language, ""); err == nil { - out = append(out, m.Module) - } - } - add("go") - r.mu.Lock() - typescript := false - known := false - for module, repo := range r.repoOf { - if repo != repository { - continue - } - known = true - if r.langs[r.typed[module]]["typescript"] { - typescript = true - break - } - } - r.mu.Unlock() - if typescript || !known { - add("typescript") - } - sort.Strings(out) - return out -} - -// buildReader reads the builder's build of a module at a fingerprint (mesh-delivery-builder_builds). -type buildReader struct{ kv kvStore } - -func (r buildReader) Build(ctx context.Context, module, fp2 string) (builds.BuilderBuild, bool, error) { - key, err := builds.BuilderBuildKey(module, fp2) - if err != nil { - return builds.BuilderBuild{}, false, err - } - value, _, found, err := r.kv.Get(ctx, key) - if err != nil || !found { - return builds.BuilderBuild{}, false, err - } - b, err := builds.DecodeBuilderBuild(value) - return b, err == nil, err -} - -// buildEnds hears the builder's build keys of a candidate on the bus (mesh-delivery-builder_builds): a watch of exactly -// those keys gives each one's value now and every write the builder makes after, so the check waits on the part that owns -// the fact and polls nothing (novox/hq ADR 0603 §10). -type buildEnds struct{ kv jetstream.KeyValue } - -func (e buildEnds) Ended(ctx context.Context, asked map[string]string) (map[string]builds.BuilderBuild, error) { - return e.until(ctx, asked, func(b builds.BuilderBuild) bool { - return b.State == builds.BuildDone || b.State == builds.BuildFailed - }) -} - -func (e buildEnds) Done(ctx context.Context, asked map[string]string) bool { - done, err := e.until(ctx, asked, func(b builds.BuilderBuild) bool { return b.State == builds.BuildDone }) - return err == nil && len(done) == len(asked) -} - -// until hears the builds' keys until each holds a build that ended holds, or ctx ends, and answers those that do, by module. -func (e buildEnds) until(ctx context.Context, asked map[string]string, ended func(builds.BuilderBuild) bool) (map[string]builds.BuilderBuild, error) { - byKey := map[string]string{} - keys := make([]string, 0, len(asked)) - for module, fp2 := range asked { - key, err := builds.BuilderBuildKey(module, fp2) - if err != nil { - return nil, err - } - byKey[key] = module - keys = append(keys, key) - } - out := map[string]builds.BuilderBuild{} - if len(keys) == 0 { - return out, nil - } - w, err := e.kv.WatchFiltered(ctx, keys) - if err != nil { - return nil, err - } - defer func() { _ = w.Stop() }() - for len(out) < len(keys) { - select { - case <-ctx.Done(): - return out, nil // the bound: the builds not ended are left out - case entry, open := <-w.Updates(): - if !open { - return out, nil - } - if entry == nil || entry.Operation() != jetstream.KeyValuePut { - continue // the end of the values now, or a key deleted: it has not ended - } - module, ours := byKey[entry.Key()] - if !ours { - continue - } - b, err := builds.DecodeBuilderBuild(entry.Value()) - if err == nil && ended(b) { - out[module] = b - } else { - delete(out, module) // asked again (builder_build_again), or not a build the check can read: not ended - } - } - } - return out, nil -} - -// ManifestDigests is the manifest digest of every module the indexer holds, by module: the catalogue the indexer's judge -// judges a change's manifests in (novox/hq ADR 0578 §2).8 §2). -func (r *repoModules) ManifestDigests() map[string]string { - r.mu.Lock() - defer r.mu.Unlock() - out := map[string]string{} - for module, digest := range r.typed { - if digest != "" { - out[module] = digest - } - } - return out -} - -// ManifestFingerprints is the fingerprint of every module the indexer holds, by module, whose catalog graph entry holds its manifest -// (design 47 G A-310). -func (r *repoModules) ManifestFingerprints() map[string]string { - r.mu.Lock() - defer r.mu.Unlock() - out := map[string]string{} - for module, fp := range r.fp { - if fp != "" { - out[module] = fp - } - } - return out -} - -// CommitOf is the commit the indexer's record of the module names: the commit its fingerprint first appeared at, so the source -// of the build the mesh runs (a later merge that leaves the module alone writes no record, the operator 2026-10-25). -func (r *repoModules) CommitOf(module string) string { - r.mu.Lock() - defer r.mu.Unlock() - return r.commit[module] -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge_test.go deleted file mode 100644 index b77fec22..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judge_test.go +++ /dev/null @@ -1,258 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "maps" - "os" - "reflect" - "slices" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/keys" - - "mesh-delivery-checker/internal/check" -) - -type memManifests map[string][]byte - -func (m memManifests) Get(_ context.Context, key string) ([]byte, uint64, bool, error) { - v, ok := m[key] - return v, 1, ok, nil -} - -func putModule(t *testing.T, r *repoModules, name, repository, manifest string) { - t.Helper() - data, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: repository, Commit: strings.Repeat("a", 40), Fp: "src1:" + strings.Repeat("a", 64), Manifest: meshcatalogclient.ManifestDigest([]byte(manifest))}) - if err != nil { - t.Fatal(err) - } - r.Put(context.Background(), name, data, false) -} - -func TestARepositoryWithATypeScriptModuleIsJudgedByTheNodeBuildModuleToo(t *testing.T) { // 1292/AC2 - own, err := os.ReadFile("../../module.json") // a manifest with a Go bundle: this module's own - if err != nil { - t.Fatal(err) - } - goManifest := string(own) - // A TypeScript bundle names neither a system nor a directory it is built from (the catalogue's parser says so). - var doc map[string]any - if err := json.Unmarshal(own, &doc); err != nil { - t.Fatal(err) - } - artifacts := doc["build"].(map[string]any)["artifacts"].([]any) - program := artifacts[0].(map[string]any) - program["language"] = "typescript" - delete(program, "system") - delete(program, "from") - delete(program, "binary") - tsRaw, err := json.Marshal(doc) - if err != nil { - t.Fatal(err) - } - tsManifest := string(tsRaw) - store := memManifests{} - for _, m := range []string{goManifest, tsManifest} { - data, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "demo", Manifest: m}) - if err != nil { - t.Fatal(err) - } - store[meshcatalogclient.ManifestDigest([]byte(m))] = data - } - r := &repoModules{manifests: store, seats: testBuildSeats()} - putModule(t, r, "mesh-delivery-checker", "novox/mesh-delivery", goManifest) - putModule(t, r, "mesh-ui", "novox/mesh-catalog", tsManifest) - if got := r.BuildModules("novox/mesh-delivery"); strings.Join(got, ",") != goBuildModule { - t.Errorf("mesh-delivery: %v, want the Go build module alone", got) - } - if got := r.BuildModules("novox/mesh-catalog"); strings.Join(got, ",") != goBuildModule+","+nodeBuildModule { - t.Errorf("mesh-catalog: %v, want both", got) - } - if got := r.CommitOf("mesh-delivery-checker"); got != strings.Repeat("a", 40) || r.CommitOf("gone") != "" { - t.Errorf("the commit the mesh runs = %q", got) - } - // 1887/A12: a repository with no module the mesh knows (mesh-lab: nested go.mod files and a root package.json) is - // judged by both build modules. - if got := r.BuildModules("novox/mesh-lab"); strings.Join(got, ",") != goBuildModule+","+nodeBuildModule { - t.Errorf("a repository with no module: %v, want both build modules", got) - } -} - -// sdkManifest is mesh-sdk's own manifest (its module.json at feat/r4e-sdk): one TypeScript package naming its paths. -const sdkManifest = `{"module":"mesh-sdk","version":"1","slug":"sdk","build":{"artifacts":[{"name":"lib","kind":"package","language":"typescript",` + - `"paths":["src","test","package.json","package-lock.json","tsconfig.json"]}]},"resources":[]}` - -func TestTheTypeScriptPackagesPathsComeFromTheIndexersModulesNotTheManifest(t *testing.T) { // ADR 0500 §5 with lead decision L8 - store := memManifests{} - data, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "mesh-sdk", Manifest: sdkManifest}) - if err != nil { - t.Fatal(err) - } - store[meshcatalogclient.ManifestDigest([]byte(sdkManifest))] = data - r := &repoModules{manifests: store} - put := func(path string, paths map[string][]string) { - v, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/mesh-sdk", Commit: strings.Repeat("a", 40), Fp: "src1:" + strings.Repeat("a", 64), - Manifest: meshcatalogclient.ManifestDigest([]byte(sdkManifest)), Path: path, Paths: paths}) - if err != nil { - t.Fatal(err) - } - r.Put(context.Background(), "mesh-sdk", v, false) - } - str := func(pkgs []check.TypeScriptPackage) string { - var out []string - for _, p := range pkgs { - out = append(out, p.Dir+":"+strings.Join(p.Paths, ",")) - } - return strings.Join(out, " ") - } - for _, row := range []struct { - name, path string - paths map[string][]string - want string - }{ - {"the indexer's word, not the manifest's five paths: the SDK's lib at the root", ".", map[string][]string{"lib": {"src", "package.json"}}, ":package.json,src"}, - {"under the module's directory", "ts", map[string][]string{"lib": {"package.json", "src"}}, "ts:ts/package.json,ts/src"}, - {"one path naming a directory is the package's own (A-285)", "modules/mesh-delivery-contracts", map[string][]string{"lib": {"ts/seats/git"}}, "modules/mesh-delivery-contracts/ts/seats/git:modules/mesh-delivery-contracts/ts/seats/git"}, - {"a value written before the indexer kept paths: nothing to serve by", ".", nil, ""}, - } { - put(row.path, row.paths) - if got := str(r.TypeScriptPackages("novox/mesh-sdk")); got != row.want { - t.Errorf("%s: packages = %q, want %q", row.name, got, row.want) - } - } - if got := r.TypeScriptPackages("novox/mesh-delivery"); len(got) != 0 { - t.Errorf("another repository: %v", got) - } -} - -// ADR 0593 §2 (D8): a Go check builds every mesh package at the version the indexer's packages state publishes, read -// there and computed nowhere here: a key's module at its commit, a deleted key gone, an unreadable value left out. -func TestThePublishedVersionsAreTheIndexersPackagesState(t *testing.T) { - var p publishedPackages - // Not read yet, the state names no package and judges none; read and empty, it is an empty list judged against. - if got := p.PackageNames(); got != nil { - t.Errorf("before the first reading the known packages = %v, want nil", got) - } - p.Answered() - if got := p.PackageNames(); got == nil || len(got) != 0 { - t.Errorf("read and empty, the known packages = %#v, want an empty map", got) - } - put := func(module, repository, dir, commit string) { - key := meshcatalog.PackagesByModuleKey(keys.EscapeName(module)) - now := time.Now().UTC() - raw, err := json.Marshal(meshcatalog.PackagesByModuleValue{V: keys.Version, Module: module, Repository: repository, Dir: dir, - Commit: commit, Requires: strings.Repeat("e", 64), - Packages: []meshcatalog.PackagesByModuleValuePackagesItem{{Package: module, Digest: strings.Repeat("d", 64)}}, At: &now}) - if err != nil { - t.Fatal(err) - } - p.Put(key, raw, false) - } - const sdk, build = "git.novox.be/novox/mesh-sdk/go", "git.novox.be/novox/mesh-catalog/modules/mesh-build-go" - put(sdk, "novox/mesh-sdk", "go", strings.Repeat("a", 40)) - put(build, "novox/mesh-catalog", "modules/mesh-build-go", strings.Repeat("b", 40)) - p.Put("broken", []byte("{"), false) - if got := p.LatestPackages(); !reflect.DeepEqual(got, buildstep.PackageVersions{sdk: strings.Repeat("a", 40), build: strings.Repeat("b", 40)}) { - t.Errorf("latest packages = %v", got) - } - key := meshcatalog.PackagesByModuleKey(keys.EscapeName(build)) - p.Put(key, nil, true) - if got := p.LatestPackages(); len(got) != 1 || got[sdk] == "" { - t.Errorf("after a delete: %v", got) - } - // Issue 1966: a module record names its module, with no series to say it, and none of the packages inside it. - if got := p.PackageNames(); !reflect.DeepEqual(got, map[string]string{sdk: ""}) { - t.Errorf("the known packages from a module record = %v", got) - } - // A series key names its package by its ecosystem name, the judge's known packages (DLV-I14, design 32), and is no - // module's published version. - now := time.Now().UTC() - series, err := json.Marshal(meshcatalog.PackagesByPackageSeriesValue{V: keys.Version, Name: sdk, Package: "mesh-sdk/go", Series: "0.1", At: &now}) - if err != nil { - t.Fatal(err) - } - p.Put(meshcatalog.PackagesByPackageSeriesKey(keys.EscapeName(sdk), "0.1"), series, false) - if got := p.PackageNames(); !reflect.DeepEqual(got, map[string]string{sdk: "mesh-sdk/go"}) || len(p.LatestPackages()) != 1 { - t.Errorf("the known packages = %v, the latest %v", got, p.LatestPackages()) - } - t.Run("a pull request's unparsed seat definitions are its entries of the indexer's seat-definitions", func(t *testing.T) { - state := map[string]string{ - "web-api.trunk": `{"seat":"web-api","source":"trunk","digest":"d"}`, - "web-api.pull." + keys.EscapeName("novox/web") + ".7": `{"seat":"web-api","source":"pull","module":"web-contracts","directory":"go","unparsed":"seat.json: bad"}`, - "mail-api.pull." + keys.EscapeName("novox/web") + ".7": `{"seat":"mail-api","source":"pull","digest":"e"}`, - "web-api.pull." + keys.EscapeName("novox/web") + ".17": `{"seat":"web-api","source":"pull","unparsed":"another pull request's"}`, - } - k := keptSeatDefinitions{keys: func(context.Context) ([]string, error) { return slices.Collect(maps.Keys(state)), nil }, - get: func(_ context.Context, key string) ([]byte, error) { return []byte(state[key]), nil }} - got, err := k.Unparsed(context.Background(), "novox/web", 7) - if want := []check.UnparsedSeat{{Seat: "web-api", Module: "web-contracts", Directory: "go", Why: "seat.json: bad"}}; err != nil || !reflect.DeepEqual(got, want) { - t.Errorf("unparsed of novox/web#7 = %+v, %v; want %+v", got, err, want) - } - }) -} - -// A manifest whose config value falls back to a predefined key (ADR 0348 §7) is read, not logged as unreadable for the -// catalogue's predefined.json this process never holds: the indexer and the assigner read it with -// catalogue.ParseManifestForInputs (issues 1751, 1906). xterm's module.json from mesh-catalog at 835bf22b. -func TestAManifestFallingBackToAPredefinedKeyIsRead(t *testing.T) { - raw, err := os.ReadFile("testdata/catalogue-835bf22b-xterm.module.json") - if err != nil { - t.Fatal(err) - } - manifest := string(raw) - data, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "xterm", Manifest: manifest}) - if err != nil { - t.Fatal(err) - } - store := memManifests{meshcatalogclient.ManifestDigest([]byte(manifest)): data} - var said []string - r := &repoModules{manifests: store, logf: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }} - putModule(t, r, "xterm", "novox/mesh-catalog", manifest) - r.BuildModules("novox/mesh-catalog") - for _, s := range said { - if strings.Contains(s, "cannot be read") { - t.Errorf("the manifest was not read: %s", s) - } - } - if _, err := parseManifest(manifest); err != nil { - t.Errorf("manifestReader: %v", err) - } - // The sandbox's composer may be an older node-engine, which knows no verdict waits: it is handed a wait as a refusal. - var held manifestVerdicts - held.put("nats", &meshcatalog.VerdictsByDigestValue{Module: "nats", State: meshcatalogclient.VerdictManifestWaits, Unknown: []string{"core-credentials"}, Judge: "j"}) - if v := held.all()["nats"]; v.State != meshcatalogclient.VerdictRefused || len(v.Problems) != 1 || len(v.Unknown) != 0 { - t.Errorf("a waiting verdict is handed to the sandbox as %+v", v) - } -} - -// The build modules are no names (design 47 G A-82): the holder of a seat is the module whose manifest, as the indexer's modules -// hold it, claims the seat. -func TestTheHolderOfASeatIsTheModuleWhoseManifestClaimsIt(t *testing.T) { - manifest := `{"module":"go-builds","version":"1","slug":"g","claims":[{"name":"node-build-go","scope":"node"}]}` - data, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "go-builds", Manifest: manifest}) - if err != nil { - t.Fatal(err) - } - r := &repoModules{manifests: memManifests{meshcatalogclient.ManifestDigest([]byte(manifest)): data}} - putModule(t, r, "go-builds", "novox/builds", manifest) - if got, ok := r.HolderOf("node-build-go"); !ok || got != "go-builds" { - t.Errorf("the holder of node-build-go = %q, %v, want go-builds", got, ok) - } - if got, ok := r.HolderOf("node-build-node"); ok { - t.Errorf("the holder of a seat no manifest claims = %q, want none", got) - } - var found []string - var changed buildsWatch - changed.Set(func() { found = append(found, "changed") }) - changed.Changed() - if len(found) != 1 { - t.Errorf("the build modules' change was not called through its watch: %v", found) - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judgeseats.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judgeseats.go deleted file mode 100644 index 2bc14572..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/judgeseats.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "maps" - "slices" - "sync/atomic" - - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" -) - -// The checker reads the seat list through mesh-sdk's seatlist.Part, the one wiring (design 47 G §204): what the reader holds is the -// indexer's judge's Seats of a request, which the manifests of a change are judged against. - -// seatList is the reader as the check's SeatList: the held definitions raw, and an error while the seat has not answered. -type seatList struct{ reader *seatlist.Reader } - -func (s seatList) Definitions() ([]json.RawMessage, error) { - if s.reader == nil { - return nil, fmt.Errorf("the checker follows no seat list") - } - if st := s.reader.Status(); !st.Heard { - return nil, fmt.Errorf("%s", st.Health()) - } - defs := s.reader.Definitions() - var out []json.RawMessage - for _, seat := range slices.Sorted(maps.Keys(defs)) { - raw, err := json.Marshal(defs[seat]) - if err != nil { - return nil, fmt.Errorf("the definition of %s cannot be encoded: %w", seat, err) - } - out = append(out, raw) - } - return out, nil -} - -// buildsWatch is what is done when the build modules may have changed (design 47 G A-82): the seat list names the build seats -// and the indexer's modules the holders of the seats, so a change of either may name a build module not watched yet. It is set once -// the parts that watch the build modules are made, and called from then on. -type buildsWatch struct{ fn atomic.Pointer[func()] } - -// Set takes the function in. -func (b *buildsWatch) Set(f func()) { b.fn.Store(&f) } - -// Changed calls it, none before it is set. -func (b *buildsWatch) Changed() { - if f := b.fn.Load(); f != nil { - (*f)() - } -} - -// definitionsOf are the seat definitions the reader holds, none before it has answered. -func definitionsOf(reader *seatlist.Reader) map[string]seatdef.Definition { - if reader == nil { - return nil - } - return reader.Definitions() -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/kept_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/kept_test.go deleted file mode 100644 index c36810ad..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/kept_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "encoding/json" - "os" - "sort" - "testing" -) - -// The checker says on the bus which states it keeps and which others may read (novox/hq ADR 0495 clauses 2 and 3): a -// reader is granted from that announcement. owed is read by the forge module, which writes mesh/check from it (ADR 0521, -// R4e round 2 finding R2-1), and by the forge bridge; previews by the indexer; verdicts by the gate. The list names every -// state of the manifest, so no state is refused at the call for want of being announced (clause 5). -func TestTheCheckerAnnouncesEveryStateItKeepsAndOwedReadable(t *testing.T) { // R2-1, ADR 0495 §2-§3, ADR 0521 - raw, err := os.ReadFile("../../module.json") - if err != nil { - t.Fatal(err) - } - var m struct { - State []struct{ Name string } `json:"state"` - } - if err := json.Unmarshal(raw, &m); err != nil { - t.Fatal(err) - } - var manifest, announced, readable []string - for _, s := range m.State { - manifest = append(manifest, s.Name) - } - for _, s := range keptStates { - announced = append(announced, s.Name) - if s.Readable { - readable = append(readable, s.Name) - } - } - sort.Strings(manifest) - sort.Strings(announced) - if !equal(manifest, announced) { - t.Errorf("announced %v, the manifest keeps %v", announced, manifest) - } - // kept is read by the Go build module for a check ask (issue 1887 design limit 1); tries by the delivery view (build_usage). - // inputs are the checker's own: the Go build module asks the seat's verb input for one (ADR 0596 §2, issue 1921). - if want := []string{"kept", "owed", "previews", "tries", "verdicts"}; !equal(readable, want) { - t.Errorf("readable %v, want %v", readable, want) - } -} - -func equal(a, b []string) bool { - if len(a) != len(b) { - return false - } - for i := range a { - if a[i] != b[i] { - return false - } - } - return true -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/main.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/main.go deleted file mode 100644 index 97c0f9ab..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/main.go +++ /dev/null @@ -1,538 +0,0 @@ -// Command mesh-delivery-checker is delivery's checker (novox/hq ADR 0453 clause 4, ADR 0447, ADR 0484, issues 1678 to 1686): for -// each candidate the indexer writes it judges every pull request's head merged onto its trunk, through the build modules' -// fetch, digest and test steps, keeps the passing packages' verdicts, writes the verdict, and owes the forge the status -// `mesh/check`, which the forge bridge sets. It runs dark: nothing reads its answers to merge or deliver (ADR 0453 clause 4). -// It serves no request subject and holds no timer: every reaction is a watch or a bus event (ADR 0437 §8). -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "io" - "log" - "os" - "os/signal" - "strings" - "sync/atomic" - "syscall" - "time" - - "github.com/nats-io/nats.go" - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - - "mesh-delivery-checker/internal/check" - - "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/announce" - "git.novox.be/novox/mesh-sdk/go/process" - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" - "git.novox.be/novox/mesh-sdk/go/views" -) - -// eventSubject is the subject of an event of the checker's stream checks (the bus names the stream CHECKS): the indexer's -// watch of the previews bucket and the forge bridge's watch of the owed bucket are woken by them. -func eventSubject(module, event string) string { - return process.ModuleStreamSubject(module, event) -} - -const ( - // defaultLink is the base of the address a status points at; the verdict key follows. - defaultLink = "mesh-delivery-view/checks" -) - -func main() { - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) - defer stop() - if err := run(ctx, os.Getenv, os.Stderr); err != nil { - fmt.Fprintln(os.Stderr, "mesh-delivery-checker:", err) - os.Exit(1) - } -} - -// durationEnv reads a duration setting from the environment, def when it is not set. -func durationEnv(getenv func(string) string, name string, def time.Duration) (time.Duration, error) { - v := strings.TrimSpace(getenv(name)) - if v == "" { - return def, nil - } - d, err := time.ParseDuration(v) - if err != nil || d <= 0 { - return 0, fmt.Errorf("%s: %q is not a duration of more than 0", name, v) - } - return d, nil -} - -// The checker's own states of its first views (views.StatusState) and of its findings, where the condition keeper raises a -// first view unanswered past its bound (ADR 0615 §6); and the indexer's states it follows (check.CandidatesBucket, -// ModulesBucket, ManifestsBucket). -func firstViewsBucket(module string) string { return check.Bucket(module, views.StatusState) } - -const ( - candidatesState = "candidates" - // seatDefinitionsState is the indexer's state of the seat definitions (design 26), keyed `.trunk` and - // `.pull..`. - seatDefinitionsState = "seat-definitions" - // The owners' views families, as their manifests' `streams` name them. - indexerViews = "indexer-views" - modulesState = "modules" - manifestsState = "manifests" -) - -// run is the program: it returns when ctx ends or the bus cannot be used. -// keptStates is every state the checker keeps and which others may read (ADR 0495 §2-§3): owed by the forge module, -// which writes mesh/check from it (ADR 0521), and by the forge bridge; previews by the indexer; verdicts by the gate; tries by the delivery view. -var keptStates = []announce.State{ - // The kept verdicts are read by the Go build module, which takes them for a check ask's unchanged packages (issue 1887's - // design limit 1); the checker alone writes them (ADR 0444 clause 5). - // Whether DEAD_LETTERS is heard (issue 734): read by the checker's own status tool alone. - {Name: "hearing"}, - // The asks each running check put (check.AsksState): read by the checker's own checker_ask_again alone. - {Name: "asks"}, - // The checking program's inputs by their digest (check.InputsStateName): the checker's own, read by its tools alone, - // which serve one on the seat's verb input to the Go build module that runs the program (check.InputVerb; ADR 0596 §2, - // issue 1921). - {Name: check.InputsStateName}, - // The composition requests it sent the composer, per check and node (check.RequestsStateName): read by its tools alone. - {Name: check.RequestsStateName}, - {Name: "kept", Readable: true}, - {Name: "owed", Readable: true}, - {Name: "previews", Readable: true}, - // The tries are read by the delivery view, which tells a try's asks from a candidate's by the asks each names (build_usage). - {Name: "tries", Readable: true}, - {Name: "verdicts", Readable: true}, - // Where its first views of the indexer's and the assigner's states stand (read by checker_first_views alone), and its - // findings of one unanswered past its bound, which the condition keeper observes as it does every reader's (ADR 0615 §6). - {Name: views.StatusState}, - {Name: "findings"}, - // The checks that could not run (check.CouldNotRunState), each asked again when its part runs: read by checker_status. - {Name: check.CouldNotRunState}, -} - -func run(ctx context.Context, getenv func(string) string, logOut io.Writer) error { - logger := log.New(logOut, "mesh-delivery-checker: ", log.LstdFlags) - cred, err := process.ReadBrokerAccount(getenv) - if err != nil { - return err - } - conn, js, err := cred.Dial() - if err != nil { - return err - } - defer conn.Close() - // Its own states: a check without one of them would be unwritten, so each stops the start, naming its grant (issue 1945). - buckets := map[string]jetstream.KeyValue{} - own := func(state string) string { return check.Bucket(cred.Module, state) } // the credential's module, never a constant (A-296) - for _, state := range []string{checker.StateVerdicts, checker.StateKept, checker.StateOwed, checker.StatePreviews, check.TriesState} { - if buckets[state], err = process.EssentialBucket(ctx, js, "checker", own(state), "judge or keep a check"); err != nil { - return err - } - } - // Calls to the build modules go out as this module's own account, naming the machine (mesh-sdk process.CallOn); the - // buckets are read and written on the connection above. - caller, err := process.Connect(cred, nats.Name(cred.Node+"."+cred.Module+".calls"), nats.RetryOnFailedConnect(true)) - if err != nil { - return err - } - defer caller.Close() - // What the checker keeps, said on the bus (ADR 0495 §2-§3): a reader is granted from it. Not fatal when it cannot be said - // now: process.Conn says it again at every reconnect. - if err := caller.Keeps(keptStates...); err != nil { - logger.Printf("what the checker keeps could not be said on the bus, so readers of owed, previews and verdicts are not granted until it is: %v", err) - } - // The candidates it checks, the modules and manifests that say which build modules judge a repository, the published - // packages, the predefined keys and the assignments are their owners' states (the indexer's, the assigner's), read by the - // first view of their read tools and then their views families, never their buckets (ADR 0615 §1, ADR 0596, DLV-M2). - // An owner that is down never holds the start: the checks start once every first view is answered, and until then the - // unanswered first view is the checker's finding. - // The module has one durable consumer of EVENTS: one hub pulls it for the views reader and for the registry seats' - // published, started once both are registered. - hub := process.NewEventHub(logger.Printf) - reader := &views.ProcessReader{Module: cred.Module, Call: caller.Call, Logf: logger.Printf, Hub: hub} - var buildsChanged buildsWatch - seatPart := seatlist.NewPart("checker", nil) - seats := seatList{reader: seatPart.Follow(seatlist.ProcessBus(caller.Call, hub), buildsChanged.Changed, logger.Printf)} - // The finding writer is made once the checker is (its observe reads the checker's kept checks); until then a first view - // unanswered is said in the log alone. - var findings atomic.Pointer[checkerFindings] - reader.Findings = func() views.WireWriter { - if f := findings.Load(); f != nil { - return f.w - } - return nil - } - // Each by the owner's read tool and views family as the owner's manifest names them. - var followed error - followOwner := func(owner, state, tool, stream string) *views.Picture { - p, err := reader.Follow(owner, state, owner+"."+tool) - followed = errors.Join(followed, err) - return p - } - candidates := followOwner(check.IndexerModule, candidatesState, "indexer_read_candidates", indexerViews) - modules := followOwner(check.IndexerModule, modulesState, "indexer_read_modules", indexerViews) - manifests := followOwner(check.IndexerModule, manifestsState, "indexer_read_manifests", indexerViews) - packages := followOwner(check.IndexerModule, meshcatalog.StatePackages, "indexer_read_packages", indexerViews) - predefined := followOwner(check.IndexerModule, check.IndexerPredefinedState, "indexer_read_predefined", indexerViews) - // The assignments is followed once the seat list names the holder of the seat mesh-module-assigner (below). - var assignments atomic.Pointer[views.Picture] - // The seventh: the indexer's seat definitions, so a pull request whose head's definition does not parse fails. - seatDefs := followOwner(check.IndexerModule, seatDefinitionsState, "indexer_read_seat_definitions", indexerViews) - if followed != nil { - return followed - } - // The build modules are no names (design 47 G A-82): the holders of the build seats, the seat list's definitions and the - // holders the indexer's modules name by their manifests' claims, found at each use. - verdicts := &manifestVerdicts{} - repos := &repoModules{manifests: kvStore{manifests}, logf: logger.Printf, verdicts: verdicts} - buildSeats := buildrun.NewBuildSeats(func() map[string]seatdef.Definition { return definitionsOf(seats.reader) }, repos.HolderOf) - repos.seats = buildSeats - - stepBound, err := durationEnv(getenv, "MESH_CHECKER_STEP_BOUND", check.DefaultStepBound) - if err != nil { - return err - } - checkBound, err := durationEnv(getenv, "MESH_CHECKER_BOUND", check.DefaultBound) - if err != nil { - return err - } - settleWindow, err := durationEnv(getenv, "MESH_CHECKER_SETTLE", check.DefaultSettle) - if err != nil { - return err - } - // The begin bound of a run ask of the checking program (check.DefaultBeginBound, issue 1921). - beginBound, err := durationEnv(getenv, "MESH_CHECKER_BEGIN_BOUND", check.DefaultBeginBound) - if err != nil { - return err - } - link := strings.TrimSpace(getenv("MESH_CHECKER_LINK")) - if link == "" { - link = defaultLink - } - tools := &buildrun.ToolchainSet{Seats: buildSeats} - presence := &buildrun.PresenceSet{} - trunks := &check.Trunks{} - // A check is one ask per pull request and build module on the build seats' check stream, pulled by a build node with a - // free slot (novox/hq ADR 0525 §1, §3): the checker chooses no node and starts no step. - asks := buildrun.JetStreamAsks{JS: js} - // The asks the build seats gave up on (issue 734): heard from DEAD_LETTERS, each ends the step its asker waits for. - // Heard in the background, and again at a reconnect or an ask while it is not; whether it is heard is kept in the - // state hearing, which checker_status says (dead_letters). - dead := &buildrun.DeadLetters{} - // The state may not be on the bus yet when a delivery started the checker (the grants part creates it, issue 1790): it - // is bound when the bus says it was created, and the latest hearing is written then. - hearingKV := process.AwaitBucket(ctx, js, conn, own(buildrun.HearingState), "checker_status cannot say whether DEAD_LETTERS is heard", logger.Printf) - hearing := buildrun.NewDeadLetterHearing(ctx, buildrun.JetStreamDeadLetters{JS: js}, []string{buildstep.AskGo, buildstep.AskNode}, dead, - logger.Printf, buildrun.SayHearing(ctx, hearingKV, logger.Printf)) - defer hearing.Stop() - // Each ask's step is heard by its own watch and, at every step end of its build module and every reconnect, read - // (buildrun.HearEnds): a watch that goes deaf loses no end (the lost check of 2026-10-28). - heard := buildrun.HearEnds(ctx, asks, asks, buildSeats.Modules(), logger.Printf) - conn.SetReconnectHandler(func(*nats.Conn) { hearing.Again(); heard.Nudge("") }) - waiter := buildrun.LostAskWaiter{Inner: heard, Dead: dead, Hearing: hearing} - asksKV := process.AwaitBucket(ctx, js, conn, own(check.AsksState), "checker_ask_again refuses every check, as it cannot tell whether its ask waits in a stream", logger.Printf) - inputsKV := process.AwaitBucket(ctx, js, conn, own(check.InputsStateName), "no checking program can be asked, so every check fails naming it", logger.Printf) - couldNotRunKV := process.AwaitBucket(ctx, js, conn, own(check.CouldNotRunState), "a check that could not run is not asked again when its part runs", logger.Printf) - requestsKV := process.AwaitBucket(ctx, js, conn, own(check.RequestsStateName), "no composition request is kept, so every composition made with the node-engine's head fails naming it", logger.Printf) - ch := &check.Checker{ - Asks: awaitedKV{asksKV}, - Verdicts: kvStore{buckets[checker.StateVerdicts]}, - Owed: kvStore{buckets[checker.StateOwed]}, - // A check that could not run waits for its part's running, and is the finding check-could-not-run after its bound. - CouldNotRun: awaitedKV{couldNotRunKV}, - Trunks: trunks, - Bound: checkBound, - Link: link, - Logf: logger.Printf, - Settle: settleWindow, - } - cf, err := newCheckerFindings(ctx, cred, conn, js, caller.State("findings"), ch.RaisedCouldNotRun, logger.Printf) - if err != nil { - return fmt.Errorf("the checker's finding writer: %w", err) - } - defer cf.w.Close() - ch.Findings = cf - findings.Store(cf) - kept := make(chan struct{}, 1) - ch.CouldNotRunKept = func() { - select { - case kept <- struct{}{}: - default: - } - } - go waitCouldNotRun(ctx, ch, kept) - // The builder's builds say how the candidates' fingerprints stand (issue 1684); where its bucket is not there yet the line - // says nothing of the builds, until the checker starts again. - var buildStates check.BuildStates - var ends check.BuildEnds - if kv := process.OptionalBucket(ctx, js, builds.BuildsBucket(builds.BuilderModule), "the line says nothing of the builds, and a check composes without waiting on them, until the checker starts again", logger.Printf); kv != nil { - buildStates, ends = buildReader{kvStore{kv}}, buildEnds{kv} - } - if ends != nil { - ch.Rebuilds = ends // a check failed on a build is asked again once that build is done again (ADR 0603 §10) - } - // The check's composition is the checker's, from what each placed node's engine answers (ADR 0593 §7, §9). - set, err := readSettings(getenv("MESH_CHECKER_SETTINGS")) - if err != nil { - return err - } - answerBound := set.answer - holdCallsFor(answerBound) - compositions := check.EngineCompositions{ - Requests: check.RequestKeeper{Store: requestsKV}, - Asker: engineAsks{call: caller.CallOn}, - Placed: placedNodes{kv: assignedNodes(&assignments)}.Placed, - Bound: answerBound, - Sandbox: seatSandbox{module: cred.Module, asker: buildrun.Asker{Seats: buildSeats, Publisher: asks, Waiter: waiter, Bound: stepBound, Dropper: buildrun.SeatDrops{Caller: caller}, Outputs: caller.CallFile}}, - Verdicts: verdicts.all, - // The node-engine's trunk head: a group with no node-engine member composes with the composer built there (ADR 0656 §5). - Composer: func() string { return trunks.Heads()[check.NodeEngineRepository] }, - } - // The tries (ADR 0501 §5) are judged by the same judge, held by the same deliveries. - tryer := &check.Tryer{Tries: kvStore{buckets[check.TriesState]}, Bound: checkBound, NotTaken: stepBound, Logf: logger.Printf, - // A whole try's start and end are said on the stream checks, for the indexer to write and delete the candidate of its group. - Say: func(ctx context.Context, event string, body checkermsg.Event) error { - data, _ := json.Marshal(body) - _, err := js.Publish(ctx, eventSubject(cred.Module, event), data, jetstream.WithRetryAttempts(10), jetstream.WithRetryWait(time.Second)) - return err - }} - announce := func(ctx context.Context, event, key string) { - body, _ := json.Marshal(map[string]string{"key": key}) - if _, err := js.Publish(ctx, eventSubject(cred.Module, event), body); err != nil { - logger.Printf("%s of %s was written and the part that reads it could not be told: %v", event, key, err) - } - } - compositions.Composer = func() string { return repos.CommitOf(check.NodeEngineModule) } - compositions.Manifests = repos - // The indexer's packages state (ADR 0593 §2), so a Go check builds every mesh package at its published version: read - // once the first views are answered, before any check starts (afterFirstViews). - published := &publishedPackages{} - compositions.Published = published - ch.Runs = repos // the node-engine a check reads beside at its running commit (ADR 0548 §6) - ch.Says = verdictSays{module: cred.Module, js: js, node: cred.Node, now: time.Now} // a pull request's verdict event, on the stream asks (design 26 *Out of step*) - ch.Judge = newJudge(judgeDeps{ - module: cred.Module, - asks: buildrun.Asker{Seats: buildSeats, Publisher: asks, Waiter: waiter, Bound: stepBound, Dropper: buildrun.SeatDrops{Caller: caller}, Outputs: caller.CallFile}, tools: tools, trunks: trunks, - kept: kvStore{buckets[checker.StateKept]}, previews: kvStore{buckets[checker.StatePreviews]}, candidates: tryCandidates{candidates}, - repos: repos, published: published, builds: buildStates, ends: ends, buildWait: set.buildWait, forge: forgeReads{call: caller.Call}, logf: logger.Printf, - announce: announce, compositions: compositions, inputs: runInputs{awaitedKV{inputsKV}}, beginBound: beginBound, - nodes: openNodeNames(ctx, js, logger.Printf), - predefined: keptPredefined{predefined}, - renames: seatRenames{call: caller.Call}, - seatDefs: seatDefinitionsOf(seatDefs), - seats: seats, - buildSeats: buildSeats, - }) - ch.Announce = announce - tryer.Judge = ch.Judge - - // The toolchains first, so a check that starts from the first candidate finds the nodes. - watch := newToolchainWatch(ctx, js, buildSeats, tools, ch.NodesChanged, logger) - // Only the toolchain reports of nodes the catalogue places the build module on count (issue 1940, ADR 0549 §3): the - // assignments is settled first, so the toolchains settle through the gate already knowing it. - gate := buildrun.NewToolchainGate(buildSeats.Modules(), tools.Put, logger.Printf) - watch.put = gate.Toolchain - // Until the assignments's first view is answered the gate passes every report on, as before the assignments is settled. - assignmentsErrs := make(chan error, 1) - // The assignments is the seat mesh-module-assigner's view, followed at the holder the seat-holders view names and as it - // moves (design 47 G §717): the holder function is the catalog half's claimant's seat-holders view (A-298), so no assigner - // module is named and nothing asks for the holder on a timer. - branches, err := meshcatalogclient.FollowBranchesProcess(reader, caller.Call) - if err != nil { - return err - } - branches.Logf = logger.Printf - pic, err := reader.FollowSeat(meshmoduleassigner.Seat, meshmoduleassigner.StateAssignments, branches.SeatHolders(ctx)) - if err != nil { - return err - } - assignments.Store(pic) - go func() { - select { - case <-ctx.Done(): - return - case <-pic.Follower().Ready(): - } - pe, err := buildrun.FollowAssignmentsIn(ctx, reader, pic.Bucket(), gate, ch.NodesChanged, logger.Printf) - if err != nil { - assignmentsErrs <- err - return - } - assignmentsErrs <- <-pe - }() - if err := watch.start(); err != nil { - return err - } - // A build seat says each time a node reported its toolchain (its event toolchain-reported, design 47 G §492): the bucket of - // the seat's holder is watched then, if it is not (Reported), and again when the seat list or the holders change (below). - for _, lang := range []string{buildstep.AskGo, buildstep.AskNode} { - seat, err := buildstep.BuildSeat(lang) - if err != nil { - return err - } - if err := hub.Handle("mesh.seat."+seat+".event.toolchain-reported.>", func(m jetstream.Msg) { watch.Reported(m) }); err != nil { - return err - } - } - // The build modules are named by the seat list and the indexer's modules: a change of either watches the steps and toolchains of - // a build module not watched yet and tells the gate which modules' reports it passes on. - buildsChanged.Set(func() { - heard.Watch(buildSeats.Modules()) - gate.UseModules(buildSeats.Modules()) - if err := watch.start(); err != nil { - logger.Printf("the toolchains of a build module cannot be watched: %v", err) - } - }) - // The node-engine seat's running.., kept last per subject (design 47): a part a check could not hear runs, - // and the check is asked again (issue 1373). Its stream and the checker's durable consumer on it are made from the - // manifest's consumes; where they are not there yet the checker says so, and a check that could not run waits for its - // bound and is said as a finding. - if cons, err := cred.EventConsumer(ctx, js, nodeengine.StreamRunning); err != nil { - logger.Printf("the consumer %s_%s on %s does not exist yet, so no check that could not run is asked again until the checker starts again: %v", cred.Node, cred.Module, nodeengine.StreamRunning, err) - } else { - cc, err := process.HearEvents(cons, func(m jetstream.Msg) { heardRunning(ctx, ch, m.Data(), logger.Printf) }, logger.Printf) - if err != nil { - return err - } - defer cc.Stop() - } - errs := make(chan error, 8) - // The watchman's presence: a node coming back online asks again a check whose forge was not heard (UnheardWords), and - // the assigner's own presence says whether a composition is being made (compositionWatch, ADR 0537 §4). A check is no - // longer held for a node: its asks wait in the build seats' streams, which redeliver a lost node's ask (ADR 0525 §6, - // issue 1912), so the node-engine's have is not watched here. - if kv := process.OptionalBucket(ctx, js, check.PresenceBucket, "no node counts as online until the checker starts again", logger.Printf); kv != nil { - if pw, err := settle(ctx, kv, func(e jetstream.KeyValueEntry) { presence.Put(e.Key(), e.Value(), !opOf(e)) }); err != nil { - process.NotWatched(logger.Printf, check.PresenceBucket, err, "no node counts as online until the checker starts again") - } else { - go func() { - errs <- follow(ctx, pw, func(e jetstream.KeyValueEntry) { - if presence.Put(e.Key(), e.Value(), !opOf(e)) { - ch.NodesChanged(ctx) // a node came online or went: a check whose forge was not heard may now be heard - } - }) - }() - } - } - // The catalog half says the trunk head of each repository, which a pull request is merged onto (design 47 G §197). Where it - // does not answer yet no trunk is known, so nothing is checked. - firstTrunks, err := followTrunks(hub, caller.Call, trunks, ch.TrunkMoved, logger.Printf) - if err != nil { - return fmt.Errorf("the checker cannot hear the catalog half's revisions: %w", err) - } - firstTrunks(ctx) - afterFirstViews := func() error { - // The modules say which repository holds a TypeScript module, so the Node build module judges it too. - pw, err := settle(ctx, packages, func(e jetstream.KeyValueEntry) { published.Put(e.Key(), e.Value(), !opOf(e)) }) - if err != nil { - return err - } - published.Answered() // read whole, an empty state too: its names are judged from here on - go func() { - errs <- follow(ctx, pw, func(e jetstream.KeyValueEntry) { published.Put(e.Key(), e.Value(), !opOf(e)) }) - }() - mw, err := settle(ctx, modules, func(e jetstream.KeyValueEntry) { repos.Put(ctx, e.Key(), e.Value(), !opOf(e)) }) - if err != nil { - return err - } - buildsChanged.Changed() // the holders of the build seats are known from here - go func() { - errs <- follow(ctx, mw, func(e jetstream.KeyValueEntry) { - before := repos.CommitOf(check.NodeEngineModule) - repos.Put(ctx, e.Key(), e.Value(), !opOf(e)) - buildsChanged.Changed() // a holder of a build seat may have moved - if after := repos.CommitOf(check.NodeEngineModule); after != before { - ch.TrunkMoved(ctx) // the node-engine the mesh runs moved: a verdict that read it beside is carried (ADR 0548 §6) - } - }) - }() - each := func(e jetstream.KeyValueEntry) { - if opOf(e) { - ch.Candidate(ctx, e.Key(), e.Value()) - } else { - ch.Forget(e.Key()) - } - } - w, err := settle(ctx, candidates, each) - if err != nil { - return err - } - // A key left running for heads no candidate holds is closed; the candidates are all known now. - if err := ch.Sweep(ctx); err != nil { - return err - } - // The tries: a queued one starts, one left running is taken over, an ended one past its keeping is removed. - tw, err := settle(ctx, buckets[check.TriesState], func(jetstream.KeyValueEntry) {}) - if err != nil { - return err - } - tryer.Reconsider(ctx) - go func() { - errs <- follow(ctx, tw, func(e jetstream.KeyValueEntry) { - if opOf(e) { - tryer.Put(ctx, e.Key(), e.Value()) - } - }) - }() - // The checker's own verdicts, updates only, in one watch: one checker_ask_again wrote asks its check again - // (Checker.VerdictWritten); a deleted key of heads a candidate holds is judged again (Checker.VerdictDeleted, issue 1733). - vw, err := buckets[checker.StateVerdicts].WatchAll(ctx, jetstream.UpdatesOnly()) - if err != nil { - return fmt.Errorf("the checker's verdicts cannot be watched, so no check can be asked again by hand and a deleted verdict would never be written again: %w", err) - } - go func() { - errs <- follow(ctx, vw, func(e jetstream.KeyValueEntry) { - if opOf(e) { - ch.VerdictWritten(ctx, e.Key(), e.Value()) - } else { - ch.VerdictDeleted(ctx, e.Key()) - } - }) - }() - go func() { errs <- follow(ctx, w, each) }() - return nil - } - reader.Start(ctx, js, cred, process.AwaitBucket(ctx, js, conn, firstViewsBucket(cred.Module), "checker_first_views reads nothing and checker_first_view_again is not heard", logger.Printf)) - // Every part has registered. Where the consumer is not there yet the hub binds it as soon as it is, and the seat list is read - // again then if it is still unread. - if err := hub.StartUntilBound(ctx, js, cred, nil, seatPart.EventsLive); err != nil { - logger.Printf("%v, so no package registry's published is heard until the consumer is there; the checker tries again", err) - } - defer hub.Stop() - // The checks, the tries and the hand asks, once every first view is answered (the lead's ruling for DLV-M2): a check - // judged without the candidates, the modules, the manifests, the published packages, the predefined keys or the - // assignments would be another check. - go func() { - select { - case <-ctx.Done(): - return - case <-reader.Ready(): - } - if err := afterFirstViews(); err != nil { - errs <- err - } - }() - logger.Printf("started on %s", cred.Node) - select { - case <-ctx.Done(): - ch.Wait() - tryer.Wait() - return nil - case err := <-errs: - return err - case err := <-assignmentsErrs: - return err - case err := <-watch.errs: - return err - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/nodes_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/nodes_test.go deleted file mode 100644 index aa903e5b..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/nodes_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "os" - "strings" - "testing" -) - -// 1912: with checks as asks on the build seats' streams, the stream redelivers a lost node's ask (ADR 0525 §6), so the -// checker program watches no node-engine have to start again a check that failed for want of a node. It reads the have's -// keys only for the nodes' names at each ask (judge.go, novox/hq ADR 0567 §12). -func TestTheCheckerWatchesNoHave(t *testing.T) { - raw, err := os.ReadFile("main.go") - if err != nil { - t.Fatal(err) - } - for _, s := range []string{"check.HaveBucket", "buildrun.HaveSet"} { - if strings.Contains(string(raw), s) { - t.Errorf("the checker program still reads %s", s) - } - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/packages.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/packages.go deleted file mode 100644 index a1925e82..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/packages.go +++ /dev/null @@ -1,108 +0,0 @@ -package main - -import ( - "strings" - "sync" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// publishedPackages is the indexer's state `packages` as the checker follows it (novox/hq ADR 0593 §2): by mesh Go -// module, the latest published commit of its repository the indexer read its packages at. The indexer alone computes a -// package's version; the checker reads it, so a Go check builds every mesh package at the version the mesh publishes -// (check.Examiner.Published) and computes none. -// -// Its series keys (meshcatalog.PackagesByPackageSeriesValue, decoded with meshcatalog.DecodePackagesByPackageSeriesValue) name each package by its name in its -// own ecosystem and the `/` it is built from (design 32): the known packages the judge takes -// (PackageNames). -type publishedPackages struct { - mu sync.Mutex - byKey map[string]meshcatalog.PackagesByModuleValue - series map[string]meshcatalog.PackagesByPackageSeriesValue - unread map[string]string - // answered is whether the state's first reading ended (Answered): until then the checker knows no package's name. - answered bool -} - -// Answered marks the state's first reading as ended, so PackageNames names what it holds, none included. -func (p *publishedPackages) Answered() { - p.mu.Lock() - defer p.mu.Unlock() - p.answered = true -} - -// Put takes one key of the state in: its value, or its deletion. A value that does not decode is kept out and remembered -// with why, never read as an older one. -func (p *publishedPackages) Put(key string, raw []byte, deleted bool) { - p.mu.Lock() - defer p.mu.Unlock() - if p.byKey == nil { - p.byKey, p.series, p.unread = map[string]meshcatalog.PackagesByModuleValue{}, map[string]meshcatalog.PackagesByPackageSeriesValue{}, map[string]string{} - } - delete(p.byKey, key) - delete(p.series, key) - delete(p.unread, key) - if deleted { - return - } - if isSeriesPackageKey(key) { - s, err := meshcatalog.DecodePackagesByPackageSeriesValue(raw) - if err != nil { - p.unread[key] = err.Error() - return - } - p.series[key] = s - return - } - v, err := meshcatalog.DecodePackagesByModuleValue(raw) - if err != nil { - p.unread[key] = err.Error() - return - } - p.byKey[key] = v -} - -// LatestPackages is the latest published version of every mesh Go module the indexer holds, by module path: the commit -// its repository was published at. A module the state does not hold is left out: a check builds it at the version -// go.mod names. -func (p *publishedPackages) LatestPackages() buildstep.PackageVersions { - p.mu.Lock() - defer p.mu.Unlock() - out := buildstep.PackageVersions{} - for _, v := range p.byKey { - if v.Module != "" && v.Commit != "" { - out[v.Module] = v.Commit - } - } - return out -} - -// PackageNames are the packages the state's series keys name, and the Go module path of each per-module record (issue -// 1966: the indexer's own knownPackages counts them the same way, with no `/` to say; never a package -// inside a module, design 32), by the name in its own ecosystem: the `/` -// each is built from (empty when the key does not say). Nil while the state is not read yet (Answered), which judges no -// name; once read never nil, an empty state included, so the judge judges every name against it. -func (p *publishedPackages) PackageNames() map[string]string { - p.mu.Lock() - defer p.mu.Unlock() - if !p.answered { - return nil - } - out := map[string]string{} - for _, s := range p.series { - if out[s.Name] == "" { - out[s.Name] = s.Package - } - } - for _, v := range p.byKey { - if _, ok := out[v.Module]; !ok && v.Module != "" { - out[v.Module] = "" - } - } - return out -} - -// isSeriesPackageKey says a key of the indexer's state packages is a package's series (`.`): a module's key -// is its path escaped, which holds no dot (keys.EscapeName). -func isSeriesPackageKey(key string) bool { return strings.Contains(key, ".") } diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/refused_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/refused_test.go deleted file mode 100644 index 9370f34f..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/refused_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package main - -import ( - "bytes" - "context" - "errors" - "log" - "strings" - "testing" - - "git.novox.be/novox/mesh-sdk/go/process" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" -) - -// 1945 (a part exits at start for a bucket it works without): a bus that refuses every toolchains bucket leaves the -// watch unstarted and the part running, said; a refused bucket it cannot work without is an error naming the bucket and -// its grant. The gate over the seat's assignments, and its refusal, are mesh-sdk's (buildrun.ToolchainGate, -// issue 774). -func TestARefusedBucketIsSaidAndThePartGoesOn(t *testing.T) { - set := &buildrun.ToolchainSet{Seats: testBuildSeats()} - var logs bytes.Buffer - logger := log.New(&logs, "", 0) - refused := refusingBus{err: errors.New("nats: permissions violation for publish to $JS.API.CONSUMER.CREATE.KV_mesh-build-node_toolchains")} - if err := newToolchainWatch(context.Background(), refusingBus{refuseOpen: true, err: refused.err}, testBuildSeats(), set, func(context.Context) {}, logger).start(); err != nil { - t.Errorf("a refused toolchains bucket stopped the start: %v", err) - } - if !strings.Contains(logs.String(), "cannot be read") { - t.Errorf("the refusals were not said: %q", logs.String()) - } - if _, err := process.EssentialBucket(context.Background(), refusingBus{refuseOpen: true, err: refused.err}, "checker", "mesh-delivery-indexer_modules", "work"); err == nil || - !strings.Contains(err.Error(), "grant of mesh-delivery-indexer_modules") { - t.Errorf("a refused bucket the part needs: %v, want an error naming its grant", err) - } -} - -// refusingBus is a bus that refuses: every open (refuseOpen), or every watch of a bucket it opens, with err. -type refusingBus struct { - refuseOpen bool - err error -} - -func (r refusingBus) KeyValue(context.Context, string) (jetstream.KeyValue, error) { - if r.refuseOpen { - return nil, r.err - } - return refusedWatch{err: r.err}, nil -} - -type refusedWatch struct { - jetstream.KeyValue - err error -} - -func (r refusedWatch) WatchAll(context.Context, ...jetstream.WatchOpt) (jetstream.KeyWatcher, error) { - return nil, r.err -} -func (r refusedWatch) Watch(context.Context, string, ...jetstream.WatchOpt) (jetstream.KeyWatcher, error) { - return nil, r.err -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/renames.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/renames.go deleted file mode 100644 index dca90323..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/renames.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "errors" - "fmt" - - "github.com/nats-io/nats.go" - - "git.novox.be/novox/mesh-contracts/go/seats/meshconfig" - - "mesh-delivery-checker/internal/check" -) - -// seatRenames asks the seat mesh-config's verb renames as the checker's own account (its `invokes` -// seat:mesh-config.renames; design 32). A call nobody answers is a holder serving no renames (the verb is optional), said -// as check.ErrRenamesNotServed with the bus's words; any other error is an ask that could not be made. -type seatRenames struct { - call func(key string, body any) (json.RawMessage, error) -} - -func (s seatRenames) Renames(_ context.Context, module string) (meshconfig.RenamesAnswer, error) { - raw, err := s.call(check.RenamesSeat, meshconfig.RenamesInput{Module: module}) - if errors.Is(err, nats.ErrNoResponders) { - return meshconfig.RenamesAnswer{}, fmt.Errorf("%w (nothing answers it: %v)", check.ErrRenamesNotServed, err) - } - if err != nil { - return meshconfig.RenamesAnswer{}, err - } - var a meshconfig.RenamesAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return meshconfig.RenamesAnswer{}, fmt.Errorf("%s answered what is not its answer: %w", check.RenamesSeat, err) - } - return a, nil -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seatlist_wiring_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seatlist_wiring_test.go deleted file mode 100644 index f8e31bca..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seatlist_wiring_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - "strings" - "testing" -) - -// The checker wires no reader of the seat list of its own (design 47 G §204, CK9): it follows the list through mesh-sdk's -// seatlist.Part, and its code holds no seatlist.Start nor a file of its own for the list. -func TestTheCheckerWiresNoSeatListReaderOfItsOwn(t *testing.T) { - if _, err := os.Stat("seatlist.go"); err == nil { - t.Error("seatlist.go is the checker's own wiring of the seat list: mesh-sdk's seatlist.Part is the one wiring") - } - files, _ := filepath.Glob("*.go") - for _, f := range files { - if strings.HasSuffix(f, "_test.go") { - continue - } - src, err := os.ReadFile(f) - if err != nil { - t.Fatal(err) - } - if strings.Contains(string(src), "seatlist.Start(") { - t.Errorf("%s starts a seat list reader of its own: use seatlist.Part.Follow", f) - } - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seats_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seats_test.go deleted file mode 100644 index 856e6f6e..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/seats_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-sdk/go/seatdef" -) - -// The holders of the build seats in the tests' seat list: names the checker's code does not know, found by the seats' acting -// rules (design 47 G A-82). -const ( - goBuildModule = "mesh-build-go" - nodeBuildModule = "mesh-build-node" -) - -// testBuildSeats is the finder over the build seats of the tests' seat list. -func testBuildSeats() *buildrun.BuildSeats { - rule := func(on string) seatdef.Definition { - return seatdef.Definition{Acts: []seatdef.ActingRule{{Step: seatdef.StepBuild, On: on}}} - } - defs := map[string]seatdef.Definition{"node-build-go": rule("go"), "node-build-node": rule("typescript")} - holders := map[string]string{"node-build-go": goBuildModule, "node-build-node": nodeBuildModule} - return buildrun.NewBuildSeats(func() map[string]seatdef.Definition { return defs }, func(seat string) (string, bool) { h, ok := holders[seat]; return h, ok }) -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings.go deleted file mode 100644 index c198376e..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "fmt" - "io/fs" - "os" - "time" - - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-sdk/go/process" - - "mesh-delivery-checker/internal/check" -) - -// The checker's own settings (its config, written by the node-engine into ${dir:state}/settings.json, named to the -// process by MESH_CHECKER_SETTINGS): compose-answer-bound, the seconds a check waits for a node's engine to answer -// compose-candidate (novox/hq ADR 0593 §7, §9: 60 by default), and build-wait-bound, the seconds a check waits for the -// builder's builds of its candidate before it composes (ADR 0603 §10: 1800 by default). The checker owns both. - -// settings are the checker's settings as read. -type settings struct { - // answer is compose-answer-bound; buildWait is build-wait-bound. - answer, buildWait time.Duration -} - -// readSettings reads the settings file at path: each value's default where no file or no value is there; a value that is -// no whole number of seconds within its range is refused, naming it. -func readSettings(path string) (settings, error) { - out := settings{answer: nec.AnswerBound, buildWait: check.DefaultBuildWait} - if path == "" { - return out, nil - } - raw, err := os.ReadFile(path) - if errors.Is(err, fs.ErrNotExist) { - return out, nil - } - if err != nil { - return settings{}, err - } - var s struct { - Answer *float64 `json:"compose-answer-bound"` - BuildWait *float64 `json:"build-wait-bound"` - } - if err := json.Unmarshal(raw, &s); err != nil { - return settings{}, fmt.Errorf("%s is no settings file: %w", path, err) - } - for _, v := range []struct { - name string - value *float64 - min, max float64 - into *time.Duration - }{{"compose-answer-bound", s.Answer, 1, 600, &out.answer}, {"build-wait-bound", s.BuildWait, 60, 7200, &out.buildWait}} { - if v.value == nil { - continue - } - if *v.value < v.min || *v.value > v.max || *v.value != float64(int(*v.value)) { - return settings{}, fmt.Errorf("%s is %v: a whole number of seconds from %v to %v", v.name, *v.value, v.min, v.max) - } - *v.into = time.Duration(*v.value) * time.Second - } - return out, nil -} - -// holdCallsFor makes a bus call wait the compose-answer-bound (novox/hq ADR 0593 §7), not the SDK's CallTimeout of 30 s: -// the request of Conn.CallOn gives up at CallTimeout, so a node-engine answering at 30.4 s was lost before the bound -// applied (issue 1126). It only ever lengthens the SDK's bound, and it is set once, before the first call. -func holdCallsFor(bound time.Duration) { - if bound > process.CallTimeout { - process.CallTimeout = bound - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings_test.go deleted file mode 100644 index 73e8b431..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/settings_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - "testing" - "time" - - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-sdk/go/process" - - "mesh-delivery-checker/internal/check" -) - -// ADR 0593 §7: the answer bound is the checker's setting compose-answer-bound, 60 s when the file or the value is -// absent; a value that is no whole number of seconds from 1 to 600 stops the start, naming it. ADR 0603 §10: the wait for -// the candidate's builds is build-wait-bound, 30 minutes when absent, from 60 to 7200 seconds. -func TestTheAnswerBoundIsTheCheckersSetting(t *testing.T) { - dir := t.TempDir() - for _, row := range []struct { - body string - want time.Duration - err bool - buildWait time.Duration - }{{"", nec.AnswerBound, false, check.DefaultBuildWait}, {`{}`, nec.AnswerBound, false, check.DefaultBuildWait}, - {`{"compose-answer-bound": 90}`, 90 * time.Second, false, check.DefaultBuildWait}, {`{"compose-answer-bound": 0}`, 0, true, 0}, - {`{"compose-answer-bound": 1.5}`, 0, true, 0}, - {`{"compose-answer-bound": 60, "build-wait-bound": 600}`, 60 * time.Second, false, 10 * time.Minute}, - {`{"build-wait-bound": 30}`, 0, true, 0}} { - path := filepath.Join(dir, "settings.json") - os.Remove(path) - if row.body != "" { - if err := os.WriteFile(path, []byte(row.body), 0o600); err != nil { - t.Fatal(err) - } - } - set, err := readSettings(path) - got := set.answer - if (err != nil) != row.err || got != row.want || set.buildWait != row.buildWait { - t.Errorf("%q: %s, %s, %v", row.body, got, set.buildWait, err) - } - // Issue 1126: the SDK's call gives up at CallTimeout (30 s), so the checker's calls must wait the bound read - // (an answer at 31 s is heard under 60 s), and the bound never shortens the SDK's own. - if err == nil { - was := process.CallTimeout - holdCallsFor(got) - if process.CallTimeout < got || process.CallTimeout < was { - t.Errorf("%q: calls wait %v under bound %v", row.body, process.CallTimeout, got) - } - process.CallTimeout = was - } - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/testdata/catalogue-835bf22b-xterm.module.json b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/testdata/catalogue-835bf22b-xterm.module.json deleted file mode 100644 index b65be8eb..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/testdata/catalogue-835bf22b-xterm.module.json +++ /dev/null @@ -1,236 +0,0 @@ -{ - "module": "xterm", - "version": "1", - "moved": true, - "capabilities": [ - "package-manager" - ], - "requires": [ - "x11-display" - ], - "claims": [ - { - "name": "node-terminal-emulator", - "scope": "node", - "serves": [ - "open" - ] - } - ], - "tools": [ - "xterm_font", - "xterm_colours" - ], - "config": { - "face": { - "type": "string", - "description": "The font family xterm draws with, as fc-list names it: JetBrainsMono Nerd Font, because research 026/04 chose it for its glyphs the prompt uses. A family the machine does not have falls back to the nearest installed face (xterm_font says which).", - "pattern": "[A-Za-z0-9 ._-]{1,64}", - "default": "JetBrainsMono Nerd Font" - }, - "size": { - "type": "number", - "description": "The size of xterm's text, in points: 12 reads well on a screen of about 100 DPI, and a denser screen needs a larger size.", - "minimum": 6, - "maximum": 72, - "predefined": "font-size", - "default": 12 - }, - "save-lines": { - "type": "number", - "description": "How many lines of scrollback xterm keeps per terminal: 10000 holds a long build log, and each line costs memory in every open terminal.", - "minimum": 0, - "maximum": 1000000, - "default": 10000 - }, - "term-name": { - "type": "string", - "description": "The TERM value xterm sets for the programs it runs: xterm-256color, because the palette has 256 colours and every program knows the name.", - "pattern": "[A-Za-z0-9._-]{1,64}", - "default": "xterm-256color" - }, - "render-font": { - "type": "boolean", - "description": "Whether xterm draws with fontconfig faces (true) or the old X core fonts (false): true, because the face is a fontconfig family and core fonts cannot show it.", - "default": true - }, - "bold-mode": { - "type": "boolean", - "description": "Whether xterm draws bold text by overstriking the normal face (true): false, because the face has a real bold style and overstriking blurs it.", - "default": false - }, - "always-bold-mode": { - "type": "boolean", - "description": "Whether xterm overstrikes bold even where the face has a bold style (true): false, for the same reason as bold-mode.", - "default": false - }, - "allow-bold-fonts": { - "type": "boolean", - "description": "Whether xterm may use the face's bold style for bold text (true): true, so bold text is drawn with the real bold weight.", - "default": true - }, - "background": { - "type": "string", - "description": "The terminal's background colour, as #rrggbb: black, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#000000" - }, - "foreground": { - "type": "string", - "description": "The terminal's text colour, as #rrggbb: a light grey, easier on the eyes than white on black.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#a8a8a8" - }, - "color0": { - "type": "string", - "description": "Palette entry 0, the normal black, as #rrggbb: #000000, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#000000" - }, - "color1": { - "type": "string", - "description": "Palette entry 1, the normal red, as #rrggbb: #9e1828, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#9e1828" - }, - "color2": { - "type": "string", - "description": "Palette entry 2, the normal green, as #rrggbb: #008800, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#008800" - }, - "color3": { - "type": "string", - "description": "Palette entry 3, the normal yellow, as #rrggbb: #d2bb4b, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#d2bb4b" - }, - "color4": { - "type": "string", - "description": "Palette entry 4, the normal blue, as #rrggbb: #414171, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#414171" - }, - "color5": { - "type": "string", - "description": "Palette entry 5, the normal magenta, as #rrggbb: #963c59, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#963c59" - }, - "color6": { - "type": "string", - "description": "Palette entry 6, the normal cyan, as #rrggbb: #418179, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#418179" - }, - "color7": { - "type": "string", - "description": "Palette entry 7, the normal white, as #rrggbb: #bebebe, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#bebebe" - }, - "color8": { - "type": "string", - "description": "Palette entry 8, the bright black, as #rrggbb: #666666, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#666666" - }, - "color9": { - "type": "string", - "description": "Palette entry 9, the bright red, as #rrggbb: #bc5766, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#bc5766" - }, - "color10": { - "type": "string", - "description": "Palette entry 10, the bright green, as #rrggbb: #61a171, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#61a171" - }, - "color11": { - "type": "string", - "description": "Palette entry 11, the bright yellow, as #rrggbb: #e7db52, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#e7db52" - }, - "color12": { - "type": "string", - "description": "Palette entry 12, the bright blue, as #rrggbb: #5085af, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#5085af" - }, - "color13": { - "type": "string", - "description": "Palette entry 13, the bright magenta, as #rrggbb: #a97a99, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#a97a99" - }, - "color14": { - "type": "string", - "description": "Palette entry 14, the bright cyan, as #rrggbb: #6ba4a4, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#6ba4a4" - }, - "color15": { - "type": "string", - "description": "Palette entry 15, the bright white, as #rrggbb: #ffffff, as both workstations ran it.", - "pattern": "#[0-9a-fA-F]{6}", - "default": "#ffffff" - }, - "copy-binding": { - "type": "string", - "description": "The key combination that copies the selection to the clipboard, in X translation syntax: Ctrl Shift ~Alt C, so a plain Ctrl+C still interrupts the program. It holds no colon, backslash or line break, which would end the resource.", - "pattern": "[^:\\\\\\n]+", - "default": "Ctrl Shift ~Alt C" - }, - "paste-binding": { - "type": "string", - "description": "The key combination that pastes the clipboard, in X translation syntax: Ctrl Shift ~Alt V, the pair of copy-binding. It holds no colon, backslash or line break, which would end the resource.", - "pattern": "[^:\\\\\\n]+", - "default": "Ctrl Shift ~Alt V" - } - }, - "items": [ - { - "collection": "environment", - "name": "terminal", - "attributes": { - "kind": "variable", - "name": "TERMINAL" - }, - "template": "files/env-terminal" - }, - { - "collection": "shell:xresources:normal", - "name": "xresources-normal", - "template": "files/xresources-normal" - } - ], - "resources": [ - { - "id": "package", - "type": "package", - "package": "xterm" - } - ], - "build": { - "artifacts": [ - { - "name": "tools", - "kind": "bundle", - "language": "go", - "system": "arch", - "from": "cmd/xterm-tools", - "binary": "xterm-tools", - "loads": [ - "xterm-tools" - ] - }, - { - "name": "files", - "kind": "archive", - "from": "files" - } - ] - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/toolchains.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/toolchains.go deleted file mode 100644 index 65f4643c..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/toolchains.go +++ /dev/null @@ -1,108 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "log" - "strings" - "sync" - - "git.novox.be/novox/mesh-sdk/go/process" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" -) - -// toolchainWatch keeps the build modules' toolchains in a buildrun.ToolchainSet and tells the builder when one changed. A -// build module's bucket is made when it is assigned, after the builder started, and the builder holds no timer (ADR 0437 -// §8): it watches the buckets that exist at start, and a build module says each time it wrote its toolchains key -// (toolchain.reported, handled by Reported); a bucket not watched yet is then watched. -type toolchainWatch struct { - ctx context.Context - js bucketOpener - seats *buildrun.BuildSeats // the build modules: the holders of the build seats (design 47 G A-82) - set *buildrun.ToolchainSet - // put takes a change of a toolchains bucket in: set.Put, or the gate of the assigned build nodes before it (issue 1940). - put func(module, key string, value []byte, deleted bool) bool - changed func(ctx context.Context) // a language's entries changed: the failed builds are tried again - logger *log.Logger - errs chan error - mu sync.Mutex - watched map[string]bool -} - -func newToolchainWatch(ctx context.Context, js bucketOpener, seats *buildrun.BuildSeats, set *buildrun.ToolchainSet, changed func(context.Context), logger *log.Logger) *toolchainWatch { - return &toolchainWatch{ctx: ctx, js: js, seats: seats, set: set, put: set.Put, changed: changed, logger: logger, errs: make(chan error, 16), watched: map[string]bool{}} -} - -// start watches the buckets that exist now; a missing one is said and waits for its build module's event. -func (w *toolchainWatch) start() error { - for _, module := range w.seats.Modules() { - if err := w.ensure(module); err != nil { - return err - } - } - return nil -} - -func (w *toolchainWatch) ensure(module string) error { - w.mu.Lock() - defer w.mu.Unlock() - if w.watched[module] { - return nil - } - name := buildrun.ToolchainsBucket(module) - kv, err := w.js.KeyValue(w.ctx, name) - switch { - case errors.Is(err, jetstream.ErrBucketNotFound): - w.logger.Printf("the bucket %s does not exist yet: it is watched when %s reports a toolchain", name, module) - return nil - case err != nil: - // Refused or not answered (its grant not on the bus yet, issue 1945): said, and opened again at the module's next report. - w.logger.Printf("the bucket %s cannot be read (%v): %s's toolchains are not heard until its next report", name, err, module) - return nil - } - watcher, err := settle(w.ctx, kv, func(e jetstream.KeyValueEntry) { w.put(module, e.Key(), e.Value(), !opOf(e)) }) - if err != nil { - process.NotWatched(w.logger.Printf, name, err, fmt.Sprintf("%s's toolchains are not heard until its next report", module)) - return nil - } - w.watched[module] = true - go func() { - w.errs <- follow(w.ctx, watcher, func(e jetstream.KeyValueEntry) { - if w.put(module, e.Key(), e.Value(), !opOf(e)) { - w.changed(w.ctx) - } - }) - }() - w.changed(w.ctx) // what the settle read is new to the builder - return nil -} - -// delivered is the part of a JetStream message the watch uses. -type delivered interface { - Subject() string - Ack() error - Term() error -} - -// Reported takes one toolchain-reported event of a build seat in: the seat is read off the subject, -// mesh.seat..event.toolchain-reported., and the bucket of its holder is watched if it is not. A subject of no build -// seat with a holder is terminated; a failure stays unacknowledged, and the server delivers it again. -func (w *toolchainWatch) Reported(m delivered) { - seat, _, _ := strings.Cut(strings.TrimPrefix(m.Subject(), "mesh.seat."), ".event.") - bm, known := w.seats.OfSeat(seat) - module := bm.Module - if !known || module == "" || bm.Toolchain == "" || !strings.HasPrefix(m.Subject(), "mesh.seat.") { - w.logger.Printf("%s: not the event of a build seat with a holder, dropped", m.Subject()) - _ = m.Term() - return - } - if err := w.ensure(module); err != nil { - w.logger.Printf("%s: left for redelivery: %v", m.Subject(), err) - return - } - _ = m.Ack() -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk.go deleted file mode 100644 index 4cbc5466..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - - "mesh-delivery-checker/internal/check" -) - -// The checker takes the trunk heads a check merges onto from the catalog half (design 47 G §197), never from the gate's state -// `source`: a first view by the seat mesh-catalog's verb `revision`, which carries the trunk head of every repository, then -// the seat's event `catalog-revision` of a trunk revision, each read again by the verb. - -// readTrunks asks the catalog half's newest trunk revision and sets the heads it carries. It says whether they changed. A -// candidate's revision, or an answer that does not read, changes nothing. -func readTrunks(ask func(string, any) (json.RawMessage, error), trunks *check.Trunks) (bool, error) { - raw, err := ask(check.TrunkVerb, meshcatalog.RevisionInput{}) - if err != nil { - return false, fmt.Errorf("the catalog half's revision cannot be read: %w", err) - } - heads, err := check.TrunkHeads(raw) - if err != nil { - return false, err - } - return trunks.Set(heads), nil -} - -// followTrunks reads the first view and then follows `catalog-revision`: a trunk revision's event reads the heads again, and a -// change of them is the trunk moving (statuses go pending, a verdict for the old trunk is judged again). A read that fails is -// said, and made again at the next event. -func followTrunks(hub interface { - Handle(string, func(jetstream.Msg)) error -}, ask func(string, any) (json.RawMessage, error), trunks *check.Trunks, moved func(context.Context), logf func(string, ...any)) (first func(context.Context), err error) { - run := func(ctx context.Context) { - changed, err := readTrunks(ask, trunks) - if err != nil { - logf("no trunk is known until the catalog half answers, so nothing is checked: %v", err) - return - } - if changed { - moved(ctx) - } - } - err = hub.Handle(meshcatalog.SubjectCatalogRevisionAll, func(m jetstream.Msg) { - var e meshcatalog.CatalogRevisionEvent - if json.Unmarshal(m.Data(), &e) == nil && e.Kind == "trunk" { - run(context.Background()) - } - _ = m.Ack() - }) - return run, err -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk_test.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk_test.go deleted file mode 100644 index 611824a6..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/trunk_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - - "mesh-delivery-checker/internal/check" -) - -// design 47 G §197: the trunk heads come from the catalog half's trunk revision, by the seat verb revision, and never from the -// gate's source; a candidate's revision and an unanswered verb change nothing. -func TestTrunkHeadsComeFromTheCatalogHalf(t *testing.T) { - answer := func(kind string, heads ...meshcatalog.Head) func(string, any) (json.RawMessage, error) { - return func(key string, _ any) (json.RawMessage, error) { - if key != "seat:mesh-catalog.revision" { - t.Fatalf("asked %s", key) - } - return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Kind: kind, Trunk: heads}}) - } - } - trunks := &check.Trunks{} - h := meshcatalog.Head{Repository: "r", Commit: "c1"} - if changed, err := readTrunks(answer("trunk", h), trunks); err != nil || !changed || trunks.Heads()["r"] != "c1" { - t.Fatalf("first view: %v %v %v", changed, err, trunks.Heads()) - } - if changed, _ := readTrunks(answer("trunk", h), trunks); changed { - t.Fatal("the same heads moved the trunk") - } - if _, err := readTrunks(answer("candidate", meshcatalog.Head{Repository: "r", Commit: "c2"}), trunks); err == nil || trunks.Heads()["r"] != "c1" { - t.Fatalf("a candidate's revision: %v %v", err, trunks.Heads()) - } - if _, err := readTrunks(func(string, any) (json.RawMessage, error) { return nil, errors.New("down") }, trunks); err == nil || trunks.Heads()["r"] != "c1" { - t.Fatalf("an unanswered verb: %v %v", err, trunks.Heads()) - } -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/try_candidates.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/try_candidates.go deleted file mode 100644 index 02120bb6..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/try_candidates.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "context" - "fmt" - "time" - - "github.com/nats-io/nats.go/jetstream" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - - "mesh-delivery-checker/internal/check" -) - -// tryCandidates answers the indexer's candidate of a try's group (novox/hq ADR 0593 §7, issue 1921): the candidate the -// engines compose and the manifest validator judges, which the indexer makes of the try's previews. It waits for it on the -// key's own watch, with no timer between reads, and gives up at the bound. -type tryCandidates struct{ kv jetstream.KeyValue } - -func (t tryCandidates) TryCandidate(ctx context.Context, group, heads string, bound time.Duration) (meshcatalog.CandidatesByGroupValue, error) { - key, err := meshcatalogclient.CandidatesKey(group) - if err != nil { - return meshcatalog.CandidatesByGroupValue{}, err - } - if bound <= 0 { - bound = time.Minute - } - waiting, cancel := context.WithTimeout(ctx, bound) - defer cancel() - w, err := t.kv.Watch(waiting, key) // the value now, then each change - if err != nil { - return meshcatalog.CandidatesByGroupValue{}, fmt.Errorf("the indexer's candidate %s cannot be watched: %w", key, err) - } - defer func() { _ = w.Stop() }() - last := "the indexer holds none" - for { - select { - case <-waiting.Done(): - return meshcatalog.CandidatesByGroupValue{}, fmt.Errorf("the indexer wrote no candidate of %s at heads %s within %s (%s)", group, heads, bound, last) - case e := <-w.Updates(): - if e == nil || e.Operation() != jetstream.KeyValuePut { - continue - } - c, err := meshcatalog.DecodeCandidatesByGroupValue(e.Value()) - switch { - case err != nil: - last = "what it holds is not a candidate: " + err.Error() - case c.Heads != heads: - last = "it holds heads " + c.Heads - case waitsOnPreview(c): - last = "it has not yet the merge previews' fingerprints" - default: - return c, nil - } - } - } -} - -// waitsOnPreview says a module of the candidate has no fingerprint yet because its preview was not read. -func waitsOnPreview(c meshcatalog.CandidatesByGroupValue) bool { - for _, p := range c.Pulls { - for _, m := range p.Fps { - if check.WaitsOnPreview(m) { - return true - } - } - } - return false -} diff --git a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/verdictsay.go b/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/verdictsay.go deleted file mode 100644 index 7a1319bd..00000000 --- a/modules/mesh-delivery-checker/cmd/mesh-delivery-checker/verdictsay.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "context" - "crypto/sha256" - "encoding/json" - "fmt" - "strings" - "time" - - "github.com/nats-io/nats.go" - "github.com/nats-io/nats.go/jetstream" - - "mesh-delivery-checker/internal/check" -) - -// verdictSays is check.VerdictSayer on the bus: verdict published on the checker's own stream asks. -type verdictSays struct { - // module is the checker's own, the credential's: the stream of its events and their source. - module string - js jetstream.JetStream - node string - now func() time.Time -} - -// Say publishes one verdict on the checker's own stream asks with the checker's event headers; the message id makes a -// second publish of the same pull request's head, verdict and out-of-step files within the duplicate window one event. -func (t verdictSays) Say(ctx context.Context, v check.VerdictEvent) error { - body, err := json.Marshal(v) - if err != nil { - return err - } - msg := nats.NewMsg(eventSubject(t.module, check.EventVerdict)) - id := fmt.Sprintf("verdict.%s.%d.%s.%s.%x", v.Repository, v.Pull, v.Head, v.Verdict, sha256.Sum256([]byte(strings.Join(v.OutOfStep, "\n")))) - msg.Header.Set("x-event-id", id) - msg.Header.Set("x-source", t.module) - msg.Header.Set("x-node", t.node) - msg.Header.Set("x-time", t.now().UTC().Format(time.RFC3339Nano)) - msg.Header.Set("content-type", "application/json") - msg.Data = body - _, err = t.js.PublishMsg(ctx, msg, jetstream.WithMsgID(id)) - return err -} diff --git a/modules/mesh-delivery-checker/files/settings.json b/modules/mesh-delivery-checker/files/settings.json deleted file mode 100644 index c6d811e8..00000000 --- a/modules/mesh-delivery-checker/files/settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "compose-answer-bound": 60, - "build-wait-bound": 1800 -} diff --git a/modules/mesh-delivery-checker/go.mod b/modules/mesh-delivery-checker/go.mod deleted file mode 100644 index c86ebab1..00000000 --- a/modules/mesh-delivery-checker/go.mod +++ /dev/null @@ -1,67 +0,0 @@ -module mesh-delivery-checker - -go 1.26.0 - -require ( - git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 - github.com/nats-io/nats-server/v2 v2.11.17 - github.com/nats-io/nats.go v1.54.0 -) - -require ( - git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/go/seats/meshconfig v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/go/seats/nodeengine v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun v0.0.0-00010101000000-000000000000 - github.com/antithesishq/antithesis-sdk-go v0.8.0-default-no-op // indirect - github.com/google/go-tpm v0.9.8 // indirect - github.com/klauspost/compress v1.20.1 // indirect - github.com/minio/highwayhash v1.0.4 // indirect - github.com/nats-io/jwt/v2 v2.8.2 // indirect - github.com/nats-io/nkeys v0.4.16 // indirect - github.com/nats-io/nuid v1.0.1 // indirect - golang.org/x/crypto v0.57.0 // indirect - golang.org/x/sys v0.48.0 // indirect - golang.org/x/time v0.16.0 // indirect -) - -require ( - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/npmpackageregistry v0.0.0-00010101000000-000000000000 // indirect -) - -replace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun => ../mesh-delivery-builder/buildrun - -require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 - -require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg v0.0.0-00010101000000-000000000000 - -replace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg => ./checkermsg - -replace git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk => ../mesh-delivery-sdk - -require ( - git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 - git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/gopackageregistryclient v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/npmpackageregistryclient v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/ociartifactregistryclient v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000 // indirect - git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect -) diff --git a/modules/mesh-delivery-checker/go.sum b/modules/mesh-delivery-checker/go.sum deleted file mode 100644 index 8ca93dfd..00000000 --- a/modules/mesh-delivery-checker/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/antithesishq/antithesis-sdk-go v0.8.0-default-no-op h1:1BOWQJweNyvZMlpAHXGLiZQn9S+QXGcz3xh94lC0w6E= -github.com/antithesishq/antithesis-sdk-go v0.8.0-default-no-op/go.mod h1:FQyySiasQQM8735Ddel3MRojmy4dA1IqCeyJ5jmPMbI= -github.com/google/go-tpm v0.9.8 h1:slArAR9Ft+1ybZu0lBwpSmpwhRXaa85hWtMinMyRAWo= -github.com/google/go-tpm v0.9.8/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY= -github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ= -github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= -github.com/minio/highwayhash v1.0.4 h1:asJizugGgchQod2ja9NJlGOWq4s7KsAWr5XUc9Clgl4= -github.com/minio/highwayhash v1.0.4/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ= -github.com/nats-io/jwt/v2 v2.8.2 h1:XXRgB60MSTnqsRwejQurVDs/hcv2dkt+86GjI+I/bMc= -github.com/nats-io/jwt/v2 v2.8.2/go.mod h1:Ag/56sq9OblL4JgdYufDd16Egb17Kr/8WwwuO/forVc= -github.com/nats-io/nats-server/v2 v2.11.17 h1:GKEghcFK6A+aFx11Yf1LjgLC3txAwvyhnYzhBIQZA8I= -github.com/nats-io/nats-server/v2 v2.11.17/go.mod h1:B1sFVz4StNosQ903ak4N1G01Fl/9f8e06mXpFIE2K24= -github.com/nats-io/nats.go v1.54.0 h1:vsXoOxjHp/GmPUN+EcI7uOf/uB+iAP+kEsAFNQN0yzA= -github.com/nats-io/nats.go v1.54.0/go.mod h1:y+DZoD1oBOYfZTU681eTUiUjI0vbqYGixNVFHcjHJ0k= -github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg= -github.com/nats-io/nkeys v0.4.16/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs= -github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= -github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= -golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= -golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= -golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE= -golang.org/x/time v0.16.0/go.mod h1:rVKOqvZeKvrDKTQiAHJ7wmwP0RzleSphoEA9RcdLA0s= diff --git a/modules/mesh-delivery-checker/internal/check/adr548_test.go b/modules/mesh-delivery-checker/internal/check/adr548_test.go deleted file mode 100644 index 10f18b30..00000000 --- a/modules/mesh-delivery-checker/internal/check/adr548_test.go +++ /dev/null @@ -1,279 +0,0 @@ -package check - -import ( - "context" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// One check per delivery (novox/hq ADR 0548): the checker's part. A merge turn holds new checks off its repository (§3); -// a trunk or sibling move never ends a running check and carries its verdict once it ended (§1, §5, §6); only a head git -// refused gets a whole check after a move (§7); the line of a carried verdict says so (§8). - -// waitFor polls a condition the checker's goroutines make true, failing after ten seconds: a bound a loaded machine does -// not reach, waited out only by a failing test. -func waitFor(t testing.TB, what string, ok func() bool) { - t.Helper() - for deadline := time.Now().Add(10 * time.Second); !ok(); time.Sleep(time.Millisecond) { - if time.Now().After(deadline) { - t.Fatalf("%s did not happen", what) - } - } -} - -// pullWithFP is a pull request touching the module m at the fingerprint `src2:` and 64 times the hex digit d. -func pullWithFP(repo string, number int, head string, d string) meshcatalog.CandidatesByGroupValuePullsItem { - p := pullOf(repo, number, head) - p.Modules = []string{"m"} - p.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"m": {Fp2: "src2:" + strings.Repeat(d, 64)}} - return p -} - -func TestOneCheckPerDelivery(t *testing.T) { - ctx := context.Background() - for name, run := range map[string]func(t *testing.T){ - // Design 47 G §278 (test 52): a new check is claimed at once; the checker reads no state of the gate, so no merge turn holds it. - "a new check is claimed at once": func(t *testing.T) { - j := passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if j.count() != 1 || r.verdict(t, keyOf(t, "feat/x", cand)).State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("judged %d times, want once at once", j.count()) - } - }, - // §1, §5: a trunk move during a running check calls the judge once, writes its verdict, keeps the status pending (a pass - // on an old trunk is never shown), then asks one carry, on the recheck stream, and no check. - "a trunk move during a check ends nothing and asks one carry after it": func(t *testing.T) { - var r *rig - var kinds []string - var from []map[string]string - judged := make(chan struct{}) - j := &judgeFunc{f: func(ctx context.Context, c *Case, call int) (Outcome, error) { - kinds, from = append(kinds, c.Kind), append(from, c.CarriedFrom) - if call == 1 { - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) // the trunk moves while it runs - r.ch.TrunkMoved(ctx) // as the watch of the source says it - close(judged) - } else if s := owedState(t, r, repoA, 7, head1); s != "pending" { - t.Errorf("the status during the carry is %q, want pending: the pass on the old trunk is never shown", s) - } - return Outcome{Verdict: passing()}, nil - }} - r = newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - <-judged - r.ch.Wait() - v := r.verdict(t, keyOf(t, "feat/x", cand)) - if j.count() != 2 || kinds[0] != KindNew || kinds[1] != KindTrunk || from[0] != nil || from[1][repoA] != trunkA { - t.Fatalf("judged %d times as %v carried from %v; want a whole check, then one carry from %s", j.count(), kinds, from, trunkA[:12]) - } - if v.State != meshdeliverycheckerclient.VerdictPass || v.Trunk[repoA] != trunkA2 || v.CarriedFrom[repoA] != trunkA { - t.Errorf("the verdict is %s on %v carried from %v; want a pass on the new trunk carried from the old", v.State, v.Trunk, v.CarriedFrom) - } - if s := owedState(t, r, repoA, 7, head1); s != "success" { - t.Errorf("the status after the carry is %q, want success", s) - } - if r.verdicts.creates[keyOf(t, "feat/x", cand)] != 1 { - t.Error("the verdict key was made again: a carry rewrites it") - } - }, - // §5: the composition is kept when no fingerprint of a module it composed changed, and runs again otherwise. - "a carry keeps a passed composition while the fingerprints stay the same": func(t *testing.T) { - var kept []bool - j := &judgeFunc{f: func(_ context.Context, c *Case, _ int) (Outcome, error) { - kept = append(kept, c.Composition != nil) - v := passing() - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionPass, Sdk: "v0.1.14-x", Fpset: c.FPSet} - return Outcome{Verdict: v}, nil - }} - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullWithFP(repoA, 7, head1, "a")) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) // the indexer's fingerprints of the candidate stay the same - r.ch.Wait() - r.ch.Candidate(ctx, "feat%2Fx", candidateOf(t, "feat/x", pullWithFP(repoA, 7, head1, "b"))) // a fingerprint changed with the move - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - if len(kept) != 3 || kept[0] || !kept[1] || kept[2] { - t.Errorf("the composition was kept in the checks %v; want composed, kept on the carry, composed again once a fingerprint changed", kept) - } - }, - // §6: a sibling's move puts the verdict out of date, its status pending saying it is carried, and the carry's verdict - // names the sibling's old commit; the status follows the carry's verdict. Which packages run is the digest's (only - // those that name MESH_CHECK_BESIDE change with a sibling), the build module's part. - "a sibling's move is carried and the status follows the carry": func(t *testing.T) { - var r *rig - var from map[string]string - j := &judgeFunc{f: func(_ context.Context, c *Case, call int) (Outcome, error) { - if call == 2 { - from = c.CarriedFrom - if s := owedState(t, r, repoA, 7, head1); s != "pending" { - t.Errorf("the status during the sibling's carry is %q, want pending", s) - } - } - return Outcome{Verdict: passing(), Beside: map[string]string{catalogueRepository: r.trunks.Heads()[catalogueRepository]}}, nil - }} - r = newRig(t, j) - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS, catalogueRepository: trunkS}) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if b := r.verdict(t, keyOf(t, "feat/x", cand)).Beside; b[catalogueRepository] != trunkS { - t.Fatalf("the verdict names the siblings %v, want the catalogue at %s", b, trunkS[:12]) - } - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS, catalogueRepository: trunkS2}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - v := r.verdict(t, keyOf(t, "feat/x", cand)) - if j.count() != 2 || from[catalogueRepository] != trunkS || v.CarriedFrom[catalogueRepository] != trunkS || v.Beside[catalogueRepository] != trunkS2 { - t.Errorf("judged %d times, carried from %v, verdict beside %v carried from %v; want one carry over the catalogue's move", j.count(), from, v.Beside, v.CarriedFrom) - } - if s := owedState(t, r, repoA, 7, head1); s != "success" { - t.Errorf("the status after the carry is %q, want success", s) - } - }, - // §7: a carry whose merge conflicts fails naming the files and asks no check; a later move onto which the head merges - // cleanly asks a whole check, not a carry. - "a carry git refuses fails naming the files, and the next move asks a whole check": func(t *testing.T) { - var kinds []string - var from []map[string]string - j := &judgeFunc{f: func(_ context.Context, c *Case, call int) (Outcome, error) { - kinds, from = append(kinds, c.Kind), append(from, c.CarriedFrom) - if call == 2 { - return Outcome{Verdict: meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: "ace", - Words: repoA + "#7 " + ConflictWords + "go.mod"}}}, nil - } - return Outcome{Verdict: passing()}, nil - }} - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - r.ch.Reconsider(ctx) // anything else that looks again asks nothing of a conflict - r.ch.Wait() - if v := r.verdict(t, keyOf(t, "feat/x", cand)); j.count() != 2 || !IsConflictFailure(v) || !strings.Contains(v.Failed.Words, "go.mod") { - t.Fatalf("judged %d times, verdict %+v; want the carry failed naming go.mod and nothing asked after it", j.count(), v.Failed) - } - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - if j.count() != 3 || kinds[1] != KindTrunk || from[1] == nil || kinds[2] != KindNew || from[2] != nil { - t.Errorf("checks %v carried from %v; want a whole check, a carry, then a whole check after the conflict", kinds, from) - } - }, - // §8, §6: the line of a carried verdict names the head on the new trunk commit first, as the gate and the merge tool - // read a line, then where it was carried from and what the carry ran and kept; while it is carried it is pending and says so. - "the line of a carry says where it was carried from": func(t *testing.T) { - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := passVerdict() - v.Trunk = map[string]string{repoA: trunkA} - v.Beside = map[string]string{catalogueRepository: trunkS2} - v.CarriedFrom = map[string]string{repoA: trunkA2, catalogueRepository: trunkS} - line := Line(v, cs, p, nil, nil, nil) - if !strings.HasPrefix(line, "pass 111111111111 on aaaaaaaaaaaa | carried from abababababab, mesh-catalog 555555555555 | unit 12 run") { - t.Errorf("the carried line = %q", line) - } - if head, trunk := checkWordsOf(line); head != head1[:12] || trunk != trunkA[:12] { - t.Errorf("the line names %s on %s first, want the head on the new trunk", head, trunk) - } - cs.CarriedFrom = map[string]string{repoA: trunkA2} - pending := Line(meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictRunning}, cs, p, nil, nil, nil) - if !strings.HasPrefix(pending, "being carried to 111111111111 on aaaaaaaaaaaa | from abababababab") { - t.Errorf("the pending line of a carry = %q", pending) - } - }, - // §3 (review round 1): two approved merges in one turn whose trunk moved each get their carry; neither holds the other's. - "two merges waiting in one turn each get their carry": func(t *testing.T) { - var r *rig - j := &judgeFunc{f: func(context.Context, *Case, int) (Outcome, error) { return Outcome{Verdict: passing()}, nil }} - r = newRig(t, j) - candA, candC := candidateOf(t, "feat/a", pullOf(repoA, 7, head1)), candidateOf(t, "feat/c", pullOf(repoA, 8, head2)) - r.ch.Candidate(ctx, "feat%2Fa", candA) - r.ch.Candidate(ctx, "feat%2Fc", candC) - r.ch.Wait() - a, _ := meshcatalog.DecodeCandidatesByGroupValue(candA) - c, _ := meshcatalog.DecodeCandidatesByGroupValue(candC) - _, _ = a, c - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) // a third merge moves the trunk under both - r.ch.TrunkMoved(ctx) - r.ch.Wait() - for _, cand := range [][]byte{candA, candC} { - group := map[bool]string{true: "feat/a", false: "feat/c"}[string(cand) == string(candA)] - if v := r.verdict(t, keyOf(t, group, cand)); v.Trunk[repoA] != trunkA2 || v.CarriedFrom[repoA] != trunkA { - t.Errorf("%s: verdict on %v carried from %v; want its carry claimed and done", group, v.Trunk, v.CarriedFrom) - } - } - }, - // §6 (review round 1): the node-engine read at the commit the mesh runs is named, and a new running commit is carried. - "a new running node-engine is carried": func(t *testing.T) { - runs := runsAt{nodeEngineModule: trunkS} - j := &judgeFunc{f: func(_ context.Context, c *Case, _ int) (Outcome, error) { - return Outcome{Verdict: passing(), Beside: map[string]string{nodeEngineRepo: runs[nodeEngineModule]}}, nil - }} - r := newRig(t, j) - r.ch.Runs = runs - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - r.ch.TrunkMoved(ctx) // nothing moved: nothing is carried - r.ch.Wait() - runs[nodeEngineModule] = trunkS2 - r.ch.TrunkMoved(ctx) // what the indexer's watch does when mesh-host's running commit changes - r.ch.Wait() - v := r.verdict(t, keyOf(t, "feat/x", cand)) - if j.count() != 2 || v.CarriedFrom[nodeEngineRepo] != trunkS || v.Beside[nodeEngineRepo] != trunkS2 { - t.Errorf("judged %d times, verdict beside %v carried from %v; want one carry over the node-engine's new running commit", j.count(), v.Beside, v.CarriedFrom) - } - }, - // §5: a carry that fails names the move it failed on. - "a carry that fails names the move": func(t *testing.T) { - j := &judgeFunc{f: func(_ context.Context, c *Case, call int) (Outcome, error) { - if call == 1 { - return Outcome{Verdict: passing()}, nil - } - return Outcome{Verdict: meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Test: "unit: m/a.TestX", Words: "want 4"}}}, nil - }} - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - v := r.verdict(t, keyOf(t, "feat/x", cand)) - if v.State != meshdeliverycheckerclient.VerdictFail || v.CarriedFrom != nil || !strings.HasPrefix(v.Failed.Words, "want 4") || - !strings.Contains(v.Failed.Words, repoA+" "+trunkA[:12]+" to "+trunkA2[:12]) { - t.Errorf("the failed carry is %s carried from %v with words %q; want the test's words, then the move", v.State, v.CarriedFrom, v.Failed.Words) - } - }, - } { - t.Run(name, run) - } -} - -// checkWordsOf is the head and trunk a line names first, as the gate reads it (mesh-delivery-gate onestatus.go checkLine). -func checkWordsOf(line string) (string, string) { - for _, part := range strings.Split(line, " | ") { - f := strings.Fields(part) - for i := 0; i+2 < len(f); i++ { - if f[i+1] == "on" && len(f[i]) == 12 && len(f[i+2]) == 12 { - return f[i], f[i+2] - } - } - } - return "", "" -} diff --git a/modules/mesh-delivery-checker/internal/check/askagain.go b/modules/mesh-delivery-checker/internal/check/askagain.go deleted file mode 100644 index 9be3f0e6..00000000 --- a/modules/mesh-delivery-checker/internal/check/askagain.go +++ /dev/null @@ -1,277 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// **A check is asked again by hand** (the lost check of 2026-10-28; tools-are-how-a-module-is-spoken-to: every recovery -// has a tool). checker_ask_again ends a candidate's running verdict, whose wait judged nothing, with AskedAgainWords and -// the why; the checker hears it on its watch of its verdicts (Checker.VerdictWritten), ends the check of those heads -// running in it, and claims and asks it anew, a whole check (IsEnded). It is refused while a step of the candidate's pull -// requests can still run (checker_status's running_steps): that step's end judges it; and while an ask the check put is -// still in its build seat's stream, taken by no node (it has no step key yet): asking again then would run the check twice -// (issue 1791's kind). The checker keeps the asks each running check put in its state asks (AsksState, by verdict key). -// An ask is not put again under its own id instead: the stream de-duplicates an id within its window, so the copy waiting -// stays and the new one is dropped, and past the window both would run; nothing takes a put ask back (buildrun.Asker). - -// AsksState is the checker's state of the asks each running check put, by verdict key (CheckAsks). -const ( - AsksState = "asks" -) - -// CheckAsks are the asks a check put, in order, as the state asks keeps them while it runs; its end deletes the key. -type CheckAsks struct { - Asks []string `json:"asks"` -} - -// AsksKV is the state asks: a KV whose key a check's end deletes, so the state holds only the checks that run. -type AsksKV interface { - KV - Delete(ctx context.Context, key string) error -} - -// forgetAsks deletes the asks a check put at its end; one that cannot be deleted is said, and the next check of the same -// heads writes the key anew. -func (ch *Checker) forgetAsks(ctx context.Context, key string) { - if err := ch.Asks.Delete(ctx, key); err != nil { - ch.logf("%s: the asks it put could not be forgotten at its end: %v", key, err) - } -} - -// recordAsks keeps the asks a check put under its verdict key; nil Asks keeps nothing. A record that cannot be written -// is said: checker_ask_again then refuses the check, as it cannot tell whether its ask waits in a stream. -func (ch *Checker) recordAsks(ctx context.Context, key string, asks []string) { - if ch.Asks == nil { - return - } - data, err := json.Marshal(CheckAsks{Asks: asks}) - if err == nil { - _, err = putOnce(ctx, ch.Asks, key, data, func(cur []byte) bool { return string(cur) == string(data) }) - } - if err != nil { - ch.logf("%s: the asks it put cannot be kept (%v), so %s refuses it", key, err, AskAgainTool) - } -} - -// AskAgainTool asks a candidate's check again by hand. -const AskAgainTool = "checker_ask_again" - -// AskedAgainWords open the words of a verdict AskAgainTool wrote; the why follows. -const AskedAgainWords = EndedWords + "asked again by hand (" + AskAgainTool + "): " - -// errAskedAgain is the cause a running check is ended with when its verdict was asked again by hand. -var errAskedAgain = buildrun.Withdrawn{Why: "its verdict was asked again by hand (" + AskAgainTool + ")"} - -// IsAskedAgain says whether a verdict was written by AskAgainTool. -func IsAskedAgain(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return IsEnded(v) && strings.HasPrefix(failedOf(v).Words, AskedAgainWords) -} - -// VerdictEntry is the verdicts state as the ask-again tool writes it: a key's value and revision (nil and 0 for none), -// and a compare-and-set. -type VerdictEntry interface { - Entry(key string) (json.RawMessage, uint64, error) - Update(key string, value []byte, revision uint64) error -} - -// AskedAgain is the answer of AskAgainTool. -type AskedAgain struct { - Key string `json:"key"` - Was string `json:"was"` - Said string `json:"said"` - Note string `json:"note"` -} - -// AskAgain ends the running verdict of the group's candidate as asked again by hand, saying why. It refuses a group with -// no candidate, a verdict that is not running (a pass stands, and a failure unless every failing step ended on a transport fault, ADR 0664; both stand until a push or a trunk move), one a step of -// whose pull requests can still run, and one whose asks are not known or one of which no node took yet. -func (r Reader) AskAgain(w VerdictEntry, group, why string) (AskedAgain, error) { - why = strings.TrimSpace(why) - if group == "" || why == "" { - return AskedAgain{}, fmt.Errorf("give the group and why it is asked again") - } - if w == nil { - return AskedAgain{}, fmt.Errorf("the verdicts state cannot be written here") - } - c, ok := r.candidate(group) - if !ok { - return AskedAgain{}, fmt.Errorf("no candidate is held for the group %q", group) - } - key, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - if err != nil { - return AskedAgain{}, err - } - raw, rev, err := w.Entry(key) - if err != nil { - return AskedAgain{}, fmt.Errorf("%s cannot be read: %w", key, err) - } - if len(raw) == 0 { - return AskedAgain{}, fmt.Errorf("%s holds no verdict: the checker claims its check when it looks at the candidate (%s)", key, StatusReadTool) - } - v, err := meshdeliverycheckerclient.DecodeVerdict(raw) - if err != nil { - return AskedAgain{}, fmt.Errorf("%s holds what is not a verdict: %w", key, err) - } - failed := v.State == meshdeliverycheckerclient.VerdictFail - if v.State != meshdeliverycheckerclient.VerdictRunning && !failed { - return AskedAgain{}, fmt.Errorf("%s is %s, not running: a verdict stands until a push or a trunk move asks it again", key, v.State) - } - steps := r.readSteps() - if failed { - // ADR 0664 (operator answer 5, issue 1748): a failure stands, except one whose failing steps all ended on a transport - // fault (a step written passing): the network failed, not the change, so it is asked again like a lost check. - if err := steps.onlyTransportFaults(c); err != nil { - return AskedAgain{}, fmt.Errorf("%s is fail, and a failure stands until a push or a trunk move asks it again, unless every failing step of it ended on a transport fault: %w", key, err) - } - } - for _, rs := range steps.running { - if asksOf(c, rs.What) { - return AskedAgain{}, fmt.Errorf("a step of it runs: %s %s on %s since %s (%s); its end judges it", rs.Module, rs.ID, rs.Node, rs.Since, rs.What) - } - } - if failed { - return r.askedAgain(w, key, rev, v, why) - } - if r.AskState.Get == nil { - return AskedAgain{}, fmt.Errorf("the checker's asks are not readable here, so whether its ask waits in a build seat's stream cannot be told") - } - kept, err := r.AskState.Get(key) - var put CheckAsks - if err == nil && len(kept) > 0 { - err = json.Unmarshal(kept, &put) - } - if err == nil && len(put.Asks) == 0 && r.Backlog != nil { - // Issue 1373: the checker names no ask, so the build seats' check streams are read: with no check ask waiting or held - // on any of them (and no step of the candidate running, above) none of it can run, and it is asked anew. - if err := r.noCheckAskWaits(); err != nil { - return AskedAgain{}, err - } - return r.askedAgain(w, key, rev, v, why) - } - if err != nil || len(put.Asks) == 0 { - return AskedAgain{}, fmt.Errorf("the checker names no ask of %s (%v), so whether one waits in a build seat's stream cannot be told; asking again could run it twice", key, err) - } - taken := map[string]bool{} - for _, s := range steps.all { - taken[s.step.ID] = true - } - for _, id := range put.Asks { - if !taken[id] { - return AskedAgain{}, fmt.Errorf("its ask %s waits in its build seat's stream, taken by no node yet: asking again would run it twice; "+ - "the seats' backlog is in mesh-delivery-view.build_usage", id) - } - } - return r.askedAgain(w, key, rev, v, why) -} - -// onlyTransportFaults is nil when the candidate's pull requests have a failed or refused step and every one of them ended -// passing (buildstep.Step.Passing); else why not. -func (s stepsRead) onlyTransportFaults(c meshcatalog.CandidatesByGroupValue) error { - n := 0 - for _, ms := range s.all { - st := ms.step - if !asksOf(c, st.What) || (st.State != buildstep.StepFailed && st.State != buildstep.StepRefused) { - continue - } - if !st.Passing { - return fmt.Errorf("its step %s on %s ended %s and not on a transport fault: %s", st.ID, st.Node, st.State, st.Reason) - } - n++ - } - if n == 0 { - return errors.New("no failed step of it is read (the newest keys of each build module's steps are)") - } - return nil -} - -// askedAgain writes the verdict v ended as asked again by hand, why saying so. -func (r Reader) askedAgain(w VerdictEntry, key string, rev uint64, v meshdeliverychecker.VerdictsByGroupHeadsValue, why string) (AskedAgain, error) { - words := AskedAgainWords + why - ended := meshdeliverychecker.VerdictsByGroupHeadsValue{Group: v.Group, Heads: v.Heads, Trunk: v.Trunk, State: meshdeliverycheckerclient.VerdictFail, - Failed: &FailedFirst{Machine: "checker", Words: words}} - data, err := meshdeliverycheckerclient.EncodeVerdict(ended) - if err != nil { - return AskedAgain{}, err - } - if err := w.Update(key, data, rev); err != nil { - return AskedAgain{}, fmt.Errorf("%s changed while it was asked again, so it was not; read it again: %w", key, err) - } - return AskedAgain{Key: key, Was: v.State, Said: words, - Note: "the checker hears it on its watch of its verdicts, ends the wait that judged nothing and asks the check anew, whole; " + - VerdictsReadTool + " shows it running again"}, nil -} - -// VerdictWritten takes a change of the checker's verdicts in (its watch, updates only): a verdict AskAgainTool wrote ends -// the check of those heads running in this process, which then considers them again; with none running, the candidate -// that holds them is considered at once. Any other verdict is the checker's own, and left. -func (ch *Checker) VerdictWritten(ctx context.Context, key string, value []byte) { - v, err := meshdeliverycheckerclient.DecodeVerdict(value) - if err != nil || !IsAskedAgain(v) { - return - } - ch.init() - ch.mu.Lock() - if l, ok := ch.live[key]; ok { - l.end(errAskedAgain) - ch.mu.Unlock() - ch.logf("%s: %s; the check running for it is ended and asked again", key, failedOf(v).Words) - return - } - var group string - var cand meshcatalog.CandidatesByGroupValue - for k, c := range ch.known { - if vk, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads); err == nil && vk == key { - group, cand = k, c - break - } - } - ch.mu.Unlock() - if group == "" { - ch.logf("%s: %s, but no candidate holds these heads, so nothing is asked", key, failedOf(v).Words) - return - } - ch.logf("%s: %s; asked again", key, failedOf(v).Words) - ch.consider(ctx, group, cand) -} - -// BacklogRow is a row of the build seats' backlog as mesh-delivery-view's build_usage answers it (its backlog): the asks -// waiting and held on one language's stream of one kind of ask, or why it could not be read. -type BacklogRow struct { - Language string `json:"language"` - Kind string `json:"kind"` - Waiting uint64 `json:"waiting"` - Held uint64 `json:"held"` - Error string `json:"error,omitempty"` -} - -// noCheckAskWaits is nil when no check or recheck ask waits or is held on any build seat's stream and every row was read; else it -// names the stream that holds asks. It cannot tell whose ask one is, so any check ask refuses: asking again could run it twice. -func (r Reader) noCheckAskWaits() error { - rows, err := r.Backlog() - if err != nil { - return fmt.Errorf("the build seats' streams cannot be read (%v), so whether an ask of the check waits in one cannot be told; asking again could run it twice", err) - } - for _, row := range rows { - if row.Kind != "check" && row.Kind != "recheck" { // both kinds are the checker's asks (judge.go) - continue - } - if row.Error != "" { - return fmt.Errorf("the %s build seat's check stream cannot be read (%s), so whether an ask of the check waits in it cannot be told", row.Language, row.Error) - } - if row.Waiting > 0 || row.Held > 0 { - return fmt.Errorf("the %s build seat's check stream holds asks (%d waiting, %d held), one of them may be the check's: asking again could run it twice; "+ - "the seats' backlog is in mesh-delivery-view.build_usage", row.Language, row.Waiting, row.Held) - } - } - return nil -} diff --git a/modules/mesh-delivery-checker/internal/check/beside.go b/modules/mesh-delivery-checker/internal/check/beside.go deleted file mode 100644 index 097d588b..00000000 --- a/modules/mesh-delivery-checker/internal/check/beside.go +++ /dev/null @@ -1,218 +0,0 @@ -package check - -import ( - "context" - "errors" - "fmt" - "slices" - "strconv" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" -) - -// BesideRef is a sibling repository a repository's tests read, placed beside the fetched tree as a plain directory (ADR -// 0484 clause 4). Ref names the pull request's head when the commit is only there. -type BesideRef struct { - Repository string `json:"repository"` - Commit string `json:"commit"` - Ref string `json:"ref,omitempty"` - // atTrunk says the sibling was read at its trunk's head, so a move of that trunk puts the verdict out of date and is - // carried (novox/hq ADR 0548 §6). Never sent: the node-engine at the commit the mesh runs, mesh-sdk at a pin and a - // group's member at its head do not move with a trunk. - atTrunk bool - // runs says the sibling was read at the commit the mesh runs (the node-engine, the indexer's record of mesh-host): a new - // running commit puts the verdict out of date and is carried (ADR 0548 §6). Never sent. - runs bool -} - -// NodeEngineModule is the module whose running commit the node-engine sibling is read at. -const NodeEngineModule = nodeEngineModule - -const ( - catalogueRepository = "novox/mesh-catalog" - nodeEngineRepo = NodeEngineRepository - nodeEngineModule = "mesh-host" - // networkingRepository holds route-proxy, whose manifest the node-engine's proofs of a composition read. - networkingRepository = "novox/mesh-networking" - // contractsRepository and deliveryRepository hold the seat definitions mesh-sdk's bootstrapped seats are compared with: its - // tests read both beside it (beside.Dir "mesh-contracts" and "mesh-delivery"), at the group's head for a member, else at the - // trunk (a group try lays them out the same way: besideFor serves the check and the try). - contractsRepository = "novox/mesh-contracts" - deliveryRepository = DeliveryRepository -) - -// besideRules are the repositories each core repository's tests read, as the tests state it (mesh-sdk go/internal/beside -// beside.go): the catalogue, the node-engine, the SDK, and the delivery's pinned forge dependencies (mesh-catalog -// checks/deps). Which commit of each is besideCommit's rule. No repository's tests read the controller, which ended by its -// unassignment (novox/hq ADR 0590, issue 1126), and its repository is emptied and archived (ADR 0593 §11), so it has no -// siblings placed either. mesh-sdk reads mesh-contracts and mesh-delivery (its bootstrapped seats against their contracts files). The catalogue's tests read the node-engine since its composer command moved there (ADR 0593 -// §7): mesh-build-go builds it for a compose ask, and with it every repository its composer requires at the zero version -// (issue 1921: with mesh-sdk alone beside, that build looked them up on the network and failed): those are not named -// here but derived from each sibling's go.mod (zeroVersionSiblings), so no rule can lack one. The node-engine's read -// mesh-networking since route-proxy's proofs of a composition moved into them (ADR 0593). -var besideRules = map[string][]string{ - nodeEngineRepo: {catalogueRepository, SDKRepository, networkingRepository}, - SDKRepository: {catalogueRepository, nodeEngineRepo, contractsRepository, deliveryRepository}, - catalogueRepository: {SDKRepository, nodeEngineRepo}, - "novox/mesh-delivery": {SDKRepository, catalogueRepository}, -} - -// besideFor are the siblings a pull request's tests read (ADR 0484 clause 4): the node-engine at the head of the group's pull -// request for it when the group holds one, else at the commit the mesh runs (the commit of the indexer's module mesh-host, -// its trunk head when none is known); any other sibling, the catalogue -// included, at the head of the group's pull request for it when the group holds one, else at its trunk, since a group's -// check judges each member against the group's heads (ADR 0482 §2, ADR 0573 §4; issue 1126: a judge a group changed was -// judged against the catalogue's trunk). And first, the change's own repository at the trunk it is merged onto, so a test -// comparing a shrink-only list with the trunk's copy reads it at $MESH_CHECK_BESIDE/ (issue 1887 A12, ADR 0529 §2): -// that is where a catalogue change's own trunk copy is placed. -func (j *Examiner) besideFor(ctx context.Context, c *Case, p Pull) ([]BesideRef, error) { - var heads map[string]string - if j.Trunks != nil { - heads = j.Trunks.Heads() - } - var out []BesideRef - if p.Trunk != "" { - out = append(out, BesideRef{Repository: p.Repository, Commit: p.Trunk}) - } - place := func(repo string) { - switch { - case repo == nodeEngineRepo && c.member(repo) != nil: - // A group's check judges each member against the group's heads (ADR 0573 §4, ADR 0482 §2), and the group's - // node-engine is what runs after its merge: it is placed at the member's head, as any member is. Neither atTrunk - // nor runs, so no move of the trunk or of the running commit puts the verdict out of date (besideAtTrunk). - member := c.member(repo) - out = append(out, BesideRef{Repository: repo, Commit: member.Head, Ref: refOf(member.Number)}) - case repo == nodeEngineRepo: - commit := "" - if j.Runs != nil { - commit = j.Runs.CommitOf(nodeEngineModule) - } - atTrunk := false - if commit == "" { - commit, atTrunk = heads[repo], true - } - if commit != "" { - // Named in the verdict either way (ADR 0548 §6, issue 568's own example): at the commit the mesh runs, a new - // running commit puts the verdict out of date; at the trunk, a trunk move does (Checker.besideNow). - out = append(out, BesideRef{Repository: repo, Commit: commit, atTrunk: atTrunk, runs: !atTrunk}) - } - default: - if member := c.member(repo); member != nil { - out = append(out, BesideRef{Repository: repo, Commit: member.Head, Ref: refOf(member.Number)}) - } else if head := heads[repo]; head != "" { - out = append(out, BesideRef{Repository: repo, Commit: head, atTrunk: true}) - } - } - } - for _, repo := range besideRules[p.Repository] { - place(repo) - } - // A sibling a test builds needs every repository its go.mod requires at the zero version beside it too: the build - // resolves them from what lies beside, with nothing fetched (issue 1921). Its go.mod names them all, indirect ones - // included, so one level is enough. - derived, err := j.zeroVersionSiblings(ctx, p.Repository, out) - if err != nil { - return nil, err - } - for _, repo := range derived { - place(repo) - } - return out, nil -} - -// zeroVersionSiblings are the repositories the root go.mod of each sibling in refs requires at the zero version that are -// not beside already, sorted; none where the checker reads no forge. A sibling with no root go.mod requires none; one the -// forge does not answer for is an error, never a sibling left out in silence. -func (j *Examiner) zeroVersionSiblings(ctx context.Context, own string, refs []BesideRef) ([]string, error) { - if j.Forge == nil { - return nil, nil - } - have := map[string]bool{own: true} - for _, r := range refs { - have[r.Repository] = true - } - var out []string - for _, r := range refs { - if r.Repository == own { - continue - } - text, err := j.Forge.File(ctx, r.Repository, "go.mod", r.Commit) - if errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return nil, fmt.Errorf("the go.mod of %s at %.12s, beside the change, cannot be read%s: %w", r.Repository, r.Commit, unheardWords(err), err) - } - for _, path := range zeroVersionRequires(text) { - repo, _, ok := trunk.PackageRepository(path) - if !ok || have[repo] { - continue - } - have[repo] = true - out = append(out, repo) - } - } - slices.Sort(out) - return out, nil -} - -// zeroVersionRequires are the module paths a go.mod's require lines (a block or single ones) name at the zero version. -func zeroVersionRequires(gomod string) []string { - var out []string - block := false - for _, line := range strings.Split(gomod, "\n") { - line, _, _ = strings.Cut(line, "//") - f := strings.Fields(line) - switch { - case len(f) == 0: - continue - case f[0] == "require" && len(f) > 1 && f[1] == "(": - block = true - continue - case block && f[0] == ")": - block = false - continue - case f[0] == "require": - f = f[1:] - case !block: - continue - } - if len(f) >= 2 && f[1] == zeroVersion { - out = append(out, f[0]) - } - } - return out -} - -// zeroVersion is the version a go.mod requires a mesh package at (ADR 0573 §4): the builder writes the version it builds. -const zeroVersion = "v0.0.0-00010101000000-000000000000" - -func (c *Case) member(repository string) *Pull { - for i := range c.Pulls { - if c.Pulls[i].Repository == repository { - return &c.Pulls[i] - } - } - return nil -} - -func refOf(number int) string { - if number <= 0 { - return "" - } - return "refs/pull/" + strconv.Itoa(number) + "/head" -} - -// besideAtTrunk are the siblings of refs read at their trunk's head that the case does not merge, by repository: what a -// verdict names as Beside (ADR 0548 §6). -func besideAtTrunk(c *Case, refs []BesideRef, into map[string]string) { - for _, r := range refs { - if _, merged := c.Trunk[r.Repository]; (r.atTrunk || r.runs) && !merged { - into[r.Repository] = r.Commit - } - } -} - -// NodeEngineRepository is the node-engine's repository, whose trunk head a check without its member composes at. -const NodeEngineRepository = "novox/mesh-host" diff --git a/modules/mesh-delivery-checker/internal/check/binaries.go b/modules/mesh-delivery-checker/internal/check/binaries.go deleted file mode 100644 index 21abbb51..00000000 --- a/modules/mesh-delivery-checker/internal/check/binaries.go +++ /dev/null @@ -1,27 +0,0 @@ -package check - -import ( - "fmt" - - "mesh-delivery-checker/rules" -) - -// A change that adds or modifies a compiled program fails (issue 1577): it is build output, `go build` in a module's folder -// writes one beside go.mod, and `git add -A` takes it. The build module's fetch step, which holds the merged clone, reads the -// first bytes of each changed file and reports the programs (the build modules' fetch tools); the checker judges the answer. - -// checkBinariesOf records what a pull request's first fetch answer read of compiled programs, whichever build module -// gave it, as a fact the rules judge (rules.JudgePrograms). A fetch that made the merge and names no program has none: the -// answer's `programs` is an omitted empty list (B2g), and `merge` tells a preview was made. -func (st *state) checkBinariesOf(t *target) { - key := fmt.Sprintf("%s#%d", t.pull.Repository, t.pull.Number) - if t.fetched.Merge == "" { - return // no merge was made: nothing was changed to judge - } - files := t.fetched.Programs - if files == nil { - files = []ProgramFile{} // reported, and none: the rules refuse nothing (a nil list is a module that reports none) - } - st.facts = append(st.facts, rules.Step{Repository: t.pull.Repository, Number: t.pull.Number, Module: t.module, Node: t.node, - Programs: &rules.Programs{Key: key, Files: &files}}) -} diff --git a/modules/mesh-delivery-checker/internal/check/binaries_test.go b/modules/mesh-delivery-checker/internal/check/binaries_test.go deleted file mode 100644 index dd5b1675..00000000 --- a/modules/mesh-delivery-checker/internal/check/binaries_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package check - -import ( - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -func TestAChangeWhoseFetchReportsProgramsFailsNamingEachFile(t *testing.T) { // 1577 - prog := func(p ...string) []buildstep.Program { - var out []buildstep.Program // no program is the omitted empty list of the typed answer - for _, f := range p { - path, commit, _ := strings.Cut(f, "@") // `path@commit`: added by a commit of the branch and removed later - out = append(out, buildstep.Program{Path: path, Kind: "ELF", Commit: commit}) - } - return out - } - for _, c := range []struct { - name string - programs []buildstep.Program - fail []string // paths named; none passes - }{ - {"none found", prog(), nil}, - {"one program", prog("modules/m/m"), []string{"modules/m/m"}}, - {"two programs", prog("a/x", "b/y"), []string{"a/x", "b/y"}}, - {"added and removed in the branch", prog("gone/prog@0123456789abcdef0123"), []string{"gone/prog: a compiled program added in 0123456789ab and removed later: it stays in history; squash the branch (issue 1577)"}}, - } { - b := newBench(t) - b.fetch.Merge, b.fetch.Programs = "mmmm", c.programs - cs := caseOf(t, pullOf(repoA, 7, head1)) - v := b.judge(t, cs).Verdict - if len(cs.Notes()) > 0 { - t.Errorf("%s: notes = %v: a merge with no program is not a fetch that reported none", c.name, cs.Notes()) - } - if len(c.fail) == 0 { - if v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("%s: verdict = %+v", c.name, v) - } - continue - } - if v.State != meshdeliverycheckerclient.VerdictFail { - t.Errorf("%s: verdict = %+v, want a failure", c.name, v) - continue - } - for _, p := range c.fail { - want := p - if !strings.Contains(p, " added in ") { - want = p + ": a compiled program is build output: remove it and add it to .gitignore (issue 1577)" - } - if !strings.Contains(v.Failed.Words, want) { - t.Errorf("%s: words %q do not name %s", c.name, v.Failed.Words, p) - } - } - } -} diff --git a/modules/mesh-delivery-checker/internal/check/bounded.go b/modules/mesh-delivery-checker/internal/check/bounded.go deleted file mode 100644 index f444671d..00000000 --- a/modules/mesh-delivery-checker/internal/check/bounded.go +++ /dev/null @@ -1,79 +0,0 @@ -package check - -import ( - "fmt" - "sort" - "sync" - "sync/atomic" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// ReadWorkers is how many values a read tool asks the bus for at once. Each value is a round trip from the tool through the -// runtime to the bus, so reading a bucket of thousands of keys one after the other outlasts the 30 s a tool call is given -// (issue 1827, as the delivery view's issue 1945). It is a copy of the view's: a few lines are not worth a group delivery -// through mesh-sdk (rules-are-weighed). -const ReadWorkers = 16 - -// MaxReads bounds the values one call of a read tool reads; past it the answer says it was cut and how to narrow it. -const MaxReads = 1000 - -// parallel runs read(i) for i in [0, n), at most ReadWorkers at a time, and answers the first error; once one failed no -// further read starts. -func parallel(n int, read func(i int) error) error { - var ( - next atomic.Int64 - failed atomic.Bool - once sync.Once - first error - wg sync.WaitGroup - ) - for w := 0; w < min(ReadWorkers, n); w++ { - wg.Add(1) - go func() { - defer wg.Done() - for !failed.Load() { - i := int(next.Add(1) - 1) - if i >= n { - return - } - if err := read(i); err != nil { - once.Do(func() { first = err }) - failed.Store(true) - } - } - }() - } - wg.Wait() - return first -} - -// capKeys keeps the first MaxReads keys and answers a note when it cut the rest, "" otherwise. -func capKeys(keys []string, narrow string) ([]string, string) { - if len(keys) <= MaxReads { - return keys, "" - } - return keys[:MaxReads], fmt.Sprintf("read %d of %d matching keys and stopped; %s", MaxReads, len(keys), narrow) -} - -// newestFirst sorts step keys (.) by id, newest first: an id is the time in base 36, then six hex digits, so the -// longer id is the later and equal lengths compare as text. A key that is no step key goes last. -func newestFirst(keys []string) { - id := func(k string) (string, bool) { - _, _, id, err := buildstep.ParseStepKey(k) - return id, err == nil - } - sort.Slice(keys, func(i, j int) bool { - a, aok := id(keys[i]) - b, bok := id(keys[j]) - switch { - case aok != bok: - return aok - case len(a) != len(b): - return len(a) > len(b) - case a != b: - return a > b - } - return keys[i] > keys[j] - }) -} diff --git a/modules/mesh-delivery-checker/internal/check/build_change.go b/modules/mesh-delivery-checker/internal/check/build_change.go deleted file mode 100644 index 45985c49..00000000 --- a/modules/mesh-delivery-checker/internal/check/build_change.go +++ /dev/null @@ -1,144 +0,0 @@ -package check - -import ( - "context" - "fmt" - "maps" - "slices" - "strings" - - "git.novox.be/novox/mesh-sdk/go/catalogue" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" -) - -// What the check builds of a candidate before it composes (novox/hq ADR 0603 §10). The builder builds every fingerprint the -// indexer's candidate carries, as a key of its own (builds..), with the build modules that run; the -// check adds no build path of its own and waits on those keys (awaitBuilds), so the node-engines compose the candidate's -// modules with their builds. It does not wait on two kinds of changed module, and names them in its composition's warnings: -// -// - one built by a build module the change alters (the recipe of mesh-build-go or mesh-build-node enters the fingerprint -// of every module it builds, ADR 0530 §1): its candidate build is the running build module's, and the check cannot run -// the candidate's build module to build it (it runs it only for its own digest and tests, ADR 0535 §1), so it is not -// composed with a build of the changed build module; it is built again once the agreed toolchain moves (ADR 0603 §3). -// The build module's own build is waited on, as any other: the running build module builds its new source. -// - one that stands on a module the change alters (build.on): its candidate fingerprint names the base's trunk -// fingerprint, and no build module builds on another module's artifact (ADR 0487 §1), so its build stands on the -// trunk's base; the base's own build is waited on. -// -// A module the trunk holds no record of yet is not built by the builder until it is merged, so it is not waited on either. -// -// A changed build module whose manifest invokes a seat verb or module tool no running build serves is one the check cannot -// run (ADR 0614 §7): the modules it builds are composed from their manifests, as above, and the words name the verb. - -// buildsOfTheCheck are the builds the check waits on before it composes (module to fingerprint) and the words for the -// changed modules it composes without waiting on their build, one line each, sorted. A manifest that cannot be read leaves -// every build waited on and nothing said of build modules or bases (nothing is said that was not read). -func (st *state) buildsOfTheCheck(ctx context.Context) (map[string]string, []string) { - wait := map[string]string{} - changed := map[string]catalogue.Manifest{} - readable := true - for _, p := range st.c.Pulls { - for module, cm := range p.Fps { - if cm.Fp2 == "" || cm.Manifest == "" { - continue // the builder builds no fingerprint of it (candidates.go: none of the new form, or no manifest) - } - wait[module] = cm.Fp2 - if !readable || st.j.Read == nil { - readable = false - continue - } - m, err := st.j.Read.Manifest(ctx, cm.Manifest) - if err != nil { - st.j.logf("%s: the manifest of %s cannot be read, so the modules built with a changed build module or base are not named and every build is waited on: %v", st.c.Key, module, err) - readable = false - continue - } - changed[module] = m - } - } - var words []string - if st.j.Runs != nil { - var unheld []string - for _, module := range slices.Sorted(maps.Keys(wait)) { - if st.j.Runs.CommitOf(module) == "" { - unheld = append(unheld, module) - delete(wait, module) - } - } - if len(unheld) > 0 { - words = append(words, fmt.Sprintf("%s: the trunk holds no record of it yet, so the builder builds it once it is merged and the check composes it without a build (ADR 0603 §10)", - strings.Join(unheld, ", "))) - } - } - if !readable { - return wait, words - } - for _, bm := range st.j.Seats.Modules() { - if st.j.languageOf(bm) == "" { - continue - } - if _, alters := changed[bm]; !alters { - continue - } - var built []string - for module, m := range changed { - if isBuildModule := st.j.languageOf(module) != ""; !isBuildModule && buildsWith(m, st.j.languageOf(bm)) { - built = append(built, module) - delete(wait, module) - } - } - slices.Sort(built) - if len(built) > 0 { - words = append(words, fmt.Sprintf("the change alters the build module %s: %s, which it builds, are built by the running %s, not the changed one, which the check cannot run, so the check does not wait on their builds; they are built again once the agreed toolchain moves (ADR 0603 §3, §10)", - bm, strings.Join(built, ", "), bm)) - } - if unserved := st.unservedVerbs(ctx, changed[bm]); len(unserved) > 0 { - what := "nothing it builds is in the change" - if len(built) > 0 { - what = strings.Join(built, ", ") + ", which it builds, are composed from their manifests" - } - words = append(words, fmt.Sprintf("the changed build module %s invokes %s, which no running build serves, so the check cannot run it: %s (ADR 0614 §7)", - bm, strings.Join(unserved, ", "), what)) - } - } - g := graph.OfManifests(changed) - for _, base := range slices.Sorted(maps.Keys(changed)) { - if on := g.Dependents(base); len(on) > 0 { - for _, module := range on { - delete(wait, module) - } - words = append(words, fmt.Sprintf("the change alters %s, a base of %s: their builds stand on the trunk's %s, since no build module builds on a candidate's base, so the check waits on the build of %s alone (ADR 0603 §10)", - base, strings.Join(on, ", "), base, base)) - } - } - return wait, words -} - -// buildsWith says whether a manifest has a bundle in the language: the artifacts whose fingerprint names that language's -// toolchain and its build module's recipe (an archive is packed with no toolchain, ADR 0478 clause 1). -func buildsWith(m catalogue.Manifest, language string) bool { - return m.Build != nil && slices.ContainsFunc(m.Build.Artifacts, func(a catalogue.Artifact) bool { - return a.Kind == catalogue.ArtifactBundle && a.Language == language - }) -} - -// unservedVerbs are the seat verbs and module tools m invokes (graph.InvokesOf) that no running build serves, sorted; none -// when nothing reads what runs, or what runs cannot be read (said in the log: nothing is said that was not read). -func (st *state) unservedVerbs(ctx context.Context, m catalogue.Manifest) []string { - if st.j.Serving == nil { - return nil - } - served, err := st.j.Serving.Served(ctx) - if err != nil { - st.j.logf("%s: what the running builds serve cannot be read, so no build module is named for a verb it invokes: %v", st.c.Key, err) - return nil - } - var out []string - for _, v := range graph.InvokesOf(m) { - if !served(v) { - out = append(out, v) - } - } - return out -} diff --git a/modules/mesh-delivery-checker/internal/check/builds.go b/modules/mesh-delivery-checker/internal/check/builds.go deleted file mode 100644 index 2493c1da..00000000 --- a/modules/mesh-delivery-checker/internal/check/builds.go +++ /dev/null @@ -1,169 +0,0 @@ -package check - -import ( - "context" - "fmt" - "maps" - "slices" - "sort" - "strings" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// The states of a build the status line says (ADR 0484 clause 7). -const ( - BuildBuilt = "built" - BuildFailed = "failed" - BuildWaiting = "waiting" -) - -// BuildStates reads the builder's build of a module at a fingerprint of the new form (builds..): the -// value and whether the key exists. -type BuildStates interface { - Build(ctx context.Context, module, fp2 string) (builds.BuilderBuild, bool, error) -} - -// NoteOf is the state of a build as the line says it: done is built, failed is failed, and one queued, building or not -// begun is waiting. -func NoteOf(b builds.BuilderBuild, found bool) string { - switch { - case !found: - return BuildWaiting - case b.State == builds.BuildDone: - return BuildBuilt - case b.State == builds.BuildFailed: - return BuildFailed - } - return BuildWaiting -} - -// buildNotes are the state of each build the case's modules carry a fingerprint of, by module, sorted. A module two pull -// requests give is said once. -func (j *Examiner) buildNotes(ctx context.Context, c *Case) []BuildNote { - if j.Builds == nil { - return nil - } - fps := map[string]string{} - for _, p := range c.Pulls { - for module, cm := range p.Fps { - if cm.Fp2 != "" { - fps[module] = cm.Fp2 - } - } - } - modules := make([]string, 0, len(fps)) - for m := range fps { - modules = append(modules, m) - } - sort.Strings(modules) - var notes []BuildNote - for _, m := range modules { - b, found, err := j.Builds.Build(ctx, m, fps[m]) - if err != nil { - j.logf("the build of %s at %s cannot be read: %v", m, fps[m], err) - found = false - } - notes = append(notes, BuildNote{Module: m, State: NoteOf(b, found)}) - } - return notes -} - -// DefaultBuildWait is how long a check waits for the candidate's builds to end before it composes (the setting -// build-wait-bound, novox/hq ADR 0603 §10): past it the check composes without the builds not ended and names them. -const DefaultBuildWait = 30 * time.Minute - -// BuildEnds waits on the builder's build keys (builds..), hearing each key's changes on the bus as the -// builder writes them: the builder owns the fact, and nothing is polled. -type BuildEnds interface { - // Ended waits until the build of every module at its fingerprint (module to fingerprint) is done or failed, or ctx ends, - // and answers the builds that ended, by module. An error is only builds that cannot be heard at all. - Ended(ctx context.Context, builds map[string]string) (map[string]builds.BuilderBuild, error) - // Done waits until the build of every module at its fingerprint is done (a failed one built again, builder_build_again), - // and says whether they all are; false when ctx ends first or they cannot be heard. - Done(ctx context.Context, builds map[string]string) bool -} - -// BuildFailedTest is what a failure of the check names a failed build of the candidate by: the test `build: `. -// A verdict failed on builds is asked again by the checker itself once each is built again (Checker.Rebuilt). -const BuildFailedTest = "build: " - -// failedBuilds are the candidate's builds a failed verdict names as failed (BuildFailedTest), by module, at the fingerprint -// the case carries for it; none for a verdict that names no failed build. -func failedBuilds(v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case) map[string]string { - if v.State != meshdeliverycheckerclient.VerdictFail { - return nil - } - out := map[string]string{} - for _, f := range append([]FailedFirst{failedOf(v)}, failedList(v.Failures)...) { - module, named := strings.CutPrefix(f.Test, BuildFailedTest) - if !named { - continue - } - for _, p := range cs.Pulls { - if cm, ok := p.Fps[module]; ok && cm.Fp2 != "" { - out[module] = cm.Fp2 - } - } - } - if len(out) == 0 { - return nil - } - return out -} - -func (j *Examiner) buildWait() time.Duration { - if j.BuildWait > 0 { - return j.BuildWait - } - return DefaultBuildWait -} - -// awaitBuilds waits for the candidate's builds before the check composes (ADR 0603 §10): the check builds what the builder -// builds of the candidate and composes with it. A failed build fails the check, naming the module and the builder's words; -// a build not ended within the bound is named in late, and the check does not compose: an engine composes a module only at -// a done build, so its answer would be the builder's lateness read as the change's refusal (issue 1921). It answers the -// words of the composition's warnings (what the check composes without waiting on its build, and why) and the failures of -// the builds that failed, for the rules to take in (rules.JudgeComposition); failed says the check has failed already. -func (st *state) awaitBuilds(ctx context.Context, failed bool) (words []string, failedBuilds []FailedFirst, late string) { - wait, words := st.buildsOfTheCheck(ctx) - if len(wait) == 0 { - return words, nil, "" - } - modules := slices.Sorted(maps.Keys(wait)) - if failed { - // The check has failed already: its red status is never held back by a build (the lead, 2026-11-03). - return append(words, fmt.Sprintf("the check failed before its builds were waited on, so it composes at once, without waiting on the builds of %s (ADR 0603 §10)", strings.Join(modules, ", "))), nil, "" - } - if st.j.BuildEnds == nil { - return append(words, fmt.Sprintf("the builds of %s cannot be heard, so the check composes without waiting on them (ADR 0603 §10)", strings.Join(modules, ", "))), nil, "" - } - bound := st.j.buildWait() - waits := fmt.Sprintf("waits on the builder's builds of %s before it composes, at most %s (ADR 0603 §10)", strings.Join(modules, ", "), bound) - st.c.note(waits) - st.j.logf("%s %s", st.c.Key, waits) - at, cancel := context.WithTimeout(ctx, bound) - ended, err := st.j.BuildEnds.Ended(at, wait) - cancel() - if err != nil { - st.j.logf("%s: the builds of %s cannot be heard: %v", st.c.Key, strings.Join(modules, ", "), err) - return append(words, fmt.Sprintf("the builds of %s cannot be heard (%v), so the check composes without waiting on them (ADR 0603 §10)", strings.Join(modules, ", "), err)), nil, "" - } - var notEnded []string - for _, module := range modules { - b, done := ended[module] - switch { - case !done: - notEnded = append(notEnded, module) - case b.State == builds.BuildFailed: - failedBuilds = append(failedBuilds, FailedFirst{Test: BuildFailedTest + module, Words: fmt.Sprintf("the candidate's build at %s failed (rebuild with builder_build_again; the check asks itself again when the build is done): %s", wait[module], b.Error)}) - } - } - if len(notEnded) > 0 && ctx.Err() == nil { - late = fmt.Sprintf("the builder's builds of %s did not end within the build-wait-bound of %s, so the change is not composed: an engine composes a module only at a done build (ADR 0603 §10)", strings.Join(notEnded, ", "), bound) - } - return words, failedBuilds, late -} diff --git a/modules/mesh-delivery-checker/internal/check/buildseats.go b/modules/mesh-delivery-checker/internal/check/buildseats.go deleted file mode 100644 index 25b6e944..00000000 --- a/modules/mesh-delivery-checker/internal/check/buildseats.go +++ /dev/null @@ -1,56 +0,0 @@ -package check - -import ( - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "mesh-delivery-checker/rules" -) - -// **The checker names no build module** (design 47 G A-82): a build module is the holder of a build seat found on the seat list -// (buildrun.BuildSeats), by the language of the toolchain its seat's acting rule `build` names. The checker's own logic keys on -// that language: the Go repositories and the manifests are judged by the build module of Go, a TypeScript module's by that of -// TypeScript. - -// Languages of the toolchains the checker's logic is keyed on (the `on` of the build seats' acting rules). -const ( - languageGo = rules.LanguageGo - languageTypeScript = rules.LanguageTypeScript -) - -// buildModuleOf is the module that holds the build seat building a language's artifacts; the seat's name while no module is -// known to hold it or the seat list is not read, so a failure always names where it is. -func (j *Examiner) buildModuleOf(language string) string { - if m, err := j.Seats.For(language, ""); err == nil { - return m.Module - } - for _, m := range j.Seats.All() { - if m.Toolchain == language { - return m.Seat - } - } - ask := buildstep.AskGo - if language == languageTypeScript { - ask = buildstep.AskNode - } - seat, _ := buildstep.BuildSeat(ask) - return seat -} - -// goModule is the build module the Go repositories are judged by. -func (j *Examiner) goModule() string { return j.buildModuleOf(languageGo) } - -// goModule is the build module the manifests, the module graph and the checking program are judged by. -func (st *state) goModule() string { return st.j.goModule() } - -// languageOf is the language of the toolchain a build module builds with, empty for a module no build seat names. -func (j *Examiner) languageOf(module string) string { - l, _ := j.Seats.LanguageOf(module) - return l -} - -// askLanguageOf is the language of the asks the module's build seat takes (buildstep.AskGo, buildstep.AskNode, ...). -func (j *Examiner) askLanguageOf(module string) string { - if m, ok := j.Seats.OfModule(module); ok { - return m.AskLanguage() - } - return "" -} diff --git a/modules/mesh-delivery-checker/internal/check/buildseats_test.go b/modules/mesh-delivery-checker/internal/check/buildseats_test.go deleted file mode 100644 index 7dc80331..00000000 --- a/modules/mesh-delivery-checker/internal/check/buildseats_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package check - -import ( - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-sdk/go/seatdef" -) - -// The holders of the build seats in the tests' seat list: names the checker's code does not know, found by the seats' -// acting rules (design 47 G A-82). -const ( - goBuildModule = "mesh-build-go" - nodeBuildModule = "mesh-build-node" -) - -// testSeats is the finder over the build seats of the tests' seat list. -func testSeats() *buildrun.BuildSeats { - rule := func(on string) seatdef.Definition { - return seatdef.Definition{Acts: []seatdef.ActingRule{{Step: seatdef.StepBuild, On: on}}} - } - defs := map[string]seatdef.Definition{"node-build-go": rule("go"), "node-build-node": rule("typescript")} - holders := map[string]string{"node-build-go": goBuildModule, "node-build-node": nodeBuildModule} - return buildrun.NewBuildSeats(func() map[string]seatdef.Definition { return defs }, func(seat string) (string, bool) { h, ok := holders[seat]; return h, ok }) -} - -// stepStatesOf is a Reader's StepStates over fixed states. -func stepStatesOf(m map[string]StateRead) func() map[string]StateRead { - return func() map[string]StateRead { return m } -} - -// The checker names no build module (design 47 G A-82): it finds each by the language its seat's acting rule builds, whatever the -// holder is called, and says that holder's name in what it judges. -func TestTheBuildModulesAreFoundOnTheSeatListWhateverTheyAreCalled(t *testing.T) { - rule := func(on string) seatdef.Definition { - return seatdef.Definition{Acts: []seatdef.ActingRule{{Step: seatdef.StepBuild, On: on}}} - } - defs := map[string]seatdef.Definition{"node-build-go": rule("go"), "node-build-node": rule("typescript"), "node-build-image": rule("kind:image")} - holders := map[string]string{"node-build-go": "compilers", "node-build-node": "bundlers"} - j := &Examiner{Seats: buildrun.NewBuildSeats(func() map[string]seatdef.Definition { return defs }, func(seat string) (string, bool) { h, ok := holders[seat]; return h, ok })} - if got := j.goModule(); got != "compilers" { - t.Errorf("the module that judges Go = %q, want its seat's holder", got) - } - if got, lang, ask := j.buildModuleOf(languageTypeScript), j.languageOf("bundlers"), j.askLanguageOf("bundlers"); got != "bundlers" || lang != languageTypeScript || ask != "node" { - t.Errorf("TypeScript: module %q, language %q, ask language %q", got, lang, ask) - } - if got := j.languageOf("mesh-build-go"); got != "" { - t.Errorf("a module that holds no build seat builds with %q", got) - } - if !changesBuildGo(catalogueRepository, "compilers", []string{"modules/compilers/main.go"}) || changesBuildGo(catalogueRepository, "compilers", []string{"modules/mesh-build-go/main.go"}) { - t.Error("a change under the Go build module's own directory is the one that changes it") - } - // With no seat on the list the failure still names where it is: the seat. - none := &Examiner{Seats: buildrun.NewBuildSeats(func() map[string]seatdef.Definition { return defs }, nil)} - if got := none.goModule(); got != "node-build-go" { - t.Errorf("a build seat no module holds is named by the seat, got %q", got) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/case.go b/modules/mesh-delivery-checker/internal/check/case.go deleted file mode 100644 index 1be72fdb..00000000 --- a/modules/mesh-delivery-checker/internal/check/case.go +++ /dev/null @@ -1,292 +0,0 @@ -package check - -import ( - "encoding/json" - "fmt" - "maps" - "slices" - "sort" - "strings" - "sync" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// Pull is one pull request the checker judges: its head, the trunk head of its repository it is merged onto and the -// modules the indexer says it touches, with the fingerprints the indexer wrote for them. -type Pull struct { - Repository string - Number int - Head string - Trunk string - Modules []string - Fps map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue -} - -// Case is one check: a candidate's pull requests that are judged (those of a repository whose trunk is not known are left -// out; hq is judged as any repository since R4g, its checks Go tests: ADR 0527), the trunk head of each repository and the key of the verdict. -type Case struct { - // AskedAt is the running that asked again a check of these heads that could not run (PartRunning), "" otherwise. - AskedAt string - // Key is the verdict key, `.`. - Key string - Group string - HeadsHash string - // Heads is the heads hash the gate matches an Approve's members by (GateHeads): a verdict carries it. - Heads string - Pulls []Pull - // Trunk is the trunk head of each repository of Pulls. - Trunk map[string]string - // Left says why pull requests of the candidate are not judged (a repository with no known trunk); empty when all are. - Left []string - // Queued is when the checker took the case up, for the time of its queue. - Queued time.Time - // Seats is the seat list the checking program made, the checker's reader's list with the heads' definitions laid over - // it, which the manifest validator's and the node-engine composer's requests carry (ADR 0636 §1, §4); none until it is - // made. - Seats []json.RawMessage - // Try narrows a try's check (its level, its tests); nil for a pull request's. - Try *TryOptions - // Judged are the check's own judge's verdicts on the manifests the change brings, by module, as the seat mesh-catalog's - // verdict value (judgeManifests): the composer's request carries them in place of the running indexer's (ADR 0578 §2). - Judged map[string]json.RawMessage - - notesMu sync.Mutex - // warnings are, by pull request, what part of its check did not run (Case.warn). - warnings map[string]string - // seriesOf are, by pull request, the changes of a package series it makes (series.go), named in its status line. - seriesOf map[string][]string - // seriesChanges are, by pull request, the same changes as structures (series.go), named in the verdict and its event. - seriesChanges map[string][]meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem - notes []string - // asks are the ids of the asks the check put on the build seats' streams, in order: a try names them (Try.Asks). - asks []string - // Watch, when set, hears each ask the check puts, as it is put (step nil), and each value its step key takes while - // the check waits for it: the node that took it, the phase it says, its end. A try writes them into its record, so a - // running try names its asks and where each stands (issue 1890). Called from the check's goroutines; it must not block - // for long. - Watch func(askID string, step *buildstep.Step) - // Progress says what the check is doing, so a check that ran past its bound names the step it was stuck at. - Progress *Progress - // Wait says what a whole try waits for now ("" when nothing); the try keeps it for try-read. Nil for a pull request's check. - Wait func(why string) - // Kind is what asked the check (KindNew, KindNodes, KindTrunk): a carry after a trunk or sibling move is asked on the - // build seats' recheck stream, every other check on check (ADR 0536 §4, ADR 0548 §5). - Kind string - // CarriedFrom are, for the carry of a verdict that passed, the trunk and sibling commits it was judged on (ADR 0548 §5); - // nil for a whole check. - CarriedFrom map[string]string - // FPSet is the candidate's fingerprint set (Candidate.Fpset): a composition names the set it judged. - FPSet string - // Composition is, for a carry, the composition that passed on the same fingerprint set: it is kept and not asked again, - // since no fingerprint of a module it composed changed (ADR 0548 §5). Nil composes. - Composition *meshdeliverychecker.VerdictsByGroupHeadsValueComposition -} - -// Asked records the id of an ask the check put. -func (c *Case) Asked(id string) { - c.notesMu.Lock() - c.asks = append(c.asks, id) - c.notesMu.Unlock() -} - -// Asks are the ids of the asks the check put, in order. -func (c *Case) Asks() []string { - c.notesMu.Lock() - defer c.notesMu.Unlock() - return append([]string(nil), c.asks...) -} - -// Progress is the step a check is at, written by the check and read when its bound passes. -type Progress struct { - mu sync.Mutex - stage, node string -} - -// Set records the step now running and the node it runs on, empty for none. -func (p *Progress) Set(stage, node string) { - p.mu.Lock() - p.stage, p.node = stage, node - p.mu.Unlock() -} - -// Now is the step recorded last and its node. -func (p *Progress) Now() (stage, node string) { - p.mu.Lock() - defer p.mu.Unlock() - return p.stage, p.node -} - -// CaseOf is the check of a candidate against the trunk heads (the gate's source.main): the pull requests that are judged. -// The checker judges pull requests into main only (ADR 0484 clause 11): the pull message carries no base, a merge into -// another branch is a closed message, so every candidate is into the trunk; a repository whose trunk head is not known has -// none to merge onto. It answers nil when no pull request is left to judge. -func CaseOf(c meshcatalog.CandidatesByGroupValue, trunk map[string]string) *Case { - key, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - if err != nil { - return nil - } - cs := &Case{Key: key, Group: c.Group, HeadsHash: c.Heads, Heads: GateHeads(c), Trunk: map[string]string{}, Progress: &Progress{}, FPSet: c.Fpset} - for _, p := range c.Pulls { - switch head, known := trunk[p.Repository]; { - case !known || head == "": - cs.Left = append(cs.Left, fmt.Sprintf("%s#%d: %s", p.Repository, p.Number, UntrunkedWordsOf([]string{p.Repository}))) - default: - cs.Pulls = append(cs.Pulls, Pull{Repository: p.Repository, Number: int(p.Number), Head: p.Head, Trunk: head, Modules: p.Modules, Fps: p.Fps}) - cs.Trunk[p.Repository] = head - } - } - if len(cs.Pulls) == 0 { - return nil - } - sort.SliceStable(cs.Pulls, func(i, j int) bool { - if cs.Pulls[i].Repository != cs.Pulls[j].Repository { - return cs.Pulls[i].Repository < cs.Pulls[j].Repository - } - return cs.Pulls[i].Number < cs.Pulls[j].Number - }) - return cs -} - -// UntrunkedWords open the words of a verdict, and of a status, of a pull request that cannot be judged because the gate's -// source.main names no trunk head of its repository (issue 1422): never a failure of the change, and judged whole once the -// trunk head is known. It is said loudly, never left silently "new". -const UntrunkedWords = "cannot be judged: the trunk head of " - -// UntrunkedWordsOf are those words for the repositories whose trunk head is not known, in order, each named once. -func UntrunkedWordsOf(repos []string) string { - rs := slices.Clone(repos) - slices.Sort(rs) - rs = slices.Compact(rs) - return UntrunkedWords + strings.Join(rs, ", ") + " is not known: the gate's source.main names none, so there is no commit to merge it onto" -} - -// IsUntrunked says whether a verdict says its heads cannot be judged for want of a trunk head (UntrunkedWords). -func IsUntrunked(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && failedOf(v).Machine == "checker" && strings.HasPrefix(failedOf(v).Words, UntrunkedWords) -} - -// **A candidate outside the mesh is not the checker's** (sweep 2026-10-30 item 7): the forge carries repositories the mesh -// does not deliver (an operator's own), whose pull requests the indexer writes as candidates like any. Their trunk the -// gate never names, so each was judged "cannot be judged" and owed the status error on mesh/check, which the forge -// module wrote 119 times per head into a repository the mesh's forge account may not write (403). A candidate every pull -// request of which is of a repository the gate's source.main names no trunk head of, and touches no module, is outside -// the mesh: judged nothing, owed nothing on the forge, said once in the log and in checker_status (outside_the_mesh). A -// new repository of the mesh is not outside: its first pull request adds a module, and is said untrunked (issue 1422). - -// OutsideWords say why a candidate is outside the mesh. -const OutsideWords = "outside the mesh: the gate's source.main names no trunk head of its repositories and it touches no module, so the checker judges nothing and owes no status on the forge" - -// Outside says every pull request of c is of a repository trunk names no head of and touches no module. Never with -// trunk unknown (nil): before the gate's source is read nothing is outside. -func Outside(c meshcatalog.CandidatesByGroupValue, trunk map[string]string) bool { - if trunk == nil || len(c.Pulls) == 0 { - return false - } - for _, p := range c.Pulls { - if trunk[p.Repository] != "" || len(p.Modules) > 0 || len(p.Fps) > 0 { - return false - } - } - return true -} - -// leftOf are the pull requests of a candidate whose repository has no trunk head known, with Trunk empty, in repository and -// number order; nil when every repository's is known. -func leftOf(c meshcatalog.CandidatesByGroupValue, trunk map[string]string) []Pull { - var out []Pull - for _, p := range c.Pulls { - if trunk[p.Repository] == "" { - out = append(out, Pull{Repository: p.Repository, Number: int(p.Number), Head: p.Head, Modules: p.Modules, Fps: p.Fps}) - } - } - sort.SliceStable(out, func(i, j int) bool { - if out[i].Repository != out[j].Repository { - return out[i].Repository < out[j].Repository - } - return out[i].Number < out[j].Number - }) - return out -} - -// GateHeads is the hash the gate matches the members an Approve covers by (mesh-catalog mesh-delivery-gate onestatus.go -// headsOf): meshdeliveryclient.Heads over every pull request of the candidate, hq's included, as -// `#=`. A lone pull request is a group of one. Empty for a candidate -// with no pull request. -func GateHeads(c meshcatalog.CandidatesByGroupValue) string { - heads := make([]meshdeliveryclient.Head, 0, len(c.Pulls)) - for _, p := range c.Pulls { - heads = append(heads, meshdeliveryclient.Head{Repository: strings.ToLower(p.Repository), Number: int(p.Number), Head: strings.ToLower(p.Head)}) - } - hash, err := meshdeliveryclient.HeadsHash(heads) - if err != nil { - return "" - } - return hash -} - -// sameTrunk says whether two verdicts' trunk heads are the same. -func sameTrunk(a, b map[string]string) bool { return maps.Equal(a, b) } - -// Trunks holds the trunk head of each repository, as the catalog half's trunk revision says (a follower of the seat mesh-catalog's -// verb revision and its event catalog-revision sets it; design 47 G §197). -type Trunks struct { - mu sync.Mutex - heads map[string]string -} - -// Set takes the trunk heads in and answers whether they changed. -func (t *Trunks) Set(heads map[string]string) bool { - t.mu.Lock() - defer t.mu.Unlock() - if maps.Equal(t.heads, heads) { - return false - } - t.heads = maps.Clone(heads) - return true -} - -// Heads are the trunk heads now (a copy). -func (t *Trunks) Heads() map[string]string { - t.mu.Lock() - defer t.mu.Unlock() - return maps.Clone(t.heads) -} - -// note puts a word of the check's way on the case, for a try's line (issue 1921). -func (c *Case) note(s string) { - c.notesMu.Lock() - defer c.notesMu.Unlock() - if !slices.Contains(c.notes, s) { - c.notes = append(c.notes, s) - } -} - -// warn says of a pull request (pullKey) that a part of its check did not run, in words for its status line (issue 1577). -func (c *Case) warn(key, words string) { - c.notesMu.Lock() - defer c.notesMu.Unlock() - if c.warnings == nil { - c.warnings = map[string]string{} - } - c.warnings[key] = words -} - -// Warning is what warn said of a pull request, or "". -func (c *Case) Warning(key string) string { - c.notesMu.Lock() - defer c.notesMu.Unlock() - return c.warnings[key] -} - -// Notes are the words noted on the case, in the order they were noted. -func (c *Case) Notes() []string { - c.notesMu.Lock() - defer c.notesMu.Unlock() - return slices.Clone(c.notes) -} diff --git a/modules/mesh-delivery-checker/internal/check/checker.go b/modules/mesh-delivery-checker/internal/check/checker.go deleted file mode 100644 index ee6bbf26..00000000 --- a/modules/mesh-delivery-checker/internal/check/checker.go +++ /dev/null @@ -1,1126 +0,0 @@ -package check - -import ( - "context" - "errors" - "fmt" - "maps" - "mesh-delivery-checker/rules" - "slices" - "sort" - "strings" - "sync" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// DefaultBound is how long a check may run before it is ended failed naming the step it was stuck at (ADR 0484, issue 1678): -// none since R4g. A check's asks wait in the build seats' streams as long as the backlog is (ADR 0525 §9), and each ask's -// step is bounded from its start on the node that took it (buildrun.Asker.Bound, DefaultStepBound); a bound set by -// MESH_CHECKER_BOUND still ends a check that ran past it. -const DefaultBound time.Duration = 0 - -// UnheardWords end the Words of a failure because a part of the mesh the check reads was not heard (the forge, issue 1624): -// like a failure of the nodes, it is not the change's, and is asked again when the part it could not hear runs. -const UnheardWords = "could not run: not heard; asked again when it runs" - -// The events the checker says on its stream checks (module.json): a preview or a status was written, so a part that reads -// the bucket, which is made when the checker is assigned and may not exist when that part starts, finds it (ADR 0437 §8: no -// timer). The body is {"key": ...}. -const ( - EventPreviewWritten = "preview.written" - EventOwedWritten = "owed.written" -) - -// Announcer says an event of the checker's stream, with the key written. -type Announcer func(ctx context.Context, event, key string) - -// Outcome is what judging a case gives: the verdict (state, failure, levels, times, composition; the checker adds the -// group, the heads and the trunk) and, for the line, what the check knows of the builds of the modules it touches. -type Outcome struct { - Verdict meshdeliverychecker.VerdictsByGroupHeadsValue - Builds []BuildNote - // Slowest names the slowest tests of a level that went over its bound ("unit pkg.Test 1.2s"). - Slowest []string - // Judged says, by pull request (pullKey), which program judged a change to a build module (ADR 0535 §5); a pull request - // that changes none is not named. - Judged map[string]string - // Beside are the siblings the check read at their trunk's head, by repository (ADR 0548 §6): the verdict names them, and - // a move of one is carried. - Beside map[string]string - // OutOfStep are, by pull request (pullKey), the generated files out of step with their seat definitions that its steps - // named (design 26): the verdict event carries them. - OutOfStep map[string][]string - // Unheard is the part of the mesh the check could not hear, when its verdict could not run (IsCouldNotRun): its running - // asks the check again (PartRunning). The zero Part is a part the judge could not name. - Unheard Part -} - -// BuildNote is the state of a candidate's module build, by module: built, failed or waiting (ADR 0484 clause 7). -type BuildNote struct { - Module string - State string -} - -// Judge runs the check of a case: the merge, the levels. It writes what it is doing to c.Progress. It answers an error -// only when it could not judge at all; a failure of the change is a Verdict. -type Judge interface { - Judge(ctx context.Context, c *Case) (Outcome, error) -} - -// Checker judges the candidates the indexer writes, dark (ADR 0453 clause 4, ADR 0484): for each candidate it claims a -// verdict key, runs the check through its Judge, writes the verdict and, for each pull request judged, the owed status -// `mesh/check` the forge bridge performs. It holds no timer; a check starts from a candidate or from a change of the -// nodes, the trunk or the controller's plan, and ends when its judge does or its bound passes. -type Checker struct { - Verdicts KV - Owed KV - Judge Judge - Trunks interface{ Heads() map[string]string } - // Runs says which commit the mesh runs of a module (the indexer's modules): the node-engine a verdict read beside at its - // running commit is out of date when that commit moves (ADR 0548 §6); nil reads every sibling at its trunk. - Runs interface{ CommitOf(module string) string } - // CouldNotRun is the checker's own state could-not-run (CouldNotRunState): the checks that could not run, each asked - // again when the part it could not hear runs (PartRunning); nil keeps none, and none is asked again. - CouldNotRun KV - // Findings writes the checker's finding of a check not asked again within CouldNotRunBound (CouldNotRunDue); nil - // writes none. - Findings FindingWriter - // CouldNotRunKept is called each time a check that could not run is kept, so the process waits for its bound anew. - CouldNotRunKept func() - // Bound is how long one check may run; none when 0 (DefaultBound). - Bound time.Duration - // Rebuilds hears the builder's build keys a failed verdict names (failedBuilds): once each is written done, the check of - // those heads is asked again by the checker itself (ADR 0603 §10), as a failure of the nodes is; nil asks none again. - Rebuilds BuildEnds - // Asks keeps the asks each running check put, by verdict key (AsksState), for checker_ask_again; nil keeps none. - Asks AsksKV - // Link is the base of the address a status points at; the verdict key follows. - Link string - // Announce says a status was written (EventOwedWritten); nil says nothing. - Announce Announcer - // Says publishes the verdict event of each pull request whose check ended (EventVerdict); nil says none. - Says VerdictSayer - Now func() time.Time - Logf func(format string, args ...any) - // Settle is how long a group's heads must stay the same after the candidate last changed them before its check starts - // (issue 1890): the members of a group are announced one after another, and a check of each partial set is thrown - // away at the next. None when 0; DefaultSettle in the module. - Settle time.Duration - - once sync.Once - wg sync.WaitGroup - mu sync.Mutex - known map[string]meshcatalog.CandidatesByGroupValue // by candidates key - inflight map[string]bool // by verdict key - again map[string]bool // by verdict key: asked again while it ran - tried map[string]bool // by verdict key: looked at since this process started - release map[string]string // by verdict key: the running that released a check that could not run - live map[string]liveCheck // by verdict key: the check running in this process, ended when its heads go - settling map[string]*window // by candidates key: the window its heads settle in - rebuilt map[string]bool // by verdict key: every failed build its verdict names is done again - awaiting map[string]bool // by verdict key: its failed builds are heard (awaitRebuilt) - // outsideSaid are the candidates outside the mesh said in the log, by group and heads. - outsideSaid map[string]bool -} - -// liveCheck is a check running in this process: the candidates key of its group and the end of its run. -type liveCheck struct { - group string - end context.CancelCauseFunc -} - -// window is a group's settle window: aborted with errUnsettled when its heads change again or the group goes, and with -// errSettled to end it at once. -type window struct{ abort context.CancelCauseFunc } - -var ( - errUnsettled = errors.New("the group's heads changed again") - errSettled = errors.New("the group's heads settled") -) - -// settleNow ends the settle window of every group at once, as if Settle had passed. -func (ch *Checker) settleNow() { - ch.mu.Lock() - defer ch.mu.Unlock() - for _, w := range ch.settling { - w.abort(errSettled) - } -} - -// DefaultSettle is the module's Settle (issue 1890). On 2026-10-26 the three members of a group were announced within 2.8 -// seconds of each other (20:31:52.3, :53.7, :55.1 UTC), a session opening or pushing them one API call after another; -// fifteen seconds holds five times that, and costs a check of 10 to 18 minutes under 2.5 % of its time. A member that comes -// later still is no waste beyond its asks already taken: the check of the heads it replaced is ended at once. -const DefaultSettle = 15 * time.Second - -// EndedWords open the words of a verdict the checker ended without judging it, its heads no candidate's any more (issue -// 1890): never a failure of the change, and judged whole if a candidate holds those heads again. -const EndedWords = "ended: " - -// IsEnded says whether a verdict was ended without being judged (EndedWords). -func IsEnded(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && failedOf(v).Machine == "checker" && strings.HasPrefix(failedOf(v).Words, EndedWords) -} - -// superseded is the cause a running check is ended with: the heads that replaced its own, or none when its group went. -type superseded struct{ by string } - -func (s superseded) Error() string { - if s.by == "" { - return EndedWords + "no candidate holds these heads any more: the group was closed or merged" - } - return EndedWords + "superseded by " + s.by -} - -// Unwrap makes a check ended so a withdrawal of its asks: the asker drops what it put on the build nodes (ADR 0664 §4, §5). -func (s superseded) Unwrap() error { return buildrun.Withdrawn{Why: s.Error()} } - -func (ch *Checker) init() { - ch.once.Do(func() { - ch.known, ch.inflight, ch.again, ch.tried = map[string]meshcatalog.CandidatesByGroupValue{}, map[string]bool{}, map[string]bool{}, map[string]bool{} - ch.release = map[string]string{} - ch.live, ch.settling = map[string]liveCheck{}, map[string]*window{} - ch.rebuilt, ch.awaiting = map[string]bool{}, map[string]bool{} - ch.outsideSaid = map[string]bool{} - }) -} - -// endOthers ends every check of the group running in this process whose verdict key is not keep: no candidate holds its -// heads any more (issue 1890). Called with ch.mu held. -func (ch *Checker) endOthers(group, keep, by string) { - for k, l := range ch.live { - if l.group == group && k != keep { - l.end(superseded{by: by}) - } - } -} - -func (ch *Checker) logf(format string, args ...any) { - if ch.Logf != nil { - ch.Logf(format, args...) - } -} - -func (ch *Checker) now() time.Time { - if ch.Now != nil { - return ch.Now() - } - return time.Now() -} - -func (ch *Checker) bound() time.Duration { - if ch.Bound > 0 { - return ch.Bound - } - return DefaultBound -} - -// Wait returns when every check started so far has ended. -func (ch *Checker) Wait() { ch.wg.Wait() } - -// Candidate takes one value of the indexer's candidates bucket in (the key is the group) and checks it if it is to be -// checked. A value that is not a candidate is said and left. -func (ch *Checker) Candidate(ctx context.Context, key string, value []byte) { - if meshcatalogclient.IsTryGroup(key) { - return // a try's candidate is judged by its try (Tryer), never as a pull request's check: judged twice, the engines were asked twice - } - ch.init() - c, err := meshcatalog.DecodeCandidatesByGroupValue(value) - if err != nil { - ch.logf("candidates.%s holds what is not a candidate, left: %v", key, err) - return - } - vkey, _ := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - ch.mu.Lock() - prev, had := ch.known[key] - ch.known[key] = c - // A check of heads this candidate no longer holds is ended at once (issue 1890): a member's new head, a member joining - // or leaving. - ch.endOthers(key, vkey, GateHeads(c)) - moved := !had || prev.Heads != c.Heads - if had && moved { - if old, err := meshdeliverycheckerclient.VerdictKey(prev.Group, prev.Heads); err == nil && old != vkey { - defer ch.clearCouldNotRun(ctx, old) // its heads were replaced, after the lock - } - } - if ch.Settle > 0 && (moved || ch.settling[key] != nil) { - // The group's heads changed: its check waits until they have been the same for Settle, so a group whose members - // are announced one after another is checked once, whole. A later change starts the window again. The wait is a - // context's bound, no timer (ADR 0437 §8). - if w := ch.settling[key]; w != nil { - w.abort(errUnsettled) - } - base, abort := context.WithCancelCause(ctx) - w := &window{abort: abort} - ch.settling[key] = w - ch.wg.Add(1) - go func() { - defer ch.wg.Done() - wait, done := context.WithTimeout(base, ch.Settle) - defer done() - <-wait.Done() - settled := errors.Is(wait.Err(), context.DeadlineExceeded) || errors.Is(context.Cause(wait), errSettled) - ch.mu.Lock() - if !settled || ch.settling[key] != w { - ch.mu.Unlock() - return // changed again, forgotten, or the checker stops - } - delete(ch.settling, key) - now, ok := ch.known[key] - ch.mu.Unlock() - abort(nil) - if ok { - ch.consider(ctx, key, now) - } - }() - ch.mu.Unlock() - return - } - ch.mu.Unlock() - ch.consider(ctx, key, c) -} - -// VerdictDeleted takes the delete of a verdict key in (the checker's watch of its verdicts, issue 1733): the gate asks the -// operator's Approve only on a verdict, so heads a candidate holds are never left without one. A check of them running in -// this process writes its verdict anew at its end; otherwise the candidate is considered at once and judged again (or said -// again to be unjudgeable). A key no candidate holds is said and left. -func (ch *Checker) VerdictDeleted(ctx context.Context, key string) { - ch.init() - ch.mu.Lock() - if ch.inflight[key] { - ch.mu.Unlock() - ch.logf("%s: deleted while its check runs; its end writes it anew", key) - return - } - var group string - var cand meshcatalog.CandidatesByGroupValue - for k, c := range ch.known { - if vk, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads); err == nil && vk == key { - group, cand = k, c - break - } - } - ch.mu.Unlock() - if group == "" { - ch.logf("%s: deleted, and no candidate holds these heads, so nothing is judged", key) - return - } - ch.logf("%s: deleted; its heads are judged again", key) - ch.consider(ctx, group, cand) -} - -// Forget takes a candidate out (its key was deleted): a check of its heads still running is ended, as no candidate holds -// them any more. -func (ch *Checker) Forget(key string) { - ch.init() - ch.mu.Lock() - gone, had := ch.known[key] - delete(ch.known, key) - if had { - if vkey, err := meshdeliverycheckerclient.VerdictKey(gone.Group, gone.Heads); err == nil { - defer ch.clearCouldNotRun(context.Background(), vkey) // its pull request closed, after the lock - } - } - if w := ch.settling[key]; w != nil { - w.abort(errUnsettled) - delete(ch.settling, key) - } - ch.endOthers(key, "", "") - ch.mu.Unlock() -} - -// NodesChanged is Reconsider after a change of the nodes: one came online or went, a build module became healthy or -// stopped being, or a toolchain moved. A check that could not run is not asked again by it, only by the running of the -// part it could not hear (PartRunning). -func (ch *Checker) NodesChanged(ctx context.Context) { - ch.init() - ch.Reconsider(ctx) -} - -// Reconsider looks at every candidate again: the trunk moved, the nodes changed or a delivery ended. A candidate whose -// verdict stands for the trunk it was judged against is left. -func (ch *Checker) Reconsider(ctx context.Context) { - ch.init() - ch.mu.Lock() - known := make(map[string]meshcatalog.CandidatesByGroupValue, len(ch.known)) - for k, c := range ch.known { - known[k] = c - } - ch.mu.Unlock() - for k, c := range known { - ch.consider(ctx, k, c) - } -} - -func (ch *Checker) heads() map[string]string { - if ch.Trunks == nil { - return nil - } - return ch.Trunks.Heads() -} - -func (ch *Checker) consider(ctx context.Context, key string, c meshcatalog.CandidatesByGroupValue) { - heads := ch.heads() - if Outside(c, heads) { - ch.outside(ctx, c) - return - } - if left := leftOf(c, heads); len(left) > 0 { - // A pull request the checker cannot claim (its repository's trunk head is not known) leaves its whole group - // unjudged, said in a failure verdict (issue 1422, issue 1733): the gate asks the Approve only on a verdict, and a - // pass of the other members would let the group merge with that one never checked. - ch.untrunked(ctx, c, left) - return - } - cs := CaseOf(c, heads) - if cs == nil { - return // no pull request at all: nothing to judge - } - cs.Queued = ch.now() - ch.mu.Lock() - if ch.settling[key] != nil { - ch.mu.Unlock() - return // its heads are settling: the window's end considers it (Settle) - } - if ch.inflight[cs.Key] { - ch.again[cs.Key] = true // looked at once more when the check ends, so a change of the trunk is never lost - ch.mu.Unlock() - return - } - ch.inflight[cs.Key] = true - run, end := context.WithCancelCause(ctx) - ch.live[cs.Key] = liveCheck{group: key, end: end} - ch.mu.Unlock() - ch.wg.Add(1) - go func() { - defer ch.wg.Done() - vkey := cs.Key - defer func() { - ch.mu.Lock() - delete(ch.inflight, vkey) - delete(ch.again, vkey) - delete(ch.live, vkey) - ch.mu.Unlock() - again := errors.Is(context.Cause(run), errAskedAgain) - end(nil) - if again && ctx.Err() == nil { - ch.consider(ctx, key, ch.candidate(key, c)) // asked again by hand (AskAgainTool): claimed anew - } - }() - for { - // Every check is asked at once: the build seats' streams hold the backlog, a new head's check and a check after a - // failure of the nodes on check, a re-judge after a trunk move on recheck, pulled after them (ADR 0525 §4, §9; - // ADR 0536 §4). The checker holds no place and no capacity of its own. - ch.prepare(ctx, key, cs) - ch.try(ctx, run, key, cs) - ch.hearFailedBuilds(ctx, key, cs) // a check it failed on a build is asked again once that build is done again - ch.mu.Lock() - more := ch.again[vkey] - delete(ch.again, vkey) - ch.mu.Unlock() - if !more || run.Err() != nil { - return - } - // Looked at again for the same heads only: a candidate that moved to other heads has its own check (issue 1890). - if next := CaseOf(ch.candidate(key, c), ch.heads()); next != nil { - if next.Key != vkey { - return - } - cs = next - } - cs.Queued = ch.now() - } - }() -} - -// untrunked says, loudly, that pull requests of a candidate cannot be judged because the gate's source.main names no trunk -// head of their repository (issue 1422): its verdict key holds a failure that says so, whatever it held before (a running -// verdict left by a check that can no longer run, or a verdict judged on a trunk that is no longer named: issue 1733), which -// checker_verdicts and checker_status show; and every pull request of the candidate gets the status error with -// UntrunkedWords, the members whose trunk is known too, as their group is not judged. It is logged once per head: when a -// status or the verdict is written, never at a look that finds them already said. Once the trunk head is known the verdict -// is stale (it names no trunk) and the heads are judged whole (prepare, claim). -func (ch *Checker) untrunked(ctx context.Context, c meshcatalog.CandidatesByGroupValue, left []Pull) { - key, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - if err != nil { - ch.logf("candidates of %s: no verdict key for heads %q, so it cannot be said that they cannot be judged: %v", c.Group, c.Heads, err) - return - } - repos := make([]string, 0, len(left)) - for _, p := range left { - repos = append(repos, p.Repository) - } - words := UntrunkedWordsOf(repos) - v := meshdeliverychecker.VerdictsByGroupHeadsValue{Group: c.Group, Heads: GateHeads(c), Trunk: map[string]string{}, State: meshdeliverycheckerclient.VerdictFail, - Failed: &FailedFirst{Machine: "checker", Words: words}} - data, err := meshdeliverycheckerclient.EncodeVerdict(v) - if err != nil { - ch.logf("%s: %s, and the verdict cannot say so: %v", key, words, err) - return - } - said, err := putOnce(ctx, ch.Verdicts, key, data, func(cur []byte) bool { - held, err := meshdeliverycheckerclient.DecodeVerdict(cur) - return err == nil && IsUntrunked(held) && failedOf(held).Words == words // the same words stand; anything else is replaced - }) - if err != nil { - ch.logf("%s: %s, and the verdict cannot say so: %v", key, words, err) - } - for _, cp := range c.Pulls { - okey, err := CheckerOwedKey(cp.Repository, int(cp.Number), cp.Head) - if err != nil { - ch.logf("%s: %v", key, err) - continue - } - o := meshdeliverychecker.OwedByRepositoryNumberHeadActValue{Act: meshdeliveryclient.OwedStatus, Context: meshdeliveryclient.StatusContextCheck, State: "error", - Target: strings.TrimSuffix(ch.Link, "/") + "/" + key, Text: rules.ClipLine(words, MaxLine)} - data, err := EncodeCheckerOwed(o) - if err != nil { - ch.logf("%s: the status of %s cannot be written: %v", key, okey, err) - continue - } - wrote, err := putOnce(ctx, ch.Owed, okey, data, func(cur []byte) bool { - held, err := DecodeCheckerOwed(cur) - o.V = keys.Version - return err == nil && held == o - }) - if err != nil { - ch.logf("%s: the status of %s cannot be written: %v", key, okey, err) - continue - } - if wrote { - said = true - if ch.Announce != nil { - ch.Announce(ctx, EventOwedWritten, okey) - } - } - } - if said { - names := make([]string, 0, len(left)) - for _, p := range left { - names = append(names, fmt.Sprintf("%s#%d@%s", p.Repository, p.Number, short12(p.Head))) - } - ch.logf("%s: %s %s; the verdict and every status of its group say so (error), and it is judged once the trunk head is known", key, strings.Join(names, ", "), words) - } -} - -// outside takes a candidate outside the mesh (case.go Outside): no verdict, and every status owed for its pull requests -// taken away, so the forge module stops writing one into a repository the mesh's forge account may not write. Said once -// per heads in the log. -func (ch *Checker) outside(ctx context.Context, c meshcatalog.CandidatesByGroupValue) { - var names []string - for _, p := range c.Pulls { - names = append(names, fmt.Sprintf("%s#%d@%s", p.Repository, p.Number, short12(p.Head))) - key, err := CheckerOwedKey(p.Repository, int(p.Number), p.Head) - if err != nil { - continue - } - _, _, found, err := ch.Owed.Get(ctx, key) - if err != nil || !found { - continue - } - d, ok := ch.Owed.(Deleter) - if !ok { - continue - } - if err := d.Delete(ctx, key); err != nil { - ch.logf("%s: the status owed of a pull request outside the mesh could not be taken away: %v", key, err) - } - } - said := c.Group + "@" + c.Heads - ch.mu.Lock() - first := !ch.outsideSaid[said] - ch.outsideSaid[said] = true - ch.mu.Unlock() - if first { - ch.logf("candidates of %s: %s %s", c.Group, strings.Join(names, ", "), OutsideWords) - } -} - -// The kinds of a check (ADR 0536 §4): a head never judged and a head asked again after a failure of the nodes are asked on -// the build seats' check stream; a carry, a verdict moved to a newer trunk or sibling commit by its package digests (ADR 0548 -// §5), on the recheck stream, pulled after. -const ( - KindNew = "new" - KindNodes = "nodes" - KindTrunk = "trunk" -) - -// prepare reads the case's verdict key and says what its check is (ADR 0548 §1, §5, §7): none (or one left running) is a -// head never judged, a whole check; a failure of the nodes is asked again after the nodes changed; a verdict judged on trunk -// or sibling commits that moved since is carried, its packages run only where the move changed their digest, and a pass's -// commits are named as the carry's CarriedFrom. A verdict that stands is ranked with the carries: its claim finds nothing -// to do. -func (ch *Checker) prepare(ctx context.Context, group string, cs *Case) { - cs.Kind, cs.CarriedFrom, cs.Composition = KindNew, nil, nil - value, _, found, err := ch.Verdicts.Get(ctx, cs.Key) - if err != nil || !found { - return - } - v, err := meshdeliverycheckerclient.DecodeVerdict(value) - switch { - case err != nil || v.State == meshdeliverycheckerclient.VerdictRunning: - return - case IsCouldNotRun(v): - cs.Kind = KindNodes - return - case failedBuilds(v, cs) != nil: - cs.Kind = KindNodes // asked again on check once its builds are done again (awaitRebuilt), never by a recheck - ch.awaitRebuilt(ctx, group, cs.Key, failedBuilds(v, cs)) - return - case IsConflictFailure(v): - return // a head git refused: its next check is whole (ADR 0548 §7) - case IsEnded(v), IsUntrunked(v): - return // ended unjudged when its heads went (issue 1890), or never judged for want of a trunk head (issue 1422): checked whole - } - cs.Kind = KindTrunk - if v.State == meshdeliverycheckerclient.VerdictPass && ch.stale(v, cs) { - cs.CarriedFrom = judgedOn(v) - if c := v.Composition; c != nil && c.State == meshdeliverycheckerclient.CompositionPass && c.Fpset != "" && c.Fpset == cs.FPSet { - kept := *c - cs.Composition = &kept - } - } -} - -// stale says whether a verdict was judged on commits that are no longer the case's: a trunk it merged moved, or a sibling it -// read at its trunk's head (ADR 0548 §6). -func (ch *Checker) stale(v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case) bool { - return !sameTrunk(v.Trunk, cs.Trunk) || besideMoved(v.Beside, ch.besideNow()) -} - -// besideNow are the commits a sibling is read at now, by repository: its trunk head, and for the node-engine the commit the -// mesh runs when the indexer names one, as besideFor places them. -func (ch *Checker) besideNow() map[string]string { - return besideAt(ch.heads(), ch.Runs) -} - -// besideAt places the commits a sibling is read at: the trunk heads, with the node-engine at the commit the mesh runs when -// runs names one (ADR 0548 §6). The checker's decision and the status read the same commits through it. -func besideAt(now map[string]string, runs interface{ CommitOf(module string) string }) map[string]string { - if runs != nil { - if c := runs.CommitOf(nodeEngineModule); c != "" { - out := make(map[string]string, len(now)+1) - for r, h := range now { - out[r] = h - } - out[nodeEngineRepo] = c - return out - } - } - return now -} - -// besideMoved says whether a sibling read at these commits moved: the commit it is read at now is known and another. -func besideMoved(beside, now map[string]string) bool { - for r, c := range beside { - if h := now[r]; h != "" && h != c { - return true - } - } - return false -} - -// judgedOn are the trunk and sibling commits a verdict was judged on, by repository. -func judgedOn(v meshdeliverychecker.VerdictsByGroupHeadsValue) map[string]string { - out := make(map[string]string, len(v.Trunk)+len(v.Beside)) - for r, c := range v.Beside { - out[r] = c - } - for r, c := range v.Trunk { - out[r] = c - } - return out -} - -// carried is what a carry's verdict names as carried from (ADR 0548 §5): the commits it was judged on before, of the -// repositories the new verdict still judges or reads; nil when none of them moved. -func carried(from map[string]string, v meshdeliverychecker.VerdictsByGroupHeadsValue) map[string]string { - out, moved := map[string]string{}, false - for r, c := range from { - now, ok := v.Trunk[r] - if !ok { - now, ok = v.Beside[r] - } - if ok && now != "" && c != "" { - out[r] = c - moved = moved || now != c - } - } - if !moved { - return nil - } - return out -} - -// moveWords say a carry's moves, for the words of a failure: "carried from to of ", each moved -// repository, sorted. -func moveWords(from map[string]string, v meshdeliverychecker.VerdictsByGroupHeadsValue) string { - var moves []string - for r, c := range from { - now, ok := v.Trunk[r] - if !ok { - now = v.Beside[r] - } - if now != "" && now != c { - moves = append(moves, fmt.Sprintf("%s %s to %s", r, short12(c), short12(now))) - } - } - sort.Strings(moves) - return strings.Join(moves, ", ") -} - -func (ch *Checker) candidate(key string, fallback meshcatalog.CandidatesByGroupValue) meshcatalog.CandidatesByGroupValue { - ch.mu.Lock() - defer ch.mu.Unlock() - if c, ok := ch.known[key]; ok { - return c - } - return fallback -} - -// claimed is a verdict key the checker holds: the revision the next write is made at. -type claimed struct{ rev uint64 } - -// claim takes the check of the case, or answers nil when it is not this checker's to run now: -// - no key: it is created running; a create that finds the key made meanwhile gets nothing; -// - a key left running is taken over as it stands when this process looks at it for the first time (it was left by the -// checker that stopped; there is one instance, as there is one builder per node, ADR 0478 clause 6), and left otherwise; -// - a pass or a failure for the trunk heads the case has stands; one for other trunk heads (the trunk moved) is taken -// again by compare-and-set on the same key, and so is a failure of the nodes after the nodes changed. -func (ch *Checker) claim(ctx context.Context, cs *Case, first bool) (*claimed, error) { - running := meshdeliverychecker.VerdictsByGroupHeadsValue{Group: cs.Group, Heads: cs.Heads, Trunk: cs.Trunk, State: meshdeliverycheckerclient.VerdictRunning} - data, err := meshdeliverycheckerclient.EncodeVerdict(running) - if err != nil { - return nil, err - } - value, rev, found, err := ch.Verdicts.Get(ctx, cs.Key) - if err != nil { - return nil, fmt.Errorf("%s: %w", cs.Key, err) - } - if !found { - rev, err := ch.Verdicts.Create(ctx, cs.Key, data) - if errors.Is(err, ErrConflict) { - return nil, nil // another checker made it first - } - if err != nil { - return nil, fmt.Errorf("%s: %w", cs.Key, err) - } - return &claimed{rev}, nil - } - cur, err := meshdeliverycheckerclient.DecodeVerdict(value) - if err != nil { - return nil, fmt.Errorf("%s holds what is not a verdict: %w", cs.Key, err) - } - retake := false - switch { - case cur.State == meshdeliverycheckerclient.VerdictRunning: - if first { - return &claimed{rev}, nil - } - return nil, nil - case IsEnded(cur), ch.stale(cur, cs): - retake = true // heads held again after their check was ended unjudged (issue 1890), or a trunk that moved - case cur.State == meshdeliverycheckerclient.VerdictFail && ch.takeRebuilt(cs.Key): - retake = true // every failed build it names is done again (ADR 0603 §10) - case IsCouldNotRun(cur): - // Its claim was released when it could not run: only the running of the part it could not hear asks it again. - cs.AskedAt, retake = ch.takeRelease(cs.Key) - } - if !retake { - return nil, nil - } - rev, err = ch.Verdicts.Update(ctx, cs.Key, data, rev) - if errors.Is(err, ErrConflict) { - return nil, nil - } - if err != nil { - return nil, fmt.Errorf("%s: %w", cs.Key, err) - } - return &claimed{rev}, nil -} - -// ConflictWords are what the words of a head that git would not merge onto its trunk say before the files in conflict. -const ConflictWords = "does not merge into the trunk: files in conflict: " - -// IsConflictFailure says whether a verdict failed because its head did not merge onto its trunk (ADR 0548 §7): it has no -// pass to carry, so its next check, at a push or at a later trunk move, is a whole check of its head. -func IsConflictFailure(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && strings.Contains(failedOf(v).Words, ConflictWords) -} - -// try claims the check, judges it within its bound under run and writes the verdict and the owed statuses. A check that ends -// because ctx did is left running: the checker takes it again when it starts. A check whose run was ended because no -// candidate holds its heads any more (issue 1890) is ended: its verdict says so, and no status is written. -func (ch *Checker) try(ctx, run context.Context, key string, cs *Case) { - if run.Err() != nil { - return // ended before it was claimed: nothing to write - } - ch.mu.Lock() - first := !ch.tried[cs.Key] - ch.tried[cs.Key] = true - ch.mu.Unlock() - cl, err := ch.claim(ctx, cs, first) - if err != nil { - ch.logf("%s: %v", cs.Key, err) - return - } - if cl == nil { - return - } - ch.clearCouldNotRun(ctx, cs.Key) // checked again: a record that it could not run, and its finding, go - began := ch.now() - if cs.Watch == nil && ch.Asks != nil { - cs.Watch = func(_ string, step *buildstep.Step) { - if step == nil { // put: kept before its wait starts - ch.recordAsks(ctx, cs.Key, cs.Asks()) - } - } - defer ch.forgetAsks(ctx, cs.Key) - } - ch.writeOwed(ctx, cs, meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictRunning}, Outcome{}) - out := ch.judge(run, cs) - // An ask the build seat gave up on, or whose step the build module's next process ended because it was replaced under - // it, judged nothing (issue 734, IsLostAsk): the check is asked again under the same claim, its status pending, and after - // LostAskTries it fails as not heard, asked again when the part it could not hear runs. A step that ended while its watch was deaf is - // read at the next nudge (HeardEnds) and judged. - for try := 1; IsLostAsk(out.Verdict) && run.Err() == nil; try++ { - if try >= LostAskTries { - out.Verdict.Failed.Words = strings.TrimSpace(failedOf(out.Verdict).Words + "\nasked " + fmt.Sprint(LostAskTries) + - " times and given up each time: " + UnheardWords) - break - } - ch.logf("%s: %s; asked again (%d of %d)", cs.Key, failedOf(out.Verdict).Words, try+1, LostAskTries) - out = ch.judge(run, cs) - } - if ctx.Err() != nil || errors.Is(context.Cause(run), errAskedAgain) { - return // the checker stops, or the verdict was asked again by hand, which wrote it: its next check claims it anew - } - var why superseded - if errors.As(context.Cause(run), &why) { - ch.end(ctx, cs, cl, why) - return - } - v := out.Verdict - v.Group, v.Heads, v.Trunk, v.Beside = cs.Group, cs.Heads, cs.Trunk, nil - for r, c := range out.Beside { - if _, merged := cs.Trunk[r]; !merged && c != "" { - if v.Beside == nil { - v.Beside = map[string]string{} - } - v.Beside[r] = c - } - } - if cs.CarriedFrom != nil { - // A carry (ADR 0548 §5): a pass names the commits it was carried from; a failure names the move it failed on. - switch v.State { - case meshdeliverycheckerclient.VerdictPass: - v.CarriedFrom = carried(cs.CarriedFrom, v) - case meshdeliverycheckerclient.VerdictFail: - if moves := moveWords(cs.CarriedFrom, v); moves != "" { - v.Failed.Words = strings.TrimSpace(failedOf(v).Words + "\nfailed on the carry over the move of " + moves + ": the move changed what these tests read") - } - } - } - if tookOf(v).Queue == 0 && !cs.Queued.IsZero() && began.After(cs.Queued) { - if v.Took == nil { - v.Took = &meshdeliverychecker.VerdictsByGroupHeadsValueTook{} - } - v.Took.Queue = began.Sub(cs.Queued).Milliseconds() - } - data, err := meshdeliverycheckerclient.EncodeVerdict(v) - if err != nil { - ch.logf("%s: the verdict cannot be written: %v", cs.Key, err) - v = meshdeliverychecker.VerdictsByGroupHeadsValue{Group: cs.Group, Heads: cs.Heads, Trunk: cs.Trunk, State: meshdeliverycheckerclient.VerdictFail, - Failed: &FailedFirst{Machine: "checker", Words: "the verdict could not be written: " + err.Error()}} - if data, err = meshdeliverycheckerclient.EncodeVerdict(v); err != nil { - return - } - } - if _, err := ch.Verdicts.Update(ctx, cs.Key, data, cl.rev); err != nil { - if _, _, found, gerr := ch.Verdicts.Get(ctx, cs.Key); gerr == nil && !found { - _, err = ch.Verdicts.Create(ctx, cs.Key, data) // its key was deleted while it ran (issue 1733): written anew - } - if err != nil { - ch.logf("%s: the verdict could not be written: %v", cs.Key, err) - return - } - } - if IsCouldNotRun(v) { - ch.keepCouldNotRun(ctx, cs, v, out.Unheard, cs.AskedAt) - } - if ch.movedSince(cs) || besideMoved(v.Beside, ch.besideNow()) { - // Judged on a trunk that moved while it ran: the verdict is kept as judged, and the status stays pending until the - // check of the new trunk, which Reconsider starts, ends. - ch.owePending(ctx, cs, v) - ch.logf("%s: %s on a trunk that moved while it ran; the status stays pending until it is carried", cs.Key, v.State) - return - } - ch.writeOwed(ctx, cs, v, out) - ch.sayVerdict(ctx, cs, v, out) - ch.logf("%s: %s", cs.Key, v.State) -} - -// end writes the verdict of a check whose heads no candidate holds any more (issue 1890), by compare-and-set on its claim, -// and says it once in the log with the asks it had put: they stay in the build seats' streams, since an ask once put cannot -// be taken back (buildrun.Asker has no withdraw), and are no longer waited for. No status is written: the heads that -// replaced these have their own check, whose pending status stands. -func (ch *Checker) end(ctx context.Context, cs *Case, cl *claimed, why superseded) { - v := meshdeliverychecker.VerdictsByGroupHeadsValue{Group: cs.Group, Heads: cs.Heads, Trunk: cs.Trunk, State: meshdeliverycheckerclient.VerdictFail, - Failed: &FailedFirst{Machine: "checker", Words: why.Error()}} - data, err := meshdeliverycheckerclient.EncodeVerdict(v) - if err == nil { - _, err = ch.Verdicts.Update(ctx, cs.Key, data, cl.rev) - } - if err != nil { - ch.logf("%s: %s, but the verdict could not say so: %v", cs.Key, why.Error(), err) - return - } - stage, _ := cs.Progress.Now() - asks := cs.Asks() - ch.logf("%s: %s; it was at %q with %d asks put %v, which are no longer waited for", cs.Key, why.Error(), stage, len(asks), asks) -} - -// judge runs the case within the bound. A judge that fails to judge, and a check that ran past its bound, are verdicts of -// failure naming the machine and the step. -func (ch *Checker) judge(ctx context.Context, cs *Case) Outcome { - out, _ := judgeWithin(ctx, ch.Judge, cs, ch.bound()) - return out -} - -var errBound = errors.New("the check ran past its bound") - -func failure(machine, words string) meshdeliverychecker.VerdictsByGroupHeadsValue { - return meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: machine, Words: words}} -} - -// writeOwed writes, for each pull request judged, the status the forge bridge performs: pending while the check runs, success -// or failure at its end (ADR 0484 clause 2). A key already holding the same value is not written again, so the bridge -// performs one act for each change; an older head's key is never touched. -func (ch *Checker) writeOwed(ctx context.Context, cs *Case, v meshdeliverychecker.VerdictsByGroupHeadsValue, out Outcome) { - for _, p := range cs.Pulls { - key, err := CheckerOwedKey(p.Repository, int(p.Number), p.Head) - if err != nil { - ch.logf("%s: %v", cs.Key, err) - continue - } - state := map[string]string{meshdeliverycheckerclient.VerdictRunning: "pending", meshdeliverycheckerclient.VerdictPass: "success", meshdeliverycheckerclient.VerdictFail: "failure"}[v.State] - o := meshdeliverychecker.OwedByRepositoryNumberHeadActValue{Act: meshdeliveryclient.OwedStatus, Context: meshdeliveryclient.StatusContextCheck, State: state, - Target: strings.TrimSuffix(ch.Link, "/") + "/" + cs.Key, Text: Line(v, cs, p, out.Builds, out.Slowest, out.Judged)} - data, err := EncodeCheckerOwed(o) - if err != nil { - ch.logf("%s: the status of %s cannot be written: %v", cs.Key, key, err) - continue - } - wrote, err := putOnce(ctx, ch.Owed, key, data, func(cur []byte) bool { - held, err := DecodeCheckerOwed(cur) - o.V = keys.Version - return err == nil && held == o - }) - if err != nil { - ch.logf("%s: the status of %s cannot be written: %v", cs.Key, key, err) - } else if wrote && ch.Announce != nil { - ch.Announce(ctx, EventOwedWritten, key) - } - } -} - -// putOnce writes data under key by create, or by compare-and-set when the key exists, and writes nothing when same says the -// key holds the value already. -func putOnce(ctx context.Context, kv KV, key string, data []byte, same func(cur []byte) bool) (bool, error) { - for try := 0; try < 4; try++ { - cur, rev, found, err := kv.Get(ctx, key) - if err != nil { - return false, err - } - if found && same(cur) { - return false, nil - } - if !found { - _, err = kv.Create(ctx, key, data) - } else { - _, err = kv.Update(ctx, key, data, rev) - } - if errors.Is(err, ErrConflict) { - continue - } - return err == nil, err - } - return false, fmt.Errorf("%s changed under every one of 4 tries", key) -} - -// Sweep closes, at start, every verdict left running for a candidate that no longer holds those heads: nothing will check -// them again, and the key would stay running for ever (the builder's Sweep, issue 1660). Call it once the candidates have been -// read. A key of heads a candidate holds now is not touched: the check takes it over. -func (ch *Checker) Sweep(ctx context.Context) error { - ch.init() - keys, err := ch.Verdicts.Keys(ctx) - if err != nil { - return err - } - ch.mu.Lock() - current := map[string]bool{} - for _, c := range ch.known { - if k, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads); err == nil { - current[k] = true - } - } - ch.mu.Unlock() - for _, key := range keys { - if current[key] { - continue - } - value, rev, found, err := ch.Verdicts.Get(ctx, key) - if err != nil || !found { - continue - } - cur, err := meshdeliverycheckerclient.DecodeVerdict(value) - if err != nil || cur.State != meshdeliverycheckerclient.VerdictRunning { - continue - } - cur.State = meshdeliverycheckerclient.VerdictFail - cur.Failed = &FailedFirst{Machine: "checker", Words: EndedWords + "closed at start: left running, and no candidate holds these heads any more, so nothing will check them"} - data, err := meshdeliverycheckerclient.EncodeVerdict(cur) - if err != nil { - return err - } - if _, err := ch.Verdicts.Update(ctx, key, data, rev); err != nil && !errors.Is(err, ErrConflict) { - return fmt.Errorf("%s: %w", key, err) - } - ch.logf("%s: closed, left running for heads no candidate holds", key) - } - return nil -} - -// TrunkMoved is Reconsider after a move of a trunk, which first owes mesh/check pending on every open pull request of a -// candidate whose verdict was judged on other trunk heads, every member's for a group (the lead's decision L4, the checker's -// part of issue 1302): the forge's success of the old trunk never stands while the check waits for a place or for a delivery -// that holds it, so the gate never reads it as the head's. -func (ch *Checker) TrunkMoved(ctx context.Context) { - ch.init() - ch.mu.Lock() - known := make(map[string]meshcatalog.CandidatesByGroupValue, len(ch.known)) - for k, c := range ch.known { - known[k] = c - } - ch.mu.Unlock() - heads := ch.heads() - for _, c := range known { - cs := CaseOf(c, heads) - if cs == nil { - continue - } - value, _, found, err := ch.Verdicts.Get(ctx, cs.Key) - if err != nil || !found { - continue // never judged: no status to put out of date - } - v, err := meshdeliverycheckerclient.DecodeVerdict(value) - if err == nil && !ch.stale(v, cs) { - continue - } - if err == nil && v.State == meshdeliverycheckerclient.VerdictPass { - cs.CarriedFrom = judgedOn(v) // the status says it is being carried to the new commit (ADR 0548 §6) - } - ch.writeOwed(ctx, cs, meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictRunning}, Outcome{}) - } - ch.Reconsider(ctx) -} - -// owePending owes mesh/check pending on the heads of a verdict judged on commits that moved while it ran: a pass says it is -// being carried to the trunk's head now (ADR 0548 §6), anything else that it is checked again. -func (ch *Checker) owePending(ctx context.Context, judged *Case, v meshdeliverychecker.VerdictsByGroupHeadsValue) { - next := judged - ckey, err := meshcatalogclient.CandidatesKey(v.Group) - ch.mu.Lock() - c, ok := ch.known[ckey] - ch.mu.Unlock() - if err == nil && ok { - if now := CaseOf(c, ch.heads()); now != nil && now.Key == judged.Key { - next = now - } - } - if v.State == meshdeliverycheckerclient.VerdictPass { - next.CarriedFrom = judgedOn(v) - } - ch.writeOwed(ctx, next, meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictRunning}, Outcome{}) -} - -// movedSince says whether a trunk the case was judged on is no longer the trunk's head. -func (ch *Checker) movedSince(cs *Case) bool { - now := ch.heads() - for repo, head := range cs.Trunk { - if now[repo] != head { - return true - } - } - return false -} - -// awaitRebuilt hears, once per verdict key, the builder's keys of the builds a failed verdict names: when each is written -// done again (builder_build_again), the check of those heads is asked again (claim retakes it), as a failure of the nodes -// is after the nodes changed, so no recheck by hand is needed (ADR 0603 §10). Heard on the builder's own writes, never -// polled; it ends with the checker. -func (ch *Checker) awaitRebuilt(ctx context.Context, group, vkey string, builds map[string]string) { - if ch.Rebuilds == nil { - return - } - ch.mu.Lock() - if ch.awaiting[vkey] { - ch.mu.Unlock() - return - } - ch.awaiting[vkey] = true - ch.mu.Unlock() - ch.wg.Add(1) - go func() { - defer ch.wg.Done() - done := ch.Rebuilds.Done(ctx, builds) - ch.mu.Lock() - delete(ch.awaiting, vkey) - if done { - ch.rebuilt[vkey] = true - } - c, known := ch.known[group] - ch.mu.Unlock() - if done && known && ctx.Err() == nil { - ch.logf("%s: the builds of %s it failed on are done again, so it is checked again (ADR 0603 §10)", vkey, strings.Join(slices.Sorted(maps.Keys(builds)), ", ")) - ch.consider(ctx, group, c) - } - }() -} - -// hearFailedBuilds starts awaitRebuilt for the case's verdict when it names failed builds (failedBuilds). -func (ch *Checker) hearFailedBuilds(ctx context.Context, group string, cs *Case) { - if ch.Rebuilds == nil || ctx.Err() != nil { - return - } - value, _, found, err := ch.Verdicts.Get(ctx, cs.Key) - if err != nil || !found { - return - } - if v, err := meshdeliverycheckerclient.DecodeVerdict(value); err == nil { - if builds := failedBuilds(v, cs); builds != nil { - ch.awaitRebuilt(ctx, group, cs.Key, builds) - } - } -} - -// takeRebuilt says whether every failed build the verdict of vkey names is done again, and forgets it: one retake each. -func (ch *Checker) takeRebuilt(vkey string) bool { - ch.mu.Lock() - defer ch.mu.Unlock() - was := ch.rebuilt[vkey] - delete(ch.rebuilt, vkey) - return was -} - -// containsWords says whether words hold the given words. -func containsWords(words, part string) bool { return strings.Contains(words, part) } diff --git a/modules/mesh-delivery-checker/internal/check/checker_test.go b/modules/mesh-delivery-checker/internal/check/checker_test.go deleted file mode 100644 index 0d48110d..00000000 --- a/modules/mesh-delivery-checker/internal/check/checker_test.go +++ /dev/null @@ -1,761 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "strings" - "sync" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -const hqRepo = "novox/hq" - -const ( - repoA = "novox/mesh-delivery" - repoSDK = "novox/mesh-sdk" - head1 = "1111111111111111111111111111111111111111" - head2 = "2222222222222222222222222222222222222222" - trunkA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - trunkA2 = "abababababababababababababababababababab" - trunkS = "5555555555555555555555555555555555555555" - trunkS2 = "5656565656565656565656565656565656565656" -) - -// candidateOf is a candidate of a group with the given pull requests, as the indexer writes one. -func candidateOf(t testing.TB, group string, pulls ...meshcatalog.CandidatesByGroupValuePullsItem) []byte { - t.Helper() - heads := make([]meshdeliveryclient.Head, len(pulls)) - for i, p := range pulls { - heads[i] = meshdeliveryclient.Head{Repository: p.Repository, Number: int(p.Number), Head: p.Head} - } - hash, err := meshdeliveryclient.HeadsHash(heads) - if err != nil { - t.Fatal(err) - } - c := meshcatalog.CandidatesByGroupValue{V: keys.Version, Group: group, Heads: hash, Pulls: pulls, Fpset: meshcatalogclient.FingerprintSet(pulls)} - data, err := json.Marshal(c) - if err != nil { - t.Fatal(err) - } - return data -} - -func pullOf(repo string, number int, head string) meshcatalog.CandidatesByGroupValuePullsItem { - return meshcatalog.CandidatesByGroupValuePullsItem{Repository: repo, Number: int64(number), Head: head, Modules: []string{}} -} - -// newRepoPullOf is a pull request of a new repository of the mesh: its trunk not named yet, its first module added. -func newRepoPullOf(repo string, number int, head string) meshcatalog.CandidatesByGroupValuePullsItem { - p := pullOf(repo, number, head) - p.Modules = []string{"mod-u"} - return p -} - -// judgeFunc is a Judge written as a function; it counts its calls. -type judgeFunc struct { - mu sync.Mutex - calls int - f func(ctx context.Context, c *Case, call int) (Outcome, error) -} - -func (j *judgeFunc) Judge(ctx context.Context, c *Case) (Outcome, error) { - j.mu.Lock() - j.calls++ - n := j.calls - j.mu.Unlock() - return j.f(ctx, c, n) -} - -func (j *judgeFunc) count() int { j.mu.Lock(); defer j.mu.Unlock(); return j.calls } - -func passing() meshdeliverychecker.VerdictsByGroupHeadsValue { - return meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictPass, Levels: map[string]meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{ - buildstep.LevelUnit: {Run: 2, Kept: 3}, buildstep.LevelIntegration: {Run: 1}}} -} - -func passJudge() *judgeFunc { - return &judgeFunc{f: func(context.Context, *Case, int) (Outcome, error) { return Outcome{Verdict: passing()}, nil }} -} - -type rig struct { - ch *Checker - verdicts *memKV - owed *memKV - trunks *Trunks -} - -func newRig(t testing.TB, j Judge) *rig { - t.Helper() - r := &rig{verdicts: newMemKV(), owed: newMemKV(), trunks: &Trunks{}} - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS}) - r.ch = &Checker{Verdicts: r.verdicts, Owed: r.owed, Judge: j, Trunks: r.trunks, Link: "https://view.invalid/checks", Now: time.Now} - return r -} - -func (r *rig) setTrunks(t testing.TB, heads map[string]string) bool { - t.Helper() - return r.trunks.Set(heads) -} - -func (r *rig) verdict(t testing.TB, key string) meshdeliverychecker.VerdictsByGroupHeadsValue { - t.Helper() - raw, _, found, _ := r.verdicts.Get(context.Background(), key) - if !found { - t.Fatalf("no verdict %s; the bucket holds %v", key, r.verdicts.vals) - } - v, err := meshdeliverycheckerclient.DecodeVerdict(raw) - if err != nil { - t.Fatal(err) - } - return v -} - -func keyOf(t testing.TB, group string, value []byte) string { - t.Helper() - c, err := meshcatalog.DecodeCandidatesByGroupValue(value) - if err != nil { - t.Fatal(err) - } - k, err := meshdeliverycheckerclient.VerdictKey(group, c.Heads) - if err != nil { - t.Fatal(err) - } - return k -} - -func TestACandidateNotYetCheckedGetsItsVerdictKeyCreatedOnceAndIsJudged(t *testing.T) { // N6/AC1, 1678 - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - key := keyOf(t, "feat/x", cand) - if v := r.verdict(t, key); v.State != meshdeliverycheckerclient.VerdictPass || v.Group != "feat/x" || v.Trunk[repoA] != trunkA { - t.Fatalf("verdict = %+v", v) - } - if r.verdicts.creates[key] != 1 || j.count() != 1 { - t.Errorf("creates = %d, judged = %d, want one each", r.verdicts.creates[key], j.count()) - } - // The same candidate heard again, with nothing changed, is not judged again. - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if j.count() != 1 { - t.Errorf("judged %d times after the same candidate again, want once", j.count()) - } - // The candidate of a whole try's group is the try's: the try judges it, never the checker as a pull request's check. - r.ch.Candidate(ctx, "try-1793913602402800439", candidateOf(t, "try-1793913602402800439", pullOf(repoA, 7, head1))) - r.ch.Wait() - if j.count() != 1 { - t.Errorf("a try's candidate was judged as a check: %d judgments, want one", j.count()) - } -} - -// racyKV lets another instance create the key just before this one's create. -type racyKV struct { - *memKV - other []byte -} - -func (r racyKV) Create(ctx context.Context, key string, value []byte) (uint64, error) { - if r.other != nil { - _, _ = r.memKV.Create(ctx, key, r.other) - } - return r.memKV.Create(ctx, key, value) -} - -func TestASecondInstancesCreateIsRefusedAndItDoesNothing(t *testing.T) { // N6/AC1 - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - key := keyOf(t, "feat/x", cand) - other, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: "feat/x", Heads: strings.SplitN(key, ".", 2)[1], State: meshdeliverycheckerclient.VerdictRunning}) - r.ch.Verdicts = racyKV{r.verdicts, other} - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if j.count() != 0 { - t.Errorf("the instance that lost the create judged %d times, want none", j.count()) - } - if v := r.verdict(t, key); v.State != meshdeliverycheckerclient.VerdictRunning { - t.Errorf("the other instance's key was changed: %+v", v) - } -} - -func TestARestartWithARunningKeyJudgesItAgain(t *testing.T) { // N6/AC2 - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - key := keyOf(t, "feat/x", cand) - left, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: "feat/x", Heads: strings.SplitN(key, ".", 2)[1], Trunk: map[string]string{repoA: trunkA}, State: meshdeliverycheckerclient.VerdictRunning}) - if _, err := r.verdicts.Create(ctx, key, left); err != nil { - t.Fatal(err) - } - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if j.count() != 1 || r.verdict(t, key).State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("judged %d times, verdict %+v, want the left check judged once and passed", j.count(), r.verdict(t, key)) - } -} - -func TestACheckPastItsBoundFailsNamingTheStuckStep(t *testing.T) { // N6/AC3 - ctx := context.Background() - j := &judgeFunc{f: func(ctx context.Context, c *Case, _ int) (Outcome, error) { - c.Progress.Set("test the unit level of mesh-delivery", "shanks") - <-ctx.Done() - return Outcome{}, ctx.Err() - }} - r := newRig(t, j) - r.ch.Bound = 30 * time.Millisecond - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - v := r.verdict(t, keyOf(t, "feat/x", cand)) - if v.State != meshdeliverycheckerclient.VerdictFail || v.Failed.Machine != "shanks" || !strings.Contains(v.Failed.Words, "stuck at: test the unit level of mesh-delivery") { - t.Fatalf("verdict = %+v, want a failure naming shanks and the stuck step", v) - } -} - -// findingsKept is a FindingWriter that keeps what is written, by ID. -type findingsKept map[string]Finding - -func (f findingsKept) Put(x Finding) error { f[x.ID] = x; return nil } -func (f findingsKept) Delete(x Finding) error { delete(f, x.ID); return nil } - -// A check that could not hear a part of the mesh says `could not run`, naming the part, and releases its claim; only the -// running of that part asks it again, once per heads and part at one fingerprint; not asked again within an hour it is the -// finding check-could-not-run, which clears when the heads are checked again, replaced, or their pull request closes -// (design 47 test 58, issue 1373; issue 1912: no check fails for want of a build node). -func TestACheckThatCouldNotRunIsAskedAgainWhenItsPartRuns(t *testing.T) { // N6/AC4, 47/58 - ctx := context.Background() - unheard := Part{Node: "g14", Module: "forge"} - newCase := func(t *testing.T, fails int) (*rig, *judgeFunc, findingsKept, *time.Time) { - j := &judgeFunc{f: func(_ context.Context, _ *Case, n int) (Outcome, error) { - if n <= fails { - return Outcome{Verdict: failure("the forge", "novox/mesh-delivery#7: go.mod cannot be read: no responders (the forge was "+UnheardWords+")"), Unheard: unheard}, nil - } - return Outcome{Verdict: passing()}, nil - }} - r := newRig(t, j) - found, now := findingsKept{}, time.Date(2026, 11, 9, 20, 0, 0, 0, time.UTC) - r.ch.CouldNotRun, r.ch.Findings, r.ch.Now = newMemKV(), found, func() time.Time { return now } - return r, j, found, &now - } - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - key := keyOf(t, "feat/x", cand) - for name, run := range map[string]func(t *testing.T){ - "it says could not run, naming the part, and only its running asks it again, once per fingerprint": func(t *testing.T) { - r, j, _, _ := newCase(t, 2) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if v := r.verdict(t, key); !IsCouldNotRun(v) || !strings.Contains(v.Failed.Words, "could not run") { - t.Fatalf("verdict = %+v, want could not run", v) - } - if rec, _, ok := r.ch.couldNotRunOf(ctx, key); !ok || rec.Part != unheard || rec.AskedAt != "" { - t.Fatalf("could-not-run.%s = %+v (%v), want the part %v", key, rec, ok, unheard) - } - r.ch.Reconsider(ctx) // nothing ran: not asked again - r.ch.NodesChanged(ctx) - r.ch.PartRunning(ctx, "g14", "mail", "src2:1") // another part ran - r.ch.Wait() - if j.count() != 1 { - t.Fatalf("judged %d times before its part ran, want once", j.count()) - } - r.ch.PartRunning(ctx, "g14", "forge", "src2:1") - r.ch.Wait() - r.ch.PartRunning(ctx, "g14", "forge", "src2:1") // the same part at the same fingerprint asks nothing more - r.ch.Wait() - if j.count() != 2 { - t.Fatalf("judged %d times after its part ran twice at one fingerprint, want twice", j.count()) - } - if rec, _, _ := r.ch.couldNotRunOf(ctx, key); rec.AskedAt != runningAt("g14", "forge", "src2:1") { - t.Fatalf("asked-at = %q after it could not run again", rec.AskedAt) - } - r.ch.PartRunning(ctx, "g14", "forge", "src2:2") // a new build of the part asks once more - r.ch.Wait() - if j.count() != 3 || r.verdict(t, key).State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("judged %d times, verdict %+v, want a pass on the third", j.count(), r.verdict(t, key)) - } - if _, _, ok := r.ch.couldNotRunOf(ctx, key); ok { - t.Error("checked again, the record that it could not run is kept") - } - }, - "not asked again within the hour it is a finding, cleared when it is checked again": func(t *testing.T) { - r, _, found, now := newCase(t, 1) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - if due := r.ch.CouldNotRunDue(ctx, now.Add(59*time.Minute)); !due.Equal(now.Add(CouldNotRunBound)) || len(found) != 0 { - t.Fatalf("before its bound: due %v, findings %v", due, found) - } - if due := r.ch.CouldNotRunDue(ctx, now.Add(CouldNotRunBound)); !due.IsZero() || found[key].Kind != CouldNotRunKind || !strings.Contains(found[key].Summary, "forge on g14") { - t.Fatalf("at its bound: due %v, findings %+v, want %s naming the part", due, found, CouldNotRunKind) - } - if observed, err := r.ch.RaisedCouldNotRun(ctx); err != nil || len(observed) != 1 || observed[0].Kind != CouldNotRunKind || observed[0].ID != key { - t.Fatalf("observed at its bound = %+v, %v, want its one finding, for the finding writer's statement", observed, err) - } - r.ch.PartRunning(ctx, "g14", "forge", "src2:1") - r.ch.Wait() - if observed, _ := r.ch.RaisedCouldNotRun(ctx); len(found) != 0 || len(observed) != 0 { - t.Errorf("checked again, the finding stays: %v, observed %v", found, observed) - } - }, - "its heads replaced, or its pull request closed, the record and the finding go": func(t *testing.T) { - r, _, found, now := newCase(t, 1) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - r.ch.CouldNotRunDue(ctx, now.Add(CouldNotRunBound)) - r.ch.Candidate(ctx, "feat%2Fx", candidateOf(t, "feat/x", pullOf(repoA, 7, head2))) - r.ch.Wait() - if _, _, ok := r.ch.couldNotRunOf(ctx, key); ok || len(found) != 0 { - t.Fatalf("heads replaced: record kept %v, findings %v", ok, found) - } - r2, _, found2, now2 := newCase(t, 1) - r2.ch.Candidate(ctx, "feat%2Fx", cand) - r2.ch.Wait() - r2.ch.CouldNotRunDue(ctx, now2.Add(CouldNotRunBound)) - r2.ch.Forget("feat%2Fx") - if _, _, ok := r2.ch.couldNotRunOf(ctx, key); ok || len(found2) != 0 { - t.Fatalf("pull request closed: record kept %v, findings %v", ok, found2) - } - }, - } { - t.Run(name, run) - } -} - -// Issue 1422: a pull request of a repository the gate's source.main names no trunk head of (one never merged through the -// gate) is never dropped silently. It is not judged, its verdict says it cannot be judged and why, its status is error with -// those words, checker_status names it, and the log says it once per head. Once its trunk head is known it is judged whole; -// in a group, the members whose trunk is known are judged and the others' status says why they are not. -func TestARepositoryWithNoTrunkCannotBeJudgedLoudlyUntilItsTrunkIsKnown(t *testing.T) { // N6/AC6, ADR 0484 clause 11, 1422 - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - var logged []string - r.ch.Logf = func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) } - const unknown = "novox/unknown" - cand := candidateOf(t, "feat/y", newRepoPullOf(unknown, 4, head1)) - key := keyOf(t, "feat/y", cand) - r.ch.Candidate(ctx, "feat/y", cand) - r.ch.Wait() - r.ch.Reconsider(ctx) // looked at again: nothing is written or said twice - r.ch.NodesChanged(ctx) - r.ch.Wait() - v := r.verdict(t, key) - if j.count() != 0 || !IsUntrunked(v) || !strings.Contains(v.Failed.Words, "the trunk head of "+unknown+" is not known") { - t.Fatalf("judged %d times, verdict %+v, want none judged and a verdict that says it cannot be judged", j.count(), v) - } - if s := owedState(t, r, unknown, 4, head1); s != "error" { - t.Errorf("%s#4 owes %q, want error", unknown, s) - } - if n := countWith(logged, "cannot be judged"); n != 1 { - t.Errorf("said %d times in the log, want once for the head: %q", n, logged) - } - reader := func() Reader { - ckey, _ := meshcatalogclient.CandidatesKey("feat/y") - return Reader{CandidateState: fakeState{ckey: cand}.read(), VerdictState: fakeState(r.verdicts.vals).read(), - Trunks: func() (map[string]string, error) { return r.trunks.Heads(), nil }} - } - st, err := reader().Status() - if err != nil { - t.Fatal(err) - } - if why := st.CannotBeJudged["feat/y"]; !strings.Contains(why, unknown) || len(st.Waiting[KindNew]) != 0 || len(st.Unjudged) != 0 { - t.Errorf("cannot_be_judged %v, waiting %v, unjudged %v: want feat/y named as cannot be judged, not as new", st.CannotBeJudged, st.Waiting, st.Unjudged) - } - if rows, _, err := reader().Verdicts("feat/y"); err != nil || len(rows) != 1 || !strings.Contains(rows[0].Reports[unknown+"#4"], "cannot be judged") { - t.Errorf("checker_verdicts %+v (%v), want the report of %s#4 to say it cannot be judged", rows, err, unknown) - } - - // Its trunk head becomes known: the heads are judged whole, the same key rewritten, the status success. - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS, unknown: trunkA2}) - r.ch.TrunkMoved(ctx) - r.ch.Wait() - if v := r.verdict(t, key); j.count() != 1 || v.State != meshdeliverycheckerclient.VerdictPass || v.Trunk[unknown] != trunkA2 { - t.Errorf("judged %d times, verdict %+v, want one pass on %s", j.count(), v, trunkA2) - } - if s := owedState(t, r, unknown, 4, head1); s != "success" { - t.Errorf("%s#4 owes %q once its trunk is known, want success", unknown, s) - } - - // Issue 1733: the gate asks the Approve only on a verdict, so no candidate is left without one. A group with a member - // whose trunk is not known is not judged in part: its verdict fails naming that repository, and every status says so; a - // running verdict left for heads that cannot be judged is replaced; a verdict key deleted is judged again on its delete. - for _, row := range []struct { - name string - pulls []meshcatalog.CandidatesByGroupValuePullsItem - running bool // a running verdict is left under the key first - deleted bool // the key is deleted after its verdict - judged int - want string // the verdict's state, and for a failure the repository it names - }{ - {name: "a member the checker cannot claim", pulls: []meshcatalog.CandidatesByGroupValuePullsItem{pullOf(repoA, 9, head2), pullOf("novox/other", 2, head2)}, want: "novox/other"}, - {name: "a running verdict left for heads that cannot be judged", pulls: []meshcatalog.CandidatesByGroupValuePullsItem{newRepoPullOf(unknown, 5, head2)}, running: true, want: unknown}, - {name: "a verdict key deleted", pulls: []meshcatalog.CandidatesByGroupValuePullsItem{pullOf(repoA, 5, head2)}, deleted: true, judged: 2, want: meshdeliverycheckerclient.VerdictPass}, - } { - t.Run(row.name, func(t *testing.T) { - j := passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/z", row.pulls...) - key := keyOf(t, "feat/z", cand) - if row.running { - group, heads, _ := meshdeliverycheckerclient.ParseVerdictKey(key) - raw, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: group, Heads: heads, State: meshdeliverycheckerclient.VerdictRunning}) - _, _ = r.verdicts.Create(ctx, key, raw) - } - r.ch.Candidate(ctx, "feat%2Fz", cand) - r.ch.Wait() - if row.deleted { - _ = r.verdicts.Delete(ctx, key) - r.ch.VerdictDeleted(ctx, key) // the watch of the verdicts hands the delete over - r.ch.Wait() - } - v := r.verdict(t, key) - if row.want == meshdeliverycheckerclient.VerdictPass { - if v.State != meshdeliverycheckerclient.VerdictPass || j.count() != row.judged { - t.Errorf("verdict %s after %d judgings, want a pass after %d", v.State, j.count(), row.judged) - } - return - } - if !IsUntrunked(v) || !strings.Contains(v.Failed.Words, row.want) || j.count() != 0 { - t.Errorf("verdict %+v after %d judgings, want a failure naming %s and none judged", v, j.count(), row.want) - } - for _, p := range row.pulls { - if s := owedState(t, r, p.Repository, int(p.Number), p.Head); s != "error" { - t.Errorf("%s#%d owes %q, want error", p.Repository, p.Number, s) - } - } - }) - } - - // ADR 0664 §5 (issue 1748): a candidate taken out (its pull request merged or closed) ends the check running for it with a - // cause its asks' wait names, never a plain cancel, so its asker drops the asks it put on the build nodes. - t.Run("a candidate taken out ends its check with a named cause", func(t *testing.T) { - ch := &Checker{} - ch.init() - run, end := context.WithCancelCause(context.Background()) - ch.live["g.heads"] = liveCheck{group: "g", end: end} - // The check's asker is waiting on an ask of its run when the candidate goes (review 1, finding 7). - drops := &checkDrops{} - fake := &buildrun.FakeAsks{} - done := make(chan struct{}) - go func() { - defer close(done) - (buildrun.Asker{Seats: testSeats(), Publisher: fake, Waiter: fake, Bound: time.Hour, Dropper: drops}).Wait(run, buildstep.StepAsk{ID: "mvl0frv18d2000", Language: buildstep.AskGo, Kind: buildstep.AskCheck}) - }() - ch.Forget("g") - <-done - var s superseded - if cause := context.Cause(run); !errors.As(cause, &s) || s.by != "" || errors.Is(cause, context.Canceled) { - t.Errorf("the check was ended with %v, want superseded with no heads, not a plain cancel", cause) - } - if len(drops.got) != 1 || !strings.Contains(drops.got[0], "mvl0frv18d2000") || !strings.Contains(drops.got[0], "no candidate holds these heads") { - t.Errorf("the asker dropped %q, want its ask withdrawn with the check's cause", drops.got) - } - }) -} - -// checkDrops keeps the asks an asker dropped: " ". -type checkDrops struct{ got []string } - -func (d *checkDrops) DropAsk(_ context.Context, a buildstep.StepAsk, why string) error { - d.got = append(d.got, a.ID+" "+why) - return nil -} - -func countWith(lines []string, words string) int { - n := 0 - for _, l := range lines { - if strings.Contains(l, words) { - n++ - } - } - return n -} - -// 1887/A10, ADR 0527 §2: hq is judged as any repository, its checks Go tests, and its pull request gets a verdict and a -// mesh/check; in a group, hq's member is judged with the others. -func TestAnHqPullRequestGetsAVerdictAndAStatus(t *testing.T) { - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS, hqRepo: trunkA2}) - r.ch.Candidate(ctx, "feat/hq", candidateOf(t, "feat/hq", pullOf(hqRepo, 3, head1))) - r.ch.Wait() - if j.count() != 1 || len(r.verdicts.vals) != 1 { - t.Fatalf("judged %d times, %d verdicts, want one", j.count(), len(r.verdicts.vals)) - } - if s := owedState(t, r, hqRepo, 3, head1); s != "success" { - t.Errorf("hq#3 owes %q, want success", s) - } - r.ch.Candidate(ctx, "feat/x", candidateOf(t, "feat/x", pullOf(hqRepo, 4, head2), pullOf(repoA, 7, head2))) - r.ch.Wait() - if s := owedState(t, r, hqRepo, 4, head2); s != "success" { - t.Errorf("hq#4 in a group owes %q, want success", s) - } -} - -func TestATrunkMoveRewritesTheSameVerdictKeyAndTheHeadsStatus(t *testing.T) { // 1300/AC7 - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - key := keyOf(t, "feat/x", cand) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - _, rev1, _, _ := r.verdicts.Get(ctx, key) - owedKey, _ := CheckerOwedKey(repoA, 7, head1) - _, orev1, _, _ := r.owed.Get(ctx, owedKey) - if !r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) { - t.Fatal("the trunks did not change") - } - r.ch.Reconsider(ctx) - r.ch.Wait() - _, rev2, _, _ := r.verdicts.Get(ctx, key) - if j.count() != 2 || rev2 <= rev1 || r.verdict(t, key).Trunk[repoA] != trunkA2 { - t.Fatalf("judged %d times, revisions %d then %d, trunk %v", j.count(), rev1, rev2, r.verdict(t, key).Trunk) - } - if r.verdicts.creates[key] != 1 { - t.Errorf("the key was created %d times, want the same key rewritten", r.verdicts.creates[key]) - } - if _, orev2, _, _ := r.owed.Get(ctx, owedKey); orev2 <= orev1 { - // the line is the same when nothing differs; the status is rewritten when the words change, here the trunk is in them - t.Logf("the status keeps revision %d (its words did not change)", orev2) - } - // A new head is another key, and the older head's status is not touched. - older, _, _, _ := r.owed.Get(ctx, owedKey) - cand2 := candidateOf(t, "feat/x", pullOf(repoA, 7, head2)) - r.ch.Candidate(ctx, "feat%2Fx", cand2) - r.ch.Wait() - if again, _, _, _ := r.owed.Get(ctx, owedKey); string(again) != string(older) { - t.Errorf("the older head's status was rewritten") - } - if n := len(r.owed.vals); n != 2 { - t.Errorf("%d statuses, want one for each head", n) - } -} - -func TestTheStatusIsPendingWhileTheCheckRunsAndEndsSuccessOrFailure(t *testing.T) { // 1300/AC5 (unit part) - ctx := context.Background() - var seen []string - var r *rig - j := &judgeFunc{f: func(ctx context.Context, c *Case, _ int) (Outcome, error) { - key, _ := CheckerOwedKey(repoA, 7, head1) - raw, _, _, _ := r.owed.Get(ctx, key) - o, _ := DecodeCheckerOwed(raw) - seen = append(seen, o.State) - c.series(repoA+"#7", []string{"web/pkg series 0.1 → 0.2"}, []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem{{Module: "web", Artifact: "pkg", From: "0.1", To: "0.2"}}) - return Outcome{Verdict: meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Test: "m/a.TestX"}}, - OutOfStep: map[string][]string{repoA + "#7": {"go/seats/x/contract.go"}}}, nil - }} - r = newRig(t, j) - says := &verdictsSaid{} - r.ch.Says = says - r.ch.Candidate(ctx, "feat/x", candidateOf(t, "feat/x", pullOf(repoA, 7, head1))) - r.ch.Wait() - key, _ := CheckerOwedKey(repoA, 7, head1) - raw, _, _, _ := r.owed.Get(ctx, key) - o, err := DecodeCheckerOwed(raw) - if err != nil { - t.Fatal(err) - } - if len(seen) != 1 || seen[0] != "pending" || o.State != "failure" || o.Context != meshdeliveryclient.StatusContextCheck || !strings.HasPrefix(o.Target, "https://view.invalid/checks/") { - t.Errorf("while running %v, at the end %+v", seen, o) - } - // DLV-C2 (design 26 *Out of step*): the check's end says one verdict event for the pull request, naming its out-of-step - // files; the running status says none. - if len(says.said) != 1 || says.said[0].Repository != repoA || says.said[0].Pull != 7 || says.said[0].Head != head1 || - says.said[0].Verdict != meshdeliverycheckerclient.VerdictFail || strings.Join(says.said[0].OutOfStep, ",") != "go/seats/x/contract.go" || - // The join (A2j finding 2): and its series changes, as the seat's event names them. - len(says.said[0].Series) != 1 || says.said[0].Series[0] != (meshdeliverychecker.VerdictHolderEventSeriesItem{Module: "web", Artifact: "pkg", From: "0.1", To: "0.2"}) { - t.Errorf("verdict events %+v", says.said) - } -} - -// verdictsSaid is a VerdictSayer that keeps what it was told. -type verdictsSaid struct { - mu sync.Mutex - said []VerdictEvent -} - -func (s *verdictsSaid) Say(_ context.Context, v VerdictEvent) error { - s.mu.Lock() - defer s.mu.Unlock() - s.said = append(s.said, v) - return nil -} - -// No check of heads no candidate holds is left running (issue 1890): at start the sweep ends a key left running; while the -// checker runs, a check whose group moved to other heads is ended at once, unjudged, and writes no status; heads held again -// are checked whole; and a group's check starts only once its heads settled, so a partial set of members is never checked. -func TestHeadsNoCandidateHoldsAreNeverLeftRunning(t *testing.T) { - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - live := keyOf(t, "feat/x", cand) - dead, _ := meshdeliverycheckerclient.VerdictKey("feat/gone", "0123456789ab") - for _, k := range []string{live, dead} { - group, heads, _ := meshdeliverycheckerclient.ParseVerdictKey(k) - raw, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: group, Heads: heads, State: meshdeliverycheckerclient.VerdictRunning}) - _, _ = r.verdicts.Create(ctx, k, raw) - } - r.ch.init() - var c meshcatalog.CandidatesByGroupValue - _ = json.Unmarshal(cand, &c) - r.ch.known["feat%2Fx"] = c - if err := r.ch.Sweep(ctx); err != nil { - t.Fatal(err) - } - if v := r.verdict(t, dead); !IsEnded(v) || !strings.Contains(v.Failed.Words, "closed at start") { - t.Errorf("the dead key = %+v", v) - } - if v := r.verdict(t, live); v.State != meshdeliverycheckerclient.VerdictRunning { - t.Errorf("the live key = %+v, want it left for the check to take over", v) - } - - // A member joins while the check of the lone member's heads runs: that check is ended, its judge's run cancelled. - started, ended := make(chan struct{}), make(chan error, 1) - j = &judgeFunc{f: func(run context.Context, _ *Case, call int) (Outcome, error) { - if call == 1 { - close(started) - <-run.Done() - ended <- context.Cause(run) - return Outcome{}, run.Err() - } - return Outcome{Verdict: passing()}, nil - }} - r = newRig(t, j) - var logged []string - var logMu sync.Mutex - r.ch.Logf = func(f string, a ...any) { logMu.Lock(); logged = append(logged, fmt.Sprintf(f, a...)); logMu.Unlock() } - alone, both := cand, candidateOf(t, "feat/x", pullOf(repoA, 7, head1), pullOf(repoSDK, 3, head2)) - r.ch.Candidate(ctx, "feat%2Fx", alone) - <-started - r.ch.Candidate(ctx, "feat%2Fx", both) - var bothC meshcatalog.CandidatesByGroupValue - _ = json.Unmarshal(both, &bothC) - select { - case err := <-ended: - if err == nil || !strings.Contains(err.Error(), "superseded by "+GateHeads(bothC)) { - t.Errorf("the old check's run ended with %v", err) - } - case <-time.After(5 * time.Second): - t.Fatal("the check of the heads the candidate left still runs") - } - r.ch.Wait() - if v := r.verdict(t, live); !IsEnded(v) || v.Failed.Words != EndedWords+"superseded by "+GateHeads(bothC) { - t.Errorf("the superseded verdict = %+v", v.Failed) - } - if v := r.verdict(t, keyOf(t, "feat/x", both)); v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("the group's verdict = %s", v.State) - } - okey, _ := CheckerOwedKey(repoA, 7, head1) - if raw, _, _, _ := r.owed.Get(ctx, okey); !strings.Contains(string(raw), `"success"`) { - t.Errorf("the head's status = %s, want the group's success, never the ended check's", raw) - } - logMu.Lock() - said := 0 - for _, l := range logged { - if strings.Contains(l, "ended: superseded") { - said++ - } - } - logMu.Unlock() - if said != 1 { - t.Errorf("the end was said %d times: %v", said, logged) - } - // The member leaves again: the ended heads are held again and checked whole. - r.ch.Candidate(ctx, "feat%2Fx", alone) - r.ch.Wait() - if v := r.verdict(t, live); v.State != meshdeliverycheckerclient.VerdictPass || j.count() != 3 { - t.Errorf("the heads held again = %s after %d judgings, want a pass of a third", v.State, j.count()) - } - - // Members announced one after another: only the settled, whole set is checked. - j = passJudge() - r = newRig(t, j) - r.ch.Settle = time.Hour - sdk := candidateOf(t, "feat/y", pullOf(repoSDK, 4, head2)) - whole := candidateOf(t, "feat/y", pullOf(repoSDK, 4, head2), pullOf(repoA, 8, head1)) - r.ch.Candidate(ctx, "feat%2Fy", sdk) - r.ch.Candidate(ctx, "feat%2Fy", whole) - r.ch.Reconsider(ctx) // a trunk move or a turn while the heads settle starts nothing - if j.count() != 0 { - t.Fatalf("judged %d times before the heads settled", j.count()) - } - r.ch.settleNow() - r.ch.Wait() - if _, _, found, _ := r.verdicts.Get(ctx, keyOf(t, "feat/y", sdk)); found || j.count() != 1 { - t.Errorf("the partial set has a verdict (%v) or the judge ran %d times, want the whole set alone", found, j.count()) - } - if v := r.verdict(t, keyOf(t, "feat/y", whole)); v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("the whole set = %s", v.State) - } -} - -func TestTheForgeBridgeIsToldOnceForEachChangeOfAStatus(t *testing.T) { // 1683 (the checker's half of AC5) - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - var told []string - r.ch.Announce = func(_ context.Context, event, key string) { told = append(told, event+" "+key) } - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - key, _ := CheckerOwedKey(repoA, 7, head1) - want := []string{EventOwedWritten + " " + key, EventOwedWritten + " " + key} // pending, then success - if len(told) != 2 || told[0] != want[0] || told[1] != want[1] { - t.Fatalf("told %v, want the status said when pending and when success", told) - } - r.ch.Candidate(ctx, "feat%2Fx", cand) // nothing changed: nothing written, nothing said - r.ch.Wait() - if len(told) != 2 { - t.Errorf("told %v after a check that changed nothing", told) - } -} - -// Sweep 2026-10-30 item 7: a pull request of a repository the mesh does not deliver (an operator's own, on the same -// forge) touches no module and its trunk the gate never names. It is outside the mesh: no verdict, no status owed (one -// owed before is taken away, so the forge module stops writing it into a repository the mesh may not write), said once. -func TestACandidateOutsideTheMeshIsJudgedNothingAndOwesNothing(t *testing.T) { - ctx, j := context.Background(), passJudge() - r := newRig(t, j) - var logged []string - r.ch.Logf = func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) } - const theirs = "someone/own-app" - okey, _ := CheckerOwedKey(theirs, 55, head1) - before, _ := EncodeCheckerOwed(meshdeliverychecker.OwedByRepositoryNumberHeadActValue{Act: meshdeliveryclient.OwedStatus, Context: meshdeliveryclient.StatusContextCheck, State: "error", Text: "cannot be judged"}) - if _, err := r.owed.Create(ctx, okey, before); err != nil { - t.Fatal(err) - } - cand := candidateOf(t, "feat/theirs", pullOf(theirs, 55, head1)) - key := keyOf(t, "feat/theirs", cand) - r.ch.Candidate(ctx, "feat/theirs", cand) - r.ch.Wait() - r.ch.Reconsider(ctx) - r.ch.Wait() - if _, _, found, _ := r.verdicts.Get(ctx, key); found || j.count() != 0 { - t.Errorf("a candidate outside the mesh was judged (%d) or given a verdict (%v)", j.count(), found) - } - if s := owedState(t, r, theirs, 55, head1); s != "" { - t.Errorf("%s#55 still owes %q on the forge", theirs, s) - } - if n := countWith(logged, "outside the mesh"); n != 1 { - t.Errorf("said %d times in the log, want once: %q", n, logged) - } - ckey, _ := meshcatalogclient.CandidatesKey("feat/theirs") - st, err := Reader{CandidateState: fakeState{ckey: cand}.read(), VerdictState: fakeState(r.verdicts.vals).read(), - Trunks: func() (map[string]string, error) { return r.trunks.Heads(), nil }}.Status() - if err != nil { - t.Fatal(err) - } - if st.Outside["feat/theirs"] == "" || len(st.CannotBeJudged) != 0 || len(st.Unjudged) != 0 { - t.Errorf("status: outside %v, cannot be judged %v, unjudged %v", st.Outside, st.CannotBeJudged, st.Unjudged) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/composition_test.go b/modules/mesh-delivery-checker/internal/check/composition_test.go deleted file mode 100644 index 56f4f5ad..00000000 --- a/modules/mesh-delivery-checker/internal/check/composition_test.go +++ /dev/null @@ -1,344 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "maps" - "mesh-delivery-checker/rules" - "slices" - "strings" - "testing" - - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - - buildsrec "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// fakeCompositions answers a check's composition from memory, recording the keys asked. -type fakeCompositions struct { - v rules.Composed - err error - asked []string - // seats are the seat lists the cases asked carried (Case.Seats), one per ask. - seats [][]json.RawMessage - // waits answers only when its context ends. - waits bool -} - -func (f *fakeCompositions) Compose(ctx context.Context, c *Case) (rules.Composed, error) { - f.asked = append(f.asked, c.Key) - f.seats = append(f.seats, c.Seats) - if f.waits { - <-ctx.Done() - return rules.Composed{}, ctx.Err() - } - return f.v, f.err -} - -// composedOn is the engines' answers of a composition by composer on the nodes, each composing it, taking took ms. -func composedOn(composer string, took int64, nodes ...string) rules.Composed { - c := rules.Composed{Group: "feat/x", Heads: "0123456789ab", Composer: composer, Nodes: nodes, Took: took, Answers: map[string]ne.ComposeCandidateAnswer{}} - for _, n := range nodes { - c.Answers[n] = ne.ComposeCandidateAnswer{Node: n, Verdict: nec.Composed} - } - return c -} - -// refusedOn is the engines' answers of a composition the node refuses with the words. -func refusedOn(composer, node, words string) rules.Composed { - return rules.Composed{Group: "feat/x", Heads: "0123456789ab", Composer: composer, Nodes: []string{node}, - Answers: map[string]ne.ComposeCandidateAnswer{node: {Node: node, Verdict: nec.Refused, Refused: words}}} -} - -// 1297/AC3 (its unit half): a check whose tests pass takes the assigner's composition of it, by the check's own key, -// into its verdict: a pass says it passed with the mesh-sdk and machines it composed, and is no longer "not run". -func TestAPassingCheckTakesTheAssignersComposition(t *testing.T) { - t.Run("a change to a build module or base names the modules built on it", aChangeToABuildModuleOrBaseNamesTheModulesBuiltOnIt) - b := newBench(t) - f := &fakeCompositions{v: composedOn("v0.1.14-x", 40, "g14")} - b.ex.Compositions = f - cs := caseOf(t, pullOf(repoA, 7, head1)) - cs.FPSet = "fps-of-the-candidate" - v := b.judge(t, cs).Verdict - if v.State != meshdeliverycheckerclient.VerdictPass || v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionPass || v.Composition.Sdk != "v0.1.14-x" || v.Took.Composition != 40 { - t.Fatalf("verdict = %+v, composition %+v", v, v.Composition) - } - if len(f.asked) != 1 || f.asked[0] != cs.Key { - t.Errorf("asked %v, want the check's key %s", f.asked, cs.Key) - } - if !compositionMentions(v, "passed") { - t.Errorf("the line says %q", compositionWords(v.Composition, v.Took.Composition)) - } - if v.Composition.Fpset == "" || v.Composition.Fpset != cs.FPSet { - t.Errorf("the composition names the fingerprint set %q, want the candidate's %q (ADR 0548 §5)", v.Composition.Fpset, cs.FPSet) - } - // Issue 1966 (the group try of 2026-11-06 10:07): the composition is handed the check's seat list, the judge's, once the - // checker's reader read it; a list not read hands none (the composer then judges no machine, ADR 0636 §1). - if len(f.seats) != 1 || f.seats[0] != nil { - t.Errorf("with no seat list reader the composition was handed %v, want none", f.seats) - } - for _, read := range []bool{true, false} { - b.ex.SeatList = fakeSeatList{read: read} - asked := len(f.seats) - rv := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if read { - if got := f.seats[len(f.seats)-1]; len(got) != 2 || string(got[0]) != `{"seat":"a"}` { - t.Errorf("with the list read the composition was handed %s, want the reader's two definitions", got) - } - } else if len(f.seats) != asked || rv.Failed == nil || !strings.Contains(rv.Failed.Words, "the seat list is not read") { - // A reader that has not read the list acts on nothing new (ADR 0636 §1): the check ends before any judge. - t.Errorf("with the list not read the composition was asked %d more, verdict %+v; want none and a fail naming the list", len(f.seats)-asked, rv.Failed) - } - } - // ADR 0635 §3: at a node-engine member's head a list not read fails the check, naming why, and asks no composer - // (a composer handed none would judge no machine and the composition would pass); read, it composes. - for _, read := range []bool{false, true} { - b.ex.SeatList = fakeSeatList{read: read} - asked := len(f.asked) - hv := b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(nodeEngineRepo, 3, head2))).Verdict - switch { - case !read && (hv.State != meshdeliverycheckerclient.VerdictFail || hv.Failed == nil || !strings.Contains(hv.Failed.Words, "seat list") || len(f.asked) != asked): - t.Errorf("at a node-engine head with the list not read: verdict %+v, composition %+v, asked %d more; want a failed composition naming the seat list, no ask", hv, hv.Composition, len(f.asked)-asked) - case read && (hv.Composition == nil || hv.Composition.State != meshdeliverycheckerclient.CompositionPass || len(f.asked) != asked+1): - t.Errorf("at a node-engine head with the list read: composition %+v, want the composer's pass", hv.Composition) - } - } - b.ex.SeatList = nil - f.asked, f.seats = f.asked[:1], f.seats[:1] - // ADR 0548 §5: a carry whose fingerprints did not change keeps the composition it passed and asks the assigner nothing. - carry := caseOf(t, pullOf(repoA, 7, head1)) - carry.CarriedFrom, carry.Composition = map[string]string{repoA: trunkA2}, v.Composition - if kept := b.judge(t, carry).Verdict; kept.Composition == nil || kept.Composition.Sdk != "v0.1.14-x" || len(f.asked) != 1 { - t.Errorf("the carry's composition is %+v and the assigner was asked %d times; want the kept one and no second ask", kept.Composition, len(f.asked)) - } -} - -func compositionMentions(v meshdeliverychecker.VerdictsByGroupHeadsValue, word string) bool { - return strings.Contains(compositionWords(v.Composition, v.Took.Composition), word) -} - -// 1297/AC2 at the checker: a failed composition fails the check, naming the machine it failed at and its words. -func TestAFailedCompositionFailsTheCheck(t *testing.T) { - b := newBench(t) - b.ex.Compositions = &fakeCompositions{v: refusedOn("x", "g14", "g14 runs web, which the change removes from its source")} - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if v.State != meshdeliverycheckerclient.VerdictFail || v.Failed.Machine != "g14" || v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionFail { - t.Fatalf("verdict = %+v", v) - } - v.Group, v.Heads = "feat/x", "0123456789ab" // the checker names them when it writes the verdict - if _, err := meshdeliverycheckerclient.EncodeVerdict(v); err != nil { - t.Errorf("the verdict cannot be written: %v", err) - } -} - -// 1297/B3d: a composition that cannot be read leaves it not run; a try is not composed. -func TestACompositionNotHadIsNotRun(t *testing.T) { - b := newBench(t) - f := &fakeCompositions{err: errors.New("the check's bound passed")} - b.ex.Compositions = f - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if v.State != meshdeliverycheckerclient.VerdictPass || v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionNotRun { - t.Fatalf("verdict = %+v", v) - } - b2 := newBench(t) - f2 := &fakeCompositions{v: refusedOn("x", "g14", "w")} - b2.ex.Compositions = f2 - cs := caseOf(t, pullOf(repoA, 7, head1)) - cs.Try = &TryOptions{} - b2.judge(t, cs) - if len(f2.asked) != 0 { - t.Errorf("a try asked for a composition: %v", f2.asked) - } -} - -// fixedTrunks are trunk heads that never move. -type fixedTrunks struct{ heads map[string]string } - -func (f fixedTrunks) Heads() map[string]string { return f.heads } - -// fakeEnds answers the builder's builds of a candidate as fakeBuilds holds them, recording the builds waited on; the -// composition must not have been asked yet when it is (ADR 0603 §10: the check composes with the builds). -type fakeEnds struct { - builds fakeBuilds - asked map[string]string - composed *fakeCompositions - early bool -} - -func (f *fakeEnds) Ended(_ context.Context, asked map[string]string) (map[string]buildsrec.BuilderBuild, error) { - f.asked = asked - f.early = f.composed != nil && len(f.composed.asked) > 0 - out := map[string]buildsrec.BuilderBuild{} - for module, fp := range asked { - if b, ok := f.builds[module+" "+fp]; ok && (b.State == buildsrec.BuildDone || b.State == buildsrec.BuildFailed) { - out[module] = b - } - } - return out, nil -} - -func (f *fakeEnds) Done(context.Context, map[string]string) bool { return false } - -// fixedServing are the verbs the running builds serve. -type fixedServing []string - -func (f fixedServing) Served(context.Context) (func(string) bool, error) { - return func(v string) bool { return slices.Contains(f, v) }, nil -} - -// knownRuns are the modules the trunk holds a record of (the indexer's modules): any commit. -type knownRuns []string - -func (k knownRuns) CommitOf(module string) string { - if slices.Contains(k, module) { - return trunkA - } - return "" -} - -// ADR 0603 §10: before it composes, the check waits on the builder's builds of the candidate's changed modules and composes -// with them; a failed build fails the check naming it, and one not ended within the bound is named. It does not wait on a -// module built by a build module the change alters, nor on one standing on a base the change alters, nor on one the trunk -// holds no record of, and names each in the composition's warnings (and so the check's line); a change that alters none of -// these says nothing. -func aChangeToABuildModuleOrBaseNamesTheModulesBuiltOnIt(t *testing.T) { - dig := func(c string) string { return strings.Repeat(c, 64) } - fp := "src2:" + dig("f") - goBuild := &catalogue.Build{Artifacts: []catalogue.Artifact{{Name: "bin", Kind: catalogue.ArtifactBundle, Language: "go"}}} - tsBuild := &catalogue.Build{Artifacts: []catalogue.Artifact{{Name: "app", Kind: catalogue.ArtifactBundle, Language: "typescript"}}} - done := buildsrec.BuilderBuild{State: buildsrec.BuildDone} - for _, row := range []struct { - name string - manifest map[string]catalogue.Manifest - builds map[string]buildsrec.BuilderBuild // by module, at fp; nil leaves the check without the builds' ends - runs knownRuns // nil: the trunk holds every module - want []string - waited string // the modules waited on, sorted - failed string // the failure's test, when the check fails on a build - tests bool // the tests fail: the check fails before it would wait - served []string // what the running builds serve (ADR 0614 §7); nil reads nothing of what runs - preview bool // the check began on the candidate the indexer wrote before its merge preview (issue 1921) - late bool // a build did not end within the bound: the change is not composed (issue 1921) - }{ - {name: "the Go build module invokes a verb no running build serves", - manifest: map[string]catalogue.Manifest{"mesh-build-go": {Module: "mesh-build-go", Build: goBuild, - Invokes: []string{"seat:package-registry.publish", "seat:artifact-store.put"}}, "web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"mesh-build-go": done}, served: []string{"seat:artifact-store.put"}, - want: []string{"the changed build module mesh-build-go invokes seat:package-registry.publish, which no running build serves, " + - "so the check cannot run it: web, which it builds, are composed from their manifests (ADR 0614 §7)"}, waited: "mesh-build-go"}, - {name: "the Go build module changes", - manifest: map[string]catalogue.Manifest{"mesh-build-go": {Module: "mesh-build-go", Build: goBuild}, "web": {Module: "web", Build: goBuild}, - "mail": {Module: "mail", Build: goBuild}, "docs": {Module: "docs"}}, - builds: map[string]buildsrec.BuilderBuild{"mesh-build-go": done, "docs": done}, - want: []string{"the build module mesh-build-go: mail, web, which it builds"}, waited: "docs, mesh-build-go"}, - {name: "the Node build module changes", - manifest: map[string]catalogue.Manifest{"mesh-build-node": {Module: "mesh-build-node", Build: goBuild}, "panel": {Module: "panel", Build: tsBuild}, - "web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"mesh-build-node": done, "web": done}, - want: []string{"the build module mesh-build-node: panel, which it builds"}, waited: "mesh-build-node, web"}, - {name: "a base changes", - manifest: map[string]catalogue.Manifest{"image": {Module: "image"}, - "web": {Module: "web", Build: &catalogue.Build{On: []catalogue.BuildsOn{{Arg: "BASE", Module: "image", Artifact: "img"}}}}}, - builds: map[string]buildsrec.BuilderBuild{"image": done}, - want: []string{"alters image, a base of web", "waits on the build of image alone"}, waited: "image"}, - {name: "neither changes", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": done}, waited: "web"}, - {name: "a build fails", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}, "mail": {Module: "mail", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": {State: buildsrec.BuildFailed, Error: "go build: undefined: x"}, "mail": done}, - waited: "mail, web", failed: "build: web"}, - {name: "the tests fail", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": {State: buildsrec.BuildBuilding}}, tests: true, - want: []string{"composes at once, without waiting on the builds of web"}}, - {name: "a build does not end within the bound", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": {State: buildsrec.BuildBuilding}}, late: true, - want: []string{"the builder's builds of web did not end within the build-wait-bound", "the change is not composed"}, waited: "web"}, - {name: "the check began before the merge preview", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": done}, preview: true, waited: "web"}, - {name: "a module the trunk does not hold", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}, "new": {Module: "new", Build: goBuild}}, - builds: map[string]buildsrec.BuilderBuild{"web": done}, runs: knownRuns{"web"}, - want: []string{"new: the trunk holds no record of it yet"}, waited: "web"}, - {name: "the builds cannot be heard", manifest: map[string]catalogue.Manifest{"web": {Module: "web", Build: goBuild}}, - want: []string{"the builds of web cannot be heard"}}, - } { - b := candidateBench(t, true) - if row.tests { - b.test = func(c stepCall) (any, error) { - r := captured[Tested](t, "go-test-failing") - r.Level, _ = c.args["level"].(string) - return r, nil - } - } - comps := &fakeCompositions{v: composedOn("x", 0, "g14")} - b.ex.Compositions = comps - var ends *fakeEnds - if row.builds != nil { - ends = &fakeEnds{builds: fakeBuilds{}, composed: comps} - for module, build := range row.builds { - ends.builds[module+" "+fp] = build - } - b.ex.BuildEnds = ends - } - if row.runs != nil { - b.ex.Runs = row.runs - } - if row.served != nil { - b.ex.Serving = fixedServing(row.served) - } - read, fps := fixedManifests{}, map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{} - for module, m := range row.manifest { - d := dig(string(rune('a' + len(read)))) - read[d], fps[module] = m, meshcatalog.CandidatesByGroupValuePullsItemFpsValue{Fp2: fp, Manifest: d} - } - b.ex.Read = read - p := pullOf(catalogueRepository, 9, head1) - p.Fps, p.Modules = fps, slices.Sorted(maps.Keys(fps)) - if row.preview { - previewed := p - b.ex.Candidates = &fakeTryCandidates{cand: meshcatalog.CandidatesByGroupValue{Fpset: "fps", Pulls: []meshcatalog.CandidatesByGroupValuePullsItem{previewed}}} - p.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{} - for module, m := range fps { - p.Fps[module] = meshcatalog.CandidatesByGroupValuePullsItemFpsValue{Manifest: m.Manifest, None2: meshcatalogclient.NoneNoPreview} - } - } - v := b.judge(t, caseOf(t, p)).Verdict - asks := 1 - if row.late { - asks = 0 // never asked: an engine would read the builder's lateness as the change's refusal - } - if v.Composition == nil || len(comps.asked) != asks || row.late && v.Composition.State != meshdeliverycheckerclient.CompositionNotRun { - t.Fatalf("%s: composed %d times: %+v", row.name, len(comps.asked), v) - } - if ends != nil { - if got := strings.Join(slices.Sorted(maps.Keys(ends.asked)), ", "); got != row.waited || ends.early { - t.Errorf("%s: waited on %q (after the composition: %v), want %q before it composes", row.name, got, ends.early, row.waited) - } - } - switch { - case row.failed != "" && (v.State != meshdeliverycheckerclient.VerdictFail || v.Failed.Test != row.failed || !strings.Contains(v.Failed.Words, "undefined: x") || - !strings.Contains(v.Failed.Words, "the check asks itself again when the build is done")): - t.Errorf("%s: the verdict is %s failing %+v, want it failed naming %s, the builder's words and how it is asked again", row.name, v.State, v.Failed, row.failed) - case row.tests && v.State != meshdeliverycheckerclient.VerdictFail: - t.Errorf("%s: %+v, want the tests' failure", row.name, v) - case row.failed == "" && !row.tests && v.State != meshdeliverycheckerclient.VerdictPass: - t.Errorf("%s: %+v", row.name, v) - } - words := compositionWords(v.Composition, v.Took.Composition) - for _, w := range row.want { - if !strings.Contains(words, w) { - t.Errorf("%s: the line says %q, want it to name %q", row.name, words, w) - } - } - if row.want == nil && v.Composition.Words != "" { - t.Errorf("%s: warned %q though the check waited on every build of the change", row.name, v.Composition.Words) - } - } -} - -func ptr[T any](v T) *T { return &v } diff --git a/modules/mesh-delivery-checker/internal/check/couldnotrun.go b/modules/mesh-delivery-checker/internal/check/couldnotrun.go deleted file mode 100644 index 067c42c5..00000000 --- a/modules/mesh-delivery-checker/internal/check/couldnotrun.go +++ /dev/null @@ -1,275 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "sort" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// A check that failed because a part of the mesh was not heard says `could not run`, naming the part, and releases its -// claim on the verdict key (design 47, *A check runs to its end*; issue 1373). It is asked again once per set of heads and -// per part returning, on the one event that can change its outcome: the node-engine seat's `running..` -// naming that part (PartRunning), never on a timer; a `running` of the same part at the same fingerprint after that asks -// nothing. One not asked again within CouldNotRunBound becomes the checker's finding CouldNotRunKind, which clears when the -// heads are checked again, replaced, or their pull request closes. -// -// What the checker knows of each such check is its own state could-not-run (CouldNotRunState), one key per verdict key, -// read by checker_status (could_not_run): kept on the bus, so a checker started again asks each one again on the next -// `running` and raises its finding at the same time. - -// CouldNotRunState is the checker's own state of its checks that could not run, by verdict key. -const CouldNotRunState = "could-not-run" - -// CouldNotRunKind is the checker's finding of a check that could not run and was not asked again within CouldNotRunBound. -const CouldNotRunKind = "check-could-not-run" - -// CouldNotRunBound is how long a check that could not run waits to be asked again before it is a finding: the check's own -// bound of an hour. -const CouldNotRunBound = time.Hour - -// Part is a part of the mesh a check could not hear: the node it runs on and its module, as the node-engine seat's -// `running..` names it. An empty field is not known: an empty Module is any part on Node, and an empty Node is -// Module wherever it runs; a Part with neither is a part the check could not name, asked again on the first `running`. -type Part struct { - Node string `json:"node,omitempty"` - Module string `json:"module,omitempty"` -} - -// String names the part as a verdict says it. -func (p Part) String() string { - switch { - case p.Node != "" && p.Module != "": - return p.Module + " on " + p.Node - case p.Module != "": - return p.Module - case p.Node != "": - return "the node " + p.Node - } - return "a part of the mesh" -} - -// matches says whether a `running` of module on node is the part's return. -func (p Part) matches(node, module string) bool { - return (p.Node == "" || p.Node == node) && (p.Module == "" || p.Module == module) -} - -// CouldNotRun is one check that could not run, as the state could-not-run keeps it. -type CouldNotRun struct { - V int `json:"v"` - Group string `json:"group"` - Heads string `json:"heads"` - Part Part `json:"part"` - Words string `json:"words"` - // Since is when its verdict was written; CouldNotRunBound after it, not asked again, it is a finding. - Since time.Time `json:"since"` - // AskedAt is the `running` that asked the check of these heads again, `.@`: a `running` of - // the same part at the same fingerprint asks nothing more. - AskedAt string `json:"asked-at,omitempty"` - // Raised says its finding is written. - Raised bool `json:"raised,omitempty"` -} - -// IsCouldNotRun says whether a verdict failed because a part of the mesh the check reads was not heard (UnheardWords): its -// claim is released, and it is asked again when that part runs (PartRunning). -func IsCouldNotRun(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && containsWords(failedOf(v).Words, UnheardWords) -} - -// running is how a `running` event is kept in AskedAt and in the release of a check. -func runningAt(node, module, fingerprint string) string { - return node + "." + module + "@" + fingerprint -} - -// PartRunning is the node-engine seat's `running..` at fingerprint: every check that could not run for want -// of that part, and was not asked again by the same part at the same fingerprint, is released and asked again once. -func (ch *Checker) PartRunning(ctx context.Context, node, module, fingerprint string) { - ch.init() - if ch.CouldNotRun == nil { - return - } - keys, err := ch.CouldNotRun.Keys(ctx) - if err != nil { - ch.logf("the checks that could not run cannot be listed, so the running of %s on %s asks none again: %v", module, node, err) - return - } - at := runningAt(node, module, fingerprint) - released := 0 - for _, key := range keys { - r, _, ok := ch.couldNotRunOf(ctx, key) - if !ok || !r.Part.matches(node, module) || r.AskedAt == at { - continue - } - ch.mu.Lock() - ch.release[key] = at - ch.mu.Unlock() - released++ - ch.logf("%s: %s runs (%s), which the check could not hear: asked again", key, module, at) - } - if released > 0 { - ch.Reconsider(ctx) - } -} - -// couldNotRunOf reads one key of the state could-not-run. -func (ch *Checker) couldNotRunOf(ctx context.Context, key string) (CouldNotRun, uint64, bool) { - raw, rev, found, err := ch.CouldNotRun.Get(ctx, key) - if err != nil || !found || len(raw) == 0 { - return CouldNotRun{}, 0, false - } - var r CouldNotRun - if json.Unmarshal(raw, &r) != nil { - return CouldNotRun{}, 0, false - } - return r, rev, true -} - -// takeRelease answers the `running` that released the check of key, once, and forgets it. -func (ch *Checker) takeRelease(key string) (string, bool) { - ch.mu.Lock() - defer ch.mu.Unlock() - at, ok := ch.release[key] - delete(ch.release, key) - return at, ok -} - -// keepCouldNotRun writes the check's record after a verdict that could not run, asked again by askedAt ("" for none). -func (ch *Checker) keepCouldNotRun(ctx context.Context, cs *Case, v meshdeliverychecker.VerdictsByGroupHeadsValue, part Part, askedAt string) { - if ch.CouldNotRun == nil { - return - } - r := CouldNotRun{V: 1, Group: cs.Group, Heads: cs.Heads, Part: part, Words: failedOf(v).Words, Since: ch.now().UTC(), AskedAt: askedAt} - data, err := json.Marshal(r) - if err != nil { - return - } - _, rev, found, err := ch.CouldNotRun.Get(ctx, cs.Key) - if err == nil && found { - _, err = ch.CouldNotRun.Update(ctx, cs.Key, data, rev) - } else if err == nil { - _, err = ch.CouldNotRun.Create(ctx, cs.Key, data) - } - if err != nil { - ch.logf("%s: could not run, and that cannot be kept, so no running of %s asks it again until it is checked again: %v", cs.Key, part, err) - return - } - if ch.CouldNotRunKept != nil { - ch.CouldNotRunKept() - } -} - -// clearCouldNotRun forgets the check of key that could not run, and clears its finding: its heads are checked again, -// replaced, or their pull request closed. -func (ch *Checker) clearCouldNotRun(ctx context.Context, key string) { - if ch.CouldNotRun == nil { - return - } - r, _, ok := ch.couldNotRunOf(ctx, key) - if !ok { - return - } - if d, can := ch.CouldNotRun.(Deleter); can { - if err := d.Delete(ctx, key); err != nil { - ch.logf("%s: the record that it could not run cannot be deleted: %v", key, err) - return - } - } - if r.Raised && ch.Findings != nil { - if err := ch.Findings.Delete(couldNotRunFinding(key, r, ch.now())); err != nil { - ch.logf("%s: its finding %s cannot be cleared: %v", key, CouldNotRunKind, err) - } - } -} - -// CouldNotRunDue raises the finding of every check that could not run and was not asked again within CouldNotRunBound by -// now, and answers when the next one is due (the zero time when none waits). The checker's process waits for that time; -// a test gives now. -func (ch *Checker) CouldNotRunDue(ctx context.Context, now time.Time) time.Time { - if ch.CouldNotRun == nil { - return time.Time{} - } - keys, err := ch.CouldNotRun.Keys(ctx) - if err != nil { - ch.logf("the checks that could not run cannot be listed, so none is raised as %s now: %v", CouldNotRunKind, err) - return time.Time{} - } - sort.Strings(keys) - var next time.Time - for _, key := range keys { - r, rev, ok := ch.couldNotRunOf(ctx, key) - if !ok || r.Raised { - continue - } - due := r.Since.Add(CouldNotRunBound) - if now.Before(due) { - if next.IsZero() || due.Before(next) { - next = due - } - continue - } - if ch.Findings == nil { - continue - } - if err := ch.Findings.Put(couldNotRunFinding(key, r, now)); err != nil { - ch.logf("%s: its finding %s cannot be written: %v", key, CouldNotRunKind, err) - continue - } - r.Raised = true - if data, err := json.Marshal(r); err == nil { - if _, err := ch.CouldNotRun.Update(ctx, key, data, rev); err != nil && !errors.Is(err, ErrConflict) { - ch.logf("%s: its finding is written, and that cannot be kept: %v", key, err) - } - } - } - return next -} - -// Finding is the checker's finding of a check that could not run, in plain fields: the checker's writer lays it out as the seat -// mesh-conditions' finding (design 47 G §689). -type Finding struct { - Kind, ID, Summary, Headline, Explanation string - At time.Time -} - -// FindingWriter writes and clears the checker's findings. -type FindingWriter interface { - Put(f Finding) error - Delete(f Finding) error -} - -// RaisedCouldNotRun is every finding the checker observes now (design 47 G §384, §461): the check that could not run -// and whose finding is written. The finding writer states them at its start and when the keeper asks. -func (ch *Checker) RaisedCouldNotRun(ctx context.Context) ([]Finding, error) { - if ch.CouldNotRun == nil { - return nil, nil - } - keys, err := ch.CouldNotRun.Keys(ctx) - if err != nil { - return nil, fmt.Errorf("the checks that could not run cannot be listed: %w", err) - } - sort.Strings(keys) - var out []Finding - for _, key := range keys { - if r, _, ok := ch.couldNotRunOf(ctx, key); ok && r.Raised { - out = append(out, couldNotRunFinding(key, r, r.Since)) - } - } - return out, nil -} - -// couldNotRunFinding is the finding of the check of key that could not run. -func couldNotRunFinding(key string, r CouldNotRun, at time.Time) Finding { - return Finding{Kind: CouldNotRunKind, ID: key, - Summary: fmt.Sprintf("the check of %s (heads %s) could not run since %s: %s was not heard, and has not run since", - r.Group, r.Heads, r.Since.UTC().Format(time.RFC3339), r.Part), - Headline: fmt.Sprintf("The check of %s could not run", r.Group), - Explanation: "A check that cannot hear a part of the mesh it reads says so and is asked again when that part runs. " + - "It has not run for an hour: look at " + r.Part.String() + ". The finding clears when the heads are checked again, " + - "replaced, or their pull request closes.", - At: at.UTC()} -} diff --git a/modules/mesh-delivery-checker/internal/check/declared.go b/modules/mesh-delivery-checker/internal/check/declared.go deleted file mode 100644 index 95bf4c55..00000000 --- a/modules/mesh-delivery-checker/internal/check/declared.go +++ /dev/null @@ -1,115 +0,0 @@ -package check - -import ( - "context" - "errors" - "fmt" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// declaredPackage is one package a group member declares: the module, its directory in the repository (RootPath for its -// root, empty when not known), and the artifact's name, paths and language. -type declaredPackage struct { - pull Pull - module, dir string - name string - paths []string - language string -} - -// manifestAtHead is a touched module's manifest read from the forge at its member's head (the lead's ruling 4 on A2c's -// finding 4): with no preview of the merge, a package new in the group is declared only there, never at the trunk. The -// module's directory is the indexer's when it knows the module, else modules/ or the repository's root, the one -// whose manifest names the module. Found is false when no such file names it. -func (st *state) manifestAtHead(ctx context.Context, p Pull, module string) (m catalogue.Manifest, dir string, found bool, err error) { - var dirs []string - if st.j.Dirs != nil { - if d, ok := st.j.Dirs.ModuleDir(module); ok { - dirs = []string{d} - } - } - if dirs == nil { - dirs = []string{"modules/" + module, meshcatalogclient.RootPath} - } - for _, d := range dirs { - file := "module.json" - if t := strings.Trim(d, "/"); t != "" && t != meshcatalogclient.RootPath { - file = t + "/module.json" - } - text, err := st.j.Forge.File(ctx, p.Repository, file, p.Head) - if errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return catalogue.Manifest{}, "", false, fmt.Errorf("the manifest of %s at the head of %s cannot be read%s: %w", module, pullKey(p), unheardWords(err), err) - } - m, err := catalogue.Decode([]byte(text)) - if err != nil { - return catalogue.Manifest{}, "", false, fmt.Errorf("the manifest %s of %s at the head of %s is no manifest: %w", file, module, pullKey(p), err) - } - if m.Module == module { - return m, d, true, nil - } - } - return catalogue.Manifest{}, "", false, nil -} - -// declaredPackages are the packages the case's members declare, read from each touched module's manifest at the merge -// preview (the candidate's); with no preview, at the member's head on the forge, so a package new in the group is asked -// for on the first check too (trunk's manifest when no forge is read): a member that declares none is not asked for. -func (st *state) declaredPackages(ctx context.Context) ([]declaredPackage, error) { - if st.j.Read == nil && st.j.Forge == nil { - return nil, nil - } - var trunk map[string]string - if st.j.Manifests != nil { - trunk = st.j.Manifests.ManifestDigests() - } - var out []declaredPackage - for _, p := range st.c.Pulls { - for _, module := range p.Modules { - var m catalogue.Manifest - var dir string - digest := p.Fps[module].Manifest - switch { - case digest != "" && st.j.Read != nil: - var err error - if m, err = st.j.Read.Manifest(ctx, digest); err != nil { - return nil, fmt.Errorf("the manifest of %s (%s) cannot be read, so the packages it declares are not known: %w", module, pullKey(p), err) - } - case st.j.Forge != nil: - var found bool - var err error - if m, dir, found, err = st.manifestAtHead(ctx, p, module); err != nil { - return nil, fmt.Errorf("%v, so the packages it declares are not known", err) - } - if !found { - continue - } - case trunk[module] != "" && st.j.Read != nil: - var err error - if m, err = st.j.Read.Manifest(ctx, trunk[module]); err != nil { - return nil, fmt.Errorf("the manifest of %s (%s) cannot be read, so the packages it declares are not known: %w", module, pullKey(p), err) - } - default: - continue - } - if dir == "" && st.j.Dirs != nil { - dir, _ = st.j.Dirs.ModuleDir(module) - } - if m.Build == nil { - continue - } - for _, a := range m.Build.Artifacts { - if a.Kind != catalogue.ArtifactPackage { - continue - } - out = append(out, declaredPackage{pull: p, module: module, dir: dir, name: a.Name, paths: a.Paths, language: a.Language}) - } - } - } - return out, nil -} diff --git a/modules/mesh-delivery-checker/internal/check/digestform_test.go b/modules/mesh-delivery-checker/internal/check/digestform_test.go deleted file mode 100644 index 1648c337..00000000 --- a/modules/mesh-delivery-checker/internal/check/digestform_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package check - -import ( - "encoding/json" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// 1930/AC3: given a check ask whose stored end keeps the digest's packages as a list of {package, digest} (issue 1930), -// or one stored before it with the packages a map keyed by package path, when the same heads are checked a second time, -// then the kept verdicts the first check wrote are read from it: every package it passed is kept, none run again. -func TestKeptVerdictsAreReadFromTheDigestsListForm(t *testing.T) { - for _, tc := range []struct { - name string - old bool - }{{"the list form", false}, {"a map stored before issue 1930", true}} { - t.Run(tc.name, func(t *testing.T) { - b := newBench(t) - var stored []string - b.asks.Answer = func(a buildstep.StepAsk) buildstep.Step { - s := b.answer(a) - if tc.old && s.State == buildstep.StepPassed { - s.Result = oldDigestForm(t, s.Result) - } - stored = append(stored, string(s.Result)) - return s - } - first := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - u := first.Levels[buildstep.LevelUnit] - if first.State != meshdeliverycheckerclient.VerdictPass || u.Run == 0 || u.Kept != 0 { - t.Fatalf("the first check = %+v", first) - } - if len(stored) == 0 { - t.Fatal("no ask's end was stored") - } - list := strings.Contains(stored[0], `"packages":[{"package":"fixture/slow"`) - if list == tc.old || strings.Contains(stored[0], `"fixture/slow":"`) != tc.old { - t.Fatalf("the stored end is not in the form under test: %s", stored[0]) - } - b.steps.calls = nil - second := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if s := second.Levels[buildstep.LevelUnit]; second.State != meshdeliverycheckerclient.VerdictPass || s.Kept != u.Run || s.Run != 0 { - t.Fatalf("the second check's unit level = %+v, want %d kept and none run", s, u.Run) - } - if n := len(b.steps.tool("test")); n != 0 { - t.Errorf("%d test steps ran on the second check", n) - } - }) - } -} - -// oldDigestForm rewrites a check answer's level digests to the form stored before issue 1930: packages as a map keyed by -// package path. -func oldDigestForm(t *testing.T, result json.RawMessage) json.RawMessage { - t.Helper() - var a struct { - Fetch json.RawMessage `json:"fetch"` - Levels []map[string]any `json:"levels"` - } - if err := json.Unmarshal(result, &a); err != nil { - t.Fatal(err) - } - for _, l := range a.Levels { - d, ok := l["digest"].(map[string]any) - if !ok { - continue - } - m := map[string]any{} - list, _ := d["packages"].([]any) - for _, p := range list { - pd := p.(map[string]any) - m[pd["package"].(string)] = pd["digest"] - } - d["packages"] = m - } - raw, err := json.Marshal(a) - if err != nil { - t.Fatal(err) - } - return raw -} diff --git a/modules/mesh-delivery-checker/internal/check/doc.go b/modules/mesh-delivery-checker/internal/check/doc.go deleted file mode 100644 index 781347eb..00000000 --- a/modules/mesh-delivery-checker/internal/check/doc.go +++ /dev/null @@ -1,6 +0,0 @@ -// Package check is the checker's logic (novox/hq ADR 0484, ADR 0453 clause 4, ADR 0447, ADR 0444), apart from the bus: the -// claim of a verdict key, the judging of a pull request's head merged onto its trunk through the build modules' fetch, -// digest and test steps, the kept verdicts, the line of the status and the act the forge bridge performs. The bus is -// reached through the small interfaces here, so each part is tested with fakes. The checker runs dark: nothing reads its -// answers to merge or deliver (ADR 0453 clause 4). -package check diff --git a/modules/mesh-delivery-checker/internal/check/engines.go b/modules/mesh-delivery-checker/internal/check/engines.go deleted file mode 100644 index bf2fc352..00000000 --- a/modules/mesh-delivery-checker/internal/check/engines.go +++ /dev/null @@ -1,261 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "fmt" - "mesh-delivery-checker/rules" - "sort" - "strings" - "sync" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" -) - -// **A check's composition is the checker's, made from what each node's engine answers** (novox/hq ADR 0593 §7, §9). -// Only a node knows whether a change composes there: the checker asks every placed node's engine -// `node-engine.compose-candidate` in parallel, with the check's candidate, and waits at most its answer bound -// (compose-answer-bound, 60 s by default). A node that refuses fails the check, naming the node and each module with the -// engine's words; a node that does not answer within the bound, answers unready or cannot be asked is named, "not composed -// on that node: no answer", and the check passes with that warning: its own composition judges again when it applies, -// and the canary rule holds. The checker decides by the answer's verdict, never by its words. -// -// **A group that changes the node-engine's composition code** (a member of the node-engine's repository) is not judged by -// the running engines, which cannot judge their own next code: each node's engine answers the composition input it -// composed from (ne.ComposeCandidateInput.Input), and the composer program built from that member's head composes it in a -// build module's sandbox (Sandbox), with every manifest of the catalogue as the case leaves it and the indexer's verdicts. - -// EngineAsker asks one node's engine to compose a check's candidate (the verb node-engine.compose-candidate on that node). -type EngineAsker interface { - ComposeCandidate(ctx context.Context, node string, ask ne.ComposeCandidateInput) (ne.ComposeCandidateAnswer, error) -} - -// Sandbox runs a compose job on the Go build seat and answers the composer's verdict. -type Sandbox interface { - Compose(ctx context.Context, job buildstep.ComposeJob) (composer.Verdict, error) -} - -// EngineCompositions is the checker's composition of a check (Examiner.Compositions). -type EngineCompositions struct { - Asker EngineAsker - // Placed names every node in assignments now (the seat mesh-module-assigner's state), or why it cannot be read: then no node - // can be asked, and the composition is not made (an error), never passed with nobody asked. - Placed func() ([]string, error) - // Bound is the answer bound (the setting compose-answer-bound); zero is nec.AnswerBound. - Bound time.Duration - // Composer is the node-engine's trunk head: a check whose group has no node-engine member is composed by the composer - // built there, so every check composes with an acting part built from the change's heads or their trunk, never with - // the running engines' own code (ADR 0656 §5; the lead's ruling Q3 on milestone 47/G). Nil, or an empty head, leaves - // such a check composed by the running engines, said. - Composer func() string - // Sandbox runs the composer of a node-engine member's head; nil leaves such a group judged by the running engines, - // said in a warning. - Sandbox Sandbox - // Manifests are the indexer's trunk manifest digests by module, and Verdicts its refusals of them: the catalogue the - // sandbox composes with, before the case's own manifests are laid over. - Manifests TrunkManifests - // Published names the latest published version of every mesh package (Examiner.Published): the composer is built - // against them and the group's heads, as a check is (issue 1921); nil names the group's heads alone. - Published interface { - LatestPackages() buildstep.PackageVersions - } - Verdicts func() map[string]meshcatalog.VerdictsByDigestValue - // Requests keeps each request sent to the composer, per check and node (issue 1921); nil keeps none. A request that - // cannot be kept fails the node's composition, naming why: never dropped. - Requests interface { - Keep(ctx context.Context, key, node string, body []byte) error - } -} - -// composerHead is the node-engine member's head of a case, or empty. -// atWord names where a composer was built: the member's head or the node-engine's trunk. -func atWord(member bool) string { - if member { - return "head" - } - return "trunk" -} - -func composerHead(c *Case) string { - if m := c.member(nodeEngineRepo); m != nil { - return m.Head - } - return "" -} - -// Compose asks every placed node and answers their answers, for the rules to judge (rules.JudgeEngines). An error is only a -// case it cannot ask at all. -func (e EngineCompositions) Compose(ctx context.Context, c *Case) (rules.Composed, error) { - began := time.Now() - key, err := nodeengineclient.CompositionKey(c.Group, c.Heads) - if err != nil { - return rules.Composed{}, err - } - var nodes []string - if e.Placed != nil { - if nodes, err = e.Placed(); err != nil { - return rules.Composed{}, fmt.Errorf("the assignments cannot be read, so no node can be asked: %w", err) - } - } - bound := e.Bound - if bound <= 0 { - bound = nec.AnswerBound - } - // One composition path (ADR 0656 §5): the composer built at the node-engine member's head, else at its trunk. - head, member := composerHead(c), true - if head == "" && e.Composer != nil { - head, member = e.Composer(), false - } - sandboxed := head != "" && e.Sandbox != nil - answers := map[string]ne.ComposeCandidateAnswer{} - silent := map[string]string{} - var mu sync.Mutex - var wg sync.WaitGroup - for _, node := range nodes { - wg.Add(1) - go func() { - defer wg.Done() - at, cancel := context.WithTimeout(ctx, bound) - defer cancel() - a, err := e.Asker.ComposeCandidate(at, node, ne.ComposeCandidateInput{Candidate: key, Input: sandboxed}) - if err == nil && sandboxed && a.Verdict != nec.Unready { - // The composer's build and run are the check's to bound, not the engine's answer bound. A composer that - // cannot run is no silent node (ADR 0593 §9 lets only that pass): the node's composition fails, naming - // the head. - if composed, serr := e.inSandbox(ctx, c, head, node, a); serr != nil { - a = ne.ComposeCandidateAnswer{V: a.V, Node: node, Candidate: a.Candidate, Verdict: nec.Refused, - Refused: "the composer at the node-engine's " + atWord(member) + " " + head + " could not compose it: " + serr.Error()} - } else { - a = composed - } - } - mu.Lock() - defer mu.Unlock() - switch { - case err != nil: - silent[node] = err.Error() - case a.Verdict == nec.Unready: - silent[node] = "unready" - default: - answers[node] = a - } - }() - } - wg.Wait() - // What composed, named by the node-engine's commit: the member's head in the sandbox, else the one the mesh runs. - composerName := "node-engine" - if sandboxed { - composerName = head - } - v := rules.Composed{Group: c.Group, Heads: c.HeadsHash, Composer: composerName, Nodes: nodes, Answers: answers, Silent: silent} - switch { - case member && head != "" && e.Sandbox == nil: - v.Warnings = append(v.Warnings, "the group changes the node-engine's composition code and was composed by the running engines") - case !sandboxed && e.Sandbox != nil && len(nodes) > 0: - v.Warnings = append(v.Warnings, "composed by the running engines: no composer at the node-engine's trunk is known to build") - } - v.Took = time.Since(began).Milliseconds() - return v, nil -} - -// inSandbox composes a node's answer again with the composer built from the node-engine member's head, over the input the -// node's engine composed from: the node's answer becomes the sandbox's, refused with the composer's words where the -// composer refuses the node. -func (e EngineCompositions) inSandbox(ctx context.Context, c *Case, head, node string, a ne.ComposeCandidateAnswer) (ne.ComposeCandidateAnswer, error) { - if len(a.Input) == 0 { - return a, fmt.Errorf("the engine of %s answered no composition input, so the node-engine's head cannot compose it", node) - } - manifests := map[string]composer.Manifest{} - if e.Manifests != nil { - fps := e.Manifests.ManifestFingerprints() - for name, digest := range e.Manifests.ManifestDigests() { - manifests[name] = composer.Manifest{Digest: digest, Fingerprint: fps[name]} - } - } - for _, p := range c.Pulls { - for name, m := range p.Fps { - if m.Manifest != "" { - manifests[name] = composer.Manifest{Digest: m.Manifest, Fingerprint: m.Fp2} - } - } - } - verdicts := map[string]json.RawMessage{} - if e.Verdicts != nil { - for name, v := range e.Verdicts() { - raw, err := json.Marshal(v) - if err != nil { - return a, fmt.Errorf("the verdict of the manifest of %s cannot be encoded: %w", name, err) - } - verdicts[name] = raw - } - } - // A manifest the change brings is the check's own judge's (ADR 0578 §2, §3): its verdict there when the judge gave one, - // else none, which the composer reads as accepted; never the running indexer's (F5 of milestone 47/G). - for _, p := range c.Pulls { - for name := range p.Fps { - delete(verdicts, name) - if raw, ok := c.Judged[name]; ok { - verdicts[name] = raw - } - } - } - // The answer carries its input as the object the seat's contract names, decoded with its numbers as written - // (json.Number), so encoding it again gives the composer the same values the engine sent. - input, err := json.Marshal(a.Input) - if err != nil { - return a, fmt.Errorf("the engine of %s answered a composition input that cannot be encoded again: %w", node, err) - } - req := composer.Request{V: composer.Version, Input: input, Depth: composer.DepthCompose, Machines: []string{node}, - Cases: []composer.Case{{Name: "change", Manifests: manifests, Verdicts: verdicts}}, Seats: c.Seats} - raw, err := json.Marshal(req) - if err != nil { - return a, err - } - if e.Requests != nil { - key, err := nodeengineclient.CompositionKey(c.Group, c.Heads) - if err == nil { - err = e.Requests.Keep(ctx, key, node, raw) - } - if err != nil { - return a, err - } - } - job := buildstep.ComposeJob{Fetch: buildstep.Fetch{Repository: nodeEngineRepo, Commit: head, Dir: meshcatalogclient.RootPath, Packages: packagesFor(e.Published, c, nodeEngineRepo)}, Dir: meshcatalogclient.RootPath, - Package: composer.Package, Request: raw, Reader: "the node-engine head of " + c.Group} - // The ask travels on the bus, whose message limit a whole request passes (1.2 MB for a large node): it goes packed. - if err := job.PackRequest(raw); err != nil { - return a, err - } - verdict, err := e.Sandbox.Compose(ctx, job) - if err != nil { - return a, fmt.Errorf("the composer at the node-engine's head %s did not answer for %s: %w", head, node, err) - } - out := ne.ComposeCandidateAnswer{V: a.V, Node: node, Candidate: a.Candidate, Verdict: nec.Composed} - notJudged := "" - for _, cv := range verdict.Cases { - if m, ok := cv.Machines[node]; ok && m.NotJudged != "" { - notJudged = m.NotJudged // the composer did not judge the node: neither composed nor refused (ADR 0593 §9) - } else if ok && !m.Composes { - out.Verdict = nec.Refused - out.Refused = strings.Join(append(append([]string{m.Reason}, m.Problems...), m.Refusals...), "; ") - } - for name, read := range cv.Manifests { - if !read.Accepted { - out.Verdict = nec.Refused - out.Modules = append(out.Modules, ne.Module{Module: name, State: nec.ModuleRefused, Words: read.Refused}) - } - } - } - sort.Slice(out.Modules, func(i, j int) bool { return out.Modules[i].Module < out.Modules[j].Module }) - if notJudged != "" && out.Verdict == nec.Composed { - out.Verdict, out.Refused = nec.NotJudged, notJudged - } - return out, nil -} diff --git a/modules/mesh-delivery-checker/internal/check/engines_test.go b/modules/mesh-delivery-checker/internal/check/engines_test.go deleted file mode 100644 index 66483abb..00000000 --- a/modules/mesh-delivery-checker/internal/check/engines_test.go +++ /dev/null @@ -1,392 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "mesh-delivery-checker/rules" - "strings" - "sync" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk" -) - -// fakeEngines answers compose-candidate by node from memory; a node it holds no answer for never answers. -type fakeEngines struct { - mu sync.Mutex - answers map[string]ne.ComposeCandidateAnswer - errs map[string]error - asked map[string]ne.ComposeCandidateInput -} - -func (f *fakeEngines) ComposeCandidate(ctx context.Context, node string, ask ne.ComposeCandidateInput) (ne.ComposeCandidateAnswer, error) { - f.mu.Lock() - if f.asked == nil { - f.asked = map[string]ne.ComposeCandidateInput{} - } - f.asked[node] = ask - a, ok := f.answers[node] - err := f.errs[node] - f.mu.Unlock() - if err != nil { - return ne.ComposeCandidateAnswer{}, err - } - if !ok { - <-ctx.Done() - return ne.ComposeCandidateAnswer{}, ctx.Err() - } - return a, nil -} - -// fakeSandbox answers a compose job with a verdict, recording the job. -type fakeSandbox struct { - mu sync.Mutex - jobs []buildstep.ComposeJob - v func(node string) composer.Verdict - err error -} - -func (f *fakeSandbox) Compose(_ context.Context, job buildstep.ComposeJob) (composer.Verdict, error) { - var req composer.Request - body, err := job.RequestBytes() - if err != nil { - return composer.Verdict{}, err - } - if err := json.Unmarshal(body, &req); err != nil { - return composer.Verdict{}, err - } - f.mu.Lock() - f.jobs = append(f.jobs, job) - f.mu.Unlock() - if f.err != nil { - return composer.Verdict{}, f.err - } - return f.v(req.Machines[0]), nil -} - -// keptRequests is the state of composition requests in memory; a put fails with err when it is set. -type keptRequests struct { - mu sync.Mutex - vals map[string][]byte - err error -} - -func (k *keptRequests) Put(_ context.Context, key string, value []byte) (uint64, error) { - k.mu.Lock() - defer k.mu.Unlock() - if k.err != nil { - return 0, k.err - } - if k.vals == nil { - k.vals = map[string][]byte{} - } - k.vals[key] = value - return 1, nil -} - -func (k *keptRequests) read() StateRead { - return StateRead{Keys: func() ([]string, error) { - var out []string - for n := range k.vals { - out = append(out, n) - } - return out, nil - }, Get: func(key string) (json.RawMessage, error) { return k.vals[key], nil }} -} - -type digests map[string]string - -func (d digests) ManifestDigests() map[string]string { return d } - -// ManifestFingerprints: the trunk entry of each module is src2: in a test. -func (d digests) ManifestFingerprints() map[string]string { - out := map[string]string{} - for m := range d { - out[m] = "src2:" + m - } - return out -} - -// ADR 0593 §7, §9 (D12, G1, G2): the checker asks every placed node's engine in parallel and makes the composition from -// their answers: a refusal fails it naming the node and each module; a node that does not answer within the bound, -// answers unready or cannot be asked is named "not composed on that node: no answer" and the check passes with that -// warning, decided by the verdict and never by its words; a group with a node-engine member is composed by the composer -// built at that member's head, over the input each node's engine composed from. -func TestTheCompositionIsMadeFromTheEnginesAnswers(t *testing.T) { - composed := func(node string) ne.ComposeCandidateAnswer { - return ne.ComposeCandidateAnswer{Node: node, Verdict: nec.Composed, - Modules: []ne.Module{{Module: "web", State: nec.ModuleComposed, Digest: "sha256:" + strings.Repeat("a", 64)}}, - Own: ne.Module{Module: nec.Own, State: nec.ModuleComposed, Digest: "sha256:" + strings.Repeat("b", 64)}} - } - cs := caseOf(t, pullOf(repoA, 7, head1)) - key, _ := nodeengineclient.CompositionKey(cs.Group, cs.HeadsHash) - var last rules.Composed // the engines' answers of the last compose, for the check's verdict to take in - compose := func(e *fakeEngines, nodes ...string) meshwants.CompositionsByGroupHeadsValue { - t.Helper() - ec := EngineCompositions{Asker: e, Placed: func() ([]string, error) { return nodes, nil }, Bound: 50 * time.Millisecond, - Composer: func() string { return strings.Repeat("7", 40) }} - raw, err := ec.Compose(context.Background(), cs) - if err != nil { - t.Fatal(err) - } - last = raw - return rules.JudgeEngines(raw) - } - - t.Run("one node refusing fails the composition, naming the node and the module", func(t *testing.T) { - refusing := composed("laptop") - refusing.Verdict = nec.Refused - refusing.Modules = []ne.Module{{Module: "web", State: nec.ModuleRefused, Words: "a required value is unset"}, - {Module: "mail", State: nec.ModuleHeld, Words: "held as today"}} - e := &fakeEngines{answers: map[string]ne.ComposeCandidateAnswer{"anchor": composed("anchor"), "laptop": refusing}} - v := compose(e, "anchor", "laptop") - if v.State != meshdeliverycheckerclient.CompositionFail || v.Machine != "laptop" || !strings.Contains(v.Words, "not composed on laptop: web: a required value is unset") || - strings.Contains(v.Words, "mail") || len(v.Machines) != 2 { - t.Errorf("verdict = %+v", v) - } - if e.asked["anchor"].Candidate != key || e.asked["laptop"].Input { - t.Errorf("asked %+v, want the check's candidate %s and no input", e.asked, key) - } - var ver meshdeliverychecker.VerdictsByGroupHeadsValue - rules.JudgeComposition(&ver, rules.Composition{Composed: &last}) - if ver.State != meshdeliverycheckerclient.VerdictFail || ver.Failed.Machine != "laptop" { - t.Errorf("the check's verdict = %+v", ver) - } - }) - - t.Run("a silent, unready or unreachable node is no answer, and the check passes naming it", func(t *testing.T) { - unready := ne.ComposeCandidateAnswer{Node: "g14", Verdict: nec.Unready, Refused: "a module refused it"} - e := &fakeEngines{answers: map[string]ne.ComposeCandidateAnswer{"anchor": composed("anchor"), "g14": unready}, - errs: map[string]error{"ace": errors.New("no responders")}} - began := time.Now() - v := compose(e, "anchor", "laptop", "g14", "ace") - if took := time.Since(began); took > 2*time.Second { - t.Errorf("the nodes were not asked in parallel within the bound: %s", took) - } - // No sandbox: the running engines composed, named so, though the node-engine's trunk head is known. - if v.State != meshdeliverycheckerclient.CompositionPass || v.Sdk != "node-engine" || len(v.Machines) != 1 { - t.Fatalf("verdict = %+v", v) - } - for _, node := range []string{"ace", "g14", "laptop"} { - if !strings.Contains(strings.Join(v.Warnings, "; "), node+": "+nec.NoAnswer) { - t.Errorf("%s is not named: %v", node, v.Warnings) - } - } - var ver meshdeliverychecker.VerdictsByGroupHeadsValue - ver.State = meshdeliverycheckerclient.VerdictPass - rules.JudgeComposition(&ver, rules.Composition{Composed: &last}) - if ver.State != meshdeliverycheckerclient.VerdictPass || !strings.Contains(composition(ver.Composition, 0, false), "laptop: "+nec.NoAnswer) { - t.Errorf("the line = %q", composition(ver.Composition, 0, false)) - } - }) - - t.Run("a node-engine member is composed by the composer at its head over each node's input", func(t *testing.T) { - c, err := meshcatalog.DecodeCandidatesByGroupValue(candidateOf(t, "feat/x", pullOf(nodeEngineRepo, 3, head2), pullOf(repoA, 7, head1))) - if err != nil { - t.Fatal(err) - } - host := CaseOf(c, map[string]string{repoA: trunkA, nodeEngineRepo: trunkA}) - host.Seats = []json.RawMessage{json.RawMessage(`{"seat":"node-x","scope":"node"}`)} // the heads' list (ADR 0636 §4) - withInput := composed("anchor") - withInput.Input = map[string]any{"nodes": []any{map[string]any{"name": "anchor"}}} - // Issue 1966 (the group try of 2026-11-06 10:07): the composer at the head composes against the check's seat list. - host.Seats = []json.RawMessage{json.RawMessage(`{"format":1,"seat":"node-uplink"}`)} - e := &fakeEngines{answers: map[string]ne.ComposeCandidateAnswer{"anchor": withInput}} - sb := &fakeSandbox{v: func(node string) composer.Verdict { - return composer.Verdict{V: composer.Version, Cases: []composer.CaseVerdict{{Name: "change", - Machines: map[string]composer.Machine{node: {Composes: false, Reason: "the new composer refuses it"}}}}} - }} - ec := EngineCompositions{Asker: e, Placed: func() ([]string, error) { return []string{"anchor"}, nil }, Bound: time.Second, - Sandbox: sb, Manifests: digests{"web": "sha256:" + strings.Repeat("c", 64)}, Published: published{}} - raw, err := ec.Compose(context.Background(), host) - v := rules.JudgeEngines(raw) - if err != nil { - t.Fatal(err) - } - if !e.asked["anchor"].Input || len(sb.jobs) != 1 { - t.Fatalf("asked %+v, jobs %+v: want the input asked and one job", e.asked, sb.jobs) - } - job := sb.jobs[0] - if job.Fetch.Dir == "" || job.Fetch.Dir != job.Dir { // a build is for one module: its fetch names the directory (A-447b) - t.Errorf("the compose job's fetch names the directory %q, want its module's %q", job.Fetch.Dir, job.Dir) - } - var req composer.Request - if len(job.Request) == 0 || job.Request[0] != '"' { - t.Errorf("the request went whole, not packed (issue 1966): %.60s", job.Request) - } - body, _ := job.RequestBytes() - _ = json.Unmarshal(body, &req) - if job.Fetch.Repository != nodeEngineRepo || job.Fetch.Commit != head2 || job.Package != composer.Package || - string(req.Input) != `{"nodes":[{"name":"anchor"}]}` || req.Cases[0].Manifests["web"].Digest == "" || - len(req.Seats) != 1 || string(req.Seats[0]) != string(host.Seats[0]) { - t.Errorf("job = %+v, request %+v", job, req) - } - // Issue 1921 (notes 66-67): the request sent to the composer is kept per check and node, exactly as sent (read back - // whole through the tool's reader); a request that cannot be kept fails the node's composition, naming why. - store := &keptRequests{} - ec.Requests = RequestKeeper{Store: store} - if _, err := ec.Compose(context.Background(), host); err != nil { - t.Fatal(err) - } - keyOfHost, _ := nodeengineclient.CompositionKey(host.Group, host.Heads) - lastBody, _ := sb.jobs[len(sb.jobs)-1].RequestBytes() - got, err := ReadRequests(store.read(), keyOfHost, "anchor") - if b, _ := json.Marshal(got); err != nil || !strings.Contains(string(b), `"request":`+string(lastBody)) { - t.Errorf("kept %s, %v: want the request of the last job under %s/anchor", b, err, keyOfHost) - } - store.err = errors.New("the state is full") - raw, _ = ec.Compose(context.Background(), host) - if v := rules.JudgeEngines(raw); v.State != meshdeliverycheckerclient.CompositionFail || !strings.Contains(v.Words, "could not be kept") || !strings.Contains(v.Words, "the state is full") || !strings.Contains(v.Words, RequestsDropTool) { - t.Errorf("a request that cannot be kept gave %+v, which must name %s", v, RequestsDropTool) - } - // The drop tool: records the hand act first (a drop whose act is not recorded is not made), then drops that check's - // requests and the pieces no other kept request names; another check's stay. - store.err = nil - other := RequestKeeper{Store: store} - if err := other.Keep(context.Background(), "g.other", "anchor", []byte("another check's request")); err != nil { - t.Fatal(err) - } - drop := StateDrop{StateRead: store.read(), Delete: func(k string) error { store.mu.Lock(); delete(store.vals, k); store.mu.Unlock(); return nil }} - before := len(store.vals) - if _, err := DropRequests(drop, keyOfHost, "the state is full", func(string) error { return errors.New("no holder") }); err == nil || len(store.vals) != before { - t.Errorf("a drop whose hand act was not recorded was made: %v, %d of %d kept", err, len(store.vals), before) - } - var acts []string - answer, err := DropRequests(drop, keyOfHost, "the state is full", func(what string) error { acts = append(acts, what); return nil }) - if b, _ := json.Marshal(answer); err != nil || len(acts) != 1 || !strings.Contains(string(b), `"dropped_requests":["anchor"]`) { - t.Errorf("the drop answered %s, %v, acts %v", b, err, acts) - } - if _, still := store.vals[RequestKey(keyOfHost, "anchor")]; still { - t.Errorf("the dropped check's request is kept still") - } - if got, err := ReadRequests(store.read(), "g.other", "anchor"); err != nil || !strings.Contains(fmt.Sprint(got), "another check's request") { - t.Errorf("another check's request after the drop: %v, %v", got, err) - } - if _, err := DropRequests(drop, keyOfHost, "again", func(string) error { return nil }); err == nil { - t.Errorf("a check with no kept request was dropped") - } - ec.Requests = nil - // Issue 1921: the composer is built at the group's heads, also a member that publishes no package yet. - if pkgs := job.Fetch.Packages; len(pkgs) != 1 || pkgs[trunk.ForgeModulePrefix+"mesh-delivery"] != head1 { - t.Errorf("the composer is fetched with the packages %v, want the group's mesh-delivery member at its head %s alone", pkgs, head1) - } - if v.State != meshdeliverycheckerclient.CompositionFail || v.Sdk != head2 || !strings.Contains(v.Words, "the new composer refuses it") { - t.Errorf("verdict = %+v", v) - } - // ADR 0593 §9 (ADR 0601 §4's kind): a node the composer did not judge is named as no answer with its words, by the - // contract's field, and the check passes; words in Reason alone are no such mark. - sb.err = nil - for _, row := range []struct { - m composer.Machine - pass bool - }{ - {composer.Machine{NotJudged: "its node-engine sends no running state for web"}, true}, - {composer.Machine{Reason: "not judged: its node-engine sends no running state for web"}, false}, - } { - sb.v = func(node string) composer.Verdict { - return composer.Verdict{V: composer.Version, Cases: []composer.CaseVerdict{{Name: "change", Machines: map[string]composer.Machine{node: row.m}}}} - } - raw, err = ec.Compose(context.Background(), host) - v = rules.JudgeEngines(raw) - named := len(v.Warnings) == 1 && strings.Contains(v.Warnings[0], "anchor: "+nec.NoAnswer+" (its node-engine sends no running state for web)") - if err != nil || (v.State == meshdeliverycheckerclient.CompositionPass) != row.pass || named != row.pass { - t.Errorf("%+v: verdict %+v, %v", row.m, v, err) - } - } - // Review finding 3 (ADR 0593 §9 lets only a node that does not answer pass): a composer at the head that cannot - // run fails the composition, naming the head, never passing as a silent node. - sb.err = errors.New("the build of mesh-compose failed") - raw, err = ec.Compose(context.Background(), host) - v = rules.JudgeEngines(raw) - if err != nil || v.State != meshdeliverycheckerclient.CompositionFail || !strings.Contains(v.Words, head2) || - !strings.Contains(v.Words, "the build of mesh-compose failed") || len(v.Warnings) != 0 { - t.Errorf("a composer that cannot run: %+v, %v", v, err) - } - }) - - // ADR 0578 §2, §3, ADR 0656 §5 (milestone 47/G D12, D13): with no node-engine member, every node is composed by the - // composer built at the node-engine's trunk over the input its engine answers, and a manifest the change brings carries - // the check's own judge's verdict, not the running indexer's: one the running indexer refused and the judge accepted - // composes, and the verdict names the trunk's composer. - t.Run("a change's manifest the check's judge accepted is composed by the composer at the node-engine's trunk", func(t *testing.T) { - p := pullOf(repoA, 7, head1) - p.Modules = []string{"mesh-build-go"} - p.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"mesh-build-go": {Fp2: "src2:" + strings.Repeat("f", 64), Manifest: strings.Repeat("d", 64)}} - brings := caseOf(t, p) - brings.Judged = map[string]json.RawMessage{"mesh-build-go": json.RawMessage(`{"module":"mesh-build-go","state":"accepted"}`)} - trunkHead := strings.Repeat("7", 40) - withInput := composed("anchor") - withInput.Input = map[string]any{"nodes": []any{map[string]any{"name": "anchor"}}} - e := &fakeEngines{answers: map[string]ne.ComposeCandidateAnswer{"anchor": withInput}} - sb := &fakeSandbox{v: func(node string) composer.Verdict { - return composer.Verdict{V: composer.Version, Cases: []composer.CaseVerdict{{Name: "change", - Machines: map[string]composer.Machine{node: {Composes: true}}}}} - }} - indexer := map[string]meshcatalog.VerdictsByDigestValue{"mesh-build-go": {Module: "mesh-build-go", State: "refused", Problems: []string{"serves no verb input"}}, "mail": {Module: "mail", State: "accepted"}} - ec := EngineCompositions{Asker: e, Placed: func() ([]string, error) { return []string{"anchor"}, nil }, Bound: time.Second, - Sandbox: sb, Composer: func() string { return trunkHead }, Published: published{}, - Verdicts: func() map[string]meshcatalog.VerdictsByDigestValue { return indexer }} - raw, err := ec.Compose(context.Background(), brings) - if err != nil { - t.Fatal(err) - } - if v := rules.JudgeEngines(raw); v.State != meshdeliverycheckerclient.CompositionPass || v.Sdk != trunkHead { - t.Fatalf("verdict = %+v, want a pass composed at the node-engine's trunk %s", v, trunkHead) - } - if len(sb.jobs) != 1 || sb.jobs[0].Fetch.Commit != trunkHead || sb.jobs[0].Fetch.Repository != nodeEngineRepo { - t.Fatalf("jobs %+v: want one job at the node-engine's trunk", sb.jobs) - } - var req composer.Request - b0, _ := sb.jobs[0].RequestBytes() - _ = json.Unmarshal(b0, &req) - if got := string(req.Cases[0].Verdicts["mesh-build-go"]); got != string(brings.Judged["mesh-build-go"]) || len(req.Cases[0].Verdicts) != 2 { - t.Errorf("the request's verdicts %s: want the check's judge's for mesh-build-go and the indexer's for mail", req.Cases[0].Verdicts) - } - // A manifest the judge gave no verdict on carries none, which the composer reads as accepted (ADR 0578 §3). - brings.Judged = nil - if _, err := ec.Compose(context.Background(), brings); err != nil { - t.Fatal(err) - } - var none composer.Request - bn, _ := sb.jobs[len(sb.jobs)-1].RequestBytes() - _ = json.Unmarshal(bn, &none) - if _, kept := none.Cases[0].Verdicts["mesh-build-go"]; kept { - t.Errorf("the running indexer's verdict on the change's manifest was kept: %s", none.Cases[0].Verdicts) - } - // A refusal the trunk's composer makes stays the node-engine's own. - sb.v = func(node string) composer.Verdict { - return composer.Verdict{V: composer.Version, Cases: []composer.CaseVerdict{{Name: "change", - Machines: map[string]composer.Machine{node: {Composes: false, Reason: "a required value is unset"}}}}} - } - raw, _ = ec.Compose(context.Background(), brings) - if v := rules.JudgeEngines(raw); v.State != meshdeliverycheckerclient.CompositionFail || !strings.Contains(v.Words, "a required value is unset") { - t.Errorf("a refusal the trunk's composer makes: %+v", v) - } - }) - - // Review finding 4: assignments that cannot be read is no mesh with no node: the composition is not made, said. - t.Run("an unreadable assignments makes no composition", func(t *testing.T) { - ec := EngineCompositions{Asker: &fakeEngines{}, Placed: func() ([]string, error) { return nil, errors.New("no such bucket") }} - if _, err := ec.Compose(context.Background(), cs); err == nil || !strings.Contains(err.Error(), "no such bucket") { - t.Errorf("an unreadable assignments: %v", err) - } - b := newBench(t) - b.ex.Compositions = ec - v := b.judge(t, cs).Verdict - if v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionNotRun || !strings.Contains(v.Composition.Words, "no such bucket") { - t.Errorf("the verdict's composition = %+v", v.Composition) - } - }) -} diff --git a/modules/mesh-delivery-checker/internal/check/every_failure_test.go b/modules/mesh-delivery-checker/internal/check/every_failure_test.go deleted file mode 100644 index 6016ff00..00000000 --- a/modules/mesh-delivery-checker/internal/check/every_failure_test.go +++ /dev/null @@ -1,119 +0,0 @@ -package check - -import ( - "encoding/json" - "fmt" - "mesh-delivery-checker/rules" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// failingResult is a level's test result in which the named tests failed, each in its own package. -func failingResult(t *testing.T, tests int, failed ...Failure) json.RawMessage { - t.Helper() - raw, err := json.Marshal(Tested{Level: buildstep.LevelIntegration, Tests: tests, Elapsed: 2, Failed: failed, Passed: len(failed) == 0}) - if err != nil { - t.Fatal(err) - } - return raw -} - -func judgeLevel(t *testing.T, tests ...json.RawMessage) (*meshdeliverychecker.VerdictsByGroupHeadsValue, *state) { - t.Helper() - st := &state{j: &Examiner{}, c: &Case{}, levels: rules.NewTally()} - st.level(t.Context(), &target{pull: Pull{Repository: "novox/x"}}, buildstep.LevelAnswer{Level: buildstep.LevelIntegration, Tests: tests}) - // 1921 (B0, D1a): the running checker only gathers the level as a fact; the rules judge it, and count what it kept. - judged := rules.Judge(rules.Facts{V: rules.FactsVersion, Steps: st.facts}) - if judged.Levels[buildstep.LevelIntegration] != (meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{Run: int64(st.levels[buildstep.LevelIntegration].Run)}) { - t.Errorf("the rules count %+v, the running checker %+v", judged.Levels, *st.levels[buildstep.LevelIntegration]) - } - if judged.State == meshdeliverycheckerclient.VerdictPass { - return nil, st - } - return &judged, st -} - -// Issue 1921: two failing tests in two packages are two entries of the verdict's Failures and two lines of a try's -// failed list, each with its own first line (its cause, issue 1901), whether the build module answered them in one test -// result (one go test run over both packages) or in two (the changed packages first, then the rest); every result is -// counted, the ones after a failure too; and Failed keeps the first one's words whole. -func TestTwoFailingTestsInTwoPackagesAreTwoEntries(t *testing.T) { - a := Failure{Package: "x/a", Test: "TestA", Cause: "a_test.go:9: want 1, got 2", Output: " a_test.go:3: logged\n a_test.go:9: want 1, got 2\n--- FAIL: TestA (0.00s)"} - b := Failure{Package: "x/b", Test: "TestB", Cause: "b_test.go:4: boom", Output: " b_test.go:4: boom\n--- FAIL: TestB (0.00s)"} - for name, tests := range map[string][]json.RawMessage{ - "one result": {failingResult(t, 10, a, b)}, - "two results": {failingResult(t, 4, a), failingResult(t, 3), failingResult(t, 3, b)}, - } { - v, st := judgeLevel(t, tests...) - if v == nil { - t.Fatalf("%s: the level passed", name) - } - want := []string{"integration: x/a.TestA: a_test.go:9: want 1, got 2", "integration: x/b.TestB: b_test.go:4: boom"} - if got := failedLines(*v); strings.Join(got, "\n") != strings.Join(want, "\n") { - t.Errorf("%s: failed lines\n%s\nwant\n%s", name, strings.Join(got, "\n"), strings.Join(want, "\n")) - } - if len(v.Failures) != 2 || v.Failed.Test != "integration: x/a.TestA" || !strings.Contains(v.Failed.Words, "a_test.go:3: logged") { - t.Errorf("%s: verdict %+v", name, v) - } - if run := st.levels[buildstep.LevelIntegration].Run; run != 10 { - t.Errorf("%s: %d tests counted, want all 10", name, run) - } - if _, err := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: "g", Heads: "0123456789ab", State: v.State, Failed: v.Failed, Failures: v.Failures}); err != nil { - t.Errorf("%s: the verdict is not written: %v", name, err) - } - } - // Design 26 *Out of step* (DLV-C2): the out-of-step files the compare step names, in each result, are the outcome's for - // the pull request, each once and sorted; a level naming none leaves the outcome with none. - step := func(files ...string) json.RawMessage { - raw, _ := json.Marshal(Tested{Level: buildstep.LevelUnit, Tests: 1, Passed: false, Why: "out of step", OutOfStep: files}) - return raw - } - _, st := judgeLevel(t, step("go/seats/b/contract.go", "go/seats/a/contract.go"), step("go/seats/a/contract.go")) - if got := st.outcome(meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail}).OutOfStep["novox/x#0"]; strings.Join(got, ",") != "go/seats/a/contract.go,go/seats/b/contract.go" { - t.Errorf("out of step %v", got) - } - if _, st := judgeLevel(t, failingResult(t, 2, a)); st.outcome(meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail}).OutOfStep != nil { - t.Errorf("a level naming no out-of-step file has %v", st.outOfStep) - } -} - -// Issue 1921: past meshdeliverycheckerclient.MaxFailures the list names the first fifty and how many more, the status line says how many -// failed and names the first, cut so "and N more" stays whole, and each entry's line is cut to rules.MaxFailureLine. -func TestFailuresAreCappedWithHowManyMore(t *testing.T) { - var fs []Failure - for i := range 60 { - fs = append(fs, Failure{Package: fmt.Sprintf("x/p%d", i), Test: "TestT", Cause: strings.Repeat("y", 500)}) - } - v, _ := judgeLevel(t, failingResult(t, 60, fs...)) - if len(v.Failures) != meshdeliverycheckerclient.MaxFailures+1 || v.Failures[meshdeliverycheckerclient.MaxFailures].Test != "and 10 more" { - t.Fatalf("%d entries, last %+v", len(v.Failures), v.Failures[len(v.Failures)-1]) - } - if len(v.Failures[0].Words) > rules.MaxFailureLine { - t.Errorf("an entry's line is %d bytes", len(v.Failures[0].Words)) - } - if n := rules.FailedCount(*v); n != 60 { - t.Errorf("failedCount = %d, want 60", n) - } - cs := &Case{Pulls: []Pull{{Repository: "novox/x", Head: "0123456789abcdef", Trunk: "fedcba9876543210"}}} - if line := Line(*v, cs, cs.Pulls[0], nil, nil, nil); !strings.HasPrefix(line, "failed (60): integration: x/p0.TestT: yyy") || - !strings.Contains(line, "…; and 59 more | 0123456789ab on fedcba987654") { - t.Errorf("line %q", line) - } - // A composition failing after them is counted in the last entry (the operator, 2026-11-02). - refused := refusedOn("x", "g14", "route-proxy is defined twice") - rules.JudgeComposition(v, rules.Composition{Composed: &refused}) - if n := rules.FailedCount(*v); n != 61 || v.Failed.Test != "integration: x/p0.TestT" { - t.Errorf("after the composition failedCount = %d, failed %+v", n, v.Failed) - } - - // A verdict that names no list (a conflict, a refused step, a verdict written before issue 1921) is one failed line, as - // before. - one := meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: "g14", Words: "files in conflict: go.mod\nmore"}} - if got := failedLines(one); len(got) != 1 || got[0] != "g14: files in conflict: go.mod" { - t.Fatalf("%q", got) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/examiner_test.go b/modules/mesh-delivery-checker/internal/check/examiner_test.go deleted file mode 100644 index e09bbee2..00000000 --- a/modules/mesh-delivery-checker/internal/check/examiner_test.go +++ /dev/null @@ -1,1671 +0,0 @@ -package check - -import ( - "cmp" - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "io/fs" - "mesh-delivery-checker/rules" - "os" - "path/filepath" - "reflect" - "slices" - "sort" - "strings" - "sync" - "testing" - "testing/fstest" - "time" - "unicode/utf8" - - "git.novox.be/novox/mesh-contracts/go/seats/meshconfig" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" - gobuild "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// captured reads a result the build module's own code marshalled (testdata/captured/README). -func captured[T any](t testing.TB, name string) T { - t.Helper() - raw, err := os.ReadFile(filepath.Join("testdata", "captured", name+".json")) - if err != nil { - t.Fatal(err) - } - var v T - if err := json.Unmarshal(raw, &v); err != nil { - t.Fatal(err) - } - return v -} - -type stepCall struct { - module, node, tool string - args map[string]any -} - -// fakeSteps stands for the build modules: a handler for each tool answers what the module's code answers. -type fakeSteps struct { - mu sync.Mutex - calls []stepCall - handlers map[string]func(c stepCall) (any, error) -} - -func (f *fakeSteps) Run(_ context.Context, module, node, tool string, args map[string]any) (json.RawMessage, error) { - c := stepCall{module, node, tool, args} - f.mu.Lock() - f.calls = append(f.calls, c) - h := f.handlers[tool] - f.mu.Unlock() - if h == nil { - return nil, fmt.Errorf("the fake build module has no tool %s", tool) - } - v, err := h(c) - if err != nil { - return nil, err - } - return json.Marshal(v) -} - -func (f *fakeSteps) tool(name string) []stepCall { - f.mu.Lock() - defer f.mu.Unlock() - var out []stepCall - for _, c := range f.calls { - if strings.HasSuffix(c.tool, "_"+name) { - out = append(out, c) - } - } - return out -} - -type entries []buildstep.NodeToolchain - -func (e entries) Entries(language string) []buildstep.NodeToolchain { - var out []buildstep.NodeToolchain - for _, x := range e { - if x.Language == language { - out = append(out, x) - } - } - return out -} - -type langs map[string][]string - -func (l langs) BuildModules(repository string) []string { - if m, ok := l[repository]; ok { - return m - } - return []string{goBuildModule} -} - -func goEntries(nodes ...string) entries { - var e entries - for _, n := range nodes { - e = append(e, buildstep.NodeToolchain{Node: n, Toolchain: buildstep.Toolchain{Language: "go", Version: "go1.27.1", Recipe: "7"}}) - e = append(e, buildstep.NodeToolchain{Node: n, Toolchain: buildstep.Toolchain{Language: "typescript", Version: "node24", Recipe: "3"}}) - } - return e -} - -const digC = "cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33cc33" -const digD = "dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44dd44" - -type bench struct { - ex *Examiner - asks *buildrun.FakeAsks - steps *fakeSteps - kept *memKV - prev *memKV - fetch Fetched - digest map[string]Digested // by level - test func(c stepCall) (any, error) - // declines is how many asks naming needs a node lacking them declines before shanks, which has every need, takes - // one (issue 890); an ask naming none runs on shanks, which then has none. - declines int - // inputs are the checking program's kept inputs; heads the heads laid for it (none when nil); run, when set, answers - // a run ask in its place (a program that does not build or answer). - inputs *memKV - heads fs.FS - run func(a buildstep.StepAsk) (buildstep.Step, bool) - mu sync.Mutex - runs []buildstep.StepAsk // the run asks put -} - -// benchAsks puts the bench's asks: a run ask is answered at once by the checking program's work (answerRun), written by -// write and kept apart (bench.runs); every other ask goes to inner (the fake bus by default), answered as it answers. -type benchAsks struct { - b *bench - inner buildrun.AskPublisher - write func(buildstep.Step) -} - -func (b *bench) asker(inner buildrun.AskPublisher, write func(buildstep.Step)) benchAsks { - return benchAsks{b: b, inner: inner, write: write} -} - -func (p benchAsks) PublishAsk(ctx context.Context, subject, id string, data []byte) error { - inner, write := p.inner, p.write - if inner == nil { - inner, write = p.b.asks, p.b.asks.Write - } - a, err := buildstep.DecodeStepAsk(data) - if err != nil || a.Run == nil { - return inner.PublishAsk(ctx, subject, id, data) - } - p.b.mu.Lock() - p.b.runs = append(p.b.runs, a) - p.b.mu.Unlock() - if step := p.b.answerRun(a); step.ID != "" { - write(step) - } - return nil -} - -// trunkD is mesh-delivery's trunk the bench's checking program is built at. -var trunkD = strings.Repeat("d", 40) - -// answerRun answers a run ask as the build module would: the checking program's work (rules.Program) over the input the -// ask names, read from the kept inputs and checked against its digest. -func (b *bench) answerRun(a buildstep.StepAsk) buildstep.Step { - began := time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC) - step := buildstep.Step{ID: a.ID, Tool: a.Kind + " ask", Node: "shanks", State: buildstep.StepPassed, Started: began, Ended: began.Add(time.Second)} - if b.run != nil { - if s, ok := b.run(a); ok { - return s - } - } - answer := buildstep.RunAnswer{Built: true, Key: "program-0123456789abcdef"} - out, err := rules.Program(a.Run.Args, func() ([]byte, error) { - body, _, _, _ := b.inputs.Get(context.Background(), a.Run.Input.Digest) - return body, a.Run.Input.Verify(body) - }, b.heads) - if err != nil { - answer.Exit, answer.Said = 2, err.Error() - } - answer.Output = out - step.Result, _ = json.Marshal(answer) - return step -} - -// refsOf reads the siblings of a fetch's arguments, as they arrive in an ask (JSON) or as the checker gave them. -func refsOf(v any) []BesideRef { - raw, _ := json.Marshal(v) - var refs []BesideRef - _ = json.Unmarshal(raw, &refs) - return refs -} - -// toolPrefix is the start of each build module's tool names, as the fake build modules answer them. -var toolPrefix = map[string]string{goBuildModule: "build_go_", nodeBuildModule: "build_node_"} - -// answer runs a check ask as the build module that pulls it does (gobuild.RunAsk), with the fake tools on shanks and the -// bench's kept verdicts, and answers the step it writes. -func (b *bench) answer(a buildstep.StepAsk) buildstep.Step { - bm, _ := testSeats().OfAskLanguage(a.Language) - module := bm.Module - const node = "shanks" - tool := func(name string) gobuild.StepFunc { - return func(ctx context.Context, args map[string]any) (any, error) { - raw, err := b.steps.Run(ctx, module, node, toolPrefix[module]+name, args) - if err != nil { - return nil, err - } - return raw, nil - } - } - steps := gobuild.AskSteps{Fetch: tool("fetch"), Test: tool("test"), - Kept: func(ctx context.Context, level, digest string) (int, bool, error) { - k, found, err := (Kept{Store: b.kept}).Lookup(ctx, level, digest) - return int(k.Tests), found, err - }} - if module == goBuildModule { - steps.Digest = tool("digest") - } - began := time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC) - if len(a.Needs) > 0 { - if b.declines > 0 { - b.declines-- - result, _ := json.Marshal(buildstep.StepDeclined{Declined: true, Needs: a.Needs, Lacks: a.Needs}) - return buildstep.Step{ID: a.ID, Tool: a.Kind + " ask", Node: "ace", State: buildstep.StepRefused, Started: began, Ended: began, - Reason: "declined", Result: result} - } - steps.Has = func(string) bool { return true } - } - out, err := gobuild.RunAsk(context.Background(), steps, a) - step := buildstep.Step{ID: a.ID, Tool: a.Kind + " ask", Node: node, State: buildstep.StepPassed, Started: began, Ended: began.Add(2 * time.Second)} - if out != nil { - step.Result, _ = json.Marshal(out) // a failed step keeps what it answered beside its error, as the step runner does - } - if err != nil { - step.State, step.Reason = buildstep.StepFailed, err.Error() - } - return step -} - -// newBench is an examiner over fake build modules that merge cleanly, digest the captured fixture's packages and pass -// the captured slow fixture's result at both levels, on shanks. -func newBench(t testing.TB) *bench { - b := &bench{steps: &fakeSteps{}, kept: newMemKV(), prev: newMemKV(), inputs: newMemKV(), heads: fstest.MapFS{}} - b.fetch = captured[Fetched](t, "go-fetch-clean") - b.fetch.Modules, b.fetch.Changed, b.fetch.Trees = []string{"."}, []string{"README.md"}, nil - one := captured[Digested](t, "go-digest") - b.digest = map[string]Digested{ - buildstep.LevelUnit: {Level: "unit", Packages: map[string]string{"fixture/slow": digA}}, - buildstep.LevelIntegration: {Level: "integration", Packages: map[string]string{"fixture/slow": digB}}, - } - _ = one - slow := captured[Tested](t, "go-test-slow") - b.test = func(c stepCall) (any, error) { - r := slow - r.Level, _ = c.args["level"].(string) - return r, nil - } - b.steps.handlers = map[string]func(stepCall) (any, error){ - "build_go_fetch": func(stepCall) (any, error) { return b.fetch, nil }, - "build_node_fetch": func(stepCall) (any, error) { return b.fetch, nil }, - "build_go_digest": func(c stepCall) (any, error) { return echoBeside(b, b.digest[c.args["level"].(string)]), nil }, - "build_go_test": func(c stepCall) (any, error) { return b.test(c) }, - "build_node_test": func(c stepCall) (any, error) { return b.test(c) }, - } - now := time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC) - b.asks = &buildrun.FakeAsks{Answer: b.answer} - b.ex = &Examiner{Module: "acme-checker", Asks: buildrun.Asker{Seats: testSeats(), Publisher: b.asker(nil, nil), Waiter: b.asks, Bound: 5 * time.Second}, Toolchains: goEntries("shanks"), Kept: Kept{Store: b.kept}, Previews: b.prev, - Modules: langs{}, Seats: testSeats(), Trunks: &Trunks{heads: map[string]string{DeliveryRepository: trunkD}}, Inputs: memInputs{b.inputs}, Now: func() time.Time { now = now.Add(50 * time.Millisecond); return now }} - return b -} - -func caseOf(t testing.TB, pulls ...meshcatalog.CandidatesByGroupValuePullsItem) *Case { - t.Helper() - raw := candidateOf(t, "feat/x", pulls...) - c, err := meshcatalog.DecodeCandidatesByGroupValue(raw) - if err != nil { - t.Fatal(err) - } - cs := CaseOf(c, map[string]string{repoA: trunkA, repoSDK: trunkS, "novox/mesh-controller": trunkA, "novox/mesh-catalog": trunkA, - "novox/mesh-host": trunkA, "novox/mesh-networking": trunkA}) - if cs == nil { - t.Fatal("no case") - } - return cs -} - -func (b *bench) judge(t testing.TB, cs *Case) Outcome { - t.Helper() - out, err := b.ex.Judge(context.Background(), cs) - if err != nil { - t.Fatal(err) - } - return out -} - -func testedWith(t testing.TB, name string, edit func(*Tested)) Tested { - r := captured[Tested](t, name) - if edit != nil { - edit(&r) - } - return r -} - -func packagesOf(c stepCall) []string { - var out []string - if l, ok := c.args["packages"].([]string); ok { - out = l - } - return out -} - -// overlapAsks counts the asks put and not yet ended, and the most at once: a wait holds until a second ask is put, or a -// second passed, so asks put one after another never overlap. -type overlapAsks struct { - Asks - mu sync.Mutex - open, top int - more chan struct{} -} - -func (o *overlapAsks) Put(ctx context.Context, a buildstep.StepAsk) error { - o.mu.Lock() - o.open++ - if o.open > o.top { - o.top = o.open - } - if o.open >= 2 { - select { - case <-o.more: - default: - close(o.more) - } - } - o.mu.Unlock() - return o.Asks.Put(ctx, a) -} - -func (o *overlapAsks) Wait(ctx context.Context, a buildstep.StepAsk) (buildstep.Step, error) { - select { - case <-o.more: - case <-time.After(time.Second): - } - defer func() { o.mu.Lock(); o.open--; o.mu.Unlock() }() - return o.Asks.Wait(ctx, a) -} - -// Design 47 G A-304 (ADR 0525 §4): a phase puts every ask of its own at once and then waits for them all, and the -// verdict is judged in the order of the members as before. -func TestAPhasesAsksAreAllOutstandingAtOnce(t *testing.T) { - b := newBench(t) - o := &overlapAsks{Asks: b.ex.Asks, more: make(chan struct{})} - b.ex.Asks = o - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))).Verdict - if o.top < 2 { - t.Errorf("at most %d asks were outstanding at once, want both members' asks of the phase", o.top) - } - if v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("verdict = %+v", v) - } - if fetch := b.steps.tool("fetch"); len(fetch) != 2 { - t.Errorf("fetch calls = %+v, want one per member", fetch) - } -} - -func TestACleanMergeIsFetchedAtItsTrunkAndBothLevelsPass(t *testing.T) { // 1679/AC1 - b := newBench(t) - cs := caseOf(t, pullOf(repoA, 7, head1)) - out := b.judge(t, cs) - v := out.Verdict - if v.State != meshdeliverycheckerclient.VerdictPass || v.Levels[buildstep.LevelUnit].Run != 2 || v.Levels[buildstep.LevelIntegration].Run != 2 { - t.Fatalf("verdict = %+v", v) - } - if v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionNotRun { - t.Errorf("composition = %+v, want not run", v.Composition) - } - if len(v.Machines) != 1 || v.Machines[0] != "shanks" { - t.Errorf("machines = %v", v.Machines) - } - fetch := b.steps.tool("fetch") - if len(fetch) != 1 || fetch[0].args["repository"] != repoA || fetch[0].args["commit"] != head1 || fetch[0].args["base"] != trunkA || fetch[0].args["ref"] != "refs/pull/7/head" { - t.Fatalf("fetch calls = %+v", fetch) - } - if v.Took.Clone <= 0 || v.Took.Unit <= 0 { - t.Errorf("took = %+v, want the clone and the unit level timed", v.Took) - } - // What passed is kept under each level's digest. - for _, k := range []string{"unit." + digA, "integration." + digB} { - if _, _, found, _ := b.kept.Get(context.Background(), k); !found { - t.Errorf("%s was not kept", k) - } - } - - // ADR 0567 §12: every check ask carries every node's name for the names check; a check whose names cannot be read, or - // that reads none, is not asked and fails saying so. - for _, c := range []struct { - names []string - err error - fails string - }{{[]string{"anchor", "laptop"}, nil, ""}, {nil, nil, "no node record"}, {nil, errors.New("no bucket"), "no bucket"}} { - b := newBench(t) - b.ex.Nodes = namesOf{c.names, c.err} - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - asks := b.asks.Asks() - if c.fails != "" { - if out.Verdict.State != meshdeliverycheckerclient.VerdictFail || len(asks) != 0 || !strings.Contains(out.Verdict.Failed.Words, c.fails) { - t.Errorf("names %v, %v: verdict %+v, %d asks", c.names, c.err, out.Verdict, len(asks)) - } - } else if len(asks) == 0 || strings.Join(asks[0].Check.NodeNames, ",") != "anchor,laptop" { - t.Errorf("the check ask carries the names %v", asks) - } - } - - // Issue 1921 (B0, D4 to D6): the check's verdict is the checking program's, built at the group's mesh-delivery head, - // else at mesh-delivery's trunk, over the facts kept as its input; a program that is never begun, does not build or - // does not answer fails the check naming it, and a rule the change's program alters judges the check. - verdictOf := func(state, words string) json.RawMessage { - out, _ := json.Marshal(rules.Answer{V: rules.AnswerVersion, Verdict: meshdeliverychecker.VerdictsByGroupHeadsValue{State: state, Failed: &FailedFirst{Words: words}}}) - return out - } - for _, row := range []struct { - name string - repo string // the member's repository: mesh-delivery unless named - run func(a buildstep.StepAsk, s buildstep.Step) (buildstep.Step, bool) - at string // the commit the program is built at; empty when it fails - fails string - runs int // the run asks put when it fails: each put twice when never begun, and none after the seat list's - }{ - {name: "at the group's mesh-delivery head", at: head1}, - {name: "at mesh-delivery's trunk", repo: "novox/mesh-catalog", at: trunkD}, - {name: "never begun", run: func(buildstep.StepAsk, buildstep.Step) (buildstep.Step, bool) { return buildstep.Step{}, true }, - fails: "no build node began the job run of the asks", runs: 2}, - {name: "does not build", run: func(_ buildstep.StepAsk, s buildstep.Step) (buildstep.Step, bool) { - s.State, s.Reason = buildstep.StepFailed, "the program does not build: undefined: rules.Nothing" - return s, true - }, fails: "by the checking program: its run ask", runs: 1}, - // Design 47 G A-290: a run whose program exited other than 0 ends its key failed, its answer kept; the checker reads it. - {name: "a program that exits other than 0", run: func(_ buildstep.StepAsk, s buildstep.Step) (buildstep.Step, bool) { - s.State, s.Reason = buildstep.StepFailed, "the program exited 2: bad flag" - s.Result, _ = json.Marshal(buildstep.RunAnswer{Key: "program-0123456789abcdef", Built: true, Exit: 2, Said: "bad flag"}) - return s, true - }, fails: "it exited 2 on shanks: bad flag", runs: 1}, - {name: "a rule the change alters", at: head1, run: func(a buildstep.StepAsk, s buildstep.Step) (buildstep.Step, bool) { - if a.Run.Args[0] != "-verdict" { - return s, false - } - s.Result, _ = json.Marshal(buildstep.RunAnswer{Key: "program-0123456789abcdef", Output: verdictOf(meshdeliverycheckerclient.VerdictFail, "the change's own rule refuses it")}) - return s, true - }, fails: "the change's own rule refuses it"}, - } { - b := newBench(t) - b.ex.BeginBound = 20 * time.Millisecond - if row.run != nil { - b.run = func(a buildstep.StepAsk) (buildstep.Step, bool) { - return row.run(a, buildstep.Step{ID: a.ID, Tool: "check ask", Node: "shanks", State: buildstep.StepPassed}) - } - } - repo := cmp.Or(row.repo, repoA) - v := b.judge(t, caseOf(t, pullOf(repo, 7, head1))).Verdict - if row.fails != "" { - if v.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(v.Failed.Words, row.fails) { - t.Errorf("%s: verdict %+v, want a fail saying %q", row.name, v, row.fails) - } - if row.at == "" && (len(b.runs) != row.runs || v.JudgedBy != nil || slices.ContainsFunc(b.runs, func(a buildstep.StepAsk) bool { return a.Run.Args[0] == "-verdict" })) { - t.Errorf("%s: %d run asks, judged by %+v: want %d and none judging", row.name, len(b.runs), v.JudgedBy, row.runs) - } - } else if v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("%s: verdict %+v", row.name, v) - } - if row.at == "" { - continue - } - if by := v.JudgedBy; by == nil || by.Program.Commit != row.at || by.Program.Package != CheckingProgramPackage || by.Key == "" || - len(by.Heads) != 1 || by.Heads[0].Repository != repo || by.Heads[0].Commit != head1 { - t.Errorf("%s: judged by %+v, want the program at %s over the member's head", row.name, v.JudgedBy, row.at) - } - if len(b.runs) != 2 || b.runs[0].Run.Args[0] != "-seats" || b.runs[1].Run.Args[0] != "-verdict" { - t.Fatalf("%s: run asks %+v, want the seat list and then the verdict", row.name, b.runs) - } - in := b.runs[1].Run.Input // the facts kept as their exact bytes under their digest - body, _, found, _ := b.inputs.Get(context.Background(), in.Digest) - if !found || in.Verify(body) != nil || in.Verb != InputVerb || - !strings.Contains(string(body), `"group":"feat/x"`) { - t.Errorf("%s: the verdict's input %+v is not kept whole: %s", row.name, in, body) - } - // Review round 1 of milestone 47/B0: a level is carried as the rules read it, without its digest, its packages or - // its kept packages' names, so a group's facts stay within the input's bound. - for _, unread := range []string{`"digest":{`, `"packages":[`, `"unmet"`, `"package":"m/`} { - if strings.Contains(string(body), unread) { - t.Errorf("%s: the facts carry %s, which no rule reads: %s", row.name, unread, body) - } - } - } -} - -// namesOf is the nodes' names as a test gives them. -type namesOf struct { - names []string - err error -} - -func (n namesOf) Names(context.Context) ([]string, error) { return n.names, n.err } - -func TestOnlyThePackageWithoutAKeptVerdictRuns(t *testing.T) { // 1288/AC1 - b := newBench(t) - b.digest[buildstep.LevelUnit] = Digested{Level: "unit", Packages: map[string]string{"fixture/slow": digA, "fixture/other": digC}} - if _, err := (Kept{Store: b.kept}).Keep(context.Background(), "unit", digC, keptValue("fixture/other", 40)); err != nil { - t.Fatal(err) - } - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - tests := b.steps.tool("test") - if len(tests) != 2 || strings.Join(packagesOf(tests[0]), ",") != "fixture/slow" || tests[0].args["level"] != "unit" { - t.Fatalf("test calls = %+v, want the unit level of fixture/slow alone, then the integration level", tests) - } - if u := out.Verdict.Levels[buildstep.LevelUnit]; u.Run != 2 || u.Kept != 40 { - t.Errorf("unit = %+v, want 2 run and 40 kept", u) - } - - // Issue 890/AC5: a package needing node-build-node is left unmet where the node lacks it; the check keeps the passes - // and asks the integration level again for a node with it, puts it again when a node declines it, and that node tests - // the package alone, its kept passes not counted twice. - // fixture/outside has no kept verdict after its pass (it reads outside itself: its digest moves every call), so a - // placed ask that ran more than its unmet packages would run and count it again. - b = newBench(t) - b.declines = 1 - outside := 0 - b.steps.handlers["build_go_digest"] = func(c stepCall) (any, error) { - d := echoBeside(b, b.digest[c.args["level"].(string)]) - if d.Level == "integration" { - outside++ - d.Packages = map[string]string{"fixture/slow": digB, "fixture/node": digC, "fixture/outside": fmt.Sprintf("%064x", outside)} - } - return d, nil - } - b.digest[buildstep.LevelIntegration] = Digested{Level: "integration", - Needs: []buildstep.PackageNeeds{{Package: "fixture/node", Needs: []string{buildstep.NeedNodeBuildNode}}}} - out = b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - asks := b.asks.Asks() - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass || len(asks) != 3 || len(asks[0].Needs) != 0 { - t.Fatalf("verdict %+v, asks %+v: want a pass of three asks, the first naming no need", out.Verdict, asks) - } - for _, a := range asks { // ADR 0584: every check ask names the agreed toolchain, so a node off it declines - if a.Check.Toolchain == nil || a.Check.Toolchain.Version != "go1.27" || a.Check.Toolchain.Recipe != "7" { - t.Errorf("a check ask names toolchain %+v, want go1.27 recipe 7", a.Check.Toolchain) - } - } - for _, a := range asks[1:] { - if strings.Join(a.Needs, ",") != buildstep.NeedNodeBuildNode || strings.Join(a.Check.Levels, ",") != buildstep.LevelIntegration || strings.Join(a.Check.Packages, ",") != "fixture/node" { - t.Errorf("a placed ask %+v, want the integration level needing node-build-node", a) - } - } - tests = b.steps.tool("test") - if last := tests[len(tests)-1]; len(tests) != 3 || strings.Join(packagesOf(last), ",") != "fixture/node" || last.args["level"] != "integration" { - t.Errorf("test calls = %+v, want the unit level, the integration level of fixture/slow and fixture/outside, then fixture/node alone", tests) - } - slow := captured[Tested](t, "go-test-slow") - if i := out.Verdict.Levels[buildstep.LevelIntegration]; int(i.Run) != 2*slow.Tests || i.Kept != 0 { - t.Errorf("integration = %+v, want %d run and nothing counted kept", i, 2*slow.Tests) - } - // And where no node has the need, the check fails once the declines outlast the bound, naming the need. - b.kept, b.declines = newMemKV(), 1000 - b.ex.Kept, b.ex.DeclineBound = Kept{Store: b.kept}, 200*time.Millisecond - if v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict; v.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(failedOf(v).Words, "needs a node with node-build-node") { - t.Errorf("with no node having the need: %+v", v) - } -} - -func TestAReAskRunsOnlyWhatFailedAndWhatChanged(t *testing.T) { // 1288/AC2, AC3 - b := newBench(t) - failing := captured[Tested](t, "go-test-failing") - b.digest[buildstep.LevelUnit] = Digested{Level: "unit", Packages: map[string]string{"fixture/failing/bad": digA, "fixture/failing/good": digB}} - b.test = func(c stepCall) (any, error) { - if c.args["level"] == "unit" { - return failing, nil - } - return captured[Tested](t, "go-test-slow"), nil - } - cs := caseOf(t, pullOf(repoA, 7, head1)) - first := b.judge(t, cs).Verdict - if first.State != meshdeliverycheckerclient.VerdictFail || first.Failed.Test != "unit: fixture/failing/bad.TestThreeIsFour" { - t.Fatalf("first = %+v", first) - } - if _, _, found, _ := b.kept.Get(context.Background(), "unit."+digB); !found { - t.Fatal("the package that passed in the failing run was not kept") - } - if _, _, found, _ := b.kept.Get(context.Background(), "unit."+digA); found { - t.Fatal("the package that failed was kept") - } - // Asked again with nothing changed: only the failed package runs; the good one is taken. - b.steps.calls = nil - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - tests := b.steps.tool("test") - if len(tests) == 0 || strings.Join(packagesOf(tests[0]), ",") != "fixture/failing/bad" { - t.Fatalf("the re-ask ran %+v, want only fixture/failing/bad", tests) - } - // The trunk moved and one digest changed: that package runs, the unchanged one is taken. - b.digest[buildstep.LevelUnit] = Digested{Level: "unit", Packages: map[string]string{"fixture/failing/bad": digA, "fixture/failing/good": digD}} - b.steps.calls = nil - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - tests = b.steps.tool("test") - if got := strings.Join(packagesOf(tests[0]), ","); got != "fixture/failing/bad,fixture/failing/good" && got != "fixture/failing/good,fixture/failing/bad" { - t.Errorf("after a digest changed the run was %q, want the failed package and the changed one", got) - } -} - -func TestANodePackageIsNeverKeptAndRunsEveryCheck(t *testing.T) { // 1288/AC4, 1292/AC1 - b := newBench(t) - b.ex.Modules = langs{repoA: {nodeBuildModule}} - b.fetch = captured[Fetched](t, "node-fetch-clean") - b.test = func(c stepCall) (any, error) { - return Tested{Level: c.args["level"].(string), Module: "modules/m", Passed: true, Elapsed: 0.4, Bound: 5}, nil - } - cs := caseOf(t, pullOf(repoA, 7, head1)) - for i := 0; i < 2; i++ { - v := b.judge(t, cs).Verdict - if v.State != meshdeliverycheckerclient.VerdictPass || v.Levels[buildstep.LevelUnit].Kept != 0 || v.Levels[buildstep.LevelUnit].Run != 1 { - t.Fatalf("round %d: verdict = %+v", i, v) - } - } - if n := len(b.steps.tool("test")); n != 4 { - t.Errorf("%d node test calls in two checks, want 4 (both levels each time)", n) - } - if n := len(b.steps.tool("digest")); n != 0 { - t.Errorf("the node build module was asked for a digest %d times", n) - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 0 { - t.Errorf("kept %v for a node package", keys) - } -} - -// The operator, 2026-11-02 ("A failed check shows only its first fault"): a check runs every member, every level and the -// composition to its end and its verdict names every failure in the order judged, Failed keeping the first whole; the -// change's own tests still run first (issue 1289), and the rest of the level after them. A member that only failed its -// tests still gets its preview, so the composition is read and its failure named after the tests'. A member whose head -// does not merge has no preview of a merge, so the assigner's composition would only wait: the check does not wait for it -// and says why it was not run (review of 650a1d0). A member whose step failed after its fetch merged is previewed from the -// fetch the failed step answered, and the composition is read (design 47 G A-286). -func TestEveryFailureOfEveryMemberLevelAndTheCompositionIsNamed(t *testing.T) { - for _, kind := range []string{"tests", "conflicts", "errors"} { - conflicts, errs := kind == "conflicts", kind == "errors" - b := newBench(t) - comps := &fakeCompositions{v: refusedOn("x", "g14", "route-proxy is defined twice"), waits: conflicts} - b.ex.Compositions = comps - b.fetch.Changed = []string{"modules/m/internal/b/b_test.go"} - b.digest[buildstep.LevelUnit] = Digested{Level: "unit", Packages: map[string]string{"m/internal/a": digA, "m/internal/b": digB}} - conflict := captured[Fetched](t, "go-fetch-conflict") - b.steps.handlers["build_go_fetch"] = func(c stepCall) (any, error) { - if conflicts && c.args["repository"] == repoSDK { - return conflict, nil - } - return b.fetch, nil - } - failing := captured[Tested](t, "go-test-failing") - b.test = func(c stepCall) (any, error) { - if errs && c.args["level"] == "unit" { - return nil, errors.New("the seat definitions in . do not generate") - } - if got := packagesOf(c); c.args["level"] == "integration" || (len(got) == 1 && got[0] == "m/internal/b") { - r := failing - r.Level = c.args["level"].(string) - return r, nil - } - return captured[Tested](t, "go-test-slow"), nil - } - cs := caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2)) - done := make(chan meshdeliverychecker.VerdictsByGroupHeadsValue, 1) - go func() { done <- b.judge(t, cs).Verdict }() - var v meshdeliverychecker.VerdictsByGroupHeadsValue - select { - case v = <-done: - case <-time.After(5 * time.Second): - t.Fatalf("conflicts %v: the check waits on a composition that only waits", conflicts) - } - if errs { - keys, _ := b.prev.Keys(context.Background()) - if len(keys) != 2 || len(comps.asked) == 0 || v.Composition == nil || v.Composition.State == meshdeliverycheckerclient.CompositionNotRun { - t.Errorf("a step failed after its fetch merged: previews %v, asked %v, composition %+v, want both previews and the composition read", - keys, comps.asked, v.Composition) - } - continue - } - var got []string - for _, f := range v.Failures { - got = append(got, firstFailed(meshdeliverychecker.VerdictsByGroupHeadsValue{Failed: &FailedFirst{Test: f.Test, Machine: f.Machine, Words: f.Words}})) - } - tests := []string{"unit: fixture/failing/bad.TestThreeIsFour", "integration: fixture/failing/bad.TestThreeIsFour"} - want := append(append(slices.Clone(tests), tests...), "g14: not composed on g14: route-proxy is defined twice") // both members fail their tests - if conflicts { - want = append([]string{"shanks: novox/mesh-sdk#12 does not merge into the trunk: files in conflict: a.go"}, tests...) - } - if v.State != meshdeliverycheckerclient.VerdictFail || len(got) != len(want) { - t.Fatalf("conflicts %v: verdict %+v: failures\n%s\nwant\n%s", conflicts, v, strings.Join(got, "\n"), strings.Join(want, "\n")) - } - for i := range want { - if !strings.HasPrefix(got[i], want[i]) { - t.Errorf("conflicts %v: failure %d is %q, want %q", conflicts, i, got[i], want[i]) - } - } - keys, _ := b.prev.Keys(context.Background()) - switch { - case !conflicts && (len(keys) != 2 || v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionFail): - t.Errorf("tests alone failed: previews %v, composition %+v, want both previews and the composition read", keys, v.Composition) - case conflicts && (len(comps.asked) != 0 || v.Composition == nil || v.Composition.State != meshdeliverycheckerclient.CompositionNotRun || - !strings.Contains(v.Composition.Words, "novox/mesh-sdk#12 (its head does not merge)")): - t.Errorf("a member did not merge: asked %v, composition %+v, want not run naming novox/mesh-sdk#12", comps.asked, v.Composition) - } - var unit []string - for _, c := range b.steps.tool("test") { - if c.args["level"] == "unit" && c.args["workspace"] != nil { - unit = append(unit, strings.Join(packagesOf(c), ",")) - } - } - if conflicts && (strings.Join(unit, " | ") != "m/internal/b | m/internal/a" || len(b.steps.tool("digest")) != 2) { - t.Errorf("the unit level ran %v and %d digests, want the changed test's package, then the rest, at both levels", unit, len(b.steps.tool("digest"))) - } - v.Group, v.Heads = "feat/x", "0123456789ab" - if _, err := meshdeliverycheckerclient.EncodeVerdict(v); err != nil { - t.Errorf("conflicts %v: the verdict cannot be written: %v", conflicts, err) - } - if line := Line(v, cs, cs.Pulls[0], nil, nil, nil); !strings.HasPrefix(line, fmt.Sprintf("failed (%d): %s", len(want), want[0][:20])) || - !strings.Contains(line, fmt.Sprintf("; and %d more | ", len(want)-1)) || utf8.RuneCountInString(line) > MaxLine { - t.Errorf("conflicts %v: line %q", conflicts, line) - } - } -} - -func TestTheChangesOwnPassingTestRunsFirstAndTheRestFollow(t *testing.T) { // 1289/AC3 - b := newBench(t) - b.fetch.Changed = []string{"modules/m/internal/b/b_test.go", "modules/m/internal/a/a.go"} - b.digest[buildstep.LevelUnit] = Digested{Level: "unit", Packages: map[string]string{"m/internal/a": digA, "m/internal/b": digB}} - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", out.Verdict) - } - var unit []string - for _, c := range b.steps.tool("test") { - if c.args["level"] == "unit" { - unit = append(unit, strings.Join(packagesOf(c), ",")) - } - } - if strings.Join(unit, " | ") != "m/internal/b | m/internal/a" { - t.Errorf("the unit level ran %v, want the changed test's package first, then the rest", unit) - } -} - -func TestAHeadThatDoesNotMergeEndsFailedNamingTheFilesAndRunsNoTest(t *testing.T) { // 1289/AC4 - b := newBench(t) - b.fetch = captured[Fetched](t, "go-fetch-conflict") - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - f := out.Verdict.Failed - if out.Verdict.State != meshdeliverycheckerclient.VerdictFail || f.Machine != "shanks" || !strings.Contains(f.Words, "files in conflict: a.go, sub/b.go") { - t.Fatalf("verdict = %+v", out.Verdict) - } - if n := len(b.steps.tool("digest")) + len(b.steps.tool("test")); n != 0 { - t.Errorf("%d digest and test steps ran after a conflict", n) - } - // 1921/D1c: the node that ran the step is a fact, so the verdict judged from the facts names its machine. - if m := out.Verdict.Machines; len(m) != 1 || m[0] != "shanks" { - t.Errorf("machines = %v, want the node that ran the step", m) - } -} - -func TestALevelWhoseDigestIsNotNamedRunsWholeAndKeepsNothing(t *testing.T) { // 1287/AC4 - b := newBench(t) - b.steps.handlers["build_go_digest"] = func(stepCall) (any, error) { return nil, fmt.Errorf("go list failed") } - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", out.Verdict) - } - tests := b.steps.tool("test") - if len(tests) != 2 || tests[0].args["packages"] != nil { - t.Errorf("test calls = %+v, want each level once, whole", tests) - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 0 { - t.Errorf("kept %v with no digest", keys) - } -} - -func TestALevelOverItsBoundIsNamedWithItsSlowestTestsAndKeepsItsVerdict(t *testing.T) { // 1300/AC3 - b := newBench(t) - b.test = func(c stepCall) (any, error) { - return testedWith(t, "go-test-slow", func(r *Tested) { - r.Level = c.args["level"].(string) - r.OverBound = true - r.OverWhich = "run" - r.Elapsed = 7 - }), nil - } - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - v := out.Verdict - if v.State != meshdeliverycheckerclient.VerdictPass || strings.Join(v.OverBound, ",") != "unit,integration" { - t.Fatalf("verdict = %+v", v) - } - if len(out.Slowest) == 0 || !strings.Contains(out.Slowest[0], "TestTakesAWhile") { - t.Errorf("slowest = %v", out.Slowest) - } -} - -func TestSkippedTestsAreCountedPerLevel(t *testing.T) { // 1300/AC6, 1251 - b := newBench(t) - b.test = func(c stepCall) (any, error) { - return testedWith(t, "go-test-slow", func(r *Tested) { r.Level = c.args["level"].(string); r.Skipped = 3 }), nil - } - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if v.Levels[buildstep.LevelUnit].Skipped != 3 || v.Levels[buildstep.LevelIntegration].Skipped != 3 { - t.Errorf("levels = %+v", v.Levels) - } -} - -func TestAGroupIsOneVerdictAndEachMembersTestsReadTheOthersAtTheirHeads(t *testing.T) { // 1624/AC1 - b := newBench(t) - cs := caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2)) - v := b.judge(t, cs).Verdict - if v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", v) - } - fetch := b.steps.tool("fetch") - if len(fetch) != 2 { - t.Fatalf("%d fetches, want one for each member", len(fetch)) - } - var forA []BesideRef - for _, f := range fetch { - if f.args["repository"] == repoA { - forA, _ = refsOf(f.args["beside"]), true - } - } - found := false - for _, r := range forA { - if r.Repository == repoSDK && r.Commit == head2 { - found = true - } - } - if !found { - t.Errorf("mesh-delivery's tests were not given the SDK member at its head: %+v", forA) - } -} - -func TestAnExaminerIsAJudgeOfTheChecker(t *testing.T) { - var _ Judge = (*Examiner)(nil) -} - -const ( - manA = "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a" -) - -func TestACleanMergeWritesItsPreviewWithTheTreesManifestsAndLocks(t *testing.T) { // 1445/AC1 - b := newBench(t) - b.fetch.Trees = map[string]buildstep.ModuleTree{ - "modules/avahi": {Tree: "7777777777777777777777777777777777777777", Manifest: manA, Locks: map[string]string{"go.sum": "ff00"}}, - "modules/bare": {Tree: "8888888888888888888888888888888888888888"}, // no manifest digest: left out - } - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - key, _ := meshdeliverycheckerclient.PreviewKey(repoA, head1, trunkA) - raw, _, found, _ := b.prev.Get(context.Background(), key) - if !found { - t.Fatalf("no preview %s; the bucket holds %v", key, b.prev.vals) - } - pv, err := meshdeliverycheckerclient.DecodePreview(raw) - if err != nil { - t.Fatal(err) - } - m := pv.Modules["modules/avahi"] - if pv.Merge != b.fetch.Merge || pv.Trunk != trunkA || pv.Head != head1 || len(pv.Modules) != 1 || m.Tree != "7777777777777777777777777777777777777777" || m.Manifest != manA || m.Locks["go.sum"] != "ff00" { - t.Errorf("preview = %+v", pv) - } -} - -func TestAHeadThatDoesNotMergeWritesAPreviewOfItsConflictsAlone(t *testing.T) { // 1445/AC5 - b := newBench(t) - b.fetch = captured[Fetched](t, "go-fetch-conflict") - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - key, _ := meshdeliverycheckerclient.PreviewKey(repoA, head1, trunkA) - raw, _, found, _ := b.prev.Get(context.Background(), key) - if !found { - t.Fatalf("no preview for a head that does not merge; the bucket holds %v", b.prev.vals) - } - pv, err := meshdeliverycheckerclient.DecodePreview(raw) - if err != nil || strings.Join(pv.Conflicts, ",") != "a.go,sub/b.go" || pv.Merge != "" || len(pv.Modules) != 0 { - t.Errorf("preview = %+v, %v, want the files in conflict and no merge", pv, err) - } -} - -func TestAPreviewIsWrittenOnceAndAnExistingKeyStands(t *testing.T) { - b := newBench(t) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - b.fetch.Merge = "5656565656565656565656565656565656565656" - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - key, _ := meshdeliverycheckerclient.PreviewKey(repoA, head1, trunkA) - if b.prev.creates[key] != 1 { - t.Errorf("the preview was created %d times, want once", b.prev.creates[key]) - } - raw, _, _, _ := b.prev.Get(context.Background(), key) - if pv, _ := meshdeliverycheckerclient.DecodePreview(raw); pv.Merge == b.fetch.Merge { - t.Error("the second merge commit replaced the first") - } -} - -func TestTheIndexerIsToldOfAPreviewOnceWhenItIsCreated(t *testing.T) { // 1445/AC4 (the checker's half) - b := newBench(t) - var told []string - b.ex.Announce = func(_ context.Context, event, key string) { told = append(told, event+" "+key) } - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - key, _ := meshdeliverycheckerclient.PreviewKey(repoA, head1, trunkA) - if len(told) != 1 || told[0] != EventPreviewWritten+" "+key { - t.Errorf("told %v, want once of %s", told, key) - } -} - -type fakeBuilds map[string]builds.BuilderBuild - -func (f fakeBuilds) Build(_ context.Context, module, fp2 string) (builds.BuilderBuild, bool, error) { - b, ok := f[module+" "+fp2] - return b, ok, nil -} - -func TestTheOutcomeSaysIfEachCandidatesModuleIsBuiltFailedOrWaiting(t *testing.T) { // N7/AC1 (the checker reads the builder's keys) - b := newBench(t) - fpA := "src2:" + strings.Repeat("a", 64) - fpB := "src2:" + strings.Repeat("b", 64) - fpC := "src2:" + strings.Repeat("c", 64) - b.ex.Builds = fakeBuilds{ - "avahi " + fpA: {State: builds.BuildDone}, - "mesh-tools " + fpB: {State: builds.BuildFailed, Error: "boom"}, - // netcheck has no key yet - } - p := pullOf(repoA, 7, head1) - p.Modules = []string{"avahi", "mesh-tools", "netcheck", "nofp"} - p.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"avahi": {Fp2: fpA}, "mesh-tools": {Fp2: fpB}, "netcheck": {Fp2: fpC}, "nofp": {None2: "toolchain not yet reported"}} - out := b.judge(t, caseOf(t, p)) - var got []string - for _, n := range out.Builds { - got = append(got, n.Module+" "+n.State) - } - if strings.Join(got, ", ") != "avahi built, mesh-tools failed, netcheck waiting" { - t.Errorf("builds = %v, want one note for each module that has a fingerprint, sorted", got) - } -} - -// trunksOf are trunk heads of every repository, as the catalog half's trunk revision names them. -func trunksOf(t *testing.T, heads map[string]string) *Trunks { - t.Helper() - tr := &Trunks{} - tr.Set(heads) - return tr -} - -func TestNothingIsKeptForAWorkspaceGivenSiblingsWhenTheDigestDoesNotSayItCoversThem(t *testing.T) { // review D1, ADR 0444, issue 1287 - b := newBench(t) - // A build module whose digest does not echo the siblings (before DigestVersion 3). - b.steps.handlers["build_go_digest"] = func(c stepCall) (any, error) { return b.digest[c.args["level"].(string)], nil } - b.ex.Trunks = trunksOf(t, map[string]string{repoSDK: trunkS, "novox/mesh-catalog": trunkA}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if got := len(b.steps.tool("fetch")); got != 1 || b.steps.tool("fetch")[0].args["beside"] == nil { - t.Fatalf("the fetch was not given siblings: %+v", b.steps.tool("fetch")) - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 0 { - t.Errorf("kept %v for a package whose digest does not say it covers the siblings", keys) - } - // And a kept verdict of the same digest from elsewhere is not taken for it: everything runs. - if _, err := (Kept{Store: b.kept}).Keep(context.Background(), "unit", digA, keptValue("fixture/slow", 2)); err != nil { - t.Fatal(err) - } - b.steps.calls = nil - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if n := len(b.steps.tool("test")); n != 2 { - t.Errorf("%d test steps, want both levels run whole", n) - } -} - -// besideDigest stands for the Go build module's digest over a workspace with siblings (DigestVersion 3, lead decision L5): -// each package's digest folds the siblings' commits in, and the answer echoes the lines it folded. -// echoBeside is d with the siblings the fetches placed, as the Go build module's digest echoes them (its DigestResult's -// Beside): every check now places the change's own repository at its trunk (issue 1887 A12). -func echoBeside(b *bench, d Digested) Digested { - var lines []string - if fetches := b.steps.tool("fetch"); len(fetches) > 0 { // the workspace digested is the last fetch's (issue 890: a placed ask fetches again) - for _, r := range refsOf(fetches[len(fetches)-1].args["beside"]) { - lines = append(lines, r.Repository+" "+r.Commit) - } - } - sort.Strings(lines) - d.Beside = lines - return d -} - -func besideDigest(b *bench) func(c stepCall) (any, error) { - return func(c stepCall) (any, error) { - level := c.args["level"].(string) - var lines []string - for _, f := range b.steps.tool("fetch") { - refs, _ := refsOf(f.args["beside"]), true - for _, r := range refs { - lines = append(lines, r.Repository+" "+r.Commit) - } - } - sort.Strings(lines) - sum := sha256.Sum256([]byte(level + "\n" + strings.Join(lines, "\n"))) - return Digested{Level: level, Packages: map[string]string{"fixture/slow": hex.EncodeToString(sum[:])}, Beside: lines}, nil - } -} - -func TestACoreRepositorysPassingPackageIsKeptUnderADigestCoveringItsSiblings(t *testing.T) { // 1287 AC1-AC3, AC7 with L5 - b := newBench(t) - b.steps.handlers["build_go_digest"] = besideDigest(b) - b.ex.Trunks = trunksOf(t, map[string]string{repoSDK: trunkS, "novox/mesh-catalog": trunkA}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - keys, _ := b.kept.Keys(context.Background()) - if len(keys) != 2 { - t.Fatalf("kept %v, want the passing package at both levels", keys) - } - // The same siblings again: everything is taken, nothing runs. - b.steps.calls = nil - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if n := len(b.steps.tool("test")); n != 0 || v.Levels[buildstep.LevelUnit].Run != 0 || v.Levels[buildstep.LevelUnit].Kept == 0 { - t.Fatalf("%d test steps, unit %+v, want none run and the kept verdict taken", n, v.Levels[buildstep.LevelUnit]) - } - // A sibling's commit moves: a new digest, so the package runs again and is kept under it. - b.ex.Trunks = trunksOf(t, map[string]string{repoSDK: trunkS2, "novox/mesh-catalog": trunkA}) - b.steps.calls = nil - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if n := len(b.steps.tool("test")); n != 2 { - t.Errorf("%d test steps after a sibling moved, want both levels run", n) - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 4 { - t.Errorf("kept %v, want a second key per level under the new digest", keys) - } -} - -func TestADigestThatEchoesOtherSiblingsKeepsNothing(t *testing.T) { // 1287 with L5: an echo that differs is not trusted - b := newBench(t) - inner := besideDigest(b) - b.steps.handlers["build_go_digest"] = func(c stepCall) (any, error) { - v, err := inner(c) - d := v.(Digested) - d.Beside = d.Beside[:len(d.Beside)-1] - return d, err - } - b.ex.Trunks = trunksOf(t, map[string]string{repoSDK: trunkS, "novox/mesh-catalog": trunkA}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 0 { - t.Errorf("kept %v under a digest that does not cover every sibling", keys) - } -} - -type pkgPaths map[string][]TypeScriptPackage - -func (p pkgPaths) TypeScriptPackages(repository string) []TypeScriptPackage { return p[repository] } - -// contractsGit is a contracts package of mesh-delivery: one directory with its own package.json. -const contractsGit = "modules/mesh-delivery-contracts/ts/seats/git" - -// groupPackageBench is a group of a TypeScript member (mesh-delivery, with a contracts package) and the mesh-sdk member, -// whose merges change changed of each repository. -func groupPackageBench(t *testing.T, changed map[string][]string) *bench { - b := newBench(t) - b.ex.Modules = langs{repoA: {goBuildModule, nodeBuildModule}, repoSDK: {goBuildModule, nodeBuildModule}} - b.ex.Packages = pkgPaths{ - repoSDK: {{Dir: "", Paths: []string{"package-lock.json", "package.json", "src", "tsconfig.json"}}}, - repoA: {{Dir: contractsGit, Paths: []string{contractsGit}}}, - } - fetch := func(c stepCall) (any, error) { - f := b.fetch - f.Changed = changed[c.args["repository"].(string)] - return f, nil - } - b.steps.handlers["build_go_fetch"], b.steps.handlers["build_node_fetch"] = fetch, fetch - return b -} - -func nodeFetchOf(b *bench, repo string) stepCall { - for _, c := range b.steps.tool("fetch") { - if c.module == nodeBuildModule && c.args["repository"] == repo { - return c - } - } - return stepCall{} -} - -// design 47 G A-284, A-285 (ADR 0500 §5): every TypeScript fetch of a group's check is given every package any member's -// merge changes, each at its member's head with its directory there; the registry's only for a package none changes. -func TestATypeScriptMemberTakesEveryPackageTheGroupChanges(t *testing.T) { - sdk := map[string]any{"repository": repoSDK, "commit": head2, "ref": "refs/pull/12/head"} - git := map[string]any{"repository": repoA, "commit": head1, "ref": "refs/pull/7/head", "path": contractsGit} - for _, row := range []struct { - name string - changed map[string][]string - want []any - }{ - {"the SDK's sources", map[string][]string{repoSDK: {"go/delivery/x.go", "src/state.ts"}}, []any{sdk}}, - {"another member's contracts package", map[string][]string{repoA: {contractsGit + "/index.ts"}}, []any{git}}, - {"both", map[string][]string{repoSDK: {"package.json"}, repoA: {contractsGit + "/package.json", "go.mod"}}, []any{sdk, git}}, - {"Go alone", map[string][]string{repoSDK: {"go/delivery/x.go"}, repoA: {"modules/mesh-delivery-contracts/go/seats/git/contract.go"}}, nil}, - {"the manifest alone", map[string][]string{repoSDK: {"module.json"}}, nil}, - {"beside the named paths", map[string][]string{repoSDK: {"srcx/a.ts"}, repoA: {contractsGit + "x/a.ts"}}, nil}, - } { - b := groupPackageBench(t, row.changed) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))) - for _, repo := range []string{repoA, repoSDK} { - got, _ := nodeFetchOf(b, repo).args["group-packages"].([]any) - if !reflect.DeepEqual(got, row.want) { - t.Errorf("%s: the Node fetch of %s was given %v, want %v", row.name, repo, got, row.want) - } - if pkg, ok := nodeFetchOf(b, repo).args["package"]; ok { - t.Errorf("%s: the Node fetch of %s was given the retired package %v", row.name, repo, pkg) - } - } - } - // A lone pull request: no group package. - b := groupPackageBench(t, map[string][]string{repoA: {contractsGit + "/index.ts"}}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if got, ok := nodeFetchOf(b, repoA).args["group-packages"]; ok { - t.Errorf("a lone pull request's Node fetch was given %v", got) - } -} - -// design 47 G A-284: the mesh-sdk member is judged first, and its TypeScript fetch still learns the package another -// member's merge changes: every member's other fetches come before any TypeScript one. -func TestEveryMembersMergeIsKnownBeforeATypeScriptFetch(t *testing.T) { - b := groupPackageBench(t, map[string][]string{repoA: {contractsGit + "/index.ts"}}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))) - var order []string - for _, c := range b.steps.tool("fetch") { - order = append(order, c.module+" "+c.args["repository"].(string)) - } - want := []string{goBuildModule + " " + repoSDK, goBuildModule + " " + repoA, nodeBuildModule + " " + repoSDK, nodeBuildModule + " " + repoA} - if !reflect.DeepEqual(order, want) { - t.Errorf("fetches in the order %v, want %v", order, want) - } -} - -func TestNoStepOfACheckCarriesPublish(t *testing.T) { // ADR 0500 §4, 1309 (the checker's half): only the builder's trunk build publishes - b := groupPackageBench(t, map[string][]string{repoSDK: {"src/state.ts"}}) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))) - if len(b.steps.calls) == 0 { - t.Fatal("no step ran") - } - for _, c := range b.steps.calls { - if _, ok := c.args["publish"]; ok { - t.Errorf("%s was given publish: %v", c.tool, c.args) - } - if strings.HasSuffix(c.tool, "_build") || strings.HasSuffix(c.tool, "_pack") { - t.Errorf("the checker asked %s: a check builds no artifact of its own", c.tool) - } - } -} - -// 1890/AC1, 1887/A5a: an ask whose step never ends (the build node stopped with it running, and nothing redelivered it in -// time) ends the check loudly at the asker's bound; the checker holds no place to free. -func TestAnAskWhoseStepNeverEndsEndsTheCheckLoudly(t *testing.T) { - b := newBench(t) - // A node took the ask and its step stays running: the bound runs from its start (review round 2 item 1). - b.asks.Answer = func(a buildstep.StepAsk) buildstep.Step { - return buildstep.Step{ID: a.ID, Tool: "check ask", Node: "shanks", State: buildstep.StepRunning, Started: time.Now()} - } - b.ex.Asks = buildrun.Asker{Seats: testSeats(), Publisher: b.asker(nil, nil), Waiter: b.asks, Bound: 20 * time.Millisecond} - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if f := out.Verdict.Failed; out.Verdict.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(f.Words, "on shanks did not end within") { - t.Fatalf("verdict = %+v", out.Verdict) - } - if n := len(b.asks.Asks()); n != 1 || b.asks.Asks()[0].Kind != buildstep.AskCheck || b.asks.Asks()[0].Check == nil { - t.Fatalf("asks = %+v, want one check ask", b.asks.Asks()) - } -} - -// 1887/A5a, ADR 0536 §4: a check is one ask per pull request and build module on the check stream, a re-judge after a -// trunk move one on the recheck stream, each carrying both levels with the kept verdicts taken by the build module. -func TestACheckIsOneAskAndAReJudgeGoesOnTheRecheckStream(t *testing.T) { - b := newBench(t) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - asks := b.asks.Asks() - if len(asks) != 1 || asks[0].Kind != buildstep.AskCheck || asks[0].Language != buildstep.AskGo || asks[0].Asker != "acme-checker" { - t.Fatalf("asks = %+v, want one Go check ask", asks) - } - if j := asks[0].Check; j == nil || !j.Kept || strings.Join(j.Levels, ",") != "unit,integration" || j.Fetch.Repository != repoA { - t.Fatalf("the job = %+v", asks[0].Check) - } - // B2g: the job carries the toolchain the checker agreed (ADR 0584), with its language, so a node on another declines it. - if tc := asks[0].Check.Toolchain; tc == nil || *tc != (buildstep.ToolchainUsed{Language: "go", Version: "go1.27", Recipe: "7"}) { - t.Errorf("the job's toolchain = %+v, want the agreed go toolchain", tc) - } - cs := caseOf(t, pullOf(repoA, 7, head1)) - cs.Kind = KindTrunk - b.judge(t, cs) - if asks := b.asks.Asks(); len(asks) != 2 || asks[1].Kind != buildstep.AskRecheck { - t.Fatalf("asks = %+v, want the re-judge on recheck", asks) - } -} - -// Review round 2 item 1: a check ask that waits in its stream longer than the step bound (no node has a free slot during a -// large rebuild) is not failed: the check goes on when a node takes it, and no second ask is put. -func TestACheckAskWaitingInItsStreamIsNotBounded(t *testing.T) { - b := newBench(t) - answer := b.asks.Answer - b.asks.Answer = nil - b.ex.Asks = buildrun.Asker{Seats: testSeats(), Publisher: b.asker(nil, nil), Waiter: b.asks, Bound: 20 * time.Millisecond} - done := make(chan Outcome, 1) - go func() { done <- b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) }() - for deadline := time.Now().Add(10 * time.Second); len(b.asks.Asks()) == 0; time.Sleep(time.Millisecond) { - if time.Now().After(deadline) { - t.Fatal("no ask was put") - } - } - time.Sleep(100 * time.Millisecond) // five bounds in the stream - select { - case out := <-done: - t.Fatalf("the check ended while its ask waited: %+v", out.Verdict) - default: - } - b.asks.End(answer(b.asks.Asks()[0])) // a node took it and ran it - if out := <-done; out.Verdict.State != meshdeliverycheckerclient.VerdictPass || len(b.asks.Asks()) != 1 { - t.Fatalf("verdict %+v after %d asks, want a pass from the one ask", out.Verdict, len(b.asks.Asks())) - } -} - -// 1917: the digest is taken inside the check ask, as the build module's own digest step answers it (no step key is asked -// of it), so a second check of the same heads keeps every package the first one passed: "N kept", nothing run again. -func TestASecondCheckOfTheSameHeadsKeepsWhatTheFirstPassed(t *testing.T) { - b := newBench(t) - first := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - u := first.Levels[buildstep.LevelUnit] - if first.State != meshdeliverycheckerclient.VerdictPass || u.Run == 0 || u.Kept != 0 { - t.Fatalf("the first check = %+v", first) - } - b.steps.calls = nil - second := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))).Verdict - if s := second.Levels[buildstep.LevelUnit]; second.State != meshdeliverycheckerclient.VerdictPass || s.Kept != u.Run || s.Run != 0 { - t.Fatalf("the second check's unit level = %+v, want %d kept and none run", s, u.Run) - } - if n := len(b.steps.tool("test")); n != 0 { - t.Errorf("%d test steps ran on the second check", n) - } -} - -// keptList is the indexer's kept predefined list as a test gives it. -type keptList string - -func (k keptList) PredefinedList(context.Context) (json.RawMessage, error) { - return json.RawMessage(k), nil -} - -// fixedManifests are the indexer's kept manifests by digest as a test gives them. -type fixedManifests map[string]catalogue.Manifest - -func (f fixedManifests) Manifest(_ context.Context, digest string) (catalogue.Manifest, error) { - m, ok := f[digest] - if !ok { - return catalogue.Manifest{}, errors.New("no manifest " + digest) - } - return m, nil -} - -// fixedDigests are the indexer's manifest digests as a test gives them. -type fixedDigests map[string]string - -func (f fixedDigests) ManifestDigests() map[string]string { return f } - -func (f fixedDigests) ManifestFingerprints() map[string]string { - out := map[string]string{} - for m := range f { - out[m] = "src2:" + m - } - return out -} - -// ADR 0578 §2: a change's manifests are judged by the indexer's judge, built at mesh-delivery's trunk and run as the -// composer is (a compose ask of its command); a refusal fails the check naming the manifest and each problem; a group -// whose mesh-delivery member changes the judge is judged by that member's head. -func TestTheIndexersJudgeJudgesTheChangesManifests(t *testing.T) { - web, trunk := strings.Repeat("e", 64), strings.Repeat("d", 40) - for _, row := range []struct { - name string - refused string - fails string - // untouched is the judge's refusal of mail, which the change leaves as it is, and trunkMail its refusal on the trunk: - // a problem the trunk had is a warning, one the change causes fails (ADR 0579 §2) - untouched, trunkMail string - // cycle reads the manifests, so the change's module graph is worked out (ADR 0603 §2): with "provisions" the change's - // web awaits mail's site while mail awaits web's database, and trunkCycle has the trunk's web await it too; with "bases" - // each is built on the other; with "verbs" each invokes the other's tool, and with "a verb and a provision" web invokes - // mail's tool while mail awaits web's database: both left out, so neither is refused (ADR 0614 §3, §6). - cycle string - trunkCycle bool - // series is the series of web's package the change raises it to, and the words its status line names (DLV-C4). - series, seriesWords string - // renames is what the change does to web's renamed-from ("bring", "drop") and what the seat mesh-config's verb - // renames answers ("served", "open" on g14, "not served", "unasked" with no seat to ask, "error"); DLV-C5. - renames, seat string - // unparsed is the indexer's words of why the pull request's head's seat definition of web-api does not parse. - unparsed string - // known are the judge's known packages the request hands it (DLV-I14, design 32): the packages state's names and, - // of web's package new in the group, the name its go.mod says at the member's head. - known []string - // emptyState is a packages state answered and empty, no member declaring a package: the judge is handed an empty - // list, never nil, so every name a manifest's packages names is refused (review round 1, DLV-I14). - emptyState bool - // indexer makes web the holder of the seat mesh-catalog, and the trunk's manifest validator answers for it: "held" with no verdict, - // "aside" with a field it does not know; ownJudge has the group change the judge, so the manifest is also judged by the - // trunk's, in a second ask (ADR 0633 clause 6, 1585/T22, T23) - indexer string - ownJudge bool - // seats is the seat list the checker holds: "read" gives the judge its two definitions as the request's Seats, "unread" - // is a reader the seat has not answered, and the manifests are then judged by nothing (ADR 0636 §1). - seats string - // try judges the same change as a whole group try, whose manifests come from the indexer's candidate of the try's - // group (issue 1921): the same judge, the same verdicts. - try bool - // heads has the change touch a contracts module too, whose head defines the Delivery seat mesh-catalog with a verb of - // a part it includes: "domain" is the domain's contracts module, so the judges get that definition over the list's - // (ADR 0636 §4, ADR 0633 §6); "outside" is mesh-contracts, whose definition of another domain's seat is not taken - // (ADR 0636 §5); "misnamed" is the domain's module whose seats/mesh-catalog/seat.json names another seat. - heads string - // headSeats lays heads that add a seat: the checking program's seat list is carried by every judge ask's request (ADR - // 0636 §4, issue 1921 D4). - headSeats bool - }{ - {name: "the seat list read is the request's Seats", seats: "read"}, - {name: "the members' heads' definitions over the list's, for the trunk's manifest validator too", seats: "read", indexer: "ok", heads: "domain"}, - {name: "a head's definition of a seat outside its module's domain is not taken", seats: "read", indexer: "ok", heads: "outside"}, - {name: "a head's definition naming another seat than its directory's fails the judging", seats: "read", heads: "misnamed", - fails: `names the seat "other", not "mesh-catalog"`}, - {name: "the seat list not read judges nothing", seats: "unread", - fails: "the change's manifests cannot be judged: the seat list is not read (the seat list is held from the bootstrap"}, - {name: "a seat definition that does not parse", unparsed: "seats/web-api/seat.json: unexpected end of JSON input", - fails: "novox/mesh-delivery#7: the seat definition of web-api (module web-contracts, go) does not parse: seats/web-api/seat.json: unexpected end of JSON input"}, - {name: "a rename brought, the verb not served", renames: "bring", seat: "not served", - fails: "web (novox/mesh-delivery#7) renames new-key from old-key: refused, the holder of the seat mesh-config does not serve its verb renames"}, - {name: "a rename brought, the verb served", renames: "bring", seat: "served"}, - {name: "a renamed-from dropped while its window is open", renames: "drop", seat: "open", - fails: "web (novox/mesh-delivery#7) drops renamed-from old-key of new-key while its rename window is open on g14"}, - {name: "a renamed-from dropped once its window closed", renames: "drop", seat: "served"}, - {name: "a rename the check cannot ask of", renames: "bring", seat: "error", fails: "the check cannot ask seat:mesh-config.renames: the bus said no"}, - {name: "a rename with no seat to ask", renames: "drop", seat: "unasked", fails: "this checker cannot ask seat:mesh-config.renames"}, - {name: "accepted"}, - {name: "a package series raised", series: "0.2", seriesWords: "web/pkg series 0.1 → 0.2"}, - {name: "a package series kept", series: "0.1"}, - {name: "a package new in the group", series: "0.1", known: []string{"git.novox.be/novox/mesh-sdk/go", "git.novox.be/novox/web/go"}}, - {name: "an answered packages state holding no name", emptyState: true, known: []string{}}, - {name: "the heads' seat list is carried by the head's judge and the trunk's", headSeats: true, indexer: "ok", ownJudge: true}, - {name: "accepted in a group try", try: true}, - {name: "refused in a group try", try: true, refused: "web has no id", fails: "the indexer's judge at mesh-delivery's trunk " + strings.Repeat("d", 40) + ` refuses the manifest of web: web has no id`}, - {name: "a cycle the group try makes", try: true, cycle: "provisions", fails: "the change's module graph has the cycle mail -> web -> mail"}, - {name: "the indexer accepted by the trunk's manifest validator", indexer: "ok"}, - {name: "the indexer held by the trunk's manifest validator", indexer: "held", fails: "the indexer web, the holder of the seat mesh-catalog, would never run: the trunk's manifest validator at mesh-delivery's trunk " + strings.Repeat("d", 40) + " does not accept its manifest: the manifest validator holds it, with no verdict yet"}, - {name: "the indexer with a field the trunk's manifest validator does not know", indexer: "aside", fails: "the manifest validator does not know its fields new_field, so it waits"}, - {name: "the indexer refused by the trunk's manifest validator", indexer: "refused", refused: "web has no id", fails: "does not accept its manifest: web has no id"}, - {name: "the indexer under a changed judge is judged by the trunk's as well", indexer: "aside", ownJudge: true, fails: "the manifest validator does not know its fields new_field, so it waits"}, - {name: "a touched manifest the judge holds", indexer: "heldplain", fails: "holds the manifest of web: it gave no verdict"}, - {name: "an untouched manifest's problem the trunk had", untouched: "mail says no credential form", trunkMail: "mail says no credential form"}, - {name: "a problem the change causes in an untouched manifest", untouched: "mail reads a state web no longer keeps", - fails: "refuses the manifest of mail: mail reads a state web no longer keeps"}, - {name: "a cycle the change makes", cycle: "provisions", fails: "the change's module graph has the cycle mail -> web -> mail"}, - {name: "a cycle the trunk has", cycle: "provisions", trunkCycle: true}, - {name: "a cycle of bases", cycle: "bases", fails: "the change's module graph has the cycle mail -> web -> mail"}, - {name: "a cycle of verbs", cycle: "verbs"}, - {name: "a cycle of a verb and a provision", cycle: "a verb and a provision"}, - {name: "refused", refused: "web requires \"amqp\"\nweb has no id", fails: "the indexer's judge at mesh-delivery's trunk " + strings.Repeat("d", 40) + ` refuses the manifest of web: web requires "amqp"; web has no id`}, - } { - b := newBench(t) - if row.headSeats { - b.heads = fstest.MapFS{ - repoA + "/modules/x-contracts/module.json": {Data: []byte(`{"module": "x-contracts", "build": {"artifacts": [{"name": "go", "kind": "package", "language": "go", "generated-from": "."}]}}`)}, - repoA + "/modules/x-contracts/seats/node-x/seat.json": {Data: []byte(`{"format":1,"seat":"node-x","scope":"node","grain":"whole","domain":"Delivery","decision":"ADR 1"}`)}, - } - } - b.ex.Manifests = fixedDigests{"mail": strings.Repeat("a", 64), "web": strings.Repeat("b", 64)} - b.ex.Trunks = fixedTrunks{heads: map[string]string{DeliveryRepository: trunk}} - b.ex.Predefined = keptList(`{"font-size": {"type": "number", "description": "the size"}}`) - if row.cycle != "" { - offer := func(name string) []catalogue.Offer { - return []catalogue.Offer{{Name: name, Scope: catalogue.ScopeMesh, AnsweredBy: catalogue.AnsweredByHelper}} - } - on := func(module string) *catalogue.Build { - return &catalogue.Build{On: []catalogue.BuildsOn{{Arg: "BASE", Module: module, Artifact: "img"}}} - } - trunkWeb := catalogue.Manifest{Module: "web", Provides: offer("database")} - if row.trunkCycle { - trunkWeb.Requires = []string{"site"} - } - mail := catalogue.Manifest{Module: "mail", Provides: offer("site"), Requires: []string{"database"}} - changed := catalogue.Manifest{Module: "web", Provides: offer("database"), Requires: []string{"site"}} - switch row.cycle { - case "bases": - mail = catalogue.Manifest{Module: "mail", Build: on("web")} - changed = catalogue.Manifest{Module: "web", Build: on("mail")} - case "verbs": - mail = catalogue.Manifest{Module: "mail", Tools: []string{"mail_send"}, Invokes: []string{"web.web_page"}} - changed = catalogue.Manifest{Module: "web", Tools: []string{"web_page"}, Invokes: []string{"mail.mail_send"}} - case "a verb and a provision": - mail = catalogue.Manifest{Module: "mail", Tools: []string{"mail_send"}, Requires: []string{"database"}} - changed = catalogue.Manifest{Module: "web", Provides: offer("database"), Invokes: []string{"mail.mail_send"}} - } - b.ex.Read = fixedManifests{strings.Repeat("a", 64): mail, strings.Repeat("b", 64): trunkWeb, web: changed} - } - if row.series != "" { - pkg := func(series string) *catalogue.Build { - return &catalogue.Build{Artifacts: []catalogue.Artifact{{Name: "pkg", Kind: catalogue.ArtifactPackage, Language: "go", Series: series}}} - } - b.ex.Read = fixedManifests{strings.Repeat("a", 64): {Module: "mail"}, strings.Repeat("b", 64): {Module: "web", Build: pkg("")}, - web: {Module: "web", Build: pkg(row.series)}} - } - if row.known != nil { - b.ex.KnownPackages = knownNames{"git.novox.be/novox/mesh-sdk/go": "mesh-sdk/go"} - b.ex.Read = fixedManifests{strings.Repeat("a", 64): {Module: "mail"}, strings.Repeat("b", 64): {Module: "web"}, web: {Module: "web", - Build: &catalogue.Build{Artifacts: []catalogue.Artifact{{Name: "pkg", Kind: catalogue.ArtifactPackage, Language: "go", Paths: []string{"go"}}}}}} - b.ex.Forge = headFiles{repoA + " modules/web/go/go.mod " + head1: "module git.novox.be/novox/web/go\n\ngo 1.26\n"} - } - if row.emptyState { - b.ex.KnownPackages = knownNames{} - b.ex.Read = fixedManifests{strings.Repeat("a", 64): {Module: "mail"}, strings.Repeat("b", 64): {Module: "web"}, web: {Module: "web"}} - } - if row.renames != "" { - renamed := map[string]catalogue.SettingDeclaration{"new-key": {Type: catalogue.ConfigString, RenamedFrom: "old-key"}} - plain := map[string]catalogue.SettingDeclaration{"new-key": {Type: catalogue.ConfigString}} - trunkWeb, headWeb := catalogue.Manifest{Module: "web", Settings: plain}, catalogue.Manifest{Module: "web", Settings: renamed} - if row.renames == "drop" { - trunkWeb, headWeb = headWeb, trunkWeb - } - b.ex.Read = fixedManifests{strings.Repeat("a", 64): {Module: "mail"}, strings.Repeat("b", 64): trunkWeb, web: headWeb} - // The verb faked from its definition: the answer of the seat's fixture (mesh-contracts seats/mesh-config). - fake := fakeRenames{} - switch row.seat { - case "not served": - fake.err = fmt.Errorf("%w (nothing answers it)", ErrRenamesNotServed) - case "error": - fake.err = errors.New("the bus said no") - case "open": - fake.answer = meshconfig.RenamesAnswer{Renames: []meshconfig.RenamesAnswerRenamesItem{{Module: "web", From: "old-key", To: "new-key", At: "2026-11-04T12:00:00Z", Open: []string{"g14"}}}} - } - if row.seat != "unasked" { - b.ex.Renames = fake - } - } - if row.unparsed != "" { - b.ex.SeatDefinitions = fakeSeatDefinitions{"novox/mesh-delivery#7": {{Seat: "web-api", Module: "web-contracts", Directory: "go", Why: row.unparsed}}} - } - if row.indexer != "" && row.indexer != "heldplain" { - b.ex.Read = fixedManifests{strings.Repeat("a", 64): {Module: "mail"}, strings.Repeat("b", 64): {Module: "web"}, - web: {Module: "web", Claims: []catalogue.Claim{{Name: CatalogSeat}}}} - } - if row.seats != "" { - b.ex.SeatList = fakeSeatList{read: row.seats == "read"} - } - var judged []buildstep.ComposeJob - answer := b.asks.Answer - b.asks.Answer = func(a buildstep.StepAsk) buildstep.Step { - if a.Compose == nil { - return answer(a) - } - judged = append(judged, *a.Compose) - read := composer.ManifestRead{Accepted: row.refused == "", Refused: row.refused} - switch row.indexer { - case "held", "heldplain": - read = composer.ManifestRead{} - case "aside": - read.SetAside = "new_field" - } - verdict, _ := json.Marshal(composer.Verdict{V: composer.Version, SDK: "exe:1", Cases: []composer.CaseVerdict{{Name: JudgeCase, - Manifests: map[string]composer.ManifestRead{"web": read, "mail": {Accepted: row.untouched == "", Refused: row.untouched}}}, - {Name: JudgeTrunk, Manifests: map[string]composer.ManifestRead{"web": {Accepted: true}, "mail": {Accepted: row.trunkMail == "", Refused: row.trunkMail}}}}}) - result, _ := json.Marshal(buildstep.ComposeAnswer{Key: "k", Verdict: verdict}) - return buildstep.Step{ID: a.ID, State: buildstep.StepPassed, Result: result} - } - p := pullOf(repoA, 7, head1) - if row.ownJudge { - p = pullOf(DeliveryRepository, 7, head1) - b.fetch.Changed = []string{JudgeDir + "/judge/judge.go"} - } - p.Modules = []string{"web"} - if row.heads != "" { - contracts, seat := "mesh-delivery-contracts", "mesh-catalog" - switch row.heads { - case "outside": - contracts = "mesh-contracts" - case "misnamed": - seat = "other" - } - dir := "modules/" + contracts - p.Modules = append(p.Modules, contracts) - // The checking program reads the heads the build node lays for its run (D6): the running checker reads none. - b.heads = fstest.MapFS{ - repoA + "/" + dir + "/module.json": {Data: []byte(`{"module": "` + contracts + `", "build": {"artifacts": [{"name": "go", "kind": "package", "language": "go", "generated-from": "."}]}}`)}, - repoA + "/" + dir + "/seats/mesh-catalog/seat.json": {Data: []byte(`{"format":1,"seat":"` + seat + `","scope":"mesh","grain":"whole","domain":"Delivery","decision":"ADR 1","includes":["lists"],` + - `"verbs":[{"name":"modules","input":{"type":"object"},"answer":{"type":"object"}}]}`)}, - repoA + "/" + dir + "/seats/mesh-catalog/fixtures/a.json": {Data: []byte(`{}`)}, - repoA + "/" + dir + "/parts/lists.json": {Data: []byte(`{"format":1,"part":"lists","verbs":[{"name":"seat-definitions","input":{"type":"object"},"answer":{"type":"object"}}]}`)}, - } - } - p.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"web": {Fp2: "src2:" + strings.Repeat("f", 64), Manifest: web}} - // Issue 1921: the judge is built at the group's heads too, also a member that publishes no package yet. - cs := caseOf(t, p, pullOf("novox/mesh-networking", 9, head2)) - if row.try { - cs = TryCase("try-1", Try{Members: []TryMember{{Repository: repoA, Head: head1, Trunk: trunkA}}}, nil) - b.ex.Candidates = &fakeTryCandidates{cand: meshcatalog.CandidatesByGroupValue{Fpset: "fps", Pulls: []meshcatalog.CandidatesByGroupValuePullsItem{p}}} - } - v := b.judge(t, cs).Verdict - if line := Line(v, cs, cs.Pulls[0], nil, nil, nil); row.seriesWords != "" && !strings.Contains(line, row.seriesWords) || row.seriesWords == "" && strings.Contains(line, "series") { - t.Errorf("%s: the line %q, want it to name %q", row.name, line, row.seriesWords) - } - // The join (A2j finding 2): the verdict names each series change as {module, artifact, from, to}. - if want := []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem(nil); row.series == "0.2" { - if want = []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem{{Module: "web", Artifact: "pkg", From: "0.1", To: "0.2"}}; !reflect.DeepEqual(v.Series, want) { - t.Errorf("%s: the verdict's series %+v, want %+v", row.name, v.Series, want) - } - } else if len(v.Series) != 0 { - t.Errorf("%s: the verdict's series %+v, want none", row.name, v.Series) - } - if row.indexer == "ok" && !slices.ContainsFunc(cs.notes, func(n string) bool { - return strings.Contains(n, "the new indexer's manifest of web is accepted by the trunk's manifest validator") - }) { - t.Errorf("%s: the case says %v, want the new indexer's manifest said accepted by the trunk's manifest validator", row.name, cs.notes) - } - if row.seats == "unread" || row.heads == "misnamed" { - if v.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(v.Failed.Words, row.fails) || len(judged) != 0 { - t.Errorf("%s: %+v, judged %d, want a fail saying %q before any judge", row.name, v.Failed, len(judged), row.fails) - } - continue - } - // Issue 1921: the judge of the change is built at the group's heads (also a member that publishes no package yet), but - // the trunk's manifest validator that rules on a new indexer is built at the latest published versions alone, never - // at a group's head (ADR 0633 clause 6): a group with a member of its own then judges the indexer a second time. - trunkRuns := 0 - if row.indexer != "" && row.indexer != "heldplain" && (row.ownJudge || !row.try) { - trunkRuns = 1 - } - if want := 1 + trunkRuns; len(judged) != want { - t.Fatalf("%s: %d judge asks %+v, want %d", row.name, len(judged), judged, want) - } - for i, j := range judged { - pkgs := j.Fetch.Packages - atGroup := pkgs["git.novox.be/novox/mesh-networking"] == head2 - switch trunkRun := trunkRuns == 1 && i == len(judged)-1; { - case row.try: - case trunkRun && atGroup: - t.Errorf("%s: the trunk's manifest validator is built with the group's head: %v", row.name, pkgs) - case !trunkRun && !atGroup: - t.Errorf("%s: the judge at %v is fetched with the packages %v, want the group's mesh-networking member at its head %s", row.name, j.Fetch.Commit, pkgs, head2) - } - } - if row.ownJudge && (len(judged) != 2 || judged[0].Fetch.Commit != head1 || judged[1].Fetch.Commit != trunk) { - t.Fatalf("%s: judged %+v, want the head's judge and then the trunk's", row.name, judged) - } - if row.ownJudge { - judged = judged[1:] - } else if trunkRuns == 1 { - judged = judged[1:] // the group's run; the trunk's is the one read below - } - if len(judged) != 1 || judged[0].Fetch.Repository != DeliveryRepository || judged[0].Fetch.Commit != trunk || - judged[0].Package != JudgePackage || judged[0].Dir != JudgeDir { - t.Fatalf("%s: judged %+v (verdict %+v)", row.name, judged, v) - } - for _, j := range judged { - var seated composer.Request - if err := json.Unmarshal(j.Request, &seated); err != nil || (len(seated.Seats) == 1) != row.headSeats || - (row.headSeats && !strings.Contains(string(seated.Seats[0]), `"node-x"`)) { - t.Errorf("%s: the judge at %v is handed the seats %s, want the heads' list: %v", row.name, j.Fetch.Commit, seated.Seats, err) - } - } - var req composer.Request - if err := json.Unmarshal(judged[0].Request, &req); err != nil || req.Cases[0].Manifests["web"].Digest != web || - req.Cases[1].Name != JudgeTrunk || req.Cases[1].Manifests["web"].Digest != strings.Repeat("b", 64) || - req.Predefined["font-size"].Type != "number" || - req.Cases[0].Manifests["mail"].Digest != strings.Repeat("a", 64) { - t.Fatalf("%s: the request %+v: %v", row.name, req, err) - } - if row.heads != "" { - // Review round 2 of G (programs.go's digest side): the checking program's -seats input carries the digest of - // every definition the reader holds, never the list, and its answer is laid over the list (below). - var digests map[string]string - b.inputs.mu.Lock() - for _, body := range b.inputs.vals { - var f rules.Facts - if json.Unmarshal(body, &f) == nil && f.SeatDigests != nil { - digests = f.SeatDigests - } - } - b.inputs.mu.Unlock() - if want := map[string]string{"a": rules.SeatDigest(json.RawMessage(`{"seat":"a"}`)), "b": rules.SeatDigest(json.RawMessage(`{"seat": "b"}`))}; !reflect.DeepEqual(digests, want) { - t.Errorf("%s: the -seats input carries the digests %v, want the reader's list's %v", row.name, digests, want) - } - var got []string - for _, raw := range req.Seats { - var d struct { - Seat string `json:"seat"` - Verbs []struct { - Name string `json:"name"` - } `json:"verbs"` - } - _ = json.Unmarshal(raw, &d) - for _, v := range d.Verbs { - d.Seat += "." + v.Name - } - got = append(got, d.Seat) - } - want := []string{"a", "b", "mesh-catalog.seat-definitions.modules"} - if row.heads == "outside" { - want = want[:2] - } - if !slices.Equal(got, want) { - t.Errorf("%s: the trunk's manifest validator's seats %v, want %v: the list's with the head's definition, its part resolved", row.name, got, want) - } - } else if row.seats == "read" && (len(req.Seats) != 2 || string(req.Seats[0]) != `{"seat":"a"}` || string(req.Seats[1]) != `{"seat":"b"}`) { - t.Errorf("%s: the request's seats %s, want the two definitions the reader holds", row.name, req.Seats) - } - if row.seats == "" && !row.headSeats && len(req.Seats) != 0 { - t.Errorf("%s: the request's seats %s, want none from a checker that follows no list", row.name, req.Seats) - } - if !reflect.DeepEqual(req.Packages, row.known) || len(req.Input) != 0 && string(req.Input) != "null" { - t.Errorf("%s: the judge's known packages %v and input %s, want the known packages %v and no input", row.name, - req.Packages, req.Input, row.known) - } - switch { - case row.fails == "" && v.State != meshdeliverycheckerclient.VerdictPass: - t.Errorf("%s: %+v", row.name, v) - case row.fails != "" && (v.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(v.Failed.Words, row.fails)): - t.Errorf("%s: %+v, want a fail saying %q", row.name, v.Failed, row.fails) - } - } - if !changesJudge(DeliveryRepository, []Fetched{{Changed: []string{JudgeDir + "/judge/judge.go"}}}) || - changesJudge(DeliveryRepository, []Fetched{{Changed: []string{JudgeDir + "/internal/index/x.go"}}}) || - changesJudge("novox/mesh-catalog", []Fetched{{Changed: []string{JudgeDir + "/judge/judge.go"}}}) { - t.Error("which merge changes the judge") - } -} - -// fakeSeatList is the seat list reader as a test gives it: two definitions once read, else the reader's words. -type fakeSeatList struct{ read bool } - -func (f fakeSeatList) Definitions() ([]json.RawMessage, error) { - if !f.read { - return nil, errors.New("the seat list is held from the bootstrap (2 seats) and nothing new is acted on") - } - return []json.RawMessage{json.RawMessage(`{"seat":"a"}`), json.RawMessage(`{"seat":"b"}`)}, nil -} - -// knownNames are the packages state's names as a test gives them, each with the / it is built from. -type knownNames map[string]string - -func (k knownNames) PackageNames() map[string]string { return k } - -// fakeRenames is the seat mesh-config's verb renames as its definition answers it. -type fakeRenames struct { - answer meshconfig.RenamesAnswer - err error -} - -func (f fakeRenames) Renames(context.Context, string) (meshconfig.RenamesAnswer, error) { - return f.answer, f.err -} - -// fakeSeatDefinitions are the indexer's unparsed seat definitions by pull request (pullKey). -type fakeSeatDefinitions map[string][]UnparsedSeat - -func (f fakeSeatDefinitions) Unparsed(_ context.Context, repository string, pull int) ([]UnparsedSeat, error) { - return f[fmt.Sprintf("%s#%d", repository, pull)], nil -} diff --git a/modules/mesh-delivery-checker/internal/check/failure_words_test.go b/modules/mesh-delivery-checker/internal/check/failure_words_test.go deleted file mode 100644 index 37e2b87c..00000000 --- a/modules/mesh-delivery-checker/internal/check/failure_words_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package check - -import ( - "encoding/json" - "mesh-delivery-checker/rules" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" -) - -// The failure the build module reported for group r4g on 2026-10-23: the test logged a line, then failed with a multi-line -// t.Fatalf. Its output is what the build module now sends (its last lines, with the error line as Cause). -func fatalAfterLog() Failure { - return Failure{ - Package: "github.com/novox/mesh-controller/internal/broker", Test: "TestTheCataloguesDerivedSubjectsMeet", - Cause: "3 subscription(s) derive to a subject no emitter owns:", - Output: " agreement_catalogue_test.go:130: judged against /var/lib/mesh-build-go-work/id/beside/mesh-catalog as cloned\n" + - " agreement_catalogue_test.go:186: 3 subscription(s) derive to a subject no emitter owns:\n" + - " a.b.c\n d.e.f\n g.h.i\n--- FAIL: TestTheCataloguesDerivedSubjectsMeet (0.00s)", - } -} - -// A failed test's words are its whole failure message, kept within their bound by their last lines (issue 1901). -func TestAFailuresWordsAreItsWholeMessageWithinTheirBound(t *testing.T) { - for _, row := range []struct { - name string - test func(t *testing.T) - }{ - {"a failed tests verdict keeps its whole failure message", func(t *testing.T) { // 1901/AC1 - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{fatalAfterLog()}}) - for _, want := range []string{"3 subscription(s) derive to a subject no emitter owns:", "a.b.c", "d.e.f", "g.h.i"} { - if !strings.Contains(v.Failed.Words, want) { - t.Errorf("words lack %q: %q", want, v.Failed.Words) - } - } - if len(v.Failed.Words) > rules.MaxWords+200 { - t.Errorf("words are %d bytes, bound %d", len(v.Failed.Words), rules.MaxWords) - } - }}, - {"the status line names the failure not the first logged line", func(t *testing.T) { // 1901/AC2 - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{fatalAfterLog()}}) - got := firstFailed(*v) - if !strings.Contains(got, "3 subscription(s) derive to a subject no emitter owns:") || strings.Contains(got, "judged against") || strings.Contains(got, "\n") { - t.Errorf("firstFailed = %q", got) - } - // With no cause told apart (an older build module), the words still start at the first line, as before. - f := fatalAfterLog() - f.Cause = "" - v = rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{f}}) - if !strings.HasPrefix(afterHid(v.Failed.Words), "agreement_catalogue_test.go:130") { - t.Errorf("words = %q", v.Failed.Words) - } - }}, - {"words longer than the bound keep their last lines", func(t *testing.T) { // 1901/AC1 - f := Failure{Output: strings.Repeat("log line of some length\n", 500) + "the cause is here"} - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{f}}) - if !strings.HasSuffix(v.Failed.Words, "the cause is here") || len(v.Failed.Words) > rules.MaxWords+200 { - t.Errorf("%d bytes, ends %q", len(v.Failed.Words), v.Failed.Words[len(v.Failed.Words)-30:]) - } - _ = meshdeliverycheckerclient.VerdictFail - }}, - // An older build module sends no "cause": its answer is still accepted and the verdict's words are its output, as before - // (1901/AC3). A failure that hides its cause (none, or a passing line) is said loudly first in the words and in the status - // line, naming the build module and the version it says it is, and the level still fails; a failure that keeps the - // contract is worded as before (1901/AC8). - {"an older build modules failure is accepted with its whole output", func(t *testing.T) { // 1901/AC3, AC8 - old := `{"package":"p","test":"TestX","output":"x_test.go:1: logged\nx_test.go:2: the old kept lines"}` - var f Failure - if err := json.Unmarshal([]byte(old), &f); err != nil || f.Cause != "" { - t.Fatalf("1901/AC3: err = %v, cause = %q", err, f.Cause) - } - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{f}}) - if afterHid(v.Failed.Words) != "x_test.go:1: logged\nx_test.go:2: the old kept lines" { - t.Errorf("1901/AC3: words = %q", v.Failed.Words) - } - if got := firstFailed(*v); !strings.Contains(got, "the build module hid the failure") || strings.Contains(got, "\n") { - t.Errorf("1901/AC3, AC8: status = %q", got) - } - - by := "mesh-build-node with mesh-sdk v0.1.14-0.20261026184535-e40dd0f56a78" - for _, c := range []struct { - name, module, by, cause, output, loud string - }{ - {"no cause", "mesh-build-node", by, "", "test/b.test.ts: something", "the build module hid the failure (mesh-build-node at mesh-sdk v0.1.14-0.20261026184535-e40dd0f56a78): it sent no cause"}, - {"a passing line as its cause", "mesh-build-node", by, "\u2714 the same merge has the same id (8.006913ms)", "", "the build module hid the failure (mesh-build-node at mesh-sdk v0.1.14-0.20261026184535-e40dd0f56a78): its cause is a passing line"}, - {"from before the contract", "mesh-build-node", "", "", "\u2714 the same merge has the same id (8.006913ms)", "the build module hid the failure (mesh-build-node at a version before the failure contract): it sent no cause"}, - {"a real cause", "mesh-build-node", by, "one plus one is not three", "not ok 3 - fails", ""}, - } { - t.Run(c.name, func(t *testing.T) { - v := rules.LevelFailure("", c.module, "", "unit", Tested{By: c.by, Failed: []Failure{{Package: "modules/gitea", Test: "fails", Cause: c.cause, Output: c.output}}}) - if v.State != meshdeliverycheckerclient.VerdictFail { - t.Errorf("1901/AC8: the verdict is %q, want it failed", v.State) - } - line := firstFailed(*v) - switch { - case c.loud == "" && strings.Contains(v.Failed.Words, "hid the failure"): - t.Errorf("1901/AC8: a failure that keeps the contract is said hidden: %q", v.Failed.Words) - case c.loud != "" && (!strings.HasPrefix(v.Failed.Words, c.loud) || !strings.Contains(line, c.loud) || !strings.Contains(v.Failures[0].Words, "hid the failure")): - t.Errorf("1901/AC8: words %q, status %q, want both to start with %q", v.Failed.Words, line, c.loud) - } - }) - } - }}, - } { - t.Run(row.name, row.test) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/fakes_test.go b/modules/mesh-delivery-checker/internal/check/fakes_test.go deleted file mode 100644 index bade10ab..00000000 --- a/modules/mesh-delivery-checker/internal/check/fakes_test.go +++ /dev/null @@ -1,81 +0,0 @@ -package check - -import ( - "context" - "sort" - "sync" -) - -// memKV is a bucket in memory with the revisions a real one gives. -type memKV struct { - mu sync.Mutex - vals map[string][]byte - revs map[string]uint64 - next uint64 - // creates counts the creates that succeeded, by key. - creates map[string]int -} - -func newMemKV() *memKV { - return &memKV{vals: map[string][]byte{}, revs: map[string]uint64{}, creates: map[string]int{}} -} - -func (m *memKV) Get(_ context.Context, key string) ([]byte, uint64, bool, error) { - m.mu.Lock() - defer m.mu.Unlock() - v, ok := m.vals[key] - return v, m.revs[key], ok, nil -} - -func (m *memKV) Create(_ context.Context, key string, value []byte) (uint64, error) { - m.mu.Lock() - defer m.mu.Unlock() - if _, ok := m.vals[key]; ok { - return 0, ErrConflict - } - m.next++ - m.vals[key], m.revs[key] = append([]byte(nil), value...), m.next - m.creates[key]++ - return m.next, nil -} - -func (m *memKV) Update(_ context.Context, key string, value []byte, revision uint64) (uint64, error) { - m.mu.Lock() - defer m.mu.Unlock() - if _, ok := m.vals[key]; !ok || m.revs[key] != revision { - return 0, ErrConflict - } - m.next++ - m.vals[key], m.revs[key] = append([]byte(nil), value...), m.next - return m.next, nil -} - -func (m *memKV) Keys(context.Context) ([]string, error) { - m.mu.Lock() - defer m.mu.Unlock() - keys := make([]string, 0, len(m.vals)) - for k := range m.vals { - keys = append(keys, k) - } - sort.Strings(keys) - return keys, nil -} - -func (m *memKV) Delete(_ context.Context, key string) error { - m.mu.Lock() - defer m.mu.Unlock() - delete(m.vals, key) - delete(m.revs, key) - return nil -} - -// memInputs keeps run inputs in a memKV, under their digest. -type memInputs struct{ kv *memKV } - -func (m memInputs) Keep(ctx context.Context, digest string, body []byte) error { - if _, _, found, _ := m.kv.Get(ctx, digest); found { - return nil - } - _, err := m.kv.Create(ctx, digest, body) - return err -} diff --git a/modules/mesh-delivery-checker/internal/check/gate_test.go b/modules/mesh-delivery-checker/internal/check/gate_test.go deleted file mode 100644 index 63680278..00000000 --- a/modules/mesh-delivery-checker/internal/check/gate_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package check - -import ( - "context" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" -) - -// What the gate reads of the checker (R4e, lead's interface with dev-r4e-gate): the verdict's heads are the gate's hash over -// `#=`, a lone pull request is a group of one, and a move of a trunk marks -// mesh/check pending on every open pull request into it, every member's for a group, before any check starts. - -func owedState(t testing.TB, r *rig, repo string, number int, head string) string { - t.Helper() - key, err := CheckerOwedKey(repo, number, head) - if err != nil { - t.Fatal(err) - } - raw, _, found, _ := r.owed.Get(context.Background(), key) - if !found { - return "" - } - o, err := DecodeCheckerOwed(raw) - if err != nil { - t.Fatal(err) - } - return o.State -} - -func TestTheVerdictsHeadsAreTheGatesHashOverLowercaseHeads(t *testing.T) { // gate (b), (c) - ctx := context.Background() - r := newRig(t, passJudge()) - upper := strings.ToUpper(head1[:20]) + head1[20:] - r.setTrunks(t, map[string]string{"Novox/Mesh-Delivery": trunkA, repoA: trunkA, repoSDK: trunkS}) - cand := candidateOf(t, "feat/x", pullOf("Novox/Mesh-Delivery", 7, upper)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - want, _ := meshdeliveryclient.HeadsHash([]meshdeliveryclient.Head{{Repository: repoA, Number: 7, Head: head1}}) - if v := r.verdict(t, keyOf(t, "feat/x", cand)); v.Heads != want { - t.Errorf("heads = %q, want the gate's hash of the lone pull request, a group of one, %q", v.Heads, want) - } -} - -func TestAGroupsVerdictHeadsCoverEveryMember(t *testing.T) { // gate (b): hq's member too, as the gate's ask covers it - ctx := context.Background() - r := newRig(t, passJudge()) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2), pullOf(hqRepo, 3, head2)) - r.ch.Candidate(ctx, "feat%2Fx", cand) - r.ch.Wait() - want, _ := meshdeliveryclient.HeadsHash([]meshdeliveryclient.Head{{Repository: repoA, Number: 7, Head: head1}, {Repository: repoSDK, Number: 12, Head: head2}, - {Repository: hqRepo, Number: 3, Head: head2}}) - if v := r.verdict(t, keyOf(t, "feat/x", cand)); v.Heads != want { - t.Errorf("heads = %q, want %q", v.Heads, want) - } -} - -func TestATrunkMoveMarksEveryOpenPullRequestIntoItPendingAtOnce(t *testing.T) { // gate (a), lead decision L4 (1302's checker part) - ctx := context.Background() - r := newRig(t, passJudge()) - r.ch.Candidate(ctx, "feat%2Fg", candidateOf(t, "feat/g", pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))) - r.ch.Candidate(ctx, "feat%2Fs", candidateOf(t, "feat/s", pullOf(repoSDK, 13, head1))) - r.ch.Wait() - for _, p := range []struct { - repo string - n int - head string - }{{repoA, 7, head1}, {repoSDK, 12, head2}, {repoSDK, 13, head1}} { - if s := owedState(t, r, p.repo, p.n, p.head); s != "success" { - t.Fatalf("%s#%d is %q before the move", p.repo, p.n, s) - } - } - // mesh-delivery's trunk moves; the re-judges it asks are held in their judge (in the stream, since R4g) meanwhile. - release := make(chan struct{}) - r.ch.Judge = &judgeFunc{f: func(context.Context, *Case, int) (Outcome, error) { <-release; return Outcome{Verdict: passing()}, nil }} - defer func() { close(release); r.ch.Wait() }() - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) - r.ch.TrunkMoved(ctx) - if s := owedState(t, r, repoA, 7, head1); s != "pending" { - t.Errorf("mesh-delivery#7, into the moved trunk, is %q, want pending", s) - } - if s := owedState(t, r, repoSDK, 12, head2); s != "pending" { - t.Errorf("mesh-sdk#12, a member of the same group, is %q, want pending", s) - } - if s := owedState(t, r, repoSDK, 13, head1); s != "success" { - t.Errorf("mesh-sdk#13, alone into a trunk that did not move, is %q, want its success to stand", s) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/graph_edges.go b/modules/mesh-delivery-checker/internal/check/graph_edges.go deleted file mode 100644 index e7c7afef..00000000 --- a/modules/mesh-delivery-checker/internal/check/graph_edges.go +++ /dev/null @@ -1,116 +0,0 @@ -package check - -import ( - "cmp" - "context" - "fmt" - "maps" - "slices" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" - "git.novox.be/novox/mesh-sdk/go/catalogue" - - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" -) - -// The module graph of a change (novox/hq ADR 0603 §2, ADR 0614 §6): the running checker gathers its ordering edges, the -// checking program judges whether it has a cycle (rules.JudgeGraph, ADR 0656 §5). The graph is worked out from the -// catalogue as the change leaves it (every trunk manifest, each one a pull request touches replaced by its candidate's) -// and as the trunk has it, with the contract's ManifestGraph, the way the indexer works out its graph's edges; the facts -// carry only each edge's two modules and its kind. - -// ManifestReader reads a kept manifest by its digest (the indexer's manifests state). -type ManifestReader interface { - Manifest(ctx context.Context, digest string) (catalogue.Manifest, error) -} - -// gatherGraph records the change's and the trunk's module graph edges as a fact; none when the case touches no manifest -// or nothing reads manifests. A change's manifest that cannot be read fails the check, said, never passes it; a trunk's -// that cannot be read leaves the trunk's graph unread, so no cycle is the trunk's. -func (st *state) gatherGraph(ctx context.Context) { - if st.j.Manifests == nil || st.j.Read == nil || st.narrowTry() { - return - } - req, touched := judgeRequest(st.j.Manifests.ManifestDigests(), st.j.Manifests.ManifestFingerprints(), st.c.Pulls) - if len(touched) == 0 { - return - } - read := func(digests map[string]composer.Manifest) (map[string]catalogue.Manifest, error) { - out := map[string]catalogue.Manifest{} - for _, module := range slices.Sorted(maps.Keys(digests)) { - m, err := st.j.Read.Manifest(ctx, digests[module].Digest) - if err != nil { - return nil, fmt.Errorf("the manifest %s of %s: %w", digests[module].Digest, module, err) - } - out[module] = m - } - return out, nil - } - change, err := read(req.Cases[0].Manifests) - if err != nil { - st.fail(st.goModule(), "the change's module graph cannot be worked out, so no cycle can be ruled out: "+err.Error()) - return - } - g := rules.Graph{} - changed := orderingEdges(graph.OfManifests(change)) - trunk := map[[2]string]string{} - if before, err := read(req.Cases[1].Manifests); err == nil { - g.Trunk = true - trunk = orderingEdges(graph.OfManifests(before)) - } - for _, pair := range slices.SortedFunc(maps.Keys(changed), cmpPair) { - e := rules.Edge{From: pair[0], To: pair[1], Kind: changed[pair]} - if _, both := trunk[pair]; !both && g.Trunk { - e.Only = rules.EdgeChange - } - g.Edges = append(g.Edges, e) - } - for _, pair := range slices.SortedFunc(maps.Keys(trunk), cmpPair) { - if _, both := changed[pair]; !both { - g.Edges = append(g.Edges, rules.Edge{From: pair[0], To: pair[1], Kind: trunk[pair], Only: rules.EdgeTrunk}) - } - } - st.facts = append(st.facts, rules.Step{Module: st.goModule(), Graph: &g}) -} - -func cmpPair(a, b [2]string) int { - return cmp.Or(strings.Compare(a[0], b[0]), strings.Compare(a[1], b[1])) -} - -// orderingEdges are the graph's ordering edges (graph.Graph.Before) between its modules, each pair once with the kind it -// is first met by: a base, a provision or bus state awaited of another provider, or a verb the graph keeps served by -// another module (a base on itself is a cycle). A verb edge the graph leaves out (ADR 0614 §3), a toolchain and a package edge are none (§6). -func orderingEdges(g graph.Graph) map[[2]string]string { - out := map[[2]string]string{} - add := func(from, to, kind string) { - if _, ours := g[to]; !ours || (from == to && kind != rules.EdgeBase) { - return - } - if _, met := out[[2]string{from, to}]; !met { - out[[2]string{from, to}] = kind - } - } - for _, name := range slices.Sorted(maps.Keys(g)) { - n := g[name] - for _, b := range n.Bases { - add(name, b, rules.EdgeBase) - } - for _, a := range n.Awaits { - for _, p := range g.Providers(a) { - add(name, p, rules.EdgeAwaits) - } - } - for _, v := range n.Invokes { - if slices.Contains(n.LeftOut, v) { - continue - } - for _, s := range g.Servers(v) { - add(name, s, rules.EdgeInvokes) - } - } - } - return out -} diff --git a/modules/mesh-delivery-checker/internal/check/heard.go b/modules/mesh-delivery-checker/internal/check/heard.go deleted file mode 100644 index b10bd09f..00000000 --- a/modules/mesh-delivery-checker/internal/check/heard.go +++ /dev/null @@ -1,21 +0,0 @@ -package check - -import ( - "errors" - - "github.com/nats-io/nats.go" -) - -// Unheard is the error of a call that reached nobody: the bus had no responder for it, or none answered within the call's -// bound. The program wraps the errors of its forge calls in it where the bus gives a timeout; a no-responders error is -// known as it is. A build step is no call since R4g: an ask whose node stops is delivered again by its stream (ADR 0525 §6). -type Unheard struct{ Err error } - -func (u *Unheard) Error() string { return u.Err.Error() } -func (u *Unheard) Unwrap() error { return u.Err } - -// isUnheard says whether err is a call that reached nobody. A step that began and failed, or ended failed, is not. -func isUnheard(err error) bool { - var u *Unheard - return errors.As(err, &u) || errors.Is(err, nats.ErrNoResponders) -} diff --git a/modules/mesh-delivery-checker/internal/check/judge.go b/modules/mesh-delivery-checker/internal/check/judge.go deleted file mode 100644 index ac25c7e3..00000000 --- a/modules/mesh-delivery-checker/internal/check/judge.go +++ /dev/null @@ -1,992 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "fmt" - "mesh-delivery-checker/rules" - "slices" - "sort" - "strings" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// RunnerWorkspace is the runner the Go build module's digest and test steps are asked with when the merge changes the build -// module itself: the installed module compiles the candidate's program from the merged tree and runs the step as it, shut -// off (ADR 0535 §1). -const RunnerWorkspace = "workspace" - -// buildGoDir is the Go build module's directory in the catalogue: a merge changing a path under it is judged by its -// candidate (ADR 0535 §1). The other build modules' own directories are judged by the installed module until its part of issue 1898. -func buildGoDir(goModule string) string { return "modules/" + goModule + "/" } - -// changesBuildGo says whether a merge of the repository with these changed paths changes the Go build module. -func changesBuildGo(repository, goModule string, changed []string) bool { - if repository != catalogueRepository || goModule == "" { - return false - } - for _, p := range changed { - if strings.HasPrefix(p, buildGoDir(goModule)) { - return true - } - } - return false -} - -// The words of the line on the program that judged a change to the Go build module (ADR 0535 §2, §5). -func judgedByOldInstalled(module string) string { - return "judged by the installed " + module + ": it does not run a candidate yet" -} -func judgedByInstalled(module string) string { return "judged by the installed " + module } -func judgedByCandidate(module string) string { return "judged by the candidate's " + module + " at " } - -// Asks puts a check's ask on its build seat's stream and waits for its step (buildrun.Asker; ADR 0525 §1, §6): the -// checker chooses no node, starts no step and holds none; whichever build node pulls the ask runs the check. -type Asks interface { - Put(ctx context.Context, a buildstep.StepAsk) error - Wait(ctx context.Context, a buildstep.StepAsk) (buildstep.Step, error) -} - -// ToolchainEntries are the nodes' toolchain entries of a language (buildrun.ToolchainSet). -type ToolchainEntries interface { - Entries(language string) []buildstep.NodeToolchain -} - -// BuildModules says which build modules judge a repository: the Go one always, the Node one when the repository holds a -// TypeScript module. -type BuildModules interface { - BuildModules(repository string) []string -} - -// Examiner judges a case through the build seats (ADR 0484 clauses 3, 4, 10, 13; ADR 0525 §1, §3): for each pull request -// and build module it puts one check ask whose job fetches the head merged onto its trunk head, then runs the unit level -// and the integration level, taking the kept verdicts of unchanged packages and running the change's own tests first; -// every level and member runs to its end also after a failure, so the verdict names every failure (the operator, -// 2026-11-02). It judges the step's answer as it judged the tools' answers. -type Examiner struct { - // Module is the checker's own module, the credential's: the asker of its asks and the owner of its buckets (design 47 G A-296). - Module string - Asks Asks - // DeclineBound is how long a placed ask may go on being declined, from its first decline, before the check fails - // saying no node with its need took it (issue 890, ADR 0552); DefaultDeclineBound when zero. - DeclineBound time.Duration - // Toolchains are the nodes' toolchains: a kept verdict names the toolchain the nodes agree on. - Toolchains ToolchainEntries - Kept Kept - // Previews is the previews bucket; nil writes none. - Previews KV - Modules BuildModules - // Seats finds the build modules from the seat list and the holders of seats (design 47 G A-82). - Seats *buildrun.BuildSeats - // Runs says which commit the mesh runs of a module (the indexer's modules); nil knows none. - Runs interface{ CommitOf(module string) string } - // Forge reads the forge's files for the siblings a repository's tests read; nil reads none. - Forge Forge - // Published names the latest published version of every mesh package a module of the mesh uses, by package, from the - // indexer's modules (novox/hq ADR 0573 §2): a check builds against those, or against a member's head for a package - // its group publishes (§4), whatever go.mod names. Nil names none, and go.mod's versions are built. - Published interface { - LatestPackages() buildstep.PackageVersions - } - // KnownPackages names the packages the indexer's packages state knows (its series keys, meshcatalog.PackagesByPackageSeriesValue), by the - // name in its own ecosystem, each with the `/` it is built from: the names a manifest's `packages` is - // judged against (manifests_judge.go, design 32). Nil, or PackageNames answering nil while the state is not read yet, - // hands the judge no names, and it judges none; an empty map is a state read and empty, against which every name fails. - KnownPackages interface { - PackageNames() map[string]string - } - // Builds says how the build of a fingerprint the candidate carries stands (the builder's builds bucket); nil says nothing - // of the builds. - Builds BuildStates - // BuildEnds waits on the builder's builds of the candidate before the check composes (ADR 0603 §10); nil composes - // without waiting, said in the composition's warnings. - BuildEnds BuildEnds - // BuildWait is how long the check waits for those builds (the setting build-wait-bound); DefaultBuildWait when 0. - BuildWait time.Duration - // Announce says a preview was written under the key (EventPreviewWritten); nil says nothing. - Announce Announcer - // Dirs names a module's directory in its repository, from the indexer's modules, so a manifest is read at a member's - // head (tagwanted.go); nil, or a module it does not know, tries modules/ and the repository's root. - Dirs interface { - ModuleDir(module string) (string, bool) - } - // Packages names, of a repository, its TypeScript package artifacts, each by its directory and the paths - // (repository-relative) it is built from, from the indexer's modules (meshcatalog.ModulesByModuleValue.ArtifactPaths, - // lead decision L8); nil serves no group package. - Packages interface { - TypeScriptPackages(repository string) []TypeScriptPackage - } - // Trunks are the trunk heads of every repository, for the siblings a repository's tests read. - Trunks interface{ Heads() map[string]string } - // Manifests are the indexer's manifest digests by module: the catalogue a change's manifests are judged in by the - // indexer's judge (manifests_judge.go, novox/hq ADR 0578 §2); nil judges no manifest. - Manifests TrunkManifests - // SeatDefinitions reads the indexer's seat definitions of a pull request (seatdefs.go, design 26); nil reads none. - SeatDefinitions SeatDefinitions - // SeatList gives the seat list the checker's reader holds from the seat mesh-catalog (verb seat-definitions): the base the - // checking program lays the heads' definitions over (programs.go, novox/hq ADR 0636 §1, §4); nil gives it none. - SeatList SeatList - // Renames asks the seat mesh-config's verb renames for a change that brings or drops a `renamed-from` (renames.go, - // design 32); nil fails such a change, saying it cannot ask. - Renames Renames - // Read reads a kept manifest by its digest: the change's module graph is worked out from them (graph_edges.go, novox/hq - // ADR 0603 §2); nil works out no graph. - Read ManifestReader - // Serving says, from what runs, whether a running healthy build serves a seat verb or module tool (novox/hq ADR 0614 - // §7): the node-engines' haves, the builder's build of each, its manifest (graph.Served). Nil reads nothing, and no - // changed build module is named for a verb it invokes. - Serving interface { - Served(ctx context.Context) (func(invoke string) bool, error) - } - // Predefined reads the catalogue's predefined keys the indexer keeps (manifests_judge.go); nil reads none. - Predefined PredefinedList - // Compositions makes a check's composition (ADR 0593 §7): the engines' answers (EngineCompositions); nil leaves the - // composition not run. - Compositions CompositionReader - // Nodes names every node with a record, read from each `.node` of the node-engine's have (novox/hq ADR 0567 - // §12): every check ask carries them (CheckJob.NodeNames) for the names check, so no repository lists them. A check - // whose names cannot be read, or that reads none, is not asked and fails saying so; nil hands no names. - Nodes interface { - Names(ctx context.Context) ([]string, error) - } - // Candidates answers the indexer's candidate of a try's group once it wrote it from the try's previews, the manifests - // and fingerprints a group try's manifest validator and composition judge (issue 1921), and of a pull request's check - // that began before the indexer read its merge preview; nil leaves such a case with no manifest or composition judged, - // said. - Candidates TryCandidates - // Inputs keeps the checking program's inputs (the state InputsStateName, served on InputVerb); BeginBound is how long a run ask may wait for a - // build node to begin it (DefaultBeginBound when zero). - Inputs Inputs - BeginBound time.Duration - Now func() time.Time - Logf func(format string, args ...any) -} - -// TryCandidates answers the candidate the indexer holds for a try's group at its heads hash, waiting for it within the -// bound; an error says it did not come (and why). -type TryCandidates interface { - TryCandidate(ctx context.Context, group, heads string, bound time.Duration) (meshcatalog.CandidatesByGroupValue, error) -} - -// CompositionReader composes a check: the engines' answers, which the rules judge (rules.JudgeEngines); an error is only -// a case that cannot be composed at all. -type CompositionReader interface { - Compose(ctx context.Context, c *Case) (rules.Composed, error) -} - -func (j *Examiner) now() time.Time { - if j.Now != nil { - return j.Now() - } - return time.Now() -} - -func (j *Examiner) logf(format string, args ...any) { - if j.Logf != nil { - j.Logf(format, args...) - } -} - -// fetchBeside are the siblings as the fetch is asked to place them. -func fetchBeside(refs []BesideRef) []buildstep.FetchBeside { - out := make([]buildstep.FetchBeside, 0, len(refs)) - for _, r := range refs { - out = append(out, buildstep.FetchBeside{Repository: r.Repository, Commit: r.Commit}) - } - return out -} - -// besideLines are the siblings as the digest step echoes them: ` `, sorted. -func besideLines(refs []BesideRef) []string { - out := make([]string, 0, len(refs)) - for _, r := range refs { - out = append(out, r.Repository+" "+r.Commit) - } - sort.Strings(out) - return out -} - -// TypeScriptPackage is one TypeScript package artifact of a repository's module: Dir, its directory in the repository -// ("" for the repository's root), and Paths, the repository-relative paths it is built from. -type TypeScriptPackage struct { - Dir string - Paths []string -} - -// target is a pull request on one build module: the node that pulled its ask, the workspace its fetch made and what it -// said. -type target struct { - pull Pull - module string - // language is the language of the toolchain the build module builds with (the `on` of its seat's acting rule). - language string - node string - toolchain string - // agreed is the toolchain the ask names, so a node off it declines the check (ADR 0584); nil when none is agreed. - agreed *buildstep.ToolchainUsed - fetched Fetched - // fetchArgs are the arguments of the fetch its check ask runs. - fetchArgs buildstep.Fetch - // priority are the Go packages (import paths) or the Node module directories whose test files the merge changes. - changed []string - // beside are the lines ` ` of the siblings the workspace was given, sorted: its tests may read them, - // so a verdict is kept or taken only under a digest that says it folded exactly these in (lead decision L5). - beside []string - // runner is RunnerWorkspace when the target's ask runs the candidate's own build module (ADR 0535 §1), empty otherwise. - runner string - // judged is what the line says of the program that judged a change to the build module; empty for any other change. - judged string -} - -// judgedBy takes what a step under runner workspace answered of the program that ran it (judged_by): the candidate, or the -// installed module (a module that does not take the runner says nothing, and is asked without it from now on: ADR 0535 §2). -// It says whether the candidate ran. -func (t *target) judgedBy(answer string) bool { - switch { - case strings.HasPrefix(answer, "candidate "): - t.judged = judgedByCandidate(t.module) + short12(strings.TrimPrefix(answer, "candidate ")) - return true - case answer == "": - t.judged, t.runner = judgedByOldInstalled(t.module), "" - default: - t.judged, t.runner = judgedByInstalled(t.module), "" - } - return false -} - -// stepArgs are a step's arguments with the target's runner. -func (t *target) stepArgs(args map[string]any) map[string]any { - if t.runner != "" { - args["runner"] = t.runner - } - return args -} - -// state is one judging. -type state struct { - j *Examiner - c *Case - targets []*target - levels rules.Tally - // facts are the facts of the check met so far, in order: what the checking program judges (milestone 47/B0). - facts []rules.Step - // composition is how the check composed, recorded after every step (compose). - composition *rules.Composition - clone time.Duration - slow []string - // beside are the siblings read at their trunk's head, by repository (ADR 0548 §6). - beside map[string]string - // judgeHead is the group's mesh-delivery member head when its merge changes the judge (ADR 0578 §2); empty judges - // with mesh-delivery's trunk. - judgeHead string - // predefinedHead is the catalogue member's head when its merge changes predefined.json: the list is read there. - predefinedHead string - // unpreviewed are the members (pullKey) with no preview of a merge, each with which of the two it is: a head that does not - // merge, or a fetch that did not answer (design 47 G A-286). The indexer holds no manifest of them, so the assigner's - // composition would only wait: it is not awaited. A member whose fetch merged is previewed whatever its later steps ended with. - unpreviewed []string - // conflicted are the members (pullKey) whose head does not merge into its trunk. - conflicted map[string]bool - // outOfStep are, by pull request (pullKey), the generated files a level's compare step found out of step (design 26). - outOfStep map[string][]string - // noCandidate says why the case has no previewed candidate of its heads from the indexer within the bound: for a whole - // try its manifests were not judged and it was not composed; for a pull request's check that began on the indexer's - // first write, its manifests were judged from that write and it was not composed. Empty when it has one, and for a pull - // request's check whose case already holds its fingerprints. - noCandidate string -} - -// narrowTry says the case is a try narrowed to a level or to some tests: it judges no manifest, graph or composition. -func (st *state) narrowTry() bool { return st.c.Try != nil && !st.c.Try.Whole() } - -// WaitsOnPreview says a candidate's module has no fingerprint yet because the indexer wrote it from a pull message alone, -// before it read the head's merge preview (meshcatalogclient.NoneNoPreview): its fingerprint comes with the preview, on the -// same heads, so a candidate that holds one is not yet the candidate of its heads previewed. -func WaitsOnPreview(m meshcatalog.CandidatesByGroupValuePullsItemFpsValue) bool { - return m.None2 == meshcatalogclient.NoneNoPreview -} - -// previewPending says a module of the case's pulls waits on its merge preview (WaitsOnPreview). -func (st *state) previewPending() bool { - for _, p := range st.c.Pulls { - for _, m := range p.Fps { - if WaitsOnPreview(m) { - return true - } - } - } - return false -} - -// takeTryCandidate gives a whole try's pulls the fingerprints and manifests the indexer's candidate of its group holds, -// once the indexer wrote it from the try's previews. A pull request's check has them from its candidate (CaseOf), but the -// indexer writes a pull request's candidate first from its pull message alone and its fingerprints only with the merge -// preview, on the same heads: a check that began on that first write takes the previewed candidate too, before it judges -// the manifests, waits on the builds and composes (ADR 0603 §10; issue 1921: a check otherwise waited on no build and -// the engines, reading the previewed candidate, refused its modules as not yet built). A case with no such candidate is -// said so, in a note and in its composition, and is no pass (Tryer.run). -func (st *state) takeTryCandidate(ctx context.Context) { - if st.c.Try != nil && !st.c.Try.Whole() || st.c.Try == nil && !st.previewPending() { - return - } - no := func(why string) { - what := "the group's manifests and composition were not judged: " - if st.c.Try == nil { - what = "the composition was not judged (the manifests were judged from the indexer's first write): " - } - st.noCandidate = what + why - st.c.note(st.noCandidate) - } - if st.j.Candidates == nil { - no("the checker reads no candidate of the indexer's") - return - } - if len(st.unpreviewed) > 0 { - no("no preview of a merge of " + strings.Join(st.unpreviewed, ", ")) - return - } - if st.c.Wait != nil { - st.c.Wait("the indexer's candidate of the group at its heads (up to " + st.j.buildWait().String() + ")") - defer st.c.Wait("") - } - cand, err := st.j.Candidates.TryCandidate(ctx, st.c.Group, st.c.HeadsHash, st.j.buildWait()) - if err != nil { - no(err.Error()) - return - } - for i, p := range st.c.Pulls { - for _, cp := range cand.Pulls { - if cp.Repository == p.Repository && cp.Head == p.Head { - st.c.Pulls[i].Modules, st.c.Pulls[i].Fps = cp.Modules, cp.Fps - } - } - } - st.c.FPSet = cand.Fpset -} - -// Judge runs the case (it is the Judge of a Checker). A failure of the change, of a merge or of a step is a verdict; an error is only a case that cannot -// be judged at all. -func (j *Examiner) Judge(ctx context.Context, c *Case) (Outcome, error) { - st := &state{j: j, c: c, levels: rules.NewTally(), - beside: map[string]string{}} - // Every member and level runs to its end, and every manifest the change touches is judged by the indexer's judge (ADR - // 0578 §2), also after a failure: the verdict names every failure, not only the first (the operator, 2026-11-02). - // Each failure is a fact recorded where it is met; the verdict is the judging rules' of the facts. - st.judgeAll(ctx) - st.takeTryCandidate(ctx) - st.seriesChanges(ctx) // DLV-C4: a fact of the check, named in the verdict whatever it is - // The heads' seat list, from the checking program, before the validator and the composer are asked (ADR 0636 §4). - // A list it cannot make ends the check there, failed naming the checking program, worded from the facts so far. - var answer rules.Answer - var by *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy - if st.seatList(ctx) { - st.judgeManifests(ctx) - st.gatherGraph(ctx) - st.judgeRenames(ctx) - st.refuseUnparsed(ctx) - st.compose(ctx) - // The verdict is the checking program's of the facts (ADR 0644 §1, ADR 0656 §5). - answer, by = st.judged(ctx) - } else { - answer = rules.Answer{V: rules.AnswerVersion, Verdict: rules.Judge(st.factsOf())} - } - for _, n := range answer.Notes { - st.c.note(n) - } - answer.Verdict.JudgedBy = by - out := st.outcome(answer.Verdict) - out.Builds = st.j.buildNotes(ctx, st.c) - return out, nil -} - -// compose records how the check composed as the facts' composition, after every step (ADR 0593 §7, §9): kept by a carry, -// not run (said why), or the engines' answers with the candidate's builds awaited first (ADR 0603 §10). A narrowed try -// and an examiner that composes nothing record none. -func (st *state) compose(ctx context.Context) { - if st.c.Composition != nil && st.c.Try == nil { - // A carry whose candidate's fingerprints did not change keeps the composition it passed (ADR 0548 §5). - kept := *st.c.Composition - st.composition = &rules.Composition{Kept: &kept} - st.c.note("the composition is kept: no fingerprint of a module it composed changed") - return - } - if st.narrowTry() { - return - } - if st.noCandidate != "" { - st.composition = &rules.Composition{NotRun: st.noCandidate} - return - } - if st.j.Compositions == nil { - return - } - if len(st.unpreviewed) > 0 { - // An engine would only say it cannot read a manifest the indexer will not have: the check does not ask it. - st.composition = &rules.Composition{NotRun: "no preview of a merge of " + strings.Join(st.unpreviewed, ", ") + - ", so the change cannot be composed"} - return - } - // ADR 0603 §10: the check composes with the candidate's builds; whether it failed already is the running rules' word - // on the facts so far, an asking choice only (the verdict is the rules' of every fact). - failed := rules.Judge(st.factsOf()).State == meshdeliverycheckerclient.VerdictFail - words, builds, late := st.awaitBuilds(ctx, failed) - c := &rules.Composition{Waits: words, BuildsFailed: builds, FPSet: st.c.FPSet} - st.composition = c - if late != "" { - c.NotRun = late // the builder's lateness, said by name, never the change's refusal (issue 1921) - return - } - comp, err := st.j.Compositions.Compose(ctx, st.c) - if err != nil { - // Said in the verdict, never left as a composition not yet run (review finding 4). - st.j.logf("%s: the composition could not be made, so it is said as not run: %v", st.c.Key, err) - c.NotRun = "not made: " + err.Error() - return - } - c.Composed = &comp -} - -// outcome is the verdict judged from the facts with the slowest tests, the siblings read and what judged a build module. -func (st *state) outcome(v meshdeliverychecker.VerdictsByGroupHeadsValue) Outcome { - slow := rules.Slowest(st.levels) - out := Outcome{Verdict: v, Slowest: slow, OutOfStep: st.outOfStep} - if len(st.beside) > 0 { - out.Beside = st.beside - } - for _, t := range st.targets { - if t.judged != "" { - if out.Judged == nil { - out.Judged = map[string]string{} - } - out.Judged[pullKey(Pull{Repository: t.pull.Repository, Number: t.pull.Number})] = t.judged - } - } - return out -} - -// stepOutOfStep keeps the out-of-step files a step of the pull request named, each once, sorted. -func (st *state) stepOutOfStep(p Pull, files []string) { - if len(files) == 0 { - return - } - if st.outOfStep == nil { - st.outOfStep = map[string][]string{} - } - k := pullKey(Pull{Repository: p.Repository, Number: p.Number}) - all := append(st.outOfStep[k], files...) - slices.Sort(all) - st.outOfStep[k] = slices.Compact(all) -} - -// fail records a failure the asking met as a fact of the check, where it is met, and answers it as a verdict of failure -// at a machine: the verdict is the judging rules' of the facts (rules.Judge), so a failure only returned is no failure -// (milestone 47/B0). -func (st *state) fail(machine, words string) { - st.facts = append(st.facts, rules.Step{Failed: &FailedFirst{Machine: machine, Words: words}}) -} - -// fail is a verdict of failure at a machine, for a step that answers one before any member is asked (tagged); it is -// recorded as facts (failedAll), never returned as the check's verdict. -func fail(machine, words string) *meshdeliverychecker.VerdictsByGroupHeadsValue { - v := failure(machine, words) - return &v -} - -// failedAll records every failure a verdict of the asking names (its list, else its first) as facts of the check. -func (st *state) failedAll(v meshdeliverychecker.VerdictsByGroupHeadsValue) { - all := failedList(v.Failures) - if len(all) == 0 { - all = []FailedFirst{failedOf(v)} - } - for _, f := range all { - st.facts = append(st.facts, rules.Step{Failed: &f}) - } -} - -// factsOf are the check's facts as met: what the checking program judges. -func (st *state) factsOf() rules.Facts { - f := st.factsHead() - f.Steps, f.CloneMS, f.Composition, f.Notes = st.facts, st.clone.Milliseconds(), st.composition, st.c.Notes() - f.Series = st.c.allSeries() - return f -} - -// levels are the levels a case's asks run: both, or the one a try names. -func (st *state) levelsOf() []string { - if st.c.Try != nil && st.c.Try.Level != "" { - return []string{st.c.Try.Level} // a try of one level runs that level alone - } - return []string{buildstep.LevelUnit, buildstep.LevelIntegration} -} - -// judgeAll puts one check ask per pull request and build module the repository needs, the mesh-sdk member first, and -// judges each answer. A head that does not merge, a pin that is not allowed or a level that fails is a failure of that -// member; every other member is still judged, and the verdict names every failure (the operator, 2026-11-02), unjoined -// to its first lines (settled does that). Only a check whose context ended stops. -// -// Every member's asks of the other languages come first and its TypeScript asks after them all: the merges those -// fetches answer say which TypeScript packages the group changes, and each TypeScript fetch is given them all -// (groupPackages; design 47 G A-284, A-285). -func (st *state) judgeAll(ctx context.Context) { - pulls := slices.Clone(st.c.Pulls) - slices.SortStableFunc(pulls, func(a, b Pull) int { - if (a.Repository == SDKRepository) != (b.Repository == SDKRepository) { - if a.Repository == SDKRepository { - return -1 - } - return 1 - } - return 0 - }) - met := len(st.facts) - stopped := func() bool { - if ctx.Err() == nil { - return false - } - if !st.failedSince(met) { // an ask that ended with the context said it already - st.fail(st.j.Module, ctx.Err().Error()) - } - return true - } - merged := make([][]Fetched, len(pulls)) - modules := make([][]string, len(pulls)) - for i, p := range pulls { - modules[i] = st.j.Modules.BuildModules(p.Repository) - } - // Each phase puts every ask of its own at once and then waits for them all (design 47 G A-304, ADR 0525 §4): its - // asks are judged in the order they were put, whatever order they end in. - phase := func(typeScript bool, group []buildstep.FetchPackage) bool { - var asked []*asking - for i, p := range pulls { - for _, bm := range modules[i] { - if (st.j.languageOf(bm) == languageTypeScript) != typeScript { - continue - } - if stopped() { - return false - } - if t := st.memberTarget(ctx, p, bm, group); t != nil { - if q := st.prepareAsk(ctx, t, &merged[i], st.levelsOf(), nil, nil); q != nil && st.putAsk(ctx, q) { - asked = append(asked, q) - } - } - } - } - for _, q := range asked { - go q.wait(ctx, st) - } - for n, q := range asked { - if n > 0 && stopped() { - return false - } - <-q.done - st.judgeAsk(ctx, q) - } - return true - } - if !phase(false, nil) { - return - } - var group []buildstep.FetchPackage - if len(st.c.Pulls) > 1 { - group = st.groupPackages(pulls, merged) - } - if !phase(true, group) { - return - } - for i, p := range pulls { - // Every build module's fetch merged (each is in merged once): the preview is written, also when the member's tests - // or a later step failed, so the indexer holds its manifests and the assigner can compose the change (review of - // 650a1d0; design 47 G A-286). - switch { - case st.narrowTry(): // a try of a level or some tests owes the indexer no preview: it judges no manifest - case len(merged[i]) == len(modules[i]): - st.j.writePreview(ctx, p, merged[i]) - case st.conflicted[pullKey(p)]: - st.unpreviewed = append(st.unpreviewed, pullKey(p)+" (its head does not merge)") - default: - st.unpreviewed = append(st.unpreviewed, pullKey(p)+" (its fetch did not answer)") - } - st.judgedByMember(p, merged[i]) - } -} - -// memberTarget is the target of the check ask of one build module of a member, nil when it cannot be asked (said as a -// failure); a TypeScript fetch is given the group's packages. -func (st *state) memberTarget(ctx context.Context, p Pull, bm string, group []buildstep.FetchPackage) *target { - t := &target{pull: p, module: bm, language: st.j.languageOf(bm)} - if tc, none := buildstep.AgreedToolchain(st.j.Toolchains.Entries(t.language)); none == "" { - t.toolchain = tc.Version - t.agreed = &buildstep.ToolchainUsed{Language: t.language, Version: tc.Version, Recipe: tc.Recipe} - } - args := buildstep.Fetch{Repository: p.Repository, Commit: p.Head, Ref: refOf(p.Number), Base: p.Trunk} - if t.language == languageTypeScript { - args.GroupPackages = group - } - if t.language == languageGo { - beside, err := st.j.besideFor(ctx, st.c, p) - if err != nil { - st.fail(forgeMachine(err, bm), err.Error()+unheardWords(err)) - return nil - } - if len(beside) > 0 { - args.Beside = fetchBeside(beside) - t.beside = besideLines(beside) - besideAtTrunk(st.c, beside, st.beside) - } - if p.Repository == catalogueRepository { - t.runner = RunnerWorkspace // used by the build module when the merge changes it (ADR 0535 §1) - } - } - t.fetchArgs = args - st.targets = append(st.targets, t) - return t -} - -// groupPackages are the TypeScript packages the group's merges change (design 47 G A-284, A-285): every package of a -// member whose named paths a merge of that member changes, each at the member's head with its directory there, in the -// order of the members and then of the directories. A change of the manifest alone, or of files outside those paths, -// changes no package (ADR 0500 §5). -func (st *state) groupPackages(pulls []Pull, merged [][]Fetched) []buildstep.FetchPackage { - if st.j.Packages == nil { - return nil - } - var out []buildstep.FetchPackage - for i, p := range pulls { - for _, pkg := range st.j.Packages.TypeScriptPackages(p.Repository) { - if changesPaths(pkg.Paths, merged[i]) { - out = append(out, buildstep.FetchPackage{Repository: p.Repository, Commit: p.Head, Ref: refOf(p.Number), Path: pkg.Dir}) - } - } - } - return out -} - -// failedSince says a failure was recorded among the facts from the nth on. -func (st *state) failedSince(n int) bool { - for _, f := range st.facts[n:] { - if f.Failed != nil { - return true - } - } - return false -} - -// DefaultDeclineBound is an Examiner's DeclineBound when it names none: longer than a check runs, so a node with the -// need that is busy is waited for, and short enough that a mesh where no node has it says so. -const DefaultDeclineBound = 30 * time.Minute - -// askNeeding puts the target's check ask, waits for it and judges its answer, with the levels to run, what the node must have and the packages it tests: none for the first ask -// of a target; for the placed one the unmet packages' needs and those packages alone, so no package the first ask judged -// is run or counted again (ADR 0552 §4). -func (st *state) askNeeding(ctx context.Context, t *target, merged *[]Fetched, levels, needs, packages []string) { - q := st.prepareAsk(ctx, t, merged, levels, needs, packages) - if q == nil || !st.putAsk(ctx, q) { - return - } - q.wait(ctx, st) - st.judgeAsk(ctx, q) -} - -// asking is one check ask put and waited for: its target, the merges its fetch adds to, the ask as put last (a declined -// one is put again under a new id) and, once done is closed, the step it ended with or why it has none. -type asking struct { - t *target - merged *[]Fetched - a buildstep.StepAsk - kind string - needs []string - placed bool - step buildstep.Step - failed bool // the ask ended with no step to judge: failOn and failWords say why - failOn string - failWords string - done chan struct{} -} - -// prepareAsk makes the target's ask with the levels to run, what the node must have and the packages it tests; nil when -// it cannot be asked (said as a failure). -func (st *state) prepareAsk(ctx context.Context, t *target, merged *[]Fetched, levels, needs, packages []string) *asking { - p := t.pull - job := &buildstep.CheckJob{Fetch: t.fetchArgs, Levels: levels, Kept: true, Packages: packages, Toolchain: t.agreed} - if st.j.Nodes != nil { - names, err := st.j.Nodes.Names(ctx) - if err == nil && len(names) == 0 { - err = fmt.Errorf("no node record is on the bus") - } - if err != nil { - st.fail(t.module, fmt.Sprintf("the check of %s#%d is not asked: the nodes' names the names check needs cannot be read: %v", p.Repository, p.Number, err)) - return nil - } - job.NodeNames = names - } - if t.runner != "" { - job.Runner, job.RunnerWhen = t.runner, buildGoDir(st.goModule()) - } - if st.c.Try != nil && st.c.Try.Run != "" { - job.Run, job.Kept = st.c.Try.Run, false // a try of some tests runs them alone: a partial run proves no package - } - kind := buildstep.AskCheck - if st.c.Kind == KindTrunk { - kind = buildstep.AskRecheck // a re-judge after a trunk move goes after every new head's check (ADR 0536 §4) - } - a := buildstep.StepAsk{ID: buildstep.NewID(), Kind: kind, Language: st.j.askLanguageOf(t.module), Asker: st.j.Module, - At: st.j.now().UTC(), Check: job, Needs: needs} - if a.Language == buildstep.AskGo { - a.Packages = st.packagesOf(p.Repository) - } - return &asking{t: t, merged: merged, a: a, kind: kind, needs: needs, placed: len(needs) > 0, done: make(chan struct{})} -} - -// putAsk puts the ask on its build seat's stream; false when it could not be put (said as a failure). -func (st *state) putAsk(ctx context.Context, q *asking) bool { - if err := st.put(ctx, q); err != nil { - st.fail(q.t.module, fmt.Sprintf("the check of %s#%d could not be asked: %v", q.t.pull.Repository, q.t.pull.Number, err)) - return false - } - return true -} - -// put puts the ask as it stands now. It touches only what is safe from a waiting goroutine: the progress, the case's -// ask ids and its watch. -func (st *state) put(ctx context.Context, q *asking) error { - p := q.t.pull - what := fmt.Sprintf("the %s ask %s of %s#%d", q.kind, q.a.ID, p.Repository, p.Number) - if q.placed { - what += " for a node with " + strings.Join(q.needs, ", ") - } - st.c.Progress.Set(what, "") - st.c.Asked(q.a.ID) - if err := st.j.Asks.Put(ctx, q.a); err != nil { - return err - } - if watch := st.c.Watch; watch != nil { - watch(q.a.ID, nil) // put: a try names it at once, not only when it ends (issue 1890) - } - return nil -} - -// wait waits for the step of the node that pulled the ask, putting a declined ask again for a node that has its need, -// and closes done. It runs on its own goroutine beside the phase's other asks, so it changes nothing of the check but -// the asking itself (the facts and failures are the judging's, in order). -func (q *asking) wait(ctx context.Context, st *state) { - defer close(q.done) - p := q.t.pull - var firstDecline time.Time - for { - waiting := ctx - if watch := st.c.Watch; watch != nil { - id := q.a.ID - waiting = buildrun.WithStepSeen(ctx, func(s buildstep.Step) { watch(id, &s) }) - } - step, err := st.j.Asks.Wait(waiting, q.a) - if err != nil { - q.failed, q.failOn = true, q.t.module - if ctx.Err() != nil { - q.failWords = ctx.Err().Error() - } else { - q.failWords = fmt.Sprintf("the check of %s#%d: %v", p.Repository, p.Number, err) - } - return - } - q.step = step - d, declined := buildstep.DeclinedOf(step) - if !declined { - return - } - // A node that lacks the need dropped the ask (issue 890): it is put again, for a node that has it to take; the - // node that declined looks at the stream again only after its next pull's wait. - now := st.j.now() - if firstDecline.IsZero() { - firstDecline = now - } - bound := st.j.DeclineBound - if bound <= 0 { - bound = DefaultDeclineBound - } - if now.Sub(firstDecline) > bound { - lacks := strings.Join(q.needs, ", ") - if lacks == "" { - lacks = strings.Join(d.Lacks, ", ") // a node off the agreed toolchain declined it (ADR 0584) - } - q.failed, q.failOn = true, step.Node - q.failWords = fmt.Sprintf("the check of %s#%d needs a node with %s, and none took it in %s: every node that pulled it lacks it (last %s)", - p.Repository, p.Number, lacks, bound, step.Node) - return - } - st.j.logf("%s#%d: the ask %s was declined on %s, which lacks %s; asked again", p.Repository, p.Number, q.a.ID, step.Node, strings.Join(d.Lacks, ", ")) - q.a.ID, q.a.At = buildstep.NewID(), now.UTC() - if err := st.put(ctx, q); err != nil { - q.failed, q.failOn = true, q.t.module - q.failWords = fmt.Sprintf("the check of %s#%d could not be asked: %v", p.Repository, p.Number, err) - return - } - } -} - -// judgeAsk judges the step an ask ended with: the fetch, then each level's digest, kept verdicts and tests. When the -// answer leaves packages unmet, their tests needing what that node lacks (issue 890), the passes are kept and the levels -// from the first such one are asked again of a node that has it (placed). -func (st *state) judgeAsk(ctx context.Context, q *asking) { - if q.failed { - st.fail(q.failOn, q.failWords) - return - } - t, merged, needs, placed, step := q.t, q.merged, q.needs, q.placed, q.step - p := t.pull - // The fetch and the digests are what of the ask's time its levels' running did not take (Took.Clone). - took := time.Duration(0) - if !step.Started.IsZero() && step.Ended.After(step.Started) { - took = step.Ended.Sub(step.Started) - } - t.node = step.Node - st.facts = append(st.facts, rules.Step{Repository: p.Repository, Number: p.Number, Module: t.module, Node: step.Node}) - st.c.Progress.Set(fmt.Sprintf("judge the check of %s#%d", p.Repository, p.Number), step.Node) - if step.State != buildstep.StepPassed { - // A step that failed after its fetch merged answers the fetch beside its error (design 47 G A-286): the merge is - // previewed from it, so its manifests are judged and the change composed whatever the step ended with. - var failed buildstep.CheckAnswer - if !placed && json.Unmarshal(step.Result, &failed) == nil && failed.Fetch.Workspace != "" && len(failed.Fetch.Conflicts) == 0 { - t.fetched, t.changed = failed.Fetch, failed.Fetch.Changed - *merged = append(*merged, t.fetched) - st.checkBinariesOf(t) - } - st.fail(step.Node, fmt.Sprintf("the check of %s#%d %s: %s", p.Repository, p.Number, step.State, step.Reason)) - return - } - var answer buildstep.CheckAnswer - if err := json.Unmarshal(step.Result, &answer); err != nil { - st.fail(step.Node, fmt.Sprintf("the check of %s#%d answered what is not a check: %s", p.Repository, p.Number, step.Result)) - return - } - if t.fetched = answer.Fetch; t.fetched.Workspace == "" { - st.fail(step.Node, fmt.Sprintf("the fetch of %s#%d answered no workspace: %s", p.Repository, p.Number, step.Result)) - return - } - if len(t.fetched.Conflicts) > 0 { - if st.conflicted == nil { - st.conflicted = map[string]bool{} - } - st.conflicted[pullKey(p)] = true - if !st.narrowTry() { - st.j.writePreview(ctx, p, append(*merged, t.fetched)) - } - st.fail(step.Node, fmt.Sprintf("%s#%d %s%s", p.Repository, p.Number, ConflictWords, strings.Join(t.fetched.Conflicts, ", "))) - return - } - t.changed = t.fetched.Changed - if !changesBuildGo(p.Repository, st.goModule(), t.changed) { - t.runner = "" // the build module used no runner: the merge does not change it - } - if !placed { // the placed ask fetched the same merge: its preview was read from the first - *merged = append(*merged, t.fetched) - st.checkBinariesOf(t) // the levels still run: the verdict names every failure - } - ran := 0.0 - for _, la := range answer.Levels { - if placed { - if len(la.Unmet) > 0 { - st.clone += max(0, took-time.Duration(ran*float64(time.Second))) - st.fail(step.Node, fmt.Sprintf("the %s level of %s#%d left %s untested on %s, which was asked for a node with %s", - la.Level, p.Repository, p.Number, unmetNames(la.Unmet), step.Node, strings.Join(needs, ", "))) - return - } - la.Kept = nil // it names its packages and takes no other's kept verdict; this is only a guard against an older module - } - ran += st.level(ctx, t, la) // the next level was run too (buildstep): its failures are named as well - } - st.clone += max(0, took-time.Duration(ran*float64(time.Second))) - if unmet := buildstep.UnmetNeeds(answer.Levels); len(unmet) > 0 && !placed { - // Every level ran, its passes kept, and some packages were left to a node with what their tests need: the levels - // from the first that left one are asked of such a node, which takes the kept passes and tests those (issue 890). - var again, packages []string - seen := map[string]bool{} - for _, la := range answer.Levels { - if len(again) > 0 || len(la.Unmet) > 0 { - again = append(again, la.Level) - } - for _, u := range la.Unmet { - if !seen[u.Package] { - seen[u.Package] = true - packages = append(packages, u.Package) - } - } - } - st.j.logf("%s#%d: %s on %s left untested; asked of a node with %s", p.Repository, p.Number, unmetOf(answer.Levels), step.Node, strings.Join(unmet, ", ")) - st.askNeeding(ctx, t, merged, again, unmet, packages) - } -} - -// unmetNames names the unmet packages and their needs. -func unmetNames(unmet []buildstep.PackageNeeds) string { - var out []string - for _, u := range unmet { - out = append(out, u.Package+" (needs "+strings.Join(u.Needs, ", ")+")") - } - return strings.Join(out, ", ") -} - -// unmetOf names the unmet packages of every level. -func unmetOf(levels []buildstep.LevelAnswer) string { - var out []string - for _, la := range levels { - if len(la.Unmet) > 0 { - out = append(out, "at "+la.Level+" "+unmetNames(la.Unmet)) - } - } - return strings.Join(out, "; ") -} - -// level judges one level of a check ask's answer as the tools' answers were judged: the digest keys the kept verdicts -// of the packages that passed, the kept ones taken are counted, and every test result is read and counted, also after -// one that did not pass, so the verdict names every failed test of the level (issue 1921). It answers the level's -// running time (its tests' and setup's). -func (st *state) level(ctx context.Context, t *target, la buildstep.LevelAnswer) float64 { - if t.runner != "" && t.language == languageGo && la.Digest != nil { - t.judgedBy(la.Digest.JudgedBy) // the installed module judged it: the runner is not used again - } - if t.runner != "" && la.Judged != "" { - t.judgedBy(la.Judged) // the candidate judged the level: nothing is kept (ADR 0535 §4) - } - _, elapsed, results := st.levels.Level(t.pull.Repository, t.pull.Number, t.module, t.language, t.node, la) - st.facts = append(st.facts, rules.Step{Repository: t.pull.Repository, Number: t.pull.Number, Module: t.module, Language: t.language, Node: t.node, Level: rules.LevelFact(la)}) - var digests map[string]string - if t.language == languageGo && la.Digest != nil { - // The packages come as a list, never keyed by a package path (issue 1930); an answer stored before reads too. - digests = la.Digest.Map() - } - for _, res := range results { - if t.runner != "" { - t.judgedBy(res.JudgedBy) - } - st.stepOutOfStep(t.pull, res.OutOfStep) - if len(digests) > 0 { - if _, err := st.j.Kept.KeepPasses(ctx, t.pull.Repository, la.Level, t.toolchain, res, digests, st.j.now()); err != nil { - st.j.logf("%s#%d: a passing package could not be kept: %v", t.pull.Repository, t.pull.Number, err) - } - } - } - return elapsed -} - -// changesPaths says whether a merge changes a file inside one of the named paths (repository-relative). -func changesPaths(paths []string, merged []Fetched) bool { - for _, f := range merged { - for _, changed := range f.Changed { - for _, named := range paths { - named = strings.TrimSuffix(strings.TrimPrefix(named, "./"), "/") - if changed == named || strings.HasPrefix(changed, named+"/") { - return true - } - } - } - } - return false -} diff --git a/modules/mesh-delivery-checker/internal/check/judged_test.go b/modules/mesh-delivery-checker/internal/check/judged_test.go deleted file mode 100644 index 080e20c2..00000000 --- a/modules/mesh-delivery-checker/internal/check/judged_test.go +++ /dev/null @@ -1,208 +0,0 @@ -package check - -import ( - "context" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// The judged_by answer of mesh-build-go's digest and test steps (ADR 0535 §5; the milestone file's interface for issue -// 1898): "installed" for a step its installed program ran, "candidate " for one the candidate's program ran -// under runner workspace; an installed module older than the runner names nothing (it ignores the unknown argument: the -// SDK's tool server checks no argument against the input schema). - -const buildGoFile = "modules/mesh-build-go/cmd/mesh-build-go/digest.go" - -// candidateBench is a bench whose fetch changes the Go build module's directory, and whose build module runs the candidate -// when asked to (a module with the runner) or ignores the runner (one without). -func candidateBench(t *testing.T, takesRunner bool) *bench { - b := newBench(t) - b.fetch.Changed = []string{"README.md", buildGoFile} - judged := func(c stepCall) string { - if !takesRunner { - return "" - } - if c.args["runner"] == RunnerWorkspace { - return "candidate " + b.fetch.Merge - } - return "installed" - } - b.steps.handlers["build_go_digest"] = func(c stepCall) (any, error) { - d := echoBeside(b, b.digest[c.args["level"].(string)]) - d.JudgedBy = judged(c) - return d, nil - } - b.test = func(c stepCall) (any, error) { - r := captured[Tested](t, "go-test-slow") - r.Level, _ = c.args["level"].(string) - r.JudgedBy = judged(c) - return r, nil - } - return b -} - -func TestAMergeChangingTheGoBuildModuleIsAskedWithTheCandidateRunner(t *testing.T) { // 1898/AC1 - b := candidateBench(t, true) - b.judge(t, caseOf(t, pullOf(catalogueRepository, 9, head1))) - asked := append(b.steps.tool("digest"), b.steps.tool("test")...) - if len(asked) < 3 { - t.Fatalf("asked %+v, want the digest and both levels", asked) - } - for _, c := range asked { - if c.args["runner"] != RunnerWorkspace { - t.Errorf("%s %v was asked with runner %v, want %s", c.tool, c.args["level"], c.args["runner"], RunnerWorkspace) - } - } -} - -func TestAMergeNotChangingTheGoBuildModuleIsAskedWithoutARunner(t *testing.T) { // 1898/AC1 - for _, tc := range []struct { - name, repo, path string - }{ - {"another module of the catalogue", catalogueRepository, "modules/mesh-build-node/cmd/mesh-build-node/main.go"}, - {"a look-alike path in another repository", repoA, buildGoFile}, - } { - t.Run(tc.name, func(t *testing.T) { - b := candidateBench(t, true) - b.fetch.Changed = []string{tc.path} - out := b.judge(t, caseOf(t, pullOf(tc.repo, 9, head1))) - for _, c := range append(b.steps.tool("digest"), b.steps.tool("test")...) { - if _, ok := c.args["runner"]; ok { - t.Errorf("%s was asked with runner %v", c.tool, c.args["runner"]) - } - } - if len(out.Judged) != 0 { - t.Errorf("judged = %v, want nothing said of the program", out.Judged) - } - }) - } -} - -func TestAnInstalledModuleWithoutTheRunnerJudgesAsTodayAndTheLineSaysSo(t *testing.T) { // 1898/AC2 - b := candidateBench(t, false) - cs := caseOf(t, pullOf(catalogueRepository, 9, head1)) - out := b.judge(t, cs) - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", out.Verdict) - } - // As today: the digests key kept verdicts, and the levels are asked without the runner the module ignored. - for _, c := range b.steps.tool("test") { - if _, ok := c.args["runner"]; ok { - t.Errorf("the %v level was asked again with the runner the module does not take", c.args["level"]) - } - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 2 { - t.Errorf("kept %v, want the passing package at both levels, as today", keys) - } - p := cs.Pulls[0] - want := "judged by the installed mesh-build-go: it does not run a candidate yet" - if got := out.Judged[pullKey(p)]; got != want { - t.Fatalf("judged = %q, want %q", got, want) - } - if l := Line(out.Verdict, cs, p, out.Builds, out.Slowest, out.Judged); !strings.Contains(l, want) { - t.Errorf("line %q does not say %q", l, want) - } -} - -func TestACandidateJudgedCheckTakesNoKeptVerdictAndWritesNone(t *testing.T) { // 1898/AC3 - b := candidateBench(t, true) - if _, err := (Kept{Store: b.kept}).Keep(context.Background(), "unit", digA, keptValue("fixture/slow", 2)); err != nil { - t.Fatal(err) - } - out := b.judge(t, caseOf(t, pullOf(catalogueRepository, 9, head1))) - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", out.Verdict) - } - tests := b.steps.tool("test") - if len(tests) != 2 { - t.Fatalf("test calls = %+v, want both levels", tests) - } - for _, c := range tests { - if _, ok := c.args["packages"]; ok { - t.Errorf("the %v level was asked for %v, want it whole", c.args["level"], c.args["packages"]) - } - } - if u := out.Verdict.Levels[buildstep.LevelUnit]; u.Kept != 0 { - t.Errorf("unit = %+v, want nothing taken", u) - } - if keys, _ := b.kept.Keys(context.Background()); len(keys) != 1 || keys[0] != "unit."+digA { - t.Errorf("kept keys = %v, want only the one kept before", keys) - } -} - -func TestTheLineNamesTheCandidateThatJudged(t *testing.T) { // 1898/AC6 - b := candidateBench(t, true) - cs := caseOf(t, pullOf(catalogueRepository, 9, head1)) - out := b.judge(t, cs) - p := cs.Pulls[0] - want := "judged by the candidate's mesh-build-go at " + b.fetch.Merge[:12] - for name, l := range map[string]string{ - "line": Line(out.Verdict, cs, p, out.Builds, out.Slowest, out.Judged), - "report": Report(out.Verdict, cs, p, out.Builds, out.Slowest, out.Judged), - } { - if !strings.Contains(l, want) { - t.Errorf("the %s %q does not say %q", name, l, want) - } - } - // A long failing line keeps the program it was judged by; only the lesser parts go. - v := out.Verdict - v.State = meshdeliverycheckerclient.VerdictFail - v.Failed = &FailedFirst{Test: "unit: git.novox.be/novox/mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go.TestADigestFoldsTheSiblings", Words: strings.Repeat("x", 60)} - if l := Line(v, cs, p, out.Builds, out.Slowest, out.Judged); !strings.Contains(l, "judged by the candidate's") { - t.Errorf("the long line %q dropped the program", l) - } -} - -func TestInAGroupOnlyTheBuildModulesPullRequestNamesItsProgram(t *testing.T) { // 1898/AC6 - b := candidateBench(t, true) - cs := caseOf(t, pullOf(catalogueRepository, 9, head1), pullOf(repoA, 7, head1)) - out := b.judge(t, cs) - for _, p := range cs.Pulls { - l := Report(out.Verdict, cs, p, out.Builds, out.Slowest, out.Judged) - says := strings.Contains(l, "judged by") - if says != (p.Repository == catalogueRepository) { - t.Errorf("%s#%d: %q", p.Repository, p.Number, l) - } - } -} - -func TestACandidateThatDoesNotCompileFailsTheCheckWithTheCompilersWords(t *testing.T) { // ADR 0535 §3, checker side - b := candidateBench(t, true) - refusal := "the candidate's mesh-build-go does not compile: modules/mesh-build-go/cmd/mesh-build-go/digest.go:12:2: undefined: x" - b.steps.handlers["build_go_digest"] = func(stepCall) (any, error) { return nil, errorString(refusal) } - b.test = func(stepCall) (any, error) { return nil, errorString(refusal) } - out := b.judge(t, caseOf(t, pullOf(catalogueRepository, 9, head1))) - if out.Verdict.State != meshdeliverycheckerclient.VerdictFail || !strings.Contains(out.Verdict.Failed.Words, "undefined: x") { - t.Fatalf("verdict = %+v, want a failure naming the compile error", out.Verdict) - } - if n := len(b.steps.tool("test")); n != 1 { - t.Errorf("%d test steps, want the first one's failure to end the check", n) - } -} - -type errorString string - -func (e errorString) Error() string { return string(e) } - -func TestTheStatusTheForgeShowsNamesTheProgramThatJudged(t *testing.T) { // 1898/AC6, the forge's line - ctx := context.Background() - words := judgedByCandidate(goBuildModule) + "fedcba987654" - j := &judgeFunc{f: func(context.Context, *Case, int) (Outcome, error) { - return Outcome{Verdict: passing(), Judged: map[string]string{repoA + "#7": words}}, nil - }} - r := newRig(t, j) - r.ch.Candidate(ctx, "feat/x", candidateOf(t, "feat/x", pullOf(repoA, 7, head1))) - r.ch.Wait() - key, _ := CheckerOwedKey(repoA, 7, head1) - raw, _, _, _ := r.owed.Get(ctx, key) - o, err := DecodeCheckerOwed(raw) - if err != nil { - t.Fatal(err) - } - if o.State != "success" || !strings.Contains(o.Text, words) { - t.Errorf("status %+v, want success saying %q", o, words) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/kept.go b/modules/mesh-delivery-checker/internal/check/kept.go deleted file mode 100644 index e00b5b2f..00000000 --- a/modules/mesh-delivery-checker/internal/check/kept.go +++ /dev/null @@ -1,101 +0,0 @@ -package check - -import ( - "context" - "errors" - "fmt" - "sort" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// Kept is the checker's bucket of kept verdicts (ADR 0444, ADR 0484 clause 13): a passing verdict per package and level, -// under the digest the language's build module named for it. A key is written once, by a create that fails when it -// exists, from the result the checker read from the test step and never from anything the code under test wrote; nothing -// changes it afterwards. -type Kept struct{ Store KV } - -// Lookup is the kept verdict of a level under a digest, or none. -func (k Kept) Lookup(ctx context.Context, level, digest string) (meshdeliverychecker.KeptByLevelDigestValue, bool, error) { - key, err := meshdeliverycheckerclient.KeptKey(level, digest) - if err != nil { - return meshdeliverychecker.KeptByLevelDigestValue{}, false, err - } - value, _, found, err := k.Store.Get(ctx, key) - if err != nil || !found { - return meshdeliverychecker.KeptByLevelDigestValue{}, false, err - } - v, err := meshdeliverycheckerclient.DecodeKept(value) - if err != nil { - return meshdeliverychecker.KeptByLevelDigestValue{}, false, fmt.Errorf("%s holds what is not a kept verdict: %w", key, err) - } - return v, true, nil -} - -// Keep writes v under level and digest by a create. It answers false, and writes nothing, when the key exists: the first -// value stands. -func (k Kept) Keep(ctx context.Context, level, digest string, v meshdeliverychecker.KeptByLevelDigestValue) (bool, error) { - key, err := meshdeliverycheckerclient.KeptKey(level, digest) - if err != nil { - return false, err - } - v.Level = level - data, err := meshdeliverycheckerclient.EncodeKept(v) - if err != nil { - return false, err - } - if _, err := k.Store.Create(ctx, key, data); err != nil { - if errors.Is(err, ErrConflict) { - return false, nil - } - return false, fmt.Errorf("%s: %w", key, err) - } - return true, nil -} - -// KeepPasses keeps, for the packages of a test result that passed with tests, a verdict under the digest the build module -// named for the package at the level. A package that failed, has no tests or has no digest is never kept (ADR 0444 clause -// 1): it runs every check. A package with skipped tests is not kept either: a pass that skipped (a test needing a -// toolchain the node lacks) would be reused for its digest and never run anywhere (issue 890). A build module that -// counts skips only per result, not per package, keeps nothing of a result in which a test skipped. It answers the -// packages it kept now. -func (k Kept) KeepPasses(ctx context.Context, repository, level, toolchain string, res Tested, digests map[string]string, at time.Time) ([]string, error) { - var kept []string - perPackage := false - for _, p := range res.Packages { - perPackage = perPackage || p.Skipped > 0 - } - if res.Skipped > 0 && !perPackage { - return kept, nil // an older build module: the skip cannot be placed in a package - } - for _, p := range res.Packages { - digest, ok := digests[p.Package] - if !ok || !p.Passed || p.NoTests || p.Tests == 0 || p.Skipped > 0 { - continue - } - if failedIn(res, p.Package) { - continue - } - made, err := k.Keep(ctx, level, digest, meshdeliverychecker.KeptByLevelDigestValue{State: meshdeliverycheckerclient.VerdictPass, Repository: repository, Package: p.Package, - Level: level, Tests: int64(p.Tests), Took: int64(p.Elapsed * 1000), Toolchain: toolchain, At: at.UTC().Format(time.RFC3339)}) - if err != nil { - return kept, err - } - if made { - kept = append(kept, p.Package) - } - } - sort.Strings(kept) - return kept, nil -} - -func failedIn(res Tested, pkg string) bool { - for _, f := range res.Failed { - if f.Package == pkg { - return true - } - } - return false -} diff --git a/modules/mesh-delivery-checker/internal/check/kept_test.go b/modules/mesh-delivery-checker/internal/check/kept_test.go deleted file mode 100644 index f6a7abd5..00000000 --- a/modules/mesh-delivery-checker/internal/check/kept_test.go +++ /dev/null @@ -1,130 +0,0 @@ -package check - -import ( - "context" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -var at0 = time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC) - -const ( - digA = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11" - digB = "bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22" -) - -func keptValue(pkg string, tests int) meshdeliverychecker.KeptByLevelDigestValue { - return meshdeliverychecker.KeptByLevelDigestValue{State: meshdeliverycheckerclient.VerdictPass, Repository: "novox/mesh-delivery", Package: pkg, Tests: int64(tests), Took: 120, Toolchain: "go1.26.0", At: at0.UTC().Format(time.RFC3339)} -} - -func TestAPassIsKeptByACreateUnderItsLevelAndDigest(t *testing.T) { // 1287/AC1 - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - made, err := k.Keep(ctx, buildstep.LevelUnit, digA, keptValue("m/a", 3)) - if err != nil || !made { - t.Fatalf("Keep = %v, %v, want made", made, err) - } - key, _ := meshdeliverycheckerclient.KeptKey(buildstep.LevelUnit, digA) - if st.creates[key] != 1 { - t.Fatalf("the key %s was created %d times, want once", key, st.creates[key]) - } - got, found, err := k.Lookup(ctx, buildstep.LevelUnit, digA) - if err != nil || !found || got.Package != "m/a" || got.Tests != 3 || got.Level != buildstep.LevelUnit || got.Toolchain != "go1.26.0" { - t.Fatalf("Lookup = %+v, %v, %v", got, found, err) - } -} - -func TestASecondKeepUnderTheSameDigestIsRefusedAndTheFirstStands(t *testing.T) { // 1287/AC2 - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - if made, err := k.Keep(ctx, buildstep.LevelUnit, digA, keptValue("m/a", 3)); err != nil || !made { - t.Fatal(made, err) - } - made, err := k.Keep(ctx, buildstep.LevelUnit, digA, keptValue("m/other", 9)) - if err != nil || made { - t.Fatalf("the second Keep = %v, %v, want refused without an error", made, err) - } - got, _, _ := k.Lookup(ctx, buildstep.LevelUnit, digA) - if got.Package != "m/a" || got.Tests != 3 { - t.Errorf("the first value did not stand: %+v", got) - } -} - -func TestOnlyPassingPackagesWithADigestAreKept(t *testing.T) { // 1287/AC3, AC4 - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - res := Tested{Level: "unit", Passed: false, Packages: []GoPackage{ - {Package: "m/a", Passed: true, Tests: 4, Elapsed: 0.2}, - {Package: "m/b", Passed: false, Tests: 2}, - {Package: "m/c", Passed: true, Tests: 1}, // no digest named - {Package: "m/d", Passed: true, NoTests: true}, // no tests - }, Failed: []Failure{{Package: "m/b", Test: "TestB"}}} - kept, err := k.KeepPasses(ctx, "novox/mesh-delivery", "unit", "go1.26.0", res, map[string]string{"m/a": digA, "m/b": digB, "m/d": digB}, at0) - if err != nil { - t.Fatal(err) - } - if strings.Join(kept, ",") != "m/a" { - t.Fatalf("kept %v, want only m/a", kept) - } - if keys, _ := st.Keys(ctx); len(keys) != 1 { - t.Errorf("the bucket holds %v, want one key", keys) - } -} - -func TestAResultWithSkippedTestsKeepsNothing(t *testing.T) { // issue 890: a pass that skipped proved nothing - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - res := Tested{Level: "integration", Passed: true, Skipped: 2, Packages: []GoPackage{{Package: "m/a", Passed: true, Tests: 4}}} - kept, err := k.KeepPasses(ctx, "novox/mesh-catalog", "integration", "go1.26.0", res, map[string]string{"m/a": digA}, at0) - if err != nil || len(kept) != 0 { - t.Fatalf("kept %v, %v, want nothing", kept, err) - } - if keys, _ := st.Keys(ctx); len(keys) != 0 { - t.Errorf("the bucket holds %v, want none", keys) - } -} - -func TestOnlyTheSkippingPackageOfAResultIsWithheld(t *testing.T) { // issue 890 - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - res := Tested{Level: "integration", Passed: true, Skipped: 2, Packages: []GoPackage{ - {Package: "m/a", Passed: true, Tests: 4, Skipped: 2}, - {Package: "m/b", Passed: true, Tests: 3}, - }} - kept, err := k.KeepPasses(ctx, "novox/mesh-catalog", "integration", "go1.26.0", res, map[string]string{"m/a": digA, "m/b": digB}, at0) - if err != nil || strings.Join(kept, ",") != "m/b" { - t.Fatalf("kept %v, %v, want only m/b", kept, err) - } -} - -func TestAKeptValueOfAnotherDigestIsNotRead(t *testing.T) { // 1287/AC7 - ctx, st := context.Background(), newMemKV() - k := Kept{Store: st} - if _, err := k.Keep(ctx, buildstep.LevelUnit, digA, keptValue("m/a", 3)); err != nil { - t.Fatal(err) - } - // A toolchain change changes the digest, so the new digest finds nothing and the old key is not deleted. - if _, found, _ := k.Lookup(ctx, buildstep.LevelUnit, digB); found { - t.Error("a digest that was not kept was found") - } - if _, found, _ := k.Lookup(ctx, buildstep.LevelIntegration, digA); found { - t.Error("a kept unit verdict was found at the integration level") - } - if keys, _ := st.Keys(ctx); len(keys) != 1 { - t.Errorf("the old key was removed: %v", keys) - } -} - -func TestAKeptVerdictThatIsNotAPassIsRefused(t *testing.T) { - k := Kept{Store: newMemKV()} - v := keptValue("m/a", 1) - v.State = meshdeliverycheckerclient.VerdictFail - if _, err := k.Keep(context.Background(), buildstep.LevelUnit, digA, v); err == nil { - t.Error("a failure was kept") - } -} diff --git a/modules/mesh-delivery-checker/internal/check/line.go b/modules/mesh-delivery-checker/internal/check/line.go deleted file mode 100644 index 0dca6e35..00000000 --- a/modules/mesh-delivery-checker/internal/check/line.go +++ /dev/null @@ -1,394 +0,0 @@ -package check - -import ( - "fmt" - "mesh-delivery-checker/rules" - "slices" - "strings" - "unicode/utf8" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// MaxLine is the most characters a status's line has: the forge shows a status's description in a short space, and the full -// report is linked (ADR 0447 clause 3, ADR 0484 clause 7). A line that would be longer drops its lesser parts in a fixed -// order, and is cut last. -const MaxLine = 140 - -// seconds is a time in milliseconds in the unit a person reads: 350ms, 4.1s, 72s. -func seconds(ms int64) string { - switch { - case ms < 1000: - return fmt.Sprintf("%dms", ms) - case ms < 10000: - return fmt.Sprintf("%.1fs", float64(ms)/1000) - } - return fmt.Sprintf("%ds", (ms+500)/1000) -} - -func short12(s string) string { - if len(s) > 12 { - return s[:12] - } - return s -} - -// firstFailed is what failed first, in words: a conflict, a refused step or a test, named before anything else. -func firstFailed(v meshdeliverychecker.VerdictsByGroupHeadsValue) string { - f := failedOf(v) - switch { - case f.Test != "": - if w := firstWordsLine(f.Words); w != "" { - return f.Test + ": " + w - } - return f.Test - case f.Machine != "" && firstWordsLine(f.Words) != "": - return f.Machine + ": " + firstWordsLine(f.Words) - case f.Machine != "": - return f.Machine - } - return firstWordsLine(f.Words) -} - -// failedLines is everything a verdict names as failed, one line each: every failed test with its first line (issue -// 1921), or what failed first when the verdict names no list (a conflict, a refused step, an older verdict). -func failedLines(v meshdeliverychecker.VerdictsByGroupHeadsValue) []string { - if len(v.Failures) == 0 { - if f := firstFailed(v); f != "" { - return []string{f} - } - return nil - } - lines := make([]string, 0, len(v.Failures)) - for _, f := range v.Failures { - lines = append(lines, firstFailed(meshdeliverychecker.VerdictsByGroupHeadsValue{Failed: &FailedFirst{Machine: f.Machine, Test: f.Test, Words: f.Words}})) - } - return lines -} - -// failedHead is the head of a failed verdict's line: what failed first, and when more did, how many failed and "and N -// more" after it (the operator, 2026-11-02), so a line cut to MaxLine still says that more failed. The words of the first -// are cut to fit room characters when room is not negative; the count and "and N more" are never cut. -func failedHead(v meshdeliverychecker.VerdictsByGroupHeadsValue, room int) string { - first := firstFailed(v) - n := rules.FailedCount(v) - if n <= 1 { - return clipRunes("failed: "+first, room) - } - prefix, more := fmt.Sprintf("failed (%d): ", n), fmt.Sprintf("; and %d more", n-1) - if room >= 0 { - first = clipRunes(first, room-utf8.RuneCountInString(prefix+more)) - } - return prefix + first + more -} - -// clipRunes cuts s to at most n characters, the last an ellipsis; a negative n leaves it whole. -func clipRunes(s string, n int) string { - r := []rune(s) - if n < 0 || len(r) <= n { - return s - } - if n < 1 { - return "" - } - return string(r[:n-1]) + "…" -} - -// firstWordsLine is the first line of a failure's words: the words are several lines (issue 1901) and the status line one. -func firstWordsLine(w string) string { return rules.FirstLine(w) } - -// pieces are the parts of a line, from the most to the least needed. -type pieces struct { - head, commits, counts, shortCounts, times, shortTimes, over, builds, composition, shortComposition, group string - // judged names the program that judged a change to a build module (ADR 0535 §5); never dropped. - judged string - // carried says, of a carried verdict, the commits it was carried from (ADR 0548 §8); never dropped. - carried string - // warning says that a part of the check did not run, for instance that an older build module could not refuse a compiled program; never dropped. - warning string - // series names each change of a package series the pull request makes (design 32, DLV-C4); never dropped. - series string -} - -// pullKey names a pull request of a case: `#`. -func pullKey(p Pull) string { return fmt.Sprintf("%s#%d", p.Repository, p.Number) } - -func (p pieces) join(drop int) string { - parts := []string{p.head, p.commits, p.carried, p.warning, p.counts, p.judged, p.series} - switch { - case drop < 3 && p.times != "": - parts = append(parts, p.times) - case drop == 3 && p.shortTimes != "": - parts = []string{p.head, p.commits, p.carried, p.warning, p.shortCounts, p.judged, p.series, p.shortTimes} - } - if p.over != "" { - parts = append(parts, p.over) - } - if drop < 1 && p.builds != "" { - parts = append(parts, p.builds) - } - if drop < 2 && p.group != "" { - parts = append(parts, p.group) - } - switch { - case drop >= 3 && p.shortComposition != "": - parts = append(parts, p.shortComposition) - case p.composition != "": - parts = append(parts, p.composition) - } - return joinSet(parts...) -} - -func levelWords(name string, r meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue) string { - s := fmt.Sprintf("%s %d run, %d kept", name, r.Run, r.Kept) - if r.Skipped > 0 { - s += fmt.Sprintf(", %d skipped", r.Skipped) - } - return s -} - -// compositionWords says the composition: not yet run, or once the engines answered (ADR 0593 §7) passed, with the nodes -// that gave no answer, or failed, with the time it took and the machines it composed (issue 1300). -func compositionWords(c *meshdeliverychecker.VerdictsByGroupHeadsValueComposition, took int64) string { - return composition(c, took, false) -} - -// composition is compositionWords; short counts the machines and leaves the SDK commit to the report. -func composition(c *meshdeliverychecker.VerdictsByGroupHeadsValueComposition, took int64, short bool) string { - if c != nil && c.State == meshdeliverycheckerclient.CompositionNotRun && c.Words != "" { - return "composition not judged: " + c.Words - } - if c == nil || c.State == meshdeliverycheckerclient.CompositionNotRun { - return "composition not yet run" - } - in := "" - if took > 0 { - in = " in " + seconds(took) - } - on := "" - if len(c.Machines) > 0 { - machines := slices.Sorted(slices.Values(c.Machines)) - on = " on " + strings.Join(machines, ", ") - switch { - case len(machines) > 1 && short: - on = fmt.Sprintf(" on %d machines", len(machines)) - case len(machines) > 1: - on = fmt.Sprintf(" on %d machines: %s", len(machines), strings.Join(machines, ", ")) - } - } - warned := "" - if c.State == meshdeliverycheckerclient.CompositionPass && c.Words != "" { - warned = " (" + c.Words + ")" // a node that did not answer (ADR 0593 §9) - } - switch { - case c.State == meshdeliverycheckerclient.CompositionPass && short: - return "composition passed" + in + on + warned // what it composed with is in the report - case c.State == meshdeliverycheckerclient.CompositionPass: - return "composition passed" + in + " with " + short12(c.Sdk) + on + warned - } - return "composition failed" + in + on + ": " + c.Words -} - -func buildWords(builds []BuildNote) string { - if len(builds) == 0 { - return "" - } - parts := make([]string, len(builds)) - for i, b := range builds { - parts[i] = b.Module + " " + b.State - } - return "builds: " + strings.Join(parts, ", ") -} - -// carriedWords say the commits a verdict was carried from, for the line of pull request p (ADR 0548 §8): its own trunk's -// commit as twelve characters, a sibling's with its name before it; only those that moved, sorted. Empty when none did. -func carriedWords(from map[string]string, v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case, p Pull) string { - var own string - var others []string - for r, old := range from { - now, ok := v.Trunk[r] - if cs != nil && !ok { - now, ok = cs.Trunk[r] - } - if !ok { - now = v.Beside[r] - } - if now == "" || now == old { - continue - } - if strings.EqualFold(r, p.Repository) { - own = short12(old) - continue - } - name := r - if i := strings.LastIndex(r, "/"); i >= 0 { - name = r[i+1:] - } - others = append(others, name+" "+short12(old)) - } - slices.Sort(others) - if own != "" { - others = append([]string{own}, others...) - } - return strings.Join(others, ", ") -} - -// parts makes the pieces of the line of a pull request of a case. -func parts(v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case, p Pull, builds []BuildNote, slowest []string, judged map[string]string) pieces { - var pc pieces - pc.judged = judged[pullKey(p)] - pc.series = seriesWords(cs, p) - if cs != nil { - pc.warning = cs.Warning(pullKey(p)) - } - commits := short12(p.Head) + " on " + short12(p.Trunk) - switch v.State { - case meshdeliverycheckerclient.VerdictRunning: - pc.head = "checking " + commits - if cs != nil && cs.CarriedFrom != nil { - // Out of date and being carried to the new commit (ADR 0548 §6): pending, and saying so. - pc.head = "being carried to " + commits - if w := carriedWords(cs.CarriedFrom, v, cs, p); w != "" { - pc.carried = "from " + w - } - } - case meshdeliverycheckerclient.VerdictPass: - pc.head = "pass " + commits - if w := carriedWords(v.CarriedFrom, v, cs, p); w != "" { - pc.carried = "carried from " + w // the counts that follow say what the carry ran and kept - } - default: - pc.head = failedHead(v, -1) - pc.commits = commits - } - var counts []string - for _, level := range []string{buildstep.LevelUnit, buildstep.LevelIntegration} { - if r, ok := v.Levels[level]; ok { - counts = append(counts, levelWords(level, r)) - } - } - pc.counts = strings.Join(counts, " | ") - var short []string - for _, level := range []string{buildstep.LevelUnit, buildstep.LevelIntegration} { - if r, ok := v.Levels[level]; ok { - name := map[string]string{buildstep.LevelUnit: "unit", buildstep.LevelIntegration: "int"}[level] - w := fmt.Sprintf("%s %d run %d kept", name, r.Run, r.Kept) - if r.Skipped > 0 { - w += fmt.Sprintf(" %d skipped", r.Skipped) - } - short = append(short, w) - } - } - pc.shortCounts = strings.Join(short, " | ") - if v.State != meshdeliverycheckerclient.VerdictRunning { - t := tookOf(v) - var times []string - for _, x := range []struct { - name string - ms int64 - }{{"queue", t.Queue}, {"clone", t.Clone}, {"lint", t.Lint}, {"unit", t.Unit}, {"int setup", t.IntegrationSetup}, {"int run", t.IntegrationRun}} { - if x.ms > 0 { - times = append(times, x.name+" "+seconds(x.ms)) - } - } - if len(times) > 0 { - pc.times = strings.Join(times, ", ") - } - // The compact form for a line that is too long: the parts that dominate, the integration level's setup and run as one. - short = short[:0:0] - if t.Clone > 0 { - short = append(short, "clone "+seconds(t.Clone)) - } - if t.Lint > 0 { - short = append(short, "lint "+seconds(t.Lint)) - } - if t.Unit > 0 { - short = append(short, "unit "+seconds(t.Unit)) - } - switch { - case t.IntegrationSetup > 0 && t.IntegrationRun > 0: - short = append(short, "int "+seconds(t.IntegrationSetup)+"+"+seconds(t.IntegrationRun)) - case t.IntegrationRun > 0: - short = append(short, "int "+seconds(t.IntegrationRun)) - } - pc.shortTimes = strings.Join(short, " ") - } - if len(v.OverBound) > 0 { - pc.over = "over bound: " + strings.Join(v.OverBound, ", ") - if len(slowest) > 0 { - pc.over += " (slowest " + strings.Join(slowest, ", ") + ")" - } - } - pc.builds = buildWords(builds) - if v.State != meshdeliverycheckerclient.VerdictRunning { - pc.composition = compositionWords(v.Composition, tookOf(v).Composition) - pc.shortComposition = composition(v.Composition, tookOf(v).Composition, true) - } - if cs != nil && len(cs.Pulls) > 1 { - pc.group = fmt.Sprintf("group %s of %d pull requests", cs.Group, len(cs.Pulls)) - if v.State != meshdeliverycheckerclient.VerdictPass { - pc.group = fmt.Sprintf("%s#%d in group %s", p.Repository, p.Number, cs.Group) - } - } - return pc -} - -// Report is the full line of a pull request's status, with no limit: what the read tools and the full report say. -func Report(v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case, p Pull, builds []BuildNote, slowest []string, judged map[string]string) string { - return parts(v, cs, p, builds, slowest, judged).join(0) -} - -// Line is the words of a status for one pull request of a case (ADR 0447 clause 3, ADR 0484 clause 7): the verdict first (a -// failure names the conflict, the refused step or the test before anything), the head and the trunk commit as twelve -// characters each, the tests run, kept and skipped per level, the time of each part, a level over its bound with its slowest -// tests, the builds of the touched modules, and that the composition was not yet run; for a change to a build module, the -// program that judged it (judged, by pullKey; ADR 0535 §5). It is at most MaxLine characters: the -// builds, then the group drop first, then the times are given in their compact form, then dropped, and the line is cut at last. -// The program that judged is never dropped: a line naming it gives the counts in their compact form, then drops them and -// what follows, and is cut in its first part rather than lose it. -func Line(v meshdeliverychecker.VerdictsByGroupHeadsValue, cs *Case, p Pull, builds []BuildNote, slowest []string, judged map[string]string) string { - pc := parts(v, cs, p, builds, slowest, judged) - if v.State == meshdeliverycheckerclient.VerdictFail { - // The head gives way first to the commits and what is never dropped: its count and "and N more" stay whole. - kept := joinSet(pc.commits, pc.carried, pc.warning, pc.judged, pc.series) - pc.head = failedHead(v, MaxLine-utf8.RuneCountInString(" | "+kept)) - } - for drop := 0; drop <= 4; drop++ { - if s := pc.join(drop); utf8.RuneCountInString(s) <= MaxLine { - return s - } - } - if pc.judged != "" || pc.series != "" { - for _, counts := range []string{pc.shortCounts, ""} { - if s := joinSet(pc.head, pc.commits, counts, pc.judged, pc.series); utf8.RuneCountInString(s) <= MaxLine { - return s - } - } - rest := " | " + joinSet(pc.commits, pc.judged, pc.series) - room := MaxLine - utf8.RuneCountInString(rest) - 1 - if head := []rune(pc.head); room > 0 && len(head) > room { - return string(head[:room]) + "…" + rest - } - } - if v.State == meshdeliverycheckerclient.VerdictFail { - // A failure's head, sized above, and the commits fit: the counts and the composition are in the report. - if s := joinSet(pc.head, pc.commits, pc.carried, pc.warning, pc.judged, pc.series); utf8.RuneCountInString(s) <= MaxLine { - return s - } - } - runes := []rune(pc.join(4)) - return string(runes[:MaxLine-1]) + "…" -} - -// joinSet joins the parts that are not empty. -func joinSet(parts ...string) string { - out := make([]string, 0, len(parts)) - for _, s := range parts { - if s != "" { - out = append(out, s) - } - } - return strings.Join(out, " | ") -} diff --git a/modules/mesh-delivery-checker/internal/check/line_test.go b/modules/mesh-delivery-checker/internal/check/line_test.go deleted file mode 100644 index 2b80bb72..00000000 --- a/modules/mesh-delivery-checker/internal/check/line_test.go +++ /dev/null @@ -1,193 +0,0 @@ -package check - -import ( - "fmt" - "strings" - "testing" - "unicode/utf8" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -func lineCase(t testing.TB, pulls ...meshcatalog.CandidatesByGroupValuePullsItem) (*Case, Pull) { - cs := caseOf(t, pulls...) - return cs, cs.Pulls[0] -} - -func passVerdict() meshdeliverychecker.VerdictsByGroupHeadsValue { - return meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictPass, - Levels: map[string]meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{buildstep.LevelUnit: {Run: 12, Kept: 301}, buildstep.LevelIntegration: {Run: 3, Kept: 40}}, - Took: &meshdeliverychecker.VerdictsByGroupHeadsValueTook{Queue: 800, Clone: 4100, Unit: 400, IntegrationSetup: 3200, IntegrationRun: 12000}, - Composition: &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionNotRun}} -} - -func TestAPassOpensWithTheVerdictAndSaysTheCommitsTheCountsTheTimesAndThatTheCompositionWasNotRun(t *testing.T) { // 1300/AC1 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - full := Report(passVerdict(), cs, p, nil, nil, nil) - for _, want := range []string{"pass 111111111111 on aaaaaaaaaaaa", "unit 12 run, 301 kept", "integration 3 run, 40 kept", "queue 800ms", "clone 4.1s", "unit 400ms", "int setup 3.2s", "int run 12s", "composition not yet run"} { - if !strings.Contains(full, want) { - t.Errorf("the report %q lacks %q", full, want) - } - } - if !strings.HasPrefix(full, "pass ") || strings.Contains(full, "composition passed") { - t.Errorf("report = %q", full) - } - line := Line(passVerdict(), cs, p, nil, nil, nil) - if n := utf8.RuneCountInString(line); n > MaxLine { - t.Errorf("the line is %d characters, want at most %d: %q", n, MaxLine, line) - } - if !strings.HasPrefix(line, "pass 111111111111 on aaaaaaaaaaaa") || !strings.Contains(line, "composition not yet run") || !strings.Contains(line, "unit 12 run 301 kept") || - !strings.Contains(line, "clone 4.1s") || !strings.Contains(line, "int 3.2s+12s") { - t.Errorf("line = %q", line) - } -} - -func TestAFailureNamesTheTestTheConflictOrTheRefusedStepFirst(t *testing.T) { // 1300/AC2 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - long := FailedFirst{Test: "integration: " + strings.Repeat("pkg/", 50) + "TestSomething", Words: strings.Repeat("because ", 40)} - for name, c := range map[string]struct { - v meshdeliverychecker.VerdictsByGroupHeadsValue - want string - }{ - "test": {meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Test: "unit: m/a.TestX", Words: "want 4"}}, "failed: unit: m/a.TestX: want 4"}, - "conflict": {meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: "shanks", Words: "novox/mesh-delivery#7 does not merge into the trunk: files in conflict: go.mod"}}, "failed: shanks: novox/mesh-delivery#7 does not merge into the trunk: files in conflict: go.mod"}, - "refused": {meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: "mesh-build-go", Words: "no go build node qualifies: builders disagree"}}, "failed: mesh-build-go: no go build node qualifies"}, - // ADR 0591: a first failure longer than the forge's limit is shortened; "failed (N)" and "and N-1 more" are never cut. - "many, the first too long": {meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &long, Failures: failureItems([]FailedFirst{long, {Machine: "g14", Words: "route-proxy is defined twice"}, - {Test: "unit: m/b.TestY", Words: "boom"}})}, "failed (3): integration: pkg/pkg/"}, - } { - line := Line(c.v, cs, p, nil, nil, nil) - if more := len(c.v.Failures) - 1; more > 0 && (!strings.Contains(line, fmt.Sprintf("…; and %d more | 111111111111", more)) || utf8.RuneCountInString(line) > MaxLine) { - t.Errorf("%s: line = %q (%d), want the first's words shortened, the count and \"and %d more\" whole, within %d", name, line, utf8.RuneCountInString(line), more, MaxLine) - } - if !strings.HasPrefix(line, c.want) || !strings.Contains(line, "111111111111 on aaaaaaaaaaaa") { - t.Errorf("%s: line = %q, want it to open with %q and name the commits", name, line, c.want) - } - } -} - -// The status line and its full report say each part of a verdict, within the forge's limit (issue 1300, ADR 0484). -func TestTheLineAndTheReportSayEachPartOfTheVerdict(t *testing.T) { - for _, row := range []struct { - name string - test func(t *testing.T) - }{ - {"a level over its bound is named with its slowest tests and the verdict is not changed", func(t *testing.T) { // 1300/AC3 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := passVerdict() - v.OverBound = []string{"unit"} - line := Line(v, cs, p, nil, []string{"unit pkg.TestSlow 7.0s"}, nil) - if !strings.HasPrefix(line, "pass ") || !strings.Contains(Report(v, cs, p, nil, []string{"unit pkg.TestSlow 7.0s"}, nil), "over bound: unit (slowest unit pkg.TestSlow 7.0s)") { - t.Errorf("line = %q", line) - } - }}, - {"skipped tests are said and never a flat pass", func(t *testing.T) { // 1300/AC6 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := passVerdict() - v.Levels[buildstep.LevelUnit] = meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{Run: 12, Kept: 301, Skipped: 2} - if line := Line(v, cs, p, nil, nil, nil); !strings.Contains(line, "unit 12 run, 301 kept, 2 skipped") { - t.Errorf("line = %q", line) - } - }}, - {"a groups members each carry the same verdict", func(t *testing.T) { // 1300/AC4 - cs := caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2)) - a := Line(passVerdict(), cs, cs.Pulls[0], nil, nil, nil) - b := Line(passVerdict(), cs, cs.Pulls[1], nil, nil, nil) - if !strings.HasPrefix(a, "pass ") || !strings.HasPrefix(b, "pass ") || !strings.Contains(Report(passVerdict(), cs, cs.Pulls[0], nil, nil, nil), "group feat/x of 2 pull requests") { - t.Errorf("lines %q and %q", a, b) - } - if !strings.Contains(a, "111111111111") || !strings.Contains(b, "222222222222") { - t.Errorf("each member's line names its own head: %q and %q", a, b) - } - }}, - {"the builds of the touched modules are said and dropped first when the line is too long", func(t *testing.T) { // ADR 0484 clause 7 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - builds := []BuildNote{{"avahi", "built"}, {"mesh-tools", "waiting"}} - if r := Report(passVerdict(), cs, p, builds, nil, nil); !strings.Contains(r, "builds: avahi built, mesh-tools waiting") { - t.Errorf("report = %q", r) - } - line := Line(passVerdict(), cs, p, builds, nil, nil) - if utf8.RuneCountInString(line) > MaxLine { - t.Errorf("line %q is too long", line) - } - short := passVerdict() - short.Took = &meshdeliverychecker.VerdictsByGroupHeadsValueTook{} - if l := Line(short, cs, p, builds[:1], nil, nil); !strings.Contains(l, "builds: avahi built") { - t.Errorf("a short line keeps its builds: %q", l) - } - }}, - {"a line is never longer than the limit", func(t *testing.T) { // 1300/AC1 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Test: "integration: " + strings.Repeat("pkg/", 50) + ".TestSomething", Words: strings.Repeat("because ", 40)}} - if line := Line(v, cs, p, nil, nil, nil); utf8.RuneCountInString(line) > MaxLine || !strings.HasPrefix(line, "failed: integration: ") { - t.Errorf("line = %q (%d)", line, utf8.RuneCountInString(line)) - } - }}, - {"a running check is pending", func(t *testing.T) { - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - if line := Line(meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictRunning}, cs, p, nil, nil, nil); line != "checking 111111111111 on aaaaaaaaaaaa" { - t.Errorf("line = %q", line) - } - }}, - {"the passed composition is said only when it was run", func(t *testing.T) { // ADR 0484 clause 6 - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := passVerdict() - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionPass, Sdk: "afbe371003a570f088be4a394d83aa7a9458f7ab"} - if r := Report(v, cs, p, nil, nil, nil); !strings.Contains(r, "composition passed with afbe371003a5") { - t.Errorf("report = %q", r) - } - v.Composition = nil - if r := Report(v, cs, p, nil, nil, nil); !strings.Contains(r, "composition not yet run") || strings.Contains(r, "composition passed") { - t.Errorf("a verdict with no composition: %q", r) - } - }}, - {"the compositions time and machines are said once the assigner reports them", func(t *testing.T) { // 1300 (C10), the assigner's verdict (issue 1297) - cs, p := lineCase(t, pullOf(repoA, 7, head1)) - v := passVerdict() - v.Took.Composition = 2400 - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionPass, Sdk: "afbe371003a570f088be4a394d83aa7a9458f7ab", Machines: []string{"g14", "ace", "novox", "shanks"}} - r := Report(v, cs, p, nil, nil, nil) - for _, want := range []string{"composition passed in 2.4s with afbe371003a5 on 4 machines: ace, g14, novox, shanks"} { - if !strings.Contains(r, want) { - t.Errorf("report %q lacks %q", r, want) - } - } - if l := Line(v, cs, p, nil, nil, nil); !strings.Contains(l, "composition passed in 2.4s") || !strings.Contains(l, "on 4 machines") || utf8.RuneCountInString(l) > MaxLine { - t.Errorf("line = %q", l) - } - v.State, v.Failed = meshdeliverycheckerclient.VerdictFail, &FailedFirst{Machine: "novox", Words: "composition refused"} - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionFail, Words: "novox: two seats claim dns", Machines: []string{"novox"}} - if r := Report(v, cs, p, nil, nil, nil); !strings.Contains(r, "composition failed in 2.4s on novox: novox: two seats claim dns") { - t.Errorf("failed composition report = %q", r) - } - }}, - } { - t.Run(row.name, row.test) - } -} - -// 1887/D7-go, review round 2 item 5: the line says the unit level's gofmt and go vet time beside its tests'. -func TestTheLineSaysTheLintTime(t *testing.T) { - b := newBench(t) - test := b.test - b.test = func(c stepCall) (any, error) { - r, err := test(c) - if res, ok := r.(Tested); ok && res.Level == buildstep.LevelUnit { - res.Lint = 1.5 - return res, err - } - return r, err - } - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if out.Verdict.Took.Lint != 1500 { - t.Fatalf("took = %+v, want the lint's 1.5 s", out.Verdict.Took) - } - cs := caseOf(t, pullOf(repoA, 7, head1)) - pc := parts(out.Verdict, cs, cs.Pulls[0], out.Builds, out.Slowest, out.Judged) - if !strings.Contains(pc.times, "lint 1.5s") || !strings.Contains(pc.shortTimes, "lint 1.5s") { - t.Errorf("times = %q, short %q, want the lint's time in both forms of the line", pc.times, pc.shortTimes) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/lostask.go b/modules/mesh-delivery-checker/internal/check/lostask.go deleted file mode 100644 index 8672fd62..00000000 --- a/modules/mesh-delivery-checker/internal/check/lostask.go +++ /dev/null @@ -1,36 +0,0 @@ -package check - -import ( - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - sdkrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" -) - -// **An ask the build seat gave up on ends its step** (novox/hq issue 734): mesh-sdk buildrun.LostAskWaiter hands the -// waiting asker a failed step on the node buildrun.LostNode when DEAD_LETTERS keeps the ask's copy (buildrun.HearDeadLetters, -// granted by buscompose DeadLetterReads), and, under buildrun.NotTakenBound, when no build node took the ask within the -// bound. What follows is the checker's: a try ends as error (Tryer.run), with the not-taken bound at the step bound -// (Tryer.NotTaken); a check is asked again under its claim, at most LostAskTries times, then fails as not heard and is -// asked again when the part it could not hear runs (Checker.try). A check's ask has no not-taken bound: it waits in the stream as long as -// the backlog is (ADR 0525 §9), and a check ended for a backlog would owe its pull request a failure. - -// LostAskTries is how often a check asks again whose ask was given up, before it fails as not heard (UnheardWords) and is -// asked again when the part it could not hear runs. -const LostAskTries = 3 - -// IsLostAsk says whether a verdict failed because its ask judged nothing: the build seat gave it up (buildrun.LostWords), or -// the build module that ran its step was replaced under it, and its next process ended the step it left running -// (sdkrun.RestartReason; a delivery of the build module, as on 2026-10-28). Neither is the change's failure: the check -// is asked again, as for a given-up ask. -func IsLostAsk(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && (strings.Contains(failedOf(v).Words, buildrun.LostWords) || - strings.Contains(failedOf(v).Words, sdkrun.RestartReason)) -} - -// IsNotTaken says whether a verdict failed because no build node took its ask within the bound (buildrun.NotTakenWords). -func IsNotTaken(v meshdeliverychecker.VerdictsByGroupHeadsValue) bool { - return v.State == meshdeliverycheckerclient.VerdictFail && strings.Contains(failedOf(v).Words, buildrun.NotTakenWords) -} diff --git a/modules/mesh-delivery-checker/internal/check/lostask_test.go b/modules/mesh-delivery-checker/internal/check/lostask_test.go deleted file mode 100644 index d1073b8d..00000000 --- a/modules/mesh-delivery-checker/internal/check/lostask_test.go +++ /dev/null @@ -1,418 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "fmt" - "maps" - "slices" - "strings" - "sync" - "testing" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - sdkrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// losingAsks is the build seat over a bus that gives up the first `lose` asks put (DEAD_LETTERS keeps them, as on -// 2026-10-26 during a bus reload: try-1793017340003993788) and answers the rest; with never set it neither answers nor -// gives up (an ask no node takes). -type losingAsks struct { - *buildrun.FakeAsks - answer func(buildstep.StepAsk) buildstep.Step - dead *buildrun.DeadLetters - never bool - - mu sync.Mutex - lose int -} - -func (l *losingAsks) PublishAsk(ctx context.Context, subject, id string, data []byte) error { - if err := l.FakeAsks.PublishAsk(ctx, subject, id, data); err != nil { - return err - } - l.mu.Lock() - lost := l.lose > 0 - if lost { - l.lose-- - } - l.mu.Unlock() - switch { - case l.never: - case lost: - h := map[string][]string{buildrun.DeadHeaderMsgID: {id}, buildrun.DeadHeaderDeliveries: {"5"}, buildrun.DeadHeaderStream: {"SEAT_NODE_BUILD_GO"}, - buildrun.DeadHeaderGaveUp: {"2026-10-26T12:26:40Z"}} - g, ok := buildrun.GivenUpOf(h) - if !ok { - return nil - } - go l.dead.Heard(g) - default: - a, err := buildstep.DecodeStepAsk(data) - if err != nil { - return err - } - l.Write(l.answer(a)) - } - return nil -} - -func losing(b *bench, lose int) *losingAsks { - l := &losingAsks{FakeAsks: &buildrun.FakeAsks{}, answer: b.answer, dead: &buildrun.DeadLetters{}, lose: lose} - b.ex.Asks = buildrun.Asker{Seats: testSeats(), Publisher: b.asker(l, l.FakeAsks.Write), Waiter: buildrun.LostAskWaiter{Inner: l.FakeAsks, Dead: l.dead}, Bound: 5 * time.Second} - return l -} - -// endable is the tries state as the end tool sees it. -type endable struct{ kv *memKV } - -func (e endable) Entry(key string) (json.RawMessage, uint64, error) { - v, rev, _, err := e.kv.Get(context.Background(), key) - return v, rev, err -} -func (e endable) Update(key string, value []byte, revision uint64) error { - _, err := e.kv.Update(context.Background(), key, value, revision) - return err -} - -// Issue 734: a try whose ask the build seat gave up on ends as error saying so, and the next ask of its head is a new try; -// one no node takes ends at the step bound; one ended by hand frees its place and stops being waited for. -func TestAnAskGivenUpOrNeverTakenEndsItsTryAsErrorAndItsHeadIsAskedAnew(t *testing.T) { - ctx := context.Background() - for _, row := range []struct { - name string - lose int - never bool - // notTaken is the try's bound on an ask no node takes: short only where it is what ends the try, an hour where the - // dead letter must (a loaded machine let 200 ms pass before the dead letter was heard). - notTaken time.Duration - want string - }{ - {"given up by the build seat", 1, false, time.Hour, buildrun.LostWords + " after 5 deliveries at 2026-10-26T12:26:40Z"}, - {"taken by no build node within the bound", 0, true, 200 * time.Millisecond, buildrun.NotTakenWords + " within 200ms"}, - } { - t.Run(row.name, func(t *testing.T) { - kv := newMemKV() - at := time.Date(2026, 10, 26, 12, 22, 20, 0, time.UTC) - id, err := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, at) - if err != nil { - t.Fatal(err) - } - b := newBench(t) - l := losing(b, row.lose) - l.never = row.never - tr := &Tryer{Tries: kv, Judge: b.ex, NotTaken: row.notTaken} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - got := readTry(t, kv, id) - if got.State != TryError || len(got.Failed) != 1 || !strings.Contains(got.Failed[0], row.want) || !strings.Contains(got.Said, row.want) { - t.Fatalf("try = %+v, want error saying %q", got, row.want) - } - again, err := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, at.Add(time.Minute)) - if err != nil || again == id { - t.Errorf("the head asked again answered %q, %v, want a new try", again, err) - } - }) - } - t.Run("ended by hand", func(t *testing.T) { - kv := newMemKV() - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, time.Now()) - b := newBench(t) - losing(b, 0).never = true - tr := &Tryer{Tries: kv, Judge: b.ex} // no bound: only the hand ends it - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - deadline := time.Now().Add(10 * time.Second) - for readTry(t, kv, id).State != TryRunning && time.Now().Before(deadline) { - time.Sleep(5 * time.Millisecond) - } - if _, err := EndTry(endable{kv}, id, "", time.Now()); err == nil { - t.Error("an end without a why was taken") - } - ended, err := EndTry(endable{kv}, id, "its ask was lost in the bus reload", time.Now()) - if err != nil || ended.State != TryError || !strings.HasPrefix(ended.Said, "ended by hand ("+TryEndTool+"): its ask was lost") { - t.Fatalf("ended = %+v, %v", ended, err) - } - raw, _, _, _ = kv.Get(ctx, id) - tr.Put(ctx, id, raw) // the watch of the tries hands the end over - tr.Wait() // returns: the try's judging stopped - if got := readTry(t, kv, id); got.State != TryError || got.Said != ended.Said { - t.Errorf("try = %+v, want the hand's end kept", got) - } - if _, err := EndTry(endable{kv}, id, "again", time.Now()); err == nil || !strings.Contains(err.Error(), "ended already") { - t.Errorf("a second end answered %v", err) - } - }) -} - -// troubled is the build seat as a check meets it when its step's end does not reach its wait: the first `restarts` asks' -// steps are ended by the build module's next process (sdkrun.RestartReason), and the first `deaf` asks' watches hear -// nothing though their steps end (the lost check of 2026-10-28). Its steps state is read through AskSteps and Steps. -type troubled struct { - *buildrun.FakeAsks - answer func(buildstep.StepAsk) buildstep.Step - - mu sync.Mutex - restarts, deaf int - deafIDs map[string]bool - steps map[string]buildstep.Step -} - -func (tr *troubled) PublishAsk(ctx context.Context, subject, id string, data []byte) error { - if err := tr.FakeAsks.PublishAsk(ctx, subject, id, data); err != nil { - return err - } - a, err := buildstep.DecodeStepAsk(data) - if err != nil { - return err - } - step := tr.answer(a) - read, _ := buildstep.AskAs[buildstep.Ask](a) - step.What = sdkrun.AskWhat(read) - tr.mu.Lock() - if tr.restarts > 0 { - tr.restarts-- - step.State, step.Reason, step.Result = buildstep.StepFailed, sdkrun.RestartReason, nil - } - deaf := tr.deaf > 0 - if deaf { - tr.deaf-- - tr.deafIDs[id] = true - } - tr.steps[id] = step - tr.mu.Unlock() - if !deaf { - tr.Write(step) - } - return nil -} - -func (tr *troubled) WatchAsk(ctx context.Context, module, asker, askID string) (<-chan buildstep.Step, error) { - tr.mu.Lock() - deaf := tr.deafIDs[askID] - tr.mu.Unlock() - if deaf { - return make(chan buildstep.Step), nil // open, and silent for good - } - return tr.FakeAsks.WatchAsk(ctx, module, asker, askID) -} - -func (tr *troubled) AskSteps(_ context.Context, _, _, askID string) ([]buildstep.Step, error) { - tr.mu.Lock() - defer tr.mu.Unlock() - if s, ok := tr.steps[askID]; ok { - return []buildstep.Step{s}, nil - } - return nil, nil -} - -// Steps is the steps state as the read tools read it, with running in place of every step the hand has not let end. -func (tr *troubled) Steps(running bool) StateRead { - tr.mu.Lock() - defer tr.mu.Unlock() - vals := map[string]json.RawMessage{} - for id, s := range tr.steps { - if running { - s.State, s.Ended, s.Started = buildstep.StepRunning, time.Time{}, time.Now() - } - raw, _ := buildstep.EncodeStep(s) - vals[s.Node+"."+id] = raw - } - return StateRead{Keys: func() ([]string, error) { - var out []string - for k := range vals { - out = append(out, k) - } - return out, nil - }, Get: func(k string) (json.RawMessage, error) { return vals[k], nil }} -} - -// kvRead is a bucket in memory as the read tools read it, live. -func kvRead(kv *memKV) StateRead { - return StateRead{Keys: func() ([]string, error) { return kv.Keys(context.Background()) }, - Get: func(k string) (json.RawMessage, error) { - v, _, _, err := kv.Get(context.Background(), k) - return v, err - }} -} - -// asVerdicts is the rig's verdicts as the ask-again tool reads and writes them. -type asVerdicts struct{ kv *memKV } - -func (a asVerdicts) Entry(key string) (json.RawMessage, uint64, error) { - v, rev, _, err := a.kv.Get(context.Background(), key) - return v, rev, err -} -func (a asVerdicts) Update(key string, value []byte, revision uint64) error { - _, err := a.kv.Update(context.Background(), key, value, revision) - return err -} - -// Issue 734 and the lost check of 2026-10-28: a check whose ask judged nothing is asked again, and one whose step ended -// unheard is judged. Given up by the build seat once, or its step ended by the build module's next process after a -// delivery replaced it, the check is asked again under its claim and passes; given up every time it fails loudly as not -// heard, could not run, asked again when its part runs. A step whose watch went deaf is read at the next step end of its build module -// (a nudge) and judged with no new ask; one no event reaches is named lost by checker_status, refused by checker_ask_again -// while its step still runs, and asked again by it once its step ended. -func TestACheckWhoseAskJudgedNothingIsAskedAgainOrRead(t *testing.T) { - ctx := context.Background() - for _, row := range []struct { - name string - lose, restarts, deaf int - how string // "nudge": a step of the build module ends; "hand": checker_ask_again - state string - asks int - }{ - {name: "given up once", lose: 1, state: meshdeliverycheckerclient.VerdictPass, asks: 2}, - {name: "given up every time", lose: LostAskTries, state: meshdeliverycheckerclient.VerdictFail, asks: LostAskTries}, - {name: "its build module replaced under it", restarts: 1, state: meshdeliverycheckerclient.VerdictPass, asks: 2}, - {name: "replaced under every ask", restarts: LostAskTries, state: meshdeliverycheckerclient.VerdictFail, asks: LostAskTries}, - {name: "its watch deaf, a step of its build module ends", deaf: 1, how: "nudge", state: meshdeliverycheckerclient.VerdictPass, asks: 1}, - {name: "its watch deaf, asked again by hand", deaf: 1, how: "hand", state: meshdeliverycheckerclient.VerdictPass, asks: 2}, - {name: "its watch deaf, asked again by hand after its build module replaced it", deaf: 1, restarts: 1, how: "hand", state: meshdeliverycheckerclient.VerdictPass, asks: 2}, - } { - t.Run(row.name, func(t *testing.T) { - b := newBench(t) - var asked func() int - var heard *buildrun.HeardEnds - var tr *troubled - if row.lose > 0 { - l := losing(b, row.lose) - asked = func() int { return len(l.Asks()) } - } else { - tr = &troubled{FakeAsks: &buildrun.FakeAsks{}, answer: b.answer, restarts: row.restarts, deaf: row.deaf, - deafIDs: map[string]bool{}, steps: map[string]buildstep.Step{}} - heard = &buildrun.HeardEnds{Inner: tr, Read: tr} - b.ex.Asks = buildrun.Asker{Seats: testSeats(), Publisher: b.asker(tr, tr.Write), Waiter: heard, Bound: 5 * time.Second} - asked = func() int { return len(tr.Asks()) } - } - r := newRig(t, b.ex) - asksKept := newMemKV() - r.ch.Asks = asksKept - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - key := keyOf(t, "feat/x", cand) - r.ch.Candidate(ctx, "feat%2Fx", cand) - if row.deaf > 0 { - for deadline := time.Now().Add(10 * time.Second); heard.Waiting() == 0 && time.Now().Before(deadline); { - time.Sleep(2 * time.Millisecond) - } - if v := r.verdict(t, key); v.State != meshdeliverycheckerclient.VerdictRunning { - t.Fatalf("a check whose step ended unheard = %s, want running until it is read", v.State) - } - } - switch row.how { - case "nudge": - heard.Nudge(nodeBuildModule) // another build module's step: not this one's to read - heard.Nudge(goBuildModule) - case "hand": - cands := newMemKV() - ckey, _ := meshcatalogclient.CandidatesKey("feat/x") - _, _ = cands.Create(ctx, ckey, cand) - none := StateRead{Keys: func() ([]string, error) { return nil, nil }, Get: func(string) (json.RawMessage, error) { return nil, nil }} - steps := map[string]StateRead{goBuildModule: none} - rd := Reader{CandidateState: kvRead(cands), VerdictState: kvRead(r.verdicts), AskState: kvRead(asksKept), - StepStates: func() map[string]StateRead { return steps }} // no node took its ask yet - if _, err := rd.AskAgain(asVerdicts{r.verdicts}, "feat/x", "its watch went deaf"); err == nil || !strings.Contains(err.Error(), "waits in its build seat's stream") { - t.Errorf("asked again while its ask waits in the stream (issue 1791's kind): %v", err) - } - steps[goBuildModule] = tr.Steps(true) - if _, err := rd.AskAgain(asVerdicts{r.verdicts}, "feat/x", "its watch went deaf"); err == nil || !strings.Contains(err.Error(), "a step of it runs") { - t.Errorf("asked again while its step runs: %v", err) - } - steps[goBuildModule] = tr.Steps(false) - if st, _ := rd.Status(); !strings.Contains(st.Lost["feat/x"], AskAgainTool) { - t.Errorf("status lost = %v, want feat/x named with %s", st.Lost, AskAgainTool) - } - // Issue 1373: a check that names no ask is asked again only when no check ask waits or is held in any build seat's stream. - { - raw, _, _, _ := r.verdicts.Get(ctx, key) - vcopy := newMemKV() - _, _ = vcopy.Create(ctx, key, raw) - noAsk := rd - noAsk.AskState = kvRead(newMemKV()) - for _, c := range []struct { - name string - rows []BacklogRow - bad string - }{{"a check ask waits", []BacklogRow{{Language: "go", Kind: "check", Waiting: 1}}, "holds asks"}, - {"a check ask is held", []BacklogRow{{Language: "go", Kind: "check", Held: 1}}, "holds asks"}, - {"a recheck ask waits", []BacklogRow{{Language: "go", Kind: "recheck", Waiting: 1}}, "holds asks"}, - {"a recheck ask is held", []BacklogRow{{Language: "go", Kind: "recheck", Held: 1}}, "holds asks"}, - {"a stream cannot be read", []BacklogRow{{Language: "go", Kind: "check", Error: "no worker"}}, "cannot be read"}, - {"only a delivery ask waits", []BacklogRow{{Language: "go", Kind: "delivery", Waiting: 3}}, ""}} { - noAsk.Backlog = func() ([]BacklogRow, error) { return c.rows, nil } - w := VerdictEntry(asVerdicts{r.verdicts}) - if c.bad == "" { - w = asVerdicts{vcopy} - } - got, err := noAsk.AskAgain(w, "feat/x", "no ask") - if (c.bad == "") != (err == nil) || (c.bad != "" && !strings.Contains(err.Error(), c.bad)) || (c.bad == "" && !strings.HasPrefix(got.Said, AskedAgainWords)) { - t.Errorf("a check naming no ask, %s: %+v, %v", c.name, got, err) - } - } - } - got, err := rd.AskAgain(asVerdicts{r.verdicts}, "feat/x", "its watch went deaf") - if err != nil || got.Key != key || !strings.HasPrefix(got.Said, AskedAgainWords) { - t.Fatalf("asked again = %+v, %v", got, err) - } - raw, _, _, _ := r.verdicts.Get(ctx, key) - r.ch.VerdictWritten(ctx, key, raw) // the checker's watch of its verdicts hands it over - } - r.ch.Wait() - v := r.verdict(t, key) - if v.State != row.state || asked() != row.asks { - t.Errorf("verdict %s (%s) after %d asks, want %s after %d", v.State, v.Failed.Words, asked(), row.state, row.asks) - } - if _, _, kept, _ := asksKept.Get(ctx, key); kept { - t.Errorf("the asks of the ended check are still kept under %s", key) - } - if row.state == meshdeliverycheckerclient.VerdictFail && (!IsLostAsk(v) || !IsCouldNotRun(v)) { - t.Errorf("a check that judged nothing every time = %q, want lost and could not run", v.Failed.Words) - } - if row.how == "hand" { - rd := Reader{VerdictState: kvRead(r.verdicts), CandidateState: kvRead(newMemKV())} - if _, err := rd.AskAgain(asVerdicts{r.verdicts}, "feat/x", "again"); err == nil { - t.Error("a judged check was asked again") - } - // ADR 0664 (issue 1748, operator answer 5): a failed verdict whose failing steps all ended on a transport fault - // is asked again; one with a failing step that did not, or with none read, stands. - for _, c := range []struct { - name string - passing []bool - ok bool - }{{"every failing step on a transport fault", []bool{true, true}, true}, {"one step failed on the source", []bool{true, false}, false}, {"no failed step read", nil, false}} { - failedKV := newMemKV() - judgedRaw, _, _, _ := r.verdicts.Get(ctx, key) - failed, _ := meshdeliverycheckerclient.DecodeVerdict(judgedRaw) - failed.State, failed.Failed = meshdeliverycheckerclient.VerdictFail, &FailedFirst{Machine: "ace", Words: "a transport fault on 3 runs"} - raw, _ := meshdeliverycheckerclient.EncodeVerdict(failed) - failedKV.Create(ctx, key, raw) - vals := map[string]json.RawMessage{} - for i, p := range c.passing { - id := fmt.Sprint("mvl0frv18d19", 80+i) - vals["ace."+id], _ = buildstep.EncodeStep(buildstep.Step{ID: id, Node: "ace", Tool: "check ask", State: buildstep.StepFailed, Passing: p, Ended: time.Now(), What: "check " + repoA + "@" + short12(head1)}) - } - rd := Reader{CandidateState: kvRead(cands2(t, cand)), VerdictState: kvRead(failedKV), AskState: kvRead(newMemKV()), StepStates: stepStatesOf(map[string]StateRead{goBuildModule: { - Keys: func() ([]string, error) { return slices.Collect(maps.Keys(vals)), nil }, Get: func(k string) (json.RawMessage, error) { return vals[k], nil }}})} - if got, err := rd.AskAgain(asVerdicts{failedKV}, "feat/x", "the network"); (err == nil) != c.ok || (c.ok && !strings.HasPrefix(got.Said, AskedAgainWords)) { - t.Errorf("%s: asked again = %+v, %v, want asked again %v", c.name, got, err, c.ok) - } - } - } - }) - } -} - -// cands2 is a candidates state holding cand under feat/x. -func cands2(t *testing.T, cand []byte) *memKV { - t.Helper() - kv := newMemKV() - ckey, _ := meshcatalogclient.CandidatesKey("feat/x") - _, _ = kv.Create(context.Background(), ckey, cand) - return kv -} diff --git a/modules/mesh-delivery-checker/internal/check/manifests_judge.go b/modules/mesh-delivery-checker/internal/check/manifests_judge.go deleted file mode 100644 index f665de95..00000000 --- a/modules/mesh-delivery-checker/internal/check/manifests_judge.go +++ /dev/null @@ -1,401 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "maps" - "mesh-delivery-checker/rules" - "slices" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" - "git.novox.be/novox/mesh-sdk/go/catalogue" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/packagename" -) - -// A change's manifests are judged by the indexer's judge (novox/hq ADR 0578 §2): the checker asks the Go build seat to -// build the judge's command (mesh-delivery's modules/mesh-delivery-indexer, cmd/mesh-judge) at mesh-delivery's trunk, or -// at the group's mesh-delivery member head when that member changes the judge, and to run it in its sandbox over the -// catalogue as the change leaves it, exactly as the composer is run (buildstep.ComposeJob, ADR 0482 §4). A manifest the -// change touches that the judge refuses fails the check, naming the manifest and each problem. The checker links no -// judge of its own. - -// The judge the checker runs. -const ( - DeliveryRepository = "novox/mesh-delivery" - JudgeDir = "modules/mesh-delivery-indexer" - JudgePackage = "mesh-delivery-indexer/cmd/mesh-judge" - // JudgeCase is the case of a judge request holding the catalogue as the change leaves it, and JudgeTrunk the one - // holding it as the trunk holds it: a problem the change case has and the trunk case lacks is the change's (ADR 0579 §2). - JudgeCase = "change" - JudgeTrunk = "trunk" - // judgeBetween is how the judge names the problems between manifests that no one module's removal ends. - judgeBetween = rules.BetweenManifests -) - -// judgePaths are the paths of mesh-delivery whose change changes the judge: a group whose mesh-delivery member changes -// one is judged by that member's head. -var judgePaths = []string{JudgeDir + "/judge/", JudgeDir + "/cmd/mesh-judge/", JudgeDir + "/go.mod", JudgeDir + "/go.sum"} - -// changesJudge says whether a merge of mesh-delivery with these changed paths changes the judge. -func changesJudge(repository string, merged []Fetched) bool { - if repository != DeliveryRepository { - return false - } - for _, f := range merged { - for _, p := range f.Changed { - for _, j := range judgePaths { - if p == j || strings.HasPrefix(p, j) { - return true - } - } - } - } - return false -} - -// TrunkManifests says the manifest digest of every module the indexer holds (its modules state), by module. -type TrunkManifests interface { - ManifestDigests() map[string]string - // ManifestFingerprints is the fingerprint of the entry of every such module in the trunk revision, by module, whose - // catalog graph entry holds that manifest (design 47 G A-310). - ManifestFingerprints() map[string]string -} - -// SeatList gives the seat definitions the manifests are judged against, whole, as raw definitions (a request's Seats): the -// list the seat list reader holds. It is an error when the list has not been read from the seat yet, since a manifest judged -// against the bootstrapped seats alone would be refused for a seat it names that the mesh defines (novox/hq ADR 0636 §1). -type SeatList interface { - Definitions() ([]json.RawMessage, error) -} - -// PredefinedList is the catalogue's list of predefined keys the indexer keeps (its state predefined, key list), the one a -// config value falling back to a key is judged against (ADR 0348 §7); nil when it keeps none. -type PredefinedList interface { - PredefinedList(ctx context.Context) (json.RawMessage, error) -} - -// judgeRequest is the judge's request over the catalogue as the case leaves it: every trunk manifest by its digest, each -// one a pull request touches replaced by the digest its candidate names; touched are the modules whose manifest the -// change adds or changes (a digest the trunk does not hold for it), sorted (novox/hq ADR 0579 §2). -func judgeRequest(trunk, trunkFps map[string]string, pulls []Pull) (req composer.Request, touched []string) { - manifests, before := map[string]composer.Manifest{}, map[string]composer.Manifest{} - for module, digest := range trunk { - if digest != "" { - manifests[module] = composer.Manifest{Digest: digest, Fingerprint: trunkFps[module]} - before[module] = composer.Manifest{Digest: digest, Fingerprint: trunkFps[module]} - } - } - seen := map[string]bool{} - for _, p := range pulls { - for module, cm := range p.Fps { - if cm.Manifest == "" { - continue - } - manifests[module] = composer.Manifest{Digest: cm.Manifest, Fingerprint: cm.Fp2} - if trunk[module] != cm.Manifest { - seen[module] = true - } - } - } - return composer.Request{V: composer.Version, Cases: []composer.Case{{Name: JudgeCase, Manifests: manifests}, - {Name: JudgeTrunk, Manifests: before}}}, slices.Sorted(maps.Keys(seen)) -} - -// judgeManifests runs the indexer's judge over the case's manifests and records its answers as a fact the rules judge -// (rules.JudgeManifests), with the trunk validator's answer on a new indexer (ADR 0633 clause 6). A case that touches no -// manifest, or an examiner that reads no trunk manifests, judges nothing; a judge that cannot be asked or does not answer -// is a failure, said, never a pass. -func (st *state) judgeManifests(ctx context.Context) { - if st.j.Manifests == nil || st.narrowTry() { - return - } - req, touched := judgeRequest(st.j.Manifests.ManifestDigests(), st.j.Manifests.ManifestFingerprints(), st.c.Pulls) - if len(touched) == 0 { - return - } - commit, at := st.judgeHead, "the group's mesh-delivery head "+st.judgeHead - if commit == "" { - if st.j.Trunks != nil { - commit = st.j.Trunks.Heads()[DeliveryRepository] - } - at = "mesh-delivery's trunk " + commit - } - if commit == "" { - st.fail(st.goModule(), "the change's manifests cannot be judged: the trunk head of "+DeliveryRepository+" is not known") - return - } - // The list of predefined keys the manifests are judged against (ADR 0578 §2): the catalogue member's predefined.json - // when the change alters it, else the one the indexer keeps. - if list, from := st.predefinedList(ctx); list != nil { - req.Predefined = list - st.c.note("the manifests are judged against the predefined keys of " + from) - } - req.Packages = st.knownPackages(ctx) - // The heads' seat list, for the change case and the trunk case both (ADR 0636 §4). - req.Seats = st.c.Seats - cv, trunkCase, ok := st.runJudge(ctx, req, touched, commit, at, st.packagesOf(DeliveryRepository)) - if !ok { - return - } - st.judgedVerdicts(cv, at) - m := rules.Manifests{Touched: touched, At: at, Change: cv, Trunk: trunkCase} - if !st.indexerOf(ctx, req, touched, cv, at, &m) { - return - } - st.facts = append(st.facts, rules.Step{Module: st.goModule(), Manifests: &m}) -} - -// runJudge asks the Go build seat to build the judge at the commit and run it over the request, and reads its change and -// trunk cases; false, the failure recorded, when it cannot be asked or does not answer. -func (st *state) runJudge(ctx context.Context, req composer.Request, touched []string, commit, at string, versions buildstep.PackageVersions) (change, trunk composer.CaseVerdict, ok bool) { - raw, err := json.Marshal(req) - if err != nil { - st.fail(st.goModule(), err.Error()) - return change, trunk, false - } - job := buildstep.ComposeJob{Fetch: buildstep.Fetch{Repository: DeliveryRepository, Commit: commit, Dir: JudgeDir, Packages: versions}, Dir: JudgeDir, - Package: JudgePackage, Request: raw, Reader: "the indexer's judge at " + at} - a := buildstep.StepAsk{ID: buildstep.NewID(), Kind: buildstep.AskCheck, Language: buildstep.AskGo, Asker: st.j.Module, - At: st.j.now().UTC(), Compose: &job} - st.c.Progress.Set(fmt.Sprintf("the judge ask %s of the manifests of %s", a.ID, strings.Join(touched, ", ")), "") - st.c.Asked(a.ID) - if err := st.j.Asks.Put(ctx, a); err != nil { - st.fail(st.goModule(), "the judge of the change's manifests could not be asked: "+err.Error()) - return change, trunk, false - } - step, err := st.j.Asks.Wait(ctx, a) - if err != nil { - st.fail(st.goModule(), "the judge of the change's manifests: "+err.Error()) - return change, trunk, false - } - change, trunk, why := judgeAnswer(step) - if why != "" { - st.fail(st.goModule(), fmt.Sprintf("the indexer's judge at %s did not judge the change's manifests: %s", at, why)) - return change, trunk, false - } - return change, trunk, true -} - -// CatalogSeat is the seat the indexer holds (rules.CatalogSeat). -const CatalogSeat = rules.CatalogSeat - -// indexerOf finds the touched module whose manifest claims the seat mesh-catalog, the indexer, and puts in m the trunk's -// manifest validator's change case on it, the one the running parts hold (ADR 0633 clause 6): when the change judges -// with its own head's validator, or at other package versions than the latest published, the trunk's is asked as well. -// False, the failure recorded, when a manifest cannot be read or the trunk's validator cannot be asked. -func (st *state) indexerOf(ctx context.Context, req composer.Request, touched []string, cv composer.CaseVerdict, judgedAt string, m *rules.Manifests) bool { - if st.j.Read == nil { - return true - } - var indexer string - for _, module := range touched { - man, err := st.j.Read.Manifest(ctx, req.Cases[0].Manifests[module].Digest) - if err != nil { - st.fail(st.goModule(), fmt.Sprintf("the manifest of %s cannot be read, so the indexer cannot be ruled out: %v", module, err)) - return false - } - for _, c := range man.Claims { - if c.Name == CatalogSeat { - indexer = module - } - } - } - if indexer == "" { - return true - } - // The validator the running parts hold is the trunk's, built at the latest published versions alone, never at the group's - // heads (ADR 0633 clause 6): a group's mesh-sdk or mesh-contracts head must not change the validator that judges the new - // indexer. The head's run is reused when it was that validator already. - if trunkVersions := st.trunkJudgeVersions(); st.judgeHead != "" || !maps.Equal(st.packagesOf(DeliveryRepository), trunkVersions) { - trunk := "" - if st.j.Trunks != nil { - trunk = st.j.Trunks.Heads()[DeliveryRepository] - } - if trunk == "" { - st.fail(st.goModule(), "the new indexer cannot be judged by the trunk's manifest validator: the trunk head of "+DeliveryRepository+" is not known") - return false - } - judgedAt = "mesh-delivery's trunk " + trunk - var ok bool - if cv, _, ok = st.runJudge(ctx, req, touched, trunk, judgedAt, trunkVersions); !ok { - return false - } - } - m.Indexer, m.IndexerAt, m.OfIndexer = indexer, judgedAt, &cv - return true -} - -// judgeAnswer reads the judge's change and trunk cases from its step, or why it cannot. -func judgeAnswer(step buildstep.Step) (change, trunk composer.CaseVerdict, why string) { - if step.State != buildstep.StepPassed { - return change, trunk, fmt.Sprintf("its step %s: %s", step.State, step.Reason) - } - var a buildstep.ComposeAnswer - if err := json.Unmarshal(step.Result, &a); err != nil { - return change, trunk, "its step's answer cannot be read: " + err.Error() - } - var v composer.Verdict - if err := json.Unmarshal(a.Verdict, &v); err != nil || v.V != composer.Version { - return change, trunk, fmt.Sprintf("its verdict cannot be read (version %d): %v", v.V, err) - } - found := 0 - for _, c := range v.Cases { - switch c.Name { - case JudgeCase: - change, found = c, found+1 - case JudgeTrunk: - trunk, found = c, found+1 - } - } - if found != 2 { - return change, trunk, "its verdict does not hold both the cases " + JudgeCase + " and " + JudgeTrunk - } - return change, trunk, "" -} - -// predefinedList is the list of predefined keys a case's manifests are judged against, and where it comes from: the -// catalogue member's predefined.json at its head when its merge changes it, else the one the indexer keeps; nil when -// neither can be read, said, and the judge then takes each fallback as its value declares it. -func (st *state) predefinedList(ctx context.Context) (catalogue.PredefinedList, string) { - read := func(raw []byte, from string) (catalogue.PredefinedList, string) { - list, err := catalogue.ParsePredefined(raw) - if err != nil { - st.c.note("the predefined keys of " + from + " cannot be read, so fallbacks are judged as their values declare them: " + err.Error()) - return nil, "" - } - return list, from - } - if st.predefinedHead != "" && st.j.Forge != nil { - text, err := st.j.Forge.File(ctx, catalogueRepository, catalogue.PredefinedFile, st.predefinedHead) - if err == nil { - return read([]byte(text), "the catalogue member's head "+st.predefinedHead) - } - st.c.note("the catalogue member's predefined.json cannot be read: " + err.Error()) - } - if st.j.Predefined == nil { - return nil, "" - } - raw, err := st.j.Predefined.PredefinedList(ctx) - if err != nil || raw == nil { - if err != nil { - st.c.note("the indexer's predefined keys cannot be read: " + err.Error()) - } - return nil, "" - } - return read(raw, "the indexer's kept list") -} - -// judgedByMember notes the group's mesh-delivery member head as the judge's when its merge changes the judge, and the -// catalogue member's head as where the predefined keys are read when its merge changes predefined.json. -func (st *state) judgedByMember(p Pull, merged []Fetched) { - if changesJudge(p.Repository, merged) { - st.judgeHead = p.Head - } - if p.Repository == catalogueRepository { - for _, f := range merged { - if slices.Contains(f.Changed, catalogue.PredefinedFile) { - st.predefinedHead = p.Head - } - } - } -} - -// knownPackages are the packages a manifest's `packages` may name, sorted (DLV-I14, design 32): every name the indexer's -// packages state holds, and, of each package a member of the case declares that the state does not know by its -// `/`, the name read at that member's head through the forge (packagename.Of, the indexer's own reader -// at a merge). A name that cannot be read is said and left out, so a manifest naming it is refused naming it. Nil, which -// judges no name, when the checker follows no packages state or has not read it yet (PackageNames nil); an answered -// state that holds no name is an empty list, never nil, so every name is judged against it. -func (st *state) knownPackages(ctx context.Context) []string { - if st.j.KnownPackages == nil { - return nil - } - names := st.j.KnownPackages.PackageNames() - if names == nil { - return nil - } - built := map[string]bool{} - for _, b := range names { - built[b] = true - } - known := make([]string, 0, len(names)) - known = append(known, slices.Collect(maps.Keys(names))...) - declared, err := st.declaredPackages(ctx) - if err != nil { - st.c.note("the packages the group's members declare are not known, so only the packages state's names are known to the judge: " + err.Error()) - } - for _, d := range declared { - if built[d.module+"/"+d.name] { - continue - } - if st.j.Forge == nil { - st.c.note(fmt.Sprintf("the name of the package %s/%s new in the group is not read: this checker reads no forge", d.module, d.name)) - continue - } - read := func(ctx context.Context, repository, commit, file string) ([]byte, error) { - text, err := st.j.Forge.File(ctx, repository, file, commit) - if errors.Is(err, ErrNotFound) { - return nil, nil - } - return []byte(text), err - } - dirs := []string{d.dir} - if d.dir == "" { - dirs = []string{"modules/" + d.module, meshcatalogclient.RootPath} - } - var why []string - for _, dir := range dirs { - a := catalogue.Artifact{Name: d.name, Kind: catalogue.ArtifactPackage, Language: d.language, Paths: d.paths} - name, err := packagename.Of(ctx, read, d.pull.Repository, d.pull.Head, dir, a) - if err == nil { - known, why = append(known, name), nil - break - } - why = append(why, err.Error()) - } - if len(why) > 0 { - st.c.note(fmt.Sprintf("the name of the package %s/%s new in the group is not read at the head of %s: %s", d.module, d.name, pullKey(d.pull), strings.Join(why, "; "))) - } - } - slices.Sort(known) - return slices.Compact(known) -} - -// trunkJudgeVersions are the versions the trunk's manifest validator is built at: the latest published version of each -// package, with no group member's head (ADR 0633 clause 6). -func (st *state) trunkJudgeVersions() buildstep.PackageVersions { - return packagesFor(st.j.Published, &Case{}, DeliveryRepository) -} - -// judgedVerdicts puts the check's own judge's verdict on each manifest the change brings on the case, as the seat -// mesh-catalog's verdict value, for the composer's request (ADR 0578 §2, §3; F5 of milestone 47/G): accepted, or refused -// with its words; a manifest the judge gave no verdict on carries none, which the composer reads as accepted. -func (st *state) judgedVerdicts(cv composer.CaseVerdict, at string) { - for _, p := range st.c.Pulls { - for name, fp := range p.Fps { - read, ok := cv.Manifests[name] - if fp.Manifest == "" || !ok || (!read.Accepted && read.Refused == "") { - continue - } - v := meshcatalog.VerdictsByDigestValue{Module: name, State: meshcatalogclient.VerdictAccepted, Judge: at} - if !read.Accepted { - v.State, v.Problems = meshcatalogclient.VerdictRefused, []string{read.Refused} - } - raw, err := json.Marshal(v) - if err != nil { - continue - } - if st.c.Judged == nil { - st.c.Judged = map[string]json.RawMessage{} - } - st.c.Judged[name] = raw - } - } -} diff --git a/modules/mesh-delivery-checker/internal/check/manifests_judge_test.go b/modules/mesh-delivery-checker/internal/check/manifests_judge_test.go deleted file mode 100644 index edf3ae6a..00000000 --- a/modules/mesh-delivery-checker/internal/check/manifests_judge_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package check - -import ( - "reflect" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" -) - -// Design 47 G A-310: a manifest the judge's request names by its digest names the fingerprint whose catalog graph entry holds -// it: a pull's own fingerprint for a module the pull changes, the trunk revision's entry for the rest. -func TestTheJudgeRequestNamesEachManifestsFingerprint(t *testing.T) { - pull := Pull{Fps: map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"changed": {Manifest: "d2", Fp2: "src2:pull"}}} - req, _ := judgeRequest(map[string]string{"changed": "d1", "same": "d3"}, map[string]string{"changed": "src2:trunk", "same": "src2:same"}, []Pull{pull}) - got, trunk := req.Cases[0].Manifests, req.Cases[1].Manifests - if !reflect.DeepEqual(got["changed"], composer.Manifest{Digest: "d2", Fingerprint: "src2:pull"}) || !reflect.DeepEqual(got["same"], composer.Manifest{Digest: "d3", Fingerprint: "src2:same"}) { - t.Errorf("the change's manifests = %+v", got) - } - if !reflect.DeepEqual(trunk["changed"], composer.Manifest{Digest: "d1", Fingerprint: "src2:trunk"}) { - t.Errorf("the trunk's manifest = %+v, want the trunk entry's fingerprint", trunk["changed"]) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/names.go b/modules/mesh-delivery-checker/internal/check/names.go deleted file mode 100644 index 18c199f8..00000000 --- a/modules/mesh-delivery-checker/internal/check/names.go +++ /dev/null @@ -1,83 +0,0 @@ -package check - -import ( - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// The holders and buckets the checker names. No contract names a holder (novox/hq ADR 0625 §4, ADR 0634 §1), so these are its -// own constants, each a state of a seat (the seat's state name beside the holder's module name) and each declared in its -// manifest (module.json: its own states, its `reads`); a unit row checks them against the manifest (TestTheNamesAreTheManifests). -const ( - // GateModule holds the seat mesh-delivery's states source and turns, which the checker reads (reads: mesh-delivery-gate.source, - // mesh-delivery-gate.turns). - GateModule = "mesh-delivery-gate" - - // IndexerModule holds the seat mesh-catalog's states, which the checker follows by their views (consumes: - // mesh-delivery-indexer.views.*). - IndexerModule = "mesh-delivery-indexer" - CandidatesBucket = IndexerModule + "_" + meshcatalog.StateCandidates - ModulesBucket = IndexerModule + "_" + meshcatalog.StateModules - ManifestsBucket = IndexerModule + "_" + meshcatalog.StateManifests - IndexerPredefinedState = meshcatalog.StatePredefined - - // WatchmanModule holds the seat mesh-watchdogs' presence (reads: mesh-delivery-watchman.presence). - WatchmanModule = "mesh-delivery-watchman" - PresenceBucket = WatchmanModule + "_" + meshwatchdogs.StatePresence - - // HaveBucket is the node-engine's state have (reads: node-engine.have); a node's own key ends in HaveNodeWord. - HaveBucket = "node-engine_" + nodeengine.StateHave - HaveNodeWord = "node" - - // compositionWaits is the state of the assigner's composition answer that waits for what it composes from. - compositionWaits = "waits" -) - -// FailedFirst names what failed: a test (its package and name) or a machine, with the words of the failure. It is the shape of -// the verdict's `failed` and of each entry of its `failures` (the seat's two types of it have the same fields), so the check -// judges with one and converts at the verdict's edge (failureItems, failedList). -type FailedFirst = checker.VerdictsByGroupHeadsValueFailed - -// failedOf is the first failure a verdict names: the zero value of a verdict that names none. -func failedOf(v checker.VerdictsByGroupHeadsValue) FailedFirst { - if v.Failed == nil { - return FailedFirst{} - } - return *v.Failed -} - -// tookOf is the time of each part of a check a verdict names: the zero value of a verdict that names none. -func tookOf(v checker.VerdictsByGroupHeadsValue) checker.VerdictsByGroupHeadsValueTook { - if v.Took == nil { - return checker.VerdictsByGroupHeadsValueTook{} - } - return *v.Took -} - -func failureItems(list []FailedFirst) []checker.VerdictsByGroupHeadsValueFailuresItem { - if list == nil { - return nil - } - out := make([]checker.VerdictsByGroupHeadsValueFailuresItem, len(list)) - for i, f := range list { - out[i] = checker.VerdictsByGroupHeadsValueFailuresItem(f) - } - return out -} - -func failedList(items []checker.VerdictsByGroupHeadsValueFailuresItem) []FailedFirst { - if items == nil { - return nil - } - out := make([]FailedFirst, len(items)) - for i, f := range items { - out[i] = FailedFirst(f) - } - return out -} - -// Bucket is the bucket of one of the checker's own states, `_`, module its credential's, never a constant -// (design 47 G A-296, §428). -func Bucket(module, state string) string { return module + "_" + state } diff --git a/modules/mesh-delivery-checker/internal/check/owed.go b/modules/mesh-delivery-checker/internal/check/owed.go deleted file mode 100644 index 4de3ad97..00000000 --- a/modules/mesh-delivery-checker/internal/check/owed.go +++ /dev/null @@ -1,49 +0,0 @@ -package check - -import ( - "fmt" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// The checker's owed state holds the one act it owes, the status mesh/check on a pull request at a head: the gate's owed act -// with the context mesh/check. Its value has the shape of the gate's (the seat mesh-delivery's owed), so its rules and its -// codec are the mesh-delivery client's; only the checker reads it, so the checker's own narrowing to its one act lives here. - -// CheckerOwedAct is the act the checker owes. -const CheckerOwedAct = meshdeliveryclient.OwedStatus + "=" + meshdeliveryclient.StatusContextCheck - -// CheckerOwedKey is the key, in the checker's owed state, of the status the checker owes on a pull request at a head. -func CheckerOwedKey(repository string, number int, head string) (string, error) { - return meshdeliveryclient.OwedKey(repository, number, head, CheckerOwedAct) -} - -func checkCheckerOwed(o meshdeliverychecker.OwedByRepositoryNumberHeadActValue) error { - if o.Act != meshdeliveryclient.OwedStatus || o.Context != meshdeliveryclient.StatusContextCheck { - return fmt.Errorf("the checker owes only a status with the context %s, not the act %q with the context %q", meshdeliveryclient.StatusContextCheck, o.Act, o.Context) - } - return nil -} - -// EncodeCheckerOwed is o as the gate's owed value, refused for any act but the checker's status. -func EncodeCheckerOwed(o meshdeliverychecker.OwedByRepositoryNumberHeadActValue) ([]byte, error) { - if err := checkCheckerOwed(o); err != nil { - return nil, err - } - return meshdeliveryclient.EncodeOwed(meshdelivery.OwedByRepositoryNumberHeadActValue(o)) -} - -// DecodeCheckerOwed reads an owed act of the checker's state. -func DecodeCheckerOwed(data []byte) (meshdeliverychecker.OwedByRepositoryNumberHeadActValue, error) { - g, err := meshdeliveryclient.DecodeOwed(data) - if err != nil { - return meshdeliverychecker.OwedByRepositoryNumberHeadActValue{}, err - } - o := meshdeliverychecker.OwedByRepositoryNumberHeadActValue(g) - if err := checkCheckerOwed(o); err != nil { - return meshdeliverychecker.OwedByRepositoryNumberHeadActValue{}, err - } - return o, nil -} diff --git a/modules/mesh-delivery-checker/internal/check/package_cause_test.go b/modules/mesh-delivery-checker/internal/check/package_cause_test.go deleted file mode 100644 index 7922e012..00000000 --- a/modules/mesh-delivery-checker/internal/check/package_cause_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package check - -import ( - "mesh-delivery-checker/rules" - "strings" - "testing" -) - -// What the build module before this fix sent for two packages that failed after their tests passed (issue 1901, its third -// meeting: a try of mesh-controller kept "PASS" as its failure's words). Each is that build module's Output for the real -// `go test -json` of a throwaway package (mesh-catalog modules/mesh-build-go testdata/captured/go-test-json-pkg-*.jsonl): -// TestMain panicking in a goroutine after m.Run, TestMain exiting 2 after m.Run, and TestMain racing after m.Run. -const ( - oldPassThenPanic = "PASS\npanic: the bus closed after the tests: nats: connection closed\ngoroutine 20 [running]:\n" + - "example.com/passthenfail.TestMain.func1()\n\t/tmp/fix-1901b/passthenfail/p_test.go:13 +0x25\n" + - "created by example.com/passthenfail.TestMain in goroutine 1\n\t/tmp/fix-1901b/passthenfail/p_test.go:13 +0x25\n" + - "FAIL\texample.com/passthenfail\t0.005s" - oldPassThenExit = "PASS\nFAIL\texample.com/mainexit\t0.004s" - // TestMain racing after m.Run passed, under go test -race (go-test-json-pkg-racemain.jsonl). - oldPassThenRace = "PASS\n==================\nWARNING: DATA RACE\nWrite at 0x000000836428 by goroutine 9:\n example.com/racemain.TestMain.func1()\n /tmp/fix-1901b/racemain/p_test.go:15 +0x24\nPrevious write at 0x000000836428 by main goroutine:\n example.com/racemain.TestMain()\n /tmp/fix-1901b/racemain/p_test.go:16 +0x44\n main.main()\n _testmain.go:48 +0x171\nGoroutine 9 (running) created at:\n example.com/racemain.TestMain()\n /tmp/fix-1901b/racemain/p_test.go:15 +0x37\n main.main()\n _testmain.go:48 +0x171\n==================\nFound 1 data race(s)\nFAIL\texample.com/racemain\t1.031s" -) - -// A package's failure is worded by its cause, cut to its bound, and never as pass (issue 1901, AC5). -func TestAPackageFailuresCauseIsWordedNeverPass(t *testing.T) { - for _, row := range []struct { - name string - test func(t *testing.T) - }{ - // A failure with no cause told apart never has "PASS" or "ok" as its words: the checker takes the panic line from the - // output, or says that the tests passed and the program failed after them (1901/AC5). - {"a package failure is never worded pass", func(t *testing.T) { // 1901/AC5 - cases := []struct { - name, output, want string - }{ - {"panic after the tests", oldPassThenPanic, "panic: the bus closed after the tests: nats: connection closed"}, - {"exit after the tests", oldPassThenExit, "the tests passed, then the test program exited with a failure"}, - {"race after the tests", oldPassThenRace, "WARNING: DATA RACE: Write at 0x000000836428 by goroutine 9 in example.com/racemain.TestMain.func1()"}, - {"a panic deep in a long output", "PASS\npanic: the seat row vanished\n" + strings.Repeat("goroutine 7 [chan receive]:\n\tsome/frame.go:1 +0x1\n", 300) + "FAIL\tp\t2.0s", "panic: the seat row vanished"}, - } - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - v := rules.LevelFailure("", "", "", "integration", Tested{Failed: []Failure{{Package: "github.com/novox/mesh-controller/cmd/mesh-controller", Output: c.output}}}) - for _, l := range strings.Split(v.Failed.Words, "\n") { - if l = strings.TrimSpace(l); l == "PASS" || strings.HasPrefix(l, "ok ") || strings.HasPrefix(l, "ok\t") { - t.Errorf("1901/AC5: the words keep %q: %q", l, v.Failed.Words) - } - } - if !strings.HasPrefix(afterHid(v.Failed.Words), c.want) { - t.Errorf("1901/AC5: words after the loud line start %q, want %q", rules.FirstLine(afterHid(v.Failed.Words)), c.want) - } - if got := firstFailed(*v); !strings.Contains(got, "the build module hid the failure") || strings.HasSuffix(got, ": PASS") { - t.Errorf("1901/AC5, AC8: status = %q", got) - } - }) - } - }}, - // A cause the build module told apart is cut to a line's bound, so the words keep their bound (the 1901 review's note). - {"a long cause is cut to a lines bound", func(t *testing.T) { // 1901/AC5 - f := Failure{Package: "p", Test: "TestX", Cause: strings.Repeat("c", 5000), Output: "x_test.go:1: something"} - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{f}}) - if len(v.Failed.Words) > rules.MaxWords+200 { - t.Errorf("1901/AC5: words are %d bytes, bound %d", len(v.Failed.Words), rules.MaxWords) - } - }}, - // The build module on main sends a package that stopped (a timeout, an exit, a kill) as only go test's last line, with no - // cause: the words then say so, never nothing (review round 1). Each output is what that build module sends for the - // capture of the same name (go-test-json-pkg-timeout, -exiting, -killed): the package's own output, its "FAIL" line. - {"a package stopped with only its fail line is still worded", func(t *testing.T) { // 1901/AC5 - for _, out := range []string{"FAIL\texample.com/timeout\t2.017s", "FAIL\texample.com/exiting\t0.006s", "FAIL\texample.com/killed\t0.006s"} { - v := rules.LevelFailure("", "", "", "integration", Tested{Failed: []Failure{{Package: "example.com/x", Output: out}}}) - if !strings.HasPrefix(afterHid(v.Failed.Words), "the package failed with no test to name and no panic, signal or build error in its output") { - t.Errorf("1901/AC5: %q is worded %q", out, v.Failed.Words) - } - } - }}, - // A line of the test's own that starts as go's summary does, or that holds a runtime's words indented, is not taken for - // either (review round 1): go prints its summary in exact forms and the runtime's lines at column 0. - {"only gos exact forms are summary or cause", func(t *testing.T) { // 1901/AC5 - out := " x_test.go:3: ok so far\nok then: the seat is free\n panic: child retried and recovered\npanic: the real one\nFAIL\tp\t1.0s" - v := rules.LevelFailure("", "", "", "unit", Tested{Failed: []Failure{{Package: "p", Output: out}}}) - if !strings.HasPrefix(afterHid(v.Failed.Words), "panic: the real one") || !strings.Contains(v.Failed.Words, "ok then: the seat is free") { - t.Errorf("1901/AC5: words = %q", v.Failed.Words) - } - }}, - // The words keep rules.MaxWords exactly, counting the cause, the marker and the newlines (review round 1). - {"the words keep their bound exactly", func(t *testing.T) { // 1901/AC5 - for _, cause := range []string{"", "a cause", strings.Repeat("c", 3000)} { - for _, n := range []int{100, 170, 171, 172, 500} { - f := Failure{Package: "p", Test: "TestX", Cause: cause, Output: strings.Repeat("a log line of twenty\n", n) + "the end"} - if w := rules.FailureWords(f); len(w) > rules.MaxWords { - t.Errorf("1901/AC5: cause %d bytes, %d lines: words are %d bytes, bound %d", len(cause), n, len(w), rules.MaxWords) - } - } - } - }}, - } { - t.Run(row.name, row.test) - } -} - -// afterHid is a verdict's words after the checker's loud line, which a failure with no cause from an older build module -// now starts with (1901/AC8). -func afterHid(w string) string { - if strings.HasPrefix(w, "the build module hid the failure") { - _, rest, _ := strings.Cut(w, "\n") - return rest - } - return w -} diff --git a/modules/mesh-delivery-checker/internal/check/pins.go b/modules/mesh-delivery-checker/internal/check/pins.go deleted file mode 100644 index 277bedc5..00000000 --- a/modules/mesh-delivery-checker/internal/check/pins.go +++ /dev/null @@ -1,76 +0,0 @@ -package check - -import ( - "context" - "errors" - "maps" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/grouppackages" -) - -// **A check builds against the group's own package heads, not against what a member names** (novox/hq ADR 0573 §2, §4; -// issue 1585). Every Go check ask carries the version of each mesh package to build against (buildstep.StepAsk.Packages): -// the head of the group's member that publishes it, else its latest published version as the indexer holds it. The build -// module writes them into the workspace, whatever version or commit a member's go.mod names, so no member is refused -// for naming no version of the group's package, an older head of it, a merge commit or a commit on no pull request: that -// refusal (issue 1624's pin check) was a fault of the check (§4), and is gone. - -// SDKRepository is the repository of mesh-sdk, and SDKModulePath its Go module. -const ( - SDKRepository = "novox/mesh-sdk" - SDKModulePath = "git.novox.be/novox/mesh-sdk/go" -) - -// ErrNotFound is what Forge.File wraps when the ref holds no such file (the forge answered 404): a file the ref does not hold, -// told from a forge that could not be read (issue 1845). -var ErrNotFound = errors.New("the ref holds no such file") - -// Forge reads a file of a repository at a ref. -type Forge interface { - // File is the text of a file at a ref (a branch, a tag or a commit). - File(ctx context.Context, repository, path, ref string) (string, error) -} - -// packagesOf is the version of each mesh package a Go build of repository (a check ask, the judge, the composer) builds -// against (ADR 0573 §2, §4, as ADR 0607 §5 changes §4): packagesFor over the latest published version of each package the -// mesh's modules use (Published) and the heads of the case's pull requests. Nil for none. -func (st *state) packagesOf(repository string) buildstep.PackageVersions { - return packagesFor(st.j.Published, st.c, repository) -} - -// packagesFor is packagesOf for a case and the published versions: every producer of a build at a group's heads takes its -// versions from here, so none leaves a member out (issue 1921). grouppackages.Heads names each package at its member's -// head, also a member that publishes no package yet. A package of repository itself comes with the tree the build -// fetched, and is left out. -func packagesFor(published interface { - LatestPackages() buildstep.PackageVersions -}, c *Case, repository string) buildstep.PackageVersions { - latest := buildstep.PackageVersions{} - if published != nil { - maps.Copy(latest, published.LatestPackages()) - } - heads := map[string]string{} - for _, m := range c.Pulls { - heads[m.Repository] = m.Head - } - versions := grouppackages.Heads(latest, heads, repository) - return versions -} - -// unheardWords ends the words of a failed read of the forge that reached nobody: the forge was not heard (UnheardWords). -func unheardWords(err error) string { - if isUnheard(err) { - return " (the forge was " + UnheardWords + ")" - } - return "" -} - -// forgeMachine is the machine a failed read of the forge is put at: the forge when nobody answered it (no build node is to -// blame, issue 1820), the build node whose workspace was read otherwise. -func forgeMachine(err error, node string) string { - if isUnheard(err) { - return "forge" - } - return node -} diff --git a/modules/mesh-delivery-checker/internal/check/pins_test.go b/modules/mesh-delivery-checker/internal/check/pins_test.go deleted file mode 100644 index e7cc68d2..00000000 --- a/modules/mesh-delivery-checker/internal/check/pins_test.go +++ /dev/null @@ -1,301 +0,0 @@ -package check - -import ( - "context" - "maps" - "slices" - "strings" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/grouppackages" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// published is the latest published version of each mesh package, as the indexer's modules say (Examiner.Published). -type published buildstep.PackageVersions - -func (p published) LatestPackages() buildstep.PackageVersions { - return maps.Clone(buildstep.PackageVersions(p)) -} - -// packagesOfFetch is the package versions the fetch of repository was handed (buildstep.StepAsk.Packages, issue 1585). -func packagesOfFetch(t *testing.T, b *bench, repository string) buildstep.PackageVersions { - t.Helper() - for _, f := range b.steps.tool("fetch") { - if f.args["repository"] == repository { - p, err := buildstep.PackagesArg(f.args["packages"]) - if err != nil { - t.Fatal(err) - } - return p - } - } - t.Fatalf("%s was not fetched", repository) - return nil -} - -// ADR 0573 §4 (issue 1585 S14): a group's Go member is built against the head of the group's mesh-sdk member, whatever its -// go.mod names (an older head of it included, which issue 1624's pin check refused), and never refused for its pin; a -// pull request alone is built against the latest published version of each package, and its own repository's packages -// come with its tree. -func TestAGroupMemberIsBuiltAgainstTheSDKMembersHeadWhateverItsGoModNames(t *testing.T) { // 1624/AC2, AC4 as ADR 0573 §4 changes them - const contract = "git.novox.be/novox/mesh-delivery/contract" - b := newBench(t) - b.fetch.Modules = []string{"modules/m"} - b.ex.Published = published{SDKModulePath: trunkS, contract: trunkA} - v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf(repoSDK, 12, head2))).Verdict - if v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", v) - } - if got := packagesOfFetch(t, b, repoA); len(got) != 1 || got[SDKModulePath] != head2 { - t.Errorf("the member's packages = %v, want mesh-sdk at the group's head %s alone (its own contract comes with its tree)", got, head2) - } - if s := besideOf(t, b, repoA)[repoSDK]; s.Commit != head2 { - t.Error("mesh-delivery's tests were not given the SDK member at its head") - } - b = newBench(t) - b.ex.Published = published{SDKModulePath: trunkS, contract: trunkA} - if v := b.judge(t, caseOf(t, pullOf("novox/mesh-catalog", 7, head1))).Verdict; v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("a pull request alone: verdict = %+v", v) - } - if got := packagesOfFetch(t, b, "novox/mesh-catalog"); len(got) != 2 || got[SDKModulePath] != trunkS || got[contract] != trunkA { - t.Errorf("a pull request alone is built against %v, want every package at its latest published version", got) - } - // A member that publishes no package yet is still named, by its root module path, at its head (issue 1921). - b = newBench(t) - b.ex.Published = published{} - if v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1), pullOf("novox/mesh-host", 8, head2))).Verdict; v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("an unpublished member: verdict = %+v", v) - } - if got := packagesOfFetch(t, b, repoA); len(got) != 1 || got["git.novox.be/novox/mesh-host"] != head2 { - t.Errorf("the member's packages = %v, want the unpublished member's root at its head %s alone", got, head2) - } - // The function every producer takes the versions from (issue 1921): rows for what a build at a group's heads is asked. - h := func(c string) string { return strings.Repeat(c, 40) } - const ( - contractsPkg = "git.novox.be/novox/mesh-contracts/go/clients/assignerclient" - toolsPkg = "git.novox.be/novox/mesh-tools/go/x" - ) - for _, tc := range []struct { - name string - latest buildstep.PackageVersions - heads map[string]string - own string - want buildstep.PackageVersions - }{ - {"none", nil, nil, "novox/mesh-host", nil}, - {"trunk latest alone", buildstep.PackageVersions{toolsPkg: h("a")}, nil, "novox/mesh-host", buildstep.PackageVersions{toolsPkg: h("a")}}, - {"a member at its head replaces the latest", buildstep.PackageVersions{SDKModulePath: h("a")}, map[string]string{SDKRepository: h("b")}, "novox/mesh-host", - buildstep.PackageVersions{SDKModulePath: h("b")}}, - {"a member that publishes no package yet is named by its root module", buildstep.PackageVersions{SDKModulePath: h("a")}, - map[string]string{"novox/mesh-contracts": h("c")}, "novox/mesh-host", - buildstep.PackageVersions{SDKModulePath: h("a"), "git.novox.be/novox/mesh-contracts": h("c")}}, - {"mesh-sdk is named by its go module before any package of it is known", nil, map[string]string{SDKRepository: h("b")}, "novox/mesh-host", - buildstep.PackageVersions{SDKModulePath: h("b")}}, - {"a member whose packages are known gets no root", buildstep.PackageVersions{contractsPkg: h("a")}, - map[string]string{"novox/mesh-contracts": h("c")}, "novox/mesh-host", buildstep.PackageVersions{contractsPkg: h("c")}}, - {"the build's own repository is left out", buildstep.PackageVersions{contractsPkg: h("a")}, - map[string]string{"novox/mesh-contracts": h("c")}, "novox/mesh-contracts", nil}, - {"a head that is no full commit names nothing", nil, map[string]string{"novox/mesh-contracts": "abc"}, "novox/mesh-host", nil}, - } { - got := grouppackages.Heads(tc.latest, tc.heads, tc.own) - if !maps.Equal(got, tc.want) { - t.Errorf("%s: got %v, want %v", tc.name, got, tc.want) - } - } - - // Design 47 G §528: a group's check asks no tagged version and waits on none; it builds its members at once, each Go - // build against the group's heads. - sdk := catalogue.Manifest{Module: "mesh-sdk", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{ - {Name: "go", Kind: catalogue.ArtifactPackage, Language: "go", Paths: []string{"go"}}, {Name: "lib", Kind: catalogue.ArtifactPackage, Language: "typescript"}}}} - digest := strings.Repeat("c", 64) - sdkPull := pullOf(repoSDK, 12, head2) - sdkPull.Modules = []string{"mesh-sdk"} - sdkPull.Fps = map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"mesh-sdk": {Fp2: "src2:" + strings.Repeat("f", 64), Manifest: digest}} - b = newBench(t) - b.fetch.Modules = []string{"modules/m"} - b.ex.Read = fixedManifests{digest: sdk} - b.ex.Modules = langs{repoA: {goBuildModule, nodeBuildModule}} - if v := b.judge(t, caseOf(t, pullOf(repoA, 7, head1), sdkPull)).Verdict; v.State != meshdeliverycheckerclient.VerdictPass { - t.Errorf("a group's verdict = %+v", v) - } - if got := packagesOfFetch(t, b, repoA); got[SDKModulePath] != head2 { - t.Errorf("mesh-delivery is built against mesh-sdk %q, want the group's head %s (all: %v)", got[SDKModulePath], head2, got) - } -} - -// headFiles are the forge's files by " "; any other is not found. -type headFiles map[string]string - -func (h headFiles) File(_ context.Context, repository, path, ref string) (string, error) { - if text, ok := h[repository+" "+path+" "+ref]; ok { - return text, nil - } - return "", ErrNotFound -} - -type runsAt map[string]string - -func (r runsAt) CommitOf(module string) string { return r[module] } - -func besideOf(t *testing.T, b *bench, repository string) map[string]BesideRef { - t.Helper() - out := map[string]BesideRef{} - for _, f := range b.steps.tool("fetch") { - if f.args["repository"] == repository { - refs, _ := refsOf(f.args["beside"]), true - for _, r := range refs { - out[r.Repository] = r - } - } - } - return out -} - -func TestTheSiblingsAreWhereTheTestsStateThem(t *testing.T) { // review D2, ADR 0484 clause 4 - const catalogue, host, controller, networking = "novox/mesh-catalog", "novox/mesh-host", "novox/mesh-controller", "novox/mesh-networking" - hostRuns := strings.Repeat("7", 40) - b := newBench(t) - b.ex.Trunks = trunksOf(t, map[string]string{SDKRepository: trunkS, catalogue: trunkA, host: trunkA2}) - b.ex.Runs = runsAt{"mesh-host": hostRuns} - // A group of mesh-sdk and the catalogue: mesh-sdk's tests read the group's catalogue head, the heads the group merges - // (ADR 0482 §2, ADR 0573 §4; issue 1126: a judge in the group was judged against the catalogue's trunk). - out := b.judge(t, caseOf(t, pullOf(SDKRepository, 7, head1), pullOf(catalogue, 12, head2))) - // ADR 0548 §6, issue 568's own example: the verdict names the node-engine at the commit the mesh runs; the catalogue, - // a member here, is not named. - if out.Beside[host] != hostRuns || out.Beside[catalogue] != "" { - t.Errorf("the siblings named for the verdict = %v, want the node-engine at %s", out.Beside, hostRuns[:12]) - } - got := besideOf(t, b, SDKRepository) - if c := got[catalogue]; c.Commit != head2 { - t.Errorf("the catalogue beside mesh-sdk in a group = %+v, want the member's head %s", c, head2) - } - if h := got[host]; h.Commit != hostRuns { - t.Errorf("the node-engine beside mesh-sdk = %+v, want the commit the mesh runs %s", h, hostRuns) - } - // ADR 0593: route-proxy's proofs of a composition moved into the node-engine's tests, which read its manifest in - // mesh-networking: beside a node-engine change, at the group's head when the group holds one, else at its trunk. - b = newBench(t) - b.ex.Trunks = trunksOf(t, map[string]string{SDKRepository: trunkS, catalogue: trunkA, host: trunkA2, networking: trunkA2}) - b.judge(t, caseOf(t, pullOf(host, 8, head1), pullOf(networking, 3, head2))) - if n := besideOf(t, b, host)[networking]; n.Commit != head2 { - t.Errorf("mesh-networking beside the node-engine in a group = %+v, want the member's head %s", n, head2) - } - // Alone, mesh-sdk's tests read the catalogue's trunk, and the verdict names it. - b = newBench(t) - const contracts = "novox/mesh-contracts" - b.ex.Trunks = trunksOf(t, map[string]string{SDKRepository: trunkS, catalogue: trunkA, host: trunkA2, contracts: trunkA, DeliveryRepository: trunkA2}) - if out := b.judge(t, caseOf(t, pullOf(SDKRepository, 7, head1))); out.Beside[catalogue] != trunkA { - t.Errorf("the siblings named for the verdict alone = %v, want the catalogue at its trunk %s", out.Beside, trunkA) - } - // mesh-sdk's tests compare its bootstrapped seats with their contracts files, read beside it: mesh-contracts and - // mesh-delivery, at their trunks alone (a group try is placed by the same besideFor, so it lays them out the same way). - if got := besideOf(t, b, SDKRepository); got[contracts].Commit != trunkA || got[DeliveryRepository].Commit != trunkA2 { - t.Errorf("beside mesh-sdk alone = %+v, want mesh-contracts at %s and mesh-delivery at %s", got, trunkA, trunkA2) - } - if c := besideOf(t, b, SDKRepository)[catalogue]; c.Commit != trunkA || c.Ref != "" { - t.Errorf("the catalogue beside mesh-sdk alone = %+v, want its trunk %s", c, trunkA) - } - // 1126/B8 (novox/hq ADR 0590, ADR 0593 §11): the controller's repository is emptied and archived, so no repository's - // tests read it and it has no siblings of its own: a pull request of it has only its own trunk beside it. - b = newBench(t) - b.ex.Trunks = trunksOf(t, map[string]string{controller: trunkA, catalogue: trunkS, host: trunkA2, SDKRepository: trunkS}) - b.judge(t, caseOf(t, pullOf(controller, 9, head2))) - if got := besideOf(t, b, controller); len(got) != 1 || got[controller].Commit != trunkA { - t.Errorf("beside the controller's own change = %+v, want its own trunk alone", got) - } - for repo, siblings := range besideRules { - if repo == controller || slices.Contains(siblings, controller) { - t.Errorf("the tests of %s read the controller, or it has siblings", repo) - } - } -} - -// 1887/A12: the change's own repository is placed beside it at the trunk it is merged onto, for every repository, so a -// shrink-only list test reads the trunk's copy at $MESH_CHECK_BESIDE/. -func TestTheChangesOwnRepositoryIsBesideItAtItsTrunk(t *testing.T) { - b := newBench(t) - b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - got := besideOf(t, b, repoA) - if own := got[repoA]; own.Commit != trunkA || own.Ref != "" { - t.Fatalf("the repository beside its own change = %+v, want its trunk %s", own, trunkA) - } -} - -// 1887/A12: a catalogue change's siblings hold the catalogue itself at the trunk it is merged onto, beside mesh-sdk, so -// the catalogue's shrink-only list tests read $MESH_CHECK_BESIDE/mesh-catalog. And the node-engine at the commit the mesh -// runs (ADR 0593 §7): mesh-build-go's composer test builds the node-engine's composer command from it. -func TestACatalogueChangeHasTheCatalogueBesideItAtItsTrunk(t *testing.T) { - hostRuns := strings.Repeat("7", 40) - b := newBench(t) - b.ex.Trunks = trunksOf(t, map[string]string{catalogueRepository: trunkA, SDKRepository: trunkS, "novox/mesh-controller": trunkA2, nodeEngineRepo: trunkA2, - contractsRepository: trunkA2, deliveryRepository: trunkA2, networkingRepository: trunkA2, "novox/mesh-tools": trunkA2}) - b.ex.Runs = runsAt{nodeEngineModule: hostRuns} - // The node-engine's go.mod at the commit the mesh runs: what it requires at the zero version is placed beside too, - // derived, not named in a rule (issue 1921); a version a build wrote, or a package outside the forge, is not. - b.ex.Forge = headFiles{nodeEngineRepo + " go.mod " + hostRuns: "module github.com/novox/mesh-host\n\ngo 1.26\n\nrequire (\n" + - "\tgit.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000\n" + - "\tgit.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 // indirect\n" + - "\tgit.novox.be/novox/mesh-tools/go v0.1.2\n\tgithub.com/nats-io/nats.go v1.40.0\n)\n\n" + - "require git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go v0.0.0-00010101000000-000000000000\n"} - out := b.judge(t, caseOf(t, pullOf(catalogueRepository, 12, head1))) - // ADR 0548 §6: the outcome names the siblings read at their trunk's head or the commit the mesh runs, never the - // repository it merges. - if out.Beside[SDKRepository] != trunkS || out.Beside[nodeEngineRepo] != hostRuns || out.Beside["novox/mesh-controller"] != "" || out.Beside[catalogueRepository] != "" { - t.Errorf("the siblings named for the verdict = %v, want mesh-sdk at its trunk, the node-engine at the commit the mesh runs, and neither the controller nor the catalogue", out.Beside) - } - got := besideOf(t, b, catalogueRepository) - if c := got[catalogueRepository]; c.Commit != trunkA || c.Ref != "" { - t.Fatalf("the catalogue beside its own change = %+v, want its trunk %s", c, trunkA) - } - // Issue 1921: and every repository the node-engine's composer requires at the zero version, which mesh-build-go's - // composer test builds from what lies beside, with nothing fetched. - for _, repo := range []string{SDKRepository, contractsRepository, deliveryRepository, networkingRepository} { - if _, ok := got[repo]; !ok { - t.Errorf("%s is not beside a catalogue change: %v", repo, got) - } - } - if _, ok := got["novox/mesh-tools"]; ok { - t.Errorf("mesh-tools, required at a version a build wrote, is beside a catalogue change: %v", got) - } - if h := got[nodeEngineRepo]; h.Commit != hostRuns { - t.Errorf("the node-engine beside a catalogue change = %+v, want the commit the mesh runs %s", h, hostRuns) - } - // A group that holds the node-engine is judged against its head, which is what runs after its merge, never the commit - // the mesh runs now; the verdict names it in no carry (it is a member, not read at a trunk or a running commit). - b = newBench(t) - b.ex.Trunks = trunksOf(t, map[string]string{catalogueRepository: trunkA, SDKRepository: trunkS, nodeEngineRepo: trunkA2}) - b.ex.Runs = runsAt{nodeEngineModule: hostRuns} - out = b.judge(t, caseOf(t, pullOf(catalogueRepository, 12, head1), pullOf(nodeEngineRepo, 8, head2))) - if h := besideOf(t, b, catalogueRepository)[nodeEngineRepo]; h.Commit != head2 { - t.Errorf("the node-engine beside a catalogue change in a group holding it = %+v, want the member's head %s", h, head2) - } - if out.Beside[nodeEngineRepo] != "" { - t.Errorf("the verdict names the member node-engine in its carry: %v", out.Beside) - } -} - -// 1887/A12: a mesh-lab head gets a check: with its trunk known it is judged like any repository, its Go fetch finding -// the nested modules itself. -func TestAMeshLabHeadGetsACheck(t *testing.T) { - const lab = "novox/mesh-lab" - raw := candidateOf(t, "fix/x", pullOf(lab, 3, head1)) - c, err := meshcatalog.DecodeCandidatesByGroupValue(raw) - if err != nil { - t.Fatal(err) - } - cs := CaseOf(c, map[string]string{lab: trunkA}) - if cs == nil || len(cs.Pulls) != 1 || cs.Pulls[0].Repository != lab || len(cs.Left) != 0 { - t.Fatalf("the case = %+v, want mesh-lab#3 judged", cs) - } - b := newBench(t) - if v := b.judge(t, cs).Verdict; v.State != meshdeliverycheckerclient.VerdictPass { - t.Fatalf("verdict = %+v", v) - } - if fetched := b.steps.tool("fetch"); len(fetched) == 0 || fetched[0].args["repository"] != lab { - t.Fatalf("mesh-lab was not fetched: %+v", fetched) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/preview.go b/modules/mesh-delivery-checker/internal/check/preview.go deleted file mode 100644 index 8cb11808..00000000 --- a/modules/mesh-delivery-checker/internal/check/preview.go +++ /dev/null @@ -1,87 +0,0 @@ -package check - -import ( - "context" - "errors" - "fmt" - "regexp" - "slices" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -var digest64 = regexp.MustCompile(`^[0-9a-f]{64}$`) - -// PreviewOf is what the fetch steps said of a pull request's head merged onto its trunk head, as the value of a previews -// key (ADR 0484 clause 3): the merge commit and, for each module directory the fetches named with a manifest, its tree, the -// digest of its manifest, the hashes of its lock files and its named paths' objects (ADR 0499 clause 2). The fetches of the repository's build modules merge the same -// trees; the first one's merge commit is kept. A module directory with no tree or no manifest digest is left out, so the -// indexer gives its module no fingerprint and says why. A head that does not merge has a preview of the files in conflict -// alone, so the indexer says the candidate's modules have no fingerprint because it does not merge (issue 1445). It -// answers false when no fetch was made. -func PreviewOf(p Pull, fetched []Fetched) (meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue, bool) { - var merge string - var conflicts []string - mods := map[string]meshdeliverychecker.PreviewsByRepositoryHeadTrunkValueModulesValue{} - for _, f := range fetched { - if len(f.Conflicts) > 0 { - conflicts = append(conflicts, f.Conflicts...) - } - } - if len(conflicts) > 0 { - slices.Sort(conflicts) - return meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue{Repository: p.Repository, Head: p.Head, Trunk: p.Trunk, Conflicts: slices.Compact(conflicts)}, true - } - for _, f := range fetched { - if f.Merge == "" { - continue - } - if merge == "" { - merge = f.Merge - } - for dir, t := range f.Trees { - if t.Tree == "" || !digest64.MatchString(t.Manifest) { - continue - } - mods[dir] = meshdeliverychecker.PreviewsByRepositoryHeadTrunkValueModulesValue{Tree: t.Tree, Manifest: t.Manifest, Locks: t.Locks, Paths: t.Paths, Packages: t.Packages} - } - } - if merge == "" { - return meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue{}, false - } - pv := meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue{Repository: p.Repository, Head: p.Head, Trunk: p.Trunk, Merge: merge} - if len(mods) > 0 { - pv.Modules = mods - } - return pv, true -} - -// writePreview writes the preview of a pull request by a create: one writer per key, and the same head on the same trunk -// head merges to the same trees, so a key that exists stands. A head that did not merge has no preview. A failure is said -// and does not fail the check: the preview is for the indexer's fingerprints, not for the verdict. -func (j *Examiner) writePreview(ctx context.Context, p Pull, fetched []Fetched) { - if j.Previews == nil { - return - } - pv, ok := PreviewOf(p, fetched) - if !ok { - return - } - key, err := meshdeliverycheckerclient.PreviewKey(p.Repository, p.Head, p.Trunk) - if err == nil { - var data []byte - if data, err = meshdeliverycheckerclient.EncodePreview(pv); err == nil { - _, err = j.Previews.Create(ctx, key, data) - if errors.Is(err, ErrConflict) { - return - } - if err == nil && j.Announce != nil { - j.Announce(ctx, EventPreviewWritten, key) - } - } - } - if err != nil { - j.logf("%s#%d: the preview cannot be written: %v", p.Repository, p.Number, fmt.Errorf("%w", err)) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/preview_named_test.go b/modules/mesh-delivery-checker/internal/check/preview_named_test.go deleted file mode 100644 index ebd968a2..00000000 --- a/modules/mesh-delivery-checker/internal/check/preview_named_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package check - -import ( - "encoding/json" - "reflect" - "testing" -) - -// I8, ADR 0499 clause 2: the preview carries each named path's object a fetch's module trees answer (mesh-sdk -// buildstep.ModuleTree's paths), so the indexer computes the candidate's narrowed tree from them by the one function. -func TestAPreviewCarriesTheNamedPathsObjectsOfAFetchsModuleTrees(t *testing.T) { - var f Fetched - // As buildstep.ModuleTrees answers a module naming its paths: the named tree and the objects. - raw := `{"workspace":"w","merge":"6666666666666666666666666666666666666666","trees":{".":{"tree":"named:aa","manifest":"` + - "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + `","paths":{"src":"1111111111111111111111111111111111111111"},"packages":["git.novox.be/novox/mesh-sdk/go"]}}}` - if err := json.Unmarshal([]byte(raw), &f); err != nil { - t.Fatal(err) - } - pv, ok := PreviewOf(Pull{Repository: "novox/mesh-sdk", Head: "h", Trunk: "t"}, []Fetched{f}) - if !ok || !reflect.DeepEqual(pv.Modules["."].Paths, map[string]string{"src": "1111111111111111111111111111111111111111"}) || pv.Modules["."].Tree != "named:aa" || - !reflect.DeepEqual(pv.Modules["."].Packages, []string{"git.novox.be/novox/mesh-sdk/go"}) { // and the packages it uses (issue 1585) - t.Errorf("preview = %+v, want the module's named tree and its objects", pv) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/programs.go b/modules/mesh-delivery-checker/internal/check/programs.go deleted file mode 100644 index 56bfc134..00000000 --- a/modules/mesh-delivery-checker/internal/check/programs.go +++ /dev/null @@ -1,294 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "fmt" - "maps" - "slices" - "strings" - "sync" - "time" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" -) - -// The checking program (novox/hq ADR 0644 §1, ADR 0656 §5; design 47's bootstrap delivery; issue 1921): every check has -// the program built from the group's mesh-delivery member head, else from mesh-delivery's trunk, and run in the build -// seat's sandbox through the seat node-build-go's ask `run`, with every member's head laid for it. It answers the heads' -// seat list first (`-seats`), which the manifest validator's and the composer's requests carry, and then the check's -// verdict of the facts the asking gathered (`-verdict`). The running checker asks, keeps and records; it judges nothing. - -const ( - // InputsStateName is the checker's own state of run inputs and InputsBucket its bucket: every input by the sha256 of - // its exact bytes, read by the checker alone. InputVerb is the seat verb a run ask's input names - // (buildstep.RunInput.Verb), which the checker's tools serve from that state (Reader.Input): the Go build module asks it - // as its manifest's invokes grants it and checks the digest (buildstep.RunInput.Read), never reading the state (ADR 0596 - // §2; the operator's answer on milestone 47/B0). - InputsStateName = "inputs" - InputVerb = meshdeliverycheckerclient.InputVerb - // CheckingProgramDir and CheckingProgramPackage are where in mesh-delivery the checking program is built. - CheckingProgramDir = "modules/mesh-delivery-checker" - CheckingProgramPackage = "mesh-delivery-checker/cmd/mesh-checking-program" - // DefaultBeginBound is how long a run ask may wait for a build node to begin its step before it is put once more, and - // after that once more before the check fails naming the job: a build module that does not know the job `run` drops - // it unanswered (the lead's ruling on milestone 47/B0, 2026-11-06). - DefaultBeginBound = 5 * time.Minute -) - -// Inputs keeps a run's input under its digest, exactly its bytes. -type Inputs interface { - Keep(ctx context.Context, digest string, body []byte) error -} - -func (j *Examiner) beginBound() time.Duration { - if j.BeginBound > 0 { - return j.BeginBound - } - return DefaultBeginBound -} - -// checkingProgram is where this check's checking program is built: the group's mesh-delivery member head, else -// mesh-delivery's trunk; false when neither is known. -func (st *state) checkingProgram() (buildstep.RunProgram, string, bool) { - p := buildstep.RunProgram{Repository: DeliveryRepository, Dir: CheckingProgramDir, Package: CheckingProgramPackage} - for _, pull := range st.c.Pulls { - if pull.Repository == DeliveryRepository { - p.Commit = pull.Head - return p, "the group's mesh-delivery head " + pull.Head, true - } - } - if st.j.Trunks != nil { - p.Commit = st.j.Trunks.Heads()[DeliveryRepository] - } - return p, "mesh-delivery's trunk " + p.Commit, p.Commit != "" -} - -// runHeads are the members' heads, as the program reads them. -func (st *state) runHeads() []buildstep.RunHead { - out := make([]buildstep.RunHead, 0, len(st.c.Pulls)) - for _, p := range st.c.Pulls { - out = append(out, buildstep.RunHead{Repository: p.Repository, Commit: p.Head}) - } - return out -} - -// runProgram has the checking program built and run with args over body, and answers its output and what judged; an -// error says why it did not answer: its input over its bound, no build node beginning the job, a step that did not -// pass, or a program that exited otherwise than 0. -func (st *state) runProgram(ctx context.Context, args []string, body []byte) (json.RawMessage, *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy, error) { - prog, at, ok := st.checkingProgram() - if !ok { - return nil, nil, fmt.Errorf("it cannot be built: the trunk head of %s is not known", DeliveryRepository) - } - if st.j.Inputs == nil { - return nil, nil, fmt.Errorf("its input cannot be kept: the checker keeps no state %s", InputsStateName) - } - in, err := buildstep.RunInputOf(InputVerb, body) - if err != nil { - return nil, nil, err - } - if err := st.j.Inputs.Keep(ctx, in.Digest, body); err != nil { - return nil, nil, fmt.Errorf("its input could not be kept in %s: %w", InputsStateName, err) - } - job := buildstep.RunJob{Program: prog, Heads: st.runHeads(), Input: in, Args: args, Reader: "the checking program at " + at} - var ids []string - for range 2 { - a := buildstep.StepAsk{ID: buildstep.NewID(), Kind: buildstep.AskCheck, Language: buildstep.AskGo, Asker: st.j.Module, - At: st.j.now().UTC(), Packages: st.packagesOf(DeliveryRepository), Run: &job} - ids = append(ids, a.ID) - st.c.Progress.Set(fmt.Sprintf("the run ask %s of the checking program %s", a.ID, strings.Join(args, " ")), "") - st.c.Asked(a.ID) - if err := st.j.Asks.Put(ctx, a); err != nil { - return nil, nil, fmt.Errorf("its run ask could not be put: %w", err) - } - waiting := ctx - if watch := st.c.Watch; watch != nil { - id := a.ID - watch(id, nil) // a try names it at once (issue 1890) - waiting = buildrun.WithStepSeen(ctx, func(s buildstep.Step) { watch(id, &s) }) - } - step, begun, err := st.waitBegun(waiting, a) - if !begun { - continue - } - if err != nil { - return nil, nil, fmt.Errorf("its run ask %s: %w", a.ID, err) - } - var answer buildstep.RunAnswer - if step.State != buildstep.StepPassed { - // A run whose program exited other than 0 ends its key failed with its answer kept (design 47 G A-290). - if json.Unmarshal(step.Result, &answer) == nil && answer.Exit != 0 { - return nil, nil, fmt.Errorf("it exited %d on %s: %s", answer.Exit, step.Node, answer.Said) - } - return nil, nil, fmt.Errorf("its run ask %s ended %s on %s: %s", a.ID, step.State, step.Node, step.Reason) - } - if err := json.Unmarshal(step.Result, &answer); err != nil { - return nil, nil, fmt.Errorf("its run ask %s answered no run answer: %w", a.ID, err) - } - if answer.Exit != 0 { - return nil, nil, fmt.Errorf("it exited %d on %s: %s", answer.Exit, step.Node, answer.Said) - } - return answer.Output, judgedByOf(prog, answer, job.Heads), nil - } - return nil, nil, fmt.Errorf("no build node began the job run of the asks %s within %s each: a build module that does not know the job run drops it", - strings.Join(ids, " and "), st.j.beginBound()) -} - -// waitBegun waits for a's step, and stops waiting, begun false, when no build node began it within the begin bound: a -// bound on the wait's context, no timer of its own. -func (st *state) waitBegun(ctx context.Context, a buildstep.StepAsk) (buildstep.Step, bool, error) { - wait, cancel := context.WithCancel(ctx) - defer cancel() - begin, stop := context.WithTimeout(ctx, st.j.beginBound()) - defer stop() - seen := make(chan struct{}) - var once sync.Once - type ended struct { - step buildstep.Step - err error - } - done := make(chan ended, 1) - go func() { - step, err := st.j.Asks.Wait(buildrun.WithStepSeen(wait, func(buildstep.Step) { once.Do(func() { close(seen) }) }), a) - done <- ended{step, err} - }() - select { - case e := <-done: - return e.step, true, e.err - case <-seen: - case <-begin.Done(): - if ctx.Err() != nil { - return buildstep.Step{}, true, ctx.Err() - } - return buildstep.Step{}, false, nil - } - e := <-done - return e.step, true, e.err -} - -// seatList asks the checking program for the heads' seat list, which the validator's and the composer's requests carry -// (ADR 0636 §4); the list and what it left out are the check's notes. A list it cannot make fails the check, naming why, -// and answers false: the check ends there, with no verdict asked of a program that could not answer its list (review -// round 1 of milestone 47/B0: a build node with no job run would hold the check two begin bounds more). -func (st *state) seatList(ctx context.Context) bool { - if st.narrowTry() { - return true - } - // The list the checker's reader holds from the seat mesh-catalog: a reader that has not read it acts on nothing new - // (ADR 0636 §1), so the check ends here, said. - var base []json.RawMessage - if st.j.SeatList != nil { - defs, err := st.j.SeatList.Definitions() - if err != nil { - st.fail(st.goModule(), "the change's manifests cannot be judged: the seat list is not read ("+err.Error()+")") - return false - } - base = defs - } - // The program answers only the heads' definitions the list does not hold as they are: the list's digests ride in its - // input, never the list, whose definitions are larger than a run's input or answer may be (review finding 3 of G). - head := st.factsHead() - if len(base) > 0 { - head.SeatDigests = map[string]string{} - for _, raw := range base { - var d struct { - Seat string `json:"seat"` - } - if json.Unmarshal(raw, &d) == nil && d.Seat != "" { - head.SeatDigests[d.Seat] = rules.SeatDigest(raw) - } - } - } - body, err := json.Marshal(head) - if err == nil { - var out json.RawMessage - if out, _, err = st.runProgram(ctx, []string{"-seats"}, body); err == nil { - var a rules.SeatsAnswer - if err = json.Unmarshal(out, &a); err == nil && a.V != rules.SeatsVersion { - err = fmt.Errorf("it answered a seat list of version %d, not %d", a.V, rules.SeatsVersion) - } - if err == nil { - st.c.Seats = overSeats(base, a.Seats) - if len(a.Seats) > 0 { - st.c.note(fmt.Sprintf("the heads carry %d seat definitions new or changed (and %d the seat list holds as they are), laid over the %d the seat list holds", len(a.Seats), a.Same, len(base))) - } - for _, l := range a.Left { - st.c.note("the heads' seat list leaves out " + l.Path + ": " + l.Why) - } - return true - } - } - } - st.fail(st.goModule(), "the heads' seat list could not be made by the checking program: "+err.Error()) - return false -} - -// overSeats is the list's definitions with each of the heads' put in place of the one of its seat, or added, sorted by -// seat: the checker reads of a definition only its seat's name. -func overSeats(list, heads []json.RawMessage) []json.RawMessage { - by := map[string]json.RawMessage{} - for _, l := range [][]json.RawMessage{list, heads} { - for _, raw := range l { - var d struct { - Seat string `json:"seat"` - } - if json.Unmarshal(raw, &d) == nil && d.Seat != "" { - by[d.Seat] = raw - } - } - } - var out []json.RawMessage // none when neither holds one: the requests then carry no list - for _, seat := range slices.Sorted(maps.Keys(by)) { - out = append(out, by[seat]) - } - return out -} - -// factsHead are the facts that name the check, without what the asking gathered. -func (st *state) factsHead() rules.Facts { - f := rules.Facts{V: rules.FactsVersion, Group: st.c.Group, Heads: st.c.Heads, Trunk: st.c.Trunk} - for _, p := range st.c.Pulls { - f.Members = append(f.Members, rules.Member{Repository: p.Repository, Number: p.Number, Head: p.Head, Trunk: p.Trunk}) - } - return f -} - -// judged is the checking program's answer to the check's facts (`-verdict`), with what judged; a program that cannot be -// asked, built or run, or does not answer a verdict, fails the check naming the checking program, never the running -// checker's rules in its place: the facts gain the failure and the verdict is worded from them as a failure. -func (st *state) judged(ctx context.Context) (rules.Answer, *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy) { - body, err := json.Marshal(st.factsOf()) - var by *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy - if err == nil { - var out json.RawMessage - if out, by, err = st.runProgram(ctx, []string{"-verdict"}, body); err == nil { - var a rules.Answer - if err = json.Unmarshal(out, &a); err == nil && a.V != rules.AnswerVersion { - err = fmt.Errorf("it answered a verdict of version %d, not %d", a.V, rules.AnswerVersion) - } - if err == nil { - return a, by - } - } - } - st.fail(st.goModule(), "the check could not be judged by the checking program: "+err.Error()) - return rules.Answer{V: rules.AnswerVersion, Verdict: rules.Judge(st.factsOf())}, by -} - -// judgedByOf is the verdict's judged-by of a run of the checking program: where it was built, the key its build is kept -// under, whether this run built it, and the heads it read. -func judgedByOf(prog buildstep.RunProgram, answer buildstep.RunAnswer, heads []buildstep.RunHead) *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy { - by := &meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy{Program: meshdeliverychecker.VerdictsByGroupHeadsValueJudgedByProgram{ - Repository: prog.Repository, Commit: prog.Commit, Dir: prog.Dir, Package: prog.Package}, Key: answer.Key, Built: answer.Built} - for _, h := range heads { - by.Heads = append(by.Heads, meshdeliverychecker.VerdictsByGroupHeadsValueJudgedByHeadsItem{Repository: h.Repository, Commit: h.Commit}) - } - return by -} diff --git a/modules/mesh-delivery-checker/internal/check/rank_test.go b/modules/mesh-delivery-checker/internal/check/rank_test.go deleted file mode 100644 index 4f3961cc..00000000 --- a/modules/mesh-delivery-checker/internal/check/rank_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package check - -import ( - "context" - "fmt" - "sync" - "testing" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// The checker's ranked start of checks (ADR 0536 §4, issue 1897): a freed place goes to a head never judged (or asked again -// after a failure of the nodes) before a head asked again only because its trunk moved; among equals the oldest asked first. -// A running check is never stopped. - -// rankRig is a checker of one check at a time whose judge records the order checks start in and holds the first check -// after hold is set until release is closed. -type rankRig struct { - *rig - mu sync.Mutex - started []*Case - hold bool - held chan struct{} // closed when the held check started - release chan struct{} - errs []error // ctx errors the held check saw when released -} - -func group(n int) string { return fmt.Sprintf("feat/g%d", n) } - -func groups(cs []*Case) []string { - out := make([]string, len(cs)) - for i, c := range cs { - out[i] = c.Group - } - return out -} - -func TestCheckerStatusNamesTheWaitingChecksByKind(t *testing.T) { // 1897/AC4 - ctx := context.Background() - j := &judgeFunc{f: func(_ context.Context, c *Case, _ int) (Outcome, error) { - if c.Group == group(2) { - return Outcome{Verdict: failure("the forge", "go.mod cannot be read: no responders (the forge was "+UnheardWords+")")}, nil - } - return Outcome{Verdict: passing()}, nil - }} - r := newRig(t, j) - cands := fakeState{} - put := func(n int, head string) { - cand := candidateOf(t, group(n), pullOf(repoA, n, head)) - ckey, _ := meshcatalogclient.CandidatesKey(group(n)) - cands[ckey] = cand - r.ch.Candidate(ctx, group(n), cand) - } - put(1, head1) // judged on the old trunk: a re-judge waits - put(2, head1) // failed for the nodes: waits for them - r.ch.Wait() - r.setTrunks(t, map[string]string{repoA: trunkA2, repoSDK: trunkS}) - put(4, head1) // judged on the new trunk: stands - r.ch.Wait() - cand3 := candidateOf(t, group(3), pullOf(repoA, 3, head1)) // never judged - ckey3, _ := meshcatalogclient.CandidatesKey(group(3)) - cands[ckey3] = cand3 - verdicts := fakeState{} - for k, v := range r.verdicts.vals { - verdicts[k] = v - } - rd := Reader{CandidateState: cands.read(), VerdictState: verdicts.read(), - Trunks: func() (map[string]string, error) { return map[string]string{repoA: trunkA2, repoSDK: trunkS}, nil }} - st, err := rd.Status() - if err != nil { - t.Fatal(err) - } - want := map[string][]string{KindNew: {group(3)}, KindNodes: {group(2)}, KindTrunk: {group(1)}} - if fmt.Sprint(st.Waiting) != fmt.Sprint(want) { - t.Errorf("waiting = %v, want %v", st.Waiting, want) - } -} - -// 568 note 21: a verdict that read the node-engine at its running commit is not waiting for a trunk move while that commit -// is still the one the mesh runs, though the node-engine's trunk has moved on (ADR 0548 §6). -func TestCheckerStatusReadsTheNodeEngineAtItsRunningCommit(t *testing.T) { // 568/21 - cand := candidateOf(t, group(1), pullOf(repoA, 1, head1)) - ckey, _ := meshcatalogclient.CandidatesKey(group(1)) - vkey := keyOf(t, group(1), cand) - dc, _ := meshcatalog.DecodeCandidatesByGroupValue(cand) - judged, err := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: group(1), Heads: dc.Heads, Trunk: map[string]string{repoA: trunkA}, Beside: map[string]string{nodeEngineRepo: "run111"}, State: meshdeliverycheckerclient.VerdictPass, Levels: passing().Levels}) - if err != nil { - t.Fatal(err) - } - heads := func() (map[string]string, error) { - return map[string]string{repoA: trunkA, nodeEngineRepo: "trunk222"}, nil - } - for _, row := range []struct { - name string - runs runsAt - want int - }{ - {"the running commit the verdict read", runsAt{nodeEngineModule: "run111"}, 0}, - {"the running commit moved", runsAt{nodeEngineModule: "run999"}, 1}, - {"no running commit known: the trunk's", nil, 1}, - } { - rd := Reader{CandidateState: fakeState{ckey: cand}.read(), VerdictState: fakeState{vkey: judged}.read(), Trunks: heads} - if row.runs != nil { - rd.Runs = row.runs - } - st, err := rd.Status() - if err != nil { - t.Fatal(err) - } - if got := len(st.Waiting[KindTrunk]); got != row.want { - t.Errorf("%s: waiting for a trunk move = %v, want %d", row.name, st.Waiting[KindTrunk], row.want) - } - } -} diff --git a/modules/mesh-delivery-checker/internal/check/read.go b/modules/mesh-delivery-checker/internal/check/read.go deleted file mode 100644 index ee8f7f1f..00000000 --- a/modules/mesh-delivery-checker/internal/check/read.go +++ /dev/null @@ -1,791 +0,0 @@ -package check - -import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - "slices" - "sort" - "strings" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// The read tools (novox/hq ADR 0484 clauses 8 and 13, issue 1686): what the checker stored, as stored, with the line of the -// status it owes in full, so a check is read live without a shell. They write nothing. - -// The read tools, named as the module is: checker_ and the noun. -const ( - VerdictsReadTool = "checker_verdicts" - PullReadTool = "checker_pull" - KeptReadTool = "checker_kept" - InputsReadTool = "checker_inputs" - // PreviewsReadTool reads the previews state (issue 1680); StatusReadTool reads what the checker runs (issue 1685: nothing holds it since R4g). - PreviewsReadTool = "checker_previews" - StatusReadTool = "checker_status" -) - -// StateRead reads one state: its keys and a key's stored value (nil for a key with none). -type StateRead struct { - Keys func() ([]string, error) - Get func(key string) (json.RawMessage, error) -} - -// Reader answers the read tools from the checker's verdicts and kept states and the indexer's candidates. -type Reader struct { - VerdictState, KeptState, CandidateState StateRead - - PreviewState StateRead // the checker's merge previews - InputState StateRead // the checking program's inputs (InputsStateName) - StepStates func() map[string]StateRead // the build modules' steps states, by module: the holders of the build seats (design 47 G A-82), found at each read - HaveState StateRead // the node-engine's have: whether a build module is on a node and running - StepBound time.Duration // how long the checker waits for one step; DefaultStepBound when 0 - // CouldNotRunState is the checker's own state could-not-run (CouldNotRunState); without it none is named. - CouldNotRunState StateRead - // Trunks are the trunk heads of every repository (the gate's source); nil names no re-judge after a trunk move. - Trunks func() (map[string]string, error) - // Runs says which commit the mesh runs of a module (the indexer's modules), as the checker's decision reads it: the - // node-engine a verdict names is compared at its running commit, not at the trunk's (ADR 0548 §6); nil knows none. - Runs interface{ CommitOf(module string) string } - // HearingState is the checker's state hearing: whether it hears DEAD_LETTERS (issue 734). - HearingState StateRead - // BuildState is the builder's builds (mesh-delivery-builder.builds): the builds of a running check's candidate that have - // not ended, which the check waits on before it composes (ADR 0603 §10); without it none is named. - BuildState StateRead - // AskState is the checker's state asks: the asks each running check put (AsksState), which checker_ask_again reads. - AskState StateRead - // Backlog is the build seats' backlog (mesh-delivery-view.build_usage); checker_ask_again reads it for a check that names no ask. - Backlog func() ([]BacklogRow, error) - Now func() time.Time -} - -// VerdictRead is a verdict as stored with every failure it names, one line each, and the full report of the status of each -// pull request it judged (Report), when the candidate still holds those heads; without it the report is that of the verdict alone. -type VerdictRead struct { - Key string `json:"key"` - Verdict json.RawMessage `json:"verdict"` - // Failed is everything the verdict names as failed, one line each, in the order judged (failedLines): every member's - // failed tests, refused steps and conflicts, the manifests' refusals and the composition (the operator, 2026-11-02). - Failed []string `json:"failed,omitempty"` - // Judged says which checking program judged the verdict, in one line (judgedLine): where it was built, the key its build - // is kept under, whether this check built it, and the heads it read (issue 1921); empty for a verdict written before. - Judged string `json:"judged,omitempty"` - Reports map[string]string `json:"reports,omitempty"` - Note string `json:"note,omitempty"` -} - -func (r Reader) candidate(group string) (meshcatalog.CandidatesByGroupValue, bool) { - key, err := meshcatalogclient.CandidatesKey(group) - if err != nil || r.CandidateState.Get == nil { - return meshcatalog.CandidatesByGroupValue{}, false - } - raw, err := r.CandidateState.Get(key) - if err != nil || len(raw) == 0 { - return meshcatalog.CandidatesByGroupValue{}, false - } - c, err := meshcatalog.DecodeCandidatesByGroupValue(raw) - return c, err == nil -} - -func (r Reader) read(key string, raw json.RawMessage) (VerdictRead, error) { - out := VerdictRead{Key: key, Verdict: raw} - v, err := meshdeliverycheckerclient.DecodeVerdict(raw) - if err != nil { - return out, fmt.Errorf("%s holds what is not a verdict: %w", key, err) - } - out.Failed = failedLines(v) - out.Judged = judgedLine(v.JudgedBy) - c, ok := r.candidate(v.Group) - if !ok || (c.Heads != v.Heads && GateHeads(c) != v.Heads) { - out.Note = "the candidate no longer holds these heads" - return out, nil - } - cs := CaseOf(c, v.Trunk) - out.Reports = map[string]string{} - if cs == nil { - if IsUntrunked(v) { - for _, p := range leftOf(c, v.Trunk) { - out.Reports[pullKey(p)] = failedOf(v).Words - } - } - return out, nil - } - for _, p := range cs.Pulls { - out.Reports[pullKey(p)] = Report(v, cs, p, nil, nil, nil) - } - for _, l := range cs.Left { - out.Reports[strings.SplitN(l, ": ", 2)[0]] = strings.SplitN(l, ": ", 2)[1] - } - return out, nil -} - -// Verdicts are the verdicts the checker holds, by key; with a group only that group's. The keys are narrowed by the group -// before any value is read, at most MaxReads are read (sixteen at a time), and cut says when it stopped there. -func (r Reader) Verdicts(group string) (out []VerdictRead, cut string, err error) { - if r.VerdictState.Keys == nil || r.VerdictState.Get == nil { - return nil, "", fmt.Errorf("the verdicts state cannot be read here") - } - keys, err := r.VerdictState.Keys() - if err != nil { - return nil, "", err - } - sort.Strings(keys) - var matching []string - for _, k := range keys { - if group != "" { - if g, _, err := meshdeliverycheckerclient.ParseVerdictKey(k); err != nil || g != group { - continue - } - } - matching = append(matching, k) - } - matching, cut = capKeys(matching, "give a group to narrow the answer") - rows := make([]VerdictRead, len(matching)) - found := make([]bool, len(matching)) - if err := parallel(len(matching), func(i int) error { - raw, err := r.VerdictState.Get(matching[i]) - if err != nil || len(raw) == 0 { - return err - } - rows[i], err = r.read(matching[i], raw) - found[i] = err == nil - return err - }); err != nil { - return nil, "", err - } - for i, row := range rows { - if found[i] { - out = append(out, row) - } - } - return out, cut, nil -} - -// Pull answers for one pull request: the verdict of the candidate that holds it, with its report; for one the indexer holds -// in no candidate, that none does. -func (r Reader) Pull(repository string, number int) (VerdictRead, error) { - if r.CandidateState.Keys == nil || r.CandidateState.Get == nil { - return VerdictRead{}, fmt.Errorf("the candidates state cannot be read here") - } - keys, err := r.CandidateState.Keys() - if err != nil { - return VerdictRead{}, err - } - keys = slices.DeleteFunc(keys, meshcatalogclient.IsTryGroup) - sort.Strings(keys) - keys, cut := capKeys(keys, "") - // The candidates are read in batches, each in parallel and taken in key order, so the read stops at the batch that holds it. - for from := 0; from < len(keys); from += ReadWorkers * 4 { - batch := keys[from:min(from+ReadWorkers*4, len(keys))] - cands := make([]meshcatalog.CandidatesByGroupValue, len(batch)) - ok := make([]bool, len(batch)) - _ = parallel(len(batch), func(i int) error { - raw, err := r.CandidateState.Get(batch[i]) - if err != nil || len(raw) == 0 { - return nil - } - c, err := meshcatalog.DecodeCandidatesByGroupValue(raw) - cands[i], ok[i] = c, err == nil - return nil - }) - for i, c := range cands { - if !ok[i] { - continue - } - for _, p := range c.Pulls { - if p.Repository != repository || int(p.Number) != number { - continue - } - vkey, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - if err != nil { - return VerdictRead{}, err - } - vraw, err := r.VerdictState.Get(vkey) - if err != nil { - return VerdictRead{}, err - } - if len(vraw) == 0 { - return VerdictRead{Key: vkey, Note: "no verdict yet for the heads of its candidate"}, nil - } - return r.read(vkey, vraw) - } - } - } - if cut != "" { - return VerdictRead{}, fmt.Errorf("no candidate among the first %d holds %s#%d", MaxReads, repository, number) - } - return VerdictRead{}, fmt.Errorf("no candidate holds %s#%d", repository, number) -} - -// Kept is the kept verdicts: one key's value as stored, or with an empty key the keys of a level (or every level) and their values. -func (r Reader) Kept(level, digest string) (map[string]json.RawMessage, string, error) { - if r.KeptState.Keys == nil || r.KeptState.Get == nil { - return nil, "", fmt.Errorf("the kept state cannot be read here") - } - if digest != "" { - key, err := meshdeliverycheckerclient.KeptKey(level, digest) - if err != nil { - return nil, "", err - } - raw, err := r.KeptState.Get(key) - if err != nil { - return nil, "", err - } - if len(raw) == 0 { - return nil, "", fmt.Errorf("no kept verdict %s", key) - } - return map[string]json.RawMessage{key: raw}, "", nil - } - keys, err := r.KeptState.Keys() - if err != nil { - return nil, "", err - } - sort.Strings(keys) - var matching []string - for _, k := range keys { - if l, _, err := meshdeliverycheckerclient.ParseKeptKey(k); err == nil && (level == "" || l == level) { - matching = append(matching, k) - } - } - matching, cut := capKeys(matching, "give a level and a digest to read one") - values := make([]json.RawMessage, len(matching)) - if err := parallel(len(matching), func(i int) (err error) { - values[i], err = r.KeptState.Get(matching[i]) - return err - }); err != nil { - return nil, "", err - } - out := map[string]json.RawMessage{} - for i, k := range matching { - if len(values[i]) > 0 { - out[k] = values[i] - } - } - return out, cut, nil -} - -// Input is the answer of the seat's verb input (InputVerb): the input kept under digest, its exact bytes, which the Go build -// module hands the checking program a run ask names (buildstep.RunInput.Read). A digest that is no sha256, one the checker -// keeps nothing under, and bytes that read back as another digest are errors, never an answer. -func (r Reader) Input(digest string) (meshdeliverychecker.InputAnswer, error) { - if r.InputState.Get == nil { - return meshdeliverychecker.InputAnswer{}, fmt.Errorf("the state %s cannot be read here", InputsStateName) - } - if len(digest) != 64 || strings.Trim(digest, "0123456789abcdef") != "" { - return meshdeliverychecker.InputAnswer{}, fmt.Errorf("%q is no sha256 digest of 64 lowercase hex digits", digest) - } - raw, err := r.InputState.Get(digest) - if err != nil { - return meshdeliverychecker.InputAnswer{}, fmt.Errorf("the input %s cannot be read: %w", digest, err) - } - if len(raw) == 0 { - return meshdeliverychecker.InputAnswer{}, fmt.Errorf("the checker keeps no input %s (an input is kept a day)", digest) - } - if sum := sha256.Sum256(raw); hex.EncodeToString(sum[:]) != digest { - return meshdeliverychecker.InputAnswer{}, fmt.Errorf("the input kept under %s reads back as %d bytes of another digest", digest, len(raw)) - } - return meshdeliverycheckerclient.InputAnswerOf(raw), nil -} - -// Inputs are the checking program's kept inputs: with a digest that input as kept, else every digest with its size. -func (r Reader) Inputs(digest string) (map[string]json.RawMessage, string, error) { - if r.InputState.Keys == nil || r.InputState.Get == nil { - return nil, "", fmt.Errorf("the state %s cannot be read here", InputsStateName) - } - if digest != "" { - raw, err := r.InputState.Get(digest) - if err != nil { - return nil, "", err - } - if len(raw) == 0 { - return nil, "", fmt.Errorf("no input %s is kept", digest) - } - return map[string]json.RawMessage{digest: raw}, "", nil - } - keys, err := r.InputState.Keys() - if err != nil { - return nil, "", err - } - sort.Strings(keys) - keys, cut := capKeys(keys, "give a digest to read one") - sizes := make([]int, len(keys)) - if err := parallel(len(keys), func(i int) error { - raw, err := r.InputState.Get(keys[i]) - sizes[i] = len(raw) - return err - }); err != nil { - return nil, "", err - } - out := map[string]json.RawMessage{} - for i, k := range keys { - out[k] = json.RawMessage(fmt.Sprintf(`{"size":%d}`, sizes[i])) - } - return out, cut, nil -} - -// PreviewRead is one preview as stored, with the parts of its key. -type PreviewRead struct { - Key string `json:"key"` - Repository string `json:"repository"` - Head string `json:"head"` - Trunk string `json:"trunk"` - Preview json.RawMessage `json:"preview"` -} - -// Previews are the previews the checker holds, each as stored. With a repository only that repository's; with a head (a -// prefix of at least 7 characters is enough) only previews of that head. A key that is no preview key is listed as unreadable. -func (r Reader) Previews(repository, head string) ([]PreviewRead, []string, string, error) { - if r.PreviewState.Keys == nil || r.PreviewState.Get == nil { - return nil, nil, "", fmt.Errorf("the previews state cannot be read here") - } - keys, err := r.PreviewState.Keys() - if err != nil { - return nil, nil, "", err - } - sort.Strings(keys) - type part struct{ repo, head, trunk string } - parts := map[string]part{} - var matching, bad []string - for _, k := range keys { - repo, h, trunk, err := meshdeliverycheckerclient.ParsePreviewKey(k) - if err != nil { - bad = append(bad, k) - continue - } - if (repository != "" && repo != repository) || (head != "" && !strings.HasPrefix(h, head)) { - continue - } - parts[k] = part{repo, h, trunk} - matching = append(matching, k) - } - matching, cut := capKeys(matching, "give a repository and a head to narrow the answer") - values := make([]json.RawMessage, len(matching)) - if err := parallel(len(matching), func(i int) (err error) { - values[i], err = r.PreviewState.Get(matching[i]) - return err - }); err != nil { - return nil, nil, "", err - } - out := []PreviewRead{} - for i, k := range matching { - if len(values[i]) > 0 { - p := parts[k] - out = append(out, PreviewRead{Key: k, Repository: p.repo, Head: p.head, Trunk: p.trunk, Preview: values[i]}) - } - } - return out, bad, cut, nil -} - -// RunningStep is a step in flight on a build module. -type RunningStep struct { - Module string `json:"module"` - Node string `json:"node"` - ID string `json:"id"` - Tool string `json:"tool"` - Since string `json:"since,omitempty"` - // What says what the step is (buildstep.AskWhat): an ask's kind, repository@head and levels. - What string `json:"what,omitempty"` -} - -// LeftStep is a key a build module left running that no step behind it runs any more, and why. -type LeftStep struct { - RunningStep - Why string `json:"why"` -} - -// Status is what runs now, read from the buckets: the steps in flight on the build modules and the candidates without a -// verdict for their heads. Nothing holds a check since R4g: a check is an ask on the build seats' streams, whose backlog -// the delivery view shows (ADR 0525 §9). -type Status struct { - Running []RunningStep `json:"running_steps"` - LeftRunning []LeftStep `json:"left_running"` - RunningByNode map[string]int `json:"running_by_node"` - Unjudged []string `json:"unjudged_candidates"` - // Waiting are the candidates whose check waits, by kind (ADR 0536 §4): new (no verdict for its heads) and nodes (failed - // for the nodes, asked again when they change) start before trunk (judged on trunk heads that moved since). - Waiting map[string][]string `json:"waiting_by_kind"` - // CannotBeJudged are the candidates no pull request of which can be judged, by group, with why: the gate's source.main - // names no trunk head of their repositories (issue 1422). They are judged once it does; until then their status is error. - CannotBeJudged map[string]string `json:"cannot_be_judged"` - // CouldNotRun are the checks that could not run, by verdict key: the part not heard, since when, the running that asked - // it again, and whether it is the finding check-could-not-run (design 47 test 58). Each is asked again when its part runs. - CouldNotRun map[string]CouldNotRun `json:"could_not_run"` - // Outside are the candidates outside the mesh, by group, with why (case.go Outside): judged nothing, owed nothing. - Outside map[string]string `json:"outside_the_mesh"` - // Lost are the candidates whose verdict is running while no step of theirs can still run and their newest step ended, by - // group, with that step: nothing will judge them (the lost check of 2026-10-28); checker_ask_again asks one again. - Lost map[string]string `json:"lost"` - // BuildsNotEnded are, of each candidate whose check runs, the builder's builds of its changed modules that are neither - // done nor failed, by group: ` `, sorted (no key yet is `not begun`). A check waits on them before it - // composes (ADR 0603 §10), but for the modules its line names as composed without them; builder_build_again builds a failed one again, and the check asks itself again when it is done. - BuildsNotEnded map[string]string `json:"builds_not_ended,omitempty"` - // DeadLetters says whether the checker hears the asks the build seats give up on (issue 734): deaf, a try or check - // whose ask is given up waits until its bound. - DeadLetters DeadLettersHeard `json:"dead_letters"` - Notes []string `json:"notes,omitempty"` -} - -// DeadLettersHeard is the checker's hearing of DEAD_LETTERS as the status says it: healthy or not, and why. -type DeadLettersHeard struct { - Heard bool `json:"heard"` - Why string `json:"why"` -} - -// deadLetters reads the checker's kept hearing (buildrun.HearingHealth). -func (r Reader) deadLetters() DeadLettersHeard { - if r.HearingState.Get == nil { - return DeadLettersHeard{Why: "the checker's state hearing is not readable here"} - } - raw, err := r.HearingState.Get(buildrun.HearingKey) - if err != nil { - return DeadLettersHeard{Why: "the checker's state hearing cannot be read: " + err.Error()} - } - a := buildrun.HearingHealth(raw) - return DeadLettersHeard{Heard: a.Healthy, Why: a.Why} -} - -// couldNotRun reads the checks that could not run, by verdict key; nil when they cannot be read, said in a note. -func (r Reader) couldNotRun(st *Status) map[string]CouldNotRun { - if r.CouldNotRunState.Keys == nil || r.CouldNotRunState.Get == nil { - st.Notes = append(st.Notes, "the checker's state could-not-run is not readable here, so no check that could not run is named") - return nil - } - keys, err := r.CouldNotRunState.Keys() - if err != nil { - st.Notes = append(st.Notes, "the checker's state could-not-run cannot be listed: "+err.Error()) - return nil - } - out := map[string]CouldNotRun{} - for _, k := range keys { - var c CouldNotRun - if raw, err := r.CouldNotRunState.Get(k); err == nil && json.Unmarshal(raw, &c) == nil { - out[k] = c - } - } - return out -} - -// Status reads the build modules' steps and the candidates against their verdicts. -func (r Reader) Status() (Status, error) { - st := Status{RunningByNode: map[string]int{}, Lost: map[string]string{}} - read := r.readSteps() - st.Running, st.LeftRunning, st.Notes = read.running, read.left, read.notes - for _, rs := range st.Running { - st.RunningByNode[rs.Node]++ - } - if r.HaveState.Get == nil { - st.Notes = append(st.Notes, "the node-engine's have is not readable here, so a step is not checked against its build module being on its node") - } - if len(st.LeftRunning) > 0 { - st.Notes = append(st.Notes, "left_running are keys a build module left running (it stopped, restarted or was unassigned before it wrote the step's end): the checker's wait for each ended at the step bound, so none holds a place; the build module never ends them") - } - st.Unjudged = []string{} - st.Waiting = map[string][]string{KindNew: {}, KindNodes: {}, KindTrunk: {}} - st.CannotBeJudged, st.Outside = map[string]string{}, map[string]string{} - var heads map[string]string - if r.Trunks == nil { - st.Notes = append(st.Notes, "the trunk heads are not readable here, so no check is named as waiting for a trunk move") - } else if h, err := r.Trunks(); err != nil { - st.Notes = append(st.Notes, "the trunk heads cannot be read, so no check is named as waiting for a trunk move: "+err.Error()) - } else { - heads = h - } - if r.CandidateState.Keys != nil && r.CandidateState.Get != nil && r.VerdictState.Get != nil { - keys, err := r.CandidateState.Keys() - if err != nil { - return st, err - } - keys = slices.DeleteFunc(keys, meshcatalogclient.IsTryGroup) // a try's candidate is judged by its try, never as a check - sort.Strings(keys) - total := len(keys) - keys, cut := capKeys(keys, "") - if cut != "" { - st.Notes = append(st.Notes, fmt.Sprintf("read the first %d candidates of %d", MaxReads, total)) - } - type judged struct { - c meshcatalog.CandidatesByGroupValue - raw json.RawMessage - ok bool - } - rows := make([]judged, len(keys)) - _ = parallel(len(keys), func(i int) error { - raw, err := r.CandidateState.Get(keys[i]) - if err != nil || len(raw) == 0 { - return nil - } - c, err := meshcatalog.DecodeCandidatesByGroupValue(raw) - if err != nil { - return nil - } - vkey, err := meshdeliverycheckerclient.VerdictKey(c.Group, c.Heads) - if err != nil { - return nil - } - rows[i].c = c - if vraw, err := r.VerdictState.Get(vkey); err == nil { - rows[i].raw, rows[i].ok = vraw, true - } - return nil - }) - for _, row := range rows { - if !row.ok { - continue - } - c, raw := row.c, row.raw - if Outside(c, heads) { - st.Outside[c.Group] = OutsideWords - continue - } - if heads != nil && len(leftOf(c, heads)) > 0 { - // Nothing of it can be judged now, whatever its verdict says (issue 1422): never counted as waiting. - var repos []string - for _, p := range leftOf(c, heads) { - repos = append(repos, p.Repository) - } - st.CannotBeJudged[c.Group] = UntrunkedWordsOf(repos) - continue - } - if len(raw) == 0 { - st.Unjudged = append(st.Unjudged, c.Group) - st.Waiting[KindNew] = append(st.Waiting[KindNew], c.Group) - continue - } - v, err := meshdeliverycheckerclient.DecodeVerdict(raw) - switch { - case err != nil: - case v.State == meshdeliverycheckerclient.VerdictRunning: - if why := read.lostWhy(c); why != "" { - st.Lost[c.Group] = why - } - if words := r.buildsNotEnded(c); words != "" { - if st.BuildsNotEnded == nil { - st.BuildsNotEnded = map[string]string{} - } - st.BuildsNotEnded[c.Group] = words - } - case IsCouldNotRun(v): - st.Waiting[KindNodes] = append(st.Waiting[KindNodes], c.Group) - case IsUntrunked(v): - // Its trunk head is known now: it waits for its whole check. - st.Waiting[KindNew] = append(st.Waiting[KindNew], c.Group) - case heads != nil: - if cs := CaseOf(c, heads); cs != nil && (!sameTrunk(v.Trunk, cs.Trunk) || besideMoved(v.Beside, besideAt(heads, r.Runs))) { - st.Waiting[KindTrunk] = append(st.Waiting[KindTrunk], c.Group) - } - } - } - } - st.DeadLetters = r.deadLetters() - st.CouldNotRun = r.couldNotRun(&st) - st.Notes = append(st.Notes, "nothing holds the checker and it holds no place: running_steps are the build modules' keys that can still be running (started within the step bound, their build module running on the node); a candidate without a verdict has its check ask in a build seat's stream, waiting for a node with a free slot, whose backlog the delivery view shows (ADR 0525 §4, §9)") - return st, nil -} - -// stepsRead is what the build modules' steps states hold: the steps that can still run, those left running, and every -// ask step read, by module, for the candidate it belongs to (stepsOf). -type stepsRead struct { - running []RunningStep - left []LeftStep - all []moduleStep - notes []string -} - -type moduleStep struct { - module string - step buildstep.Step -} - -// readSteps reads the newest MaxReads keys of each build module's steps state. -func (r Reader) readSteps() stepsRead { - out := stepsRead{running: []RunningStep{}, left: []LeftStep{}} - var states map[string]StateRead - if r.StepStates != nil { - states = r.StepStates() - } - mods := make([]string, 0, len(states)) - for m := range states { - mods = append(mods, m) - } - sort.Strings(mods) - now, bound := time.Now(), r.StepBound - if r.Now != nil { - now = r.Now() - } - if bound <= 0 { - bound = DefaultStepBound - } - for _, m := range mods { - sr := states[m] - if sr.Keys == nil || sr.Get == nil { - continue - } - keys, err := sr.Keys() - if err != nil { - out.notes = append(out.notes, "the steps of "+m+" cannot be listed: "+err.Error()) - continue - } - // A step that runs started lately: the newest ids are read first, at most MaxReads of them. - newestFirst(keys) - keys, cut := capKeys(keys, "") - if cut != "" { - out.notes = append(out.notes, "the steps of "+m+": read the newest "+fmt.Sprint(MaxReads)+" of its keys; a step older than those is not shown") - } - steps := make([]buildstep.Step, len(keys)) - decoded := make([]bool, len(keys)) - _ = parallel(len(keys), func(i int) error { - raw, err := sr.Get(keys[i]) - if err != nil || len(raw) == 0 { - return nil - } - step, err := buildstep.DecodeStep(raw) - steps[i], decoded[i] = step, err == nil - return nil - }) - for i := range keys { - if !decoded[i] { - continue - } - step := steps[i] - out.all = append(out.all, moduleStep{module: m, step: step}) - if step.State != buildstep.StepRunning { - continue - } - rs := RunningStep{Module: m, Node: step.Node, ID: step.ID, Tool: step.Tool, What: step.What} - if !step.Started.IsZero() { - rs.Since = step.Started.UTC().Format("2006-01-02T15:04:05Z") - } - if why := r.leftWhy(m, step, now, bound); why != "" { - out.left = append(out.left, LeftStep{RunningStep: rs, Why: why}) - continue - } - out.running = append(out.running, rs) - } - } - return out -} - -// asksOf says whether a step is an ask of one of the candidate's pull requests: its What names `@` -// (buildstep.AskWhat). -func asksOf(c meshcatalog.CandidatesByGroupValue, what string) bool { - for _, p := range c.Pulls { - if p.Repository != "" && strings.Contains(what, p.Repository+"@"+short12(p.Head)) { - return true - } - } - return false -} - -// lostWhy says why a candidate whose verdict is running has a check that nothing will judge, or "": no step of its pull -// requests can still run, and its newest one ended. -func (s stepsRead) lostWhy(c meshcatalog.CandidatesByGroupValue) string { - for _, rs := range s.running { - if asksOf(c, rs.What) { - return "" - } - } - var last *buildstep.Step - for i := range s.all { - st := s.all[i].step - if buildstep.Ended(st) && asksOf(c, st.What) && (last == nil || st.Ended.After(last.Ended)) { - last = &s.all[i].step - } - } - if last == nil { - return "" // its ask waits in its build seat's stream: no node took it yet - } - return fmt.Sprintf("its verdict is running, but no step of it runs: its last step %s on %s ended %s at %s and no verdict followed; %s asks it again", - last.ID, last.Node, last.State, last.Ended.UTC().Format(time.RFC3339), AskAgainTool) -} - -// DefaultStepBound is how long one check ask's step may run once a build node took it (its key written running; the fetch, -// the digests and the levels), never counting its wait in the stream; past it the check fails naming the node. -const DefaultStepBound = 30 * time.Minute - -// leftWhy says why a key in the running state runs no step any more, or "" when it can still be running: its build module -// is not on its node or is not running there (the node-engine's have), or it started longer ago than the step bound, when -// the checker stopped waiting for it. -func (r Reader) leftWhy(module string, step buildstep.Step, now time.Time, bound time.Duration) string { - if r.HaveState.Get != nil { - key, err := nodeengineclient.HaveKey(step.Node, module) - if err == nil { - raw, err := r.HaveState.Get(key) - switch { - case err != nil: - case len(raw) == 0: - return fmt.Sprintf("%s is not on %s", module, step.Node) - default: - if h, err := nodeengineclient.DecodeHave(raw); err == nil && h.Phase != nodeengineclient.PhaseRunning { - return fmt.Sprintf("%s on %s is %s", module, step.Node, h.Phase) - } - } - } - } - if step.Started.IsZero() { - return fmt.Sprintf("it says no start, so no wait of the checker's (bound %s) can be behind it", bound) - } - if end := step.Started.Add(bound); now.After(end) { - return fmt.Sprintf("started %s, past the step bound of %s: the checker stopped waiting for it at %s", step.Started.UTC().Format(time.RFC3339), bound, end.UTC().Format(time.RFC3339)) - } - return "" -} - -// buildsNotEnded are the builder's builds of the candidate's changed modules that are neither done nor failed, as -// Status.BuildsNotEnded says them; empty when none, or when the builder's builds are not readable here. -func (r Reader) buildsNotEnded(c meshcatalog.CandidatesByGroupValue) string { - if r.BuildState.Get == nil { - return "" - } - var out []string - for _, p := range c.Pulls { - for module, cm := range p.Fps { - if cm.Fp2 == "" || cm.Manifest == "" { - continue - } - key, err := builds.BuilderBuildKey(module, cm.Fp2) - if err != nil { - continue - } - raw, err := r.BuildState.Get(key) - if err != nil { - continue - } - state := "not begun" - if len(raw) > 0 { - b, err := builds.DecodeBuilderBuild(raw) - if err != nil || b.State == builds.BuildDone || b.State == builds.BuildFailed { - continue - } - state = b.State - } - out = append(out, module+" "+state) - } - } - sort.Strings(out) - return strings.Join(slices.Compact(out), ", ") -} - -// judgedLine is a verdict's JudgedBy in one line, as the verdict tools answer it. -func judgedLine(by *meshdeliverychecker.VerdictsByGroupHeadsValueJudgedBy) string { - if by == nil { - return "" - } - built := "kept" - if by.Built { - built = "built for this check" - } - heads := make([]string, len(by.Heads)) - for i, h := range by.Heads { - heads[i] = h.Repository + " " + h.Commit - } - return fmt.Sprintf("by the checking program %s of %s at %s (%s), %s as %s, over %s", by.Program.Package, by.Program.Repository, - by.Program.Commit, by.Program.Dir, built, by.Key, strings.Join(heads, ", ")) -} diff --git a/modules/mesh-delivery-checker/internal/check/read_test.go b/modules/mesh-delivery-checker/internal/check/read_test.go deleted file mode 100644 index 1ba52ccd..00000000 --- a/modules/mesh-delivery-checker/internal/check/read_test.go +++ /dev/null @@ -1,446 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "fmt" - "strings" - "sync/atomic" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - buildsrec "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -type fakeState map[string][]byte - -func (f fakeState) read() StateRead { - return StateRead{ - Keys: func() ([]string, error) { - var k []string - for key := range f { - k = append(k, key) - } - return k, nil - }, - Get: func(key string) (json.RawMessage, error) { - if v, ok := f[key]; ok { - return json.RawMessage(v), nil - } - return nil, nil - }, - } -} - -// readerOf is a reader over a verdict the checker wrote through its own code and the candidate it judged. -func readerOf(t *testing.T) (Reader, string) { - t.Helper() - r := newRig(t, passJudge()) - r.setTrunks(t, map[string]string{repoA: trunkA, repoSDK: trunkS, hqRepo: trunkA2}) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1), pullOf(hqRepo, 3, head2)) - r.ch.Candidate(context.Background(), "feat%2Fx", cand) - r.ch.Wait() - ckey, _ := meshcatalogclient.CandidatesKey("feat/x") - verdicts, kept := fakeState{}, fakeState{} - for k, v := range r.verdicts.vals { - verdicts[k] = v - } - if _, err := (Kept{Store: r.verdicts}).Keep(context.Background(), "unit", digA, keptValue("m/a", 3)); err != nil { - t.Fatal(err) - } - kept["unit."+digA] = r.verdicts.vals["unit."+digA] - return Reader{VerdictState: verdicts.read(), KeptState: kept.read(), CandidateState: fakeState{ckey: cand}.read()}, keyOf(t, "feat/x", cand) -} - -func TestTheVerdictToolsAnswerTheStoredVerdictWithTheFullReportOfEachPullRequest(t *testing.T) { // N9/AC1, 1686 - rd, key := readerOf(t) - vs, _, err := rd.Verdicts("") - if err != nil || len(vs) != 1 || vs[0].Key != key { - t.Fatalf("verdicts = %+v, %v", vs, err) - } - if _, err := meshdeliverycheckerclient.DecodeVerdict(vs[0].Verdict); err != nil { - t.Errorf("the verdict is not as stored: %v", err) - } - if got := vs[0].Reports["novox/mesh-delivery#7"]; !strings.HasPrefix(got, "pass 111111111111 on aaaaaaaaaaaa") || !strings.Contains(got, "composition not yet run") { - t.Errorf("reports = %v", vs[0].Reports) - } - if vs[0].Judged != "" { - t.Errorf("judged = %q of a verdict with no JudgedBy", vs[0].Judged) - } - if only, _, _ := rd.Verdicts("feat/other"); len(only) != 0 { - t.Errorf("another group's verdicts = %+v", only) - } - // A failed verdict is answered with every failure it names, one line each (the operator, 2026-11-02). - raw, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: "feat/x", Heads: "0123456789ab", State: meshdeliverycheckerclient.VerdictFail, - Failed: &FailedFirst{Test: "unit: m/a.TestA", Words: "a_test.go:3: boom\nmore"}, - Failures: failureItems([]FailedFirst{{Test: "unit: m/a.TestA", Words: "a_test.go:3: boom"}, {Machine: "g14", Words: "route-proxy is defined twice"}}), - // Issue 1921: it names the checking program that judged it, its build and the heads it read. - JudgedBy: judgedByOf(buildstep.RunProgram{Repository: repoA, Commit: head1, Dir: CheckingProgramDir, Package: CheckingProgramPackage}, - buildstep.RunAnswer{Key: "program-0123456789abcdef", Built: true}, []buildstep.RunHead{{Repository: repoA, Commit: head1}})}) - want := "by the checking program " + CheckingProgramPackage + " of " + repoA + " at " + head1 + " (" + CheckingProgramDir + - "), built for this check as program-0123456789abcdef, over " + repoA + " " + head1 - if got, _ := rd.read(key, raw); strings.Join(got.Failed, " | ") != "unit: m/a.TestA: a_test.go:3: boom | g14: route-proxy is defined twice" || got.Judged != want { - t.Errorf("failed = %q, judged %q, want %q", got.Failed, got.Judged, want) - } -} - -// 1887/A10: an hq pull request is answered as any other, its report its verdict's (ADR 0527 §2), never a note that a script -// judges it. -func TestAnHqPullRequestIsAnsweredAsAnyOther(t *testing.T) { - rd, _ := readerOf(t) - hq, err := rd.Pull(hqRepo, 3) - if err != nil || hq.Note != "" || hq.Verdict == nil { - t.Errorf("hq pull = %+v, %v", hq, err) - } - p, err := rd.Pull(repoA, 7) - if err != nil || p.Note != "" || !strings.HasPrefix(p.Reports["novox/mesh-delivery#7"], "pass ") { - t.Errorf("pull = %+v, %v", p, err) - } - if _, err := rd.Pull(repoA, 99); err == nil { - t.Error("a pull request no candidate holds was answered") - } -} - -func TestTheKeptToolAnswersTheKeysAsStoredByLevelAndByDigest(t *testing.T) { // N9/AC1 - rd, _ := readerOf(t) - all, _, err := rd.Kept("", "") - if err != nil || len(all) != 1 { - t.Fatalf("kept = %v, %v", all, err) - } - one, _, err := rd.Kept("unit", digA) - if err != nil || len(one) != 1 { - t.Fatalf("one kept = %v, %v", one, err) - } - for _, raw := range one { - if k, err := meshdeliverycheckerclient.DecodeKept(raw); err != nil || k.Package != "m/a" { - t.Errorf("kept value = %+v, %v", k, err) - } - } - if none, _, _ := rd.Kept("integration", ""); len(none) != 0 { - t.Errorf("integration = %v", none) - } - if _, _, err := rd.Kept("unit", digB); err == nil { - t.Error("a digest nothing kept was answered") - } - // Issue 1921: checker_inputs answers the checking program's inputs, every digest with its size, or one as kept. - inputs := newMemKV() - body := []byte(`{"v":1,"group":"feat/x"}`) - in, _ := buildstep.RunInputOf(InputVerb, body) - _ = memInputs{inputs}.Keep(context.Background(), in.Digest, body) - rd.InputState = kvRead(inputs) - if all, _, err := rd.Inputs(""); err != nil || string(all[in.Digest]) != fmt.Sprintf(`{"size":%d}`, len(body)) { - t.Errorf("inputs = %s, %v", all, err) - } - if one, _, err := rd.Inputs(in.Digest); err != nil || string(one[in.Digest]) != string(body) { - t.Errorf("one input = %s, %v", one, err) - } - if _, _, err := rd.Inputs(strings.Repeat("0", 64)); err == nil { - t.Error("an input nothing kept was answered") - } - // The operator's answer on B0: the seat's verb input answers the exact bytes, which the build module's read takes; - // a digest that is none, one nothing is kept under, and bytes kept under another digest are errors. - if got, err := meshdeliverycheckerclient.ReadInput(func(key string, b any) (json.RawMessage, error) { - if key != "seat:mesh-delivery-checker.input" { - return nil, fmt.Errorf("asked %s", key) - } - a, err := rd.Input(b.(meshdeliverychecker.InputInput).Digest) - if err != nil { - return nil, err - } - return json.Marshal(a) - }, in.Digest); err != nil || string(got) != string(body) { - t.Errorf("the input through the verb = %s, %v", got, err) - } - _, _ = inputs.Create(context.Background(), strings.Repeat("1", 64), []byte(`{"v":2}`)) - for _, digest := range []string{"d1", strings.Repeat("0", 64), strings.Repeat("1", 64)} { - if a, err := rd.Input(digest); err == nil { - t.Errorf("the verb answered %s: %+v", digest, a) - } - } -} - -// 1680 live acceptance: checker_previews shows the preview key of an open pull request, as stored, narrowed by repository and head. -func TestThePreviewsToolShowsTheStoredPreviewOfAPullRequest(t *testing.T) { - pv := meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue{Repository: repoA, Head: head1, Trunk: head2, Merge: "m1", Modules: map[string]meshdeliverychecker.PreviewsByRepositoryHeadTrunkValueModulesValue{}} - data, err := meshdeliverycheckerclient.EncodePreview(pv) - if err != nil { - t.Fatal(err) - } - key, _ := meshdeliverycheckerclient.PreviewKey(repoA, head1, head2) - rd := Reader{PreviewState: fakeState{key: data, "junk": []byte("{}")}.read()} - got, bad, _, err := rd.Previews(repoA, head1[:8]) - if err != nil || len(got) != 1 || got[0].Key != key || got[0].Head != head1 || got[0].Trunk != head2 || string(got[0].Preview) != string(data) { - t.Fatalf("previews = %+v, %v", got, err) - } - if len(bad) != 1 || bad[0] != "junk" { - t.Errorf("unreadable = %v", bad) - } - if got, _, _, _ := rd.Previews("novox/other", ""); len(got) != 0 { - t.Errorf("another repository's = %+v", got) - } - if _, _, _, err := (Reader{}).Previews("", ""); err == nil { - t.Error("answered with no state to read") - } -} - -// checker_status reads the steps in flight by node and the candidates without a verdict; since R4g nothing holds the checker -// and it has no capacity of its own (ADR 0525 §4, §7; 1685 closed by the hold's deletion). -func TestTheStatusToolReadsRunningSteps(t *testing.T) { - running, _ := buildstep.EncodeStep(buildstep.Step{ID: "abc1", Tool: "build_go_test", Node: "shanks", State: buildstep.StepRunning, Started: time.Date(2026, 10, 22, 15, 0, 0, 0, time.UTC)}) - passed, _ := buildstep.EncodeStep(buildstep.Step{ID: "abc2", Tool: "build_go_test", Node: "ace", State: buildstep.StepPassed}) - cand := candidateOf(t, "feat/x", pullOf(repoA, 7, head1)) - ckey, _ := meshcatalogclient.CandidatesKey("feat/x") - // ADR 0603 §10: feat/y's check runs and waits on the builds of its changed modules: web building, mail not begun; api - // is done and named nowhere. - fp, man := "src2:"+strings.Repeat("f", 64), strings.Repeat("e", 64) - py := pullOf(repoA, 8, head2) - py.Modules, py.Fps = []string{"api", "mail", "web"}, map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"web": {Fp2: fp, Manifest: man}, "mail": {Fp2: fp, Manifest: man}, "api": {Fp2: fp, Manifest: man}} - candY := candidateOf(t, "feat/y", py) - ykey, _ := meshcatalogclient.CandidatesKey("feat/y") - runningV, _ := meshdeliverycheckerclient.EncodeVerdict(meshdeliverychecker.VerdictsByGroupHeadsValue{Group: "feat/y", Heads: "0123456789ab", State: meshdeliverycheckerclient.VerdictRunning}) - builds := fakeState{} - for module, state := range map[string]string{"web": buildsrec.BuildBuilding, "api": buildsrec.BuildDone} { - k, _ := buildsrec.BuilderBuildKey(module, fp) - builds[k], _ = json.Marshal(buildsrec.BuilderBuild{V: 1, State: state}) - } - rd := Reader{ - StepStates: stepStatesOf(map[string]StateRead{"mesh-build-go": fakeState{"shanks.abc1": running, "ace.abc2": passed}.read()}), - CandidateState: fakeState{ckey: cand, ykey: candY}.read(), - VerdictState: fakeState{keyOf(t, "feat/y", candY): runningV}.read(), - BuildState: builds.read(), - HaveState: fakeState{"shanks.mesh-build-go": haveOf(t, nodeengineclient.PhaseRunning)}.read(), - HearingState: fakeState{"dead-letters": []byte(`{"heard":false,"streams":["SEAT_NODE_BUILD_GO"],` + - `"why":"DEAD_LETTERS is not heard, so an ask a build seat gives up on is waited for until its bound: SEAT_NODE_BUILD_GO: permissions violation",` + - `"at":"2026-10-26T13:00:00Z"}`)}.read(), - Now: func() time.Time { return time.Date(2026, 10, 22, 15, 10, 0, 0, time.UTC) }, - } - st, err := rd.Status() - if err != nil { - t.Fatal(err) - } - if len(st.Running) != 1 || st.Running[0].Node != "shanks" || st.RunningByNode["shanks"] != 1 || st.RunningByNode["ace"] != 0 { - t.Errorf("running = %+v %v", st.Running, st.RunningByNode) - } - if len(st.Unjudged) != 1 || st.Unjudged[0] != "feat/x" { - t.Errorf("unjudged = %v", st.Unjudged) - } - if got := st.BuildsNotEnded; len(got) != 1 || got["feat/y"] != "mail not begun, web building" { - t.Errorf("builds not ended = %v, want feat/y's mail not begun and web building", got) - } - // Issue 734, review round 1: a checker that does not hear DEAD_LETTERS says so in its status, with why. - if st.DeadLetters.Heard || !strings.Contains(st.DeadLetters.Why, "permissions violation") { - t.Errorf("dead letters = %+v, want not heard, saying why", st.DeadLetters) - } - // What cannot be read is said, not guessed. - st, err = Reader{}.Status() - if err != nil || len(st.Notes) == 0 || st.DeadLetters.Heard { - t.Errorf("empty status = %+v, %v", st, err) - } -} - -func haveOf(t *testing.T, phase string) []byte { - t.Helper() - raw, err := nodeengineclient.EncodeHave(nodeengine.HaveByNodeModuleValue{Phase: phase, Health: nodeengineclient.HealthOK}) - if err != nil { - t.Fatal(err) - } - return raw -} - -func stepOf(t *testing.T, id, node string, started time.Time) []byte { - t.Helper() - raw, err := buildstep.EncodeStep(buildstep.Step{ID: id, Tool: "build_go_fetch", Node: node, State: buildstep.StepRunning, Started: started}) - if err != nil { - t.Fatal(err) - } - return raw -} - -// 1890: a key a build module left running (it stopped, restarted or was unassigned before it wrote the step's end) holds -// no place in the checker, whose wait for it ended at the step bound; the status says it is left running and why, and -// counts only the steps that can still run. -func TestTheStatusCountsOnlyStepsThatCanStillRunAndSaysWhyTheOthersAreLeft(t *testing.T) { // 1890/AC2, AC3 - now := time.Date(2026, 10, 23, 7, 0, 0, 0, time.UTC) - rd := Reader{ - StepStates: stepStatesOf(map[string]StateRead{ - "mesh-build-go": fakeState{ - "g14.fresh1": stepOf(t, "fresh1", "g14", now.Add(-5*time.Minute)), - "g14.old1": stepOf(t, "old1", "g14", now.Add(-33*time.Hour)), - "ace.gone1": stepOf(t, "gone1", "ace", now.Add(-2*time.Minute)), - "shanks.down1": stepOf(t, "down1", "shanks", now.Add(-time.Minute)), - }.read(), - }), - HaveState: fakeState{ - "g14.mesh-build-go": haveOf(t, nodeengineclient.PhaseRunning), - "shanks.mesh-build-go": haveOf(t, nodeengineclient.PhaseFailed), - }.read(), - StepBound: 30 * time.Minute, - Now: func() time.Time { return now }, - } - st, err := rd.Status() - if err != nil { - t.Fatal(err) - } - if len(st.Running) != 1 || st.Running[0].ID != "fresh1" || len(st.RunningByNode) != 1 || st.RunningByNode["g14"] != 1 { - t.Fatalf("running = %+v by node %v", st.Running, st.RunningByNode) - } - why := map[string]string{} - for _, l := range st.LeftRunning { - why[l.ID] = l.Why - } - if len(why) != 3 { - t.Fatalf("left running = %+v", st.LeftRunning) - } - for id, want := range map[string]string{"old1": "past the step bound of 30m0s", "gone1": "mesh-build-go is not on ace", "down1": "mesh-build-go on shanks is failed"} { - if !strings.Contains(why[id], want) { - t.Errorf("why %s = %q, want it to say %q", id, why[id], want) - } - } -} - -// 1887/A10: a group of hq's pull requests alone is judged by the checker since R4g, so without a verdict it is unjudged. -func TestTheStatusCountsAnHQOnlyGroupAsUnjudged(t *testing.T) { - cand := candidateOf(t, "pb-x", pullOf(hqRepo, 3, head2)) - ckey, _ := meshcatalogclient.CandidatesKey("pb-x") - st, err := Reader{CandidateState: fakeState{ckey: cand}.read(), VerdictState: fakeState{}.read()}.Status() - if err != nil { - t.Fatal(err) - } - if len(st.Unjudged) != 1 || st.Unjudged[0] != "pb-x" { - t.Errorf("unjudged = %v, want pb-x", st.Unjudged) - } - // A check that could not run is named with the part not heard (design 47 test 58). - rec := json.RawMessage(`{"v":1,"group":"feat/x","heads":"0123456789ab","part":{"node":"g14","module":"forge"},"since":"2026-11-09T20:00:00Z"}`) - st, err = Reader{CandidateState: fakeState{}.read(), VerdictState: fakeState{}.read(), CouldNotRunState: fakeState{"feat/x.0123456789ab": rec}.read()}.Status() - if err != nil { - t.Fatal(err) - } - if c := st.CouldNotRun["feat/x.0123456789ab"]; c.Part != (Part{Node: "g14", Module: "forge"}) { - t.Errorf("could_not_run = %+v, want the check of feat/x naming forge on g14", st.CouldNotRun) - } -} - -// slowBucket wraps a state with a Get that takes delay and counts the values read, the lists and the most Gets in flight. -type slowBucket struct { - vals fakeState - reads, lists atomic.Int64 - inFlight, maxFlight atomic.Int64 -} - -func (b *slowBucket) get(key string) (json.RawMessage, error) { - b.reads.Add(1) - now := b.inFlight.Add(1) - for m := b.maxFlight.Load(); now > m && !b.maxFlight.CompareAndSwap(m, now); m = b.maxFlight.Load() { - } - time.Sleep(time.Millisecond) - b.inFlight.Add(-1) - return b.vals.read().Get(key) -} - -func (b *slowBucket) read() StateRead { - return StateRead{Keys: func() ([]string, error) { b.lists.Add(1); return b.vals.read().Keys() }, Get: b.get} -} - -func (b *slowBucket) Keys() ([]string, error) { return b.read().Keys() } -func (b *slowBucket) Get(key string) (json.RawMessage, error) { return b.get(key) } -func (b *slowBucket) Create(string, []byte) error { return nil } - -// 1827: a read tool lists the keys of a bucket of thousands and reads only the values its answer needs, sixteen at a time, at -// most MaxReads, saying when it cut (on the live shape of 2026-10-26: kept 7990 keys, steps 10034, tries 1000s). -func TestReadToolsReadOnlyWhatTheyNeedInParallelAndSayWhenCut(t *testing.T) { // 1827 - kept := fakeState{} - for i := 0; i < 1800; i++ { - level := map[bool]string{true: "unit", false: "integration"}[i%9 != 0] - k, _ := meshdeliverycheckerclient.KeptKey(level, fmt.Sprintf("%064x", i)) - kept[k] = []byte(`{"v":1}`) - } - previews := fakeState{} - for i := 0; i < 1500; i++ { - repo := map[bool]string{true: repoA, false: repoSDK}[i < 300] - k, _ := meshdeliverycheckerclient.PreviewKey(repo, fmt.Sprintf("%08x%032x", i, 0), trunkA) - previews[k] = []byte(`{"v":1}`) - } - for _, row := range []struct { - name string - call func(*slowBucket) (rows int, cut string, err error) - rows int - reads, lists int64 - cut bool - }{ - {"kept of one key", func(b *slowBucket) (int, string, error) { - m, c, err := Reader{KeptState: b.read()}.Kept("unit", fmt.Sprintf("%064x", 1)) - return len(m), c, err - }, 1, 1, 0, false}, - {"kept of a level", func(b *slowBucket) (int, string, error) { - m, c, err := Reader{KeptState: b.read()}.Kept("integration", "") - return len(m), c, err - }, 200, 200, 1, false}, - {"kept of everything", func(b *slowBucket) (int, string, error) { - m, c, err := Reader{KeptState: b.read()}.Kept("", "") - return len(m), c, err - }, MaxReads, MaxReads, 1, true}, - {"previews of a repository", func(b *slowBucket) (int, string, error) { - p, _, c, err := Reader{PreviewState: b.read()}.Previews(repoSDK, "") - return len(p), c, err - }, 1200, 1000, 1, true}, - {"previews of a repository and a head", func(b *slowBucket) (int, string, error) { - p, _, c, err := Reader{PreviewState: b.read()}.Previews(repoA, fmt.Sprintf("%08x", 7)) - return len(p), c, err - }, 1, 1, 1, false}, - } { - b := &slowBucket{} - b.vals = kept - if strings.HasPrefix(row.name, "previews") { - b.vals = previews - } - start := time.Now() - rows, cut, err := row.call(b) - if err != nil { - t.Fatalf("%s: %v", row.name, err) - } - if rows != min(row.rows, MaxReads) || b.reads.Load() != row.reads || b.lists.Load() != row.lists || (cut != "") != row.cut { - t.Errorf("%s: %d rows, %d reads, %d lists, cut %q; want %d, %d, %d, cut %v", row.name, rows, b.reads.Load(), b.lists.Load(), cut, min(row.rows, MaxReads), row.reads, row.lists, row.cut) - } - if row.reads > 20 && b.maxFlight.Load() < 2 { - t.Errorf("%s: values were read one at a time", row.name) - } - t.Logf("%s: %d values in %v (one after the other: %v)", row.name, b.reads.Load(), time.Since(start), time.Duration(b.reads.Load())*time.Millisecond) - } - - // The status reads the newest steps of a bucket of 1500 first, at most MaxReads, and finds the one that runs. - now := time.Date(2026, 10, 26, 7, 0, 0, 0, time.UTC) - steps := fakeState{} - for i := 0; i < 1500; i++ { - id := fmt.Sprintf("mvo5%04dabcdef", i) - step := buildstep.Step{ID: id, Tool: "t", Node: "g14", State: buildstep.StepPassed} - if i == 1499 { - step.State, step.Started = buildstep.StepRunning, now.Add(-time.Minute) - } - raw, _ := buildstep.EncodeStep(step) - steps["g14."+id] = raw - } - sb := &slowBucket{vals: steps} - st, err := Reader{StepStates: stepStatesOf(map[string]StateRead{"mesh-build-go": sb.read()}), HaveState: fakeState{"g14.mesh-build-go": haveOf(t, nodeengineclient.PhaseRunning)}.read(), - Now: func() time.Time { return now }}.Status() - if err != nil || sb.reads.Load() != MaxReads || len(st.Running) != 1 || sb.maxFlight.Load() < 2 { - t.Errorf("status: %d reads (want %d), running %+v, in flight %d, %v", sb.reads.Load(), MaxReads, st.Running, sb.maxFlight.Load(), err) - } - - // A new try reads the newest TryScan of 1500 ended tries, not all, and still finds the same head asked twice. - tries := fakeState{} - for i := 0; i < 1500; i++ { - raw, _ := encodeTry(Try{ID: fmt.Sprintf("try-%d", 1700000000000000000+i), Repository: repoA, Head: head1, Trunk: trunkA, State: TryPass}) - tries[fmt.Sprintf("try-%d", 1700000000000000000+i)] = raw - } - tb := &slowBucket{vals: tries} - if _, err := AskTry(tb, tryTrunks, TryAsk{Repository: repoA, Head: head2, Trunk: trunkA}, now); err != nil || tb.reads.Load() != TryScan { - t.Errorf("AskTry read %d of 1500 tries (want %d), %v", tb.reads.Load(), TryScan, err) - } -} diff --git a/modules/mesh-delivery-checker/internal/check/renames.go b/modules/mesh-delivery-checker/internal/check/renames.go deleted file mode 100644 index fee28b0f..00000000 --- a/modules/mesh-delivery-checker/internal/check/renames.go +++ /dev/null @@ -1,100 +0,0 @@ -package check - -import ( - "context" - "errors" - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-contracts/go/seats/meshconfig" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// A config value renamed (design 32 *A config value renamed*, DLV-C5): the checker asks the seat mesh-config's verb -// renames, never the module, for every change that brings `renamed-from` or drops one. A holder that does not serve the -// verb refuses `renamed-from`, naming that; while a rename's window is open, dropping its `renamed-from` is refused, -// naming the nodes holding it open; a check that cannot ask fails, saying so. - -// RenamesSeat is the verb the checker asks, as its manifest's `invokes` names it. -var RenamesSeat = rules.RenamesSeat - -// ErrRenamesNotServed is the answer of Renames when the seat's holder serves no verb renames. -var ErrRenamesNotServed = errors.New("the holder of the seat " + meshconfig.Seat + " does not serve its verb " + meshconfig.VerbRenames) - -// Renames asks the seat mesh-config's verb renames for a module's open renames. -type Renames interface { - Renames(ctx context.Context, module string) (meshconfig.RenamesAnswer, error) -} - -// renameChange is what a pull request's head does to a module's `renamed-from` against the trunk: the declarations it -// brings (key from old name) and the ones it drops (key, old name). -type renameChange struct { - brought, dropped map[string]string -} - -// renamesOf compares a module's config declarations at the trunk and at the head. -func renamesOf(was, now catalogue.Manifest) renameChange { - rc := renameChange{brought: map[string]string{}, dropped: map[string]string{}} - for key, d := range now.Settings { - if d.RenamedFrom != "" && was.Settings[key].RenamedFrom != d.RenamedFrom { - rc.brought[key] = d.RenamedFrom - } - } - for key, d := range was.Settings { - if d.RenamedFrom != "" && now.Settings[key].RenamedFrom != d.RenamedFrom { - rc.dropped[key] = d.RenamedFrom - } - } - return rc -} - -// judgeRenames records, of each module a member changes that brings or drops a `renamed-from`, what it brings and drops -// and how the seat mesh-config's verb renames answered for it, as a fact the rules judge (rules.JudgeRenames): a rename -// whose value cannot be moved, and the drop of one whose window is open, fail the check (design 32). -func (st *state) judgeRenames(ctx context.Context) { - if st.c.Try != nil { - return - } - for _, p := range st.c.Pulls { - for _, module := range p.Modules { - r := rules.Renames{Module: module, Key: pullKey(p)} - was, now, ok, err := st.manifestPair(ctx, p, module) - if err != nil { - r.Unread = err.Error() - st.facts = append(st.facts, rules.Step{Repository: p.Repository, Number: p.Number, Renames: &r}) - continue - } - if !ok { - was = catalogue.Manifest{} // a module new on the pull request brings every rename it declares - if now, ok = st.headManifest(ctx, p, module); !ok { - continue - } - } - rc := renamesOf(was, now) - if len(rc.brought) == 0 && len(rc.dropped) == 0 { - continue - } - r.Brought, r.Dropped = rc.brought, rc.dropped - if st.j.Renames != nil { - answer, err := st.j.Renames.Renames(ctx, module) - switch { - case err != nil: - r.Unasked, r.NotServed = err.Error(), errors.Is(err, ErrRenamesNotServed) - default: - r.Answer = &answer - } - } - st.facts = append(st.facts, rules.Step{Repository: p.Repository, Number: p.Number, Renames: &r}) - } - } -} - -func (st *state) headManifest(ctx context.Context, p Pull, module string) (catalogue.Manifest, bool) { - if digest := p.Fps[module].Manifest; digest != "" && st.j.Read != nil { - if st.j.Manifests != nil && st.j.Manifests.ManifestDigests()[module] == digest { - return catalogue.Manifest{}, false // unchanged - } - m, err := st.j.Read.Manifest(ctx, digest) - return m, err == nil - } - return catalogue.Manifest{}, false -} diff --git a/modules/mesh-delivery-checker/internal/check/requests.go b/modules/mesh-delivery-checker/internal/check/requests.go deleted file mode 100644 index a18ba5dc..00000000 --- a/modules/mesh-delivery-checker/internal/check/requests.go +++ /dev/null @@ -1,299 +0,0 @@ -package check - -import ( - "bytes" - "compress/gzip" - "context" - "crypto/sha256" - "encoding/base64" - "encoding/hex" - "encoding/json" - "fmt" - "io" - "sort" - "strings" - "time" -) - -// The composition requests a check sends (issue 1921, notes 66-67): the request the checker sends the composer for one -// node, the checking program's whole input of a composition, is kept in the checker's own state RequestsStateName, so a -// session reproduces a check's composition exactly. The checker is a foundation part (novox/hq ADR 0619 §1-3), so its -// kept data is a section of its manifest's old form, here `state`, and not an object store asked in `requires`. -// -// **What bounds it.** A request is the node-engine's whole composition input of one node, 0.7 to 0.8 MB as JSON and -// growing with the mesh. A state's value holds at most 256 KiB (kvlimit.ModuleStateValueBytes, headers included), and -// the compressed request already passed it (issue 1966: 300127 bytes), so it is kept gzip-compressed in pieces: the key -// of the check and node holds a small document naming the request's digest, size and number of pieces, and each piece is -// a value of its own under RequestChunkKey, of at most RequestChunkBytes. The kept data is the checker's, a foundation -// part's, so it stays in the old section `state` (ADR 0619 §2) and is read through the tool runner's state read, which -// serves no object store of the old sections. The manifest's ttl-seconds is the lifetime (3 days) of the pointer and of -// every piece and its max-mib the most the state holds (256): the bus drops a value past the one and refuses a put past the -// other, so nothing waits on a timer here; a state that is full says so in the check's words, which name RequestsDropTool. A check kept again overwrites the key; -// a request whose pieces cannot all be put is refused naming the size (never dropped, never half kept: the pointer is -// put after its pieces). -const ( - // RequestsStateName is the checker's state of kept requests and RequestsBucket its bucket. - RequestsStateName = "compositions" - // RequestsReadTool is the tool that reads a kept request. - RequestsReadTool = "checker_composition_requests" - // RequestsDropTool is the tool that drops a check's kept requests by hand (a state that is full is freed with it). - RequestsDropTool = "checker_composition_requests_drop" - // RequestChunkBytes is the most compressed bytes one piece holds: base64 of it, in its document, stays well under - // the state's 256 KiB value. - RequestChunkBytes = 128 << 10 -) - -// KeptRequest is the pointer to a request as the state holds it: whose it is, and what its pieces make. -type KeptRequest struct { - V int `json:"v"` - Check string `json:"check"` - Node string `json:"node"` - KeptAt time.Time `json:"kept_at"` - // Size and Digest are the request's, uncompressed: its bytes and their sha256. - Size int `json:"size"` - Digest string `json:"digest"` - // Pieces is how many pieces (RequestChunkKey of the digest, 0 to Pieces-1) make the request's gzip bytes. - Pieces int `json:"pieces"` -} - -// KeptPiece is one piece of a request's gzip bytes, base64-encoded. -type KeptPiece struct { - V int `json:"v"` - Digest string `json:"digest"` - Index int `json:"index"` - Gzip string `json:"gzip"` -} - -// isPieceKey is whether a key is a piece's: under pieces/ and a sha256 digest, which no check's key (group.heads/node) is. -func isPieceKey(k string) bool { - rest, ok := strings.CutPrefix(k, "pieces/") - if !ok { - return false - } - digest, _, _ := strings.Cut(rest, "/") - return len(digest) == 64 -} - -// RequestChunkKey is the key of a request's piece: under the request's digest, so a request kept again for the same -// bytes writes the same pieces. -func RequestChunkKey(digest string, index int) string { - return fmt.Sprintf("pieces/%s/%d", digest, index) -} - -// RequestKey is the key a check's request to a node is kept under: the check's key (group.heads) and the node. -func RequestKey(key, node string) string { return key + "/" + node } - -// RequestPutter writes a value of the state, replacing one of that key. -type RequestPutter interface { - Put(ctx context.Context, key string, value []byte) (uint64, error) -} - -// RequestKeeper keeps the requests of a check. -type RequestKeeper struct { - Store RequestPutter - Now func() time.Time -} - -// Keep stores body under the check's key and the node. An error says why it could not, for the verdict's words. -func (k RequestKeeper) Keep(ctx context.Context, key, node string, body []byte) error { - var packed bytes.Buffer - zw := gzip.NewWriter(&packed) - if _, err := zw.Write(body); err != nil { - return fmt.Errorf("the composition request of check %s to %s could not be compressed: %w", key, node, err) - } - if err := zw.Close(); err != nil { - return fmt.Errorf("the composition request of check %s to %s could not be compressed: %w", key, node, err) - } - now := time.Now - if k.Now != nil { - now = k.Now - } - sum := sha256.Sum256(body) - digest := hex.EncodeToString(sum[:]) - pieces := (packed.Len() + RequestChunkBytes - 1) / RequestChunkBytes - fail := func(err error) error { - return fmt.Errorf("the composition request of check %s to %s (%d bytes, %d compressed in %d pieces) could not be kept in the state %s: %w (when the state is full, free it by dropping the kept requests of a check no longer read, with the tool %s)", - key, node, len(body), packed.Len(), pieces, RequestsStateName, err, RequestsDropTool) - } - for i := 0; i < pieces; i++ { - end := min((i+1)*RequestChunkBytes, packed.Len()) - doc, err := json.Marshal(KeptPiece{V: 2, Digest: digest, Index: i, Gzip: base64.StdEncoding.EncodeToString(packed.Bytes()[i*RequestChunkBytes : end])}) - if err != nil { - return err - } - if _, err := k.Store.Put(ctx, RequestChunkKey(digest, i), doc); err != nil { - return fail(err) - } - } - doc, err := json.Marshal(KeptRequest{V: 2, Check: key, Node: node, KeptAt: now().UTC(), Size: len(body), Digest: digest, Pieces: pieces}) - if err != nil { - return err - } - if _, err := k.Store.Put(ctx, RequestKey(key, node), doc); err != nil { - return fail(err) - } - return nil -} - -// ReadRequests answers the kept requests: with a check and a node, that request whole; with a check, the nodes it kept -// one for; with neither, every kept request. A node without a check is refused. get is the state's value of a key. -func ReadRequests(s StateRead, key, node string) (any, error) { - if s.Keys == nil || s.Get == nil { - return nil, fmt.Errorf("the state %s cannot be read here", RequestsStateName) - } - if node != "" && key == "" { - return nil, fmt.Errorf("a node is asked for with the check's key (group.heads) of the request") - } - if node != "" { - raw, err := s.Get(RequestKey(key, node)) - if err != nil { - return nil, err - } - if len(raw) == 0 { - return nil, fmt.Errorf("no composition request of check %s to %s is kept (none was sent, or it is past its lifetime)", key, node) - } - kept, body, err := unpackRequest(s, raw) - if err != nil { - return nil, fmt.Errorf("the kept request %s: %w", RequestKey(key, node), err) - } - return map[string]any{"check": kept.Check, "node": kept.Node, "size": kept.Size, "digest": kept.Digest, "kept_at": kept.KeptAt, "request": json.RawMessage(body)}, nil - } - keys, err := s.Keys() - if err != nil { - return nil, err - } - sort.Strings(keys) - listed := []map[string]any{} - for _, k := range keys { - if key != "" && !strings.HasPrefix(k, key+"/") { - continue - } - if isPieceKey(k) { - continue // a piece of a request, not a request: never read here - } - raw, err := s.Get(k) - if err != nil { - return nil, err - } - var kept KeptRequest - if err := json.Unmarshal(raw, &kept); err != nil { - listed = append(listed, map[string]any{"key": k, "unreadable": err.Error()}) - continue - } - listed = append(listed, map[string]any{"check": kept.Check, "node": kept.Node, "size": kept.Size, "digest": kept.Digest, "kept_at": kept.KeptAt}) - } - return map[string]any{"kept": listed}, nil -} - -// unpackRequest reads a kept pointer's pieces and the request's bytes, which must match the digest they were kept with. -func unpackRequest(s StateRead, raw []byte) (KeptRequest, []byte, error) { - var kept KeptRequest - if err := json.Unmarshal(raw, &kept); err != nil { - return kept, nil, fmt.Errorf("is no kept request: %w", err) - } - if kept.V != 2 { - return kept, nil, fmt.Errorf("is a kept request of version %d, which this reader does not read (it reads version 2; an older one is past its lifetime within 3 days)", kept.V) - } - var packed []byte - for i := 0; i < kept.Pieces; i++ { - raw, err := s.Get(RequestChunkKey(kept.Digest, i)) - if err != nil { - return kept, nil, err - } - var piece KeptPiece - if err := json.Unmarshal(raw, &piece); err != nil || len(raw) == 0 { - return kept, nil, fmt.Errorf("lacks its piece %d of %d (past its lifetime, or never kept)", i, kept.Pieces) - } - part, err := base64.StdEncoding.DecodeString(piece.Gzip) - if err != nil { - return kept, nil, fmt.Errorf("holds no base64 in its piece %d: %w", i, err) - } - packed = append(packed, part...) - } - zr, err := gzip.NewReader(bytes.NewReader(packed)) - if err != nil { - return kept, nil, fmt.Errorf("holds no gzip: %w", err) - } - body, err := io.ReadAll(zr) - if err != nil { - return kept, nil, fmt.Errorf("holds no complete gzip: %w", err) - } - if sum := sha256.Sum256(body); hex.EncodeToString(sum[:]) != kept.Digest { - return kept, nil, fmt.Errorf("does not match the digest it was kept with") - } - return kept, body, nil -} - -// StateDrop is the state of kept requests as the drop tool needs it: its keys, a value, and a key removed. -type StateDrop struct { - StateRead - Delete func(key string) error -} - -// DropRequests drops the requests kept for a check, after record (the hand act, recorded first: a drop that is not -// recorded is not made), and answers what it dropped: the nodes, and the pieces. A piece another kept request still names -// (the same request kept for another check has the same digest) stays. A check naming no kept request is refused. -func DropRequests(s StateDrop, key, why string, record func(what string) error) (any, error) { - if strings.TrimSpace(key) == "" || strings.TrimSpace(why) == "" { - return nil, fmt.Errorf("drop takes check (its key, group.heads) and why") - } - if s.Keys == nil || s.Get == nil || s.Delete == nil { - return nil, fmt.Errorf("the state %s cannot be changed here", RequestsStateName) - } - keys, err := s.Keys() - if err != nil { - return nil, err - } - sort.Strings(keys) - var drop []string - kept := map[string]bool{} // digests the requests that stay name - dropping := map[string]bool{} - for _, k := range keys { - if isPieceKey(k) { - continue - } - raw, err := s.Get(k) - if err != nil { - return nil, err - } - var r KeptRequest - if len(raw) == 0 || json.Unmarshal(raw, &r) != nil { - continue - } - if strings.HasPrefix(k, key+"/") { - drop = append(drop, k) - dropping[r.Digest] = true - continue - } - kept[r.Digest] = true - } - if len(drop) == 0 { - return nil, fmt.Errorf("no composition request of check %s is kept (none was sent, or it is past its lifetime): nothing was dropped", key) - } - if err := record(fmt.Sprintf("dropped the %d kept composition requests of check %s (%s)", len(drop), key, RequestsStateName)); err != nil { - return nil, fmt.Errorf("the hand act was not recorded, so nothing was dropped: %w", err) - } - var pieces int - for _, k := range drop { - if err := s.Delete(k); err != nil { - return nil, fmt.Errorf("dropping %s: %w", k, err) - } - } - for _, k := range keys { - if !isPieceKey(k) { - continue - } - digest := strings.Split(strings.TrimPrefix(k, "pieces/"), "/")[0] - if dropping[digest] && !kept[digest] { - if err := s.Delete(k); err != nil { - return nil, fmt.Errorf("dropping %s: %w", k, err) - } - pieces++ - } - } - nodes := make([]string, len(drop)) - for i, k := range drop { - nodes[i] = strings.TrimPrefix(k, key+"/") - } - return map[string]any{"check": key, "dropped_requests": nodes, "dropped_pieces": pieces, "why": why}, nil -} diff --git a/modules/mesh-delivery-checker/internal/check/results.go b/modules/mesh-delivery-checker/internal/check/results.go deleted file mode 100644 index d3accb7c..00000000 --- a/modules/mesh-delivery-checker/internal/check/results.go +++ /dev/null @@ -1,43 +0,0 @@ -package check - -import ( - "mesh-delivery-checker/rules" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// The shapes of what the build modules' steps answer, as their code writes them (mesh-catalog modules/mesh-build-go -// cmd/mesh-build-go levels.go TestResult, digest.go DigestResult, fetch.go FetchResult; modules/mesh-build-node -// cmd/mesh-build-node check.go TestResult, fetch.go FetchResult) with the fields the fetch step adds for a merge (ADR 0484 -// clause 3). Only the fields the checker reads are named; the rest is ignored. - -// Fetched is the answer of a build module's fetch step, whole: the Delivery SDK's FetchAnswer, which a check answer keeps -// (B2g; the build seats' part FetchAnswer): the workspace, the merge preview with its conflicts, changed paths, programs and -// trees. A conflict is an answer. -type Fetched = buildstep.FetchAnswer - -// ProgramFile is a changed file that is a compiled program (rules.ProgramFile). -type ProgramFile = rules.ProgramFile - -// The test step's answer is read by the judging rules (milestone 47/B0); these name their shapes here. -type ( - Failure = rules.Failure - TestTime = rules.TestTime - Tested = rules.Tested - GoPackage = rules.GoPackage -) - -// Digested is the answer of the Go build module's digest step: one digest per package that has tests at the level, by import -// path. -type Digested struct { - // Beside are the lines ` ` of the siblings the digest folded in (DigestVersion 3, lead decision L5), - // sorted; absent when the workspace has none or the build module folds none in. - Beside []string `json:"beside,omitempty"` - Level string `json:"level"` - Packages map[string]string `json:"packages"` - WithoutTests []string `json:"without_tests,omitempty"` - // JudgedBy is the program that ran the digest, as Tested.JudgedBy says. - JudgedBy string `json:"judged_by,omitempty"` - // Needs are the packages whose tests need something of the node at this level (issue 890). - Needs []buildstep.PackageNeeds `json:"needs,omitempty"` -} diff --git a/modules/mesh-delivery-checker/internal/check/seatdefs.go b/modules/mesh-delivery-checker/internal/check/seatdefs.go deleted file mode 100644 index 94e92cc9..00000000 --- a/modules/mesh-delivery-checker/internal/check/seatdefs.go +++ /dev/null @@ -1,41 +0,0 @@ -package check - -import ( - "context" - "mesh-delivery-checker/rules" -) - -// A pull request whose head's seat definition does not parse fails its check, naming the seat and where it lives (design -// 26; the lead's ruling on A2i's finding 1): the indexer keeps such a definition as the pull request's entry of its state -// seat-definitions with `unparsed` set, and cannot refuse the manifest itself, whose digest the trunk may share. - -// UnparsedSeat is one seat definition of a pull request's head that does not parse (rules.UnparsedSeat). -type UnparsedSeat = rules.UnparsedSeat - -// SeatDefinitions reads the indexer's seat definitions of one pull request (its state seat-definitions, through its -// first view, DLV-M2). -type SeatDefinitions interface { - Unparsed(ctx context.Context, repository string, pull int) ([]UnparsedSeat, error) -} - -// refuseUnparsed records, of each pull request, the indexer's seat definitions of its head that do not parse, or why -// they cannot be read, as a fact the rules judge (rules.JudgeUnparsed). -func (st *state) refuseUnparsed(ctx context.Context) { - if st.j.SeatDefinitions == nil || st.c.Try != nil { - return - } - for _, p := range st.c.Pulls { - u := rules.Unparsed{Key: pullKey(p)} - bad, err := st.j.SeatDefinitions.Unparsed(ctx, p.Repository, p.Number) - if err != nil { - u.Unread = err.Error() - } - u.Seats = bad - if u.Unread != "" || len(u.Seats) > 0 { - st.facts = append(st.facts, rules.Step{Repository: p.Repository, Number: p.Number, Unparsed: &u}) - } - } -} - -// SeatDefinitionsMachine names what refused a seat definition in a verdict's failure. -const SeatDefinitionsMachine = rules.SeatDefinitionsMachine diff --git a/modules/mesh-delivery-checker/internal/check/series.go b/modules/mesh-delivery-checker/internal/check/series.go deleted file mode 100644 index 1a6d91db..00000000 --- a/modules/mesh-delivery-checker/internal/check/series.go +++ /dev/null @@ -1,150 +0,0 @@ -package check - -import ( - "context" - "fmt" - "slices" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// A change of a package series is named in the verdict, so the review of the change sees it (design 32 *A package, its -// series*; DLV-C4): for each module a pull request touches, every artifact both its trunk manifest and its head's -// manifest declare whose series differs, as `/ series → `. An artifact that names none -// is in catalogue.DefaultSeries. A manifest that cannot be read is said in the same words, never left out. - -// seriesChanges names, on the case per pull request, each change of an artifact's series the pull request makes. -func (st *state) seriesChanges(ctx context.Context) { - for _, p := range st.c.Pulls { - var words []string - var changes []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem - for _, module := range p.Modules { - was, now, ok, err := st.manifestPair(ctx, p, module) - if err != nil { - words = append(words, fmt.Sprintf("%s: its series not compared (%v)", module, err)) - continue - } - if ok { - moved := seriesMoved(module, was, now) - changes = append(changes, moved...) - for _, c := range moved { - words = append(words, seriesLine(c)) - } - } - } - if len(words) > 0 { - st.c.series(pullKey(p), words, changes) - } - } -} - -// manifestPair is a touched module's trunk manifest and its manifest at the pull request's head: the head's by the digest -// its candidate names, else read from the forge at the head. ok is false when the module is new on the pull request, -// unchanged, or not read here. -func (st *state) manifestPair(ctx context.Context, p Pull, module string) (was, now catalogue.Manifest, ok bool, err error) { - if st.j.Read == nil || st.j.Manifests == nil { - return was, now, false, nil - } - trunk := st.j.Manifests.ManifestDigests()[module] - digest := p.Fps[module].Manifest - if trunk == "" || digest == trunk { - return was, now, false, nil - } - if was, err = st.j.Read.Manifest(ctx, trunk); err != nil { - return was, now, false, fmt.Errorf("the trunk's manifest not read: %w", err) - } - switch { - case digest != "": - if now, err = st.j.Read.Manifest(ctx, digest); err != nil { - return was, now, false, fmt.Errorf("the head's manifest not read: %w", err) - } - case st.j.Forge != nil: - var found bool - if now, _, found, err = st.manifestAtHead(ctx, p, module); err != nil || !found { - return was, now, false, err - } - default: - return was, now, false, nil - } - return was, now, true, nil -} - -// seriesLine is a series change as the status line names it: `/ series → `. -func seriesLine(c meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem) string { - return fmt.Sprintf("%s/%s series %s → %s", c.Module, c.Artifact, c.From, c.To) -} - -// seriesMoved are the artifacts of module whose series the head's manifest changes from the trunk's, by artifact. An -// artifact new on the head is no series change (the lead's ruling): it has no series on the trunk. -func seriesMoved(module string, was, now catalogue.Manifest) []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem { - if was.Build == nil || now.Build == nil { - return nil - } - before := map[string]string{} - for _, a := range was.Build.Artifacts { - before[a.Name] = a.SeriesOf() - } - var out []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem - for _, a := range now.Build.Artifacts { - if old, ok := before[a.Name]; ok && old != a.SeriesOf() { - out = append(out, meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem{Module: module, Artifact: a.Name, From: old, To: a.SeriesOf()}) - } - } - slices.SortFunc(out, func(a, b meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem) int { - return strings.Compare(a.Artifact, b.Artifact) - }) - return out -} - -// series keeps, of a pull request (pullKey), the words of the series changes it makes and the changes themselves. -func (c *Case) series(key string, words []string, changes []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem) { - c.notesMu.Lock() - defer c.notesMu.Unlock() - if c.seriesOf == nil { - c.seriesOf, c.seriesChanges = map[string][]string{}, map[string][]meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem{} - } - c.seriesOf[key], c.seriesChanges[key] = words, changes -} - -// SeriesChanges are the series changes a pull request (pullKey) makes, as the verdict and its event name them. -func (c *Case) SeriesChanges(key string) []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem { - c.notesMu.Lock() - defer c.notesMu.Unlock() - return slices.Clone(c.seriesChanges[key]) -} - -// allSeries are the series changes of every pull request of the case, each module's artifact once, by module and -// artifact: what the verdict names (meshdeliverychecker.VerdictsByGroupHeadsValue.Series). -func (c *Case) allSeries() []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem { - var out []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem - seen := map[string]bool{} - for _, p := range c.Pulls { - for _, s := range c.SeriesChanges(pullKey(p)) { - if k := s.Module + "/" + s.Artifact; !seen[k] { - seen[k] = true - out = append(out, s) - } - } - } - slices.SortFunc(out, func(a, b meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem) int { - return strings.Compare(a.Module+"/"+a.Artifact, b.Module+"/"+b.Artifact) - }) - return out -} - -// Series are the series changes a pull request (pullKey) makes, as the check named them; none when it makes none. -func (c *Case) Series(key string) []string { - c.notesMu.Lock() - defer c.notesMu.Unlock() - return slices.Clone(c.seriesOf[key]) -} - -// seriesWords is a pull request's series changes for its status line. -func seriesWords(c *Case, p Pull) string { - if c == nil { - return "" - } - return strings.Join(c.Series(pullKey(p)), ", ") -} diff --git a/modules/mesh-delivery-checker/internal/check/store.go b/modules/mesh-delivery-checker/internal/check/store.go deleted file mode 100644 index 6e7dbe22..00000000 --- a/modules/mesh-delivery-checker/internal/check/store.go +++ /dev/null @@ -1,24 +0,0 @@ -package check - -import ( - "context" - "errors" -) - -// ErrConflict is what a KV answers when a Create finds the key or an Update finds another revision than the one given. -var ErrConflict = errors.New("the key exists or changed") - -// KV is one of the checker's buckets: read, create and compare-and-set, each write answering the key's new revision. Only -// the checker's credential writes them (ADR 0444 clause 5, ADR 0484 clause 2). -type KV interface { - Get(ctx context.Context, key string) (value []byte, revision uint64, found bool, err error) - Create(ctx context.Context, key string, value []byte) (uint64, error) - Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) - // Keys are every key of the bucket. - Keys(ctx context.Context) ([]string, error) -} - -// Deleter is a KV that can take a key away; a KV that cannot leaves it. -type Deleter interface { - Delete(ctx context.Context, key string) error -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/README b/modules/mesh-delivery-checker/internal/check/testdata/captured/README deleted file mode 100644 index f2b348a3..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/README +++ /dev/null @@ -1,17 +0,0 @@ -go-test-failing.json, go-test-slow.json, go-test-empty.json and go-digest.json are what mesh-catalog's mesh-build-go answers -(TestResult and DigestResult, cmd/mesh-build-go levels.go and digest.go), marshalled by the module's own code: a throwaway -test beside the module's test_test.go and digest_test.go (workspaceFor + runUnit on the fixtures testdata/captured/failing, -slow and empty, and digestFixture + digests on testdata/repos/digest) at mesh-catalog origin/main cdbdb474, captured on -2026-10-21 for issue 1679 and not kept there. The fixtures' package names are the build module's own (fixture/failing/bad...). - -go-fetch-clean.json and go-fetch-conflict.json are what mesh-build-go's fetch step ends with (FetchResult, cmd/mesh-build-go -fetch.go and preview.go) for a fetch with a base, the Result bytes of the step in mesh-catalog's own tests -TestFetchWithABaseMergesAndAnswersThePreview and TestFetchWithAConflictEndsPassedAndNamesTheFiles (preview_test.go), copied -into a throwaway test that wrote them out, at mesh-catalog feat/r4c5-checker d5804927 (the git commands are the test's fake, -so the shas are its constants; the shape and the field names are the module's own marshalling). node-fetch-clean.json is the -FetchResult json.Marshal of mesh-build-node's fetch (fetch.go, preview.go) in its test TestFetchWithABaseMergesAndAnswersThePreview -(cmd/mesh-build-node/preview_test.go) at the same commit. Captured on 2026-10-21 for issue 1679. Both clean fetches carry `"programs":[]`, which a merging fetch of the build modules answers since issue 1577 (mesh-sdk buildstep.MergeInto's Programs), added by hand to the captured bytes: a fixture without it is an older build module's, and its line warns that no compiled program is refused. - -forge-get-file-go-mod.json is the answer of the forge's gitea_get_file (mesh-catalog modules/gitea, tools/index.ts) for modules/mesh-delivery-builder/go.mod of -novox/mesh-delivery at main, called through the mesh on 2026-10-21 for issue 1624; the checker's whole-module test answers its content through the seat -git's verb file (milestone 47/G W6.14). diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/forge-get-file-go-mod.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/forge-get-file-go-mod.json deleted file mode 100644 index a591e7e9..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/forge-get-file-go-mod.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "file": { - "content": "module mesh-delivery-builder\n\ngo 1.26.0\n\nrequire (\n\tgit.novox.be/novox/mesh-sdk/go v0.1.14-0.20261021100209-cb54756c3865\n\tgithub.com/nats-io/nats-server/v2 v2.11.17\n\tgithub.com/nats-io/nats.go v1.54.0\n)\n\nrequire (\n\tgithub.com/antithesishq/antithesis-sdk-go v0.7.0-default-no-op // indirect\n\tgithub.com/google/go-tpm v0.9.8 // indirect\n\tgithub.com/klauspost/compress v1.20.0 // indirect\n\tgithub.com/minio/highwayhash v1.0.4 // indirect\n\tgithub.com/nats-io/jwt/v2 v2.8.1 // indirect\n\tgithub.com/nats-io/nkeys v0.4.16 // indirect\n\tgithub.com/nats-io/nuid v1.0.1 // indirect\n\tgolang.org/x/crypto v0.57.0 // indirect\n\tgolang.org/x/sys v0.48.0 // indirect\n\tgolang.org/x/time v0.15.0 // indirect\n)\n", - "path": "modules/mesh-delivery-builder/go.mod", - "ref": "main", - "sha": "4431597e2e4d2a26b59006f9f8c54659a964ec68", - "size": 691 - } -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-digest.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-digest.json deleted file mode 100644 index 50f848bc..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-digest.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "level": "unit", - "packages": { - "fixture/digest/a": "c288afe6252c38d9e7286ecb5f28b3c961780871fcc253670dcc3151c11c1df2", - "fixture/digest/c": "11abf5f32f0f12caafd76c565e4c2c2f3bf2e3ce6c2c7fcb6c2264a06db40087" - }, - "without_tests": [ - "fixture/digest/b", - "fixture/digest/d" - ] -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-clean.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-clean.json deleted file mode 100644 index a87c2221..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-clean.json +++ /dev/null @@ -1 +0,0 @@ -{"workspace":"mvii1iwu5414e4","commit":"0123456789abcdef0123456789abcdef01234567","modules":["."],"base":"abcdef0123456789abcdef0123456789abcdef01","merge":"fedcba9876543210fedcba9876543210fedcba98","changed":["a.go","modules/m/main.go"],"programs":[],"trees":{"modules/m":{"tree":"1111111111111111111111111111111111111111","manifest":"ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356","locks":{"go.sum":"c5fc83c01e92404452b986527d239140ccf9a48b88e0c268fbf38c2e1429e9c9"}}}} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-conflict.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-conflict.json deleted file mode 100644 index e1d8a999..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-fetch-conflict.json +++ /dev/null @@ -1 +0,0 @@ -{"workspace":"mvii1iwz5050eb","commit":"0123456789abcdef0123456789abcdef01234567","modules":["."],"base":"abcdef0123456789abcdef0123456789abcdef01","conflicts":["a.go","sub/b.go"]} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-empty.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-empty.json deleted file mode 100644 index daee148b..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-empty.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "level": "unit", - "passed": true, - "why": "the repository has no tests at the unit level", - "no_tests": true, - "tests": 0, - "skipped": 0, - "packages": [ - { - "dir": ".", - "package": "fixture/empty", - "passed": true, - "elapsed": 0, - "tests": 0, - "no_tests": true - } - ], - "elapsed": 0, - "bound": 5, - "over_bound": false -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-failing.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-failing.json deleted file mode 100644 index 2f6d90b0..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-failing.json +++ /dev/null @@ -1,45 +0,0 @@ -{ - "level": "unit", - "passed": false, - "why": "1 failed: fixture/failing/bad TestThreeIsFour", - "tests": 2, - "skipped": 0, - "packages": [ - { - "dir": ".", - "package": "fixture/failing/bad", - "passed": false, - "elapsed": 0.003, - "tests": 1 - }, - { - "dir": ".", - "package": "fixture/failing/good", - "passed": true, - "elapsed": 0.002, - "tests": 1 - } - ], - "elapsed": 0.003, - "bound": 5, - "slowest": [ - { - "package": "fixture/failing/bad", - "test": "TestThreeIsFour", - "elapsed": 0 - }, - { - "package": "fixture/failing/good", - "test": "TestTwo", - "elapsed": 0 - } - ], - "failed": [ - { - "package": "fixture/failing/bad", - "test": "TestThreeIsFour", - "output": " bad_test.go:7: Three() = 3, want 4\n--- FAIL: TestThreeIsFour (0.00s)" - } - ], - "over_bound": false -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-slow.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-slow.json deleted file mode 100644 index aa6e5754..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/go-test-slow.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "level": "unit", - "passed": true, - "tests": 2, - "skipped": 0, - "packages": [ - { - "dir": ".", - "package": "fixture/slow", - "passed": true, - "elapsed": 0.065, - "tests": 2 - } - ], - "elapsed": 0.065, - "bound": 5, - "slowest": [ - { - "package": "fixture/slow", - "test": "TestTakesAWhile", - "elapsed": 0.06 - }, - { - "package": "fixture/slow", - "test": "TestQuick", - "elapsed": 0 - } - ], - "over_bound": false -} diff --git a/modules/mesh-delivery-checker/internal/check/testdata/captured/node-fetch-clean.json b/modules/mesh-delivery-checker/internal/check/testdata/captured/node-fetch-clean.json deleted file mode 100644 index 55ecfeca..00000000 --- a/modules/mesh-delivery-checker/internal/check/testdata/captured/node-fetch-clean.json +++ /dev/null @@ -1 +0,0 @@ -{"workspace":"mvii1sxt979f67","commit":"0123456789abcdef0123456789abcdef01234567","modules":["modules/m"],"installed":[],"refused":[],"base":"abcdef0123456789abcdef0123456789abcdef01","merge":"0123456789abcdef0123456789abcdef01234567","changed":["modules/m/package.json"],"programs":[],"trees":{"modules/m":{"tree":"0123456789abcdef0123456789abcdef01234567","manifest":"0581b699d9d05517e69b21f5cedbeb2eee8aba2b205280b561be731594d7feef"}}} diff --git a/modules/mesh-delivery-checker/internal/check/trunkview.go b/modules/mesh-delivery-checker/internal/check/trunkview.go deleted file mode 100644 index d4f4f13d..00000000 --- a/modules/mesh-delivery-checker/internal/check/trunkview.go +++ /dev/null @@ -1,31 +0,0 @@ -package check - -import ( - "encoding/json" - "fmt" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" -) - -// TrunkVerb is the key of the seat verb the trunk heads are asked by (design 47 G §197). -const TrunkVerb = "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRevision - -// TrunkHeads reads the trunk head of every repository out of the seat mesh-catalog's answer of `revision`. A candidate's -// revision is no trunk's and is refused. -func TrunkHeads(raw json.RawMessage) (map[string]string, error) { - var a meshcatalog.RevisionAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return nil, fmt.Errorf("the catalog half's revision does not read: %w", err) - } - if a.Revision == nil { - return nil, fmt.Errorf("the catalog half's revision answers no revision (none-yet: %v)", a.NoneYet) - } - if a.Revision.Kind != "trunk" { - return nil, fmt.Errorf("the catalog half's revision %d is a %q revision, not a trunk's", a.Revision.Revision, a.Revision.Kind) - } - heads := map[string]string{} - for _, h := range a.Revision.Trunk { - heads[h.Repository] = h.Commit - } - return heads, nil -} diff --git a/modules/mesh-delivery-checker/internal/check/try.go b/modules/mesh-delivery-checker/internal/check/try.go deleted file mode 100644 index 9b35b4b2..00000000 --- a/modules/mesh-delivery-checker/internal/check/try.go +++ /dev/null @@ -1,852 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "regexp" - "slices" - "sort" - "strconv" - "strings" - "sync" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/keys" -) - -// A try is a pushed head judged by the checker as a pull request's head is, before its pull request is opened (ADR 0501 -// §5; issue 686's `mesh-controller.try`, which calls these tools from R4e): the head merged onto its trunk, the unit and the -// integration level through the build modules. It owes the forge nothing (no status, no preview) and writes no verdict; its -// state is the value of its key in the checker's state tries. - -// The try's tools and state. -const ( - TryTool = "checker_try" - TryReadTool = "checker_try_read" - TryEndTool = "checker_try_end" - // TriesTool lists the tries that wait or run (OpenTries). - TriesTool = "checker_tries" - // TriesState is the checker's state of tries: the bucket mesh-delivery-checker_tries, keys try-. - TriesState = "tries" - // MaxTries and MaxTriesWaiting are checked when a try is asked, mesh-wide: a new try is refused while MaxTries tries run - // on a build node or MaxTriesWaiting wait for one (queued, or their asks not yet taken by a node). It is no bound on - // what runs later: tries let in while waiting may later all run at once, and each node's build capacity bounds what - // runs on it. A try waiting behind the seat's other asks holds no node, so it no longer blocks the next try (issue - // 1890: at 19:53 on 2026-10-26 four tries sat queued behind 44 checks for up to 47 minutes and no fifth could be asked). - MaxTries = 4 - MaxTriesWaiting = 8 - // TryScan is how many of the newest tries a new ask reads to find the ones that wait or run (issue 1827). - TryScan = 64 - // TryKeep is how long an ended try's key is kept before the checker removes it. - TryKeep = 7 * 24 * time.Hour -) - -// The states of a try: asked and waiting, running, and its end. -const ( - TryQueued = "queued" - TryRunning = "running" - TryPass = "pass" - TryFail = "fail" - TryError = "error" -) - -var ( - tryHeadOK = regexp.MustCompile(`^([0-9a-f]{40}|[0-9a-f]{64})$`) - tryTestsOK = regexp.MustCompile(`^[A-Za-z0-9_^$|().*+?/\[\]-]+$`) - tryRepoOK = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9][A-Za-z0-9._-]*$`) - tryTrunkOK = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._/-]*$`) -) - -// Try is the value of a tries key. -type Try struct { - V int `json:"v"` - ID string `json:"id"` - // Repository, Head, Trunk and Branch are the first member's (the try's headline, and all a try of one repository - // names); Members are every member of the try, by repository: each an exact commit merged onto a trunk commit (a - // group try, issue 1921). A try written before members read as one member. - Repository string `json:"repository"` - // Head is the commit tried; Trunk the trunk commit it is merged onto, and Branch the trunk's branch when it was asked by name. - Head string `json:"head"` - Trunk string `json:"trunk"` - Branch string `json:"branch,omitempty"` - Members []TryMember `json:"members,omitempty"` - // Tests is a go test -run pattern: only those tests run, and nothing is kept. Level is unit or integration: only that - // level runs. Empty is every test of both levels. - Tests string `json:"tests,omitempty"` - Level string `json:"level,omitempty"` - State string `json:"state"` - // Told is true from the moment a whole try's start is said on the checker's stream until its end is said there: kept in the - // try's own state, so an end that is written and not said (the checker stopped between) is said when the checker starts. - Told bool `json:"told,omitempty"` - // Waiting is what the running try waits for now (its group's candidate from the indexer, ADR 0639 §5), empty when it waits for nothing; - // try-read gives it, so a try that sits still says why. - Waiting string `json:"waiting,omitempty"` - // Said is the try's report in words: what failed first, the tests run, kept and skipped per level, the time of each part. - Said string `json:"said,omitempty"` - // Took is the time from its start to its end, as Go writes a duration. - Took string `json:"took,omitempty"` - // Failed is every test that failed, each with its first line, at most meshdeliverycheckerclient.MaxFailures and then how many more - // (issue 1921); or what else failed first (a conflict, a refused step). - Failed []string `json:"failed,omitempty"` - Levels map[string]meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue `json:"levels,omitempty"` - Machines []string `json:"machines,omitempty"` - // Composition is what the composition on every placed node came to (a whole try, not one narrowed to a level or to - // some tests), and Notes the words the check noted on its way (the manifests judged, the module graph, the kept - // verdicts taken): the same as a group's pull-request check reports. - Composition *meshdeliverychecker.VerdictsByGroupHeadsValueComposition `json:"composition,omitempty"` - Notes []string `json:"notes,omitempty"` - // Asks are the ids of the asks the try put on the build seats' streams, each ending on the step key . of its - // build module: the delivery view counts those steps as a try's, not a candidate's check (build_usage). - Asks []string `json:"asks,omitempty"` - // Steps say where each ask stands while the try runs and after: asked until a node takes it, then the node, its - // state, the phase its step says and when (issue 1890: a 31-minute try could not be told queued from running). - Steps []TryStep `json:"steps,omitempty"` - Asked time.Time `json:"asked"` - Started time.Time `json:"started,omitzero"` - Ended time.Time `json:"ended,omitzero"` -} - -// TryMember is one repository of a try: the exact commit asked, never the branch's head now, and the trunk commit it is -// merged onto. Branch is the trunk's branch when the trunk was asked by name. -type TryMember struct { - Repository string `json:"repository"` - Head string `json:"head"` - Trunk string `json:"trunk"` - Branch string `json:"branch,omitempty"` -} - -// MembersOf are the try's members: its Members, or the one its headline names. -func (t Try) MembersOf() []TryMember { - if len(t.Members) > 0 { - return t.Members - } - return []TryMember{{Repository: t.Repository, Head: t.Head, Trunk: t.Trunk, Branch: t.Branch}} -} - -// TryAsked is a TryStep's state before a build node took its ask: it waits in the seat's stream. -const TryAsked = "asked" - -// TryStep is one ask of a try as its step key last said it. -type TryStep struct { - Ask string `json:"ask"` - Node string `json:"node,omitempty"` - State string `json:"state"` - Phase string `json:"phase,omitempty"` - Started time.Time `json:"started,omitzero"` - PhaseAt time.Time `json:"phase-at,omitzero"` - Ended time.Time `json:"ended,omitzero"` -} - -// tryWriter writes a running try's record again as its asks are put and their steps move: one writer per try, holding -// the revision it wrote last, so the try's end is written at that revision. A write refused (the try ended by hand) -// stops the writes; the end's own write then says it. -type tryWriter struct { - mu sync.Mutex - tr *Tryer - ctx context.Context - key string - t Try - rev uint64 - stopped bool -} - -// heard takes an ask put (step nil) or a value of its step key in, and writes the record when it changed. -func (w *tryWriter) heard(askID string, step *buildstep.Step) { - w.mu.Lock() - defer w.mu.Unlock() - if w.stopped { - return - } - changed := false - if !slices.Contains(w.t.Asks, askID) { - w.t.Asks, changed = append(w.t.Asks, askID), true - } - i := slices.IndexFunc(w.t.Steps, func(s TryStep) bool { return s.Ask == askID }) - if i < 0 { - w.t.Steps, changed = append(w.t.Steps, TryStep{Ask: askID, State: TryAsked}), true - i = len(w.t.Steps) - 1 - } - if step != nil { - now := TryStep{Ask: askID, Node: step.Node, State: step.State, Phase: step.Phase, Started: step.Started, - PhaseAt: step.PhaseAt, Ended: step.Ended} - changed = changed || now != w.t.Steps[i] - w.t.Steps[i] = now - } - if changed { - w.write() - } -} - -// waiting writes what the try waits for now ("" when it waits for nothing). -func (w *tryWriter) waiting(why string) { - w.mu.Lock() - defer w.mu.Unlock() - if w.stopped || w.t.Waiting == why { - return - } - w.t.Waiting = why - w.write() -} - -// write writes the record at the revision written last; a refusal stops the writes. -func (w *tryWriter) write() { - data, err := encodeTry(w.t) - if err != nil { - return - } - rev, err := w.tr.Tries.Update(w.ctx, w.key, data, w.rev) - if err != nil { - w.stopped = true - w.tr.logf("%s: its asks' progress is no longer written: %v", w.key, err) - return - } - w.rev = rev -} - -// end stops the writes and answers the record and its revision, for the try's end. -func (w *tryWriter) end() (Try, uint64) { - w.mu.Lock() - defer w.mu.Unlock() - w.stopped = true - return w.t, w.rev -} - -// HoldsANode says whether the try holds a build node now: it runs, and a step of its asks runs on a node, or it names -// no step (a checker that writes its asks only at the end, or one about to put its first). -func (t Try) HoldsANode() bool { - if t.State != TryRunning { - return false - } - if len(t.Steps) == 0 { - return true - } - return slices.ContainsFunc(t.Steps, func(s TryStep) bool { return s.State == buildstep.StepRunning || s.State == buildstep.StepAgain }) -} - -// Over says whether the try came to its end. -func (t Try) Over() bool { return t.State == TryPass || t.State == TryFail || t.State == TryError } - -// DecodeTry reads a tries value. -func DecodeTry(raw []byte) (Try, error) { - var t Try - if err := json.Unmarshal(raw, &t); err != nil { - return Try{}, fmt.Errorf("not a try: %w", err) - } - if !meshcatalogclient.IsTryGroup(t.ID) || t.Repository == "" || t.Head == "" || t.Trunk == "" { - return Try{}, errors.New("not a try: it names no id, repository, head or trunk") - } - t.Members = t.MembersOf() - return t, nil -} - -func encodeTry(t Try) ([]byte, error) { - t.V = keys.Version - return json.Marshal(t) -} - -// TryAsk is what checker_try is given. -type TryAsk struct { - Repository, Head, Trunk, Tests, Level string - // Members are the repositories of a group try, each an exact commit and, optionally, its trunk (a commit, or the - // trunk's branch for its head now); the single Repository, Head and Trunk above are one member. Both at once are refused. - Members []TryAskMember -} - -// TryAskMember is one repository of a group try as it is asked. -type TryAskMember struct { - Repository, Head, Trunk string -} - -// TryStore is the tries state as the tools see it. -type TryStore interface { - Keys() ([]string, error) - Get(key string) (json.RawMessage, error) - Create(key string, value []byte) error -} - -// NotChecked is the error of a try of a repository the checker judges no level of: one whose trunk the gate's source does -// not name. -func NotChecked(repository string) error { - return fmt.Errorf("no level of %s is checked by the checker", repository) -} - -// tryMember checks one member as it is asked and resolves its trunk: the gate's trunk head of its repository, a commit -// asked, or the trunk's branch (its head now). A repository with no trunk head is no repository the checker checks. -func tryMember(trunks map[string]string, a TryAskMember) (TryMember, error) { - repo := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(a.Repository), ".git")) - head := strings.ToLower(strings.TrimSpace(a.Head)) - switch { - case !tryRepoOK.MatchString(repo) || strings.Contains(repo, ".."): - return TryMember{}, fmt.Errorf("%q is not a repository, /", a.Repository) - case !tryHeadOK.MatchString(head): - return TryMember{}, fmt.Errorf("the head %q of %s is not a full commit, 40 or 64 hex digits", a.Head, repo) - } - trunkHead := "" - for r, h := range trunks { - if strings.EqualFold(r, repo) { - trunkHead = h - } - } - if trunkHead == "" { - return TryMember{}, NotChecked(repo) - } - m := TryMember{Repository: repo, Head: head} - switch trunk := strings.TrimSpace(a.Trunk); { - case trunk == "": - m.Trunk = trunkHead - case tryHeadOK.MatchString(strings.ToLower(trunk)): - m.Trunk = strings.ToLower(trunk) - case tryTrunkOK.MatchString(trunk) && !strings.Contains(trunk, ".."): - // The checker judges pull requests into the trunk alone (ADR 0484 clause 11): a branch named is the trunk's head now. - m.Trunk, m.Branch = trunkHead, trunk - default: - return TryMember{}, fmt.Errorf("the trunk %q of %s is neither a full commit nor a branch", a.Trunk, repo) - } - return m, nil -} - -// sameMembers says whether two tries ask the same commits onto the same trunk commits. -func sameMembers(a, b []TryMember) bool { - return slices.EqualFunc(a, b, func(x, y TryMember) bool { - return x.Repository == y.Repository && x.Head == y.Head && x.Trunk == y.Trunk - }) -} - -// AskTry writes a new try, queued, and answers its id: a try of the same commits that waits or runs is the answer to a second -// ask, and a new try is refused while MaxTries tries run on a build node or MaxTriesWaiting wait for one. A try is a group of -// one or more members (issue 1921), each an exact commit, never the branch's head now: a push after the ask changes nothing -// of what is tried. trunks are the gate's trunk heads of every repository (source.main): a repository with none is no -// repository the checker checks. A trunk asked by name is its trunk head now. -func AskTry(store TryStore, trunks map[string]string, a TryAsk, now time.Time) (string, error) { - asked := a.Members - if a.Repository != "" || a.Head != "" || a.Trunk != "" { - if len(asked) > 0 { - return "", errors.New("name the members, or one repository with its head, not both") - } - asked = []TryAskMember{{Repository: a.Repository, Head: a.Head, Trunk: a.Trunk}} - } - if len(asked) == 0 { - return "", errors.New("name what to try: members (repository and head each), or a repository and its head") - } - switch { - case a.Level != "" && a.Level != buildstep.LevelUnit && a.Level != buildstep.LevelIntegration: - return "", fmt.Errorf("the level %q is neither unit nor integration", a.Level) - case a.Tests != "" && !tryTestsOK.MatchString(a.Tests): - return "", fmt.Errorf("the tests %q are not a go test -run pattern", a.Tests) - } - var members []TryMember - for _, m := range asked { - tm, err := tryMember(trunks, m) - if err != nil { - return "", err - } - if slices.ContainsFunc(members, func(o TryMember) bool { return o.Repository == tm.Repository }) { - return "", fmt.Errorf("%s is named twice: a try holds one commit of a repository", tm.Repository) - } - members = append(members, tm) - } - slices.SortFunc(members, func(x, y TryMember) int { return strings.Compare(x.Repository, y.Repository) }) - first := members[0] - t := Try{Repository: first.Repository, Head: first.Head, Trunk: first.Trunk, Branch: first.Branch, Members: members, - Tests: a.Tests, Level: a.Level, State: TryQueued, Asked: now.UTC()} - keys, err := store.Keys() - if err != nil { - return "", fmt.Errorf("the tries cannot be listed: %w", err) - } - // An ended try's key stays TryKeep, so the bucket holds thousands; one that waits or runs was asked lately. Only the - // newest TryScan keys are read, in parallel (a try id is the time in nanoseconds, so the newest sorts last). - sort.Slice(keys, func(i, j int) bool { - if len(keys[i]) != len(keys[j]) { - return len(keys[i]) > len(keys[j]) - } - return keys[i] > keys[j] - }) - if len(keys) > TryScan { - keys = keys[:TryScan] - } - tries := make([]Try, len(keys)) - live := make([]bool, len(keys)) - if err := parallel(len(keys), func(i int) error { - raw, err := store.Get(keys[i]) - if err != nil || len(raw) == 0 { - return nil - } - cur, err := DecodeTry(raw) - tries[i], live[i] = cur, err == nil && !cur.Over() - return nil - }); err != nil { - return "", err - } - var holding, waiting []string - for i, cur := range tries { - if !live[i] { - continue - } - if sameMembers(cur.MembersOf(), t.Members) && cur.Tests == t.Tests && cur.Level == t.Level { - return cur.ID, nil - } - said := fmt.Sprintf("%s (%s, %s)", cur.ID, sayMembers(cur.MembersOf()), cur.State) - if cur.HoldsANode() { - holding = append(holding, said) - } else { - waiting = append(waiting, said) - } - } - // Checked at this ask only: a try queued behind the seat's checks holds no node; tries let in while waiting may later - // all run at once, and each node's build capacity bounds what runs on it. - if len(holding) >= MaxTries || len(waiting) >= MaxTriesWaiting { - return "", fmt.Errorf("a new try is refused while %d tries run on a build node or %d wait for one; now %d run and %d wait: running %s; waiting %s; ask again when one ends", - MaxTries, MaxTriesWaiting, len(holding), len(waiting), orNone(holding), orNone(waiting)) - } - t.ID = "try-" + strconv.FormatInt(now.UnixNano(), 10) - data, err := encodeTry(t) - if err != nil { - return "", err - } - if err := store.Create(t.ID, data); err != nil { - return "", fmt.Errorf("the try %s cannot be written: %w", t.ID, err) - } - return t.ID, nil -} - -// ReadTry is a try's value, as stored. -func ReadTry(store TryStore, id string) (json.RawMessage, error) { - if !meshcatalogclient.IsTryGroup(id) { - return nil, fmt.Errorf("%q is not a try's id, try-", id) - } - raw, err := store.Get(id) - if err != nil { - return nil, err - } - if len(raw) == 0 { - return nil, fmt.Errorf("no try %s: it was never asked, or it ended more than %s ago and was removed", id, TryKeep) - } - return raw, nil -} - -// TryEnder is the tries state as the end tool sees it: one key with its revision, and a write at that revision. -type TryEnder interface { - Entry(key string) (json.RawMessage, uint64, error) - Update(key string, value []byte, revision uint64) error -} - -// EndTry ends a try that waits or runs as error, saying why and that it was ended by hand (issue 734): its place is -// free at once, and the next checker_try of its head asks anew. A try that ended already is refused, naming its end. -func EndTry(store TryEnder, id, why string, now time.Time) (Try, error) { - if !meshcatalogclient.IsTryGroup(id) { - return Try{}, fmt.Errorf("%q is not a try's id, try-", id) - } - why = strings.TrimSpace(why) - if why == "" { - return Try{}, errors.New("say why the try is ended: the why is kept in what it says") - } - raw, rev, err := store.Entry(id) - if err != nil { - return Try{}, err - } - if len(raw) == 0 { - return Try{}, fmt.Errorf("no try %s: it was never asked, or it ended more than %s ago and was removed", id, TryKeep) - } - t, err := DecodeTry(raw) - if err != nil { - return Try{}, err - } - if t.Over() { - return Try{}, fmt.Errorf("%s ended already, %s at %s", id, t.State, t.Ended.Format(time.RFC3339)) - } - t.State, t.Ended = TryError, now.UTC() - if !t.Started.IsZero() { - t.Took = t.Ended.Sub(t.Started).Round(time.Second).String() - } - t.Said = strings.TrimSpace("ended by hand (" + TryEndTool + "): " + why + " | " + t.Said) - t.Said = strings.TrimSuffix(t.Said, " |") - data, err := encodeTry(t) - if err != nil { - return Try{}, err - } - if err := store.Update(id, data, rev); err != nil { - return Try{}, fmt.Errorf("%s changed while it was ended, read it and end it again: %w", id, err) - } - return t, nil -} - -// TryOptions narrow what a try runs; a pull request's check has none. -type TryOptions struct { - Level string - Run string -} - -// Whole says the try runs everything a group's pull-request check runs (both levels of every member, the manifests judged by -// the manifest validator, the module graph, the composition on every placed node): it is narrowed to neither a level nor -// some tests. Such a try judges by the same code as the check, so the two cannot drift (issue 1921). -func (o *TryOptions) Whole() bool { return o != nil && o.Level == "" && o.Run == "" } - -// TryCase is the case a try is judged as: one pull of the group per member at its exact commit, with no pull request number -// (no ref: the commit is fetched as it is), the group named by the try's key, and the heads hash its candidate is keyed by. -func TryCase(key string, t Try, watch func(string, *buildstep.Step)) *Case { - cs := &Case{Key: key, Group: key, Watch: watch, Trunk: map[string]string{}, Queued: t.Asked, Progress: &Progress{}, - Try: &TryOptions{Level: t.Level, Run: t.Tests}} - var heads []meshdeliveryclient.Head - for _, m := range t.MembersOf() { - cs.Pulls = append(cs.Pulls, Pull{Repository: m.Repository, Head: m.Head, Trunk: m.Trunk}) - cs.Trunk[m.Repository] = m.Trunk - heads = append(heads, meshdeliveryclient.Head{Repository: m.Repository, Head: m.Head}) - } - cs.HeadsHash, _ = meshdeliveryclient.HeadsHash(heads) - cs.Heads = cs.HeadsHash - return cs -} - -// compositionMade says whether a composition was made; notMadeWords what it says when it was not. -func compositionMade(c *meshdeliverychecker.VerdictsByGroupHeadsValueComposition) bool { - return c != nil && c.State != meshdeliverycheckerclient.CompositionNotRun -} - -func notMadeWords(c *meshdeliverychecker.VerdictsByGroupHeadsValueComposition) string { - if c == nil || c.Words == "" { - return "no reason was given" - } - return c.Words -} - -// Tryer runs the tries: it takes a queued try, judges it through the same Judge as a pull request's check (a check ask, ADR -// 0525 §1), and writes its end. It holds no timer: a try starts when its key is written. -type Tryer struct { - Tries KV - Judge Judge - Bound time.Duration - // NotTaken ends a try whose ask no build node took within it as error (issue 734); none when 0. - NotTaken time.Duration - Now func() time.Time - Logf func(format string, args ...any) - - // Say publishes an event of the checker's stream (checkermsg.Started, checkermsg.Ended) with its body; nil says nothing. The - // indexer, which hears them, writes and deletes the candidate of a whole try's group. - // It answers the publish's error, nil only when the stream acknowledged it: an end not acknowledged is said again. - Say func(ctx context.Context, event string, body checkermsg.Event) error - - mu sync.Mutex - ending map[string]bool // tries whose end is being said now - wg sync.WaitGroup - inflight map[string]context.CancelFunc - seen map[string]bool // looked at since this process started: a try left running is taken over the first time -} - -func (tr *Tryer) now() time.Time { - if tr.Now != nil { - return tr.Now() - } - return time.Now() -} - -func (tr *Tryer) logf(format string, args ...any) { - if tr.Logf != nil { - tr.Logf(format, args...) - } -} - -// Wait returns when every try started so far has ended. -func (tr *Tryer) Wait() { tr.wg.Wait() } - -// Put takes a value of the tries state in: a queued try is started, and a try left running is taken over the first time -// this process sees it (the checker that ran it stopped; there is one checker). -func (tr *Tryer) Put(ctx context.Context, key string, value []byte) { - t, err := DecodeTry(value) - if err != nil { - tr.logf("tries.%s holds what is not a try, left: %v", key, err) - return - } - tr.mu.Lock() - if tr.inflight == nil { - tr.inflight, tr.seen = map[string]context.CancelFunc{}, map[string]bool{} - } - if t.Over() { - // Ended while it ran (checker_try_end): its judging stops, and its asks are no longer waited for. - if stop := tr.inflight[key]; stop != nil { - stop() - } - tr.mu.Unlock() - tr.sendEnd(ctx, key) // ended by hand while it ran, or its end was written and not said: the indexer drops its candidate - return - } - if tr.inflight[key] != nil || (t.State == TryRunning && tr.seen[key]) { - tr.mu.Unlock() - return - } - run, stop := context.WithCancel(ctx) - tr.seen[key] = true - tr.inflight[key] = stop - tr.mu.Unlock() - tr.wg.Add(1) - go func() { - defer tr.wg.Done() - defer func() { tr.mu.Lock(); delete(tr.inflight, key); tr.mu.Unlock(); stop() }() - tr.run(run, key) - }() -} - -// Reconsider looks at every try again (a delivery that held them ended), and removes the ended tries older than TryKeep. -func (tr *Tryer) Reconsider(ctx context.Context) { - keys, err := tr.Tries.Keys(ctx) - if err != nil { - tr.logf("the tries cannot be listed: %v", err) - return - } - for _, k := range keys { - raw, _, found, err := tr.Tries.Get(ctx, k) - if err != nil || !found { - continue - } - if t, err := DecodeTry(raw); err == nil && t.Over() && tr.now().Sub(t.Ended) > TryKeep { - if d, ok := tr.Tries.(interface { - Delete(ctx context.Context, key string) error - }); ok { - if err := d.Delete(ctx, k); err != nil { - tr.logf("%s ended %s ago and could not be removed: %v", k, tr.now().Sub(t.Ended).Round(time.Hour), err) - } - } - continue - } - tr.Put(ctx, k, raw) - } -} - -// run claims the try by compare-and-set, judges it within its bound and writes its end. -func (tr *Tryer) run(ctx context.Context, key string) { - raw, rev, found, err := tr.Tries.Get(ctx, key) - if err != nil || !found { - return - } - t, err := DecodeTry(raw) - if err != nil || t.Over() { - return - } - t.State, t.Started = TryRunning, tr.now().UTC() - t.Told = (&TryOptions{Level: t.Level, Run: t.Tests}).Whole() // its start is said below; its end is owed from now on - data, err := encodeTry(t) - if err != nil { - return - } - if rev, err = tr.Tries.Update(ctx, key, data, rev); err != nil { - if !errors.Is(err, ErrConflict) { - tr.logf("%s: %v", key, err) - } - return - } - progress := &tryWriter{tr: tr, ctx: ctx, key: key, t: t, rev: rev} - if t.Told { - started := checkermsg.Event{ID: key} - for _, m := range t.MembersOf() { - started.Members = append(started.Members, checkermsg.Member{Repository: m.Repository, Head: m.Head, Trunk: m.Trunk}) - } - tr.say(ctx, checkermsg.Started, started) - } - cs := TryCase(key, t, progress.heard) - cs.Wait = progress.waiting - judging := ctx - if tr.NotTaken > 0 { - judging = buildrun.NotTakenBound(ctx, tr.NotTaken) - } - out, judged := judgeWithin(judging, tr.Judge, cs, tr.Bound) - if ctx.Err() != nil { - return // left running: the checker takes it over when it starts; or ended by hand, which wrote its end - } - t, rev = progress.end() - t.Waiting = "" - v := out.Verdict - t.Ended = tr.now().UTC() - t.Took = t.Ended.Sub(t.Started).Round(time.Second).String() - t.Levels, t.Machines, t.Asks = v.Levels, v.Machines, cs.Asks() - t.Composition = v.Composition - t.Said = Report(v, cs, cs.Pulls[0], out.Builds, out.Slowest, out.Judged) - if len(cs.Pulls) > 1 { - t.Said = sayMembers(t.MembersOf()) + ": " + t.Said - } - t.Notes = cs.Notes() - for _, n := range t.Notes { - t.Said += " | " + n - } - switch { - case !judged || IsCouldNotRun(v) || IsLostAsk(v) || IsNotTaken(v): - // Not the change's: its ask was given up or never taken (issue 734), or a part of the mesh was not heard. - t.State = TryError - if IsLostAsk(v) || IsNotTaken(v) { - t.Failed = []string{failedOf(v).Words} - } - case v.State == meshdeliverycheckerclient.VerdictPass && cs.Try.Whole() && compositionMade(v.Composition) && strings.Contains(v.Composition.Words, nodeengineclient.NoAnswer): - // A node that gave no answer passes a group's check with a warning; a try that is to say the group composes says it - // did not, naming the node (ADR 0637 §3). - t.State = TryError - t.Failed = []string{"the composition was not made on every placed node: " + v.Composition.Words} - case v.State == meshdeliverycheckerclient.VerdictPass && cs.Try.Whole() && !compositionMade(v.Composition): - // A whole try that judged the tests and not the manifests and the composition has not tried what its group's check - // judges: it is no pass, and it says why (failure must be loud). It is an error, not the change's failure. - t.State = TryError - t.Failed = []string{"the composition was not made: " + notMadeWords(v.Composition)} - case v.State == meshdeliverycheckerclient.VerdictPass: - t.State = TryPass - default: - t.State = TryFail - t.Failed = failedLines(v) - } - if t.Tests != "" { - t.Said += " | only the tests " + t.Tests + " (Go); nothing kept" - } - if data, err = encodeTry(t); err == nil { - _, err = tr.Tries.Update(ctx, key, data, rev) - } - if err != nil { - tr.logf("%s: its end could not be written: %v", key, err) - return - } - tr.logf("%s: %s", key, t.State) - // The end just written comes back on the key's watch, and Put then stops this run's context (it stops judging a try - // ended by hand) while this run says the end, which Put's own say leaves to it: the end is said on a context that - // stop does not cancel, or it is never said and the indexer keeps the candidate of the try's group. - tr.sendEnd(context.WithoutCancel(ctx), key) -} - -// sendEnd says the end of an ended try whose start was said (Told) with the state it ended in, then clears Told in the try's -// own state; a checker that stopped between the two says it again at its start. Every end is said so: pass, fail, error and -// by hand. -func (tr *Tryer) sendEnd(ctx context.Context, key string) { - tr.mu.Lock() - if tr.ending[key] { - tr.mu.Unlock() - return - } - if tr.ending == nil { - tr.ending = map[string]bool{} - } - tr.ending[key] = true - tr.mu.Unlock() - defer func() { tr.mu.Lock(); delete(tr.ending, key); tr.mu.Unlock() }() - raw, rev, found, err := tr.Tries.Get(ctx, key) - if err != nil || !found { - return - } - t, err := DecodeTry(raw) - if err != nil || !t.Over() || !t.Told { - return - } - if tr.say(ctx, checkermsg.Ended, checkermsg.Event{ID: key, State: t.State}) != nil { - return // not acknowledged: Told stays, and the next Put or Reconsider says the end again - } - t.Told = false - data, err := encodeTry(t) - if err == nil { - _, err = tr.Tries.Update(ctx, key, data, rev) - } - if err != nil && !errors.Is(err, ErrConflict) { - tr.logf("%s: its end was said and Told could not be cleared: %v", key, err) - } -} - -// say publishes a try event; a failure is said in the log (the indexer finds the open tries again at its start). -func (tr *Tryer) say(ctx context.Context, event string, body checkermsg.Event) error { - if tr.Say == nil { - return nil - } - err := tr.Say(ctx, event, body) - if err != nil { - tr.logf("%s of %s could not be said: %v", event, body.ID, err) - } - return err -} - -// OpenTries are every try that waits or runs now: what checker_tries answers, so a part that missed an event (the indexer at -// its start) finds them without reading the checker's state. All the keys are read (an old open try is as open as a new one), in -// parallel. -func OpenTries(store TryStore) ([]checkermsg.Open, error) { - keys, err := store.Keys() - if err != nil { - return nil, fmt.Errorf("the tries cannot be listed: %w", err) - } - sort.Slice(keys, func(i, j int) bool { - if len(keys[i]) != len(keys[j]) { - return len(keys[i]) > len(keys[j]) - } - return keys[i] > keys[j] - }) - found := make([]*checkermsg.Open, len(keys)) - if err := parallel(len(keys), func(i int) error { - raw, err := store.Get(keys[i]) - if err != nil || len(raw) == 0 { - return nil - } - t, err := DecodeTry(raw) - if err != nil || t.Over() { - return nil - } - o := checkermsg.Open{ID: t.ID, State: t.State, Whole: (&TryOptions{Level: t.Level, Run: t.Tests}).Whole()} - for _, m := range t.MembersOf() { - o.Members = append(o.Members, checkermsg.Member{Repository: m.Repository, Head: m.Head, Trunk: m.Trunk}) - } - found[i] = &o - return nil - }); err != nil { - return nil, err - } - out := []checkermsg.Open{} // none open is an empty list, never null: a reader tells it from no answer (issue 1921) - for _, o := range found { - if o != nil { - out = append(out, *o) - } - } - return out, nil -} - -// judgeWithin runs a case within the bound (none when 0: the asks' steps are bounded from their start, ADR 0525 §6). A -// judge that fails to judge, and a check that ran past its bound, are verdicts of failure naming the machine and the step, -// and judged says false for them. -func judgeWithin(ctx context.Context, j Judge, cs *Case, bound time.Duration) (Outcome, bool) { - run, cancel := context.WithCancel(ctx) - if bound > 0 { - run, cancel = context.WithTimeoutCause(ctx, bound, errBound) - } - defer cancel() - out, err := j.Judge(run, cs) - stage, node := cs.Progress.Now() - if node == "" { - node = "checker" - } - switch { - case ctx.Err() != nil: - return Outcome{}, false - case context.Cause(run) == errBound: - if stage == "" { - stage = "starting" - } - return Outcome{Verdict: failure(node, fmt.Sprintf("the check ran past its bound of %s, stuck at: %s", bound, stage))}, false - case err != nil: - return Outcome{Verdict: failure(node, err.Error())}, false - } - return out, true -} - -// sayMembers names a try's members, each repository at its short commit. -func sayMembers(ms []TryMember) string { - var out []string - for _, m := range ms { - out = append(out, fmt.Sprintf("%s@%.12s", m.Repository, m.Head)) - } - return strings.Join(out, " + ") -} - -// orNone joins the tries said, or says none. -func orNone(said []string) string { - if len(said) == 0 { - return "none" - } - return strings.Join(said, ", ") -} diff --git a/modules/mesh-delivery-checker/internal/check/try_test.go b/modules/mesh-delivery-checker/internal/check/try_test.go deleted file mode 100644 index d6dc4827..00000000 --- a/modules/mesh-delivery-checker/internal/check/try_test.go +++ /dev/null @@ -1,505 +0,0 @@ -package check - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "mesh-delivery-checker/rules" - "slices" - "strings" - "testing" - "time" - - "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// memTries is the tries state as the tools see it, over a memKV. -type memTries struct{ kv *memKV } - -func (m memTries) Keys() ([]string, error) { return m.kv.Keys(context.Background()) } -func (m memTries) Get(key string) (json.RawMessage, error) { - v, _, _, err := m.kv.Get(context.Background(), key) - return v, err -} -func (m memTries) Create(key string, value []byte) error { - _, err := m.kv.Create(context.Background(), key, value) - return err -} - -// writtenTries is a memKV that keeps every running try it was given to write, in order. -type writtenTries struct { - *memKV - running []Try -} - -func (w *writtenTries) Update(ctx context.Context, key string, value []byte, rev uint64) (uint64, error) { - if t, err := DecodeTry(value); err == nil && !t.Over() { - w.running = append(w.running, t) - } - return w.memKV.Update(ctx, key, value, rev) -} - -var tryTrunks = map[string]string{repoA: trunkA, repoSDK: trunkS, hqRepo: trunkA} - -func readTry(t testing.TB, kv *memKV, id string) Try { - t.Helper() - raw, _, found, _ := kv.Get(context.Background(), id) - if !found { - t.Fatalf("no try %s", id) - } - tr, err := DecodeTry(raw) - if err != nil { - t.Fatal(err) - } - return tr -} - -func TestATryIsAskedQueuedOncePerHeadAndAtMostFourAtOnce(t *testing.T) { // ADR 0501 §5 - kv := newMemKV() - store := memTries{kv} - at := time.Date(2026, 10, 22, 18, 0, 0, 0, time.UTC) - id, err := AskTry(store, tryTrunks, TryAsk{Repository: repoA, Head: head1, Trunk: "main"}, at) - if err != nil || !strings.HasPrefix(id, "try-") { - t.Fatalf("id %q, %v", id, err) - } - tr := readTry(t, kv, id) - if tr.State != TryQueued || tr.Trunk != trunkA || tr.Branch != "main" || tr.Head != head1 || tr.Repository != repoA { - t.Errorf("try = %+v, want queued on the trunk's head", tr) - } - again, err := AskTry(store, tryTrunks, TryAsk{Repository: repoA, Head: head1, Trunk: "main"}, at.Add(time.Second)) - if err != nil || again != id { - t.Errorf("a second ask of the same head answered %q, %v, want %q", again, err, id) - } - // Issue 1890: tries on a build node (a step of theirs running, or none named yet) and tries waiting for one (queued, or - // their asks not taken) are bounded apart, MaxTries and MaxTriesWaiting; the refusal names both counts. - // A group try is the same try whatever the order of its members; the commits asked are the commits tried. - group := TryAsk{Members: []TryAskMember{{Repository: repoSDK, Head: head2}, {Repository: repoA, Head: head1}}} - gid, err := AskTry(store, tryTrunks, group, at.Add(time.Second)) - if err != nil || gid == id { - t.Fatalf("the group try = %q, %v, want a try of its own", gid, err) - } - group.Members[0], group.Members[1] = group.Members[1], group.Members[0] - if again, err := AskTry(store, tryTrunks, group, at.Add(time.Minute)); err != nil || again != gid { - t.Errorf("the group asked in another order answered %q, %v, want %q", again, err, gid) - } - if g := readTry(t, kv, gid); len(g.Members) != 2 || g.Members[0].Repository != repoA || g.Members[0].Head != head1 || g.Members[1].Trunk != trunkS { - t.Errorf("group try = %+v, want its members by repository at the commits asked", g.Members) - } - if open, err := OpenTries(store); err != nil || len(open) != 2 || !open[0].Whole || len(open[1].Members) != 2 && len(open[0].Members) != 2 { - t.Errorf("open tries = %+v, %v, want the two queued", open, err) - } - if !meshcatalogclient.IsTryGroup(gid) || meshcatalogclient.IsTryGroup("feat/x") || meshcatalogclient.IsTryGroup("fix/1921-try-1") { - t.Errorf("which candidates keys are a try's group: %s is, a branch is not", gid) - } - onNode := func(i int) Try { - return Try{ID: fmt.Sprintf("try-%d", i), Repository: repoA, Head: fmt.Sprintf("%040x", i), Trunk: trunkA, State: TryRunning, - Steps: []TryStep{{Ask: "a", State: buildstep.StepRunning}}} - } - waitingFor := func(i int) Try { - return Try{ID: fmt.Sprintf("try-%d", i), Repository: repoA, Head: fmt.Sprintf("%040x", i), Trunk: trunkA, State: TryRunning, - Steps: []TryStep{{Ask: "a", State: TryAsked}}} - } - for _, c := range []struct { - what string - holding, wait int - refused string - }{ - {"four queued behind the checks, none on a node: a fifth is taken", 0, 4, ""}, - {"four on a node", MaxTries, 0, "now 4 run and 0 wait"}, - {"three on a node, eight waiting", 3, MaxTriesWaiting, "a new try is refused while 4 tries run on a build node or 8 wait for one; now 3 run and 8 wait"}, - } { - kv := newMemKV() - for i := range c.holding + c.wait { - tr := waitingFor(i + 1) - if i < c.holding { - tr = onNode(i + 1) - } - data, _ := encodeTry(tr) - _, _ = kv.Create(context.Background(), tr.ID, data) - } - _, err := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoSDK, Head: head1}, at.Add(time.Minute)) - if (c.refused == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), c.refused)) { - t.Errorf("%s: answered %v, want refused %q", c.what, err, c.refused) - } - } - // Issue 1748: a try whose step ended on a transport fault, the bus giving its ask again, still runs. - if !(Try{State: TryRunning, Steps: []TryStep{{Ask: "a", State: buildstep.StepAgain}}}).HoldsANode() { - t.Error("a try with a step given again no longer runs") - } -} - -func TestATryOfARepositoryWithNoCheckedLevelIsRefused(t *testing.T) { // the agreed error of the try - store := memTries{newMemKV()} - for _, repo := range []string{"novox/no-trunk", "novox/mesh-lab"} { // hq is checked since R4g (ADR 0527): see tryTrunks - _, err := AskTry(store, tryTrunks, TryAsk{Repository: repo, Head: head1, Trunk: "main"}, time.Now()) - if err == nil || err.Error() != "no level of "+repo+" is checked by the checker" { - t.Errorf("%s: %v", repo, err) - } - } - for _, bad := range []TryAsk{{Repository: repoA, Head: "abc"}, {Repository: repoA, Head: head1, Level: "e2e"}, - {Repository: repoA, Head: head1, Tests: "a b"}, {Repository: "x", Head: head1}, {Repository: repoA, Head: head1, Trunk: "-x"}, - // A group try (issue 1921): members or one repository, never both or none; one commit per repository; every member checked. - {}, - {Repository: repoA, Head: head1, Members: []TryAskMember{{Repository: repoSDK, Head: head2}}}, - {Members: []TryAskMember{{Repository: repoA, Head: head1}, {Repository: repoA, Head: head2}}}, - {Members: []TryAskMember{{Repository: repoA, Head: head1}, {Repository: repoSDK, Head: "main"}}}, - {Members: []TryAskMember{{Repository: repoA, Head: head1}, {Repository: "novox/no-trunk", Head: head2}}}} { - if _, err := AskTry(store, tryTrunks, bad, time.Now()); err == nil { - t.Errorf("%+v was taken", bad) - } - } -} - -// fakeTryCandidates answers the indexer's candidate of a try's group from memory, recording the keys asked. -type fakeTryCandidates struct { - cand meshcatalog.CandidatesByGroupValue - err error - asked []string - // during is called while the try waits for the candidate. - during func() -} - -func (f *fakeTryCandidates) TryCandidate(_ context.Context, group, heads string, _ time.Duration) (meshcatalog.CandidatesByGroupValue, error) { - if f.during != nil { - f.during() - } - f.asked = append(f.asked, group+"."+heads) - return f.cand, f.err -} - -// wholeBench makes the bench judge a whole try as the checker does: the composition on every placed node and the -// indexer's candidate of the try's group. -func wholeBench(b *bench, cand meshcatalog.CandidatesByGroupValue) (*fakeCompositions, *fakeTryCandidates) { - comps := &fakeCompositions{v: composedOn("v0.1.14-x", 40, "g14", "shanks")} - cands := &fakeTryCandidates{cand: cand} - b.ex.Compositions, b.ex.Candidates = comps, cands - return comps, cands -} - -// A group try (issue 1921): several repositories each at the exact commit asked, judged by the code of a group's pull-request -// check; the two cannot drift, and a later check of the same heads keeps what the try passed. -func TestATryIsJudgedAsAGroupsCheckAndOwesTheForgeNothing(t *testing.T) { // ADR 0501 §5, issue 1921 - ctx := context.Background() - kv := newMemKV() - id, err := AskTry(memTries{kv}, tryTrunks, TryAsk{Members: []TryAskMember{{Repository: repoSDK, Head: head2, Trunk: trunkS}, {Repository: repoA, Head: head1, Trunk: trunkA}}}, time.Now()) - if err != nil { - t.Fatal(err) - } - b := newBench(t) - comps, cands := wholeBench(b, meshcatalog.CandidatesByGroupValue{Fpset: "fps-of-the-try"}) - written := &writtenTries{memKV: kv} - var waiting string - cands.during = func() { waiting = readTry(t, kv, id).Waiting } // ADR 0639 §5: the stored try says what it waits for - var said []string - var tr *Tryer - echoed := false - tr = &Tryer{Tries: written, Judge: b.ex, Say: func(ctx context.Context, event string, e checkermsg.Event) error { - if event == checkermsg.Ended && !echoed { - // The end the run wrote comes back on the key's watch while the run says it: Put stops the run's context and - // leaves the saying to the run, whose say must not be stopped with it (issue 1921). - echoed = true - raw, _, _, _ := kv.Get(context.Background(), id) - tr.Put(context.Background(), id, raw) - } - if err := ctx.Err(); err != nil { - return err // a publish on a stopped context is never acknowledged - } - said = append(said, fmt.Sprintf("%s %s %d %s", event, e.ID, len(e.Members), e.State)) - return nil - }} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - // The indexer hears the start with the members at their commits, and the end (it writes and deletes the group's candidate). - if len(said) != 2 || said[0] != "try.started "+id+" 2 " || said[1] != "try.ended "+id+" 0 "+TryPass { - t.Errorf("events = %v, want the start with 2 members and the end", said) - } - // What the indexer reads: checkermsg.Decode accepts the events as said and refuses a body that is no try's. - for _, row := range []struct { - name, event, body string - ok bool - }{ - {"a start with its members", checkermsg.Started, `{"id":"` + id + `","members":[{"repository":"` + repoA + `","head":"` + head1 + `","trunk":"` + trunkA + `"}]}`, true}, - {"an end with its state", checkermsg.Ended, `{"id":"` + id + `","state":"pass"}`, true}, - {"an id that is no try's", checkermsg.Ended, `{"id":"pull-7","state":"pass"}`, false}, - {"a start with no member", checkermsg.Started, `{"id":"` + id + `"}`, false}, - {"a body that is no JSON", checkermsg.Ended, `try-7`, false}, - } { - if e, err := checkermsg.Decode(row.event, []byte(row.body)); (err == nil) != row.ok || (row.ok && e.ID != id) { - t.Errorf("Decode of %s = %+v, %v, want ok=%v", row.name, e, err, row.ok) - } - } - got := readTry(t, kv, id) - if !strings.Contains(waiting, "candidate") || got.Waiting != "" { - t.Errorf("waiting while the candidate was awaited = %q, after the end %q: want the reason, then none", waiting, got.Waiting) - } - if got.Told { // the end was said: nothing is owed - t.Errorf("the ended try still owes its end: %+v", got) - } - if open, err := OpenTries(memTries{kv}); err != nil || len(open) != 0 { - t.Errorf("open tries after the end = %v, %v", open, err) - } else if raw, _ := json.Marshal(open); string(raw) != "[]" { // issue 1921: none open is [] on the wire, never null - t.Errorf("none open is answered %s, want []", raw) - } - if got.State != TryPass || got.Took == "" || len(got.Members) != 2 || got.Members[0].Repository != repoA || got.Members[1].Head != head2 || - got.Said == "" || got.Levels[buildstep.LevelUnit].Run+got.Levels[buildstep.LevelUnit].Kept != 4 { // the bench gives both members the same digests - t.Fatalf("try = %+v", got) - } - if got.Composition == nil || got.Composition.State != meshdeliverycheckerclient.CompositionPass || len(got.Machines) == 0 { - t.Errorf("the try's composition = %+v, want the composition on every placed node, as a check's", got.Composition) - } - // The same key the indexer's candidate of the group has: the try's id and the heads of its members. - if len(comps.asked) != 1 || comps.asked[0] != id || len(cands.asked) != 1 || !strings.HasPrefix(cands.asked[0], id+".") { - t.Errorf("composed %v, candidate asked %v, want the try's key %s", comps.asked, cands.asked, id) - } - // Issue 1890: while it runs, the try names its asks as soon as they are put, and then where each step stands. - if len(written.running) < 3 || len(written.running[1].Asks) != 1 || written.running[1].Steps[0].State != TryAsked { - t.Errorf("the running try's writes = %+v, want its ask named while it ran", written.running) - } - // Two check asks and the checking program's two run asks (the seat list and the verdict). - if len(got.Steps) != 4 || got.Steps[0].Node != "shanks" || got.Steps[0].State != buildstep.StepPassed || len(got.Asks) != 4 { - t.Errorf("the ended try's steps = %+v, asks %v", got.Steps, got.Asks) - } - for _, a := range got.Asks { // the delivery view counts their steps as a try's (build_usage) - if _, err := buildstep.AskStepKey("", "shanks", a); err != nil { - t.Errorf("the try names %q, which is no ask id: %v", a, err) - } - } - // Each member is fetched at the exact commit asked, merged onto its own trunk, with no pull request's ref. - fetch := b.steps.tool("fetch") - if len(fetch) != 2 { - t.Fatalf("fetch = %+v, want one per member", fetch) - } - for _, f := range fetch { - want := map[string][2]string{repoA: {head1, trunkA}, repoSDK: {head2, trunkS}}[f.args["repository"].(string)] - if f.args["commit"] != want[0] || f.args["base"] != want[1] || f.args["ref"] != nil { - t.Errorf("fetch = %+v, want the exact commit %s merged onto %s", f.args, want[0], want[1]) - } - } - // A whole try writes the merge previews the indexer makes its candidate from; it writes no status and asks no Approve. - if keys, _ := b.prev.Keys(ctx); len(keys) != 2 { - t.Errorf("a whole try wrote the previews %v, want one per member", keys) - } - // An ended try is not run again. - raw, _, _, _ = kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - if n := len(b.steps.tool("fetch")); n != 2 { - t.Errorf("an ended try was fetched again: %d fetches", n) - } - // The kept verdicts: what the try passed is kept under the digests of the merged trees, so the pull-request check of - // the same head on the same trunk runs none of it again (and a push of other code runs only what changed). - before := len(b.steps.tool("test")) - out := b.judge(t, caseOf(t, pullOf(repoA, 7, head1))) - if out.Verdict.State != meshdeliverycheckerclient.VerdictPass || out.Verdict.Levels[buildstep.LevelUnit].Kept == 0 || out.Verdict.Levels[buildstep.LevelUnit].Run != 0 || - out.Verdict.Levels[buildstep.LevelIntegration].Run != 0 { - t.Errorf("the check of the tried head = %+v, want everything the try passed kept and nothing run", out.Verdict.Levels) - } - if after := len(b.steps.tool("test")); after != before { - t.Errorf("the check of the tried head ran %d test steps, want none: the try's passes are kept", after-before) - } -} - -func TestATryOfOneLevelAndSomeTestsRunsThemAloneAndKeepsNothing(t *testing.T) { - ctx := context.Background() - kv := newMemKV() - id, err := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1, Level: buildstep.LevelIntegration, Tests: "TestBus|TestStore"}, time.Now()) - if err != nil { - t.Fatal(err) - } - b := newBench(t) - tr := &Tryer{Tries: kv, Judge: b.ex, Say: func(context.Context, string, checkermsg.Event) error { - t.Error("a try of one level and some tests said an event: it has no candidate") - return nil - }} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - if open, _ := OpenTries(memTries{kv}); len(open) != 0 { - t.Errorf("open = %v", open) - } - tests := b.steps.tool("test") - if len(tests) != 1 || tests[0].args["level"] != buildstep.LevelIntegration || tests[0].args["run"] != "TestBus|TestStore" { - t.Fatalf("tests = %+v, want the integration level alone with the run pattern", tests) - } - if n := len(b.steps.tool("digest")); n != 0 { - t.Errorf("%d digests for a try of some tests", n) - } - if keys, _ := b.kept.Keys(ctx); len(keys) != 0 { - t.Errorf("kept %v from a try of some tests", keys) - } - if got := readTry(t, kv, id); got.State != TryPass || !strings.Contains(got.Said, "only the tests TestBus|TestStore") { - t.Errorf("try = %+v", got) - } - t.Run("a try of one level keeps only that level", func(t *testing.T) { - kv := newMemKV() - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1, Level: buildstep.LevelUnit}, time.Now()) - b := newBench(t) - tr := &Tryer{Tries: kv, Judge: b.ex} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - keys, _ := b.kept.Keys(ctx) - if got := readTry(t, kv, id); got.State != TryPass || len(keys) == 0 { - t.Fatalf("try = %+v, kept %v", got, keys) - } - for _, k := range keys { - if !strings.HasPrefix(k, buildstep.LevelUnit+".") { - t.Errorf("a try of the unit level kept %s", k) - } - } - }) -} - -func TestAFailingTryNamesWhatFailedAndAStepThatCannotRunIsAnError(t *testing.T) { - ctx := context.Background() - kv := newMemKV() - b := newBench(t) - b.test = func(c stepCall) (any, error) { return captured[Tested](t, "go-test-failing"), nil } - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, time.Now()) - tr := &Tryer{Tries: kv, Judge: b.ex} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - if got := readTry(t, kv, id); got.State != TryFail || len(got.Failed) != 2 || !strings.Contains(got.Failed[0], "unit: fixture/failing/bad.TestThreeIsFour") || - !strings.Contains(got.Failed[1], "integration: fixture/failing/bad.TestThreeIsFour") { // both levels ran (the operator, 2026-11-02) - t.Errorf("try = %+v", got) - } - // A whole try that could not judge the manifests and the composition is no pass; a failed composition is named beside - // the failed tests (the group's check does as much), and a candidate of other heads is not the try's. - for _, row := range []struct { - name string - cand *fakeTryCandidates - comp *rules.Composed - state, said string - }{ - {name: "the indexer wrote no candidate", cand: &fakeTryCandidates{err: errors.New("the indexer wrote no candidate within 5m0s")}, state: TryError, said: "the indexer wrote no candidate within 5m0s"}, - {name: "a node that gave no composition answer", cand: &fakeTryCandidates{}, comp: &rules.Composed{Composer: "x", Nodes: []string{"g14"}}, - state: TryError, said: "g14: " + nodeengineclient.NoAnswer}, - {name: "a composition refused on a node", cand: &fakeTryCandidates{}, comp: ptr(refusedOn("x", "g14", "web: needs a state mail does not keep")), - state: TryFail, said: "not composed on g14: web: needs a state mail does not keep"}, - } { - kv := newMemKV() - b := newBench(t) - comps, _ := wholeBench(b, meshcatalog.CandidatesByGroupValue{}) - b.ex.Candidates = row.cand - if row.comp != nil { - comps.v = *row.comp - } - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, time.Now()) - var ends []string - tr := &Tryer{Tries: kv, Judge: b.ex, Say: func(_ context.Context, event string, e checkermsg.Event) error { - if event == checkermsg.Ended { - ends = append(ends, e.State) - } - return nil - }} - raw, _, _, _ := kv.Get(ctx, id) - tr.Put(ctx, id, raw) - tr.Wait() - if len(ends) != 1 || ends[0] != row.state { // every end says its state: error as well as fail - t.Errorf("%s: ends said = %v, want once %s", row.name, ends, row.state) - } - if got := readTry(t, kv, id); got.State != row.state || !strings.Contains(strings.Join(got.Failed, " | ")+got.Said, row.said) { - t.Errorf("%s: try = %s, failed %v, said %q, want %s naming %q", row.name, got.State, got.Failed, got.Said, row.state, row.said) - } - } - // No build node is no error since R4g: the try's ask waits in the check stream until a node pulls it (ADR 0525 §4). -} - -func TestATryLeftRunningIsTakenOver(t *testing.T) { - ctx := context.Background() - // Whether the start was said is in the try's own state (Told), so a checker that starts later says the end of a try it did - // not start, whatever way the try ended. - for _, row := range []struct { - name string - told bool - failFirst bool // the stream does not acknowledge the first end said - after func(t *testing.T, kv *memKV, id string) - ends []string - }{ - {name: "the first publish of the end is not acknowledged", told: true, failFirst: true, ends: []string{TryPass}}, - {name: "left running after its start was said", told: true, ends: []string{TryPass}}, - {name: "left running with no start said", told: false, ends: []string{TryPass}}, - {name: "ended by hand after the start was said", told: true, ends: []string{TryError}, after: func(t *testing.T, kv *memKV, id string) { - if _, err := EndTry(endable{kv}, id, "stuck", time.Now()); err != nil { - t.Fatal(err) - } - }}, - } { - t.Run(row.name, func(t *testing.T) { - kv := newMemKV() - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, time.Now()) - // Left running by a checker that stopped. - raw, rev, _, _ := kv.Get(ctx, id) - cur, _ := DecodeTry(raw) - cur.State, cur.Told = TryRunning, row.told - data, _ := encodeTry(cur) - if _, err := kv.Update(ctx, id, data, rev); err != nil { - t.Fatal(err) - } - if row.after != nil { - row.after(t, kv, id) - } - b := newBench(t) - wholeBench(b, meshcatalog.CandidatesByGroupValue{}) - var ends []string - failed := false - tr := &Tryer{Tries: kv, Judge: b.ex, Say: func(_ context.Context, event string, e checkermsg.Event) error { - if event == checkermsg.Ended { - if row.failFirst && !failed { - failed = true - return errors.New("the stream did not acknowledge") - } - ends = append(ends, e.State) - } - return nil - }} - tr.Reconsider(ctx) - tr.Wait() - if row.failFirst && !readTry(t, kv, id).Told { - t.Errorf("an end the stream did not acknowledge cleared Told: it is never said again") - } - tr.Reconsider(ctx) // said once: the second look finds nothing owed - tr.Wait() - if row.after == nil { - if got := readTry(t, kv, id); got.State != TryPass { - t.Errorf("the try left running = %+v, want taken over and passed", got) - } - } - if !slices.Equal(ends, row.ends) || readTry(t, kv, id).Told { - t.Errorf("ends said = %v, want %v, and nothing owed after", ends, row.ends) - } - }) - } - // An ended try is kept TryKeep and removed by the next Reconsider after. - t.Run("an ended try is kept seven days", func(t *testing.T) { - kv := newMemKV() - id, _ := AskTry(memTries{kv}, tryTrunks, TryAsk{Repository: repoA, Head: head1}, time.Now()) - raw, rev, _, _ := kv.Get(ctx, id) - cur, _ := DecodeTry(raw) - ended := time.Now() - cur.State, cur.Ended = TryPass, ended - data, _ := encodeTry(cur) - if _, err := kv.Update(ctx, id, data, rev); err != nil { - t.Fatal(err) - } - now := ended.Add(TryKeep - time.Hour) - tr := &Tryer{Tries: kv, Now: func() time.Time { return now }} - tr.Reconsider(ctx) - if _, _, found, _ := kv.Get(ctx, id); !found { - t.Fatal("the try was removed before TryKeep") - } - now = ended.Add(TryKeep + time.Hour) - tr.Reconsider(ctx) - if _, _, found, _ := kv.Get(ctx, id); found { - t.Error("the try was kept past TryKeep") - } - }) -} diff --git a/modules/mesh-delivery-checker/internal/check/verdictevent.go b/modules/mesh-delivery-checker/internal/check/verdictevent.go deleted file mode 100644 index 1fcfcc16..00000000 --- a/modules/mesh-delivery-checker/internal/check/verdictevent.go +++ /dev/null @@ -1,44 +0,0 @@ -package check - -import ( - "context" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" -) - -// EventVerdict is the checker's event that says a pull request's check ended, with the generated files its steps found out -// of step with their seat definitions (design 26 *Out of step*; ADR 0620): on the checker's own family asks (its stream -// asks), so the indexer learns them from the event and runs no generator, and a waiter on the stream checks does not wake. -const EventVerdict = meshdeliverychecker.HolderEventVerdict - -// VerdictEvent is the body of verdict, as the seat definition of mesh-delivery-checker says it. -type VerdictEvent = meshdeliverychecker.VerdictHolderEvent - -// VerdictSayer publishes a verdict event. -type VerdictSayer interface { - Say(ctx context.Context, v VerdictEvent) error -} - -// sayVerdict says, for each pull request of a check that ended in pass or fail, one verdict event naming its head and its -// out-of-step files. A try owes no pull request anything and says none (ADR 0501 §5). A publish that fails is said in the -// log: the verdict and the status are written already, and the next check of the heads says it again. -func (ch *Checker) sayVerdict(ctx context.Context, cs *Case, v meshdeliverychecker.VerdictsByGroupHeadsValue, out Outcome) { - if ch.Says == nil || cs.Try != nil || (v.State != meshdeliverycheckerclient.VerdictPass && v.State != meshdeliverycheckerclient.VerdictFail) { - return - } - for _, p := range cs.Pulls { - key := pullKey(Pull{Repository: p.Repository, Number: p.Number}) - e := VerdictEvent{Repository: p.Repository, Pull: int64(p.Number), Head: p.Head, Verdict: v.State, OutOfStep: out.OutOfStep[key]} - for _, s := range cs.SeriesChanges(key) { - e.Series = append(e.Series, meshdeliverychecker.VerdictHolderEventSeriesItem{Module: s.Module, Artifact: s.Artifact, From: s.From, To: s.To}) - } - if err := ch.Says.Say(ctx, e); err != nil { - ch.logf("%s: the verdict of %s#%d was written, and its event could not be said: %v", cs.Key, p.Repository, p.Number, err) - } - } -} - -// AsksStream is the checker's stream that carries verdict, one that keeps its history (ADR 0592 §6): the bus names it -// ASKS. -const AsksStream = "asks" diff --git a/modules/mesh-delivery-checker/manifest_test.go b/modules/mesh-delivery-checker/manifest_test.go deleted file mode 100644 index 1b393318..00000000 --- a/modules/mesh-delivery-checker/manifest_test.go +++ /dev/null @@ -1,325 +0,0 @@ -package manifest - -import ( - "bytes" - "encoding/json" - checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "io/fs" - "os" - "path/filepath" - "reflect" - "slices" - "strings" - "testing" - - "mesh-delivery-checker/internal/check" -) - -// The controller's manifest reader is mesh-controller internal/catalogue (Manifest, manifest.go: Consumes :521, -// Invokes :590, Streams :613, Reads :659; StreamDeclaration, streams.go), an internal package of another repository. -// These tests decode the manifest strictly into the fields those types name (DisallowUnknownFields) and hold the -// rules of issues 1678 and 1683 (ADR 0453 §4, ADR 0484). The merge gate's module check proves the controller takes the manifest. - -type state struct { - Name string `json:"name"` - History int `json:"history,omitempty"` - TTL int `json:"ttl-seconds,omitempty"` - MaxMiB int `json:"max-mib,omitempty"` -} - -type artifact struct { - Name string `json:"name"` - Kind string `json:"kind"` - Language string `json:"language"` - System string `json:"system"` - From string `json:"from"` - Binary string `json:"binary"` - Loads []string `json:"loads"` - Paths []string `json:"paths"` - Series string `json:"series"` -} - -type stream struct { - Name string `json:"name"` - Events []string `json:"events"` - MaxAgeDays int `json:"max-age-days,omitempty"` - MaxMiB int `json:"max-mib,omitempty"` - Retention string `json:"retention,omitempty"` -} - -type manifest struct { - Claims []struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` - } `json:"claims"` - Streams []stream `json:"streams"` - Tools []string `json:"tools"` - Module string `json:"module"` - Version string `json:"version"` - Slug string `json:"slug"` - Consumes []string `json:"consumes"` - State []state `json:"state"` - Reads []string `json:"reads"` - Uses []string `json:"uses"` - Invokes []string `json:"invokes"` - Config map[string]struct { - Type string `json:"type"` - Description string `json:"description"` - Pattern string `json:"pattern"` - Default any `json:"default"` - Minimum *float64 `json:"minimum"` - Maximum *float64 `json:"maximum"` - Trusted bool `json:"trusted"` - } `json:"config"` - OwnSecrets map[string]string `json:"own-secrets"` - SecretsOwner string `json:"secrets-owner"` - Resources []map[string]any `json:"resources"` - Build struct { - Artifacts []artifact `json:"artifacts"` - } `json:"build"` -} - -func load(t *testing.T) (manifest, map[string]json.RawMessage) { - t.Helper() - raw, err := os.ReadFile("module.json") - if err != nil { - t.Fatal(err) - } - var m manifest - dec := json.NewDecoder(bytes.NewReader(raw)) - dec.DisallowUnknownFields() - if err := dec.Decode(&m); err != nil { - t.Fatalf("the controller's reader refuses a key this manifest has: %v", err) - } - var top map[string]json.RawMessage - if err := json.Unmarshal(raw, &top); err != nil { - t.Fatal(err) - } - return m, top -} - -func sorted(s []string) []string { - s = slices.Clone(s) - slices.Sort(s) - return s -} - -func TestManifestDeclarations(t *testing.T) { // 1678/AC1 - m, top := load(t) - // 1373: checker_ask_again asks the delivery view's build_usage for the build seats' backlog; the list below is the rest. - if !slices.Contains(m.Invokes, "mesh-delivery-view.build_usage") { - t.Error("invokes lack mesh-delivery-view.build_usage, which checker_ask_again asks for the build seats' streams") - } - m.Invokes = slices.DeleteFunc(slices.Clone(m.Invokes), func(s string) bool { return s == "mesh-delivery-view.build_usage" }) - if m.Module != "mesh-delivery-checker" { - t.Errorf("module = %q", m.Module) - } - // The histories of the checker's four seat states (checked against internal/check names below); the names follow the seat mesh-delivery-checker. - if want := []state{{Name: "asks", History: 1}, {Name: "hearing", History: 1}, {Name: "inputs", TTL: 86400}, {Name: "compositions", TTL: 259200, MaxMiB: 256}, {Name: "kept", History: 1}, {Name: "owed", History: 5}, {Name: "previews", History: 5}, {Name: "tries", History: 1}, {Name: "verdicts", History: 5}, {Name: "first-views", History: 1}, {Name: "findings", History: 1}, {Name: "could-not-run", History: 1}}; !reflect.DeepEqual(m.State, want) { - t.Errorf("state = %+v, want %+v", m.State, want) - } - // The build modules are no names (design 47 G A-82): the steps and toolchains of the build seats are read as the seat's holder's states. - if want := []string{"mesh-delivery-builder.builds", "mesh-delivery-watchman.presence", "node-engine.have", - "seat:mesh-module-assigner.assignments", "seat:node-build-go.steps", "seat:node-build-go.toolchains", "seat:node-build-node.steps", "seat:node-build-node.toolchains"}; !reflect.DeepEqual(sorted(m.Reads), want) { - t.Errorf("reads = %v, want %v", m.Reads, want) - } - // DLV-M2 (ADR 0615 §1, ADR 0596): the indexer's candidates, modules, manifests, packages and predefined keys and the - // assigner's assignments are asked by their read tools and followed by their views families, never read. - for _, r := range m.Reads { - if strings.HasPrefix(r, "mesh-delivery-indexer.") || strings.HasPrefix(r, "mesh-delivery-assigner.") { - t.Errorf("reads %s: the indexer's and the assigner's state is read by their tools and views families alone", r) - } - } - // 1887/A5b: the checker reads nothing of the controller's plans-building (ADR 0483 superseded by ADR 0525 §7), calls no - // build module's tool and holds no capacity: it puts check asks on the Go and Node build seats (ADR 0525 §1, §4). - // The forge's file read through the seat git, never a module's tool (milestone 47/G W6.14), the build seats' drop, and of - // the node seat node-engine its one verb compose-candidate, asked of every placed node (ADR 0593 §7), never its other verbs. - if want := []string{"mesh-delivery-indexer.indexer_read_candidates", "mesh-delivery-indexer.indexer_read_manifests", - "mesh-delivery-indexer.indexer_read_modules", "mesh-delivery-indexer.indexer_read_packages", "mesh-delivery-indexer.indexer_read_predefined", - "mesh-delivery-indexer.indexer_read_seat_definitions", - "seat:git.file", "seat:mesh-catalog.registrations", "seat:mesh-catalog.revision", "seat:mesh-catalog.seat-definitions", "seat:mesh-delivery.source", "seat:mesh-config.renames", "seat:mesh-hand-acts.hand-act", "seat:mesh-module-assigner.assignments", "seat:node-build-go.drop", "seat:node-build-node.drop", "seat:node-engine.compose-candidate", - // The image build seat's drop too: a check of a module built by it is asked of it, and dropped of it (buildrun.SeatDrops). - "seat:node-build-image.drop", - // A-290, A-291: a step's answer its key keeps as an object, read through each build seat's verb output. - "seat:node-build-go.output", "seat:node-build-node.output", "seat:node-build-image.output", - // A-231: the check asks on each build seat's queues check and recheck. - "seat:node-build-go.accept.check", "seat:node-build-go.accept.recheck", "seat:node-build-node.accept.check", - "seat:node-build-node.accept.recheck", "seat:node-build-image.accept.check", "seat:node-build-image.accept.recheck"}; !reflect.DeepEqual(sorted(m.Invokes), sorted(want)) { - t.Errorf("invokes = %v, want the seat git's file, the seats' drop and compose-candidate", m.Invokes) - } - if want := []string{"node-build-go", "node-build-node"}; !reflect.DeepEqual(sorted(m.Uses), want) { - t.Errorf("uses = %v, want the Go and Node build seats", m.Uses) - } - // The checker is woken by a build seat's report of a toolchain, as the builder is: the bucket may not exist when it starts. - // Design 47 G §717, §614: the catalog half, whose claimant's seat-holders view names the holder of the assignments. - if want := []string{"mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-conditions.findings-asked", "mesh-delivery-indexer.views.candidates", "mesh-delivery-indexer.views.manifests", "mesh-delivery-indexer.views.modules", - "mesh-delivery-indexer.views.packages", "mesh-delivery-indexer.views.predefined", "mesh-delivery-indexer.views.seat-definitions", "mesh-delivery.source-moved", "node-build-go.toolchain-reported", "node-build-node.toolchain-reported", "node-engine.running", "seat:mesh-module-assigner.views.assignments", "seat:mesh-module-assigner.views.seat-holders"}; !reflect.DeepEqual(sorted(m.Consumes), want) { - t.Errorf("consumes = %v, want %v", m.Consumes, want) - } - if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" { - t.Errorf("own-secrets = %v, want exactly broker", m.OwnSecrets) - } - if m.SecretsOwner != "mesh-delivery-checker" { - t.Errorf("secrets-owner = %q", m.SecretsOwner) - } - // The checker says each time it wrote a preview or a status, so the indexer's and the forge bridge's watches find the buckets (ADR 0484 clauses 2 and 3). - // Its family asks (design 26): verdict, apart from checks so a waiter on a verdict does not wake on a check; a stream that keeps its history, the predefined form of an event family - // (ADR 0592 §6, the manifest validator). - if want := []stream{{Name: "checks", Events: []string{"preview.**", "owed.**", "try.**"}}, {Name: "asks", Events: []string{"verdict"}, Retention: "history"}, {Name: "mesh-delivery-checker-findings", Events: []string{"finding-written.*", "finding-cleared.*", "findings-stated.*"}, MaxAgeDays: 7, MaxMiB: 16}}; !reflect.DeepEqual(m.Streams, want) { - t.Errorf("streams = %+v, want %+v", m.Streams, want) - } - // Only the checker's account publishes try.* on its stream checks: the stream is declared by its owner alone, and no other - // module of the repository declares a stream carrying a try event (the indexer and the others only consume them). - siblings, err := filepath.Glob("../*/module.json") - if err != nil || len(siblings) < 2 { - t.Fatalf("the sibling manifests = %v, %v", siblings, err) - } - for _, f := range siblings { - raw, err := os.ReadFile(f) - if err != nil { - t.Fatal(err) - } - var other struct { - Module string `json:"module"` - Streams []stream `json:"streams"` - } - if err := json.Unmarshal(raw, &other); err != nil { - t.Fatalf("%s: %v", f, err) - } - for _, st := range other.Streams { - for _, e := range st.Events { - if strings.HasPrefix(e, "try.") && other.Module != m.Module { - t.Errorf("%s declares the stream %s with %s: only the checker's account publishes try events", other.Module, st.Name, e) - } - } - } - } - // The read tools (issue 1686) are served by the tools bundle, not by the checker's process. - // The try's tools (ADR 0501 §5; the end by hand, issue 734) beside them: mesh-controller's try calls them. A lost check - // is asked again by hand (checker_ask_again). - if want := []string{"checker_ask_again", "checker_composition_requests", "checker_composition_requests_drop", "checker_first_view_again", "checker_first_views", "checker_inputs", "checker_kept", "checker_previews", "checker_pull", "checker_status", "checker_tries", "checker_try", "checker_try_end", "checker_try_read", "checker_verdicts"}; !reflect.DeepEqual(sorted(m.Tools), want) { - t.Errorf("tools = %v, want %v", m.Tools, want) - } - // The try is the checker's own seat's verbs (ADR 0547 §10, issue 774), served beside the tools by the same code; so is - // input, a checking program's input the Go build module asks (issue 1921, ADR 0596 §2). - if len(m.Claims) != 1 || m.Claims[0].Name != "mesh-delivery-checker" || m.Claims[0].Scope != "mesh" || - !reflect.DeepEqual(m.Claims[0].Serves, []string{"try", "try-read", "try-end", "input"}) { - t.Errorf("claims = %+v, want the mesh seat mesh-delivery-checker serving try, try-read, try-end and input", m.Claims) - } - for _, k := range []string{"logs", "emits", "provides", "requires", "defines-seats", "seat", "seats", "bus"} { // config: compose-answer-bound (ADR 0593 §7) - if _, ok := top[k]; ok { - t.Errorf("the manifest declares %q: the checker has none", k) - } - } - // D-11 (ADR 0634 §1): the holders and buckets internal/check names, no contract naming a holder, are the manifest's: a state - // the checker reads is a `reads` (.) and a state of the indexer it follows a `consumes` of its views family. - for bucket, read := range map[string]string{ - check.PresenceBucket: "mesh-delivery-watchman.presence", check.HaveBucket: "node-engine.have"} { - if strings.Replace(bucket, "_", ".", 1) != read || !slices.Contains(m.Reads, read) { - t.Errorf("the bucket %s is not the declared read %s", bucket, read) - } - } - for bucket, view := range map[string]string{check.CandidatesBucket: "candidates", check.ModulesBucket: "modules", check.ManifestsBucket: "manifests"} { - if bucket != check.IndexerModule+"_"+view || !slices.Contains(m.Consumes, check.IndexerModule+".views."+view) { - t.Errorf("the bucket %s is not a followed view of the indexer's %s", bucket, view) - } - } - if !slices.Contains(m.Consumes, check.IndexerModule+".views."+check.IndexerPredefinedState) { - t.Errorf("the indexer's state %s is followed, and not declared", check.IndexerPredefinedState) - } - for own, history := range map[string]int{checker.StateVerdicts: 5, checker.StateKept: 1, checker.StateOwed: 5, checker.StatePreviews: 5} { - if !slices.ContainsFunc(m.State, func(st state) bool { return st.Name == own && st.History == history }) { - t.Errorf("the state %s is not a state of the manifest at history %d", own, history) - } - } -} - -func TestManifestProcess(t *testing.T) { // 1678/AC1 - m, _ := load(t) - var procs []map[string]any - for _, r := range m.Resources { - if r["type"] == "process" { - procs = append(procs, r) - } - if r["type"] == "tool" || r["type"] == "seat" { - t.Errorf("resource %v: the checker serves no tool and holds no seat", r) - } - } - if len(procs) != 1 { - t.Fatalf("%d process resources, want one", len(procs)) - } - p := procs[0] - if p["artifact"] != "program" || !reflect.DeepEqual(p["run"], []any{"./mesh-delivery-checker"}) { - t.Errorf("process = %v", p) - } - env, _ := p["env"].(map[string]any) - if len(env) != 2 || env["MESH_BROKER_FILE"] != "${dir:state}/broker" || env["MESH_CHECKER_SETTINGS"] != "${dir:state}/settings.json" { - t.Errorf("env = %v, want the broker file and the settings file", p["env"]) - } - // Its settings hold its answer bound (ADR 0593 §7: compose-answer-bound, 60 s, the checker's) and its wait for the - // candidate's builds (ADR 0603 §10: build-wait-bound, 1800 s), never a capacity of its own (ADR 0525 §4). - for _, r := range m.Resources { - if r["id"] == "settings" && !reflect.DeepEqual(r["takes-config"], []any{"compose-answer-bound", "build-wait-bound"}) { - t.Errorf("the settings resource %v takes other than compose-answer-bound and build-wait-bound", r) - } - } - if c, ok := m.Config["build-wait-bound"]; !ok || c.Default != float64(1800) || c.Trusted { - t.Errorf("build-wait-bound = %+v, want 1800 by default, not trusted", c) - } - if c, ok := m.Config["compose-answer-bound"]; !ok || c.Default != float64(60) || c.Trusted { - t.Errorf("compose-answer-bound = %+v, want 60 by default, not trusted", c) - } - if _, bus := p["bus"]; bus { - t.Errorf("the process declares a bus: it runs on its own broker credential") - } - // The settings template is packed as the files archive (novox/hq ADR 0349 §2), or the indexer's judge refuses it. - // Issue 1966's bootstrap: the package artifact checkermsg is out of the manifest until the new builder runs; the - // follow-up puts it back with its assertion (ADR 0634 section 2: four artifacts, the fourth the package checkermsg). - if len(m.Build.Artifacts) != 3 { - t.Fatalf("artifacts = %+v, want the program, the tools and the files archive", m.Build.Artifacts) - } - if a := m.Build.Artifacts[2]; a.Name != "files" || a.Kind != "archive" || a.From != "files" { - t.Errorf("files artifact = %+v, want an archive from files/", a) - } - if a := m.Build.Artifacts[1]; a.Name != "tools" || a.Kind != "bundle" || a.Language != "go" || a.Binary != "mesh-delivery-checker-tools" || - !reflect.DeepEqual(a.Loads, []string{"mesh-delivery-checker-tools"}) { - t.Errorf("tools artifact = %+v", a) - } - if st, err := os.Stat(m.Build.Artifacts[1].From); err != nil || !st.IsDir() { - t.Errorf("the tools' source %s: %v", m.Build.Artifacts[1].From, err) - } - a := m.Build.Artifacts[0] - if st, err := os.Stat(a.From); err != nil || !st.IsDir() || a.Binary != "mesh-delivery-checker" || !strings.HasSuffix(a.From, a.Binary) || a.Kind != "bundle" || a.Language != "go" { - t.Errorf("artifact = %+v", a) - } -} - -func TestServesNoRequestSubjectAndHoldsNoTicker(t *testing.T) { // 1678/AC6 (ADR 0437 §8: no timer, a wait is a context's bound) - forbidden := []string{"Subscribe(", "QueueSubscribe", "ChanSubscribe", "time.NewTicker", "time.Tick", "time.NewTimer", "time.AfterFunc", "time.After(", "time.Sleep"} - var files int - for _, root := range []string{"cmd", "internal"} { - err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { - return err - } - files++ - src, err := os.ReadFile(path) - if err != nil { - return err - } - for _, f := range forbidden { - if strings.Contains(string(src), f) { - t.Errorf("%s holds %s: the checker serves no request subject and holds no timer", path, f) - } - } - return nil - }) - if err != nil { - t.Fatal(err) - } - } - if files < 1 { - t.Fatalf("read %d source files, want the program's", files) - } -} diff --git a/modules/mesh-delivery-checker/module.json b/modules/mesh-delivery-checker/module.json deleted file mode 100644 index 9116b3ff..00000000 --- a/modules/mesh-delivery-checker/module.json +++ /dev/null @@ -1,214 +0,0 @@ -{ - "module": "mesh-delivery-checker", - "version": "0.1", - "slug": "checker", - "claims": [ - { - "name": "mesh-delivery-checker", - "scope": "mesh", - "serves": [ - "try", - "try-read", - "try-end", - "input" - ] - } - ], - "consumes": [ - "mesh-catalog.catalog-revision", - "mesh-catalog.registration-changed", - "mesh-delivery.source-moved", - "seat:mesh-module-assigner.views.seat-holders", - "mesh-catalog.seat-definition-changed", - "mesh-catalog.seat-definition-removed", - "mesh-delivery-indexer.views.candidates", - "mesh-delivery-indexer.views.manifests", - "mesh-delivery-indexer.views.modules", - "mesh-delivery-indexer.views.packages", - "mesh-delivery-indexer.views.predefined", - "mesh-delivery-indexer.views.seat-definitions", - "mesh-conditions.findings-asked", - "seat:mesh-module-assigner.views.assignments", - "node-build-go.toolchain-reported", - "node-build-node.toolchain-reported", - "node-engine.running" - ], - "streams": [ - { "name": "checks", "events": ["preview.**", "owed.**", "try.**"] }, - { "name": "asks", "events": ["verdict"], "retention": "history" }, - { "name": "mesh-delivery-checker-findings", "events": ["finding-written.*", "finding-cleared.*", "findings-stated.*"], "max-age-days": 7, "max-mib": 16 } - ], - "state": [ - { "name": "asks", "history": 1 }, - { "name": "hearing", "history": 1 }, - { "name": "inputs", "ttl-seconds": 86400 }, - { "name": "compositions", "ttl-seconds": 259200, "max-mib": 256 }, - { "name": "kept", "history": 1 }, - { "name": "owed", "history": 5 }, - { "name": "previews", "history": 5 }, - { "name": "tries", "history": 1 }, - { "name": "verdicts", "history": 5 }, - { "name": "first-views", "history": 1 }, - { "name": "findings", "history": 1 }, - { "name": "could-not-run", "history": 1 } - ], - "reads": [ - "mesh-delivery-builder.builds", - "mesh-delivery-watchman.presence", - "node-engine.have", - "seat:mesh-module-assigner.assignments", - "seat:node-build-go.steps", - "seat:node-build-go.toolchains", - "seat:node-build-node.steps", - "seat:node-build-node.toolchains" - ], - "uses": [ - "node-build-go", - "node-build-node" - ], - "tools": [ - "checker_ask_again", - "checker_composition_requests", - "checker_composition_requests_drop", - "checker_inputs", - "checker_kept", - "checker_previews", - "checker_pull", - "checker_status", - "checker_first_views", - "checker_first_view_again", - "checker_tries", - "checker_try", - "checker_try_end", - "checker_try_read", - "checker_verdicts" - ], - "invokes": [ - "mesh-delivery-indexer.indexer_read_candidates", - "mesh-delivery-indexer.indexer_read_manifests", - "mesh-delivery-indexer.indexer_read_modules", - "mesh-delivery-indexer.indexer_read_packages", - "mesh-delivery-indexer.indexer_read_predefined", - "mesh-delivery-indexer.indexer_read_seat_definitions", - "seat:git.file", - "seat:mesh-catalog.revision", - "seat:mesh-catalog.registrations", - "seat:mesh-delivery.source", - "seat:mesh-catalog.seat-definitions", - "seat:mesh-config.renames", - "mesh-delivery-view.build_usage", - "seat:mesh-hand-acts.hand-act", - "seat:mesh-module-assigner.assignments", - "seat:node-build-go.drop", - "seat:node-build-node.drop", - "seat:node-engine.compose-candidate", - "seat:node-build-image.drop", - "seat:node-build-go.output", - "seat:node-build-node.output", - "seat:node-build-image.output", - "seat:node-build-go.accept.check", - "seat:node-build-go.accept.recheck", - "seat:node-build-node.accept.check", - "seat:node-build-node.accept.recheck", - "seat:node-build-image.accept.check", - "seat:node-build-image.accept.recheck" - ], - - "own-secrets": { - "broker": "${dir:state}/broker" - }, - "secrets-owner": "mesh-delivery-checker", - "config": { - "compose-answer-bound": { - "type": "number", - "description": "seconds a check waits for each placed node's engine to answer compose-candidate (novox/hq ADR 0593 §7, §9); a node that has not answered by then is named in the verdict as not composed on that node: no answer, and the check passes with that warning.", - "minimum": 1, - "maximum": 600, - "trusted": false, - "default": 60 - }, - "build-wait-bound": { - "type": "number", - "description": "seconds a check waits for the builder's builds of its candidate to end before it composes with them (novox/hq ADR 0603 §10); a build failed fails the check, naming it, and a build not ended by then is named in the verdict and the check composes without it.", - "minimum": 60, - "maximum": 7200, - "trusted": false, - "default": 1800 - } - }, - "resources": [ - { - "id": "account", - "type": "user", - "name": "mesh-delivery-checker", - "shell": "/usr/bin/nologin", - "home": "/var/lib/mesh-delivery-checker" - }, - { - "id": "state", - "type": "directory", - "mode": "0700", - "owner": "mesh-delivery-checker", - "place": ".", - "unknown": "program" - }, - { - "id": "settings", - "type": "file", - "path": "${dir:state}/settings.json", - "mode": "0600", - "owner": "mesh-delivery-checker", - "takes-config": [ - "compose-answer-bound", - "build-wait-bound" - ], - "merge": "json", - "template": "files/settings.json" - }, - { - "id": "checker", - "type": "process", - "name": "mesh-delivery-checker", - "artifact": "program", - "run": [ - "./mesh-delivery-checker" - ], - "env": { - "MESH_BROKER_FILE": "${dir:state}/broker", - "MESH_CHECKER_SETTINGS": "${dir:state}/settings.json" - }, - "user": "mesh-delivery-checker", - "health": { - "kind": "unit" - } - } - ], - "build": { - "artifacts": [ - { - "name": "program", - "kind": "bundle", - "language": "go", - "system": "arch", - "from": "cmd/mesh-delivery-checker", - "binary": "mesh-delivery-checker" - }, - { - "name": "tools", - "kind": "bundle", - "language": "go", - "system": "arch", - "from": "cmd/mesh-delivery-checker-tools", - "binary": "mesh-delivery-checker-tools", - "loads": [ - "mesh-delivery-checker-tools" - ] - }, - { - "name": "files", - "kind": "archive", - "from": "files" - } - ] - } -} diff --git a/modules/mesh-delivery-checker/reads-outside b/modules/mesh-delivery-checker/reads-outside deleted file mode 100644 index f6b2d871..00000000 --- a/modules/mesh-delivery-checker/reads-outside +++ /dev/null @@ -1 +0,0 @@ -These tests read files outside this directory (the whole catalogue, the repository, or its root files): mesh-build-go never keeps this package's verdict (issue 1935). diff --git a/modules/mesh-delivery-checker/rules/facts.go b/modules/mesh-delivery-checker/rules/facts.go deleted file mode 100644 index b913e864..00000000 --- a/modules/mesh-delivery-checker/rules/facts.go +++ /dev/null @@ -1,306 +0,0 @@ -// Package rules is the checker's judging rules, the checking program's: given the facts of one check (what the build -// modules, the manifest validator and the composer answered, and what failed in the asking), the verdict (novox/hq ADR -// 0644, ADR 0656; design 47's bootstrap delivery). The running checker asks, waits, keeps and records; it gathers the -// facts and has them judged by the checking program built from the change's heads (cmd/mesh-checking-program), which -// imports this package, so a change to a rule is judged by its own rule. -// -// It is the checker module's own package (novox/hq ADR 0634 §1): no seat carries it and no other module imports it. -package rules - -import ( - "encoding/json" - "fmt" - "path" - "sort" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// FactsVersion is the version of Facts this program reads. A checking program built from a later change reads the facts -// a running checker of this version writes, so a version is added to, never changed; a program refuses one it does not -// read, naming it (DecodeFacts). -const FactsVersion = 1 - -// Facts are everything one check judges, in the order the running checker met it. -type Facts struct { - V int `json:"v"` - // Group and Heads name the check (meshdeliverychecker.VerdictsByGroupHeadsValue's Group and Heads); Trunk is each repository's trunk head merged. - Group string `json:"group"` - Heads string `json:"heads"` - Trunk map[string]string `json:"trunk,omitempty"` - Members []Member `json:"members"` - // SeatDigests are, by seat, the digests (SeatDigest) of the definitions the running checker's reader holds: the - // `-seats` answer leaves out every head definition that is the same (milestone 47/G, the lead's ruling on review - // finding 3: a whole list is over a run answer's bound). None from a checker that hands none: then every head - // definition is answered. - SeatDigests map[string]string `json:"seat-digests,omitempty"` - // Series are the changes of the members' modules' series, as structures, put in the verdict whatever it is (DLV-C4). - Series []meshdeliverychecker.VerdictsByGroupHeadsValueSeriesItem `json:"series,omitempty"` - // Steps are, in the order met, each level a build module answered and each failure the asking met. - Steps []Step `json:"steps,omitempty"` - // CloneMS is how much of the steps' time their levels' running did not take (meshdeliverychecker.VerdictsByGroupHeadsValueTook.Clone). - CloneMS int64 `json:"clone-ms,omitempty"` - // Composition is how the check composed, after every step; nil when it was not composed (a narrowed try, a checker - // that composes nothing): its composition is then not run, unsaid. - Composition *Composition `json:"composition,omitempty"` - // Notes are the running checker's notes of the check, for a reader. - Notes []string `json:"notes,omitempty"` -} - -// Member is one pull request of the check. -type Member struct { - Repository string `json:"repository"` - Number int `json:"number"` - Head string `json:"head"` - Trunk string `json:"trunk,omitempty"` -} - -// Step is one fact of the check, in the order met: the node that ran it, and one of a level its build module answered -// (Level, as the build module gave it), what its fetch read of compiled programs (Programs), the manifest validator's -// answers (Manifests), the module graph's ordering edges (Graph) or a failure the asking met (Failed: a step not asked, refused or not answered, a conflict, worded -// by the running checker). A step with none only says which node ran it. -type Step struct { - Repository string `json:"repository"` - Number int `json:"number"` - Module string `json:"module,omitempty"` - // Language is the language of the toolchain the build module that ran the step builds with (a level's counts differ by it). - Language string `json:"language,omitempty"` - Node string `json:"node,omitempty"` - Level *buildstep.LevelAnswer `json:"level,omitempty"` - Failed *FailedFirst `json:"failed,omitempty"` - Programs *Programs `json:"programs,omitempty"` - Manifests *Manifests `json:"manifests,omitempty"` - // Graph is the module graph's ordering edges, as the change leaves them and as the trunk has them (JudgeGraph). - Graph *Graph `json:"graph,omitempty"` - // Renames is what a module brings and drops of `renamed-from` and how the seat mesh-config answered (JudgeRenames). - Renames *Renames `json:"renames,omitempty"` - // Unparsed is what the indexer keeps of a pull request's seat definitions that do not parse (JudgeUnparsed). - Unparsed *Unparsed `json:"unparsed,omitempty"` -} - -// LevelFact is a level answer as the facts carry it: only what the rules read (Tally.Level), so a group's facts stay -// within the run input's bound (buildstep.RunInputOf). The digest, the unmet packages and the level's judged program -// are left out, the kept packages are one count, and each test result keeps only the fields Tested names, without its -// packages, bounds and judged program (review round 1 of milestone 47/B0). An answer this cannot read is carried as given, -// for the rules to refuse it. -func LevelFact(la buildstep.LevelAnswer) *buildstep.LevelAnswer { - out := &buildstep.LevelAnswer{Level: la.Level} - kept := 0 - for _, k := range la.Kept { - kept += k.Tests - } - if len(la.Kept) > 0 { - out.Kept = []buildstep.KeptTaken{{Tests: kept}} - } - for _, raw := range la.Tests { - var res Tested - if err := json.Unmarshal(raw, &res); err != nil { - out.Tests = append(out.Tests, raw) - continue - } - res.Packages, res.JudgedBy, res.Bound, res.SetupBound, res.OverWhich = nil, "", 0, 0, "" - small, err := json.Marshal(res) - if err != nil { - small = raw - } - out.Tests = append(out.Tests, small) - } - return out -} - -// DecodeFacts reads facts, refusing a version this program does not read, naming it. -func DecodeFacts(data []byte) (Facts, error) { - var f Facts - if err := json.Unmarshal(data, &f); err != nil { - return Facts{}, fmt.Errorf("rules: the facts are not JSON of their shape: %w", err) - } - if f.V != FactsVersion { - return Facts{}, fmt.Errorf("rules: facts of version %d, this checking program reads version %d", f.V, FactsVersion) - } - return f, nil -} - -// Counts are one level's counts across a check's members. -type Counts struct { - Run, Kept, Skipped int - Elapsed, Setup float64 - Lint float64 - Over bool - Slowest []TestTime -} - -// Tally is a check's counts by level. -type Tally map[string]*Counts - -// NewTally is a tally of both levels, at nothing. -func NewTally() Tally { - return Tally{buildstep.LevelUnit: {}, buildstep.LevelIntegration: {}} -} - -// Level judges one level of a member's check step as it was answered: the kept ones taken are counted, and every test -// result is read and counted, also after one that did not pass, so the verdict names every failed test of the level -// (issue 1921). It answers the level's failure (nil when it passed), its running time (its tests' and setup's) and the -// results read, for the running checker to keep the passes from. -func (t Tally) Level(repository string, number int, module, language, node string, la buildstep.LevelAnswer) (*meshdeliverychecker.VerdictsByGroupHeadsValue, float64, []Tested) { - a := t[la.Level] - if a == nil { - return failAt(node, fmt.Sprintf("the check of %s#%d answered the level %q, which is no level", repository, number, la.Level)), 0, nil - } - for _, k := range la.Kept { - a.Kept += k.Tests - } - elapsed := 0.0 - var results, failed []Tested - for _, raw := range la.Tests { - var res Tested - if err := json.Unmarshal(raw, &res); err != nil { - return failAt(node, fmt.Sprintf("the %s level of %s#%d answered what is not a result: %v", la.Level, repository, number, err)), elapsed, results - } - results = append(results, res) - elapsed += res.Elapsed + res.Setup + res.Lint - if language == LanguageTypeScript { - if !res.NoTests { - a.Run++ // a Node package is counted whole: its build module counts no tests (ADR 0484 clause 10) - } - } else { - a.Run += res.Tests - a.Skipped += res.Skipped - a.Slowest = append(a.Slowest, res.Slowest...) - } - a.Elapsed += res.Elapsed - a.Setup += res.Setup - a.Lint += res.Lint - a.Over = a.Over || res.OverBound - if !res.Passed { - failed = append(failed, res) - } - } - if len(failed) > 0 { - return LevelFailure(repository, module, language, la.Level, failed...), elapsed, results - } - return nil, elapsed, results -} - -// Judge is the verdict of the facts (Judged without its notes). -func Judge(f Facts) meshdeliverychecker.VerdictsByGroupHeadsValue { return Judged(f).Verdict } - -// Judged is the answer of the facts: every failure in the order met, joined and settled, or a pass; the levels' -// counts, times, machines and the levels over their bound; then the composition taken in; and the notes the rules give -// a reader (the manifest validator's warnings and acceptances). -func Judged(f Facts) Answer { - var notes []string - tally := NewTally() - var failures []*meshdeliverychecker.VerdictsByGroupHeadsValue - nodes := map[string]bool{} - for _, s := range f.Steps { - if s.Node != "" { - nodes[s.Node] = true - } - switch { - case s.Failed != nil: - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: s.Failed}) - case s.Level != nil: - if v, _, _ := tally.Level(s.Repository, s.Number, s.Module, s.Language, s.Node, *s.Level); v != nil { - failures = append(failures, v) - } - case s.Programs != nil: - if ff := JudgePrograms(s.Node, *s.Programs); ff != nil { - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: ff}) - } - case s.Graph != nil: - ff, said := JudgeGraph(*s.Graph, s.Module) - notes = append(notes, said...) - if ff != nil { - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: ff}) - } - case s.Renames != nil: - ffs, said := JudgeRenames(*s.Renames) - notes = append(notes, said...) - for _, ff := range ffs { - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &ff}) - } - case s.Unparsed != nil: - for _, ff := range JudgeUnparsed(*s.Unparsed) { - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &ff}) - } - case s.Manifests != nil: - ff, said := JudgeManifests(*s.Manifests, s.Module) - notes = append(notes, said...) - if ff != nil { - failures = append(failures, &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: ff}) - } - } - } - v := meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictPass} - if joined := Settled(JoinFailures(failures...)); joined != nil { - v = *joined - } - v.Group, v.Heads, v.Trunk, v.Series = f.Group, f.Heads, f.Trunk, f.Series - Outcome(&v, tally, f.CloneMS, nodes) - if f.Composition != nil { - JudgeComposition(&v, *f.Composition) - } - return Answer{V: AnswerVersion, Verdict: v, Notes: notes} -} - -// Outcome fills a verdict's levels (on a pass, or when it names none), times, machines and levels over their bound from -// the tally, and says its composition is not yet run (ADR 0484 clause 6). -func Outcome(v *meshdeliverychecker.VerdictsByGroupHeadsValue, tally Tally, cloneMS int64, nodes map[string]bool) { - if v.State == meshdeliverycheckerclient.VerdictPass || len(v.Levels) == 0 { - v.Levels = map[string]meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{} - for level, a := range tally { - v.Levels[level] = meshdeliverychecker.VerdictsByGroupHeadsValueLevelsValue{Run: int64(a.Run), Kept: int64(a.Kept), Skipped: int64(a.Skipped)} - } - } - ms := func(s float64) int64 { return int64(s * 1000) } - u, i := tally[buildstep.LevelUnit], tally[buildstep.LevelIntegration] - v.Took = &meshdeliverychecker.VerdictsByGroupHeadsValueTook{Clone: cloneMS, Unit: ms(u.Elapsed), Lint: ms(u.Lint), IntegrationSetup: ms(i.Setup), IntegrationRun: ms(i.Elapsed)} - v.Machines = nil - for node := range nodes { - v.Machines = append(v.Machines, node) - } - sort.Strings(v.Machines) - v.OverBound = nil - for _, level := range []string{buildstep.LevelUnit, buildstep.LevelIntegration} { - if tally[level].Over { - v.OverBound = append(v.OverBound, level) - } - } - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionNotRun} -} - -// Slowest are, of each level over its bound, its three slowest tests, worded for the status line. -func Slowest(tally Tally) []string { - var slow []string - for _, level := range []string{buildstep.LevelUnit, buildstep.LevelIntegration} { - a := tally[level] - if !a.Over { - continue - } - sort.SliceStable(a.Slowest, func(x, y int) bool { return a.Slowest[x].Elapsed > a.Slowest[y].Elapsed }) - for n, s := range a.Slowest { - if n == 3 { - break - } - slow = append(slow, fmt.Sprintf("%s %s %.1fs", level, path.Base(s.Package)+"."+s.Test, s.Elapsed)) - } - } - return slow -} - -func failAt(machine, words string) *meshdeliverychecker.VerdictsByGroupHeadsValue { - return &meshdeliverychecker.VerdictsByGroupHeadsValue{State: meshdeliverycheckerclient.VerdictFail, Failed: &FailedFirst{Machine: machine, Words: words}} -} - -// AnswerVersion is the version of Answer this program writes. -const AnswerVersion = 1 - -// Answer is what the checking program answers on the last line of its standard output: its version, the verdict and the -// notes its rules give a reader of the check. -type Answer struct { - V int `json:"v"` - Verdict meshdeliverychecker.VerdictsByGroupHeadsValue `json:"verdict"` - Notes []string `json:"notes,omitempty"` -} diff --git a/modules/mesh-delivery-checker/rules/graph.go b/modules/mesh-delivery-checker/rules/graph.go deleted file mode 100644 index 9e370d4f..00000000 --- a/modules/mesh-delivery-checker/rules/graph.go +++ /dev/null @@ -1,114 +0,0 @@ -package rules - -import ( - "fmt" - "slices" - "strings" -) - -// The module graph's cycle rule (novox/hq ADR 0603 §2, ADR 0614 §3, §6), judged by the checking program (ADR 0656 §5): -// the check refuses a change whose module graph has a cycle and names it; a cycle the trunk already has among the same -// modules is the trunk's, said in the notes. The running checker gathers the graph's ordering edges of the catalogue as -// the change leaves it and as the trunk has it, worked out with the contract's graph (sdk/graph: bases, the provisions -// and bus state a module awaits, the verb edges the graph keeps), as module names and the edge's kind, never the -// manifests themselves, so the facts stay within the run input's bound. - -// The kinds of an ordering edge. -const ( - EdgeBase = "base" - EdgeAwaits = "awaits" - EdgeInvokes = "invokes" -) - -// Edge is one ordering edge: From waits for To. Only is EdgeChange when only the change's graph has it, EdgeTrunk when -// only the trunk's does, empty when both have it. -type Edge struct { - From string `json:"from"` - To string `json:"to"` - Kind string `json:"kind"` - Only string `json:"only,omitempty"` -} - -// Where only one graph has an edge. -const ( - EdgeChange = "change" - EdgeTrunk = "trunk" -) - -// Graph is the module graph's facts: every ordering edge of either graph, between modules of the catalogue. Trunk says -// whether the trunk's graph was read; when it was not, every edge is the change's and no cycle is the trunk's. -type Graph struct { - Edges []Edge `json:"edges"` - Trunk bool `json:"trunk"` -} - -// JudgeGraph is the cycle rule over the graph's facts: the failure of a cycle the change makes, or the note of no cycle -// or of a cycle the trunk already has. -func JudgeGraph(g Graph, machine string) (*FailedFirst, []string) { - cycle := firstCycle(g.Edges, func(e Edge) bool { return e.Only != EdgeTrunk || !g.Trunk }) - if cycle == nil { - return nil, []string{"the change's module graph has no cycle"} - } - words := strings.Join(cycle, " -> ") - if g.Trunk { - among := firstCycle(g.Edges, func(e Edge) bool { - return e.Only != EdgeChange && slices.Contains(cycle, e.From) && slices.Contains(cycle, e.To) - }) - if among != nil { - return nil, []string{"the module graph has the cycle " + words + ", which the trunk already has among the same modules (ADR 0603 §2)"} - } - } - return &FailedFirst{Machine: machine, Words: fmt.Sprintf("the change's module graph has the cycle %s: these modules would "+ - "each wait for the other for good, through their bases, the provisions and bus state they await, or the verbs they invoke (ADR 0603 §2, ADR 0614 §6)", words)}, nil -} - -// firstCycle is the first cycle of the edges kept, as the modules it passes through with the first named again at its -// end, visiting the modules and each one's edges in sorted order (as sdk/graph's Cycle does), or nil for none. -func firstCycle(edges []Edge, keep func(Edge) bool) []string { - next := map[string][]string{} - for _, e := range edges { - if keep(e) && !slices.Contains(next[e.From], e.To) { - next[e.From] = append(next[e.From], e.To) - } - } - var names []string - for name, to := range next { - slices.Sort(to) - names = append(names, name) - } - slices.Sort(names) - const ( - unseen = iota - onPath - done - ) - state := map[string]int{} - var path []string - var visit func(string) []string - visit = func(name string) []string { - state[name] = onPath - path = append(path, name) - for _, to := range next[name] { - switch state[to] { - case onPath: - i := slices.Index(path, to) - return append(slices.Clone(path[i:]), to) - case unseen: - if c := visit(to); c != nil { - return c - } - } - } - path = path[:len(path)-1] - state[name] = done - return nil - } - for _, name := range names { - if state[name] == unseen { - if c := visit(name); c != nil { - return c - } - } - } - return nil -} diff --git a/modules/mesh-delivery-checker/rules/judged.go b/modules/mesh-delivery-checker/rules/judged.go deleted file mode 100644 index e981fc24..00000000 --- a/modules/mesh-delivery-checker/rules/judged.go +++ /dev/null @@ -1,288 +0,0 @@ -package rules - -import ( - "fmt" - nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" - ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" - "maps" - "slices" - "sort" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer" -) - -// The facts the manifest validator, the fetch's programs and the composer answered, and the rules that judge them -// (milestone 47/B0, D1b): the running checker asks and records; these rules give the verdict. - -// CatalogSeat is the seat the indexer holds: the module whose manifest claims it is the indexer, found by the claim and -// never by its name (novox/hq ADR 0633 clause 6, ADR 0601 §4). -const CatalogSeat = "mesh-catalog" - -// BetweenManifests is the manifest validator's name for what it judges between manifests (the catalogue as a whole). -const BetweenManifests = "(between manifests)" - -// Manifests are the manifest validator's answers on the manifests a check touches (ADR 0578 §2, ADR 0579 §2, ADR 0633 -// clause 6): its change and trunk cases where it judged (At), and, when a touched module claims the seat mesh-catalog, -// that indexer and the trunk validator's change case on it (IndexerAt names where it judged). -type Manifests struct { - Touched []string `json:"touched"` - At string `json:"at"` - Change composer.CaseVerdict `json:"change"` - Trunk composer.CaseVerdict `json:"trunk"` - Indexer string `json:"indexer,omitempty"` - IndexerAt string `json:"indexer-at,omitempty"` - OfIndexer *composer.CaseVerdict `json:"of-indexer,omitempty"` -} - -// Programs are what one member's fetch read of the change's compiled programs (issue 1577): Files nil when the build -// module reported none (an older module: not refused, the running checker warns), else each changed file that is one. -type Programs struct { - Key string `json:"key"` - Files *[]ProgramFile `json:"files"` -} - -// ProgramFile is a changed file that is a compiled program, its format (ELF, Mach-O or PE) and, when it was added in an -// earlier commit of the branch and removed later, that commit: the build step's own (buildstep.Program). -type ProgramFile = buildstep.Program - -// Composition is how the check's composition went (ADR 0593 §7, §9; ADR 0603 §10; ADR 0548 §5): kept from the check it -// carries, not run (NotRun says why), or composed by the engines; BuildsFailed are the candidate's builds that failed -// before it composed, worded by the running checker; Waits are what it composed without waiting on, as warnings. -type Composition struct { - Kept *meshdeliverychecker.VerdictsByGroupHeadsValueComposition `json:"kept,omitempty"` - NotRun string `json:"not-run,omitempty"` - BuildsFailed []FailedFirst `json:"builds-failed,omitempty"` - Waits []string `json:"waits,omitempty"` - Composed *Composed `json:"composed,omitempty"` - FPSet string `json:"fp-set,omitempty"` -} - -// Composed are the engines' answers to one composition: the nodes placed, each answering node's answer, each silent -// node with why (for the journal only), what composed (the node-engine's commit), the composer's own warnings and how -// long it took in milliseconds. -type Composed struct { - Group string `json:"group"` - Heads string `json:"heads"` - Composer string `json:"composer"` - Nodes []string `json:"nodes"` - Answers map[string]ne.ComposeCandidateAnswer `json:"answers,omitempty"` - Silent map[string]string `json:"silent,omitempty"` - Warnings []string `json:"warnings,omitempty"` - Took int64 `json:"took,omitempty"` -} - -// JudgeManifests is the failure of the manifests' facts (nil when they pass) and the notes a reader gets: the trunk -// validator refusing, holding or setting aside the new indexer's manifest fails first (ADR 0633 clause 6); else a new or -// changed manifest is judged in full, a held one is refused, and of one the change leaves as it is a problem the trunk -// already had is a warning while one the change causes fails the check (ADR 0579 §2). -func JudgeManifests(m Manifests, machine string) (*FailedFirst, []string) { - var notes []string - if m.Indexer != "" { - var problems []string - ok := false - var iv composer.ManifestRead - if m.OfIndexer != nil { - iv, ok = m.OfIndexer.Manifests[m.Indexer] - } - switch { - case !ok: - problems = append(problems, "the manifest validator gave no verdict") - case iv.Refused != "": - problems = append(problems, strings.Split(iv.Refused, "\n")...) - case !iv.Accepted: - problems = append(problems, "the manifest validator holds it, with no verdict yet") - case iv.SetAside != "": - problems = append(problems, "the manifest validator does not know its fields "+iv.SetAside+", so it waits") - } - if len(problems) > 0 { - return &FailedFirst{Machine: machine, Words: fmt.Sprintf("the indexer %s, the holder of the seat %s, would never run: the trunk's manifest validator at %s does not accept its manifest: %s", - m.Indexer, CatalogSeat, m.IndexerAt, strings.Join(problems, "; "))}, nil - } - notes = append(notes, fmt.Sprintf("the new indexer's manifest of %s is accepted by the trunk's manifest validator at %s", m.Indexer, m.IndexerAt)) - } - var refused, warned []string - for _, module := range slices.Sorted(maps.Keys(m.Change.Manifests)) { - r := m.Change.Manifests[module] - if r.Accepted { - continue - } - if r.Refused == "" { // held: a verdict not given is not one given (ADR 0633 clause 6) - if slices.Contains(m.Touched, module) { - refused = append(refused, fmt.Sprintf("the indexer's judge at %s holds the manifest of %s: it gave no verdict", m.At, module)) - } - continue - } - had := map[string]bool{} - for _, p := range strings.Split(m.Trunk.Manifests[module].Refused, "\n") { - had[p] = true - } - var caused, old []string - for _, p := range strings.Split(r.Refused, "\n") { - if slices.Contains(m.Touched, module) || !had[p] { - caused = append(caused, p) - } else { - old = append(old, p) - } - } - if len(caused) > 0 { - what := "the manifest of " + module - if module == BetweenManifests { - what = "the catalogue" - } - refused = append(refused, fmt.Sprintf("the indexer's judge at %s refuses %s: %s", m.At, what, strings.Join(caused, "; "))) - } - if len(old) > 0 { - warned = append(warned, module+": "+strings.Join(old, "; ")) - } - } - if len(warned) > 0 { - notes = append(notes, "warnings of the indexer's judge on manifests the change leaves as they are (ADR 0579 §2): "+strings.Join(warned, " | ")) - } - if len(refused) > 0 { - return &FailedFirst{Machine: machine, Words: strings.Join(refused, "\n")}, notes - } - return nil, append(notes, fmt.Sprintf("the manifests of %s are accepted by the indexer's judge at %s", strings.Join(m.Touched, ", "), m.At)) -} - -// JudgePrograms fails a member whose change adds or modifies a compiled program, naming each file (issue 1577); a fetch -// that reported none, or found none, passes. -func JudgePrograms(machine string, p Programs) *FailedFirst { - if p.Files == nil { - return nil - } - var found []string - for _, f := range *p.Files { - if f.Commit != "" { - found = append(found, fmt.Sprintf("%s: a compiled program added in %s and removed later: it stays in history; squash the branch (issue 1577)", f.Path, short12(f.Commit))) - continue - } - found = append(found, f.Path+": a compiled program is build output: remove it and add it to .gitignore (issue 1577)") - } - if len(found) == 0 { - return nil - } - return &FailedFirst{Machine: machine, Words: fmt.Sprintf("%s: %s", p.Key, strings.Join(found, "; "))} -} - -func short12(s string) string { - if len(s) > 12 { - return s[:12] - } - return s -} - -// JudgeEngines is the check's composition verdict from the engines' answers (ADR 0593 §7, §9): failed when any node's -// answer is refused, naming each node and each module refused with its words (the first refusing node is the machine); -// passed otherwise. Every placed node without an answer is named with nec.NoAnswer as a warning. -func JudgeEngines(c Composed) meshwants.CompositionsByGroupHeadsValue { - v := meshwants.CompositionsByGroupHeadsValue{Group: c.Group, Heads: c.Heads, State: meshdeliverycheckerclient.CompositionPass, Sdk: c.Composer, Took: c.Took} - sorted := append([]string(nil), c.Nodes...) - sort.Strings(sorted) - var fails []string - for _, node := range sorted { - a, answered := c.Answers[node] - if !answered { - v.Warnings = append(v.Warnings, node+": "+nec.NoAnswer) - continue - } - if a.Verdict == nec.NotJudged { - // Composed in the sandbox and not judged (ADR 0593 §9): named as no answer, with the composer's words; decided - // by the verdict, never by the words (ADR 0601 §4's kind). - v.Warnings = append(v.Warnings, node+": "+nec.NoAnswer+" ("+a.Refused+")") - continue - } - v.Machines = append(v.Machines, node) - if a.Verdict != nec.Refused { - continue - } - if v.Machine == "" { - v.Machine = node - } - var words []string - if a.Refused != "" { - words = append(words, a.Refused) - } - for _, m := range append(append([]ne.Module(nil), a.Modules...), a.Own) { - if m.State == nec.ModuleRefused { - words = append(words, m.Module+": "+m.Words) - } - } - if len(words) == 0 { - words = append(words, "refused, naming no module") - } - fails = append(fails, "not composed on "+node+": "+strings.Join(words, "; ")) - } - if len(fails) > 0 { - v.State, v.Words = meshdeliverycheckerclient.CompositionFail, strings.Join(fails, "; ") - } - if len(sorted) == 0 { - v.Warnings = append(v.Warnings, "no node is placed, so the change was composed on none") - } - v.Warnings = append(v.Warnings, c.Warnings...) - return v -} - -// JudgeComposition takes the composition's facts into the verdict: a failed build of the candidate fails it, named after -// the tests' failures; a failed composition fails it, naming its machine; a passed one with warnings says them. -func JudgeComposition(v *meshdeliverychecker.VerdictsByGroupHeadsValue, c Composition) { - if c.Kept != nil { - kept := *c.Kept - v.Composition = &kept - return - } - for _, f := range c.BuildsFailed { - if v.State != meshdeliverycheckerclient.VerdictFail { - v.State, v.Failed = meshdeliverycheckerclient.VerdictFail, &f - continue - } - AddFailure(v, f) - } - if c.NotRun != "" { - v.Composition = &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: meshdeliverycheckerclient.CompositionNotRun, Words: c.NotRun} - return - } - if c.Composed == nil { - return - } - comp := JudgeEngines(*c.Composed) - comp.Warnings = append(comp.Warnings, c.Waits...) // ADR 0603 §10 - v.Composition = compositionOf(comp) - if v.Took == nil { - v.Took = &meshdeliverychecker.VerdictsByGroupHeadsValueTook{} - } - v.Took.Composition = comp.Took - if comp.State == meshdeliverycheckerclient.CompositionPass && len(comp.Warnings) > 0 && v.Composition != nil { - v.Composition.Words = strings.Join(comp.Warnings, "; ") // passed with a warning (ADR 0593 §9) - } - if comp.State == meshdeliverycheckerclient.CompositionFail { - machine := comp.Machine - if machine == "" { - machine = "the composition" - } - f := FailedFirst{Machine: machine, Words: comp.Words} - if v.State == meshdeliverycheckerclient.VerdictFail { - AddFailure(v, f) // the tests failed too: the composition is named after them (the operator, 2026-11-02) - } else { - v.State, v.Failed = meshdeliverycheckerclient.VerdictFail, &f - } - } - if v.Composition != nil && v.Composition.State != meshdeliverycheckerclient.CompositionNotRun { - v.Composition.Fpset = c.FPSet // a carry keeps it while the fingerprints stay the same (ADR 0548 §5) - } -} - -// compositionWaits is the state of the assigner's composition answer that waits for what it composes from. -const compositionWaits = "waits" - -// compositionOf is what the verdict carries of a composition answer c: nil while c waits (the check is still running). -func compositionOf(c meshwants.CompositionsByGroupHeadsValue) *meshdeliverychecker.VerdictsByGroupHeadsValueComposition { - if c.State == compositionWaits { - return nil - } - return &meshdeliverychecker.VerdictsByGroupHeadsValueComposition{State: c.State, Words: c.Words, Sdk: c.Sdk, Machines: c.Machines} -} diff --git a/modules/mesh-delivery-checker/rules/language.go b/modules/mesh-delivery-checker/rules/language.go deleted file mode 100644 index ac7e4474..00000000 --- a/modules/mesh-delivery-checker/rules/language.go +++ /dev/null @@ -1,8 +0,0 @@ -package rules - -// LanguageTypeScript is the language of the toolchain whose build module counts a package whole, not its tests (ADR 0484 clause -// 10): the `on` of a build seat's acting rule, never the name of a build module (design 47 G A-82). -const LanguageTypeScript = "typescript" - -// LanguageGo is the language of the toolchain of the build module that judges the Go repositories and the manifests. -const LanguageGo = "go" diff --git a/modules/mesh-delivery-checker/rules/names.go b/modules/mesh-delivery-checker/rules/names.go deleted file mode 100644 index 5915cdd8..00000000 --- a/modules/mesh-delivery-checker/rules/names.go +++ /dev/null @@ -1,38 +0,0 @@ -package rules - -import checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - -// FailedFirst names what failed: a test (its package and name) or a machine, with the words of the failure. It is the shape -// of the verdict's `failed` and of each entry of its `failures` (the seat's two types of it have the same fields), so the -// rules judge with one and convert at the verdict's edge (failureItems, failedList). -type FailedFirst = checker.VerdictsByGroupHeadsValueFailed - -func failureItems(list []FailedFirst) []checker.VerdictsByGroupHeadsValueFailuresItem { - if list == nil { - return nil - } - out := make([]checker.VerdictsByGroupHeadsValueFailuresItem, len(list)) - for i, f := range list { - out[i] = checker.VerdictsByGroupHeadsValueFailuresItem(f) - } - return out -} - -func failedList(items []checker.VerdictsByGroupHeadsValueFailuresItem) []FailedFirst { - if items == nil { - return nil - } - out := make([]FailedFirst, len(items)) - for i, f := range items { - out[i] = FailedFirst(f) - } - return out -} - -// failedOf is the first failure a verdict names: the zero value of a verdict that names none. -func failedOf(v checker.VerdictsByGroupHeadsValue) FailedFirst { - if v.Failed == nil { - return FailedFirst{} - } - return *v.Failed -} diff --git a/modules/mesh-delivery-checker/rules/program.go b/modules/mesh-delivery-checker/rules/program.go deleted file mode 100644 index 9cc8b87b..00000000 --- a/modules/mesh-delivery-checker/rules/program.go +++ /dev/null @@ -1,54 +0,0 @@ -package rules - -import ( - "encoding/json" - "errors" - "flag" - "fmt" - "io" - "io/fs" -) - -// Program is the checking program's work (cmd/mesh-checking-program; novox/hq ADR 0644, ADR 0656): over its arguments, -// its input (read only when asked for the verdict) and the heads laid for it (nil when none are), the one JSON line it -// answers, or why it refuses. `-seats` answers the seat list of the heads (SeatsOf), `-verdict` the verdict of the -// facts in the input (Judged), refusing facts of a version it does not read. -func Program(args []string, input func() ([]byte, error), heads fs.FS) ([]byte, error) { - set := flag.NewFlagSet("mesh-checking-program", flag.ContinueOnError) - set.SetOutput(io.Discard) - verdict := set.Bool("verdict", false, "answer the verdict of the facts in the input") - seats := set.Bool("seats", false, "answer the seat list of the heads laid for it") - if err := set.Parse(args); err != nil { - return nil, err - } - switch { - case *seats: - if heads == nil { - return nil, errors.New("no heads are laid for it") - } - data, err := input() - if err != nil { - return nil, fmt.Errorf("the input cannot be read: %w", err) - } - facts, err := DecodeFacts(data) - if err != nil { - return nil, err - } - answer, err := SeatsOf(heads, facts) - if err != nil { - return nil, err - } - return json.Marshal(answer) - case *verdict: - data, err := input() - if err != nil { - return nil, fmt.Errorf("the input cannot be read: %w", err) - } - facts, err := DecodeFacts(data) - if err != nil { - return nil, err - } - return json.Marshal(Judged(facts)) - } - return nil, errors.New("nothing asked: give -verdict or -seats") -} diff --git a/modules/mesh-delivery-checker/rules/refusals.go b/modules/mesh-delivery-checker/rules/refusals.go deleted file mode 100644 index 88142158..00000000 --- a/modules/mesh-delivery-checker/rules/refusals.go +++ /dev/null @@ -1,99 +0,0 @@ -package rules - -import ( - "fmt" - "maps" - "slices" - "strings" - - "git.novox.be/novox/mesh-contracts/go/seats/meshconfig" -) - -// The refusals of what the running checker asked of other seats for a check (milestone 47/G): it asks and records the -// answers as facts, and these rules decide what fails. - -// RenamesSeat is the verb a change that brings or drops a `renamed-from` is asked of (design 32). -var RenamesSeat = "seat:" + meshconfig.Seat + "." + meshconfig.VerbRenames - -// Renames is what one module of a member brings and drops of `renamed-from` (key to the old key), and how the seat -// mesh-config's verb renames answered for it: Answer when it answered; else Unasked says why it was not asked or did not -// answer, NotServed that its holder does not serve the verb. Unread says the module's manifests could not be read. -type Renames struct { - Module string `json:"module"` - Key string `json:"key"` - Brought map[string]string `json:"brought,omitempty"` - Dropped map[string]string `json:"dropped,omitempty"` - Answer *meshconfig.RenamesAnswer `json:"answer,omitempty"` - Unasked string `json:"unasked,omitempty"` - NotServed bool `json:"not-served,omitempty"` - Unread string `json:"unread,omitempty"` -} - -// JudgeRenames fails a rename the seat cannot move the value of, and the drop of a `renamed-from` while its window is -// open on a node; it says which module the seat answered for. -func JudgeRenames(r Renames) (failed []FailedFirst, notes []string) { - at := func(words string) { failed = append(failed, FailedFirst{Machine: meshconfig.Seat, Words: words}) } - switch { - case r.Unread != "": - at(fmt.Sprintf("%s (%s): its config renames cannot be judged: %s", r.Module, r.Key, r.Unread)) - return failed, nil - case r.NotServed: - for _, key := range slices.Sorted(maps.Keys(r.Brought)) { - at(fmt.Sprintf("%s (%s) renames %s from %s: refused, %s, so the value cannot be moved", r.Module, r.Key, key, r.Brought[key], r.Unasked)) - } - for _, key := range slices.Sorted(maps.Keys(r.Dropped)) { - at(fmt.Sprintf("%s (%s) drops renamed-from %s of %s: refused, %s, so whether its window is open is not known", r.Module, r.Key, r.Dropped[key], key, r.Unasked)) - } - return failed, nil - case r.Answer == nil && r.Unasked == "": - at(fmt.Sprintf("%s (%s) changes a renamed-from, and this checker cannot ask %s", r.Module, r.Key, RenamesSeat)) - return failed, nil - case r.Answer == nil: - at(fmt.Sprintf("%s (%s) changes a renamed-from, and the check cannot ask %s: %s", r.Module, r.Key, RenamesSeat, r.Unasked)) - return failed, nil - } - for _, key := range slices.Sorted(maps.Keys(r.Dropped)) { - for _, n := range r.Answer.Renames { - if n.Module == r.Module && n.From == r.Dropped[key] && n.To == key && len(n.Open) > 0 { - open := slices.Sorted(slices.Values(n.Open)) - at(fmt.Sprintf("%s (%s) drops renamed-from %s of %s while its rename window is open on %s", r.Module, r.Key, n.From, key, strings.Join(open, ", "))) - } - } - } - return failed, []string{fmt.Sprintf("%s.%s answered for %s (%s)", meshconfig.Seat, meshconfig.VerbRenames, r.Module, r.Key)} -} - -// SeatDefinitionsMachine names what refused a seat definition in a verdict's failure. -const SeatDefinitionsMachine = "seat-definitions" - -// UnparsedSeat is one seat definition of a pull request's head that does not parse, as the indexer keeps it in its state -// seat-definitions: the seat (the contracts module's name when its index did not parse), its module's directory and the -// indexer's words of why. -type UnparsedSeat struct { - Seat string `json:"seat"` - Module string `json:"module"` - Directory string `json:"directory"` - Why string `json:"why"` -} - -// Unparsed is what the indexer keeps of one pull request's seat definitions that do not parse; Unread says why its state -// could not be read. -type Unparsed struct { - Key string `json:"key"` - Seats []UnparsedSeat `json:"seats,omitempty"` - Unread string `json:"unread,omitempty"` -} - -// JudgeUnparsed fails a pull request whose head's seat definition does not parse, naming the seat and where it lives -// (design 26; the lead's ruling on A2i's finding 1), and one whose seat definitions cannot be read, since none is then -// known to parse. -func JudgeUnparsed(u Unparsed) []FailedFirst { - if u.Unread != "" { - return []FailedFirst{{Machine: SeatDefinitionsMachine, Words: fmt.Sprintf("%s: the indexer's seat definitions of it cannot be read, so none is known to parse: %s", u.Key, u.Unread)}} - } - var out []FailedFirst - for _, s := range u.Seats { - out = append(out, FailedFirst{Machine: SeatDefinitionsMachine, Words: fmt.Sprintf("%s: the seat definition of %s (module %s, %s) does not parse: %s", u.Key, s.Seat, s.Module, s.Directory, s.Why)}) - } - return out -} diff --git a/modules/mesh-delivery-checker/rules/results.go b/modules/mesh-delivery-checker/rules/results.go deleted file mode 100644 index a0a3af40..00000000 --- a/modules/mesh-delivery-checker/rules/results.go +++ /dev/null @@ -1,63 +0,0 @@ -package rules - -import "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - -// The shapes of a test step's answer, as the build modules' code writes them (mesh-catalog modules/mesh-build-go -// cmd/mesh-build-go levels.go TestResult; modules/mesh-build-node cmd/mesh-build-node check.go TestResult). Only the -// fields the rules read are named; the rest is ignored. - -// GoPackage is one package of a Go test result. -type GoPackage struct { - Dir string `json:"dir"` - Package string `json:"package"` - Passed bool `json:"passed"` - Elapsed float64 `json:"elapsed"` - Tests int `json:"tests"` - NoTests bool `json:"no_tests,omitempty"` - // Skipped is how many of the package's tests skipped; an older build module names none. - Skipped int `json:"skipped,omitempty"` -} - -// Failure is a test that failed, or a package that failed with no test to name: the build modules' failure contract -// (buildstep.Failure, issue 1901), its cause never empty and never a passing line from a build module that keeps it. -type Failure = buildstep.Failure - -// TestTime is one test and the seconds it took. -type TestTime struct { - Package string `json:"package"` - Test string `json:"test"` - Elapsed float64 `json:"elapsed"` -} - -// Tested is the answer of a test step of either build module (the Go one names packages and counts tests; the Node one -// names the package directory as Module and counts none). -type Tested struct { - Level string `json:"level"` - Module string `json:"module,omitempty"` - Passed bool `json:"passed"` - Why string `json:"why,omitempty"` - NoTests bool `json:"no_tests,omitempty"` - Tests int `json:"tests"` - Skipped int `json:"skipped"` - Packages []GoPackage `json:"packages,omitempty"` - Elapsed float64 `json:"elapsed"` - Setup float64 `json:"setup,omitempty"` - // Lint is the unit level's gofmt and go vet time, before its tests (mesh-build-go, ADR 0529 §1). - Lint float64 `json:"lint,omitempty"` - Bound float64 `json:"bound"` - SetupBound float64 `json:"setup_bound,omitempty"` - Slowest []TestTime `json:"slowest,omitempty"` - Failed []Failure `json:"failed,omitempty"` - OverBound bool `json:"over_bound"` - OverWhich string `json:"over_which,omitempty"` - // JudgedBy is the program that ran the step (ADR 0535 §5): "installed", or "candidate " under runner - // workspace; absent from a build module that does not take the runner. - JudgedBy string `json:"judged_by,omitempty"` - // By is the build module that answered and the SDK it was built with (buildstep.By); absent from a build module from - // before the failure contract. - By string `json:"by,omitempty"` - // OutOfStep are the generated files the step's regenerate-and-compare found out of step with their seat definitions, - // each a repository-relative path (design 26): the running checker names them in its verdict event. Absent from a - // build module that does not compare. - OutOfStep []string `json:"out-of-step,omitempty"` -} diff --git a/modules/mesh-delivery-checker/rules/seats.go b/modules/mesh-delivery-checker/rules/seats.go deleted file mode 100644 index 50040e14..00000000 --- a/modules/mesh-delivery-checker/rules/seats.go +++ /dev/null @@ -1,343 +0,0 @@ -package rules - -import ( - "bytes" - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "io/fs" - "maps" - "path" - "slices" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/seatdefs" -) - -// The seat list a check judges and composes against (novox/hq ADR 0636 §1, §4, ADR 0644 §1; the lead's rulings Q2 on -// milestones 47/B0 and 47/G): the checking program built from the heads reads the definitions the heads' contracts -// modules carry, by the domain SDK's one reading (sdk/seatdefs), the indexer's too, and answers them; the running checker -// lays them by seat over the list its reader holds from the seat mesh-catalog (verb seat-definitions) and reads no file -// of the heads. The reader's list is not in the program's input: it is large, and the answer carries only what the heads -// change. - -// SeatsVersion is the version of SeatsAnswer this program writes. -const SeatsVersion = 1 - -// SeatsAnswer is the checking program's `-seats` answer: every seat definition the heads carry, resolved, as the requests' -// field `seats` carries them, sorted by seat, that is new or differs from the reader's (Facts.SeatDigests); how many -// were the same and left out of it (Same); and each definition left out, with why. -type SeatsAnswer struct { - V int `json:"v"` - Seats []json.RawMessage `json:"seats"` - Same int `json:"same,omitempty"` - Left []LeftOut `json:"left,omitempty"` -} - -// SeatDigest is a definition's digest, the same for the same definition whatever its bytes' order and spacing: the sha256 -// of its JSON with every object's keys sorted, numbers as written. Bytes that are not JSON are digested as given. -func SeatDigest(raw json.RawMessage) string { - d := json.NewDecoder(bytes.NewReader(raw)) - d.UseNumber() - var v any - canonical := []byte(raw) - if d.Decode(&v) == nil { - if out, err := json.Marshal(v); err == nil { - canonical = out - } - } - sum := sha256.Sum256(canonical) - return hex.EncodeToString(sum[:]) -} - -// LeftOut is a seat definition the list does not take from the heads: its file and why. -type LeftOut struct { - Path string `json:"path"` - Why string `json:"why"` -} - -// SeatsOf is the seat definitions of the facts' members' heads that are new or differ from the reader's (Facts.SeatDigests). heads holds each member's head at its repository's path; a contracts module is a manifest there whose package artifact names -// `generated-from` (seatdefs.Of). A seat of the mesh's own a module that is not its domain's defines (seatdefs.Refusal), -// and a seat two modules define where neither hands it over, are left out, said; a definition that does not parse is an -// error. -// Heads with no contracts module give an empty list. -func SeatsOf(heads fs.FS, f Facts) (SeatsAnswer, error) { - out := SeatsAnswer{V: SeatsVersion} - type taken struct { - raw json.RawMessage - module, file string - } - by := map[string]taken{} - refused := map[string]bool{} - repositories := map[string]string{} - for _, m := range f.Members { - repositories[m.Repository] = m.Head - } - for _, repository := range slices.Sorted(maps.Keys(repositories)) { - if _, err := fs.Stat(heads, repository); errors.Is(err, fs.ErrNotExist) { - continue // no head of it is laid: it carries no definition here - } - sub, err := fs.Sub(heads, repository) - if err != nil { - return SeatsAnswer{}, err - } - manifests, err := manifestsIn(sub) - if err != nil { - return SeatsAnswer{}, err - } - forge := localForge(sub) - for _, c := range seatdefs.ContractsModules(manifests) { - defs, unparsed, err := forge.Read(context.Background(), repository, repositories[repository], c) - if err != nil { - return SeatsAnswer{}, fmt.Errorf("the seat definitions of %s at the head of %s cannot be read: %w", c.Module, repository, err) - } - // A head's definition that does not parse, or names another seat than its directory's, leaves the group's seats - // unknown: no list is made, so no manifest is judged by a list without it (design 26). - for _, seat := range slices.Sorted(maps.Keys(unparsed)) { - return SeatsAnswer{}, fmt.Errorf("the seat definition of %s (module %s) at the head of %s is not taken: %s", seat, c.Module, repository, unparsed[seat]) - } - for _, seat := range slices.Sorted(maps.Keys(defs)) { - d, file := defs[seat], defFile(repository, c, seat) - if why := seatdefs.Refusal(c.Module, d); why != "" { - out.Left = append(out.Left, LeftOut{Path: file, Why: why}) - continue - } - if prev, twice := by[seat]; twice || refused[seat] { - switch { - case refused[seat]: - out.Left = append(out.Left, LeftOut{Path: file, Why: "the seat " + seat + " is defined in more than one place"}) - continue - case seatdefs.HandsOver(prev.module, c.Module, d): - case seatdefs.HandsOver(c.Module, prev.module, d): - continue - default: - why := "the seat " + seat + " is defined in more than one place: " + prev.file + ", " + file - out.Left = append(out.Left, LeftOut{Path: prev.file, Why: why}, LeftOut{Path: file, Why: why}) - delete(by, seat) - refused[seat] = true - continue - } - } - raw, err := json.Marshal(d) - if err != nil { - out.Left = append(out.Left, LeftOut{Path: file, Why: err.Error()}) - continue - } - by[seat] = taken{raw: raw, module: c.Module, file: file} - } - } - } - out.Seats = make([]json.RawMessage, 0, len(by)) - for _, seat := range slices.Sorted(maps.Keys(by)) { - if held, ok := f.SeatDigests[seat]; ok && held == SeatDigest(by[seat].raw) { - out.Same++ // the reader holds it as the heads carry it: the checker's own list keeps it - continue - } - raw := by[seat].raw - // The answer is the judging view of a seat, what the manifest validator and the composer read (the lead's ruling after - // the group check of 616c42434ac2), with digests or without: no verb's or message's schema, which neither reads, and - // which the build seat's step state, keeping this answer, would refuse where a property is named like a credential - // (mesh-vault's verb input `credentials`). - raw = withoutMessageSchemas(raw) - if f.SeatDigests == nil { - // A checker from before G hands no digests, so every definition is answered: whole they are over a run answer's - // bound, so every description but the seat's own is left out too, which decides no verdict; review round 2 of G. - raw = withoutInnerDescriptions(raw) - } - out.Seats = append(out.Seats, raw) - } - return out, nil -} - -// defFile is a seat's definition file in a head, by its repository's path. -func defFile(repository string, c seatdefs.Module, seat string) string { - return path.Join(repository, c.Defs, "seats", seat, "seat.json") -} - -// manifestsIn are the raw manifests of a head by their directory (the root's is "."), the generated contracts, the tests' -// data and the packages' dependencies left out. -func manifestsIn(head fs.FS) (map[string]string, error) { - out := map[string]string{} - err := fs.WalkDir(head, ".", func(p string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() && (d.Name() == ".git" || d.Name() == "testdata" || d.Name() == "node_modules") { - return fs.SkipDir - } - if !d.IsDir() && d.Name() == "module.json" { - raw, err := fs.ReadFile(head, p) - if err != nil { - return err - } - out[path.Dir(p)] = string(raw) - } - return nil - }) - return out, err -} - -// localForge is the domain SDK's reading bound to a head laid on disk: a directory or a file the head does not hold is -// none. -func localForge(head fs.FS) seatdefs.Forge { - return seatdefs.Forge{ - Files: func(_ context.Context, _, _ string, dirs []string) (map[string]map[string]string, error) { - out := map[string]map[string]string{} - for _, dir := range dirs { - files := map[string]string{} - err := fs.WalkDir(head, dir, func(p string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if !d.IsDir() { - files[strings.TrimPrefix(p, dir+"/")] = "" - } - return nil - }) - if errors.Is(err, fs.ErrNotExist) { - continue - } - if err != nil { - return nil, err - } - out[dir] = files - } - return out, nil - }, - File: func(_ context.Context, _, _, file string) ([]byte, error) { - raw, err := fs.ReadFile(head, file) - if errors.Is(err, fs.ErrNotExist) { - return nil, nil - } - return raw, err - }, - } -} - -// withoutMessageSchemas is a definition whose verbs' input and answer, and whose accepted messages', events' and holder -// events' bodies, are each the empty schema, and whose types (`defs`) are only those something it keeps still refers to -// (a state's key forms, a provision's form, another kept type). Every other field is kept as written; bytes that are not -// a JSON object are given back as they are. -func withoutMessageSchemas(raw json.RawMessage) json.RawMessage { - d := json.NewDecoder(bytes.NewReader(raw)) - d.UseNumber() - var def map[string]any - if d.Decode(&def) != nil { - return raw - } - empty := func(list any, fields ...string) { - items, _ := list.([]any) - for _, it := range items { - m, ok := it.(map[string]any) - if !ok { - continue - } - for _, f := range fields { - if _, has := m[f]; has { - m[f] = map[string]any{} - } - } - } - } - empty(def["verbs"], "input", "answer") - for _, field := range []string{"accepts", "events", "holder-events"} { - empty(def[field], "body") - } - if defs, ok := def["defs"].(map[string]any); ok { - rest := maps.Clone(def) - delete(rest, "defs") - kept := map[string]bool{} - todo := typesReferred(rest) - for len(todo) > 0 { - name := todo[len(todo)-1] - todo = todo[:len(todo)-1] - if kept[name] || defs[name] == nil { - continue - } - kept[name] = true - todo = append(todo, typesReferred(defs[name])...) - } - maps.DeleteFunc(defs, func(name string, _ any) bool { return !kept[name] }) - if len(defs) == 0 { - delete(def, "defs") - } - } - out, err := json.Marshal(def) - if err != nil { - return raw - } - return out -} - -// typesReferred are the names of the definition's types (`{"$ref": "#/defs/"}`) v refers to, anywhere in it. -func typesReferred(v any) []string { - var out []string - var walk func(any) - walk = func(v any) { - switch t := v.(type) { - case map[string]any: - if ref, ok := t["$ref"].(string); ok { - if name, found := strings.CutPrefix(ref, "#/defs/"); found { - out = append(out, name) - } - } - for _, x := range t { - walk(x) - } - case []any: - for _, x := range t { - walk(x) - } - } - } - walk(v) - return out -} - -// schemaData are the keywords of a schema whose value is data, never a schema: what they hold is kept as written. -var schemaData = map[string]bool{"examples": true, "enum": true, "const": true, "default": true} - -// withoutInnerDescriptions is a definition with only the seat's own description kept: every other `description` string, -// of a verb, an event or anything inside their schemas, is left out, every other field kept (a property named -// description is a schema, kept; a schema's data keywords are kept whole). Bytes that are not a JSON object are given -// back as they are. -func withoutInnerDescriptions(raw json.RawMessage) json.RawMessage { - d := json.NewDecoder(bytes.NewReader(raw)) - d.UseNumber() - var def map[string]any - if d.Decode(&def) != nil { - return raw - } - var walk func(v any) - walk = func(v any) { - switch t := v.(type) { - case map[string]any: - for k, val := range t { - if _, prose := val.(string); prose && k == "description" { - delete(t, k) - continue - } - if !schemaData[k] { - walk(val) - } - } - case []any: - for _, e := range t { - walk(e) - } - } - } - for k, val := range def { - if k != "description" { - walk(val) - } - } - out, err := json.Marshal(def) - if err != nil { - return raw - } - return out -} diff --git a/modules/mesh-delivery-checker/rules/words.go b/modules/mesh-delivery-checker/rules/words.go deleted file mode 100644 index 57c232d0..00000000 --- a/modules/mesh-delivery-checker/rules/words.go +++ /dev/null @@ -1,281 +0,0 @@ -package rules - -import ( - "fmt" - "strings" - - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" -) - -// The words rules: how a failed test, a failed package and a list of failures are worded in a verdict (issue 1901, issue -// 1921). Moved whole from the running checker's package (milestone 47/B0): they are the checking program's. - -// FailedName names a failure: its package and test. -func FailedName(f Failure) string { - if f.Test == "" { - return f.Package - } - return f.Package + "." + f.Test -} - -// MaxWords is the most a failed test's words in a verdict hold (issue 1901). **The words keep the failure's last lines**, -// where a test says why it failed: the first line is often something it logged before (the group r4g verdict kept a -// "judged against" line and lost the t.Fatalf after it), and the 120 characters kept before cut a docker command's echo -// before docker's own error. Four kilobytes holds a long message and is small beside a verdict's other parts. -const MaxWords = 4096 - -// FailureWords is a failure's words: its output's last lines up to MaxWords, with the line the build module told apart as -// the test's error first when the output does not start with it, so the first line of the words is the cause and the -// status line, which is cut short, names it. **A failure's words are never "PASS" or "ok"** (issue 1901, its third -// meeting: a package that failed after its tests passed was worded "PASS"): go test's summary lines are dropped, and with -// no cause told apart (an older build module) the cause is the output's first "panic:", "fatal error:" or "signal:" -// line, read before the output is cut, or the words say the tests passed and the program failed after them. -func FailureWords(f Failure) string { - return FailureWordsAfter(f, "") -} - -// FailureWordsAfter is FailureWords with the line loud put first, within the same MaxWords: what the checker itself says of -// the failure (that the build module hid it), which the status line then names. -func FailureWordsAfter(f Failure, loud string) string { - out, passed := DropSummary(strings.TrimSpace(f.Output)) - cause := strings.TrimSpace(f.Cause) - if cause == "" { - cause = CauseIn(out) - } - switch { - case cause == "" && out == "" && passed: - cause = "the tests passed, then the test program exited with a failure: an os.Exit or a panic in TestMain or after the tests" - case cause == "" && out == "": - // The build module on main sends a package that stopped (a timeout, an exit, a kill) as its "FAIL" line alone. - cause = "the package failed with no test to name and no panic, signal or build error in its output (an older build module sent no cause)" - } - if len(cause) > MaxCause { - cause = strings.ToValidUTF8(cause[:MaxCause], "") + "..." - } - // The words keep MaxWords exactly: the loud line, the cause and their newlines, the marker and the kept lines. - budget := MaxWords - if cause != "" { - budget -= len(cause) + 1 - } - if loud != "" { - budget -= len(loud) + 1 - } - if len(out) > budget { - const marker = "... " - tail := out[len(out)-(budget-len(marker)):] - // Start on a whole line, and a whole character. - if i := strings.IndexByte(tail, '\n'); i >= 0 { - tail = strings.TrimLeft(tail[i:], "\n") - } - out = marker + strings.ToValidUTF8(tail, "") - } - if cause != "" && !strings.HasPrefix(FirstLine(out), cause) { - out = strings.TrimRight(cause+"\n"+out, "\n") - } - if loud != "" { - out = strings.TrimRight(loud+"\n"+out, "\n") - } - return out -} - -// HidWords is the checker's loud line for a failure the build module sent hiding its cause (issue 1901, its thorough -// fix): with no cause, or a passing line as its cause, under the failure contract (buildstep.Hidden). It names the build -// module and the version it says it is (Tested.By, " with mesh-sdk "); one from before the contract -// says nothing of itself. "" when the failure keeps the contract. The check fails either way: only its words change. -func HidWords(module, by string, f Failure) string { - why := buildstep.Hidden(f) - if why == "" { - return "" - } - version := "a version before the failure contract" - if module == "" { - module = "an unnamed build module" - } - if m, v, ok := strings.Cut(by, " with "); ok && m != "" { - module, version = m, v - } - return fmt.Sprintf("the build module hid the failure (%s at %s): %s", module, version, why) -} - -// MaxCause is the most a cause in a failure's words holds: one long line (the 1901 review: a cause of a kilobyte could -// push the words past MaxWords). -const MaxCause = 1024 - -// DropSummary drops go test's summary lines from a failure's output ("PASS", "FAIL", "ok ", "FAIL "), and says -// whether a "PASS" was among them: the package's tests passed before it failed. -func DropSummary(out string) (string, bool) { - passed := false - lines := strings.Split(out, "\n") - kept := lines[:0] - for _, l := range lines { - // go's exact forms only (review round 1): "PASS" or "FAIL" alone, "ok \t\t