openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
154 lines (153 loc) • 4.8 kB
JavaScript
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { l as gitNullConfigPath } from "./git-exec-DHBL622T.mjs";
import { n as gitEnvironment, o as requireGit } from "./git-BBx5S0aA.mjs";
import { i as timeWorktreePreparationPhase } from "./preparation-timing-YO2wGpby.mjs";
import { t as withWorktreeGitConfig } from "./checkout-git-config-ClOP9WBP.mjs";
import { n as captureWorkspaceManifestDocument } from "./workspace-manifest-worker-CfX0osDw.mjs";
import { a as runWorkspaceInventoryCommandToFile } from "./workspace-sync-inventory-COAk-OVx.mjs";
import { n as prepareWorkerWorkspaceGitPack } from "./workspace-git-base-DmGgNjwY.mjs";
import { realpathSync } from "node:fs";
import path from "node:path";
import fs$1 from "node:fs/promises";
//#region src/gateway/worker-environments/local-workspace-checkout.ts
function localWorkspaceProjectionPath(e) {
if (!/^[a-f0-9-]{36}$/u.test(e.worktree.id)) throw new Error("Invalid managed worktree identity");
return path.join(realpathSync(resolveStateDir(e.env)), "worktree-projections", e.worktree.id, "workspace");
}
/** Build independent, source-only Git metadata before any guest can write this checkout. */
async function prepareLocalWorkspaceCheckout(e) {
const { signal: t, assertCurrent: n, destination: r } = e;
const i = await fs$1.mkdtemp(path.join(e.temporaryRoot, "git-"));
try {
const a = await withWorktreeGitConfig(e.source, true, {
signal: t,
beforeRun: n
}, (n) => n.withContentEnvironment((n) => prepareWorkerWorkspaceGitPack({
root: e.source,
baseCommit: e.baseCommit,
temporaryRoot: i,
signal: t,
baseEnv: n
})));
n();
await fs$1.mkdir(r, {
recursive: true,
mode: 448
});
const o = {
PATH: process.env.PATH,
HOME: i,
GIT_CONFIG_NOSYSTEM: "1",
GIT_CONFIG_GLOBAL: gitNullConfigPath(),
GIT_CONFIG_SYSTEM: gitNullConfigPath(),
GIT_NO_REPLACE_OBJECTS: "1",
GIT_TERMINAL_PROMPT: "0"
};
const git = (e, i) => requireGit(r, e, {
baseEnv: o,
env: o,
input: i,
signal: t,
beforeRun: n
});
await git([
"init",
"--quiet",
"--template=",
"--object-format=" + (e.baseCommit.length === 40 ? "sha1" : "sha256")
]);
n();
await runWorkspaceInventoryCommandToFile({
argv: [
"git",
"-C",
r,
"index-pack",
"--stdin"
],
inputPath: a,
outputPath: path.join(i, "index-pack-result"),
baseEnv: gitEnvironment(o),
signal: t,
timeoutMs: 3e5,
maxOutputBytes: 4096
});
n();
await fs$1.writeFile(path.join(r, ".git", "shallow"), e.baseCommit + "\n", { mode: 384 });
await git([
"checkout",
"--quiet",
"-b",
e.branch,
e.baseCommit
]);
} finally {
await fs$1.rm(i, {
recursive: true,
force: true
});
}
}
async function assertLocalWorkspaceDirectory(e) {
const t = await fs$1.lstat(e);
if (!t.isDirectory() || t.isSymbolicLink() || await fs$1.realpath(e) !== e) throw new Error("Local sandbox workspace directory changed; preserved for recovery");
}
/** Only an unpublished projection reservation may replace an interrupted initial checkout. */
async function initializeLocalWorkspaceProjection(e) {
const { signal: t, assertCurrent: n, dependencies: r } = e;
const i = path.dirname(e.projectionPath);
await fs$1.mkdir(i, {
recursive: true,
mode: 448
});
await assertLocalWorkspaceDirectory(i);
n();
await fs$1.rm(e.projectionPath, {
recursive: true,
force: true
});
const a = await fs$1.mkdtemp(path.join(i, ".prepare-"));
try {
const o = path.join(a, "workspace");
const s = {
source: e.owner.worktree.path,
destination: o,
temporaryRoot: a,
baseCommit: e.baseCommit,
branch: e.owner.worktree.branch,
signal: t,
assertCurrent: n
};
if (!(r && await (await import("./local-workspace-template-DvmnFTa8.mjs")).cloneLocalWorkspaceTemplate({
...s,
repoRoot: e.owner.worktree.repoRoot,
templateRoot: path.dirname(i),
env: e.owner.env ?? process.env,
sandbox: r.sandbox,
guard: {
...r.allocation,
signal: r.allocation.signal ? AbortSignal.any([t, r.allocation.signal]) : t,
commitGuard: () => {
r.allocation.commitGuard();
n();
}
}
}))) await timeWorktreePreparationPhase("checkout", () => prepareLocalWorkspaceCheckout(s));
const c = await timeWorktreePreparationPhase("snapshot", async () => captureWorkspaceManifestDocument({
root: o,
baseCommit: e.baseCommit,
buildCacheInputs: new Set(await e.buildCacheInputs()),
signal: t
}));
n();
await fs$1.rename(o, e.projectionPath);
return c;
} finally {
await fs$1.rm(a, {
recursive: true,
force: true
});
}
}
//#endregion
export { prepareLocalWorkspaceCheckout as i, initializeLocalWorkspaceProjection as n, localWorkspaceProjectionPath as r, assertLocalWorkspaceDirectory as t };