Skip to content

Commit d0186cb

Browse files
fix(server): restore reservations match missing worktrees under symlinked ancestors
A worktree that storage cleanup removed has no realPath, so its reservation fell back to the literal path and missed an overlap through a symlinked ancestor. Resolve the nearest existing ancestor and rejoin the missing tail. The rollback test now also holds a turn start while files are being restored, not only while the provider session is opening. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8e4748b commit d0186cb

3 files changed

Lines changed: 68 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ it.effect.each([{ restoreFiles: true }, { restoreFiles: false }])(
366366
const scopeId = CheckpointScopeId.make("rollback-reservation-scope");
367367
const release = Deferred.makeUnsafe<void>();
368368
const openEntered = Deferred.makeUnsafe<void>();
369+
const restoreEntered = Deferred.makeUnsafe<void>();
370+
const releaseRestore = Deferred.makeUnsafe<void>();
369371
const calls: string[] = [];
370372
const projection = {
371373
thread: {
@@ -403,7 +405,11 @@ it.effect.each([{ restoreFiles: true }, { restoreFiles: false }])(
403405
}),
404406
Layer.mock(GitWorkflow.GitWorkflowService)({}),
405407
Layer.mock(CheckpointService.CheckpointServiceV2)({
406-
restore: () => Effect.sync(() => calls.push("files")),
408+
restore: () =>
409+
Deferred.succeed(restoreEntered, undefined).pipe(
410+
Effect.andThen(Deferred.await(releaseRestore)),
411+
Effect.andThen(Effect.sync(() => calls.push("files"))),
412+
),
407413
}),
408414
Layer.mock(EventSink.EventSinkV2)({ write: () => Effect.succeed([]) }),
409415
IdAllocator.layer,
@@ -439,22 +445,34 @@ it.effect.each([{ restoreFiles: true }, { restoreFiles: false }])(
439445
yield* Deferred.await(openEntered);
440446
// A synchronous realPath lets the forked start reach the reservation
441447
// before the next assertion, so "not entered" means "waiting".
442-
const turnStart = yield* withTurnStartReservation(
448+
const startTurn = withTurnStartReservation(
443449
FileSystem.makeNoop({ realPath: (target) => Effect.succeed(target) }),
444450
path,
445451
path.join(worktreePath, "packages", "app"),
446452
Effect.sync(() => calls.push("turn start")),
447453
).pipe(Effect.forkChild({ startImmediately: true }));
454+
const turnStart = yield* startTurn;
448455
if (restoreFiles) {
449456
assert.deepEqual(calls, []);
457+
yield* Deferred.succeed(release, undefined);
458+
// The reservation must still be held while files are being restored,
459+
// not only while the provider rollback is in flight.
460+
yield* Deferred.await(restoreEntered);
461+
const duringRestore = yield* startTurn;
462+
assert.deepEqual(calls, []);
463+
yield* Deferred.succeed(releaseRestore, undefined);
464+
yield* Fiber.join(duringRestore);
450465
} else {
451466
// Nothing is rewritten, so the turn start must not wait for the provider.
452467
yield* Fiber.join(turnStart);
468+
yield* Deferred.succeed(release, undefined);
453469
}
454-
yield* Deferred.succeed(release, undefined);
455470
yield* Fiber.join(rollback);
456471
yield* Fiber.join(turnStart);
457-
assert.deepEqual(calls, restoreFiles ? ["files", "turn start"] : ["turn start"]);
472+
assert.deepEqual(
473+
calls,
474+
restoreFiles ? ["files", "turn start", "turn start"] : ["turn start"],
475+
);
458476
}).pipe(Effect.provide(testLayer));
459477
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
460478
);

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,37 +964,59 @@ effectIt.effect.each(["cleanup-first", "startup-first"])(
964964

965965
// regression: #12341 a turn start overlapping a checkpoint restore wrote into the checkout
966966
// The filesystem is synchronous so the turn start either reaches the provider or parks on the
967-
// lease before the restore is released; realPath models the symlink alias.
967+
// lease before the restore is released; realPath models the symlink alias and missing worktrees.
968968
effectIt.effect.each([
969969
{
970970
overlap: "a project-root thread during a restore of the project checkout",
971971
restorePath: "/tmp/native-account-project",
972972
worktreePath: undefined,
973973
realPaths: {},
974+
missing: [],
974975
},
975976
{
976977
overlap: "a symlink alias of the restoring checkout",
977978
restorePath: "/tmp/restore-overlap/link",
978979
worktreePath: "/tmp/restore-overlap/checkout",
979980
realPaths: { "/tmp/restore-overlap/link": "/tmp/restore-overlap/checkout" },
981+
missing: [],
980982
},
981983
{
982984
overlap: "a directory inside the restoring checkout",
983985
restorePath: "/tmp/restore-overlap/checkout",
984986
worktreePath: "/tmp/restore-overlap/checkout/packages/app",
985987
realPaths: {},
988+
missing: [],
989+
},
990+
{
991+
overlap: "a not-yet-recreated worktree under a symlink alias of the restoring one",
992+
restorePath: "/tmp/restore-overlap/link/missing-child",
993+
worktreePath: "/tmp/restore-overlap/checkout/missing-child",
994+
realPaths: { "/tmp/restore-overlap/link": "/tmp/restore-overlap/checkout" },
995+
missing: [
996+
"/tmp/restore-overlap/link/missing-child",
997+
"/tmp/restore-overlap/checkout/missing-child",
998+
],
986999
},
9871000
] as const)(
9881001
"does not start provider work in $overlap until the restore finishes",
989-
({ restorePath, worktreePath, realPaths }) =>
1002+
({ restorePath, worktreePath, realPaths, missing }) =>
9901003
Effect.gen(function* () {
9911004
const order: Array<string> = [];
9921005
const releaseRestore = yield* Deferred.make<void>();
9931006
const restoreEntered = yield* Deferred.make<void>();
9941007
const fileSystem = FileSystem.makeNoop({
9951008
exists: () => Effect.succeed(true),
9961009
realPath: (candidate) =>
997-
Effect.succeed((realPaths as Record<string, string>)[candidate] ?? candidate),
1010+
(missing as ReadonlyArray<string>).includes(candidate)
1011+
? Effect.fail(
1012+
PlatformError.systemError({
1013+
_tag: "NotFound",
1014+
module: "FileSystem",
1015+
method: "realPath",
1016+
pathOrDescriptor: candidate,
1017+
}),
1018+
)
1019+
: Effect.succeed((realPaths as Record<string, string>)[candidate] ?? candidate),
9981020
});
9991021
const harness = makeLocalCommandHarness({
10001022
text: "Continue",

‎apps/server/src/workspace/workspaceLease.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ const release = (reservation: Reservation) =>
6969
Deferred.doneUnsafe(released, Effect.void);
7070
});
7171

72+
// A worktree removed by storage cleanup is recreated inside the reserved
73+
// section, so resolve its nearest existing ancestor: a symlink alias above it
74+
// must still canonicalize the same on both sides. Walks at most the path depth.
75+
const canonicalize = (
76+
fileSystem: FileSystem.FileSystem,
77+
path: Path.Path,
78+
target: string,
79+
): Effect.Effect<string> =>
80+
fileSystem.realPath(target).pipe(
81+
Effect.catchReason("PlatformError", "NotFound", () => {
82+
const parent = path.dirname(target);
83+
return parent === target
84+
? Effect.succeed(target)
85+
: canonicalize(fileSystem, path, parent).pipe(
86+
Effect.map((resolved) => path.join(resolved, path.basename(target))),
87+
);
88+
}),
89+
Effect.orElseSucceed(() => target),
90+
);
91+
7292
const withReservation =
7393
(kind: Reservation["kind"]) =>
7494
<A, E, R>(
@@ -77,8 +97,7 @@ const withReservation =
7797
cwd: string,
7898
effect: Effect.Effect<A, E, R>,
7999
): Effect.Effect<A, E, R> =>
80-
fileSystem.realPath(cwd).pipe(
81-
Effect.orElseSucceed(() => cwd),
100+
canonicalize(fileSystem, path, cwd).pipe(
82101
Effect.flatMap((canonical) => {
83102
const reservation: Reservation = { cwd: canonical, kind };
84103
return reserve(path, reservation).pipe(

0 commit comments

Comments
 (0)