@@ -38,6 +38,7 @@ import * as ProjectionStore from "./ProjectionStore.ts";
3838import {
3939 buildBoundedThreadProjection ,
4040 decodeThreadHistoryCursor ,
41+ projectedRowEncodedBytes ,
4142 selectHistoryPageFromCursor ,
4243 THREAD_HISTORY_PAGE_POLICY ,
4344} from "./threadHistoryPaging.ts" ;
@@ -863,7 +864,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
863864 } ) ,
864865 ) ;
865866
866- it . effect ( "pages complete user turns through SQL regardless of tool count or payload size " , ( ) =>
867+ it . effect ( "bounds SQL history inside long user turns and recovers every item through paging " , ( ) =>
867868 Effect . gen ( function * ( ) {
868869 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
869870 const sql = yield * SqlClient . SqlClient ;
@@ -1007,18 +1008,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
10071008 nodePlan . map ( ( row ) => row . detail ) ,
10081009 "SEARCH orchestration_v2_projection_nodes USING INDEX orchestration_v2_projection_nodes_live_idx (thread_id=?)" ,
10091010 ) ;
1010- // Only the selected turn cohort and two lookahead anchors are decoded .
1011- assert . lengthOf ( initial . projection . turnItems , 12 * 102 ) ;
1011+ // Human turns must not bypass the SQL row limit before payload decoding .
1012+ assert . lengthOf ( initial . projection . turnItems , 77 ) ;
10121013 const bounded = buildBoundedThreadProjection ( {
10131014 projection : initial . projection ,
10141015 snapshotSequence : 0 ,
10151016 } ) ;
1016- assert . lengthOf ( bounded . projection . visibleTurnItems , 10 * 102 ) ;
1017- assert . strictEqual ( bounded . projection . visibleTurnItems [ 0 ] ?. sourceItemId , allIds [ 35 * 102 ] ) ;
1017+ assert . lengthOf ( bounded . projection . visibleTurnItems , THREAD_HISTORY_PAGE_POLICY . maxItems ) ;
1018+ assert . strictEqual ( bounded . projection . visibleTurnItems [ 0 ] ?. sourceItemId , allIds . at ( - 75 ) ) ;
10181019 const loaded = bounded . projection . visibleTurnItems . map ( ( row ) => String ( row . sourceItemId ) ) ;
10191020 let cursor = bounded . historyCursor ;
1020- for ( const turns of [ 20 , 15 ] ) {
1021- assert . isNotNull ( cursor ) ;
1021+ for ( let pages = 0 ; cursor !== null ; pages += 1 ) {
1022+ assert . isBelow ( pages , allIds . length ) ;
10221023 const anchor = decodeThreadHistoryCursor ( cursor ! ) ;
10231024 const snapshot = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
10241025 rowLimit : 77 ,
@@ -1031,17 +1032,42 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
10311032 cursor : cursor ! ,
10321033 snapshotSequence : 0 ,
10331034 } ) ;
1034- assert . lengthOf ( page . items , turns * 102 ) ;
1035+ assert . isAtMost ( snapshot . projection . turnItems . length , 77 ) ;
1036+ assert . isAbove ( page . items . length , 0 ) ;
1037+ assert . isAtMost ( page . items . length , THREAD_HISTORY_PAGE_POLICY . maxItems ) ;
10351038 loaded . unshift ( ...page . items . map ( ( row ) => String ( row . sourceItemId ) ) ) ;
10361039 cursor = page . nextCursor ;
10371040 }
10381041 assert . isNull ( cursor ) ;
10391042 assert . deepEqual ( loaded , allIds ) ;
1043+ // Exercise the production history service after wire projection as well.
1044+ yield * sql `UPDATE orchestration_v2_projection_turn_items
1045+ SET payload_json = json_set(payload_json, '$.output', ${ "x" . repeat ( 32_000 ) } )
1046+ WHERE thread_id = ${ threadId } AND type = 'command_execution'
1047+ AND ordinal > ${ allIds . length - 150 } AND ordinal <= ${ allIds . length - 75 } ` ;
1048+ const recovered : string [ ] = [ ] ;
1049+ let historyCursor = bounded . historyCursor ;
1050+ for ( let pages = 0 ; recovered . length < 75 ; pages += 1 ) {
1051+ assert . isBelow ( pages , 75 ) ;
1052+ assert . isNotNull ( historyCursor ) ;
1053+ const page = yield * projectionStore . getThreadHistoryPage ( threadId , historyCursor ! ) ;
1054+ assert . isAbove ( page . items . length , 0 ) ;
1055+ assert . isAtMost ( page . items . length , THREAD_HISTORY_PAGE_POLICY . maxItems ) ;
1056+ assert . isAtMost (
1057+ page . items . reduce ( ( size , row ) => size + projectedRowEncodedBytes ( row ) , 0 ) ,
1058+ THREAD_HISTORY_PAGE_POLICY . maxEncodedBytes ,
1059+ ) ;
1060+ if ( pages === 0 ) assert . isBelow ( page . items . length , 75 ) ;
1061+ recovered . unshift ( ...page . items . map ( ( row ) => String ( row . sourceItemId ) ) ) ;
1062+ historyCursor = page . nextCursor ;
1063+ }
1064+ assert . deepEqual ( recovered , allIds . slice ( - 75 - recovered . length , - 75 ) ) ;
10401065 } ) ,
10411066 ) ;
10421067
10431068 it . effect ( "caps a SQL window by runs that a notification started" , ( ) =>
10441069 Effect . gen ( function * ( ) {
1070+ const policy = { ...THREAD_HISTORY_PAGE_POLICY , maxItems : 1_000 } ;
10451071 // A thread woken by PR watches: 12 user turns, then 400 runs that a
10461072 // notification started, with a user turn every 50 runs. The user-turn
10471073 // window reached back to the 12th-newest user turn and read almost every row.
@@ -1183,7 +1209,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
11831209
11841210 const { statements, tracer } = traceSqlStatements ( ) ;
11851211 const initial = yield * projectionStore
1186- . getThreadSnapshotWindow ( threadId , { rowLimit : 77 , userTurnLimit : 10 } )
1212+ . getThreadSnapshotWindow ( threadId , { rowLimit : 1_000 , userTurnLimit : 10 } )
11871213 . pipe ( Effect . withTracer ( tracer ) ) ;
11881214 // The 152 newest turns are the 152 newest started runs, 261 to 412. Only
11891215 // three of them are user turns, short of the 12 the user-turn window needs.
@@ -1210,6 +1236,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12101236 const bounded = buildBoundedThreadProjection ( {
12111237 projection : initial . projection ,
12121238 snapshotSequence : 0 ,
1239+ policy,
12131240 } ) ;
12141241 assert . strictEqual ( bounded . projection . visibleTurnItems [ 0 ] ?. sourceItemId , startIds [ 262 ] ) ;
12151242 const loaded = bounded . projection . visibleTurnItems . map ( ( row ) => String ( row . sourceItemId ) ) ;
@@ -1218,7 +1245,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12181245 assert . isBelow ( pages , 5 ) ;
12191246 const anchor = decodeThreadHistoryCursor ( cursor ) ;
12201247 const snapshot = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
1221- rowLimit : 77 ,
1248+ rowLimit : 1_000 ,
12221249 userTurnLimit : 20 ,
12231250 anchorItemId : TurnItemId . make ( anchor . si ) ,
12241251 anchorThreadId : ThreadId . make ( anchor . st ) ,
@@ -1229,6 +1256,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12291256 items : snapshot . projection . visibleTurnItems ,
12301257 cursor,
12311258 snapshotSequence : 0 ,
1259+ policy,
12321260 } ) ;
12331261 loaded . unshift ( ...page . items . map ( ( row ) => String ( row . sourceItemId ) ) ) ;
12341262 cursor = page . nextCursor ;
0 commit comments