diff --git a/internal/compose/frombus/answer_rows_test.go b/internal/compose/frombus/answer_rows_test.go index de6fd3e6..5ea57583 100644 --- a/internal/compose/frombus/answer_rows_test.go +++ b/internal/compose/frombus/answer_rows_test.go @@ -675,7 +675,9 @@ func otherNodeNotResolvedIsNamed(t *testing.T) { given[n] = &p } a := Answer(ctx, b, Mesh{}, Scope{Node: "n1", Assignments: given}, map[string]string{"cons": "src2:" + strings.Repeat("1", 64)}, nil, "p-h1", 1) - if a.Verdict != nodeengine.CandidateAnswerVerdictRefused || !strings.Contains(a.Refused, "prov on n2 lacks container-runtime") { + // cons is held for the requirement nothing answers (A-452) and its why names n2's provider. + if a.Verdict != nodeengine.CandidateAnswerVerdictRefused || len(a.Refusals) != 1 || a.Refusals[0].Module != "cons" || + !strings.Contains(a.Refusals[0].Why, "prov on n2 lacks container-runtime") { t.Errorf("a refusal for want of n2's provider = %+v", a) } } diff --git a/internal/compose/resolution/resolve.go b/internal/compose/resolution/resolve.go index ba6ce3ed..d1c48eb5 100644 --- a/internal/compose/resolution/resolve.go +++ b/internal/compose/resolution/resolve.go @@ -151,9 +151,6 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No // once, and saying the same thing twice makes a person hunt for the difference between two // identical lines before realising there is none. reported := map[string]bool{} - // A requirement nothing answers costs the modules that want it, not the machine (A-452, ADR 0636 §3): said once per name, - // each consumer is held after the walk, when the set is known. - var unanswered []string // **An optional requirement is settled last** (novox/hq ADR 0330). Whether a name is optional is // a question about every module that wants it, and a module pulled in later may want it hard; so @@ -184,6 +181,22 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No absent[want] = true reported[want] = true } + // A requirement nothing answers costs the modules that want it, not the machine (A-452, ADR 0636 §3): said once per name, + // each consumer is held after the walk, when the set is known. + // The value is the remedy the mesh-wide refusal adds, empty for a requirement nothing on the node answers. + unanswered := map[string]string{} + var unansweredOrder []string + holdWhenUnanswered := func(want, remedy string) { + if !wantedHard(want) && wantedOptionally(want) { + // Only optional consumers: left absent, never held (ADR 0330 §3). + leaveAbsent(want) + return + } + if _, ok := unanswered[want]; !ok { + unansweredOrder = append(unansweredOrder, want) + } + unanswered[want] = remedy + } for { if len(queue) == 0 { @@ -376,9 +389,8 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No } else if len(answers) > 1 { remedy = "— assign one of these to a node: " + strings.Join(answers, ", ") } - problems = append(problems, fmt.Sprintf( - "nothing in this mesh provides %q, wanted by %s %s", - want, because[want], remedy)) + // Not answered on the node nor in the rest of the mesh: costs its consumers only (A-452). + holdWhenUnanswered(want, remedy) case len(where) == 1: if c, pinned := world.Pinned[want]; pinned && !chosenMatches(c, where[0]) { // One provider, and it is not the one this machine was told to use. Silently @@ -486,7 +498,7 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No continue } reported[want] = true - unanswered = append(unanswered, want) + holdWhenUnanswered(want, "") continue case 1: // No choice to make, so none is made. This is the case that lets `install i3` bring @@ -601,7 +613,7 @@ func resolve(catalogue map[string]mcat.Manifest, assigned []string, node mcat.No problems = append(problems, checkCapabilities(resolution.Modules, node)...) claims, held := holdClaims(resolution.Modules, node, elsewhere, world.Holdings) - held, unheld := holdUnanswered(resolution.Modules, unanswered, because, held) + held, unheld := holdUnanswered(resolution.Modules, unansweredOrder, unanswered, because, held) problems = append(problems, unheld...) problems = append(problems, checkResources(resolution.Modules)...) resolution.Claims = claims @@ -684,11 +696,19 @@ func checkCapabilities(modules []mcat.Manifest, node mcat.Node) []string { return problems } +// unansweredWords is why a consumer is held for a requirement nothing answers, naming the provision. +func unansweredWords(want, consumer, remedy string) string { + if remedy == "" { + return fmt.Sprintf("nothing provides %q, wanted by %s", want, consumer) + } + return fmt.Sprintf("nothing in this mesh provides %q, wanted by %s %s", want, consumer, remedy) +} + // holdUnanswered holds each module of the set that wants a requirement nothing answers, with the words "nothing provides" naming the // provision (A-452): it stays in the set, so what it provides still counts for the others. A requirement none of the set's modules // wants (an assigned name the catalogue does not know) has no consumer to hold and comes back as a problem of the node; so does a // set whose every module is held, since then nothing could be composed. -func holdUnanswered(modules []mcat.Manifest, unanswered []string, because map[string]string, held map[string]string) (map[string]string, []string) { +func holdUnanswered(modules []mcat.Manifest, unanswered []string, remedies map[string]string, because map[string]string, held map[string]string) (map[string]string, []string) { var problems []string for _, want := range unanswered { found := false @@ -704,17 +724,21 @@ func holdUnanswered(modules []mcat.Manifest, unanswered []string, because map[st continue } found = true + if m.IsOptional(want) { + // Optional for this module: not held (ADR 0330 §3). + continue + } if held == nil { held = map[string]string{} } - why := fmt.Sprintf("nothing provides %q, wanted by %s", want, m.Module) + why := unansweredWords(want, m.Module, remedies[want]) if prior, ok := held[m.Module]; ok { why = prior + "; " + why } held[m.Module] = why } if !found { - problems = append(problems, fmt.Sprintf("nothing provides %q, wanted by %s", want, because[want])) + problems = append(problems, unansweredWords(want, because[want], remedies[want])) } } if len(unanswered) > 0 && len(held) >= len(modules) { diff --git a/internal/compose/resolution/resolve_test.go b/internal/compose/resolution/resolve_test.go index 7eb06e43..f40e6a02 100644 --- a/internal/compose/resolution/resolve_test.go +++ b/internal/compose/resolution/resolve_test.go @@ -53,31 +53,74 @@ func TestARequirementWithOneAnswerIsTakenSilently(t *testing.T) { } } -func TestARequirementWithSeveralAnswersIsRefusedAndNamed(t *testing.T) { - // The mesh does not pick. A default would be a choice made for somebody who finds out later, - // and naming the candidates is the whole remedy. - _, err := Resolve(shelf( - mod("editor", nil, []string{"shell"}, nil), - mod("bash", []string{"shell"}, nil, nil), - mod("zsh", []string{"shell"}, nil, nil), - mod("fish", []string{"shell"}, nil, nil), - ), []string{"editor"}, workstation(), mcat.World{}) - - if err == nil { - t.Fatal("a requirement with three answers was resolved without asking") - } - for _, want := range []string{"bash", "fish", "zsh", "choose one"} { - if !strings.Contains(err.Error(), want) { - t.Errorf("the refusal does not mention %q: %v", want, err) - } - } -} - -func TestARequirementWithNoAnswerIsRefused(t *testing.T) { - _, err := Resolve(shelf(mod("i3", nil, []string{"xorg"}, nil)), []string{"i3"}, workstation(), mcat.World{}) - - if err == nil || !strings.Contains(err.Error(), "nothing provides") { - t.Fatalf("a requirement nothing satisfies gave %v", err) +// A requirement nothing answers costs the modules that want it, not the machine (A-452; ace, 2026-11-16 23:11: ten unanswered +// requirements refused the whole composition), whether nothing on the node or nothing in the whole mesh answers it; one that +// only optional consumers want is left absent (ADR 0330 §3); several answers are still the node's refusal, naming them (the +// mesh does not pick: a default would be a choice made for somebody who finds out later); and a node whose every module is held, +// or an assigned name nobody wants, is still refused. +func TestARequirementNoOneAnswersHoldsItsConsumersAndSeveralAnswersRefuseTheNode(t *testing.T) { + optional := mod("viewer", nil, []string{"codec"}, nil) + optional.OptionalRequires = map[string]bool{"codec": true} + meshDB := mcat.Manifest{Module: "postgres", Provides: []mcat.Offer{{Name: "postgres-database", Scope: mcat.ScopeMesh}}} + for _, c := range []struct { + name string + shelf map[string]mcat.Manifest + assigned []string + held map[string]string // module -> words in its why + refused []string // words of the node's refusal + absent bool + }{ + {name: "on the node", assigned: []string{"ca-trust", "baserow", "sheet-viewer", "editor"}, + shelf: shelf(mod("ca-trust", nil, []string{"internal-acme-ca"}, nil), + mod("baserow", []string{"spreadsheet"}, []string{"route"}, nil), + mod("sheet-viewer", nil, []string{"spreadsheet"}, nil), mod("editor", nil, nil, nil)), + held: map[string]string{"ca-trust": `nothing provides "internal-acme-ca"`, "baserow": `nothing provides "route"`}}, + {name: "in the whole mesh", assigned: []string{"baserow", "editor"}, + shelf: shelf(meshDB, mod("baserow", nil, []string{"postgres-database"}, nil), mod("editor", nil, nil, nil)), + held: map[string]string{"baserow": `nothing in this mesh provides "postgres-database", wanted by baserow`}}, + {name: "wanted only optionally", assigned: []string{"viewer", "editor"}, + shelf: shelf(optional, mod("editor", nil, nil, nil)), absent: true}, + {name: "every module held", assigned: []string{"ca-trust"}, + shelf: shelf(mod("ca-trust", nil, []string{"internal-acme-ca"}, nil)), + refused: []string{"nothing provides", "every one is held"}}, + {name: "nobody wants it", assigned: []string{"ghost"}, shelf: shelf(mod("editor", nil, nil, nil)), + refused: []string{"nothing provides"}}, + {name: "several answers", assigned: []string{"editor"}, + shelf: shelf(mod("editor", nil, []string{"shell"}, nil), mod("bash", []string{"shell"}, nil, nil), + mod("zsh", []string{"shell"}, nil, nil), mod("fish", []string{"shell"}, nil, nil)), + refused: []string{"bash", "fish", "zsh", "choose one"}}, + } { + t.Run(c.name, func(t *testing.T) { + got, held, err := ResolveHolding(c.shelf, c.assigned, workstation(), mcat.World{}) + if c.refused != nil { + if err == nil { + t.Fatal("the node was composed, not refused") + } + for _, w := range c.refused { + if !strings.Contains(err.Error(), w) { + t.Errorf("the refusal does not mention %q: %v", w, err) + } + } + return + } + if err != nil { + t.Fatalf("the node was refused whole: %v", err) + } + if len(held) != len(c.held) { + t.Errorf("held %v, want modules %v", held, c.held) + } + for m, w := range c.held { + if !strings.Contains(held[m], w) { + t.Errorf("%s is held with %q, want %q", m, held[m], w) + } + } + if c.absent && (len(held) != 0 || len(got.Absent) == 0) { + t.Errorf("an optional requirement was held or not left absent: held %v absent %v", held, got.Absent) + } + if len(got.Modules) != len(c.assigned) { + t.Errorf("the node does not keep every module of its set: %v", names(got)) + } + }) } } diff --git a/internal/compose/resolution/unanswered_requirement_test.go b/internal/compose/resolution/unanswered_requirement_test.go deleted file mode 100644 index 42c853a0..00000000 --- a/internal/compose/resolution/unanswered_requirement_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package resolution - -import ( - "strings" - "testing" - - mcat "git.novox.be/novox/mesh-sdk/go/catalogue" -) - -// A-452 (ace, 2026-11-16 23:11: ten unanswered requirements refused the whole composition): a requirement nothing answers holds -// each module that wants it, with the "nothing provides" words naming the provision, and every other module of the node resolves; -// a held module that provides still counts for the modules that want what it provides (A-405, A-407). -func TestAnUnansweredRequirementHoldsItsConsumersAndTheNodeComposesTheRest(t *testing.T) { - got, held, err := ResolveHolding(shelf( - mod("ca-trust", nil, []string{"internal-acme-ca"}, nil), - mod("baserow", []string{"spreadsheet"}, []string{"postgres-database", "route"}, nil), - mod("sheet-viewer", nil, []string{"spreadsheet"}, nil), - mod("editor", nil, nil, nil), - ), []string{"ca-trust", "baserow", "sheet-viewer", "editor"}, workstation(), mcat.World{}) - if err != nil { - t.Fatalf("an unanswered requirement refused the whole node: %v", err) - } - for module, provision := range map[string]string{"ca-trust": "internal-acme-ca", "baserow": "postgres-database"} { - if !strings.Contains(held[module], `nothing provides "`+provision+`"`) { - t.Errorf("%s is not held for %q: %v", module, provision, held) - } - } - if !strings.Contains(held["baserow"], `nothing provides "route"`) || len(held) != 2 { - t.Errorf("baserow is not held for each of its requirements, or another module is held: %v", held) - } - names := map[string]bool{} - for _, m := range got.Modules { - names[m.Module] = true - } - if !names["editor"] || !names["sheet-viewer"] || !names["baserow"] { - t.Errorf("the node does not hold every module in its set: %v", names) - } -} - -// What nothing could be composed of, or no consumer could name, is still the machine's refusal. -func TestAnUnansweredRequirementRefusesTheNodeWhenEveryModuleIsHeldOrNoneWantsIt(t *testing.T) { - _, _, err := ResolveHolding(shelf(mod("ca-trust", nil, []string{"internal-acme-ca"}, nil)), - []string{"ca-trust"}, workstation(), mcat.World{}) - if err == nil || !strings.Contains(err.Error(), "nothing provides") { - t.Errorf("a node with every module held was composed: %v", err) - } - _, _, err = ResolveHolding(shelf(mod("editor", nil, nil, nil)), []string{"ghost"}, workstation(), mcat.World{}) - if err == nil || !strings.Contains(err.Error(), "nothing provides") { - t.Errorf("an assigned module nobody knows did not refuse the node: %v", err) - } -} diff --git a/internal/compose/resolve.go b/internal/compose/resolve.go index fcd2fc98..44982e3b 100644 --- a/internal/compose/resolve.go +++ b/internal/compose/resolve.go @@ -291,22 +291,31 @@ func (c *composer) resolutionHolding(nodeName string) (catalogue.Resolution, map At: onNetwork[nodeName], PublicDomain: node.PublicDomain, Account: node.Account, AccountHome: node.AccountHome, AgentAccount: node.AgentAccount, AgentAccountHome: node.AgentAccountHome}, world) - if err != nil { - // What another node's set would have offered is missing when that set does not resolve here: said with the - // refusal, so "nothing provides" is read beside why (issue 1126, probe #532). + // What another node's set would have offered is missing when that set does not resolve here: said with the refusal, and with + // each module held for a requirement nothing answers, so "nothing provides" is read beside why (issue 1126, probe #532). + why := func() string { + var out string if len(left) > 0 { - var why []string + var parts []string for _, l := range left { - why = append(why, l.String()) + parts = append(parts, l.String()) } - err = fmt.Errorf("%w\n\nand %d node(s) offer nothing here, as they could not be resolved: %s", err, len(left), strings.Join(why, "; ")) + out += fmt.Sprintf("\n\nand %d node(s) offer nothing here, as they could not be resolved: %s", len(left), strings.Join(parts, "; ")) } if len(c.unhostable) > 0 { - err = fmt.Errorf("%w\n\nand these modules offer nothing here, as their node cannot run them in this composition: %s", - err, strings.Join(c.unhostable, "; ")) + out += fmt.Sprintf("\n\nand these modules offer nothing here, as their node cannot run them in this composition: %s", + strings.Join(c.unhostable, "; ")) } + return out + } + if err != nil { // The node's own set does not compose: the only failure a mesh-wide gatherer may pass over. - return catalogue.Resolution{}, nil, notResolvable{err} + return catalogue.Resolution{}, nil, notResolvable{fmt.Errorf("%w%s", err, why())} + } + for m, w := range held { + if strings.Contains(w, "nothing provides") || strings.Contains(w, "nothing in this mesh provides") { + held[m] = w + why() + } } return resolved, held, nil }