diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index 1af26f74..72177123 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -425,9 +425,9 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, hub := process.NewEventHub(logger.Printf) // The seat list (seatlist.go), read over the connection and heard on the hub, given to catalogue at every change, and a // pass made then: registered before the hub starts. - gate := &seatGate{} + gate := seatlist.NewPart("assigner", seatListAgainCallers) if bus != nil { - gate.follow(seatlist.ProcessBus(bus.Call, hub), wake, logger.Printf) + gate.Follow(seatlist.ProcessBus(bus.Call, hub), wake, logger.Printf) } // The indexer's manifests, modules and module graph, through its first views and views family, never its buckets // (DLV-F2r, ADR 0631): the first pass waits for the three answers, as it waits for every watch to hand what stands. @@ -502,7 +502,7 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, logger.Printf("a pipeline's end or a registration is not heard, so the wants keep the trunk-only form until the assigner starts again: %v", err) } } - hearReadsChanged(ctx, js, hub, reads, findings, gate.eventsLive, logger.Printf) + hearReadsChanged(ctx, js, hub, reads, findings, gate.EventsLive, logger.Printf) hearRunning(ctx, js, w, wake, findings, logger.Printf) } if bus != nil && kvs[buckets.Puts] != nil { @@ -603,7 +603,7 @@ func hearReadsChanged(ctx context.Context, js jetstream.JetStream, hub *process. } w.Heard(process.EventsStream) if live != nil { - live() // the seat list, unread while the seat was silent, is read now (seatGate.eventsLive) + live() // the seat list, unread while the seat was silent, is read now (seatlist.Part.EventsLive) } go func() { <-ctx.Done() diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 8c5d6aa2..ef0ccc4f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -18,6 +18,8 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate" "git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay" "git.novox.be/novox/mesh-sdk/go/motion" + "git.novox.be/novox/mesh-sdk/go/seatlist" + "sync" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" "git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient" @@ -79,7 +81,8 @@ type passer struct { seat *seatActs // seats is the seat list the process reads (seatlist.go): until it is read, seat-holders is not written and a proposal // laid again is not ended on a seat it does not name (ADR 0636 §1). Nil reads nothing. - seats *seatGate + seats *seatlist.Part + seatWaitSaid sync.Once publish publisher saidProposed map[string]bool // copied says the retired state was copied into `assignments` by this process, or found empty (copyRetired). @@ -1248,11 +1251,11 @@ func (p *passer) seatHolders(ctx context.Context) { if len(assignments) == 0 { return // no assignments read yet: nothing is known of who holds what, and nothing is deleted } - if !p.seats.read() { + if !p.seats.Heard() { // ADR 0636 §1: which claim names which seat is the list's, so nothing is written from the bootstrap alone; the // reader's change makes a pass. if p.seats != nil { - p.seats.waitSaid.Do(func() { p.logf("seat-holders is not written until the seat list is read: %s", p.seats.health()) }) + p.seatWaitSaid.Do(func() { p.logf("seat-holders is not written until the seat list is read: %s", p.seats.Health()) }) } return } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go index dfb5d04d..8efd714f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go @@ -440,10 +440,10 @@ func (p *passer) mustAnswer(ctx context.Context, prop mma.Proposal, rev uint64, func (p *passer) layAgain(ctx context.Context, prop mma.Proposal, rev uint64, current map[string]mma.Assignments, revs map[string]uint64, why string) { held, recorded := p.lays(ctx) again, laid, findings := assign.Propose(assign.ProposeIn{ID: prop.ID, By: prop.By, Why: prop.Why, At: prop.At, Changes: prop.Changes, - Current: current, Revisions: revs, Held: held, Recorded: recorded, SeatListRead: p.seats.read()}) + Current: current, Revisions: revs, Held: held, Recorded: recorded, SeatListRead: p.seats.Heard()}) if slices.ContainsFunc(findings, plc.WaitsForSeatList) { // ADR 0636 §1: nothing is ended on a list not read; the reader's change makes a pass, which lays it again. - p.logf("the proposal %s is not laid again yet: %s", prop.ID, p.seats.health()) + p.logf("the proposal %s is not laid again yet: %s", prop.ID, p.seats.Health()) return } if len(findings) > 0 { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go index 6d63a9f9..e459a792 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go @@ -19,6 +19,7 @@ import ( mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/seatlist" "git.novox.be/novox/mesh-sdk/go/views" "mesh-delivery-assigner/internal/assign" @@ -86,7 +87,7 @@ type seatTools struct { now func() time.Time // seats is the seat list the bundle reads (seatlist.go): until it is read, propose judges no mesh seat the bootstrapped // definitions do not name as held once (ADR 0636 §1). Nil reads nothing. - seatList *seatGate + seatList *seatlist.Part } func newSeatTools(st seatStore, ask func(string, any) (json.RawMessage, error)) *seatTools { @@ -265,7 +266,7 @@ func (s *seatTools) doPropose(args map[string]any, by string) (any, error) { enrollers := plc.EnrollersOf(current) now := s.now() p, laid, findings := assign.Propose(assign.ProposeIn{ID: plc.NewProposalID(now), By: by, Why: arg(args, "why"), At: now, - Changes: changes, Current: current, Revisions: revs, Held: held, Recorded: recorded, Warrant: warrant, SeatListRead: s.seatList.read()}) + Changes: changes, Current: current, Revisions: revs, Held: held, Recorded: recorded, Warrant: warrant, SeatListRead: s.seatList.Heard()}) if refusal := plc.AddNodeRefusal(p, moduleOfCaller(by, enrollers), enrollers); refusal != "" { findings = append(findings, refusal) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go index 704f820c..c884c495 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go @@ -257,7 +257,7 @@ func TestTheSeatsVerbs(t *testing.T) { // Such a session is proved as its machine's tool runner (issue 627); the healers too, no other module. for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "ace.mesh-healers": false, "ace.messenger": true} { - if err := seatlist.Refusal(seatListAgainTool, seatListAgainCallers, caller); (err != nil) != refused { + if err := seatlist.Refusal("assigner_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused { t.Errorf("%s: refused=%v, err %v", caller, refused, err) } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go index ec80d6e0..61e8d076 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go @@ -1,103 +1,13 @@ package main -import ( - "errors" - "sync" - "sync/atomic" +import stdio "git.novox.be/novox/mesh-sdk/go" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/catalogue" - "git.novox.be/novox/mesh-sdk/go/seatlist" -) - -// The assigner reads the seat list as every part does (novox/hq ADR 0635 §1, ADR 0636 §1, §7; issue 1966): through the seat -// mesh-catalog's verb seat-definitions and its two events, from the bootstrapped definitions until the seat has answered. What -// the reader holds is given to catalogue at every change, which the check of a proposal and the seat-holders state read: which -// mesh seat is replicated, and which name is a seat's. Both of its runtimes hold one: the process (the proposals it lays again, -// the state seat-holders) over its connection and the event hub, and the tools' bundle (the seat's verb propose) over the -// runtime. Until the list is read, a mesh seat the bootstrapped definitions do not name is never judged held once +// The assigner reads the seat list as every part does, through mesh-sdk's one wiring (go/seatlist.Part; design 47 G §204): its +// reader over the seat mesh-catalog's verb seat-definitions and its two events, its tools `assigner_seat_list` and +// `assigner_seat_list_again`. What the reader holds is given to catalogue at every change, which the check of a proposal and the +// seat-holders state read; until the list is read, a mesh seat the bootstrapped definitions do not name is never judged held once // (plc.CheckAssignments), and seat-holders is not written. -// Seat list tools (ADR 0636 §7): what the reader holds, and reading it again now, which every operator's session and the -// healers may ask. -const ( - seatListTool = "assigner_seat_list" - seatListAgainTool = "assigner_seat_list_again" -) - // A call through the mesh MCP server is proved only as its machine's tool runner (.node-tools, issue 627), so // node-tools stands for an operator's session until a session is proved (issue 1966). var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"} - -// seatGate is the reader a runtime of the assigner judges by, set once it is started; before, the list counts as unread. -type seatGate struct { - reader atomic.Pointer[seatlist.Reader] - // waitSaid says the journal was told that seat-holders waits for the list, so it is told once per wait. - waitSaid sync.Once -} - -// read says the seat mesh-catalog has answered the reader: the list is read (seatlist.Status's Heard). A nil gate, or one -// whose reader has not started, has read nothing. -func (g *seatGate) read() bool { - if g == nil { - return false - } - r := g.reader.Load() - return r != nil && r.Status().Heard -} - -// health is why the list is not read, or empty. -func (g *seatGate) health() string { - if g == nil { - return "the assigner follows no seat list" - } - r := g.reader.Load() - if r == nil { - return "the seat list reader has not started yet" - } - return r.Status().Health() -} - -// follow starts the reader over bus: what it holds is given to catalogue at once and at every change, and changed is called -// then (the process makes a pass, which lays again what waited). -func (g *seatGate) follow(bus seatlist.Bus, changed func(), logf func(string, ...any)) *seatlist.Reader { - reader, err := seatlist.Start(seatlist.Config{Bus: bus, Bootstrap: seatlist.Bootstrapped(), - OnChange: func(seats []catalogue.Seat) { - catalogue.UseSeats(seats) - if changed != nil { - changed() - } - }}) - if err != nil { - logf("the assigner cannot follow its seat list, so a mesh seat the bootstrapped definitions do not name waits for it: %v", err) - } - if reader != nil { - catalogue.UseSeats(reader.Seats()) - g.reader.Store(reader) - } - return reader -} - -// eventsLive is called once the process's consumer on EVENTS is heard: the seat may have said it speaks before that consumer -// existed, whose events start at the stream's end, so while the list is unread it is read now (ADR 0636 §1). -func (g *seatGate) eventsLive() { - if r := g.reader.Load(); r != nil { - r.ReloadIfUnread() - } -} - -// tools are the reader's two tools over whatever reader the gate holds at the call. -func (g *seatGate) tools() []stdio.Tool { - shape := seatlist.Tools(&seatlist.Reader{}, seatListTool, seatListAgainTool, seatListAgainCallers) - for i := range shape { - run := i - shape[i].Run = func(args map[string]any) (any, error) { - r := g.reader.Load() - if r == nil { - return nil, errors.New("the seat list reader has not started yet: the runtime has not served this module") - } - return seatlist.Tools(r, seatListTool, seatListAgainTool, seatListAgainCallers)[run].Run(args) - } - } - return shape -} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go index 67f44e2f..8cf119c9 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist_test.go @@ -3,6 +3,9 @@ package main import ( "encoding/json" "errors" + "os" + "path/filepath" + "strings" "sync" "testing" "time" @@ -50,21 +53,44 @@ func (standInCatalog) Subscribe(string, func(string, json.RawMessage) error) err // gateOver is a seat gate following the stand-in, once its first read has ended. A read one gives catalogue the test list // (catalogue.UseSeats), as a host's does, for the tests after it too: the list is the same everywhere. -func gateOver(t *testing.T, silent bool) *seatGate { +func gateOver(t *testing.T, silent bool) *seatlist.Part { t.Helper() if silent { // A silent seat reached no one: its reader asks again within the bus's retry bound (issue 1966), which a test // that waits for it to stay unread keeps short. t.Cleanup(busretry.Waits(20*time.Millisecond, time.Millisecond, 5*time.Millisecond)) } - g := &seatGate{} - r := g.follow(standInCatalog{silent: silent}, nil, t.Logf) + g := seatlist.NewPart("assigner", seatListAgainCallers) + r := g.Follow(standInCatalog{silent: silent}, nil, t.Logf) if r == nil { t.Fatal("the reader did not start") } <-r.Ready() - if g.read() == silent { - t.Fatalf("the gate read %v over a stand-in silent %v: %s", g.read(), silent, g.health()) + if g.Heard() == silent { + t.Fatalf("the gate read %v over a stand-in silent %v: %s", g.Heard(), silent, g.Health()) } return g } + +// Design 47 G §204: the seat list's reader is wired once, by mesh-sdk's go/seatlist.Part; no part wires its own. A search of the +// assigner's code finds no start of a reader and no tools of its own. +func TestTheSeatListIsWiredByTheSDKAlone(t *testing.T) { + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + for _, f := range files { + if strings.HasSuffix(f, "_test.go") { + continue + } + raw, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + for _, own := range []string{"seatlist.Start(", "seatlist.Tools(", "OnChange:"} { + if strings.Contains(string(raw), own) { + t.Errorf("%s wires the seat list itself (%s), where mesh-sdk's seatlist.Part does it", f, own) + } + } + } +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go index 9b946428..1c7ec141 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -386,15 +386,15 @@ func serveTools() error { } }() // The seat list (seatlist.go), read through the runtime once Serve has its channel: the seat's verbs judge by it. - gate := &seatGate{} + gate := seatlist.NewPart("assigner", seatListAgainCallers) go func() { <-stdio.Served() - gate.follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-delivery-assigner: "+f+"\n", a...) }) + gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-delivery-assigner: "+f+"\n", a...) }) }() seatVerbs := newSeatTools(runtimeStore{}, stdio.Ask) seatVerbs.seatList = gate tools := append(s.tools(), seatVerbs.tools()...) - tools = append(tools, gate.tools()...) + tools = append(tools, gate.Tools()...) wants := newWantsTools(runtimeStore{}, stdio.Ask, func(e, id string, b any) error { // The seat's event, said on the seat: a claimed seat's event is never the module's own. return stdio.SeatPublish(wantsEventSubject(e), b, id)