Skip to content

Commit e30fcc7

Browse files
ryansolidClaude via Cursor
andcommitted
fix(signals): a derivation's untracked read of a held store value is born held (A29 at the store's untracked paths)
Step 4 of DESIGN-CONSOLIDATION move 3b. Core read() serves a staged value to a deriving reader and enters its transaction in the same arm — an untracked read inside a memo is still that memo's read (context persists under untrack), so a mainline memo over a held signal is born held. The store's untracked paths — nodeValue, pendingBackingVisible, readSource's adoption-hold view — restated the value half and dropped the entry: a mainline memo's or user effect's untrack(() => s.n) PUBLISHED the action's unrevealed write. They now call enterStagedRead on the arm that serves the staged value; enterStagedRead takes a null node for a backing (the fold's transaction is passed). The two store tunnels (latest() for store keys, authoritativeServe) keep serving without entering, as before. Discovery: the posture matrix gains memoUntracked / effectUntracked reader kinds (621 -> 851 cells) — the kind that sees S4 and S5, and that shows two more store/signal divergences recorded, not ruled, as S6 (staged-ambient write read by a derivation created inside a foreign action) and S7 (optimistic override with the only reader gated away). Commits docs/DESIGN-CONSOLIDATION.md (dropped from next by c7ec862's squash accident) with a ledger of moves 3a/3b so far; the code citations now point at it. Gate: suite green (+14 pins); oracles identical; the 5-reader subset of the matrix identical to next; +1 B minified core, +130 B minified store; two brotli caps ratcheted. Co-authored-by: Claude via Cursor <[email protected]>
1 parent a3d1828 commit e30fcc7

8 files changed

Lines changed: 407 additions & 20 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+
A memo or user effect created on mainline whose untracked read (`untrack(() => s.n)`, `deep(s)`) is of a store key held by a live action is now born held (A29), as the same read of a signal is: the pass enters the action's transaction and publishes nothing until the action commits. Previously the store's untracked paths served the held value without entering, so a mainline memo published the action's unrevealed write to the screen. Covers keys with and without a node and `reconcile` adoptions held by an action.

‎packages/signals/docs/DESIGN-CONSOLIDATION.md‎

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,16 +1602,18 @@ function heldFromStale(el: Signal<any> | Computed<any>, c: Computed<any>): boole
16021602
let stagedEntry: Transition | null = null;
16031603

16041604
export function enterStagedRead(
1605-
el: Signal<any> | Computed<any>,
1606-
t: Transition | null | undefined = el._transition
1605+
el: Signal<any> | Computed<any> | null,
1606+
t: Transition | null | undefined = el!._transition
16071607
): void {
16081608
if (!t || t === activeTransition || pendingCheckActive) return;
16091609
// A companion (the latest() shadow, the isPending() verdict signal) is the
16101610
// engine's mirror of the flushed world — reading it, or being it, is an
16111611
// observation, not a derivation from the hold: latest(x) never enters x's
16121612
// transaction, and the shadow's own pass never enters either (it would
1613-
// flip activeTransition under the reader that pulled it).
1614-
if (el._x?._parentSource || (context as Computed<any> | null)?._x?._parentSource) return;
1613+
// flip activeTransition under the reader that pulled it). (`el` is null for
1614+
// a store backing served under a hold — no node, the transaction is the
1615+
// fold's.)
1616+
if (el?._x?._parentSource || (context as Computed<any> | null)?._x?._parentSource) return;
16151617
// Verdict machinery (GlobalQueue._verdictPull: companion creation and the
16161618
// latest()/isPending() pulls — the latest() shadow is created before it is
16171619
// marked optimistic, so the bit alone cannot tell) and optimistic nodes
@@ -1641,7 +1643,7 @@ export function enterStagedRead(
16411643
* node's COMMITTED value? One implementation of the rule the fast paths
16421644
* (readNodeFast, read's fast block) carry as their trivial ternary and that
16431645
* every slow site — read's tail, the store's backing selection, the lane and
1644-
* verdict arms — used to restate by hand (DESIGN-CONSOLIDATION, move 3b). In order:
1646+
* verdict arms — used to restate by hand (docs/DESIGN-CONSOLIDATION.md, move 3b). In order:
16451647
* - no reader at all (an untracked read) — the committed frame;
16461648
* - a reader under an optimistic lane the engine says reads committed
16471649
* (laneReadsCommitted: another lane's hold, #3460);
@@ -1746,7 +1748,7 @@ export function hasActiveOverride(el: Signal<any> | Computed<any>): boolean {
17461748
* an optimistic write is a write; until its flush no reader sees it). One
17471749
* implementation for read()'s override arm, the verdict channels
17481750
* (latestRead, computePendingState) and the store's selection
1749-
* (DESIGN-CONSOLIDATION, move 3b). */
1751+
* (docs/DESIGN-CONSOLIDATION.md, move 3b). */
17501752
export function visibleOverride(el: Signal<any> | Computed<any>): boolean {
17511753
return hasActiveOverride(el) && !unflushedOverride(el);
17521754
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
visibleOverride,
4141
readerSeesCommitted,
4242
recordStaleReplay,
43+
enterStagedRead,
4344
prepareComputed,
4445
read as readNode,
4546
READ_SLOW,
@@ -424,6 +425,17 @@ function staleReplay(txn: Transition): void {
424425
if (c !== null && !(c._config & CONFIG_CHILDREN_FORBIDDEN)) recordStaleReplay(txn, c);
425426
}
426427

428+
/** The other half of serving a held backing: a deriving reader (a pass in
429+
* owner context) served the pending backing a live transaction holds
430+
* derives from that transaction's world and enters it (A29, core
431+
* enterStagedRead on the same arm) — its result is held with the fold, not
432+
* published into the mainline frame. Without it a mainline memo's untracked
433+
* read of a held key (`untrack(() => s.n)`, `deep(s)`) published the
434+
* unrevealed value while the same read of a signal was born held. */
435+
function enterHeldBacking(target: StoreNextTarget, txn = liveFoldTransition(target)): void {
436+
if (txn !== null && readerContext() !== null) enterStagedRead(null, txn);
437+
}
438+
427439
/** Core read()'s `activeTransition !== el._transition`: a hold belongs to a
428440
* FOREIGN transaction unless the flush running now is that transaction's —
429441
* its own stale readers (a render effect recomputing in it, whose run the
@@ -1688,6 +1700,10 @@ function readSource(target: StoreNextTarget): Record<PropertyKey, any> {
16881700
if (target.ht !== PLAIN_HOLD) staleReplay(currentTransition(target.ht as Transition));
16891701
return hv;
16901702
}
1703+
} else if (target.ht !== null && !latestReadActive && !inDraft(target) && !getWriteOverride()) {
1704+
// An owner-context deriving reader served the ADOPTED view under a live
1705+
// adoption hold derives from the adoption's transaction (A29).
1706+
enterHeldBacking(target, heldAdoptionTransition(target));
16911707
}
16921708
return pendingBackingVisible(target, false) ? target.pb! : target.v;
16931709
}
@@ -1721,7 +1737,10 @@ function pendingBackingVisible(target: StoreNextTarget, speculative: boolean): b
17211737
// ordinary readers keep committed until the transaction's reveal).
17221738
// Stale readers and owner-less peeks of a TRANSACTION-held backing see
17231739
// committed, as core read() serves them (#3336, heldFromReader).
1724-
((speculative || inOwnerContext()) && !heldTruthMasked(target) && !heldFromReader(target)) ||
1740+
((speculative || inOwnerContext()) &&
1741+
!heldTruthMasked(target) &&
1742+
!heldFromReader(target) &&
1743+
(enterHeldBacking(target), true)) ||
17251744
// A projection's pending backing is authoritative-elect: serve it to
17261745
// context-free readers too UNLESS a transition is holding the node
17271746
// commits (downstream async hold — stale committed is the contract)
@@ -1830,7 +1849,12 @@ function nodeValue(node: Signal<any>, backing: any): any {
18301849
// parent computed).
18311850
(latestReadActive ||
18321851
authoritativeServe() ||
1833-
!readerSeesCommitted(node, readerContext(), (node as any)._firewall || node, false))
1852+
// A deriving reader served the staged value enters its
1853+
// transaction (A29) as core read() does on the same arm: the
1854+
// pass is the hold's, its result held with it — an untracked read
1855+
// inside a mainline memo must not publish the unrevealed frame.
1856+
(!readerSeesCommitted(node, readerContext(), (node as any)._firewall || node, false) &&
1857+
(enterStagedRead(node), true)))
18341858
? node._pendingValue
18351859
: backing;
18361860
return v === (FORCE as any) ? backing : v;

‎packages/signals/tests/posture-store-parity.test.ts‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
* S3 — INV-4 after disposing a projection mid-refetch: its own file,
2020
* tests/inv4-projection-dispose-shadow.test.ts (the live actions S1/S2
2121
* leave behind would mask the quiescence check here). Spec O5.
22+
* S4 — (fixed, 3b steps 2–3) a stale reader's UNTRACKED read of a held store
23+
* key replays at the commit as the signal's does — node, backing, and
24+
* adoption-hold paths (recordStaleReplay).
25+
* S5 — (fixed, 3b step 4) a mainline derivation's UNTRACKED read of a held
26+
* store key is born held (A29) as the signal's is — the store's untracked
27+
* paths served the pending value without entering the transaction.
28+
* S6, S7 — DIVERGENCES recorded at their current values, not ruled (see the
29+
* block comment above them).
30+
* Discovery for S4–S7: the matrix's `memoUntracked` / `effectUntracked`
31+
* reader kinds (an untracked read inside a derivation), added with S5.
2232
*
2333
* (A first cut also reported the projection's seed leaking as a value inside
2434
* boundary content, and `isPending` false / override invisible behind a
@@ -29,8 +39,11 @@
2939
import { describe, expect, it } from "vitest";
3040
import {
3141
action,
42+
createEffect,
3243
createLoadingBoundary,
3344
createMemo,
45+
createOptimistic,
46+
createOptimisticStore,
3447
createRenderEffect,
3548
createRoot,
3649
createSignal,
@@ -260,3 +273,179 @@ describe("S4 — a stale reader's untracked read of a foreign hold replays at th
260273
});
261274
}
262275
});
276+
277+
/** A deriving reader (memo, user effect) created MAINLINE whose UNTRACKED
278+
* read is of a value held by a live action: the pass is served the staged
279+
* value and enters the transaction — born held (A29) — so nothing is
280+
* published until the action commits. The signal did this (core read()
281+
* enters on the same arm that serves the staged value; `context` persists
282+
* under untrack). The store's untracked paths (nodeValue, the backing's
283+
* pendingBackingVisible, the adoption-hold view) served the pending value
284+
* WITHOUT entering: a mainline memo published the action's unrevealed write
285+
* to the screen while the same read of a signal was held. */
286+
type HeldShape = "signal" | "store+node" | "store" | "store reconcile" | "store reconcile+node";
287+
function heldShape(shape: HeldShape) {
288+
if (shape === "signal") {
289+
const [x, setX] = createSignal(0);
290+
return { read: x, write: () => setX(1) };
291+
}
292+
const [s, setS] = createStore({ n: 0 });
293+
if (shape.endsWith("+node")) {
294+
createRoot(() =>
295+
createRenderEffect(
296+
() => s.n,
297+
() => {}
298+
)
299+
);
300+
flush();
301+
}
302+
return {
303+
read: () => s.n,
304+
write: () =>
305+
shape.startsWith("store reconcile")
306+
? setS(reconcile({ n: 1 }))
307+
: setS(d => {
308+
d.n = 1;
309+
})
310+
};
311+
}
312+
describe("S5 — a mainline derivation's UNTRACKED read of a held value is born held (A29) — signal vs store", () => {
313+
for (const shape of [
314+
"signal",
315+
"store+node",
316+
"store",
317+
"store reconcile",
318+
"store reconcile+node"
319+
] as HeldShape[]) {
320+
it(`${shape}: memo → render effect publishes nothing until the action commits`, async () => {
321+
const { read, write } = heldShape(shape);
322+
let release!: () => void;
323+
action(function* () {
324+
write();
325+
yield new Promise<void>(res => (release = res));
326+
})();
327+
flush();
328+
const log: number[] = [];
329+
const [u, setU] = createSignal(0);
330+
createRoot(() => {
331+
const m = createMemo(() => {
332+
u();
333+
return untrack(read);
334+
});
335+
createRenderEffect(m, v => {
336+
log.push(v);
337+
});
338+
});
339+
flush();
340+
setU(1); // a re-run off the hold is held too
341+
flush();
342+
expect(log).toEqual([]);
343+
release();
344+
await settle();
345+
expect(log).toEqual([1]);
346+
});
347+
it(`${shape}: user effect runs once, after the commit`, async () => {
348+
const { read, write } = heldShape(shape);
349+
let release!: () => void;
350+
action(function* () {
351+
write();
352+
yield new Promise<void>(res => (release = res));
353+
})();
354+
flush();
355+
const log: number[] = [];
356+
createRoot(() => {
357+
createEffect(
358+
() => untrack(read),
359+
v => {
360+
log.push(v);
361+
}
362+
);
363+
});
364+
flush();
365+
expect(log).toEqual([]);
366+
release();
367+
await settle();
368+
expect(log).toEqual([1]);
369+
});
370+
}
371+
});
372+
373+
/** DIVERGENCES the 7-reader matrix shows and this file only RECORDS (both
374+
* sides pinned at their current value; not ruled — flip the store or the
375+
* signal when the maintainer rules):
376+
*
377+
* S6 — staged, ambient (a write before any flush), reader created INSIDE a
378+
* foreign action (which adopts the write, spec O1): the signal's memo →
379+
* render effect publishes the committed 0 (A28 / #3510: adopted before
380+
* any flush = unflushed, served committed); the store's publishes the
381+
* pending 1 (pendingBackingVisible: owner context → pending backing).
382+
* The verdict channels already agree (S1); the derivation reads do not.
383+
* S7 — optimistic store, override active, the only reader gated away: the
384+
* signal's x() still reads the override 5 while the action is live
385+
* (A17); the store's s.n reads 0 — the override is invisible to an
386+
* untracked read once no reader observes the key.
387+
*/
388+
describe("S6 — DIVERGENCE (recorded): staged-ambient write read by a derivation created inside a foreign action", () => {
389+
function publishedInsideForeignAction(read: () => number) {
390+
const log: number[] = [];
391+
action(function* () {
392+
createRoot(() => {
393+
const m = createMemo(read);
394+
createRenderEffect(m, v => {
395+
log.push(v);
396+
});
397+
});
398+
yield never();
399+
})();
400+
flush();
401+
return log;
402+
}
403+
it("signal: publishes the committed 0", () => {
404+
const [x, setX] = createSignal(0);
405+
setX(1);
406+
expect(publishedInsideForeignAction(x)).toEqual([0]);
407+
});
408+
it("store: publishes the pending 1", () => {
409+
const [s, setS] = createStore({ n: 0 });
410+
setS(d => {
411+
d.n = 1;
412+
});
413+
expect(publishedInsideForeignAction(() => s.n)).toEqual([1]);
414+
});
415+
});
416+
417+
describe("S7 — DIVERGENCE (recorded): optimistic override, the only reader gated away", () => {
418+
function gateAway(read: () => number) {
419+
const [show, setShow] = createSignal(true);
420+
createRoot(() => {
421+
createRenderEffect(
422+
() => (show() ? read() : "gated"),
423+
() => {}
424+
);
425+
});
426+
flush();
427+
setShow(false);
428+
flush();
429+
return read();
430+
}
431+
it("signal: x() still reads the override while the action is live", () => {
432+
const [x, setX] = createOptimistic(0);
433+
action(function* () {
434+
setX(5);
435+
yield never();
436+
})();
437+
flush();
438+
expect(gateAway(x)).toBe(5);
439+
});
440+
it("store: s.n reads the committed 0 once nothing observes the key", () => {
441+
const [s, setS] = createOptimisticStore({ n: 0 });
442+
action(function* () {
443+
setS(d => {
444+
d.n = 5;
445+
});
446+
yield never();
447+
})();
448+
flush();
449+
expect(gateAway(() => s.n)).toBe(0);
450+
});
451+
});

0 commit comments

Comments
 (0)