@devries-cameron20 / t3code-1 / commits / e9eb473d52
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(server): Storage Cleanup sizes leftover worktrees about 4.8x faster (median 13,343 ms -> 2,759 ms) (#33937)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Andrei committed Nov 12, 2026 at 07:15 UTC e9eb473d52b5fa814819969cdc5b66b2915e028d
1 file changed +26 -28
apps/server/src/storageCleanup.ts
+26 -28
@@ -380,36 +380,34 @@ const make = Effect.gen(function* () {
380 let entries = 1;
381 let bytes = 0;
382 while (pending.length > 0) {
383 - const target = pending.pop()!;
384 - if (target === skipped) continue;
385 - // Effect's stat follows links. Probe with readLink first to get lstat
386 - // semantics, including skipping dangling links and directory links.
387 - const isLink = yield* fs.readLink(target).pipe(
388 - Effect.as(true),
389 - Effect.catchIf(
390 - (error) =>
391 - error.cause instanceof Error &&
392 - "code" in error.cause &&
393 - error.cause.code === "EINVAL",
394 - () => Effect.succeed(false),
395 - ),
383 + const inspected = yield* Effect.forEach(
384 + pending.splice(-8),
385 + // Untraced: this runs once per entry, and a worktree can hold millions.
386 + Effect.fnUntraced(function* (target) {
387 + if (target === skipped) return { target, bytes: 0, names: [] };
388 + const stat = yield* fs.lstat(target);
389 + if (stat.type === "SymbolicLink") return { target, bytes: 0, names: [] };
390 + if ((yield* fs.realPath(target)) !== target) return null;
391 + if (stat.type === "Directory")
392 + return { target, bytes: 0, names: yield* fs.readDirectory(target) };
393 + // Removing a shared hardlink leaves its blocks allocated.
394 + if (stat.type !== "File" || Option.getOrElse(stat.nlink, () => 1n) !== 1n)
395 + return { target, bytes: 0, names: [] };
396 + const bytes = Option.match(stat.blocks, {
397 + onNone: () => Number(stat.size),
398 + onSome: (blocks) => Number(blocks * 512n),
399 + });
400 + return { target, bytes, names: [] };
401 + }),
402 + { concurrency: "unbounded" },
403 );
397 - if (isLink) continue;
398 - if ((yield* fs.realPath(target)) !== target) return null;
399 - const stat = yield* fs.stat(target);
400 - if (stat.type === "File") {
401 - // Removing a shared hardlink leaves its blocks allocated.
402 - if (Option.getOrElse(stat.nlink, () => 1n) !== 1n) continue;
403 - bytes += Option.match(stat.blocks, {
404 - onNone: () => Number(stat.size),
405 - onSome: (blocks) => Number(blocks * 512n),
406 - });
407 - } else if (stat.type === "Directory") {
408 - const names = yield* fs.readDirectory(target);
409 - entries += names.length;
404 + for (const result of inspected) {
405 + if (result === null) return null;
406 + bytes += result.bytes;
407 + entries += result.names.length;
408 if (entries > 2_000_000) return null;
411 - for (const name of names) {
412 - const child = path.join(target, name);
409 + for (const name of result.names) {
410 + const child = path.join(result.target, name);
411 if (!inside(root, child)) return null;
412 pending.push(child);
413 }