@@ -56,6 +56,7 @@ import * as Option from "effect/Option";
5656import * as Duration from "effect/Duration" ;
5757import * as Effect from "effect/Effect" ;
5858import * as FileSystem from "effect/FileSystem" ;
59+ import * as Fiber from "effect/Fiber" ;
5960import * as Layer from "effect/Layer" ;
6061import * as Queue from "effect/Queue" ;
6162import * as Schema from "effect/Schema" ;
@@ -456,6 +457,8 @@ interface PendingPiPrompt {
456457 readonly method : "select" | "confirm" | "input" | "editor" ;
457458 readonly questionId : string ;
458459 readonly approvalKey : string ;
460+ readonly expiresAt : number | null ;
461+ expirationFiber : Fiber . Fiber < void > | null ;
459462 runtimeRequest : OrchestrationV2RuntimeRequest ;
460463 readonly node : OrchestrationV2ExecutionNode ;
461464 readonly turnItem : OrchestrationV2TurnItem ;
@@ -1270,6 +1273,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12701273
12711274 const cancelPrompt = ( pending : PendingPiPrompt , resolvedAt : DateTime . Utc ) =>
12721275 Effect . gen ( function * ( ) {
1276+ if ( pending . expirationFiber !== null ) yield * Fiber . interrupt ( pending . expirationFiber ) ;
12731277 yield * connection
12741278 . send ( {
12751279 type : "extension_ui_response" ,
@@ -1368,6 +1372,12 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
13681372 const state = threadState ;
13691373 const turn = state ?. activeTurn ?? null ;
13701374 const createdAt = yield * DateTime . now ;
1375+ const timeout = recordNumber ( event , "timeout" ) ;
1376+ // Pi treats zero as no timeout and negative durations as immediate expiry.
1377+ const expiresAt =
1378+ timeout === undefined || timeout === 0
1379+ ? null
1380+ : DateTime . toEpochMillis ( createdAt ) + Math . max ( 0 , timeout ) ;
13711381 const requestId = yield * idAllocator . allocate . runtimeRequest ( {
13721382 driver : PI_PROVIDER ,
13731383 ...( turn === null ? { } : { providerTurnId : turn . providerTurn . id } ) ,
@@ -1440,15 +1450,18 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
14401450 requestId,
14411451 questions : [ piQuestion ( nativeRequestId , method , title , event ) ] ,
14421452 } ;
1443- pendingPrompts . set ( String ( requestId ) , {
1453+ const pending : PendingPiPrompt = {
14441454 nativeRequestId,
14451455 method,
14461456 questionId : nativeRequestId ,
14471457 approvalKey,
1458+ expiresAt,
1459+ expirationFiber : null ,
14481460 runtimeRequest,
14491461 node,
14501462 turnItem,
1451- } ) ;
1463+ } ;
1464+ pendingPrompts . set ( String ( requestId ) , pending ) ;
14521465 yield * emit ( {
14531466 type : "runtime_request.updated" ,
14541467 driver : PI_PROVIDER ,
@@ -1457,6 +1470,22 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
14571470 } ) ;
14581471 yield * emit ( { type : "node.updated" , driver : PI_PROVIDER , node } ) ;
14591472 yield * emit ( { type : "turn_item.updated" , driver : PI_PROVIDER , turnItem } ) ;
1473+ if ( expiresAt !== null ) {
1474+ const now = yield * DateTime . now ;
1475+ pending . expirationFiber = yield * Effect . sleep (
1476+ Duration . millis ( Math . max ( 0 , expiresAt - DateTime . toEpochMillis ( now ) ) ) ,
1477+ ) . pipe (
1478+ Effect . andThen (
1479+ Queue . offer ( connection . events , {
1480+ type : "t3.ui_request_expired" ,
1481+ requestId : String ( requestId ) ,
1482+ pending,
1483+ } ) ,
1484+ ) ,
1485+ Effect . asVoid ,
1486+ Effect . forkIn ( scope ) ,
1487+ ) ;
1488+ }
14601489 } ) ;
14611490
14621491 const emitExtensionError = Effect . fnUntraced ( function * ( event : PiRpcRecord ) {
@@ -2132,6 +2161,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
21322161 }
21332162 return ;
21342163 }
2164+ case "t3.ui_request_expired" : {
2165+ const requestId = recordString ( event , "requestId" ) ;
2166+ if ( requestId === undefined ) return ;
2167+ const pending = pendingPrompts . get ( requestId ) ;
2168+ if ( pending === undefined || pending !== event [ "pending" ] ) return ;
2169+ pendingPrompts . delete ( requestId ) ;
2170+ yield * cancelPrompt ( pending , yield * DateTime . now ) ;
2171+ return ;
2172+ }
21352173 case "t3.flush_extension_errors" : {
21362174 // Startup extension failures are informational and do not block
21372175 // Pi, so attach them to the next real turn instead of creating a
@@ -2211,7 +2249,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
22112249 rollbackBarrier ,
22122250 protocolError ( "Pi transport closed during rollback" ) ,
22132251 ) ;
2214- const hadPendingWake = pendingWake !== null ;
22152252 pendingWake = null ;
22162253 const interrupted = state ?. activeTurn ?. interrupted === true ;
22172254 if ( state ?. activeTurn != null ) {
@@ -2223,7 +2260,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
22232260 class : "transport_error" ,
22242261 } ) ;
22252262 yield * finalizeTurn ( state , false ) ;
2226- } else if ( hadPendingWake ) {
2263+ } else {
22272264 yield * cancelPendingPrompts ( yield * DateTime . now ) ;
22282265 }
22292266 if ( unsolicitedActivityDetected ) {
@@ -2844,6 +2881,17 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
28442881 `No pending Pi extension request ${ requestInput . requestId } ` ,
28452882 ) ;
28462883 }
2884+ const answeredAt = yield * DateTime . now ;
2885+ if (
2886+ pending . expiresAt !== null &&
2887+ DateTime . toEpochMillis ( answeredAt ) >= pending . expiresAt
2888+ ) {
2889+ pendingPrompts . delete ( String ( requestInput . requestId ) ) ;
2890+ yield * cancelPrompt ( pending , answeredAt ) ;
2891+ return yield * protocolError (
2892+ `Pi extension request ${ requestInput . requestId } has expired` ,
2893+ ) ;
2894+ }
28472895 const response = piUiResponse ( pending , requestInput . decision , requestInput . answers ) ;
28482896 yield * connection . send ( {
28492897 type : "extension_ui_response" ,
@@ -2853,6 +2901,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
28532901 // Dropped only once Pi has the answer, so a failed send leaves the
28542902 // request retryable and still cancellable during teardown.
28552903 pendingPrompts . delete ( String ( requestInput . requestId ) ) ;
2904+ if ( pending . expirationFiber !== null ) yield * Fiber . interrupt ( pending . expirationFiber ) ;
28562905 if ( pending . method === "confirm" && requestInput . decision === "acceptForSession" ) {
28572906 sessionApprovals . add ( pending . approvalKey ) ;
28582907 }
0 commit comments