Skip to content

Commit b6d266e

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(signals): store twins of the lane-authority fixes; late readers of a hold re-derive at the commit (#3330, #3331)
Every rule pinned for signals in #3335/#3334/#3330/#3331 is pinned against the equivalent optimistic-store shape (tests/store/lane-authority-twins). #3335 and #3334 already held; #3330 and #3331 did not. #3330 store twin: an adoption under a live transaction now holds on optimistic families too — a sync derive adopting truth under the action's transaction is held truth, not lane business; unheld, handlers read the swapped-in backing early, latest() inverted, and the optimistic write compared equal to it and wrote no override. `notifyOptimisticWrites` judges the write against the view readers see (`heldMaskView(t) ?? t.v`). A held adoption notifies its nodes at the outermost setter exit (`stageHeldAdoptions`), staged under the transaction, so the commit promotes silently instead of delivering the adopted values as fresh writes that re-ran every subscriber against a frame the lane had already published. Plain-store reconcile inside an action holds the same way; a key first read under a held adoption is born holding (one `stageHeldKey` with the #3336 fold case: committed from the view readers see, the held value staged). #3331 store twin: the authoritative landing on an override-covered node (setSignal under projectionWriteActive) dispatches to the engine (`_landOnOverride` → supersedeOverride) instead of staging silently on the plain path; `supersededRead` serves the committed truth once a mainline landing has committed ahead of the override's revert. Core, surfaced by the store twin and reproduced with a plain signal: a reader that first links to a node while another transaction holds a staged write read the committed value and never learned of the commit (commits are silent; the staging walk was the notification). `heldFromStale` records such readers for the transaction's commit replay, skipping effects the transaction itself computed (parked run / contested re-derive) — replaying those published the frame twice. On the A28 write path the async landing's supersession decision runs at the write's promotion (promoteUnflushed's override arm hands the node to supersedeOverride, still under the flight's provenance). A settle that reverts optimism re-derives its contested effects (#3322) after the revert, with the gated replay: ahead of the heap run the truth is committed but the overrides still display, and the re-derive composed the two — the #3164 tear, surfaced by deep() over an optimistic store whose held adoption the committing transaction's own readers see. Core floor 22,252 → 22,638 (conscious). Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 3292328 commit b6d266e

11 files changed

Lines changed: 723 additions & 60 deletions
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+
Store twins of the lane-authority fixes. An optimistic store's optimistic write after a `yield` now reveals on its lane with its derivations when the action's transaction already holds the same truth (#3330 store twin): adoptions under a live transaction hold on optimistic families too, a held adoption notifies its nodes at write time so the commit promotes silently instead of re-running every subscriber, and a tentative write is judged against the view readers see rather than the swapped-in backing. A derived optimistic store's own truth landing a different value over a tentative edit supersedes the override for the graph now, with action provenance (#3331 store twin): the authoritative landing reaches the engine's supersession, and a tracked reader of a superseded node reads the committed truth once the landing has committed ahead of the override's revert. Surfaced alongside and fixed in core: a reader that first links to a node while another transaction holds a staged write (an effect created during the hold, a store key first read under it) read the committed value but never learned of the commit — such readers now re-derive when the transaction reveals. Plain-store `reconcile` inside an action, and store keys first read under a held adoption, hold like every other write: handlers read committed, `latest()` the staged value, and the reveal comes with the transaction.

‎packages/signals/docs/INTERNALS-ASYNC-STATE.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ Semantics of the `(_pendingValue, _overrideValue)` pair for an optimistic node
6868
refetch landing after a later action's override on the same node — the two transactions have
6969
merged, so `_overrideOwner` cannot tell them apart) and returns before setting the bit: staged,
7070
held to the commit, unobservable. `origin === 0` (mainline) is never stale.
71+
**Store twin:** a derived optimistic store's truth lands on its nodes through `setSignal`
72+
under `projectionWriteActive` (the authoritative posture); on an override-covered node that
73+
dispatches to `GlobalQueue._landOnOverride` (optimistic.ts) — stage the truth whatever its
74+
relation to the committed value (a landing equal to committed still differs from the override),
75+
sync companions, then `supersedeOverride`. `supersededRead` serves the truth staged OR committed:
76+
a mainline landing commits at the head of its flush, ahead of the heap run, and the override
77+
drops only at the batch's end — in that window the graph must not fall back to the override it
78+
has left (the store's async-derive landing reaches it; a node's `asyncWrite` rides its flight's
79+
transaction and never does). Pinned in `tests/store/lane-authority-twins.test.ts`.
7180

7281
## 2. Lanes (`lanes.ts`)
7382

@@ -76,6 +85,7 @@ Semantics of the `(_pendingValue, _overrideValue)` pair for an optimistic node
7685
- Lane lifecycle: created on optimistic write → nodes join via `insertSubs(node, true)` → `assignOrMergeLane` → lane-routed effects run when the lane is not held (`runLaneEffects` → `laneHeld`) → cleaned up by `cleanupCompletedLanes` when the owning transition completes (or when orphaned, `_transition === null`).
7786
- `_pendingAsync` add/delete sites: added in `recompute`'s async catch under a lane (core.ts ~264), removed on async resolution (`asyncWrite`, async.ts ~214) and on lane-corrected recompute (core.ts ~254). The set records the async the lane _owns_, not what holds it.
7887
- Replay gating (#3330): `laneReadsCommitted` hands a lane reader the committed `_value` of a staged node and records the reader in the batch's `_gatedSubs` for a re-run at commit — only when `_pendingValue !== _value`. A lane recompute that already published the value (INV-11) leaves the two equal; recording the reader anyway replayed its effects against an unchanged frame.
88+
- Late readers of a transaction hold (#3330 store twin → general): the stale-reader term of `read()`'s value selections (`heldFromStale`, core.ts — the fast paths and the slow path) serves a render effect the committed `_value` of a node another live transaction staged, and records the reader in that transaction's `_gatedSubs`. The commit is silent (the staging walk was the notification), so a reader that linked after the walk — an effect created during the hold, a store key first read under it — would otherwise show the old value past the reveal. An effect the transaction itself computed (`_valueTransition` resolves to it) is not recorded: it re-derives at the commit through its parked run or the contested re-derive (#3322), and a replay would publish the frame twice. Pinned in `tests/spec-async-semantics.test.ts` ("a reader that links to a held node during the hold").
7989
- Hold rule (`laneHeld`, #3289; per-node lookup #3335): a lane is held iff some `_pendingAsync` node is in the `_asyncReporters` of **any live transaction** (`waitingTransition(node) !== null`) — i.e. a render effect observed it pending and no boundary consumed the status (INV-3, the one registration site). Not "its transaction's": lanes merge across transactions (#2912) and the merged root's transaction recorded only one member's observations. Same rule as `transitionComplete`: unrendered async and fallback-caught async hold nothing. The two facts arrive in either order (a node created by the lane's own reveal is observed first and stamped on a later re-ask), which is why the hold is a predicate over both records rather than a registration.
8090

8191
## 3. Transitions (`scheduler.ts`)
@@ -86,7 +96,7 @@ Semantics of the `(_pendingValue, _overrideValue)` pair for an optimistic node
8696
- `_pendingNodes` — nodes whose `_pendingValue` commits when the transition completes (`commitPendingNodes` → `commitPendingNode`).
8797
- `_optimisticNodes` — nodes whose override reverts at completion (`resolveOptimisticNodes`).
8898
- Incomplete-transition flush stashes queues (`stashQueues`) and continues with a fresh view; completion restores them, commits pending, reverts optimistic, replays `_gatedSubs`, cleans lanes.
89-
- `_contested` — effects whose single value slot was written under this transaction and then overwritten by another live transaction or by mainline (#3322). Effects are not shared state, so a shared effect never merges transactions (memos do, via their `_transition` stamp); instead `Effect._valueTransition` records which view produced `_value`, `recompute`, when that owner changes, registers the effect on every owed live transaction, and `finalizePureQueue` re-dirties them **before** its heap run so the re-derive and the effect phase land in the same pass — the other view's value is never published. Rules that fall out: a stale (render) reader with no transaction active is mainline and sees a foreign transaction's staged signal as committed (`read`'s fast path and `readNodeFast` apply `stale && el._transition !== null`, matching the slow path); a value computed mainline needs no protection (mainline publishes what it computes, and a transaction whose writes never touched the effect finds it still correct at commit).
99+
- `_contested` — effects whose single value slot was written under this transaction and then overwritten by another live transaction or by mainline (#3322). Effects are not shared state, so a shared effect never merges transactions (memos do, via their `_transition` stamp); instead `Effect._valueTransition` records which view produced `_value`, `recompute`, when that owner changes, registers the effect on every owed live transaction, and `finalizePureQueue` re-dirties them **before** its heap run so the re-derive and the effect phase land in the same pass — the other view's value is never published. Exception: a settle that reverts optimism (a non-empty `_optimisticNodes`) re-dirties them **after** `_resolveOptimistic`, with the gated replay — between `commitPendingNodes` and the revert the truth is committed but the overrides still display, and a re-derive there composes the two (the #3164 tear; the reveal wake sits post-revert for the same reason). The slot meanwhile holds the frame already on screen, so nothing new is published early. Rules that fall out: a stale (render) reader with no transaction active is mainline and sees a foreign transaction's staged signal as committed (`read`'s fast path and `readNodeFast` apply `stale && el._transition !== null`, matching the slow path); a value computed mainline needs no protection (mainline publishes what it computes, and a transaction whose writes never touched the effect finds it still correct at commit).
90100
- Finalize re-entry (#3319): `finalizePureQueue` can _enter_ a held transaction partway through — a store commit hook (`bumpDeep` on a node the transaction owns), a boundary `_checkSources` write, or a stamped recompute in its heap — and `initTransition` then adopts the batch being finalized. Two rules keep that consistent. **State:** finalize captures the batch it started with and, if `currentBatch` changed, commits/reverts nothing batch-derived (the entered transaction owns it now); a _completing_ transaction whose ambient batch was separate (the #2916 shape) still settles its own containers, since adoption never touched them. **Effects:** ownership. A run applies with the commit of the transaction that computed its value (`Effect._valueTransition`). The ordinary effect phase runs with `activeTransition` set only in a flush whose finalize entered one, so `runEffect` leaves runs owned by a still-held transaction queued — `_modified` stays set — for the next gate to stash with the owner, while everything computed mainline (the write that caused the flush) applies now. Lanes are exempt by construction: they apply their own effects ahead of their transaction (the optimistic view) and their runner ORs `LANE_RUN` into the `type` it passes; the creation-time immediate run in `effect()` passes it too. The exemption is keyed on the _effect_ still having a lane, not on the runner: after a supersession demotes the cascade (#3331, §1), a `LANE_RUN` runner reaching a now lane-less effect whose value was computed under a still-held transaction leaves it queued like any owned run — otherwise the lane would apply the corrected derivation ahead of the commit that is supposed to reveal it. Known residue: writes staged by a hook _before_ the entry are adopted (held) and, because finalize's heap runs after its hooks, their dependents recompute owner-stamped and park with them; an entry that happens _inside_ that heap can leave an earlier mainline-computed effect applied over an adopted source — narrow, and inherited from adoption rather than from this rule.
91101
- `transitionComplete`: prunes dead reporters (`reporterBlocksSource`), transition is done when no live reporter still blocks a pending source and no active-override node is blocked on someone else's async.
92102
- Reveal-hold (#3305, #3334): a render reader landing on a node with `STATUS_PENDING` **throws regardless of the node's `_transition` stamp** (`read`'s pending branch has no stale/foreign-transaction carve-out). The staged-_value_ rule above is separate and stands: a stale mainline reader sees a foreign transaction's staged signal as committed, but an in-flight async is never served as its pre-flight value — the stamp is pending-node bookkeeping and does not mean that transaction also holds the flight's inputs (they may be committed, lane-revealed, or held only by another reveal waiting on the same flight). The throw reaches `GlobalQueue.notify`, which opens a transaction for the reveal if none is active (#3305) and records the source as its reporter (INV-3); the reveal completes when the flight lands.

‎packages/signals/docs/INTERNALS-STORE-STATE.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@ there is nothing to diff.)
146146
graph, which is what makes post-reconcile `snapshot` free.
147147
- Commit hook: lane settle folds the winning lane value into raw, then the
148148
node returns to passthrough (no pending state retained).
149+
- **Adoption under a live transaction** (#3074, extended for the #3330 store
150+
twin): `adoptPB` swaps the backing eagerly but records a hold (`ht` = the
151+
transaction, `hv` = the pre-hold committed view) that committed-visibility
152+
readers are served through `heldMaskView` — on optimistic families too (a
153+
sync derive adopting truth under a transaction is held truth, not lane
154+
business; unheld, handlers read the swapped-in backing early and an
155+
optimistic write equal to it compared as a no-op, so no override and no
156+
lane formed). A held adoption's nodes are notified at the outermost setter
157+
exit (`stageHeldAdoptions`, the adoption twin of `notifyWrites`): staged
158+
under the transaction's batch, transition-stamped, subscribers recompute in
159+
that flush and park with the transaction, and the commit promotes silently
160+
— the drain, which for a parked transaction IS the commit, would otherwise
161+
deliver the adopted values as fresh writes and re-run every subscriber
162+
against a frame the lane already published. `ab` moves to the adopted
163+
backing (the view the nodes were last told, #3296) so the drain only
164+
path-copies. `notifyOptimisticWrites` judges a tentative write against the
165+
view readers see (`heldMaskView(t) ?? t.v`), not the backing slot. A plain
166+
store's eager adoption (`reconcile` in a setter) under a live transaction
167+
takes the same hold — its inline notify already staged the nodes; the
168+
backing must not show handlers what the tracked read masks. A key first
169+
read under a held adoption is **born holding** (`heldAdoptionTransition` /
170+
`stageHeldKey` in `getNode`): committed value from `hv`, the adopted value
171+
staged under the transaction — the adoption's notification ran before the
172+
node existed and the drain has nothing left to say. (The #3336 PR adds the
173+
same for setter-staged `pb` holds and makes the first tracked read serve
174+
the node's value; the two compose.)
149175

150176
## 4. Identity rules
151177

‎packages/signals/src/core/core.ts‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,26 @@ function unflushedView<T>(el: Signal<T> | Computed<T>): T {
13101310
return (staged !== undefined && staged !== NOT_PENDING ? staged : el._value) as T;
13111311
}
13121312

1313+
/**
1314+
* Stale-reader term of the value selections below: a render effect reading a
1315+
* node some OTHER live transaction has staged sees the committed value. The
1316+
* commit is silent — the staging walk was the notification — so a reader
1317+
* that linked AFTER that walk (an effect created during the hold, a store
1318+
* key first read under it) would show the old value past the reveal: record
1319+
* it for the transaction's commit replay (the `_gatedSubs` contract lanes
1320+
* already use). An effect the transaction itself computed re-derives at its
1321+
* commit on its own (parked run, or the contested re-derive, #3322) and is
1322+
* not recorded — replaying it too would publish the frame twice.
1323+
*/
1324+
function heldFromStale(el: Signal<any> | Computed<any>, c: Computed<any>): boolean {
1325+
const t = el._transition;
1326+
if (t === null || t === activeTransition) return false;
1327+
const txn = currentTransition(t);
1328+
const vt: Transition | null | undefined = (c as any)._valueTransition;
1329+
if (vt == null || currentTransition(vt) !== txn) txn._gatedSubs.add(c);
1330+
return true;
1331+
}
1332+
13131333
export function readNodeFast<T>(el: Signal<T>): T | typeof READ_SLOW {
13141334
if (
13151335
latestReadActive ||
@@ -1341,7 +1361,7 @@ export function readNodeFast<T>(el: Signal<T>): T | typeof READ_SLOW {
13411361
!c ||
13421362
el._pendingValue === NOT_PENDING ||
13431363
c._config & CONFIG_CHILDREN_FORBIDDEN ||
1344-
(stale && el._transition !== null)
1364+
(stale && heldFromStale(el, c as Computed<any>))
13451365
? el._value
13461366
: el._config & CONFIG_UNFLUSHED
13471367
? unflushedView(el)
@@ -1388,7 +1408,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
13881408
!c ||
13891409
el._pendingValue === NOT_PENDING ||
13901410
c._config & CONFIG_CHILDREN_FORBIDDEN ||
1391-
(stale && el._transition !== null)
1411+
(stale && heldFromStale(el, c as Computed<any>))
13921412
? el._value
13931413
: el._config & CONFIG_UNFLUSHED
13941414
? unflushedView(el)
@@ -1572,7 +1592,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
15721592
GlobalQueue._laneReadsCommitted!(el, owner, c as Computed<any>)) ||
15731593
el._pendingValue === NOT_PENDING ||
15741594
c._config & CONFIG_CHILDREN_FORBIDDEN ||
1575-
(stale && el._transition && activeTransition !== el._transition) ||
1595+
(stale && heldFromStale(el, c as Computed<any>)) ||
15761596
// A17 for HELD truth (#3164, see CONFIG_HELD_TRUTH): staged confirming
15771597
// truth — fold-staged onto an armed family, or entangle-stolen by an
15781598
// awaited until() — is masked from ordinary readers until its
@@ -1674,8 +1694,13 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
16741694
// _overrideValue slot (flagged by CONFIG_OPTIMISTIC — a masked read of the
16751695
// always-present config instead of a missing-property probe), and every
16761696
// module that installs one installs the engine first.
1677-
if (el._config & CONFIG_OPTIMISTIC && !projectionWriteActive)
1678-
return GlobalQueue._optimisticWrite!(el, v);
1697+
if (el._config & CONFIG_OPTIMISTIC) {
1698+
if (!projectionWriteActive) return GlobalQueue._optimisticWrite!(el, v);
1699+
// An authoritative store landing on an override-covered node: the store
1700+
// twin of asyncWrite's override branch, decided by the engine (#3331).
1701+
const o = el._x?._overrideValue;
1702+
if (o !== undefined && o !== NOT_PENDING) return GlobalQueue._landOnOverride!(el, v);
1703+
}
16791704

16801705
const currentValue = el._pendingValue === NOT_PENDING ? el._value : (el._pendingValue as T);
16811706

0 commit comments

Comments
 (0)