@@ -150,6 +150,7 @@ function drainApplyQueue(): void {
150150 if ( item . ops !== undefined || item . si !== undefined )
151151 firstError = applyStructural ( item , next , firstError ) ;
152152 }
153+ firstError = runLateSweeps ( firstError ) ;
153154 if ( firstError !== UNSET ) {
154155 // Unhandled patch errors HALT like unhandled effect errors (re-audit 2,
155156 // P1-4): app state is undefined past an unboundaried throw.
@@ -168,12 +169,99 @@ function visibleStructRows(t: StoreNextTarget): any {
168169 return t . fam ?. opt === true ? t . px : ( t . pb ?? t . v ) ;
169170}
170171
171- /** Per-drain generation for late-resync dedup (audit follow-up P2): several
172- * held items on ONE channel each ran the late sweep — same entries, same
173- * live rebuild, item-count × consumer-count applications. Entries stamp the
174- * drain they were resynced in; repeats within it skip. */
172+ /** Per-drain generation for late-resync dedup (audit follow-up P2): entries
173+ * stamp the drain they were resynced in; repeats within one flush's drains
174+ * (lane + regular) skip. Incremented once per top-level drain. */
175175let drainGen = 0 ;
176176
177+ /** Late-registrant bookkeeping, swept once at DRAIN END (fold audit P1):
178+ * sweeping per item resynced a late entry to the LIVE view and then a
179+ * LATER queued item — whose emission snapshot legitimately included that
180+ * entry — re-applied its baseline-relative ops on top, rebuilding the same
181+ * DOM row twice. The drain records, per channel, the HIGHEST emission
182+ * watermark (`maxRq`: anyone at or below was in some snapshot and received
183+ * real, baseline-sound ops), the FIXED window's far edge (`winEnd`,
184+ * captured at the channel's first item so mid-drain registrants are
185+ * excluded), one row item and the slot items as resync vehicles. The sweep
186+ * then covers exactly the entries NO snapshot reached: `sq` in
187+ * (maxRq, winEnd]. Registrations append in `sq` order, so those are a
188+ * SUFFIX — the tail scan stays O(#late). */
189+ interface PcSweep {
190+ maxRq : number ;
191+ winEnd : number ;
192+ row : QueuedApply | null ;
193+ slots : QueuedApply [ ] | null ;
194+ }
195+ let sweeps : Map < PatchChannel , PcSweep > | null = null ;
196+
197+ function noteSweep ( item : QueuedApply , winEnd : number ) : void {
198+ if ( item . pc === undefined ) return ;
199+ let s = ( sweeps ??= new Map ( ) ) . get ( item . pc ) ;
200+ if ( s === undefined ) sweeps . set ( item . pc , ( s = { maxRq : 0 , winEnd, row : null , slots : null } ) ) ;
201+ const rq = ( item . rq as number ) | 0 ;
202+ if ( rq > s . maxRq ) s . maxRq = rq ;
203+ if ( item . si !== undefined ) ( s . slots ??= [ ] ) . push ( item ) ;
204+ else s . row = item ;
205+ }
206+
207+ function sweepList (
208+ live : ( RowOpsEntry & {
209+ hq ?: boolean ;
210+ hqs ?: Set < number > ;
211+ sq ?: number ;
212+ dg ?: number ;
213+ q ?: unknown ;
214+ } ) [ ] ,
215+ s : PcSweep ,
216+ item : QueuedApply ,
217+ firstError : unknown
218+ ) : unknown {
219+ for ( let j = live . length - 1 ; j >= 0 ; j -- ) {
220+ const entry = live [ j ] ;
221+ const sq = ( entry . sq as number ) | 0 ;
222+ if ( sq <= s . maxRq ) break ; // suffix exhausted — everyone else rode a snapshot
223+ if ( sq > s . winEnd ) continue ; // registered mid-drain: outside the window
224+ if ( entry . u === true || entry . hq === true ) continue ;
225+ if ( item . si !== undefined && entry . hqs ?. has ( item . si ) === true ) continue ;
226+ if ( item . si === undefined && entry . dg === drainGen ) continue ;
227+ if ( entry . owner !== null && isDisposed ( entry . owner ) ) continue ;
228+ const oq = entry . q as any ;
229+ if ( queueIsHeld ( oq ) ) {
230+ deferHeldStructural ( entry as any , oq , item ) ;
231+ continue ;
232+ }
233+ if ( item . si === undefined ) entry . dg = drainGen ;
234+ try {
235+ structuralResync ( entry , item ) ;
236+ } catch ( err ) {
237+ if ( ! routeEntryError ( entry as any , err ) && firstError === UNSET ) firstError = err ;
238+ }
239+ }
240+ return firstError ;
241+ }
242+
243+ function runLateSweeps ( firstError : unknown ) : unknown {
244+ const m = sweeps ;
245+ sweeps = null ;
246+ if ( m === null ) return firstError ;
247+ for ( const [ pc , s ] of m ) {
248+ if ( s . row !== null ) {
249+ const live = pc . ro as ( RowOpsEntry & { sq ?: number } ) [ ] | null ;
250+ if ( live !== null && live . length !== 0 )
251+ firstError = sweepList ( live as any , s , s . row , firstError ) ;
252+ }
253+ if ( s . slots !== null ) {
254+ const live = pc . sp as ( RowOpsEntry & { sq ?: number } ) [ ] | null ;
255+ if ( live !== null && live . length !== 0 ) {
256+ for ( let i = 0 ; i < s . slots . length ; i ++ ) {
257+ firstError = sweepList ( live as any , s , s . slots [ i ] , firstError ) ;
258+ }
259+ }
260+ }
261+ }
262+ return firstError ;
263+ }
264+
177265/** Drain-side `next` resolution (structural audit F2): live targets read
178266 * the VISIBLE view at drain time. (The old-contract superseded-work
179267 * generation gate lived here; the #3164 fold ruling removed landing-time
@@ -253,49 +341,11 @@ function applyStructural(item: QueuedApply, next: any, firstError: unknown): unk
253341 if ( ! routeEntryError ( entry as any , err ) && firstError === UNSET ) firstError = err ;
254342 }
255343 }
256- // LATE REGISTRANTS (round 10.13, P1; mechanics rebuilt by the structural
257- // audit): a consumer that registered between emission and the drain is
258- // outside the emission snapshot — baseline-relative ops would corrupt
259- // it, silence left held-window registrants permanently stale. It takes
260- // the RESYNC form against live state. THE WINDOW IS FIXED (F1): only
261- // registrants with `sq` in (item.rq, drain-start rq] — a consumer
262- // registering DURING this drain initialized from current state and gets
263- // nothing. Registrations append in `sq` order, so late entries are a
264- // SUFFIX: the tail scan is O(#late), not O(consumers²) (F6). Held owner
265- // queues defer exactly like the snapshot path (F1) — never through the
266- // hold.
267- if ( item . pc !== undefined ) {
268- const live = ( item . si !== undefined ? item . pc . sp : item . pc . ro ) as
269- | ( RowOpsEntry & { hq ?: boolean ; sq ?: number ; dg ?: number ; q ?: unknown } ) [ ]
270- | null ;
271- if ( live !== null && live . length !== 0 ) {
272- const itemRq = ( ( item . rq as number ) | 0 ) as number ;
273- for ( let j = live . length - 1 ; j >= 0 ; j -- ) {
274- const entry = live [ j ] ;
275- const sq = ( entry . sq as number ) | 0 ;
276- if ( sq <= itemRq ) break ; // suffix exhausted — everyone else was in the snapshot
277- if ( sq > maxSq ) continue ; // registered mid-drain: outside the window
278- if ( entry . u === true || entry . hq === true ) continue ;
279- // ONE live rebuild per entry per drain (audit follow-up P2): every
280- // held item on this channel runs this sweep — the resync reads the
281- // same live truth each time, so repeats are pure waste. Slot items
282- // stay per-item (distinct indices are distinct deliveries).
283- if ( item . si === undefined && entry . dg === drainGen ) continue ;
284- if ( entry . owner !== null && isDisposed ( entry . owner ) ) continue ;
285- const oq = entry . q as any ;
286- if ( queueIsHeld ( oq ) ) {
287- deferHeldStructural ( entry as any , oq , item ) ;
288- continue ;
289- }
290- if ( item . si === undefined ) entry . dg = drainGen ;
291- try {
292- structuralResync ( entry , item ) ;
293- } catch ( err ) {
294- if ( ! routeEntryError ( entry as any , err ) && firstError === UNSET ) firstError = err ;
295- }
296- }
297- }
298- }
344+ // LATE REGISTRANTS: recorded here, swept ONCE at DRAIN END (fold audit
345+ // P1 — the per-item sweep resynced an entry to the LIVE view and then a
346+ // LATER item's real ops re-applied on top, double-building rows). See
347+ // noteSweep/runLateSweeps.
348+ noteSweep ( item , maxSq ) ;
299349 // Structural deliveries are attribution EVENTS (round 10.12, P2): they
300350 // run in commit drains, not effects, so no rerun event exists for them
301351 // — the engine records a synthetic one (name, causes, count, timing).
@@ -328,13 +378,34 @@ function structuralResync(entry: { fn: Function }, item: QueuedApply): void {
328378}
329379
330380/** Deferred structural re-apply for a held consumer (round 10.13): runs
331- * FROM its owner queue at release, one queued run per entry per hold
332- * window, always in the live resync form. */
381+ * FROM its owner queue at release, always in the live resync form. Row
382+ * consumers dedup to one queued run per hold window (the resync reads
383+ * live truth — repeats are pure waste); SLOT consumers dedup PER INDEX
384+ * (fold audit P1): distinct indexes are distinct deliveries — a shared
385+ * flag collapsed multi-slot batches to the first index, leaving the rest
386+ * permanently stale. */
333387function deferHeldStructural (
334- entry : { fn : Function ; owner : Owner | null ; u ?: boolean ; hq ?: boolean } ,
388+ entry : { fn : Function ; owner : Owner | null ; u ?: boolean ; hq ?: boolean ; hqs ?: Set < number > } ,
335389 oq : any ,
336390 item : QueuedApply
337391) : void {
392+ if ( item . si !== undefined ) {
393+ const si = item . si ;
394+ const set = ( entry . hqs ??= new Set < number > ( ) ) ;
395+ if ( set . has ( si ) ) return ;
396+ set . add ( si ) ;
397+ oq . enqueue ( EFFECT_RENDER , ( ) => {
398+ set . delete ( si ) ;
399+ if ( entry . u === true ) return ;
400+ if ( entry . owner !== null && isDisposed ( entry . owner ) ) return ;
401+ try {
402+ structuralResync ( entry , item ) ;
403+ } catch ( err ) {
404+ if ( ! routeEntryError ( entry as any , err ) ) deferHalt ( err ) ;
405+ }
406+ } ) ;
407+ return ;
408+ }
338409 deferIntoQueue ( entry , oq , ( ) => {
339410 try {
340411 structuralResync ( entry , item ) ;
@@ -578,12 +649,12 @@ function drainOptimistic(): void {
578649 drainGen ++ ;
579650 for ( let i = 0 ; i < q . length ; i ++ ) {
580651 const item = q [ i ] ;
581- // Same superseded-work gate as the regular drain (structural audit, F4).
582652 const next = drainNext ( item ) ;
583653 if ( next === UNSET ) continue ;
584654 if ( item . ops !== undefined || item . si !== undefined )
585655 firstError = applyStructural ( item , next , firstError ) ;
586656 }
657+ firstError = runLateSweeps ( firstError ) ;
587658 if ( firstError !== UNSET ) {
588659 haltReactivity ( firstError ) ;
589660 throw firstError ;
0 commit comments