Skip to content

Commit a5066e7

Browse files
authored
Merge pull request #3558 from solidjs/fix/optimistic-move-duplicate-3548
fix(signals): lane pass over a superseded override reads the displayed override (#3548)
2 parents be46a04 + 739404d commit a5066e7

4 files changed

Lines changed: 203 additions & 2 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+
Fix an optimistic override superseded by its source's landing leaking the landed truth into a later action's lane frame (#3548). A lane pass composes the frame it applies ahead of the commit, so a superseded node now serves it the displayed override — A18 (c): the screen keeps the override until the owning transaction commits — and the reader is recorded for replay at that commit, since the superseded drop notifies nobody. Before, a fresh write's lane reaching a reader shared with the superseded node (two filtered keyed `<For>` lists over one optimistic store) re-derived one list from the truth while its neighbour still displayed the override, rendering the same row in two lanes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
6262
| A15 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:199` | async.ts×4 core.ts×5 lanes.ts×2 scheduler.ts×2 store.ts×1 | async-chain-supersession.test.ts×1 first-observer-stale-reader.test.ts×1 lane-hold-on-observation.test.ts×1 lane-outside-view.test.ts×1 overlapping-flights.test.ts×3 posture-born-held-and-observation.test.ts×4 posture-store-parity.test.ts×2 reveal-carve-out.test.ts×2 shared-effect-no-entangle.test.ts×1 spec-async-semantics.test.ts×2 stale-read-uninitialized-cross-transition.test.ts×1 superseded-source-blocks-3462.test.ts×2 treeshake.test.ts×4 visibility-oracle-store.states.ts×6 visibility-oracle.states.ts×6 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B3)] Transition entanglement is graph-driven; lanes settle as one reveal — (was B3) Transition entanglement is graph-driven: writes whose async work … |
6363
| A16 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:173` | verdict.ts×1 | spec-async-semantics.test.ts×1 strict-read-pending-store.test.ts×2 uninitialized-visibility.test.ts×1 visibility-oracle-store.states.ts×2 visibility-oracle.states.ts×2 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B5)] `isPending` never throws in untracked contexts — (was B5) `isPending` never throws in untracked contexts — thunks that throw real errors or read… |
6464
| A17 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:31` | async.ts×4 constants.ts×2 core.ts×10 invariants.ts×3 optimistic.ts×6 scheduler.ts×2 verdict.ts×2 signals.ts×2 optimistic.ts×1 store.ts×1 | optimistic-undefined-override.test.ts×1 posture-store-parity.test.ts×1 refresh-await.test.ts×1 reveal-gating-contract.test.ts×3 spec-async-semantics.test.ts×10 createOptimisticStore.test.ts×1 treeshake.test.ts×1 until.test.ts×1 visibility-oracle-store.states.ts×24 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×25 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from C4)] An active override is the displayed value until its transaction commits, and the graph's value until its own source answers — **Statement (curre… |
65-
| A18 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:41` | async.ts×3 constants.ts×1 core.ts×7 optimistic.ts×6 scheduler.ts×3 types.ts×2 verdict.ts×2 optimistic.ts×1 | body-end-supersession-visibility.test.ts×4 createOptimistic.test.ts×1 lane-outside-view.test.ts×1 posture-store-parity.test.ts×5 spec-async-semantics.test.ts×3 flight-owned-transaction.test.ts×1 superseded-before-first-commit.test.ts×5 visibility-oracle-store.states.ts×9 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×24 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-07 (promoted from B4)] An override lives exactly as long as its own transaction; a newer truth from the source supersedes it in the graph immediately, on screen at com… |
65+
| A18 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:41` | async.ts×3 constants.ts×1 core.ts×7 optimistic.ts×7 scheduler.ts×3 types.ts×2 verdict.ts×2 optimistic.ts×1 | body-end-supersession-visibility.test.ts×4 createOptimistic.test.ts×1 lane-outside-view.test.ts×1 optimistic-move-duplicate-3548.test.ts×2 posture-store-parity.test.ts×5 spec-async-semantics.test.ts×3 flight-owned-transaction.test.ts×1 superseded-before-first-commit.test.ts×5 visibility-oracle-store.states.ts×9 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×24 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-07 (promoted from B4)] An override lives exactly as long as its own transaction; a newer truth from the source supersedes it in the graph immediately, on screen at com… |
6666
| A19 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:101` | async.ts×1 core.ts×2 optimistic.ts×1 verdict.ts×2 | spec-async-semantics.test.ts×3 superseded-before-first-commit.test.ts×1 uninitialized-visibility.test.ts×1 visibility-oracle-store.states.ts×8 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×11 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled 2026-07-07 (promoted from C1)] `isPending(x)` ≡ the observable value is not final (three causes) — (was C1 — **partially reverses an earlier decision**) **Definition: `isPending(x)` ≡ the value… |
6767
| A20 | superseded | `docs/SPEC-ASYNC-SEMANTICS.md:350` | invariants.ts×1 | question-scoped-pending.test.ts×2 spec-async-semantics.test.ts×3 createOptimisticStore.test.ts×1 | [superseded 2026-07-13 by A24] (superseded) Optimistic writes announce a store-wide pending — (**SUPERSEDED 2026-07-13 by A24** — the mask is deleted; optimistic writes are verdict-inert. Kept for the… |
6868
| A21 | superseded | `docs/SPEC-ASYNC-SEMANTICS.md:357` | — | question-scoped-pending.test.ts×3 spec-async-semantics.test.ts×3 | [superseded 2026-07-13 by A24] (superseded) The store-wide mask — (**SUPERSEDED 2026-07-13 by A24** — the store-wide mask is deleted with the mask model; nothing silences a new question. The effective… |

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ function endOptimism(transition: Transition): boolean {
429429
* answered with a DIFFERENT value (A18 supersession, #3331): the optimism is
430430
* over for the graph — a tracked reader sees the staged truth — while the
431431
* override remains the DISPLAYED value for untracked reads (and for a stale
432-
* reader of some other transaction).
432+
* reader of some other transaction), and for a LANE pass (#3548, below).
433433
*/
434434
function overrideRead(el: OptimisticNode, c: Computed<any>): unknown {
435435
if (stale && readsHeldCommitted(el as Computed<any>, c)) return el._value;
@@ -441,6 +441,22 @@ function overrideRead(el: OptimisticNode, c: Computed<any>): unknown {
441441
// a body-ended node read the committed truth beside a display still
442442
// showing the override.
443443
const owner = resolveTransition(el);
444+
// A lane pass composes the frame the lane applies AHEAD of the commit, so
445+
// it reads what is on screen — for a superseded node, the override (A18
446+
// (c): the applied screen keeps it until the transaction commits). The
447+
// supersession dropped this node's own lane; a LATER write's lane reaching
448+
// a shared reader (a list filtered on two rows' fields, #3548) otherwise
449+
// handed that pass the staged truth and applied it at once: one list
450+
// re-derived from the truth while its neighbours still displayed the
451+
// override — the same row rendered in two lanes. The reader is recorded
452+
// for replay at the owner's commit under laneReadsCommitted's contract:
453+
// the superseded drop notifies nobody (resolveOptimisticNodes assumes its
454+
// subscribers already derive from the truth), so the replay is what
455+
// brings this reader the revealed value.
456+
if (currentOptimisticLane !== null) {
457+
(owner ?? globalQueue._batch)._gatedSubs.add(c);
458+
return unwrapOverride(el._x?._overrideValue);
459+
}
444460
if (stale && owner && activeTransition !== owner) return unwrapOverride(el._x?._overrideValue);
445461
// A superseded read is a staged read (A29) whether the truth is staged or
446462
// already committed: the pass that derives from it derives from the
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/**
2+
* #3548 — overlapping optimistic moves duplicate an item across filtered
3+
* keyed lists.
4+
*
5+
* Reduced from the report's playground: a `createOptimisticStore` backed by
6+
* an async iterable (manual `publish`), two cards, three lanes each rendered
7+
* as a keyed `mapArray` over `cards.filter(c => c.lane === lane)`. Four
8+
* optimistic moves via actions overlap; the authoritative result of the FIRST
9+
* move is published and only that action resolves while the later ones stay
10+
* pending.
11+
*
12+
* 1. card 0 → lane 1 (order 1) [action 1]
13+
* 2. card 0 → lane 2 (order 6) [action 2]
14+
* 3. card 1 → lane 1 (order 9) [action 3]
15+
* 4. publish {card0: lane 1, card1: lane 0}; resolve action 1 only; task turn
16+
* 5. card 1 within lane 1 (order 3) [action 4]
17+
*
18+
* Reported: after step 5 lane 1 renders cards 0 and 1 AND lane 2 renders
19+
* card 0 — card 0 is on screen twice although the store places it once (lane
20+
* 2). Expected: lane 1 = [1], lane 2 = [0], lane 0 = [].
21+
*
22+
* The invariant pinned: in every frame the lanes publish, no card id appears
23+
* in two lanes, and the rendered placement equals the store's own placement.
24+
*/
25+
import {
26+
action,
27+
createOptimisticStore,
28+
createRenderEffect,
29+
createRoot,
30+
flush,
31+
mapArray
32+
} from "../src/index.js";
33+
34+
afterEach(() => flush());
35+
36+
interface Card {
37+
id: number;
38+
lane: number;
39+
order: number;
40+
version: number;
41+
}
42+
interface Source {
43+
cards: Card[];
44+
}
45+
46+
describe("#3548 overlapping optimistic moves across filtered keyed lists", () => {
47+
it("never renders a card in two lanes while an older move lands under newer pending moves", async () => {
48+
let source: Source = {
49+
cards: [0, 1].map(id => ({ id, lane: 0, order: 0, version: 0 }))
50+
};
51+
let publish!: (value: Source) => void;
52+
async function* stream() {
53+
yield source;
54+
while (true) yield await new Promise<Source>(resolve => (publish = resolve));
55+
}
56+
57+
const pending = new Map<number, () => void>();
58+
const frames: { lanes: number[][]; store: number[] }[] = [];
59+
let view!: Source;
60+
let dispose!: () => void;
61+
62+
createRoot(d => {
63+
dispose = d;
64+
const [v, setView] = createOptimisticStore<Source>(stream, source);
65+
view = v;
66+
67+
const move = action(function* (id: number, lane: number, order: number, version: number) {
68+
setView(state => {
69+
const card = state.cards.find(card => card.id === id)!;
70+
Object.assign(card, { lane, order, version });
71+
});
72+
yield new Promise<void>(resolve => pending.set(version, resolve));
73+
});
74+
(globalThis as any).__move3548 = move;
75+
76+
// Three lanes, each a keyed `<For>` over the filtered+sorted store —
77+
// the `each` getter runs inside mapArray's own computation, as in
78+
// `<For each={view.cards.filter(...).sort(...)} keyed={c => c.id}>`.
79+
const lanes = [0, 1, 2].map(lane =>
80+
mapArray(
81+
() => view.cards.filter(c => c.lane === lane).sort((a, b) => a.order - b.order),
82+
card => card,
83+
{ keyed: (c: Card) => c.id }
84+
)
85+
);
86+
87+
// The DOM stand-in: one frame per flush, each lane's rendered ids.
88+
createRenderEffect(
89+
() => ({
90+
lanes: lanes.map(lane => lane().map(card => card().id)),
91+
store: view.cards.map(c => c.lane)
92+
}),
93+
frame => {
94+
frames.push(frame);
95+
}
96+
);
97+
});
98+
99+
const move = (globalThis as any).__move3548 as (
100+
id: number,
101+
lane: number,
102+
order: number,
103+
version: number
104+
) => Promise<void>;
105+
const start = (id: number, lane: number, order: number, version: number) => {
106+
void move(id, lane, order, version);
107+
flush();
108+
};
109+
// First yield lands.
110+
flush();
111+
await Promise.resolve();
112+
await Promise.resolve();
113+
flush();
114+
expect(frames.at(-1)!.lanes).toEqual([[0, 1], [], []]);
115+
116+
start(0, 1, 1, 1); // 1. card 0 → lane 1
117+
start(0, 2, 6, 2); // 2. card 0 → lane 2
118+
start(1, 1, 9, 3); // 3. card 1 → lane 1
119+
expect(frames.at(-1)!.lanes).toEqual([[], [1], [0]]);
120+
121+
// 4. authoritative result of action 1 lands; only action 1 resolves.
122+
// Mainline provenance: it supersedes card 0's newer override for the
123+
// graph (A18), while the screen keeps the override until the merged
124+
// transaction (actions 2–4) commits (A18 (c)).
125+
source = {
126+
cards: [
127+
{ id: 0, lane: 1, order: 1, version: 1 },
128+
{ id: 1, lane: 0, order: 0, version: 0 }
129+
]
130+
};
131+
publish(source);
132+
pending.get(1)!();
133+
await new Promise(resolve => setTimeout(resolve, 0));
134+
flush();
135+
136+
// 5. card 1 within lane 1 — a fresh lane reaching lane 1's list, which
137+
// also derives from card 0's superseded field.
138+
start(1, 1, 3, 4);
139+
140+
const last = frames.at(-1)!;
141+
// Store's own placement: card 0 in lane 2, card 1 in lane 1.
142+
expect(view.cards.map(c => c.lane)).toEqual([2, 1]);
143+
144+
// No frame ever shows a card in two lanes, and every frame's rendering
145+
// agrees with the store's placement read in the same effect.
146+
for (const frame of frames) {
147+
const seen = new Map<number, number[]>();
148+
frame.lanes.forEach((ids, lane) =>
149+
ids.forEach(id => seen.set(id, [...(seen.get(id) ?? []), lane]))
150+
);
151+
for (const [id, inLanes] of seen) {
152+
expect(
153+
inLanes,
154+
`card ${id} rendered in lanes ${inLanes} — frame ${JSON.stringify(frame)}`
155+
).toHaveLength(1);
156+
}
157+
const fromStore = [[], [], []] as number[][];
158+
frame.store.forEach((lane, id) => fromStore[lane].push(id));
159+
expect(
160+
frame.lanes.map(l => [...l].sort()),
161+
`rendered lanes disagree with the store — frame ${JSON.stringify(frame)}`
162+
).toEqual(fromStore);
163+
}
164+
expect(last.lanes).toEqual([[], [1], [0]]);
165+
166+
// Settle the remaining actions: the merged transaction commits, the
167+
// overrides drop, and the published truth (card 0 in lane 1, card 1 in
168+
// lane 0) reveals to every reader — the lane-pass readers replayed at the
169+
// commit included.
170+
pending.get(2)!();
171+
pending.get(3)!();
172+
pending.get(4)!();
173+
await new Promise(resolve => setTimeout(resolve, 0));
174+
flush();
175+
expect(view.cards.map(c => c.lane)).toEqual([1, 0]);
176+
expect(frames.at(-1)!).toEqual({ lanes: [[1], [0], []], store: [1, 0] });
177+
dispose();
178+
delete (globalThis as any).__move3548;
179+
});
180+
});

0 commit comments

Comments
 (0)