Skip to content

Commit f18d433

Browse files
fix(server): rollback checks workspace isolation before clearing a leftover worktree folder
A file-restoring rollback into a shared workspace is rejected; it now rejects before removing a leftover folder that lost its .git, so the folder's files stay, as they did before recovery was added. AI-assisted; verified with the tests and tools listed above.
1 parent 262e832 commit f18d433

2 files changed

Lines changed: 63 additions & 38 deletions

File tree

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ it.effect.each([
176176
{ order: "cleanup-first", restoreFiles: false, unmanaged: true },
177177
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true },
178178
{ order: "cleanup-first", restoreFiles: false, leavesFolder: true },
179-
])("locks rollback with %s", ({ order, restoreFiles, unmanaged = false, leavesFolder = false }) =>
179+
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true, shared: true },
180+
])("locks rollback with %s", ({ order, restoreFiles, unmanaged, leavesFolder, shared }) =>
180181
Effect.gen(function* () {
181182
const fileSystem = yield* FileSystem.FileSystem;
182183
const path = yield* Path.Path;
@@ -278,16 +279,31 @@ it.effect.each([
278279
Effect.succeed({
279280
schemaVersion: 1,
280281
snapshotSequence: 0,
281-
threads: [],
282+
threads: shared
283+
? [
284+
{
285+
id: ThreadId.make("rollback-worktree-sharer"),
286+
projectId,
287+
worktreePath,
288+
deletedAt: null,
289+
lineage: {},
290+
},
291+
]
292+
: [],
282293
archivedThreads: [],
283-
}),
294+
} as never),
295+
getCheckpointContext: () => Effect.succeed({ checkpointScopes: [] } as never),
296+
getThreadProviderContext: () => Effect.succeed({ providerSessions: [] } as never),
284297
}),
285298
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
286299
open: (input) =>
287300
Effect.gen(function* () {
288301
assert.equal(input.runtimePolicy.cwd, worktreePath);
289302
if (!(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie))) {
290-
return yield* new ProviderWorkspaceMissingError({ threadId, cwd: worktreePath });
303+
return yield* new ProviderWorkspaceMissingError({
304+
threadId,
305+
cwd: worktreePath,
306+
});
291307
}
292308
calls.push("session");
293309
yield* Deferred.succeed(openEntered, undefined);
@@ -336,10 +352,14 @@ it.effect.each([
336352
if (order === "rollback-first") assert.isFalse(cleanupEntered);
337353
yield* Deferred.succeed(release, undefined);
338354
if (cleanup !== undefined) yield* Fiber.join(cleanup);
339-
if (unmanaged) {
355+
if (unmanaged || shared) {
340356
const error = yield* Fiber.join(rollback).pipe(Effect.flip);
341357
assert.equal(error._tag, "CheckpointRollbackExecutionError");
342358
assert.deepEqual(calls, ["cleanup"]);
359+
if (shared) {
360+
assert.equal(error.reason, "shared-workspace");
361+
assert.isTrue(yield* fileSystem.exists(path.join(worktreePath, "unsaved.txt")));
362+
}
343363
return;
344364
}
345365
yield* Fiber.join(rollback);

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -217,46 +217,51 @@ export const layer: Layer.Layer<
217217
};
218218
});
219219

220-
const { worktreePath, branch } = context.thread;
221-
// Recreated only where the server would have made it, as turn start does.
222-
// A failed removal can leave the folder without its checkout; only a file restore replaces it.
223-
if (
224-
worktreePath != null &&
225-
branch != null &&
226-
!(yield* fileSystem.exists(
227-
input.restoreFiles === false ? worktreePath : path.join(worktreePath, ".git"),
228-
)) &&
229-
(yield* isManagedWorktreePath(worktreePath))
230-
) {
231-
const project = yield* projects.get(context.thread.projectId);
232-
if (Option.isSome(project)) {
233-
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
234-
yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
235-
yield* gitWorkflow.createWorktree({
236-
cwd: project.value.workspaceRoot,
237-
refName: branch,
238-
path: worktreePath,
239-
});
240-
}
241-
}
242-
243-
if (
244-
input.restoreFiles !== false &&
245-
!(yield* isCheckpointRestoreIsolated(context.thread, scope, {
246-
fileSystem,
247-
projections,
248-
projects,
249-
path,
250-
}))
251-
) {
252-
return yield* new CheckpointRollbackExecutionError({
220+
const restoreFiles = input.restoreFiles !== false;
221+
const isolated = isCheckpointRestoreIsolated(context.thread, scope, {
222+
fileSystem,
223+
projections,
224+
projects,
225+
path,
226+
});
227+
const sharedWorkspace = () =>
228+
new CheckpointRollbackExecutionError({
253229
reason: "shared-workspace",
254230
threadId: input.threadId,
255231
providerThreadId: input.providerThreadId,
256232
checkpointId: input.checkpointId,
257233
});
234+
235+
const { worktreePath, branch } = context.thread;
236+
// Recreated only where the server would have made it, as turn start does.
237+
if (worktreePath != null && branch != null) {
238+
// A failed removal can leave the folder without its checkout; only a file restore replaces it.
239+
const leftover =
240+
restoreFiles &&
241+
(yield* fileSystem.exists(worktreePath)) &&
242+
!(yield* fileSystem.exists(path.join(worktreePath, ".git")));
243+
if (
244+
(leftover || !(yield* fileSystem.exists(worktreePath))) &&
245+
(yield* isManagedWorktreePath(worktreePath))
246+
) {
247+
const project = yield* projects.get(context.thread.projectId);
248+
if (Option.isSome(project)) {
249+
if (leftover) {
250+
if (!(yield* isolated)) return yield* sharedWorkspace();
251+
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
252+
}
253+
yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
254+
yield* gitWorkflow.createWorktree({
255+
cwd: project.value.workspaceRoot,
256+
refName: branch,
257+
path: worktreePath,
258+
});
259+
}
260+
}
258261
}
259262

263+
if (restoreFiles && !(yield* isolated)) return yield* sharedWorkspace();
264+
260265
const modelSelection = context.thread.modelSelection;
261266
const resolvedRuntimePolicy = yield* runtimePolicy.resolve({
262267
thread: context.thread,

0 commit comments

Comments
 (0)