diff --git a/cmd/mesh-compose/main.go b/cmd/mesh-compose/main.go index d8f5d8a6..dc12980d 100644 --- a/cmd/mesh-compose/main.go +++ b/cmd/mesh-compose/main.go @@ -152,7 +152,11 @@ func runWithSeats(req composer.Request) (composer.Verdict, error) { for i := range v.Cases { v.Cases[i].Machines = map[string]composer.Machine{} for _, node := range machines { - v.Cases[i].Machines[node] = composer.Machine{NotJudged: NoSeatList} + // Blamed is always written, blaming none here (design 47 G §721, A-469h). + v.Cases[i].Machines[node] = composer.Machine{NotJudged: NoSeatList, Blamed: []struct { + Module string `json:"module"` + Problem string `json:"problem"` + }{}} } } return v, nil diff --git a/cmd/mesh-compose/main_test.go b/cmd/mesh-compose/main_test.go index eeef56c4..07d216b7 100644 --- a/cmd/mesh-compose/main_test.go +++ b/cmd/mesh-compose/main_test.go @@ -161,9 +161,112 @@ func TestTheCommandWritesOneVerdictPerManifestAndMachine(t *testing.T) { if m := resolverMachine(true); strings.Contains(m.Reason+strings.Join(m.Problems, " "), "both declare") || m.NotJudged != "" { t.Errorf("with the seat list, the uplink's and the resolver's holders on one machine: %+v, want the uplink's stepping back", m) } - if m := resolverMachine(false); m.Composes || m.NotJudged != NoSeatList || m.Reason != "" { - t.Errorf("without a seat list: %+v, want not judged, saying why", m) + if m := resolverMachine(false); m.Composes || m.NotJudged != NoSeatList || m.Reason != "" || m.Blamed == nil { + t.Errorf("without a seat list: %+v, want not judged, saying why, blaming none", m) } + + // Design 47 G §721, §733 (the node-engine's composer): a machine's verdict names the modules it blames, each with its + // problem, and always writes blamed (A-469h). On a node composed as its node-engine composes it (ComposedHow), blamed is + // the modules the change refuses that the node held neither in its read nor composing without the change (A-469j). + file := func(module, path, content string) string { + return `{"module": "` + module + `", "version": "1", "resources": [{"id": "f", "type": "file", "path": "` + path + + `", "mode": "0600", "content": "` + content + `"}]}` + } + plain := func(module string) string { return `{"module": "` + module + `", "version": "1"}` } + secret := `N=${secret:repository}\\n` + search := map[string]string{"m1": plain("m1"), "m2": file("m2", "/var/lib/m2/env", secret), "m3": plain("m3")} + for _, row := range []struct { + name string + manifests map[string]string + assigned []string + how *compose.ComposedHow + tries int // the request's blame_tries + blamed []blame // each module blamed, its problem holding the words given + }{ + // G-24 H-AC1: a fixture module whose manifest is refused on the machine is blamed by name. + {"a module the change refuses is blamed by name", map[string]string{"app": file("app", "/var/lib/app/env", `N=${secret:repository}\\n`)}, + []string{"app"}, &compose.ComposedHow{Node: "a", Builds: map[string]string{}}, 20, []blame{{Module: "app", Problem: "repository"}}}, + // Held composing without the change (HeldBefore): left out of blamed. + {"a module held before the change is not blamed", map[string]string{"app": file("app", "/var/lib/app/env", `N=${secret:repository}\\n`)}, + []string{"app"}, &compose.ComposedHow{Node: "a", Builds: map[string]string{}, HeldBefore: map[string]string{"app": "held"}}, 0, []blame{}}, + // G-24 H-AC2 (A-468l): two modules owning one path are both blamed, with that path. + {"two modules owning one path are both blamed", map[string]string{"one": file("one", "/etc/shared.conf", "x"), "two": file("two", "/etc/shared.conf", "y")}, + []string{"one", "two"}, &compose.ComposedHow{Node: "a", Builds: map[string]string{}}, 0, + []blame{{Module: "one", Problem: `one and two both declare the path "/etc/shared.conf"`}, {Module: "two", Problem: `one and two both declare the path "/etc/shared.conf"`}}}, + // The same, on a node whose input says not how it composes: nothing is held, both owners are blamed from the refusal. + {"two path owners, composed without how the node composes", map[string]string{"one": file("one", "/etc/shared.conf", "x"), "two": file("two", "/etc/shared.conf", "y")}, + []string{"one", "two"}, nil, 20, + []blame{{Module: "one", Problem: `one and two both declare the path "/etc/shared.conf"`}, {Module: "two", Problem: `one and two both declare the path "/etc/shared.conf"`}}}, + // G-24 H-AC5: a machine that fails with no module held is searched over the changed modules placed on it (here every + // one, the input naming no manifest its builds were made of, A-469k), in assignment order; the search ends at the + // first whose hold ends the problem, so m3 is never tried. + {"the search ends at the first module whose hold ends the problem", search, []string{"m1", "m2", "m3"}, &compose.ComposedHow{Node: "a"}, 20, + []blame{{Module: "m2", Problem: "repository"}}}, + // A module whose case manifest is the one its build was made of is not changed (A-469k), so never held: m2's + // problem is then no changed module's, and none is blamed. + {"a module its build was made of is not tried", search, []string{"m1", "m2", "m3"}, + &compose.ComposedHow{Node: "a", Manifests: map[string]string{"m2": digest(t, search["m2"])}}, 20, []blame{}}, + // A-470v: a hold that changes the problem but leaves the machine not composing keeps that module held and blamed, + // and the search goes on against the retry's problem: both modules the change refuses are blamed. + {"every module the change refuses is blamed", map[string]string{"m1": file("m1", "/var/lib/m1/env", secret), "m2": file("m2", "/var/lib/m2/env", secret), "m3": plain("m3")}, + []string{"m1", "m2", "m3"}, &compose.ComposedHow{Node: "a"}, 20, []blame{{Module: "m1", Problem: "repository"}, {Module: "m2", Problem: "repository"}}}, + // G-24 H-AC4: past the request's blame_tries the modules not yet tried are named so. + {"past blame_tries the untried are named", search, []string{"m1", "m2", "m3"}, &compose.ComposedHow{Node: "a"}, 1, + []blame{{Module: "m2", Problem: composer.NotTried}, {Module: "m3", Problem: composer.NotTried}}}, + // A-468i: a request with no blame_tries gets no search; the machine fails on the node's own account (H-AC3). + {"a request with no blame_tries is not searched", search, []string{"m1", "m2", "m3"}, &compose.ComposedHow{Node: "a"}, 0, []blame{}}, + // A-469j: one search, the composer's, inside the node-engine's composition (ComposeSent): an input that says not how + // its node composes is composed without one, so its failure is the node's own. + {"a node that says not how it composes is not searched", search, []string{"m1", "m2", "m3"}, nil, 20, []blame{}}, + } { + t.Run(row.name, func(t *testing.T) { + r := composer.Request{V: composer.Version, Depth: composer.DepthCompose, Machines: []string{"a"}, Seats: seats(t), BlameTries: row.tries, + Cases: []composer.Case{{Name: "change", Manifests: map[string]composer.Manifest{}}}} + for name, raw := range row.manifests { + r.Cases[0].Manifests[name] = composer.Manifest{Raw: json.RawMessage(raw)} + } + r = withInput(t, r, compose.Input{Nodes: []compose.Node{{Name: "a", Assigned: row.assigned}}, ComposedHow: row.how}) + var out, errOut bytes.Buffer + if code := run(nil, bytes.NewReader(encode(t, r)), &out, &errOut); code != 0 { + t.Fatalf("exit %d: %s", code, errOut.String()) + } + if !strings.Contains(out.String(), `"blamed":`) { + t.Errorf("the verdict writes no blamed: %s", out.String()) + } + var v composer.Verdict + if err := json.Unmarshal(out.Bytes(), &v); err != nil { + t.Fatal(err) + } + m := v.Cases[0].Machines["a"] + if held := row.how != nil && row.how.HeldBefore != nil; m.Composes != held { + t.Errorf("composes = %v, want %v: %+v", m.Composes, held, m) + } + if len(m.Blamed) != len(row.blamed) { + t.Fatalf("blamed %+v, want %+v (%+v)", m.Blamed, row.blamed, m) + } + for i, b := range row.blamed { + if m.Blamed[i].Module != b.Module || !strings.Contains(m.Blamed[i].Problem, b.Problem) { + t.Errorf("blamed %+v, want %+v", m.Blamed, row.blamed) + } + } + }) + } +} + +// digest is the content digest of a manifest (compose.ContentDigest). +func digest(t *testing.T, raw string) string { + t.Helper() + d, err := compose.ContentDigest([]byte(raw)) + if err != nil { + t.Fatal(err) + } + return d +} + +// blame is one module a machine's verdict blames (composer.Machine.Blamed). +type blame = struct { + Module string `json:"module"` + Problem string `json:"problem"` } // 1621/AC3: over the captured catalogue and a mesh of three machines, the command's verdict equals, machine for @@ -211,6 +314,9 @@ func TestTheCommandAnswersWhatTheLinkedCompositionAnswers(t *testing.T) { } } } + // G-24 H-AC3 (§721): a machine that does not compose and blames no module (ace: its own sealing key) fails on the + // node's own account, blamed written empty. + want.Blamed = []blame{} if !reflect.DeepEqual(got, want) { t.Errorf("%s: %s: command %+v, linked %+v", depth, node, got, want) } @@ -249,8 +355,8 @@ func TestACatalogueCheckoutIsComposedForANode(t *testing.T) { } } -// A run step hands the command no standard input: the request is read from the file -request names, and a file that is -// not there is a request it cannot read. +// A run step hands the command no standard input: the request is read from the file -request names, or from the one +// MESH_RUN_INPUT names, and a file that is not there is a request it cannot read. func TestTheRequestIsReadFromAFile(t *testing.T) { file := filepath.Join(t.TempDir(), "request.json") r := composer.Request{V: composer.Version, Cases: []composer.Case{{Name: "only", Manifests: map[string]composer.Manifest{ @@ -265,14 +371,10 @@ func TestTheRequestIsReadFromAFile(t *testing.T) { if code, _, stderr := runJSON(t, []string{"-request", file + ".gone"}, nil); code != 1 || !strings.Contains(stderr, "cannot be read") { t.Errorf("a request file that is not there: exit %d, %q", code, stderr) } -} -// A pipeline step names its input file in MESH_RUN_INPUT: the command reads the request there and the verdict is the last -// line of standard output (design 47 G A-167). -func TestThePipelineStepReadsMESH_RUN_INPUT(t *testing.T) { - file := filepath.Join(t.TempDir(), "input.json") - r := composer.Request{V: composer.Version, Depth: composer.DepthCompose, Cases: []composer.Case{{Name: "only", Manifests: map[string]composer.Manifest{ - "demo": {Raw: json.RawMessage(`{"module":"demo","version":"1"}`)}}}}} + // A pipeline step names its input file in MESH_RUN_INPUT: the command reads the request there and the verdict is the + // last line of standard output (design 47 G A-167). + r.Depth = composer.DepthCompose if err := os.WriteFile(file, encode(t, r), 0o600); err != nil { t.Fatal(err) } @@ -282,7 +384,7 @@ func TestThePipelineStepReadsMESH_RUN_INPUT(t *testing.T) { t.Fatalf("exit %d: %s", code, errOut.String()) } lines := strings.Split(strings.TrimRight(out.String(), "\n"), "\n") - var v composer.Verdict + v = composer.Verdict{} if err := json.Unmarshal([]byte(lines[len(lines)-1]), &v); err != nil || len(v.Cases) != 1 || !v.Cases[0].Manifests["demo"].Accepted { t.Fatalf("last line is no verdict: %v\n%s", err, out.String()) } diff --git a/internal/compose/frombus/answer.go b/internal/compose/frombus/answer.go index 660d5c7f..cf2ca2b0 100644 --- a/internal/compose/frombus/answer.go +++ b/internal/compose/frombus/answer.go @@ -75,7 +75,7 @@ func Answer(ctx context.Context, r Reader, mesh Mesh, s Scope, fps map[string]st return refuse(fmt.Errorf("frombus: the credentials of %s: %w", s.Node, err)) } } - composed, skipped, stood, err := composeEach(in, s.Node, fps, false, s.Running, failed, ranOf(ctx, r, mesh, s.Running, fps)) + composed, skipped, stood, err := composeEach(in, s.Node, fps, false, s.Running, failed, ranOf(ctx, r, mesh, s.Running, fps), true) if err != nil { // Not one module's: the node's own (a value served elsewhere, the mesh's network), said once as the node's. if len(failed) == 0 { @@ -331,8 +331,12 @@ func standsInFor(x, node string) (string, string) { // else the manifest it is composed from. Only the part of the offer that cannot compose is left out, by provision // (culprit): de-spiegel's routes, its names unset, leave route-proxy without them while mailu keeps its smtp account, // and the hold says so. +// +// search says a refusal is laid on a culprit (culprit). The node composing itself always searches; a composer's sandbox +// (ComposeSent) does not, since it runs the one search for culprits itself, bounded by its request (design 47 G §721; +// A-469j): there the first refusal no hold of the read answers is the node's (err). func composeEach(in compose.Input, node string, fps map[string]string, applied bool, running []compositions.Composed, failed map[string]string, - ran func(module string) (catalogue.Manifest, bool)) (out map[string]compositions.Composed, skipped, stood []string, err error) { + ran func(module string) (catalogue.Manifest, bool), search bool) (out map[string]compositions.Composed, skipped, stood []string, err error) { mode := compose.Foreseeing if applied { mode = compose.Reading @@ -379,6 +383,9 @@ func composeEach(in compose.Input, node string, fps map[string]string, applied b var leftOut map[string]string out, skipped, stood, leftOut, err = compose.CompositionsHolding(in, node, fps, mode) if err != nil { + if !search { + return nil, nil, nil, err + } module, leave := culprit(in, node, fps, mode, failed, err) if module == "" { return nil, nil, nil, firstReason(err, failed) diff --git a/internal/compose/frombus/frombus.go b/internal/compose/frombus/frombus.go index 2cc0868d..991efd3b 100644 --- a/internal/compose/frombus/frombus.go +++ b/internal/compose/frombus/frombus.go @@ -703,7 +703,7 @@ func Compose(ctx context.Context, r Reader, mesh Mesh, s Scope, fps map[string]s return nil, nil, held, fmt.Errorf("frombus: the credentials of %s: %w", s.Node, err) } } - out, skipped, _, err = composeEach(in, s.Node, fps, true, s.Running, held, ranOf(ctx, r, mesh, s.Running, fps)) + out, skipped, _, err = composeEach(in, s.Node, fps, true, s.Running, held, ranOf(ctx, r, mesh, s.Running, fps), true) return out, skipped, held, err } @@ -730,36 +730,146 @@ func ComposingInput(ctx context.Context, r Reader, mesh Mesh, s Scope, fps map[s // ComposeSent composes node from an input ComposingInput read, through the path Compose takes (composeEach, the read's holds // first): what a check's sandbox runs. False when the input says no way of composing node (an input from a node-engine -// before ComposedHow): the caller cannot compose it as the node does. -func ComposeSent(in compose.Input, node string) (map[string]engcomp.Composed, bool, error) { +// before ComposedHow): the caller cannot compose it as the node does. A node the change refuses is a *Refuses. +// +// It runs the one search for culprits (design 47 G §721; A-469j, A-468i): where the composition fails with no module held +// that the node held neither in the read nor before the change, and no path owned twice names its owners, each of +// s.Changed is held in turn, one composition each, and the first whose hold ends the problem (the try composes or fails +// otherwise) is blamed with it; one that leaves the machine not composing stays held, and the search goes on (A-470v). At most s.Tries tries; past them each module not yet tried is blamed NotTried. With no +// tries there is no search: the failure is the node's own (err). +func ComposeSent(in compose.Input, node string, s Search) (map[string]engcomp.Composed, bool, error) { c := in.ComposedHow if c == nil || c.Node != node { return nil, false, nil } - held := maps.Clone(c.Held) - if held == nil { - held = map[string]string{} + compose1 := func(also ...string) (map[string]engcomp.Composed, map[string]string, error) { + held := maps.Clone(c.Held) + if held == nil { + held = map[string]string{} + } + // A module the node holds composing without the change is held as there: the candidate keeps it as the node does, + // which is no change and no search (issue 1126; design 47 G §721, HeldBefore left out of blamed). + for m, why := range c.HeldBefore { + if _, was := held[m]; !was { + held[m] = why + } + } + for _, m := range also { + held[m] = triedHeld + } + out, _, _, err := composeEach(in, node, c.Builds, false, c.Running, held, nil, false) + return out, held, err } - out, _, _, err := composeEach(in, node, c.Builds, false, c.Running, held, nil) - if err != nil { - return nil, true, err + ours := func(m string) bool { + _, was := c.Held[m] + _, before := c.HeldBefore[m] + return !was && !before } + out, held, err := compose1() // A module the sandbox held that the node-engine held neither in the read nor composing without the change is the change // refusing it: the node does not compose, said in that module's words, never passed with the rest composed (issue 774, // review of the sandbox input). var refused []string + blamed := map[string]string{} for _, m := range sortedKeys(held) { - _, was := c.Held[m] - if _, before := c.HeldBefore[m]; !was && !before { + if ours(m) { refused = append(refused, m+": "+held[m]) + blamed[m] = held[m] } } - if len(refused) > 0 { - return nil, true, fmt.Errorf("on %s the change refuses %s", node, strings.Join(refused, "; ")) + // Two modules owning one path are each blamed, with that path, outside the search (design 47 G §721; A-468l). + var refusal *resolution.Refusal + if err != nil && errors.As(err, &refusal) { + for _, p := range refusal.OwnedTwice { + words := fmt.Sprintf("%s and %s both declare the path %q", p.Modules[0], p.Modules[1], p.Path) + for _, owner := range p.Modules { + if ours(owner) { + blamed[owner] = words + } + } + } } - return out, true, nil + // A hold that changes the problem but leaves the machine not composing keeps that module held and blamed, and the + // search goes on against the retry's problem, from the first changed module not held, within the same tries, until the + // machine composes or no hold changes it (A-470v): every module the change refuses is blamed, never only the first. + if err != nil && len(blamed) == 0 && s.Tries > 0 { + problem, tries := err.Error(), 0 + var kept []string + search: + for { + tried := map[string]bool{} // against the problem of now + for _, mod := range s.Changed { + if slices.Contains(kept, mod) { + continue + } + if tries == s.Tries { + for _, untried := range s.Changed { + if _, done := blamed[untried]; !done && !tried[untried] { + blamed[untried] = NotTried + } + } + break search + } + tries++ + tried[mod] = true + _, _, again := compose1(append(slices.Clone(kept), mod)...) + if again == nil { + blamed[mod] = problem + break search + } + if again.Error() != problem { + blamed[mod], problem = problem, again.Error() + kept = append(kept, mod) + continue search + } + } + break // no hold changes the problem + } + } + if len(blamed) == 0 { + return out, true, err + } + words := strings.Join(refused, "; ") + if err != nil { + words = err.Error() + } + e := &Refuses{Node: node, words: words} + for _, m := range sortedKeys(blamed) { + e.Blamed = append(e.Blamed, Blamed{Module: m, Problem: blamed[m]}) + } + return nil, true, e } +// Search is the composer's one search for culprits in ComposeSent (design 47 G §721): the candidate's changed modules +// placed on the node, in assignment order, and the request's blame_tries. +type Search struct { + Changed []string + Tries int +} + +// NotTried is the problem of a changed module the search for culprits did not hold, its bound reached (design 47 G §721). +const NotTried = "not tried: the search reached its bound" + +// triedHeld is the words a module is held with while the search for culprits tries it. +const triedHeld = "held by the composer's search for the module the machine's problem is of" + +// Refuses is ComposeSent's refusal of a node by the change, answered structured (design 47 G §721; A-469j): each module +// the sandbox held that its node-engine held neither in the read nor composing without the change (HeldBefore), and +// each other owner of a path one of them was held for, with its problem. +type Refuses struct { + Node string + Blamed []Blamed + words string +} + +// Blamed is one module a refusal of the change blames, with its problem. +type Blamed struct { + Module string + Problem string +} + +func (e *Refuses) Error() string { return fmt.Sprintf("on %s the change refuses %s", e.Node, e.words) } + // Plans is the provisioning plan of each module handed to the provisioner on the scope's node, composed from the bus // alone (novox/hq ADR 0570 clause 2): the controller's handedPlans over the same input (mesh-control // cmd/mesh-controller/compose_sdk.go), its rendering in the Foreseeing mode with the mesh's generators. A plan names diff --git a/internal/compose/resolution/resolve.go b/internal/compose/resolution/resolve.go index 34b8fe93..37e1f5d5 100644 --- a/internal/compose/resolution/resolve.go +++ b/internal/compose/resolution/resolve.go @@ -20,7 +20,19 @@ func heldKey(claim, kind string) string { // // Every reason at once rather than the first, and each says what to do about it. A person // resolving these fixes them in one pass or in four. -type Refusal struct{ Problems []string } +type Refusal struct { + Problems []string + // OwnedTwice is each path two modules both own among Problems, as a pair: a composer blames both owners with the + // path, never by the words (design 47 G §721; A-468l). + OwnedTwice []OwnedTwice +} + +// OwnedTwice is one path two modules both declare as their own (checkResources), the module that declared it first +// first. +type OwnedTwice struct { + Path string + Modules [2]string +} func (r *Refusal) Error() string { return "these assignments cannot be applied:\n - " + strings.Join(r.Problems, "\n - ") @@ -616,7 +628,8 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No // Listed after the holding: an optional consumer of a requirement nothing answers is left absent there (ADR 0330 §3, §7). resolution.Absent = absentOf(absent, order, catalogue) problems = append(problems, unheld...) - problems = append(problems, checkResources(resolution.Modules)...) + owned, twice := resourceProblems(resolution.Modules) + problems = append(problems, owned...) resolution.Claims = claims // Judged over the closure — what this node will actually run — so a holder pulled in by a @@ -641,7 +654,7 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No problems = append(problems, why) } sort.Strings(problems) - return mcat.Resolution{}, nil, &Refusal{Problems: problems} + return mcat.Resolution{}, nil, &Refusal{Problems: problems, OwnedTwice: twice} } return resolution, held, nil } @@ -910,6 +923,13 @@ func canonicalSeat(name string) string { // resolves with no refusal. What is refused is the contradiction — a path one module owns and // another merely accesses — because shared data is the operator's and nobody's to own. func checkResources(modules []mcat.Manifest) []string { + problems, _ := resourceProblems(modules) + return problems +} + +// resourceProblems is checkResources' problems, with each path two modules both own as a pair beside its words. +func resourceProblems(modules []mcat.Manifest) ([]string, []OwnedTwice) { + var twice []OwnedTwice // Judged as the machine composes them: where the node-resolver holder writes the resolver file, the // uplink holder's rendering of it is not composed, so it is not a second owner (novox/hq ADR 0247). modules = steppedBack(modules) @@ -987,6 +1007,9 @@ func checkResources(modules []mcat.Manifest) []string { if other, taken := owner[key]; taken && other != m.Module { problems = append(problems, fmt.Sprintf( "%s and %s both declare the %s %q", other, m.Module, field, value)) + if field == "path" { + twice = append(twice, OwnedTwice{Path: value, Modules: [2]string{other, m.Module}}) + } } owner[key] = m.Module if field == "path" { @@ -1007,6 +1030,7 @@ func checkResources(modules []mcat.Manifest) []string { if other, taken := owner[key]; taken && other != m.Module { problems = append(problems, fmt.Sprintf( "%s and %s both declare the path %q", other, m.Module, fact.Path)) + twice = append(twice, OwnedTwice{Path: fact.Path, Modules: [2]string{other, m.Module}}) } owner[key] = m.Module ownedPath[fact.Path] = m.Module @@ -1042,7 +1066,7 @@ func checkResources(modules []mcat.Manifest) []string { } } } - return problems + return problems, twice } // meshNetwork is what to assign to a machine that needs to reach another one. diff --git a/internal/composer/composer.go b/internal/composer/composer.go index c95bc208..5013e4e8 100644 --- a/internal/composer/composer.go +++ b/internal/composer/composer.go @@ -9,6 +9,7 @@ package composer import ( "encoding/json" + "errors" "fmt" "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" @@ -27,6 +28,7 @@ import ( "github.com/novox/mesh-host/internal/compose" "github.com/novox/mesh-host/internal/compose/frombus" + "github.com/novox/mesh-host/internal/compose/resolution" ) // The shapes of mesh-sdk's composer package, by the names this package's code was written with. @@ -236,8 +238,21 @@ func runCase(r Request, c Case, depth string) (CaseVerdict, error) { } cv.Machines = map[string]Machine{} for _, node := range r.Machines { - m := machine(in, node, depth, r.Declarations) - if words := awaitedOn(in, node, awaited); words != "" { + // A module awaited on node whose build can never be made, the revision giving it no fingerprint (Case.None), does + // not leave the machine not judged: it is held as its node-engine holds it, the machine judged from that + // composition, and the module blamed with the revision's none words (design 47 G A-470j, A-470k). Each other + // awaited build keeps the machine not judged, as below. + never, pending := neverOn(in, node, awaited, c.None) + at := in // the input m is composed from + if len(never) > 0 { + at = heldAgain(in, held, never) + } + // The one search for culprits runs inside the node-engine's composition (frombus.ComposeSent), over the case's + // changed modules on node, bounded by the request's blame_tries; a request with none gets no search (design 47 G + // §721; A-468i, A-469j, A-469k). + search := frombus.Search{Changed: changedOn(in, node, c, held.ComposedHow), Tries: r.BlameTries} + m := machine(at, node, depth, r.Declarations, search) + if words := awaitedOn(at, node, pending); words != "" { // Composed without the build it runs at: named per node and module, never a silent pass (issue 1921). The // machine is then not judged, as for a node whose running state its engine does not send (ADR 0593 §9). Where // it does not compose without that build (a tool runner's bundle), it is composed with the module held as its @@ -247,7 +262,8 @@ func runCase(r Request, c Case, depth string) (CaseVerdict, error) { // shanks). how := "composed from the change's manifest without its build: " if !m.Composes && m.NotJudged == "" { - switch again := machine(heldAgain(in, held, awaited), node, depth, r.Declarations); { + lifted := heldAgain(at, held, pending) + switch again := machine(lifted, node, depth, r.Declarations, search); { case again.Composes: m, how = again, "composed with it held as its node-engine holds it, since it does not compose without its build: " case again.NotJudged == "": @@ -261,6 +277,12 @@ func runCase(r Request, c Case, depth string) (CaseVerdict, error) { m.Reason += "; " + words } } + if len(never) > 0 && m.NotJudged == "" { + m = blameNever(m, never, c.None) // after the held composition is judged, its search and the awaited words included + } + if m.Blamed == nil { + m.Blamed = []blame{} // always written, so a verdict blaming none is told from an old composer's (A-469h) + } cv.Machines[node] = m } return cv, nil @@ -415,6 +437,54 @@ func heldAgain(in, held compose.Input, awaited map[string]string) compose.Input return in } +// neverOn splits the modules overrule lifted with their build awaited: never is each placed on node whose build can never +// be made, the case naming it in None (the revision gives it no fingerprint), with its awaited words; pending is awaited +// without them (design 47 G A-470j, A-470k). +func neverOn(in compose.Input, node string, awaited, none map[string]string) (never, pending map[string]string) { + pending = awaited + for _, mod := range assignedOn(in, node) { + if _, unbuildable := none[mod]; !unbuildable || awaited[mod] == "" { + continue + } + if never == nil { + never, pending = map[string]string{}, maps.Clone(awaited) + } + never[mod] = awaited[mod] + delete(pending, mod) + } + return never, pending +} + +// blameNever is m, judged with each of never held (its blames and the search already in it), with each of never blamed +// with the revision's none words, in place of any problem its composition gave it: the machine composes with none of +// them, so it does not compose, and says why (design 47 G §721, A-470j). A held composition that still fails and blames +// nobody stays on the node's own account (§722): none of never is blamed in its place. +func blameNever(m Machine, never, none map[string]string) Machine { + mods := slices.Sorted(maps.Keys(never)) + var said []string + for _, mod := range mods { + said = append(said, mod+" ("+none[mod]+")") + } + words := "composed with it held as its node-engine holds it, since its build can never be made: " + strings.Join(said, ", ") + if !m.Composes && len(m.Blamed) == 0 { + m.Reason += "; " + words + return m + } + if m.Composes { + m.Composes, m.Reason = false, words + } else { + m.Reason += "; " + words + } + for _, mod := range mods { + if i := slices.IndexFunc(m.Blamed, func(b blame) bool { return b.Module == mod }); i >= 0 { + m.Blamed[i].Problem = none[mod] + continue + } + m.Blamed = append(m.Blamed, blame{Module: mod, Problem: none[mod]}) + } + return m +} + // awaitedOn is the words of each module placed on node that is composed without its build (overrule), joined, sorted. func awaitedOn(in compose.Input, node string, awaited map[string]string) string { var out []string @@ -452,19 +522,26 @@ func HoldingsOf(shelf map[string]catalogue.Manifest, nodes []compose.Node) []cat return out } -func machine(in compose.Input, node, depth string, declarations bool) Machine { +func machine(in compose.Input, node, depth string, declarations bool, search frombus.Search) Machine { if depth == DepthResolve { if _, err := compose.Resolve(in, node); err != nil { - return Machine{Reason: err.Error()} + return Machine{Reason: err.Error(), Blamed: ownedTwice(err)} } return Machine{Composes: true} } // Composed as the node-engine composes it, when its input says how (compose.Input.ComposedHow, issue 774): the same // path, at the same builds, a module folded in its own composition kept as it runs. if !declarations { - if _, how, err := frombus.ComposeSent(in, node); how { + if _, how, err := frombus.ComposeSent(in, node, search); how { if err != nil { - return Machine{Reason: err.Error()} + m := Machine{Reason: err.Error()} + var refuses *frombus.Refuses + if errors.As(err, &refuses) { + for _, b := range refuses.Blamed { + m.Blamed = append(m.Blamed, blame{Module: b.Module, Problem: b.Problem}) + } + } + return m } return Machine{Composes: true} } @@ -478,7 +555,7 @@ func machine(in compose.Input, node, depth string, declarations bool) Machine { return Machine{NotJudged: NotJudged + strings.Join(folded, ", ") + " (its own composition carries them as they run, and the sandbox cannot): " + err.Error()} } - return Machine{Reason: err.Error()} + return Machine{Reason: err.Error(), Blamed: ownedTwice(err)} } m := Machine{Composes: len(problems) == 0, Problems: problems, Refusals: d.Refusals()} if len(problems) > 0 { @@ -490,6 +567,71 @@ func machine(in compose.Input, node, depth string, declarations bool) Machine { return m } +// NotTried is the problem of a changed module the search for culprits did not hold, its bound reached (design 47 G §721). +const NotTried = frombus.NotTried + +// problemOf is a machine's problem in words: why it does not compose, with what the validator refuses. +func problemOf(m Machine) string { + if len(m.Problems) == 0 { + return m.Reason + } + return m.Reason + ": " + strings.Join(m.Problems, "; ") +} + +// changedOn is each module placed on node, in assignment order, whose case manifest's content digest differs from the +// manifest its build was made of as the input names it (ComposedHow.Manifests), or that the input names none for: the +// candidate's changed modules (design 47 G §721; A-469k). A module the case carries no manifest of is not changed. +func changedOn(in compose.Input, node string, c Case, how *compose.ComposedHow) []string { + var out []string + for _, mod := range assignedOn(in, node) { + man, carried := c.Manifests[mod] + if !carried || len(man.Raw) == 0 { + continue + } + if digest, err := compose.ContentDigest(man.Raw); err == nil && digest == manifestOf(how, mod) { + continue + } + out = append(out, mod) + } + return out +} + +// assignedOn is the modules placed on node, in assignment order. +func assignedOn(in compose.Input, node string) []string { + for _, n := range in.Nodes { + if n.Name == node { + return n.Assigned + } + } + return nil +} + +// blame is one module a machine's verdict blames, with its problem (Machine.Blamed, design 47 G §721). +type blame = struct { + Module string `json:"module"` + Problem string `json:"problem"` +} + +// ownedTwice blames both owners of each path a refusal names two modules owning, with that path (design 47 G §721; +// A-468l), where nothing held either: a composition that holds no module. +func ownedTwice(err error) []blame { + var refusal *resolution.Refusal + if !errors.As(err, &refusal) { + return nil + } + problem := map[string]string{} + for _, p := range refusal.OwnedTwice { + for _, owner := range p.Modules { + problem[owner] = fmt.Sprintf("%s and %s both declare the path %q", p.Modules[0], p.Modules[1], p.Path) + } + } + var out []blame + for _, m := range slices.Sorted(maps.Keys(problem)) { + out = append(out, blame{Module: m, Problem: problem[m]}) + } + return out +} + // NotJudged opens the words of a machine the sandbox does not judge (Machine.NotJudged, the contract's field, ADR 0593 §9): // its node-engine sent an input that does not say how it composes (compose.Input.ComposedHow), and the node runs modules // folded in its own composition. The machine is then neither composed nor refused; a reader decides by the field. diff --git a/internal/composer/composer_test.go b/internal/composer/composer_test.go index fcf7cacc..c5c33b43 100644 --- a/internal/composer/composer_test.go +++ b/internal/composer/composer_test.go @@ -13,6 +13,7 @@ import ( engcomp "github.com/novox/mesh-host/internal/compositions" "os" "reflect" + "slices" "strings" "testing" @@ -164,7 +165,7 @@ func TestARequestItCannotRunIsRefused(t *testing.T) { if err != nil { t.Fatal(err) } - if m := refused.Cases[0].Machines["a"]; m.Composes || !strings.Contains(m.Reason, "app: ") || !strings.Contains(m.Reason, "repository") { + if m := refused.Cases[0].Machines["a"]; m.Composes || !strings.Contains(m.Reason, "app has a file") || !strings.Contains(m.Reason, "repository") { t.Errorf("a module the change refuses on a node composed as its node-engine does: %+v", m) } // Held composing without the change, blamed for a refusal beyond the read (issue 1960's unset names): no refusal of @@ -320,24 +321,39 @@ func TestARequestItCannotRunIsRefused(t *testing.T) { } runner := `{"module": "app", "version": "1", "tool-runner": true, ` + `"build": {"artifacts": [{"name": "runtime", "kind": "bundle", "language": "go", "from": "cmd/runner", "binary": "runner"}]}}` + office := `{"module": "office", "version": "1", "resources": [{"id": "c", "type": "file", "path": "/etc/office.conf", "template": "x"}]}` for _, row := range []struct { name, raw, digest string words string // in NotJudged, its build's paths not declared; "" composes at its build beside string // a module the change refuses on the same node: words are then in the refusal + none string // the revision's none words for app (Case.None): judged held, app blamed with them + tries int // the request's blame_tries }{ - {"the digest of the case's manifest", plain, same, "", ""}, - {"another digest, the same contract", plain, strings.Repeat("b", 64), "of another manifest than the change's (its build was made of the manifest " + strings.Repeat("b", 64), ""}, - {"no digest, the same contract", plain, "", "", ""}, + {"the digest of the case's manifest", plain, same, "", "", "", 20}, + {"another digest, the same contract", plain, strings.Repeat("b", 64), "of another manifest than the change's (its build was made of the manifest " + strings.Repeat("b", 64), "", "", 20}, + {"no digest, the same contract", plain, "", "", "", "", 20}, {"no digest, another contract", strings.Replace(plain, `"version": "1"`, `"version": "2"`, 1), "", - "app is composed by the running node-engine at its build fp1, of another manifest than the change's (its node-engine names no manifest", ""}, + "app is composed by the running node-engine at its build fp1, of another manifest than the change's (its node-engine names no manifest", "", "", 20}, {"a tool runner, its bundle awaited", runner, "", - "composed with it held as its node-engine holds it, since it does not compose without its build: app is composed by the running node-engine at its build fp1", ""}, + "composed with it held as its node-engine holds it, since it does not compose without its build: app is composed by the running node-engine at its build fp1", "", "", 20}, // The group check of c5d32074c945, shanks: the change refused lab there, and the words were those of the // composition from the change's manifests unbuilt ("mesh-build-go runs as its own account and its build // produced no bundle to serve"), not of the one with them held, so the lift's own refusals failed the node. {"a tool runner awaited beside a module the change refuses", runner, "", "refused with it held as its node-engine holds it, since it does not compose without its build: app is composed by the running node-engine at its build fp1", - `{"module": "office", "version": "1", "resources": [{"id": "c", "type": "file", "path": "/etc/office.conf", "template": "x"}]}`}, + office, "", 20}, + // G-24 H3 (A-470j, A-470k; live at revision 449, lidarr, radarr and gitea): a build the revision gives no + // fingerprint can never be made, so it does not leave the machine not judged: the machine is judged held, and + // app is blamed with the revision's none words. An awaited build the case does not name so stays not judged + // (the rows above). + {"a build that can never be made, its machine judged held", plain, strings.Repeat("b", 64), "", "", "builders disagree: node 22 and node 24", 20}, + {"a tool runner whose bundle can never be made", runner, "", "", "", "no build module", 20}, + // The held composition is judged first, the search included, and the build that can never be made is blamed + // beside what it blames (review round 1: office refused beside it went unblamed). + {"a build that can never be made beside a module the change refuses", runner, "", "", office, "no build module", 20}, + // A held composition that still fails and blames nobody else (no search asked) fails on the node's own + // account: the build that can never be made is not blamed in its place (§722). + {"a build that can never be made beside a refusal nobody is blamed for", runner, "", "", office, "no build module", 0}, } { in := compose.Input{Catalogue: map[string]catalogue.Manifest{"app": built}, Nodes: []compose.Node{{Name: "a", Assigned: []string{"app"}}}, ComposedHow: &compose.ComposedHow{Node: "a", Builds: map[string]string{"app": "fp1"}}} @@ -345,16 +361,41 @@ func TestARequestItCannotRunIsRefused(t *testing.T) { in.ComposedHow.Manifests = map[string]string{"app": row.digest} } manifests := map[string]Manifest{"app": {Raw: json.RawMessage(row.raw)}} + var none map[string]string + if row.none != "" { + none = map[string]string{"app": row.none} + } if row.beside != "" { manifests["office"] = Manifest{Raw: json.RawMessage(row.beside)} in.Nodes[0].Assigned = append(in.Nodes[0].Assigned, "office") } - v, err := Run(withInput(t, Request{V: Version, Machines: []string{"a"}, Depth: DepthCompose, Declarations: true, - Cases: []Case{{Name: "change", Manifests: manifests}}}, in)) + // A row with a build that can never be made beside a refused module is composed as its node-engine composes it + // (no declarations), where the search for culprits runs (frombus.ComposeSent). + v, err := Run(withInput(t, Request{V: Version, Machines: []string{"a"}, Depth: DepthCompose, Declarations: row.none == "" || row.beside == "", BlameTries: row.tries, + Cases: []Case{{Name: "change", Manifests: manifests, None: none}}}, in)) if err != nil { t.Fatal(err) } m := v.Cases[0].Machines["a"] + if row.none != "" { + want, problem := []string{"app"}, map[string]string{} + switch { + case row.beside != "" && row.tries == 0: + want = nil + case row.beside != "": + want = append(want, "office") + } + var got []string + for _, b := range m.Blamed { + got, problem[b.Module] = append(got, b.Module), b.Problem + } + slices.Sort(got) + if m.NotJudged != "" || m.Composes || !slices.Equal(got, want) || (want != nil && problem["app"] != row.none) || + !strings.Contains(m.Reason, "its build can never be made") { + t.Errorf("%s: machine %+v, want it judged with app held and blamed with %q, and %v blamed", row.name, m, row.none, want) + } + continue + } if row.beside != "" { said := fmt.Sprint(m.Reason, m.Problems, m.Refusals) if m.Composes || m.NotJudged != "" || !strings.Contains(said, "office") || strings.Contains(said, "bundle") || !strings.Contains(m.Reason, row.words) {