openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
71 lines (70 loc) • 4.72 kB
JavaScript
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs";
import { t as getOpenClawStateFeatureSchemaSql } from "./openclaw-state-feature-schema-CEI_WUTe.mjs";
import { n as localWorkspaceDigestReference, t as localWorkspaceDigestBytes } from "./local-workspace-cas-DxFAvLyr.mjs";
//#region src/gateway/worker-environments/local-workspace-cas.kernel.ts
const packs = "local_workspace_tree_packs";
const pathSets = "local_workspace_source_path_sets";
const epochKey = "workspace.manifest.sweepEpoch";
const query = (e) => getNodeSqliteKysely(e);
function currentLocalWorkspaceSweepEpoch(e) {
if (!tableExists(e, "config_machine_state")) return 0;
const t = executeSqliteQueryTakeFirstSync(e, query(e).selectFrom("config_machine_state").select("value_json").where("state_key", "=", epochKey));
return t ? Number(t.value_json) : 0;
}
/** One bounded worker transaction. Preparation does not change any projection authority. */
function prepareLocalWorkspaceContentInDatabase(e, t) {
if (!tableExists(e, packs)) e.exec(getOpenClawStateFeatureSchemaSql("local_workspace_projections"));
const n = currentLocalWorkspaceSweepEpoch(e);
if (t.packs?.length) {
if (t.packs.length > 256) throw new Error("Local workspace pack preparation exceeds its batch limit");
const r = t.packs.map((e) => ({
tree_digest: localWorkspaceDigestBytes(e.treeRef),
records_json: JSON.stringify(e.records),
children_json: JSON.stringify(e.children),
sweep_epoch: n
}));
const i = executeSqliteQuerySync(e, query(e).selectFrom(packs).select(["tree_digest", "sweep_epoch"]).where("tree_digest", "in", r.map((e) => e.tree_digest))).rows;
const a = new Set(i.map((e) => localWorkspaceDigestReference(e.tree_digest)));
const o = r.filter((e) => !a.has(localWorkspaceDigestReference(e.tree_digest)));
if (o.length) executeSqliteQuerySync(e, query(e).insertInto(packs).values(o).onConflict((e) => e.column("tree_digest").doUpdateSet((e) => ({ sweep_epoch: e.case().when(`${packs}.sweep_epoch`, "<", n).then(n).else(e.ref(`${packs}.sweep_epoch`)).end() }))));
const s = i.filter((e) => e.sweep_epoch < n).map((e) => e.tree_digest);
if (s.length) executeSqliteQuerySync(e, query(e).updateTable(packs).set({ sweep_epoch: n }).where("tree_digest", "in", s).where("sweep_epoch", "<", n));
}
if (t.paths) {
const r = localWorkspaceDigestBytes(t.paths.digest);
executeSqliteQuerySync(e, query(e).insertInto(pathSets).values({
path_digest: r,
paths_zstd: Buffer.from(t.paths.compressed),
sweep_epoch: n
}).onConflict((e) => e.column("path_digest").doUpdateSet((e) => ({ sweep_epoch: e.case().when(`${pathSets}.sweep_epoch`, "<", n).then(n).else(e.ref(`${pathSets}.sweep_epoch`)).end() }))));
}
return n;
}
function beginLocalWorkspaceSweepInDatabase(e) {
const t = currentLocalWorkspaceSweepEpoch(e) + 1;
executeSqliteQuerySync(e, query(e).insertInto("config_machine_state").values({
state_key: epochKey,
value_json: String(t),
updated_at_ms: Date.now()
}).onConflict((e) => e.column("state_key").doUpdateSet({
value_json: String(t),
updated_at_ms: Date.now()
})));
return t;
}
/** The epoch predicate protects content reused after the read snapshot. */
function deleteLocalWorkspaceSweepBatchInDatabase(e, t, n) {
if (n.packs.length > 256 || n.paths.length > 256) throw new Error("Local workspace sweep exceeds its batch limit");
if (n.packs.length) executeSqliteQuerySync(e, query(e).deleteFrom(packs).where("tree_digest", "in", n.packs.map((e) => Buffer.from(e))).where("sweep_epoch", "<", t - 1));
if (n.paths.length) executeSqliteQuerySync(e, query(e).deleteFrom(pathSets).where("path_digest", "in", n.paths.map((e) => Buffer.from(e))).where("sweep_epoch", "<", t - 1));
}
function pruneLocalWorkspaceLegacyBatchInDatabase(e, t) {
if (!executeSqliteQueryTakeFirstSync(e, query(e).selectFrom("local_workspace_projections").select("tree_root_digest").where("worktree_id", "=", t))?.tree_root_digest) return false;
const n = executeSqliteQuerySync(e, query(e).selectFrom("local_workspace_manifest_nodes").select("path").where("worktree_id", "=", t).limit(256)).rows;
if (!n.length) return false;
executeSqliteQuerySync(e, query(e).deleteFrom("local_workspace_manifest_nodes").where("worktree_id", "=", t).where("path", "in", n.map((e) => e.path)));
return n.length === 256;
}
//#endregion
export { pruneLocalWorkspaceLegacyBatchInDatabase as a, prepareLocalWorkspaceContentInDatabase as i, currentLocalWorkspaceSweepEpoch as n, deleteLocalWorkspaceSweepBatchInDatabase as r, beginLocalWorkspaceSweepInDatabase as t };