Skip to content

Commit 658eecd

Browse files
brenelzryansolidClaude via Cursorcursoragent
authored
fix(signals): keep a chained optimistic write back to the base's previous value (#3674)
* fix(signals): chained optimistic write to base value is kept (#3672) A chained optimistic store (`createOptimisticStore(base)` over a `createStore`) serves the base's live value; its own nodes are links whose `_value` is never served and never updated when the base commits. The engine's no-op check in optimisticWrite compared the draft's write against that stale `_value`, so once a first action had been confirmed in the base, a second action writing the key back to its earlier value (position 1 then 0), re-adding a key the base had deleted, or popping a row the base had appended emitted no override while the action's other writes showed. notifyOptimisticWrites now syncs a chained node that carries no active override to the visible committed value right before the engine write, on the value, presence and length emits alike. Non-chained families are untouched; the optimistic module is tree-shakeable, and every size scenario stays under its cap. * fix: address review findings The optimistic add and delete branches sync a chained node's `_value` to the unwrapped raw of the inner store's child, not its proxy, so the settle-time revert compare against the override's raw no longer notifies readers of a row that was deleted and re-added in one action. The add and delete branches read `old[key]` once per key instead of running a chained backing's get trap twice. The rules index lists optimistic.ts among §7b's citations, which the `emit` comment's citation had left stale. * fix(signals): chained optimistic revert compares against the base's live value (#3672) The write side (previous commits) handed optimisticWrite's no-op check the visible committed value. The revert consulted the same link `_value`: resolveOptimisticNodes decides whether to notify by comparing the override against `_value`, and a chained node's `_value` is never served, so it only ever held the seed or the last write-time refresh. When the base committed WHILE an override was active (a server confirm arriving mid-action) and the action then wrote the key back to its pre-write value, the compare matched and notified nobody — a `createMemo` over the view stayed at the guess while the base and untracked reads showed the truth. A render effect off the lane was rescued by readsHeldCommitted's replay, which is why the shape hid. Chain `GlobalQueue._resolveOptimistic` in installNextBlockedHalf (the `_transitionBlocked` pattern): for every chained host in the reverting batch, refresh its armed value/length nodes to `unwrapValue(base[key])` and its armed presence nodes to `key in base`, untracked, then delegate. The compare is exact — readers re-derive iff truth differs from the guess, and a confirmed guess settles without re-running them. Stays in the tree-shakeable optimistic module; core floor 0 B. Size: "hydrating + every store primitive family" 31.80 -> 31.87 KB (measured 31,862 B against next's 31,786: -12 B write side, +88 B revert side). Not a frozen floor cap; floor-caps.json unchanged. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Ryan Carniato <[email protected]> Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 55e2022 commit 658eecd

6 files changed

Lines changed: 602 additions & 9 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Fix a chained optimistic store dropping a write back to the base's previous value (#3672)
6+
7+
- `createOptimisticStore(base)` over a `createStore` serves the base's live value; its own nodes are links whose `_value` is never served and never updated when the base commits. The engine's no-op check for an optimistic write compared against that stale `_value`, so once a first action had been confirmed in the base, a second action writing the key back to its earlier value (`position` from 1 back to 0), re-adding a key the base had deleted, or popping a row the base had appended emitted no override while the action's other writes showed. The optimistic setter now syncs a chained node without an active override to the visible committed value before the engine write.
8+
- The revert consulted the same stale `_value`: the settle's notify compare (override vs `_value`) decided whether readers learn the guess lifted. When the base committed while an override was active and the action then wrote the key back to its pre-write value, the compare matched and notified nobody — a `createMemo` over the view stayed at the guess while the base (and untracked reads) showed the truth. The optimistic module now refreshes every armed chained node to the base's live value right before the revert, so the compare is exact: readers re-derive when truth differs from the guess and stay put when a confirmed guess settles.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
3333
| R (OS) | 46 | 2 | 0 | 44 |
3434
| R (PJ) | 37 | 8 | 5 | 28 |
3535
| R (RS) | 38 | 9 | 2 | 27 |
36-
| § | 24 | 14 | 8 | 8 |
36+
| § | 24 | 14 | 9 | 8 |
3737

3838
## Unresolved citations
3939

@@ -390,7 +390,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
390390
| §6c | live | `docs/INTERNALS-STORE-STATE.md:325` | projection.ts×2 store.ts×1 | createProjection.async.test.ts×1 flight-owned-transaction.test.ts×1 | Store-wide status gating (RUL-7) |
391391
| §6d | live | `docs/INTERNALS-STORE-STATE.md:338` | reconcile.ts×1 target.ts×2 | — | Diff reachability (RUL-11) |
392392
| §7 | live | `docs/INTERNALS-STORE-STATE.md:350` | optimistic.ts×1 projection.ts×1 | — | Projections & optimism layering |
393-
| §7b | live | `docs/INTERNALS-STORE-STATE.md:360` | projection.ts×1 reconcile.ts×1 store.ts×11 target.ts×3 store.ts×1 | — | Chained backing (cross-store) — spec |
393+
| §7b | live | `docs/INTERNALS-STORE-STATE.md:360` | optimistic.ts×2 projection.ts×1 reconcile.ts×1 store.ts×11 target.ts×3 store.ts×1 | optimistic-chained-revert-3672-memo.test.ts×1 | Chained backing (cross-store) — spec |
394394
| §8 | live | `docs/INTERNALS-STORE-STATE.md:431` | — | reconcile-resend-identity.test.ts×1 | Assumptions / open questions |
395395
| §8b | live | `docs/INTERNALS-STORE-STATE.md:487` | — | — | Suite-mined rules (2026-08-16) — index & rulings needed |
396396
| §9 | live | `docs/INTERNALS-STORE-STATE.md:724` | — | — | Decision log |

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
getOwner,
3737
isEqual,
3838
setSignal,
39+
untrack,
3940
type Computed,
4041
type Refreshable,
4142
type Signal
@@ -137,6 +138,41 @@ function installNextBlockedHalf(): void {
137138
stores.clear();
138139
};
139140
}
141+
// Revert-side link refresh (#3672, §7b/O6): a chained node's `_value` is
142+
// never served — the base's live value is — so the engine cannot read
143+
// committed truth from it. The engine consults it at exactly two moments:
144+
// optimisticWrite's no-op check (the setter hands it the visible value,
145+
// see `emit` in notifyOptimisticWrites) and resolveOptimisticNodes'
146+
// notify compare (here). Left at the write-time value, a base commit
147+
// during the override — confirm 7, user writes back to the pre-write 5 —
148+
// made the revert compare 5 === 5 and notify nobody while the base read 7:
149+
// a memo over the view stayed at the guess forever (render effects were
150+
// rescued by readsHeldCommitted's replay). Refresh every armed node of
151+
// each chained host in the reverting batch to the base's live value right
152+
// before the compare, so it is exact: notify iff truth differs from the
153+
// guess. Untracked: a flush from inside a computation must not link it.
154+
const resolvePrev = GlobalQueue._resolveOptimistic!;
155+
GlobalQueue._resolveOptimistic = nodes => {
156+
let seen: Set<StoreNextTarget> | null = null;
157+
for (const node of nodes) {
158+
const t: StoreNextTarget | undefined = (node as any)._host;
159+
if (t?.ch !== true || seen?.has(t)) continue;
160+
(seen ??= new Set()).add(t);
161+
untrack(() => {
162+
const base: any = t.v;
163+
for (const k of Reflect.ownKeys(t.n!)) {
164+
const n = t.n![k as any];
165+
if (hasActiveOverride(n)) n._value = unwrapValue(base[k]);
166+
}
167+
if (t.h !== null)
168+
for (const k of Reflect.ownKeys(t.h)) {
169+
const h = t.h[k as any];
170+
if (hasActiveOverride(h)) h._value = k in base;
171+
}
172+
});
173+
}
174+
resolvePrev(nodes);
175+
};
140176
const chained = GlobalQueue._transitionBlocked!;
141177
GlobalQueue._transitionBlocked = transition => {
142178
for (const store of transition._optimisticStores) {
@@ -584,20 +620,29 @@ export function notifyOptimisticWrites(t: StoreNextTarget, pb: Record<PropertyKe
584620
? !!unwrapOverride(node._x?._overrideValue)
585621
: key in old;
586622
};
623+
// Chained nodes are links (§7b, O6): their `_value` is never served and
624+
// never learns the base's commits, yet optimisticWrite's no-op check reads
625+
// it (#3672). Hand it the visible committed value at the write; the revert
626+
// compare gets the same treatment in installNextBlockedHalf.
627+
const emit = (node: Signal<any>, ov: any, nv: any): void => {
628+
if (t.ch && !hasActiveOverride(node)) node._value = ov;
629+
setSignal(node, () => nv);
630+
};
587631
let structural = false;
588632
const isArr = Array.isArray(pb);
589633
for (const key of Reflect.ownKeys(pb)) {
590634
if ((isArr && key === "length") || key === $OWNER) continue;
591635
const nv = unwrapValue(pb[key as any]);
592636
if (!visiblePresent(key)) {
593637
// Optimistic add: value node + presence node + membership bump.
594-
setSignal(getNode(t, key, old[key as any]), () => nv);
595-
setSignal(getHasNode(t, key, key in old), true as any);
638+
const ov = unwrapValue(old[key as any]);
639+
emit(getNode(t, key, ov), ov, nv);
640+
emit(getHasNode(t, key, key in old), key in old, true);
596641
structural = true;
597642
} else {
598643
const ov = visible(key, old[key as any]);
599644
if (!isEqual(ov, nv) && !targetsEqual(ov, nv)) {
600-
setSignal(getNode(t, key, ov), () => nv);
645+
emit(getNode(t, key, ov), ov, nv);
601646
if (isArr) structural = true;
602647
}
603648
}
@@ -606,14 +651,15 @@ export function notifyOptimisticWrites(t: StoreNextTarget, pb: Record<PropertyKe
606651
if ((isArr && key === "length") || key === $OWNER) continue;
607652
if (key in pb || !visiblePresent(key)) continue;
608653
// Optimistic delete: node reads undefined, presence flips, membership bumps.
609-
setSignal(getNode(t, key, old[key as any]), () => undefined);
610-
setSignal(getHasNode(t, key, true), false as any);
654+
const ov = unwrapValue(old[key as any]);
655+
emit(getNode(t, key, ov), ov, undefined);
656+
emit(getHasNode(t, key, true), true, false);
611657
structural = true;
612658
}
613659
if (isArr) {
614660
const oldLen = visible("length", (old as any[]).length);
615661
if (oldLen !== (pb as any[]).length) {
616-
setSignal(getNode(t, "length", oldLen), () => (pb as any[]).length);
662+
emit(getNode(t, "length", oldLen), oldLen, (pb as any[]).length);
617663
structural = true;
618664
}
619665
}

0 commit comments

Comments
 (0)