diff --git a/node-tools/internal/bus/bindings.go b/node-tools/internal/bus/bindings.go index 85ead67..4a13023 100644 --- a/node-tools/internal/bus/bindings.go +++ b/node-tools/internal/bus/bindings.go @@ -62,8 +62,28 @@ func (c *Conn) Bindings(module string) []membership.BusBinding { // A-16, A-27). func (c *Conn) JetStream() (jetstream.JetStream, error) { return jetstream.New(c.nc) } -func (c *Conn) objectStore(ctx context.Context, module, local string) (jetstream.ObjectStore, membership.BusBinding, error) { +// objectBucketOf is the bus name of the module's object store of a local name: its binding's when it has one, else, for a +// name no binding of any type holds, the store grants creates for a module's old `objects` entry, `_` +// (catalogue.ObjectsBucket; design 47 G lead answers A-380): the bus's rights decide whether it is reached. A name bound +// with another storage type is refused as the binding refuses it. +func (c *Conn) objectBucketOf(module, local string) (membership.BusBinding, error) { b, err := c.Binding(module, local, catalogue.StorageObjectStore) + if err == nil { + return b, nil + } + if m := c.Membership(module); m != nil { + for _, held := range m.Bindings { + if held.Local == local { + return b, err + } + } + return membership.BusBinding{Local: local, Storage: catalogue.StorageObjectStore, ObjectStore: catalogue.ObjectsBucket(module, local)}, nil + } + return b, err +} + +func (c *Conn) objectStore(ctx context.Context, module, local string) (jetstream.ObjectStore, membership.BusBinding, error) { + b, err := c.objectBucketOf(module, local) if err != nil { return nil, b, err } @@ -73,6 +93,10 @@ func (c *Conn) objectStore(ctx context.Context, module, local string) (jetstream } obs, err := js.ObjectStore(ctx, b.ObjectStore) if errors.Is(err, jetstream.ErrBucketNotFound) || errors.Is(err, jetstream.ErrStreamNotFound) { + if b.Template == "" { + return nil, b, fmt.Errorf("%s was bound no object-store called %q, and the store %s grants creates for an old `objects` entry of that name is not on the bus", + module, local, b.ObjectStore) + } return nil, b, fmt.Errorf("%s's objects %q are bound and their store %s is not on the bus", module, local, b.ObjectStore) } return obs, b, err @@ -178,7 +202,7 @@ func (c *Conn) ObjectsInfo(module string, r buscalls.ObjectsRequest) (buscalls.F if err != nil { return buscalls.Fill{}, err } - b, _ := c.Binding(module, r.Objects, catalogue.StorageObjectStore) + b, _ := c.objectBucketOf(module, r.Objects) fill, err := c.streamFill("OBJ_" + b.ObjectStore) if err != nil { return buscalls.Fill{}, err diff --git a/node-tools/internal/bus/bindings_test.go b/node-tools/internal/bus/bindings_test.go index 8e163d6..6521fb5 100644 --- a/node-tools/internal/bus/bindings_test.go +++ b/node-tools/internal/bus/bindings_test.go @@ -33,6 +33,9 @@ func TestABundleReachesItsOwnBoundObjectsAndStreamsOnly(t *testing.T) { if _, err := js.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: "m_files"}); err != nil { t.Fatal(err) } + if _, err := js.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: "m_old"}); err != nil { + t.Fatal(err) + } if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "m_events", Subjects: []string{"mesh.state.m.events.>"}}); err != nil { t.Fatal(err) } @@ -92,6 +95,13 @@ func TestABundleReachesItsOwnBoundObjectsAndStreamsOnly(t *testing.T) { if listed, err := conn.ObjectsList("m", buscalls.ObjectsRequest{Objects: "files"}); err != nil || len(listed.Objects) != 1 { t.Fatalf("listed %+v, %v", listed, err) } + // A-380: an old `objects` entry is reached by the store grants creates for it, `_`, with no binding of that name. + if _, err := conn.ObjectsPut("m", buscalls.ObjectsPutRequest{Objects: "old", Name: "r", Data: []byte("old section")}); err != nil { + t.Fatalf("put into the old objects section: %v", err) + } + if listed, err := conn.ObjectsList("m", buscalls.ObjectsRequest{Objects: "old"}); err != nil || len(listed.Objects) != 1 { + t.Fatalf("listed the old objects section %+v, %v", listed, err) + } page, err := conn.StreamRead("m", buscalls.StreamReadRequest{Stream: "events", Limit: 1}) if err != nil || len(page.Messages) != 1 || page.Next != 2 || page.Last != 2 { t.Fatalf("read %+v, %v", page, err) @@ -121,7 +131,7 @@ func TestABundleReachesItsOwnBoundObjectsAndStreamsOnly(t *testing.T) { name, module, refusal string call func(module string) error }{ - {"a local name not bound", "m", `bound no object-store called "photos"`, func(module string) error { + {"a local name not bound", "m", `bound no object-store called "photos", and the store m_photos grants creates`, func(module string) error { _, err := conn.ObjectsGet(module, buscalls.ObjectsNameRequest{Objects: "photos", Name: "a"}) return err }}, diff --git a/node-tools/internal/bus/bus.go b/node-tools/internal/bus/bus.go index b0c0c04..8e843af 100644 --- a/node-tools/internal/bus/bus.go +++ b/node-tools/internal/bus/bus.go @@ -682,6 +682,12 @@ func (c *Conn) HandleSubjectCalled(subject string, h CalledHandler) (func(), err return c.answerBoth(subject, "", h) } +// HandleSubjectCalledQueue is HandleSubjectCalled in one queue group: of every holder that serves the subject in the +// group, one answers (lead answer A-393: a node-scoped seat's verb asked with no machine). +func (c *Conn) HandleSubjectCalledQueue(subject, queue string, h CalledHandler) (func(), error) { + return c.answerBoth(subject, queue, h) +} + // answerBoth answers a subject and, when it is a tool's, the caller-named pattern beside it, in one queue. func (c *Conn) answerBoth(subject, queue string, h CalledHandler) (func(), error) { stop, err := c.answerCalledOn(subject, queue, h) @@ -1069,6 +1075,13 @@ func ConsumerOf(node, module string) string { return node + "_" + module } +// ToolsSuffix ends the name of the tool runner's own durable consumer of a module: the runner and the module's program each +// need every event, so each reads under a name of its own (lead answer A-397); only a work queue shares one name. +const ToolsSuffix = "_tools" + +// ToolsConsumerOf is the tool runner's durable consumer of a module on a machine: `__tools`. +func ToolsConsumerOf(node, module string) string { return ConsumerOf(node, module) + ToolsSuffix } + // NakDelay is how long an event a handler failed waits before it is offered again: a transient cause // gets another attempt, a permanent one exhausts the consumer's max-deliver rather than spinning. The // consumer then gives the event up, and the mesh keeps it in DEAD_LETTERS until a person delivers @@ -1119,7 +1132,7 @@ var tickHook func() // answers true (nil is always): a module whose children asked for no heartbeat costs the bus no consumer-info request. func (c *Conn) ConsumeAsBeatingWhen(module string, deliver func(Envelope) error, beat func(Beat), wanted func() bool) (func(), error) { asked := func() bool { return beat != nil && (wanted == nil || wanted()) } - durable := ConsumerOf(c.node, module) + durable := ToolsConsumerOf(c.node, module) js, err := jetstream.New(c.nc) if err != nil { return nil, err diff --git a/node-tools/internal/bus/state.go b/node-tools/internal/bus/state.go index ed6c196..1e482f9 100644 --- a/node-tools/internal/bus/state.go +++ b/node-tools/internal/bus/state.go @@ -5,7 +5,9 @@ import ( "encoding/json" "errors" "fmt" + "maps" "regexp" + "slices" "sort" "strings" "sync" @@ -174,64 +176,50 @@ func (c *Conn) StatePage(module, name, after string, maxBytes int, budget time.D each := func() (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), StateTimeout) } - var keys []string - var get func(key string) (*StateEntry, error) + // Every current value is read in one stream per bucket (currentOf), never one bus request per key: a page of the + // assignments (about 330 keys) read key by key took 13 s from a node far from the bus (2026-11-14). + current := map[string]*StateEntry{} if s.PerNode { fam := c.family(s) - if keys, err = fam.Keys(ctx); err != nil { + keys, err := fam.Keys(ctx) + if err != nil { return StatePage{}, err } - buckets := map[string]jetstream.KeyValue{} // each node's bucket bound once - get = func(key string) (*StateEntry, error) { - node, err := pernode.NodeOf(key) - if err != nil { - return nil, err + nodes := map[string]bool{} + for _, k := range keys { + if node, err := pernode.NodeOf(k); err == nil { + nodes[node] = true } - kv, bound := buckets[node] - if !bound { - octx, ocancel := each() // the open is a bus request in the loop: its own deadline, never ctx's - kv, err = fam.Open(octx, node) - ocancel() - if err != nil { - return nil, err - } - buckets[node] = kv + } + for node := range nodes { + octx, ocancel := each() // each node's bucket is a bus request of its own: its own deadline + kv, err := fam.Open(octx, node) + ocancel() + if err != nil { + return StatePage{}, err } if kv == nil { - return nil, nil - } - gctx, gcancel := each() - defer gcancel() - e, err := kv.Get(gctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil + continue } + wctx, wcancel := each() + got, err := currentOf(wctx, kv) + wcancel() if err != nil { - return nil, err + return StatePage{}, err } - return &StateEntry{Key: e.Key(), Value: valueOf(e.Value()), Revision: e.Revision(), At: e.Created().UTC()}, nil + maps.Copy(current, got) } } else { kv, err := c.bucket(ctx, s) if err != nil { return StatePage{}, err } - if keys, err = keysOf(ctx, kv); err != nil { + if current, err = currentOf(ctx, kv); err != nil { return StatePage{}, err } - get = func(key string) (*StateEntry, error) { - gctx, gcancel := each() - defer gcancel() - e, err := kv.Get(gctx, key) - if errors.Is(err, jetstream.ErrKeyNotFound) { - return nil, nil - } - if err != nil { - return nil, err - } - return &StateEntry{Key: e.Key(), Value: valueOf(e.Value()), Revision: e.Revision(), At: e.Created().UTC()}, nil - } } + keys := slices.Collect(maps.Keys(current)) + get := func(key string) (*StateEntry, error) { return current[key], nil } sort.Strings(keys) page := StatePage{Entries: []StateEntry{}} started := PageClock() @@ -719,3 +707,28 @@ func providerReference(v any) bool { // referenceName is a node's or a module's name as mesh-sdk names them: lower case, digits and dashes. var referenceName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`) + +// currentOf is every current value of a bucket, read in one stream (a watch of every key, deletes left out, ended at its +// first nil): the values a page answers, without a bus request per key. +func currentOf(ctx context.Context, kv jetstream.KeyValue) (map[string]*StateEntry, error) { + w, err := kv.WatchAll(ctx, jetstream.IgnoreDeletes()) + if err != nil { + return nil, err + } + defer func() { _ = w.Stop() }() + out := map[string]*StateEntry{} + for { + select { + case <-ctx.Done(): + return nil, fmt.Errorf("the values of %s are not all read: %w", kv.Bucket(), ctx.Err()) + case e, ok := <-w.Updates(): + if !ok { + return out, nil + } + if e == nil { + return out, nil + } + out[e.Key()] = &StateEntry{Key: e.Key(), Value: valueOf(e.Value()), Revision: e.Revision(), At: e.Created().UTC()} + } + } +} diff --git a/node-tools/internal/bus/streams.go b/node-tools/internal/bus/streams.go index 33e25e8..4f7b09e 100644 --- a/node-tools/internal/bus/streams.go +++ b/node-tools/internal/bus/streams.go @@ -269,6 +269,8 @@ func (h *hearing) reconcile() { if m := h.c.Membership(h.module); m != nil { for _, heard := range m.Hears { if heard.Stream != "" && heard.Consumer != "" { + // The runner's own durable beside the program's (A-397). + heard.Consumer += ToolsSuffix want[heard] = true } } diff --git a/node-tools/internal/firstview/firstview.go b/node-tools/internal/firstview/firstview.go index 604c93c..5fa5ef7 100644 --- a/node-tools/internal/firstview/firstview.go +++ b/node-tools/internal/firstview/firstview.go @@ -58,18 +58,19 @@ func (s Spec) Name() string { return AssignerSeat + "." + s.State } // Views is the first views: the assigner's two, and the catalog half's per watched branch. type Views struct { - logf func(string, ...any) - mu sync.Mutex - followers map[string]*views.Follower // by `.` - byState map[string]*views.Follower // by the state an event names, the owners' views families - adopted map[string]*adoption // by `.`, the views the branch follower adopted - hearers map[string][]*adoption // event key `.` -> the adopted views that hear it - branches *meshcatalogclient.Branches - holder *holding // the module that claims the assigner seat, once the catalog half names it - ctx context.Context - cancel context.CancelFunc - wg sync.WaitGroup - deps Deps + logf func(string, ...any) + mu sync.Mutex + followers map[string]*views.Follower // by `.` + byState map[string]*views.Follower // by the state an event names, the owners' views families + adopted map[string]*adoption // by `.`, the views the branch follower adopted + unheardKeys map[string]bool // event keys said as heard by no view + hearers map[string][]*adoption // event key `.` -> the adopted views that hear it + branches *meshcatalogclient.Branches + holder *holding // the module that claims the assigner seat, once the catalog half names it + ctx context.Context + cancel context.CancelFunc + wg sync.WaitGroup + deps Deps // heard is closed and made again at every event heard, so a wait for the picture wakes on events, never on a timer. heard chan struct{} // onKey are the hearers of one key of an owner's state, by ` ` (OnSeatHolder). @@ -207,7 +208,7 @@ func StartWith(parent context.Context, d Deps, logf func(string, ...any)) (*View func (v *Views) hear(env bus.Envelope) error { defer v.heardOne() v.mu.Lock() - hs := append([]*adoption(nil), v.hearers[env.Key]...) + hs := v.hearersOf(env.Key) v.mu.Unlock() if len(hs) > 0 { // an event of the catalog half or the gate's source, heard by the views the branch follower adopted for _, a := range hs { @@ -219,6 +220,7 @@ func (v *Views) hear(env bus.Envelope) error { } ev, err := views.DecodeEvent(env.Body) if err != nil { + v.unheard(env.Key) return nil // not a views event: nothing to hold, nothing to retry } v.mu.Lock() @@ -235,6 +237,37 @@ func (v *Views) hear(env bus.Envelope) error { return nil } +// hearersOf are the adopted views that hear the event key `.` or, as its subject carries further tokens +// (`.`, the mesh-catalog seat's catalog-revision), the key's shorter prefixes: the way mesh-sdk's views reader +// finds its adopted hears. The caller holds v.mu. +func (v *Views) hearersOf(key string) []*adoption { + for k := key; k != ""; { + if hs := v.hearers[k]; len(hs) > 0 { + return append([]*adoption(nil), hs...) + } + i := strings.LastIndex(k, ".") + if i <= 0 { + break + } + k = k[:i] + } + return nil +} + +// unheard says once per key an event no adopted view and no owner's state hears, never dropping it silently. +func (v *Views) unheard(key string) { + v.mu.Lock() + if v.unheardKeys == nil { + v.unheardKeys = map[string]bool{} + } + seen := v.unheardKeys[key] + v.unheardKeys[key] = true + v.mu.Unlock() + if !seen { + v.logf("[mesh-tools] an event %s is heard by no view of this tool runner: it is dropped (said once per key)", key) + } +} + // finding writes or clears the runner's finding of a first view unanswered past its bound, through its writer (design 47 G A-28). func (v *Views) finding(f condseat.Finding, tool string, raised bool) { if v.deps.Findings == nil { diff --git a/node-tools/internal/firstview/firstview_test.go b/node-tools/internal/firstview/firstview_test.go index 5221d6e..74c2837 100644 --- a/node-tools/internal/firstview/firstview_test.go +++ b/node-tools/internal/firstview/firstview_test.go @@ -156,10 +156,14 @@ func TestTheRunnerHoldsTheFourStatesFromTheOwnersFirstViewsAndTheirEvents(t *tes o.catalog, o.modules = 4, []string{"web", "db", fixtureHolder} o.mu.Unlock() ev, _ := json.Marshal(catalogseat.CatalogRevisionEvent{V: 1, Branch: "main", Kind: meshcatalogclient.KindTrunk, Revision: 4}) - if err := o.deliver(bus.Envelope{Key: catalogseat.Seat + "." + catalogseat.EventCatalogRevision, Body: ev}); err != nil { + if err := o.deliver(bus.Envelope{Key: catalogseat.Seat + "." + catalogseat.EventCatalogRevision + ".main", Body: ev}); err != nil { t.Fatal(err) } eventually(t, "the catalog-revision was not taken", func() bool { m, err := v.Modules(); return err == nil && len(m) == 3 }) + // An event no view hears is said once per key, not dropped silently. + for i := 0; i < 2; i++ { + _ = o.deliver(bus.Envelope{Key: "some-seat.unknown-event.x", Body: json.RawMessage(`"x"`)}) + } // And the halves by branch, with their revision, which mesh_wait names checking an emitter (design 34 every-event §14). if h, err := v.Halves(); err != nil || h["main"].Revision.Revision != 4 || len(h["main"].Entries) != 3 { t.Errorf("the halves: %+v, %v", h, err) @@ -207,6 +211,9 @@ func TestTheRunnerHoldsTheFourStatesFromTheOwnersFirstViewsAndTheirEvents(t *tes } lmu.Lock() defer lmu.Unlock() + if n := strings.Count(strings.Join(logs, "\n"), "heard by no view"); n != 1 { + t.Errorf("an unheard event was said %d times, want once: %v", n, logs) + } if !strings.Contains(strings.Join(logs, "\n"), "a gap in") { t.Errorf("the gap was not said in the log: %v", logs) } diff --git a/node-tools/internal/meshtest/meshtest.go b/node-tools/internal/meshtest/meshtest.go index 3fc053e..04fca72 100644 --- a/node-tools/internal/meshtest/meshtest.go +++ b/node-tools/internal/meshtest/meshtest.go @@ -303,7 +303,7 @@ func (l *Logs) All() string { return strings.Join(l.lines, "\n") } // redelivery in seconds rather than the mesh's minutes. func (m *Mesh) Consumer(t *testing.T, node, module string, filters []string, ackWait time.Duration) { t.Helper() - cfg := &nats.ConsumerConfig{Durable: node + "_" + module, AckPolicy: nats.AckExplicitPolicy, + cfg := &nats.ConsumerConfig{Durable: node + "_" + module + "_tools", AckPolicy: nats.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 10, DeliverPolicy: nats.DeliverNewPolicy} if len(filters) == 1 { cfg.FilterSubject = filters[0] @@ -340,7 +340,7 @@ func (m *Mesh) Publish(t *testing.T, subject string, body any) { // delivered. func (m *Mesh) Pending(t *testing.T, node, module string) (ackPending, notDelivered uint64) { t.Helper() - info, err := m.js.ConsumerInfo("EVENTS", node+"_"+module) + info, err := m.js.ConsumerInfo("EVENTS", node+"_"+module+"_tools") if err != nil { t.Fatal(err) } @@ -430,7 +430,7 @@ func (m *Mesh) ModuleStream(t *testing.T, module, name string) { // ConsumerOn makes a module's durable consumer on a machine on any stream, as Consumer does on EVENTS. func (m *Mesh) ConsumerOn(t *testing.T, stream, node, module string, filters []string, ackWait time.Duration) { t.Helper() - cfg := &nats.ConsumerConfig{Durable: node + "_" + module, AckPolicy: nats.AckExplicitPolicy, + cfg := &nats.ConsumerConfig{Durable: node + "_" + module + "_tools", AckPolicy: nats.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 10, DeliverPolicy: nats.DeliverNewPolicy} if len(filters) == 1 { cfg.FilterSubject = filters[0] @@ -445,7 +445,7 @@ func (m *Mesh) ConsumerOn(t *testing.T, stream, node, module string, filters []s // PendingOn is what a module's consumer on a stream still holds, as Pending is on EVENTS. func (m *Mesh) PendingOn(t *testing.T, stream, node, module string) (ackPending, notDelivered uint64) { t.Helper() - info, err := m.js.ConsumerInfo(stream, node+"_"+module) + info, err := m.js.ConsumerInfo(stream, node+"_"+module+"_tools") if err != nil { t.Fatal(err) } @@ -466,7 +466,7 @@ func (m *Mesh) Messages(t *testing.T, stream string) uint64 { // it cannot narrow is withdrawn (novox/hq ADR 0363 §6). func (m *Mesh) RemoveConsumer(t *testing.T, stream, node, module string) { t.Helper() - if err := m.js.DeleteConsumer(stream, node+"_"+module); err != nil { + if err := m.js.DeleteConsumer(stream, node+"_"+module+"_tools"); err != nil { t.Fatal(err) } } diff --git a/node-tools/internal/runtime/busstate.go b/node-tools/internal/runtime/busstate.go index 567c3de..7cbc110 100644 --- a/node-tools/internal/runtime/busstate.go +++ b/node-tools/internal/runtime/busstate.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "sort" + "time" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/buscalls" @@ -177,15 +178,18 @@ func (c *consumers) objectsGet(module string, r buscalls.ObjectsNameRequest) (an if st == nil { return nil, fmt.Errorf("the runner has no bus to read %s of %s from", r.Name, module) } - ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout) - defer cancel() + ctx, cancel, opened := openingContext() info, rd, found, err := st.ObjectsOpen(ctx, module, r) + opened() if err != nil || !found { + cancel() return nil, err } if info.Size > buscalls.ChunkBytes { - return c.openGet(module, info, rd, sessionDigest(info.Digest), false) + // the reader reads on ctx after this call returns: it ends with the session, not with this call + return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(info.Digest), false) } + defer cancel() defer rd.Close() data, err := io.ReadAll(rd) if err != nil { @@ -208,11 +212,32 @@ func (c *consumers) sentGet(module string, r buscalls.ObjectsSentGetRequest) (an if st == nil { return nil, fmt.Errorf("the runner has no bus to read %s from", r.Object) } - ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout) - defer cancel() + ctx, cancel, opened := openingContext() info, rd, err := st.SentOpen(ctx, r) + opened() if err != nil { + cancel() return nil, err } - return c.openGet(module, info, rd, sessionDigest(r.Digest), true) + return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(r.Digest), true) +} + +// openingContext is the context an object is opened on: it is bound at bus.StateTimeout while opening, and once opened (the +// returned function) is bound no more: a reader of an object that outlives the call reads on it until its session ends. +func openingContext() (context.Context, context.CancelFunc, func()) { + ctx, cancel := context.WithCancel(context.Background()) + t := time.AfterFunc(bus.StateTimeout, cancel) + return ctx, cancel, func() { t.Stop() } +} + +// cancelOnClose ends the context an object's reader reads on when the reader is closed: at the end of its get session. +type cancelOnClose struct { + io.ReadCloser + cancel context.CancelFunc +} + +func (c cancelOnClose) Close() error { + err := c.ReadCloser.Close() + c.cancel() + return err } diff --git a/node-tools/internal/runtime/caller_test.go b/node-tools/internal/runtime/caller_test.go index d44a0d2..611e7fe 100644 --- a/node-tools/internal/runtime/caller_test.go +++ b/node-tools/internal/runtime/caller_test.go @@ -102,9 +102,10 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { } return err }) + // Every caller is admitted, an unproven one too (the operator, 2026-11-15 23:25 CET). if strings.HasPrefix(subject, "mesh.seat.") { - if !strings.Contains(reply.Error, "node-desk.who takes no unproven caller") || reply.Result.Call != "" { - t.Errorf("%s: an unproven caller of a seat's verb was answered %+v (error %q), want refused before the bundle", subject, reply.Result, reply.Error) + if reply.Error != "" { + t.Errorf("%s: an unproven caller of a seat's verb was refused: %q, want answered", subject, reply.Error) } continue } @@ -116,9 +117,9 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { defs map[string]seatdef.Definition says string }{ - {nil, "is not yet read"}, - {map[string]seatdef.Definition{"node-desk": {Seat: "node-desk"}}, "names no verb who"}, - {map[string]seatdef.Definition{"node-desk": {Seat: "node-desk", Verbs: []seatdef.Verb{{Name: "who", Callers: &seatdef.Callers{List: []string{}}}}}}, "open to no caller"}, + {nil, ""}, + {map[string]seatdef.Definition{"node-desk": {Seat: "node-desk"}}, ""}, + {map[string]seatdef.Definition{"node-desk": {Seat: "node-desk", Verbs: []seatdef.Verb{{Name: "who", Callers: &seatdef.Callers{List: []string{}}}}}}, ""}, {map[string]seatdef.Definition{"node-desk": {Seat: "node-desk", Verbs: []seatdef.Verb{{Name: "who", Class: seatdef.ClassAct}}}}, ""}, } { defs := c.defs @@ -134,12 +135,12 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { return map[string]seatdef.Definition{"mesh-catalog": {Verbs: []seatdef.Verb{{Name: "input", Chunks: true}, {Name: "entry"}}}} }} for key, want := range map[string]bool{ - "seat:mesh-catalog.input": true, - "seat:mesh-catalog.entry": false, - "seat:mesh-catalog.input@home-server": true, - "seat:mesh-catalog.entry@home-server": false, - "seat:mesh-unread.input": false, - "mesh-delivery-indexer.indexer": false, + "seat:mesh-catalog.input": true, + "seat:mesh-catalog.entry": false, + "seat:mesh-catalog.input@home-server": true, + "seat:mesh-catalog.entry@home-server": false, + "seat:mesh-unread.input": false, + "mesh-delivery-indexer.indexer": false, } { if got := c.chunked(key); got != want { t.Errorf("chunked(%s) = %v, want %v", key, got, want) diff --git a/node-tools/internal/runtime/gate.go b/node-tools/internal/runtime/gate.go index a29d84d..70e2feb 100644 --- a/node-tools/internal/runtime/gate.go +++ b/node-tools/internal/runtime/gate.go @@ -1,7 +1,6 @@ package runtime import ( - "fmt" "strings" "git.novox.be/novox/mesh-sdk/go/approval" @@ -55,32 +54,10 @@ func WithSeatDefinitions(defs func() map[string]seatdef.Definition) Option { return func(c *consumers) { c.definitions = defs } } -// admitted says why a seat's verb is refused to a caller, nil when it is answered: an unproven caller is refused, and so is -// every caller while the seat's definition is not read, names no such verb, or the verb's effective callers are empty; a -// proven caller is among them, since the grants part composes a verb's call subject only for its effective callers (A-179). -func (c *consumers) admitted(seat, verb, caller string) error { - if c.definitions == nil { - return nil - } - if caller == "" { - return fmt.Errorf("Not run: %s.%s takes no unproven caller: call it on its subject that names its caller, "+ - "mesh.seat.%s.call.%s.", seat, verb, seat, verb) - } - def, ok := c.definitions()[seat] - if !ok { - return fmt.Errorf("Not run: the definition of the seat %s is not yet read by this tool runner's seat list, so no caller "+ - "of %s.%s is admitted", seat, seat, verb) - } - for _, v := range def.Verbs { - if v.Name == verb { - if len(v.EffectiveCallers()) == 0 { - return fmt.Errorf("Not run: %s.%s is open to no caller", seat, verb) - } - return nil - } - } - return fmt.Errorf("Not run: the seat %s's definition names no verb %s, so no caller is admitted", seat, verb) -} +// admitted says why a seat's verb is refused to a caller: never. Every caller is admitted, whether or not the seat's definition +// is read and whatever callers it names (the operator, 2026-11-15 23:25 CET: every module, agent and operator may call anything; +// approval stays as the mark on a verb, held by the gate). +func (c *consumers) admitted(seat, verb, caller string) error { return nil } // unservedMarks is, of the module's marked tools, those its bundle's registrations under its own name do not register. func (r *runner) unservedMarks(module string, regs []launch.Registration) []string { diff --git a/node-tools/internal/runtime/gate_env_test.go b/node-tools/internal/runtime/gate_env_test.go new file mode 100644 index 0000000..abf3bc9 --- /dev/null +++ b/node-tools/internal/runtime/gate_env_test.go @@ -0,0 +1,43 @@ +package runtime + +import ( + "testing" + + mt "github.com/novox/mesh-tools/node-tools/internal/meshtest" +) + +// Design 47 G lead answers A-377: the call a module's tool reaches the gate by carries the module's composed +// environment, so the gate tells a first-view tool (the one whose MESH_VIEW_CALLERS_ the bundle holds) from an +// ordinary one while its catalog half is unread. +func TestAModuleToolsCallCarriesItsBundlesComposedEnvironment(t *testing.T) { + for name, tc := range map[string]struct { + envs map[string]map[string]string + want bool + }{ + "a first-view tool": {map[string]map[string]string{"ledger": {"MESH_VIEW_CALLERS_WHO": "grants"}}, true}, + "an ordinary tool": {nil, false}, + } { + t.Run(name, func(t *testing.T) { + mesh := mt.New(t) + mesh.Issue(t, mt.MembershipOf("ledger", "anchor", true, map[string][]string{"node-desk": {"who"}})) + nodeTools := connect(t, "node-tools", "anchor") + gate := &gateHolding{} + stop, err := Run(nodeTools, []Served{{"ledger", []string{mt.Fixture("caller-echo.py")}}}, tc.envs, func(string, ...any) {}, deskDefined, WithGate(gate)) + if err != nil { + t.Fatal(err) + } + defer stop() + asker := connect(t, "node-tools", "anchor") + mt.Until(t, func() error { _, err := asker.Ask("ledger.who@anchor", map[string]any{}, ""); return err }) + var found bool + for _, c := range gate.calls { + if c.Seat == "" && c.Name == "who" { + _, found = c.Env["MESH_VIEW_CALLERS_WHO"] + } + } + if found != tc.want { + t.Errorf("the gate saw the first-view word: %v, want %v (calls %+v)", found, tc.want, gate.calls) + } + }) + } +} diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index b29e08e..f248a02 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -241,6 +241,9 @@ type runner struct { // §424, §618), never the runner's own. fingerprintOf func(module string) string + // envOf is a module's composed environment, the words the gate tells a first-view tool by (lead answers A-377). + envOf func(module string) map[string]string + seatsAgain func() stops []func() // the watch, the announcement and the seats, in the order they began } @@ -275,6 +278,7 @@ func Run(conn *bus.Conn, served []Served, envs map[string]map[string]string, log base := os.Environ() r.envFor = func(module string) []string { return BundleEnv(base, envs[module], module, node) } r.fingerprintOf = func(module string) string { return envs[module][FingerprintWord] } + r.envOf = func(module string) map[string]string { return envs[module] } // The module's events, for every child of it that subscribes (ADR 0198): one consumer per module, // bound the first time any of its children subscribes, each event handed to every child that did. @@ -395,6 +399,7 @@ func (r *runner) claimed() map[string]bool { func (r *runner) load(s Served) *loadedModule { l := &loadedModule{served: s, seen: map[string]string{}} claimed := r.claimed() + var all []launch.Registration // every bundle's registrations, the module's marks checked against them together (A-398) for _, entry := range s.Entrypoints { path, _ := filepath.Abs(entry) module := s.Module @@ -425,13 +430,7 @@ func (r *runner) load(s Served) *loadedModule { fail(clashed) continue } - // A bundle whose marks name a tool it does not register is not loaded: no tool of its module is served here, rather - // than one served unmarked that the manifest meant to mark (design 46 7-approval-mark §4). - if unserved := r.unservedMarks(module, child.Registrations); len(unserved) > 0 { - child.Stop() - fail(fmt.Sprintf("its marks name %s, which it does not register (design 46 7-approval-mark §4)", strings.Join(unserved, ", "))) - continue - } + all = append(all, child.Registrations...) l.children = append(l.children, child.Stop) for _, reg := range child.Registrations { got := registration{module: reg.Module, owner: module, tools: reg.Tools} @@ -445,6 +444,18 @@ func (r *runner) load(s Served) *loadedModule { } } } + // A module whose marks name a tool none of its bundles registers is not loaded: no tool of it is served here, rather + // than one served unmarked that the manifest meant to mark (design 46 7-approval-mark §4). Checked once, against every + // bundle of the module together (lead answer A-398: gitea's eight bundles each overwrote the gate's set). + if unserved := r.unservedMarks(s.Module, all); len(unserved) > 0 { + for _, stop := range l.children { + stop() + } + l.children, l.regs, l.own = nil, nil, nil + l.failed = fmt.Sprintf("its marks name %s, which none of its bundles registers (design 46 7-approval-mark §4)", strings.Join(unserved, ", ")) + r.logf("[mesh-tools] %s is not loaded: %s; its tools are not served here", s.Module, l.failed) + return l + } if repairs := r.repairsOf(s.Module); repairs != nil { l.regs, l.own = append(l.regs, *repairs), append(l.own, *repairs) } @@ -479,7 +490,8 @@ func (r *runner) serve(l *loadedModule) { module, own := reg.module, reg.repairs stop, err := r.conn.HandleCalled(reg.module+"."+t.Name, func(caller string, body json.RawMessage) (any, error) { return r.events.gated(approval.Call{Address: module + "." + t.Name, Module: module, Name: t.Name, Own: own, - Fingerprint: r.fingerprintOf(module), Caller: caller, Args: argsOf(body), Description: t.Description}, t, caller) + Fingerprint: r.fingerprintOf(module), Env: r.envOf(module), Caller: caller, Args: argsOf(body), + Description: t.Description}, t, caller) }) if err != nil { r.logf("[mesh-tools] cannot serve %s.%s: %v", reg.module, t.Name, err) @@ -782,7 +794,7 @@ func serveSeats(conn *bus.Conn, modules []string, current func() []registration, // Its callers checked, then through the approval gate, before the bundle (design 47 G §610, A-179; design 46 // 7-approval-mark §20). module, v := module, v - stop, err := conn.HandleSubjectCalled(v.Subject, func(caller string, body json.RawMessage) (any, error) { + handle := func(caller string, body json.RawMessage) (any, error) { if err := admitted(v.Seat, v.Verb, caller); err != nil { return nil, err } @@ -793,13 +805,26 @@ func serveSeats(conn *bus.Conn, modules []string, current func() []registration, } // A verb marked chunks: the bundle answers the whole file, the runner sends it in chunks (design 47 G A-257). return conn.AnswerChunked(v.Seat, v.Verb, body, answer) - }) + } + stop, err := conn.HandleSubjectCalled(v.Subject, handle) if err != nil { logf("[mesh-tools] cannot serve %s's %s on %s: %v", v.Seat, v.Verb, v.Subject, err) continue } stops = append(stops, stop) logf("[mesh-tools] serving %s's %s on %s, admitted where %s holds the seat", v.Seat, v.Verb, v.Subject, module) + // A verb of a node-scoped seat is also answered on the subject that names no machine, by any one holder, in + // one queue group per seat and verb (lead answer A-393). + if open, ok := anyHolderSubject(v, m.Node); ok && !have[open] { + have[open] = true + openStop, err := conn.HandleSubjectCalledQueue(open, "seat."+v.Seat+"."+v.Verb, handle) + if err != nil { + logf("[mesh-tools] cannot serve %s's %s on %s: %v", v.Seat, v.Verb, open, err) + continue + } + stops = append(stops, openStop) + logf("[mesh-tools] serving %s's %s on %s, in a queue group, for a call that names no machine", v.Seat, v.Verb, open) + } } } } @@ -816,6 +841,15 @@ func serveSeats(conn *bus.Conn, modules []string, current func() []registration, }, serve } +// anyHolderSubject is a node-scoped seat verb's subject without its machine, the one asked with no machine named; false +// where the verb's subject does not end in the machine (a seat held once). +func anyHolderSubject(v bus.SeatVerb, node string) (string, bool) { + if node == "" || !strings.HasSuffix(v.Subject, "."+v.Verb+"."+node) { + return "", false + } + return strings.TrimSuffix(v.Subject, "."+node), true +} + // consumers holds, per module the runtime serves, the one durable consumer its events arrive on and // the children its events are handed to (novox/hq ADR 0198). type consumers struct { @@ -1029,7 +1063,7 @@ func (b *moduleBus) Subscribe(deliver func(json.RawMessage) error, beat func(jso return err } m.stop = stop - c.logf("[mesh-tools] %s's events arrive on its consumer %s", module, bus.ConsumerOf(c.conn.Node(), module)) + c.logf("[mesh-tools] %s's events arrive on its consumer %s", module, bus.ToolsConsumerOf(c.conn.Node(), module)) return nil } diff --git a/node-tools/internal/runtime/runtime_test.go b/node-tools/internal/runtime/runtime_test.go index b8e8906..1c48105 100644 --- a/node-tools/internal/runtime/runtime_test.go +++ b/node-tools/internal/runtime/runtime_test.go @@ -226,7 +226,7 @@ func TestAnEntrypointThatIsNotExecutableIsRefused(t *testing.T) { if err != nil { t.Fatal(err) } - if !logs.Has("marked's bundle", "failed to load: its marks name wipe, which it does not register (design 46 7-approval-mark §4)") { + if !logs.Has("marked is not loaded: its marks name wipe, which none of its bundles registers (design 46 7-approval-mark §4)") { t.Errorf("a bundle not serving its marked tool was not refused at the load:\n%s", logs.All()) } if _, err := call(t, asker, "served.who@anchor", map[string]any{}); err != nil { @@ -312,3 +312,47 @@ func TestAnUntakenEventSaysWhoFailed(t *testing.T) { t.Errorf("the event is not named by its id: %q", got) } } + +// A node-scoped seat's verb asked with no machine is answered by a holder, by the tool form the SDK asks +// (lead answer A-393). +func TestANodeScopedSeatVerbIsAlsoAnsweredWithNoMachine(t *testing.T) { + mesh := mt.New(t) + mesh.Issue(t, mt.MembershipOf("theta", "anchor", false, map[string][]string{"node-shelf": {"list"}})) + nodeTools := connect(t, "node-tools", "anchor") + asker := connect(t, "console", "workstation") + regs := func() []registration { + return []registration{{module: "node-shelf", owner: "theta", tools: []launch.Tool{{Name: "list", + Run: func(string, json.RawMessage) (json.RawMessage, error) { return json.RawMessage(`{"shelf":["x"]}`), nil }}}}} + } + gated := func(_ approval.Call, tool launch.Tool, caller string) (any, error) { + return tool.Run(caller, nil) + } + nodeTools.Follow("theta") + stop, _ := serveSeats(nodeTools, []string{"theta"}, regs, gated, func(_, _, _ string) error { return nil }, + func(string) bool { return false }, nil, func(string) string { return "" }, (&mt.Logs{}).Logf) + defer stop() + for _, key := range []string{"seat:node-shelf.list@anchor", "seat:node-shelf.list"} { + t.Run(key, func(t *testing.T) { + mt.Until(t, func() error { + got, err := call(t, asker, key, map[string]any{}) + if err == nil { + same(t, got, `{"shelf":["x"]}`) + } + return err + }) + }) + } +} + +func TestAnyHolderSubject(t *testing.T) { + for _, c := range []struct{ subject, node, want string }{ + {"mesh.seat.node-build-go.tool.steps.shanks", "shanks", "mesh.seat.node-build-go.tool.steps"}, + {"mesh.seat.mesh-delivery.tool.show", "shanks", ""}, + {"mesh.seat.node-build-go.tool.steps.shanks", "", ""}, + } { + got, ok := anyHolderSubject(bus.SeatVerb{Seat: "s", Verb: "steps", Subject: c.subject}, c.node) + if got != c.want || ok != (c.want != "") { + t.Errorf("%s on %q: %q %v", c.subject, c.node, got, ok) + } + } +} diff --git a/node-tools/internal/runtime/sessions_test.go b/node-tools/internal/runtime/sessions_test.go index 4d67f94..97adf21 100644 --- a/node-tools/internal/runtime/sessions_test.go +++ b/node-tools/internal/runtime/sessions_test.go @@ -37,12 +37,26 @@ func (f *fakeObjects) ObjectsPutStream(ctx context.Context, _ string, r buscalls return buscalls.ObjectInfo{Name: r.Name, Size: uint64(len(data)), Description: description}, nil } -func (f *fakeObjects) ObjectsOpen(context.Context, string, buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) { - return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), true, nil +// ctxReader reads as the bus's object reader does: on the context it was opened on, failing once that is cancelled. +type ctxReader struct { + ctx context.Context + r io.Reader } -func (f *fakeObjects) SentOpen(context.Context, buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) { - return buscalls.ObjectInfo{Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), nil +func (c ctxReader) Read(p []byte) (int, error) { + if err := c.ctx.Err(); err != nil { + return 0, err + } + return c.r.Read(p) +} +func (c ctxReader) Close() error { return nil } + +func (f *fakeObjects) ObjectsOpen(ctx context.Context, _ string, _ buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) { + return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, true, nil +} + +func (f *fakeObjects) SentOpen(ctx context.Context, _ buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) { + return buscalls.ObjectInfo{Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, nil } func aSession(t *testing.T, f *fakeObjects) *moduleBus {