Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-02-28 - Batch concurrent fs operations to avoid EMFILE limits
**Learning:** When iterating over a large number of files in `ArtifactStore.gc`, sequential `fs.stat` and `fs.rm` checks create performance bottlenecks and sequential loops. Although fully parallelizing could cause EMFILE errors, batched concurrent execution solves both performance and limit issues.
**Action:** Use chunked batched `Promise.all` requests with a chunk size of 10 for mass `fs.stat` and `fs.rm` operations.
32 changes: 23 additions & 9 deletions src/core/sub-agent/artifacts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
if (!e.isFile()) return false;
const filePath = path.join(root, e.name);
return isWithinDir(root, filePath);
});

for (let i = 0; i < validEntries.length; i += 10) {
const chunk = validEntries.slice(i, i + 10);
const stats = await Promise.all(
chunk.map(async (entry) => {
const filePath = path.join(root, entry.name);
const stat = await fs.stat(filePath).catch(() => null);
return { entry, filePath, stat };
}),
);
for (const { entry, filePath, stat } of stats) {
if (stat) {
files.push({ name: entry.name, path: filePath, mtimeMs: stat.mtimeMs, size: stat.size });
}
}
}

const maxAgeMs = options?.maxAgeMs ?? LIMITS.artifactTtlMs;
Expand All @@ -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).
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/prompt_templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
Expand Down
Loading