Files
mesh-sdk/go/seatlist/holder.go
T
jschoubben 5fa64be894 Revert the revert of G's mesh-sdk merge (#191): G's sdk stands again over reader release 3
Main reverted G's half merge (#189, reverted by 8afc3eb in #191) and then took the reader release's S1-S5, which
copied G's code. G's form holds where they differ (process_bus.go, seats.go, state.go and their tests); the
release's kept names stay so main's callers keep building: SeatViewPrefix beside SeatReadPrefix, and QueueInvoke's
empty strings on not-ok (with its test TestQueueInvoke). The tree differs from G's head f30dee54 only by those and
the release's two tests (objects cap option, seat states and stream kinds).

Builds: design 47 G (the milestone's sdk part, as at f30dee54)

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

88 lines
3.2 KiB
Go

// Who holds a seat now (design 47 G A-126, beside the seat list): the module on record as the holder of a seat, asked of
// the seat that places modules (`mesh-module-assigner`) through its verb `assignments` with `seats` true, which answers the
// seat's holders. It is the one such reading: a module that reads a seat holder's state (a `reads` of `seat:<seat>.<state>`,
// A-116) and the asker of the operator (operatorask) call it, and no part keeps its own copy. It names the one seat and
// verb it asks, as this package names the seat that serves the seat list, and no module.
package seatlist
import (
"encoding/json"
"fmt"
"slices"
"strings"
)
const (
// HolderSeat is the seat whose verb answers who holds each seat.
HolderSeat = "mesh-module-assigner"
// HolderVerb is its verb, asked with `seats` true.
HolderVerb = "assignments"
)
// HolderCaller calls a seat verb or module tool as `seat:<seat>.<verb>` or `<module>.<tool>` and returns its answer (stdio.Ask is
// one; so is a process's calls).
type HolderCaller func(key string, body any) (json.RawMessage, error)
// HolderKey is the address asked, as stdio.Ask takes it; a host's manifest `invokes` lists it.
func HolderKey() string { return "seat:" + HolderSeat + "." + HolderVerb }
// HolderOf is the module holding the seat now: the first holder the seat's answer lists. It is an error while the answer cannot be
// read or no module holds the seat.
func HolderOf(call HolderCaller, seat string) (string, error) {
raw, err := call(HolderKey(), map[string]string{"seats": "true"})
if err != nil {
return "", fmt.Errorf("who holds the seat %s is not asked of %s: %w", seat, HolderKey(), err)
}
var held struct {
Seats []struct {
Seat string `json:"seat"`
Holders []struct {
Module string `json:"module"`
} `json:"holders"`
} `json:"seats"`
}
if err := json.Unmarshal(raw, &held); err != nil {
return "", fmt.Errorf("the answer of %s is not the seat holders': %w", HolderKey(), err)
}
for _, s := range held.Seats {
if s.Seat == seat && len(s.Holders) > 0 {
return s.Holders[0].Module, nil
}
}
return "", fmt.Errorf("no module holds the seat %s on record", seat)
}
// HoldersIn is a part's holder function (Part.Holders, design 47 G A-295): the modules holding a seat on any node, read from
// values, the seat-holders view of the seat that places modules as the part last heard it (a views follower's or a watched
// state's Values), so a callers entry `seat:<seat>` is resolved with no call per checked call. The view keys a seat held once
// `<seat>` and a node seat `<seat>.<node>`; none while the view cannot be read or nobody holds the seat.
func HoldersIn[V ~[]byte](values func() (map[string]V, error)) func(seat string) []string {
return func(seat string) []string {
keys, err := values()
if err != nil {
return nil
}
var out []string
for key, raw := range keys {
if key != seat && !strings.HasPrefix(key, seat+".") {
continue
}
var held struct {
Holders []struct {
Module string `json:"module"`
} `json:"holders"`
}
if json.Unmarshal([]byte(raw), &held) != nil {
continue
}
for _, h := range held.Holders {
if h.Module != "" && !slices.Contains(out, h.Module) {
out = append(out, h.Module)
}
}
}
return out
}
}