Skip to content

Commit 4e40669

Browse files
fix(signals): an optimistic write over a held row guesses the row's keys, not its slot (#3796)
An optimistic store chained over a derived store composes its draft from the inner store's raws (the descriptor read of the inner proxy): a row's slot holds the row's committed backing. When an earlier write in the same action staged that row (a local `setLocal` held with the action), the view the setter is judged against resolves the slot to the row's staging instead. The two differ by identity only, so the setter's exit guessed the container's slot itself — with the committed row — and that slot guess served a fresh target over the old raw, shadowing the row's own `title` and `saving` guesses: `Old / saving: false` while the action was pending. A raw in a chained draft is now resolved to the inner slot it stands for — as a read of it is served (`resolveChainedRaw`) — before it is compared, so an unwritten slot is the same row in both frames and no guess. The edit shows while the action is pending (A17: an active override is the displayed value until its transaction commits). Fixes #3796 Co-authored-by: Cursor <[email protected]>
1 parent 277e6d6 commit 4e40669

5 files changed

Lines changed: 82 additions & 3 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 store over a derived store hiding its update after an action's local write to the same row: the row a held write staged is no longer guessed over the container's slot (#3796).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
6161
| A14 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:187` | — | spec-async-semantics.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B2)] Companion nodes get child lanes that do not merge with the owner — (was B2) `isPending`/`latest` companion nodes get child lanes that do not mer… |
6262
| A15 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:223` | async.ts×2 constants.ts×1 core.ts×3 lanes.ts×4 scheduler.ts×14 verdict.ts×3 | async-chain-supersession.test.ts×1 first-observer-stale-reader.test.ts×1 fuzz-findings-l2.test.ts×17 held-restore.test.ts×1 l2-contract.test.ts×7 lane-contract.test.ts×1 lane-hold-on-observation.test.ts×1 lane-outside-view.test.ts×1 lane-pass-stamped-effect-3662.test.ts×1 mount-over-foreign-hold-3761.test.ts×2 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 tick-scoped-pass-transaction.test.ts×1 treeshake.test.ts×4 visibility-oracle-store.states.ts×6 visibility-oracle.states.ts×7 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:195` | scheduler.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… |
64-
| A17 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:53` | async.ts×3 constants.ts×1 lanes.ts×6 scheduler.ts×2 verdict.ts×1 map.ts×1 store.ts×3 | fuzz-findings-l2.test.ts×6 lane-uninitialized-landing-3648.test.ts×5 optimistic-read-lane-not-transaction-3698.test.ts×2 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×2 kanban-a17-fixture.test.ts×3 optimistic-list-mutation-matrix.test.ts×1 optimistic-maparray-index-frame-f1.test.ts×1 optimistic-untracked-reads-f3-f5.test.ts×1 signal-store-twins-qd.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×20 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… |
64+
| A17 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:53` | async.ts×3 constants.ts×1 lanes.ts×6 scheduler.ts×2 verdict.ts×1 map.ts×1 store.ts×3 | fuzz-findings-l2.test.ts×6 lane-uninitialized-landing-3648.test.ts×5 optimistic-over-held-row-3796.test.ts×1 optimistic-read-lane-not-transaction-3698.test.ts×2 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×2 kanban-a17-fixture.test.ts×3 optimistic-list-mutation-matrix.test.ts×1 optimistic-maparray-index-frame-f1.test.ts×1 optimistic-untracked-reads-f3-f5.test.ts×1 signal-store-twins-qd.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×20 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… |
6565
| A18 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:63` | action.ts×1 async.ts×2 core.ts×3 lanes.ts×8 scheduler.ts×2 types.ts×1 verdict.ts×1 map.ts×1 optimistic.ts×1 projection.ts×1 | body-end-supersession-visibility.test.ts×4 createOptimistic.test.ts×3 lane-contract.test.ts×1 lane-frame-deferred-run-3662.test.ts×1 lane-outside-view.test.ts×1 lane-uninitialized-landing-3648.test.ts×5 optimistic-move-duplicate-3548.test.ts×2 optimistic-read-lane-not-transaction-3698.test.ts×4 posture-store-parity.test.ts×5 spec-async-semantics.test.ts×3 flight-owned-transaction.test.ts×1 optimistic-list-mutation-matrix.test.ts×1 optimistic-untracked-reads-f3-f5.test.ts×1 signal-store-twins-qd.test.ts×1 unchanged-presence-no-hold-3743.test.ts×2 superseded-before-first-commit.test.ts×4 visibility-oracle-store.states.ts×8 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×19 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 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:123` | async.ts×1 constants.ts×1 core.ts×6 scheduler.ts×2 types.ts×1 verdict.ts×4 store.ts×1 | fuzz-findings-l2.test.ts×4 lane-uninitialized-landing-3648.test.ts×2 mount-over-foreign-hold-3761.test.ts×2 spec-async-semantics.test.ts×3 derived-presence-async-3726.test.ts×2 superseded-before-first-commit.test.ts×3 uninitialized-visibility.test.ts×1 visibility-oracle-store.states.ts×8 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×14 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled, amended in place 2026-07-07 (promoted from C1)] `isPending(x)` ≡ the observable value is not final (three causes) — (was C1 — **partially reverses an earlier decision**) **Definition: `isPendi… |
6767
| A20 | superseded | `docs/SPEC-ASYNC-SEMANTICS.md:382` | — | 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… |

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
installOptHooks,
5454
LaneView,
5555
nameStore,
56+
resolveChainedRaw,
5657
sameKey,
5758
storeSetter,
5859
targetsEqual,
@@ -115,7 +116,12 @@ export function notifyOptimisticWrites(
115116
let structural = false;
116117
for (const key of Reflect.ownKeys(pb)) {
117118
if ((isArr && key === "length") || key === $OWNER) continue;
118-
const nv = unwrapValue(pb[key as any]);
119+
// A chained draft holds the inner store's raws; a raw is the inner slot it
120+
// resolves to (as a read of it serves) — the row a held write staged is
121+
// the same row, not a guess over the slot (#3796).
122+
let nv = pb[key as any];
123+
if (inner && nv) nv = resolveChainedRaw(t, key, nv);
124+
nv = unwrapValue(nv);
119125
if (!(key in old)) {
120126
guess(getNode(t, key), key, false, nv);
121127
guess(getHasNode(t, key), key, true, true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ function holdWithDerive(target: StoreTarget, node: Signal<any>): void {
18121812
* at this key, is served as the inner store's wrapper — never a fresh
18131813
* raw-keyed one — so the chained targets are the same objects across
18141814
* settled and pending views. */
1815-
function resolveChainedRaw(target: StoreTarget, key: PropertyKey, v: object): any {
1815+
export function resolveChainedRaw(target: StoreTarget, key: PropertyKey, v: object): any {
18161816
const innerT: StoreTarget = (target.v as any)[$TARGET];
18171817
if (innerT.ch) {
18181818
const iv = resolveChainedRaw(innerT, key, v);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
action,
3+
createOptimisticStore,
4+
createRenderEffect,
5+
createRoot,
6+
createSignal,
7+
createStore,
8+
flush
9+
} from "../src/index.js";
10+
11+
const tick = () => new Promise<void>(r => setTimeout(r, 0));
12+
async function drain(n = 3) {
13+
for (let i = 0; i < n; i++) {
14+
await tick();
15+
flush();
16+
}
17+
}
18+
19+
type Card = { id: number; title: string; failed?: boolean; saving?: boolean };
20+
21+
// #3796: an action's write to a row of the derived store an optimistic store
22+
// wraps holds that row's staging; the optimistic write that follows edits the
23+
// row's keys, not the container's slot — the slot is the same row in both
24+
// frames and is no guess. The edit shows while the action is pending (A17).
25+
describe("optimistic store over a row the action holds (#3796)", () => {
26+
for (const localWrite of [true, false])
27+
it(`shows the guesses while pending${localWrite ? " after a local write" : ""}`, async () => {
28+
const shown: string[] = [];
29+
let save!: () => Promise<void>;
30+
let complete!: () => void;
31+
createRoot(() => {
32+
const [server, setServer] = createSignal<Card[]>([{ id: 1, title: "Old" }]);
33+
const [local, setLocal] = createStore<Card[]>(draft => {
34+
const s = server();
35+
draft.length = 0;
36+
for (const r of s) draft.push({ ...r, failed: false });
37+
}, []);
38+
const [cards, setOptimistic] = createOptimisticStore(local);
39+
createRenderEffect(
40+
() => `${cards[0]?.title} / saving: ${!!cards[0]?.saving}`,
41+
v => {
42+
shown.push(v);
43+
}
44+
);
45+
save = action(function* () {
46+
if (localWrite)
47+
setLocal(draft => {
48+
draft[0].failed = false;
49+
});
50+
setOptimistic(draft => {
51+
draft[0].title = "New";
52+
draft[0].saving = true;
53+
});
54+
yield new Promise<void>(r => (complete = r));
55+
setServer([{ id: 1, title: "New" }]);
56+
});
57+
});
58+
flush();
59+
expect(shown).toEqual(["Old / saving: false"]);
60+
const p = save();
61+
await drain();
62+
expect(shown).toEqual(["Old / saving: false", "New / saving: true"]);
63+
complete();
64+
await p;
65+
await drain();
66+
expect(shown.at(-1)).toBe("New / saving: false");
67+
});
68+
});

0 commit comments

Comments
 (0)