From 89e36241cb6cc4c4b667108c3b9080d039324e09 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 14 Nov 2026 01:33:48 +0100 Subject: [PATCH] A-386 kept-runner-gains-its-carried-modules-grants: a runner kept as it runs is the union of the list in force and a carrying runner's grants from the modules placed on its node Builds: design 47 G A-386 (G-22-lead-answers-2.md) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- modules/mesh-grants/grants_test.go | 76 +++++++++++++++++++ .../mesh-grants/internal/buscompose/users.go | 16 ++++ modules/mesh-grants/internal/grants/part.go | 1 + .../mesh-grants/internal/grants/refused.go | 29 +++++++ 4 files changed, 122 insertions(+) diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index c45d6b98..c5404958 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5521,3 +5521,79 @@ func TestARunnerCarryingABoundLedgerReadsItsStateHistoryAndSnapshots(t *testing. } } } + +// A-386: a runner kept as it runs (no manifest placed on its node says it is one) gains what its carried modules' bindings give, and loses nothing. +func TestARunnerKeptAsItRunsGainsWhatItsCarriedModulesBindingsGive(t *testing.T) { + const runner = `{"module":"node-tools","version":"1","tool-runner":true,"own-secrets":{"broker":"/var/lib/nt/broker"}}` + const carried = `{"module":"mod-c","version":"1","tools":["t"],"requires":[{"name":"records","storage":"ledger","template":"ledger",` + + `"types":[{"name":"decision","schema":{},"operations":[{"name":"propose","effect":"set-fields"}]}]}]}` + b := fixture() + b.manifestOf("node-tools", runner) + b.manifestOf("mod-c", carried) + b.assign("a", "node-tools") + b.assign("a", "mod-c") + busKey, err := seal.GenerateKeyPair() + if err != nil { + t.Fatal(err) + } + b.put(grants.ReadHave, "a.nats", nodeengine.HaveByNodeModuleValue{V: keys.Version, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK, + UserList: &nodeengine.UserListWritten{Source: grantsstate.GrantsUserListSource, Revision: 8, Wanted: "sha256:aa", Digest: "sha256:aa", Users: 5, + Key: busKey.PublicKey, Follows: grantsstate.FollowGrants}}) + for _, u := range shadowOf(t, b) { + b.put(grants.ReadAccounts, u.User, map[string]any{"user": u.User, "hash": "$2a$10$x", "generation": 1, "fingerprint": "sha256:1"}) + } + p := partOn(b) + p.Bus = grants.BusAddress{URL: "nats://a.mesh:4222", Fingerprint: "ab"} + p.Objects = &fakeObjects{} + binding := membership.BusBinding{Local: "records", Storage: "ledger", Template: "ledger", Stream: "mod-c_records", History: "mod-c_records", + Subject: "mesh.ledger.mod-c.records.history", State: "mod-c_records_state", Snapshot: "mesh.ledger.mod-c.records.snapshot"} + p.Compose() // the pass that learns the bus module is the broker + body, _ := json.Marshal(grants.BrokerEvent{V: 1, Module: "mod-c", Local: "records", Storage: "ledger", Template: "ledger", Binding: binding}) + if _, err := p.HearBroker("mesh.seat.mesh-broker.event.created", body, p.Brokers()); err != nil { + t.Fatal(err) + } + p.Compose() + first, _, _ := p.Sealed() + key := grants.SealedUserKey(first.Digest, "a.node-tools") + var was grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][key], &was); err != nil { + t.Fatal(err) + } + isLedger := func(s string) bool { return strings.Contains(s, "mod-c_records") } + if !slices.ContainsFunc(was.Publish, isLedger) || !slices.Contains(was.Publish, "mesh.control.a.tools-alive") { + t.Fatalf("the runner of a composes no ledger rights, or is no runner: %v", was.Publish) + } + // The list in force holds the runner without them (the pre-G runner's), and now no manifest placed on a says it is the tool + // runner: the pass keeps it as it runs (ADR 0601 §4), and the list changes. + was.Publish = slices.DeleteFunc(slices.Clone(was.Publish), isLedger) + b.own[grants.StateUserList][key], _ = json.Marshal(was) + b.manifestOf("node-tools", strings.Replace(runner, `"tool-runner":true,`, "", 1)) + b.assign("b", "mod-x") + p.Compose() + sealed, _, _ := p.Sealed() + if sealed.Digest == first.Digest { + t.Fatal("the list did not change") + } + var got grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][grants.SealedUserKey(sealed.Digest, "a.node-tools")], &got); err != nil { + t.Fatal(err) + } + for _, want := range []string{ + "$JS.API.STREAM.INFO.KV_mod-c_records_state", "$JS.API.DIRECT.GET.KV_mod-c_records_state.>", "$KV.mod-c_records_state.>", + "$JS.API.CONSUMER.CREATE.KV_mod-c_records_state.>", "$JS.API.DIRECT.GET.mod-c_records.>", "$JS.API.CONSUMER.CREATE.mod-c_records.>", + "$JS.API.DIRECT.GET.mod-c_records_snapshot.>"} { + if !slices.Contains(got.Publish, want) { + t.Errorf("the runner of a lacks %s", want) + } + } + for _, s := range was.Publish { + if !slices.Contains(got.Publish, s) { + t.Errorf("the kept runner lost %s", s) + } + } + for _, no := range []string{"mesh.ledger.mod-c.records.history.>", "mesh.ledger.mod-c.records.snapshot.>"} { + if slices.Contains(got.Publish, no) { + t.Errorf("the runner may publish on the ledger's %s", no) + } + } +} diff --git a/modules/mesh-grants/internal/buscompose/users.go b/modules/mesh-grants/internal/buscompose/users.go index 751c3022..8be05795 100644 --- a/modules/mesh-grants/internal/buscompose/users.go +++ b/modules/mesh-grants/internal/buscompose/users.go @@ -723,3 +723,19 @@ func newEventStreams(kept []KeptEvent) []string { } return out } + +// KeptRunnerGrants are the grants a tool runner that a pass keeps as it runs (no placed manifest says it is one, ADR 0601 §4) is composed +// as a carrying runner for: what the modules placed on its node that a runner carries, with their bindings and events, give it (design 47 +// G A-386), never as a plain module. runner is the module the kept user is named for. +func KeptRunnerGrants(r Records, node, runner string) (Permissions, error) { + logs := DeclaredLogs(r) + var carried []Declared + for _, d := range r.Assigned[node] { + if CarriedByRunner(d) { + d = withReadLogs(withAnnouncedReads(d, r.Announced), logs) + d.Hears = ComposedHears(d) + carried = append(carried, d) + } + } + return PermissionsFor(Principal{Kind: KindNodeTools, Node: node, Module: runner, Carries: carried, NewEventStreams: newEventStreams(r.Kept)}) +} diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 713ba3fa..752573a4 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -379,6 +379,7 @@ func (p *Part) Compose() Result { // alone (design 47 G A-385); a runner kept for what it runs, whose manifest does not say it is one, is never composed as a plain module // (ADR 0601 §4). kept = unionComposed(kept, users, runnersKept) + kept = unionKeptRunners(kept, listRecs, runnersKept) for _, user := range sortedKeys(refused) { res.PrincipalsRefused = append(res.PrincipalsRefused, user+": "+refused[user]) } diff --git a/modules/mesh-grants/internal/grants/refused.go b/modules/mesh-grants/internal/grants/refused.go index 5742165b..af4c4b4a 100644 --- a/modules/mesh-grants/internal/grants/refused.go +++ b/modules/mesh-grants/internal/grants/refused.go @@ -211,3 +211,32 @@ func unionComposed(kept map[string]User, composed []User, apart map[string]strin } return out } + +// unionKeptRunners is kept with each tool runner kept as it runs (runnersKept) holding the union of the list in force and the grants of a +// carrying runner on its node, composed from the modules the pass places there with their bindings and events (design 47 G A-386); a +// runner that cannot be composed so stays as the list in force holds it. It only adds. +func unionKeptRunners(kept map[string]User, r buscompose.Records, runnersKept map[string]string) map[string]User { + if len(kept) == 0 || len(runnersKept) == 0 { + return kept + } + out := make(map[string]User, len(kept)) + for name, u := range kept { + out[name] = u + } + for name := range runnersKept { + u, ok := out[name] + node, runner, cut := strings.Cut(name, ".") + if !ok || !cut { + continue + } + perms, err := buscompose.KeptRunnerGrants(r, node, runner) + if err != nil { + continue + } + u.Publish = set(append(slices.Clone(u.Publish), perms.Publish...)) + u.Subscribe = set(append(slices.Clone(u.Subscribe), perms.Subscribe...)) + u.AllowResponses = u.AllowResponses || perms.AllowResponses + out[name] = u + } + return out +}