@@ -735,6 +735,16 @@ export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, Projec
735735
736736export const ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION = 2 ;
737737
738+ // Completed items of these types can fold into one deferred-work row in snapshot windows.
739+ const DEFERRABLE_WORK_TYPES = [
740+ "command_execution" ,
741+ "reasoning" ,
742+ "file_change" ,
743+ "file_search" ,
744+ "web_search" ,
745+ "dynamic_tool" ,
746+ ] as const ;
747+
738748function upsertById < T extends { readonly id : string } > ( items : ReadonlyArray < T > , next : T ) : Array < T > {
739749 const index = items . findIndex ( ( item ) => item . id === next . id ) ;
740750 if ( index === - 1 ) {
@@ -3032,14 +3042,54 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30323042 SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
30333043 ` ;
30343044 const boundary = anchors [ 0 ] ! ;
3045+ // Folding work needs whole segments, so the window would otherwise walk every item
3046+ // since the boundary. Types outside the deferrable set always keep their own row,
3047+ // so the window cannot reach below the newest `tailRows` of them; start at the
3048+ // latest segment of each run that reaches that point so its groups stay whole.
3049+ const tailRows = Math . max ( window . rowLimit , THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT ) ;
3050+ const workStart =
3051+ window . deferWork !== true || boundary . latest === null
3052+ ? boundary . ordinal
3053+ : Math . max (
3054+ boundary . ordinal ,
3055+ ( yield * sql < { readonly ordinal : number | null } > `
3056+ ${ eligible } , visible_tail AS (
3057+ SELECT ordinal FROM eligible
3058+ WHERE ordinal >= ${ boundary . ordinal }
3059+ AND type NOT IN ${ sql . in ( DEFERRABLE_WORK_TYPES ) }
3060+ ORDER BY ordinal DESC
3061+ LIMIT ${ tailRows }
3062+ ), tail AS (
3063+ SELECT MIN(ordinal) AS ordinal FROM visible_tail HAVING COUNT(*) >= ${ tailRows }
3064+ ), tail_runs AS (
3065+ SELECT DISTINCT item.run_id
3066+ FROM tail CROSS JOIN eligible AS item
3067+ WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3068+ )
3069+ SELECT MIN(COALESCE(
3070+ (
3071+ SELECT start.ordinal FROM eligible AS start
3072+ WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3073+ AND start.ordinal <= tail.ordinal
3074+ ORDER BY start.ordinal DESC LIMIT 1
3075+ ),
3076+ (
3077+ SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3078+ WHERE first.run_id = tail_runs.run_id
3079+ ),
3080+ tail.ordinal
3081+ )) AS ordinal
3082+ FROM tail LEFT JOIN tail_runs
3083+ ` ) [ 0 ] ?. ordinal ?? 0 ,
3084+ ) ;
30353085 return sql `
30363086 ${ eligible } , blocked_runs AS MATERIALIZED (
30373087 SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
30383088 WHERE thread_id = ${ threadId } AND run_id IS NOT NULL
30393089 AND ${ window . deferWork ? 1 : 0 } = 1
30403090 AND run_id IN (
30413091 SELECT run_id FROM eligible
3042- WHERE ordinal >= ${ boundary . ordinal } AND run_id IS NOT NULL
3092+ WHERE ordinal >= ${ workStart } AND run_id IS NOT NULL
30433093 )
30443094 AND (type IN ('run_interrupt_request', 'run_interrupt_result')
30453095 OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
@@ -3048,14 +3098,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30483098 MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
30493099 OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
30503100 CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3051- AND type IN ('command_execution', 'reasoning', 'file_change', 'file_search', 'web_search', 'dynamic_tool')
3101+ AND type IN ${ sql . in ( DEFERRABLE_WORK_TYPES ) }
30523102 AND NOT (${ inlineWorkOutput } )
30533103 AND run_id NOT IN (SELECT run_id FROM blocked_runs)
30543104 THEN 1 ELSE 0 END AS can_defer
30553105 FROM eligible AS item
30563106 WHERE ${ window . deferWork ? 1 : 0 } = 1
30573107 AND ${ boundary . latest === null ? 0 : 1 } = 1
3058- AND ordinal >= ${ boundary . ordinal }
3108+ AND ordinal >= ${ workStart }
30593109 ), deferred_work AS (
30603110 SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
30613111 MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
0 commit comments