openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
88 lines (87 loc) • 3.02 kB
JavaScript
import { _ as resolveGlobalSingleton } from "./runtime-doctor-migrations-JGL9tD-z.js";
import { s as resolveStateDir } from "./account-core-u1qUW4Od.js";
import { t as createSubsystemLogger } from "./subsystem-DLheYoE0.js";
import "./cli-invocation-Df8-W3Wc.js";
import "./openclaw-root-CzRKbUMH.js";
import fsPromises from "node:fs/promises";
import path from "node:path";
import { randomUUID } from "node:crypto";
import { fileURLToPath } from "node:url";
import { getEnvironmentData, isMainThread, setEnvironmentData } from "node:worker_threads";
import { setImmediate, setTimeout } from "node:timers/promises";
import { replaceFileAtomic } from "@openclaw/fs-safe/atomic";
//#region src/infra/runtime-profile.ts
const notices = resolveGlobalSingleton(Symbol.for("openclaw.runtimeExecutionNotices"), () => {
const e = "openclaw.runtimeExecutionNotices";
const t = getEnvironmentData(e);
const n = t instanceof SharedArrayBuffer ? t : new SharedArrayBuffer(4);
setEnvironmentData(e, n);
return new Int32Array(n);
});
//#endregion
//#region src/logging/diagnostic-worker-gc-profile.ts
const MAX_FILES = 20;
const FILE_PATTERN = /^code-mode-gc-\d+-[a-f0-9-]+\.(?:heapprofile|cpuprofile)\.json$/;
let writes = Promise.resolve();
/** The Gateway reports storms lazily; its registry retains the final capture admission. */
async function reportWorkerGcStorm(e, t, n) {
const { profiles: r, ...i } = e;
const a = {
...i,
workerThreadId: t,
workerSlot: n
};
const o = createSubsystemLogger("agents/code-mode");
if (r) try {
const e = await writeWorkerGcProfiles(r);
o.warn("Code Mode worker GC storm", {
...a,
profileFiles: e
});
} catch {
o.warn("Code Mode worker GC storm", {
...a,
captureStatus: "unavailable",
captureReason: "profile-write-failed"
});
}
else o.warn("Code Mode worker GC storm", a);
if (i.captureStatus === "requested") {
const { getEnabledCategories: e } = await import("node:trace_events");
if (e()) o.warn("Code Mode worker GC storm", {
...a,
captureStatus: "unavailable",
captureReason: "tracing-active"
});
else return i;
}
}
/** The Gateway serializes all worker writes so the global file cap survives concurrent storms. */
function writeWorkerGcProfiles(e) {
const t = writes.then(async () => {
const t = path.join(resolveStateDir(), "diagnostics");
await fsPromises.mkdir(t, {
recursive: true,
mode: 448
});
const n = (await fsPromises.readdir(t)).filter((e) => FILE_PATTERN.test(e)).toSorted();
for (const e of n.slice(0, Math.max(0, n.length - MAX_FILES + 2))) await fsPromises.unlink(path.join(t, e));
const r = `code-mode-gc-${Date.now()}-${randomUUID()}`;
const i = [];
for (const [n, a] of [["heap", e.heap], ["cpu", e.cpu]]) {
const e = `${r}.${n}profile.json`;
await replaceFileAtomic({
filePath: path.join(t, e),
content: a,
mode: 384,
dirMode: 448
});
i.push(e);
}
return i;
});
writes = t.catch(() => {});
return t;
}
//#endregion
export { reportWorkerGcStorm };