@@ -2012,6 +2012,226 @@ it.effect("SQL list older settled threads below a cursor", () =>
20122012 } ) . pipe ( Effect . provide ( layerTest ) ) ,
20132013) ;
20142014
2015+ // regression: #25806 pending request cards outlive the history window that drops their turn
2016+ it . effect ( "SQL snapshot windows keep cards for requests still pending" , ( ) =>
2017+ Effect . gen ( function * ( ) {
2018+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
2019+ const now = yield * DateTime . now ;
2020+ const threadId = ThreadId . make ( "thread:pending-request-window" ) ;
2021+ yield * store . apply ( {
2022+ id : EventId . make ( "event:pending-request-window:thread" ) ,
2023+ type : "thread.created" ,
2024+ threadId,
2025+ occurredAt : now ,
2026+ payload : {
2027+ createdBy : "user" ,
2028+ creationSource : "web" ,
2029+ id : threadId ,
2030+ projectId : ProjectId . make ( "project:pending-request-window" ) ,
2031+ title : "Pending request window" ,
2032+ providerInstanceId,
2033+ modelSelection,
2034+ runtimeMode : "full-access" ,
2035+ interactionMode : "default" ,
2036+ branch : null ,
2037+ worktreePath : null ,
2038+ activeProviderThreadId : null ,
2039+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
2040+ forkedFrom : null ,
2041+ createdAt : now ,
2042+ updatedAt : now ,
2043+ archivedAt : null ,
2044+ settledOverride : null ,
2045+ settledAt : null ,
2046+ lastVisitedAt : null ,
2047+ deletedAt : null ,
2048+ } ,
2049+ } ) ;
2050+ let ordinal = 0 ;
2051+ const itemBase = ( runId : RunId , nodeId : NodeId , id : string ) => ( {
2052+ id : TurnItemId . make ( `item:pending-request-window:${ id } ` ) ,
2053+ threadId,
2054+ runId,
2055+ nodeId,
2056+ providerThreadId : null ,
2057+ providerTurnId : null ,
2058+ nativeItemRef : null ,
2059+ parentItemId : null ,
2060+ ordinal : ++ ordinal ,
2061+ status : "completed" as const ,
2062+ title : null ,
2063+ startedAt : now ,
2064+ completedAt : now ,
2065+ updatedAt : now ,
2066+ } ) ;
2067+ const request = ( id : string , nodeId : NodeId , kind : "user_input" | "command" ) => ( {
2068+ id : RuntimeRequestId . make ( `request:pending-request-window:${ id } ` ) ,
2069+ nodeId,
2070+ providerTurnId : null ,
2071+ nativeRequestRef : null ,
2072+ kind,
2073+ responseCapability : {
2074+ type : "live" as const ,
2075+ providerSessionId : ProviderSessionId . make ( "session:pending-request-window" ) ,
2076+ } ,
2077+ createdAt : now ,
2078+ } ) ;
2079+ const turnCount = THREAD_HISTORY_PAGE_POLICY . maxUserTurns + 4 ;
2080+ for ( let turn = 1 ; turn <= turnCount ; turn += 1 ) {
2081+ const runId = RunId . make ( `run:pending-request-window:${ turn } ` ) ;
2082+ const nodeId = NodeId . make ( `node:pending-request-window:${ turn } ` ) ;
2083+ yield * store . apply ( {
2084+ id : EventId . make ( `event:pending-request-window:run:${ turn } ` ) ,
2085+ type : "run.created" ,
2086+ threadId,
2087+ runId,
2088+ nodeId,
2089+ driver,
2090+ occurredAt : now ,
2091+ payload : {
2092+ id : runId ,
2093+ threadId,
2094+ ordinal : turn ,
2095+ providerInstanceId,
2096+ modelSelection,
2097+ providerThreadId : null ,
2098+ userMessageId : MessageId . make ( `message:pending-request-window:${ turn } ` ) ,
2099+ rootNodeId : nodeId ,
2100+ activeAttemptId : null ,
2101+ status : "completed" ,
2102+ requestedAt : now ,
2103+ startedAt : now ,
2104+ completedAt : now ,
2105+ checkpointId : null ,
2106+ contextHandoffId : null ,
2107+ } ,
2108+ } ) ;
2109+ const items : Array < OrchestrationV2TurnItem > = [
2110+ {
2111+ ...itemBase ( runId , nodeId , `message:${ turn } ` ) ,
2112+ type : "user_message" ,
2113+ createdBy : "user" ,
2114+ creationSource : "web" ,
2115+ messageId : MessageId . make ( `message:pending-request-window:${ turn } ` ) ,
2116+ inputIntent : "turn_start" ,
2117+ text : `Turn ${ turn } ` ,
2118+ attachments : [ ] ,
2119+ } ,
2120+ ] ;
2121+ if ( turn === 1 ) {
2122+ const question = {
2123+ id : "scope" ,
2124+ header : "Scope" ,
2125+ question : "Which scope?" ,
2126+ options : [ { label : "All" , description : "Everything" } ] ,
2127+ } ;
2128+ items . push (
2129+ {
2130+ ...itemBase ( runId , nodeId , "question:pending" ) ,
2131+ status : "running" ,
2132+ type : "user_input_request" ,
2133+ requestId : request ( "question:pending" , nodeId , "user_input" ) . id ,
2134+ questions : [ question ] ,
2135+ } ,
2136+ {
2137+ ...itemBase ( runId , nodeId , "approval:pending" ) ,
2138+ status : "running" ,
2139+ type : "approval_request" ,
2140+ requestId : request ( "approval:pending" , nodeId , "command" ) . id ,
2141+ requestKind : "command" ,
2142+ prompt : "rm -rf build" ,
2143+ } ,
2144+ {
2145+ ...itemBase ( runId , nodeId , "question:resolved" ) ,
2146+ type : "user_input_request" ,
2147+ requestId : request ( "question:resolved" , nodeId , "user_input" ) . id ,
2148+ questions : [ question ] ,
2149+ } ,
2150+ ) ;
2151+ for ( const [ id , kind , status ] of [
2152+ [ "question:pending" , "user_input" , "pending" ] ,
2153+ [ "approval:pending" , "command" , "pending" ] ,
2154+ [ "question:resolved" , "user_input" , "resolved" ] ,
2155+ ] as const ) {
2156+ yield * store . apply ( {
2157+ id : EventId . make ( `event:pending-request-window:request:${ id } ` ) ,
2158+ type : "runtime-request.updated" ,
2159+ threadId,
2160+ nodeId,
2161+ driver,
2162+ occurredAt : now ,
2163+ payload : {
2164+ ...request ( id , nodeId , kind ) ,
2165+ status,
2166+ resolvedAt : status === "pending" ? null : now ,
2167+ } ,
2168+ } ) ;
2169+ }
2170+ }
2171+ for ( const item of items ) {
2172+ yield * store . apply ( {
2173+ id : EventId . make ( `event:pending-request-window:${ item . id } ` ) ,
2174+ type : "turn-item.updated" ,
2175+ threadId,
2176+ runId,
2177+ nodeId,
2178+ driver,
2179+ occurredAt : now ,
2180+ payload : item ,
2181+ } ) ;
2182+ }
2183+ }
2184+
2185+ const pendingCardIds = [
2186+ TurnItemId . make ( "item:pending-request-window:question:pending" ) ,
2187+ TurnItemId . make ( "item:pending-request-window:approval:pending" ) ,
2188+ ] ;
2189+ const { statements, tracer } = traceSqlStatements ( ) ;
2190+ const snapshot = yield * store
2191+ . getThreadSnapshotWindow ( threadId , {
2192+ rowLimit : THREAD_HISTORY_PAGE_POLICY . maxItems + 2 ,
2193+ userTurnLimit : THREAD_HISTORY_PAGE_POLICY . maxUserTurns ,
2194+ } )
2195+ . pipe ( Effect . withTracer ( tracer ) ) ;
2196+ const sql = yield * SqlClient . SqlClient ;
2197+ const query = statements . find (
2198+ ( statement ) =>
2199+ statement . includes ( "FROM orchestration_v2_projection_runtime_requests AS request" ) &&
2200+ statement . includes ( "SELECT item.payload_json" ) ,
2201+ ) ;
2202+ assert . isDefined ( query ) ;
2203+ const plan = ( yield * sql . unsafe < { readonly detail : string } > (
2204+ `EXPLAIN QUERY PLAN ${ query } ` ,
2205+ ) ) . map ( ( row ) => row . detail ) ;
2206+ assert . include (
2207+ plan [ 0 ] ,
2208+ "SEARCH request USING INDEX orchestration_v2_projection_runtime_requests_thread_status_idx" ,
2209+ ) ;
2210+ assert . include (
2211+ plan [ 1 ] ,
2212+ "SEARCH item USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx" ,
2213+ ) ;
2214+ const bounded = buildBoundedThreadProjection ( {
2215+ projection : snapshot . projection ,
2216+ snapshotSequence : snapshot . snapshotSequence ,
2217+ } ) ;
2218+ assert . isTrue ( bounded . hasMoreHistory ) ;
2219+ assert . notInclude (
2220+ bounded . projection . turnItems . map ( ( item ) => item . id ) ,
2221+ TurnItemId . make ( "item:pending-request-window:question:resolved" ) ,
2222+ ) ;
2223+ for ( const projection of [ snapshot . projection , bounded . projection ] ) {
2224+ const visibleIds = projection . visibleTurnItems . map ( ( row ) => row . sourceItemId ) ;
2225+ assert . notInclude ( visibleIds , TurnItemId . make ( "item:pending-request-window:message:1" ) ) ;
2226+ assert . includeMembers (
2227+ projection . turnItems . map ( ( item ) => item . id ) ,
2228+ pendingCardIds ,
2229+ ) ;
2230+ for ( const id of pendingCardIds ) assert . notInclude ( visibleIds , id ) ;
2231+ }
2232+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
2233+ ) ;
2234+
20152235it . effect ( "lists only active subagent IDs without loading shell data" , ( ) =>
20162236 Effect . gen ( function * ( ) {
20172237 const store = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments