UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

158 lines (157 loc) • 3.67 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as runWithAsyncStore } from "./async-store-C6naia_Y.mjs"; import { t as runInDetachedAsyncContext } from "./detached-async-context-D_mi-wUl.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; import { performance } from "node:perf_hooks"; //#region src/infra/main-thread-stall.ts const STALL_MS = 250; const UNATTRIBUTED_STALL_MS = 1e3; const HEARTBEAT_MS = 20; const MAX_PENDING_STALLS = 8; const taskState = resolveGlobalSingleton(Symbol.for("openclaw.mainThreadTask"), () => ({ context: new AsyncLocalStorage() })); /** Synchronous callers can enter a stored owner context without losing their enclosing task. */ function getMainThreadTaskName() { return taskState.observer?.currentName() ?? taskState.context.getStore(); } /** * Name code-owned work only; request text, paths, and identifiers do not belong in labels. * Phases label synchronous segments; only task boundaries establish an inherited async name. */ function runWithMainThreadTask(e, t, n) { const r = taskState.observer; return r ? r.run(e, t, n) : t(); } /** Measure explicit synchronous scopes; unscoped stalls rely on best-effort loop sampling. */ function createMainThreadStallMonitor(e = () => performance.now()) { taskState.observer?.stop(); const t = []; let n; let r = e(); let i = 0; let a = 0; let o = 0; let s = false; let c = 0; let l = 0; let u = "unattributed"; let d; let f = u; let p; let m = 0; const observeLoop = () => { const n = e(); const i = r; r = n; const o = n - i; if (o <= UNATTRIBUTED_STALL_MS || t.some((e) => e.startedAt <= i + HEARTBEAT_MS && e.endedAt >= n - HEARTBEAT_MS)) return; if (t.length < MAX_PENDING_STALLS) t.push({ elapsedMs: o, task: "unattributed", taskMs: o, startedAt: i, endedAt: n }); else a++; }; const segment = (e) => { const t = e - l; if (t > m) { f = u; p = d; m = t; } l = e; }; const h = { currentName: () => s ? u : void 0, stop() { clearInterval(n); n = void 0; if (taskState.observer === h) { delete taskState.observer; taskState.context.disable(); } t.length = 0; i = 0; a = 0; o = 0; }, run(n, r, a) { const h = e(); const g = !s; if (g) { s = true; c = l = h; u = f = "unattributed"; d = p = void 0; m = 0; } else segment(h); const _ = u; const v = d; u = n; d = a; try { return a === void 0 ? runWithAsyncStore(taskState.context, n, r) : r(); } finally { const n = e(); segment(n); u = _; d = v; if (g) { s = false; const e = n - c; o = Math.max(o, e); if (e >= STALL_MS) { if (t.length < MAX_PENDING_STALLS) t.push({ elapsedMs: e, task: p ? `${f}:${p}` : f, taskMs: m, startedAt: c, endedAt: n }); else i++; } } } } }; taskState.observer = h; return { setEventLoopSampling(t) { if (t === Boolean(n)) return; clearInterval(n); r = e(); n = t ? runInDetachedAsyncContext(() => setInterval(observeLoop, HEARTBEAT_MS).unref()) : void 0; }, drain() { if (n) observeLoop(); const e = t.splice(0); const r = i; const s = a; i = 0; a = 0; const c = o; o = 0; return { stalls: e, dropped: r, unattributedDropped: s, delayMaxMs: c }; }, reset() { r = e(); t.length = 0; i = 0; a = 0; o = 0; c = l = e(); m = 0; f = u; p = d; }, stop: () => h.stop() }; } //#endregion export { getMainThreadTaskName as n, runWithMainThreadTask as r, createMainThreadStallMonitor as t };