mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-27 00:17:29 +00:00
perf: cache short-term artifacts dir
This commit is contained in:
@@ -32,6 +32,7 @@ const PHASE_SIGNAL_LIGHT_BOOST_MAX = 0.05;
|
||||
const PHASE_SIGNAL_REM_BOOST_MAX = 0.08;
|
||||
const PHASE_SIGNAL_HALF_LIFE_DAYS = 14;
|
||||
const inProcessShortTermLocks = new Map<string, Promise<void>>();
|
||||
const ensuredShortTermDirs = new Map<string, Promise<void>>();
|
||||
|
||||
export type PromotionWeights = {
|
||||
frequency: number;
|
||||
@@ -520,6 +521,25 @@ function resolveLockPath(workspaceDir: string): string {
|
||||
return path.join(workspaceDir, SHORT_TERM_LOCK_RELATIVE_PATH);
|
||||
}
|
||||
|
||||
function resolveShortTermArtifactsDir(workspaceDir: string): string {
|
||||
return path.dirname(resolveLockPath(workspaceDir));
|
||||
}
|
||||
|
||||
async function ensureShortTermArtifactsDir(workspaceDir: string): Promise<void> {
|
||||
const artifactsDir = resolveShortTermArtifactsDir(workspaceDir);
|
||||
const existing = ensuredShortTermDirs.get(artifactsDir);
|
||||
if (existing) {
|
||||
await existing;
|
||||
return;
|
||||
}
|
||||
const ensuring = fs.mkdir(artifactsDir, { recursive: true }).catch((err) => {
|
||||
ensuredShortTermDirs.delete(artifactsDir);
|
||||
throw err;
|
||||
});
|
||||
ensuredShortTermDirs.set(artifactsDir, ensuring);
|
||||
await ensuring;
|
||||
}
|
||||
|
||||
function parseLockOwnerPid(raw: string): number | null {
|
||||
const match = raw.trim().match(/^(\d+):/);
|
||||
if (!match) {
|
||||
@@ -586,7 +606,7 @@ async function withInProcessShortTermLock<T>(lockPath: string, task: () => Promi
|
||||
async function withShortTermLock<T>(workspaceDir: string, task: () => Promise<T>): Promise<T> {
|
||||
const lockPath = resolveLockPath(workspaceDir);
|
||||
return withInProcessShortTermLock(lockPath, async () => {
|
||||
await fs.mkdir(path.dirname(lockPath), { recursive: true });
|
||||
await ensureShortTermArtifactsDir(workspaceDir);
|
||||
const startedAt = Date.now();
|
||||
|
||||
while (true) {
|
||||
@@ -721,7 +741,7 @@ async function writePhaseSignalStore(
|
||||
store: ShortTermPhaseSignalStore,
|
||||
): Promise<void> {
|
||||
const phaseSignalPath = resolvePhaseSignalPath(workspaceDir);
|
||||
await fs.mkdir(path.dirname(phaseSignalPath), { recursive: true });
|
||||
await ensureShortTermArtifactsDir(workspaceDir);
|
||||
const tmpPath = `${phaseSignalPath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
||||
await fs.writeFile(tmpPath, `${JSON.stringify(store, null, 2)}\n`, "utf-8");
|
||||
await fs.rename(tmpPath, phaseSignalPath);
|
||||
@@ -729,7 +749,7 @@ async function writePhaseSignalStore(
|
||||
|
||||
async function writeStore(workspaceDir: string, store: ShortTermRecallStore): Promise<void> {
|
||||
const storePath = resolveStorePath(workspaceDir);
|
||||
await fs.mkdir(path.dirname(storePath), { recursive: true });
|
||||
await ensureShortTermArtifactsDir(workspaceDir);
|
||||
const tmpPath = `${storePath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
||||
await fs.writeFile(tmpPath, `${JSON.stringify(store, null, 2)}\n`, "utf-8");
|
||||
await fs.rename(tmpPath, storePath);
|
||||
|
||||
Reference in New Issue
Block a user