openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
78 lines (77 loc) • 3.13 kB
JavaScript
import { r as resolveStateDir } from "./state-dir-CYkBUd_G.mjs";
import { t as createSubsystemLogger } from "./subsystem-4NzR43uz.mjs";
import { i as listAgentIds } from "./agent-roster-DH6r_8f4.mjs";
import { d as resolveAgentWorkspaceDir } from "./agent-scope-config-BEk6-ThS.mjs";
import { i as sessionChanges } from "./session-row-changes-Dj2yVWo-.mjs";
import { r as resolveSandboxConfigForAgent } from "./config-CqeBZkfZ.mjs";
import { i as withWorktreeMutationLease } from "./allocation-li13b6uz.mjs";
import { c as withWorktreeRunEnd } from "./run-end-lifecycle-CRbsD4Aj.mjs";
import { r as resolveWorktreeBase } from "./base-ref-DthJolKX.mjs";
import { d as withWorktreeSources, s as resolveRepository } from "./service-preparation-BB0VCcOe.mjs";
import { n as prepareLocalWorkspaceTemplate } from "./local-workspace-template-qvQ3aHg1.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/gateway/worker-environments/local-workspace-prewarm.ts
const log = createSubsystemLogger("agents/worktrees");
async function prewarmLocalWorkspaceTemplates(params) {
const config = params.getConfig();
if (config.worktreeAcceleration === false) return;
const controller = new AbortController();
const signal = AbortSignal.any([params.signal, controller.signal]);
const unsubscribe = sessionChanges.subscribe((change) => {
if ("all" in change && typeof change.scope === "string" && change.scope.startsWith("config")) controller.abort();
});
const commitGuard = () => {
if (params.getConfig() !== config) controller.abort();
signal.throwIfAborted();
};
const env = process.env;
try {
for (const agentId of listAgentIds(config)) {
const sandbox = resolveSandboxConfigForAgent(config, agentId);
if (sandbox.backend !== "docker" && sandbox.backend !== "podman") continue;
try {
commitGuard();
const repository = await resolveRepository(resolveAgentWorkspaceDir(config, agentId));
await withWorktreeRunEnd(env, () => withWorktreeMutationLease({
env,
signal,
commitGuard,
id: `template-prewarm:${agentId}`
}, (guard) => withWorktreeSources(env, async (retainRepository) => {
await retainRepository({
...guard,
repository
});
const base = await resolveWorktreeBase(repository.repoRoot, void 0, signal, guard.commitGuard);
const templateRoot = path.join(await fs.realpath(resolveStateDir(env)), "worktree-projections");
guard.commitGuard();
await fs.mkdir(templateRoot, {
recursive: true,
mode: 448
});
await (await prepareLocalWorkspaceTemplate({
source: repository.repoRoot,
repoRoot: repository.repoRoot,
baseCommit: base.commit,
temporaryRoot: templateRoot,
templateRoot,
env,
sandbox,
guard: {
...guard,
signal: guard.signal ?? signal
}
}))?.record.release();
})));
} catch {
if (signal.aborted) return;
log.debug(`sandbox dependency template prewarm unavailable for agent ${agentId}`);
}
}
} finally {
unsubscribe();
}
}
//#endregion
export { prewarmLocalWorkspaceTemplates };