@@ -41,20 +41,32 @@ const ZERO_STEP = {
4141// single fixed sleep. Snapshot writes are debounced/async, so a hardcoded delay
4242// is too short under heavy parallel CI load (the snapshot hasn't flushed yet) →
4343// flaky reads of a stale status. Polling is robust regardless of machine load.
44- async function pollStatus ( tracer : { getTracePath ( ) : string | undefined } , expected : string , timeoutMs = 4000 ) {
44+ async function pollTrace (
45+ tracer : { getTracePath ( ) : string | undefined } ,
46+ accept : ( snapshot : TraceFile ) => boolean ,
47+ description : string ,
48+ timeoutMs = 4000 ,
49+ ) {
4550 const start = Date . now ( )
46- let last = "<none>"
51+ let last : TraceFile | undefined
4752 while ( Date . now ( ) - start < timeoutMs ) {
4853 try {
4954 const snap = JSON . parse ( await fs . readFile ( tracer . getTracePath ( ) ! , "utf-8" ) ) as TraceFile
50- last = snap . summary . status
51- if ( last === expected ) return snap
55+ last = snap
56+ if ( accept ( snap ) ) return snap
5257 } catch {
5358 /* file mid-write or not yet created — keep polling */
5459 }
5560 await new Promise ( ( r ) => setTimeout ( r , 25 ) )
5661 }
57- throw new Error ( `timed out after ${ timeoutMs } ms waiting for status '${ expected } ' (last seen '${ last } ')` )
62+ throw new Error (
63+ `timed out after ${ timeoutMs } ms waiting for ${ description } ` +
64+ `(last status '${ last ?. summary . status ?? "<none>" } ', spans ${ last ?. spans . length ?? 0 } )` ,
65+ )
66+ }
67+
68+ async function pollStatus ( tracer : { getTracePath ( ) : string | undefined } , expected : string , timeoutMs = 4000 ) {
69+ return pollTrace ( tracer , ( snapshot ) => snapshot . summary . status === expected , `status '${ expected } '` , timeoutMs )
5870}
5971
6072// ---------------------------------------------------------------------------
@@ -75,11 +87,13 @@ describe("buildTraceFile — snapshot isolation", () => {
7587 state : { status : "completed" , input : { } , output : "ok" , time : { start : 1 , end : 2 } } ,
7688 } )
7789
78- // Wait for snapshot to write
79- await new Promise ( ( r ) => setTimeout ( r , 50 ) )
80-
81- // Read the snapshot
82- const snap1 = JSON . parse ( await fs . readFile ( tracer . getTracePath ( ) ! , "utf-8" ) ) as TraceFile
90+ // Snapshot writes are asynchronous/debounced; poll instead of racing a
91+ // fixed sleep on loaded CI hosts.
92+ const snap1 = await pollTrace (
93+ tracer ,
94+ ( snapshot ) => snapshot . spans . some ( ( span ) => span . kind === "tool" && span . name === "bash" ) ,
95+ "the first tool span" ,
96+ )
8397 const snap1Model = snap1 . metadata . model
8498
8599 // Now mutate the metadata via enrichFromAssistant
@@ -103,9 +117,11 @@ describe("buildTraceFile — snapshot isolation", () => {
103117 state : { status : "completed" , input : { } , output : "ok" , time : { start : 1 , end : 2 } } ,
104118 } )
105119
106- // Wait for snapshot
107- await new Promise ( ( r ) => setTimeout ( r , 50 ) )
108- const snap1 = JSON . parse ( await fs . readFile ( tracer . getTracePath ( ) ! , "utf-8" ) ) as TraceFile
120+ const snap1 = await pollTrace (
121+ tracer ,
122+ ( snapshot ) => snapshot . spans . some ( ( span ) => span . kind === "tool" && span . name === "bash" ) ,
123+ "the first tool span" ,
124+ )
109125 const span1Count = snap1 . spans . length
110126
111127 // Add more spans
@@ -115,9 +131,13 @@ describe("buildTraceFile — snapshot isolation", () => {
115131 state : { status : "completed" , input : { } , output : "content" , time : { start : 3 , end : 4 } } ,
116132 } )
117133
118- // Wait for second snapshot
119- await new Promise ( ( r ) => setTimeout ( r , 50 ) )
120- const snap2 = JSON . parse ( await fs . readFile ( tracer . getTracePath ( ) ! , "utf-8" ) ) as TraceFile
134+ const snap2 = await pollTrace (
135+ tracer ,
136+ ( snapshot ) =>
137+ snapshot . spans . length > span1Count &&
138+ snapshot . spans . some ( ( span ) => span . kind === "tool" && span . name === "read" ) ,
139+ `the second tool span after ${ span1Count } spans` ,
140+ )
121141
122142 // Second snapshot should have more spans
123143 expect ( snap2 . spans . length ) . toBeGreaterThan ( span1Count )
0 commit comments