Files
mesh-delivery/modules/mesh-grants/internal/buscompose/chunks.go
T
jschoubben a2ac794e7a GR-chunks (A-60, §681): the chunk subjects of a chunks verb go to the principal that answers it, the reverse to an account naming it; the keeper's standing restore of restricted streams
The seat list's verbs saying `chunks: true` (ChunkVerbsOf, as KeptEventsOf) give the principal that answers the verb (a module's
own account for a seat it holds, the tool runner's for a carried module's) the publish of the chunk subjects and the
subscription to the acks, and an account naming the verb by name in `invokes` the reverse; a `*` entry none. The bus module's
keeper holds a standing grant of the restore subjects of the restricted streams, and only those.

Builds: design 47 G §681, A-45, A-60
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-11 23:03:05 +01:00

105 lines
3.5 KiB
Go

package buscompose
import (
"slices"
"sort"
"strings"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// **The chunk subjects of a verb that sends a file** (design 47 G §681, A-45, A-60): a verb whose seat definition says `chunks: true`
// has its answer's file sent in chunks on `mesh.seat.<seat>.chunk.<verb>.<call>.<n>`, each acknowledged by the caller on
// `mesh.seat.<seat>.ack.<verb>.<call>.<n>`. They are granted to the principal that answers the verb (publish the chunks, subscribe to
// the acknowledgements) and, reversed, to an account that names the verb by name in `invokes`; a `*` entry grants none.
// ChunkVerb is one verb of a seat whose definition says it sends chunks.
type ChunkVerb struct{ Seat, Verb string }
// ChunkVerbsOf is every verb the seat definitions say send chunks, sorted. Pure.
func ChunkVerbsOf(defs map[string]seatdef.Definition) []ChunkVerb {
var out []ChunkVerb
for _, d := range defs {
for _, v := range d.Verbs {
if v.Chunks {
out = append(out, ChunkVerb{Seat: d.Seat, Verb: v.Name})
}
}
}
sort.Slice(out, func(i, j int) bool {
if out[i].Seat != out[j].Seat {
return out[i].Seat < out[j].Seat
}
return out[i].Verb < out[j].Verb
})
return out
}
func chunkSubject(c ChunkVerb) string { return "mesh.seat." + c.Seat + ".chunk." + c.Verb + ".>" }
func ackSubject(c ChunkVerb) string { return "mesh.seat." + c.Seat + ".ack." + c.Verb + ".>" }
// chunkGrants are the chunk subjects p is granted: as the principal that answers a chunked verb, the publish of its chunks and
// the subscription to their acknowledgements (a module's own account for a seat it holds, the tool runner's for each seat a
// carried module holds; a process serves nothing, ADR 0436 §3), and as an account that names a chunked verb by name in `invokes`,
// the subscription to the chunks and the publish of the acknowledgements.
func chunkGrants(p Principal) (pub, sub []string) {
if len(p.Chunked) == 0 {
return nil, nil
}
var served []Seat
switch p.Kind {
case KindModule:
served = p.Holds
case KindNodeTools:
for _, d := range p.Carries {
served = append(served, d.Holds...)
}
}
for _, c := range p.Chunked {
for _, s := range served {
if s.Name == c.Seat && slices.Contains(s.Serves, c.Verb) {
pub, sub = append(pub, chunkSubject(c)), append(sub, ackSubject(c))
break
}
}
if slices.Contains(p.Invokes, "seat:"+c.Seat+"."+c.Verb) {
sub, pub = append(sub, chunkSubject(c)), append(pub, ackSubject(c))
}
}
return pub, sub
}
// restoreGrants are the keeper's standing grant of the bus's stream restore subjects and their chunk subjects, for the bus names
// of the streams ADR 0436 §3 restricts and only those (design 47 G §681, A-60): the restore request of each and the chunks the
// snapshot is sent to.
func restoreGrants(names []string) []string {
var out []string
for _, n := range names {
if !safeSubject.MatchString(n) || strings.Contains(n, ".") {
continue
}
out = append(out, "$JS.API.STREAM.RESTORE."+n, "$JS.SNAPSHOT.RESTORE."+n+".>")
}
return out
}
// restrictedStreamNames are the bus names of the streams every placed module declares restricted (ADR 0363 §4), sorted, once each.
func restrictedStreamNames(r Records) []string {
seen := map[string]bool{}
for _, mods := range r.Assigned {
for _, d := range mods {
for _, s := range d.Streams {
if s.Restricted {
seen[s.Name] = true
}
}
}
}
out := make([]string, 0, len(seen))
for n := range seen {
out = append(out, n)
}
sort.Strings(out)
return out
}