@@ -1335,6 +1335,182 @@ layer("ProviderEventIngestorV2", (it) => {
13351335 ) ,
13361336 ) ;
13371337
1338+ it . effect . each ( [ "interrupted" , "cancelled" ] as const ) (
1339+ "preserves an approval resolved after terminal normalization reads it pending (%s)" ,
1340+ ( status ) =>
1341+ Effect . scoped (
1342+ Effect . gen ( function * ( ) {
1343+ const eventSink = yield * EventSink . EventSinkV2 ;
1344+ const eventStore = yield * EventStore . EventStoreV2 ;
1345+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
1346+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
1347+ const now = yield * DateTime . now ;
1348+ const threadEvent = yield * threadCreatedEvent ( now ) ;
1349+ const threadId = threadEvent . threadId ;
1350+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
1351+ providerInstanceId : modelSelection . instanceId ,
1352+ threadId,
1353+ } ) ;
1354+ const providerThreadId = idAllocator . derive . providerThread ( {
1355+ driver : CODEX_DRIVER ,
1356+ nativeThreadId : `${ threadId } :approval-race` ,
1357+ } ) ;
1358+ const providerTurnId = idAllocator . derive . providerTurn ( {
1359+ driver : CODEX_DRIVER ,
1360+ nativeTurnId : `${ threadId } :approval-race` ,
1361+ } ) ;
1362+ const nodeId = NodeId . make ( `${ threadId } :approval` ) ;
1363+ const request : OrchestrationV2RuntimeRequest = {
1364+ id : RuntimeRequestId . make ( `${ threadId } :approval` ) ,
1365+ nodeId,
1366+ providerTurnId,
1367+ nativeRequestRef : null ,
1368+ kind : "command" ,
1369+ status : "pending" ,
1370+ responseCapability : { type : "live" , providerSessionId } ,
1371+ createdAt : now ,
1372+ resolvedAt : null ,
1373+ } ;
1374+ const node : OrchestrationV2ExecutionNode = {
1375+ id : nodeId ,
1376+ threadId,
1377+ runId : null ,
1378+ parentNodeId : null ,
1379+ rootNodeId : nodeId ,
1380+ kind : "approval_request" ,
1381+ status : "waiting" ,
1382+ countsForRun : false ,
1383+ providerThreadId,
1384+ providerTurnId,
1385+ nativeItemRef : null ,
1386+ runtimeRequestId : request . id ,
1387+ checkpointScopeId : null ,
1388+ startedAt : now ,
1389+ completedAt : null ,
1390+ } ;
1391+ const item : OrchestrationV2TurnItem = {
1392+ id : TurnItemId . make ( `${ threadId } :approval` ) ,
1393+ threadId,
1394+ runId : null ,
1395+ nodeId,
1396+ providerThreadId,
1397+ providerTurnId,
1398+ nativeItemRef : null ,
1399+ parentItemId : null ,
1400+ ordinal : 1 ,
1401+ status : "waiting" ,
1402+ title : null ,
1403+ startedAt : now ,
1404+ completedAt : null ,
1405+ updatedAt : now ,
1406+ type : "approval_request" ,
1407+ requestId : request . id ,
1408+ requestKind : "command" ,
1409+ } ;
1410+ const seedEvents : Array < OrchestrationV2DomainEvent > = [ threadEvent ] ;
1411+ for ( const payload of [
1412+ { type : "runtime-request.updated" as const , payload : request } ,
1413+ { type : "node.updated" as const , payload : node } ,
1414+ { type : "turn-item.updated" as const , payload : item } ,
1415+ ] )
1416+ seedEvents . push ( {
1417+ id : yield * idAllocator . allocate . event ( { threadId } ) ,
1418+ threadId,
1419+ occurredAt : now ,
1420+ ...payload ,
1421+ } ) ;
1422+ yield * eventSink . write ( { events : seedEvents } ) ;
1423+ const normalized = yield * Deferred . make < void > ( ) ;
1424+ const releaseTerminalWrite = yield * Deferred . make < void > ( ) ;
1425+ const gatedSink = EventSink . EventSinkV2 . of ( {
1426+ ...eventSink ,
1427+ write : ( input ) =>
1428+ Deferred . succeed ( normalized , undefined ) . pipe (
1429+ Effect . andThen ( Deferred . await ( releaseTerminalWrite ) ) ,
1430+ Effect . andThen ( eventSink . write ( input ) ) ,
1431+ ) ,
1432+ } ) ;
1433+ const ingestor = yield * ProviderEventIngestor . ProviderEventIngestorV2 . pipe (
1434+ Effect . provide ( Layer . fresh ( ProviderEventIngestor . layer ) ) ,
1435+ Effect . provideService ( EventSink . EventSinkV2 , gatedSink ) ,
1436+ ) ;
1437+ const terminal = yield * ingestor
1438+ . ingestNormalized ( {
1439+ providerSessionId,
1440+ providerInstanceId : modelSelection . instanceId ,
1441+ threadId,
1442+ event : {
1443+ type : "provider_turn.updated" ,
1444+ driver : CODEX_DRIVER ,
1445+ providerTurn : {
1446+ id : providerTurnId ,
1447+ providerThreadId,
1448+ nodeId,
1449+ runAttemptId : null ,
1450+ nativeTurnRef : null ,
1451+ ordinal : 1 ,
1452+ status,
1453+ startedAt : now ,
1454+ completedAt : now ,
1455+ } ,
1456+ } ,
1457+ } )
1458+ . pipe ( Effect . forkScoped ) ;
1459+ yield * Deferred . await ( normalized ) ;
1460+ const responseEvents : Array < OrchestrationV2DomainEvent > = [ ] ;
1461+ for ( const payload of [
1462+ {
1463+ type : "runtime-request.updated" as const ,
1464+ payload : {
1465+ ...request ,
1466+ status : "resolved" as const ,
1467+ decision : "accept" as const ,
1468+ resolvedAt : now ,
1469+ } ,
1470+ } ,
1471+ {
1472+ type : "node.updated" as const ,
1473+ payload : { ...node , status : "completed" as const , completedAt : now } ,
1474+ } ,
1475+ {
1476+ type : "turn-item.updated" as const ,
1477+ payload : { ...item , status : "completed" as const , completedAt : now } ,
1478+ } ,
1479+ ] )
1480+ responseEvents . push ( {
1481+ id : yield * idAllocator . allocate . event ( { threadId } ) ,
1482+ threadId,
1483+ occurredAt : now ,
1484+ ...payload ,
1485+ } ) ;
1486+ yield * eventSink . write ( { events : responseEvents } ) ;
1487+ yield * Deferred . succeed ( releaseTerminalWrite , undefined ) ;
1488+ const committedTerminal = yield * Fiber . join ( terminal ) ;
1489+ const projection = yield * projections . getThreadProjection ( threadId ) ;
1490+ const approved = projection . runtimeRequests . find ( ( entry ) => entry . id === request . id ) ! ;
1491+ assert . equal ( approved . status , "resolved" ) ;
1492+ assert . equal ( approved . decision , "accept" ) ;
1493+ assert . equal ( projection . nodes . find ( ( entry ) => entry . id === nodeId ) ! . status , "completed" ) ;
1494+ assert . equal (
1495+ projection . turnItems . find ( ( entry ) => entry . id === item . id ) ! . status ,
1496+ "completed" ,
1497+ ) ;
1498+ assert . deepEqual (
1499+ committedTerminal . map ( ( stored ) => stored . event . type ) ,
1500+ [ "provider-turn.updated" ] ,
1501+ ) ;
1502+ const history = yield * eventStore . read ( { threadId } ) . pipe ( Stream . runCollect ) ;
1503+ assert . isFalse (
1504+ history . some (
1505+ ( stored ) =>
1506+ stored . event . type === "runtime-request.updated" &&
1507+ stored . event . payload . status === "cancelled" ,
1508+ ) ,
1509+ ) ;
1510+ } ) ,
1511+ ) ,
1512+ ) ;
1513+
13381514 it . effect ( "persists a failed provider terminal as one expected error item" , ( ) =>
13391515 Effect . gen ( function * ( ) {
13401516 const now = yield * DateTime . now ;
0 commit comments