UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

192 lines (191 loc) • 5.76 kB
import { l as isRecord } from "../record-coerce-CVPuDqcc.mjs"; import { a as SqliteSnapshotCleanupError, c as createSqliteLifecycleAggregateError, s as classifyRetiredSqliteSnapshotRemovalError, u as throwSqliteLifecycleErrors } from "../sqlite-lifecycle-errors-D8BU4VLa.mjs"; import { t as encodeOpenClawStateWorkerError } from "../openclaw-state-worker-error-C8y1JGQN.mjs"; import { n as createSqliteReadOnlyNativeResourceClient, t as SQLITE_NATIVE_RESOURCE_PORT } from "../sqlite-readonly-native-resource.types-Bv4W2eSC.mjs"; import { t as serveOwnedWorkerTasks } from "../worker-task-server-J1vG-XRl.mjs"; import path from "node:path"; import fs from "node:fs/promises"; import { MessagePort, workerData } from "node:worker_threads"; import { createAsyncLock } from "@openclaw/fs-safe/advanced"; //#region src/infra/sqlite-snapshot-staging-runtime.ts /** Private token connections share a process per launch context, never a copy/read permit. */ function createSqliteSnapshotStagingRuntime(e) { const t = /* @__PURE__ */ new Set(); const n = createAsyncLock(); async function closeSession(e) { await e.worker.close(); t.delete(e); } function session(n) { for (const e of t) if (e.worker.compatible(n)) return e; const r = { worker: e({ ...n, retainLifetime: false, retainOnOperationError: true }), directories: 0 }; t.add(r); return r; } return { close() { return n(async () => { if ([...t].some((e) => e.directories !== 0)) throw new Error("SQLite snapshot staging owner still has retained directories"); const e = []; for (const n of t) try { await closeSession(n); } catch (t) { e.push(t); } throwSqliteLifecycleErrors(e, "SQLite snapshot staging owners failed to close"); }); }, async allocate(e, r, i, a) { return n(async () => { const o = session(i); let s; try { s = await o.worker.run(e, { mode: r ? "staging-create-legacy" : "staging-create", preparationId: a }); o.directories++; } catch (e) { if (o.directories === 0) try { await closeSession(o); } catch (t) { throw createSqliteLifecycleAggregateError([e, t], "SQLite snapshot allocation and owner cleanup failed", e); } throw e; } let c = false; return { directory: s, retire: () => n(async () => { if (!c) { await o.worker.run(s, { mode: "staging-retire" }); c = true; o.directories--; } if (o.directories === 0 && t.has(o)) await closeSession(o); }) }; }); } }; } //#endregion //#region src/infra/sqlite-snapshot-staging.worker.ts const resourcePort = isRecord(workerData) ? workerData[SQLITE_NATIVE_RESOURCE_PORT] : void 0; if (!(resourcePort instanceof MessagePort)) throw new Error("SQLite snapshot staging requires its native lifetime owner"); const native = createSqliteReadOnlyNativeResourceClient(resourcePort); const runtime = createSqliteSnapshotStagingRuntime((e) => native.createSession(e)); const directories = /* @__PURE__ */ new Map(); async function closeDirectory(e) { const t = directories.get(e); if (!t) return; if (!t.removed) { await t.retire(); try { await fs.rm(e, { force: true, recursive: true, maxRetries: 3, retryDelay: 20 }); } catch (t) { throw classifyRetiredSqliteSnapshotRemovalError(e, t); } t.removed = true; } await native.removed(e); directories.delete(e); } async function closeResource(e) { if (e !== void 0) { await closeDirectory(e); return; } const t = []; for (const e of directories.keys()) try { await closeDirectory(e); } catch (e) { t.push(e); } if (directories.size === 0) try { await runtime.close(); } catch (e) { t.push(e); } throwSqliteLifecycleErrors(t, "SQLite snapshot staging cleanup failed"); } serveOwnedWorkerTasks(async (e) => { const t = e; const n = t.type === "prepare" ? t.abortPort : void 0; let abort; try { const e = new AbortController(); if (n) { abort = () => { e.abort(/* @__PURE__ */ new Error("SQLite snapshot preparation stopped")); }; n.on("message", abort); } let r; try { const n = await runtime.allocate(path.resolve(t.launch.cwd, t.root), t.allowLegacyWorker, t.launch, t.preparationId); r = n.directory; directories.set(r, { retire: n.retire, removed: false }); if (t.type === "allocate") return { type: "allocated", directory: r }; e.signal.throwIfAborted(); const i = await native.runOnce(path.resolve(t.launch.cwd, t.pathname), { mode: t.preserveSourceArtifacts ? "sync" : "async", stagingRoot: r, expectedSourceIdentity: t.expectedSourceIdentity, signal: e.signal }, { ...t.launch, deadlineOwnedByCaller: t.deadlineOwnedByCaller }); e.signal.throwIfAborted(); return { type: "prepared", directory: r, location: i }; } catch (e) { let t = e; let n = e instanceof SqliteSnapshotCleanupError; if (r !== void 0) try { await closeDirectory(r); } catch (r) { n = true; t = createSqliteLifecycleAggregateError([e, r], "SQLite snapshot preparation and cleanup failed", e); } const i = encodeOpenClawStateWorkerError(t, { includeOrdinary: true }) ?? encodeOpenClawStateWorkerError(/* @__PURE__ */ new Error("SQLite snapshot staging failed"), { includeOrdinary: true }); if (!i) throw t; return { type: "failed", error: i, ...n ? { cleanupFailure: true } : {}, ...r !== void 0 && directories.has(r) ? { directory: r } : {} }; } } finally { if (abort) n?.off("message", abort); n?.close(); } }, { closeResource, encodeResourceError: (e) => encodeOpenClawStateWorkerError(e, { includeOrdinary: true }) }); //#endregion export {};