@@ -36,7 +36,7 @@ import { deriveBlockedOnThreadWait } from "@t3tools/shared/orchestrationV2Pendin
3636import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime" ;
3737import * as DateTime from "effect/DateTime" ;
3838import * as Deferred from "effect/Deferred" ;
39- import type * as Duration from "effect/Duration" ;
39+ import * as Duration from "effect/Duration" ;
4040import * as Cause from "effect/Cause" ;
4141import * as Clock from "effect/Clock" ;
4242import * as Effect from "effect/Effect" ;
@@ -515,6 +515,90 @@ function makeMockRuntime(input: {
515515 } ) ;
516516}
517517
518+ const isProviderAdapterProtocolError = Schema . is ( ProviderAdapter . ProviderAdapterProtocolError ) ;
519+
520+ const makePromptInactivityHarness = Effect . fnUntraced ( function * ( input : {
521+ readonly environment : ( runtimeOrdinal : number ) => Readonly < Record < string , string > > ;
522+ readonly runtimeMode ?: "full-access" | "approval-required" ;
523+ } ) {
524+ const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
525+ const path = yield * Path . Path ;
526+ const natives : Array < AcpSessionRuntime . AcpSessionRuntime [ "Service" ] > = [ ] ;
527+ const instanceId = ProviderInstanceId . make ( "acp-prompt-inactivity" ) ;
528+ const adapter = yield * makeAcpAdapterV2 ( {
529+ instanceId,
530+ flavor : {
531+ driver : ACP_TEST_DRIVER ,
532+ capabilities : AcpProviderCapabilitiesV2 ,
533+ promptInactivityTimeout : "15 minutes" ,
534+ makeRuntime : makeMockRuntime ( {
535+ childProcessSpawner,
536+ mockAgentPath : yield * path . fromFileUrl (
537+ new URL ( "../../../scripts/acp-mock-agent.ts" , import . meta. url ) ,
538+ ) ,
539+ environment : input . environment ,
540+ wrapRuntime : ( runtime ) => {
541+ natives . push ( runtime ) ;
542+ return runtime ;
543+ } ,
544+ } ) ,
545+ } ,
546+ selfInvocation : yield * resolveSelfInvocation ( ) ,
547+ } ) ;
548+ const threadId = ThreadId . make ( "thread-acp-prompt-inactivity" ) ;
549+ const runtimePolicy = ProviderAdapter . ProviderAdapterV2RuntimePolicy . make ( {
550+ runtimeMode : input . runtimeMode ?? "full-access" ,
551+ interactionMode : "default" ,
552+ cwd : process . cwd ( ) ,
553+ } ) ;
554+ const modelSelection = { instanceId, model : "default" } as const ;
555+ const runtime = yield * adapter . openSession ( {
556+ threadId,
557+ providerSessionId : ProviderSessionId . make ( "provider-session-acp-prompt-inactivity" ) ,
558+ modelSelection,
559+ runtimePolicy,
560+ } ) ;
561+ const providerThread = yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
562+ const startTurn = ( ordinal : number ) =>
563+ DateTime . now . pipe (
564+ Effect . flatMap ( ( now ) =>
565+ runtime . startTurn (
566+ makeTurnInput ( { threadId, providerThread, instanceId, runtimePolicy, now, ordinal } ) ,
567+ ) ,
568+ ) ,
569+ ) ;
570+ return {
571+ runtime,
572+ natives,
573+ startTurn,
574+ /** A turn still running makes the next start fail, without waiting on time. */
575+ nextTurnRejected : ( ordinal : number ) =>
576+ startTurn ( ordinal ) . pipe (
577+ Effect . flip ,
578+ Effect . map (
579+ ( error ) =>
580+ isProviderAdapterProtocolError ( error . cause ) &&
581+ error . cause . reason === "previous_turn_active" ,
582+ ) ,
583+ Effect . catchCause ( ( ) => Effect . succeed ( false ) ) ,
584+ ) ,
585+ emitUpdate : ( notification : EffectAcpSchema . SessionNotification ) =>
586+ natives . at ( - 1 ) ! . request ( "_test/emit-update" , notification ) ,
587+ terminals : ( count : number ) =>
588+ runtime . events . pipe (
589+ Stream . filter ( ( event ) => event . type === "turn.terminal" ) ,
590+ Stream . take ( count ) ,
591+ Stream . runCollect ,
592+ Effect . map ( ( events ) =>
593+ Array . from ( events , ( event ) => {
594+ if ( event . type !== "turn.terminal" ) throw new Error ( "Expected a turn terminal" ) ;
595+ return event ;
596+ } ) ,
597+ ) ,
598+ ) ,
599+ } ;
600+ } ) ;
601+
518602function rawProtocolMethod ( event : EffectAcpProtocol . AcpProtocolLogEvent ) : string | undefined {
519603 if ( event . stage !== "raw" || typeof event . payload !== "string" ) return undefined ;
520604 for ( const line of event . payload . split ( "\n" ) ) {
@@ -3924,6 +4008,157 @@ describe("AcpAdapterV2", () => {
39244008 } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
39254009 ) ;
39264010
4011+ it . effect ( "fails a prompt its agent leaves silent past the inactivity timeout" , ( ) =>
4012+ Effect . gen ( function * ( ) {
4013+ const harness = yield * makePromptInactivityHarness ( {
4014+ environment : ( runtimeOrdinal ) =>
4015+ runtimeOrdinal === 1 ? { T3_ACP_HANG_PROMPT_FOREVER : "1" } : { } ,
4016+ } ) ;
4017+ const thought = ( sessionId : string ) => ( {
4018+ sessionId,
4019+ update : {
4020+ sessionUpdate : "agent_thought_chunk" as const ,
4021+ content : { type : "text" as const , text : "Still working." } ,
4022+ } ,
4023+ } ) ;
4024+ yield * harness . startTurn ( 1 ) ;
4025+ yield * TestClock . adjust ( "10 minutes" ) ;
4026+ yield * harness . emitUpdate ( thought ( "mock-session-1" ) ) ;
4027+ yield * TestClock . adjust ( "5 minutes" ) ;
4028+ // Neither another session's progress nor this session's metadata extends this turn.
4029+ yield * harness . emitUpdate ( thought ( "foreign-session" ) ) ;
4030+ yield * harness . emitUpdate ( {
4031+ sessionId : "mock-session-1" ,
4032+ update : { sessionUpdate : "available_commands_update" , availableCommands : [ ] } ,
4033+ } ) ;
4034+ yield * TestClock . adjust ( Duration . millis ( 10 * 60_000 - 1 ) ) ;
4035+ assert . isTrue ( yield * harness . nextTurnRejected ( 2 ) ) ;
4036+
4037+ yield * TestClock . adjust ( "1 millis" ) ;
4038+ yield * harness . startTurn ( 2 ) ;
4039+ const [ silent , next ] = yield * harness . terminals ( 2 ) ;
4040+ assert . equal ( silent ?. status , "failed" ) ;
4041+ assert . include ( silent ?. failure ?. message , "15 minutes" ) ;
4042+ assert . equal ( next ?. status , "completed" ) ;
4043+ assert . lengthOf ( harness . natives , 2 ) ;
4044+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
4045+ ) ;
4046+
4047+ it . effect (
4048+ "holds the inactivity timeout while a tool runs and restarts it when the tool ends" ,
4049+ ( ) =>
4050+ Effect . gen ( function * ( ) {
4051+ const harness = yield * makePromptInactivityHarness ( {
4052+ environment : ( runtimeOrdinal ) =>
4053+ runtimeOrdinal === 1 ? { T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG : "execute" } : { } ,
4054+ } ) ;
4055+ yield * harness . startTurn ( 1 ) ;
4056+ yield * harness . runtime . events . pipe (
4057+ Stream . filter (
4058+ ( event ) =>
4059+ event . type === "turn_item.updated" &&
4060+ event . turnItem . nativeItemRef ?. nativeId === "tracked-tool-call" ,
4061+ ) ,
4062+ Stream . runHead ,
4063+ ) ;
4064+ yield * TestClock . adjust ( "1 hour" ) ;
4065+ assert . isTrue ( yield * harness . nextTurnRejected ( 2 ) ) ;
4066+
4067+ yield * harness . emitUpdate ( {
4068+ sessionId : "mock-session-1" ,
4069+ update : {
4070+ sessionUpdate : "tool_call_update" ,
4071+ toolCallId : "tracked-tool-call" ,
4072+ status : "completed" ,
4073+ } ,
4074+ } ) ;
4075+ yield * TestClock . adjust ( Duration . millis ( 15 * 60_000 - 1 ) ) ;
4076+ assert . isTrue ( yield * harness . nextTurnRejected ( 2 ) ) ;
4077+ yield * TestClock . adjust ( "1 millis" ) ;
4078+ yield * harness . startTurn ( 2 ) ;
4079+ const [ silent , next ] = yield * harness . terminals ( 2 ) ;
4080+ assert . equal ( silent ?. status , "failed" ) ;
4081+ assert . equal ( next ?. status , "completed" ) ;
4082+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
4083+ ) ;
4084+
4085+ it . effect ( "holds the inactivity timeout while the agent waits on an approval" , ( ) =>
4086+ Effect . gen ( function * ( ) {
4087+ const harness = yield * makePromptInactivityHarness ( {
4088+ environment : ( ) => ( { T3_ACP_EMIT_TOOL_CALLS : "1" } ) ,
4089+ runtimeMode : "approval-required" ,
4090+ } ) ;
4091+ yield * harness . startTurn ( 1 ) ;
4092+ const pending = Option . getOrThrow (
4093+ yield * harness . runtime . events . pipe (
4094+ Stream . filter (
4095+ ( event ) =>
4096+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
4097+ ) ,
4098+ Stream . runHead ,
4099+ ) ,
4100+ ) ;
4101+ if ( pending . type !== "runtime_request.updated" ) {
4102+ return yield * Effect . die ( "Expected a pending approval" ) ;
4103+ }
4104+ // Only the request is left open, so it alone holds the deadline.
4105+ yield * harness . emitUpdate ( {
4106+ sessionId : "mock-session-1" ,
4107+ update : {
4108+ sessionUpdate : "tool_call_update" ,
4109+ toolCallId : "tool-call-1" ,
4110+ status : "completed" ,
4111+ } ,
4112+ } ) ;
4113+ yield * TestClock . adjust ( "1 hour" ) ;
4114+ assert . isTrue ( yield * harness . nextTurnRejected ( 2 ) ) ;
4115+
4116+ yield * harness . runtime . respondToRuntimeRequest ( {
4117+ requestId : pending . runtimeRequest . id ,
4118+ decision : "accept" ,
4119+ } ) ;
4120+ const [ approved ] = yield * harness . terminals ( 1 ) ;
4121+ assert . equal ( approved ?. status , "completed" ) ;
4122+ assert . lengthOf ( harness . natives , 1 ) ;
4123+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
4124+ ) ;
4125+
4126+ it . effect ( "fails a running prompt once when its agent process exits" , ( ) =>
4127+ Effect . gen ( function * ( ) {
4128+ const harness = yield * makePromptInactivityHarness ( {
4129+ environment : ( runtimeOrdinal ) =>
4130+ runtimeOrdinal === 1
4131+ ? { T3_ACP_EMIT_TOOL_CALLS : "1" }
4132+ : runtimeOrdinal === 2
4133+ ? { T3_ACP_HANG_PROMPT_FOREVER : "1" }
4134+ : { } ,
4135+ runtimeMode : "approval-required" ,
4136+ } ) ;
4137+ yield * harness . startTurn ( 1 ) ;
4138+ // The dying agent leaves an approval unanswered.
4139+ yield * harness . runtime . events . pipe (
4140+ Stream . filter (
4141+ ( event ) =>
4142+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
4143+ ) ,
4144+ Stream . runHead ,
4145+ ) ;
4146+ yield * harness . natives [ 0 ] ! . notify ( "_test/exit" , { } ) ;
4147+ const [ exited ] = yield * harness . terminals ( 1 ) ;
4148+ assert . equal ( exited ?. status , "failed" ) ;
4149+
4150+ // The replacement's silent prompt still times out.
4151+ yield * harness . startTurn ( 2 ) ;
4152+ yield * TestClock . adjust ( "15 minutes" ) ;
4153+ yield * harness . startTurn ( 3 ) ;
4154+ const [ silent , next ] = yield * harness . terminals ( 2 ) ;
4155+ assert . equal ( silent ?. status , "failed" ) ;
4156+ assert . notEqual ( silent ?. providerTurnId , exited ?. providerTurnId ) ;
4157+ assert . equal ( next ?. status , "completed" ) ;
4158+ assert . lengthOf ( harness . natives , 3 ) ;
4159+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
4160+ ) ;
4161+
39274162 it . live ( "does not poll the process ledger while a cgroup lease owns teardown" , ( ) =>
39284163 Effect . gen ( function * ( ) {
39294164 if ( ( yield * HostProcess . Platform ) !== "linux" ) return ;
0 commit comments