@@ -54,6 +54,7 @@ import * as Option from "effect/Option";
5454import * as Duration from "effect/Duration" ;
5555import * as Effect from "effect/Effect" ;
5656import * as FileSystem from "effect/FileSystem" ;
57+ import * as Fiber from "effect/Fiber" ;
5758import * as Layer from "effect/Layer" ;
5859import * as Queue from "effect/Queue" ;
5960import * as Schema from "effect/Schema" ;
@@ -354,6 +355,8 @@ interface PendingPiPrompt {
354355 readonly method : "select" | "confirm" | "input" | "editor" ;
355356 readonly questionId : string ;
356357 readonly approvalKey : string ;
358+ readonly expiresAt : number | null ;
359+ expirationFiber : Fiber . Fiber < void > | null ;
357360 runtimeRequest : OrchestrationV2RuntimeRequest ;
358361 readonly node : OrchestrationV2ExecutionNode ;
359362 readonly turnItem : OrchestrationV2TurnItem ;
@@ -1165,6 +1168,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
11651168
11661169 const cancelPrompt = ( pending : PendingPiPrompt , resolvedAt : DateTime . Utc ) =>
11671170 Effect . gen ( function * ( ) {
1171+ if ( pending . expirationFiber !== null ) yield * Fiber . interrupt ( pending . expirationFiber ) ;
11681172 yield * connection
11691173 . send ( {
11701174 type : "extension_ui_response" ,
@@ -1263,6 +1267,12 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12631267 const state = threadState ;
12641268 const turn = state ?. activeTurn ?? null ;
12651269 const createdAt = yield * DateTime . now ;
1270+ const timeout = recordNumber ( event , "timeout" ) ;
1271+ // Pi treats zero as no timeout and negative durations as immediate expiry.
1272+ const expiresAt =
1273+ timeout === undefined || timeout === 0
1274+ ? null
1275+ : DateTime . toEpochMillis ( createdAt ) + Math . max ( 0 , timeout ) ;
12661276 const requestId = yield * idAllocator . allocate . runtimeRequest ( {
12671277 driver : PI_PROVIDER ,
12681278 ...( turn === null ? { } : { providerTurnId : turn . providerTurn . id } ) ,
@@ -1335,15 +1345,18 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
13351345 requestId,
13361346 questions : [ piQuestion ( nativeRequestId , method , title , event ) ] ,
13371347 } ;
1338- pendingPrompts . set ( String ( requestId ) , {
1348+ const pending : PendingPiPrompt = {
13391349 nativeRequestId,
13401350 method,
13411351 questionId : nativeRequestId ,
13421352 approvalKey,
1353+ expiresAt,
1354+ expirationFiber : null ,
13431355 runtimeRequest,
13441356 node,
13451357 turnItem,
1346- } ) ;
1358+ } ;
1359+ pendingPrompts . set ( String ( requestId ) , pending ) ;
13471360 yield * emit ( {
13481361 type : "runtime_request.updated" ,
13491362 driver : PI_PROVIDER ,
@@ -1352,6 +1365,22 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
13521365 } ) ;
13531366 yield * emit ( { type : "node.updated" , driver : PI_PROVIDER , node } ) ;
13541367 yield * emit ( { type : "turn_item.updated" , driver : PI_PROVIDER , turnItem } ) ;
1368+ if ( expiresAt !== null ) {
1369+ const now = yield * DateTime . now ;
1370+ pending . expirationFiber = yield * Effect . sleep (
1371+ Duration . millis ( Math . max ( 0 , expiresAt - DateTime . toEpochMillis ( now ) ) ) ,
1372+ ) . pipe (
1373+ Effect . andThen (
1374+ Queue . offer ( connection . events , {
1375+ type : "t3.ui_request_expired" ,
1376+ requestId : String ( requestId ) ,
1377+ pending,
1378+ } ) ,
1379+ ) ,
1380+ Effect . asVoid ,
1381+ Effect . forkIn ( scope ) ,
1382+ ) ;
1383+ }
13551384 } ) ;
13561385
13571386 const emitExtensionError = Effect . fnUntraced ( function * ( event : PiRpcRecord ) {
@@ -2028,6 +2057,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
20282057 }
20292058 return ;
20302059 }
2060+ case "t3.ui_request_expired" : {
2061+ const requestId = recordString ( event , "requestId" ) ;
2062+ if ( requestId === undefined ) return ;
2063+ const pending = pendingPrompts . get ( requestId ) ;
2064+ if ( pending === undefined || pending !== event [ "pending" ] ) return ;
2065+ pendingPrompts . delete ( requestId ) ;
2066+ yield * cancelPrompt ( pending , yield * DateTime . now ) ;
2067+ return ;
2068+ }
20312069 case "t3.flush_extension_errors" : {
20322070 // Startup extension failures are informational and do not block
20332071 // Pi, so attach them to the next real turn instead of creating a
@@ -2107,7 +2145,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
21072145 rollbackBarrier ,
21082146 protocolError ( "Pi transport closed during rollback" ) ,
21092147 ) ;
2110- const hadPendingWake = pendingWake !== null ;
21112148 pendingWake = null ;
21122149 const interrupted = state ?. activeTurn ?. interrupted === true ;
21132150 if ( state ?. activeTurn != null ) {
@@ -2119,7 +2156,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
21192156 class : "transport_error" ,
21202157 } ) ;
21212158 yield * finalizeTurn ( state , false ) ;
2122- } else if ( hadPendingWake ) {
2159+ } else {
21232160 yield * cancelPendingPrompts ( yield * DateTime . now ) ;
21242161 }
21252162 if ( unsolicitedActivityDetected ) {
@@ -2738,6 +2775,17 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
27382775 `No pending Pi extension request ${ requestInput . requestId } ` ,
27392776 ) ;
27402777 }
2778+ const answeredAt = yield * DateTime . now ;
2779+ if (
2780+ pending . expiresAt !== null &&
2781+ DateTime . toEpochMillis ( answeredAt ) >= pending . expiresAt
2782+ ) {
2783+ pendingPrompts . delete ( String ( requestInput . requestId ) ) ;
2784+ yield * cancelPrompt ( pending , answeredAt ) ;
2785+ return yield * protocolError (
2786+ `Pi extension request ${ requestInput . requestId } has expired` ,
2787+ ) ;
2788+ }
27412789 const response = piUiResponse ( pending , requestInput . decision , requestInput . answers ) ;
27422790 yield * connection . send ( {
27432791 type : "extension_ui_response" ,
@@ -2747,6 +2795,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
27472795 // Dropped only once Pi has the answer, so a failed send leaves the
27482796 // request retryable and still cancellable during teardown.
27492797 pendingPrompts . delete ( String ( requestInput . requestId ) ) ;
2798+ if ( pending . expirationFiber !== null ) yield * Fiber . interrupt ( pending . expirationFiber ) ;
27502799 if ( pending . method === "confirm" && requestInput . decision === "acceptForSession" ) {
27512800 sessionApprovals . add ( pending . approvalKey ) ;
27522801 }
0 commit comments