@@ -43,6 +43,7 @@ import * as Queue from "effect/Queue";
4343import * as Schema from "effect/Schema" ;
4444import * as Scope from "effect/Scope" ;
4545import * as Stream from "effect/Stream" ;
46+ import * as TestClock from "effect/testing/TestClock" ;
4647import { Tool } from "effect/ai" ;
4748import { formatClaudeResumeCompactionQuestion } from "@t3tools/shared/claudeCompaction" ;
4849import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
@@ -1990,6 +1991,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
19901991 readonly subtype ?: string ;
19911992 readonly isError ?: boolean ;
19921993 readonly errors ?: ReadonlyArray < string > ;
1994+ readonly userMessageUuid ?: string ;
19931995 readonly apiErrorStatus ?: number ;
19941996 // null omits the field, as the CLI does on a zero-turn result.
19951997 readonly terminalReason ?: SDKResultMessage [ "terminal_reason" ] | null ;
@@ -2016,6 +2018,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20162018 session_id : WAKE_NATIVE_SESSION ,
20172019 ...( input . origin === undefined ? { } : { origin : input . origin } ) ,
20182020 ...( input . errors === undefined ? { } : { errors : input . errors } ) ,
2021+ ...( input . userMessageUuid === undefined ? { } : { user_message_uuid : input . userMessageUuid } ) ,
20192022 ...( input . apiErrorStatus === undefined ? { } : { api_error_status : input . apiErrorStatus } ) ,
20202023 ...( input . terminalReason === null
20212024 ? { }
@@ -3778,6 +3781,129 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37783781 ) ,
37793782 ) ;
37803783
3784+ it . effect ( "lets the native SDK acknowledge Stop before closing its own query" , ( ) =>
3785+ Effect . scoped (
3786+ Effect . gen ( function * ( ) {
3787+ let closes = 0 ;
3788+ let acknowledge = Effect . void ;
3789+ const harness = yield * makeWakeHarnessWithOptions ( {
3790+ interrupt : Effect . suspend ( ( ) => {
3791+ assert . equal ( closes , 0 ) ;
3792+ return acknowledge ;
3793+ } ) ,
3794+ close : ( messages ) =>
3795+ Effect . sync ( ( ) => {
3796+ closes ++ ;
3797+ } ) . pipe ( Effect . andThen ( Queue . shutdown ( messages ) ) ) ,
3798+ } ) ;
3799+ yield * harness . runtime . startTurn (
3800+ makeClaudeTestTurnInput ( {
3801+ threadId : harness . threadId ,
3802+ providerThread : harness . providerThread ,
3803+ now : yield * DateTime . now ,
3804+ attemptId : RunAttemptId . make ( "graceful-stop" ) ,
3805+ text : "Work until stopped" ,
3806+ attachments : [ ] ,
3807+ } ) ,
3808+ ) ;
3809+ const userMessageUuid = harness . offeredMessages . at ( - 1 ) ?. uuid ;
3810+ if ( userMessageUuid === undefined ) return yield * Effect . die ( "Missing native user message" ) ;
3811+ acknowledge = harness . offerAndWait (
3812+ makeResultFrame ( {
3813+ uuid : "00000000-0000-4000-8000-000000000620" ,
3814+ result : "" ,
3815+ subtype : "error_during_execution" ,
3816+ errors : [ "Error: Request was aborted." ] ,
3817+ terminalReason : "aborted_streaming" ,
3818+ userMessageUuid,
3819+ } ) ,
3820+ ) ;
3821+ yield * awaitUntil (
3822+ ( ) =>
3823+ harness . events . some (
3824+ ( event ) =>
3825+ event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
3826+ ) ,
3827+ "running native turn" ,
3828+ ) ;
3829+ const active = harness . events . find (
3830+ ( event ) =>
3831+ event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
3832+ ) ;
3833+ if ( active ?. type !== "provider_turn.updated" )
3834+ return yield * Effect . die ( "Missing running native turn" ) ;
3835+ yield * harness . runtime . interruptTurn ( {
3836+ providerThread : harness . providerThread ,
3837+ providerTurnId : active . providerTurn . id ,
3838+ requestRuntimeRestart : true ,
3839+ } ) ;
3840+ assert . equal ( ( yield * Queue . take ( harness . terminalReceipts ) ) . status , "interrupted" ) ;
3841+ assert . equal ( closes , 1 ) ;
3842+ assert . lengthOf ( harness . terminalEvents ( ) , 1 ) ;
3843+ } ) . pipe ( Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
3844+ ) ,
3845+ ) ;
3846+
3847+ it . effect ( "bounds an unacknowledged native Stop and settles its turn once" , ( ) =>
3848+ Effect . scoped (
3849+ Effect . gen ( function * ( ) {
3850+ let closes = 0 ;
3851+ const interruptStarted = yield * Deferred . make < void > ( ) ;
3852+ const harness = yield * makeWakeHarnessWithOptions ( {
3853+ interrupt : Deferred . succeed ( interruptStarted , undefined ) . pipe (
3854+ Effect . andThen ( Effect . never ) ,
3855+ ) ,
3856+ close : ( messages ) =>
3857+ Effect . sync ( ( ) => {
3858+ closes ++ ;
3859+ } ) . pipe ( Effect . andThen ( Queue . shutdown ( messages ) ) ) ,
3860+ } ) ;
3861+ yield * harness . runtime . startTurn (
3862+ makeClaudeTestTurnInput ( {
3863+ threadId : harness . threadId ,
3864+ providerThread : harness . providerThread ,
3865+ now : yield * DateTime . now ,
3866+ attemptId : RunAttemptId . make ( "unacknowledged-stop" ) ,
3867+ text : "Work until stopped" ,
3868+ attachments : [ ] ,
3869+ } ) ,
3870+ ) ;
3871+ yield * awaitUntil (
3872+ ( ) =>
3873+ harness . events . some (
3874+ ( event ) =>
3875+ event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
3876+ ) ,
3877+ "running native turn" ,
3878+ ) ;
3879+ const active = harness . events . find (
3880+ ( event ) =>
3881+ event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
3882+ ) ;
3883+ if ( active ?. type !== "provider_turn.updated" )
3884+ return yield * Effect . die ( "Missing running native turn" ) ;
3885+ const stop = yield * harness . runtime
3886+ . interruptTurn ( {
3887+ providerThread : harness . providerThread ,
3888+ providerTurnId : active . providerTurn . id ,
3889+ requestRuntimeRestart : true ,
3890+ } )
3891+ . pipe ( Effect . forkChild ) ;
3892+ yield * Deferred . await ( interruptStarted ) ;
3893+ assert . equal ( closes , 0 ) ;
3894+ yield * TestClock . adjust ( "11 seconds" ) ;
3895+ assert . isDefined (
3896+ stop . pollUnsafe ( ) ,
3897+ "Stop must finish despite a missing SDK acknowledgement" ,
3898+ ) ;
3899+ yield * Fiber . join ( stop ) ;
3900+ assert . equal ( ( yield * Queue . take ( harness . terminalReceipts ) ) . status , "interrupted" ) ;
3901+ assert . equal ( closes , 1 ) ;
3902+ assert . lengthOf ( harness . terminalEvents ( ) , 1 ) ;
3903+ } ) . pipe ( Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
3904+ ) ,
3905+ ) ;
3906+
37813907 it . effect ( "stops background work after the turn settled" , ( ) =>
37823908 Effect . scoped (
37833909 Effect . gen ( function * ( ) {
0 commit comments