Skip to content

Commit 7ac952c

Browse files
t3dotggfireboltdude1357claude
authored
fix(server): worktree cleanup no longer runs sweeps back to back (#19418)
Co-authored-by: Tanner Sharon <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d0a73b5 commit 7ac952c

5 files changed

Lines changed: 296 additions & 59 deletions

File tree

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

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

330+
const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
331+
const store = yield* ProjectionStore.ProjectionStoreV2;
332+
const now = yield* DateTime.now;
333+
const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
334+
const threadId = ThreadId.make(id);
335+
const thread = {
336+
createdBy: "user" as const,
337+
creationSource: "web" as const,
338+
id: threadId,
339+
projectId: ProjectId.make("project:worktree-paths"),
340+
title: id,
341+
providerInstanceId,
342+
modelSelection,
343+
runtimeMode: "full-access" as const,
344+
interactionMode: "default" as const,
345+
branch: input.worktreePath === null ? null : id,
346+
worktreePath: input.worktreePath,
347+
activeProviderThreadId: null,
348+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
349+
forkedFrom: null,
350+
createdAt: now,
351+
updatedAt: now,
352+
archivedAt: input.archived === true ? now : null,
353+
settledOverride: null,
354+
settledAt: null,
355+
lastVisitedAt: null,
356+
deletedAt: null,
357+
};
358+
return store
359+
.apply({
360+
id: EventId.make(`event:${id}:created`),
361+
type: "thread.created",
362+
threadId,
363+
occurredAt: now,
364+
payload: thread,
365+
})
366+
.pipe(Effect.as(thread));
367+
};
368+
yield* create("thread:active", { worktreePath: "/worktrees/active" });
369+
yield* create("thread:archived", { worktreePath: "/worktrees/archived", archived: true });
370+
yield* create("thread:local", { worktreePath: null });
371+
const deleted = yield* create("thread:deleted", { worktreePath: "/worktrees/deleted" });
372+
yield* store.apply({
373+
id: EventId.make("event:thread:deleted:deleted"),
374+
type: "thread.deleted",
375+
threadId: deleted.id,
376+
occurredAt: now,
377+
payload: { ...deleted, deletedAt: now },
378+
});
379+
380+
assert.deepEqual(yield* store.getThreadWorktreePaths(), [
381+
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
382+
]);
383+
});
384+
385+
it.effect("memory worktree path lookup reads only active threads", () =>
386+
threadWorktreePathsReadActiveThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
387+
);
388+
330389
it.effect("memory projection keeps restart-cancelled work through a stale run.updated", () =>
331390
restartCancelledWorkSurvivesStaleRunUpdate.pipe(Effect.provide(ProjectionStore.layerMemory)),
332391
);
@@ -553,6 +612,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
553612
"SQL recovery selects unfinished runless native subagents",
554613
() => runlessNativeSubagentNeedsRecovery,
555614
);
615+
it.effect(
616+
"worktree path lookup reads only active threads",
617+
() => threadWorktreePathsReadActiveThreads,
618+
);
556619

557620
it.effect(
558621
"keeps restart-cancelled work through a stale run.updated",

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ export interface ProjectionStoreV2Shape {
416416
readonly getThreadsWithPullRequests: (
417417
threadId?: ThreadId,
418418
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
419+
/**
420+
* Active (not deleted, not archived) threads that have a worktree path, the
421+
* same threads as the shell snapshot's `threads`. Reads only the thread rows,
422+
* so callers can check who uses a path without a shell snapshot.
423+
*/
424+
readonly getThreadWorktreePaths: () => Effect.Effect<
425+
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
426+
ProjectionStoreV2Error
427+
>;
419428
readonly getTurnStartContext: (
420429
threadId: ThreadId,
421430
runId: RunId,
@@ -5699,6 +5708,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56995708
);
57005709
}).pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));
57015710

5711+
const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = () =>
5712+
sql<{ readonly thread_id: string; readonly worktree_path: string }>`
5713+
SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path
5714+
FROM orchestration_v2_projection_threads
5715+
WHERE deleted_at IS NULL
5716+
AND json_extract(payload_json, '$.archivedAt') IS NULL
5717+
AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
5718+
`.pipe(
5719+
Effect.map((rows) =>
5720+
rows.map((row) => ({
5721+
threadId: ThreadId.make(row.thread_id),
5722+
worktreePath: row.worktree_path,
5723+
})),
5724+
),
5725+
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
5726+
);
5727+
57025728
const shellThreadStateFromRow = (input: {
57035729
readonly row: ShellThreadRow;
57045730
readonly runOrdinalsByThreadId: ReadonlyMap<ThreadId, Map<RunId, number>>;
@@ -6035,6 +6061,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60356061
getThread,
60366062
getSettlementCandidates,
60376063
getThreadsWithPullRequests,
6064+
getThreadWorktreePaths,
60386065
getThreadProjection,
60396066
getTurnStartContext,
60406067
getTurnStartHistory,
@@ -6184,6 +6211,18 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
61846211
left.id.localeCompare(right.id),
61856212
);
61866213
}),
6214+
getThreadWorktreePaths: () =>
6215+
Ref.get(replayState).pipe(
6216+
Effect.map((state) =>
6217+
[...state.projections.values()].flatMap(({ thread }) =>
6218+
thread.deletedAt === null &&
6219+
thread.archivedAt === null &&
6220+
thread.worktreePath !== null
6221+
? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
6222+
: [],
6223+
),
6224+
),
6225+
),
61876226
getThreadsWithPullRequests: (threadId) =>
61886227
Ref.get(replayState).pipe(
61896228
Effect.map((state) =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ it.effect(
218218
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
219219
getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"),
220220
getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"),
221+
getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"),
221222
getThreadProjection: () => Effect.die("control effects must not load transcript"),
222223
getTurnStartContext: () => Effect.die("unused"),
223224
getTurnStartHistory: () => Effect.die("unused"),

0 commit comments

Comments
 (0)