From 78a0c4d154363036fd90cc5bb931d7e8800b88d6 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 11:45:40 +0100 Subject: [PATCH 1/5] stdio: a stopping tool runner's fault reaches the bundle as such and is asked again (H-A-542) A call a stopping tool runner did not take reached a bundle as a bare words error, or, before the runner said so, as a "timeout", so a build step's publish failed when the runner restarted (main.440). The runtime's answer now names its typed fault (the error's data error-kind); stdio keeps it (ErrStopping, ErrorKind), and Ask asks again after a pause at the kept-call pace until the new runner answers or Ask's own bound ends, then hands the caller ErrStopping. A plain refusal and a bare timeout are never asked again. Builds: design 47 H2 H-A-542 --- go/stdio.go | 51 +++++++++++++++++++++++++--- go/stdio_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/go/stdio.go b/go/stdio.go index 4fb2ee17..523dcc69 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -55,6 +55,10 @@ type message struct { Error *struct { Code int `json:"code"` Message string `json:"message"` + // Data carries a typed fault's kind, `error-kind` (a stopping tool runner's ErrorKindStopping, design 47 H2 H-A-542). + Data struct { + ErrorKind string `json:"error-kind"` + } `json:"data"` } `json:"error,omitempty"` } @@ -132,7 +136,7 @@ func (c *channel) askAs(id, method string, params any, timeout time.Duration) (j select { case m := <-answer: if m.Error != nil { - return nil, &refused{message: m.Error.Message, code: m.Error.Code} + return nil, &refused{message: m.Error.Message, code: m.Error.Code, kind: m.Error.Data.ErrorKind} } return m.Result, nil case <-time.After(timeout): @@ -147,15 +151,31 @@ func (c *channel) askAs(id, method string, params any, timeout time.Duration) (j type refused struct { message string code int + // kind is the typed fault the answer names, its `error-kind` (ErrorKindStopping), "" for none. + kind string } func (r *refused) Error() string { return r.message } -// Is makes a compare-and-set the runtime refused because another writer was first ErrChanged. +// Is makes a compare-and-set the runtime refused because another writer was first ErrChanged, and a call a stopping tool +// runner did not take ErrStopping. func (r *refused) Is(target error) bool { - return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod + return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod || + target == ErrStopping && r.kind == ErrorKindStopping } +// ErrorKind is the typed fault the runtime's answer named (ErrorKindStopping), "" for none: a reader that does not import +// this package tells it by this method, never by the words. +func (r *refused) ErrorKind() string { return r.kind } + +// ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542): +// the answer's `error-kind`. Ask asks again after a pause until the new runner answers or its bound ends. +const ErrorKindStopping = "stopping" + +// ErrStopping is a call a stopping tool runner did not take (ErrorKindStopping): nothing was done, and the new runner answers +// it. Told apart by its kind, never by its words. +var ErrStopping = errors.New("the tool runner is stopping") + // CodeNoMethod is the error code the runtime answers a verb it does not have with (JSON-RPC's method not found). const CodeNoMethod = -32601 @@ -583,9 +603,32 @@ func Ask(key string, body any) (json.RawMessage, error) { if err != nil { return nil, err } - return c.ask("mesh/ask", map[string]any{"key": key, "body": body}, 60*time.Second) + // A call a stopping tool runner did not take is asked again after a pause, at the kept-call pace, until the new runner + // answers or Ask's own bound ends; then its fault is the caller's as such, ErrStopping (design 47 H2 H-A-542). + start, wait := askNow(), busretry.KeptPace().First + for { + left := askBound - askNow().Sub(start) + answer, err := c.ask("mesh/ask", map[string]any{"key": key, "body": body}, left) + if !errors.Is(err, ErrStopping) { + return answer, err + } + if left = askBound - askNow().Sub(start); wait >= left { + return nil, fmt.Errorf("%s: the tool runner was stopping, asked again until Ask's bound of %s: %w", key, askBound, err) + } + fmt.Fprintf(os.Stderr, "[mesh-sdk] %s: the tool runner is stopping (%v); asked again in %s\n", key, err, wait) + pause(wait) + wait = min(wait*2, busretry.KeptPace().Max) + } } +// askBound is how long Ask waits for its answer, asks again included; pause is how it waits between two asks and askNow its +// clock, a test's to replace so nothing sleeps. +var ( + askBound = 60 * time.Second + pause = time.Sleep + askNow = time.Now +) + // TopicMatches says whether an event's type matches a pattern: `*` is one dot-separated segment, `#` // (or `**`) any number, as the TypeScript SDK matches; the bus's own `>` as the last token matches one or more // further tokens (design 47 G A-101), so an adopted follower's `..>` hears every event on the seat's event. diff --git a/go/stdio_test.go b/go/stdio_test.go index cb462e73..0f915f1c 100644 --- a/go/stdio_test.go +++ b/go/stdio_test.go @@ -9,6 +9,8 @@ import ( "sync" "testing" "time" + + "git.novox.be/novox/mesh-sdk/go/busretry" ) // A Go bundle serves as the module the runtime names, answers calls concurrently, and emits through @@ -413,3 +415,88 @@ func subABundleAsksAnAcknowledgedPublishAndASubscriptionForNewMessages(t *testin } _ = inW.Close() } + +// A call a stopping tool runner did not take reaches the bundle as the typed fault stopping and is asked again after a pause, +// at the kept-call pace, until the new runner answers; past Ask's bound the fault is the caller's as such, ErrStopping; a +// plain refusal, and a bare timeout, are never asked again (design 47 H2 H-A-542). Nothing sleeps: the pause moves a clock. +func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) { + servesAlone(t) + t.Setenv(ServedAs, "builder") + clock := time.Unix(0, 0) + var paused []time.Duration + wasPause, wasNow, wasBound := pause, askNow, askBound + t.Cleanup(func() { pause, askNow, askBound = wasPause, wasNow, wasBound }) + pause = func(d time.Duration) { paused = append(paused, d); clock = clock.Add(d) } + askNow = func() time.Time { return clock } + askBound = 10 * time.Second + inR, inW := io.Pipe() + outR, outW := io.Pipe() + go func() { + _ = ServeOn("", []Tool{{Name: "publish", Run: func(map[string]any) (any, error) { + answer, err := Ask("seat:go-package-registry.publish", map[string]any{}) + if err != nil { + return map[string]any{"stopping": errors.Is(err, ErrStopping), "words": err.Error()}, nil + } + return json.RawMessage(answer), nil + }}}, inR, outW) + }() + lines := bufio.NewScanner(outR) + next := func() map[string]any { + done := make(chan map[string]any, 1) + go func() { + if lines.Scan() { + var m map[string]any + _ = json.Unmarshal(lines.Bytes(), &m) + done <- m + } + }() + select { + case m := <-done: + return m + case <-time.After(3 * time.Second): + t.Fatal("the bundle said nothing") + return nil + } + } + send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) } + stopping := map[string]any{"code": -32000, "message": "registry's publish was not taken: the tool runner is stopping", + "data": map[string]any{"error-kind": "stopping"}} + for i, row := range []struct { + name string + answers []map[string]any // the runtime's answers to the asks, in order: an error, or a result + asks int + want string + }{ + {"answered by the new runner after two stoppings", []map[string]any{{"error": stopping}, {"error": stopping}, {"result": "published"}}, 3, + `"published"`}, + {"stopping past the bound", []map[string]any{{"error": stopping}, {"error": stopping}, {"error": stopping}}, 3, + `"stopping":true`}, + {"a plain refusal", []map[string]any{{"error": map[string]any{"code": -32000, "message": "refused"}}}, 1, `"stopping":false`}, + {"a bare timeout", []map[string]any{{"error": map[string]any{"code": -32000, "message": "timeout"}}}, 1, `"words":"timeout"`}, + } { + t.Run(row.name, func(t *testing.T) { + clock, paused = time.Unix(0, 0), nil + t.Cleanup(busretry.Waits(time.Second, 2*time.Second, 4*time.Second)) + send(map[string]any{"jsonrpc": "2.0", "id": 100 + i, "method": "tools/call", "params": map[string]any{"name": "publish"}}) + asks := 0 + for _, a := range row.answers { + m := next() + if m["method"] != "mesh/ask" { + t.Fatalf("the bundle said %v, want an ask", m) + } + asks++ + a["jsonrpc"], a["id"] = "2.0", m["id"] + send(a) + } + r := next() + text, _ := json.Marshal(r["result"]) + if r["id"] != float64(100+i) || asks != row.asks || !strings.Contains(string(text), strings.ReplaceAll(row.want, `"`, `\"`)) { + t.Errorf("asked %d times, answered %v; want %d asks and %s", asks, r, row.asks, row.want) + } + if want := row.asks - 1; len(paused) != want || (want >= 2 && paused[1] != 2*paused[0]) { + t.Errorf("paused %v, want %d pauses at the kept-call pace", paused, want) + } + }) + } + _ = inW.Close() +} From 6459dee6aae0cfa24e17ec78fffe4ef782458314 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 11:51:56 +0100 Subject: [PATCH 2/5] stdio (TypeScript): a stopping tool runner's fault reaches the bundle as such and is asked again (H-A-544) As in the Go stdio: the runtime's answer names its typed fault in the error's data error-kind, kept on the error as errorKind (isStopping); a broker request (mesh/ask) met by stopping is asked again after a pause at the kept-call pace (the settings kept-call-pace-first and -max, 1 s doubling to 30 s by default) until the new runner answers or 60 s end, then the fault is the caller's. A plain refusal is never asked again; a TypeScript bundle (the forge, the registries' holder) meets a restarting runner as a Go one does. Builds: design 47 H2 H-A-542, H-A-544 --- src/stdio/index.ts | 66 ++++++++++++++++++++++++++++++-- test/fixtures/stdio-stopping.mjs | 16 ++++++++ test/stdio.test.ts | 46 ++++++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/stdio-stopping.mjs diff --git a/src/stdio/index.ts b/src/stdio/index.ts index dc2f6528..a6dacdea 100644 --- a/src/stdio/index.ts +++ b/src/stdio/index.ts @@ -22,6 +22,59 @@ import { runAs } from "../callers/index.js"; /** The protocol version this speaks; the runtime says the same. */ export const PROTOCOL = "2025-03-26"; +/** The typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542): the runtime's + * answer's `error-kind`, kept on the error as `errorKind`. */ +export const ERROR_KIND_STOPPING = "stopping"; + +/** Whether err is a call a stopping tool runner did not take: told by its kind, never by its words (H-A-542). */ +export function isStopping(err: unknown): boolean { + return typeof err === "object" && err !== null && (err as { errorKind?: unknown }).errorKind === ERROR_KIND_STOPPING; +} + +/** How long a call is asked again while the tool runner is stopping: the Go stdio Ask's bound (H-A-544). */ +const STOPPING_BOUND_MS = 60_000; + +/** A duration setting as the node-engine composes it (MESH_CONFIG_, design 47 H2 H-A-370), in milliseconds: Go's + * duration text ("1s", "250ms", "1m30s"); an absent setting is its default, one that is no duration is an error naming it. */ +function settingMs(key: string, fallback: number): number { + const word = process.env[`MESH_CONFIG_${key.toUpperCase().replace(/[-.]/g, "_")}`]; + if (word === undefined || word === "") return fallback; + const unit: Record = { ns: 1e-6, us: 1e-3, "µs": 1e-3, ms: 1, s: 1000, m: 60_000, h: 3_600_000 }; + let ms = 0; + let rest = word.trim(); + while (rest !== "") { + const part = /^(\d+(?:\.\d+)?)(ns|us|µs|ms|s|m|h)/.exec(rest); + if (!part) throw new Error(`the setting ${key} (${word}) is no duration`); + ms += Number(part[1]) * unit[part[2]]; + rest = rest.slice(part[0].length); + } + return ms; +} + +/** Asks once and again, while the answer is the typed fault stopping, after a pause at the kept-call pace (the settings + * kept-call-pace-first and kept-call-pace-max, 1 s doubling to 30 s by default, as the Go stdio's Ask), until the new + * runner answers or the bound ends; then the stopping fault is the caller's as such (H-A-542, H-A-544). A plain refusal + * and a bare timeout are never asked again. */ +async function askAgainWhileStopping(key: string, once: () => Promise): Promise { + const start = Date.now(); + let wait = settingMs("kept-call-pace-first", 1000); + const most = settingMs("kept-call-pace-max", 30_000); + for (;;) { + try { + return await once(); + } catch (err) { + const left = STOPPING_BOUND_MS - (Date.now() - start); + if (!isStopping(err) || wait >= left) { + if (isStopping(err)) (err as Error).message = `${key}: the tool runner was stopping, asked again until the bound of 60s: ${(err as Error).message}`; + throw err; + } + process.stderr.write(`[mesh-sdk] ${key}: the tool runner is stopping (${(err as Error).message}); asked again in ${wait}ms\n`); + await new Promise((r) => setTimeout(r, wait)); + wait = Math.min(wait * 2, most); + } + } +} + interface Request { jsonrpc: string; id?: number | string | null; @@ -108,8 +161,10 @@ function startSession(): Session { throw new Error(`the bus did not acknowledge ${env.key}: ${String(answer.error)}`); } }, + // A call a stopping tool runner did not take is asked again after a pause until the new runner answers or the bound + // ends, as the Go stdio's Ask does (design 47 H2 H-A-542, H-A-544). request: (async (key: string, body: Req): Promise => - (await ask("mesh/ask", { key, body })) as Res) as Broker["request"], + (await askAgainWhileStopping(key, () => ask("mesh/ask", { key, body }))) as Res) as Broker["request"], handle: (async () => { throw new Error("a launched bundle answers through its tools, not by binding subjects (novox/hq ADR 0193)"); }) as Broker["handle"], @@ -230,11 +285,16 @@ function startSession(): Session { continue; // not framed; nobody to answer } // The runtime's answer to something this bundle asked. - const reply = request as unknown as { id?: string; method?: string; result?: unknown; error?: { message?: string; code?: number } }; + const reply = request as unknown as { + id?: string; method?: string; result?: unknown; + error?: { message?: string; code?: number; data?: { "error-kind"?: string } }; + }; if (reply.method === undefined && typeof reply.id === "string" && awaiting.has(reply.id)) { const waiting = awaiting.get(reply.id)!; awaiting.delete(reply.id); - if (reply.error) waiting.reject(Object.assign(new Error(reply.error.message ?? "the runtime refused"), { code: reply.error.code })); + // A typed fault rides as the error's data `error-kind` (a stopping tool runner's stopping, H-A-542): kept as errorKind. + if (reply.error) waiting.reject(Object.assign(new Error(reply.error.message ?? "the runtime refused"), + { code: reply.error.code, errorKind: reply.error.data?.["error-kind"] })); else waiting.resolve(reply.result); continue; } diff --git a/test/fixtures/stdio-stopping.mjs b/test/fixtures/stdio-stopping.mjs new file mode 100644 index 00000000..bb2f39ab --- /dev/null +++ b/test/fixtures/stdio-stopping.mjs @@ -0,0 +1,16 @@ +// A launched bundle whose one tool asks a seat's verb through the runtime, and says how the ask ended: its answer, or +// whether its fault was the typed fault stopping (design 47 H2 H-A-542, H-A-544). +import { broker } from "../../dist/messaging/index.js"; +import { registerModuleTools } from "../../dist/tools/index.js"; +import { serveRegisteredOverStdio, isStopping } from "../../dist/stdio/index.js"; + +registerModuleTools("builder", () => [ + { name: "publish", description: "asks the registry", input: {}, run: async () => { + try { + return { answer: await broker().request("seat:go-package-registry.publish", {}) }; + } catch (err) { + return { stopping: isStopping(err), words: err.message }; + } + } }, +]); +await serveRegisteredOverStdio(); diff --git a/test/stdio.test.ts b/test/stdio.test.ts index 583409bb..9d82bf78 100644 --- a/test/stdio.test.ts +++ b/test/stdio.test.ts @@ -378,3 +378,49 @@ async function aBundlesFindingWriter(): Promise { child.kill(); } } + +test("a call a stopping tool runner did not take is asked again after a pause until the new runner answers; a plain refusal is not (design 47 H2 H-A-542, H-A-544)", async () => { + const child = spawn(process.execPath, [fileURLToPath(new URL("./fixtures/stdio-stopping.mjs", import.meta.url))], + { stdio: ["pipe", "pipe", "pipe"], env: { ...process.env, MESH_SERVED_MODULE: "builder", + MESH_CONFIG_KEPT_CALL_PACE_FIRST: "1ms", MESH_CONFIG_KEPT_CALL_PACE_MAX: "2ms" } }); + const seen: any[] = []; + let buffered = ""; + let said = ""; + child.stderr.on("data", (d) => (said += d.toString())); + child.stdout.on("data", (d) => { + buffered += d.toString(); + let at: number; + while ((at = buffered.indexOf("\n")) >= 0) { + seen.push(JSON.parse(buffered.slice(0, at))); + buffered = buffered.slice(at + 1); + } + }); + const until = async (match: (m: any) => boolean) => { + for (let i = 0; i < 150 && !seen.some(match); i++) await new Promise((r) => setTimeout(r, 20)); + return seen.find(match); + }; + const send = (m: unknown) => child.stdin.write(JSON.stringify(m) + "\n"); + const stopping = { code: -32000, message: "registry's publish was not taken: the tool runner is stopping", data: { "error-kind": "stopping" } }; + try { + send({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26", capabilities: {} } }); + await until((m) => m.id === 1); + for (const row of [ + { id: 2, answers: [{ error: stopping }, { error: stopping }, { result: { published: true } }], want: { answer: { published: true } } }, + { id: 3, answers: [{ error: { code: -32000, message: "refused" } }], want: { stopping: false, words: "refused" } }, + ]) { + send({ jsonrpc: "2.0", id: row.id, method: "tools/call", params: { name: "publish", arguments: {} } }); + for (const a of row.answers) { + const asked = await until((m) => m.method === "mesh/ask" && !m.answered); + assert.ok(asked, `the bundle did not ask again (row ${row.id})`); + asked.answered = true; + send({ jsonrpc: "2.0", id: asked.id, ...a }); + } + const done = await until((m) => m.id === row.id); + assert.deepEqual(JSON.parse(done.result.content[0].text), row.want); + assert.equal(seen.filter((m) => m.method === "mesh/ask" && !m.answered).length, 0, `asked more than ${row.answers.length} times`); + } + assert.match(said, /the tool runner is stopping/); + } finally { + child.kill(); + } +}); From fdb85e8bd5fe492c9e9e9c8403a5c07feb5caab1 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 12:05:15 +0100 Subject: [PATCH 3/5] process, stdio: a kept call paces on a stopping tool runner; the ask's bound is a setting (H-A-549) Review round 1: the process caller read only a reply's error, so a kept call (CallKept, e.g. seat:git.trees) ended at once on stopping instead of pacing until the new runner answers. requestAsk now reads error-kind stopping as busretry.Stopping, which busretry.Fault counts as a transport fault, as away. The Go stdio Ask's 60 s bound becomes the setting ask-bound (default 60 s), read by the settings reader. Builds: design 47 H2 H-A-542, H-A-549 --- go/busretry/busretry.go | 18 ++++++++++++++++-- go/busretry/busretry_test.go | 4 ++++ go/process/call.go | 20 +++++++++++++++----- go/process/call_test.go | 9 +++++++-- go/stdio.go | 24 +++++++++++++++++------- go/stdio_test.go | 7 ++++--- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/go/busretry/busretry.go b/go/busretry/busretry.go index 52daccef..2b205d5f 100644 --- a/go/busretry/busretry.go +++ b/go/busretry/busretry.go @@ -163,12 +163,26 @@ func (u *Unanswered) Error() string { func (u *Unanswered) Unwrap() error { return u.Last } // Fault says err is a transport fault of a kept call (§775): a request that reached no one, a call's own deadline -// passing, or a holder's answer of the kind away (§776); a callee's refusal never is. +// passing, a holder's answer of the kind away (§776), or a stopping tool runner's answer of the kind stopping (design 47 +// H2 H-A-542, H-A-549); a callee's refusal never is. func Fault(err error) bool { var away *Away - return TimedOut(err) || Undelivered(err) || errors.As(err, &away) + var stopping *Stopping + return TimedOut(err) || Undelivered(err) || errors.As(err, &away) || errors.As(err, &stopping) } +// ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 +// H-A-542): its reply's `error-kind`. Nothing was done, and the new runner answers the call. +const ErrorKindStopping = "stopping" + +// Stopping is a tool runner's answer of the kind stopping, read as an error: the runner's words. +type Stopping struct{ Words string } + +func (s *Stopping) Error() string { return s.Words } + +// ErrorKind is the typed fault the answer named: stopping. +func (s *Stopping) ErrorKind() string { return ErrorKindStopping } + // ErrorKindAway is the seat error kind a holder answers when a part it depends on is away (design 47 H2 §776): its // answer's `error-kind`, beside `part` (what did not answer) and `words`. A caller reads it as a transport fault. const ErrorKindAway = "away" diff --git a/go/busretry/busretry_test.go b/go/busretry/busretry_test.go index d805b115..36a9f2bf 100644 --- a/go/busretry/busretry_test.go +++ b/go/busretry/busretry_test.go @@ -179,6 +179,10 @@ func subAKeptCallIsPaced(t *testing.T) { !strings.Contains(string(raw), `"error-kind":"away"`) { t.Fatalf("%s read as %+v", raw, a) } + // A stopping tool runner's answer is a transport fault too (design 47 H2 H-A-549), wrapped or not. + if !Fault(&Stopping{Words: "not taken"}) || !Fault(fmt.Errorf("asked: %w", &Stopping{Words: "not taken"})) { + t.Fatal("a stopping tool runner's answer is no transport fault") + } if AwayIn([]byte(`{"error":"no","error-kind":"refused"}`)) != nil || AwayIn([]byte(`[1]`)) != nil || Fault(&Answered{Words: "away"}) { t.Fatal("a refusal reads as away") } diff --git a/go/process/call.go b/go/process/call.go index 8cf84bd9..6a421ab0 100644 --- a/go/process/call.go +++ b/go/process/call.go @@ -189,6 +189,15 @@ func callWith(request func(string, []byte) (json.RawMessage, error), blackout fu return busretry.Do("the call on "+fallback, askBoth(request, called, fallback, data), blackout) } +// replyError is the error a reply naming one is the call's: a stopping tool runner's kind stopping is a transport fault a +// kept call's pacing asks again on, as away is (design 47 H2 H-A-542, H-A-549); any other is the callee's answer. +func replyError(words, kind string) error { + if kind == busretry.ErrorKindStopping { + return &busretry.Stopping{Words: words} + } + return answered(words) +} + // answered is the error a callee answered with: delivered, never asked again (busretry.Answered). func answered(words string) error { return &busretry.Answered{Words: words} } @@ -272,16 +281,17 @@ func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMess return nil, err } var r struct { - Result json.RawMessage `json:"result"` - Error string `json:"error"` - Node string `json:"node"` - Held *approval.Held `json:"held"` + Result json.RawMessage `json:"result"` + Error string `json:"error"` + ErrorKind string `json:"error-kind"` + Node string `json:"node"` + Held *approval.Held `json:"held"` } if err := json.Unmarshal(msg.Data, &r); err != nil { return nil, fmt.Errorf("%s answered something that is not a seat's answer: %w", subject, err) } if r.Error != "" { - return nil, answered(r.Error) + return nil, replyError(r.Error, r.ErrorKind) } if r.Held != nil { return c.awaitGated(subject, *r.Held) diff --git a/go/process/call_test.go b/go/process/call_test.go index 093ae36f..f841e0ed 100644 --- a/go/process/call_test.go +++ b/go/process/call_test.go @@ -142,20 +142,25 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { t.Errorf("%s: asked %d times (%v), want %d", row.name, asks, err, row.asks) } } - // Design 47 H2 §776: a holder's answer of the kind away is a transport fault, called again at the pace; a refusal - // of another kind is the caller's answer at once. + // Design 47 H2 §776: a holder's answer of the kind away is a transport fault, called again at the pace; so is a + // stopping tool runner's reply of the kind stopping (H-A-542, H-A-549); a refusal of another kind is the caller's + // answer at once. for _, row := range []struct { name string answers []string asks int }{ {"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2}, + {"stopping, then answered by the new runner", []string{"stopping", `{"merged":true}`}, 2}, {"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1}, } { asks := 0 request := func(string, []byte) (json.RawMessage, error) { a := row.answers[min(asks, len(row.answers)-1)] asks++ + if a == "stopping" { + return nil, replyError("git's trees was not taken: the tool runner is stopping", "stopping") + } return json.RawMessage(a), nil } got, err := callKeptWith(busretry.Kept{Seat: "git", Verb: "merge-pull"}, request, "called", "fallback", nil) diff --git a/go/stdio.go b/go/stdio.go index 523dcc69..d06b6d30 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -170,7 +170,7 @@ func (r *refused) ErrorKind() string { return r.kind } // ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542): // the answer's `error-kind`. Ask asks again after a pause until the new runner answers or its bound ends. -const ErrorKindStopping = "stopping" +const ErrorKindStopping = busretry.ErrorKindStopping // ErrStopping is a call a stopping tool runner did not take (ErrorKindStopping): nothing was done, and the new runner answers // it. Told apart by its kind, never by its words. @@ -604,7 +604,12 @@ func Ask(key string, body any) (json.RawMessage, error) { return nil, err } // A call a stopping tool runner did not take is asked again after a pause, at the kept-call pace, until the new runner - // answers or Ask's own bound ends; then its fault is the caller's as such, ErrStopping (design 47 H2 H-A-542). + // answers or Ask's own bound (the setting ask-bound) ends; then its fault is the caller's as such, ErrStopping (design 47 H2 + // H-A-542, H-A-549). + askBound, err := SettingDuration(AskBoundSetting, DefaultAskBound) + if err != nil { + return nil, err + } start, wait := askNow(), busretry.KeptPace().First for { left := askBound - askNow().Sub(start) @@ -621,12 +626,17 @@ func Ask(key string, body any) (json.RawMessage, error) { } } -// askBound is how long Ask waits for its answer, asks again included; pause is how it waits between two asks and askNow its -// clock, a test's to replace so nothing sleeps. +// AskBoundSetting is the module's setting of how long Ask waits for its answer, asks again included, a duration text; +// DefaultAskBound is its default (design 47 H2 H-A-549). +const ( + AskBoundSetting = "ask-bound" + DefaultAskBound = 60 * time.Second +) + +// pause is how Ask waits between two asks and askNow its clock, a test's to replace so nothing sleeps. var ( - askBound = 60 * time.Second - pause = time.Sleep - askNow = time.Now + pause = time.Sleep + askNow = time.Now ) // TopicMatches says whether an event's type matches a pattern: `*` is one dot-separated segment, `#` diff --git a/go/stdio_test.go b/go/stdio_test.go index 0f915f1c..f366988e 100644 --- a/go/stdio_test.go +++ b/go/stdio_test.go @@ -424,11 +424,12 @@ func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) t.Setenv(ServedAs, "builder") clock := time.Unix(0, 0) var paused []time.Duration - wasPause, wasNow, wasBound := pause, askNow, askBound - t.Cleanup(func() { pause, askNow, askBound = wasPause, wasNow, wasBound }) + wasPause, wasNow := pause, askNow + t.Cleanup(func() { pause, askNow = wasPause, wasNow }) pause = func(d time.Duration) { paused = append(paused, d); clock = clock.Add(d) } askNow = func() time.Time { return clock } - askBound = 10 * time.Second + // The bound is the setting ask-bound (H-A-549), here 10 s for its default 60 s. + t.Setenv("MESH_CONFIG_ASK_BOUND", "10s") inR, inW := io.Pipe() outR, outW := io.Pipe() go func() { From 07bfc71c33950f200fda326edad0c1a0b81b02ba Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 12:05:15 +0100 Subject: [PATCH 4/5] stdio (TypeScript): the ask's bound is the setting ask-bound; the pause has a seam (H-A-549) Review round 1: the bound of asking again while the tool runner is stopping is the setting ask-bound (default 60 s), and the pause and the clock are askPacing, which the test replaces as the Go test does pause and askNow, so nothing sleeps. Rows added: past the bound the fault is the caller's, a bare timeout stays a failure. Builds: design 47 H2 H-A-542, H-A-544, H-A-549 --- src/stdio/index.ts | 24 +++++++++++++++++------- test/fixtures/stdio-stopping.mjs | 16 ++++++++++++---- test/stdio.test.ts | 13 +++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/stdio/index.ts b/src/stdio/index.ts index a6dacdea..f602e979 100644 --- a/src/stdio/index.ts +++ b/src/stdio/index.ts @@ -31,8 +31,17 @@ export function isStopping(err: unknown): boolean { return typeof err === "object" && err !== null && (err as { errorKind?: unknown }).errorKind === ERROR_KIND_STOPPING; } -/** How long a call is asked again while the tool runner is stopping: the Go stdio Ask's bound (H-A-544). */ -const STOPPING_BOUND_MS = 60_000; +/** The setting of how long a call is asked again while the tool runner is stopping, and its default: the Go stdio Ask's + * (H-A-544, H-A-549). */ +export const ASK_BOUND_SETTING = "ask-bound"; +const DEFAULT_ASK_BOUND_MS = 60_000; + +/** How a call waits between two asks while the tool runner is stopping, and its clock: a test's to replace, as the Go + * stdio's pause and askNow are, so nothing sleeps. */ +export const askPacing = { + pause: (ms: number): Promise => new Promise((r) => setTimeout(r, ms)), + now: (): number => Date.now(), +}; /** A duration setting as the node-engine composes it (MESH_CONFIG_, design 47 H2 H-A-370), in milliseconds: Go's * duration text ("1s", "250ms", "1m30s"); an absent setting is its default, one that is no duration is an error naming it. */ @@ -53,23 +62,24 @@ function settingMs(key: string, fallback: number): number { /** Asks once and again, while the answer is the typed fault stopping, after a pause at the kept-call pace (the settings * kept-call-pace-first and kept-call-pace-max, 1 s doubling to 30 s by default, as the Go stdio's Ask), until the new - * runner answers or the bound ends; then the stopping fault is the caller's as such (H-A-542, H-A-544). A plain refusal + * runner answers or the bound (the setting ask-bound, 60 s by default) ends; then the stopping fault is the caller's as such (H-A-542, H-A-544). A plain refusal * and a bare timeout are never asked again. */ async function askAgainWhileStopping(key: string, once: () => Promise): Promise { - const start = Date.now(); + const start = askPacing.now(); + const bound = settingMs(ASK_BOUND_SETTING, DEFAULT_ASK_BOUND_MS); let wait = settingMs("kept-call-pace-first", 1000); const most = settingMs("kept-call-pace-max", 30_000); for (;;) { try { return await once(); } catch (err) { - const left = STOPPING_BOUND_MS - (Date.now() - start); + const left = bound - (askPacing.now() - start); if (!isStopping(err) || wait >= left) { - if (isStopping(err)) (err as Error).message = `${key}: the tool runner was stopping, asked again until the bound of 60s: ${(err as Error).message}`; + if (isStopping(err)) (err as Error).message = `${key}: the tool runner was stopping, asked again until the bound of ${bound}ms: ${(err as Error).message}`; throw err; } process.stderr.write(`[mesh-sdk] ${key}: the tool runner is stopping (${(err as Error).message}); asked again in ${wait}ms\n`); - await new Promise((r) => setTimeout(r, wait)); + await askPacing.pause(wait); wait = Math.min(wait * 2, most); } } diff --git a/test/fixtures/stdio-stopping.mjs b/test/fixtures/stdio-stopping.mjs index bb2f39ab..84e7b17e 100644 --- a/test/fixtures/stdio-stopping.mjs +++ b/test/fixtures/stdio-stopping.mjs @@ -1,15 +1,23 @@ // A launched bundle whose one tool asks a seat's verb through the runtime, and says how the ask ended: its answer, or -// whether its fault was the typed fault stopping (design 47 H2 H-A-542, H-A-544). +// whether its fault was the typed fault stopping, and the pauses between its asks (design 47 H2 H-A-542, H-A-544, +// H-A-549). Its pause moves a clock instead of sleeping. import { broker } from "../../dist/messaging/index.js"; import { registerModuleTools } from "../../dist/tools/index.js"; -import { serveRegisteredOverStdio, isStopping } from "../../dist/stdio/index.js"; +import { serveRegisteredOverStdio, isStopping, askPacing } from "../../dist/stdio/index.js"; + +let clock = 0; +let paused = []; +askPacing.now = () => clock; +askPacing.pause = async (ms) => { paused.push(ms); clock += ms; }; registerModuleTools("builder", () => [ { name: "publish", description: "asks the registry", input: {}, run: async () => { + clock = 0; + paused = []; try { - return { answer: await broker().request("seat:go-package-registry.publish", {}) }; + return { answer: await broker().request("seat:go-package-registry.publish", {}), paused }; } catch (err) { - return { stopping: isStopping(err), words: err.message }; + return { stopping: isStopping(err), words: err.message, paused }; } } }, ]); diff --git a/test/stdio.test.ts b/test/stdio.test.ts index 9d82bf78..b39f7388 100644 --- a/test/stdio.test.ts +++ b/test/stdio.test.ts @@ -379,10 +379,10 @@ async function aBundlesFindingWriter(): Promise { } } -test("a call a stopping tool runner did not take is asked again after a pause until the new runner answers; a plain refusal is not (design 47 H2 H-A-542, H-A-544)", async () => { +test("a call a stopping tool runner did not take is asked again after a pause until the new runner answers or the bound ends; a plain refusal and a bare timeout are not (design 47 H2 H-A-542, H-A-544, H-A-549)", async () => { const child = spawn(process.execPath, [fileURLToPath(new URL("./fixtures/stdio-stopping.mjs", import.meta.url))], { stdio: ["pipe", "pipe", "pipe"], env: { ...process.env, MESH_SERVED_MODULE: "builder", - MESH_CONFIG_KEPT_CALL_PACE_FIRST: "1ms", MESH_CONFIG_KEPT_CALL_PACE_MAX: "2ms" } }); + MESH_CONFIG_KEPT_CALL_PACE_FIRST: "2s", MESH_CONFIG_KEPT_CALL_PACE_MAX: "4s", MESH_CONFIG_ASK_BOUND: "10s" } }); const seen: any[] = []; let buffered = ""; let said = ""; @@ -405,8 +405,13 @@ test("a call a stopping tool runner did not take is asked again after a pause un send({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26", capabilities: {} } }); await until((m) => m.id === 1); for (const row of [ - { id: 2, answers: [{ error: stopping }, { error: stopping }, { result: { published: true } }], want: { answer: { published: true } } }, - { id: 3, answers: [{ error: { code: -32000, message: "refused" } }], want: { stopping: false, words: "refused" } }, + // The pace and the bound are the settings (2 s doubling to 4 s, a bound of 10 s, H-A-549); the pause moves a clock. + { id: 2, answers: [{ error: stopping }, { error: stopping }, { result: { published: true } }], + want: { answer: { published: true }, paused: [2000, 4000] } }, + { id: 3, answers: [{ error: stopping }, { error: stopping }, { error: stopping }], + want: { stopping: true, words: `seat:go-package-registry.publish: the tool runner was stopping, asked again until the bound of 10000ms: ${stopping.message}`, paused: [2000, 4000] } }, + { id: 4, answers: [{ error: { code: -32000, message: "refused" } }], want: { stopping: false, words: "refused", paused: [] } }, + { id: 5, answers: [{ error: { code: -32000, message: "timeout" } }], want: { stopping: false, words: "timeout", paused: [] } }, ]) { send({ jsonrpc: "2.0", id: row.id, method: "tools/call", params: { name: "publish", arguments: {} } }); for (const a of row.answers) { From b3509e7d31fce25c2c46d5f7410f92c0e5a39861 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 12:09:05 +0100 Subject: [PATCH 5/5] process: a reply's error-kind is read from its bytes, and tested so (H-A-549 (1)) Review round 2: the row for a stopping tool runner built its fault itself, so requestAsk's reading of error-kind went untested. The reading of a reply (result, error, error-kind, held) is now readReply, and the kept-call table reads the runner's raw reply {"error": ..., "error-kind": "stopping"} through it. Builds: design 47 H2 H-A-549 (1) --- go/process/call.go | 24 +++++++++++++++++------- go/process/call_test.go | 21 +++++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/go/process/call.go b/go/process/call.go index 6a421ab0..7d790525 100644 --- a/go/process/call.go +++ b/go/process/call.go @@ -280,6 +280,19 @@ func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMess if err != nil { return nil, err } + result, held, err := readReply(subject, msg.Data) + if err != nil { + return nil, err + } + if held != nil { + return c.awaitGated(subject, *held) + } + return result, nil +} + +// readReply reads one reply ({result, error, error-kind, node, held}): a reply naming an error is the call's failure, +// of the kind stopping a transport fault a kept call paces on (design 47 H2 H-A-549 (1)); a held call names its hold. +func readReply(subject string, data []byte) (json.RawMessage, *approval.Held, error) { var r struct { Result json.RawMessage `json:"result"` Error string `json:"error"` @@ -287,14 +300,11 @@ func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMess Node string `json:"node"` Held *approval.Held `json:"held"` } - if err := json.Unmarshal(msg.Data, &r); err != nil { - return nil, fmt.Errorf("%s answered something that is not a seat's answer: %w", subject, err) + if err := json.Unmarshal(data, &r); err != nil { + return nil, nil, fmt.Errorf("%s answered something that is not a seat's answer: %w", subject, err) } if r.Error != "" { - return nil, replyError(r.Error, r.ErrorKind) + return nil, nil, replyError(r.Error, r.ErrorKind) } - if r.Held != nil { - return c.awaitGated(subject, *r.Held) - } - return r.Result, nil + return r.Result, r.Held, nil } diff --git a/go/process/call_test.go b/go/process/call_test.go index f841e0ed..2e7236aa 100644 --- a/go/process/call_test.go +++ b/go/process/call_test.go @@ -149,22 +149,31 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { name string answers []string asks int + replies bool // the answers are the runner's raw replies, read as requestAsk reads them (readReply) + want string // the call's answer, where it is not the last answer }{ - {"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2}, - {"stopping, then answered by the new runner", []string{"stopping", `{"merged":true}`}, 2}, - {"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1}, + {"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2, false, ""}, + // H-A-549 (1): the runner's reply of the kind stopping, read from its bytes, is paced on until the new runner answers. + {"stopping, then answered by the new runner", []string{`{"error":"git's trees was not taken: the tool runner is stopping","error-kind":"stopping"}`, + `{"result":{"merged":true}}`}, 2, true, `{"merged":true}`}, + {"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1, false, ""}, } { asks := 0 request := func(string, []byte) (json.RawMessage, error) { a := row.answers[min(asks, len(row.answers)-1)] asks++ - if a == "stopping" { - return nil, replyError("git's trees was not taken: the tool runner is stopping", "stopping") + if row.replies { + result, _, err := readReply("mesh.seat.git.call.trees", []byte(a)) + return result, err } return json.RawMessage(a), nil } + want := row.want + if want == "" { + want = row.answers[len(row.answers)-1] + } got, err := callKeptWith(busretry.Kept{Seat: "git", Verb: "merge-pull"}, request, "called", "fallback", nil) - if asks != row.asks || err != nil || string(got) != row.answers[len(row.answers)-1] { + if asks != row.asks || err != nil || string(got) != want { t.Errorf("%s: asked %d times, %s, %v", row.name, asks, got, err) } }