Author SHA1 Message Date
mesh-tools e8f58540ee Merge pull request 'A stopping tool runner answers stopping, never silence (design 47 H2, H-A-542)' (#90) from fix/runner-stopping into main 2026-11-23 11:10:49 +00:00
jschoubben 089d98b406 node-tools: a stopping tool runner answers the typed fault stopping (H-A-542)
A call the runner does not take once its stop began was answered nothing, so its
caller's wait ended in a bare "timeout" and a build step's publish failed (main.440).
It is now answered with error-kind stopping beside its words, on a tool's reply and
a seat proof's alike; the calling runner keeps that kind and hands it to its bundle
as the JSON-RPC error's data, so mesh-sdk's stdio tells it by its kind. A call cut
at the drain bound stays unanswered (H-A-369).

Builds: design 47 H2 H-A-542 (amends H-A-369)
2026-11-23 11:43:19 +01:00
mesh-tools f26ca923c9 Merge pull request 'node-tools: the runner creates the _tools twin from the membership's spec (design 47 H2 H-A-472)' (#89) from fix/tools-twin-membership into main 2026-11-22 20:05:02 +00:00
8 changed files with 132 additions and 27 deletions
@@ -5,6 +5,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
@@ -237,7 +238,8 @@ func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *te
} }
// at its stop the runner drains (design 47 H2 §813 (12), H-A-353; the bound 2 s here for the setting's 30 s): a call of 1 s in // at its stop the runner drains (design 47 H2 §813 (12), H-A-353; the bound 2 s here for the setting's 30 s): a call of 1 s in
// flight is answered, one of 6 s is cut at the bound, said, and its caller answered nothing; a call after the stop is answered nothing // flight is answered, one of 6 s is cut at the bound, said, and its caller answered nothing; a call after the stop is answered
// the typed fault stopping (H-A-542)
type answer struct { type answer struct {
got string got string
err error err error
@@ -259,12 +261,13 @@ func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *te
return nil return nil
}) })
stop() stop()
// a call after the stop began is answered nothing, never refused, so the caller's kept call paces and meets the new runner // a call after the stop began is answered the typed fault stopping, never silence, so its caller asks again after a pause and
// (H-A-369): until the drain began it is still answered // meets the new runner (H-A-369 as H-A-542 amends it): until the drain began it is still answered
mt.Until(t, func() error { mt.Until(t, func() error {
answered, err := asker.AskWithin("ledger.who", map[string]any{}, "", 500*time.Millisecond) answered, err := asker.AskWithin("ledger.who", map[string]any{}, "", 500*time.Millisecond)
if err == nil || err.Error() != "timeout" { var kinded interface{ ErrorKind() string }
return fmt.Errorf("a call after the stop was answered %s, %v; want no answer", answered.Result, err) if !errors.As(err, &kinded) || kinded.ErrorKind() != "stopping" {
return fmt.Errorf("a call after the stop was answered %s, %v; want the typed fault stopping", answered.Result, err)
} }
return nil return nil
}) })
+27 -1
View File
@@ -688,9 +688,30 @@ func (c *Conn) servedOn(module, tool string) []Served {
type reply struct { type reply struct {
Result any `json:"result,omitempty"` Result any `json:"result,omitempty"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
// ErrorKind is the typed fault beside Error, when the handler's error names one (ErrorKind() string): a stopping
// tool runner's stopping (design 47 H2 H-A-542).
ErrorKind string `json:"error-kind,omitempty"`
Node string `json:"node,omitempty"` Node string `json:"node,omitempty"`
} }
// KindedError is an answer's error with its typed fault (`error-kind`, design 47 H2 H-A-542): a stopping tool runner's
// stopping. Its words are the answerer's; ErrorKind is told apart by its kind, never by its words.
type KindedError struct{ Words, Kind string }
func (e *KindedError) Error() string { return e.Words }
// ErrorKind is the typed fault the answer named.
func (e *KindedError) ErrorKind() string { return e.Kind }
// errorKindOf is the typed fault err names (ErrorKind() string), "" for none.
func errorKindOf(err error) string {
var kinded interface{ ErrorKind() string }
if errors.As(err, &kinded) {
return kinded.ErrorKind()
}
return ""
}
// answerOn answers one subject with one handler, and says which machine answered (ADR 0159). // 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) { 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) })
@@ -715,7 +736,7 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
return return
} }
if err != nil { if err != nil {
r.Error = err.Error() r.Error, r.ErrorKind = err.Error(), errorKindOf(err)
} else { } else {
r.Result = nullable(result) r.Result = nullable(result)
} }
@@ -952,11 +973,16 @@ func answerOf(data []byte) (Answered, error) {
var r struct { var r struct {
Result json.RawMessage `json:"result"` Result json.RawMessage `json:"result"`
Error string `json:"error"` Error string `json:"error"`
ErrorKind string `json:"error-kind"`
Node string `json:"node"` Node string `json:"node"`
} }
if err := json.Unmarshal(data, &r); err != nil { if err := json.Unmarshal(data, &r); err != nil {
return Answered{}, err return Answered{}, err
} }
if r.Error != "" && r.ErrorKind != "" {
// A typed fault (a stopping tool runner's stopping, design 47 H2 H-A-542) reaches the caller as such.
return Answered{}, &KindedError{Words: r.Error, Kind: r.ErrorKind}
}
if r.Error != "" { if r.Error != "" {
return Answered{}, errors.New(r.Error) return Answered{}, errors.New(r.Error)
} }
@@ -243,3 +243,37 @@ func TestACallWaitsTheBoundItsVerbNamesAndTimesOutThere(t *testing.T) {
t.Errorf("the call waited %s, want its own bound of 200ms, not the tool runner's %s", took, bus.RequestTimeout) t.Errorf("the call waited %s, want its own bound of 200ms, not the tool runner's %s", took, bus.RequestTimeout)
} }
} }
// stoppingFault is the error a stopping tool runner's drain hands for a call it does not take (launch's afterStop).
type stoppingFault struct{}
func (stoppingFault) Error() string { return "not taken: the tool runner is stopping" }
func (stoppingFault) ErrorKind() string { return "stopping" }
// A call a stopping tool runner does not take is answered the typed fault stopping, never silence, and its caller is
// handed that kind as such, beside the answerer's words; a plain refusal carries no kind (design 47 H2 H-A-542).
func TestAStoppingRunnersFaultReachesItsCallerByItsKind(t *testing.T) {
url := mt.URL(t)
server := dial(t, url, "ledger", "anchor", "")
stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.who", func(_, _ string, body json.RawMessage) (any, error) {
if strings.Contains(string(body), "plain") {
return nil, errors.New("ledger keeps no such key")
}
return nil, fmt.Errorf("ledger's who: %w", stoppingFault{})
})
if err != nil {
t.Fatal(err)
}
defer stop()
server.Flush()
asker := dial(t, url, "node-tools", "shanks", "shanks.node-tools")
_, err = asker.Ask("ledger.who", map[string]any{}, "")
var kinded interface{ ErrorKind() string }
if !errors.As(err, &kinded) || kinded.ErrorKind() != "stopping" || !strings.Contains(err.Error(), "the tool runner is stopping") {
t.Errorf("the call was answered %v, want the typed fault stopping with the answerer's words", err)
}
_, err = asker.Ask("ledger.who", map[string]any{"plain": true}, "")
if err == nil || errors.As(err, &kinded) || err.Error() != "ledger keeps no such key" {
t.Errorf("a plain refusal was answered %v, want its words and no kind", err)
}
}
+10 -5
View File
@@ -195,8 +195,8 @@ func (c *Conn) SeatProve(module, subject string, body json.RawMessage) (json.Raw
} }
// SeatAnswer answers every proof on a subject the module's seat traffic lists among its answers, with what // SeatAnswer answers every proof on a subject the module's seat traffic lists among its answers, with what
// answer returns. A returned error is answered as `{"error": "<words>"}`, never as silence, but for one that says it is // answer returns. A returned error is answered as `{"error": "<words>"}`, with its `error-kind` when it names one (a proof
// unanswered (a proof after the stop or cut at the bound). // after the stop: stopping, design 47 H2 H-A-542), never as silence, but for one that says it is unanswered (cut at the bound).
func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, body json.RawMessage) (json.RawMessage, error)) (func(), error) { func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, body json.RawMessage) (json.RawMessage, error)) (func(), error) {
t, err := c.trafficOf(module) t, err := c.trafficOf(module)
if err != nil { if err != nil {
@@ -220,14 +220,19 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
return return
} }
out, err := answer(msg.Subject, body) out, err := answer(msg.Subject, body)
// A proof arriving after the runner's stop, or cut at its bound, is answered nothing, as answerCalledOn does // A proof cut at the runner's drain bound is answered nothing, as answerCalledOn does (design 47 H2 §813 (12),
// (design 47 H2 §813 (12), H-A-369). // H-A-369); one after the stop is answered stopping below (H-A-542).
var unanswered interface{ Unanswered() bool } var unanswered interface{ Unanswered() bool }
if errors.As(err, &unanswered) && unanswered.Unanswered() { if errors.As(err, &unanswered) && unanswered.Unanswered() {
return return
} }
if err != nil { if err != nil {
b, _ := json.Marshal(map[string]string{"error": err.Error()}) said := map[string]string{"error": err.Error()}
// A typed fault beside the words: a stopping tool runner's stopping (design 47 H2 H-A-542).
if kind := errorKindOf(err); kind != "" {
said["error-kind"] = kind
}
b, _ := json.Marshal(said)
_ = msg.Respond(b) _ = msg.Respond(b)
return return
} }
+8
View File
@@ -181,6 +181,9 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
if string(body) == `{"cut":true}` { if string(body) == `{"cut":true}` {
return nil, unansweredProof{} return nil, unansweredProof{}
} }
if string(body) == `{"stopping":true}` {
return nil, stoppingFault{}
}
return json.RawMessage(`{"linked":true}`), nil return json.RawMessage(`{"linked":true}`), nil
}) })
if err != nil { if err != nil {
@@ -195,6 +198,11 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
if string(answer) != `{"linked":true}` { if string(answer) != `{"linked":true}` {
t.Fatalf("the proof was answered %s", answer) t.Fatalf("the proof was answered %s", answer)
} }
// A proof after the stop is answered the typed fault stopping, never silence (design 47 H2 H-A-542).
if raw, err := conn.SeatProve("telegram", "mesh.seat.intake.proof.code.telegram", json.RawMessage(`{"stopping":true}`)); err != nil ||
!strings.Contains(string(raw), `"error-kind":"stopping"`) {
t.Errorf("a proof after the stop was answered %s, %v; want its error-kind stopping", raw, err)
}
// A proof arriving after the stop, or cut at the bound, is answered nothing (design 47 H2 §813 (12), H-A-369): the // A proof arriving after the stop, or cut at the bound, is answered nothing (design 47 H2 §813 (12), H-A-369): the
// answerer handles proofs in order, so the first reply on one inbox after a cut proof and a whole one is the whole one's. // answerer handles proofs in order, so the first reply on one inbox after a cut proof and a whole one is the whole one's.
raw, err := nats.Connect(mt.URL(t)) raw, err := nats.Connect(mt.URL(t))
+11 -6
View File
@@ -33,16 +33,21 @@ var asks = struct {
ended chan struct{} ended chan struct{}
}{of: map[uint64]*inFlight{}} }{of: map[uint64]*inFlight{}}
// afterStop is an ask made after the stop began: it is not taken and its caller is answered nothing, never refused, so the // ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542,
// caller's kept call paces and meets the new runner (design 47 H2 §813 (12) as H-A-369 amends it). // amending H-A-369): the answer's `error-kind`, so its caller asks again and meets the new runner; never silence.
const ErrorKindStopping = "stopping"
// afterStop is an ask made after the stop began: it is not taken, and its caller is answered the typed fault stopping
// (ErrorKind), never silence, so the caller asks again after a pause and meets the new runner (design 47 H2 §813 (12) as
// H-A-369 and H-A-542 amend it).
type afterStop struct{ module, verb string } type afterStop struct{ module, verb string }
func (e *afterStop) Error() string { func (e *afterStop) Error() string {
return fmt.Sprintf("%s's %s was not taken: the tool runner is stopping, and a call after its stop is answered nothing", e.module, e.verb) return fmt.Sprintf("%s's %s was not taken: the tool runner is stopping; ask again, the new runner answers it", e.module, e.verb)
} }
// Unanswered says the call is left without an answer, so its kept call asks again and meets the new runner. // ErrorKind is the typed fault the call is answered with: stopping.
func (e *afterStop) Unanswered() bool { return true } func (e *afterStop) ErrorKind() string { return ErrorKindStopping }
// cutCall is a call the drain cut at its bound. Its caller is answered nothing, so it sees a transport fault (§813 (12)): the // cutCall is a call the drain cut at its bound. Its caller is answered nothing, so it sees a transport fault (§813 (12)): the
// bus reads Unanswered and sends no reply. // bus reads Unanswered and sends no reply.
@@ -55,7 +60,7 @@ func (e *cutCall) Error() string {
// Unanswered says the call is left without an answer, so its caller sees a transport fault. // Unanswered says the call is left without an answer, so its caller sees a transport fault.
func (e *cutCall) Unanswered() bool { return true } func (e *cutCall) Unanswered() bool { return true }
// begin records an ask about to be made; once the runner drains it takes none, and its caller is answered nothing. // begin records an ask about to be made; once the runner drains it takes none, and its caller is answered stopping.
func begin(module, verb string) (uint64, *inFlight, error) { func begin(module, verb string) (uint64, *inFlight, error) {
asks.mu.Lock() asks.mu.Lock()
defer asks.mu.Unlock() defer asks.mu.Unlock()
+17 -4
View File
@@ -34,6 +34,16 @@ func TestALostWriteIsAnsweredWithItsOwnCodeAndNothingElseIs(t *testing.T) {
if got := errorCode(errors.New("issues keeps no state numbers")); got == CodeChanged || got != -32000 { if got := errorCode(errors.New("issues keeps no state numbers")); got == CodeChanged || got != -32000 {
t.Errorf("a plain refusal is answered %d", got) t.Errorf("a plain refusal is answered %d", got)
} }
// A typed fault reaches the bundle as its kind in the error's data, wrapped or not (design 47 H2 H-A-542); a plain
// refusal carries none.
for _, err := range []error{&afterStop{module: "registry", verb: "publish"}, fmt.Errorf("asked: %w", &afterStop{module: "registry", verb: "publish"})} {
if data, _ := errorAnswer(err)["data"].(map[string]any); data["error-kind"] != ErrorKindStopping {
t.Errorf("%v is answered %v, want its error-kind stopping", err, errorAnswer(err))
}
}
if _, has := errorAnswer(errors.New("issues keeps no state numbers"))["data"]; has {
t.Error("a plain refusal is answered an error-kind")
}
} }
// A bundle that answers the handshake and an empty tool list, and notes each start and its pid in $OUT. // A bundle that answers the handshake and an empty tool list, and notes each start and its pid in $OUT.
@@ -55,7 +65,7 @@ done
// itself is stopping, none is, by a restart or by a call. // itself is stopping, none is, by a restart or by a call.
// //
// **The tool runner drains at a stop** (design 47 H2 §813 (12), H-A-342 (2), H-A-353, H-A-369): once it drains, no new call is // **The tool runner drains at a stop** (design 47 H2 §813 (12), H-A-342 (2), H-A-353, H-A-369): once it drains, no new call is
// taken and its caller is answered nothing (Unanswered), never refused; a call // taken and its caller is answered the typed fault stopping (H-A-542), never silence; a call
// in flight that ends within the drain bound is answered; one still in flight at the bound is cut, said with its bundle, its verb // in flight that ends within the drain bound is answered; one still in flight at the bound is cut, said with its bundle, its verb
// and its age, and left unanswered (Unanswered, so the bus sends its caller nothing: a transport fault). // and its age, and left unanswered (Unanswered, so the bus sends its caller nothing: a transport fault).
func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) { func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
@@ -71,7 +81,7 @@ func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
}{ }{
{"a bundle that dies while the runtime serves", false, 2, 0, false}, {"a bundle that dies while the runtime serves", false, 2, 0, false},
{"a bundle signalled with the stopping runtime", true, 1, 0, false}, {"a bundle signalled with the stopping runtime", true, 1, 0, false},
{"a call that ends within the drain bound is answered, and a call after the stop is answered nothing", false, 0, 10 * time.Second, true}, {"a call that ends within the drain bound is answered, and a call after the stop is answered stopping", false, 0, 10 * time.Second, true},
{"a call that outlives the drain bound is cut and said", false, 0, 50 * time.Millisecond, false}, {"a call that outlives the drain bound is cut and said", false, 0, 50 * time.Millisecond, false},
} { } {
t.Run(row.name, func(t *testing.T) { t.Run(row.name, func(t *testing.T) {
@@ -130,9 +140,12 @@ func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
} }
} }
if row.release { if row.release {
// A call made after the stop is answered the typed fault stopping, never silence (H-A-542).
var kinded interface{ ErrorKind() string }
var unanswered interface{ Unanswered() bool } var unanswered interface{ Unanswered() bool }
if _, err := run("", json.RawMessage(`{}`)); !errors.As(err, &unanswered) || !unanswered.Unanswered() { if _, err := run("", json.RawMessage(`{}`)); !errors.As(err, &kinded) || kinded.ErrorKind() != ErrorKindStopping ||
t.Errorf("a call made after the stop ended %v, want it answered nothing, never refused", err) errors.As(err, &unanswered) {
t.Errorf("a call made after the stop ended %v, want it answered stopping, never silence", err)
} }
f, err := os.OpenFile(hold, os.O_WRONLY, 0) f, err := os.OpenFile(hold, os.O_WRONLY, 0)
if err != nil { if err != nil {
+13 -2
View File
@@ -634,8 +634,7 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an
return return
} }
if err != nil { if err != nil {
_ = c.write(map[string]any{"jsonrpc": "2.0", "id": m.ID, _ = c.write(map[string]any{"jsonrpc": "2.0", "id": m.ID, "error": errorAnswer(err)})
"error": map[string]any{"code": errorCode(err), "message": err.Error()}})
return return
} }
_ = c.write(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": result}) _ = c.write(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": result})
@@ -988,6 +987,18 @@ func (c *child) deathReason() error {
// stdio.CodeChanged): told by its code, never by its words (novox/hq ADR 0259). // stdio.CodeChanged): told by its code, never by its words (novox/hq ADR 0259).
const CodeChanged = -32010 const CodeChanged = -32010
// errorAnswer is the JSON-RPC error a bundle is answered err with: its code and words, and, when err names a typed fault
// (ErrorKind() string: a stopping tool runner's stopping, design 47 H2 H-A-542), that kind as the error's data
// `error-kind`, so mesh-sdk's stdio tells it by its kind, never by its words.
func errorAnswer(err error) map[string]any {
answer := map[string]any{"code": errorCode(err), "message": err.Error()}
var kinded interface{ ErrorKind() string }
if errors.As(err, &kinded) && kinded.ErrorKind() != "" {
answer["data"] = map[string]any{"error-kind": kinded.ErrorKind()}
}
return answer
}
// errorCode is the code an error is answered to a bundle with: a lost compare-and-set's own, else the // errorCode is the code an error is answered to a bundle with: a lost compare-and-set's own, else the
// general one. // general one.
func errorCode(err error) int { func errorCode(err error) int {