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..7d790525 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} } @@ -271,20 +280,31 @@ func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMess if err != nil { 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, held, err := readReply(subject, msg.Data) + if err != nil { + return nil, err } - 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 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"` + ErrorKind string `json:"error-kind"` + Node string `json:"node"` + Held *approval.Held `json:"held"` + } + 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, answered(r.Error) + 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 093ae36f..2e7236aa 100644 --- a/go/process/call_test.go +++ b/go/process/call_test.go @@ -142,24 +142,38 @@ 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 + 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}, - {"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 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) } } diff --git a/go/stdio.go b/go/stdio.go index 4fb2ee17..d06b6d30 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 = 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. +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,42 @@ 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 (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) + 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) + } } +// 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 ( + 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..f366988e 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,89 @@ 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 := 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 } + // 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() { + _ = 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() +} diff --git a/src/stdio/index.ts b/src/stdio/index.ts index dc2f6528..f602e979 100644 --- a/src/stdio/index.ts +++ b/src/stdio/index.ts @@ -22,6 +22,69 @@ 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; +} + +/** 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. */ +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 (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 = 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 = 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 ${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 askPacing.pause(wait); + wait = Math.min(wait * 2, most); + } + } +} + interface Request { jsonrpc: string; id?: number | string | null; @@ -108,8 +171,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 +295,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..84e7b17e --- /dev/null +++ b/test/fixtures/stdio-stopping.mjs @@ -0,0 +1,24 @@ +// 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, 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, 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", {}), paused }; + } catch (err) { + return { stopping: isStopping(err), words: err.message, paused }; + } + } }, +]); +await serveRegisteredOverStdio(); diff --git a/test/stdio.test.ts b/test/stdio.test.ts index 583409bb..b39f7388 100644 --- a/test/stdio.test.ts +++ b/test/stdio.test.ts @@ -378,3 +378,54 @@ 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 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: "2s", MESH_CONFIG_KEPT_CALL_PACE_MAX: "4s", MESH_CONFIG_ASK_BOUND: "10s" } }); + 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 [ + // 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) { + 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(); + } +});