Files
mesh-delivery/modules/mesh-grants/cmd/mesh-grants/main.go
T
jschoubben 20af25ea40 The seat's events wait for the first view unacknowledged; the checks prove the order and the dropped case
The process hands an event of the seat mesh-broker to BindingsView.Hold, which
applies and acknowledges it after the answer or past the bound, so a restart
finds it delivered again (one delivered again while held keeps its place by its
stream sequence). Unit rows: the answer's bucket differs from the held created's
and the held event's stands. Whole test: an answer that lists a binding dropped
since its ask does not write it back (fails without the check). Neutral caller
name in the by-hand asks.

Builds: design 25 bindings-first-view §3, §4, §8 (hq d0760629, PR 543)

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 11:01:39 +01:00

258 lines
12 KiB
Go

// Command mesh-grants is the grants part (novox/hq ADR 0547 clauses 5 to 8, issue 1945): a process on its own bus
// credential that watches assignments, the assigner's wants, the indexer's manifests, the builder's builds, mesh-config's
// resolved values and the node agents' haves, and on every change composes each assignment's membership, the bus's user
// list and each manifest's buckets, logs and streams with the bus composer the controller uses (mesh-sdk buscompose).
//
// **Memberships are published for real** (the operator, 2026-10-25 22:31): since release 6 nobody publishes them, so a
// module placed since cannot serve its seat or reach a new state. Each is published in the part's own state memberships
// only when it differs from the one last published there. **A bucket, log or stream a manifest declares that the bus lacks is
// created** (the operator, 2026-10-26 08:54; issue 1790), once per manifest, with the manifest's limits, on the event
// that brought the manifest; one the bus holds is never changed. **The user list is written sealed to the bus node**
// (ADR 0547 clause 7) as an object of the part's own bucket named in `user-list.sealed`, which the bus node follows
// alone since the controller ended by its unassignment (novox/hq ADR 0590, issue 1126). **The grants** the assigner
// reads are written, and **the account consumers** made; a stream the bus holds gains only the subjects its manifest adds (a consumer the filters), never loses one or changes a limit; any other difference is only said.
//
// It holds no timer, and never exits for a refused read, write or publish: a refusal is said and tried again at the next
// event.
//
// Started by the tool runner, with no credential of its own, it serves those tools (a process never serves, ADR 0436).
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/announce"
"git.novox.be/novox/mesh-sdk/go/process"
"git.novox.be/novox/mesh-sdk/go/seatlist"
"git.novox.be/novox/mesh-sdk/go/views"
"mesh-grants/internal/grants"
)
func main() {
if os.Getenv("MESH_BROKER_FILE") == "" {
if err := serveTools(); err != nil {
fmt.Fprintln(os.Stderr, "mesh-grants:", err)
os.Exit(1)
}
return
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx, os.Getenv, os.Stderr); err != nil {
fmt.Fprintln(os.Stderr, "mesh-grants:", err)
os.Exit(1)
}
}
// bindingsTiming is the bound and the waits of the first view of the bindings; zero is the design's (two minutes; from ten
// seconds, doubling, at most five minutes: design 25 bindings-first-view §5). The whole-module test sets small ones.
var bindingsTiming struct{ bound, wait, max time.Duration }
// keptStates is what the part keeps (ADR 0495 §2-§3): each readable, for the view and a session, but the wanted accounts,
// by the vault alone (ADR 0561 §2).
func keptStates() []announce.State { return grants.Announced() }
// run is the process: it returns when ctx ends, or when it has no credential or cannot dial the bus at all. A bucket or
// read the bus refuses never ends it.
func run(ctx context.Context, getenv func(string) string, logOut io.Writer) error {
logger := log.New(logOut, "mesh-grants: ", log.LstdFlags)
path := strings.TrimSpace(getenv("MESH_BROKER_FILE"))
if path == "" {
return errors.New("MESH_BROKER_FILE is not set, so there is no credential to read the bus with")
}
account, err := process.ReadAccount(path)
if err != nil {
return err
}
f := &grants.Follower{Logf: logger.Printf}
conn, err := process.Connect(account, nats.Name(account.Node+"."+account.Module), nats.RetryOnFailedConnect(true),
nats.ReconnectHandler(func(*nats.Conn) { f.Wake() }))
if err != nil {
return err
}
defer conn.Close()
if err := conn.Keeps(keptStates()...); err != nil {
logger.Printf("what the part keeps could not be said on the bus, so its readers are not granted until it is: %v", err)
}
// The reads of other parts' state, on a connection of the same account: a read the bus refuses is said and composed
// past (busReader), never the end of the process.
opts := []nats.Option{
nats.Name(account.Node + "." + account.Module + ".reads"),
nats.UserInfo(account.User, account.Password),
nats.CustomInboxPrefix(account.InboxPrefix()),
nats.MaxReconnects(-1),
nats.RetryOnFailedConnect(true),
nats.ReconnectHandler(func(*nats.Conn) { f.Wake() }),
}
if strings.TrimSpace(account.Fingerprint) != "" {
opts = append(opts, nats.Secure(process.PinnedToFingerprint(account.Fingerprint)))
}
nc, err := nats.Connect(account.URL, opts...)
if err != nil {
return fmt.Errorf("connecting to the bus: %w", err)
}
defer nc.Close()
js, err := jetstream.New(nc)
if err != nil {
return err
}
bus := newBusReader(ctx, js)
// The module has one durable consumer of EVENTS: one hub pulls it and hands each message to the part that hears it (the
// owners' views events, the seat mesh-catalog's verdicts and seat list events, the other seats' events the follower hears),
// started once all are registered.
hub := process.NewEventHub(logger.Printf)
defer hub.Stop()
// The owners' five states by their first views (ADR 0615, ADR 0622): asked through the part's own account, then
// followed on the owners' views families, which the hearing below hands them.
sayCond := sayVia(ctx, js, account.Module)
fv := grants.NewViews(func(tool string) func(context.Context, string) (views.Answer, error) {
return grants.TimedAsk(tool, views.AskVia(grants.Unwrapped(conn.Call), tool), time.Now, logger.Printf)
}, sayCond, f.Wake, logger.Printf)
fv.Rest, fv.Watching = bus, bus
byState := map[string]*views.Follower{}
for _, v := range grants.FirstViews {
byState[v.State] = fv.Followers[v.Read]
}
hearViews := views.Hear(ctx, byState, logger.Printf)
f.Part = &grants.Part{Read: fv, Call: conn, Store: ownStore{conn}, Emit: emitting{conn}, Views: fv, FirstViewContext: ctx,
Create: creating{ctx: ctx, js: js}, Consumers: creating{ctx: ctx, js: js},
Objects: sealedObjects{ctx: ctx, js: js}, Purging: ownPurging{ctx: ctx, js: js, module: account.Module}, Logf: logger.Printf,
Bus: grants.BusAddress{URL: account.URL, Fingerprint: account.Fingerprint}}
f.Watch = fv
// The first view of the bindings (design 25 bindings-first-view): the seat mesh-broker's list, asked at each start and by
// hand, with the seat's events held until its answer is applied or its bound passes.
f.Part.Bindings = &grants.BindingsView{Say: sayCond, Changed: f.Wake, Logf: logger.Printf,
Bound: bindingsTiming.bound, Wait: bindingsTiming.wait, MaxWait: bindingsTiming.max,
Ask: func(context.Context) (json.RawMessage, error) {
return grants.Unwrapped(conn.Call)(grants.CallBrokerList, map[string]any{})
}}
f.Part.Bindings.Start(ctx, f.Part)
// The seat list: read over the connection and the hub, given to catalogue at every change, and a pass composes again then.
gate := &seatGate{}
gate.follow(seatlist.ProcessBus(conn.Call, hub), f.Wake, logger.Printf)
f.Part.SeatList = gate
f.Part.Say = sayCond
fv.Start(ctx, f.Wake)
f.Asked = func(wake func()) error {
return conn.State(grants.StateUserList).Watch(grants.KeyAsked, func(c stdio.StateChange) error {
if !c.Current {
wake()
}
return nil
})
}
// The part makes this module's own durable consumer on EVENTS from the manifest's consumes (filtered on the two
// condition subjects and the vault's account-issued; ADR 0561 §6), as it makes every module's; it is pulled and every message acknowledged, so its pending never grows. Where it is not there yet the pass says so and
// hears again at the next change. The vault's bus-accounts is watched beside it, so a hash is composed either way.
//
// Beside EVENTS, the module streams its consumes reach (the bus module's own stream, whose created, recreated and
// dropped carry the bindings: ADR 0595 §1, §7), each on the durable consumer its own membership names; one not
// there yet is opened at the next change, and one opened is never opened twice.
opened := map[string]bool{}
registered := false
f.Hear = func(handle func(string, []byte)) error {
if !registered {
registered = true
// An owner's views event goes to the follower of its state, which acknowledges it (firstviews.go).
// The hub says when the bus made its consumer: a picture asked before then is asked again (views.Follower.ConsumerMade).
hub.OnBound(func(created time.Time) {
for _, f := range byState {
f.ConsumerMade(ctx, created)
}
})
if err := hub.Handle(views.EventsPattern, hearViews); err != nil {
return err
}
// The seat mesh-catalog's verdict events go to the verdicts' first view (ADR 0622 §2), which takes each at its revision.
for _, event := range []string{meshcatalog.EventManifestAccepted, meshcatalog.EventManifestRefused} {
if err := hub.Handle("mesh.seat."+meshcatalog.Seat+".event."+event, func(m jetstream.Msg) {
if err := fv.HearVerdict(ctx, strings.TrimPrefix(m.Subject(), "mesh.seat."+meshcatalog.Seat+".event."), m.Data(), logger.Printf); err != nil {
logger.Printf("views: %s is not a verdict event, dropped: %v", m.Subject(), err)
_ = m.Term()
return
}
_ = m.Ack()
}); err != nil {
return err
}
}
// The other seats' events the part follows its passes by (Consumes).
for _, subject := range grants.SeatEventSubjects() {
if err := hub.Handle(subject, func(m jetstream.Msg) {
// The seat mesh-broker's events wait for the first view's answer, unacknowledged (design 25
// bindings-first-view §3): a restart finds them delivered again.
if strings.HasPrefix(m.Subject(), grants.BrokerEventPrefix) && f.Part.Bindings != nil {
var seq uint64
if md, err := m.Metadata(); err == nil {
seq = md.Sequence.Stream
}
if f.Part.Bindings.Hold(seq, m.Subject(), m.Data(), func() { _ = m.Ack() }) {
return
}
}
handle(m.Subject(), m.Data())
_ = m.Ack()
}); err != nil {
return err
}
}
}
if err := hub.Start(ctx, js, account); err != nil {
return err
}
gate.eventsLive()
hears, err := grants.OwnHears(ownStore{conn}, account.Node)
if err != nil {
return err
}
for _, h := range hears {
if opened[h.Stream] {
continue
}
cons, err := js.Consumer(ctx, h.Stream, h.Consumer)
if err != nil {
return fmt.Errorf("the consumer %s on %s: %w", h.Consumer, h.Stream, err)
}
if _, err := cons.Consume(func(m jetstream.Msg) {
handle(m.Subject(), m.Data())
_ = m.Ack()
}, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { logger.Printf("hearing the bus: %v", err) })); err != nil {
return err
}
opened[h.Stream] = true
}
return nil
}
// Once, before the first pass writes a list's keys: the sealed lists' keys a delete left in the state user-list are
// purged (issue 1966), said with the count and the bytes before and after.
if sw, err := f.Part.SweepSealed(); err != nil {
logger.Printf("the sealed lists' keys of %s were not swept: %v", grants.StateUserList, err)
} else {
for _, e := range sw.Errors {
logger.Printf("%s", e)
}
logger.Printf("swept the sealed lists' keys of %s: %d marked by a delete and %d of lists no longer kept purged, the kept lists %v untouched (no index: %v); %d bytes before, %d after",
grants.StateUserList, sw.Marked, sw.Orphaned, sw.Kept, sw.NoIndex, sw.BytesBefore, sw.BytesAfter)
}
logger.Printf("started on the account %s: publishing memberships, writing grants and the user list sealed to the bus node, which it follows, and creating the account consumers (the heard-stream ones too) and the declared buckets, logs and streams the bus lacks; a held stream and a held consumer only ever gain the subjects and filters a manifest adds", account.User)
f.Run(ctx)
return nil
}