diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..9ef7e933 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-11 - Optimize ArtifactStore garbage collection performance +**Learning:** Sequential fs.stat and fs.rm operations in large directories (like the artifact store after extended sessions) caused significant Event Loop blocking and O(N) execution time, dramatically slowing down `maybeGc()`. +**Action:** Replaced sequential awaits in `ArtifactStore.gc` loops with concurrent batching (Promise.all) in chunks of 10. Chunked iteration is essential because it avoids opening too many file descriptors (EMFILE) while vastly improving throughput over sequential await. diff --git a/src/core/sub-agent/artifacts/store.ts b/src/core/sub-agent/artifacts/store.ts index 8a7aff5c..e869b0a4 100644 --- a/src/core/sub-agent/artifacts/store.ts +++ b/src/core/sub-agent/artifacts/store.ts @@ -93,13 +93,26 @@ export class ArtifactStore { const entries = await fs.readdir(root, { withFileTypes: true }).catch(() => []); const files: Array<{ name: string; path: string; mtimeMs: number; size: number }> = []; - for (const entry of entries) { - if (!entry.isFile()) continue; - const filePath = path.join(root, entry.name); - if (!isWithinDir(root, filePath)) continue; - const stat = await fs.stat(filePath).catch(() => null); - if (!stat) continue; - files.push({ name: entry.name, path: filePath, mtimeMs: stat.mtimeMs, size: stat.size }); + const validEntries = entries.filter( + (e) => e.isFile() && isWithinDir(root, path.join(root, e.name)), + ); + + for (let i = 0; i < validEntries.length; i += 10) { + const chunk = validEntries.slice(i, i + 10); + await Promise.all( + chunk.map(async (entry) => { + const filePath = path.join(root, entry.name); + const stat = await fs.stat(filePath).catch(() => null); + if (stat) { + files.push({ + name: entry.name, + path: filePath, + mtimeMs: stat.mtimeMs, + size: stat.size, + }); + } + }), + ); } const maxAgeMs = options?.maxAgeMs ?? LIMITS.artifactTtlMs; @@ -118,8 +131,9 @@ export class ArtifactStore { removedBytes += file.size; }; - for (const file of expired) { - await removeFile(file); + for (let i = 0; i < expired.length; i += 10) { + const chunk = expired.slice(i, i + 10); + await Promise.all(chunk.map((file) => removeFile(file))); } // Recompute remaining after TTL removal (newest first). @@ -130,17 +144,23 @@ export class ArtifactStore { let currentFiles = remaining.length; let currentBytes = remaining.reduce((acc, f) => acc + f.size, 0); + const toRemove: Array<{ path: string; size: number }> = []; for (let i = remaining.length - 1; i >= 0; i--) { const tooManyFiles = currentFiles > maxFiles; const tooManyBytes = currentBytes > maxTotalBytes; if (!tooManyFiles && !tooManyBytes) break; const oldest = remaining[i]; - await removeFile(oldest); + toRemove.push(oldest); currentFiles -= 1; currentBytes -= oldest.size; } + for (let i = 0; i < toRemove.length; i += 10) { + const chunk = toRemove.slice(i, i + 10); + await Promise.all(chunk.map((file) => removeFile(file))); + } + return { removedFiles, removedBytes }; } diff --git a/tests/integration/prompt_templates.test.ts b/tests/integration/prompt_templates.test.ts index 21905259..f1360d78 100644 --- a/tests/integration/prompt_templates.test.ts +++ b/tests/integration/prompt_templates.test.ts @@ -36,7 +36,9 @@ describe('Prompt templates', () => { expect(planSystem).toContain('You are SalmonLoop.'); expect(patchSystem).toContain('You are PATCH, a phase-native diff compiler.'); - expect(autopilotSystem).toContain('You are a senior software engineer running in "autopilot" mode.'); + expect(autopilotSystem).toContain( + 'You are a senior software engineer running in "autopilot" mode.', + ); expect(answerSystem).toContain('You are a coding assistant in "answer" mode.'); expect(researchSystem).toContain('You are a research assistant.'); });