openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
142 lines (141 loc) • 4.61 kB
JavaScript
import { u as isStringArray } from "./record-coerce-CVPuDqcc.mjs";
import { s as requireGitBuffer } from "./git-BBx5S0aA.mjs";
import { a as splitNullBuffer } from "./git-path-inventory-3RPYKCuB.mjs";
import { b as workspacePathAncestors, g as buildCacheInputPaths, l as serializeWorkerWorkspaceManifest } from "./workspace-manifest-C118sSRH.mjs";
import { createHash } from "node:crypto";
import { isUtf8 } from "node:buffer";
//#region src/gateway/worker-environments/local-workspace-inventory.ts
function decodeLocalWorkspaceGitPaths(e) {
if (!isUtf8(e)) throw new Error("Local sandbox source paths must be UTF-8; rename the invalid Git path before retrying");
return e.toString("utf8").split("\0").filter(Boolean);
}
function parseLocalWorkspaceSourcePaths(e) {
const t = JSON.parse(e);
if (!isStringArray(t)) throw new Error("Invalid local workspace source admission");
return t;
}
/** Admit untracked source once, before any guest can change canonical ignore rules. */
async function admitLocalWorkspaceSourcePaths(e) {
const t = await requireGitBuffer(e.root, [
"ls-files",
"--cached",
"--others",
"--exclude-standard",
"-z"
], {
signal: e.signal,
beforeRun: e.assertCurrent
});
e.assertCurrent();
return JSON.stringify([...new Set(decodeLocalWorkspaceGitPaths(t))]);
}
/** Read guest ignore files with the host's index/config, never guest Git metadata. */
async function readLocalWorkspaceGitExclusions(e, t, n) {
const r = await requireGitBuffer(e, [
`--work-tree=${t}`,
"ls-files",
"--full-name",
"--others",
"--ignored",
"--exclude-standard",
"--directory",
"-z"
]);
return {
paths: new Set(decodeLocalWorkspaceGitPaths(r)),
retainedPaths: n
};
}
/** Ignore changes are source edits, not authority to enroll canonical-only bytes. */
async function selectLocalWorkspaceCanonicalPaths(e) {
e.signal?.throwIfAborted();
e.assertCurrent();
let t;
if (e.preparedPaths) t = new Set(e.preparedPaths);
else {
const n = await requireGitBuffer(e.root, [
"ls-files",
"--cached",
"-z"
], {
signal: e.signal,
beforeRun: e.assertCurrent
});
e.assertCurrent();
const r = parseLocalWorkspaceSourcePaths(e.admittedPaths);
t = /* @__PURE__ */ new Set([...r, ...decodeLocalWorkspaceGitPaths(n)]);
}
if (e.baselinePaths) for (const n of e.baselinePaths) t.add(n);
for (const e of t) {
const n = e.lastIndexOf("/");
if (n !== -1) t.add(e.slice(0, n));
}
return t;
}
async function prepareLocalWorkspaceBuildInputs(e) {
const { worktree: t, projection: n, signal: r, assertCurrent: i } = e;
const a = parseLocalWorkspaceSourcePaths(n.source_paths_json);
const o = t.removedAt === void 0 ? await selectLocalWorkspaceCanonicalPaths({
root: t.path,
admittedPaths: n.source_paths_json,
signal: r,
assertCurrent: i
}) : new Set(a);
const s = buildCacheInputPaths(o);
if (t.snapshotRef && n.baseline_ref) {
const e = await requireGitBuffer(t.repoRoot, [
"ls-tree",
"-r",
"-z",
"--name-only",
t.snapshotRef
], {
signal: r,
beforeRun: i
});
i();
for (const t of buildCacheInputPaths(decodeLocalWorkspaceGitPaths(e))) {
s.add(t);
o.add(t);
}
}
const c = new Set(a);
const l = [...s].filter((e) => !c.has(e));
if (l.length) await e.retainSourcePaths(JSON.stringify([...a, ...l]));
return {
paths: o,
retained: [...s]
};
}
/** Git archives omit accepted ignored leaves and empty directories; retain that overlay separately. */
async function captureLocalWorkspaceArchiveBaseline(e) {
const { signal: t, assertCurrent: n } = e;
if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u.test(e.snapshotCommit)) throw new Error("Invalid archive snapshot commit");
const r = await requireGitBuffer(e.root, [
"ls-tree",
"-r",
"-z",
"--name-only",
e.snapshotCommit
], {
signal: t,
beforeRun: n,
maxOutputBytes: 67108864
});
n();
const i = new Set(splitNullBuffer(r).map((e) => e.toString("hex")));
const a = e.manifest.entries.filter((e) => i.has(Buffer.from(e.path).toString("hex")));
const o = /* @__PURE__ */ new Set();
for (const e of a) for (const t of workspacePathAncestors(e.path)) o.add(t);
const s = serializeWorkerWorkspaceManifest({
...e.manifest,
entries: a,
directories: [...o].toSorted()
});
return {
rawManifest: s,
manifestRef: "sha256:" + createHash("sha256").update(s).digest("hex")
};
}
//#endregion
export { readLocalWorkspaceGitExclusions as a, prepareLocalWorkspaceBuildInputs as i, captureLocalWorkspaceArchiveBaseline as n, selectLocalWorkspaceCanonicalPaths as o, parseLocalWorkspaceSourcePaths as r, admitLocalWorkspaceSourcePaths as t };