The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
fix(server): Edit from here recreates a worktree folder that lost its checkout (#35194)
Julius Marminge committed
Nov 12, 2026 at 03:48 UTC
1032135cf754dfa22ffd0e67266b320712e51e72
2 files changed
+82
-38
apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts
+44
-9
@@ -174,7 +174,10 @@ it.effect.each([
174
{ order: "cleanup-first", restoreFiles: true },
175
{ order: "rollback-first", restoreFiles: true },
176
{ order: "cleanup-first", restoreFiles: false, unmanaged: true },
177
-])("locks rollback with %s", ({ order, restoreFiles, unmanaged = false }) =>
177
+ { order: "cleanup-first", restoreFiles: true, leavesFolder: true },
178
+ { order: "cleanup-first", restoreFiles: false, leavesFolder: true },
179
+ { order: "cleanup-first", restoreFiles: true, leavesFolder: true, shared: true },
180
+])("locks rollback with %s", ({ order, restoreFiles, unmanaged, leavesFolder, shared }) =>
181
Effect.gen(function* () {
182
const fileSystem = yield* FileSystem.FileSystem;
183
const path = yield* Path.Path;
@@ -185,7 +188,7 @@ it.effect.each([
188
const worktreePath = unmanaged
189
? path.join(workspaceRoot, "elsewhere", "feature")
190
: path.join(worktreesDir, "repo", "feature");
188
- yield* fileSystem.makeDirectory(worktreePath, { recursive: true });
191
+ yield* fileSystem.makeDirectory(path.join(worktreePath, ".git"), { recursive: true });
192
const threadId = ThreadId.make("rollback-worktree");
193
const projectId = ProjectId.make("rollback-worktree-project");
194
const providerThreadId = ProviderThreadId.make("rollback-worktree-provider-thread");
@@ -246,14 +249,18 @@ it.effect.each([
249
path: worktreePath,
250
});
251
calls.push("create");
249
- yield* fileSystem.makeDirectory(worktreePath).pipe(Effect.orDie);
252
+ yield* fileSystem
253
+ .makeDirectory(path.join(worktreePath, ".git"), { recursive: true })
254
+ .pipe(Effect.orDie);
255
return {} as never;
256
}),
257
}),
258
Layer.mock(CheckpointService.CheckpointServiceV2)({
259
restore: () =>
260
Effect.gen(function* () {
256
- assert.isTrue(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie));
261
+ assert.isTrue(
262
+ yield* fileSystem.exists(path.join(worktreePath, ".git")).pipe(Effect.orDie),
263
+ );
264
calls.push("files");
265
}),
266
}),
@@ -272,16 +279,31 @@ it.effect.each([
279
Effect.succeed({
280
schemaVersion: 1,
281
snapshotSequence: 0,
275
- 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
+ : [],
293
archivedThreads: [],
277
- }),
294
+ } as never),
295
+ getCheckpointContext: () => Effect.succeed({ checkpointScopes: [] } as never),
296
+ getThreadProviderContext: () => Effect.succeed({ providerSessions: [] } as never),
297
}),
298
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
299
open: (input) =>
300
Effect.gen(function* () {
301
assert.equal(input.runtimePolicy.cwd, worktreePath);
302
if (!(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie))) {
284
- return yield* new ProviderWorkspaceMissingError({ threadId, cwd: worktreePath });
303
+ return yield* new ProviderWorkspaceMissingError({
304
+ threadId,
305
+ cwd: worktreePath,
306
+ });
307
}
308
calls.push("session");
309
yield* Deferred.succeed(openEntered, undefined);
@@ -306,6 +328,11 @@ it.effect.each([
328
cleanupEntered = true;
329
if (order === "cleanup-first") yield* Deferred.await(release);
330
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
331
+ // A failed `git worktree remove` can leave the folder without its checkout.
332
+ if (leavesFolder) {
333
+ yield* fileSystem.makeDirectory(worktreePath);
334
+ yield* fileSystem.writeFileString(path.join(worktreePath, "unsaved.txt"), "keep");
335
+ }
336
calls.push("cleanup");
337
}),
338
);
@@ -325,10 +352,14 @@ it.effect.each([
352
if (order === "rollback-first") assert.isFalse(cleanupEntered);
353
yield* Deferred.succeed(release, undefined);
354
if (cleanup !== undefined) yield* Fiber.join(cleanup);
328
- if (unmanaged) {
355
+ if (unmanaged || shared) {
356
const error = yield* Fiber.join(rollback).pipe(Effect.flip);
357
assert.equal(error._tag, "CheckpointRollbackExecutionError");
358
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
+ }
363
return;
364
}
365
yield* Fiber.join(rollback);
@@ -336,12 +367,16 @@ it.effect.each([
367
const rollbackCalls = restoreFiles
368
? ["session", "files", "projection"]
369
: ["session", "projection"];
370
+ const recreates = !leavesFolder || restoreFiles;
371
assert.deepEqual(
372
calls,
373
order === "cleanup-first"
342
- ? ["cleanup", "prune", "create", ...rollbackCalls]
374
+ ? ["cleanup", ...(recreates ? ["prune", "create"] : []), ...rollbackCalls]
375
: [...rollbackCalls, "cleanup"],
376
);
377
+ if (!recreates) {
378
+ assert.isTrue(yield* fileSystem.exists(path.join(worktreePath, "unsaved.txt")));
379
+ }
380
}).pipe(Effect.provide(testLayer));
381
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
382
);
apps/server/src/orchestration-v2/CheckpointRollbackService.ts
+38
-29
@@ -217,42 +217,51 @@ export const layer: Layer.Layer<
217
};
218
});
219
220
- const { worktreePath, branch } = context.thread;
221
- // Recreated only where the server would have made it, as turn start does.
222
- if (
223
- worktreePath != null &&
224
- branch != null &&
225
- !(yield* fileSystem.exists(worktreePath)) &&
226
- (yield* isManagedWorktreePath(worktreePath))
227
- ) {
228
- const project = yield* projects.get(context.thread.projectId);
229
- if (Option.isSome(project)) {
230
- yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
231
- yield* gitWorkflow.createWorktree({
232
- cwd: project.value.workspaceRoot,
233
- refName: branch,
234
- path: worktreePath,
235
- });
236
- }
237
- }
238
-
239
- if (
240
- input.restoreFiles !== false &&
241
- !(yield* isCheckpointRestoreIsolated(context.thread, scope, {
242
- fileSystem,
243
- projections,
244
- projects,
245
- path,
246
- }))
247
- ) {
248
- 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({
229
reason: "shared-workspace",
230
threadId: input.threadId,
231
providerThreadId: input.providerThreadId,
232
checkpointId: input.checkpointId,
233
});
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
+ }
261
}
262
263
+ if (restoreFiles && !(yield* isolated)) return yield* sharedWorkspace();
264
+
265
const modelSelection = context.thread.modelSelection;
266
const resolvedRuntimePolicy = yield* runtimePolicy.resolve({
267
thread: context.thread,