Skip to content

Commit a4c439b

Browse files
fix: fold-audit round — per-index slot defers, drain-end sweep, reveal coverage
Four P1s at b31e992, harness-first: - Held slot deliveries defer PER INDEX (entry.hqs): the shared hq flag collapsed multi-slot batches to the first index behind a hold. - Late-registrant sweep moved to DRAIN END with a per-channel highest- emission watermark: the per-item sweep resynced to live state and a later item's baseline ops re-applied on top, double-building rows. Audience unchanged: exactly the entries no snapshot reached, still a suffix tail scan. - stagedApply descends only through live draft proxies (stagedChild): recursing into a SHALLOW store's raw rows mutated committed truth in place — visible before the reveal, notified to no one. Raw children replace their slot wholesale. - Staged-truth folds emit at the reveal: t.sf marks optimistic-family drafts written under the authoritative posture (the staging bracket); both fold-site row-ops gates exempt it, and a slot-diff twin ticks changed slots. The old opt-family gates cover override materializations only — a root array retained through a DESCENDANT override is not overlaid, so the settle loop's resync never reached it. Full signals (1,511 + 2 expected-fail) + web (719) green; four size ratchets with notes (15.2/27.35/16.9/19.45). Co-authored-by: Cursor <[email protected]>
1 parent 68087b5 commit a4c439b

8 files changed

Lines changed: 437 additions & 62 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Close the fold-audit P1s on the patch channel and the #3164 reveal seam: held slot deliveries defer per index instead of collapsing to the first (later slots stayed stale behind holds), the late-registrant sweep runs once at drain end against the highest emission watermark (a resync followed by a later item's stale ops double-built rows), staged truth no longer merges into raw shallow rows (in-place mutation was visible before the reveal with no notification — raw children now replace their slot wholesale), and staged-truth folds emit row ops and slot ticks at the reveal (the optimistic-family gates exist for override materializations; a root array retained only through a descendant's override was revealing silently).

‎packages/signals/AUDIT-BRIEF-R6.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
# Audit brief — rounds 6–9 + patch-mode default flip + node delivery
22

3+
## Round 10.18 (2026-09-01) — fold audit (4 P1s: slots under holds, sweep ordering, reveal coverage)
4+
5+
Audit at b31e9929 confirmed the consecutive-landing divergence resolved by
6+
the fold integration and found four P1s — two channel-side, two in the
7+
fold reveal seam. All fixed, harness-first:
8+
9+
- **P1 per-index held-slot defers**: deferIntoQueue's shared `hq` flag
10+
collapsed a multi-slot batch to its first index — later indexes stayed
11+
permanently stale behind a hold. Slot items now dedup per (entry,
12+
index) via `entry.hqs`; row items keep the single-run dedup (their
13+
resync reads live truth — repeats are waste).
14+
- **P1 drain-END late sweep**: the per-item sweep resynced a late
15+
registrant to the LIVE view and then a LATER item — whose emission
16+
snapshot legitimately included that entry — re-applied baseline-
17+
relative ops on top, double-building the row. The sweep now runs once
18+
per drain per channel against the HIGHEST emission watermark: entries
19+
at/below `maxRq` rode a snapshot (real, baseline-sound ops); the sweep
20+
covers exactly (maxRq, winEnd] — still a suffix tail scan.
21+
- **P1 staged descent never mutates raws**: stagedApply recursed into
22+
children READ THROUGH the draft — for SHALLOW stores that read returns
23+
the raw row, and the merge mutated committed truth in place: visible
24+
to every reader BEFORE the reveal, notified to no one. `stagedChild`
25+
gates descent on the child being a live draft proxy; raw children
26+
replace their slot wholesale (parks and reveals like any slot write).
27+
- **P1 reveal coverage for structural channels**: both fold-site row-ops
28+
emissions gated on `t.fam?.opt !== true` — right for OVERRIDE
29+
materializations (lane channel), wrong for STAGED TRUTH folding at the
30+
reveal: a root array whose retention came from a DESCENDANT override
31+
is not overlaid, so the settle loop's resync never reached it and the
32+
gate silenced the fold's own ops. `t.sf` (set in ensurePB when an
33+
optimistic-family draft is written under the authoritative posture —
34+
exactly the staging bracket) exempts staged folds, and a slot-diff
35+
twin ticks changed slots (staged reveals bypass every walk).
36+
- Size: +40/+52/+180/+110 B across the four affected tiers (sweep
37+
bookkeeping is most of it) — four ratchets with notes.
38+
339
## Round 10.17 (2026-09-01) — #3164 FOLD reconciliation (rebase onto the re-ruled contract)
440

541
Upstream re-ruled landing consumption (a536e29b): the three interim #3123

‎packages/signals/src/store/next/optimistic.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,19 @@ function stampPendingNodes(txn: Transition): void {
331331
}
332332
}
333333

334+
/** Staged descent guard (fold audit P1): recursion may only continue
335+
* through a live draft PROXY — a raw child (a SHALLOW store's row) has no
336+
* staging trap under it, and merging into it would mutate committed truth
337+
* in place: visible to every reader BEFORE the reveal, with no value/slot
338+
* notification ever. Raw children replace their slot wholesale instead —
339+
* the staged slot write parks and reveals like any other. */
340+
function stagedChild(cur: any, key: PropertyKey): any | undefined {
341+
const child = cur[key];
342+
return child !== null && typeof child === "object" && (child as any)[$TARGET] !== undefined
343+
? child
344+
: undefined;
345+
}
346+
334347
/** Keyed identity-preserving deep merge through live draft proxies — the
335348
* staged twin of the adoption walk. Reads see the pending backing (staged
336349
* view), so consecutive landings during one hold compose; key-matched rows
@@ -393,7 +406,12 @@ function stagedApply(cur: any, incoming: any, keyFn: KeyFn | null): void {
393406
}
394407
if (matched !== undefined) {
395408
if (unwrapValue(cur[i]) !== matched) cur[i] = matched;
396-
stagedApply(cur[i], nv, keyFn);
409+
const child = stagedChild(cur, i);
410+
if (child !== undefined) stagedApply(child, nv, keyFn);
411+
else {
412+
const pv = unwrapValue(cur[i]);
413+
if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[i] = nv;
414+
}
397415
} else {
398416
const pv = unwrapValue(cur[i]);
399417
if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[i] = nv;
@@ -404,9 +422,11 @@ function stagedApply(cur: any, incoming: any, keyFn: KeyFn | null): void {
404422
const nv = incoming[i];
405423
const pv = unwrapValue(cur[i]);
406424
if (pv === nv) continue;
407-
if (isWrappable(nv) && isWrappable(pv) && Array.isArray(nv) === Array.isArray(pv))
408-
stagedApply(cur[i], nv, keyFn);
409-
else if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[i] = nv;
425+
if (isWrappable(nv) && isWrappable(pv) && Array.isArray(nv) === Array.isArray(pv)) {
426+
const child = stagedChild(cur, i);
427+
if (child !== undefined) stagedApply(child, nv, keyFn);
428+
else if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[i] = nv;
429+
} else if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[i] = nv;
410430
}
411431
}
412432
if (cur.length !== len) cur.length = len;
@@ -430,7 +450,9 @@ function stagedApply(cur: any, incoming: any, keyFn: KeyFn | null): void {
430450
continue;
431451
}
432452
}
433-
stagedApply(cur[k], nv, keyFn);
453+
const child = stagedChild(cur, k);
454+
if (child !== undefined) stagedApply(child, nv, keyFn);
455+
else if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) cur[k] = nv;
434456
} else if (!isEqual(pv, nv) && !targetsEqual(pv, nv)) {
435457
cur[k] = nv;
436458
}

‎packages/signals/src/store/next/patch.ts‎

Lines changed: 122 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
175175
let 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. */
333387
function 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

Comments
 (0)