Skip to content

Commit e371645

Browse files
authored
Merge pull request #4039 from solidjs/fix/per-setter-written-keys-u8
fix(signals): a later mainline store setter that does not write a held key publishes on the mainline tick
2 parents d1cb801 + 7afc8d3 commit e371645

7 files changed

Lines changed: 123 additions & 86 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(signals): a later mainline store setter that does not write a held key publishes on the mainline tick — a setter's written keys are its own set, so its notify visits only the keys it wrote (D-8, U-8, #3743)

‎packages/signals/docs/OPEN-QUESTIONS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Each is pinned `it.fails`; the ruled screen is in `SCENARIOS.md`. Confirming her
1919
| **U-5** | D-9 — a landing does not reveal a gate its stale reader re-derives onto a new flight (A15 reveal corollary) | `fuzz-findings-l2.test.ts`, fuzzer finding F2 | it reveals | not measured |
2020
| **U-6** | D-26 — a stale reader of a held re-guess shows the revealed guess, not the unrevealed one (A17, #3460) | `fuzz-findings-l2.test.ts`, fuzzer finding F5b | shows `1,2` where `1,1` is ruled | needs a per-node slot for the revealed lane value |
2121
| **U-7** | D-30 — a store row first read after an adoption derives nothing from the hold (#3706) | `adoption-unchanged-key-read-3706.test.ts:278` | built (#3986): the row is paired at that read by the family's key, as the adoption would have paired it; the pin is a plain `it` | not measured |
22-
| **U-8** | D-8 — a later mainline setter that does not write the held key publishes on the mainline tick (#3743) | `store/unchanged-presence-no-hold-3743.test.ts:491` | it is held | not measured |
22+
| **U-8** | D-8 — a later mainline setter that does not write the held key publishes on the mainline tick (#3743) | `store/unchanged-presence-no-hold-3743.test.ts:491` | built (2026-10-13): a setter's written keys are its own set, the batch's earlier chained behind (`touch`, store.ts); the notify visits this setter's keys, the fold keeps the batch's union | −41 B brotli / −14 B minified on `+ createStore` |
2323
| **U-9** | D-14 — a held tree under a re-armed boundary re-derives without a redraw loop | `loading-fallback-in-flush-3540.test.ts:1020` | a redraw loop | Q-2 ruled 2026-10-09 (D-16); the loop is still the deviation |
2424

2525
**Answer format that is enough:** `U-1, U-3–U-9 stand, U-6 after 2.0` — or the line you prefer. U-2 is withdrawn with the direction rule's time half (once drafted as F-8; F-2). Q-1 through Q-5 are ruled and carried by the rules. C-1 is D-12. C-2 is D-7. C-3 is D-32.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ An `action`'s writes are held until every body in the transaction has returned a
6868

6969
While a hold has a proposal for a slot (a staged, unrevealed value), a further write to that slot — the same value or another — is a second suggestion for the same slot, and the two cannot finish at different times: the writer's tick joins the hold and reveals with it. A tick whose writes net to the slot's committed value made no proposal: the slot is not held, pends nothing, and a later write to it is a plain mainline write. A held _derivation_ is not a proposal: a mainline write to a node whose held value is a pass result becomes `prev` for the hold's re-derivation and does not suppress it (`setB(101)` mid-hold, `b` re-derives to `200` under the hold; a `prev`-reading derivation folds the write); a second _write_ is still last-write-wins. Writes apply first, then derivations re-run.
7070

71-
**Collapses:** [A34](./SPEC-ASYNC-SEMANTICS.md#a34-a-write-is-a-proposal-one-on-a-held-node-entangles-its-tick-one-that-nets-to-the-committed-value-is-none) (1)–(3), rule B (#3733), #3743 (a store fold's notifications are diffs). **Status:** standing; one store shape pinned `it.fails` (`store/unchanged-presence-no-hold-3743.test.ts`, "a later mainline setter that does not write the held key publishes on the mainline tick").
71+
**Collapses:** [A34](./SPEC-ASYNC-SEMANTICS.md#a34-a-write-is-a-proposal-one-on-a-held-node-entangles-its-tick-one-that-nets-to-the-committed-value-is-none) (1)–(3), rule B (#3733), #3743 (a store fold's notifications are diffs). **Status:** standing. The store shape — a later mainline setter that does not write the held key publishes on the mainline tick — is built (U-8, 2026-10-13: a setter's written keys are its own set, the batch's earlier setters' chained behind it, so the notify visits this setter's keys and the fold keeps the batch's union; `store/unchanged-presence-no-hold-3743.test.ts`).
7272

7373
### D-9. Entanglement is graph-driven; what shares an on-screen async derivation settles as one
7474

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ import {
102102
storeLookup,
103103
storeOwners,
104104
type StoreFamily,
105-
type StoreTarget
105+
type StoreTarget,
106+
type WrittenKeys
106107
} from "./target.js";
107108
import { affectsHooks, optHooks } from "./hooks.js";
108109
import {
@@ -627,7 +628,7 @@ function scanAccessorsOnce(target: StoreTarget): boolean {
627628
const OVERLAY_MIN_KEYS = 32;
628629
const OVERLAY_REBUILD_MAX_KEYS = 1024;
629630
const OVERLAY_REBUILD_MIN_ADDS = 16;
630-
const WK_ALL: Set<PropertyKey> = new Set();
631+
const WK_ALL = new Set() as WrittenKeys;
631632

632633
const plainProto = (o: object): boolean => {
633634
const p = Object.getPrototypeOf(o);
@@ -640,7 +641,8 @@ function overlayRebuilds(t: StoreTarget, pb: Record<PropertyKey, any>): boolean
640641
if (t.kc > OVERLAY_REBUILD_MAX_KEYS) return false;
641642
if (t.del !== null && t.del.size !== 0) return true;
642643
const wk = t.wk;
643-
if (wk !== null && wk !== WK_ALL && wk.size <= OVERLAY_REBUILD_MIN_ADDS) return false;
644+
if (wk !== null && wk !== WK_ALL && wk.u === undefined && wk.size <= OVERLAY_REBUILD_MIN_ADDS)
645+
return false;
644646
const v = t.v;
645647
let adds = 0;
646648
for (const key of Reflect.ownKeys(pb))
@@ -974,10 +976,11 @@ function foldTarget(t: StoreTarget, old: Record<PropertyKey, any>): void {
974976
const v = t.v;
975977
const wk = t.wk;
976978
if (wk !== null && wk !== WK_ALL) {
977-
for (const key of wk) {
978-
if (hasOwn.call(pb, key)) copyOwn(v, pb, key);
979-
else delete (v as any)[key];
980-
}
979+
for (let w: WrittenKeys | undefined = wk; w !== undefined; w = w.u)
980+
for (const key of w) {
981+
if (hasOwn.call(pb, key)) copyOwn(v, pb, key);
982+
else delete (v as any)[key];
983+
}
981984
} else {
982985
for (const key of Reflect.ownKeys(pb)) {
983986
const d = Object.getOwnPropertyDescriptor(pb, key)!;
@@ -1160,7 +1163,10 @@ function notifyWrites(t: StoreTarget): void {
11601163
// value can change when ANY key is written), or a non-plain prototype
11611164
// (class instances: prototype getters derive from arbitrary fields).
11621165
// Overlay pbs chain to the COMMITTED object (#3044) — plainness is the
1163-
// committed container's prototype, not the overlay's.
1166+
// committed container's prototype, not the overlay's. THIS setter's keys
1167+
// only (D-8, #3743): a key an earlier setter of the batch wrote and this
1168+
// one did not is no second proposal — its node was told then, and telling
1169+
// a held node its unchanged value again would join this tick to the hold.
11641170
const wk0 = t.wk;
11651171
const writtenKeys =
11661172
wk0 === WK_ALL || t.a === true || !plainProto(t.ovl ? (t.v as object) : pb) ? null : wk0;
@@ -1896,6 +1902,23 @@ const UNSAFE_KEYS = new Set<PropertyKey>(["__proto__", "prototype", "constructor
18961902
// ---------------------------------------------------------------------------
18971903
// traps
18981904

1905+
/** A trap write: the target is told at the setter's exit, the key recorded
1906+
* in this setter's `wk`. A setter's first write to a target still staged by
1907+
* an earlier setter of the batch opens a set of its own (the notify visits
1908+
* it, D-8) and chains the earlier one behind for the fold's union. */
1909+
function touch(target: StoreTarget, key: PropertyKey): void {
1910+
let wk = target.wk;
1911+
if (!pendingNotify.has(target)) {
1912+
pendingNotify.add(target);
1913+
if (wk !== null && wk !== WK_ALL) {
1914+
wk = new Set() as WrittenKeys;
1915+
wk.u = target.wk!;
1916+
target.wk = wk;
1917+
}
1918+
}
1919+
if (wk !== WK_ALL) (target.wk ??= new Set() as WrittenKeys).add(key);
1920+
}
1921+
18991922
const traps: ProxyHandler<StoreTarget> = {
19001923
get(target, key, receiver) {
19011924
// One typeof gates every brand-symbol compare off the hot string path.
@@ -2147,16 +2170,12 @@ const traps: ProxyHandler<StoreTarget> = {
21472170
if (key === "__proto__") return true; // pollution guard (core R30)
21482171
const uv = target.s ? value : unwrapValue(value);
21492172
const pb = ensurePB(target);
2150-
pendingNotify.add(target);
21512173
if (Array.isArray(pb)) {
2174+
touch(target, key);
21522175
if (key === "length") target.wk = WK_ALL;
2153-
else if (target.wk !== WK_ALL) {
2154-
const wk = (target.wk ??= new Set());
2155-
wk.add(key);
2156-
wk.add("length");
2157-
}
2176+
else touch(target, "length");
21582177
} else {
2159-
if (target.wk !== WK_ALL) (target.wk ??= new Set()).add(key);
2178+
touch(target, key);
21602179
if (!(key in pb)) target.kc++;
21612180
}
21622181
if (UNSAFE_KEYS.has(key)) {
@@ -2192,8 +2211,7 @@ const traps: ProxyHandler<StoreTarget> = {
21922211
if ("value" in desc) desc = { ...desc, value: unwrapValue(desc.value) };
21932212
const pb = ensurePB(target);
21942213
if (target.a || !(desc.enumerable && desc.writable && desc.configurable)) target.sc = 1;
2195-
pendingNotify.add(target);
2196-
if (target.wk !== WK_ALL) (target.wk ??= new Set()).add(key);
2214+
touch(target, key);
21972215
Object.defineProperty(pb, key, desc);
21982216
if (target.del !== null) target.del.delete(key);
21992217
if (override) notifyWrites(target);
@@ -2205,8 +2223,7 @@ const traps: ProxyHandler<StoreTarget> = {
22052223
const override = !draft && writeOverride;
22062224
if (!draft && !override) return true;
22072225
const pb = ensurePB(target);
2208-
pendingNotify.add(target);
2209-
if (target.wk !== WK_ALL) (target.wk ??= new Set()).add(key);
2226+
touch(target, key);
22102227
delete pb[key as any];
22112228
if (target.ovl && hasOwn.call(target.v, key)) (target.del ??= new Set()).add(key);
22122229
if (override) notifyWrites(target);

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface StoreFamily {
4444
woke?: boolean;
4545
}
4646

47+
/** One setter's written keys; `u` is the batch's earlier setters' (the
48+
* fold's union), present only once a second setter writes the target. */
49+
export interface WrittenKeys extends Set<PropertyKey> {
50+
u?: WrittenKeys;
51+
}
52+
4753
export interface StoreTarget {
4854
/** Committed backing: source object (shared) or owned clone. */
4955
v: Record<PropertyKey, any>;
@@ -61,19 +67,22 @@ export interface StoreTarget {
6167
/** Lazy CONTAINER node: membership/iteration/`$TRACK`/`length`
6268
* subscriptions (§6) and the backing's staging home (plan sec. 31.3). */
6369
k: Signal<any> | null;
64-
/** Keys written through the traps since the last fold commit. Bounds the
65-
* setter notify/hold-check to O(written) instead of O(subscribed nodes) —
66-
* a record with thousands of per-key subscriptions (selection maps) would
67-
* otherwise pay a full node scan on every write. null = no trap writes
68-
* this batch (bulk paths fall back to the full scan); WK_ALL = bound
69-
* unusable (array length write). LOAD-BEARING SHAPE RULE: array proxy
70-
* targets carry their fields as named properties on a real array, and V8
70+
/** Keys written through the traps by the current setter, with the earlier
71+
* setters' of the batch chained behind (`u`) until the fold commit. Bounds
72+
* the setter notify/hold-check to O(written) instead of O(subscribed
73+
* nodes) — a record with thousands of per-key subscriptions (selection
74+
* maps) would otherwise pay a full node scan on every write — and to THIS
75+
* setter's keys (D-8: a key an earlier setter of the batch wrote is told
76+
* once, #3743). null = no trap writes this batch (bulk paths fall back to
77+
* the full scan); WK_ALL = bound unusable (array length write).
78+
* LOAD-BEARING SHAPE RULE: array proxy targets carry their fields as named
79+
* properties on a real array, and V8
7180
* normalizes an array to dictionary properties as the named count grows
7281
* (empirically at counts ≡ 0 mod 3 from 18 up on V8 13.x) — every trap
7382
* field read then becomes a hash lookup (~15% uibench, tree suites
7483
* worst). Future write-side state MUST ride an extension object, not new
7584
* named fields. */
76-
wk: Set<PropertyKey> | null;
85+
wk: WrittenKeys | null;
7786
/** Lazy deep-witness node: `deep()` subscribes ONE node per record instead
7887
* of one per path; write paths bump it only when it exists. Separate from
7988
* `k` so $TRACK/mapArray never rerun on leaf value changes (R9). */

‎packages/signals/tests/store/unchanged-presence-no-hold-3743.test.ts‎

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -481,63 +481,60 @@ describe("#3743 unchanged presence under an open action", () => {
481481
// mid-hold writes `a.value` and `b.value` while an action holds `b.flag`.
482482
// A34 (1): an explicit repeat of the held key is a proposal and joins; a
483483
// key the setter did NOT write is not — `a.value` publishes with the
484-
// mainline tick, `b.flag` stays held. Current L2 joins: the written keys
485-
// `notifyWrites` visits are `t.wk`, retained for the pending fold and
486-
// CUMULATIVE across the batch's setters, so the later setter re-notifies
487-
// the held `flag` node with its unchanged value and `setSignal` joins
488-
// before the equality gate (the write is a proposal). The fix direction is
489-
// per-setter written keys: the notify visits the keys THIS setter wrote,
490-
// the fold keeps the batch's union.
491-
it.fails(
492-
"a later mainline setter that does not write the held key publishes on the mainline tick",
493-
async () => {
494-
type State = { a: { value: number }; b: { value: number; flag: boolean } };
495-
const values: number[] = [];
496-
const flags: boolean[] = [];
497-
let state!: State;
498-
let setState!: (fn: (draft: State) => void) => void;
499-
createRoot(() => {
500-
[state, setState] = createStore<State>({ a: { value: 0 }, b: { value: 0, flag: true } });
501-
createRenderEffect(
502-
() => state.a.value,
503-
v => void values.push(v)
504-
);
505-
createRenderEffect(
506-
() => state.b.flag,
507-
v => void flags.push(v)
508-
);
509-
});
510-
flush();
511-
expect(values).toEqual([0]);
512-
expect(flags).toEqual([true]);
513-
514-
const gate = deferred();
515-
const toggle = action(function* toggle() {
516-
setState(draft => {
517-
draft.b.flag = false;
518-
});
519-
yield gate.promise;
520-
});
521-
const run = toggle();
522-
flush();
523-
expect(flags).toEqual([true]);
484+
// mainline tick, `b.flag` stays held. L2 joined once: the written keys
485+
// `notifyWrites` visited were `t.wk`, retained for the pending fold and
486+
// cumulative across the batch's setters, so the later setter re-notified
487+
// the held `flag` node with its unchanged value and `setSignal` joined
488+
// before the equality gate (the write is a proposal). A setter's written
489+
// keys are its own set now, the batch's earlier chained behind (U-8): the
490+
// notify visits the keys THIS setter wrote, the fold keeps the union.
491+
it("a later mainline setter that does not write the held key publishes on the mainline tick", async () => {
492+
type State = { a: { value: number }; b: { value: number; flag: boolean } };
493+
const values: number[] = [];
494+
const flags: boolean[] = [];
495+
let state!: State;
496+
let setState!: (fn: (draft: State) => void) => void;
497+
createRoot(() => {
498+
[state, setState] = createStore<State>({ a: { value: 0 }, b: { value: 0, flag: true } });
499+
createRenderEffect(
500+
() => state.a.value,
501+
v => void values.push(v)
502+
);
503+
createRenderEffect(
504+
() => state.b.flag,
505+
v => void flags.push(v)
506+
);
507+
});
508+
flush();
509+
expect(values).toEqual([0]);
510+
expect(flags).toEqual([true]);
524511

512+
const gate = deferred();
513+
const toggle = action(function* toggle() {
525514
setState(draft => {
526-
draft.a.value = 1;
527-
draft.b.value = 1;
515+
draft.b.flag = false;
528516
});
529-
flush();
530-
// Spec: `a.value` is nobody's proposal — it publishes now; the held
531-
// key stays held.
532-
expect(values).toEqual([0, 1]);
533-
expect(flags).toEqual([true]);
517+
yield gate.promise;
518+
});
519+
const run = toggle();
520+
flush();
521+
expect(flags).toEqual([true]);
534522

535-
gate.resolve();
536-
await run;
537-
await Promise.resolve();
538-
flush();
539-
expect(values).toEqual([0, 1]);
540-
expect(flags).toEqual([true, false]);
541-
}
542-
);
523+
setState(draft => {
524+
draft.a.value = 1;
525+
draft.b.value = 1;
526+
});
527+
flush();
528+
// Spec: `a.value` is nobody's proposal — it publishes now; the held
529+
// key stays held.
530+
expect(values).toEqual([0, 1]);
531+
expect(flags).toEqual([true]);
532+
533+
gate.resolve();
534+
await run;
535+
await Promise.resolve();
536+
flush();
537+
expect(values).toEqual([0, 1]);
538+
expect(flags).toEqual([true, false]);
539+
});
543540
});

‎scripts/size/scenarios.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,8 +2406,17 @@ module.exports = [
24062406
// minified, 93,304 -> 93,338) — the same `lazy()`/`lazyModule()` guard
24072407
// as the compiled hydrating note. Cap set at measured + 10 B rounded up
24082408
// to 0.01 KB. Accepted by the maintainer 2026-10-13.
2409-
limit: "29.52 KB",
2410-
capMinified: 93338,
2409+
// Size-Exception (a later store setter's written keys are its own set,
2410+
// D-8/U-8, #4039, 2026-10-14): 29.52 -> 29.57 KB, CI measured 29,559 B
2411+
// against `next` @ d1cb8013's 29,509 (+50 B; 39 B over the cap) with
2412+
// −16 B minified (93,396 -> 93,380). The minified growth over the
2413+
// recorded 93,338 is `next`'s own (+58 B from the U-4 `arm` sweep, #4031,
2414+
// which stayed under this cap); this PR's bytes are brotli layout against
2415+
// that base. Cap set at measured + 10 B rounded up to 0.01 KB, recorded
2416+
// minified at the head's bytes. Accepted by the maintainer 2026-10-14
2417+
// (the floors unchanged, 0 B).
2418+
limit: "29.57 KB",
2419+
capMinified: 93380,
24112420
alias
24122421
},
24132422
{

0 commit comments

Comments
 (0)