mesh-grants: grants-for composes each revision's grants once

grants-for composed every module twice per ask (3 to 4 s against the
runner's 5 s bound, cut three times on 2026-11-22). The tools program
that serves it now keeps each kept revision's composed grants beside the
revision (the newest's and the candidate's), bounded by
grants-for-revisions-per-branch, so a second ask for a kept revision
composes nothing; it reads its inputs at each ask and journals each
stage's time on its standard error.

Builds: design 47 H2 H-A-478, H-A-478 addendum
This commit is contained in:
2026-11-22 16:01:39 +01:00
parent b691428ffc
commit 1f765ef17b
3 changed files with 132 additions and 24 deletions
+5 -2
View File
@@ -314,11 +314,14 @@ func recordHandActAs(cause string) func(what, why string) error {
func serveTools() error {
tv := &toolViews{}
gate := newSeatGate()
p := &grants.Part{Read: runtimeReader{views: tv}, Call: runtimeBus{}, Store: grants.BigValues(runtimeStore{}, nil), Emit: runtimeBus{}, SeatList: gate}
// Its journal is its standard error, which the tool runner keeps: grants-for says each stage's time there (H-A-478).
logf := func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-grants: "+f+"\n", a...) }
p := &grants.Part{Read: runtimeReader{views: tv}, Call: runtimeBus{}, Store: grants.BigValues(runtimeStore{}, nil), Emit: runtimeBus{}, SeatList: gate,
Logf: logf}
// The reader asks through the runtime, which serves once Serve has its channel.
go func() {
<-stdio.Served()
gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-grants: "+f+"\n", a...) })
gate.Follow(seatlist.StdioBus(), nil, logf)
}()
tools := append(toolsOf(p), gate.Tools()...)
for i := range tools {
+38
View File
@@ -5342,6 +5342,44 @@ func TestGrantsFor(t *testing.T) {
}
}
// grants-for composes each revision's grants once and keeps them with the revision (design 47 H2 H-A-478 and its addendum):
// a second ask for the same revisions composes nothing again; an ask of another candidate against the same newest composes
// the candidate alone; each stage's time is in the journal.
func TestGrantsForComposesARevisionOnce(t *testing.T) {
candidateX := strings.Replace(xManifest, `"state"`, `"emits":["hello"],"state"`, 1)
b := fixture()
b.revision = map[string]map[string]string{"newest": {"mod-x": xManifest, "mod-y": xManifest}, "candidate": {"mod-x": candidateX, "mod-y": xManifest}}
b.revisionNumber = map[string]int64{"newest": 9, "candidate": 8}
p := partOn(b)
var journal []string
p.Logf = func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) }
ask := func(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, string) {
t.Helper()
journal = nil
got, err := p.GrantsFor(in)
if err != nil || got.Error != "" {
t.Fatalf("grants-for: %+v, %v", got, err)
}
if len(journal) != 1 || !strings.HasPrefix(journal[0], "grants-for main ") || !strings.Contains(journal[0], "the inputs ") {
t.Fatalf("grants-for said %q, want one line of its stages' times", journal)
}
return got, journal[0]
}
first, said := ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
if len(first.Modules) != 1 || first.Modules[0].Module != "mod-x" || strings.Count(said, "(composed)") != 2 {
t.Errorf("the first ask answered %+v and said %q, want mod-x and both revisions' grants composed", first, said)
}
again, said := ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
if strings.Contains(said, "composed") || strings.Count(said, "(kept)") != 2 || !reflect.DeepEqual(first, again) {
t.Errorf("a second ask for the same revisions said %q and answered %+v, want both kept and %+v", said, again, first)
}
// A run's candidate is another revision: composed once, the newest's grants kept.
_, said = ask(meshgrants.GrantsForInput{Branch: "main", Run: "r1"})
if !strings.Contains(said, "the newest's grants (kept)") || !strings.Contains(said, "the candidate's grants (composed)") {
t.Errorf("an ask of another candidate said %q, want the newest's grants kept and the candidate's composed", said)
}
}
// grants-for keeps what H-A-195 says of the catalog: so many revisions to a branch as the setting
// grants-for-revisions-per-branch says, a run's candidate counted within its branch's; only the followed branches', freed when
// one leaves them; and an event below the newest held is the numbering started again, which drops what is kept.
@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshgrants"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
@@ -94,9 +95,12 @@ func (p *Part) following(branch string) bool {
// revision was taken in as, so grants-for asks the seat mesh-catalog's revision only for a revision it does not hold, never
// to learn the newest.
type revisionsKept struct {
mu sync.Mutex
order map[string][]string
kept map[string]map[string]catalogue.Manifest
mu sync.Mutex
order map[string][]string
kept map[string]map[string]catalogue.Manifest
// grants are the grants each kept revision composes to, by module (grantsOfEntries), composed once per revision and kept as
// long as the revision is (design 47 H2 H-A-478).
grants map[string]map[string]map[string]bool
newest map[string]int64
sources map[string]map[int64]int64
sourced map[string][]int64
@@ -113,6 +117,7 @@ func revisionKey(branch string, r meshcatalog.CatalogRevision) string {
func (k *revisionsKept) init() {
if k.kept == nil {
k.kept, k.order, k.newest = map[string]map[string]catalogue.Manifest{}, map[string][]string{}, map[string]int64{}
k.grants = map[string]map[string]map[string]bool{}
k.sources, k.sourced = map[string]map[int64]int64{}, map[string][]int64{}
}
}
@@ -124,6 +129,24 @@ func (k *revisionsKept) get(branch string, r meshcatalog.CatalogRevision) (map[s
return m, ok
}
// grantsOf is the grants kept of the revision key names; false when none is.
func (k *revisionsKept) grantsOf(key string) (map[string]map[string]bool, bool) {
k.mu.Lock()
defer k.mu.Unlock()
g, ok := k.grants[key]
return g, ok
}
// keepGrants keeps the grants the revision key names composes to, while that revision is kept: so the grants are bounded as
// the revisions are (H-A-195's setting).
func (k *revisionsKept) keepGrants(key string, g map[string]map[string]bool) {
k.mu.Lock()
defer k.mu.Unlock()
if _, ok := k.kept[key]; ok {
k.grants[key] = g
}
}
func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manifests map[string]catalogue.Manifest, max int) {
k.mu.Lock()
defer k.mu.Unlock()
@@ -135,6 +158,7 @@ func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manife
k.kept[key] = manifests
for len(k.order[branch]) > max {
delete(k.kept, k.order[branch][0])
delete(k.grants, k.order[branch][0])
k.order[branch] = k.order[branch][1:]
}
if r.Kind != "candidate" && r.Source != 0 {
@@ -167,6 +191,7 @@ func boolsOf[V any](m map[string]V) map[string]bool {
func (k *revisionsKept) dropLocked(branch string) {
for _, key := range k.order[branch] {
delete(k.kept, key)
delete(k.grants, key)
}
delete(k.order, branch)
delete(k.newest, branch)
@@ -250,14 +275,15 @@ func (p *Part) HearCatalogRevision(_ context.Context, data []byte) error {
// is answered from what it holds with no read; one it knows but does not hold is read by that number. Otherwise a revision
// read whole is kept: once the first page of an answer names a revision that is kept, it is answered from what is kept,
// without the pages after it. The branch's newest is read on by the number its first page names, so its pages are of one
// revision though a newer one is written between them; a revision whose pages name another is not kept.
func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[string]catalogue.Manifest, none bool, err error) {
// revision though a newer one is written between them; a revision whose pages name another is not kept. kept names the revision
// as it is kept (revisionKey), empty when it is not: what its grants are kept by (H-A-478).
func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[string]catalogue.Manifest, kept string, none bool, err error) {
branch := in.Branch
followed := p.following(branch)
newestAsked := in.Group == "" && in.Heads == "" && in.Source == 0 && in.Revision == 0
if n, ok := p.revisions.known(in); ok {
if m, held := p.revisions.get(branch, meshcatalog.CatalogRevision{Revision: n}); held {
return m, false, nil
return m, revisionKey(branch, meshcatalog.CatalogRevision{Revision: n}), false, nil
}
if newestAsked {
in.Revision = n
@@ -269,14 +295,14 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri
for {
raw, err := p.Call.Call(CallRevision, in)
if err != nil {
return nil, false, fmt.Errorf("%s does not answer: %w", CallRevision, err)
return nil, "", false, fmt.Errorf("%s does not answer: %w", CallRevision, err)
}
var a meshcatalog.RevisionAnswer
if err := json.Unmarshal(raw, &a); err != nil {
return nil, false, fmt.Errorf("%s answered what is not a revision: %w", CallRevision, err)
return nil, "", false, fmt.Errorf("%s answered what is not a revision: %w", CallRevision, err)
}
if a.NoneYet {
return nil, true, nil
return nil, "", true, nil
}
switch {
case a.Revision == nil:
@@ -286,7 +312,7 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri
p.revisions.asked(branch, a.Revision.Revision)
}
if m, ok := p.revisions.get(branch, *a.Revision); ok {
return m, false, nil
return m, revisionKey(branch, *a.Revision), false, nil
}
first = a.Revision
if newestAsked {
@@ -302,15 +328,16 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri
}
m, err := catalogue.Decode(body)
if err != nil {
return nil, false, fmt.Errorf("the manifest of %s in the revision does not decode: %w", e.Module, err)
return nil, "", false, fmt.Errorf("the manifest of %s in the revision does not decode: %w", e.Module, err)
}
manifests[e.Module] = m
}
if a.Next == "" || a.Next == in.After {
if keep && first != nil && followed {
p.revisions.put(branch, *first, manifests, p.revisionsPerBranch())
kept = revisionKey(branch, *first)
}
return manifests, false, nil
return manifests, kept, false, nil
}
in.After = a.Next
}
@@ -389,26 +416,40 @@ func (p *Part) GrantsFor(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, er
default:
return meshgrants.GrantsFor{}, errors.New("grants-for names a source revision or a run")
}
cand, none, err := p.revisionEntries(candidate)
// Each stage's time in the journal (H-A-478): the inputs, each revision's read and each one's grants, composed or kept.
var stages []string
stage := func(what string, start time.Time) {
stages = append(stages, fmt.Sprintf("%s %s", what, time.Since(start).Round(time.Millisecond)))
}
defer func() {
if p.Logf != nil {
p.Logf("grants-for %s: %s", candidateName(candidate), strings.Join(stages, ", "))
}
}()
start := time.Now()
cand, candKept, none, err := p.revisionEntries(candidate)
stage("the candidate read", start)
if err != nil || none {
return meshgrants.GrantsFor{Error: orWhy(err, "the candidate's catalog half is not held: the branch has no revision of it")}, nil
}
newest, none, err := p.revisionEntries(meshcatalog.RevisionInput{Branch: in.Branch})
start = time.Now()
newest, newestKept, none, err := p.revisionEntries(meshcatalog.RevisionInput{Branch: in.Branch})
stage("the newest read", start)
if err != nil || none {
return meshgrants.GrantsFor{Error: orWhy(err, "the branch's newest catalog half is not held")}, nil
}
base := map[string]catalogue.Manifest{}
for module := range cand {
if m, ok := newest[module]; ok {
base[module] = m
}
}
start = time.Now()
input := p.inputs()
was, err := grantsOfEntries(input, base)
stage("the inputs", start)
start = time.Now()
was, composed, err := p.grantsOfRevision(input, newestKept, newest)
stage("the newest's grants ("+composed+")", start)
if err != nil {
return meshgrants.GrantsFor{Error: "the branch's newest does not compose: " + err.Error()}, nil
}
will, err := grantsOfEntries(input, cand)
start = time.Now()
will, composed, err := p.grantsOfRevision(input, candKept, cand)
stage("the candidate's grants ("+composed+")", start)
if err != nil {
return meshgrants.GrantsFor{Error: "the candidate does not compose: " + err.Error()}, nil
}
@@ -435,6 +476,32 @@ func (p *Part) GrantsFor(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, er
return out, nil
}
// candidateName names the candidate an ask is of, for the journal.
func candidateName(c meshcatalog.RevisionInput) string {
if c.Group != "" {
return c.Branch + " " + c.Group
}
return c.Branch + " source " + strconv.FormatInt(c.Source, 10)
}
// grantsOfRevision is the grants a revision's manifests compose to with in, by module: those kept of the revision kept names
// when it has them, else composed and kept with it (H-A-478: once per revision). how says which.
func (p *Part) grantsOfRevision(in Inputs, kept string, manifests map[string]catalogue.Manifest) (grants map[string]map[string]bool, how string, err error) {
if kept != "" {
if g, ok := p.revisions.grantsOf(kept); ok {
return g, "kept", nil
}
}
g, err := grantsOfEntries(in, manifests)
if err != nil {
return nil, "composed", err
}
if kept != "" {
p.revisions.keepGrants(kept, g)
}
return g, "composed", nil
}
func orWhy(err error, why string) string {
if err != nil {
return err.Error()