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 309e0395e..eb704a538 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -418,7 +418,8 @@ func serveTools() error { tools = append(tools, askAgainTools(runtimeStore{}, stdio.Ask, time.Now)...) tools = append(tools, wantDropTools(runtimeStore{}, stdio.Ask)...) tools = append(tools, asksTools(stdio.QueueWaiting, stdio.State(process.TakenState), stdio.Ask)...) // the seat's work queue (§777) - tools = append(tools, keepNowTools(stdio.Ask)...) // the keeping by hand (§813 (2)) + tools = append(tools, keepNowTools(stdio.Ask)...) + tools = append(tools, validateBeforeTools(runtimeStore{})...) // what a validate step did not count (§814) // the keeping by hand (§813 (2)) 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 index 7d3bacc8f..d7bd437c1 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep.go @@ -15,6 +15,9 @@ import ( "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" + stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-assigner/internal/buckets" ) // 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 @@ -23,8 +26,8 @@ import ( // 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 its base's (H-A-154), 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. +// Every mover's step ends the same: failed `cycle` naming the cycle, failed `manifest-refused` naming every problem the case +// before does not hold (design 47 H2 §814; assigner_validate_before answers the ones it holds), or passed. const ( // validatorPackage is the manifest validator's import path in the module holding the seat mesh-catalog (the indexer's @@ -112,16 +115,12 @@ func (r *compositionRun) judgeValidate(ctx context.Context, step meshwants.StepR if err != nil { return couldNotRun("the step's catalog revision is not read: " + err.Error()) } - before, err := r.before(ctx, step, half) - if err != nil { - return couldNotRun("the base of the case before is not read: " + err.Error()) - } // A revision that names no base (the branch's first take-in) is judged against an empty case before, and says so (H-A-151). empty := "" if half.Revision.Base <= 0 { empty = emptyBeforeWords } - if problems := causedProblems(v.Change, v.Before, altered(half, before)); len(problems) > 0 { + if problems := causedProblems(v.Change, v.Before); len(problems) > 0 { said := reasonManifestRefused + ": " + strings.Join(problems, "; ") if empty != "" { said += " (" + empty + ")" @@ -173,60 +172,44 @@ func (r *compositionRun) before(ctx context.Context, step meshwants.StepRun, hal return r.revision(ctx, base) } -// 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; design 47 H2 §814): of every manifest, and between +// manifests, each problem the case before did not have; a problem the case before holds is never counted, whether the +// revision alters that manifest or not (assigner_validate_before answers those). +func causedProblems(change, before composer.CaseVerdict) []string { + return problemsBy(change, before, false) } -// 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 { +// uncountedProblems are the change case's problems the case before holds too, which causedProblems does not count (§814). +func uncountedProblems(change, before composer.CaseVerdict) []string { + return problemsBy(change, before, true) +} + +// problemsBy are the change case's problems, per manifest, that the case before had (had) or did not have (!had). +func problemsBy(change, before composer.CaseVerdict, had bool) []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{} + held := map[string]bool{} for _, p := range strings.Split(before.Manifests[module].Refused, "\n") { - had[p] = true + held[p] = true } - var caused []string + var named []string for _, p := range strings.Split(read.Refused, "\n") { - if p != "" && (slices.Contains(touched, module) || !had[p]) { - caused = append(caused, p) + if p != "" && held[p] == had { + named = append(named, p) } } - if len(caused) == 0 { + if len(named) == 0 { continue } what := module if module == betweenManifests { what = "between manifests" } - out = append(out, what+": "+strings.Join(caused, "; ")) + out = append(out, what+": "+strings.Join(named, "; ")) } return out } @@ -380,3 +363,50 @@ func validateRunVerdict(answer []byte) (map[string]any, string) { } return verdict, "" } + +// validateBeforeTool is the tool that answers what a validate step did not count (design 47 H2 §814). +const validateBeforeTool = "assigner_validate_before" + +// validateBefore answers, for the validate run of pipeline, the problems of the change case that its case before holds too, +// which the step did not count, for module alone when one is named; read from the run the state composition-runs keeps. +func validateBefore(st store, pipeline, module string) (map[string]any, error) { + key, err := validateKeyOf(meshwants.StepRun{Pipeline: strings.TrimSpace(pipeline)}) + if err != nil { + return nil, err + } + raw, _, ok, err := st.get(buckets.CompositionRunsState, key) + if err != nil { + return nil, fmt.Errorf("the state %s is not read: %w", buckets.CompositionRunsState, err) + } + if !ok { + return nil, fmt.Errorf("no validate run %s is kept", key) + } + var run meshwants.CompositionRun + if err := json.Unmarshal(raw, &run); err != nil { + return nil, fmt.Errorf("the validate run %s does not read: %w", key, err) + } + var v validateVerdict + if b, err := json.Marshal(run.Verdict); err != nil || json.Unmarshal(b, &v) != nil { + return nil, fmt.Errorf("the validate run %s holds no verdict that reads", key) + } + change, before := v.Change, v.Before + if module = strings.TrimSpace(module); module != "" { + change = composer.CaseVerdict{Name: change.Name, Manifests: map[string]composer.ManifestRead{module: change.Manifests[module]}} + } + uncounted := uncountedProblems(change, before) + if uncounted == nil { + uncounted = []string{} + } + return map[string]any{"run": key, "outcome": run.Outcome, "uncounted": uncounted}, nil +} + +// validateBeforeTools are the module's tool of the validate step's before case. +func validateBeforeTools(st store) []stdio.Tool { + return []stdio.Tool{{Name: validateBeforeTool, Description: "The problems a validate step did not count (design 47 H2 §814): of the " + + "validate run of a pipeline's take-in revision, every problem of the change case its case before holds too, per manifest, " + + "for one module when named. Read-only.", + Input: schema(map[string]string{"pipeline": "the pipeline's key, .", "module": "one module; absent: every one"}, []string{"pipeline"}), + Run: func(args map[string]any) (any, error) { + return validateBefore(st, arg(args, "pipeline"), arg(args, "module")) + }}} +} 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 index 0326f4f19..4d7985b13 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/validatestep_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "slices" "strings" "sync" "testing" @@ -14,6 +15,8 @@ import ( 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" + + "mesh-delivery-assigner/internal/buckets" ) // 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 @@ -86,8 +89,8 @@ func TestTheValidateStepRefusesAManifestProblemAndACycle(t *testing.T) { 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"}}}}}) + {Name: changeCase, Manifests: map[string]composer.ManifestRead{"web": {Refused: "unknown field \"colour\"\nan inherited problem"}, "db": {Refused: "an old problem"}, "indexer": {Accepted: true}}}, + {Name: beforeCase, Manifests: map[string]composer.ManifestRead{"db": {Refused: "an old problem"}, "web": {Refused: "an inherited 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 { @@ -115,10 +118,21 @@ func TestTheValidateStepRefusesAManifestProblemAndACycle(t *testing.T) { 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") { + strings.Contains(o.Said, "an old problem") || strings.Contains(o.Said, "an inherited problem") { // H2 §814: only new problems t.Errorf("mover %d's validate step ended %+v", i, o) } } + // assigner_validate_before (H2 §814): the problems the before case holds, which the step did not count. + runsKV := memStore{buckets.CompositionRunsState: &memKV{values: map[string][]byte{}}} + kept, _, _, _ := runs.read("main.7" + validateKeySuffix) + runsKV[buckets.CompositionRunsState].values["main.7"+validateKeySuffix], _ = json.Marshal(kept) + before, err := validateBeforeTools(runsKV)[0].Run(map[string]any{"pipeline": "main.7", "module": "web"}) + if b, _ := before.(map[string]any); err != nil || !slices.Equal(b["uncounted"].([]string), []string{"web: an inherited problem"}) { + t.Errorf("assigner_validate_before answered %+v, %v", before, err) + } + if _, err := validateBeforeTools(runsKV)[0].Run(map[string]any{"pipeline": "main.6"}); err == nil { + t.Error("assigner_validate_before answers a run not kept") + } if len(asks) != 1 { t.Fatalf("%d validator asks were put for one take-in, want 1", len(asks)) } diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index af6afbc90..dee553321 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -105,7 +105,7 @@ 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_want_drop", "assigner_asks", "assigner_ask_drop", "assigner_keep_now"}) { + "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", "assigner_asks", "assigner_ask_drop", "assigner_keep_now", "assigner_validate_before"}) { 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 { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index 3bb05e6ef..2a2479559 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -134,7 +134,8 @@ "assigner_want_drop", "assigner_asks", "assigner_ask_drop", - "assigner_keep_now" + "assigner_keep_now", + "assigner_validate_before" ], "state": [ { "name": "assignments", "history": 64 },