@@ -43,6 +43,10 @@ export function toIso(unixSeconds: number): string {
4343 return new Date ( unixSeconds * 1000 ) . toISOString ( )
4444}
4545
46+ function cloneRange ( range : Range ) : Range {
47+ return { ...range }
48+ }
49+
4650// MARK: - Subdivision
4751
4852/** Default number of segments to split the older remainder into. */
@@ -84,7 +88,7 @@ export function subdivideRanges(
8488 const secondsLeft = newEnd - start
8589 const segments = Math . min ( n , secondsLeft )
8690 const segmentDuration = Math . floor ( secondsLeft / segments )
87- if ( segmentDuration < 30 ) {
91+ if ( segmentDuration <= 1 ) {
8892 result . push ( range )
8993 continue
9094 }
@@ -96,7 +100,7 @@ export function subdivideRanges(
96100 if ( lastSegment ) {
97101 // handle the edge case where there are multiple objects created in a same second
98102 // but our fetch didn't return all of them because of the limit of 100.
99- result . push ( { gte : toIso ( segGte ) , lt : toIso ( newEnd + 1 ) , cursor : range . cursor } )
103+ result . push ( { gte : toIso ( segGte ) , lt : toIso ( newEnd + 1 ) , cursor : null } )
100104 } else {
101105 result . push ( { gte : toIso ( segGte ) , lt : toIso ( segLt ) , cursor : null } )
102106 }
@@ -179,7 +183,7 @@ export async function* streamingSubdivide<T>(opts: {
179183
180184 /** Snapshot of all ranges not yet fully fetched (queued + in flight). */
181185 function snapshotRemaining ( ) : Range [ ] {
182- return [ ...inflightRanges . values ( ) , ...queue ]
186+ return [ ...inflightRanges . values ( ) , ...queue ] . map ( cloneRange )
183187 }
184188
185189 // Fill up to concurrency
@@ -211,15 +215,18 @@ export async function* streamingSubdivide<T>(opts: {
211215 queue . push ( range )
212216 }
213217
214- // Launch new work BEFORE yielding so fetches run while consumer processes
218+ const eventRange = cloneRange ( range )
219+ const remaining = snapshotRemaining ( )
220+
221+ // Launch new work after snapshotting so checkpoints cannot observe later cursor mutation.
215222 while ( launchNext ( ) ) { }
216223
217224 yield {
218- range,
225+ range : eventRange ,
219226 data,
220227 hasMore,
221228 exhausted : ! hasMore ,
222- remaining : snapshotRemaining ( ) ,
229+ remaining,
223230 }
224231 }
225232 } finally {
0 commit comments