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) {