@@ -40,6 +40,17 @@ const TEST_EXECUTION_CONTEXT = {
4040 userId : 'user-1' ,
4141}
4242
43+ /** Wraps `leaf` in `depth` levels built by `level`, innermost first. */
44+ function nest (
45+ depth : number ,
46+ level : ( child : unknown , index : number ) => unknown ,
47+ leaf : unknown = 'leaf'
48+ ) : unknown {
49+ let value = leaf
50+ for ( let index = 0 ; index < depth ; index ++ ) value = level ( value , index )
51+ return value
52+ }
53+
4354describe ( 'compactExecutionPayload' , ( ) => {
4455 beforeEach ( ( ) => {
4556 clearLargeValueCacheForTests ( )
@@ -337,11 +348,6 @@ describe('compactExecutionPayload', () => {
337348 } )
338349
339350 it ( 'compacts trees nested deeper than one synchronous stack can walk' , async ( ) => {
340- const nest = ( depth : number , level : ( child : unknown , index : number ) => unknown ) => {
341- let value : unknown = 'leaf'
342- for ( let index = 0 ; index < depth ; index ++ ) value = level ( value , index )
343- return value
344- }
345351 // Both overflow an unbroken synchronous walk even on Vitest's larger worker stack.
346352 const plain = nest ( 10_000 , ( child ) => ( { child } ) )
347353 // A Date has a toJSON, so every level serializes its subtree from where the walk stands.
@@ -354,6 +360,22 @@ describe('compactExecutionPayload', () => {
354360 )
355361 } )
356362
363+ it ( 'compacts an object shared across a resumed walk where the walk first reaches it' , async ( ) => {
364+ const shared = { text : 'x' . repeat ( 8192 ) }
365+ const depth = 300
366+ const deep = nest ( depth , ( child ) => ( { child } ) , { shared } )
367+
368+ const compacted = await compactExecutionPayload (
369+ { deep, later : shared } ,
370+ { thresholdBytes : 4096 , preserveRoot : true , ...TEST_EXECUTION_CONTEXT }
371+ )
372+
373+ let tail = compacted . deep as Record < string , unknown >
374+ for ( let index = 0 ; index < depth ; index ++ ) tail = tail . child as Record < string , unknown >
375+ expect ( isLargeValueRef ( ( tail . shared as { text : unknown } ) . text ) ) . toBe ( true )
376+ expect ( compacted . later ) . toBe ( shared )
377+ } )
378+
357379 it ( 'rejects durable compaction when storage context is incomplete' , async ( ) => {
358380 await expect (
359381 compactExecutionPayload (
0 commit comments