@@ -12,6 +12,7 @@ import {
1212 type OrchestrationV2ProviderTurn ,
1313} from "@t3tools/contracts" ;
1414import * as DateTime from "effect/DateTime" ;
15+ import * as Cause from "effect/Cause" ;
1516import * as Config from "effect/Config" ;
1617import * as Logger from "effect/Logger" ;
1718import * as References from "effect/References" ;
@@ -33,6 +34,7 @@ import * as EventSink from "./EventSink.ts";
3334import * as Orchestrator from "./Orchestrator.ts" ;
3435import * as ProjectionStore from "./ProjectionStore.ts" ;
3536import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts" ;
37+ import * as ProviderSessionManager from "./ProviderSessionManager.ts" ;
3638import * as RunExecutionService from "./RunExecutionService.ts" ;
3739import * as ThreadManagementService from "./ThreadManagementService.ts" ;
3840import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts" ;
@@ -49,11 +51,17 @@ it.effect.each([
4951 "interrupt-timeout" ,
5052 "identity-timeout" ,
5153 "identity-timeout-force" ,
54+ "unsupported-compact" ,
55+ "prompt-refusal" ,
56+ "manager-release" ,
57+ "manager-release-interrupt" ,
58+ "provider-stopped" ,
59+ "stream-ended" ,
5260] as const ) ( "Stop crosses native start: %s" , ( scenario ) =>
5361 Effect . scoped (
5462 Effect . gen ( function * ( ) {
5563 const cwd = yield * checkpointWorkspace ( `start-stop-${ scenario } ` ) ;
56- const events = yield * Queue . unbounded < ProviderAdapter . ProviderAdapterV2Event > ( ) ;
64+ const events = yield * Queue . unbounded < ProviderAdapter . ProviderAdapterV2Event , Cause . Done > ( ) ;
5765 const entered = yield * Deferred . make < void > ( ) ;
5866 const accept = yield * Deferred . make < void > ( ) ;
5967 const nativeAccepted = yield * Deferred . make < void > ( ) ;
@@ -94,6 +102,18 @@ it.effect.each([
94102 turn ,
95103 ) =>
96104 Effect . gen ( function * ( ) {
105+ if ( scenario === "prompt-refusal" ) {
106+ return yield * new ProviderAdapter . ProviderAdapterTurnStartError ( {
107+ driver,
108+ threadId : turn . threadId ,
109+ providerThreadId : turn . providerThread . id ,
110+ runId : turn . runId ,
111+ cause : {
112+ _tag : "ClaudeAppendSystemPromptFileError" ,
113+ path : "/task-owned/missing-file" ,
114+ } ,
115+ } ) ;
116+ }
97117 yield * Deferred . succeed ( entered , undefined ) ;
98118 yield * Deferred . await ( accept ) ;
99119 assert . isUndefined ( active , "native turns must never overlap" ) ;
@@ -290,6 +310,15 @@ it.effect.each([
290310 const originalStart = execution . startRootRun ;
291311 let staleInput : RunExecutionService . RunExecutionServiceV2StartRootRunInput | undefined ;
292312 using _beforeOwnership = vi . spyOn ( execution , "startRootRun" ) . mockImplementation ( ( input ) => {
313+ if ( scenario === "unsupported-compact" ) {
314+ const session = { ...input . session } ;
315+ delete session . compactThread ;
316+ return originalStart ( {
317+ ...input ,
318+ message : { ...input . message , text : "/compact" } ,
319+ session,
320+ } ) ;
321+ }
293322 if ( scenario !== "before-ownership" ) return originalStart ( input ) ;
294323 staleInput = input ;
295324 return Deferred . succeed ( beforeOwnership , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ;
@@ -312,6 +341,26 @@ it.effect.each([
312341 } ) ;
313342 yield * send ( "first" ) ;
314343 const startup = yield * worker . drain ( 1 ) . pipe ( Effect . forkScoped ) ;
344+ if ( scenario === "unsupported-compact" || scenario === "prompt-refusal" ) {
345+ yield * Fiber . join ( startup ) ;
346+ const failed = yield * orchestrator . getThreadProjection ( threadId ) ;
347+ const run = failed . runs [ 0 ] ! ;
348+ assert . equal ( run . status , "failed" ) ;
349+ assert . equal ( failed . attempts [ 0 ] ?. status , "failed" ) ;
350+ assert . equal ( failed . nodes . find ( ( node ) => node . id === run . rootNodeId ) ?. status , "failed" ) ;
351+ assert . isTrue (
352+ Option . isNone (
353+ yield * execution . getStartOwnership ( {
354+ threadId,
355+ runId : run . id ,
356+ attemptId : run . activeAttemptId ! ,
357+ } ) ,
358+ ) ,
359+ ) ;
360+ assert . equal ( starts , 0 ) ;
361+ assert . deepEqual ( interrupts , [ ] ) ;
362+ return ;
363+ }
315364 if ( scenario === "before-ownership" ) {
316365 yield * Deferred . await ( beforeOwnership ) ;
317366 yield * orchestrator . dispatch ( {
@@ -333,6 +382,75 @@ it.effect.each([
333382 if ( scenario === "queued-promotion" ) yield * send ( "promoted" , true ) ;
334383 const initial = yield * orchestrator . getThreadProjection ( threadId ) ;
335384 const run = initial . runs . find ( ( run ) => run . status === "running" ) ! ;
385+ if (
386+ [
387+ "manager-release" ,
388+ "manager-release-interrupt" ,
389+ "provider-stopped" ,
390+ "stream-ended" ,
391+ ] . includes ( scenario )
392+ ) {
393+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
394+ const providerSessionId = initial . providerThreads . find (
395+ ( thread ) => thread . id === run . providerThreadId ,
396+ ) ! . providerSessionId ! ;
397+ const runtime = Option . getOrThrow ( yield * manager . get ( providerSessionId ) ) ;
398+ if ( scenario === "manager-release-interrupt" ) {
399+ const accepted = yield * orchestrator . streamDomainEvents . pipe (
400+ Stream . filter (
401+ ( event ) =>
402+ event . type === "provider-turn.updated" && event . payload . status === "running" ,
403+ ) ,
404+ Stream . take ( 1 ) ,
405+ Stream . runDrain ,
406+ Effect . forkScoped ,
407+ ) ;
408+ yield * Deferred . succeed ( accept , undefined ) ;
409+ yield * Fiber . join ( accepted ) ;
410+ }
411+ const waiter = yield * Deferred . succeed ( ownedWait , undefined ) . pipe (
412+ Effect . andThen (
413+ execution . interruptOwnedStart ( {
414+ threadId,
415+ runId : run . id ,
416+ attemptId : run . activeAttemptId ! ,
417+ } ) ,
418+ ) ,
419+ Effect . forkScoped ,
420+ ) ;
421+ yield * Deferred . await ( ownedWait ) ;
422+ if ( scenario === "manager-release-interrupt" ) yield * Deferred . await ( interruptCalled ) ;
423+ if ( scenario === "provider-stopped" || scenario === "stream-ended" ) {
424+ if ( scenario === "provider-stopped" )
425+ yield * Queue . offer ( events , {
426+ type : "provider_session.updated" ,
427+ driver,
428+ providerSession : { ...runtime . providerSession , status : "stopped" } ,
429+ } ) ;
430+ yield * Queue . end ( events ) ;
431+ } else {
432+ yield * manager . release ( { providerSessionId, reason : "manual_shutdown" } ) ;
433+ }
434+ yield * Fiber . join ( waiter ) ;
435+ yield * Fiber . join ( startup ) ;
436+ assert . isTrue (
437+ Option . isNone (
438+ yield * execution . getStartOwnership ( {
439+ threadId,
440+ runId : run . id ,
441+ attemptId : run . activeAttemptId ! ,
442+ } ) ,
443+ ) ,
444+ ) ;
445+ const failed = yield * orchestrator . getThreadProjection ( threadId ) ;
446+ assert . equal ( failed . runs [ 0 ] ?. status , "failed" ) ;
447+ assert . equal ( failed . attempts [ 0 ] ?. status , "failed" ) ;
448+ assert . isFalse ( failed . turnItems . some ( ( item ) => item . type === "run_interrupt_result" ) ) ;
449+ yield * Deferred . succeed ( accept , undefined ) ;
450+ yield * Deferred . succeed ( releaseStart , undefined ) ;
451+ assert . equal ( starts , scenario === "manager-release-interrupt" ? 1 : 0 ) ;
452+ return ;
453+ }
336454 const original = store . getThreadRecords ;
337455 let armed = true ;
338456 using _snapshot = vi . spyOn ( store , "getThreadRecords" ) . mockImplementation ( ( ...args ) =>
@@ -448,6 +566,15 @@ it.effect.each([
448566 assert . deepEqual ( interrupts , [ ] ) ;
449567 const outcome = yield * threads . forceStopThread ( { threadId, runId : run . id } ) ;
450568 assert . equal ( outcome . outcome , "released" ) ;
569+ assert . isTrue (
570+ Option . isNone (
571+ yield * execution . getStartOwnership ( {
572+ threadId,
573+ runId : run . id ,
574+ attemptId : run . activeAttemptId ! ,
575+ } ) ,
576+ ) ,
577+ ) ;
451578 const forced = yield * orchestrator . getThreadProjection ( threadId ) ;
452579 assert . equal ( forced . runs [ 0 ] ?. status , "interrupted" ) ;
453580 assert . isTrue (
0 commit comments