Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/seed.go
T

193 lines
7.4 KiB
Go

package index
import (
"context"
"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
// LeftOut says why the seed leaves the module out: an artifact of it no build seat builds, named with its language or
// kind (design 47 G A-324, A-329). Empty: the module is seeded.
LeftOut string
}
// 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 {
if m.LeftOut != "" {
out.Left = append(out.Left, fmt.Sprintf("%s (%s)", m.Module, m.LeftOut))
continue
}
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 writes the first registrations from the module graph the indexer holds, when it holds no registration (§8): a
// module in the module graph is a registration, so they are written at once, each as version 1, with no ask (the operator,
// 2026-11-14: "a module in the module graph IS a registration"). The registrations a seed key already composed are written as
// composed; otherwise they are composed from modules. A seed key still open is ended applied. again is kept for the tool's
// form and changes nothing. It answers what it wrote.
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
}
old, _, found, err := r.load(ctx, SeedKey)
if err != nil {
return "", err
}
regs := old.Seed
var left []string
if !found || len(regs) == 0 {
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)
}
regs, left = seed.Registrations, seed.Left
}
if len(regs) == 0 {
return "", errors.New("no module of the trunk revision can be registered: " + strings.Join(left, "; "))
}
if len(held) > 0 && (!found || old.State != ProposalOpen) {
return "", errors.New("the indexer holds registrations already: the seed is for the first start only")
}
// A seed that stopped part way (its key still open) writes the modules it has not written yet.
have := map[string]bool{}
for _, k := range held {
have[k] = true
}
w := oc.Warrant{Ask: "module-graph"}
written := 0
for _, reg := range regs {
if have[RegistrationKey(reg.Module)] {
continue
}
p := Proposal{V: 1, Verb: verbRegister, Module: reg.Module, Caller: by, At: r.now(), State: ProposalOpen,
Lines: []string{fmt.Sprintf("%s: registered from the module graph (%s)", reg.Module, reg.Origin)}}
if err := r.write(ctx, "", p, reg, w); err != nil {
return "", err
}
written++
}
said := fmt.Sprintf("%d registrations written from the module graph (%d held before)", written, len(held))
if found && old.State == ProposalOpen {
if err := r.end(ctx, SeedKey, old.Ask, ProposalApplied, said); err != nil {
r.logf("the seed key is not ended: %v", err)
}
}
if r.SeedWaits != nil && old.Ask != "" {
r.SeedWaits(old.Ask, false)
}
for _, l := range left {
r.logf("the seed left out: %s", l)
}
r.logf("the seed: %s", said)
return said, 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))
}