openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
147 lines (146 loc) • 4.74 kB
JavaScript
import { t as drainGlobalSingletonLifecycleState } from "../global-singleton-D7yf05nR.mjs";
import { c as runOutsideAsyncWorkScope, t as AsyncWorkScope } from "../async-work-scope-BXagjUap.mjs";
import { t as truncateUtf8Prefix } from "../utf8-truncate-BH7Ecfuh.mjs";
import { o as withCliProcessScope, r as retainCliProcessJobUntilExit } from "../runtime-cleanup-scope-DuOL68nJ.mjs";
import { i as updateRepairParentMessageSchema, n as UPDATE_REPAIR_TEXT_MAX_CHARS, t as UPDATE_REPAIR_IPC_MAX_BYTES } from "../update-repair-protocol-CvVmDR4d.mjs";
//#region src/infra/update-repair.worker.ts
const deferredReason = "Inference repair is deferred until after the update has failed. Updates do not require inference.";
const controller = new AbortController();
const work = new AsyncWorkScope();
const admissionEnv = { ...process.env };
const pendingSends = /* @__PURE__ */ new Set();
let started = false;
let closing = false;
let finalExitCode = 0;
let execution = Promise.resolve();
function send(e) {
const t = new Promise((t, n) => {
if (!process.connected || !process.send || Buffer.byteLength(JSON.stringify(e)) > 65536) {
n(/* @__PURE__ */ new Error("Repair orchestrator disconnected."));
return;
}
process.send(e, (e) => e ? n(e) : t());
});
pendingSends.add(t);
t.then(() => pendingSends.delete(t), (e) => {
pendingSends.delete(t);
stop(1, e);
});
return t;
}
function stop(e, t, n = []) {
if (e !== 0) finalExitCode = e;
if (closing) return;
closing = true;
controller.abort(t ?? /* @__PURE__ */ new Error("Repair worker finished."));
work.beginClose(controller.signal.reason);
process.off("message", onMessage);
runOutsideAsyncWorkScope(async () => {
try {
await startup.catch(() => void 0);
await execution;
await work.drain();
await drainGlobalSingletonLifecycleState();
for (const e of n) await send(e);
} catch {
finalExitCode = 1;
process.stderr.write("Update repair worker failed while settling execution or cleanup.\n");
} finally {
await Promise.allSettled(pendingSends);
process.off("disconnect", onDisconnect);
process.off("SIGINT", onSignal);
process.off("SIGTERM", onSignal);
process.stdin.destroy();
if (process.connected) process.disconnect?.();
process.exitCode = finalExitCode;
}
});
}
function finish(e, t, n = 0) {
stop(n, n !== 0 ? new Error(t) : void 0, [{
type: "event",
event: {
type: "stopped",
status: e,
reason: t
}
}, {
type: "result",
result: {
status: e,
attempts: [],
finalValidation: {
ok: false,
score: 0,
summary: t
},
reason: t
}
}]);
}
function fail(e) {
const t = e instanceof Error ? e.message : String(e);
finish("aborted", truncateUtf8Prefix(t, UPDATE_REPAIR_TEXT_MAX_CHARS), 1);
}
function onDisconnect() {
stop(started ? 1 : 0, /* @__PURE__ */ new Error("Repair orchestrator disconnected."));
}
function onSignal() {
const e = /* @__PURE__ */ new Error("Repair worker cancelled.");
controller.abort(e);
if (!started) finish("aborted", e.message);
}
function onMessage(e) {
if (closing) return;
try {
if (Buffer.byteLength(JSON.stringify(e)) > 65536) throw new Error("Repair request exceeded its bounded diagnostic budget.");
const t = updateRepairParentMessageSchema.parse(e);
if (t.type === "cancel") {
controller.abort(new Error(t.reason));
if (!started) finish("aborted", t.reason);
return;
}
if (t.type === "validation-result" || t.type === "validation-error") throw new Error("Repair worker did not request validation.");
if (started) throw new Error("Repair worker already owns an execution.");
started = true;
if (t.type === "start") {
finish("unavailable", deferredReason);
return;
}
execution = startup.then(() => work.track(async () => {
controller.signal.throwIfAborted();
const { runDelegatedUpdateRepairTurn: e } = await import("../update-repair-turn-worker-DG0u4bze.mjs");
controller.signal.throwIfAborted();
const n = await e(t, admissionEnv, controller.signal, (e) => {
send({
type: "event",
event: {
type: "route-selected",
...e
}
}).catch(() => {});
});
if (!closing) stop(0, void 0, [{
type: "turn-result",
result: n
}]);
})).catch((e) => fail(e));
} catch (e) {
fail(e);
}
}
process.once("disconnect", onDisconnect);
process.on("SIGINT", onSignal);
process.on("SIGTERM", onSignal);
process.on("message", onMessage);
const startup = Promise.resolve().then(() => withCliProcessScope(retainCliProcessJobUntilExit));
startup.then(() => {
if (!closing) send({
type: "ready",
candidateRehearsal: true,
repairTurns: true,
executorDelegation: "pid-start-v1"
}).catch(() => {});
}, (e) => fail(e));
//#endregion
export {};