Skip to content

Commit a8a8949

Browse files
ryansolidcursoragentClaude via Cursor
authored
test(signals): store oracle + four signal states; latest() seed leak and body-end visibility fixes (#3455)
* test(signals): store oracle + four signal states; fix latest() seed leak and body-end visibility Oracle grows from 90 to 207 cells. The harness (cell vocabulary, readers, hold management, runner) moves to tests/visibility-oracle.harness.ts, shared by the signal oracle (now 14 states: + body ended, un-superseded, held truth, loading window over a held input) and a new store oracle (8 states through plain stores, optimistic stores and projections). Two runtime fixes the new cells found: - latest(() => store.key) on an unresolved projection returned the SEED. read() routes a latest() read to the companion before its firewall logic, and the leaf's own _value is the seed; latestRead now judges "uninitialized" on the leaf's owner (the firewall) and throws like every other read (A25, A7). - The body-end supersession window (#3427) read differently from a landing supersession: a stale re-run showed the committed truth beside a display still showing the override, a fresh mainline memo published it, isPending read false while latest read the truth. One cause: an override written inside an action never passes the adoption loop that stamps _transition, and body-end stages nothing that would queue it, so the node carried no stamp. Ownership for an override node is _overrideOwner (#2912); supersededRead and the verdict resolve it now. Pinning that exposed a fourth: a latest() pull through supersededRead fell to enterStagedRead's initTransition path and captured the caller's block — verdict pulls are observations and never enter from mainline. Rulings recorded: A27 (verdict-quiet is a hydration invariant; the window's first real landing is initial-load class even over a held input), A18 body-end visibility, A29 verdict-pull note, A7 owner-judged. Also a type fix in born-held (bornHeld narrowed to never). Signals 2005 / solid 618 / web 783 green; brotli caps pass (core floor 8806 of 8850). Oracle: 207 cells, zero violations, 15 observed (12 on A28/#3337). Co-authored-by: Cursor <[email protected]> * chore(size): isPending/latest cap 11.25 -> 11.30 KB for the body-end visibility fixes (#3455) Measured at 11267 B against next's 11213 (+54): supersededRead's owner resolution and committed-truth entry, the verdict's body-end A18 (d) branch, and uninitializedSource's owner walk — all in the verdict/ optimistic modules this scenario retains. Core floor and +createStore stay in cap. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]> Co-authored-by: Claude via Cursor <[email protected]>
1 parent 899c2c4 commit a8a8949

12 files changed

Lines changed: 1077 additions & 357 deletions

‎.changeset/body-end-visibility.md‎

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+
The window after an action body ends and its override is superseded by the committed truth (#3427) now reads like a landing supersession for every reader: a stale reader re-run by an unrelated write keeps displaying the override, a memo created mainline during the window is held with the transaction, and `isPending()` reads true while the truth differs from the override (`latest()` already answered the truth). The node carries no transaction stamp in that window — an override written inside an action never passes the adoption loop that stamps one — so `supersededRead` and the verdict now resolve the owning transaction through `_overrideOwner`. Also: a `latest()` / `isPending()` pull from mainline never enters a transaction (it is an observation); one did through the supersession path and captured the caller's synchronous block.
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+
`latest(() => store.key)` on a derived store (projection) that has not yet resolved threw for tracked and untracked reads but returned the **seed** through `latest()`: `read()` routes a `latest()` read to the companion before its firewall/status logic, and the leaf's own `_value` is the seed. `latest()` now judges "uninitialized" on the leaf's owner — the projection's firewall — and throws `NotReadyError` like every other read (A25: the seed is a draft, never a value; A7).

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

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

‎packages/signals/docs/SPEC-ASYNC-SEMANTICS.md‎

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

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,26 +1506,33 @@ function heldFromStale(el: Signal<any> | Computed<any>, c: Computed<any>): boole
15061506
* transaction (born held) and mainline is never touched. */
15071507
let stagedEntry: Transition | null = null;
15081508

1509-
export function enterStagedRead(el: Signal<any> | Computed<any>): void {
1510-
const t = el._transition;
1511-
if (t === null || t === activeTransition || pendingCheckActive) return;
1509+
export function enterStagedRead(
1510+
el: Signal<any> | Computed<any>,
1511+
t: Transition | null | undefined = el._transition
1512+
): void {
1513+
if (!t || t === activeTransition || pendingCheckActive) return;
15121514
// Verdict machinery (GlobalQueue._verdictPull: companion creation and the
15131515
// latest()/isPending() pulls — the latest() shadow is created before it is
15141516
// marked optimistic, so the bit alone cannot tell) and optimistic nodes
15151517
// (own lane posture, A31) read staged truth by design; they keep the
15161518
// entering path.
15171519
// (`context` is non-null here: every caller selected a value for a reader.)
15181520
const ctx = context as Computed<any>;
1519-
if (
1520-
activeTransition === null &&
1521-
!globalQueue._running &&
1522-
!GlobalQueue._verdictPull &&
1523-
ctx._flags & REACTIVE_RECOMPUTING_DEPS &&
1524-
!(ctx._config & CONFIG_OPTIMISTIC) &&
1525-
(stagedEntry === null || stagedEntry === t)
1526-
)
1527-
stagedEntry = t;
1528-
else globalQueue.initTransition(t);
1521+
if (activeTransition === null && !globalQueue._running) {
1522+
// Verdict pulls are observations, not derivations: a latest() /
1523+
// isPending() call from mainline must never enter a transaction (it
1524+
// would capture the rest of the caller's synchronous block).
1525+
if (GlobalQueue._verdictPull) return;
1526+
if (
1527+
ctx._flags & REACTIVE_RECOMPUTING_DEPS &&
1528+
!(ctx._config & CONFIG_OPTIMISTIC) &&
1529+
(stagedEntry === null || stagedEntry === t)
1530+
) {
1531+
stagedEntry = t;
1532+
return;
1533+
}
1534+
}
1535+
globalQueue.initTransition(t);
15291536
}
15301537

15311538
export function readNodeFast<T>(el: Signal<T>): T | typeof READ_SLOW {

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,19 @@ function endOptimism(transition: Transition): boolean {
338338
* OTHER transaction, the same visibility a foreign transaction's staged
339339
* write has. */
340340
function supersededRead(el: OptimisticNode): unknown {
341-
if (stale && el._transition && activeTransition !== el._transition)
342-
return unwrapOverride(el._x?._overrideValue);
343-
if (el._pendingValue === NOT_PENDING) return el._value;
344-
// The staged truth is a staged read like any other (A29): the pass that
345-
// derives from it is the transaction's.
346-
enterStagedRead(el);
347-
return el._pendingValue;
341+
// The owning transaction: `_overrideOwner` (#2912), not the stamp — an
342+
// override written directly inside an action never passes the adoption
343+
// loop that stamps `_transition`, and a body-end supersession (#3427)
344+
// stages nothing that would queue it. Without the owner a stale reader of
345+
// a body-ended node read the committed truth beside a display still
346+
// showing the override.
347+
const owner = resolveTransition(el);
348+
if (stale && owner && activeTransition !== owner) return unwrapOverride(el._x?._overrideValue);
349+
// A superseded read is a staged read (A29) whether the truth is staged or
350+
// already committed: the pass that derives from it derives from the
351+
// owning transaction's world (the override is still displayed by it).
352+
enterStagedRead(el, owner);
353+
return el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value;
348354
}
349355

350356
/**

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
STATUS_ERROR,
1818
STATUS_PENDING,
1919
STATUS_UNINITIALIZED,
20-
CONFIG_HAS_COMPANIONS
20+
CONFIG_HAS_COMPANIONS,
21+
CONFIG_OVERRIDE_SUPERSEDED
2122
} from "./constants.js";
2223
import {
2324
context,
@@ -219,6 +220,15 @@ function computePendingState(el: Signal<any> | Computed<any>): boolean {
219220
// the window's own landing in flight to its commit — verdict-quiet like the
220221
// rest of the window (the UNINITIALIZED check suppresses exactly this frame
221222
// for windowless first loads; born-committed nodes need their own gate, #2990).
223+
// A18 (d) for a body-end supersession (#3427): the truth at hand is the
224+
// COMMITTED value — nothing staged — yet the display still shows the
225+
// override; pending iff they differ, as for a staged arrival below.
226+
if (
227+
el._config & CONFIG_OVERRIDE_SUPERSEDED &&
228+
el._pendingValue === NOT_PENDING &&
229+
hasActiveOverride(el)
230+
)
231+
return !el._equals || !el._equals(el._value as any, unwrapOverride(el._x?._overrideValue));
222232
if (el._pendingValue !== NOT_PENDING && !comp._loading) {
223233
// A18 (d): under a displayed override the observable value is the
224234
// override, so the verdict is "the arrived truth differs from it" —
@@ -392,6 +402,16 @@ function getLatestValueComputed<T>(el: Signal<T> | Computed<T>): Computed<T> {
392402
}
393403

394404
/** The latest()-mode read path, installed as GlobalQueue._latestRead. */
405+
/** A7: the source has no visible value yet — judged on the OWNER, as read()
406+
* does: a store leaf behind a projection's firewall is a plain signal whose
407+
* `_value` is the seed (A25: a draft, never a value), and read() routes a
408+
* latest() read here before its own firewall/status logic. An override
409+
* displays a value even before the first commit (A17). */
410+
function uninitializedSource(el: Signal<any> | Computed<any>): boolean {
411+
const owner = ((el as FirewallSignal<any>)._firewall || el) as Computed<any>;
412+
return !!(owner._statusFlags & STATUS_UNINITIALIZED) && !hasActiveOverride(el);
413+
}
414+
395415
function latestRead<T>(el: Signal<T> | Computed<T>): T {
396416
const pendingComputed = getLatestValueComputed(el);
397417
const prevPending = latestReadActive;
@@ -440,13 +460,15 @@ function latestRead<T>(el: Signal<T> | Computed<T>): T {
440460
// uninitialized source has no visible value — latest() throws in every
441461
// scope rather than fabricate `undefined` for a `T` that excludes it
442462
// (A7; the unowned scope used to return undefined here).
443-
if (e instanceof NotReadyError && !((el as Computed<T>)._statusFlags & STATUS_UNINITIALIZED))
444-
return visibleValue;
463+
if (e instanceof NotReadyError && !uninitializedSource(el)) return visibleValue;
445464
throw e;
446465
} finally {
447466
setLatestReadActive(prevPending);
448467
}
449-
if (pendingComputed._statusFlags & STATUS_PENDING) return visibleValue;
468+
if (pendingComputed._statusFlags & STATUS_PENDING) {
469+
if (uninitializedSource(el)) throw new NotReadyError(el);
470+
return visibleValue;
471+
}
450472
if (stale && currentOptimisticLane && pendingComputed._x?._optimisticLane) {
451473
const pcLane = findLane(pendingComputed._x?._optimisticLane);
452474
const curLane = findLane(currentOptimisticLane);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* A18 body-end corollary (#3427) — visibility during the correction window.
3+
*
4+
* When the action bodies have ended and nothing authoritative is in flight,
5+
* each override is superseded by the truth at hand — here the COMMITTED value,
6+
* since no landing staged anything. The window that follows must look like a
7+
* landing supersession to every reader: the display keeps the override until
8+
* the commit (A18 c), a fresh derivation is held (A29), and the verdict says
9+
* the truth differs (A18 d). Found by the visibility oracle: with nothing
10+
* staged the node carried no `_transition` stamp (an override written inside
11+
* an action never passes the adoption loop), so `supersededRead` served the
12+
* committed truth to a stale reader beside a display still showing the
13+
* override, a fresh memo published it, and `isPending` read false while
14+
* `latest` read the truth. Ownership for an override node is `_overrideOwner`
15+
* (#2912); the read path and the verdict now resolve it.
16+
*/
17+
import { describe, expect, it } from "vitest";
18+
import {
19+
action,
20+
createMemo,
21+
createOptimistic,
22+
createRenderEffect,
23+
createRoot,
24+
createSignal,
25+
flush,
26+
isPending,
27+
latest
28+
} from "../src/index.js";
29+
30+
const settle = async () => {
31+
await Promise.resolve();
32+
await Promise.resolve();
33+
flush();
34+
};
35+
36+
describe("A18 body-end supersession: the correction window", () => {
37+
it("display keeps the override, a stale re-run keeps it, a fresh derivation is held, the verdict says it differs", async () => {
38+
const flights: Array<() => void> = [];
39+
const [u, setU] = createSignal(0);
40+
const staleLog: number[] = [];
41+
let x!: () => number;
42+
let setX!: (v: number) => void;
43+
createRoot(() => {
44+
[x, setX] = createOptimistic(0);
45+
const downstream = createMemo(() => {
46+
const n = x();
47+
return new Promise<string>(r => flights.push(() => r(`${n}!`)));
48+
});
49+
createRenderEffect(downstream, () => {});
50+
});
51+
flush();
52+
flights.shift()!(); // prime downstream(0)
53+
await settle();
54+
// A pre-existing reader in another root (a sibling component), also
55+
// tracking an unrelated signal so it can be re-run mainline.
56+
createRoot(() => {
57+
createRenderEffect(
58+
() => {
59+
u();
60+
return x();
61+
},
62+
v => {
63+
staleLog.push(v);
64+
}
65+
);
66+
});
67+
flush();
68+
staleLog.length = 0;
69+
70+
action(function* () {
71+
setX(1);
72+
yield Promise.resolve(); // the body ends; downstream(1) is still up
73+
})();
74+
flush();
75+
await settle();
76+
await settle();
77+
// Body-end: the override (1) is superseded by the committed truth (0); the
78+
// graph re-derives (a downstream flight for 0 starts) and the transaction
79+
// waits for it.
80+
expect(flights.length).toBe(2);
81+
expect(x()).toBe(1); // display
82+
expect(latest(x)).toBe(0); // truth
83+
expect(isPending(x)).toBe(true); // differs
84+
// (Reading the verdicts here is deliberate: a latest() pull once entered
85+
// the owning transaction ambiently and the two checks below depended on
86+
// whether latest() had been called first.)
87+
88+
// A stale reader re-run by an unrelated write keeps displaying the override.
89+
setU(1);
90+
flush();
91+
expect(staleLog).toEqual([1]);
92+
93+
// A fresh mainline derivation derives from the truth and is held.
94+
const fresh: number[] = [];
95+
createRoot(() => {
96+
const m = createMemo(() => x());
97+
createRenderEffect(m, v => {
98+
fresh.push(v);
99+
});
100+
});
101+
flush();
102+
expect(fresh).toEqual([]);
103+
104+
// The 0-flight lands: the transaction commits, everything reveals at once.
105+
for (const f of flights.splice(0)) f();
106+
await settle();
107+
await settle();
108+
expect(x()).toBe(0);
109+
expect(isPending(x)).toBe(false);
110+
expect(fresh).toEqual([0]);
111+
expect(staleLog.at(-1)).toBe(0);
112+
});
113+
});

0 commit comments

Comments
 (0)