@@ -21,6 +21,8 @@ import {
2121 toolOutputIndicatesFailure ,
2222} from "@t3tools/shared/toolOutput" ;
2323
24+ // A checkpoint over vendored or generated trees can list 15k+ files; the card needs a prefix.
25+ const MAX_WIRE_CHECKPOINT_FILES = 200 ;
2426const MAX_DETAIL_STRING_BYTES = 32_768 ;
2527const MAX_DYNAMIC_VALUE_BYTES = 16_384 ;
2628const MAX_ON_DEMAND_BYTES = 256 * 1024 ;
@@ -96,8 +98,59 @@ function summarizeDynamicValue(value: unknown): unknown {
9698 } ;
9799}
98100
101+ type CheckpointFiles = Pick < OrchestrationV2Checkpoint , "files" | "filesTotal" > ;
102+
103+ // Same input, same output object: snapshot compaction relies on shared item identity.
104+ const cappedCheckpointFiles = new WeakMap < object , object > ( ) ;
105+
106+ function capCheckpointFiles < T extends CheckpointFiles > ( value : T ) : T {
107+ if ( value . files . length <= MAX_WIRE_CHECKPOINT_FILES || value . filesTotal !== undefined ) {
108+ return value ;
109+ }
110+ const cached = cappedCheckpointFiles . get ( value ) ;
111+ if ( cached !== undefined ) return cached as T ;
112+ let additions = 0 ;
113+ let deletions = 0 ;
114+ for ( const file of value . files ) {
115+ additions += file . additions ;
116+ deletions += file . deletions ;
117+ }
118+ const capped = {
119+ ...value ,
120+ files : value . files . slice ( 0 , MAX_WIRE_CHECKPOINT_FILES ) ,
121+ filesTotal : { count : value . files . length , additions, deletions } ,
122+ } ;
123+ cappedCheckpointFiles . set ( value , capped ) ;
124+ return capped ;
125+ }
126+
127+ /** Idempotent; bounded snapshots that skip `projectThreadProjectionForWire` still need it. */
128+ export function capProjectionCheckpointFilesForWire (
129+ projection : OrchestrationV2ThreadProjection ,
130+ ) : OrchestrationV2ThreadProjection {
131+ const checkpoints = projection . checkpoints . map ( capCheckpointFiles ) ;
132+ const turnItems = projection . turnItems . map ( capCheckpointItemFiles ) ;
133+ const visibleTurnItems = projection . visibleTurnItems . map ( ( row ) => {
134+ const item = capCheckpointItemFiles ( row . item ) ;
135+ return item === row . item ? row : { ...row , item } ;
136+ } ) ;
137+ const unchanged = ( next : ReadonlyArray < unknown > , previous : ReadonlyArray < unknown > ) =>
138+ next . every ( ( value , index ) => value === previous [ index ] ) ;
139+ return unchanged ( checkpoints , projection . checkpoints ) &&
140+ unchanged ( turnItems , projection . turnItems ) &&
141+ unchanged ( visibleTurnItems , projection . visibleTurnItems )
142+ ? projection
143+ : { ...projection , checkpoints, turnItems, visibleTurnItems } ;
144+ }
145+
146+ function capCheckpointItemFiles ( item : OrchestrationV2TurnItem ) : OrchestrationV2TurnItem {
147+ return item . type === "checkpoint" ? capCheckpointFiles ( item ) : item ;
148+ }
149+
99150export function projectTurnItemForWire ( item : OrchestrationV2TurnItem ) : OrchestrationV2TurnItem {
100151 switch ( item . type ) {
152+ case "checkpoint" :
153+ return capCheckpointFiles ( item ) ;
101154 case "handoff" : {
102155 const { summary : _summary , ...projected } = item ;
103156 return projected ;
@@ -241,6 +294,7 @@ export function projectThreadProjectionForWire(
241294 } ;
242295 return {
243296 ...projection ,
297+ checkpoints : projection . checkpoints . map ( capCheckpointFiles ) ,
244298 contextHandoffs : projection . contextHandoffs . map ( projectContextHandoffForWire ) ,
245299 turnItems : projection . turnItems . map ( project ) ,
246300 visibleTurnItems : projection . visibleTurnItems . map ( ( row ) => ( {
@@ -257,7 +311,9 @@ export function projectDomainEventForWire(
257311 ? { ...event , payload : projectTurnItemForWire ( event . payload ) }
258312 : event . type === "context-handoff.updated"
259313 ? { ...event , payload : projectContextHandoffForWire ( event . payload ) }
260- : event ;
314+ : event . type === "checkpoint.captured"
315+ ? { ...event , payload : capCheckpointFiles ( event . payload ) }
316+ : event ;
261317}
262318
263319const LIVE_DEDUPE_MEMORY = 32 ;
0 commit comments