From 2365e418115519b576a12842aa12cd74b8f71c6f Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 23:14:53 +0100 Subject: [PATCH] Copy the kept call's ask id from its request header into the call's _meta as mesh/ask MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every tool call and seat verb the tool runner serves hands the bundle the ask id the kept call set in the header x-ask (stdio.AskHeader) under _meta mesh/ask (stdio.MetaAsk), beside mesh/call; a call whose header carries none names none. Builds: design 47 H2 §775, H-A-351 --- node-tools/internal/bus/bus.go | 13 +++++--- node-tools/internal/bus/caller.go | 5 +-- node-tools/internal/bus/caller_bus_test.go | 6 ++-- node-tools/internal/launch/launch.go | 33 +++++++++++++++----- node-tools/internal/runtime/caller_test.go | 34 +++++++++++++++++++++ node-tools/internal/runtime/gate.go | 6 ++-- node-tools/internal/runtime/runtime.go | 10 +++--- node-tools/internal/runtime/runtime_test.go | 2 +- node-tools/test/fixtures/caller-echo.py | 5 +-- 9 files changed, 87 insertions(+), 27 deletions(-) diff --git a/node-tools/internal/bus/bus.go b/node-tools/internal/bus/bus.go index 6b6178b..f3d3bb8 100644 --- a/node-tools/internal/bus/bus.go +++ b/node-tools/internal/bus/bus.go @@ -20,6 +20,7 @@ import ( "encoding/json" "errors" "fmt" + stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/membership" "log" "regexp" @@ -626,7 +627,7 @@ type reply struct { // answerOn answers one subject with one handler, and says which machine answered (ADR 0159). func (c *Conn) answerOn(subject, queue string, h Handler) (func(), error) { - return c.answerCalledOn(subject, queue, func(_ string, body json.RawMessage) (any, error) { return h(body) }) + return c.answerCalledOn(subject, queue, func(_, _ string, body json.RawMessage) (any, error) { return h(body) }) } // answerCalledOn answers one subject, handing the handler the caller the subject a call arrived on names: @@ -636,7 +637,11 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e cb := func(msg *nats.Msg) { go func() { var r reply - result, err := h(callerOn(subject, msg.Subject), json.RawMessage(msg.Data)) + ask := "" + if msg.Header != nil { + ask = msg.Header.Get(stdio.AskHeader) + } + result, err := h(callerOn(subject, msg.Subject), ask, json.RawMessage(msg.Data)) // A call the runner's drain cut at its stop is answered nothing: its caller sees a transport fault (design 47 H2 // §813 (12)). var unanswered interface{ Unanswered() bool } @@ -695,7 +700,7 @@ func nullable(v any) any { // HandleSubject answers a subject outright: a seat's verb where the mesh issued it. func (c *Conn) HandleSubject(subject string, h Handler) (func(), error) { - return c.HandleSubjectCalled(subject, func(_ string, body json.RawMessage) (any, error) { return h(body) }) + return c.HandleSubjectCalled(subject, func(_, _ string, body json.RawMessage) (any, error) { return h(body) }) } // HandleSubjectCalled answers a tool subject and its caller-named counterpart (novox/hq issue 365): a call on @@ -733,7 +738,7 @@ func (c *Conn) answerBoth(subject, queue string, h CalledHandler) (func(), error // Handle serves `.` where the mesh issued that module, and follows its membership: // when a new one arrives, it serves where it now says and stops where it no longer does. func (c *Conn) Handle(key string, h Handler) (func(), error) { - return c.HandleCalled(key, func(_ string, body json.RawMessage) (any, error) { return h(body) }) + return c.HandleCalled(key, func(_, _ string, body json.RawMessage) (any, error) { return h(body) }) } // HandleCalled is Handle telling the handler who called: the bus user a caller-named subject names, or "" for diff --git a/node-tools/internal/bus/caller.go b/node-tools/internal/bus/caller.go index 24e07fe..94cb94d 100644 --- a/node-tools/internal/bus/caller.go +++ b/node-tools/internal/bus/caller.go @@ -106,8 +106,9 @@ func callerOn(pattern, subject string) string { return UserOfToken(subject[dot+1:]) } -// CalledHandler answers one call, told who called: the bus user the subject named, or "" when it named none. -type CalledHandler func(caller string, body json.RawMessage) (any, error) +// CalledHandler answers one call, told who called: the bus user the subject named, or "" when it named none; and the ask id the +// call carried in its request header stdio.AskHeader, or "" (design 47 H2 §775, H-A-351). +type CalledHandler func(caller, ask string, body json.RawMessage) (any, error) // callers is what a connection keeps of its caller-named calls: those in flight, by subject, so a refused // publish ends its call at once instead of after RequestTimeout. diff --git a/node-tools/internal/bus/caller_bus_test.go b/node-tools/internal/bus/caller_bus_test.go index 6a6f051..d3a8133 100644 --- a/node-tools/internal/bus/caller_bus_test.go +++ b/node-tools/internal/bus/caller_bus_test.go @@ -58,7 +58,7 @@ type heard struct { callers []string } -func (h *heard) handler(caller string, _ json.RawMessage) (any, error) { +func (h *heard) handler(caller, _ string, _ json.RawMessage) (any, error) { h.mu.Lock() defer h.mu.Unlock() h.callers = append(h.callers, caller) @@ -79,7 +79,7 @@ func TestACallerFallsBackAtOnceWhereNothingServesTheCallerNamedSubject(t *testin h := &heard{} // An older runtime: the subject that names no caller, alone. stop, err := server.HandleSubject("mesh.mod.ledger.tool.who", func(body json.RawMessage) (any, error) { - return h.handler("", body) + return h.handler("", "", body) }) if err != nil { t.Fatal(err) @@ -223,7 +223,7 @@ func TestACallWaitsTheBoundItsVerbNamesAndTimesOutThere(t *testing.T) { url := grantedBus(t, map[string][]string{"anchor.node-tools": {"_INBOX.>"}, "shanks.node-tools": {"mesh.mod.*.tool.>"}}) server := dial(t, url, "node-tools", "anchor", "anchor.node-tools") release := make(chan struct{}) - stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.slow", func(string, json.RawMessage) (any, error) { + stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.slow", func(string, string, json.RawMessage) (any, error) { <-release return map[string]any{}, nil }) diff --git a/node-tools/internal/launch/launch.go b/node-tools/internal/launch/launch.go index be13279..59dea2d 100644 --- a/node-tools/internal/launch/launch.go +++ b/node-tools/internal/launch/launch.go @@ -48,7 +48,19 @@ type Tool struct { Run func(caller string, args json.RawMessage) (json.RawMessage, error) // RunWithin is Run that waits within for the bundle's answer, the `timeout` the verb's seat definition names (design 47 H §760, // H-A-43); zero waits CallTimeout. A tool the runtime serves itself has none: Run is its call. - RunWithin func(caller string, args json.RawMessage, within time.Duration) (json.RawMessage, error) + RunWithin func(caller, ask string, args json.RawMessage, within time.Duration) (json.RawMessage, error) + // within is the wait Within set, which Call hands RunWithin. + within time.Duration +} + +// Call runs the tool for caller, handing a bundle the ask id the call carried in its request header (stdio.AskHeader; design 47 H2 +// §775, H-A-351), which the runtime names in the call's `_meta` under stdio.MetaAsk; "" when it carried none. A tool the runtime +// serves itself is its Run. +func (t Tool) Call(caller, ask string, args json.RawMessage) (json.RawMessage, error) { + if t.RunWithin == nil { + return t.Run(caller, args) + } + return t.RunWithin(caller, ask, args, t.within) } // Within is the tool as called for a verb whose definition names a timeout: its Run waits within for the bundle's answer. A zero @@ -58,7 +70,10 @@ func (t Tool) Within(within time.Duration) Tool { return t } run := t.RunWithin - t.Run = func(caller string, args json.RawMessage) (json.RawMessage, error) { return run(caller, args, within) } + t.Run = func(caller string, args json.RawMessage) (json.RawMessage, error) { + return run(caller, "", args, within) + } + t.within = within return t } @@ -70,8 +85,9 @@ const MetaCaller = "mesh/caller" // callParams is a `tools/call`'s params: the tool, its arguments as the caller sent them, and in `_meta`, which the // runtime writes and no caller can, the caller — only when the subject named one — and the id of the call in flight // (mesh-sdk buscalls.MetaCall), which a ledger's apply names so the runtime stamps the caller it proved (novox/hq ADR -// 0597 §7). -func callParams(name, caller, call string, args json.RawMessage) map[string]any { +// 0597 §7), and the ask id the call carried in its request header, only when it carried one (stdio.MetaAsk; design 47 H2 +// §775, H-A-351). +func callParams(name, caller, call, ask string, args json.RawMessage) map[string]any { params := map[string]any{"name": name, "arguments": args} meta := map[string]any{} if caller != "" { @@ -80,6 +96,9 @@ func callParams(name, caller, call string, args json.RawMessage) map[string]any if call != "" { meta[buscalls.MetaCall] = call } + if ask != "" { + meta[stdio.MetaAsk] = ask + } if len(meta) > 0 { params["_meta"] = meta } @@ -813,7 +832,7 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an input = json.RawMessage("{}") } tool := Tool{Name: name, Description: t.Description, Input: input, - Run: nil, RunWithin: func(caller string, args json.RawMessage, within time.Duration) (json.RawMessage, error) { + Run: nil, RunWithin: func(caller, ask string, args json.RawMessage, within time.Duration) (json.RawMessage, error) { if len(args) == 0 || string(args) == "null" { args = json.RawMessage("{}") } @@ -823,7 +842,7 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an if within > 0 { bound = within } - res, err := asking("tools/call", callParams(full, caller, call, args), bound) + res, err := asking("tools/call", callParams(full, caller, call, ask, args), bound) if err != nil { if errors.Is(err, errNoAnswer) { // Its caller is gone by now and nobody else would hear of it (issue 1966). @@ -860,7 +879,7 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an return b, nil }} tool.Run = func(caller string, args json.RawMessage) (json.RawMessage, error) { - return tool.RunWithin(caller, args, 0) + return tool.RunWithin(caller, "", args, 0) } if _, seen := groups[under]; !seen { order = append(order, under) diff --git a/node-tools/internal/runtime/caller_test.go b/node-tools/internal/runtime/caller_test.go index 86c4a0f..4970a0d 100644 --- a/node-tools/internal/runtime/caller_test.go +++ b/node-tools/internal/runtime/caller_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + stdio "git.novox.be/novox/mesh-sdk/go" "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" @@ -20,6 +21,7 @@ import ( type saidCaller struct { Caller string `json:"caller"` Call string `json:"call"` + Ask *string `json:"ask"` Arguments map[string]any `json:"arguments"` } @@ -65,7 +67,39 @@ func TestACallOnTheCallerNamedSubjectReachesTheModuleWithItsCaller(t *testing.T) if got.Arguments["caller"] != "novox.node-tools" { t.Errorf("%s: the arguments did not reach the module as sent: %v", key, got.Arguments) } + // A call that carries no ask id in its header names none in `_meta` (design 47 H2 §775, H-A-351). + if got.Ask != nil { + t.Errorf("%s reached the module with ask %q, want none for a call whose header carries none", key, *got.Ask) + } } + // **The ask id a kept call sets in its request header is copied into the call's `_meta` as mesh/ask** (design 47 H2 §775, + // H-A-351), for a module's tool and for a seat's verb. + url := mt.URL(t) + t.Run("TheAskIdOfTheHeaderReachesTheModule", func(t *testing.T) { + raw, err := nats.Connect(url) + if err != nil { + t.Fatal(err) + } + defer raw.Close() + for _, subject := range []string{"mesh.mod.ledger.tool.who", "mesh.seat.node-desk.tool.who.anchor"} { + msg := nats.NewMsg(subject) + msg.Header.Set(stdio.AskHeader, "ask-7f3a") + msg.Data = []byte(`{}`) + var reply struct { + Result saidCaller `json:"result"` + } + mt.Until(t, func() error { + answer, err := raw.RequestMsg(msg, 5*time.Second) + if err == nil { + err = json.Unmarshal(answer.Data, &reply) + } + return err + }) + if reply.Result.Ask == nil || *reply.Result.Ask != "ask-7f3a" { + t.Errorf("%s reached the module with ask %v, want the header's ask-7f3a under mesh/ask", subject, reply.Result.Ask) + } + } + }) } // deskDefined is the seat list's definition of the fixture seat node-desk, its verb who. diff --git a/node-tools/internal/runtime/gate.go b/node-tools/internal/runtime/gate.go index 499aef4..977811b 100644 --- a/node-tools/internal/runtime/gate.go +++ b/node-tools/internal/runtime/gate.go @@ -25,11 +25,11 @@ type Gate interface { } // gatedRun is how a handler calls a tool: through the gate where there is one. -type gatedRun func(call approval.Call, t launch.Tool, caller string) (any, error) +type gatedRun func(call approval.Call, t launch.Tool, caller, ask string) (any, error) // gated calls the tool through the gate, or straight where the runner has none. -func (c *consumers) gated(call approval.Call, t launch.Tool, caller string) (any, error) { - run := func() (any, error) { return t.Run(caller, call.Args) } +func (c *consumers) gated(call approval.Call, t launch.Tool, caller, ask string) (any, error) { + run := func() (any, error) { return t.Call(caller, ask, call.Args) } if c.gate == nil { return run() } diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index 3cd8a7e..22bb122 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -518,10 +518,10 @@ func (r *runner) serve(l *loadedModule) { // 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) { + stop, err := r.conn.HandleCalled(address, func(caller, ask 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) + Description: t.Description}, t, caller, ask) }) if err != nil { r.logf("[mesh-tools] cannot serve %s.%s: %v", reg.module, t.Name, err) @@ -815,7 +815,7 @@ func serveSeats(conn *bus.Conn, modules []string, current func() []registration, // record of the calls its gate holds, by caller, and no bundle names it (lead answers A-265, A-266, A-271). if v.Verb == seatdef.ApprovalsVerb && approvals != nil { seat := v.Seat - stop, err := conn.HandleSubjectCalled(v.Subject, func(caller string, _ json.RawMessage) (any, error) { + stop, err := conn.HandleSubjectCalled(v.Subject, func(caller, _ string, _ json.RawMessage) (any, error) { return approvals(caller) }) if err != nil { @@ -834,9 +834,9 @@ func serveSeats(conn *bus.Conn, modules []string, current func() []registration, // Through the approval gate, for any caller, before the bundle (design 47 H §752; design 46 // 7-approval-mark §20). module, v := module, v - handle := func(caller string, body json.RawMessage) (any, error) { + handle := func(caller, ask string, body json.RawMessage) (any, error) { answer, err := gated(approval.Call{Address: v.Seat + "." + v.Verb, Module: module, Seat: v.Seat, Name: v.Verb, - Fingerprint: fingerprintOf(module), Caller: caller, Args: argsOf(body), Description: t.Description}, t.Within(within("seat:"+v.Seat+"."+v.Verb)), caller) + Fingerprint: fingerprintOf(module), Caller: caller, Args: argsOf(body), Description: t.Description}, t.Within(within("seat:"+v.Seat+"."+v.Verb)), caller, ask) if err != nil || !chunked("seat:"+v.Seat+"."+v.Verb) { return answer, err } diff --git a/node-tools/internal/runtime/runtime_test.go b/node-tools/internal/runtime/runtime_test.go index 32bc75a..1de9c1b 100644 --- a/node-tools/internal/runtime/runtime_test.go +++ b/node-tools/internal/runtime/runtime_test.go @@ -328,7 +328,7 @@ func TestANodeScopedSeatVerbIsAlsoAnsweredWithNoMachine(t *testing.T) { 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) { + gated := func(_ approval.Call, tool launch.Tool, caller, _ string) (any, error) { return tool.Run(caller, nil) } nodeTools.Follow("theta") diff --git a/node-tools/test/fixtures/caller-echo.py b/node-tools/test/fixtures/caller-echo.py index a7f7c3e..e478db9 100755 --- a/node-tools/test/fixtures/caller-echo.py +++ b/node-tools/test/fixtures/caller-echo.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 # A tools bundle that says who the runtime said called it (novox/hq issue 365): MCP over stdio, no SDK. Its tool # and its seat verb answer the caller the call's `_meta` named — "" when it named none —, the id of the call in flight -# it named (novox/hq ADR 0597 §7), and the arguments as given. +# it named (novox/hq ADR 0597 §7), the ask id it named under mesh/ask — null when it named none (design 47 H2 §775, H-A-351) —, +# and the arguments as given. import json, sys def say(m): @@ -19,7 +20,7 @@ for line in sys.stdin: say({"id": rid, "result": {"tools": TOOLS}}) elif m == "tools/call": meta = p.get("_meta") or {} - said = {"caller": meta.get("mesh/caller", ""), "call": meta.get("mesh/call", ""), "arguments": p.get("arguments") or {}} + said = {"caller": meta.get("mesh/caller", ""), "call": meta.get("mesh/call", ""), "ask": meta.get("mesh/ask"), "arguments": p.get("arguments") or {}} say({"id": rid, "result": {"content": [{"type": "text", "text": json.dumps(said)}]}}) elif rid is not None: say({"id": rid, "result": {}})