The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 01:45 UTC
fix(server): storage cleanup no longer counts hardlinked files as recovered space (#17910)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Tristan Knight committed
Oct 19, 2026 at 21:20 UTC
cae5762bddf3d1d8c3b203efe8300c8b9f68144c
2 files changed
+78
-15
apps/server/src/storageCleanup.test.ts
+70
-13
@@ -5,6 +5,7 @@ import * as Deferred from "effect/Deferred";
5
import * as Fiber from "effect/Fiber";
6
import * as FileSystem from "effect/FileSystem";
7
import * as Layer from "effect/Layer";
8
+import * as Option from "effect/Option";
9
import type * as Path from "effect/Path";
10
import type * as Scope from "effect/Scope";
11
import * as Stream from "effect/Stream";
@@ -236,6 +237,16 @@ describe("merged pull request cleanup", () => {
237
});
238
});
239
240
+const allocatedBytes = (fs: FileSystem.FileSystem, file: string) =>
241
+ fs.stat(file).pipe(
242
+ Effect.map((stat) =>
243
+ Option.getOrElse(
244
+ Option.map(stat.blocks, (blocks) => Number(blocks) * 512),
245
+ () => Number(stat.size),
246
+ ),
247
+ ),
248
+ );
249
+
250
const cleanupFixture = (
251
storageCleanup: StorageCleanupSettings = {
252
...DEFAULT_SERVER_SETTINGS.storageCleanup,
@@ -823,6 +834,7 @@ describe("orphan worktree reports", () => {
834
orphan = `${fixture.config.worktreesDir}/repository/orphan`;
835
yield* fs.makeDirectory(orphan, { recursive: true });
836
yield* fs.writeFileString(`${orphan}/file`, "x");
837
+ const bytes = yield* allocatedBytes(fs, `${orphan}/file`);
838
yield* fs.utimes(orphan, 1, 1);
839
const later = `${fixture.config.worktreesDir}/repository/z-later`;
840
yield* fs.makeDirectory(later);
@@ -841,18 +853,20 @@ describe("orphan worktree reports", () => {
853
expect(orphanEntry(firstReport)?.bytes).toBeNull();
854
expect(firstReport.entries.find((entry) => entry.path === later)?.bytes).toBeNull();
855
const nextReport = yield* fixture.service.runNow;
844
- expect(orphanEntry(nextReport)?.bytes).toBe(limit === "walk-timeout" ? null : 1);
856
+ expect(orphanEntry(nextReport)?.bytes).toBe(limit === "walk-timeout" ? null : bytes);
857
expect(nextReport.entries.find((entry) => entry.path === later)?.bytes).toBe(0);
846
- yield* fs.writeFileString(`${orphan}/file`, "changed");
858
+ yield* fs.writeFileString(`${orphan}/file`, "changed".repeat(10_000));
859
+ const changedBytes = yield* allocatedBytes(fs, `${orphan}/file`);
860
+ expect(changedBytes).not.toBe(bytes);
861
expect(orphanEntry(yield* fixture.service.runNow)?.bytes).toBe(
848
- limit === "walk-timeout" ? null : 1,
862
+ limit === "walk-timeout" ? null : bytes,
863
);
864
expect(walks).toBe(limit === "walk-timeout" ? 1 : 2);
865
if (limit === "walk-timeout") return;
866
fixture.setStoredWorktreePaths([orphan]);
867
expect(orphanEntry(yield* fixture.service.runNow)).toBeUndefined();
868
fixture.setStoredWorktreePaths([]);
855
- expect(orphanEntry(yield* fixture.service.runNow)?.bytes).toBe(7);
869
+ expect(orphanEntry(yield* fixture.service.runNow)?.bytes).toBe(changedBytes);
870
expect(walks).toBe(3);
871
}),
872
),
@@ -868,7 +882,7 @@ describe("orphan worktree reports", () => {
882
const orphan = `${fixture.config.worktreesDir}/${location === "nested" ? "repository/" : ""}orphan`;
883
yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "orphan", orphan]);
884
const sizes = yield* Effect.forEach(yield* fixture.fs.readDirectory(orphan), (name) =>
871
- fixture.fs.stat(`${orphan}/${name}`).pipe(Effect.map((stat) => Number(stat.size))),
885
+ allocatedBytes(fixture.fs, `${orphan}/${name}`),
886
);
887
const outside = yield* fixture.fs.makeTempDirectoryScoped();
888
yield* fixture.fs.writeFileString(`${outside}/external`, "outside data");
@@ -1059,7 +1073,11 @@ describe("orphan worktree reports", () => {
1073
yield* fixture.fs.utimes(orphan, 1, 1);
1074
const report = yield* fixture.service.runNow;
1075
expect(report.entries).toEqual([
1062
- expect.objectContaining({ path: orphan, outcome: "kept", bytes: 1 }),
1076
+ expect.objectContaining({
1077
+ path: orphan,
1078
+ outcome: "kept",
1079
+ bytes: yield* allocatedBytes(fixture.fs, `${orphan}/file`),
1080
+ }),
1081
]);
1082
expect(yield* fixture.fs.exists(`${project}/src/nested`)).toBe(true);
1083
expect(yield* fixture.fs.exists(orphan)).toBe(true);
@@ -1103,7 +1121,7 @@ describe("orphan worktree reports", () => {
1121
expect.objectContaining({
1122
path: folder,
1123
outcome: "kept",
1106
- bytes: 13,
1124
+ bytes: yield* allocatedBytes(fixture.fs, `${folder}/payload`),
1125
reason: "No thread uses this folder, and it isn't a git worktree",
1126
}),
1127
]);
@@ -1131,7 +1149,11 @@ describe("orphan worktree reports", () => {
1149
Effect.ensuring(fixture.fs.chmod(blocked, 0o700).pipe(Effect.orDie)),
1150
);
1151
expect(report.entries).toEqual([
1134
- expect.objectContaining({ path: orphan, outcome: "kept", bytes: 1 }),
1152
+ expect.objectContaining({
1153
+ path: orphan,
1154
+ outcome: "kept",
1155
+ bytes: yield* allocatedBytes(fixture.fs, `${orphan}/file`),
1156
+ }),
1157
]);
1158
expect(yield* fixture.fs.exists(orphan)).toBe(true);
1159
}),
@@ -1178,7 +1200,11 @@ describe("orphan worktree reports", () => {
1200
}
1201
const report = yield* fixture.service.runNow;
1202
expect(report.entries).toHaveLength(200);
1181
- expect(report.entries.every((entry) => entry.bytes === 1)).toBe(true);
1203
+ const bytes = yield* allocatedBytes(
1204
+ fixture.fs,
1205
+ `${fixture.config.worktreesDir}/repository/orphan-0/file`,
1206
+ );
1207
+ expect(report.entries.every((entry) => entry.bytes === bytes)).toBe(true);
1208
expect(report.omittedCount).toBe(5);
1209
expect(report.counts).toEqual({ removed: 0, kept: 205, failed: 0 });
1210
expect(report.bytesFreed).toBe(0);
@@ -1249,26 +1275,57 @@ describe("storage cleanup reports and local file policies", () => {
1275
),
1276
);
1277
1252
- it.live("measures nested regular files without following symlinks", () =>
1278
+ it.live("reports allocated bytes of single-link files without following symlinks", () =>
1279
runCleanupTest(
1280
Effect.gen(function* () {
1281
const { service, fs, worktree, setPolicy } = yield* cleanupFixture();
1282
setPolicy("tracked-changes");
1283
const sizes = yield* Effect.forEach(yield* fs.readDirectory(worktree), (name) =>
1258
- fs.stat(`${worktree}/${name}`).pipe(Effect.map((stat) => Number(stat.size))),
1284
+ allocatedBytes(fs, `${worktree}/${name}`),
1285
);
1286
const outside = yield* fs.makeTempDirectoryScoped();
1287
yield* fs.writeFileString(`${outside}/external`, "must not count");
1288
+ yield* fs.writeFileString(`${outside}/store-file`, "shared ".repeat(10_000));
1289
yield* fs.makeDirectory(`${worktree}/node_modules/nested`, { recursive: true });
1290
yield* fs.writeFileString(`${worktree}/node_modules/nested/file`, "count me");
1291
+ sizes.push(yield* allocatedBytes(fs, `${worktree}/node_modules/nested/file`));
1292
+ yield* fs.link(`${outside}/store-file`, `${worktree}/node_modules/hardlinked`);
1293
yield* fs.symlink(outside, `${worktree}/node_modules/directory-link`);
1294
yield* fs.symlink(`${outside}/external`, `${worktree}/node_modules/file-link`);
1295
yield* fs.symlink(`${outside}/missing`, `${worktree}/node_modules/dangling-link`);
1296
const report = yield* service.runNow;
1268
- const bytes = sizes.reduce((sum, size) => sum + size, 8);
1297
+ const bytes = sizes.reduce((sum, size) => sum + size, 0);
1298
expect(report.entries[0]).toMatchObject({ outcome: "removed", bytes, files: null });
1299
expect(report.bytesFreed).toBe(bytes);
1300
expect(yield* fs.readFileString(`${outside}/external`)).toBe("must not count");
1301
+ expect(yield* fs.readFileString(`${outside}/store-file`)).toBe("shared ".repeat(10_000));
1302
+ expect(yield* fs.exists(worktree)).toBe(false);
1303
+ }),
1304
+ ),
1305
+ );
1306
+ it.live("falls back to file lengths when link counts and allocated blocks are unavailable", () =>
1307
+ runCleanupTest(
1308
+ Effect.gen(function* () {
1309
+ const fs = yield* FileSystem.FileSystem;
1310
+ const { service, worktree } = yield* cleanupFixture().pipe(
1311
+ Effect.provideService(FileSystem.FileSystem, {
1312
+ ...fs,
1313
+ stat: (path) =>
1314
+ fs
1315
+ .stat(path)
1316
+ .pipe(
1317
+ Effect.map((stat) => ({ ...stat, nlink: Option.none(), blocks: Option.none() })),
1318
+ ),
1319
+ }),
1320
+ );
1321
+ const sizes = yield* Effect.forEach(yield* fs.readDirectory(worktree), (name) =>
1322
+ fs.stat(`${worktree}/${name}`).pipe(Effect.map((stat) => Number(stat.size))),
1323
+ );
1324
+ const bytes = sizes.reduce((sum, size) => sum + size, 0);
1325
+ const report = yield* service.runNow;
1326
+ expect(report.entries[0]).toMatchObject({ outcome: "removed", bytes });
1327
+ expect(report.bytesFreed).toBe(bytes);
1328
+ expect(yield* fs.exists(worktree)).toBe(false);
1329
}),
1330
),
1331
);
@@ -1484,7 +1541,7 @@ describe("storage cleanup reports and local file policies", () => {
1541
expect(yield* service.latestReport).toBeNull();
1542
yield* fs.writeFileString(`${worktree}/${file}`, "local data\n");
1543
const sizes = yield* Effect.forEach(yield* fs.readDirectory(worktree), (name) =>
1487
- fs.stat(`${worktree}/${name}`).pipe(Effect.map((stat) => Number(stat.size))),
1544
+ allocatedBytes(fs, `${worktree}/${name}`),
1545
);
1546
const bytes = sizes.reduce((sum, size) => sum + size, 0);
1547
const report = yield* service.runNow;
apps/server/src/storageCleanup.ts
+8
-2
@@ -270,8 +270,14 @@ const make = Effect.gen(function* () {
270
if (isLink) continue;
271
if ((yield* fs.realPath(target)) !== target) return null;
272
const stat = yield* fs.stat(target);
273
- if (stat.type === "File") bytes += Number(stat.size);
274
- else if (stat.type === "Directory") {
273
+ if (stat.type === "File") {
274
+ // Removing a shared hardlink leaves its blocks allocated.
275
+ if (Option.getOrElse(stat.nlink, () => 1n) !== 1n) continue;
276
+ bytes += Option.match(stat.blocks, {
277
+ onNone: () => Number(stat.size),
278
+ onSome: (blocks) => Number(blocks * 512n),
279
+ });
280
+ } else if (stat.type === "Directory") {
281
const names = yield* fs.readDirectory(target);
282
entries += names.length;
283
if (entries > 2_000_000) return null;