11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { assert , describe , it } from "@effect/vitest" ;
3- import { ProviderDriverKind , type ProviderReplayEntry } from "@t3tools/contracts" ;
3+ import { EventId , ProviderDriverKind , type ProviderReplayEntry } from "@t3tools/contracts" ;
4+ import * as DateTime from "effect/DateTime" ;
45import * as Effect from "effect/Effect" ;
56import * as FileSystem from "effect/FileSystem" ;
67import * as Layer from "effect/Layer" ;
78import * as Path from "effect/Path" ;
9+ import * as SqlClient from "effect/sql/SqlClient" ;
810
911import {
1012 ClaudeOrchestratorReplayHarness ,
1113 makeClaudeRestartReplayHarness ,
1214} from "../Adapters/ClaudeAdapterV2.testkit.ts" ;
1315import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
1416import * as EffectWorker from "../EffectWorker.ts" ;
17+ import * as EventSink from "../EventSink.ts" ;
18+ import * as Orchestrator from "../Orchestrator.ts" ;
1519import * as SqlitePersistence from "../../persistence/Sqlite.ts" ;
1620import { provideDeterministicTestRuntime } from "./DeterministicRuntime.ts" ;
1721import { CLAUDE_BACKGROUND_SUBAGENT_AFTER_ROOT_PROMPT } from "./fixtures/claude_background_subagent_after_root/input.ts" ;
@@ -25,6 +29,7 @@ import * as ProviderReplayHarness from "./ProviderReplayHarness.ts";
2529import { runOrchestratorV2Scenario } from "./OrchestratorScenario.ts" ;
2630import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace" ;
2731import { readProviderReplayTranscript } from "@t3tools/provider-testing/replayTranscript" ;
32+ import { makeProviderReplayGate } from "@t3tools/provider-testing/replayGate" ;
2833
2934const SCENARIO = "claude_background_subagent_after_root" ;
3035const SESSION_ID = "cca274e4-25ae-4171-b972-bbb31118517e" ;
@@ -119,8 +124,9 @@ const readRestartTranscript = Effect.fn("readRestartTranscript")(function* (
119124 ...recorded ,
120125 entries : [
121126 ...recorded . entries . slice ( 0 , rootResult + 1 ) ,
122- // The server dies here, with the background subagent still running.
123- { type : "runtime_exit" , status : "success" } ,
127+ // Leave the provider query open so the first runtime can be snapshotted
128+ // while the background subagent is still running. The runtime is then
129+ // torn down normally; only the durable snapshot is used for recovery.
124130 {
125131 type : "expect_outbound" ,
126132 label : "query.open:resume" ,
@@ -163,6 +169,7 @@ const runRestart = Effect.fn("runRestart")(function* (input: {
163169 const layerDatabase = SqlitePersistence . layerFromPath ( path . join ( tempDir , "state.sqlite" ) ) . pipe (
164170 Layer . provide ( NodeServices . layer ) ,
165171 ) ;
172+ const snapshotPath = path . join ( tempDir , "restart-state.sqlite" ) ;
166173 const scenario = ( name : string , steps : typeof materialized . steps ) => ( {
167174 name : `${ SCENARIO } :${ name } ` ,
168175 transcript,
@@ -172,10 +179,58 @@ const runRestart = Effect.fn("runRestart")(function* (input: {
172179 runtimePolicyOverride : { cwd : workspace } ,
173180 } ) ;
174181
182+ const beforeScenario = scenario ( "before-restart" , phase1Steps ) ;
183+ const beforeReplayGate = makeProviderReplayGate (
184+ phase1Steps . flatMap ( ( step ) =>
185+ step . type === "release_replay_gate" || step . type === "release_replay_gate_after_waiting"
186+ ? [ step . label ]
187+ : [ ] ,
188+ ) ,
189+ ) ;
190+ const beforeLayer = ProviderReplayHarness . layerProviderReplay ( beforeScenario , harness , {
191+ databaseLayer : layerDatabase ,
192+ replayGate : beforeReplayGate ,
193+ } ) ;
194+ const snapshotBeforeProviderTeardown = Effect . gen ( function * ( ) {
195+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
196+ yield * worker . drain ( ) ;
197+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
198+ const eventSink = yield * EventSink . EventSinkV2 ;
199+ const threadId = materialized . projectionThreadIds [ 0 ] ;
200+ if ( threadId === undefined ) {
201+ return yield * Effect . die ( "restart scenario has no projection thread" ) ;
202+ }
203+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
204+ const session = projection . providerSessions [ 0 ] ;
205+ if ( session === undefined ) {
206+ return yield * Effect . die ( "restart scenario has no provider session" ) ;
207+ }
208+ // Older runtimes could persist this orphaned shape across a crash; current
209+ // cleanup settles the provider session when its CLI exits, so seed it here.
210+ const now = yield * DateTime . now ;
211+ yield * eventSink . write ( {
212+ events : [
213+ {
214+ id : EventId . make ( "event:restart-background-note:stopped-provider-session" ) ,
215+ type : "provider-session.updated" ,
216+ threadId,
217+ providerInstanceId : session . providerInstanceId ,
218+ occurredAt : now ,
219+ payload : { ...session , status : "stopped" , updatedAt : now , lastError : null } ,
220+ } ,
221+ ] ,
222+ } ) ;
223+ const sql = yield * SqlClient . SqlClient ;
224+ yield * sql `VACUUM INTO ${ snapshotPath } ` ;
225+ } ) . pipe ( Effect . provide ( Layer . merge ( beforeLayer , layerDatabase ) ) , Effect . orDie ) ;
226+ const snapshotDatabase = SqlitePersistence . layerFromPath ( snapshotPath ) . pipe (
227+ Layer . provide ( NodeServices . layer ) ,
228+ ) ;
175229 const before = yield * Effect . scoped (
176- runOrchestratorV2ProviderReplayScenario ( scenario ( "before-restart" , phase1Steps ) , harness , {
177- databaseLayer : layerDatabase ,
178- } ) ,
230+ runOrchestratorV2Scenario ( beforeScenario , {
231+ replayGate : beforeReplayGate ,
232+ afterSteps : snapshotBeforeProviderTeardown ,
233+ } ) . pipe ( Effect . provide ( beforeLayer ) ) ,
179234 ) ;
180235 const settled = projectionFor ( before , SCENARIO ) ;
181236 assert . equal ( settled . runs [ 0 ] ?. status , "completed" ) ;
@@ -192,7 +247,9 @@ const runRestart = Effect.fn("runRestart")(function* (input: {
192247 } ) . pipe (
193248 Effect . provide (
194249 ProviderReplayHarness . layerProviderReplay ( restartScenario , harness , {
195- databaseLayer : layerDatabase ,
250+ databaseLayer : SqlitePersistence . layerFromPath ( snapshotPath ) . pipe (
251+ Layer . provide ( NodeServices . layer ) ,
252+ ) ,
196253 recoverOnStartup : true ,
197254 continueThreadsAfterServerUpdate : input . continueThreadsAfterServerUpdate ,
198255 } ) ,
@@ -209,7 +266,7 @@ const runRestart = Effect.fn("runRestart")(function* (input: {
209266
210267 const after = yield * Effect . scoped (
211268 runOrchestratorV2ProviderReplayScenario ( scenario ( "after-restart" , phase2Steps ) , harness , {
212- databaseLayer : layerDatabase ,
269+ databaseLayer : snapshotDatabase ,
213270 continueThreadsAfterServerUpdate : input . continueThreadsAfterServerUpdate ,
214271 } ) ,
215272 ) ;
0 commit comments