UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

87 lines (86 loc) • 2.95 kB
import { R as resolveGlobalSingleton, f as resolveStateDir } from "./paths-YAGBztX6.js"; import { t as createSubsystemLogger } from "./subsystem-BMqVrVqn.js"; import "./openclaw-root-D1JbMbZv.js"; import "./cli-invocation-Df8-W3Wc.js"; import path from "node:path"; import fsPromises from "node:fs/promises"; import { randomUUID } from "node:crypto"; import { fileURLToPath } from "node:url"; import { setImmediate, setTimeout } from "node:timers/promises"; import { getEnvironmentData, isMainThread, setEnvironmentData } from "node:worker_threads"; 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 };