1+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
12import { assert , it , vi } from "@effect/vitest" ;
23import {
34 CheckpointId ,
@@ -9,19 +10,27 @@ import {
910 ThreadId ,
1011} from "@t3tools/contracts" ;
1112import * as Effect from "effect/Effect" ;
13+ import * as FileSystem from "effect/FileSystem" ;
14+ import * as Path from "effect/Path" ;
15+ import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
1216import * as Layer from "effect/Layer" ;
1317
18+ import { isCheckpointRestoreIsolated } from "./CheckpointRestoreSafety.ts" ;
1419import { CheckpointServiceV2 } from "./CheckpointService.ts" ;
1520import {
1621 CheckpointRollbackServiceV2 ,
17- layer as checkpointRollbackServiceLayer ,
22+ layer as checkpointRollbackLayer ,
1823} from "./CheckpointRollbackService.ts" ;
1924import { EventSinkV2 } from "./EventSink.ts" ;
2025import { layer as idAllocatorLayer } from "./IdAllocator.ts" ;
2126import { ProjectionStoreReadError , ProjectionStoreV2 } from "./ProjectionStore.ts" ;
2227import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts" ;
2328import { RuntimePolicyV2 } from "./RuntimePolicy.ts" ;
2429
30+ const checkpointRollbackServiceLayer = checkpointRollbackLayer . pipe (
31+ Layer . provide ( NodeServices . layer ) ,
32+ ) ;
33+
2534it . effect ( "rejects a non-ready checkpoint before opening a session or restoring files" , ( ) => {
2635 const threadId = ThreadId . make ( "thread:rollback-non-ready" ) ;
2736 const providerThreadId = ProviderThreadId . make ( "provider-thread:rollback-non-ready" ) ;
@@ -34,12 +43,13 @@ it.effect("rejects a non-ready checkpoint before opening a session or restoring
3443 const resolveRuntimePolicy = vi . fn ( ( ) => Effect . die ( "runtime policy resolution must not run" ) ) ;
3544 const projection = {
3645 thread : {
46+ worktreePath : process . cwd ( ) ,
3747 activeProviderThreadId : providerThreadId ,
3848 modelSelection : { instanceId : providerInstanceId , model : "test-model" } ,
3949 } ,
4050 providerThreads : [ { id : providerThreadId , providerSessionId, providerInstanceId } ] ,
4151 checkpoints : [ { id : checkpointId , scopeId, status : "stale" } ] ,
42- checkpointScopes : [ { id : scopeId } ] ,
52+ checkpointScopes : [ { id : scopeId , cwd : process . cwd ( ) } ] ,
4353 } as unknown as OrchestrationV2ThreadProjection ;
4454 const testLayer = checkpointRollbackServiceLayer . pipe (
4555 Layer . provide (
@@ -49,6 +59,13 @@ it.effect("rejects a non-ready checkpoint before opening a session or restoring
4959 idAllocatorLayer ,
5060 Layer . mock ( ProjectionStoreV2 ) ( {
5161 getThreadProjection : ( ) => Effect . succeed ( projection ) ,
62+ getShellSnapshot : ( ) =>
63+ Effect . succeed ( {
64+ schemaVersion : 1 ,
65+ snapshotSequence : 0 ,
66+ threads : [ ] ,
67+ archivedThreads : [ ] ,
68+ } ) ,
5269 } ) ,
5370 Layer . mock ( ProviderSessionManagerV2 ) ( { open } ) ,
5471 Layer . mock ( RuntimePolicyV2 ) ( { resolve : resolveRuntimePolicy } ) ,
@@ -109,7 +126,7 @@ it.effect("rejects a rollback when another provider thread became active", () =>
109126 } ,
110127 ] ,
111128 checkpoints : [ { id : checkpointId , scopeId, status : "ready" } ] ,
112- checkpointScopes : [ { id : scopeId } ] ,
129+ checkpointScopes : [ { id : scopeId , cwd : process . cwd ( ) } ] ,
113130 } as unknown as OrchestrationV2ThreadProjection ;
114131 const testLayer = checkpointRollbackServiceLayer . pipe (
115132 Layer . provide (
@@ -119,6 +136,13 @@ it.effect("rejects a rollback when another provider thread became active", () =>
119136 idAllocatorLayer ,
120137 Layer . mock ( ProjectionStoreV2 ) ( {
121138 getThreadProjection : ( ) => Effect . succeed ( projection ) ,
139+ getShellSnapshot : ( ) =>
140+ Effect . succeed ( {
141+ schemaVersion : 1 ,
142+ snapshotSequence : 0 ,
143+ threads : [ ] ,
144+ archivedThreads : [ ] ,
145+ } ) ,
122146 } ) ,
123147 Layer . mock ( ProviderSessionManagerV2 ) ( { open } ) ,
124148 Layer . mock ( RuntimePolicyV2 ) ( { resolve : resolveRuntimePolicy } ) ,
@@ -170,6 +194,7 @@ it.effect("rejects a rollback when provider selection changed before execution",
170194 const resolveRuntimePolicy = vi . fn ( ( ) => Effect . die ( "runtime policy resolution must not run" ) ) ;
171195 const projection = {
172196 thread : {
197+ worktreePath : process . cwd ( ) ,
173198 activeProviderThreadId : providerThreadId ,
174199 modelSelection : { instanceId : selectedProviderInstanceId , model : "test-model" } ,
175200 } ,
@@ -181,7 +206,7 @@ it.effect("rejects a rollback when provider selection changed before execution",
181206 } ,
182207 ] ,
183208 checkpoints : [ { id : checkpointId , scopeId, status : "ready" } ] ,
184- checkpointScopes : [ { id : scopeId } ] ,
209+ checkpointScopes : [ { id : scopeId , cwd : process . cwd ( ) } ] ,
185210 } as unknown as OrchestrationV2ThreadProjection ;
186211 const testLayer = checkpointRollbackServiceLayer . pipe (
187212 Layer . provide (
@@ -191,6 +216,13 @@ it.effect("rejects a rollback when provider selection changed before execution",
191216 idAllocatorLayer ,
192217 Layer . mock ( ProjectionStoreV2 ) ( {
193218 getThreadProjection : ( ) => Effect . succeed ( projection ) ,
219+ getShellSnapshot : ( ) =>
220+ Effect . succeed ( {
221+ schemaVersion : 1 ,
222+ snapshotSequence : 0 ,
223+ threads : [ ] ,
224+ archivedThreads : [ ] ,
225+ } ) ,
194226 } ) ,
195227 Layer . mock ( ProviderSessionManagerV2 ) ( { open } ) ,
196228 Layer . mock ( RuntimePolicyV2 ) ( { resolve : resolveRuntimePolicy } ) ,
@@ -235,13 +267,14 @@ it.effect("reports a missing provider turn as a structured rollback failure", ()
235267 const restore = vi . fn ( ( ) => Effect . die ( "checkpoint restore must not run" ) ) ;
236268 const projection = {
237269 thread : {
270+ worktreePath : process . cwd ( ) ,
238271 activeProviderThreadId : providerThreadId ,
239272 modelSelection : { instanceId : providerInstanceId , model : "test-model" } ,
240273 } ,
241274 providerThreads : [ { id : providerThreadId , providerSessionId, providerInstanceId } ] ,
242275 providerSessions : [ ] ,
243276 checkpoints : [ { id : checkpointId , scopeId, status : "ready" , appRunOrdinal : 1 } ] ,
244- checkpointScopes : [ { id : scopeId } ] ,
277+ checkpointScopes : [ { id : scopeId , cwd : process . cwd ( ) } ] ,
245278 runs : [ ] ,
246279 attempts : [ ] ,
247280 providerTurns : [ ] ,
@@ -254,6 +287,13 @@ it.effect("reports a missing provider turn as a structured rollback failure", ()
254287 idAllocatorLayer ,
255288 Layer . mock ( ProjectionStoreV2 ) ( {
256289 getThreadProjection : ( ) => Effect . succeed ( projection ) ,
290+ getShellSnapshot : ( ) =>
291+ Effect . succeed ( {
292+ schemaVersion : 1 ,
293+ snapshotSequence : 0 ,
294+ threads : [ ] ,
295+ archivedThreads : [ ] ,
296+ } ) ,
257297 } ) ,
258298 Layer . mock ( ProviderSessionManagerV2 ) ( {
259299 open : ( ) => Effect . succeed ( { } as never ) ,
@@ -330,7 +370,14 @@ it.effect("wraps underlying failures with an unexpected-failure reason and cause
330370 } ) . pipe ( Effect . provide ( testLayer ) ) ;
331371} ) ;
332372
333- it . effect . each ( [ true , false ] ) ( "rewinds provider history with restoreFiles=%s" , ( restoreFiles ) => {
373+ it . effect . each ( [
374+ { restoreFiles : true , shared : "none" } ,
375+ { restoreFiles : false , shared : "root" } ,
376+ { restoreFiles : true , shared : "root" } ,
377+ { restoreFiles : true , shared : "worktree" } ,
378+ { restoreFiles : false , shared : "worktree" } ,
379+ { restoreFiles : true , shared : "historical" } ,
380+ ] ) ( "rewinds safely with %s" , ( { restoreFiles, shared } ) => {
334381 const threadId = ThreadId . make ( "rewind-files" ) ;
335382 const providerThreadId = ProviderThreadId . make ( "rewind-provider" ) ;
336383 const providerSessionId = ProviderSessionId . make ( "rewind-session" ) ;
@@ -345,6 +392,7 @@ it.effect.each([true, false])("rewinds provider history with restoreFiles=%s", (
345392 } ;
346393 const projection = {
347394 thread : {
395+ worktreePath : shared === "root" ? null : process . cwd ( ) ,
348396 activeProviderThreadId : providerThreadId ,
349397 modelSelection : { instanceId, model : "test" } ,
350398 } ,
@@ -353,7 +401,7 @@ it.effect.each([true, false])("rewinds provider history with restoreFiles=%s", (
353401 providerTurns : [ ] ,
354402 nodes : [ ] ,
355403 checkpoints : [ { id : checkpointId , scopeId, status : "ready" , appRunOrdinal : null } ] ,
356- checkpointScopes : [ { id : scopeId } ] ,
404+ checkpointScopes : [ { id : scopeId , cwd : process . cwd ( ) } ] ,
357405 runs : [ { id : "run-1" , ordinal : 1 , status : "completed" , rootNodeId : null } ] ,
358406 } as unknown as OrchestrationV2ThreadProjection ;
359407 const testLayer = checkpointRollbackServiceLayer . pipe (
@@ -378,7 +426,31 @@ it.effect.each([true, false])("rewinds provider history with restoreFiles=%s", (
378426 } ) ,
379427 } ) ,
380428 idAllocatorLayer ,
381- Layer . mock ( ProjectionStoreV2 ) ( { getThreadProjection : ( ) => Effect . succeed ( projection ) } ) ,
429+ Layer . mock ( ProjectionStoreV2 ) ( {
430+ getThreadProjection : ( ) => Effect . succeed ( projection ) ,
431+ getCheckpointContext : ( ) =>
432+ Effect . succeed ( {
433+ checkpointScopes : [ { cwd : process . cwd ( ) } ] ,
434+ runs : [ ] ,
435+ checkpoints : [ ] ,
436+ } as never ) ,
437+ getShellSnapshot : ( ) =>
438+ Effect . succeed ( {
439+ schemaVersion : 1 ,
440+ snapshotSequence : 0 ,
441+ threads : [ ] ,
442+ archivedThreads :
443+ shared === "worktree" || shared === "historical"
444+ ? [
445+ {
446+ id : ThreadId . make ( "other-thread" ) ,
447+ deletedAt : null ,
448+ worktreePath : shared === "worktree" ? process . cwd ( ) : null ,
449+ } as never ,
450+ ]
451+ : [ ] ,
452+ } ) ,
453+ } ) ,
382454 Layer . mock ( ProviderSessionManagerV2 ) ( {
383455 open : ( ) =>
384456 Effect . succeed ( {
@@ -395,10 +467,49 @@ it.effect.each([true, false])("rewinds provider history with restoreFiles=%s", (
395467 ) ;
396468 return Effect . gen ( function * ( ) {
397469 const service = yield * CheckpointRollbackServiceV2 ;
470+ if ( restoreFiles && shared !== "none" ) {
471+ const error = yield * Effect . flip (
472+ service . execute ( { threadId, providerThreadId, checkpointId, scopeId, restoreFiles } ) ,
473+ ) ;
474+ assert . equal ( error . reason , "shared-workspace" ) ;
475+ assert . deepEqual ( calls , [ ] ) ;
476+ return ;
477+ }
398478 yield * service . execute ( { threadId, providerThreadId, checkpointId, scopeId, restoreFiles } ) ;
399479 assert . deepEqual (
400480 calls ,
401481 restoreFiles ? [ "provider" , "files" , "projection" ] : [ "provider" , "projection" ] ,
402482 ) ;
403483 } ) . pipe ( Effect . provide ( testLayer ) ) ;
404484} ) ;
485+
486+ it . effect . skipIf ( ! symlinksSupported ) (
487+ "rejects an archived thread sharing a worktree through a symlink" ,
488+ ( ) =>
489+ Effect . gen ( function * ( ) {
490+ const fileSystem = yield * FileSystem . FileSystem ;
491+ const cwd = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-restore-isolation-" } ) ;
492+ const path = yield * Path . Path ;
493+ const alias = path . join ( cwd , "alias" ) ;
494+ yield * fileSystem . symlink ( cwd , alias ) ;
495+ const threadId = ThreadId . make ( "restore-alias-current" ) ;
496+ const otherId = ThreadId . make ( "restore-alias-archived" ) ;
497+ const projections = ProjectionStoreV2 . of ( {
498+ getShellSnapshot : ( ) =>
499+ Effect . succeed ( {
500+ schemaVersion : 1 ,
501+ snapshotSequence : 0 ,
502+ threads : [ ] ,
503+ archivedThreads : [ { id : otherId , deletedAt : null , worktreePath : alias } as never ] ,
504+ } ) ,
505+ getCheckpointContext : ( ) =>
506+ Effect . succeed ( { runs : [ ] , checkpointScopes : [ ] , checkpoints : [ ] } ) ,
507+ } as never ) ;
508+ const isolated = yield * isCheckpointRestoreIsolated (
509+ { id : threadId , worktreePath : cwd } ,
510+ { cwd } ,
511+ { fileSystem, projections } ,
512+ ) ;
513+ assert . isFalse ( isolated ) ;
514+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
515+ ) ;
0 commit comments