Skip to content

Commit 0212cba

Browse files
brenelzclaude
andcommitted
fix(signals): draft reads compose overrides over a truth-staged backing
With several optimistic-store actions in flight (optimistic `votes++`, server confirm, `refresh(store)`), the first vote's truth landing is staged into the transaction that still retains the second vote (#3164 fold) and that vote's increment replays over it. A third click's draft then read the staged truth WITHOUT the replayed override: draft reads composed live overrides only while `pb === null`, on the assumption that an existing pending backing was the draft's own view-seeded clone. ensurePB's hand-off parks a truth-staged backing and reseeds from the view on the draft's first WRITE, but `votes++` reads first — base 1, wrote 2, and the override it emitted landed on the value already displayed. The click was invisible and the count stuck (or fell back) until truth caught up; ten rapid votes showed 1, 2, 3, 4, then 2, then 5. Draft reads now compose overrides whenever the pending backing is not the draft's own tentative clone (tentativePBs), in all four draft paths (data key, hot path, has, ownKeys). Regression in the #2951 refetch-hold suite: three votes in flight, the third made after the first landing, every visible value monotonic. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 439a2fe commit 0212cba

3 files changed

Lines changed: 130 additions & 5 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Optimistic increments keep stacking after a sibling landing. With several
6+
optimistic-store actions in flight (optimistic `votes++`, server confirm,
7+
`refresh(store)`), the first vote's truth landing is staged into the
8+
transaction that still retains the second vote, and that vote's increment
9+
replays over it. A third click's draft then read the staged truth WITHOUT
10+
the replayed override: draft reads composed live overrides only while no
11+
pending backing existed, and `votes++` reads before the first write triggers
12+
the view-reseed hand-off. It read base, wrote base + 1, and its override
13+
landed on the value already on screen — the click was invisible and the
14+
count stuck (or fell back) until truth caught up. Draft reads now compose
15+
overrides whenever the pending backing is not the draft's own view-seeded
16+
clone.

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,22 @@ export const stagedTruthPB = new WeakMap<StoreNextTarget, Record<PropertyKey, an
635635
* are consumed at setter exit. */
636636
const tentativePBs = new WeakSet<object>();
637637

638+
/** A draft read composes the live optimistic view until the draft has opened
639+
* its OWN view-seeded backing (ensurePB seeds that clone from the view and
640+
* registers it in tentativePBs; from then on reads must see the draft's
641+
* writes, not the overrides they superseded). A pending backing that exists
642+
* for any other reason is not that clone — a truth landing staged into a
643+
* retaining transaction (#3164 fold) is authoritative truth WITHOUT the
644+
* live overrides. ensurePB parks such a backing on the draft's first WRITE
645+
* and reseeds from the view, but the reads that precede that write went to
646+
* the staged truth: `votes++` read base, wrote base+1, and the override it
647+
* emitted landed on the value already displayed — a second in-flight
648+
* increment made after a sibling's landing was invisible (#2951's compose
649+
* half, one landing later). */
650+
function draftSeesOverrides(target: StoreNextTarget): boolean {
651+
return target.pb === null || !tentativePBs.has(target.pb);
652+
}
653+
638654
/** Committed-time privatization for parent-chain slot updates (path copying). */
639655
function privatizeCommitted(target: StoreNextTarget): void {
640656
if (ownedRaw.has(target.v)) return;
@@ -1392,7 +1408,7 @@ function serveDataKey(
13921408
// #2951). Once ensurePB runs, the seeded clone carries the view.
13931409
// AUTHORITATIVE drafts (projection derive) never overlay — ensurePB's
13941410
// seeding rule, applied to the read side (#3108).
1395-
if (target.fam?.opt && target.pb === null && !authoritativeServe()) {
1411+
if (target.fam?.opt && draftSeesOverrides(target) && !authoritativeServe()) {
13961412
const node = target.n?.[key as any];
13971413
if (node !== undefined && hasActiveOverride(node))
13981414
v = unwrapOverride(node._x?._overrideValue);
@@ -1614,7 +1630,7 @@ const traps: ProxyHandler<StoreNextTarget> = {
16141630
v === undefined &&
16151631
inDraft(target) &&
16161632
target.fam?.opt &&
1617-
target.pb === null &&
1633+
draftSeesOverrides(target) &&
16181634
// AUTHORITATIVE drafts (landing folds) never seed from overrides —
16191635
// the caller's optimism is not truth (has-trap twin below).
16201636
!authoritativeServe()
@@ -1655,7 +1671,7 @@ const traps: ProxyHandler<StoreNextTarget> = {
16551671
if (node !== undefined && hasActiveOverride(node))
16561672
present = !!unwrapOverride(node._x?._overrideValue);
16571673
}
1658-
} else if (target.fam?.opt && target.pb === null && !authoritativeServe()) {
1674+
} else if (target.fam?.opt && draftSeesOverrides(target) && !authoritativeServe()) {
16591675
const node = target.h?.[key as any];
16601676
if (node !== undefined && hasActiveOverride(node))
16611677
present = !!unwrapOverride(node._x?._overrideValue);
@@ -1688,7 +1704,7 @@ const traps: ProxyHandler<StoreNextTarget> = {
16881704
!authoritativeServe() &&
16891705
target.fam?.opt &&
16901706
target.h !== null &&
1691-
(!inDraft(target) || target.pb === null)
1707+
(!inDraft(target) || draftSeesOverrides(target))
16921708
) {
16931709
let set: Set<PropertyKey> | null = null;
16941710
for (const key of Reflect.ownKeys(target.h)) {

‎packages/signals/tests/optimistic-store-refetch-hold.test.ts‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
*/
1717
import { expect, test } from "vitest";
1818
import {
19+
action,
1920
createEffect,
2021
createOptimisticStore,
2122
createRoot,
2223
createSignal,
23-
flush
24+
flush,
25+
refresh
2426
} from "../src/index.js";
2527

2628
const tick = () => new Promise(r => setTimeout(r, 0));
@@ -188,3 +190,94 @@ test("#2951: bare write on a derived store with settled truth keeps flash semant
188190
expect(views.at(-1)).toEqual(["A"]);
189191
dispose();
190192
});
193+
194+
// The compose half, one landing later: three votes in flight as actions
195+
// (optimistic `votes++`, server confirm, `refresh(store)`). When the first
196+
// vote's truth lands while the second is still retained, the landing is
197+
// staged into the retaining transaction and the second vote's increment is
198+
// replayed over it. A THIRD click's draft then read the staged truth WITHOUT
199+
// that replayed override (draft reads composed overrides only while no
200+
// pending backing existed — ensurePB's hand-off reseeds from the view on
201+
// the first write, but `votes++` reads first): base 1, wrote 2, and the
202+
// override landed on the value already displayed — the click was invisible
203+
// and the count stuck until truth caught up.
204+
test("#2951 compose half: an increment made after a sibling landing still stacks on the retained increment", async () => {
205+
type Row = { id: number; votes: number };
206+
const db: Row[] = [{ id: 1, votes: 0 }];
207+
const fetches: Array<() => void> = [];
208+
const getList = () => new Promise<Row[]>(res => fetches.push(() => res(structuredClone(db))));
209+
const confirms: Array<() => void> = [];
210+
const addVote = () =>
211+
new Promise<void>(res =>
212+
confirms.push(() => {
213+
db[0].votes++;
214+
res();
215+
})
216+
);
217+
const views: number[] = [];
218+
let list!: Row[];
219+
let vote!: () => unknown;
220+
let dispose!: () => void;
221+
createRoot(d => {
222+
dispose = d;
223+
let setList!: (fn: (l: Row[]) => void) => void;
224+
[list, setList] = createOptimisticStore<Row[]>(() => getList(), []);
225+
vote = action(function* () {
226+
setList(l => {
227+
l[0].votes++;
228+
});
229+
yield addVote();
230+
refresh(list as any);
231+
});
232+
createEffect(
233+
() => list[0]?.votes,
234+
v => {
235+
views.push(v);
236+
}
237+
);
238+
});
239+
flush();
240+
fetches.shift()!();
241+
await tick();
242+
flush();
243+
const settle = async () => {
244+
await tick();
245+
flush();
246+
await tick();
247+
flush();
248+
};
249+
250+
vote(); // A
251+
await settle();
252+
vote(); // B
253+
await settle();
254+
expect(list[0].votes).toBe(2);
255+
256+
confirms.shift()!(); // A confirmed → A refreshes
257+
await settle();
258+
fetches.shift()!(); // A's truth (1) lands; B still in flight, its +1 replays
259+
await settle();
260+
expect(list[0].votes).toBe(2);
261+
262+
vote(); // C: must stack on B's retained increment, not on the staged truth
263+
await settle();
264+
expect(list[0].votes).toBe(3);
265+
266+
confirms.shift()!();
267+
await settle();
268+
fetches.shift()!(); // B's truth (2) lands; C's +1 replays
269+
await settle();
270+
expect(list[0].votes).toBe(3);
271+
272+
confirms.shift()!();
273+
await settle();
274+
fetches.shift()!(); // C's truth (3) lands
275+
await settle();
276+
expect(list[0].votes).toBe(3);
277+
// Every visible value is monotonic — no click ever reads back a smaller
278+
// count (equal-value re-runs at a landing's override → committed swap are
279+
// fine).
280+
expect(views.filter((v, i) => i === 0 || v !== views[i - 1])).toEqual([0, 1, 2, 3]);
281+
dispose();
282+
flush();
283+
});

0 commit comments

Comments
 (0)