Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/seed.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

173 lines
6.7 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
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"
)
// SeedModule is one module the trunk revision holds, as the seed reads it (design 47 G §8): its origin and the path of its
// manifest, and for each artifact whether its build seat answered `steps` and with which steps.
type SeedModule struct {
Module string
Origin string
Path string
Artifacts []SeedArtifact
}
// SeedArtifact is an artifact of a module and what its build seat answered `steps` with: Answered false is a seat that has not
// answered, which holds the seed (never read as none), and Steps empty with Answered true is none, so every step the
// artifact's seat detects none for is bypass.
type SeedArtifact struct {
Name string
Seat string
Answered bool
Steps []string
}
// SeedProposal is the first registrations composed from what the indexer took in (§8): the registrations proposed, the
// modules left out because their default branch is not protected, and the build seats still unanswered.
type SeedProposal struct {
Registrations []meshcatalog.Registration
Left []string
Unanswered []string
}
// ComposeSeed proposes one registration for every module its trunk revision holds (§8): the origin and path of each, its
// watched branch the repository's default branch read through the seat git, and the statuses the check judged until then:
// lint, unit tests, integration tests and composition required, code review optional, approval bypass; each step an
// artifact's build seat answers steps with none is bypass, so no module is held by a required step it has nothing for. A
// module whose default branch the forge does not protect is left out and named. A build seat that has not answered holds
// the seed.
func (r *Registry) ComposeSeed(modules []SeedModule) (SeedProposal, error) {
var out SeedProposal
slices.SortFunc(modules, func(a, b SeedModule) int { return strings.Compare(a.Module, b.Module) })
for _, m := range modules {
for _, a := range m.Artifacts {
if !a.Answered {
out.Unanswered = append(out.Unanswered, a.Seat)
}
}
}
slices.Sort(out.Unanswered)
out.Unanswered = slices.Compact(out.Unanswered)
if len(out.Unanswered) > 0 {
return out, nil
}
for _, m := range modules {
b, err := r.Branch(m.Origin, "")
if err != nil {
return out, fmt.Errorf("the seat git does not answer the default branch of %s: %w", m.Origin, err)
}
if b.Error != "" || b.Branch == "" {
return out, fmt.Errorf("the default branch of %s is not read: %s", m.Origin, b.Error)
}
if p, err := r.Branch(m.Origin, b.Branch); err != nil || !p.Protected || p.Error != "" {
out.Left = append(out.Left, fmt.Sprintf("%s (the branch %s of %s is not protected)", m.Module, b.Branch, m.Origin))
continue
}
reg := meshcatalog.Registration{Module: m.Module, Origin: m.Origin, Path: m.Path, Branches: []string{b.Branch},
Policy: meshcatalog.Policy{Steps: meshcatalog.StepStatuses{Lint: "required", Unit: "required", Integration: "required",
Composition: "required", Review: "optional", Approval: "bypass"}}}
for _, a := range m.Artifacts {
var s meshcatalog.StepStatuses
if !slices.Contains(a.Steps, "lint") {
s.Lint = "bypass"
}
if !slices.Contains(a.Steps, "unit") {
s.Unit = "bypass"
}
if !slices.Contains(a.Steps, "integration") {
s.Integration = "bypass"
}
if s != (meshcatalog.StepStatuses{}) {
if reg.Policy.Artifacts == nil {
reg.Policy.Artifacts = map[string]meshcatalog.StepStatuses{}
}
reg.Policy.Artifacts[a.Name] = s
}
}
out.Registrations = append(out.Registrations, reg)
}
return out, nil
}
// ProposeSeed raises the first registrations as one ask (§8), when the indexer holds no registration and the seed's key is not
// open or applied; again (indexer_seed_again) it raises the ask once more from a key a Decline, an expiry or a drop ended.
// It answers the ask's id, or why none was raised.
func (r *Registry) ProposeSeed(ctx context.Context, modules []SeedModule, again bool, by string) (string, error) {
r.mu.Lock()
defer r.mu.Unlock()
held, err := r.Registrations.Keys(ctx)
if err != nil {
return "", err
}
if len(held) > 0 {
return "", errors.New("the indexer holds registrations already: the seed is for the first start only")
}
if old, _, found, err := r.load(ctx, SeedKey); err != nil {
return "", err
} else if found && old.State == ProposalOpen {
return old.Ask, nil
} else if found && !again {
return "", fmt.Errorf("the seed ended %s: indexer_seed_again asks it again", old.State)
}
seed, err := r.ComposeSeed(modules)
if err != nil {
return "", err
}
if len(seed.Unanswered) > 0 {
return "", fmt.Errorf("the build seats %v have not answered steps: the seed waits for them (call-unanswered after five minutes)", seed.Unanswered)
}
if len(seed.Registrations) == 0 {
return "", errors.New("no module of the trunk revision can be registered: " + strings.Join(seed.Left, "; "))
}
change, err := json.Marshal(seed.Registrations)
if err != nil {
return "", err
}
p := Proposal{V: 1, Verb: verbSeed, Change: change, Seed: seed.Registrations, Caller: by, At: r.now(), State: ProposalOpen}
for _, g := range seed.Registrations {
p.Lines = append(p.Lines, fmt.Sprintf("%s: registered from %s at %s, watching %s", g.Module, g.Origin, g.Path, strings.Join(g.Branches, ", ")))
}
for _, l := range seed.Left {
p.Lines = append(p.Lines, "left out: "+l)
}
if p.Digest, err = DigestOf(map[string]any{"verb": verbSeed, "change": json.RawMessage(change)}); err != nil {
return "", err
}
if err := r.keepAndAsk(ctx, SeedKey, &p); err != nil {
return "", err
}
if r.SeedWaits != nil {
// The seed waits on the operator while its ask is open; only the warrant clears the finding (design 47 G §704).
r.SeedWaits(p.Ask, true)
}
return p.Ask, nil
}
// applySeed writes every registration the seed proposes, each as version 1 on the warrant, and ends the key applied.
func (r *Registry) applySeed(ctx context.Context, key string, p Proposal, w oc.Warrant) error {
if held, err := r.Registrations.Keys(ctx); err != nil || len(held) > 0 {
return r.endKey(ctx, key, p, ProposalRefused, "the indexer holds registrations already, so the seed writes none")
}
for _, reg := range p.Seed {
q := p
q.Verb, q.Module, q.Was = verbRegister, reg.Module, 0
q.Lines = []string{fmt.Sprintf("%s: registered by the first registrations", reg.Module)}
if err := r.write(ctx, "", q, reg, w); err != nil {
return err
}
}
if r.SeedWaits != nil {
r.SeedWaits(p.Ask, false)
}
return r.end(ctx, key, p.Ask, ProposalApplied, fmt.Sprintf("applied: %d registrations on %s", len(p.Seed), w.Ask))
}