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 @@
## 2024-07-17 - Optimize I/O batching across files
**Learning:** Optimizing file operations across many files requires batching concurrent `fs.stat` and `fs.rm` (e.g., `Promise.all` with chunk size of 10) to avoid EMFILE limits, while state-dependent operations (like calculating cumulative totals) must build their removal queue sequentially before concurrent execution.
**Action:** When refactoring sequential file system operations into batched concurrent promises, always separate state calculation into a sequential pass and restrict batched promises to pure I/O effects.
54 changes: 34 additions & 20 deletions src/core/sub-agent/artifacts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,22 @@ 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());

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);
if (!isWithinDir(root, filePath)) return null;
const stat = await fs.stat(filePath).catch(() => null);
if (!stat) return null;
return { name: entry.name, path: filePath, mtimeMs: stat.mtimeMs, size: stat.size };
}),
);
for (const s of stats) {
if (s) files.push(s);
}
}

const maxAgeMs = options?.maxAgeMs ?? LIMITS.artifactTtlMs;
Expand All @@ -109,18 +118,7 @@ export class ArtifactStore {
const nowMs = Date.now();
const expired = files.filter((f) => nowMs - f.mtimeMs > maxAgeMs);

let removedFiles = 0;
let removedBytes = 0;

const removeFile = async (file: { path: string; size: number }) => {
await fs.rm(file.path, { force: true }).catch(() => null);
removedFiles += 1;
removedBytes += file.size;
};

for (const file of expired) {
await removeFile(file);
}
const toRemove: Array<{ path: string; size: number }> = [...expired];

// Recompute remaining after TTL removal (newest first).
const remaining = files
Expand All @@ -136,11 +134,27 @@ export class ArtifactStore {
if (!tooManyFiles && !tooManyBytes) break;

const oldest = remaining[i];
await removeFile(oldest);
toRemove.push(oldest);
currentFiles -= 1;
currentBytes -= oldest.size;
}

let removedFiles = 0;
let removedBytes = 0;

// Process removal queue in chunks concurrently
for (let i = 0; i < toRemove.length; i += 10) {
const chunk = toRemove.slice(i, i + 10);
await Promise.all(
chunk.map(async (file) => {
await fs.rm(file.path, { force: true }).catch(() => null);
// Counters updated synchronously post-operation since Promise.all guarantees completion
removedFiles += 1;
removedBytes += file.size;
}),
);
}

return { removedFiles, removedBytes };
}

Expand Down
Loading