diff --git a/node-tools/internal/runtime/ledgers_test.go b/node-tools/internal/runtime/ledgers_test.go index 412809b..3315484 100644 --- a/node-tools/internal/runtime/ledgers_test.go +++ b/node-tools/internal/runtime/ledgers_test.go @@ -67,6 +67,18 @@ func TestTheLedgerRepairsAreToolsOfEveryLedgerOwner(t *testing.T) { }) } t.Run("a ledger bound later brings them", func(t *testing.T) { + // Its own tool answers the first call after each binding comes or goes: the repairs' refresh keeps the + // subscriptions it still names (design 47 H, H-A-160). + for round := 1; round <= 10; round++ { + plain.Bindings = nil + if round%2 == 1 { + plain.Bindings = owner.Bindings + } + mesh.Issue(t, plain) + if _, err := asker.AskWithin("plain.who@anchor", map[string]any{}, "", callWithin); err != nil { + t.Fatalf("binding change %d: the first call of plain.who failed: %v — dropped while its tools were served again", round, err) + } + } plain.Bindings = owner.Bindings mesh.Issue(t, plain) mt.Until(t, func() error { diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index ff6e68d..5ade9af 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -185,10 +185,6 @@ func (r *runner) refreshRepairs() { if served == (repairs != nil) { continue } - for i := len(l.serving) - 1; i >= 0; i-- { - l.serving[i]() - } - l.serving, l.names = nil, nil l.own, l.regs = withRepairs(l.own, repairs), withRepairs(l.regs, repairs) r.serve(l) changed = true @@ -207,14 +203,14 @@ type loadedModule struct { regs []registration // every registration its bundles made own []registration // those under the name of a module served here failed string - names []string // the tools it serves, as module.tool - serving []func() // its tools' subscriptions + names []string // the tools it serves, as module.tool + serving map[string]func() // its tools' subscriptions, by module.tool; serve alone changes it } // unload stops serving the module's tools and stops its bundles. func (l *loadedModule) unload() { - for i := len(l.serving) - 1; i >= 0; i-- { - l.serving[i]() + for _, stop := range l.serving { + stop() } l.serving, l.names = nil, nil for i := len(l.children) - 1; i >= 0; i-- { @@ -493,25 +489,47 @@ func refusal(own []registration) error { return nil } -// serve binds a loaded module's tools. r.mu is held. +// serve binds a loaded module's tools as its registrations name them now, and is the one routine that changes its +// subscriptions (design 47 H, H-A-158, H-A-160): a tool still named keeps its subscription, a newly named one is +// subscribed, and only then is one no longer named stopped — no tool is served twice, and none is dropped while the +// module's tools are served again. r.mu is held. func (r *runner) serve(l *loadedModule) { + if l.serving == nil { + l.serving = map[string]func(){} + } + named := map[string]bool{} + l.names = nil for _, reg := range l.own { for _, t := range reg.tools { t := t - // Told who called, from the subject the call arrived on, never from its body (novox/hq issue 365). - // Through the approval gate, before the bundle or the runner's own tool (design 46 7-approval-mark §20). - 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), 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) + address := reg.module + "." + t.Name + if named[address] { continue } - l.names = append(l.names, reg.module+"."+t.Name) - l.serving = append(l.serving, stop) + named[address] = true + if _, kept := l.serving[address]; !kept { + // Told who called, from the subject the call arrived on, never from its body (novox/hq issue 365). + // Through the approval gate, before the bundle or the runner's own tool (design 46 7-approval-mark §20). + module, own := reg.module, reg.repairs + stop, err := r.conn.HandleCalled(address, 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), 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) + delete(named, address) + continue + } + l.serving[address] = stop + } + l.names = append(l.names, address) + } + } + for address, stop := range l.serving { + if !named[address] { + stop() + delete(l.serving, address) } } }