diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry.go index 8eb969dd4..c27d0bc7d 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" stdio "git.novox.be/novox/mesh-sdk/go" "mesh-delivery-indexer/internal/index" @@ -21,6 +22,8 @@ type entryAnswer struct { Paths map[string][]string `json:"paths"` Fp string `json:"fp,omitempty"` ManifestDigest string `json:"manifest-digest,omitempty"` + // the entry's edges, each with its kind, the word it comes from and why (design 47 H §870, H-A-851) + Edges []meshcatalog.Edge `json:"edges,omitempty"` } // entryReadTool is indexer_entry (H-A-204; tools-are-how-a-module-is-spoken-to) over the indexer's catalog-revisions objects: @@ -29,8 +32,8 @@ type entryAnswer struct { func entryReadTool(o index.Objects) stdio.Tool { return stdio.Tool{Name: index.EntryPinsTool, Description: "Read one module's catalog graph entry pins: the packages it builds against with the version each is pinned to " + - "(an empty version while none is published), the paths each artifact is built from, its fingerprint and its manifest " + - "digest, at the newest trunk revision or at a trunk revision given; a small answer, for when the revision whole is too " + + "(an empty version while none is published), the paths each artifact is built from, its fingerprint, its manifest " + + "digest and its edges (kind, the word or event each comes from, why), at the newest trunk revision or at a trunk revision given; a small answer, for when the revision whole is too " + "large for the bus (H-A-204).", Input: map[string]any{"type": "object", "required": []string{"module"}, "properties": map[string]any{ "module": map[string]any{"type": "string", "description": "the module whose entry is read"}, @@ -50,7 +53,7 @@ func entryReadTool(o index.Objects) stdio.Tool { return nil, err } out := entryAnswer{Module: e.Module, Revision: rev.Revision, Packages: e.Packages, Paths: map[string][]string{}, - Fp: e.Fp, ManifestDigest: e.ManifestDigest} + Fp: e.Fp, ManifestDigest: e.ManifestDigest, Edges: e.Edges} if out.Packages == nil { out.Packages = map[string]string{} } diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry_test.go index aefb9942a..99e323e69 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry_test.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer-tools/entry_test.go @@ -57,6 +57,11 @@ func TestEntryTool(t *testing.T) { Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.26"}}, meshcatalog.CatalogEntry{Module: "other", Fp: "src2:o"}) keep(9, meshcatalog.CatalogEntry{Module: "mesh-grants", Fp: "src2:new", ManifestDigest: "sha256:b", Inputs: paths, Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.27", "busgrants": ""}}) + // 1984/AC-870d (H-A-838, H-A-851): the entry's word edge and event edge are answered with kind 6, their word and why. + edges := []meshcatalog.Edge{ + {Kind: 6, From: "engine:program", To: "mesh-grants:program", Word: "word:build.artifacts.race", Why: "new at the revision: mesh-grants"}, + {Kind: 6, From: "emitter:program", To: "mesh-grants:program", Word: "emitter.made", Why: "new at the revision: emitter"}} + keep(5, meshcatalog.CatalogEntry{Module: "mesh-grants", Fp: "src2:edges", Edges: edges}) tool := entryReadTool(o) if tool.Name != "indexer_entry" { t.Fatalf("name %s", tool.Name) @@ -71,6 +76,8 @@ func TestEntryTool(t *testing.T) { Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.27", "busgrants": ""}, Paths: paths.Paths}, ""}, {"given", map[string]any{"module": "mesh-grants", "revision": float64(7)}, entryAnswer{Module: "mesh-grants", Revision: 7, Fp: "src2:old", ManifestDigest: "sha256:a", Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.26"}, Paths: paths.Paths}, ""}, + {"edges", map[string]any{"module": "mesh-grants", "revision": float64(5)}, entryAnswer{Module: "mesh-grants", Revision: 5, Fp: "src2:edges", + Packages: map[string]string{}, Paths: map[string][]string{}, Edges: edges}, ""}, {"given as digits, no paths", map[string]any{"module": "other", "revision": "7"}, entryAnswer{Module: "other", Revision: 7, Fp: "src2:o", Packages: map[string]string{}, Paths: map[string][]string{}}, ""}, {"not in the newest", map[string]any{"module": "other"}, entryAnswer{}, "holds no module other"}, diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_branches_test.go b/modules/mesh-delivery-indexer/internal/index/catalog_branches_test.go index ab31890f9..ff78823e5 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_branches_test.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_branches_test.go @@ -153,3 +153,38 @@ func TestAHigherNeedsRulesDerivesTheUnmovedEntriesNeedsAgain(t *testing.T) { t.Errorf("a take-in at unchanged rules derived needs again: %v", needs) } } + +// Design 47 H §870 (1984/AC-870a): a take-in writes the word edge into the revision it keeps, against its base revision: the +// word new at the second take-in orders its mover after the node-engine mover, the one the first take-in named gets none. +func TestATakeInKeepsTheWordEdgesNewAgainstItsBase(t *testing.T) { + ctx := context.Background() + ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20)} + engine := wordEntry("engine", map[string]any{"claims": []any{map[string]any{"name": "node-engine"}}}, nil, nil) + app := wordEntry("app", map[string]any{}, []string{"word:settings"}, nil) + mv := []meshcatalog.TakeInEntry{{Module: "engine"}, {Module: "app"}} + if err := ix.KeepBranchEntries(ctx, "main", 1, map[string]meshcatalog.CatalogEntry{"engine": engine, "app": app}, mv); err != nil { + t.Fatal(err) + } + app.Needs = []string{"word:settings", "word:build.artifacts.race"} + if err := ix.KeepBranchEntries(ctx, "main", 2, map[string]meshcatalog.CatalogEntry{"engine": engine, "app": app}, mv); err != nil { + t.Fatal(err) + } + a, err := Revision(ctx, ix.Revisions, nil, meshcatalog.RevisionInput{Branch: "main", Source: 2}) + if err != nil { + t.Fatal(err) + } + for _, e := range a.Entries { + if e.Module != "app" { + continue + } + var words []string + for _, x := range e.Edges { + if x.Kind == 6 { + words = append(words, x.Word) + } + } + if !slices.Equal(words, []string{"word:build.artifacts.race"}) { + t.Errorf("app holds the word edges %v, want only the one new at the base", words) + } + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_edges.go b/modules/mesh-delivery-indexer/internal/index/catalog_edges.go index 15712a0b0..53f930baf 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_edges.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_edges.go @@ -494,3 +494,108 @@ func edgesOfPackages(a meshcatalog.CatalogEntry, all map[string]meshcatalog.Cata } } } + +// WithNewEdges adds, to the entries of a take-in's revision, the edges of kind 6 that its movers bring (design 47 H §870, +// H-A-838): a word and an event new at the revision, between the artifacts of the movers. base is the entries of the +// revision's base (none for a branch's first); entries are not changed. +// 1. A word: each need `word:` of a mover A that no entry of base names gives an edge from the artifact of each +// mover that holds the seat node-engine, the reader of every manifest word (ADR 0614 §4), to A's. +// 2. An event: each `consumes` entry of a mover A that names an event a mover B's manifest emits at the revision and +// did not at the base gives an edge from B's artifact to A's. +// +// An edge's `word` is the need, or `.`; its `why` says it is new at the revision and names the mover that +// brings it. A cycle of these edges with other delivery edges is delivered as one step (graph.RefusedCycle refuses only +// one holding a build edge). +func WithNewEdges(entries, base map[string]meshcatalog.CatalogEntry, movers []meshcatalog.TakeInEntry) map[string]meshcatalog.CatalogEntry { + moved := map[string]bool{} + for _, m := range movers { + moved[m.Module] = true + } + baseNeeds := map[string]bool{} + for _, b := range base { + for _, n := range b.Needs { + baseNeeds[n] = true + } + } + var engines []string + for _, m := range slices.Sorted(maps.Keys(entries)) { + if moved[m] && manifestOf(entries[m]).ClaimsSeat(nodeEngineSeat) { + engines = append(engines, m) + } + } + out := maps.Clone(entries) + for _, name := range slices.Sorted(maps.Keys(entries)) { + if !moved[name] { + continue + } + a := entries[name] + to := primaryOf(a) + if to == "" { + continue + } + var added []meshcatalog.Edge + for _, n := range a.Needs { + if !strings.HasPrefix(n, catalogue.ReadWord) || baseNeeds[n] { + continue + } + for _, e := range engines { + if from := primaryOf(entries[e]); e != name && from != "" { + added = append(added, meshcatalog.Edge{Kind: 6, From: from, To: to, Word: n, Why: "new at the revision: " + name}) + } + } + } + for _, subject := range a.Consumes { + for _, b := range slices.Sorted(maps.Keys(entries)) { + from := primaryOf(entries[b]) + if !moved[b] || b == name || from == "" { + continue + } + for _, ev := range emitsOf(entries[b]) { + word := b + "." + ev + if eventMatches(subject, word) && !slices.Contains(emitsOf(base[b]), ev) { + added = append(added, meshcatalog.Edge{Kind: 6, From: from, To: to, Word: word, Why: "new at the revision: " + b}) + } + } + } + } + if len(added) == 0 { + continue + } + a.Edges = slices.Clone(a.Edges) + for _, x := range added { + if !slices.Contains(a.Edges, x) { + a.Edges = append(a.Edges, x) + } + } + sortEdges(a.Edges) + out[name] = a + } + return out +} + +// nodeEngineSeat is the seat whose holder reads every manifest word (ADR 0614 §4). +const nodeEngineSeat = "node-engine" + +// emitsOf are the events the entry's manifest emits, named locally. +func emitsOf(e meshcatalog.CatalogEntry) []string { + if len(e.Manifest) == 0 { + return nil + } + return manifestOf(e).Emits +} + +// eventMatches says whether a consumed event, `.` with `*` for one name and `**` for the rest, names word. +func eventMatches(pattern, word string) bool { + p, w := strings.Split(pattern, "."), strings.Split(word, ".") + for i, t := range p { + switch { + case t == "**": + return true + case i >= len(w): + return false + case t != "*" && t != w[i]: + return false + } + } + return len(p) == len(w) +} diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go b/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go index 352d48874..e458a3ef6 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go @@ -318,3 +318,72 @@ func TestTheTrunkCataloguesHoldNoRefusedCycle(t *testing.T) { } } } + +func wordEntry(module string, manifest map[string]any, needs, consumes []string) meshcatalog.CatalogEntry { + manifest["module"], manifest["version"] = module, "1.0.0" + return meshcatalog.CatalogEntry{Module: module, Manifest: manifest, Needs: needs, Consumes: consumes, + Artifacts: []meshcatalog.Artifact{{Ref: meshcatalog.ArtifactRef(module + ":program"), Language: "go"}}} +} + +func kind6Words(e meshcatalog.CatalogEntry) []string { + var out []string + for _, x := range e.Edges { + if x.Kind == 6 { + out = append(out, x.From+" "+x.Word+" | "+x.Why) + } + } + return out +} + +// Design 47 H §870 1., 1984/AC-870a: a mover whose entry needs a word no base entry names gets the edge from the mover that holds +// the seat node-engine, and one whose word a base entry names gets none. +func TestAMoverNeedingANewWordIsOrderedAfterTheNodeEngineMover(t *testing.T) { + engine := wordEntry("engine", map[string]any{"claims": []any{map[string]any{"name": "node-engine"}}}, nil, nil) + newer := wordEntry("newer", map[string]any{}, []string{"word:build.artifacts.race"}, nil) + known := wordEntry("known", map[string]any{}, []string{"word:settings"}, nil) + base := map[string]meshcatalog.CatalogEntry{"old": {Module: "old", Needs: []string{"word:settings"}}} + movers := []meshcatalog.TakeInEntry{{Module: "engine"}, {Module: "newer"}, {Module: "known"}} + got := WithNewEdges(map[string]meshcatalog.CatalogEntry{"engine": engine, "newer": newer, "known": known}, base, movers) + if w := kind6Words(got["newer"]); !slices.Equal(w, []string{"engine:program word:build.artifacts.race | new at the revision: newer"}) { + t.Errorf("newer holds %v, want the edge from the node-engine mover", w) + } + if w := kind6Words(got["known"]); len(w) != 0 { + t.Errorf("known holds %v, want no edge: a base entry names its word", w) + } + if w := kind6Words(got["engine"]); len(w) != 0 { + t.Errorf("the node-engine mover holds %v, want none", w) + } +} + +// Design 47 H §870 2., 1984/AC-870b: a consumer of an event its emitter newly emits gets the edge from the emitter, and one +// whose emitter emitted it at the base gets none. +func TestAConsumerOfANewlyEmittedEventIsOrderedAfterItsEmitter(t *testing.T) { + emitter := wordEntry("emitter", map[string]any{"emits": []any{"made", "kept"}}, nil, nil) + fresh := wordEntry("fresh", map[string]any{}, nil, []string{"emitter.made"}) + stale := wordEntry("stale", map[string]any{}, nil, []string{"emitter.kept", "*.nothing"}) + base := map[string]meshcatalog.CatalogEntry{"emitter": wordEntry("emitter", map[string]any{"emits": []any{"kept"}}, nil, nil)} + movers := []meshcatalog.TakeInEntry{{Module: "emitter"}, {Module: "fresh"}, {Module: "stale"}} + got := WithNewEdges(map[string]meshcatalog.CatalogEntry{"emitter": emitter, "fresh": fresh, "stale": stale}, base, movers) + // AC-870d (H-A-838): kind 6, the event as `word`, "new at the revision" naming the mover as `why`. + if w := kind6Words(got["fresh"]); !slices.Equal(w, []string{"emitter:program emitter.made | new at the revision: emitter"}) { + t.Errorf("fresh holds %v, want the edge from the emitter", w) + } + if w := kind6Words(got["stale"]); len(w) != 0 { + t.Errorf("stale holds %v, want none: the emitter emitted it at the base", w) + } +} + +// Design 47 H §870 3., 1984/AC-870c: a cycle of a word edge and an event edge is delivered as one step, so it is no refused +// cycle (only one holding a build edge is). +func TestACycleOfAWordEdgeAndAnEventEdgeIsNotRefused(t *testing.T) { + engine := wordEntry("engine", map[string]any{"claims": []any{map[string]any{"name": "node-engine"}}}, nil, []string{"newer.said"}) + newer := wordEntry("newer", map[string]any{"emits": []any{"said"}}, []string{"word:resources.tool-files"}, nil) + movers := []meshcatalog.TakeInEntry{{Module: "engine"}, {Module: "newer"}} + got := WithNewEdges(map[string]meshcatalog.CatalogEntry{"engine": engine, "newer": newer}, nil, movers) + if len(kind6Words(got["engine"])) != 1 || len(kind6Words(got["newer"])) != 1 { + t.Fatalf("edges %v and %v, want one each way", kind6Words(got["engine"]), kind6Words(got["newer"])) + } + if cycle := graph.RefusedCycle(got); cycle != nil { + t.Errorf("refused %v, want one step", cycle) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_revisions.go b/modules/mesh-delivery-indexer/internal/index/catalog_revisions.go index 46827a484..03b917d44 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_revisions.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_revisions.go @@ -421,6 +421,20 @@ func (ix *Indexer) keepBranchEntries(ctx context.Context, branch string, src int if judge.NeedsRules > newest.NeedsRules { entries = withRuleNeeds(entries, movers) // H-A-605 (4): the rules moved since the base, so each unmoved entry is read again } + if len(movers) > 0 { + // design 47 H §870: the word and event edges the movers bring, new against the revision's base + base := map[string]meshcatalog.CatalogEntry{} + if newest.Revision > 0 { + _, held, _, err := readNewestOf(ctx, ix.Revisions, branch) + if err != nil { + return 0, fmt.Errorf("the base catalog graph revision: %w", err) + } + for _, e := range held { + base[e.Module] = e + } + } + entries = WithNewEdges(entries, base, movers) + } at := ix.now().UTC() rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Revision: number, Kind: "trunk", Branch: branch, Source: src, Call: call.Call, NeedsRules: judge.NeedsRules, CallKind: call.Kind, Module: call.Module, Version: call.Version, Repository: call.Repository, At: &at}, entries)