Skip to content

Commit edbd559

Browse files
fix(server): delete a removed worktree's checkpoint refs through the project repo
Linked worktrees share the main repository's ref store, so removing a worktree leaves its refs/t3 checkpoint refs behind. Thread ref cleanup skipped a cwd that was no longer a repository, which leaked every ref of a worktree thread whose worktree was removed before the cleanup effect ran. It now falls back to the project's workspace root in that case. AI-assisted; verified with the tests and tools listed above.
1 parent 424b0d5 commit edbd559

6 files changed

Lines changed: 65 additions & 10 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,43 @@ it.effect("records a repository probe failure as an error checkpoint, not a miss
156156
assert.deepEqual(statuses, { baseline: "error", turn: "error" });
157157
}),
158158
);
159+
160+
it.effect("deletes a removed worktree's thread refs through the project repository", () =>
161+
Effect.gen(function* () {
162+
const deleted: Array<{ readonly cwd: string; readonly prefixes: ReadonlyArray<string> }> = [];
163+
const layerTest = CheckpointService.layer.pipe(
164+
Layer.provide(
165+
Layer.mergeAll(
166+
IdAllocator.layer,
167+
Layer.mock(CheckpointStore.CheckpointStore)({
168+
isGitRepository: (cwd) => Effect.succeed(cwd === "/repo"),
169+
deleteCheckpointRefsUnder: (input) => Effect.sync(() => void deleted.push(input)),
170+
}),
171+
),
172+
),
173+
Layer.provideMerge(NodeCrypto.layer),
174+
);
175+
const scopeId = CheckpointScopeId.make("checkpoint-scope:removed-worktree");
176+
const scopePrefix = (yield* CheckpointService.checkpointRefForScopeOrdinal({
177+
scopeId,
178+
ordinalWithinScope: 0,
179+
})).replace(/\/ordinal\/0$/, "");
180+
181+
yield* Effect.gen(function* () {
182+
const checkpoints = yield* CheckpointService.CheckpointServiceV2;
183+
yield* checkpoints.deleteThreadRefs({
184+
threadId: ThreadId.make("thread:removed-worktree"),
185+
scopes: [{ id: scopeId, cwd: "/repo/.worktrees/feature" }],
186+
legacyCwd: "/repo/.worktrees/feature",
187+
workspaceRoot: "/repo",
188+
});
189+
}).pipe(Effect.provide(layerTest));
190+
191+
assert.deepEqual(deleted, [
192+
{
193+
cwd: "/repo",
194+
prefixes: [scopePrefix, "refs/t3/checkpoints/dGhyZWFkOnJlbW92ZWQtd29ya3RyZWU"],
195+
},
196+
]);
197+
}).pipe(Effect.provide(NodeCrypto.layer)),
198+
);

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export interface CheckpointServiceV2Shape {
162162
readonly threadId: ThreadId;
163163
readonly scopes: ReadonlyArray<{ readonly id: CheckpointScopeId; readonly cwd: string }>;
164164
readonly legacyCwd: string | null;
165+
readonly workspaceRoot: string | null;
165166
}) => Effect.Effect<void, CheckpointServiceV2Error>;
166167
}
167168

@@ -624,14 +625,20 @@ export const layer: Layer.Layer<
624625
yield* Effect.forEach(
625626
prefixesByCwd,
626627
([cwd, prefixes]) =>
627-
withWorkspaceLock(
628-
cwd,
629-
Effect.gen(function* () {
630-
// A removed worktree took its refs with it.
631-
if (!(yield* checkpointStore.isGitRepository(cwd))) return;
632-
yield* checkpointStore.deleteCheckpointRefsUnder({ cwd, prefixes });
633-
}),
634-
).pipe(
628+
Effect.gen(function* () {
629+
// A removed linked worktree's refs live on in the main repository's ref store.
630+
const target = (yield* checkpointStore.isGitRepository(cwd))
631+
? cwd
632+
: input.workspaceRoot !== null &&
633+
(yield* checkpointStore.isGitRepository(input.workspaceRoot))
634+
? input.workspaceRoot
635+
: null;
636+
if (target === null) return;
637+
yield* withWorkspaceLock(
638+
target,
639+
checkpointStore.deleteCheckpointRefsUnder({ cwd: target, prefixes }),
640+
);
641+
}).pipe(
635642
Effect.mapError(
636643
(cause) =>
637644
new CheckpointDeleteThreadRefsError({ threadId: input.threadId, cwd, cause }),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
118118
type: Schema.Literal("checkpoint-refs.cleanup"),
119119
scopes: Schema.Array(Schema.Struct({ id: CheckpointScopeId, cwd: Schema.String })),
120120
legacyCwd: Schema.NullOr(Schema.String),
121+
workspaceRoot: Schema.NullOr(Schema.String),
121122
}),
122123
Schema.Struct({
123124
type: Schema.Literal("attachment.cleanup"),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ export const layerExecutor: Layer.Layer<
590590
threadId: effect.threadId,
591591
scopes: effect.request.scopes,
592592
legacyCwd: effect.request.legacyCwd,
593+
workspaceRoot: effect.request.workspaceRoot,
593594
})
594595
.pipe(
595596
Effect.mapError(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,12 @@ it.effect("queues provider and resource cleanup and preserves an earlier deletio
301301
{ type: "scheduled-tasks.cleanup" },
302302
{ type: "terminal.cleanup" },
303303
{ type: "preview.cleanup" },
304-
{ type: "checkpoint-refs.cleanup", scopes: [], legacyCwd: "/workspace/feature" },
304+
{
305+
type: "checkpoint-refs.cleanup",
306+
scopes: [],
307+
legacyCwd: "/workspace/feature",
308+
workspaceRoot: "/workspace",
309+
},
305310
{ type: "attachment.cleanup", attachmentIds: ["shared_file"] },
306311
],
307312
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const planThreadDeletion = Effect.fn("ThreadDeletion.planThreadDeletion")
2727
| "providerSessions"
2828
| "checkpointScopes"
2929
>;
30-
/** The project's root, where a thread without a worktree kept its pre-v2 checkpoint refs. */
30+
/** The project's root; its ref store also holds the refs of the project's linked worktrees. */
3131
readonly workspaceRoot: string | null;
3232
readonly attachmentIds: ReadonlyArray<string>;
3333
readonly now: DateTime.Utc;
@@ -244,6 +244,7 @@ export const planThreadDeletion = Effect.fn("ThreadDeletion.planThreadDeletion")
244244
type: "checkpoint-refs.cleanup",
245245
scopes: projection.checkpointScopes.map((scope) => ({ id: scope.id, cwd: scope.cwd })),
246246
legacyCwd: projection.thread.worktreePath ?? input.workspaceRoot,
247+
workspaceRoot: input.workspaceRoot,
247248
},
248249
});
249250
const attachmentIds = Array.from(new Set(input.attachmentIds));

0 commit comments

Comments
 (0)