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
This commit is contained in:
+63
-3
@@ -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_<KEY>, 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<string, number> = { 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<unknown>): Promise<unknown> {
|
||||
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 <Req, Res>(key: string, body: Req): Promise<Res> =>
|
||||
(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;
|
||||
}
|
||||
|
||||
Vendored
+16
@@ -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();
|
||||
@@ -378,3 +378,49 @@ async function aBundlesFindingWriter(): Promise<void> {
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user