Skip to content

Commit b8237c8

Browse files
committed
fix(signals): until() reads a derivation as truth only when its own guesses are confirmed (#3943)
Review of #3978: confirmed means the staged truth equals the guess (a re-guess over an earlier confirmation keeps waiting), per dependency (an unconfirmed guess elsewhere does not block), and a derivation that reads a held write is not the truth. The truth read and the confirmation wake move behind GlobalQueue hooks installed by the first until/refresh, so isPending/latest no longer pays for them (-71 B minified vs next); the cap raise is reverted. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LVm3wseHt9wRTY8LfPiHmQ
1 parent 2e6c993 commit b8237c8

6 files changed

Lines changed: 248 additions & 24 deletions

File tree

‎.changeset/until-confirmed-derivation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
`until()` through a derivation of an optimistic value resolves when the source confirms the guess (#3943). Once every guess the derivation depends on has been confirmed by its own source, its value is the truth (same inputs), so `until(() => doubled() === 2)` sees it; nothing is recomputed. A correction still re-derives the derivation inside the action's hold.
5+
`until()` through a derivation of an optimistic value resolves once the source confirms the guess (#3943). A derivation's optimistic value counts as the truth when every guess it depends on has been confirmed (its source produced a value equal to the guess) and none of its other inputs has a write still held by the action; nothing is recomputed. A guess made again after a confirmation, an unconfirmed guess the derivation reads, or a held write it reads keeps `until()` waiting, as before.

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,45 @@ export function supersede(n: Signal<any> | Computed<any>, value: unknown, change
405405
if (n._subs !== null) insertSubs(n);
406406
} else {
407407
n._config |= CONFIG_HELD;
408-
// A confirmation is silent (A17); only truth readers are told — of the
409-
// lane's derivations too, which are the truth once every guess is
410-
// confirmed (#3943).
411-
for (let i = 0; i < l._nodes.length; i++)
412-
for (let s = l._nodes[i]._subs; s !== null; s = s._nextSub)
413-
if (s._sub._config & CONFIG_AUTHORITATIVE) enqueueSub(s._sub);
408+
// A confirmation is silent (A17); only truth readers are told.
409+
GlobalQueue._confirmed?.(l);
414410
}
415411
n._pendingValue = value;
416412
joinFuture(parent);
417413
}
418414

415+
/** Truth readers (`until`, `refresh`; CONFIG_AUTHORITATIVE) — pay-for-use:
416+
* the first one installs these on GlobalQueue (signals.ts `watch`).
417+
*
418+
* A truth reader's read of a lane node: the lane's value when it is the
419+
* truth (#3943), else the truth staged beneath a guess, else the committed
420+
* value. */
421+
export function truthRead(el: Signal<any> | Computed<any>): unknown {
422+
return truth(el)
423+
? laneValueOf(el)
424+
: el._config & CONFIG_GUESS && el._pendingValue !== NOT_PENDING
425+
? el._pendingValue
426+
: el._value;
427+
}
428+
/** A guess confirmed: its lane's truth readers re-read — of the lane's
429+
* derivations too, which may be the truth now (#3943). */
430+
export function truthConfirmed(l: Transaction): void {
431+
for (let i = 0; i < l._nodes.length; i++)
432+
for (let s = l._nodes[i]._subs; s !== null; s = s._nextSub)
433+
if (s._sub._config & CONFIG_AUTHORITATIVE) enqueueSub(s._sub);
434+
}
435+
/** A lane node's value is the truth when every guess beneath it is
436+
* confirmed — its source staged a value equal to the guess — and nothing
437+
* beneath it is held in the parent: same inputs, same value. */
438+
function truth(n: Signal<any> | Computed<any>): unknown {
439+
if (n._config & CONFIG_GUESS)
440+
return n._pendingValue !== NOT_PENDING && n._equals && n._equals(n._x!._lane, n._pendingValue);
441+
if (n._pendingValue !== NOT_PENDING) return;
442+
if (n._config & CONFIG_OVERRIDE)
443+
for (let d = (n as Computed<any>)._deps; d; d = d._nextDep) if (!truth(d._dep)) return;
444+
return 1;
445+
}
446+
419447
/** A lane's end, two ways. `into === null`: the parent landed — the lane's
420448
* values are the truth now: a guess lands the truth staged beneath it or
421449
* reverts to the one it covered (`_value`, untouched by the guess); a
@@ -550,18 +578,9 @@ function dissolveLane(l: Transaction, into: Transaction | null, except?: Signal<
550578
* member throws like any. */
551579
export function laneRead(c: Computed<any> | null, el: Signal<any> | Computed<any>): unknown {
552580
const guess = el._config & CONFIG_GUESS;
553-
const l = txOf(el);
554-
// A truth reader (`until`): the truth staged beneath a guess, else the
555-
// committed value — or, for a derivation of guesses every one of which
556-
// its own source has confirmed, the lane's value: same inputs, so the
557-
// truth (#3943).
581+
if (c !== null && c._config & CONFIG_AUTHORITATIVE) return GlobalQueue._truth!(el);
558582
if (c === null) return laneValueOf(el);
559-
if (c._config & CONFIG_AUTHORITATIVE)
560-
return l._nodes.every(n => !(n._config & CONFIG_GUESS) || n._pendingValue !== NOT_PENDING)
561-
? laneValueOf(el)
562-
: guess && el._pendingValue !== NOT_PENDING
563-
? el._pendingValue
564-
: el._value;
583+
const l = txOf(el);
565584
const status = (el as Computed<any>)._statusFlags;
566585
if (!tracking || c._config & CONFIG_CHILDREN_FORBIDDEN) {
567586
// An untracked read inside lane work derives from the lane like a

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ export class GlobalQueue implements IQueue {
640640
/** The store's row judgment for a guessed slot (optimistic.ts): the truth
641641
* is the guess's echo when reconcile would write nothing into it. */
642642
declare static _echoes: ((guess: unknown, truth: unknown) => boolean) | undefined;
643+
/** Truth readers (lanes.ts `truthRead`/`truthConfirmed`), installed by the
644+
* first `until`/`refresh` (signals.ts `watch`). */
645+
declare static _truth: ((el: any) => unknown) | undefined;
646+
declare static _confirmed: ((l: Transaction) => void) | undefined;
643647
/** `affects()` on a store (store/affects.ts): a store's node for a key
644648
* (store/store.ts `getNode`); a bare registration (birth inheritance); a
645649
* witnessed mark on an untracked probe (verdict.ts). */

‎packages/signals/src/signals.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
dirtyQueue,
3232
flushTransaction,
3333
globalQueue,
34+
GlobalQueue,
3435
joinFuture,
3536
passTx,
3637
resolveTx,
@@ -39,7 +40,7 @@ import {
3940
} from "./core/scheduler.js";
4041
import { REACTIVE_JOINED, STATUS_ERROR, STATUS_PENDING } from "./core/constants.js";
4142
import { unwrapStatusError } from "./core/error.js";
42-
import { optimisticWrite } from "./core/lanes.js";
43+
import { optimisticWrite, truthConfirmed, truthRead } from "./core/lanes.js";
4344

4445
/**
4546
* Low-level reactive-cleanup primitive. Registers a callback that runs when
@@ -766,6 +767,10 @@ function watch<T>(
766767
inPass?: (v: T) => void,
767768
own: Transaction | null = flushTransaction
768769
): void {
770+
if (config & CONFIG_AUTHORITATIVE) {
771+
GlobalQueue._truth = truthRead;
772+
GlobalQueue._confirmed = truthConfirmed;
773+
}
769774
// `config` is on the node for its first pass too (the pass runs inside
770775
// `computed()`): `until`'s predicate is authoritative from its first read,
771776
// or an already-displayed guess would satisfy it in a later slice. (REDERIVE

‎packages/signals/tests/until-memo-confirmation-3943.test.ts‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
action,
44
createMemo,
55
createOptimistic,
6+
createOptimisticStore,
67
createRenderEffect,
78
createRoot,
89
createSignal,
@@ -42,3 +43,201 @@ for (const via of ["direct", "memo"] as const)
4243
await tick();
4344
expect(done).toBe(true);
4445
});
46+
47+
function setup() {
48+
let setSource!: (v: number) => void;
49+
let setX!: (v: number) => void;
50+
let api!: {
51+
count: () => number;
52+
doubled: () => number;
53+
sum: () => number;
54+
x: () => number;
55+
guess: (v: number) => void;
56+
};
57+
createRoot(() => {
58+
const [source, ss] = createSignal(0);
59+
const [x, sx] = createSignal(0);
60+
setSource = ss;
61+
setX = sx;
62+
const [count, guess] = createOptimistic(() => source());
63+
const doubled = createMemo(() => count() * 2);
64+
const sum = createMemo(() => count() + x());
65+
createRenderEffect(count, () => {});
66+
createRenderEffect(doubled, () => {});
67+
createRenderEffect(sum, () => {});
68+
api = { count, doubled, sum, x, guess };
69+
});
70+
flush();
71+
return { setSource, setX, ...api };
72+
}
73+
74+
const watch = (p: Promise<unknown>) => {
75+
const s = { done: false };
76+
p.then(() => (s.done = true));
77+
return s;
78+
};
79+
80+
// Review of #3978, 1: a truth staged beneath an earlier guess does not
81+
// confirm the next guess — confirmed means the staged truth equals the guess.
82+
for (const via of ["direct", "memo"] as const) {
83+
it(`until() ${via}: a re-guess the source has not confirmed keeps waiting (two saves)`, async () => {
84+
const m = setup();
85+
const save = action(function* (n: number) {
86+
m.guess(n);
87+
yield Promise.resolve();
88+
yield until(() => (via === "memo" ? m.doubled() === n * 2 : m.count() === n));
89+
});
90+
const first = watch(save(1));
91+
await tick();
92+
m.setSource(1);
93+
flush();
94+
await tick();
95+
expect(first.done).toBe(true);
96+
97+
const second = watch(save(2));
98+
await tick();
99+
expect(second.done).toBe(false);
100+
m.setSource(2);
101+
flush();
102+
await tick();
103+
expect(second.done).toBe(true);
104+
});
105+
106+
it(`until() ${via}: one action guessing twice waits for the second confirmation`, async () => {
107+
const m = setup();
108+
const save = action(function* () {
109+
m.guess(1);
110+
yield Promise.resolve();
111+
yield until(() => m.count() === 1);
112+
m.guess(2);
113+
yield Promise.resolve();
114+
yield until(() => (via === "memo" ? m.doubled() === 4 : m.count() === 2));
115+
});
116+
const s = watch(save());
117+
await tick();
118+
m.setSource(1);
119+
flush();
120+
await tick();
121+
expect(s.done).toBe(false);
122+
m.setSource(2);
123+
flush();
124+
await tick();
125+
expect(s.done).toBe(true);
126+
});
127+
}
128+
129+
// Review of #3978, 2: a derivation that also reads a held plain write is not
130+
// the truth — its lane value (guess + the old `x`) never commits.
131+
it("until() does not resolve on a derivation mixing a confirmed guess with a held write", async () => {
132+
const m = setup();
133+
const seen: string[] = [];
134+
const run = action(function* () {
135+
m.setX(10);
136+
m.guess(1);
137+
yield Promise.resolve();
138+
yield until(() => m.x() === 10);
139+
seen.push("x");
140+
yield until(() => m.sum() === 1);
141+
seen.push("sum=1");
142+
});
143+
run();
144+
await tick();
145+
m.setSource(1);
146+
flush();
147+
await tick();
148+
expect(seen).toEqual(["x"]);
149+
});
150+
151+
// Review of #3978, 3: confirmation is per dependency — an unconfirmed guess
152+
// elsewhere in the action does not block a derivation that does not read it.
153+
it("until() on a derivation of a confirmed guess ignores another, unconfirmed guess", async () => {
154+
let setA!: (v: number) => void;
155+
let save!: () => Promise<unknown>;
156+
createRoot(() => {
157+
const [a, sa] = createSignal(0);
158+
const [b] = createSignal(0);
159+
setA = sa;
160+
const [optA, guessA] = createOptimistic(() => a());
161+
const [optB, guessB] = createOptimistic(() => b());
162+
const doubledA = createMemo(() => optA() * 2);
163+
createRenderEffect(doubledA, () => {});
164+
createRenderEffect(optB, () => {});
165+
save = action(function* () {
166+
guessA(1);
167+
guessB(1);
168+
yield Promise.resolve();
169+
yield until(() => doubledA() === 2);
170+
});
171+
});
172+
flush();
173+
const s = watch(save());
174+
await tick();
175+
expect(s.done).toBe(false);
176+
setA(1);
177+
flush();
178+
await tick();
179+
expect(s.done).toBe(true);
180+
});
181+
182+
// The store form of review 1: a field edited twice in one action waits for
183+
// the second confirmation.
184+
it("until() on an optimistic store field edited twice waits for the second confirmation", async () => {
185+
let setSource!: (v: string) => void;
186+
let save!: () => Promise<unknown>;
187+
createRoot(() => {
188+
const [source, ss] = createSignal("");
189+
setSource = ss;
190+
const [store, setStore] = createOptimisticStore(() => ({ text: source() }), { text: "" });
191+
const upper = createMemo(() => store.text.toUpperCase());
192+
createRenderEffect(upper, () => {});
193+
save = action(function* () {
194+
setStore(s => void (s.text = "a"));
195+
yield Promise.resolve();
196+
yield until(() => store.text === "a");
197+
setStore(s => void (s.text = "b"));
198+
yield Promise.resolve();
199+
yield until(() => upper() === "B");
200+
});
201+
});
202+
flush();
203+
const s = watch(save());
204+
await tick();
205+
setSource("a");
206+
flush();
207+
await tick();
208+
expect(s.done).toBe(false);
209+
setSource("b");
210+
flush();
211+
await tick();
212+
expect(s.done).toBe(true);
213+
});
214+
215+
// A store row the server sends back as a fresh object confirms by content
216+
// (the row echoes the guess, #3898), so a derivation over it is the truth too.
217+
it("until() on a derivation of an optimistic store row confirmed by a fresh, equal row", async () => {
218+
type Row = { id: string; text: string };
219+
let setSource!: (v: Row[]) => void;
220+
let save!: () => Promise<unknown>;
221+
createRoot(() => {
222+
const [source, ss] = createSignal<Row[]>([]);
223+
setSource = ss;
224+
const [store, setStore] = createOptimisticStore(() => ({ items: source() }), {
225+
items: [] as Row[]
226+
});
227+
const texts = createMemo(() => store.items.map(i => i.text).join(","));
228+
createRenderEffect(texts, () => {});
229+
save = action(function* () {
230+
setStore(s => void s.items.push({ id: "1", text: "a" }));
231+
yield Promise.resolve();
232+
yield until(() => texts() === "a");
233+
});
234+
});
235+
flush();
236+
const s = watch(save());
237+
await tick();
238+
expect(s.done).toBe(false);
239+
setSource([{ id: "1", text: "a" }]);
240+
flush();
241+
await tick();
242+
expect(s.done).toBe(true);
243+
});

‎scripts/size/scenarios.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,11 +1287,8 @@ module.exports = [
12871287
// #3898 stacks on this; its combined size is remeasured before merge.
12881288
// #3898 stacked on next (2026-10-08): measured at 9,727 B brotli / 27,398 B minified
12891289
// by CI (Size run 37877131816). Cap set to that brotli size, rounded up to 0.01 KB.
1290-
// #3943 (2026-10-11), stacked on #3977 (which took this scenario to 9,714 B /
1291-
// 27,356 B): until() reads a derivation of confirmed guesses as the truth —
1292-
// 9,733 B / 27,411 B locally, +6 B brotli over next. Cap rounded up to 0.01 KB.
1293-
limit: "9.74 KB",
1294-
capMinified: 27411,
1290+
limit: "9.73 KB",
1291+
capMinified: 27398,
12951292
alias
12961293
},
12971294
{

0 commit comments

Comments
 (0)