Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/graph_registrations.go
T
jschoubben 93efda5db3 Design 47 H1: the graph skips a pending register, the seed's left-out, and reads the branch
Review round 1. A module with a register proposal pending is skipped (the
operator's Approve registers it); a module the seed left out keeps
seed-left-out and is not registered; the branch is the first watched
branch the repository's registered modules hold alike, else its default
branch read through the seat git, and a module whose default branch is not
read is skipped and said, registered at the next kept trunk revision. One
listing of the registrations finds the modules with none, so a revision
whose modules are all registered reads no registration.

Builds: design 47 H H-A-262, H-A-263, H-A-264, H-A-265, H-A-266
2026-11-21 13:18:41 +01:00

247 lines
10 KiB
Go

package index
import (
"context"
"fmt"
"path"
"slices"
"strings"
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
)
// seedSteps are the seed's module-wide statuses (§8, design 47 H §745): the statuses the check judged until then, lint, unit
// tests, integration tests, composition and validate required, code review optional, approval bypass.
func seedSteps() meshcatalog.StepStatuses {
return meshcatalog.StepStatuses{Lint: "required", Unit: "required", Integration: "required",
Composition: "required", Review: "optional", Approval: "bypass", Validate: "required"}
}
// GraphModule is a module of a trunk revision's module graph as its registration is composed when it enters the graph
// (design 47 H H-A-262): its repository, the path of its manifest and the names of its artifacts.
type GraphModule struct {
Module string
Origin string
Path string
Artifacts []string
// LeftOut says why the seed left the module out (its finding seed-left-out, A-324): such a module is not registered by
// the graph (H-A-265 (2)). Empty: it is registered.
LeftOut string
}
// GraphModulesOf are the modules of a trunk revision's entries, sorted by module.
func GraphModulesOf(entries []meshcatalog.CatalogEntry) []GraphModule {
out := make([]GraphModule, 0, len(entries))
for _, e := range entries {
m := GraphModule{Module: e.Module, Origin: e.Repository, Path: path.Join(e.Path, ModuleManifest)}
for _, a := range e.Artifacts {
m.Artifacts = append(m.Artifacts, artifactName(a.Ref))
}
out = append(out, m)
}
slices.SortFunc(out, func(a, b GraphModule) int { return strings.Compare(a.Module, b.Module) })
return out
}
// enteringPolicy is the policy of a module entering the graph (H-A-262, H-A-263): the module-wide steps every other
// registered module of its repository holds alike, else the seed's; a per-artifact status only for an artifact name the module
// has that every sibling naming it holds identically; nothing else.
func enteringPolicy(m GraphModule, siblings []meshcatalog.Registration) meshcatalog.Policy {
p := meshcatalog.Policy{Steps: seedSteps()}
if len(siblings) > 0 {
alike := true
for _, s := range siblings[1:] {
alike = alike && s.Policy.Steps == siblings[0].Policy.Steps
}
if alike {
p.Steps = siblings[0].Policy.Steps
}
}
for _, a := range m.Artifacts {
var held []meshcatalog.StepStatuses
for _, s := range siblings {
if st, ok := s.Policy.Artifacts[a]; ok {
held = append(held, st)
}
}
if len(held) == 0 || slices.ContainsFunc(held[1:], func(st meshcatalog.StepStatuses) bool { return st != held[0] }) {
continue
}
if p.Artifacts == nil {
p.Artifacts = map[string]meshcatalog.StepStatuses{}
}
p.Artifacts[a] = held[0]
}
return p
}
// RegisterEntering registers every module of mods that has no registration (design 47 H H-A-262 to H-A-266; the operator,
// 2026-11-14: a module in the module graph is a registration): origin its repository, path its manifest's, the branch its
// repository's registered modules watch first alike, else the repository's default branch read through the seat git
// (enteringBranch), the policy of its repository's other registered modules (enteringPolicy), version 1 with no ask, said
// registration-changed. A module already registered is left as it is, and none is ever removed here. Skipped, each said in the
// journal: a module with a register proposal pending (its ask stands, H-A-265 (1)), one the seed left out (LeftOut, H-A-265
// (2)), and one whose default branch is not read (registered at the next kept revision, H-A-266). While no registration is
// held it registers none: the seed writes the first (H-A-264 (2)). It answers the modules it registered, sorted.
func (r *Registry) RegisterEntering(ctx context.Context, mods []GraphModule, by string) ([]string, error) {
r.mu.Lock()
defer r.mu.Unlock()
// One listing finds the modules with no registration; the registrations themselves are read only when one has none.
keys, err := r.Registrations.Keys(ctx)
if err != nil {
return nil, fmt.Errorf("the registrations are not listed: %w", err)
}
if len(keys) == 0 {
// With none held, the seed is the first and only writer: its policy reads the build seats' steps (H-A-264 (2)).
r.logf("registrations: none is held, so no module entering the module graph is registered here: the seed writes the first (H-A-264)")
return nil, nil
}
listed := map[string]bool{}
for _, k := range keys {
listed[k] = true
}
var entering []GraphModule
for _, m := range mods {
if m.Module != "" && !listed[RegistrationKey(m.Module)] {
entering = append(entering, m)
}
}
if len(entering) == 0 {
return nil, nil
}
held, err := r.RegistrationsAnswer(ctx, meshcatalog.RegistrationsInput{})
if err != nil {
return nil, fmt.Errorf("the registrations are not read: %w", err)
}
have := map[string]bool{}
byOrigin := map[string][]meshcatalog.Registration{}
for _, reg := range held.Registrations {
have[reg.Module] = true
byOrigin[reg.Origin] = append(byOrigin[reg.Origin], reg)
}
var written []string
var errs []string
for _, m := range entering {
if have[m.Module] {
continue
}
if m.LeftOut != "" {
r.logf("registrations: %s entered the module graph and is not registered: the seed left it out (%s), so it keeps seed-left-out (H-A-265)", m.Module, m.LeftOut)
continue
}
if p, _, found, err := r.load(ctx, ProposalKey(verbRegister, m.Module, "")); err != nil {
errs = append(errs, fmt.Sprintf("%s: its register proposal is not read: %v", m.Module, err))
continue
} else if found && p.State == ProposalOpen {
r.logf("registrations: %s entered the module graph with a register proposal pending (ask %s): it is not registered here, the operator's Approve registers it (H-A-265)", m.Module, p.Ask)
continue
}
branch, err := r.enteringBranch(m.Origin, byOrigin[m.Origin])
if err != nil {
r.logf("registrations: %s entered the module graph and is not registered now: %v; the next kept trunk revision registers it (H-A-266)", m.Module, err)
continue
}
reg := meshcatalog.Registration{Module: m.Module, Origin: m.Origin, Path: m.Path, Branches: []string{branch},
Policy: enteringPolicy(m, byOrigin[m.Origin])}
p := Proposal{V: 1, Verb: verbRegister, Module: m.Module, Caller: by, At: r.now(), State: ProposalOpen,
Lines: []string{fmt.Sprintf("%s: registered as it entered the module graph (%s, %s)", m.Module, m.Origin, m.Path)}}
if err := r.write(ctx, "", p, reg, oc.Warrant{Ask: "module-graph"}); err != nil {
errs = append(errs, fmt.Sprintf("%s: %v", m.Module, err))
continue
}
have[m.Module] = true
written = append(written, m.Module)
r.logf("registrations: %s entered the module graph of %s with no registration: registered as version 1 from %s (%s), watching %s (H-A-262)",
m.Module, TrunkBranch, m.Origin, m.Path, branch)
}
if len(errs) > 0 {
return written, fmt.Errorf("the registrations of modules entering the module graph are not written: %s", strings.Join(errs, "; "))
}
return written, nil
}
// enteringBranch is the branch a module entering the graph watches (H-A-265 (3), H-A-266): the first watched branch every
// registered module of its repository holds alike, else the repository's default branch as the seed reads it from the seat
// git; an error when that is not read.
func (r *Registry) enteringBranch(origin string, siblings []meshcatalog.Registration) (string, error) {
first := ""
alike := len(siblings) > 0
for _, s := range siblings {
if len(s.Branches) == 0 || (first != "" && s.Branches[0] != first) {
alike = false
break
}
first = s.Branches[0]
}
if alike && first != "" {
return first, nil
}
if r.Branch == nil {
return "", fmt.Errorf("the default branch of %s is not asked of the seat git", origin)
}
b, err := r.Branch(origin, "")
if err != nil {
return "", fmt.Errorf("the seat git does not answer the default branch of %s: %w", origin, err)
}
if b.Error != "" || b.Branch == "" {
return "", fmt.Errorf("the default branch of %s is not read: %s", origin, b.Error)
}
return b.Branch, nil
}
// registerEntering registers each module of a kept trunk revision's entries that has no registration (H-A-262, H-A-264 (1),
// (3)): a module entering the trunk's graph, at any kind of kept trunk revision. A dry run, or an indexer with no
// registrations, registers none. A failure is said in the journal: the revision is kept whatever it answers.
func (ix *Indexer) registerEntering(ctx context.Context, entries []meshcatalog.CatalogEntry) {
if ix.isDry || ix.Registry == nil || ix.Registry.Registrations == nil {
return
}
written, err := ix.Registry.RegisterEntering(ctx, ix.withLeftOut(GraphModulesOf(entries)), ix.Registry.Module)
if len(written) > 0 {
ix.logf("registrations: %d modules entering the module graph of %s registered: %s (H-A-262)", len(written), TrunkBranch, strings.Join(written, ", "))
}
if err != nil {
ix.logf("registrations: %v (H-A-262)", err)
}
}
// RegisterTrunkGraph registers, once at the indexer's start, each module of the current trunk graph that has no registration
// (H-A-264 (1)), as a kept trunk revision does; it answers the modules it registered.
func (ix *Indexer) RegisterTrunkGraph(ctx context.Context) ([]string, error) {
if ix.isDry || ix.Registry == nil || ix.Registry.Registrations == nil || ix.Revisions == nil {
return nil, nil
}
_, entries, found, err := readNewestOf(ctx, ix.Revisions, TrunkBranch)
if err != nil {
return nil, fmt.Errorf("the newest trunk revision is not read: %w", err)
}
if !found {
return nil, nil
}
return ix.Registry.RegisterEntering(ctx, ix.withLeftOut(GraphModulesOf(entries)), ix.Registry.Module)
}
// withLeftOut marks each module the indexer holds the finding seed-left-out of (A-324, H-A-265 (2)).
func (ix *Indexer) withLeftOut(mods []GraphModule) []GraphModule {
if ix.Findings == nil {
return mods
}
left := map[string]string{}
for _, f := range ix.Findings.Held() {
if f.Kind == SeedLeftOutKind && f.Scope == mcc.ScopeModule {
left[f.ID] = f.Summary
}
}
for i := range mods {
if why, ok := left[keyID(mods[i].Module)]; ok {
if why == "" {
why = "seed-left-out"
}
mods[i].LeftOut = why
}
}
return mods
}