44 EventId ,
55 type ModelSelection ,
66 type OrchestrationEvent ,
7+ type OrchestrationThread ,
78 ProviderDriverKind ,
89 type ProjectId ,
910 type OrchestrationSession ,
@@ -16,6 +17,7 @@ import { isTemporaryWorktreeBranch, WORKTREE_BRANCH_PREFIX } from "@t3tools/shar
1617import * as Cache from "effect/Cache" ;
1718import * as Cause from "effect/Cause" ;
1819import * as Crypto from "effect/Crypto" ;
20+ import * as DateTime from "effect/DateTime" ;
1921import * as Duration from "effect/Duration" ;
2022import * as Effect from "effect/Effect" ;
2123import * as Equal from "effect/Equal" ;
@@ -49,6 +51,10 @@ import { GitWorkflowService } from "../../git/GitWorkflowService.ts";
4951const isProviderAdapterRequestError = Schema . is ( ProviderAdapterRequestError ) ;
5052const isProviderDriverKind = Schema . is ( ProviderDriverKind ) ;
5153
54+ // Server-side timestamps for reactor-appended activities. Client-supplied
55+ // command timestamps can predate the thread activity they must follow.
56+ const nowIso = Effect . map ( DateTime . now , DateTime . formatIso ) ;
57+
5258type ProviderIntentEvent = Extract <
5359 OrchestrationEvent ,
5460 {
@@ -275,6 +281,43 @@ function stalePendingRequestDetail(
275281 return `Stale pending ${ requestKind } request: ${ requestId } . Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.` ;
276282}
277283
284+ function pendingUserInputRequests (
285+ activities : OrchestrationThread [ "activities" ] ,
286+ ) : ReadonlyArray < { readonly requestId : string ; readonly turnId : TurnId | null } > {
287+ const openRequests = new Map < string , TurnId | null > ( ) ;
288+ const ordered = [ ...activities ] . toSorted (
289+ ( left , right ) =>
290+ left . createdAt . localeCompare ( right . createdAt ) || left . id . localeCompare ( right . id ) ,
291+ ) ;
292+
293+ for ( const activity of ordered ) {
294+ const payload =
295+ typeof activity . payload === "object" && activity . payload !== null
296+ ? ( activity . payload as Record < string , unknown > )
297+ : null ;
298+ const requestId = typeof payload ?. requestId === "string" ? payload . requestId : null ;
299+ if ( requestId === null ) continue ;
300+
301+ if ( activity . kind === "user-input.requested" ) {
302+ openRequests . set ( requestId , activity . turnId ) ;
303+ } else if ( activity . kind === "user-input.resolved" ) {
304+ openRequests . delete ( requestId ) ;
305+ } else if ( activity . kind === "provider.user-input.respond.failed" ) {
306+ const detail = typeof payload ?. detail === "string" ? payload . detail . toLowerCase ( ) : "" ;
307+ if (
308+ detail . includes ( "stale pending user-input request" ) ||
309+ detail . includes ( "unknown pending user-input request" ) ||
310+ detail . includes ( "unknown pending user input request" ) ||
311+ detail . includes ( "unknown pending codex user input request" )
312+ ) {
313+ openRequests . delete ( requestId ) ;
314+ }
315+ }
316+ }
317+
318+ return [ ...openRequests ] . map ( ( [ requestId , turnId ] ) => ( { requestId, turnId } ) ) ;
319+ }
320+
278321function buildGeneratedWorktreeBranchName ( raw : string ) : string {
279322 const normalized = raw
280323 . trim ( )
@@ -366,6 +409,36 @@ const make = Effect.gen(function* () {
366409 ) ,
367410 ) ;
368411
412+ const appendCancelledUserInputActivity = Effect . fn (
413+ "ProviderCommandReactor.appendCancelledUserInputActivity" ,
414+ ) ( function * ( input : {
415+ readonly threadId : ThreadId ;
416+ readonly requestId : string ;
417+ readonly turnId : TurnId | null ;
418+ readonly createdAt : string ;
419+ } ) {
420+ const commandId = yield * serverCommandId ( "user-input-cancelled" ) ;
421+ const eventId = yield * serverEventId ( ) ;
422+ yield * orchestrationEngine . dispatch ( {
423+ type : "thread.activity.append" ,
424+ commandId,
425+ threadId : input . threadId ,
426+ activity : {
427+ id : eventId ,
428+ tone : "info" ,
429+ kind : "user-input.resolved" ,
430+ summary : "User input cancelled" ,
431+ payload : {
432+ requestId : input . requestId ,
433+ cancelled : true ,
434+ } ,
435+ turnId : input . turnId ,
436+ createdAt : input . createdAt ,
437+ } ,
438+ createdAt : input . createdAt ,
439+ } ) ;
440+ } ) ;
441+
369442 const formatFailureDetail = ( cause : Cause . Cause < unknown > ) : string => {
370443 const failReason = cause . reasons . find ( Cause . isFailReason ) ;
371444 const providerError = isProviderAdapterRequestError ( failReason ?. error )
@@ -1194,6 +1267,28 @@ const make = Effect.gen(function* () {
11941267
11951268 // Orchestration turn ids are not provider turn ids, so interrupt by session.
11961269 yield * providerService . interruptTurn ( { threadId : event . payload . threadId } ) ;
1270+ // Some providers discard their callbacks without emitting a matching
1271+ // resolution event. Close those requests once the interrupt succeeds,
1272+ // deriving from a fresh read: the provider can append real resolutions
1273+ // while handling the interrupt. Cancellations get server-side timestamps
1274+ // so they order after the request activity even when the interrupt
1275+ // command predates it.
1276+ const postInterruptThread = yield * resolveThread ( event . payload . threadId ) ;
1277+ if ( ! postInterruptThread ) {
1278+ return ;
1279+ }
1280+ yield * Effect . forEach (
1281+ pendingUserInputRequests ( postInterruptThread . activities ) ,
1282+ ( { requestId, turnId } ) =>
1283+ Effect . flatMap ( nowIso , ( cancelledAt ) =>
1284+ appendCancelledUserInputActivity ( {
1285+ threadId : event . payload . threadId ,
1286+ requestId,
1287+ turnId,
1288+ createdAt : cancelledAt ,
1289+ } ) ,
1290+ ) ,
1291+ ) ;
11971292 } ) ;
11981293
11991294 const processApprovalResponseRequested = Effect . fn ( "processApprovalResponseRequested" ) ( function * (
0 commit comments