T3 whole: the tool runner is started by its entry against an embedded bus

main's body is `serve(ctx)`, which the process calls with the service manager's signals and one integration test calls with a
credential file and the node-engine's environment: it serves a bundle's tool and seat verbs over a seat list read from a stand-in of
the seat mesh-catalog, waits a verb its definition's timeout, and rereads the list for a verb the first read does not name.

Builds: design 47 H §774, §760, H-A-16, H-A-61

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
2026-11-20 02:44:40 +01:00
parent 81663a4d9e
commit d9fe2568de
3 changed files with 220 additions and 7 deletions
+17 -7
View File
@@ -53,6 +53,17 @@ func main() {
fmt.Fprintf(os.Stderr, "node-tools: %q is not a mode; this runtime serves (novox/hq ADR 0193)\n", os.Args[1])
os.Exit(2)
}
// Stopped by the service manager's signal; the runtime itself is serve, which a whole test starts by its entry (design 47 H §774).
ctx, stopSignals := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stopSignals()
if err := serve(ctx); err != nil {
log.Fatalf("node-tools: %v", err)
}
}
// serve is the tool runner: it reads its credential and serves until ctx ends, then shuts down in order. It is the entry a whole test
// starts, beside the process's own (design 47 H §774, H-A-61).
func serve(ctx context.Context) error {
until := time.Now().Add(startWait)
cred := credential(until)
conn := connectPatiently(cred, until)
@@ -61,11 +72,11 @@ func main() {
served, err := runtime.ServedModulesFrom(os.Getenv(runtime.ToolModules), cred.Module)
if err != nil {
log.Fatalf("node-tools: %v", err)
return err
}
envs, err := runtime.TakeToolEnvs()
if err != nil {
log.Fatalf("node-tools: %v", err)
return err
}
// Its own secret ledgers: each module's ledger connection, the only one its history is published on (novox/hq ADR
// 0597 §9). Followed, so a credential minted or retired is taken with no restart.
@@ -102,7 +113,7 @@ func main() {
go gate.Recover()
stop, err := runtime.Run(conn, served, envs, log.Printf, opts...)
if err != nil {
log.Fatalf("node-tools: %v", err)
return err
}
// And says it is there (novox/hq to-be 45 S11): the machine's runtime, on its machine's name. A
// runtime on another credential — a module's own, in a test — is not the machine's.
@@ -131,14 +142,12 @@ func main() {
surface.Approvals = gateTools{Gate: gate, state: approvalsState{conn: conn, module: cred.Module}}
up, err = console.Serve(surface, listen)
if err != nil {
log.Fatalf("node-tools: %v", err)
return err
}
log.Printf("mesh console listening on http://%s/mcp as %s", up.Address, who)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
<-signals
<-ctx.Done()
// First of all: the service manager signalled every bundle with this process, and none is started again
// (issue 1126).
launch.ShutDown()
@@ -149,6 +158,7 @@ func main() {
_ = up.Close()
}
conn.Close()
return nil
}
// startWait is how long a runtime waits, from its start, for its broker credential to be on the machine and for the bus
@@ -0,0 +1,164 @@
//go:build integration
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"git.novox.be/novox/mesh-sdk/go/seatlist"
"github.com/novox/mesh-tools/node-tools/internal/bus"
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
)
// catalogStandIn is the seat mesh-catalog's verb `seat-definitions`, as the seat list's reader asks it: the seat node-desk whose
// first listing names the verbs who and slow (slow with a timeout of one second) and, from the second listing on, also the verb
// fresh, which sends its answer in chunks.
type catalogStandIn struct {
mu sync.Mutex
listings int
}
func (c *catalogStandIn) definition(withFresh bool) json.RawMessage {
one := 1
verbs := []seatdef.Verb{{Name: "who"}, {Name: "slow", Timeout: &one}}
if withFresh {
verbs = append(verbs, seatdef.Verb{Name: "fresh", Chunks: true})
}
raw, _ := json.Marshal(seatdef.Definition{Format: seatdef.Format, Seat: "node-desk", Scope: seatdef.ScopeNode, Grain: seatdef.SeatList,
Decision: "ADR 0001", Domain: seatdef.Domains[0], Verbs: verbs})
return raw
}
func (c *catalogStandIn) handle(body json.RawMessage) (any, error) {
var asked struct {
Seat string `json:"seat"`
}
_ = json.Unmarshal(body, &asked)
c.mu.Lock()
defer c.mu.Unlock()
if asked.Seat == "" {
c.listings++
return seatlist.Listing{Revision: uint64(c.listings), Seats: []seatlist.Entry{{Seat: "node-desk", Source: seatlist.SourceTrunk,
Digest: map[bool]string{false: "d1", true: "d2"}[c.listings > 1]}}}, nil
}
withFresh := c.listings > 1
return seatlist.Answer{Seat: asked.Seat, Found: true, Revision: uint64(c.listings), Entry: &seatlist.Entry{Seat: asked.Seat,
Source: seatlist.SourceTrunk, Digest: map[bool]string{false: "d1", true: "d2"}[withFresh], Definition: c.definition(withFresh)}}, nil
}
// **The tool runner, started whole by its entry** (design 47 H §774, H-A-16, H-A-61): the process's own `serve`, on an embedded bus with
// JetStream, with a credential file and the environment the node-engine writes, serving one bundle's tool and seat verbs, over the
// seat list read from a stand-in of the seat mesh-catalog.
// - the bundle's tool and its seat verb are answered on the bus (§734's way of serving a verb from a bundle);
// - a verb whose definition names a timeout of one second is waited one second by the serving runner, not the runner's own
// thirty (design 47 H §760, H-A-43);
// - a bundle's ask of a seat verb its first seat list does not name is asked after one reread of the list, with its call token
// for a verb that sends chunks (design 47 G A-490m).
func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *testing.T) {
mesh := mt.New(t)
mesh.Issue(t, mt.MembershipOf("ledger", "anchor", true, map[string][]string{"node-desk": {"who", "slow"}}))
dir := t.TempDir()
credential, _ := json.Marshal(bus.Credential{URL: mt.URL(t), Module: runtimeModule, Node: "anchor"})
file := filepath.Join(dir, "broker")
if err := os.WriteFile(file, credential, 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("MESH_BROKER_FILE", file)
t.Setenv("MESH_TOOL_MODULES", "ledger="+mt.Fixture("whole-bundle.py"))
t.Setenv("MESH_CONSOLE_LISTEN", "127.0.0.1:0")
open := func(module, node string) *bus.Conn {
c, err := bus.Connect(bus.Credential{URL: mt.URL(t), Module: module, Node: node, User: node + "." + module, Password: "unused"})
if err != nil {
t.Fatal(err)
}
c.Logf = func(string, ...any) {}
t.Cleanup(c.Close)
return c
}
catalog := &catalogStandIn{}
holder := open("mesh-catalog", "anchor")
if _, err := holder.HandleSubject("mesh.seat.mesh-catalog.tool.seat-definitions", func(body json.RawMessage) (any, error) { return catalog.handle(body) }); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var fresh []map[string]any
if _, err := holder.HandleSubject("mesh.seat.node-desk.tool.fresh", func(body json.RawMessage) (any, error) {
var got map[string]any
_ = json.Unmarshal(body, &got)
mu.Lock()
fresh = append(fresh, got)
mu.Unlock()
return map[string]any{"ok": true}, nil
}); err != nil {
t.Fatal(err)
}
holder.Flush()
ctx, stop := context.WithCancel(context.Background())
ended := make(chan error, 1)
go func() { ended <- serve(ctx) }()
t.Cleanup(func() {
stop()
select {
case err := <-ended:
if err != nil {
t.Errorf("serve ended with %v", err)
}
case <-time.After(30 * time.Second):
t.Error("the tool runner did not stop within 30s")
}
})
// a module's account, which the approval gate runs unmarked while the catalog half is unread (the stand-in serves no catalog half)
asker := open("shop", "workstation")
ask := func(key string) (string, error) {
answered, err := asker.Ask(key, map[string]any{}, "")
return string(answered.Result), err
}
// the bundle's tool and its seat verb are served
for _, key := range []string{"ledger.who", "seat:node-desk.who@anchor"} {
mt.Until(t, func() error {
got, err := ask(key)
if err == nil && !strings.Contains(got, `"tool":`) {
t.Errorf("%s answered %s, want the bundle's", key, got)
}
return err
})
}
// a verb whose definition names a timeout of one second is waited one second
began := time.Now()
if _, err := ask("seat:node-desk.slow@anchor"); err == nil {
t.Error("the verb that answers after three seconds was answered within its timeout of one second")
}
if took := time.Since(began); took < time.Second || took > 2500*time.Millisecond {
t.Errorf("the verb of a timeout of one second was waited %s, want about one second, not the runner's own %s", took, bus.RequestTimeout)
}
// the bundle's ask of a verb its first list does not name is asked after one reread, with its call token
mt.Until(t, func() error {
got, err := ask("ledger.reach")
if err == nil && strings.Contains(got, "error") {
t.Errorf("the bundle's ask of node-desk.fresh failed: %s", got)
}
return err
})
catalog.mu.Lock()
listings := catalog.listings
catalog.mu.Unlock()
mu.Lock()
defer mu.Unlock()
if listings != 2 || len(fresh) != 1 || fresh[0]["call"] == nil || fresh[0]["call"] == "" {
t.Errorf("the list was read %d times (want 2) and the holder of node-desk.fresh was asked %v (want once, with a call token)", listings, fresh)
}
}
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# A tools bundle for the tool runner's whole test (design 47 H §774): MCP over stdio, no SDK. A tool `who`, the seat node-desk's verbs
# `who` (answers at once) and `slow` (answers after three seconds), and a tool `reach` that asks the seat verb node-desk.fresh as the
# bundle does, through the runtime's `mesh/ask`, and says what came back.
import json, sys, time
def say(m):
m["jsonrpc"] = "2.0"; sys.stdout.write(json.dumps(m) + "\n"); sys.stdout.flush()
def text(rid, value):
say({"id": rid, "result": {"content": [{"type": "text", "text": json.dumps(value)}]}})
TOOLS = [
{"name": "who", "description": "the module's own tool", "inputSchema": {"type": "object", "properties": {}}},
{"name": "reach", "description": "asks the seat verb node-desk.fresh", "inputSchema": {"type": "object", "properties": {}}},
{"name": "node-desk.who", "description": "the seat's verb", "inputSchema": {"type": "object", "properties": {}}},
{"name": "node-desk.slow", "description": "the seat's verb that answers late", "inputSchema": {"type": "object", "properties": {}}},
]
for line in sys.stdin:
req = json.loads(line); rid = req.get("id"); m = req.get("method"); p = req.get("params") or {}
if m == "initialize":
say({"id": rid, "result": {"protocolVersion": "2025-03-26", "capabilities": {"tools": {}}, "serverInfo": {"name": "ledger", "version": "1"}}})
elif m == "tools/list":
say({"id": rid, "result": {"tools": TOOLS}})
elif m == "tools/call":
name = p.get("name")
if name == "node-desk.slow":
time.sleep(3)
if name == "reach":
say({"id": "q1", "method": "mesh/ask", "params": {"key": "seat:node-desk.fresh", "body": {}}})
for answer in sys.stdin:
a = json.loads(answer)
if a.get("id") == "q1":
text(rid, a.get("result") if "error" not in a else {"error": a["error"]})
break
else:
text(rid, {"tool": name})
elif rid is not None:
say({"id": rid, "result": {}})