Skip to content

Commit c81c14e

Browse files
committed
refactor(storage): simplify settled worktree cleanup
Drop the symlink-alias path resolution threaded through thread launch, terminals, provider startup and rollback, and the retry loops for a worktree path changing while waiting for its lease. Startup and rollback now take the same path.resolve lease key cleanup and terminals already use. Settled retention counts from settledAt.
1 parent faaaab3 commit c81c14e

12 files changed

Lines changed: 338 additions & 947 deletions

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

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ it.effect("rejects a rollback when another provider thread became active", () =>
179179
const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run"));
180180
const projection = {
181181
thread: {
182+
worktreePath: null,
182183
activeProviderThreadId,
183184
modelSelection: { instanceId: providerInstanceId, model: "test-model" },
184185
},
@@ -390,41 +391,18 @@ it.effect("rejects an unavailable rollback target before opening a session", ()
390391
}).pipe(Effect.provide(layerTest));
391392
});
392393

393-
it.effect.each(
394-
[
395-
{ order: "normal", restoreFiles: true, mutation: "none", alias: "none" },
396-
{ order: "cleanup-first", restoreFiles: true, mutation: "none", alias: "none" },
397-
{ order: "cleanup-first", restoreFiles: false, mutation: "none", alias: "none" },
398-
{ order: "rollback-first", restoreFiles: true, mutation: "none", alias: "none" },
399-
{ order: "cleanup-first", restoreFiles: true, mutation: "none", alias: "direct" },
400-
{ order: "cleanup-first", restoreFiles: false, mutation: "none", alias: "parent" },
401-
{ order: "cleanup-first", restoreFiles: true, mutation: "none", alias: "missing-parent" },
402-
{ order: "cleanup-first", restoreFiles: false, mutation: "none", alias: "dangling" },
403-
{ order: "rollback-first", restoreFiles: true, mutation: "none", alias: "direct" },
404-
{ order: "rollback-first", restoreFiles: false, mutation: "none", alias: "parent" },
405-
{ order: "rollback-first", restoreFiles: true, mutation: "none", alias: "missing-parent" },
406-
{ order: "rollback-first", restoreFiles: false, mutation: "none", alias: "dangling" },
407-
{ order: "cleanup-first", restoreFiles: true, mutation: "worktree", alias: "none" },
408-
{ order: "cleanup-first", restoreFiles: true, mutation: "root", alias: "none" },
409-
].filter(({ alias }) => alias === "none" || symlinksSupported),
410-
)("locks rollback with %s", ({ order, restoreFiles, alias, mutation }) =>
394+
it.effect.each([
395+
{ order: "normal", restoreFiles: true },
396+
{ order: "cleanup-first", restoreFiles: true },
397+
{ order: "cleanup-first", restoreFiles: false },
398+
{ order: "rollback-first", restoreFiles: true },
399+
])("locks rollback with %s", ({ order, restoreFiles }) =>
411400
Effect.gen(function* () {
412401
const fileSystem = yield* FileSystem.FileSystem;
413402
const path = yield* Path.Path;
414403
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-rollback-" });
415-
let worktreePath = path.join(workspaceRoot, "worktree");
416-
const initialWorktreePath = worktreePath;
404+
const worktreePath = path.join(workspaceRoot, "worktree");
417405
yield* fileSystem.makeDirectory(worktreePath);
418-
const aliasPath = path.join(workspaceRoot, "alias");
419-
const parentAlias = alias === "parent" || alias === "missing-parent";
420-
if (alias !== "none") {
421-
yield* fileSystem.symlink(parentAlias ? workspaceRoot : worktreePath, aliasPath);
422-
}
423-
const threadPath =
424-
alias === "none" ? worktreePath : parentAlias ? path.join(aliasPath, "worktree") : aliasPath;
425-
if (alias === "missing-parent" || alias === "dangling") {
426-
yield* fileSystem.remove(worktreePath, { recursive: true });
427-
}
428406
const threadId = ThreadId.make("rollback-worktree");
429407
const projectId = ProjectId.make("rollback-worktree-project");
430408
const providerThreadId = ProviderThreadId.make("rollback-worktree-provider-thread");
@@ -434,19 +412,15 @@ it.effect.each(
434412
const scopeId = CheckpointScopeId.make("rollback-worktree-scope");
435413
const branch = "feature/rollback";
436414
const release = Deferred.makeUnsafe<void>();
437-
const releaseNew = Deferred.makeUnsafe<void>();
438-
const readEntered = Deferred.makeUnsafe<void>();
439-
let readCount = 0;
440-
let newLeaseReleased = false;
441415
const openEntered = Deferred.makeUnsafe<void>();
442416
const calls: string[] = [];
443417
let cleanupEntered = false;
444418
const providerThread = { id: providerThreadId, providerSessionId, providerInstanceId };
445-
let projection = {
419+
const projection = {
446420
thread: {
447421
id: threadId,
448422
projectId,
449-
worktreePath: mutation === "root" ? null : threadPath,
423+
worktreePath,
450424
branch,
451425
activeProviderThreadId: providerThreadId,
452426
modelSelection: { instanceId: providerInstanceId, model: "test-model" },
@@ -505,16 +479,7 @@ it.effect.each(
505479
}),
506480
IdAllocator.layer,
507481
Layer.mock(ProjectionStore.ProjectionStoreV2)({
508-
getThreadRecords: () => {
509-
const current = projection;
510-
readCount++;
511-
return readCount === 1 && mutation !== "none"
512-
? Deferred.succeed(readEntered, undefined).pipe(
513-
Effect.andThen(Deferred.await(release)),
514-
Effect.as(current),
515-
)
516-
: Effect.succeed(current);
517-
},
482+
getThreadRecords: () => Effect.succeed(projection),
518483
getShellSnapshot: () =>
519484
Effect.succeed({
520485
schemaVersion: 1,
@@ -526,7 +491,6 @@ it.effect.each(
526491
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
527492
open: (input) =>
528493
Effect.gen(function* () {
529-
if (mutation !== "none") assert.isTrue(newLeaseReleased);
530494
assert.equal(input.runtimePolicy.cwd, worktreePath);
531495
if (!(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie))) {
532496
return yield* new ProviderWorkspaceMissingError({ threadId, cwd: worktreePath });
@@ -549,11 +513,11 @@ it.effect.each(
549513
yield* Effect.gen(function* () {
550514
const service = yield* CheckpointRollbackService.CheckpointRollbackServiceV2;
551515
const cleanupEffect = withWorkspaceLease(
552-
initialWorktreePath,
516+
worktreePath,
553517
Effect.gen(function* () {
554518
cleanupEntered = true;
555519
if (order === "cleanup-first") yield* Deferred.await(release);
556-
yield* fileSystem.remove(initialWorktreePath, { recursive: true, force: true });
520+
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
557521
calls.push("cleanup");
558522
}),
559523
);
@@ -564,32 +528,6 @@ it.effect.each(
564528
const rollback = yield* service
565529
.execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles })
566530
.pipe(Effect.forkChild({ startImmediately: true }));
567-
if (mutation !== "none") yield* Deferred.await(readEntered);
568-
const newPath = path.join(workspaceRoot, "new-worktree");
569-
const newHolder =
570-
mutation !== "none"
571-
? yield* withWorkspaceLease(
572-
newPath,
573-
Deferred.await(releaseNew).pipe(
574-
Effect.andThen(
575-
Effect.sync(() => {
576-
newLeaseReleased = true;
577-
}),
578-
),
579-
),
580-
).pipe(Effect.forkChild({ startImmediately: true }))
581-
: undefined;
582-
if (mutation !== "none") {
583-
worktreePath = newPath;
584-
projection = {
585-
...projection,
586-
thread: { ...projection.thread, worktreePath },
587-
checkpointScopes: projection.checkpointScopes.map((scope) => ({
588-
...scope,
589-
cwd: worktreePath,
590-
})),
591-
};
592-
}
593531
if (order === "cleanup-first") assert.deepEqual(calls, []);
594532
if (order === "rollback-first") yield* Deferred.await(openEntered);
595533
const laterCleanup =
@@ -599,11 +537,6 @@ it.effect.each(
599537
if (order === "rollback-first") assert.isFalse(cleanupEntered);
600538
yield* Deferred.succeed(release, undefined);
601539
if (cleanup !== undefined) yield* Fiber.join(cleanup);
602-
if (newHolder !== undefined) {
603-
assert.deepEqual(calls, ["cleanup"]);
604-
yield* Deferred.succeed(releaseNew, undefined);
605-
yield* Fiber.join(newHolder);
606-
}
607540
yield* Fiber.join(rollback);
608541
if (laterCleanup !== undefined) yield* Fiber.join(laterCleanup);
609542
const rollbackCalls = restoreFiles
@@ -614,11 +547,7 @@ it.effect.each(
614547
order === "cleanup-first"
615548
? ["cleanup", "prune", "create", ...rollbackCalls]
616549
: order === "rollback-first"
617-
? [
618-
...(alias === "missing-parent" || alias === "dangling" ? ["prune", "create"] : []),
619-
...rollbackCalls,
620-
"cleanup",
621-
]
550+
? [...rollbackCalls, "cleanup"]
622551
: rollbackCalls,
623552
);
624553
}).pipe(Effect.provide(testLayer));

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

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import * as Path from "effect/Path";
1616
import * as Schema from "effect/Schema";
1717

1818
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
19-
import { resolveWorkspacePath, withWorkspaceLease } from "../workspace/workspaceLease.ts";
19+
import { withWorkspaceLease } from "../workspace/workspaceLease.ts";
2020
import {
2121
isCheckpointRestoreIsolated,
2222
SHARED_WORKSPACE_RESTORE_MESSAGE,
2323
} from "./CheckpointRestoreSafety.ts";
2424
import { CheckpointServiceV2 } from "./CheckpointService.ts";
2525
import { EventSinkV2 } from "./EventSink.ts";
2626
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
27-
import * as ProjectionStore from "./ProjectionStore.ts";
27+
import { ProjectionStoreV2 } from "./ProjectionStore.ts";
2828
import * as ProjectStore from "./ProjectStore.ts";
2929
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3030
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
@@ -90,7 +90,7 @@ export const layer: Layer.Layer<
9090
| GitWorkflowService.GitWorkflowService
9191
| EventSinkV2
9292
| IdAllocator.IdAllocatorV2
93-
| ProjectionStore.ProjectionStoreV2
93+
| ProjectionStoreV2
9494
| ProviderSessionManagerV2
9595
| RuntimePolicyV2
9696
| FileSystem.FileSystem
@@ -104,16 +104,22 @@ export const layer: Layer.Layer<
104104
const gitWorkflow = yield* GitWorkflowService.GitWorkflowService;
105105
const eventSink = yield* EventSinkV2;
106106
const ids = yield* IdAllocator.IdAllocatorV2;
107-
const projections = yield* ProjectionStore.ProjectionStoreV2;
107+
const projections = yield* ProjectionStoreV2;
108108
const sessions = yield* ProviderSessionManagerV2;
109109
const runtimePolicy = yield* RuntimePolicyV2;
110110
const fileSystem = yield* FileSystem.FileSystem;
111111
const projects = yield* ProjectStore.ProjectStoreV2;
112112
const path = yield* Path.Path;
113113
const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor;
114114

115-
const readProjection = (threadId: ThreadId) =>
116-
projections.getThreadRecords(threadId, [
115+
const execute = Effect.fn("orchestrationV2.checkpointRollback.execute")(function* (input: {
116+
readonly threadId: ThreadId;
117+
readonly providerThreadId: ProviderThreadId;
118+
readonly checkpointId: CheckpointId;
119+
readonly scopeId: CheckpointScopeId;
120+
readonly restoreFiles?: boolean;
121+
}) {
122+
const projection = yield* projections.getThreadRecords(input.threadId, [
117123
"providerThreads",
118124
"providerSessions",
119125
"checkpoints",
@@ -123,17 +129,6 @@ export const layer: Layer.Layer<
123129
"nodes",
124130
"providerTurns",
125131
]);
126-
127-
const execute = Effect.fn("orchestrationV2.checkpointRollback.execute")(function* (
128-
input: {
129-
readonly threadId: ThreadId;
130-
readonly providerThreadId: ProviderThreadId;
131-
readonly checkpointId: CheckpointId;
132-
readonly scopeId: CheckpointScopeId;
133-
readonly restoreFiles?: boolean;
134-
},
135-
projection: Effect.Success<ReturnType<typeof readProjection>>,
136-
) {
137132
const providerThread = projection.providerThreads.find(
138133
(candidate) => candidate.id === input.providerThreadId,
139134
);
@@ -181,6 +176,7 @@ export const layer: Layer.Layer<
181176
const targetAttempt = projection.attempts.find(
182177
(attempt) => attempt.id === targetRun?.activeAttemptId,
183178
);
179+
// A goal run can span several native turns; roll back to its last.
184180
const targetTurn =
185181
latestProviderTurnForAttempt(projection.providerTurns, targetAttempt?.id) ??
186182
projection.providerTurns.find((turn) => turn.id === targetAttempt?.providerTurnId);
@@ -379,36 +375,11 @@ export const layer: Layer.Layer<
379375
return CheckpointRollbackServiceV2.of({
380376
execute: (input) =>
381377
Effect.gen(function* () {
382-
const resolvePath = (worktreePath: string | null) =>
383-
worktreePath == null
384-
? Effect.succeed(null)
385-
: resolveWorkspacePath(worktreePath).pipe(
386-
Effect.provideService(FileSystem.FileSystem, fileSystem),
387-
Effect.provideService(Path.Path, path),
388-
);
389-
const initial = yield* projections.getThreadRecords(input.threadId, []);
390-
let worktreePath = yield* resolvePath(initial.thread.worktreePath);
391-
while (true) {
392-
const effect = Effect.gen(function* () {
393-
const storedProjection = yield* readProjection(input.threadId);
394-
const freshPath = yield* resolvePath(storedProjection.thread.worktreePath);
395-
if (freshPath !== worktreePath) return freshPath;
396-
const projection =
397-
freshPath === null
398-
? storedProjection
399-
: {
400-
...storedProjection,
401-
thread: { ...storedProjection.thread, worktreePath: freshPath },
402-
};
403-
yield* execute(input, projection);
404-
return undefined;
405-
});
406-
const freshPath = yield* worktreePath === null
407-
? effect
408-
: withWorkspaceLease(worktreePath, effect);
409-
if (freshPath === undefined) return;
410-
worktreePath = freshPath;
411-
}
378+
// Storage cleanup removes worktrees under the same lease.
379+
const { thread } = yield* projections.getThreadRecords(input.threadId, []);
380+
yield* thread.worktreePath === null
381+
? execute(input)
382+
: withWorkspaceLease(path.resolve(thread.worktreePath), execute(input));
412383
}).pipe(
413384
Effect.mapError((cause) =>
414385
isCheckpointRollbackExecutionError(cause)

0 commit comments

Comments
 (0)