Skip to content

Commit 61d86e3

Browse files
t3dotggclaude
andcommitted
fix(server): cleanup reads the active snapshot once and rechecks the same threads
- readThreads reads only the active shell snapshot. Its second read, of the archive, always returned no threads, because the snapshot puts archived threads in archivedThreads. - getThreadWorktreePaths skips archived threads, so the under-lease recheck sees the same threads as the first check. Before this, a worktree shared with an archived thread was kept on every sweep with the false reason "Thread activity or shared worktree changed since check". - The sweep tests move into storageCleanup.test.ts and use its fixture. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent be8ffa4 commit 61d86e3

5 files changed

Lines changed: 155 additions & 470 deletions

File tree

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
327327
assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
328328
});
329329

330-
const threadWorktreePathsSkipDeletedThreads = Effect.gen(function* () {
330+
const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
331331
const store = yield* ProjectionStore.ProjectionStoreV2;
332332
const now = yield* DateTime.now;
333333
const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
@@ -377,19 +377,13 @@ const threadWorktreePathsSkipDeletedThreads = Effect.gen(function* () {
377377
payload: { ...deleted, deletedAt: now },
378378
});
379379

380-
assert.deepEqual(
381-
(yield* store.getThreadWorktreePaths()).toSorted((left, right) =>
382-
left.threadId.localeCompare(right.threadId),
383-
),
384-
[
385-
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
386-
{ threadId: ThreadId.make("thread:archived"), worktreePath: "/worktrees/archived" },
387-
],
388-
);
380+
assert.deepEqual(yield* store.getThreadWorktreePaths(), [
381+
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
382+
]);
389383
});
390384

391-
it.effect("memory worktree path lookup skips deleted threads and keeps archived ones", () =>
392-
threadWorktreePathsSkipDeletedThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
385+
it.effect("memory worktree path lookup reads only active threads", () =>
386+
threadWorktreePathsReadActiveThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
393387
);
394388

395389
it.effect("memory projection keeps restart-cancelled work through a stale run.updated", () =>
@@ -619,8 +613,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
619613
() => runlessNativeSubagentNeedsRecovery,
620614
);
621615
it.effect(
622-
"worktree path lookup skips deleted threads and keeps archived ones",
623-
() => threadWorktreePathsSkipDeletedThreads,
616+
"worktree path lookup reads only active threads",
617+
() => threadWorktreePathsReadActiveThreads,
624618
);
625619

626620
it.effect(

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ export interface ProjectionStoreV2Shape {
416416
threadId?: ThreadId,
417417
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
418418
/**
419-
* Every non-deleted thread, archived or not, that has a worktree path. Reads
420-
* only the thread rows, so callers can check who uses a path without a shell
421-
* snapshot.
419+
* Active (not deleted, not archived) threads that have a worktree path, the
420+
* same threads as the shell snapshot's `threads`. Reads only the thread rows,
421+
* so callers can check who uses a path without a shell snapshot.
422422
*/
423423
readonly getThreadWorktreePaths: () => Effect.Effect<
424424
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
@@ -5656,7 +5656,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56565656
sql<{ readonly thread_id: string; readonly worktree_path: string }>`
56575657
SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path
56585658
FROM orchestration_v2_projection_threads
5659-
WHERE deleted_at IS NULL AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
5659+
WHERE deleted_at IS NULL
5660+
AND json_extract(payload_json, '$.archivedAt') IS NULL
5661+
AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
56605662
`.pipe(
56615663
Effect.map((rows) =>
56625664
rows.map((row) => ({
@@ -6156,7 +6158,9 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
61566158
Ref.get(replayState).pipe(
61576159
Effect.map((state) =>
61586160
[...state.projections.values()].flatMap(({ thread }) =>
6159-
thread.deletedAt === null && thread.worktreePath !== null
6161+
thread.deletedAt === null &&
6162+
thread.archivedAt === null &&
6163+
thread.worktreePath !== null
61606164
? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
61616165
: [],
61626166
),

0 commit comments

Comments
 (0)