Skip to content

Commit 8e4748b

Browse files
fix(server): restore reservations cover only file-restoring worktree rollbacks
Conversation-only rewinds and shared-workspace threads rewrite no files, so they no longer hold the restore reservation across the provider rollback. Turn starts let project lookup errors surface instead of skipping the reservation, and reservation overlap uses path.relative containment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96fd14c commit 8e4748b

5 files changed

Lines changed: 144 additions & 28 deletions

File tree

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

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import * as SqlitePersistence from "../persistence/Sqlite.ts";
3232
import * as ServerConfig from "../config.ts";
3333
import * as ServerSettings from "../serverSettings.ts";
3434
import * as GitWorkflow from "../git/GitWorkflowService.ts";
35-
import { withWorkspaceLease } from "../workspace/workspaceLease.ts";
35+
import { withTurnStartReservation, withWorkspaceLease } from "../workspace/workspaceLease.ts";
3636
import { ProviderWorkspaceMissingError } from "../provider/Errors.ts";
3737
import { resolveCodexRollbackTurnCount } from "./Adapters/CodexAdapterV2.ts";
3838
import * as CheckpointService from "./CheckpointService.ts";
@@ -347,6 +347,118 @@ it.effect.each([
347347
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
348348
);
349349

350+
it.effect.each([{ restoreFiles: true }, { restoreFiles: false }])(
351+
"holds turn starts inside the worktree during rollback with restoreFiles $restoreFiles",
352+
({ restoreFiles }) =>
353+
Effect.gen(function* () {
354+
const fileSystem = yield* FileSystem.FileSystem;
355+
const path = yield* Path.Path;
356+
const workspaceRoot = yield* fileSystem.realPath(
357+
yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-rollback-reservation-" }),
358+
);
359+
const worktreesDir = path.join(workspaceRoot, "worktrees");
360+
const worktreePath = path.join(worktreesDir, "repo", "feature");
361+
yield* fileSystem.makeDirectory(worktreePath, { recursive: true });
362+
const threadId = ThreadId.make("rollback-reservation");
363+
const providerThreadId = ProviderThreadId.make("rollback-reservation-provider-thread");
364+
const providerInstanceId = ProviderInstanceId.make("rollback-reservation-provider");
365+
const checkpointId = CheckpointId.make("rollback-reservation-checkpoint");
366+
const scopeId = CheckpointScopeId.make("rollback-reservation-scope");
367+
const release = Deferred.makeUnsafe<void>();
368+
const openEntered = Deferred.makeUnsafe<void>();
369+
const calls: string[] = [];
370+
const projection = {
371+
thread: {
372+
id: threadId,
373+
projectId: ProjectId.make("rollback-reservation-project"),
374+
worktreePath,
375+
branch: "feature/rollback-reservation",
376+
activeProviderThreadId: providerThreadId,
377+
modelSelection: { instanceId: providerInstanceId, model: "test-model" },
378+
},
379+
providerThread: {
380+
id: providerThreadId,
381+
providerSessionId: ProviderSessionId.make("rollback-reservation-session"),
382+
providerInstanceId,
383+
},
384+
providerSession: undefined,
385+
checkpoint: { id: checkpointId, scopeId, status: "ready", appRunOrdinal: 0 },
386+
scope: { id: scopeId, cwd: worktreePath },
387+
runsToRollback: [],
388+
targetAttempt: undefined,
389+
rolledBackAttemptIds: [],
390+
staleCheckpoints: [],
391+
rootNodes: [],
392+
providerTurns: [],
393+
} as unknown as ProjectionStore.ProjectionCheckpointRollbackContext;
394+
const testLayer = CheckpointRollbackService.layer.pipe(
395+
Layer.provide(
396+
Layer.mergeAll(
397+
ThreadCommandExecutor.layer,
398+
NodeServices.layer,
399+
Layer.mock(ServerConfig.ServerConfig)({ worktreesDir } as never),
400+
ServerSettings.layerTest(),
401+
Layer.mock(ProjectStore.ProjectStoreV2)({
402+
get: () => Effect.succeed(Option.some({ workspaceRoot } as never)),
403+
}),
404+
Layer.mock(GitWorkflow.GitWorkflowService)({}),
405+
Layer.mock(CheckpointService.CheckpointServiceV2)({
406+
restore: () => Effect.sync(() => calls.push("files")),
407+
}),
408+
Layer.mock(EventSink.EventSinkV2)({ write: () => Effect.succeed([]) }),
409+
IdAllocator.layer,
410+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
411+
getThread: () => Effect.succeed(projection.thread),
412+
getCheckpointRollbackContext: () => Effect.succeed(projection),
413+
getShellSnapshot: () =>
414+
Effect.succeed({
415+
schemaVersion: 1,
416+
snapshotSequence: 0,
417+
threads: [],
418+
archivedThreads: [],
419+
}),
420+
}),
421+
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
422+
open: () =>
423+
Deferred.succeed(openEntered, undefined).pipe(
424+
Effect.andThen(Deferred.await(release)),
425+
Effect.as({} as never),
426+
),
427+
}),
428+
Layer.mock(RuntimePolicy.RuntimePolicyV2)({
429+
resolve: () => Effect.succeed({ cwd: worktreePath } as never),
430+
}),
431+
),
432+
),
433+
);
434+
yield* Effect.gen(function* () {
435+
const service = yield* CheckpointRollbackService.CheckpointRollbackServiceV2;
436+
const rollback = yield* service
437+
.execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles })
438+
.pipe(Effect.forkChild({ startImmediately: true }));
439+
yield* Deferred.await(openEntered);
440+
// A synchronous realPath lets the forked start reach the reservation
441+
// before the next assertion, so "not entered" means "waiting".
442+
const turnStart = yield* withTurnStartReservation(
443+
FileSystem.makeNoop({ realPath: (target) => Effect.succeed(target) }),
444+
path,
445+
path.join(worktreePath, "packages", "app"),
446+
Effect.sync(() => calls.push("turn start")),
447+
).pipe(Effect.forkChild({ startImmediately: true }));
448+
if (restoreFiles) {
449+
assert.deepEqual(calls, []);
450+
} else {
451+
// Nothing is rewritten, so the turn start must not wait for the provider.
452+
yield* Fiber.join(turnStart);
453+
}
454+
yield* Deferred.succeed(release, undefined);
455+
yield* Fiber.join(rollback);
456+
yield* Fiber.join(turnStart);
457+
assert.deepEqual(calls, restoreFiles ? ["files", "turn start"] : ["turn start"]);
458+
}).pipe(Effect.provide(testLayer));
459+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
460+
);
461+
350462
const seedRollbackHistory = Effect.gen(function* () {
351463
const store = yield* ProjectionStore.ProjectionStoreV2;
352464
const now = yield* DateTime.now;

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,19 +363,15 @@ export const layer: Layer.Layer<
363363
function* (input) {
364364
// Storage cleanup removes worktrees under the same lease.
365365
const thread = yield* projections.getThread(input.threadId);
366-
const cwd =
367-
thread.worktreePath ??
368-
(yield* projects.get(thread.projectId).pipe(
369-
Effect.map((project) => Option.getOrUndefined(project)?.workspaceRoot),
370-
Effect.orElseSucceed(() => undefined),
371-
));
372-
const rollback =
373-
thread.worktreePath === null
374-
? execute(input)
375-
: withWorkspaceLease(path.resolve(thread.worktreePath), execute(input));
376-
yield* cwd === undefined
366+
if (thread.worktreePath === null) return yield* execute(input);
367+
const worktreePath = path.resolve(thread.worktreePath);
368+
const rollback = withWorkspaceLease(worktreePath, execute(input));
369+
// Shared-workspace restores are rejected and conversation-only rewinds
370+
// touch no files; reserving for them would stall turn starts on the
371+
// slow provider rollback for nothing.
372+
yield* input.restoreFiles === false
377373
? rollback
378-
: withCheckpointRestoreReservation(fileSystem, path.resolve(cwd), rollback);
374+
: withCheckpointRestoreReservation(fileSystem, path, worktreePath, rollback);
379375
},
380376
(effect, input) =>
381377
effect.pipe(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ effectIt.effect.each([
10101010
// isolation check, provider rollback, and git restore of `restorePath`.
10111011
const restore = yield* withCheckpointRestoreReservation(
10121012
fileSystem,
1013+
yield* Path.Path,
10131014
restorePath,
10141015
Deferred.succeed(restoreEntered, undefined).pipe(
10151016
Effect.andThen(Deferred.await(releaseRestore)),
@@ -1022,7 +1023,7 @@ effectIt.effect.each([
10221023
yield* Fiber.join(restore);
10231024
yield* Fiber.join(startup);
10241025
expect(order).toEqual(["checkpoint files restored", "provider session opened"]);
1025-
}),
1026+
}).pipe(Effect.provide(Path.layer)),
10261027
);
10271028

10281029
effectIt.effect("reads current startup state after waiting for the workspace lease", () =>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,14 +1345,14 @@ export const layer: Layer.Layer<
13451345
const { thread } = yield* projectionStore.getThreadRecords(input.threadId, []);
13461346
const cwd =
13471347
thread.worktreePath ??
1348-
(yield* projects.getById(thread.projectId).pipe(
1349-
Effect.map((project) => Option.getOrUndefined(project)?.workspaceRoot),
1350-
Effect.orElseSucceed(() => undefined),
1351-
));
1348+
(yield* projects
1349+
.getById(thread.projectId, { includeDeleted: true })
1350+
.pipe(Effect.map((project) => Option.getOrUndefined(project)?.workspaceRoot)));
13521351
const providerTurn = yield* cwd === undefined
13531352
? start(input)
13541353
: withTurnStartReservation(
13551354
fileSystem,
1355+
path,
13561356
path.resolve(cwd),
13571357
thread.worktreePath === null
13581358
? start(input)

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Deferred from "effect/Deferred";
22
import * as Effect from "effect/Effect";
33
import type * as FileSystem from "effect/FileSystem";
4+
import type * as Path from "effect/Path";
45
import * as Semaphore from "effect/Semaphore";
56

67
const leases = new Map<string, { semaphore: Semaphore.Semaphore; users: number }>();
@@ -35,20 +36,25 @@ interface Reservation {
3536
const reservations = new Set<Reservation>();
3637
let reservationReleased = Deferred.makeUnsafe<void>();
3738

38-
const contains = (parent: string, child: string) =>
39-
child === parent ||
40-
child.startsWith(parent.endsWith("/") ? parent : `${parent}/`) ||
41-
child.startsWith(parent.endsWith("\\") ? parent : `${parent}\\`);
39+
// path.relative folds win32 drive-letter case and treats a POSIX backslash as
40+
// part of a name, unlike a raw prefix check.
41+
const contains = (path: Path.Path, parent: string, child: string) => {
42+
const relative = path.relative(parent, child);
43+
return (
44+
relative === "" ||
45+
(!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`))
46+
);
47+
};
4248

43-
const conflicts = (a: Reservation, b: Reservation) =>
49+
const conflicts = (path: Path.Path, a: Reservation, b: Reservation) =>
4450
(a.kind === "restore" || b.kind === "restore") &&
45-
(contains(a.cwd, b.cwd) || contains(b.cwd, a.cwd));
51+
(contains(path, a.cwd, b.cwd) || contains(path, b.cwd, a.cwd));
4652

47-
const reserve = (reservation: Reservation): Effect.Effect<void> =>
53+
const reserve = (path: Path.Path, reservation: Reservation): Effect.Effect<void> =>
4854
Effect.suspend(() => {
4955
for (const held of reservations) {
50-
if (conflicts(held, reservation)) {
51-
return Deferred.await(reservationReleased).pipe(Effect.andThen(reserve(reservation)));
56+
if (conflicts(path, held, reservation)) {
57+
return Deferred.await(reservationReleased).pipe(Effect.andThen(reserve(path, reservation)));
5258
}
5359
}
5460
reservations.add(reservation);
@@ -67,14 +73,15 @@ const withReservation =
6773
(kind: Reservation["kind"]) =>
6874
<A, E, R>(
6975
fileSystem: FileSystem.FileSystem,
76+
path: Path.Path,
7077
cwd: string,
7178
effect: Effect.Effect<A, E, R>,
7279
): Effect.Effect<A, E, R> =>
7380
fileSystem.realPath(cwd).pipe(
7481
Effect.orElseSucceed(() => cwd),
7582
Effect.flatMap((canonical) => {
7683
const reservation: Reservation = { cwd: canonical, kind };
77-
return reserve(reservation).pipe(
84+
return reserve(path, reservation).pipe(
7885
Effect.andThen(effect),
7986
Effect.ensuring(release(reservation)),
8087
);

0 commit comments

Comments
 (0)