11import { assert , it } from "@effect/vitest" ;
22import {
3+ CheckpointId ,
4+ CheckpointScopeId ,
35 CommandId ,
46 NodeId ,
57 ProviderSessionId ,
@@ -8,6 +10,7 @@ import {
810 RunAttemptId ,
911 RunId ,
1012 ThreadId ,
13+ VcsProcessExitError ,
1114} from "@t3tools/contracts" ;
1215import * as Cause from "effect/Cause" ;
1316import * as DateTime from "effect/DateTime" ;
@@ -21,6 +24,7 @@ import * as Ref from "effect/Ref";
2124import * as TestClock from "effect/testing/TestClock" ;
2225
2326import * as CheckpointRollbackService from "./CheckpointRollbackService.ts" ;
27+ import * as CheckpointService from "./CheckpointService.ts" ;
2428import * as EffectOutbox from "./EffectOutbox.ts" ;
2529import * as EffectWorker from "./EffectWorker.ts" ;
2630import * as RunFinalizationService from "./RunFinalizationService.ts" ;
@@ -31,6 +35,7 @@ import * as RuntimeRequestService from "./RuntimeRequestService.ts";
3135import * as ThreadTitleRegenerationService from "./ThreadTitleRegenerationService.ts" ;
3236import * as ThreadManagementService from "./ThreadManagementService.ts" ;
3337import * as ServerSettings from "../serverSettings.ts" ;
38+ import { GIT_INDEX_LOCKED_DETAIL } from "../vcs/VcsProcess.ts" ;
3439
3540const threadId = ThreadId . make ( "thread:effect-worker-restart" ) ;
3641const oldSessionId = ProviderSessionId . make ( "provider-session:effect-worker-restart:old" ) ;
@@ -83,6 +88,7 @@ function layerExecutorFor(input: {
8388 readonly threads ?: Partial < ThreadManagementService . ThreadManagementService [ "Service" ] > ;
8489 readonly continueAfterRestart ?: boolean ;
8590 readonly interrupt ?: ProviderTurnControlService . ProviderTurnControlServiceV2Shape [ "interrupt" ] ;
91+ readonly rollback ?: CheckpointRollbackService . CheckpointRollbackServiceV2Shape [ "execute" ] ;
8692} ) {
8793 const record = ( event : string ) => Ref . update ( input . events , ( events ) => [ ...events , event ] ) ;
8894 const layerDependencies = Layer . mergeAll (
@@ -136,7 +142,9 @@ function layerExecutorFor(input: {
136142 ) ,
137143 Layer . succeed (
138144 CheckpointRollbackService . CheckpointRollbackServiceV2 ,
139- CheckpointRollbackService . CheckpointRollbackServiceV2 . of ( { execute : ( ) => Effect . void } ) ,
145+ CheckpointRollbackService . CheckpointRollbackServiceV2 . of ( {
146+ execute : input . rollback ?? ( ( ) => Effect . void ) ,
147+ } ) ,
140148 ) ,
141149 Layer . succeed (
142150 RuntimeRequestService . RuntimeRequestServiceV2 ,
@@ -231,6 +239,73 @@ it.effect("settles a stopped run when its adapter has already lost the native tu
231239 } ) ,
232240) ;
233241
242+ const rollbackCheckpointId = CheckpointId . make ( "checkpoint:effect-worker-rollback" ) ;
243+ const rollbackScopeId = CheckpointScopeId . make ( "checkpoint-scope:effect-worker-rollback" ) ;
244+ const restoreFailure = ( detail : string ) =>
245+ new CheckpointRollbackService . CheckpointRollbackExecutionError ( {
246+ reason : "unexpected-failure" ,
247+ threadId,
248+ providerThreadId,
249+ checkpointId : rollbackCheckpointId ,
250+ cause : new CheckpointService . CheckpointRestoreError ( {
251+ scopeId : rollbackScopeId ,
252+ checkpointId : rollbackCheckpointId ,
253+ cause : new VcsProcessExitError ( {
254+ operation : "GitVcsDriverCore.restoreCheckpoint" ,
255+ command : "git" ,
256+ cwd : "/private/repo" ,
257+ exitCode : 128 ,
258+ detail,
259+ failureKind : "command-failed" ,
260+ } ) ,
261+ } ) ,
262+ } ) ;
263+
264+ it . effect . each ( [
265+ {
266+ name : "a held Git index lock" ,
267+ failure : restoreFailure ( GIT_INDEX_LOCKED_DETAIL ) ,
268+ message : GIT_INDEX_LOCKED_DETAIL ,
269+ } ,
270+ {
271+ name : "raw Git output" ,
272+ failure : restoreFailure ( "error: could not clean /private/repo/secret.txt" ) ,
273+ message : CheckpointRollbackService . ROLLBACK_FAILED_MESSAGE ,
274+ } ,
275+ ] ) ( "reports a rollback blocked by $name only after the last attempt" , ( { failure, message } ) =>
276+ Effect . gen ( function * ( ) {
277+ const now = yield * DateTime . now ;
278+ const failures = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
279+ const layer = layerExecutorFor ( {
280+ events : yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ,
281+ rollback : ( ) => Effect . fail ( failure ) ,
282+ threads : {
283+ dispatch : ( command ) =>
284+ Ref . update ( failures , ( current ) =>
285+ command . type === "checkpoint.rollback.fail" ? [ ...current , command . message ] : current ,
286+ ) . pipe ( Effect . as ( { sequence : 1 , storedEvents : [ ] } ) ) ,
287+ } ,
288+ } ) ;
289+ yield * Effect . gen ( function * ( ) {
290+ const executor = yield * EffectWorker . OrchestrationEffectExecutorV2 ;
291+ const rollback : EffectOutbox . OrchestrationEffectV2 = {
292+ ...restartEffect ( now , { type : "detach" } ) ,
293+ request : {
294+ type : "provider-thread.rollback" ,
295+ providerThreadId,
296+ checkpointId : rollbackCheckpointId ,
297+ scopeId : rollbackScopeId ,
298+ restoreFiles : true ,
299+ } ,
300+ } ;
301+ yield * Effect . flip ( executor . execute ( rollback , { willRetry : true } ) ) ;
302+ assert . deepEqual ( yield * Ref . get ( failures ) , [ ] ) ;
303+ yield * Effect . flip ( executor . execute ( rollback , { willRetry : false } ) ) ;
304+ } ) . pipe ( Effect . provide ( layer ) ) ;
305+ assert . deepEqual ( yield * Ref . get ( failures ) , [ message ] ) ;
306+ } ) ,
307+ ) ;
308+
234309it . effect ( "requeues a claim when a pre-execution worker check fails" , ( ) =>
235310 Effect . gen ( function * ( ) {
236311 const now = DateTime . formatIso ( yield * DateTime . now ) ;
0 commit comments