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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user