S3 ask-once-ts: the TypeScript stdio asks once; the kind stopping goes
Builds: design 47 H2 H-A-570 (2) (3) (4)
This commit is contained in:
+25
-43
@@ -22,27 +22,10 @@ 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). */
|
||||
/** The setting of how long a call waits for its answer, and its default: the Go stdio Ask's (design 47 H2 H-A-570 (4)). */
|
||||
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<void> => new Promise((r) => setTimeout(r, ms)),
|
||||
now: (): number => Date.now(),
|
||||
};
|
||||
|
||||
/** 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 {
|
||||
@@ -60,31 +43,31 @@ function settingMs(key: string, fallback: number): number {
|
||||
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<unknown>): Promise<unknown> {
|
||||
const start = askPacing.now();
|
||||
/** Asks once, as the Go stdio's Ask does, and asks nothing again itself: a call its caller marks safe to repeat is called
|
||||
* again by a kept call through no responders as through every transport fault. A call met by no responders is the runtime's
|
||||
* refusal, returned as such; at the bound (the setting ask-bound, 60 s by default) the fault is "no answer within <bound>"
|
||||
* (design 47 H2 H-A-570 (2), (4)). */
|
||||
async function askWithinBound(once: () => Promise<unknown>): Promise<unknown> {
|
||||
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);
|
||||
}
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const late = new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(`no answer within ${boundText(bound)}`)), bound);
|
||||
});
|
||||
try {
|
||||
return await Promise.race([once(), late]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
/** A bound as Go's duration prints it for the whole milliseconds a setting gives ("50ms", "10s", "1m0s"). */
|
||||
function boundText(ms: number): string {
|
||||
if (ms < 1000) return `${ms}ms`;
|
||||
const s = ms / 1000;
|
||||
if (s < 60) return `${s}s`;
|
||||
return `${Math.floor(s / 60)}m${s % 60}s`;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
jsonrpc: string;
|
||||
id?: number | string | null;
|
||||
@@ -171,10 +154,9 @@ 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).
|
||||
// A call is made once and its fault said at the bound, as the Go stdio's Ask does (design 47 H2 H-A-570 (2), (4)).
|
||||
request: (async <Req, Res>(key: string, body: Req): Promise<Res> =>
|
||||
(await askAgainWhileStopping(key, () => ask("mesh/ask", { key, body }))) as Res) as Broker["request"],
|
||||
(await askWithinBound(() => 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"],
|
||||
@@ -302,7 +284,7 @@ function startSession(): Session {
|
||||
if (reply.method === undefined && typeof reply.id === "string" && awaiting.has(reply.id)) {
|
||||
const waiting = awaiting.get(reply.id)!;
|
||||
awaiting.delete(reply.id);
|
||||
// A typed fault rides as the error's data `error-kind` (a stopping tool runner's stopping, H-A-542): kept as errorKind.
|
||||
// A typed fault rides as the error's data `error-kind` (an `away`, §776): 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);
|
||||
|
||||
Reference in New Issue
Block a user