From 1b3cc20127cc278081aaa3e433813e4ec5689d86 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 14 Nov 2026 22:07:56 +0100 Subject: [PATCH] Indexer: a head the source names with no merged message, or none it can read, is read whole through the seat git and taken in as a merge of every file of its tree, with its templates' needs and the predefined list (A-401, A-402) Builds: design 47 G A-401, A-402, A-93, A-85 --- .../internal/index/branch_take_in_test.go | 115 ++++++++++++++++++ .../internal/index/source.go | 99 +++++++++++++++ 2 files changed, 214 insertions(+) diff --git a/modules/mesh-delivery-indexer/internal/index/branch_take_in_test.go b/modules/mesh-delivery-indexer/internal/index/branch_take_in_test.go index 64786d5b..38170e4a 100644 --- a/modules/mesh-delivery-indexer/internal/index/branch_take_in_test.go +++ b/modules/mesh-delivery-indexer/internal/index/branch_take_in_test.go @@ -11,6 +11,7 @@ import ( "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" + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // Design 47 G §13, §14, §291, A-93: a watched branch other than the first is taken in as a pure computation from the merged @@ -111,3 +112,117 @@ func TestABranchTakeInKeepsNoRevisionWithARefusedCycle(t *testing.T) { t.Errorf("the journal %q does not say the cycle %s", logs.String(), want) } } + +// Design 47 G A-401, A-93: a head the source names that has no merged message (a branch newly watched) is read whole at that +// commit through the seat git and taken in as a merge of every file of its tree, so the branch keeps its first revision; +// a head with a merged message is taken in as before and the seat git is not asked. +func TestAHeadWithNoMergedMessageIsReadWholeAtItsCommit(t *testing.T) { + for _, row := range []struct { + name string + hasMessage bool + readFails bool + wantRevision bool + wantRead bool + }{ + {name: "no merged message: read whole", wantRevision: true, wantRead: true}, + {name: "a merged message: taken in as before", hasMessage: true, wantRevision: true}, + {name: "no merged message and the seat git cannot read it: no revision", readFails: true, wantRead: true}, + } { + t.Run(row.name, func(t *testing.T) { + ctx := context.Background() + ix, _, merged := newFormIndexerUnmerged(t, builderModules(t)...) + ix.Revisions = newMemObjects(0) + ix.Graph = newMem() + var logs strings.Builder + ix.Logf = func(format string, args ...any) { fmt.Fprintf(&logs, format+"\n", args...) } + ix.Message = func(context.Context, string, string, int) ([]byte, error) { + if !row.hasMessage { + return nil, fmt.Errorf("the forge gives no merged pull request") + } + return json.Marshal(merged) + } + reads := 0 + ix.ReadFile = func(context.Context, string, string, string) ([]byte, error) { return nil, nil } // no predefined list + ix.Read = func(_ context.Context, _, sha, dir string) (RepoRead, error) { + reads++ + if row.readFails || sha != merged.Merge { + return RepoRead{}, fmt.Errorf("the seat git does not answer") + } + r := RepoRead{Root: merged.Root, Trees: merged.Trees, Manifests: map[string]string{}, Locks: map[string]map[string]string{}, Packages: map[string][]string{}} + if _, ok := merged.Trees[dir]; ok { + r.Manifests[dir], r.Locks[dir], r.Packages[dir] = merged.Manifests[dir], merged.Locks[dir], merged.Packages[dir] + } + return r, nil + } + answer := meshdelivery.SourceAnswer{Branch: "beta", Revision: 9, Repositories: []meshdelivery.SourceHead{{Repository: catalogueRepo, Head: merged.Merge, Trunk: "beta"}}} + if err := ix.ApplyBranchSource(ctx, "beta", answer); err != nil { + t.Fatal(err) + } + if (reads > 0) != row.wantRead { + t.Errorf("the seat git was asked %d times, want asked: %v", reads, row.wantRead) + } + rev, entries, err := ReadRevision(ctx, ix.Revisions, BranchObject("beta", 1)) + if (err == nil) != row.wantRevision { + t.Fatalf("branch.beta.1 read: %v, want a revision: %v", err, row.wantRevision) + } + if row.wantRevision && (rev.Branch != "beta" || len(entries) != len(builderModules(t))) { + t.Errorf("branch.beta.1 = %+v with %d entries, want every module", rev, len(entries)) + } + if said := strings.Contains(logs.String(), "has no merged message"); said != row.wantRead { + t.Errorf("the journal %q says the whole read: %v, want %v", logs.String(), said, row.wantRead) + } + }) + } +} + +// Design 47 G A-401, A-402: the message a head read whole gives equals the forge bridge's: a templated module's needs +// include its template's text (ADR 0632 §2), and the catalogue's predefined list is carried, or said absent. +func TestAHeadReadWholeCarriesTheTemplateNeedsAndThePredefinedList(t *testing.T) { + templated := strings.Replace(catalogueManifest(t, "avahi"), `"resources": [`, `"resources": [ + {"id": "conf", "type": "file", "path": "/etc/avahi-x.conf", "template": "files/x.conf"},`, 1) + for _, row := range []struct { + name string + template []byte // nil: the forge has no such file + predefined []byte + wantNeeds bool + wantList string + wantNotFound bool + }{ + {name: "template and predefined list", template: []byte("host=${seat:node-service-manager:reach}"), predefined: []byte(`{"a":1}`), wantNeeds: true, wantList: `{"a":1}`}, + {name: "no template file and no list", wantNotFound: true}, + } { + t.Run(row.name, func(t *testing.T) { + ix, _, merged := newFormIndexerUnmerged(t, mod{name: "avahi", manifest: templated}) + ix.ReadFile = func(_ context.Context, repository, sha, p string) ([]byte, error) { + if repository != catalogueRepo || sha != merged.Merge { + return nil, fmt.Errorf("unexpected read of %s at %s", repository, sha) + } + switch p { + case "modules/avahi/files/x.conf": + return row.template, nil + case "predefined.json": + return row.predefined, nil + } + return nil, fmt.Errorf("unexpected file %s", p) + } + ix.Read = func(_ context.Context, _, _, dir string) (RepoRead, error) { + return RepoRead{Root: merged.Root, Trees: merged.Trees, Manifests: map[string]string{dir: merged.Manifests[dir]}, Locks: map[string]map[string]string{dir: merged.Locks[dir]}}, nil + } + f, err := ix.wholeAt(context.Background(), catalogueRepo, merged.Merge) + if err != nil { + t.Fatal(err) + } + text := map[string]string{} + if row.template != nil { + text["files/x.conf"] = string(row.template) + } + want, _ := catalogue.ModuleNeeds([]byte(templated), text) + if got := f.Needs["modules/avahi"]; !slices.Equal(got, want) || (row.wantNeeds && !slices.Contains(got, catalogue.ReadForm+catalogue.ReferenceFormSeatReach)) { + t.Errorf("needs %v, want %v", got, want) + } + if string(f.Predefined) != row.wantList || f.PredefinedRead != row.wantNotFound { + t.Errorf("predefined %q (read-absent %v), want %q (%v)", f.Predefined, f.PredefinedRead, row.wantList, row.wantNotFound) + } + }) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/source.go b/modules/mesh-delivery-indexer/internal/index/source.go index ed801dff..88bb3bed 100644 --- a/modules/mesh-delivery-indexer/internal/index/source.go +++ b/modules/mesh-delivery-indexer/internal/index/source.go @@ -2,13 +2,17 @@ package index import ( "context" + "encoding/json" "errors" "fmt" "maps" + "path" "slices" "strings" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + "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-contracts/go/clients/meshcatalogclient" @@ -196,6 +200,12 @@ func (ix *Indexer) takeInHeads(ctx context.Context, heads map[string]string) []s continue } f, err := ix.mergedAt(ctx, repository, head) + if err != nil && ctx.Err() == nil && ix.Read != nil { + // A head the source names that has no merged message (a branch newly watched, whose head no merge on it made) is + // read whole at that commit, as a clone does (A-401, A-93). + ix.logf("catalog graph: %s at %.12s has no merged message (%v): read whole at that commit through the seat git, as a merge of every file of its tree", repository, head, err) + f, err = ix.wholeAt(ctx, repository, head) + } if err == nil { _, err = ix.ApplyMessage(ctx, f) } @@ -258,3 +268,92 @@ func (ix *Indexer) mergedAt(ctx context.Context, repository, head string) (forge } return f, nil } + +// wholeAt is the merged message of a head that has none (A-401): the head's tree and files read through the seat git +// (Read), every module directory of it with its manifest, lock files and mesh packages, as a merge whose changed files are +// every file of that tree, so it is computed as any other head (applyMoving; A-93). +func (ix *Indexer) wholeAt(ctx context.Context, repository, head string) (forgemsg.Forge, error) { + f := forgemsg.Forge{V: keys.Version, Kind: forgemsg.ForgeMerged, Repository: repository, Merge: head, Head: head} + file := func(p string) ([]byte, error) { + if ix.ReadFile == nil { + return nil, errors.New("the indexer reads no file of the forge") + } + return ix.ReadFile(ctx, repository, head, p) + } + first, err := ix.Read(ctx, repository, head, "") + if err != nil { + return f, fmt.Errorf("the seat git could not read %s at %.12s: %w", repository, head, err) + } + f.Root, f.Trees = first.Root, maps.Clone(first.Trees) + for _, dir := range slices.Sorted(maps.Keys(first.Trees)) { + read := first + if dir != "" { + if read, err = ix.Read(ctx, repository, head, dir); err != nil { + return f, fmt.Errorf("the seat git could not read %q of %s at %.12s: %w", dir, repository, head, err) + } + } + if f.Manifests == nil { + f.Manifests = map[string]string{} + } + f.Manifests[dir] = read.Manifests[dir] + // The reader capabilities of a manifest that names templates, its templates' text too, as the forge bridge reads them + // (ADR 0632 §2): a template the forge does not have is no need, any other failure to read one is the head's. + if paths := manifestTemplatePaths(read.Manifests[dir]); len(paths) > 0 { + texts := map[string]string{} + for _, t := range paths { + content, err := file(path.Join(dir, t)) + if err != nil { + return f, fmt.Errorf("the seat git could not read the template %q of %q of %s at %.12s: %w", t, dir, repository, head, err) + } + if content != nil { + texts[t] = string(content) + } + } + if f.Needs == nil { + f.Needs = map[string][]string{} + } + f.Needs[dir], _ = catalogue.ModuleNeeds([]byte(read.Manifests[dir]), texts) + } + if l := read.Locks[dir]; len(l) > 0 { + if f.Locks == nil { + f.Locks = map[string]map[string]string{} + } + f.Locks[dir] = l + } + if p := read.Packages[dir]; len(p) > 0 { + if f.Packages == nil { + f.Packages = map[string][]string{} + } + f.Packages[dir] = p + } + } + // The catalogue's predefined list at the commit, as the forge bridge carries it (ADR 0565 §2). + if trunk.SameRepository(repository, catalogueRepository) { + raw, err := file(predefinedFile) + if err != nil { + return f, fmt.Errorf("the seat git could not read %s of %s at %.12s: %w", predefinedFile, repository, head, err) + } + if raw == nil { + f.PredefinedRead = true + } else { + f.Predefined = json.RawMessage(raw) + } + } + return f, nil +} + +// The catalogue repository and its predefined list's file, whose merged message carries the list (the forge bridge's +// CatalogRepository and PredefinedFile). +const ( + catalogueRepository = "novox/mesh-catalog" + predefinedFile = "predefined.json" +) + +// manifestTemplatePaths is the template paths a manifest names, none for one that cannot be read. +func manifestTemplatePaths(raw string) []string { + m, err := catalogue.Decode([]byte(raw)) + if err != nil { + return nil + } + return catalogue.TemplatePaths(m) +}