Skip to content

Commit 034b506

Browse files
Info-Cadokhush-2106claudejuliusmarminge
authored
fix(server): storage cleanup reaches archived threads' worktrees (#16473)
Co-authored-by: Khush-Koyani <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b20f552 commit 034b506

4 files changed

Lines changed: 98 additions & 22 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
512512
assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
513513
});
514514

515-
const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
515+
const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
516516
const store = yield* ProjectionStore.ProjectionStoreV2;
517517
const now = yield* DateTime.now;
518518
const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
@@ -565,6 +565,10 @@ const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
565565
assert.deepEqual(yield* store.getThreadWorktreePaths(), [
566566
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
567567
]);
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+
]);
568572
});
569573

570574
const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
@@ -650,8 +654,10 @@ it.effect("memory command reads skip finished leaf nodes of finished runs", () =
650654
commandReadsSkipSettledLeafNodes.pipe(Effect.provide(ProjectionStore.layerMemory)),
651655
);
652656

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)),
655661
);
656662

657663
it.effect("memory projection keeps restart-cancelled work through a stale run.updated", () =>
@@ -878,8 +884,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
878884
() => runlessNativeSubagentNeedsRecovery,
879885
);
880886
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,
883889
);
884890

885891
it.effect(

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ export interface ProjectionStoreV2Shape {
422422
readonly getThreadsWithPullRequests: (
423423
threadId?: ThreadId,
424424
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
425-
readonly getThreadWorktreePaths: () => Effect.Effect<
425+
readonly getThreadWorktreePaths: (options?: {
426+
readonly includeArchived?: boolean;
427+
}) => Effect.Effect<
426428
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
427429
ProjectionStoreV2Error
428430
>;
@@ -5739,12 +5741,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57395741
);
57405742
}).pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));
57415743

5742-
const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = () =>
5744+
const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = (options) =>
57435745
sql<{ readonly thread_id: string; readonly worktree_path: string }>`
57445746
SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path
57455747
FROM orchestration_v2_projection_threads
57465748
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`}
57485750
AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
57495751
`.pipe(
57505752
Effect.map((rows) =>
@@ -6240,12 +6242,12 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62406242
left.id.localeCompare(right.id),
62416243
);
62426244
}),
6243-
getThreadWorktreePaths: () =>
6245+
getThreadWorktreePaths: (options) =>
62446246
Ref.get(replayState).pipe(
62456247
Effect.map((state) =>
62466248
[...state.projections.values()].flatMap(({ thread }) =>
62476249
thread.deletedAt === null &&
6248-
thread.archivedAt === null &&
6250+
(options?.includeArchived === true || thread.archivedAt === null) &&
62496251
thread.worktreePath !== null
62506252
? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
62516253
: [],

‎apps/server/src/storageCleanup.test.ts‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,26 @@ const cleanupFixture = (
306306
getShellSnapshot: (input?: { location?: string }) =>
307307
Effect.sync(() => {
308308
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+
};
310320
}),
311-
getThreadWorktreePaths: () =>
321+
getThreadWorktreePaths: (input?: { includeArchived?: boolean }) =>
312322
Effect.sync(() =>
313323
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+
: [],
317329
),
318330
),
319331
getThreadShell: (threadId: ThreadId) =>
@@ -1176,6 +1188,41 @@ describe("orphan worktree reports", () => {
11761188
});
11771189

11781190
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+
);
11791226
it.live.each(["before-check", "while-measuring"] as const)(
11801227
"keeps a worktree protected by a missing project root (%s)",
11811228
(timing) =>
@@ -1264,6 +1311,18 @@ describe("storage cleanup reports and local file policies", () => {
12641311
shell({ id: ThreadId.make("thread-2"), branch: "feature", worktreePath: worktree }),
12651312
],
12661313
],
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+
],
12671326
] as const)("keeps a worktree when %s during size measurement", ([, threadsWhileMeasuring]) =>
12681327
runCleanupTest(
12691328
Effect.gen(function* () {
@@ -1542,6 +1601,17 @@ describe("storage cleanup reports and local file policies", () => {
15421601
shell({ id: ThreadId.make("thread-2"), branch: "feature", worktreePath: worktree }),
15431602
]);
15441603
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);
15451615
fixture.disable();
15461616
expect((yield* service.runNow).entries).toEqual([]);
15471617
yield* fs.remove(worktree, { recursive: true });

‎apps/server/src/storageCleanup.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,9 @@ const make = Effect.gen(function* () {
291291
// The snapshot takes seconds on large databases, so it runs on the reader
292292
// worker instead of the event loop.
293293
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);
297295
const projects = yield* projectStore.listShells();
298-
return { projects, threads };
296+
return { projects, threads: [...snapshot.threads, ...snapshot.archivedThreads] };
299297
});
300298

301299
const containsProjectRoot = Effect.fn("StorageCleanup.containsProjectRoot")(function* (
@@ -740,9 +738,9 @@ const make = Effect.gen(function* () {
740738
const latestProjects = yield* projectStore.listShells();
741739
if (yield* containsProjectRoot(worktreePath, [project, ...latestProjects]))
742740
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);
746744
if (hasTerminal(worktreePath)) return "open terminal";
747745
if (deleted) {
748746
if (

0 commit comments

Comments
 (0)