Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go
T
jschoubben 4938fb2d73 Names N2b: every seat list part's callers name seats, resolved from the seat-holders view
The builder, its tools bundle, the assigner (process and bundle) and the view's tools bundle answered
their seat list's again tool to the modules node-tools and mesh-healers. Their lists are now the
operator, seat:node-tool-runner and seat:mesh-healers, resolved by a holder function:
- meshcatalogclient's Branches.Claimant (moved from the builder's versions.HolderOf, which now asks it)
  names the claimant of mesh-module-assigner from the trunk's half, and Branches.SeatHolders follows
  that claimant's seat-holders view through the part's wiring. It drops the view and follows the new
  claimant's on a catalog change, asks nothing on a timer, and answers seatlist.HoldersIn.
- the assigner answers HoldersIn from its own seat-holders picture (its world in the process, the
  watched copy in the bundle).
The builder's and the view's manifests consume seat:mesh-module-assigner.views.seat-holders. The
assigner's seat-holders first-view tool answers both, by module as A-131 has it.

Builds: design 47 G A-298, A-295, A-296, A-210, A-131, §717, §616, §614

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

243 lines
7.5 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"slices"
"sync"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/views"
"mesh-delivery-builder/internal/build"
)
// **The builder reads the catalog half per watched branch** (design 47 G §348, §479, §614; A-5, A-15): the shared branch follower
// of meshcatalogclient, adopted on the builder's views reader, gives each watched branch's entries; the manifests, the packages
// and each package's name in its ecosystem are read from the entries, never the indexer's states.
// entryManifests keeps the manifest of each entry heard, by the entry's manifest digest (the entry holds the manifest whole).
type entryManifests struct {
mu sync.Mutex
byFp map[string]catalogue.Manifest
}
// keep takes the manifest of e in; an entry with no manifest or digest keeps none.
func (m *entryManifests) keep(e meshcatalog.CatalogEntry) error {
if e.ManifestDigest == "" || len(e.Manifest) == 0 {
return nil
}
raw, err := json.Marshal(e.Manifest)
if err != nil {
return err
}
// Decoded, never judged (novox/hq ADR 0578 §3): what a build needs of it is read, a field it does not know set aside.
mf, err := catalogue.Decode(raw)
if err != nil {
return fmt.Errorf("the manifest of the entry of %s does not decode: %w", e.Module, err)
}
m.mu.Lock()
defer m.mu.Unlock()
if m.byFp == nil {
m.byFp = map[string]catalogue.Manifest{}
}
m.byFp[e.ManifestDigest] = mf
return nil
}
// Manifest is the manifest the entry heard with that digest holds.
func (m *entryManifests) Manifest(_ context.Context, digest string) (catalogue.Manifest, error) {
m.mu.Lock()
defer m.mu.Unlock()
mf, ok := m.byFp[digest]
if !ok {
return catalogue.Manifest{}, fmt.Errorf("no entry of the catalog half holds the manifest %s", digest)
}
return mf, nil
}
// catalogHalf is the builder's part over the branch follower: what changes in a branch's half goes to the builder, and the
// entries of every followed half are what the package names are read from.
type catalogHalf struct {
branches *meshcatalogclient.Branches
b *build.Builder
manifests *entryManifests
logf func(string, ...any)
// named is, by module, the package names of its first entry last heard, so a name newly heard looks at its module again.
mu sync.Mutex
named map[string]string
// ready is told when every followed branch's first view is in, once; halves are the views adopted (sweepWiring).
ready func()
halves map[string]*views.Follower
once sync.Once
ctx context.Context
// floor is told each entry change: a floor whose tags or requirements were silent is asked again (design 47 G A-119).
floor func(context.Context)
// holders is told each entry change: a claim of a build seat may have moved (design 47 G A-82).
holders func()
}
// first says whether branch is the first watched branch of module's registration (design 47 G §371); a module with no
// registration heard is taken as watched on this branch, the first one.
func (c *catalogHalf) first(branch, module string) bool {
reg, ok := c.branches.Registrations.Registration(module)
return !ok || len(reg.Branches) == 0 || reg.Branches[0] == branch
}
// Changed is the follower's Changed: one module's entry of a branch came, or went.
func (c *catalogHalf) Changed(ctx context.Context) func(branch, module string, entry json.RawMessage, deleted bool, at time.Time) {
return func(branch, module string, entry json.RawMessage, deleted bool, _ time.Time) {
if c.holders != nil {
defer c.holders()
}
first := c.first(branch, module)
if deleted {
c.b.EntryGone(branch, first, module)
return
}
origin := build.Origin{Branch: branch, First: first}
if cat, ok := c.branches.Catalog(branch); ok {
if half, ok := cat.View(); ok {
origin.Source = half.Revision.Source
}
}
var e meshcatalog.CatalogEntry
if err := json.Unmarshal(entry, &e); err != nil || e.Module != module {
c.logf("the catalog half of the branch %q holds what is not the entry of %s, left: %v", branch, module, err)
return
}
if err := c.manifests.keep(e); err != nil {
c.logf("%v", err)
}
c.b.Entry(ctx, origin, e)
if c.floor != nil {
defer c.floor(ctx)
}
c.mu.Lock()
was := c.named[module]
now := namesOf(e)
if c.named == nil {
c.named = map[string]string{}
}
c.named[module] = now
c.mu.Unlock()
if first && now != "" && now != was {
c.logf("the catalog half's entry names a package of %s: its build is looked at again", module)
c.b.PackageNamed(ctx, module)
}
}
}
// namesOf are the package names an entry's artifacts carry, in one line.
func namesOf(e meshcatalog.CatalogEntry) string {
var names []string
for _, a := range e.Artifacts {
if a.Package != nil && a.Package.Name != "" {
names = append(names, a.Ref+"="+a.Package.Name)
}
}
slices.Sort(names)
return fmt.Sprint(names)
}
// sweepWiring is the follower's wiring on the builder's reader that learns each catalog half view adopted, so ready is told once
// when the first view of every branch followed is in (a branch with no revision yet is ready and empty at once).
type sweepWiring struct {
meshcatalogclient.Wiring
half *catalogHalf
}
func (w sweepWiring) Adopt(f *views.Follower, seatName, event string, hear func(context.Context, []byte) error) error {
if err := w.Wiring.Adopt(f, seatName, event, hear); err != nil {
return err
}
if seatName == meshcatalog.Seat && event == meshcatalog.EventCatalogRevision {
w.half.adopted(f)
}
return nil
}
// Follow follows a plain view through the wrapped wiring (meshcatalogclient's SeatHolders, A-298).
func (w sweepWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) {
f, ok := w.Wiring.(interface {
Follow(owner, state, tool string) (func() map[string]json.RawMessage, error)
})
if !ok {
return nil, fmt.Errorf("the builder's reader follows no plain view of %s.%s", owner, state)
}
return f.Follow(owner, state, tool)
}
func (w sweepWiring) Drop(owner, state string) error {
err := w.Wiring.Drop(owner, state)
w.half.dropped(state)
return err
}
func (c *catalogHalf) adopted(f *views.Follower) {
c.mu.Lock()
if c.halves == nil {
c.halves = map[string]*views.Follower{}
}
c.halves[f.State] = f
c.mu.Unlock()
go func() {
select {
case <-c.ctx.Done():
case <-f.Ready():
c.checkReady()
}
}()
}
func (c *catalogHalf) dropped(state string) {
c.mu.Lock()
delete(c.halves, state)
c.mu.Unlock()
c.checkReady()
}
// pictured are the build keys of the fingerprints the adopted halves' pictures hold, whether or not their entries were told yet.
func (c *catalogHalf) pictured() []string {
c.mu.Lock()
halves := make([]*views.Follower, 0, len(c.halves))
for _, f := range c.halves {
halves = append(halves, f)
}
c.mu.Unlock()
var keys []string
for _, f := range halves {
for module, raw := range f.Values() {
var e meshcatalog.CatalogEntry
if json.Unmarshal(raw, &e) != nil || e.Fp == "" {
continue
}
if k, err := builds.BuilderBuildKey(module, e.Fp); err == nil {
keys = append(keys, k)
}
}
}
return keys
}
// checkReady calls ready once every adopted half's first view is in.
func (c *catalogHalf) checkReady() {
c.mu.Lock()
all := len(c.halves) > 0
for _, f := range c.halves {
select {
case <-f.Ready():
default:
all = false
}
}
c.mu.Unlock()
if all && c.ready != nil {
c.once.Do(c.ready)
}
}