@@ -12,8 +12,12 @@ import {
1212 VcsUnsupportedOperationError ,
1313} from "@t3tools/contracts" ;
1414import * as DateTime from "effect/DateTime" ;
15+ import * as Deferred from "effect/Deferred" ;
1516import * as Effect from "effect/Effect" ;
17+ import * as Fiber from "effect/Fiber" ;
1618import * as Layer from "effect/Layer" ;
19+ import * as Ref from "effect/Ref" ;
20+ import * as TestClock from "effect/testing/TestClock" ;
1721
1822import * as CheckpointStore from "../checkpointing/CheckpointStore.ts" ;
1923import * as CheckpointService from "./CheckpointService.ts" ;
@@ -98,6 +102,79 @@ it.effect(
98102 } ) ,
99103) ;
100104
105+ it . effect (
106+ "bounds a slow baseline snapshot but waits for an in-flight capture of the same ref" ,
107+ ( ) =>
108+ Effect . gen ( function * ( ) {
109+ const scope : OrchestrationV2CheckpointScope = {
110+ id : CheckpointScopeId . make ( "checkpoint-scope:slow-baseline" ) ,
111+ threadId : ThreadId . make ( "thread:slow-baseline" ) ,
112+ runId : RunId . make ( "run:slow-baseline:1" ) ,
113+ nodeId : NodeId . make ( "node:slow-baseline:1" ) ,
114+ parentScopeId : null ,
115+ providerThreadId : ProviderThreadId . make ( "provider-thread:slow-baseline" ) ,
116+ kind : "root_run" ,
117+ ordinalWithinParent : 0 ,
118+ advancesAppRunCount : true ,
119+ cwd : "/repo/slow" ,
120+ createdAt : DateTime . makeUnsafe ( "2026-07-28T00:00:00.000Z" ) ,
121+ } ;
122+ const refs = yield * Ref . make < ReadonlySet < string > > ( new Set ( ) ) ;
123+ const snapshots = yield * Ref . make ( 0 ) ;
124+ const firstSnapshotStarted = yield * Deferred . make < void > ( ) ;
125+ const layerTest = CheckpointService . layer . pipe (
126+ Layer . provide (
127+ Layer . mergeAll (
128+ IdAllocator . layer ,
129+ Layer . mock ( CheckpointStore . CheckpointStore ) ( {
130+ isGitRepository : ( ) => Effect . succeed ( true ) ,
131+ hasCheckpointRef : ( { checkpointRef } ) =>
132+ Ref . get ( refs ) . pipe ( Effect . map ( ( current ) => current . has ( checkpointRef ) ) ) ,
133+ captureCheckpoint : ( { checkpointRef } ) =>
134+ Effect . gen ( function * ( ) {
135+ yield * Ref . update ( snapshots , ( count ) => count + 1 ) ;
136+ yield * Deferred . succeed ( firstSnapshotStarted , undefined ) ;
137+ yield * Effect . sleep ( "10 seconds" ) ;
138+ yield * Ref . update ( refs , ( current ) => new Set ( [ ...current , checkpointRef ] ) ) ;
139+ } ) ,
140+ } ) ,
141+ ) ,
142+ ) ,
143+ Layer . provideMerge ( NodeCrypto . layer ) ,
144+ ) ;
145+
146+ yield * Effect . gen ( function * ( ) {
147+ const checkpoints = yield * CheckpointService . CheckpointServiceV2 ;
148+ // A stopped run's post-turn capture writes the next run's baseline ref while holding the lock.
149+ const postTurn = yield * checkpoints
150+ . capture ( {
151+ scope,
152+ ordinalWithinScope : 1 ,
153+ runId : scope . runId ! ,
154+ nodeId : scope . nodeId ! ,
155+ appRunOrdinal : 1 ,
156+ capturedAt : scope . createdAt ,
157+ } )
158+ . pipe ( Effect . forkChild ) ;
159+ yield * Deferred . await ( firstSnapshotStarted ) ;
160+ const waitingBaseline = yield * checkpoints
161+ . captureBaseline ( { scope, ordinalWithinScope : 1 } )
162+ . pipe ( Effect . result , Effect . forkChild ) ;
163+ yield * TestClock . adjust ( "10 seconds" ) ;
164+ assert . strictEqual ( ( yield * Fiber . join ( postTurn ) ) . status , "ready" ) ;
165+ assert . strictEqual ( ( yield * Fiber . join ( waitingBaseline ) ) . _tag , "Success" ) ;
166+
167+ const slowBaseline = yield * checkpoints
168+ . captureBaseline ( { scope, ordinalWithinScope : 2 } )
169+ . pipe ( Effect . result , Effect . forkChild ) ;
170+ yield * TestClock . adjust ( "5 seconds" ) ;
171+ assert . strictEqual ( ( yield * Fiber . join ( slowBaseline ) ) . _tag , "Failure" ) ;
172+ assert . strictEqual ( yield * Ref . get ( snapshots ) , 2 ) ;
173+ assert . strictEqual ( ( yield * Ref . get ( refs ) ) . size , 1 ) ;
174+ } ) . pipe ( Effect . provide ( layerTest ) ) ;
175+ } ) ,
176+ ) ;
177+
101178it . effect ( "records a repository probe failure as an error checkpoint, not a missing one" , ( ) =>
102179 Effect . gen ( function * ( ) {
103180 const scope : OrchestrationV2CheckpointScope = {
0 commit comments