Files
mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/claimant.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

164 lines
5.0 KiB
Go

package meshcatalogclient
import (
"context"
"encoding/json"
"slices"
"sync"
seat "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/seatlist"
)
// seatHoldersState is the state of the seat that places modules (seatlist.HolderSeat) its definition offers as a view: who holds
// which seat, keyed `<seat>` and `<seat>.<node>` (§616, §699).
const seatHoldersState = "seat-holders"
// Claimant is the module that claims seat: the one whose entry in the first followed branch's half (the trunk's) has a manifest
// that claims it (design 47 G A-82, §614, A-298). More than one claimant, or none, is none.
func (b *Branches) Claimant(seatName string) (string, bool) {
followed := b.Followed()
if len(followed) == 0 {
return "", false
}
cat, ok := b.Catalog(followed[0])
if !ok {
return "", false
}
half, ok := cat.View()
if !ok {
return "", false
}
var holders []string
for _, e := range half.Entries {
if slices.Contains(b.claimed(e), seatName) {
holders = append(holders, e.Module)
}
}
if len(holders) != 1 {
return "", false
}
return holders[0], true
}
// claimed is the seats an entry's manifest claims, decoded once per manifest digest.
func (b *Branches) claimed(e seat.CatalogEntry) []string {
b.claimsMu.Lock()
defer b.claimsMu.Unlock()
if seats, ok := b.claims[e.ManifestDigest]; ok && e.ManifestDigest != "" {
return seats
}
var seats []string
if raw, err := json.Marshal(e.Manifest); err == nil {
if mf, err := catalogue.Decode(raw); err == nil {
for _, c := range mf.Claims {
seats = append(seats, c.Name)
}
}
}
if e.ManifestDigest != "" {
if b.claims == nil {
b.claims = map[string][]string{}
}
b.claims[e.ManifestDigest] = seats
}
return seats
}
// viewFollowing is a Wiring that also follows an owner's state as a plain view (Reader.Follow, ProcessReader.Follow), and answers
// the keys it last heard.
type viewFollowing interface {
Follow(owner, state, tool string) (func() map[string]json.RawMessage, error)
}
func (w readerWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) {
f := w.r.Follow(owner, state, tool)
return f.Values, nil
}
func (w processWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) {
p, err := w.r.Follow(owner, state, tool)
if err != nil {
return nil, err
}
return p.Follower().Values, nil
}
// seatHolders is the part's follow of the claimant's seat-holders view: the claimant followed and what it answers.
type seatHolders struct {
mu sync.Mutex
owner string
values func() map[string]json.RawMessage
kick chan struct{}
}
// SeatHolders is the part's holder function (seatlist.Part.Holders, design 47 G A-295, A-298, §717): it follows the view
// `seat-holders` of the module the catalog half names as the claimant of the seat that places modules (seatlist.HolderSeat), its
// first view asked of the claimant's tool `seat-holders`, and at each catalog change naming another claimant drops that view and
// follows the new one's (§532); with no claimant it keeps what it has. Nothing is asked on a timer. The part's manifest consumes
// `seat:mesh-module-assigner.views.seat-holders`. A Wiring that follows no plain view answers no holders, said in the log.
func (b *Branches) SeatHolders(ctx context.Context) func(seat string) []string {
sh := &seatHolders{kick: make(chan struct{}, 1)}
b.holders.Store(sh)
fw, ok := b.w.(viewFollowing)
if !ok {
b.logf("meshcatalogclient: this part's reader follows no plain view, so no seat's holders are known")
} else {
go func() {
for {
select {
case <-ctx.Done():
return
case <-sh.kick:
}
b.refollow(sh, fw)
}
}()
sh.changed()
}
return seatlist.HoldersIn(func() (map[string]json.RawMessage, error) {
sh.mu.Lock()
values := sh.values
sh.mu.Unlock()
if values == nil {
return nil, nil
}
return values(), nil
})
}
// changed asks the follow to read the claimant again, without blocking the caller (a view's change is told under its lock).
func (sh *seatHolders) changed() {
if sh == nil {
return
}
select {
case sh.kick <- struct{}{}:
default:
}
}
// refollow follows the claimant's seat-holders view when the claimant is another than the one followed.
func (b *Branches) refollow(sh *seatHolders, fw viewFollowing) {
claimant, ok := b.Claimant(seatlist.HolderSeat)
sh.mu.Lock()
defer sh.mu.Unlock()
if !ok || claimant == sh.owner {
return
}
if sh.owner != "" {
if err := b.w.Drop(sh.owner, seatHoldersState); err != nil {
b.logf("meshcatalogclient: the seat-holders view of %s is not dropped: %v", sh.owner, err)
}
sh.owner, sh.values = "", nil
}
values, err := fw.Follow(claimant, seatHoldersState, claimant+"."+seatHoldersState)
if err != nil {
b.logf("meshcatalogclient: the seat-holders view of %s is not followed: %v", claimant, err)
return
}
sh.owner, sh.values = claimant, values
b.logf("meshcatalogclient: the seat-holders view of %s, the claimant of %s, is followed", claimant, seatlist.HolderSeat)
}