@devries-cameron20 / t3code-1 / commits / 5ec4f500ef
Fork 0

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

fix(server): cleanup refreshes the size of a kept orphan worktree (#39017)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 19, 2026 at 09:17 UTC 5ec4f500ef36adf517c9944d1e2190aad719fb84
3 files changed +225 -24
apps/server/src/storageCleanup.test.ts
+95
@@ -1,5 +1,6 @@
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import * as Clock from "effect/Clock";
4 import * as Effect from "effect/Effect";
5 import * as Context from "effect/Context";
6 import type * as Digest from "effect/crypto/Digest";
@@ -2395,6 +2396,100 @@ describe("orphan worktree reports", () => {
2396 ),
2397 );
2398
2399 + it.effect(
2400 + "measures new orphans first and rotates cached refreshes when the budget runs out, not counting discovery",
2401 + () =>
2402 + runCleanupTest(
2403 + Effect.gen(function* () {
2404 + const fs = yield* FileSystem.FileSystem;
2405 + const clock = yield* Clock.Clock;
2406 + let now = NOW_MS;
2407 + let parent: string | undefined;
2408 + let slowDirectory: string | undefined;
2409 + let slowDiscovery: string | undefined;
2410 + const fixture = yield* cleanupFixture().pipe(
2411 + Effect.provideService(Clock.Clock, {
2412 + ...clock,
2413 + currentTimeMillis: Effect.sync(() => now),
2414 + }),
2415 + Effect.provideService(FileSystem.FileSystem, {
2416 + ...fs,
2417 + readDirectory: (directory, options) =>
2418 + fs.readDirectory(directory, options).pipe(
2419 + Effect.map((names) => {
2420 + if (directory === slowDirectory || directory === slowDiscovery) now += 30_000;
2421 + return directory === parent ? names.sort() : names;
2422 + }),
2423 + ),
2424 + }),
2425 + );
2426 + fixture.setThreads([]);
2427 + parent = `${fixture.config.worktreesDir}/repository`;
2428 + const cached = `${parent}/a-cached`;
2429 + const later = `${parent}/b-cached`;
2430 + const discovered = `${parent}/c-discovered`;
2431 + for (const directory of [cached, later]) {
2432 + yield* fs.makeDirectory(`${directory}/build`, { recursive: true });
2433 + yield* fs.utimes(directory, 1, 1);
2434 + }
2435 + const bundle = `${later}/build/bundle.js`;
2436 + yield* fs.writeFileString(bundle, "x".repeat(64 * 1024));
2437 + const size = yield* allocatedBytes(fs, bundle);
2438 + const first = yield* fixture.service.runNow;
2439 + expect(first.entries.find((entry) => entry.path === later)?.bytes).toBe(size);
2440 +
2441 + slowDirectory = `${cached}/build`;
2442 + yield* fs.remove(bundle);
2443 + yield* fs.makeDirectory(discovered);
2444 + yield* fs.utimes(discovered, 1, 1);
2445 + const second = yield* fixture.service.runNow;
2446 + expect(second.entries.find((entry) => entry.path === discovered)?.bytes).toBe(0);
2447 + expect(second.entries.find((entry) => entry.path === later)?.bytes).toBe(size);
2448 +
2449 + slowDiscovery = parent;
2450 + const third = yield* fixture.service.runNow;
2451 + expect(third.entries.find((entry) => entry.path === later)?.bytes).toBe(0);
2452 + }),
2453 + ),
2454 + );
2455 +
2456 + it.live("retains the last orphan size on a failed refresh and can refresh it later", () =>
2457 + runCleanupTest(
2458 + Effect.gen(function* () {
2459 + const fs = yield* FileSystem.FileSystem;
2460 + let unreadable: string | undefined;
2461 + const fixture = yield* cleanupFixture().pipe(
2462 + Effect.provideService(FileSystem.FileSystem, {
2463 + ...fs,
2464 + readDirectory: (directory, options) =>
2465 + fs.readDirectory(
2466 + directory === unreadable ? `${directory}/missing-directory` : directory,
2467 + options,
2468 + ),
2469 + }),
2470 + );
2471 + fixture.setThreads([]);
2472 + const orphan = `${fixture.config.worktreesDir}/repository/orphan`;
2473 + const build = `${orphan}/build`;
2474 + const bundle = `${build}/bundle.js`;
2475 + yield* fs.makeDirectory(build, { recursive: true });
2476 + yield* fs.writeFileString(bundle, "build output");
2477 + yield* fs.utimes(orphan, 1, 1);
2478 + const size = yield* allocatedBytes(fs, bundle);
2479 + const orphanBytes = Effect.map(
2480 + fixture.service.runNow,
2481 + (report) => report.entries.find((entry) => entry.path === orphan)?.bytes,
2482 + );
2483 + expect(yield* orphanBytes).toBe(size);
2484 + yield* fs.remove(bundle);
2485 + unreadable = build;
2486 + expect(yield* orphanBytes).toBe(size);
2487 + unreadable = undefined;
2488 + expect(yield* orphanBytes).toBe(0);
2489 + }),
2490 + ),
2491 + );
2492 +
2493 it.live("continues after an inaccessible directory's git probe", (context) =>
2494 runCleanupTest(
2495 Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+45 -24
@@ -519,6 +519,7 @@ const make = Effect.gen(function* () {
519 projects: ReadonlyArray<{ readonly workspaceRoot: string }>,
520 entries: StorageCleanupReportEntry[],
521 scratchRoot: string | null,
522 + trigger: StorageCleanupReport["trigger"],
523 ) {
524 const rows = yield* sql<{ worktreePath: string }>`
525 SELECT DISTINCT json_extract(payload_json, '$.worktreePath') AS "worktreePath"
@@ -573,11 +574,8 @@ const make = Effect.gen(function* () {
574 }
575 return result;
576 });
576 - // Let the first walk time out and cache unmeasurable folders; later walks share the remaining budget.
577 const now = yield* Clock.currentTimeMillis;
578 - const measurementDeadline = now + 30_000;
579 - let firstMeasurement = true;
580 - const reported = new Set<string>();
578 + const reported = new Map<string, { path: string; reason: string }>();
579 const report = Effect.fnUntraced(
580 function* (worktreePath: string, scratch = false) {
581 const key = pathKey(worktreePath);
@@ -592,30 +590,13 @@ const make = Effect.gen(function* () {
590 if (modified !== null && modified.getTime() > now - ORPHAN_WORKTREE_GRACE_MS) return;
591 const gitPath = path.join(worktreePath, ".git");
592 const linked = (yield* fs.exists(gitPath)) && (yield* isLinkedWorktree(worktreePath));
595 - reported.add(key);
596 - const remaining = measurementDeadline - (yield* Clock.currentTimeMillis);
597 - if (!orphanSizes.has(key) && (firstMeasurement || remaining > 0)) {
598 - const measured = yield* measureWorktree(worktreePath).pipe(
599 - firstMeasurement ? Effect.asSome : Effect.timeoutOption(remaining),
600 - );
601 - firstMeasurement = false;
602 - if (Option.isSome(measured)) orphanSizes.set(key, measured.value.bytes);
603 - }
604 - const bytes = orphanSizes.get(key) ?? null;
605 - entries.push({
606 - kind: "worktree",
607 - outcome: "kept",
593 + reported.set(key, {
594 + path: worktreePath,
595 reason: linked
596 ? "No thread uses this worktree"
597 : scratch
598 ? "No thread uses this folder"
599 : "No thread uses this folder, and it isn't a git worktree",
613 - path: worktreePath,
614 - projectPath: null,
615 - threadId: null,
616 - threadTitle: null,
617 - bytes,
618 - files: null,
600 });
601 },
602 Effect.orElseSucceed(() => undefined),
@@ -659,6 +640,44 @@ const make = Effect.gen(function* () {
640 }
641 if (scratchRoot !== null)
642 for (const folder of yield* directories(scratchRoot, null)) yield* report(folder, true);
643 +
644 + // Discovery runs git probes, so the measurement budget starts after it.
645 + const measurementDeadline = (yield* Clock.currentTimeMillis) + 30_000;
646 + // Give the first new folder a full walk so every sweep can make progress.
647 + let firstMeasurement = true;
648 + const refresh = trigger === "manual" ? [...orphanSizes] : [];
649 + for (const [key, { path }] of reported) {
650 + if (orphanSizes.has(key)) continue;
651 + const remaining = measurementDeadline - (yield* Clock.currentTimeMillis);
652 + if (!firstMeasurement && remaining <= 0) break;
653 + const measured = yield* measureWorktree(path).pipe(
654 + firstMeasurement ? Effect.asSome : Effect.timeoutOption(remaining),
655 + );
656 + firstMeasurement = false;
657 + if (Option.isSome(measured)) orphanSizes.set(key, measured.value.bytes);
658 + }
659 + for (const [key, bytes] of refresh) {
660 + const orphan = reported.get(key);
661 + if (orphan === undefined || bytes === null) continue;
662 + const remaining = measurementDeadline - (yield* Clock.currentTimeMillis);
663 + if (remaining <= 0) break;
664 + const measured = yield* measureWorktree(orphan.path).pipe(Effect.timeoutOption(remaining));
665 + // A slow or failed refresh moves to the end too, so it cannot starve the next orphan.
666 + orphanSizes.delete(key);
667 + orphanSizes.set(key, Option.getOrUndefined(measured)?.bytes ?? bytes);
668 + }
669 + for (const [key, orphan] of reported) {
670 + entries.push({
671 + ...orphan,
672 + kind: "worktree",
673 + outcome: "kept",
674 + projectPath: null,
675 + threadId: null,
676 + threadTitle: null,
677 + bytes: orphanSizes.get(key) ?? null,
678 + files: null,
679 + });
680 + }
681 for (const key of orphanSizes.keys()) {
682 if (!reported.has(key)) orphanSizes.delete(key);
683 }
@@ -1024,6 +1043,7 @@ const make = Effect.gen(function* () {
1043 serverSettings: ServerSettings,
1044 now: number,
1045 entries: StorageCleanupReportEntry[],
1046 + trigger: StorageCleanupReport["trigger"],
1047 ) {
1048 if (!anyWorktreePolicy(serverSettings, worktreeCleanupEnabled))
1049 return { waiting: false, kept: [] };
@@ -1606,6 +1626,7 @@ const make = Effect.gen(function* () {
1626 yield* projectStore.list({ includeDeleted: true }),
1627 entries,
1628 scratchRoot,
1629 + trigger,
1630 );
1631 return { waiting, kept };
1632 });
@@ -1775,7 +1796,7 @@ const make = Effect.gen(function* () {
1796 yield* cleanCheckpointRefs().pipe(
1797 Effect.catch((cause) => Effect.logWarning("checkpoint ref cleanup failed", { cause })),
1798 );
1778 - const deletedWorktrees = yield* cleanWorktrees(serverSettings, now, entries).pipe(
1799 + const deletedWorktrees = yield* cleanWorktrees(serverSettings, now, entries, trigger).pipe(
1800 Effect.catch((error) =>
1801 Effect.gen(function* () {
1802 entries.push({
scripts/scenarios/orphan-worktree-cleanup-refreshes-size.e2e.ts new
+85
@@ -0,0 +1,85 @@
1 +// @effect-diagnostics nodeBuiltinImport:off globalJsonInEffect:off - the scenario seeds an 8 MB file and rewrites settings.json as the repro wrote it.
2 +import * as NodeCrypto from "node:crypto";
3 +import { expect } from "e2e";
4 +import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 +import * as Schema from "effect/Schema";
7 +
8 +import { T3_HOME } from "../e2e.config.ts";
9 +import { run } from "./shell.ts";
10 +import { runPromise, test, WEB } from "./t3.ts";
11 +
12 +const decodeSettings = Schema.decodeSync(
13 + Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
14 +);
15 +
16 +test(
17 + "Cleanup updates the displayed size of a kept worktree",
18 + WEB,
19 + async ({ t3, app, browser, screen }) => {
20 + const project = await t3.project("cleanup-size", { "README.md": "Example project" });
21 + const settingsPath = `${T3_HOME}/userdata/settings.json`;
22 + const original = await runPromise(
23 + Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(settingsPath)),
24 + );
25 + const worktree = `${T3_HOME}/worktrees/cleanup-size-orphan`;
26 + const build = `${worktree}/build`;
27 + const git = (...args: ReadonlyArray<string>) => runPromise(run("git", args, { cwd: project }));
28 + await git("worktree", "add", "--detach", worktree, "HEAD");
29 + try {
30 + await runPromise(
31 + Effect.gen(function* () {
32 + const fs = yield* FileSystem.FileSystem;
33 + yield* fs.makeDirectory(build);
34 + yield* fs.writeFile(`${build}/bundle.js`, NodeCrypto.randomBytes(8 * 1024 * 1024));
35 + yield* fs.utimes(worktree, 1, 1);
36 + const settings = decodeSettings(original);
37 + const storageCleanup =
38 + typeof settings.storageCleanup === "object" && settings.storageCleanup !== null
39 + ? settings.storageCleanup
40 + : {};
41 + yield* fs.writeFileString(
42 + settingsPath,
43 + JSON.stringify({
44 + ...settings,
45 + storageCleanup: { ...storageCleanup, worktreeOnDelete: true },
46 + }),
47 + );
48 + }),
49 + );
50 +
51 + await t3.open("/settings/maintenance");
52 + const runCleanup = screen.getByRole("button", "Delete now", { exact: true });
53 + await runCleanup.tap();
54 + await screen.getByRole("button", /^Last run/).tap();
55 + await screen.getByText(/^8\.\d{2} MB$/).waitFor();
56 + const orphanRow = browser
57 + .locator('#storage-cleanup-last-run [data-slot="settings-row"]')
58 + .filter({ has: screen.getByText(worktree, { exact: true }) });
59 + await orphanRow.getByText(/^8\.\d{2} MB$/).waitFor();
60 +
61 + await runPromise(
62 + Effect.gen(function* () {
63 + const fs = yield* FileSystem.FileSystem;
64 + yield* fs.remove(`${build}/bundle.js`);
65 + }),
66 + );
67 + await runCleanup.tap();
68 + await runCleanup.waitFor();
69 + await screen.getByText("No thread uses this worktree", { exact: true }).scrollIntoView();
70 + try {
71 + await expect(orphanRow.getByText(/^\d+(?:\.\d+)? KB$/)).toBeVisible();
72 + await expect(browser.locator("#storage-cleanup-last-run")).toContainText(
73 + /\b\d+(?:\.\d+)? KB\b/,
74 + );
75 + } finally {
76 + await app.screenshot("kept-worktree-after-build-file-removal");
77 + }
78 + } finally {
79 + await git("worktree", "remove", "--force", worktree);
80 + await runPromise(
81 + Effect.flatMap(FileSystem.FileSystem, (fs) => fs.writeFileString(settingsPath, original)),
82 + );
83 + }
84 + },
85 +);