Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/retake.go
T
jschoubben 50cca4309f BL17c: the operator is asked once a key's event builds are spent; Build once more builds it as a person's build, Leave it nothing
The builder's asker on the seat operator-channel (level approve, about the key, state asked), the teller's three bus
adapters (publish, the router's record through the seat's holder on the seat list, decided heard through the hub's
events handler); the manifest uses the seat and consumes decided.

Builds: design 47 G §568, A-74
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-11 23:25:00 +01:00

125 lines
4.8 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
stdio "git.novox.be/novox/mesh-sdk/go"
"github.com/nats-io/nats.go/jetstream"
"mesh-delivery-builder/internal/build"
)
// The builder's retake of a key failed on a transport fault, on the node-engine seat's events (design 47 G §568, ADR 0673): the
// wiring of build.Retaker to the events consumer.
type retaking struct{ rt *build.Retaker }
// heard hands a message of the events consumer to the retake: one of its subjects whose state cannot take it is left for the bus to
// give again (Nak); every message goes on to the other parts.
func (g *retaking) heard(ctx context.Context, m jetstream.Msg, logf func(string, ...any)) (stop bool) {
meta, err := m.Metadata()
if err != nil {
return false
}
handled, err := g.rt.Heard(ctx, m.Subject(), m.Data(), meta.Sequence.Stream)
if handled && err != nil {
logf("%v; heard again", err)
if nakErr := m.Nak(); nakErr != nil {
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
}
return true
}
return false
}
// The builder's three bus adapters for the operator-channel client (design 47 G §568), as the teller has them: its own ask
// published, the router's record read through the seat, its own `decided` heard through the module's hub.
// publishVia publishes an accept of the seat, de-duplicated by id: the same id twice is one message.
func publishVia(ctx context.Context, js jetstream.JetStream) func(subject string, body any, id string) error {
return func(subject string, body any, id string) error {
data, err := json.Marshal(body)
if err != nil {
return err
}
var opts []jetstream.PublishOpt
if id != "" {
opts = append(opts, jetstream.WithMsgID(id))
}
_, err = js.Publish(ctx, subject, data, opts...)
return err
}
}
// routerBucket is the bucket of the router's asks: the state `asks` of the seat operator-channel (the contract's StateAsks) in
// the bucket of the seat's holder on the seat list, which the seat mesh-module-assigner's verb assignments answers with seats
// (design 47 G A-74); no module's name is in the program.
func routerBucket(call func(key string, body any) (json.RawMessage, error)) (string, error) {
raw, err := call("seat:"+mma.Seat+"."+mma.VerbAssignments, mma.AssignmentsInput{Seats: mma.AssignmentsInputSeatsTrue})
if err != nil {
return "", fmt.Errorf("who holds the seat %s is not asked of the seat %s: %w", oc.Seat, mma.Seat, err)
}
var held struct {
Seats []mma.SeatHolders `json:"seats"`
}
if err := json.Unmarshal(raw, &held); err != nil {
return "", fmt.Errorf("the answer of %s.%s is not its seat's: %w", mma.Seat, mma.VerbAssignments, err)
}
for _, s := range held.Seats {
if s.Seat == oc.Seat && len(s.Holders) > 0 {
return s.Holders[0].Module + "_" + oc.StateAsks, nil
}
}
return "", fmt.Errorf("no module holds the seat %s on the seat list, so the router's record of an ask is not read", oc.Seat)
}
// recordVia reads the router's record of one of this module's asks: a get of `<module>.<id>` in the bucket the seat's holder
// keeps (record is process.Conn.Ask).
func recordVia(call func(key string, body any) (json.RawMessage, error), record func(bucket, id string) (*stdio.StateEntry, error)) func(id string) (*stdio.StateEntry, error) {
return func(id string) (*stdio.StateEntry, error) {
bucket, err := routerBucket(call)
if err != nil {
return nil, err
}
return record(bucket, id)
}
}
// decisions holds the handler of the router's `decided` event for this module: the module's one hub has the pattern
// `mesh.seat.*.event.>`, which covers it, so the events handler hands the message here (a hub refuses two patterns that overlap).
type decisions struct {
subject string
handle func(jetstream.Msg)
}
// hear is the client's Subscribe: it keeps the handler of the warrants the router says to this module.
func (d *decisions) hear(logf func(string, ...any)) func(pattern string, handle func(stdio.Envelope) error) error {
return func(pattern string, handle func(stdio.Envelope) error) error {
d.handle = func(m jetstream.Msg) {
if err := handle(stdio.Envelope{Key: pattern, Body: m.Data()}); err != nil {
logf("a warrant could not be handled and is given again: %v", err)
if nakErr := m.Nak(); nakErr != nil {
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
}
return
}
if err := m.Ack(); err != nil {
logf("a warrant was handled but could not be acknowledged: %v", err)
}
}
return nil
}
}
// heard takes the message when it is this module's `decided`.
func (d *decisions) heard(m jetstream.Msg) bool {
if d.handle == nil || m.Subject() != d.subject {
return false
}
d.handle(m)
return true
}