@@ -10,6 +10,7 @@ import {
1010 type ModelSelection ,
1111 type OrchestrationV2ProviderThread ,
1212 type OrchestrationV2TurnItem ,
13+ type OrchestrationV2ThreadHistoryPage ,
1314 type OrchestrationV2ThreadShellSnapshot ,
1415 NodeId ,
1516 PlanId ,
@@ -1916,6 +1917,89 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
19161917 Array . from ( { length : 1_000 } , ( _ , index ) => `turn-item:bounded-sql-history:${ index + 1 } ` ) ,
19171918 ) ;
19181919
1920+ yield * setNewestTurnType ( "command_execution" ) ;
1921+ yield * sql `
1922+ UPDATE orchestration_v2_projection_turn_items
1923+ SET run_id = 'run:bounded-sql-history:1',
1924+ payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:1')
1925+ WHERE thread_id = ${ threadId }
1926+ ` ;
1927+ const longTurnSnapshot = yield * humanWindow ( ) ;
1928+ assert . lengthOf ( longTurnSnapshot . projection . visibleTurnItems , sqlPageLimit ) ;
1929+ const longTurnPage = buildBoundedThreadProjection ( {
1930+ ...longTurnSnapshot ,
1931+ } ) ;
1932+ const promptId = TurnItemId . make ( "turn-item:bounded-sql-history:1" ) ;
1933+ assert . strictEqual ( longTurnPage . projection . visibleTurnItems [ 0 ] ?. sourceItemId , promptId ) ;
1934+ const longTurnIds = new Set (
1935+ longTurnPage . projection . visibleTurnItems . map ( ( row ) => row . sourceItemId ) ,
1936+ ) ;
1937+ let longTurnCursor = longTurnPage . historyCursor ;
1938+ for ( let pages = 0 ; longTurnCursor !== null ; pages += 1 ) {
1939+ assert . isBelow ( pages , 20 ) ;
1940+ assert . notStrictEqual ( decodeThreadHistoryCursor ( longTurnCursor ) . si , promptId ) ;
1941+ const page = yield * projectionStore . getThreadHistoryPage ( threadId , longTurnCursor ) ;
1942+ assert . strictEqual ( page . items [ 0 ] ?. sourceItemId , promptId ) ;
1943+ for ( const row of page . items ) longTurnIds . add ( row . sourceItemId ) ;
1944+ longTurnCursor = page . nextCursor ;
1945+ }
1946+ assert . strictEqual ( longTurnIds . size , 1_000 ) ;
1947+ const conversation = yield * projectionStore . getThreadHistoryPage (
1948+ threadId ,
1949+ longTurnPage . historyCursor ! ,
1950+ undefined ,
1951+ true ,
1952+ ) ;
1953+ assert . strictEqual ( conversation . items [ 0 ] ?. sourceItemId , promptId ) ;
1954+ const deferred = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
1955+ rowLimit : sqlPageLimit ,
1956+ userTurnLimit : 10 ,
1957+ deferWork : true ,
1958+ } ) ;
1959+ assert . lengthOf ( deferred . projection . visibleTurnItems , 3 ) ;
1960+ assert . strictEqual ( deferred . projection . visibleTurnItems [ 0 ] ?. sourceItemId , promptId ) ;
1961+ const anchor = deferred . projection . visibleTurnItems [ 1 ] ! ;
1962+ assert . strictEqual ( anchor . item . deferredWork ?. itemCount , 999 ) ;
1963+ const workIds = new Set < TurnItemId > ( ) ;
1964+ let workCursor : string | null = anchor . item . deferredWork ! . cursor ;
1965+ for ( let pages = 0 ; workCursor !== null ; pages += 1 ) {
1966+ assert . isBelow ( pages , 20 ) ;
1967+ const page : OrchestrationV2ThreadHistoryPage = yield * projectionStore . getThreadHistoryPage (
1968+ threadId ,
1969+ workCursor ,
1970+ anchor . sourceItemId ,
1971+ false ,
1972+ "work" ,
1973+ ) ;
1974+ assert . isAtMost ( page . items . length , 75 ) ;
1975+ for ( const row of page . items ) workIds . add ( row . sourceItemId ) ;
1976+ workCursor = page . nextCursor ;
1977+ }
1978+ assert . strictEqual ( workIds . size , 999 ) ;
1979+ yield * sql `
1980+ UPDATE orchestration_v2_projection_turn_items
1981+ SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
1982+ WHERE thread_id = ${ threadId } AND ordinal = 1
1983+ ` ;
1984+ const backgroundWindow = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
1985+ rowLimit : sqlPageLimit ,
1986+ userTurnLimit : 10 ,
1987+ deferWork : true ,
1988+ } ) ;
1989+ assert . lengthOf ( backgroundWindow . projection . visibleTurnItems , sqlPageLimit ) ;
1990+ yield * sql `
1991+ UPDATE orchestration_v2_projection_turn_items
1992+ SET type = 'user_message', payload_json = json_set(payload_json, '$.type', 'user_message')
1993+ WHERE thread_id = ${ threadId } AND ordinal = 1
1994+ ` ;
1995+ yield * sql `
1996+ UPDATE orchestration_v2_projection_turn_items
1997+ SET run_id = 'run:bounded-sql-history:' || ordinal,
1998+ payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:' || ordinal)
1999+ WHERE thread_id = ${ threadId }
2000+ ` ;
2001+ yield * setNewestTurnType ( "user_message" ) ;
2002+
19192003 const tiedIds = [
19202004 TurnItemId . make ( "turn-item:bounded-sql-history:tie:b" ) ,
19212005 TurnItemId . make ( "turn-item:bounded-sql-history:tie:a" ) ,
0 commit comments