@@ -1490,6 +1490,226 @@ it.effect("SQL list older settled threads below a cursor", () =>
14901490 } ) . pipe ( Effect . provide ( layerTest ) ) ,
14911491) ;
14921492
1493+ // regression: #25806 pending request cards outlive the history window that drops their turn
1494+ it . effect ( "SQL snapshot windows keep cards for requests still pending" , ( ) =>
1495+ Effect . gen ( function * ( ) {
1496+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
1497+ const now = yield * DateTime . now ;
1498+ const threadId = ThreadId . make ( "thread:pending-request-window" ) ;
1499+ yield * store . apply ( {
1500+ id : EventId . make ( "event:pending-request-window:thread" ) ,
1501+ type : "thread.created" ,
1502+ threadId,
1503+ occurredAt : now ,
1504+ payload : {
1505+ createdBy : "user" ,
1506+ creationSource : "web" ,
1507+ id : threadId ,
1508+ projectId : ProjectId . make ( "project:pending-request-window" ) ,
1509+ title : "Pending request window" ,
1510+ providerInstanceId,
1511+ modelSelection,
1512+ runtimeMode : "full-access" ,
1513+ interactionMode : "default" ,
1514+ branch : null ,
1515+ worktreePath : null ,
1516+ activeProviderThreadId : null ,
1517+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
1518+ forkedFrom : null ,
1519+ createdAt : now ,
1520+ updatedAt : now ,
1521+ archivedAt : null ,
1522+ settledOverride : null ,
1523+ settledAt : null ,
1524+ lastVisitedAt : null ,
1525+ deletedAt : null ,
1526+ } ,
1527+ } ) ;
1528+ let ordinal = 0 ;
1529+ const itemBase = ( runId : RunId , nodeId : NodeId , id : string ) => ( {
1530+ id : TurnItemId . make ( `item:pending-request-window:${ id } ` ) ,
1531+ threadId,
1532+ runId,
1533+ nodeId,
1534+ providerThreadId : null ,
1535+ providerTurnId : null ,
1536+ nativeItemRef : null ,
1537+ parentItemId : null ,
1538+ ordinal : ++ ordinal ,
1539+ status : "completed" as const ,
1540+ title : null ,
1541+ startedAt : now ,
1542+ completedAt : now ,
1543+ updatedAt : now ,
1544+ } ) ;
1545+ const request = ( id : string , nodeId : NodeId , kind : "user_input" | "command" ) => ( {
1546+ id : RuntimeRequestId . make ( `request:pending-request-window:${ id } ` ) ,
1547+ nodeId,
1548+ providerTurnId : null ,
1549+ nativeRequestRef : null ,
1550+ kind,
1551+ responseCapability : {
1552+ type : "live" as const ,
1553+ providerSessionId : ProviderSessionId . make ( "session:pending-request-window" ) ,
1554+ } ,
1555+ createdAt : now ,
1556+ } ) ;
1557+ const turnCount = THREAD_HISTORY_PAGE_POLICY . maxUserTurns + 4 ;
1558+ for ( let turn = 1 ; turn <= turnCount ; turn += 1 ) {
1559+ const runId = RunId . make ( `run:pending-request-window:${ turn } ` ) ;
1560+ const nodeId = NodeId . make ( `node:pending-request-window:${ turn } ` ) ;
1561+ yield * store . apply ( {
1562+ id : EventId . make ( `event:pending-request-window:run:${ turn } ` ) ,
1563+ type : "run.created" ,
1564+ threadId,
1565+ runId,
1566+ nodeId,
1567+ driver,
1568+ occurredAt : now ,
1569+ payload : {
1570+ id : runId ,
1571+ threadId,
1572+ ordinal : turn ,
1573+ providerInstanceId,
1574+ modelSelection,
1575+ providerThreadId : null ,
1576+ userMessageId : MessageId . make ( `message:pending-request-window:${ turn } ` ) ,
1577+ rootNodeId : nodeId ,
1578+ activeAttemptId : null ,
1579+ status : "completed" ,
1580+ requestedAt : now ,
1581+ startedAt : now ,
1582+ completedAt : now ,
1583+ checkpointId : null ,
1584+ contextHandoffId : null ,
1585+ } ,
1586+ } ) ;
1587+ const items : Array < OrchestrationV2TurnItem > = [
1588+ {
1589+ ...itemBase ( runId , nodeId , `message:${ turn } ` ) ,
1590+ type : "user_message" ,
1591+ createdBy : "user" ,
1592+ creationSource : "web" ,
1593+ messageId : MessageId . make ( `message:pending-request-window:${ turn } ` ) ,
1594+ inputIntent : "turn_start" ,
1595+ text : `Turn ${ turn } ` ,
1596+ attachments : [ ] ,
1597+ } ,
1598+ ] ;
1599+ if ( turn === 1 ) {
1600+ const question = {
1601+ id : "scope" ,
1602+ header : "Scope" ,
1603+ question : "Which scope?" ,
1604+ options : [ { label : "All" , description : "Everything" } ] ,
1605+ } ;
1606+ items . push (
1607+ {
1608+ ...itemBase ( runId , nodeId , "question:pending" ) ,
1609+ status : "running" ,
1610+ type : "user_input_request" ,
1611+ requestId : request ( "question:pending" , nodeId , "user_input" ) . id ,
1612+ questions : [ question ] ,
1613+ } ,
1614+ {
1615+ ...itemBase ( runId , nodeId , "approval:pending" ) ,
1616+ status : "running" ,
1617+ type : "approval_request" ,
1618+ requestId : request ( "approval:pending" , nodeId , "command" ) . id ,
1619+ requestKind : "command" ,
1620+ prompt : "rm -rf build" ,
1621+ } ,
1622+ {
1623+ ...itemBase ( runId , nodeId , "question:resolved" ) ,
1624+ type : "user_input_request" ,
1625+ requestId : request ( "question:resolved" , nodeId , "user_input" ) . id ,
1626+ questions : [ question ] ,
1627+ } ,
1628+ ) ;
1629+ for ( const [ id , kind , status ] of [
1630+ [ "question:pending" , "user_input" , "pending" ] ,
1631+ [ "approval:pending" , "command" , "pending" ] ,
1632+ [ "question:resolved" , "user_input" , "resolved" ] ,
1633+ ] as const ) {
1634+ yield * store . apply ( {
1635+ id : EventId . make ( `event:pending-request-window:request:${ id } ` ) ,
1636+ type : "runtime-request.updated" ,
1637+ threadId,
1638+ nodeId,
1639+ driver,
1640+ occurredAt : now ,
1641+ payload : {
1642+ ...request ( id , nodeId , kind ) ,
1643+ status,
1644+ resolvedAt : status === "pending" ? null : now ,
1645+ } ,
1646+ } ) ;
1647+ }
1648+ }
1649+ for ( const item of items ) {
1650+ yield * store . apply ( {
1651+ id : EventId . make ( `event:pending-request-window:${ item . id } ` ) ,
1652+ type : "turn-item.updated" ,
1653+ threadId,
1654+ runId,
1655+ nodeId,
1656+ driver,
1657+ occurredAt : now ,
1658+ payload : item ,
1659+ } ) ;
1660+ }
1661+ }
1662+
1663+ const pendingCardIds = [
1664+ TurnItemId . make ( "item:pending-request-window:question:pending" ) ,
1665+ TurnItemId . make ( "item:pending-request-window:approval:pending" ) ,
1666+ ] ;
1667+ const { statements, tracer } = traceSqlStatements ( ) ;
1668+ const snapshot = yield * store
1669+ . getThreadSnapshotWindow ( threadId , {
1670+ rowLimit : THREAD_HISTORY_PAGE_POLICY . maxItems + 2 ,
1671+ userTurnLimit : THREAD_HISTORY_PAGE_POLICY . maxUserTurns ,
1672+ } )
1673+ . pipe ( Effect . withTracer ( tracer ) ) ;
1674+ const sql = yield * SqlClient . SqlClient ;
1675+ const query = statements . find (
1676+ ( statement ) =>
1677+ statement . includes ( "FROM orchestration_v2_projection_runtime_requests AS request" ) &&
1678+ statement . includes ( "SELECT item.payload_json" ) ,
1679+ ) ;
1680+ assert . isDefined ( query ) ;
1681+ const plan = ( yield * sql . unsafe < { readonly detail : string } > (
1682+ `EXPLAIN QUERY PLAN ${ query } ` ,
1683+ ) ) . map ( ( row ) => row . detail ) ;
1684+ assert . include (
1685+ plan [ 0 ] ,
1686+ "SEARCH request USING INDEX orchestration_v2_projection_runtime_requests_thread_status_idx" ,
1687+ ) ;
1688+ assert . include (
1689+ plan [ 1 ] ,
1690+ "SEARCH item USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx" ,
1691+ ) ;
1692+ const bounded = buildBoundedThreadProjection ( {
1693+ projection : snapshot . projection ,
1694+ snapshotSequence : snapshot . snapshotSequence ,
1695+ } ) ;
1696+ assert . isTrue ( bounded . hasMoreHistory ) ;
1697+ assert . notInclude (
1698+ bounded . projection . turnItems . map ( ( item ) => item . id ) ,
1699+ TurnItemId . make ( "item:pending-request-window:question:resolved" ) ,
1700+ ) ;
1701+ for ( const projection of [ snapshot . projection , bounded . projection ] ) {
1702+ const visibleIds = projection . visibleTurnItems . map ( ( row ) => row . sourceItemId ) ;
1703+ assert . notInclude ( visibleIds , TurnItemId . make ( "item:pending-request-window:message:1" ) ) ;
1704+ assert . includeMembers (
1705+ projection . turnItems . map ( ( item ) => item . id ) ,
1706+ pendingCardIds ,
1707+ ) ;
1708+ for ( const id of pendingCardIds ) assert . notInclude ( visibleIds , id ) ;
1709+ }
1710+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1711+ ) ;
1712+
14931713it . effect ( "lists only active subagent IDs without loading shell data" , ( ) =>
14941714 Effect . gen ( function * ( ) {
14951715 const store = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments