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 reaches archived threads' worktrees (#16473)
Co-authored-by: Khush-Koyani <khushkoyani@gmail.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Info-Cado committed
Oct 20, 2026 at 00:07 UTC
034b50616165fc25195e25bfddb188639bfefa34
4 files changed
+98
-22
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+11
-5
@@ -512,7 +512,7 @@ const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
512
assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
513
});
514
515
-const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
515
+const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
516
const store = yield* ProjectionStore.ProjectionStoreV2;
517
const now = yield* DateTime.now;
518
const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
@@ -565,6 +565,10 @@ const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
565
assert.deepEqual(yield* store.getThreadWorktreePaths(), [
566
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
567
]);
568
+ assert.deepEqual(yield* store.getThreadWorktreePaths({ includeArchived: true }), [
569
+ { threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
570
+ { threadId: ThreadId.make("thread:archived"), worktreePath: "/worktrees/archived" },
571
+ ]);
572
});
573
574
const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
@@ -650,8 +654,10 @@ it.effect("memory command reads skip finished leaf nodes of finished runs", () =
654
commandReadsSkipSettledLeafNodes.pipe(Effect.provide(ProjectionStore.layerMemory)),
655
);
656
653
-it.effect("memory worktree path lookup reads only active threads", () =>
654
- threadWorktreePathsReadActiveThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
657
+it.effect(
658
+ "memory worktree path lookup defaults to active threads and can include archived threads",
659
+ () =>
660
+ threadWorktreePathsIncludeArchivedOnRequest.pipe(Effect.provide(ProjectionStore.layerMemory)),
661
);
662
663
it.effect("memory projection keeps restart-cancelled work through a stale run.updated", () =>
@@ -878,8 +884,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
884
() => runlessNativeSubagentNeedsRecovery,
885
);
886
it.effect(
881
- "worktree path lookup reads only active threads",
882
- () => threadWorktreePathsReadActiveThreads,
887
+ "worktree path lookup defaults to active threads and can include archived threads",
888
+ () => threadWorktreePathsIncludeArchivedOnRequest,
889
);
890
891
it.effect(
apps/server/src/orchestration-v2/ProjectionStore.ts
+7
-5
@@ -422,7 +422,9 @@ export interface ProjectionStoreV2Shape {
422
readonly getThreadsWithPullRequests: (
423
threadId?: ThreadId,
424
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
425
- readonly getThreadWorktreePaths: () => Effect.Effect<
425
+ readonly getThreadWorktreePaths: (options?: {
426
+ readonly includeArchived?: boolean;
427
+ }) => Effect.Effect<
428
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
429
ProjectionStoreV2Error
430
>;
@@ -5739,12 +5741,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5741
);
5742
}).pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));
5743
5742
- const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = () =>
5744
+ const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = (options) =>
5745
sql<{ readonly thread_id: string; readonly worktree_path: string }>`
5746
SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path
5747
FROM orchestration_v2_projection_threads
5748
WHERE deleted_at IS NULL
5747
- AND json_extract(payload_json, '$.archivedAt') IS NULL
5749
+ ${options?.includeArchived === true ? sql`` : sql`AND json_extract(payload_json, '$.archivedAt') IS NULL`}
5750
AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
5751
`.pipe(
5752
Effect.map((rows) =>
@@ -6240,12 +6242,12 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
6242
left.id.localeCompare(right.id),
6243
);
6244
}),
6243
- getThreadWorktreePaths: () =>
6245
+ getThreadWorktreePaths: (options) =>
6246
Ref.get(replayState).pipe(
6247
Effect.map((state) =>
6248
[...state.projections.values()].flatMap(({ thread }) =>
6249
thread.deletedAt === null &&
6248
- thread.archivedAt === null &&
6250
+ (options?.includeArchived === true || thread.archivedAt === null) &&
6251
thread.worktreePath !== null
6252
? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
6253
: [],
apps/server/src/storageCleanup.test.ts
+75
-5
@@ -306,14 +306,26 @@ const cleanupFixture = (
306
getShellSnapshot: (input?: { location?: string }) =>
307
Effect.sync(() => {
308
snapshotReads++;
309
- return { threads: input?.location === "archive" ? [] : threads };
309
+ const visible = threads.filter((thread) => thread.deletedAt === null);
310
+ return {
311
+ threads:
312
+ input?.location === "archive"
313
+ ? []
314
+ : visible.filter((thread) => thread.archivedAt === null),
315
+ archivedThreads:
316
+ input?.location === "active"
317
+ ? []
318
+ : visible.filter((thread) => thread.archivedAt !== null),
319
+ };
320
}),
311
- getThreadWorktreePaths: () =>
321
+ getThreadWorktreePaths: (input?: { includeArchived?: boolean }) =>
322
Effect.sync(() =>
323
threads.flatMap((thread) =>
314
- thread.worktreePath === null
315
- ? []
316
- : [{ threadId: thread.id, worktreePath: thread.worktreePath }],
324
+ thread.deletedAt === null &&
325
+ (input?.includeArchived === true || thread.archivedAt === null) &&
326
+ thread.worktreePath !== null
327
+ ? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
328
+ : [],
329
),
330
),
331
getThreadShell: (threadId: ThreadId) =>
@@ -1176,6 +1188,41 @@ describe("orphan worktree reports", () => {
1188
});
1189
1190
describe("storage cleanup reports and local file policies", () => {
1191
+ it.effect("automatically removes an eligible archived thread checkout", () =>
1192
+ runCleanupTest(
1193
+ Effect.gen(function* () {
1194
+ yield* TestClock.setTime(NOW_MS);
1195
+ const fixture = yield* cleanupFixture();
1196
+ fixture.setThreads([
1197
+ shell({
1198
+ branch: "feature",
1199
+ worktreePath: fixture.worktree,
1200
+ archivedAt: at(-DAY_MS),
1201
+ }),
1202
+ ]);
1203
+ yield* fixture.activate(Stream.empty);
1204
+ const [report] = yield* fixture.service.reports.pipe(
1205
+ Stream.filter((report) => report !== null),
1206
+ Stream.take(1),
1207
+ Stream.runCollect,
1208
+ );
1209
+ yield* fixture.service.drain;
1210
+ expect(report).toMatchObject({
1211
+ trigger: "automatic",
1212
+ counts: { removed: 1, kept: 0, failed: 0 },
1213
+ entries: [
1214
+ {
1215
+ threadId: "thread-1",
1216
+ path: fixture.worktree,
1217
+ outcome: "removed",
1218
+ reason: "Inactive for 30 days",
1219
+ },
1220
+ ],
1221
+ });
1222
+ expect(yield* fixture.fs.exists(fixture.worktree)).toBe(false);
1223
+ }),
1224
+ ),
1225
+ );
1226
it.live.each(["before-check", "while-measuring"] as const)(
1227
"keeps a worktree protected by a missing project root (%s)",
1228
(timing) =>
@@ -1264,6 +1311,18 @@ describe("storage cleanup reports and local file policies", () => {
1311
shell({ id: ThreadId.make("thread-2"), branch: "feature", worktreePath: worktree }),
1312
],
1313
],
1314
+ [
1315
+ "an archived thread starts using it",
1316
+ (worktree: string) => [
1317
+ shell({ branch: "feature", worktreePath: worktree }),
1318
+ shell({
1319
+ id: ThreadId.make("thread-2"),
1320
+ branch: "feature",
1321
+ worktreePath: worktree,
1322
+ archivedAt: at(-DAY_MS),
1323
+ }),
1324
+ ],
1325
+ ],
1326
] as const)("keeps a worktree when %s during size measurement", ([, threadsWhileMeasuring]) =>
1327
runCleanupTest(
1328
Effect.gen(function* () {
@@ -1542,6 +1601,17 @@ describe("storage cleanup reports and local file policies", () => {
1601
shell({ id: ThreadId.make("thread-2"), branch: "feature", worktreePath: worktree }),
1602
]);
1603
expect((yield* service.runNow).entries[0]?.reason).toBe("Shared by 2 threads");
1604
+ setThreads([
1605
+ shell({ branch: "feature", worktreePath: worktree }),
1606
+ shell({
1607
+ id: ThreadId.make("thread-2"),
1608
+ branch: "feature",
1609
+ worktreePath: worktree,
1610
+ archivedAt: at(-DAY_MS),
1611
+ }),
1612
+ ]);
1613
+ expect((yield* service.runNow).entries[0]?.reason).toBe("Shared by 2 threads");
1614
+ expect(yield* fs.exists(worktree)).toBe(true);
1615
fixture.disable();
1616
expect((yield* service.runNow).entries).toEqual([]);
1617
yield* fs.remove(worktree, { recursive: true });
apps/server/src/storageCleanup.ts
+5
-7
@@ -291,11 +291,9 @@ const make = Effect.gen(function* () {
291
// The snapshot takes seconds on large databases, so it runs on the reader
292
// worker instead of the event loop.
293
const readThreads = Effect.fn("StorageCleanup.readThreads")(function* () {
294
- const { threads } = yield* projections
295
- .getShellSnapshot({ location: "active" })
296
- .pipe(NodeSqliteClient.readOnly);
294
+ const snapshot = yield* projections.getShellSnapshot().pipe(NodeSqliteClient.readOnly);
295
const projects = yield* projectStore.listShells();
298
- return { projects, threads };
296
+ return { projects, threads: [...snapshot.threads, ...snapshot.archivedThreads] };
297
});
298
299
const containsProjectRoot = Effect.fn("StorageCleanup.containsProjectRoot")(function* (
@@ -740,9 +738,9 @@ const make = Effect.gen(function* () {
738
const latestProjects = yield* projectStore.listShells();
739
if (yield* containsProjectRoot(worktreePath, [project, ...latestProjects]))
740
return "contains a project checkout";
743
- const latest = (yield* projections.getThreadWorktreePaths()).filter(
744
- (entry) => path.resolve(entry.worktreePath) === worktreePath,
745
- );
741
+ const latest = (yield* projections.getThreadWorktreePaths({
742
+ includeArchived: true,
743
+ })).filter((entry) => path.resolve(entry.worktreePath) === worktreePath);
744
if (hasTerminal(worktreePath)) return "open terminal";
745
if (deleted) {
746
if (