openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
96 lines (95 loc) • 6.19 kB
JavaScript
import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { f as requestSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { i as worktreeRegistryPublication, r as withWorktreeRegistryWorkerReceipt } from "./registry-publication-BHPzcpK8.mjs";
import { a as worktreeGcRevision, i as rowToRecord, n as getRegistryWorktreeInDatabase } from "./registry-read.kernel-CTVSmZF2.mjs";
import { a as finalizeWorktreeRemovalInDatabase, c as updateRegistryWorktreeInDatabase, d as reservePendingWorktreeInDatabase, i as deleteRegistryWorktreeInDatabase, l as worktreeRunEndMutation, m as reapWorktreeRunLeasesInDatabase, n as assertWorktreeRegistryPredicates, o as insertRegistryWorktreeInDatabase, r as claimWorktreeRemovalInDatabase, t as abortWorktreeRemovalInDatabase, u as releasePendingWorktreeInDatabase } from "./registry-run-end.worker-CvUihXAy.mjs";
import { r as releaseWorktreeRunLeaseInDatabase, t as admitWorktreeRunLeaseInDatabase } from "./run-lease-store.kernel-C6DsqJN0.mjs";
import { n as hasLocalWorkspaceProjectionInDatabase } from "./local-workspace-store.kernel-B9-PAsu0.mjs";
//#region src/agents/worktrees/provisioned-snapshot.worker.ts
function writeProvisionedSnapshotInDatabase(e, t) {
const n = getNodeSqliteKysely(e);
if (t.kind === "reset") executeSqliteQuerySync(e, n.deleteFrom("worktree_provisioned_file_chunks").where("worktree_id", "=", t.worktreeId));
else executeSqliteQuerySync(e, n.insertInto("worktree_provisioned_file_chunks").values({
worktree_id: t.worktreeId,
path: t.path,
chunk_index: t.chunkIndex,
data: t.data
}));
}
//#endregion
//#region src/agents/worktrees/registry-retirement.worker.ts
function deferWorktreeCleanupInWorker({ observed: e, reason: t, retry: n, removalToken: r }, i) {
return runOpenClawStateWriteTransaction(({ db: i }) => withWorktreeRegistryWorkerReceipt(i, () => {
requestSqliteWorkerOperationAdmission({
stage: "transaction",
facts: void 0
});
if (r) assertWorktreeRegistryPredicates(i, [{
kind: "removal-claim",
id: e.id,
token: r
}]);
const a = getRegistryWorktreeInDatabase(i, e.id);
const o = worktreeGcRevision(e);
if (!a || a.removedAt !== void 0 || worktreeGcRevision(a) !== o) return false;
const s = executeSqliteQuerySync(i, getNodeSqliteKysely(i).updateTable("worktrees").set({ gc_protection_json: t === null ? null : JSON.stringify({
revision: o,
reason: t,
retry: n
}) }).where("id", "=", e.id).returningAll()).rows;
worktreeRegistryPublication.rows(i, s);
requestSqliteWorkerOperationAdmission({
stage: "commit",
facts: void 0
});
return true;
}), i, { operationLabel: "worktrees.deferCleanup" });
}
function retireMissingWorktreeInWorker({ observed: e, removedAt: t }, n) {
return runOpenClawStateWriteTransaction(({ db: n }) => withWorktreeRegistryWorkerReceipt(n, () => {
requestSqliteWorkerOperationAdmission({
stage: "transaction",
facts: void 0
});
if (hasLocalWorkspaceProjectionInDatabase(n, e.id)) return {
record: getRegistryWorktreeInDatabase(n, e.id),
protection: "local-workspace-projection"
};
const r = executeSqliteQuerySync(n, getNodeSqliteKysely(n).updateTable("worktrees").set({ removed_at: t }).where("id", "=", e.id).where("removed_at", "is", null).where((e) => e.or([e("snapshot_ref", "is", null), e("snapshot_ref", "not like", "refs/openclaw/snapshots/exact-%")])).where("path", "=", e.path).where("last_active_at", "=", e.lastActiveAt).where("repo_root", "=", e.repoRoot).where("repo_fingerprint", "=", e.repoFingerprint).returningAll()).rows[0];
worktreeRegistryPublication.rows(n, r ? [r] : []);
const i = r ? rowToRecord(r) : getRegistryWorktreeInDatabase(n, e.id);
requestSqliteWorkerOperationAdmission({
stage: "commit",
facts: void 0
});
return { record: i };
}), n, { operationLabel: "worktrees.retireMissing" });
}
//#endregion
//#region src/agents/worktrees/dispatch.worker.ts
const worktreeOperations = {
"worktrees.assertPredicates": ({ predicates: e }, { open: t }) => assertWorktreeRegistryPredicates(t().db, e),
"worktrees.delete": worktreeRunEndMutation("worktrees.delete", deleteRegistryWorktreeInDatabase),
"worktrees.reservePending": worktreeRunEndMutation("worktrees.reservePending", reservePendingWorktreeInDatabase),
"worktrees.releasePending": worktreeRunEndMutation("worktrees.releasePending", releasePendingWorktreeInDatabase),
"worktrees.insert": worktreeRunEndMutation("worktrees.insert", insertRegistryWorktreeInDatabase),
"worktrees.update": worktreeRunEndMutation("worktrees.update", updateRegistryWorktreeInDatabase),
"worktrees.claimRemoval": worktreeRunEndMutation("worktrees.claimRemoval", claimWorktreeRemovalInDatabase),
"worktrees.finalizeRemoval": worktreeRunEndMutation("worktrees.finalizeRemoval", finalizeWorktreeRemovalInDatabase),
"worktrees.abortRemoval": worktreeRunEndMutation("worktrees.abortRemoval", abortWorktreeRemovalInDatabase),
"worktrees.writeProvisionedSnapshot": worktreeRunEndMutation("worktrees.writeProvisionedSnapshot", writeProvisionedSnapshotInDatabase),
"worktrees.retireMissing": (e, { open: t, stateOptions: n }) => retireMissingWorktreeInWorker(e, {
...n(),
database: t()
}),
"worktrees.deferCleanup": (e, { open: t, stateOptions: n }) => deferWorktreeCleanupInWorker(e, {
...n(),
database: t()
}),
"worktrees.admitRunLease": (e, { writeAdmitted: t }) => t(({ db: t }) => withWorktreeRegistryWorkerReceipt(t, () => admitWorktreeRunLeaseInDatabase(t, e)), { operationLabel: "worktrees.admitRunLease" }),
"worktrees.releaseRunLease": ({ worktreeId: e, token: t }, { writeAdmitted: n }) => n(({ db: n }) => withWorktreeRegistryWorkerReceipt(n, () => releaseWorktreeRunLeaseInDatabase(n, e, t)), { operationLabel: "worktrees.releaseRunLease" }),
"worktrees.reapRunLeases": ({ scopes: e }, { writeAdmitted: t }) => t(({ db: t }) => withWorktreeRegistryWorkerReceipt(t, () => reapWorktreeRunLeasesInDatabase(t, e)), { operationLabel: "worktrees.reapRunLeases" })
};
//#endregion
export { worktreeOperations };