From d1dc482b058d7354c9999ee4b44a877fec2027fd Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 23:07:23 +0100 Subject: [PATCH] node-tools: a runner rereads the list only for a verb of a seat it defines, one read shared; asks of undefined seats and tools unchanged (A-490m) Builds: design 47 G A-490m Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- node-tools/internal/runtime/caller_test.go | 64 ++++++++++++++------- node-tools/internal/runtime/gate.go | 67 ++++++++++++++++++---- node-tools/internal/runtime/runtime.go | 16 +++++- 3 files changed, 111 insertions(+), 36 deletions(-) diff --git a/node-tools/internal/runtime/caller_test.go b/node-tools/internal/runtime/caller_test.go index 4e0c228..fd8c2f7 100644 --- a/node-tools/internal/runtime/caller_test.go +++ b/node-tools/internal/runtime/caller_test.go @@ -3,16 +3,17 @@ package runtime import ( "encoding/json" "errors" + "runtime" "strings" "testing" "time" "git.novox.be/novox/mesh-sdk/go/approval" "git.novox.be/novox/mesh-sdk/go/seatdef" + "github.com/novox/mesh-tools/node-tools/internal/bus" "github.com/nats-io/nats.go" - "github.com/novox/mesh-tools/node-tools/internal/bus" mt "github.com/novox/mesh-tools/node-tools/internal/meshtest" ) @@ -151,35 +152,56 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { t.Error("a runner that holds no seat definitions asks no chunks") } }) - // A seat verb a bundle asks that the definitions held do not name is read again once, never taken for one that sends no chunks: - // the holder refuses an ask that carries no call, and the bundle saw only that refusal (the compositions of 2026-11-19). - t.Run("AVerbTheListDoesNotNameIsReadAgainAndNeverAskedWithoutItsCall", func(t *testing.T) { + // Design 47 G A-490m: a verb of a seat the list defines whose definition does not name it is read again once and never asked + // without its call; a seat the list does not define, a module's tool and a bundle asking through moduleBus.Ask behave as before. + t.Run("AVerbTheDefinitionDoesNotNameIsReadAgainAndNeverAskedWithoutItsCall", func(t *testing.T) { stale := map[string]seatdef.Definition{"mesh-wants": {Verbs: []seatdef.Verb{{Name: "revision"}}}} fresh := map[string]seatdef.Definition{"mesh-wants": {Verbs: []seatdef.Verb{{Name: "revision"}, {Name: "input", Chunks: true}}}} for _, row := range []struct { - name string - again func(held *map[string]seatdef.Definition) error - want bool - wantErr string - wantRead int + name string + key string + again func(held *map[string]seatdef.Definition) error + wantWay string // "plain", "chunked" or "" for a refusal + wantErr string + wantReads int }{ - {"a read that names it", func(h *map[string]seatdef.Definition) error { *h = fresh; return nil }, true, "", 1}, - {"a read that fails", func(h *map[string]seatdef.Definition) error { return errors.New("the seat does not answer") }, false, "does not answer", 1}, - {"a read that still does not name it", func(h *map[string]seatdef.Definition) error { return nil }, false, "does not name the verb seat:mesh-wants.input", 1}, + {"a defined seat's verb the read names", "seat:mesh-wants.input", func(h *map[string]seatdef.Definition) error { *h = fresh; return nil }, "chunked", "", 1}, + {"a read that fails", "seat:mesh-wants.input", func(h *map[string]seatdef.Definition) error { return errors.New("the seat does not answer") }, "", "does not answer", 1}, + {"a read that still does not name it", "seat:mesh-wants.input@g14", func(h *map[string]seatdef.Definition) error { return nil }, "", "names no verb input", 1}, + {"a seat the list does not define", "seat:issue-tracker.list", func(h *map[string]seatdef.Definition) error { return nil }, "plain", "", 0}, + {"a module's tool", "mesh-delivery-indexer.indexer", func(h *map[string]seatdef.Definition) error { return nil }, "plain", "", 0}, + {"a verb the list names without chunks", "seat:mesh-wants.revision", func(h *map[string]seatdef.Definition) error { return nil }, "plain", "", 0}, } { t.Run(row.name, func(t *testing.T) { - held, reads := stale, 0 - c := &consumers{definitions: func() map[string]seatdef.Definition { return held }, reread: func() error { reads++; return row.again(&held) }} - got, err := c.chunkedAsked("seat:mesh-wants.input") - if got != row.want || reads != row.wantRead || (row.wantErr == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), row.wantErr)) { - t.Errorf("chunkedAsked = %v, %v after %d reads, want %v, %q after %d", got, err, reads, row.want, row.wantErr, row.wantRead) + held, reads, way := stale, 0, "" + c := &consumers{definitions: func() map[string]seatdef.Definition { return held }, reread: func() error { reads++; return row.again(&held) }, + askPlain: func(_, _ string, _ any) (bus.Answered, error) { way = "plain"; return bus.Answered{}, nil }, + askChunked: func(_, _ string, _ any) (bus.Answered, error) { way = "chunked"; return bus.Answered{}, nil }} + _, err := (&moduleBus{all: c, module: "m"}).Ask(json.RawMessage(`{"key":"` + row.key + `","body":{}}`)) + if way != row.wantWay || reads != row.wantReads || (row.wantErr == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), row.wantErr)) { + t.Errorf("Ask %s: asked %q, %v after %d reads, want %q, %q after %d", row.key, way, err, reads, row.wantWay, row.wantErr, row.wantReads) } }) } - held := fresh - c := &consumers{definitions: func() map[string]seatdef.Definition { return held }, reread: func() error { t.Error("a verb the list names is not read again"); return nil }} - if got, err := c.chunkedAsked("seat:mesh-wants.input@g14"); !got || err != nil { - t.Errorf("a named chunked verb: %v, %v", got, err) + // the asks that wait on a read share it + release, reads := make(chan struct{}), 0 + held := stale + c := &consumers{definitions: func() map[string]seatdef.Definition { return held }, reread: func() error { reads++; <-release; held = fresh; return nil }} + done := make(chan bool, 3) + for i := 0; i < 3; i++ { + go func() { got, err := c.chunkedAsked("seat:mesh-wants.input"); done <- got && err == nil }() + } + for c.rereadWaiting() < 2 { + runtime.Gosched() + } + close(release) + for i := 0; i < 3; i++ { + if !<-done { + t.Error("an ask waiting on the shared read was not answered chunked") + } + } + if reads != 1 { + t.Errorf("%d reads of the seat list for three waiting asks, want 1", reads) } if got, err := (&consumers{}).chunkedAsked("seat:mesh-wants.input"); got || err != nil { t.Errorf("a runner that holds no definitions asks no chunks: %v, %v", got, err) diff --git a/node-tools/internal/runtime/gate.go b/node-tools/internal/runtime/gate.go index 3b11ebb..8777b3d 100644 --- a/node-tools/internal/runtime/gate.go +++ b/node-tools/internal/runtime/gate.go @@ -83,9 +83,10 @@ func WithSeatDefinitionsAgain(again func() error) Option { } // chunkedAsked is chunked for a verb a bundle asks, where the answer decides whether the runner chooses the `call` the holder -// requires (AskAsChunked): a seat verb the definitions held do not name is not taken for one that sends no chunks, since the -// holder then refuses an ask that carries no call and the bundle sees only that refusal. The seat list is read again once, and -// a verb it still does not name is an error that says why. A runner that holds no definitions (nil) asks no chunks. +// requires (AskAsChunked) (design 47 G A-490m). A verb of a seat the definitions held DEFINE, whose definition does not name it, +// is not taken for one that sends no chunks, since the holder then refuses an ask that carries no call: the seat list is read +// again once (one read shared by the asks that wait on it) and a verb still unnamed is an error saying so. An ask of a seat the +// definitions do not define at all, a module's tool and a runner that holds no definitions (nil) are asked as before. func (c *consumers) chunkedAsked(key string) (bool, error) { if c.definitions == nil || !strings.HasPrefix(key, "seat:") { return false, nil @@ -95,27 +96,69 @@ func (c *consumers) chunkedAsked(key string) (bool, error) { if !ok { return false, nil } - find := func() (chunks, found bool) { - for _, v := range c.definitions()[seat].Verbs { + find := func() (chunks, defined, named bool) { + d, defined := c.definitions()[seat] + for _, v := range d.Verbs { if v.Name == verb { - return v.Chunks, true + return v.Chunks, true, true } } - return false, false + return false, defined, false } - if chunks, found := find(); found { + chunks, defined, named := find() + if named || !defined { return chunks, nil } var why string if c.reread != nil { - if err := c.reread(); err != nil { - why = ": reading the seat list again failed: " + err.Error() + if err := c.rereadShared(); err != nil { + why = " (reading the seat list again failed: " + err.Error() + ")" } - if chunks, found := find(); found { + chunks, defined, named = find() + if named { return chunks, nil } + if !defined { + return false, nil + } } - return false, fmt.Errorf("the seat list this runner holds does not name the verb %s, so it cannot tell whether the verb sends its answer in chunks (the ask is not sent)%s", key, why) + return false, fmt.Errorf("the definition of the seat %s names no verb %s, so the runner cannot tell whether it sends its answer in chunks and does not ask it%s", seat, verb, why) +} + +// rereadShared reads the seat list again; asks that arrive while a read runs wait for it and share its result. +func (c *consumers) rereadShared() error { + c.rereadMu.Lock() + if f := c.rereading; f != nil { + f.waiters++ + c.rereadMu.Unlock() + <-f.done + return f.err + } + f := &rereadFlight{done: make(chan struct{})} + c.rereading = f + c.rereadMu.Unlock() + f.err = c.reread() + c.rereadMu.Lock() + c.rereading = nil + c.rereadMu.Unlock() + close(f.done) + return f.err +} + +type rereadFlight struct { + done chan struct{} + err error + waiters int // the asks that joined the read (under rereadMu) +} + +// rereadWaiting is how many asks have joined the read now running; a test waits on it instead of sleeping. +func (c *consumers) rereadWaiting() int { + c.rereadMu.Lock() + defer c.rereadMu.Unlock() + if c.rereading == nil { + return 0 + } + return c.rereading.waiters } // chunked says whether key names a seat's verb whose definition says `chunks` (design 47 G A-249): its answer is received in diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index 374c7e1..5d8d1d3 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -872,7 +872,11 @@ type consumers struct { // definitions are the seat definitions a seat verb's callers are read from (gate.go); nil admits every caller. definitions func() map[string]seatdef.Definition // reread reads the seat list again (gate.go, chunkedAsked); nil reads nothing. - reread func() error + reread func() error + rereadMu sync.Mutex + rereading *rereadFlight + // askPlain and askChunked are how a bundle's ask is sent (the bus connection's AskAs and AskAsChunked); a test gives its own. + askPlain, askChunked func(module, key string, body any) (bus.Answered, error) } type moduleEvents struct { @@ -1001,13 +1005,19 @@ func (b *moduleBus) Ask(params json.RawMessage) (json.RawMessage, error) { if len(asked.Body) == 0 { body = map[string]any{} } - ask := b.all.conn.AskAs + ask := b.all.askPlain + if ask == nil { + ask = b.all.conn.AskAs + } chunks, err := b.all.chunkedAsked(asked.Key) if err != nil { return nil, err } if chunks { - ask = b.all.conn.AskAsChunked // the runner receives the chunks of the answer; the bundle receives none (design 47 G A-249) + if ask = b.all.askChunked; ask == nil { + ask = b.all.conn.AskAsChunked + } + // the runner receives the chunks of the answer; the bundle receives none (design 47 G A-249) } answered, err := ask(b.module, asked.Key, body) if err != nil {