@@ -102,6 +102,27 @@ import {
102102import { attrHooks } from "./attribution-hooks.js" ;
103103import { devTrackHeldPending } from "./invariants.js" ;
104104import { cleanup , disposeChildren , inheritId , linkChild , markDisposal } from "./owner.js" ;
105+ // The reader seat's shadow (S0): reachable only under `__TEST__`
106+ // (documentation/plans/reader-seat-rebuild.md §3).
107+ import {
108+ RAISE_FL ,
109+ RAISE_H ,
110+ SLOT_C ,
111+ SLOT_E ,
112+ SLOT_NONE ,
113+ SLOT_NR ,
114+ SLOT_P ,
115+ SLOT_SNAPSHOT ,
116+ shadowActual ,
117+ shadowExternalRaise ,
118+ shadowConsumeBypass ,
119+ shadowEnter ,
120+ shadowExpect ,
121+ shadowLeave ,
122+ shadowSeatClose ,
123+ shadowSeatOpen ,
124+ shadowThrown
125+ } from "./seat.js" ;
105126import {
106127 notifyEpoch ,
107128 bumpNotifyEpoch ,
@@ -350,6 +371,14 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
350371 // content creates is that mount's too (#4166). Lanes only, like the seat.
351372 const prevMount = laneRoot ;
352373 const mount = laneStageHook && ( laneRoot = create && ( prevLane || prevMount ) ) ;
374+ // Shadow seat (S0): the node's own lane, as the seating below spells it.
375+ const shadowOwnLane = __TEST__
376+ ? ( el . _flags & REACTIVE_LANE_DIRTY ||
377+ ( el . _config & ( CONFIG_OVERRIDE | CONFIG_GUESS ) ) === CONFIG_OVERRIDE ) &&
378+ el . _x ?. _transaction ?. _lane
379+ ? el . _x . _transaction
380+ : null
381+ : null ;
353382 setPassLane (
354383 ! laneStageHook
355384 ? null
@@ -482,6 +511,18 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
482511 ! el . _x . _reask
483512 ) ;
484513
514+ // Shadow seat (S0): opened beside the seating at the head — after the
515+ // held re-pass arm above, which is the pass's own hold work — and
516+ // compared to it (documentation/plans/reader-seat-rebuild.md §3.2).
517+ if ( __TEST__ )
518+ shadowSeatOpen (
519+ el ,
520+ create ,
521+ creatorPass ( context ) ,
522+ shadowOwnLane ,
523+ mount ,
524+ ! ! ( ( creatorPass ( context ) ?. _flags as number ) & REACTIVE_JOINED )
525+ ) ;
485526 const oldcontext = context ;
486527 context = el ;
487528 el . _depsTail = null ;
@@ -605,6 +646,16 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
605646 : 0
606647 : REACTIVE_JOINED )
607648 ) ;
649+ // Shadow seat (S0): the born-held decision beside the old one, then
650+ // the seat is restored.
651+ if ( __TEST__ )
652+ shadowSeatClose (
653+ el ,
654+ ! ! isEffect ,
655+ joined ,
656+ ! ! ( ( creatorPass ( oldcontext ) ?. _flags as number ) & REACTIVE_JOINED ) ,
657+ mount
658+ ) ;
608659 // A verdict reader is one for as long as it probes, or reads a memo
609660 // that does (`read`): a pass that did neither (REACTIVE_PROBED, the same
610661 // bit) is an ordinary derivation again.
@@ -1129,6 +1180,7 @@ export function ext(el: { _x: NodeExtension | null }): NodeExtension {
11291180 * boundary's, not the tick's (A29's boundary exemption, #3540). */
11301181export function joinPass ( c : Computed < any > , el : Signal < any > | Computed < any > ) : void {
11311182 c . _flags |= REACTIVE_JOINED ;
1183+ if ( __TEST__ ) shadowExternalRaise ( c , RAISE_H ) ;
11321184 // A new memo's first pass is the hold's work (born held) but not the
11331185 // tick's: a render effect that reads it decides — under a boundary that
11341186 // catches it, the boundary's; otherwise the tick joins the hold (#3540:
@@ -1148,6 +1200,7 @@ export function joinPass(c: Computed<any>, el: Signal<any> | Computed<any>): voi
11481200 * `foldedRead`, D-34) — a reader is never a render effect there. */
11491201export function joinPassAt ( c : Computed < any > , t : Transaction ) : void {
11501202 c . _flags |= REACTIVE_JOINED ;
1203+ if ( __TEST__ ) shadowExternalRaise ( c , RAISE_H ) ;
11511204 joinPassTx ( t , ( ! ( c as any ) . _type && c . _statusFlags & STATUS_UNINITIALIZED ) || freshHook ?.( c ) ) ;
11521205}
11531206
@@ -1833,6 +1886,10 @@ export function pullComputed(owner: Computed<any>, c: Computed<any>): void {
18331886}
18341887
18351888export function read < T > ( el : Signal < T > | Computed < T > ) : T {
1889+ // Shadow (S0): every read is run through `shadowRead`, which re-enters
1890+ // here once (the bypass) and compares the slot the table predicts with
1891+ // the one the deciders below chose.
1892+ if ( __TEST__ && ! shadowConsumeBypass ( ) ) return shadowRead ( el ) ;
18361893 if ( __DEV__ ) DEV . hooks . onRead ?.( el ) ;
18371894 if ( __DEV__ && ! tracking ) checkForeignRead ( ) ;
18381895 if ( __DEV__ && asyncTailFlights !== 0 && ! tracking && untrackDepth === 0 )
@@ -1869,6 +1926,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
18691926 ! ( el . _config & CONFIG_OVERRIDE ) && // lanes: the slow path's one arm
18701927 ( ! __DEV__ || ! strictRead )
18711928 ) {
1929+ if ( __TEST__ ) shadowExpect ( c as Computed < any > | null , el , tracking , 0 , spectating , false ) ;
18721930 if ( c ) {
18731931 if ( tracking ) link ( el , c as Computed < any > ) ;
18741932 // L2: a pass that reads something already in the future joins it —
@@ -1882,22 +1940,30 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
18821940 // future (REACTIVE_JOINED): a first pass that did is born held (A29),
18831941 // wherever it was created. Lane work sees the screen (`frameRead`).
18841942 if ( el . _config & CONFIG_HELD && ! ( c . _config & CONFIG_CHILDREN_FORBIDDEN ) ) {
1885- if ( frameRead ( c as Computed < any > , el ) ) return el . _value as T ;
1943+ if ( frameRead ( c as Computed < any > , el ) ) {
1944+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
1945+ return el . _value as T ;
1946+ }
18861947 joinPass ( c as Computed < any > , el ) ;
18871948 }
18881949 }
18891950 // Committed visibility for untracked and children-forbidden readers.
1890- if ( ! c || el . _pendingValue === NOT_PENDING || c . _config & CONFIG_CHILDREN_FORBIDDEN )
1951+ if ( ! c || el . _pendingValue === NOT_PENDING || c . _config & CONFIG_CHILDREN_FORBIDDEN ) {
1952+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
18911953 return el . _value as T ;
1954+ }
18921955 // (A plain signal is never uninitialized — the carve-out is `serve`'s.)
18931956 // Lane work over a folded lane (D-34), once the frame has a
18941957 // transaction, reads the screen too.
18951958 if (
18961959 ( ( observeFlightHook && c . _config & CONFIG_VERDICT ) || ( laneStageHook && passLane ?. folded ) ) &&
18971960 stagedScreen ( c as Computed < any > )
1898- )
1961+ ) {
1962+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
18991963 return el . _value as T ;
1964+ }
19001965 stagedRead ( c as Computed < any > ) ;
1966+ if ( __TEST__ ) shadowActual ( SLOT_P ) ;
19011967 return el . _pendingValue as T ;
19021968 }
19031969
@@ -1924,6 +1990,18 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
19241990 if ( ! ( c as any ) . _type ) ( c as Computed < any > ) . _flags |= owner . _config & CONFIG_VERDICT ;
19251991 }
19261992 }
1993+ if ( __TEST__ )
1994+ shadowExpect (
1995+ c as Computed < any > ,
1996+ el ,
1997+ tracking ,
1998+ 0 ,
1999+ spectating ,
2000+ snapshotCaptureActive &&
2001+ ( ( c as Computed < any > ) . _config & CONFIG_IN_SNAPSHOT_SCOPE ) !== 0 &&
2002+ el . _x ?. _snapshotValue !== undefined &&
2003+ ! ( owner . _statusFlags & STATUS_ERROR )
2004+ ) ;
19272005 // L2, as in the fast block above (tracked or not). A node born into the
19282006 // future (uninitialized) has no committed value: every reader derives
19292007 // from the future there, a render effect included (A29: a stale reader
@@ -1937,7 +2015,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
19372015 joinPass ( c as Computed < any > , el ) ;
19382016 else committed = true ;
19392017 }
1940- }
2018+ } else if ( __TEST__ ) shadowExpect ( null , el , tracking , 0 , spectating , false ) ;
19412019 // Lanes: a lane's node (after the pull — the node is current). NOT_PENDING
19422020 // falls through: lane work's read of a pending member throws like any.
19432021 // (CONFIG_OVERRIDE is the lane engine's: no engine, no lane's node.)
@@ -1966,6 +2044,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
19662044 const snapshot = sv === NO_SNAPSHOT ? undefined : sv ;
19672045 const current = el . _pendingValue !== NOT_PENDING ? el . _pendingValue : el . _value ;
19682046 if ( current !== snapshot ) ( c as Computed < any > ) . _flags |= REACTIVE_SNAPSHOT_STALE ;
2047+ if ( __TEST__ ) shadowActual ( SLOT_SNAPSHOT ) ;
19692048 return snapshot as T ;
19702049 }
19712050 }
@@ -2047,11 +2126,15 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
20472126 // - owner._time < clock: only on a later cycle than the one the error was found
20482127 if ( tracking && ( owner as Computed < any > ) . _time < clock ) {
20492128 recompute ( owner as Computed < unknown > ) ;
2129+ if ( __TEST__ ) shadowActual ( SLOT_E ) ;
20502130 return read ( el ) ;
20512131 } else throw ( owner as Computed < any > ) . _x ?. _error ;
20522132 }
20532133
2054- if ( committed ) return el . _value as T ;
2134+ if ( committed ) {
2135+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
2136+ return el . _value as T ;
2137+ }
20552138 // D-41: an untracked read reads the screen, and a shown error is on it
20562139 // until the answer that replaces it commits.
20572140 if ( ! c && el . _x ?. _shownError ) throw el . _x . _shownError ;
@@ -2099,22 +2182,47 @@ export function serve(el: Signal<any> | Computed<any>, c: Computed<any> | null):
20992182 const u = c && unflushedStaged ? unflushedValue ( el ) : NOT_PENDING ;
21002183 if ( u !== NOT_PENDING ) {
21012184 markLateLinker ( c ! ) ;
2185+ // (A held node's stash is its flushed staging: the staging's slot.)
2186+ if ( __TEST__ ) shadowActual ( el . _config & CONFIG_HELD ? SLOT_P : SLOT_C ) ;
21022187 return u ;
21032188 }
2104- if ( readerSeesCommitted ( el , c ) ) return el . _value ;
2189+ if ( readerSeesCommitted ( el , c ) ) {
2190+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
2191+ return el . _value ;
2192+ }
21052193 if (
21062194 observeFlightHook &&
21072195 c ! . _config & CONFIG_VERDICT &&
21082196 ! ( ( el as Computed < any > ) . _statusFlags & STATUS_UNINITIALIZED ) &&
21092197 stagedScreen ( c ! )
2110- )
2198+ ) {
2199+ if ( __TEST__ ) shadowActual ( SLOT_C ) ;
21112200 return el . _value ;
2201+ }
21122202 stagedRead ( c ! , el ) ;
21132203 // D-41: the staged answer is an error (`stageError`).
21142204 if ( el . _config & CONFIG_STAGED_ERROR ) throw el . _pendingValue ;
2205+ if ( __TEST__ ) shadowActual ( SLOT_P ) ;
21152206 return el . _pendingValue ;
21162207}
21172208
2209+ /** Shadow (S0, `seat.ts`): run `read` once for real and compare the slot it
2210+ * served with the table's prediction recorded at the decision point. A
2211+ * throw is a slot too: NotReady is NR, anything else E. */
2212+ function shadowRead < T > ( el : Signal < T > | Computed < T > ) : T {
2213+ shadowEnter ( ) ;
2214+ let thrown = SLOT_NONE ;
2215+ try {
2216+ return read ( el ) ;
2217+ } catch ( e ) {
2218+ thrown = e instanceof NotReadyError ? SLOT_NR : SLOT_E ;
2219+ shadowThrown ( e ) ;
2220+ throw e ;
2221+ } finally {
2222+ shadowLeave ( thrown ) ;
2223+ }
2224+ }
2225+
21182226/** A pass read a staging of this flush: it derives from what the flush may
21192227 * yet hold (REACTIVE_STAGED_READ — the seam decides). Lane work too (plan sec. 28, a
21202228 * lane sees the screen plus its own guesses) — a folded lane's only before
@@ -2133,6 +2241,7 @@ export function serve(el: Signal<any> | Computed<any>, c: Computed<any> | null):
21332241export function stagedRead ( c : Computed < any > , el ?: Signal < any > | Computed < any > ) : void {
21342242 if ( ! ( ( el as Computed < any > | undefined ) ?. _statusFlags ! & STATUS_UNINITIALIZED ) ) {
21352243 c . _flags |= REACTIVE_STAGED_READ ;
2244+ if ( __TEST__ ) shadowExternalRaise ( c , RAISE_FL ) ;
21362245 if ( laneSeamsHook && passLane !== null ) stagedReaders . push ( c ) ;
21372246 }
21382247}
0 commit comments