@@ -213,55 +213,87 @@ it.effect.each(["startup", "shutdown"] as const)(
213213 } ) . pipe ( Effect . provide ( layerRecoveryTest ) ) ,
214214) ;
215215
216- it . effect ( "stops a session its thread detached, which kept its live status" , ( ) =>
217- Effect . gen ( function * ( ) {
218- const projections = yield * ProjectionStore . ProjectionStoreV2 ;
219- const recovery = yield * ProviderRuntimeRecovery . ProviderRuntimeRecoveryService ;
220- const now = yield * DateTime . now ;
221- const threadId = yield * createThread ( "detached-session" ) ;
222- const providerSessionId = ProviderSessionId . make ( "session:recovery:detached" ) ;
223- yield * projections . apply ( {
224- id : EventId . make ( "event:recovery:detached-session:attached" ) ,
225- type : "provider-session.attached" ,
226- threadId,
227- driver,
228- providerInstanceId,
229- occurredAt : now ,
230- payload : {
231- id : providerSessionId ,
216+ it . effect . each ( [ "startup" , "shutdown" ] as const ) (
217+ "stops a detached session and its orphaned active provider thread on %s" ,
218+ ( trigger ) =>
219+ Effect . gen ( function * ( ) {
220+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
221+ const recovery = yield * ProviderRuntimeRecovery . ProviderRuntimeRecoveryService ;
222+ const now = yield * DateTime . now ;
223+ const threadId = yield * createThread ( "detached-session" ) ;
224+ const providerSessionId = ProviderSessionId . make ( "session:recovery:detached" ) ;
225+ yield * projections . apply ( {
226+ id : EventId . make ( "event:recovery:detached-session:attached" ) ,
227+ type : "provider-session.attached" ,
228+ threadId,
232229 driver,
233230 providerInstanceId,
234- status : "ready" ,
235- cwd : "/workspace" ,
236- model : modelSelection . model ,
237- capabilities : CodexProviderCapabilitiesV2 ,
238- createdAt : now ,
239- updatedAt : now ,
240- lastError : null ,
241- } ,
242- } ) ;
243- yield * projections . apply ( {
244- id : EventId . make ( "event:recovery:detached-session:detached" ) ,
245- type : "provider-session.detached" ,
246- threadId,
247- driver,
248- providerInstanceId,
249- occurredAt : now ,
250- payload : { providerSessionId, detachedAt : now , reason : "Thread settled." } ,
251- } ) ;
252- assert . deepEqual ( yield * projections . getRecoveryThreadIds ( "runtime" ) , [ threadId ] ) ;
231+ occurredAt : now ,
232+ payload : {
233+ id : providerSessionId ,
234+ driver,
235+ providerInstanceId,
236+ status : "ready" ,
237+ cwd : "/workspace" ,
238+ model : modelSelection . model ,
239+ capabilities : CodexProviderCapabilitiesV2 ,
240+ createdAt : now ,
241+ updatedAt : now ,
242+ lastError : null ,
243+ } ,
244+ } ) ;
245+ yield * projections . apply ( {
246+ id : EventId . make ( "event:recovery:detached-session:detached" ) ,
247+ type : "provider-session.detached" ,
248+ threadId,
249+ driver,
250+ providerInstanceId,
251+ occurredAt : now ,
252+ payload : { providerSessionId, detachedAt : now , reason : "Thread settled." } ,
253+ } ) ;
254+ const providerThreadId = ProviderThreadId . make ( "provider-thread:recovery:detached" ) ;
255+ yield * projections . apply ( {
256+ id : EventId . make ( "event:recovery:detached-provider-thread" ) ,
257+ type : "provider-thread.updated" ,
258+ threadId,
259+ occurredAt : now ,
260+ payload : {
261+ id : providerThreadId ,
262+ appThreadId : threadId ,
263+ ownerNodeId : null ,
264+ driver,
265+ providerInstanceId,
266+ providerSessionId : null ,
267+ nativeThreadRef : null ,
268+ nativeConversationHeadRef : null ,
269+ status : "active" ,
270+ firstRunOrdinal : null ,
271+ lastRunOrdinal : null ,
272+ handoffIds : [ ] ,
273+ forkedFrom : null ,
274+ createdAt : now ,
275+ updatedAt : now ,
276+ } ,
277+ } ) ;
278+ assert . deepEqual ( yield * projections . getRecoveryThreadIds ( "runtime" ) , [ threadId ] ) ;
253279
254- const summary = yield * recovery . reconcile ( "startup" ) ;
280+ const summary = yield * recovery . reconcile ( trigger ) ;
255281
256- assert . equal ( summary . stoppedSessions , 1 ) ;
257- const sql = yield * SqlClient . SqlClient ;
258- const rows = yield * sql < { readonly status : string } > `
282+ assert . equal ( summary . stoppedSessions , 1 ) ;
283+ const sql = yield * SqlClient . SqlClient ;
284+ const rows = yield * sql < { readonly status : string } > `
259285 SELECT status FROM orchestration_v2_projection_provider_sessions
260286 WHERE provider_session_id = ${ providerSessionId }
261287 ` ;
262- assert . deepEqual ( rows , [ { status : "stopped" } ] ) ;
263- assert . deepEqual ( yield * projections . getRecoveryThreadIds ( "runtime" ) , [ ] ) ;
264- } ) . pipe ( Effect . provide ( layerRecoveryTest ) ) ,
288+ assert . deepEqual ( rows , [ { status : "stopped" } ] ) ;
289+ const projection = yield * projections . getThreadProjection ( threadId ) ;
290+ assert . equal ( projection . providerThreads [ 0 ] ?. status , "idle" ) ;
291+ const eventStore = yield * EventStore . EventStoreV2 ;
292+ const sequence = yield * eventStore . latestSequence ( ) ;
293+ assert . deepEqual ( yield * projections . getRecoveryThreadIds ( "runtime" ) , [ ] ) ;
294+ yield * recovery . reconcile ( trigger ) ;
295+ assert . equal ( yield * eventStore . latestSequence ( ) , sequence ) ;
296+ } ) . pipe ( Effect . provide ( layerRecoveryTest ) ) ,
265297) ;
266298
267299it . effect . each ( [ "attached" , "detached" , "replaced" ] as const ) (
@@ -964,11 +996,18 @@ it.effect("bounds streaming recovery reads by candidate runs and preserves messa
964996 assert . equal ( statements . length , 2 ) ;
965997 for ( const [ query , params ] of statements ) {
966998 const plan = yield * sql . unsafe < { detail : string } > ( `EXPLAIN QUERY PLAN ${ query } ` , params ) ;
967- assert . isTrue ( plan [ 0 ] ?. detail . includes ( "SEARCH run " ) , JSON . stringify ( plan ) ) ;
999+ assert . isTrue (
1000+ plan . some ( ( row ) => row . detail . includes ( "SEARCH message_run " ) ) ,
1001+ JSON . stringify ( plan ) ,
1002+ ) ;
9681003 assert . isTrue (
9691004 plan . some ( ( row ) => row . detail . includes ( "messages_run_idx" ) ) ,
9701005 JSON . stringify ( plan ) ,
9711006 ) ;
1007+ assert . isFalse (
1008+ plan . some ( ( row ) => / \b S C A N m e s s a g e \b / . test ( row . detail ) ) ,
1009+ JSON . stringify ( plan ) ,
1010+ ) ;
9721011 assert . isFalse (
9731012 plan . some ( ( row ) => row . detail . includes ( "messages_thread_created_idx" ) ) ,
9741013 JSON . stringify ( plan ) ,
0 commit comments