@solidjs/signals 2.0.0-rc.13 → 2.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist/dev-shared.js +2000 -3768
  2. package/dist/dev.attribution.js +132 -79
  3. package/dist/dev.js +7226 -8488
  4. package/dist/observe/affects.js +125 -104
  5. package/dist/observe/boundaries.js +458 -665
  6. package/dist/observe/core/action.js +63 -66
  7. package/dist/observe/core/async.js +240 -383
  8. package/dist/observe/core/attribution-queries.js +1 -1
  9. package/dist/observe/core/attribution.js +240 -183
  10. package/dist/observe/core/constants.js +115 -187
  11. package/dist/observe/core/context.js +3 -3
  12. package/dist/observe/core/core.js +840 -1438
  13. package/dist/observe/core/dev.js +3 -3
  14. package/dist/observe/core/effect.js +75 -86
  15. package/dist/observe/core/external.js +5 -5
  16. package/dist/observe/core/graph.js +61 -70
  17. package/dist/observe/core/heap.js +75 -98
  18. package/dist/observe/core/invariants.js +1 -34
  19. package/dist/observe/core/lanes.js +704 -181
  20. package/dist/observe/core/owner.js +80 -101
  21. package/dist/observe/core/scheduler.js +708 -1351
  22. package/dist/observe/core/verdict.js +302 -621
  23. package/dist/observe/flatten.js +79 -0
  24. package/dist/observe/index.js +15 -13
  25. package/dist/observe/map.js +232 -235
  26. package/dist/observe/reveal.js +212 -0
  27. package/dist/observe/signals.js +133 -215
  28. package/dist/observe/store/affects.js +135 -0
  29. package/dist/observe/store/index.js +29 -24
  30. package/dist/observe/store/optimistic.js +538 -0
  31. package/dist/observe/store/projection.js +302 -0
  32. package/dist/observe/store/reconcile.js +257 -0
  33. package/dist/observe/store/store.js +1945 -277
  34. package/dist/observe/store/storePath.js +1 -1
  35. package/dist/{prod/store/next → observe/store}/target.js +17 -18
  36. package/dist/observe/store/types.js +73 -0
  37. package/dist/observe/store/utils.js +1 -13
  38. package/dist/prod/affects.js +127 -106
  39. package/dist/prod/boundaries.js +442 -654
  40. package/dist/prod/core/action.js +71 -74
  41. package/dist/prod/core/async.js +307 -442
  42. package/dist/prod/core/constants.js +112 -186
  43. package/dist/prod/core/context.js +3 -3
  44. package/dist/prod/core/core.js +852 -1458
  45. package/dist/prod/core/effect.js +88 -99
  46. package/dist/prod/core/external.js +5 -5
  47. package/dist/prod/core/graph.js +61 -70
  48. package/dist/prod/core/heap.js +75 -98
  49. package/dist/prod/core/invariants.js +1 -32
  50. package/dist/prod/core/lanes.js +666 -181
  51. package/dist/prod/core/owner.js +75 -96
  52. package/dist/prod/core/scheduler.js +677 -1332
  53. package/dist/prod/core/verdict.js +297 -621
  54. package/dist/prod/flatten.js +77 -0
  55. package/dist/prod/index.js +15 -13
  56. package/dist/prod/map.js +143 -146
  57. package/dist/prod/reveal.js +212 -0
  58. package/dist/prod/signals.js +144 -226
  59. package/dist/prod/store/affects.js +135 -0
  60. package/dist/prod/store/index.js +28 -19
  61. package/dist/prod/store/optimistic.js +533 -0
  62. package/dist/prod/store/projection.js +295 -0
  63. package/dist/prod/store/reconcile.js +257 -0
  64. package/dist/prod/store/store.js +1840 -272
  65. package/dist/prod/store/storePath.js +1 -1
  66. package/dist/{observe/store/next → prod/store}/target.js +13 -17
  67. package/dist/prod/store/types.js +73 -0
  68. package/dist/prod/store/utils.js +1 -11
  69. package/dist/types/affects.d.ts +11 -30
  70. package/dist/types/boundaries.d.ts +62 -176
  71. package/dist/types/carved.d.ts +1 -0
  72. package/dist/types/core/action.d.ts +5 -6
  73. package/dist/types/core/async.d.ts +10 -5
  74. package/dist/types/core/attribution-hooks.d.ts +20 -1
  75. package/dist/types/core/attribution.d.ts +20 -9
  76. package/dist/types/core/constants.d.ts +125 -180
  77. package/dist/types/core/core.d.ts +104 -264
  78. package/dist/types/core/dev.d.ts +2 -2
  79. package/dist/types/core/effect.d.ts +0 -7
  80. package/dist/types/core/graph.d.ts +7 -1
  81. package/dist/types/core/heap.d.ts +5 -7
  82. package/dist/types/core/index.d.ts +5 -5
  83. package/dist/types/core/invariants.d.ts +0 -29
  84. package/dist/types/core/lanes.d.ts +65 -87
  85. package/dist/types/core/owner.d.ts +3 -3
  86. package/dist/types/core/scheduler.d.ts +249 -331
  87. package/dist/types/core/types.d.ts +51 -85
  88. package/dist/types/core/verdict.d.ts +3 -0
  89. package/dist/types/flatten.d.ts +8 -0
  90. package/dist/types/index.d.ts +6 -4
  91. package/dist/types/reveal.d.ts +19 -0
  92. package/dist/types/signals.d.ts +35 -91
  93. package/dist/types/store/affects.d.ts +1 -0
  94. package/dist/types/store/index.d.ts +35 -15
  95. package/dist/types/store/optimistic.d.ts +23 -0
  96. package/dist/types/store/projection.d.ts +18 -0
  97. package/dist/types/store/reconcile.d.ts +3 -0
  98. package/dist/types/store/store.d.ts +165 -143
  99. package/dist/types/store/storePath.d.ts +1 -1
  100. package/dist/types/store/target.d.ts +151 -0
  101. package/dist/types/store/types.d.ts +84 -0
  102. package/package.json +1 -1
  103. package/dist/observe/core/optimistic.js +0 -669
  104. package/dist/observe/store/next/optimistic.js +0 -717
  105. package/dist/observe/store/next/projection.js +0 -266
  106. package/dist/observe/store/next/reconcile.js +0 -375
  107. package/dist/observe/store/next/store.js +0 -2608
  108. package/dist/prod/core/optimistic.js +0 -658
  109. package/dist/prod/store/next/optimistic.js +0 -710
  110. package/dist/prod/store/next/projection.js +0 -257
  111. package/dist/prod/store/next/reconcile.js +0 -373
  112. package/dist/prod/store/next/store.js +0 -2484
  113. package/dist/types/core/optimistic.d.ts +0 -6
  114. package/dist/types/store/next/optimistic.d.ts +0 -25
  115. package/dist/types/store/next/projection.d.ts +0 -27
  116. package/dist/types/store/next/reconcile.d.ts +0 -10
  117. package/dist/types/store/next/store.d.ts +0 -145
  118. package/dist/types/store/next/target.d.ts +0 -196
@@ -1,329 +1,1897 @@
1
- import "../core/core.js";
1
+ import { CONFIG_MANUAL_WRITE, CONFIG_OVERRIDE, CONFIG_HELD, $REFRESH, NOT_PENDING, STATUS_PENDING, STATUS_ERROR, EFFECT_RENDER, REACTIVE_RECOMPUTING_DEPS, REACTIVE_STAGED_READ, CONFIG_VERDICT, CONFIG_CHILDREN_FORBIDDEN, CONFIG_PROMOTED, REACTIVE_DIRTY, REACTIVE_CHECK } from "../core/constants.js";
2
2
 
3
- import { GlobalQueue } from "../core/scheduler.js";
3
+ import { setSignal, isEqual, read, verdict, context, notePromotedWrite, untrack, slotSignal, stagedScreen, stagedRead } from "../core/core.js";
4
4
 
5
- import "../core/invariants.js";
5
+ import { setSlotUnobserved } from "../core/graph.js";
6
6
 
7
- import "../core/verdict.js";
7
+ import { getObserver } from "../core/owner.js";
8
8
 
9
- import "../core/effect.js";
9
+ import { insertIntoHeap } from "../core/heap.js";
10
10
 
11
- import { $OWNER, lookupTarget as lookupTarget$1 } from "./next/target.js";
11
+ import { txOf, flushTransaction, holdNode, joinFuture, insertSubs, schedule, passLane, globalQueue, queuePendingNode, GlobalQueue, joinPassTx, dirtyQueue } from "../core/scheduler.js";
12
+
13
+ import { lookupTarget, setLookup, $OWNER, isOwned, markDescendants, devAssertNeverUserMutation } from "./target.js";
14
+
15
+ import { $TARGET, rawValuesUsed, isRawValue, isWrappable, markRawIngest, writeOverride, markRawOne, $RECORD, $PROXY, $TRACK } from "./types.js";
16
+
17
+ export { markRaw } from "./types.js";
12
18
 
13
19
  /**
14
- * Brand symbols used internally by the store proxy / projection plumbing.
15
- * Cross-package wiring; not part of the user-facing API.
20
+ * Store — plain stores on L2 (plan sec. 31, S1).
21
+ *
22
+ * A store is a tree of L2 nodes and nothing else carries reactive state:
23
+ *
24
+ * - **Leaf nodes** (one per tracked-or-written key, lazy; `slotSignal`, one
25
+ * literal): `_value` committed, `_pendingValue` this batch's staging.
26
+ * - **Presence nodes** (`in` tracks presence, not value — R13) and the
27
+ * **deep witness** (`deep()` subscribes one node per record).
28
+ * - The **container node** `k` (plan sec. 31.3, Q-A): the `$TRACK` node given a
29
+ * value. `_value` is the committed backing, `_pendingValue` the pending
30
+ * backing the draft mutates — so a write to a key nobody reads has a
31
+ * staging home the scheduler owns, and park/commit/revert of the backing
32
+ * is the hold model's, not a fold ledger of the store's. Structural
33
+ * readers (`ownKeys`, iteration, `$TRACK`, `length`, `deep`) subscribe to
34
+ * it; value readers never do — a key with no leaf reads through the
35
+ * container's visible backing without subscribing (R1: node existence is
36
+ * unobservable).
16
37
  *
17
- * @internal
18
- */ const $TRACK = Symbol(0), $TARGET = Symbol(0), $PROXY = Symbol(0),
19
- // The view record behind a merge()/omit() proxy (a `MergeView` or an
20
- // `OmitView`, utils.ts); a store answers `undefined` on its symbol fast
21
- // path. One read classifies any `$PROXY`-marked object — store, view, or
22
- // foreign — where asking `$TARGET` first and then each view kind was two
23
- // to four trap hops (see `viewOf`).
24
- $RECORD = Symbol(0), // Node-map slot carrying a record-level `affects()` mark: any read through
25
- // the record witnesses it into the active isPending() probe.
26
- $AFFECTS = Symbol(0);
27
-
28
- // Structural field names of store targets (StoreNextTarget aliases these, so
29
- // shared machinery — affects walks, tests — reads targets via the consts).
30
- const STORE_VALUE = "v", STORE_NODE = "n", STORE_HAS = "h";
31
-
32
- function lookupTarget(e, t) {
33
- // Family registrations (projections/optimistic) first, then the global
34
- // next lookup. Proxies resolve through $TARGET directly.
35
- return (t ? lookupTarget$1(e, t) : undefined) ?? lookupTarget$1(e, null);
36
- }
37
-
38
- // Values marked raw never acquire a proxy identity: wrap() serves them as-is
39
- // everywhere — deep stores hold them as leaf values replaced by reference.
40
- // Once raw, always raw (identity stays single, just unwrapped). Consulted
41
- // only on wrap-creation and ingest paths; reads never touch it.
42
- const rawValues = new WeakSet;
38
+ * The committed backing (`t.v`) is owned raw (CoW: a user object is shared
39
+ * until the first write privatizes it and its ancestor chain). It mutates at
40
+ * exactly one moment — the flush's commit (`_storeCommit`), after the
41
+ * container node committed its staging. Reads: context-free readers see the
42
+ * committed backing until the flush; a pass sees the staging (signal
43
+ * parity, RUL-1); the draft reads its own staging.
44
+ *
45
+ * Perf contracts (plan sec. 31.4): lazy nodes; one literal per node; O(written) per
46
+ * setter (`wk`); overlay drafts for large plain records; no new named
47
+ * fields on array targets; zero allocation for adopted-but-unread objects.
48
+ */ let optHooks = null;
43
49
 
44
- /**
45
- * Marks a value as raw: no store will ever wrap it — every store presents it
46
- * as-is, tracked by reference at whatever slot holds it and updated by
47
- * replacement. Useful for class instances and external objects (editors,
48
- * scene graphs, Maps) and for record-shaped data updated wholesale. Sticky
49
- * for the value's lifetime.
50
- */
51
- // Flipped on the first mark and exported as a LIVE binding: reconcile
52
- // consults it on every recursable pair, and importing the boolean directly
53
- // lets those sites skip even the function call when no shallow store or raw
54
- // mark exists anywhere in the app.
55
- let rawValuesUsed = false;
56
-
57
- function isRawValue(e) {
58
- return rawValuesUsed && rawValues.has(e);
59
- }
60
-
61
- function markRawOne(e) {
62
- if (isWrappable(e)) {
63
- // A store proxy is already tracked elsewhere: the shallow boundary passes
64
- // it through by reference (replaced, never edited — same slot semantics
65
- // as a raw) instead of claiming it raw. The sticky mark is global, so
66
- // marking a live proxy would make wrap() serve it verbatim through every
67
- // OTHER store too — downstream deep stores then captured it instead of
68
- // wrapping it in their own family, and their writes landed in the
69
- // upstream store's override layer (#2932).
70
- if (e[$TARGET] !== undefined) return;
71
- rawValuesUsed = true;
72
- rawValues.add(e);
73
- }
74
- }
75
-
76
- function markRawIngest(e) {
77
- if (Array.isArray(e)) {
78
- for (let t = 0, o = e.length; t < o; t++) markRawOne(e[t]);
79
- } else {
80
- for (const t in e) markRawOne(e[t]);
50
+ function installOptHooks(e) {
51
+ optHooks = e;
52
+ }
53
+
54
+ let affectsHooks = null;
55
+
56
+ function installAffectsHooks(e) {
57
+ affectsHooks = e;
58
+ }
59
+
60
+ /** Key of the deep-witness slot node (never a user key). */ const $DEEP = Symbol(0);
61
+
62
+ // ---------------------------------------------------------------------------
63
+ // wrap / dedupe
64
+ /** Pre-shaped constructor for OBJECT proxy targets: V8 tips a bare `{}` into
65
+ * dictionary mode once ~19 named properties are assigned onto it (the #3044
66
+ * `ovl`/`del` fields crossed that line — every trap's field read became a
67
+ * hash lookup, a 15% deep-dbmon tick regression). Declaring every field in a
68
+ * constructor pre-allocates in-object slots so the map stays fast, with
69
+ * headroom for future fields. The prototype is reset to `Object.prototype`
70
+ * so proxy-forwarded semantics (getPrototypeOf, constructor) are exactly a
71
+ * plain object's. Array targets keep the bare-`[]` path — they must carry
72
+ * the array exotic class for `Array.isArray(proxy)`.
73
+ *
74
+ * ARRAY SHAPE RULE: arrays normalize their named properties to dictionary
75
+ * mode as the count grows (V8 13.x: counts ≡ 0 mod 3 from 18 up), so the
76
+ * target's named field count is capped at 20 — any future write-side state
77
+ * beyond `wk` must ride an extension object (see target.ts), never new
78
+ * named fields here. */ function TargetShape() {
79
+ this.v = undefined;
80
+ this.ch = undefined;
81
+ this.pb = undefined;
82
+ this.n = undefined;
83
+ this.h = undefined;
84
+ this.k = undefined;
85
+ this.dk = undefined;
86
+ this.u = undefined;
87
+ this.pk = undefined;
88
+ this.px = undefined;
89
+ this.d = undefined;
90
+ this.a = undefined;
91
+ this.sc = undefined;
92
+ this.kc = undefined;
93
+ this.nc = undefined;
94
+ this.fam = undefined;
95
+ this.s = undefined;
96
+ this.ovl = undefined;
97
+ this.del = undefined;
98
+ this.wk = undefined;
99
+ }
100
+
101
+ TargetShape.prototype = Object.prototype;
102
+
103
+ function createTarget(e, n, t, r = n?.fam ?? null) {
104
+ // The proxy target carries the array exotic class when the value is an
105
+ // array, so Array.isArray(proxy) is true; the fields live on it directly.
106
+ // Direct field assignment in one fixed order (no Object.assign literal
107
+ // copy): every target shares a hidden-class transition chain — createTarget
108
+ // was the #2 store cost in the uibench creation profile.
109
+ const i = Array.isArray(e) ? [] : new TargetShape;
110
+ i.v = e;
111
+ // Chained-backing flag (backing IS another store's proxy, §7b) — cached so
112
+ // the hot read path never does a per-read symbol lookup on the backing.
113
+ i.ch = e[$TARGET] !== undefined;
114
+ i.pb = null;
115
+ i.n = null;
116
+ i.h = null;
117
+ i.k = null;
118
+ i.dk = null;
119
+ i.wk = null;
120
+ i.u = n;
121
+ i.pk = t;
122
+ i.px = null;
123
+ i.d = false;
124
+ i.a = false;
125
+ i.sc = 0;
126
+ i.kc = 0;
127
+ i.nc = 0;
128
+ i.fam = r;
129
+ i.s = false;
130
+ i.ovl = false;
131
+ i.del = null;
132
+ i.px = new Proxy(i, traps);
133
+ setLookup(e, i);
134
+ return i;
135
+ }
136
+
137
+ function wrap(e, n = null, t = null, r = n?.fam ?? null) {
138
+ // markRaw'd values never wrap through ANY store (R42; sticky raw-marking
139
+ // is one half of the never-both-wrapped-and-raw invariant, RUL-12).
140
+ if (rawValuesUsed && isRawValue(e)) return e;
141
+ const i = lookupTarget(e, r);
142
+ if (i !== undefined) return i.px;
143
+ const o = e[$TARGET];
144
+ if (o !== undefined && o.px === e) {
145
+ // Foreign-family proxies re-wrap into THIS family (writes stay isolated);
146
+ // same-family and plain-store proxies pass through.
147
+ if (r === null || o.fam === r) return e;
148
+ return createTarget(e, n, t, r).px;
149
+ }
150
+ return createTarget(e, n, t, r).px;
151
+ }
152
+
153
+ /** Unwrap our own proxies to their current backing; leave everything else.
154
+ * A projection draft wrapper is registered as its target's alias
155
+ * (projection.ts `wrapDraft`) — adopted as a backing, it would forward to
156
+ * itself (#3767). */ function unwrapValue(e) {
157
+ if (e == null || typeof e !== "object") return e;
158
+ const n = e[$TARGET];
159
+ if (n !== undefined && (n.px === e || lookupTarget(e, n.fam) === n) && n.v !== undefined) {
160
+ // A draft escaping into other storage must be a REAL container that
161
+ // becomes this target's committed backing at fold (the shared-raw
162
+ // contract) — a prototype overlay is neither.
163
+ if (n.ovl) materializePB(n);
164
+ return n.pb ?? n.v;
81
165
  }
166
+ return e;
82
167
  }
83
168
 
84
- const OBJECT_PROTO = Object.prototype;
169
+ // ---------------------------------------------------------------------------
170
+ // nodes: pure subscription points (values used only for equality gating)
171
+ // Shared slot-node equality (create-floor diet): ONE function for every
172
+ // leaf — `this` is the node (method-call convention at every _equals site),
173
+ // `_host` is the baked-in target backref. Logical-slot equality: values
174
+ // resolving to the same child target are the same slot (privatization/
175
+ // adoption swap raw identity without changing the logical value — only
176
+ // changed leaves notify, R9).
177
+ const slotNodeEquals = function(e, n) {
178
+ return isEqual(e, n) || sameLogicalSlot(this.ot, e, n);
179
+ };
85
180
 
86
- // Per-prototype memo for the custom-proto branch of isWrappable: the verdict
87
- // is fully determined by the prototype (tag and Node lineage both live on
88
- // the chain), so each class pays the tag call once — not per read.
89
- const wrappableProtos = new WeakMap;
181
+ /** The deep witness: the store decides when it notifies. */ const never = false;
90
182
 
91
- function isWrappable(e) {
92
- if (e == null || typeof e !== "object" || Object.isFrozen(e)) return false;
93
- // Plain data and user class instances wrap; platform objects never do
94
- // (#2952). Native code brand-checks internal slots and throws through a
95
- // proxy (`Map.prototype.size`, `Date.prototype.getTime`, ...), so
96
- // collections and other built-ins can't honestly be stores — they get the
97
- // markRaw-children contract automatically: served raw, mutations land raw,
98
- // the property holding them still tracks (reassignment notifies). The tag
99
- // check separates them structurally: user classes stringify as
100
- // `[object Object]` while every native/host object carries its own brand
101
- // (`[object Map]`, `[object Date]`, `[object Headers]`, ...), including
102
- // subclasses, which inherit the tag. getPrototypeOf keeps the hot path
103
- // (plain and null-proto objects) intrinsic-only — no property lookup.
104
- const t = Object.getPrototypeOf(e);
105
- if (t === OBJECT_PROTO || t === null) return true;
106
- if (Array.isArray(e)) return true;
107
- let o = wrappableProtos.get(t);
108
- if (o === undefined) {
109
- o = Object.prototype.toString.call(e) === "[object Object]" && (
110
- // Dynamic Node check (kept dynamic so test/SSR overrides of
111
- // `globalThis.Node` are observed at call time): shimmed DOMs implement
112
- // nodes as plain user classes, which pass the tag check.
113
- typeof Node === "undefined" || !(e instanceof Node));
114
- wrappableProtos.set(t, o);
183
+ function sameKey(e, n) {
184
+ return e === n || e !== e && n !== n;
185
+ }
186
+
187
+ /** The container node's arrangement guess: the rows (or keys) the setter
188
+ * left the container with, in order. Its comparator judges a landing or a
189
+ * revert against it by row identity — by key when the family has one
190
+ * (the server's object for a guessed row is the same row). */ class LaneView {
191
+ rows;
192
+ base;
193
+ removed;
194
+ constructor(e,
195
+ /** The backing the guess is shown over: the committed one at the
196
+ * guess, the composed arrangement after a re-base (optimistic.ts). */
197
+ n,
198
+ /** Keys the setter REMOVED from the view it saw (a deliberate delete,
199
+ * not a row the truth has yet to show): a re-base does not restore
200
+ * them. `null` when none. */
201
+ t = null) {
202
+ this.rows = e;
203
+ this.base = n;
204
+ this.removed = t;
115
205
  }
116
- return o;
117
206
  }
118
207
 
119
- let writeOverride = false;
208
+ /** A container node's value as a backing: the base an arrangement guess is
209
+ * shown over, else the value itself. */ function asBacking(e) {
210
+ return e instanceof LaneView ? e.base : e;
211
+ }
212
+
213
+ /** The container node's comparator: a staging is a backing (identity — a
214
+ * pending backing is never "equal" to the committed one); a lane value is
215
+ * an arrangement (`LaneView`), judged by the optimistic machinery. */ const containerEquals = function(e, n) {
216
+ return e === n || optHooks !== null && optHooks.arrangement(this.ot, e, n);
217
+ };
120
218
 
121
- function setWriteOverride(e) {
122
- writeOverride = e;
219
+ function sameLogicalSlot(e, n, t) {
220
+ if (n === null || typeof n !== "object" || t === null || typeof t !== "object") return false;
221
+ const r = lookupTarget(n, e.fam);
222
+ return r !== undefined && r === lookupTarget(t, e.fam);
123
223
  }
124
224
 
125
- function getWriteOverride() {
126
- return writeOverride;
225
+ /** Slot nodes whose last subscriber left while they still held a staging:
226
+ * released at the commit that resolves it (`drainFolds`). */ const deferredReleases = new Set;
227
+
228
+ function releaseSlot(e) {
229
+ const n = e.ot;
230
+ const t = e.rt;
231
+ if (t === $TRACK) {
232
+ if (n.k === e) n.k = null;
233
+ } else if (t === $DEEP) {
234
+ if (n.dk === e) n.dk = null;
235
+ } else if (n.n !== null && n.n[t] === e) {
236
+ delete n.n[t];
237
+ n.nc--;
238
+ } else if (n.h !== null && n.h[t] === e) delete n.h[t];
239
+ // The family's index keeps only targets that still carry a node.
240
+ if (n.fam !== null && n.nc === 0 && n.k === null && n.dk === null && (n.h === null || Reflect.ownKeys(n.h).length === 0)) n.fam.live.delete(n);
127
241
  }
128
242
 
129
- // Own enumerable keys including symbols (`Object.keys` drops symbol-keyed props). #2769
130
- function ownEnumerableKeys(e) {
131
- return Reflect.ownKeys(e).filter(t => Object.prototype.propertyIsEnumerable.call(e, t));
243
+ /** A node was born on `target`: a family target enters the live index. */ function noteNode(e, n, t) {
244
+ if (e.fam !== null) e.fam.live.add(e);
245
+ markDescendants(e);
246
+ if (affectsHooks !== null) affectsHooks.born(e, n, t);
132
247
  }
133
248
 
134
- /**
135
- * Scope inheritance for late-created nodes: every live mark whose identity
136
- * scope contains the owning record's raw — and, for keyed marks, whose key
137
- * is this property — gets counted on the new node. Inherited marks live
138
- * exactly as long as the scope's carrier — the release hook below drops
139
- * them with the entry.
140
- */ function inheritAffectsMarks(e, t, o) {
141
- // A live scope exists, so affects.ts already installed the mark engine.
142
- for (const [r, s] of affectsScopes) {
143
- if (r.o?.t && s.scope.has(t) && (s.key === undefined || s.key === o)) {
144
- GlobalQueue.M(e);
145
- s.inherited.push(e);
249
+ // Shared slot-node release handler: registered once; the core sweep
250
+ // dispatches CONFIG_SLOT_NODE nodes here (graph.ts) instead of holding a
251
+ // per-node closure in a per-node NodeExtension. A dropped node is
252
+ // unreachable through the store — a fresh read makes a fresh node.
253
+ setSlotUnobserved(e => {
254
+ // A staged write — or a lane's value (a guess, S4) — is state only the
255
+ // node holds: defer the release to the commit that resolves it.
256
+ if (e.Ae !== NOT_PENDING || e.u & CONFIG_OVERRIDE) deferredReleases.add(e); else releaseSlot(e);
257
+ });
258
+
259
+ /** A node born while its container has a staging is born from the two
260
+ * frames (A29): committed from the committed backing, the staging from the
261
+ * pending one — and held by the container's transaction when the container
262
+ * is held (#3706: the unit of the hold is the key — a key the batch left
263
+ * unchanged stages nothing and holds no one). */ function bornStaged(e, n, t, r) {
264
+ if (n.We && n.We(n.pe, t)) return;
265
+ queuePendingNode(n);
266
+ n.Ae = t;
267
+ const i = e.k;
268
+ if (i === null) return;
269
+ // a node-less staging holds nothing (`stageOn`)
270
+ // Held by the container's transaction only for a key the HELD staging
271
+ // changed — a mainline layer's change above it stages mainline. (A
272
+ // container carrying a guess is a lane's: the flush's park decides.)
273
+ if ((i.u & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD && heldKeyChanged(e, r)) holdNode(n, txOf(i));
274
+ // A28 (4): a batch written inside a creation-time pass is promoted — its
275
+ // late-materialized leaf with it.
276
+ if (i.u & CONFIG_PROMOTED) notePromotedWrite(n);
277
+ }
278
+
279
+ function getNode(e, n,
280
+ // First-read dedupe (create-floor slice 2): the get trap probes
281
+ // accessor-ness right before creating the node — pass the verdict through
282
+ // so creation skips the second descriptor scan. -1 = unknown (other
283
+ // callers), 0/1 = probed.
284
+ t = -1) {
285
+ const r = e.n ??= Object.create(null);
286
+ let i = r[n];
287
+ if (i === undefined) {
288
+ // Accessor-ness resolved ONCE per node (no per-object descriptor scan
289
+ // on reads): accessor keys serve through Reflect.get with the proxy
290
+ // receiver.
291
+ const o = e.pb;
292
+ const l = i = slotSignal(committed(e)[n], slotNodeEquals, e, n, t === -1 ? isOwnAccessor(o ?? e.v, n) : t === 1);
293
+ if (o !== null && !l.acc) bornStaged(e, l, e.del !== null && e.del.has(n) ? undefined : o[n], n);
294
+ r[n] = i;
295
+ e.nc++;
296
+ noteNode(e, l, n);
297
+ }
298
+ return i;
299
+ }
300
+
301
+ function getHasNode(e, n) {
302
+ const t = e.h ??= Object.create(null);
303
+ let r = t[n];
304
+ if (r === undefined) {
305
+ const i = r = slotSignal(n in committed(e), isEqual, e, n, false);
306
+ const o = e.pb;
307
+ if (o !== null) bornStaged(e, i, n in o && !(e.del !== null && e.del.has(n)), n);
308
+ t[n] = r;
309
+ noteNode(e, i, n);
310
+ }
311
+ return r;
312
+ }
313
+
314
+ /** The container node (plan sec. 31.3): created by the first structural subscription
315
+ * or the first write that needs to stage. Its committed value is the
316
+ * committed backing. */ function getContainerNode(e) {
317
+ let n = e.k;
318
+ if (n === null) {
319
+ n = e.k = slotSignal(e.v, containerEquals, e, $TRACK, false);
320
+ noteNode(e, n, $TRACK);
321
+ // Born while a batch is staged without a node (`stageOn`): the node
322
+ // takes the staging as its own — the pre-batch backing committed, the
323
+ // pending backing staged — so whoever asked for it reads the frame it
324
+ // would have read had the node been there from the write.
325
+ if (e.pb !== null && !directCommit) {
326
+ n.pe = preBatch(e);
327
+ n.Ae = e.pb;
328
+ queuePendingNode(n);
146
329
  }
147
330
  }
331
+ return n;
148
332
  }
149
333
 
150
- const affectsScopes = new Map;
334
+ /** Q-A's escape hatch (plan sec. 33; measured plan sec. 41.4): a staging needs the container
335
+ * node as its home only when something can read the frame through it — a
336
+ * structural subscriber (the node exists), lane work (the staging is the
337
+ * lane's), a pass outside a flush (its write is promoted at the pass's end,
338
+ * A28 (4)), or a derive a transaction holds (the staging is the derive's,
339
+ * `holdWithDerive`). Otherwise the backing and the fold queue are the whole
340
+ * staging: nothing allocates, nothing is swept at the commit (dbmon's tick
341
+ * re-created, queued, swept and dropped ~7000 container nodes). If the
342
+ * flush parks, the seam materializes and holds the node then
343
+ * (`_storePark`) — the one case the staging home exists for. */ function stageOn(e, n) {
344
+ if (e.k === null && passLane === null && (context === null || globalQueue.ee) && !deriveHolds(e)) return;
345
+ const t = getContainerNode(e);
346
+ if (t.Ae === NOT_PENDING) queuePendingNode(t);
347
+ t.Ae = n;
348
+ // A28 (4): a write issued inside a pass outside a flush (a creation-time
349
+ // derive) is promoted at the pass's end — the batch with it.
350
+ if (context !== null) notePromotedWrite(t);
351
+ if (e.fam !== null) holdWithDerive(e, t);
352
+ }
353
+
354
+ /** The projection's derive is held by a transaction and this is its own
355
+ * write (not a user setter's): the staging is the derive's (`holdWithDerive`). */ function deriveHolds(e) {
356
+ const n = e.fam?.node;
357
+ return n != null && !userWrite && (n.u & CONFIG_HELD) !== 0;
358
+ }
359
+
360
+ GlobalQueue.Tn = e => {
361
+ for (let n = 0; n < foldList.length; n++) {
362
+ const t = foldList[n];
363
+ if (t.k === null && t.pb !== null) holdNode(getContainerNode(t), e);
364
+ }
365
+ };
366
+
367
+ function getDeepNode(e) {
368
+ let n = e.dk;
369
+ if (n === null) {
370
+ n = e.dk = slotSignal(0, never, e, $DEEP, false);
371
+ noteNode(e, n, $DEEP);
372
+ }
373
+ return n;
374
+ }
151
375
 
152
- /** Next-store node factory for affects carriers/slots: injected by the
153
- * rewrite module (next targets alias the legacy field names, so everything
154
- * here EXCEPT node creation works on them structurally). */ let nextAffectsNodeResolver = null;
376
+ function bumpDeep(e) {
377
+ if (e.dk !== null) setSignal(e.dk, 1);
378
+ }
155
379
 
156
- function setNextAffectsNodeResolver(e) {
157
- nextAffectsNodeResolver = e;
380
+ /** The structural readers' one notification: a membership/arrangement
381
+ * change on the container (the staging is already on the node; `setSignal`
382
+ * re-stages the same backing and walks the subscribers — and, on later
383
+ * steps, routes a held or lane-owned container the way any node is). */ function notifyContainer(e, n) {
384
+ // A container carrying an arrangement guess takes the write as a landing
385
+ // on the guess (`laneWrite` → `supersede`, judged by `containerEquals`);
386
+ // otherwise the structural readers are walked (the staging is already on
387
+ // the node; the comparator is the store's, done).
388
+ if (e.u & CONFIG_OVERRIDE) setSignal(e, n); else {
389
+ if (e.u & CONFIG_HELD) joinFuture(txOf(e));
390
+ insertSubs(e);
391
+ schedule();
392
+ }
158
393
  }
159
394
 
160
- /** Next-store optimistic view for the declaration walk (optimistic rows
161
- * pushed before the declaration are in motion too — legacy reads its write
162
- * overlays; next composes armed-node overrides). */ let nextOptimisticViewResolver = null;
395
+ // ---------------------------------------------------------------------------
396
+ // clones (CoW privatization)
397
+ const hasOwn = Object.prototype.hasOwnProperty;
398
+
399
+ const lookupGetter = Object.prototype.__lookupGetter__;
400
+
401
+ const lookupSetter = Object.prototype.__lookupSetter__;
163
402
 
164
- function setNextOptimisticViewResolver(e) {
165
- nextOptimisticViewResolver = e;
403
+ const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
404
+
405
+ function isOwnAccessor(e, n) {
406
+ return hasOwn.call(e, n) && (lookupGetter.call(e, n) !== undefined || lookupSetter.call(e, n) !== undefined);
166
407
  }
167
408
 
168
- /** @internal birth inheritance for nodes created inside a live mark window —
169
- * exported for the rewrite's node factories. */ function affectsScopesLive() {
170
- return affectsScopes.size > 0;
409
+ /** Shallow clone into store ownership. Plain data (grade 2) spreads; anything
410
+ * else copies descriptors (accessors kept, everything made configurable). */ function cloneRaw(e, n) {
411
+ n.sc || scanAccessorsOnce(n);
412
+ if (n.sc === 2) {
413
+ const t = {
414
+ ...e
415
+ };
416
+ t[$OWNER] = n;
417
+ return t;
418
+ }
419
+ const t = Object.getOwnPropertyDescriptors(e);
420
+ for (const r of Reflect.ownKeys(t)) {
421
+ const i = t[r];
422
+ if (r === "length" && Array.isArray(e)) continue;
423
+ i.configurable = true;
424
+ if (!i.get && !i.set) i.writable = true; else n.a = true;
425
+ }
426
+ const r = Array.isArray(e) ? Object.defineProperties([], t) : Object.create(Object.getPrototypeOf(e), t);
427
+ r[$OWNER] = n;
428
+ return r;
171
429
  }
172
430
 
173
- /**
174
- * Snapshots the identities reachable from `value` into `scope`, reading
175
- * through write overlays (an optimistic row pushed before the declaration is
176
- * in motion too). Untracked by construction: walks raw values, never traps.
177
- * Every LIVE node under each reachable record — property leaves, `$TRACK`,
178
- * and has-nodes — collects into `found`: those are the graph edges existing
179
- * readers subscribed through, so the mark registers on them directly and
180
- * rides the status rails to everything derived. (Nodes born later inherit
181
- * from the scope in `getNode`.)
182
- */ function walkAffectsScope(e, t, o, r,
183
- // Cycle guard, fresh per declaration: the scope itself can't serve — a
184
- // re-declaration on the same carrier unions into a scope that already
185
- // holds the root, and must still descend to pick up records added since.
186
- s) {
187
- if (!isWrappable(e)) return;
188
- const f = e[$TARGET] || lookupTarget(e, r);
189
- // Next targets: walk the pending backing when present (a draft's writes are
190
- // in motion too) and cover BOTH identities in the scope.
191
- let n = f ? f.pb ?? f[STORE_VALUE] : e;
192
- if (s.has(n)) return;
193
- s.add(n);
194
- t.scope.add(n);
195
- if (f && f.pb) t.scope.add(f[STORE_VALUE]);
196
- // Next optimistic families: enumerate the VISIBLE view (armed-node
197
- // overrides compose membership/values the raw doesn't carry).
198
- if (f && f.fam?.opt && nextOptimisticViewResolver) n = nextOptimisticViewResolver(f, n);
199
- if (f) {
200
- collectRecordNodes(f[STORE_NODE], o);
201
- collectRecordNodes(f[STORE_HAS], o);
202
- // The key-set and deep-witness nodes are record-level channels: a deep()
203
- // probe reads ONLY these (one pair per record), so a declared affects
204
- // scope must mark them like any property node.
205
- if (f.k) o.push(f.k);
206
- if (f.dk) o.push(f.dk);
207
- // Carry the effective family into untouched descendants (projections/
208
- // optimistic stores register children under their family).
209
- r = f.fam ?? r;
210
- }
211
- // Overlays are gone (next has no layer): raw enumeration; the optimistic
212
- // view composition above already folded armed-node membership/values in.
213
- if (Array.isArray(n)) {
214
- for (let e = 0, f = n.length; e < f; e++) {
215
- walkAffectsScope(n[e], t, o, r, s);
216
- }
217
- const e = Object.getOwnPropertySymbols(n);
218
- for (let f = 0, c = e.length; f < c; f++) {
219
- if (e[f] === $OWNER) continue;
220
- const c = Object.getOwnPropertyDescriptor(n, e[f]);
221
- if (!c || c.get) continue;
222
- walkAffectsScope(c.value, t, o, r, s);
431
+ /** Wide plain clone (many keys): null-prototype build, then reset. */ function wideClone(e, n) {
432
+ const t = Object.create(null);
433
+ for (const n of Reflect.ownKeys(e)) t[n] = e[n];
434
+ Object.setPrototypeOf(t, Object.prototype);
435
+ t[$OWNER] = n;
436
+ return t;
437
+ }
438
+
439
+ function copyOwn(e, n, t) {
440
+ const r = Object.getOwnPropertyDescriptor(n, t);
441
+ if (r.get || r.set || !r.enumerable || !r.writable || !r.configurable) Object.defineProperty(e, t, r); else e[t] = r.value;
442
+ }
443
+
444
+ /** One descriptor scan per container (grade 1/2, `a`, `kc`). */ function scanAccessorsOnce(e) {
445
+ const n = e.v;
446
+ const t = Reflect.ownKeys(n);
447
+ let r = Object.getPrototypeOf(n) === Object.prototype;
448
+ for (const i of t) {
449
+ if (lookupGetter.call(n, i) !== undefined || lookupSetter.call(n, i) !== undefined) {
450
+ e.a = true;
451
+ r = false;
452
+ break;
223
453
  }
454
+ if (r && !propertyIsEnumerable.call(n, i)) r = false;
455
+ }
456
+ e.sc = r ? 2 : 1;
457
+ e.kc = t.length;
458
+ return !e.a;
459
+ }
460
+
461
+ // ---------------------------------------------------------------------------
462
+ // the pending backing
463
+ const OVERLAY_MIN_KEYS = 32;
464
+
465
+ const OVERLAY_REBUILD_MAX_KEYS = 1024;
466
+
467
+ const OVERLAY_REBUILD_MIN_ADDS = 16;
468
+
469
+ const WK_ALL = new Set;
470
+
471
+ const plainProto = e => {
472
+ const n = Object.getPrototypeOf(e);
473
+ return n === Object.prototype || n === Array.prototype || n === null;
474
+ };
475
+
476
+ /** An overlay with many adds (or any delete) rebuilds the committed
477
+ * container at flatten so its shape stays fast (#3689). */ function overlayRebuilds(e, n) {
478
+ if (e.kc > OVERLAY_REBUILD_MAX_KEYS) return false;
479
+ if (e.del !== null && e.del.size !== 0) return true;
480
+ const t = e.wk;
481
+ if (t !== null && t !== WK_ALL && t.size <= OVERLAY_REBUILD_MIN_ADDS) return false;
482
+ const r = e.v;
483
+ let i = 0;
484
+ for (const e of Reflect.ownKeys(n)) if (!hasOwn.call(r, e) && ++i > OVERLAY_REBUILD_MIN_ADDS) return true;
485
+ return false;
486
+ }
487
+
488
+ /** Downgrade an overlay to a real clone (a consumer needs a container). */ function materializePB(e) {
489
+ const n = e.pb;
490
+ if (!e.ovl) return n;
491
+ const t = e.sc === 2 ? wideClone(e.v, e) : cloneRaw(e.v, e);
492
+ if (e.del !== null) {
493
+ for (const n of e.del) delete t[n];
494
+ e.kc -= e.del.size;
495
+ e.del = null;
496
+ }
497
+ for (const r of Reflect.ownKeys(n)) e.sc === 2 ? t[r] = n[r] : copyOwn(t, n, r);
498
+ e.pb = t;
499
+ e.ovl = false;
500
+ // The staging IS the pending backing: re-point the container node.
501
+ if (e.k !== null && e.k.Ae === n) e.k.Ae = t;
502
+ return t;
503
+ }
504
+
505
+ /** The batch's pending backing, created on the first draft write: an overlay
506
+ * of an owned plain-data record above the key threshold, a clone otherwise.
507
+ * Staged on the container node (the scheduler owns its lifetime) and queued
508
+ * for the fold at the flush's commit. */ function ensurePB(e) {
509
+ let n = e.pb;
510
+ // An optimistic edit composes on the view the user saw and becomes
511
+ // guesses at the setter's exit: nothing is staged, nothing folds
512
+ // (optimistic.ts).
513
+ if (userWrite && e.fam?.opt === true && !pendingNotify.has(e)) return e.pb = optHooks.draft(e);
514
+ if (n !== null && !isOwned(n)) {
515
+ // An adopted object awaiting its fold is the user's: the draft writes a
516
+ // clone of it. The clone is the batch's staging — unless the adoption is
517
+ // HELD (another transaction's): then the held staging stays the
518
+ // adoption and the draft is a mainline layer above it (#3612/#3688: a
519
+ // mainline setter mid-hold publishes mainline; the held keys stay held).
520
+ n = e.pb = cloneRaw(n, e);
521
+ const t = e.k;
522
+ if (t !== null && !(t.u & CONFIG_HELD)) t.Ae = n;
523
+ } else if (n === null) {
524
+ const t = e.v;
525
+ if (!directCommit) queueFold(e);
526
+ if (!e.ch && !Array.isArray(t) && (e.sc !== 0 ? !e.a : scanAccessorsOnce(e)) && e.kc > OVERLAY_MIN_KEYS && isOwned(t)) {
527
+ n = e.pb = Object.create(t);
528
+ e.ovl = true;
529
+ } else n = e.pb = cloneRaw(t, e);
530
+ if (!directCommit) stageOn(e, n);
531
+ }
532
+ return n;
533
+ }
534
+
535
+ /** Privatize `target`'s committed backing (and its ancestor chain — a shared
536
+ * parent cannot point at an owned child). Not a reactive event. */ function privatizeCommitted(e) {
537
+ if (isOwned(e.v)) return;
538
+ const n = e.v;
539
+ const t = cloneRaw(n, e);
540
+ // Mid-batch (a staging is open on this target) the fold still sees the
541
+ // backing the batch started from: the queue recorded it (`foldOlds`).
542
+ e.v = t;
543
+ e.ch = false;
544
+ if (e.u) {
545
+ privatizeCommitted(e.u);
546
+ devAssertNeverUserMutation(e.u.v);
547
+ const t = e.u.v;
548
+ const r = parentSlotKey(e, n);
549
+ if (t[r] === n) t[r] = e.v;
550
+ }
551
+ }
552
+
553
+ /** The key this target sits under in its parent — re-resolved by identity
554
+ * when an array parent moved it (#3282). Not searched when the slot already
555
+ * holds this target's backing (a parent adopted whole carries its children's
556
+ * new raws before the children fold — the miss was O(rows) per changed row
557
+ * of a keyed reconcile). */ function parentSlotKey(e, n) {
558
+ const t = e.pk;
559
+ const r = e.u.v;
560
+ if (r[t] === n || r[t] === e.v || !Array.isArray(r)) return t;
561
+ const i = r.indexOf(n);
562
+ if (i === -1) return t;
563
+ e.pk = i;
564
+ return i;
565
+ }
566
+
567
+ /** Overlay flatten (#3044): own keys onto the owned committed backing in
568
+ * place — the backing keeps its identity. */ function flattenOverlay(e, n) {
569
+ privatizeCommitted(e);
570
+ const t = e.v;
571
+ for (const r of Reflect.ownKeys(n)) e.sc === 2 ? t[r] = n[r] : copyOwn(t, n, r);
572
+ if (e.del !== null) {
573
+ for (const n of e.del) delete t[n];
574
+ e.kc -= e.del.size;
575
+ e.del = null;
576
+ }
577
+ e.pb = null;
578
+ e.ovl = false;
579
+ e.wk = null;
580
+ // written-keys window closes with the commit
581
+ }
582
+
583
+ /** Adoption (a setter's returned replacement; reconcile and projection
584
+ * landings in S3): the incoming object becomes the backing by reference at
585
+ * the fold — no clones, no user-object mutation, ownership resets to
586
+ * shared. It is the container's staging until then (signal parity: a
587
+ * handler reads the committed frame until the flush; a hold keeps it
588
+ * staged with the rest). The nodes are told now, diffed against the view
589
+ * they were LAST told (#3296): the draft's pending backing when one
590
+ * preceded the adoption this batch — its node writes were real, and the
591
+ * adoption's re-stage cancels or confirms each — else the committed
592
+ * backing. */ function adoptPB(e, n, t = true) {
593
+ if (!directCommit) queueFold(e);
594
+ let r;
595
+ if (e.pb !== null) {
596
+ if (e.ovl) materializePB(e);
597
+ r = e.pb;
598
+ } else r = e.v;
599
+ if (directCommit) {
600
+ e.pb = null;
601
+ if (e.k !== null) e.k.pe = n;
224
602
  } else {
225
- const e = Reflect.ownKeys(n);
226
- for (let f = 0, c = e.length; f < c; f++) {
227
- if (e[f] === $OWNER) continue;
228
- const c = Object.getOwnPropertyDescriptor(n, e[f]);
229
- if (!c || c.get) continue;
230
- walkAffectsScope(c.value, t, o, r, s);
603
+ // Eager (INTERNALS §3): the backing swaps now — handlers read the
604
+ // adopted object before the flush — while the container node, when
605
+ // there is one (`stageOn`), keeps the committed frame (`_value`) for
606
+ // readers a hold keeps on it, and stages the adoption for the fold's
607
+ // path copy.
608
+ e.pb = n;
609
+ stageOn(e, n);
610
+ }
611
+ e.v = n;
612
+ e.ch = n[$TARGET] !== undefined;
613
+ e.ovl = false;
614
+ e.del = null;
615
+ e.sc = 0;
616
+ e.a = false;
617
+ e.wk = null;
618
+ // adoption supersedes staged trap writes
619
+ const i = n[$OWNER];
620
+ if (i !== undefined && i.fam === e.fam) n[$OWNER] = e; else setLookup(n, e);
621
+ if (t && r !== n) notifyFold(e, r, n);
622
+ }
623
+
624
+ // ---------------------------------------------------------------------------
625
+ // the fold: owned raw's one mutation point, at the flush's commit
626
+ /** The fold queue: the targets with a batch open (`pb !== null` IS the
627
+ * membership — a target is queued exactly while it carries a staging; the
628
+ * drain skips one already folded), in two reusable arrays. Nothing here
629
+ * allocates per batch once warm: a dbmon tick queues ~7000 containers, and
630
+ * a map built and drained per tick cost ~540 KB of table allocation — most
631
+ * of the tick's young-generation GC, and the whole of its gap to `next`
632
+ * (plan sec. 41.5).
633
+ *
634
+ * Beside each target, its PRE-BATCH committed backing — `t.v` as the batch
635
+ * opened on it (`foldOlds`, parallel to `foldList`). That one record is the
636
+ * fold's base in every case: a draft's backing does not move until the fold
637
+ * (so the record is `t.v` still), an adoption swaps it eagerly (the record
638
+ * is the frame committed readers keep), a mid-batch privatization or a
639
+ * draft over an adopted raw moves it again (the record is what the batch
640
+ * started from, which is all the fold wants to know). The record lives
641
+ * exactly as long as the batch: a weak map keyed by target kept every
642
+ * container's LAST adopted-away backing alive until its next adoption —
643
+ * for a keyed reconcile, the whole previous tree promoted out of the
644
+ * nursery every tick (the saturated listened-paths shape ran 15–25% over
645
+ * the fork, all of it this; plan sec. 43.2) — and for a store adopted
646
+ * once, for its lifetime.
647
+ *
648
+ * The target carries no slot for its record (ARRAY SHAPE RULE, target.ts),
649
+ * so a lookup by target (`preBatch`: a container node born onto an open
650
+ * staging, a committed-frame read of a node-less staging, a park) indexes
651
+ * the list through a map built on first use per batch — the drain itself
652
+ * walks the two lists in step and never builds it. */ let foldList = [];
653
+
654
+ let foldOlds = [];
655
+
656
+ let foldSpare = [];
657
+
658
+ let foldOldsSpare = [];
659
+
660
+ let foldIndex = null;
661
+
662
+ /** Before the staging is set and before an adoption swaps the backing. */ function queueFold(e) {
663
+ if (e.pb !== null) return;
664
+ schedule();
665
+ if (foldIndex !== null) foldIndex.set(e, foldList.length);
666
+ foldList.push(e);
667
+ foldOlds.push(e.v);
668
+ }
669
+
670
+ /** The committed backing before this batch (valid while `pb !== null`). */ function preBatch(e) {
671
+ if (foldIndex === null) {
672
+ foldIndex = new Map;
673
+ for (let e = 0; e < foldList.length; e++) foldIndex.set(foldList[e], e);
674
+ }
675
+ const n = foldIndex.get(e);
676
+ return n === undefined ? e.v : foldOlds[n];
677
+ }
678
+
679
+ /** The flush committed its pending nodes: fold every pending backing whose
680
+ * container node committed (its staging is gone — `_value` is the backing
681
+ * now) into the committed home; one a later step holds (its node still
682
+ * staged) waits for the flush that commits it. Then the deferred slot
683
+ * releases. */ function drainFolds() {
684
+ if (foldList.length !== 0) {
685
+ const e = foldList;
686
+ const n = foldOlds;
687
+ foldList = foldSpare;
688
+ foldOlds = foldOldsSpare;
689
+ foldSpare = e;
690
+ foldOldsSpare = n;
691
+ foldIndex = null;
692
+ for (let t = 0; t < e.length; t++) {
693
+ const r = e[t];
694
+ if (r.pb === null) continue;
695
+ // folded already (a direct commit)
696
+ const i = r.k;
697
+ if (i !== null && i.Ae !== NOT_PENDING) {
698
+ // held: commits with its hold
699
+ foldList.push(r);
700
+ foldOlds.push(n[t]);
701
+ } else foldTarget(r, n[t]);
702
+ }
703
+ e.length = 0;
704
+ n.length = 0;
705
+ }
706
+ if (deferredReleases.size !== 0) {
707
+ for (const e of deferredReleases) {
708
+ if (e.Ae === NOT_PENDING && !(e.u & CONFIG_OVERRIDE) && e.t === null) {
709
+ deferredReleases.delete(e);
710
+ releaseSlot(e);
711
+ }
231
712
  }
232
713
  }
233
714
  }
234
715
 
235
- /** All live signal nodes of one record's node map (string + symbol keyed). */ function collectRecordNodes(e, t) {
236
- if (!e) return;
237
- for (const o of Object.keys(e)) t.push(e[o]);
238
- const o = Object.getOwnPropertySymbols(e);
239
- for (let r = 0, s = o.length; r < s; r++) {
240
- // Another mark's carrier is its own channel — counting it here would
241
- // extend that sibling scope's lifetime to this declaration's.
242
- if (o[r] !== $AFFECTS) t.push(e[o[r]]);
716
+ /** Core asks: is a slot node's truth in flight? Its family's derive is —
717
+ * lanes (A17, #2864: a guess mid-refetch stands in for the flight) and
718
+ * marks (an ambient `affects()` over a declared reload lives to the
719
+ * landing) read it. */ GlobalQueue.F = e => {
720
+ const n = e.ot?.fam;
721
+ return n != null && n.node !== null && (n.node.G & STATUS_PENDING) !== 0;
722
+ };
723
+
724
+ /** Core asks (lanes.ts `covered`): the value a slot node's guess covered.
725
+ * A chained link's committed value is the inner store's at the last
726
+ * read-through — refreshed here, since a guess served the reads since
727
+ * (#3672: the revert compares against the base's live value). */ GlobalQueue.rn = e => {
728
+ const n = e.ot;
729
+ if (n.ch) {
730
+ const t = e.rt;
731
+ const r = n.v;
732
+ if (n.h !== null && n.h[t] === e) e.pe = untrack(() => t in r); else if (n.n !== null && n.n[t] === e) e.pe = untrack(() => unwrapValue(r[t]));
733
+ }
734
+ return e.pe;
735
+ };
736
+
737
+ GlobalQueue._n = () => {
738
+ drainFolds();
739
+ optHooks?.sweep();
740
+ };
741
+
742
+ /** A projection's creation run commits directly (a memo's first value is
743
+ * its `_value`, not a staging): the draft still writes a clone of the seed,
744
+ * nothing is staged on the nodes, and the setter's exit folds the touched
745
+ * containers at once. Only the synchronous first run. */ let directCommit = false;
746
+
747
+ function runDirect(e) {
748
+ const n = directCommit;
749
+ directCommit = true;
750
+ try {
751
+ e();
752
+ } finally {
753
+ directCommit = n;
243
754
  }
244
755
  }
245
756
 
246
- /**
247
- * Witness live mark coverage of a record into the active isPending() probe.
248
- * Tracked reads don't need this — they go through real signal nodes, which
249
- * carry marks directly (declaration walk or birth inheritance). This covers
250
- * UNTRACKED probes reading through records whose nodes never materialized
251
- * (no observer ever subscribed, so no node exists to carry the mark).
252
- * Callers guard on `pendingCheckActive`, so plain reads never pay for this.
253
- *
254
- * @internal
255
- */ function witnessAffectsMark(e, t) {
256
- // Callers guard on `pendingCheckActive`, which only flips inside
257
- // isPending() — the verdict layer is loaded and its hook installed.
258
- const o = e[STORE_NODE]?.[$AFFECTS];
259
- if (o?.o?.t) GlobalQueue.Et(o);
260
- if (affectsScopes.size) {
261
- // Chained backings (§7b): a wrapper's STORE_VALUE can be another store's
262
- // proxy — marks cover by identity of the BASE raw, so resolve the chain
263
- // and check every identity along it.
264
- let r = e[STORE_VALUE];
265
- for (const [e, s] of affectsScopes) {
266
- if (e !== o && e.o?.t && (s.key === undefined || s.key === t)) {
267
- let t = r;
268
- for (;;) {
269
- if (s.scope.has(t)) {
270
- GlobalQueue.Et(e);
757
+ /** Fold one target's pending backing into its committed home. */ function foldTarget(e, n) {
758
+ {
759
+ {
760
+ const t = e.k;
761
+ const r = e.pb;
762
+ if (r !== null) {
763
+ if (r === e.v) {
764
+ // An eager adoption nothing wrote after: the backing is already
765
+ // the adopted object (shared ownership — never cloned).
766
+ e.pb = null;
767
+ e.wk = null;
768
+ } else if (e.ovl && (e.v !== n || !overlayRebuilds(e, r))) {
769
+ // Overlay flatten (#3044): the backing keeps its identity, so the
770
+ // `t.v === old` gate below skips path copying (the parent slot
771
+ // already points here) and the adopted-notify (setter
772
+ // notifications happened at write time). A backing privatized
773
+ // mid-batch is a fresh clone the parent already points at.
774
+ flattenOverlay(e, r);
775
+ } else if (e.v !== n) {
776
+ // Privatized mid-batch (a child's path copy repointed us): the
777
+ // owned backing takes the written keys.
778
+ privatizeCommitted(e);
779
+ const t = e.v;
780
+ const i = e.wk;
781
+ if (i !== null && i !== WK_ALL) {
782
+ for (const e of i) {
783
+ if (hasOwn.call(r, e)) copyOwn(t, r, e); else delete t[e];
784
+ }
785
+ } else {
786
+ for (const e of Reflect.ownKeys(r)) {
787
+ const i = Object.getOwnPropertyDescriptor(r, e);
788
+ if (i.get || i.set || !i.enumerable || !i.writable || !i.configurable) Object.defineProperty(t, e, i); else if (i.value !== n[e] || !hasOwn.call(n, e)) t[e] = i.value;
789
+ }
790
+ for (const e of Reflect.ownKeys(n)) {
791
+ if (!hasOwn.call(r, e)) delete t[e];
792
+ }
793
+ }
794
+ e.pb = null;
795
+ e.wk = null;
796
+ } else {
797
+ // The clone (or adopted object) becomes the committed backing
798
+ // (path copy below).
799
+ e.v = e.ovl ? materializePB(e) : r;
800
+ e.ch = e.v[$TARGET] !== undefined;
801
+ e.pb = null;
802
+ e.wk = null;
803
+ }
804
+ }
805
+ if (t !== null) {
806
+ // The node's committed value is the backing. A container nobody
807
+ // subscribes to was a transient staging home (INTERNALS §5: an
808
+ // observer-less write holds a transient record until the fold,
809
+ // discarded with it) — gone with the batch.
810
+ if (t.t === null) e.k = null; else if (t.pe instanceof LaneView) t.pe.base = e.v; else t.pe = e.v;
811
+ }
812
+ if (e.v !== n && e.u) {
813
+ const t = parentSlotKey(e, n);
814
+ if (e.u.v[t] === n) {
815
+ privatizeCommitted(e.u);
816
+ devAssertNeverUserMutation(e.u.v);
817
+ e.u.v[t] = e.v;
818
+ }
819
+ }
820
+ }
821
+ }
822
+ }
823
+
824
+ /** A user setter wrote a key whose staging is a derivation another
825
+ * transaction holds (A34 (3), #3612): the hold re-derives over the write. */ let heldDerivationHit = false;
826
+
827
+ /** The open setter is a USER'S (`storeSetter(…, guard = true)`), not the
828
+ * derive's draft or landing. */ let userWrite = false;
829
+
830
+ /** Is the open setter a user's? (reconcile.ts: a user's reconcile on an
831
+ * optimistic family writes the draft.) */ function userWriting() {
832
+ return userWrite;
833
+ }
834
+
835
+ function heldDerivation(e, n, t) {
836
+ if (e.fam === null || !userWrite) return false;
837
+ if (n !== undefined) return (n.u & (CONFIG_HELD | CONFIG_MANUAL_WRITE)) === CONFIG_HELD && txOf(n) !== flushTransaction;
838
+ const r = e.k;
839
+ return r !== null && (r.u & CONFIG_HELD) !== 0 && txOf(r) !== flushTransaction && heldKeyChanged(e, t);
840
+ }
841
+
842
+ /** Setter exit: one node write per changed observed key (the leaves stage
843
+ * like signals — holds and lanes engage here on later steps), presence and
844
+ * deep witnesses, and the container when membership changed. */ function notifyWrites(e) {
845
+ let n = e.pb;
846
+ if (n === null) return;
847
+ // Optimistic channel: a user's writes on an optimistic family become
848
+ // guesses on the nodes (lanes); the draft is discarded, the committed
849
+ // backing never touched.
850
+ if (userWrite && e.fam?.opt === true) {
851
+ // The draft is discarded first: the diff reads the target (its nodes
852
+ // are born from the committed frame, not the draft).
853
+ e.pb = null;
854
+ e.wk = null;
855
+ e.pb = optHooks.writes(e, n);
856
+ return;
857
+ }
858
+ const t = e.v;
859
+ const r = e.n;
860
+ // Written-keys bound: trap writes record their keys, so the notify visits
861
+ // O(written) nodes instead of every subscription on the record (a selection
862
+ // map with thousands of per-key subscribers pays two visits per select,
863
+ // not a full scan). Falls back to the full node scan when the bound can't
864
+ // hold: no trap granularity (wk null), an array length write (WK_ALL —
865
+ // implicit index deletes), accessors on the record (t.a — a getter node's
866
+ // value can change when ANY key is written), or a non-plain prototype
867
+ // (class instances: prototype getters derive from arbitrary fields).
868
+ // Overlay pbs chain to the COMMITTED object (#3044) — plainness is the
869
+ // committed container's prototype, not the overlay's.
870
+ const i = e.wk;
871
+ const o = i === WK_ALL || e.a === true || !plainProto(e.ovl ? e.v : n) ? null : i;
872
+ if (e.fam !== null && !heldDerivationHit && o !== null) for (const n of o) if (heldDerivation(e, r?.[n], n)) {
873
+ heldDerivationHit = true;
874
+ break;
875
+ }
876
+ if (r !== null) {
877
+ const i = o ?? Reflect.ownKeys(r);
878
+ for (const o of i) {
879
+ const i = r[o];
880
+ if (i === undefined) continue;
881
+ if (i.acc === true || hasOwn.call(n, o) && lookupGetter.call(n, o) !== undefined) {
882
+ // Accessor keys: the node is linked for shape-change notification,
883
+ // its value is never served (the getter runs with the proxy
884
+ // receiver on read) — FORCE wakes the readers.
885
+ i.acc = isOwnAccessor(n, o);
886
+ const e = Object.getOwnPropertyDescriptor(t, o);
887
+ const r = Object.getOwnPropertyDescriptor(n, o);
888
+ if (e && (e.get || e.set) || r && (r.get || r.set)) {
889
+ if (e?.get !== r?.get || e?.set !== r?.set || e?.value !== r?.value) setSignal(i, () => FORCE);
890
+ continue;
891
+ }
892
+ if (!isEqual(e?.value, r?.value)) setSignal(i, () => r?.value);
893
+ continue;
894
+ }
895
+ const l = e.del !== null && e.del.has(o) ? undefined : n[o];
896
+ setSignal(i, () => l);
897
+ if (e.fam !== null) {
898
+ // The derive's write is a derivation; a user setter's is a proposal
899
+ // (A34 (3)'s discriminator).
900
+ if (!userWrite) {
901
+ i.u &= ~CONFIG_MANUAL_WRITE;
902
+ holdWithDerive(e, i);
903
+ } else i.u |= CONFIG_MANUAL_WRITE;
904
+ }
905
+ }
906
+ }
907
+ const l = e.h;
908
+ if (l !== null) {
909
+ const t = o ?? Reflect.ownKeys(l);
910
+ for (const r of t) {
911
+ const t = l[r];
912
+ if (t !== undefined) {
913
+ setSignal(t, r in n && !(e.del !== null && e.del.has(r)));
914
+ if (e.fam !== null) holdWithDerive(e, t);
915
+ }
916
+ }
917
+ }
918
+ if (e.dk !== null) {
919
+ if (e.del !== null && e.del.size !== 0) bumpDeep(e); else for (const r of o ?? Reflect.ownKeys(n)) {
920
+ if (r === $OWNER) continue;
921
+ const i = n[r];
922
+ const o = t[r];
923
+ if (i !== null && typeof i === "object" ? !targetsEqual(o, i) : !isEqual(o, i)) {
924
+ bumpDeep(e);
925
+ break;
926
+ }
927
+ }
928
+ }
929
+ // The container: structural readers hear membership/arrangement changes
930
+ // only (R9) — the compare is the store's, against the committed frame. A
931
+ // container carrying an arrangement guess is told whether or not anyone
932
+ // subscribes: the write is a landing on the guess (`notifyContainer`).
933
+ const u = e.k;
934
+ if (u !== null && (u.t !== null || u.u & CONFIG_OVERRIDE)) {
935
+ let r;
936
+ if (e.ovl) {
937
+ r = e.del !== null && e.del.size !== 0;
938
+ if (!r) {
939
+ for (const e of Reflect.ownKeys(n)) {
940
+ if (!hasOwn.call(t, e)) {
941
+ r = true;
271
942
  break;
272
943
  }
273
- const o = t?.[$TARGET];
274
- if (o === undefined) break;
275
- const r = o.pb ?? o[STORE_VALUE];
276
- if (r === t) break;
277
- t = r;
278
944
  }
279
945
  }
946
+ } else {
947
+ r = Array.isArray(n) && Array.isArray(t) ? arrayStructureChanged(t, n) : membershipChanged(t, n);
280
948
  }
949
+ if (r) notifyContainer(u, n);
281
950
  }
282
951
  }
283
952
 
284
- /**
285
- * Resolves the store nodes an `affects()` declaration marks: with a `key`,
286
- * the named slot's leaf node (upserted so the mark has an addressable
287
- * carrier); without, the record's $AFFECTS carrier plus every LIVE node in
288
- * its subtree (the edges existing readers subscribed through), with the
289
- * subtree's identities snapshotted into the mark's scope so nodes created
290
- * during the window — and untracked probes over captured proxies — resolve
291
- * against it (#2882).
953
+ /** Accessor-node wake sentinel: readers re-run and re-invoke the getter. */ const FORCE = Symbol();
954
+
955
+ /** Two raws that wrap to the same target are the same logical value. */ function targetsEqual(e, n) {
956
+ if (e === null || typeof e !== "object" || n === null || typeof n !== "object") return false;
957
+ const t = lookupTarget(e, null);
958
+ return t !== undefined && t === lookupTarget(n, null);
959
+ }
960
+
961
+ /** Positional identity IS content for arrays (R15). */ function arrayStructureChanged(e, n) {
962
+ if (e.length !== n.length) return true;
963
+ for (let t = 0; t < n.length; t++) {
964
+ const r = e[t];
965
+ const i = n[t];
966
+ if (!isEqual(r, i) && !targetsEqual(r, i)) return true;
967
+ }
968
+ return false;
969
+ }
970
+
971
+ function membershipChanged(e, n) {
972
+ const t = Reflect.ownKeys(n);
973
+ if (Reflect.ownKeys(e).length - +isOwned(e) !== t.length - +isOwned(n)) return true;
974
+ for (const n of t) if (n !== $OWNER && !(n in e)) return true;
975
+ return false;
976
+ }
977
+
978
+ /** One leaf's notification for an adoption diff (descriptor-aware). */ function notifyKeyDiff(e, n, t, r, i = true) {
979
+ if (e.acc === true || i && hasOwn.call(r, n) && lookupGetter.call(r, n) !== undefined) {
980
+ e.acc = isOwnAccessor(r, n);
981
+ const i = Object.getOwnPropertyDescriptor(t, n);
982
+ const o = Object.getOwnPropertyDescriptor(r, n);
983
+ if (i && (i.get || i.set) || o && (o.get || o.set)) {
984
+ if (i?.get !== o?.get || i?.set !== o?.set || i?.value !== o?.value) setSignal(e, () => FORCE);
985
+ return;
986
+ }
987
+ const l = i?.value;
988
+ const u = o?.value;
989
+ if (!isEqual(l, u) && !targetsEqual(l, u)) setSignal(e, typeof u === "function" ? () => u : u);
990
+ } else {
991
+ const i = t[n];
992
+ const o = r[n];
993
+ if (!isEqual(i, o) && !targetsEqual(i, o)) setSignal(e, typeof o === "function" ? () => o : o);
994
+ }
995
+ }
996
+
997
+ function hasAccessorFlag(e) {
998
+ return e.acc === true;
999
+ }
1000
+
1001
+ /** One leaf's notification when the caller already has both values. */ function notifyKeyValue(e, n, t, r, i, o) {
1002
+ if (e.acc === true) {
1003
+ notifyKeyDiff(e, n, i, o, false);
1004
+ return;
1005
+ }
1006
+ if (!isEqual(t, r) && !targetsEqual(t, r)) setSignal(e, typeof r === "function" ? () => r : r);
1007
+ }
1008
+
1009
+ /** The structural half of an adoption's notifications (reconcile does the
1010
+ * leaves itself): presence by `in`, the container by membership. Presence
1011
+ * is DIFFED `old` → `neu` like the leaves (#3743): a presence node is
1012
+ * written only where `in` changed between the view it was last told
1013
+ * (#3296) and the adoption — `setSignal` joins a held node's transaction
1014
+ * before its equality gate (A34 (1)), so repeating an unchanged absence to
1015
+ * a node an action holds would make the whole tick the action's (an
1016
+ * unrelated `a.value` stayed stale until the action settled). A real
1017
+ * change still writes, and on a held node still proposes. A live chained
1018
+ * `old` (a store proxy, §7b) reflects the inner store, not what the node
1019
+ * was last told — written unconditionally, as before.
292
1020
  *
293
- * @internal
294
- */ function getStoreAffectsNodes(e, t) {
295
- GlobalQueue.p ||= e => {
296
- const t = affectsScopes.get(e);
297
- if (!t) return;
298
- affectsScopes.delete(e);
299
- for (let e = 0; e < t.inherited.length; e++) GlobalQueue.N(t.inherited[e]);
300
- };
301
- if (t === undefined) {
302
- const t = nextAffectsNodeResolver(e, $AFFECTS);
303
- let o = affectsScopes.get(t);
304
- if (!o) affectsScopes.set(t, o = {
305
- scope: new Set,
306
- inherited: []
307
- });
308
- const r = [ t ];
309
- walkAffectsScope(e[$PROXY], o, r, e.fam, new Set);
1021
+ * The container is told of an arrangement change when it has structural
1022
+ * subscribers OR carries an arrangement guess (S4): for a guessed
1023
+ * container the write is the landing that judges the guess (Q-D, plan
1024
+ * sec. 39 — confirm, supersede, or hold beneath), not a subscriber
1025
+ * notification, and a landing whose truth changed no guessed key's value
1026
+ * or presence (a newer question's rows beneath an optimistic push) still
1027
+ * answers the arrangement — before, only the presence write reached the
1028
+ * lane, by accident of being unconditional. */ function notifyFoldTail(e, n, t) {
1029
+ const r = e.h;
1030
+ if (r !== null) {
1031
+ const e = n[$TARGET];
1032
+ for (const i of Reflect.ownKeys(r)) if (e || i in n !== i in t) setSignal(r[i], i in t);
1033
+ }
1034
+ const i = e.k;
1035
+ if (i !== null && (i.t !== null || i.u & CONFIG_OVERRIDE)) {
1036
+ const e = Array.isArray(t) && Array.isArray(n) ? arrayStructureChanged(n, t) : membershipChanged(n, t);
1037
+ if (e) notifyContainer(i, t);
1038
+ }
1039
+ }
1040
+
1041
+ /** An adoption's notifications: every node of the target, diffed `old` →
1042
+ * `neu` (value nodes by key, presence by `in`, the container by
1043
+ * membership, the deep witness by identity). */ function notifyFold(e, n, t) {
1044
+ if (e.dk !== null && n !== t) bumpDeep(e);
1045
+ const r = e.n;
1046
+ if (r !== null) {
1047
+ for (const e of Reflect.ownKeys(r)) notifyKeyDiff(r[e], e, n, t);
1048
+ }
1049
+ notifyFoldTail(e, n, t);
1050
+ }
1051
+
1052
+ // ---------------------------------------------------------------------------
1053
+ // visibility
1054
+ let writing = 0;
1055
+
1056
+ /** Roots of the stores with an open setter: reads of THEIR drafts are the
1057
+ * writer's (no tracking, pending backing served); every other store reads
1058
+ * normally — a derive's external reads are its dependencies (#3037). */ let writeScopes = null;
1059
+
1060
+ const pendingNotify = new Set;
1061
+
1062
+ function scopeKey(e) {
1063
+ if (e.fam !== null) return e.fam;
1064
+ let n = e;
1065
+ while (n.u !== null) n = n.u;
1066
+ return n;
1067
+ }
1068
+
1069
+ function inDraft(e) {
1070
+ return writeScopes !== null && writeScopes.has(scopeKey(e));
1071
+ }
1072
+
1073
+ /** Shallow store: values served verbatim (a nested store proxy still wraps
1074
+ * into the family so writes stay isolated). */ function serveShallow(e, n, t) {
1075
+ if (t !== null && typeof t === "object" && t[$TARGET] !== undefined) return draftServe(e, wrap(t, e, n));
1076
+ return t;
1077
+ }
1078
+
1079
+ /** A child proxy served to a draft joins the draft's scope. */ function draftServe(e, n) {
1080
+ if (writeScopes !== null && inDraft(e)) {
1081
+ const e = n?.[$TARGET];
1082
+ if (e !== undefined && e.v !== undefined) writeScopes.add(scopeKey(e));
1083
+ }
1084
+ return n;
1085
+ }
1086
+
1087
+ /** The committed backing: the container node's committed value while a
1088
+ * batch is staged on it (an adoption swaps `t.v` eagerly — INTERNALS §3,
1089
+ * "adoption is eager by contract" — and the node keeps the frame committed
1090
+ * readers under a hold still see), `t.v` otherwise. */ function committed(e) {
1091
+ const n = e.k;
1092
+ if (n !== null) return n.Ae !== NOT_PENDING ? asBacking(n.pe) : e.v;
1093
+ // A node-less staging (`stageOn`): the fold queue remembers the frame.
1094
+ return e.pb !== null ? preBatch(e) : e.v;
1095
+ }
1096
+
1097
+ /** The pass that sees this flush's staging (signal parity — `read()`'s
1098
+ * staged arm): the computation running (`context`, never the owner — a
1099
+ * handler, an effect callback or `onSettled` has an owner but no pass),
1100
+ * unless it is a frame reader (children-forbidden, A32). Null = the
1101
+ * committed frame. */ function stagingReader() {
1102
+ let e = context;
1103
+ if (e === null) return null;
1104
+ if (e.Et) e = e.ut ?? null;
1105
+ return e === null || e.u & CONFIG_CHILDREN_FORBIDDEN ? null : e;
1106
+ }
1107
+
1108
+ /** The backing a read serves (RUL-1, signal parity): the draft and the write
1109
+ * override read the pending backing; a tracked reader reads it too — only
1110
+ * for presence and descriptors, its leaf (born from the two frames) decides
1111
+ * the value through core; a context-free or frame reader the committed
1112
+ * backing; a pass reading UNTRACKED asks the container node — `read()`
1113
+ * without a link: the node's value is the backing, chosen by the hold rules
1114
+ * (a render effect outside the parking flush sees committed and re-derives
1115
+ * at the landing; a memo joins the future; a staging is marked read). */
1116
+ /** The backing a read of `target` is served from — the truth (committed,
1117
+ * or the staging a pass may see). An optimistic family's lane values are
1118
+ * NOT composed here: a guessed key always has its node, and the node is
1119
+ * what serves its value (`nodeValue`); presence and enumeration compose
1120
+ * per key / per walk (`optimisticHas`, `enumerationSource`). */ function readSource(e, n, t = false) {
1121
+ // An optimistic setter's read before its first write sees what the writer
1122
+ // sees — the lanes' values and the tick's own guesses over the truth
1123
+ // (#3665), never a staging awaiting its fold: the draft is born on the
1124
+ // read (and notified at the setter's exit like any).
1125
+ if (userWrite && e.fam !== null && e.fam.opt === true && !pendingNotify.has(e) && inDraft(e)) {
1126
+ const n = ensurePB(e);
1127
+ pendingNotify.add(e);
1128
+ return n;
1129
+ }
1130
+ const r = e.pb;
1131
+ if (r === null) return e.v;
1132
+ if (inDraft(e) || writeOverride) {
1133
+ // The derive reads its own store through its draft: a key (or the
1134
+ // container) a transaction holds — a user's write inside an action,
1135
+ // held with it — is the future, and the pass that reads it derives
1136
+ // from the future (L2: a read of something already held joins it): the
1137
+ // derive's whole result reveals with the action (core R31 / #3733:
1138
+ // "under the transaction and revealed with it"), not key by key. A
1139
+ // user's setter reading its draft is no pass and joins nothing here.
1140
+ if (writeOverride && !userWrite) {
1141
+ const t = n !== undefined ? e.n?.[n] : undefined;
1142
+ if (t !== undefined) {
1143
+ if ((t.u & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD) joinPassTx(txOf(t));
1144
+ } else {
1145
+ const t = e.k;
1146
+ if (t !== null && (t.u & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD && (n === undefined || heldKeyChanged(e, n))) joinPassTx(txOf(t));
1147
+ }
1148
+ }
310
1149
  return r;
311
1150
  }
312
- const o = e.n?.[t] ?? nextAffectsNodeResolver(e, t);
313
- // Keyed marks resolve by identity too (#2904): another store family's
314
- // proxy can share this record's raw (a derived store swaps its backing to
315
- // the source's raw when its projection lands), and reads through it never
316
- // touch this target's node map. Scope is exactly the owning record's raw,
317
- // narrowed to this key for witness and birth inheritance.
318
- let r = affectsScopes.get(o);
319
- if (!r) affectsScopes.set(o, r = {
320
- scope: new Set,
321
- inherited: [],
322
- key: t
323
- });
324
- r.scope.add(e[STORE_VALUE]);
325
- if (e.pb) r.scope.add(e.pb);
326
- return [ o ];
327
- }
328
-
329
- export { $AFFECTS, $PROXY, $RECORD, $TARGET, $TRACK, STORE_HAS, STORE_NODE, STORE_VALUE, affectsScopesLive, getStoreAffectsNodes, getWriteOverride, inheritAffectsMarks, isRawValue, isWrappable, markRawIngest, markRawOne, nextAffectsNodeResolver, nextOptimisticViewResolver, ownEnumerableKeys, rawValuesUsed, setNextAffectsNodeResolver, setNextOptimisticViewResolver, setWriteOverride, witnessAffectsMark };
1151
+ const i = e.k;
1152
+ // A key whose leaf carries a lane's value (a guess, S4): the leaf serves
1153
+ // it — the container's frame is not consulted (a reader is not made a
1154
+ // stale reader of the container's hold for a key it does not see through
1155
+ // it).
1156
+ if (n !== undefined) {
1157
+ const t = e.n?.[n];
1158
+ if (t !== undefined && t.u & CONFIG_OVERRIDE) return committed(e);
1159
+ }
1160
+ const o = i !== null && (i.u & CONFIG_HELD) !== 0;
1161
+ // A held container: the hold's unit is the key (#3706). A key the HELD
1162
+ // staging left unchanged reads the ambient view — a mainline layer's
1163
+ // write above the hold publishes mainline (#3688, #3612); a key it
1164
+ // changed reads the frame core serves this reader.
1165
+ if (o && n !== undefined && !heldKeyChanged(e, n, t)) return r;
1166
+ const l = stagingReader();
1167
+ if (l === null) {
1168
+ if (getObserver() !== null) return r;
1169
+ // a frame reader's leaf decides the value
1170
+ // A projection's writes are its truth as soon as made (the derive is
1171
+ // the authority, nothing it writes is a proposal): a context-free reader
1172
+ // sees them before the flush — unless a hold keeps them staged.
1173
+ if (familyAhead(e)) return r;
1174
+ // A verdict window (`isPending`/`latest`) with no reader (a top-level
1175
+ // probe) judges the container like any node — for a key the batch
1176
+ // changed; an unchanged key is committed and final (A22: pending is per
1177
+ // key). A held container's committed frame is the node's.
1178
+ if (verdict === null || !o && n !== undefined && !keyChanged(e, n, t)) return o ? asBacking(i.pe) : e.v;
1179
+ return i === null ? committed(e) : asBacking(read(i));
1180
+ }
1181
+ // A pass reading a key the batch left unchanged: the same value in both
1182
+ // frames — committed, holding no one. Otherwise the container's frame by
1183
+ // core's rules — `read()` of the node WITHOUT a link (a value reader never
1184
+ // subscribes to the container): a render effect outside a hold's flush
1185
+ // sees committed and re-derives at the landing, a memo joins the future,
1186
+ // a staging read is marked. The key's own leaf, when it has one, decides
1187
+ // the value the same way.
1188
+ if (!o) {
1189
+ // A plain pass this flush already served a staging as the screen
1190
+ // (REACTIVE_STAGED_READ — what `read()` would set again here) reads an
1191
+ // unheld staging directly: an unchanged key is the same value in both
1192
+ // frames, a changed one is what the mark already stands for. The
1193
+ // per-key question is asked once per pass, not once per key (a
1194
+ // `mapArray` over 1k shifted rows asked it 1k times).
1195
+ if (l.R & REACTIVE_STAGED_READ && !(l.u & (CONFIG_VERDICT | CONFIG_CHILDREN_FORBIDDEN))) return r;
1196
+ if (n !== undefined && !keyChanged(e, n, t)) return committed(e);
1197
+ // A node-less staging (`stageOn`): what `read()` of its node would do
1198
+ // for this pass — a verdict reader sees the screen, any other derives
1199
+ // from the staging and is marked as having read it.
1200
+ if (i === null) {
1201
+ if (l.u & CONFIG_VERDICT && stagedScreen(l)) return committed(e);
1202
+ stagedRead(l);
1203
+ return r;
1204
+ }
1205
+ }
1206
+ return asBacking(untrack(() => read(i)));
1207
+ }
1208
+
1209
+ /** Did the HELD staging (the container node's, not a mainline layer above
1210
+ * it) change `key`? */ function heldKeyChanged(e, n, t = true) {
1211
+ return changedBetween(e, e.k.pe, e.k.Ae, n, t);
1212
+ }
1213
+
1214
+ /** A family target's unheld staging, read with no pass (a handler). */ function familyAhead(e) {
1215
+ return e.fam !== null && context === null && !(e.k !== null && e.k.u & CONFIG_HELD);
1216
+ }
1217
+
1218
+ /** Did this batch change `key` — value (slot equality), presence, and (for
1219
+ * a `shape` reader — a descriptor read, a node's hold) enumerability or
1220
+ * accessor-ness? A swapped or non-plain prototype, or a chained backing,
1221
+ * changes every key (#3706: those hold the whole container).
1222
+ *
1223
+ * A VALUE read asks without `shape`: a key whose value is the same in both
1224
+ * frames serves the same value from either, whatever its descriptor did —
1225
+ * and the descriptor probes (`__lookupGetter__`, `propertyIsEnumerable`,
1226
+ * twice each) are the cost of the question on a wide container read under
1227
+ * a staging (a keyed reconcile's 1k rows, read by `mapArray` in the flush
1228
+ * that staged them: ~3× the tick). A container with accessors seen (`a`)
1229
+ * keeps the full test — a getter's answer is not a slot's. */ function keyChanged(e, n, t = true) {
1230
+ return changedBetween(e, committed(e), e.pb, n, t);
1231
+ }
1232
+
1233
+ function changedBetween(e, n, t, r, i) {
1234
+ if (e.ch || t[$TARGET] !== undefined || Object.getPrototypeOf(t) !== Object.getPrototypeOf(n) || !plainProto(n)) return true;
1235
+ if (e.del !== null && e.del.has(r)) return r in n;
1236
+ const o = hasOwn.call(t, r) || e.ovl && hasOwn.call(e.v, r);
1237
+ if (o !== hasOwn.call(n, r)) return true;
1238
+ if (!o) return false;
1239
+ if ((i || e.a) && (isOwnAccessor(n, r) || isOwnAccessor(t, r) || propertyIsEnumerable.call(n, r) !== propertyIsEnumerable.call(t, r))) return true;
1240
+ const l = t[r];
1241
+ const u = n[r];
1242
+ return !isEqual(u, l) && !sameLogicalSlot(e, u, l);
1243
+ }
1244
+
1245
+ /** Value of a key with a leaf: core's — `read()` links a tracked reader and,
1246
+ * tracked or not, applies the hold and A28 rules a signal's read does. A
1247
+ * projection's unheld staging is ahead for a handler (`familyAhead`). */ function nodeValue(e, n, t) {
1248
+ let r = read(e);
1249
+ if (e.Ae !== NOT_PENDING && !(e.u & CONFIG_HELD) && context === null && t.fam !== null) r = e.Ae;
1250
+ return r === FORCE ? n : r;
1251
+ }
1252
+
1253
+ /** Serve a present data key: link the leaf for a tracked reader (creating it
1254
+ * on first read), wrap a wrappable child through the per-node wrap cache. */ function serveDataKey(e, n, t, r, i, o = -1) {
1255
+ // Read-through (§7b): the inner store's node is the truth; the outer's is
1256
+ // a subscription point only — unless it carries a guess (an optimistic
1257
+ // store over a store: the guess is the outer's, shown over the inner).
1258
+ const l = e.ch && r === e.v;
1259
+ let u = t;
1260
+ // A write-override read (a derive's draft) serves the backing: the derive
1261
+ // composes on the truth, never on a lane's or a hold's frame.
1262
+ if (!inDraft(e) && !writeOverride) {
1263
+ if (i === undefined && getObserver() !== null) i = getNode(e, n, o);
1264
+ if (i !== undefined) {
1265
+ const n = nodeValue(i, t, e);
1266
+ if (l) {
1267
+ // The link's committed value follows the inner store's (what a lane
1268
+ // over it covers — its reveal and revert compare against it, S4).
1269
+ i.pe = t;
1270
+ if (i.u & CONFIG_OVERRIDE) u = n;
1271
+ } else u = n;
1272
+ }
1273
+ }
1274
+ if (e.s) return serveShallow(e, n, u);
1275
+ // A derive's draft wrapper answers `$TARGET` without being a proxy: the
1276
+ // inner store's alias (#3767), resolved like a raw (#3859).
1277
+ if (e.ch && !l && u !== null && typeof u === "object" && u[$TARGET]?.px !== u) u = resolveChainedRaw(e, n, u);
1278
+ if (i !== undefined) {
1279
+ if (i.pxv === u && u !== undefined) return draftServe(e, i.px);
1280
+ if (!isWrappable(u)) return u;
1281
+ const t = wrap(u, e, n);
1282
+ i.px = t;
1283
+ i.pxv = u;
1284
+ return draftServe(e, t);
1285
+ }
1286
+ if (!isWrappable(u)) return u;
1287
+ return draftServe(e, wrap(u, e, n));
1288
+ }
1289
+
1290
+ /** Is `n` among `c`'s dependencies (any pass's — a kept tail included)? */ function linkedTo(e, n) {
1291
+ for (let t = e._; t !== null; t = t.p) if (t.I === n) return true;
1292
+ return false;
1293
+ }
1294
+
1295
+ /** The projection's obligation (plan sec. 31.7 Q-B): before any value is served
1296
+ * through a family target, the derive — the firewall — is brought up to
1297
+ * date WITHOUT the reader subscribing to it. Core's `read()` of the
1298
+ * firewall with no link does all of it: the pull, the status gate
1299
+ * (uninitialized / pending → NotReady, errored → the error; a verdict
1300
+ * window judges it; a pending source registers the reader for the landing)
1301
+ * and the reader's height. The derive's own draft ops are exempt (the write
1302
+ * override): the derive is the author. */ function pullFamily(e) {
1303
+ const n = e.fam.node;
1304
+ if (n === null || writeOverride || inDraft(e)) return;
1305
+ // A settled derive: the pull alone, no link (the barrier). A derive with a
1306
+ // flight up (or errored): the reader observes the flight exactly as a
1307
+ // reader of a memo would — linked, so it re-runs at the landing (the
1308
+ // next settled pass pulls without linking and the stale link trims), a
1309
+ // verdict reader registered, a tracked pass suspended.
1310
+ if (n.G & (STATUS_PENDING | STATUS_ERROR)) {
1311
+ // A render effect outside the flight's own flush is the frame, not a
1312
+ // derivation: it keeps what it shows and learns of the landing from the
1313
+ // leaves the landing changes (unchanged leaves say nothing — it is not a
1314
+ // stale reader of the derive). Inside the flush it suspends like any.
1315
+ // One that already observes the flight (linked by the pass that saw it
1316
+ // go up — the frame's hold, `blocked`) keeps observing it: a re-run for
1317
+ // another reason (a guess it read, the seam) that dropped the link would
1318
+ // release the hold with the flight still up.
1319
+ const t = context;
1320
+ if (t !== null && t.i === EFFECT_RENDER && n.u & CONFIG_HELD && flushTransaction !== txOf(n) && !linkedTo(t, n)) return;
1321
+ // An optimistic family carrying guesses that stand in for the flight
1322
+ // (A17): the guesses are the display — a reader is served the leaves
1323
+ // (committed plus the guesses) and does not suspend on the derive; a
1324
+ // verdict window still asks it (`isPending` is true while the flight is
1325
+ // up). The family's, not the target's: the edit stands in for the one
1326
+ // flight the family has.
1327
+ const r = e.fam.overlaid;
1328
+ if (verdict === null && n.G & STATUS_PENDING && r !== undefined && r.size !== 0) return;
1329
+ read(n);
1330
+ } else untrack(() => read(n));
1331
+ }
1332
+
1333
+ /** A projection's writes are its derive's: a staging made while the derive
1334
+ * is held (a flight up — its continuation writes after an `await`, a
1335
+ * callback's late write) belongs to the derive's transaction and reveals
1336
+ * with it, never drained early — and answers the derive's question: a quiet
1337
+ * re-ask's landing is quiet through its reveal (A19 exc. 2; the leaf's own
1338
+ * classification, as core reads it — `heldNotFinal`). */ function holdWithDerive(e, n) {
1339
+ const t = e.fam?.node;
1340
+ if (t !== undefined && t !== null && !userWrite && // the derive's own write (its draft or landing), not a user setter's
1341
+ t.u & CONFIG_HELD && n.Ae !== NOT_PENDING &&
1342
+ // A node carrying a guess is the lane's: its staging is the truth beneath
1343
+ // the guess (`setSignal` → `laneWrite`), never re-homed by the store.
1344
+ !(n.u & (CONFIG_HELD | CONFIG_OVERRIDE))) {
1345
+ holdNode(n, txOf(t));
1346
+ n.m.be = t.m.be;
1347
+ }
1348
+ }
1349
+
1350
+ /** A chained backing's child (§7b): a raw the inner family owns, or holds
1351
+ * at this key, is served as the inner store's wrapper — never a fresh
1352
+ * raw-keyed one — so the chained targets are the same objects across
1353
+ * settled and pending views. */ function resolveChainedRaw(e, n, t) {
1354
+ const r = e.v[$TARGET];
1355
+ if (r.ch) {
1356
+ const e = resolveChainedRaw(r, n, t);
1357
+ return e === t ? t : wrap(e, r, n);
1358
+ }
1359
+ const i = lookupTarget(t, r.fam);
1360
+ if (i !== undefined) return i.px;
1361
+ if ((r.v[n] === t || r.pb?.[n] === t) && isWrappable(t)) return wrap(t, r, n);
1362
+ return t;
1363
+ }
1364
+
1365
+ /** The backing a structural read serves: a tracked reader subscribes to the
1366
+ * container and reads the frame core serves it — committed or staging by
1367
+ * the hold rules (`read()` on the node: the node's value IS the backing);
1368
+ * anyone else reads by `readSource`. */ function enumerationSource(e) {
1369
+ if (!inDraft(e) && !writeOverride && getObserver() !== null) {
1370
+ // A container's arrangement guess is not a backing: the view composes
1371
+ // over the base it is shown over.
1372
+ const n = asBacking(read(getContainerNode(e)));
1373
+ return e.fam?.opt === true ? optHooks.view(e, n) : n;
1374
+ }
1375
+ const n = readSource(e);
1376
+ return optRead(e) ? optHooks.view(e, n) : n;
1377
+ }
1378
+
1379
+ /** A read on an optimistic family that composes the lanes' values: not the
1380
+ * draft's (it composed already), not a derive's write-override read (the
1381
+ * derive composes on the truth). */ function optRead(e) {
1382
+ return e.fam !== null && e.fam.opt === true && !inDraft(e) && !writeOverride;
1383
+ }
1384
+
1385
+ /** Pollution keys are never served from the prototype (core R30). */ const UNSAFE_KEYS = new Set([ "__proto__", "prototype", "constructor" ]);
1386
+
1387
+ // ---------------------------------------------------------------------------
1388
+ // traps
1389
+ const traps = {
1390
+ get(e, n, t) {
1391
+ // One typeof gates every brand-symbol compare off the hot string path.
1392
+ if (typeof n !== "string") {
1393
+ if (n === $TARGET) return e;
1394
+ if (n === $PROXY) return t;
1395
+ if (n === $OWNER) return undefined;
1396
+ // ownership stamp: never a user key
1397
+ if (n === $RECORD) return undefined;
1398
+ // a store is no view (see `viewOf`)
1399
+ // refresh()/isPending resolve the projection computed through $REFRESH.
1400
+ if (n === $REFRESH) return e.fam?.node ?? undefined;
1401
+ if (n === $TRACK) {
1402
+ if (e.fam !== null) pullFamily(e);
1403
+ if (!inDraft(e) && getObserver() !== null) {
1404
+ read(getContainerNode(e));
1405
+ // Structural chaining (§7b): a chained backing's $TRACK reads
1406
+ // through to the INNER store's container — structural
1407
+ // notifications land on the source's own node.
1408
+ const n = readSource(e);
1409
+ if (n[$TARGET] !== undefined) n[$TRACK];
1410
+ }
1411
+ return undefined;
1412
+ }
1413
+ // user symbols fall through to the generic path
1414
+ }
1415
+ // (The witness before the pull: a mark on an uninitialized derived
1416
+ // store is witnessed, then the pull throws — loading, declared pending.)
1417
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.witness(e, n);
1418
+ if (e.fam !== null) pullFamily(e);
1419
+ // Hot inline case: existing PLAIN node (non-accessor) read outside any
1420
+ // draft — the dbmon/uibench effect re-read shape. Core's `read()` serves
1421
+ // it (linking a tracked reader; the hold rules either way). Skips
1422
+ // serveDataKey's frame, the FORCE compare (only accessor keys ever hold
1423
+ // the sentinel), and isWrappable for primitives. ONE node-map lookup
1424
+ // serves this block and the accessor probe below. Before the backing is
1425
+ // consulted: a deleted key's node serves `undefined` (notifyWrites), and
1426
+ // a chained target's node carrying a GUESS is the value (A17 — the base
1427
+ // beneath it, and any hold there, is not this reader's concern; a link
1428
+ // without one reads through below).
1429
+ const r = e.n?.[n];
1430
+ if (writeScopes === null && !writeOverride) {
1431
+ const t = r;
1432
+ if (t !== undefined && t.acc !== true && (!e.ch || t.u & CONFIG_OVERRIDE)) {
1433
+ const r = nodeValue(t, undefined, e);
1434
+ if (r === null || typeof r !== "object") return r;
1435
+ if (e.s) return serveShallow(e, n, r);
1436
+ if (t.pxv === r) return t.px;
1437
+ if (isWrappable(r)) {
1438
+ const i = wrap(r, e, n);
1439
+ t.px = i;
1440
+ t.pxv = r;
1441
+ return i;
1442
+ }
1443
+ return r;
1444
+ }
1445
+ }
1446
+ const i = readSource(e, n);
1447
+ // Overlay delete (#3044): a prototype overlay cannot shadow a delete, so
1448
+ // deleted keys are tracked aside and read as absent in the pending view.
1449
+ if (e.del !== null && i === e.pb && e.del.has(n)) {
1450
+ if (!inDraft(e) && getObserver() !== null) read(getNode(e, n));
1451
+ return undefined;
1452
+ }
1453
+ // Accessor keys serve through Reflect.get with the PROXY receiver
1454
+ // (R20/R29: internal reads track; the node is linked for shape-change
1455
+ // notification but its value is never served). Accessor-ness comes from
1456
+ // the node's cached flag; the first TRACKED read (which creates the
1457
+ // node) probes once — untracked node-less reads take the plain path,
1458
+ // where a raw-receiver getter still returns correct committed values.
1459
+ // Tracking suppression is PER-TARGET (inDraft), never global (#3037).
1460
+ let o = -1;
1461
+ {
1462
+ let l;
1463
+ if (r !== undefined) l = r.acc === true; else if (!inDraft(e) && getObserver() !== null) {
1464
+ l = isOwnAccessor(i, n);
1465
+ if (i === (e.pb ?? e.v)) o = l ? 1 : 0;
1466
+ } else l = false;
1467
+ if (l) {
1468
+ if (!inDraft(e) && getObserver() !== null) read(r ?? getNode(e, n, o));
1469
+ const l = Reflect.get(i, n, t);
1470
+ if (e.s) return serveShallow(e, n, l);
1471
+ return isWrappable(l) ? draftServe(e, wrap(l, e, n)) : l;
1472
+ }
1473
+ }
1474
+ // Plain-data fast path: no descriptor allocation per read. Inherited
1475
+ // pollution keys are never served (core R30) — checked before the
1476
+ // proto-function branch can leak `constructor`. Overlay pending backings
1477
+ // chain to the committed backing, so "own in the view" means own on
1478
+ // either layer — a genuine prototype method is one that is own on
1479
+ // NEITHER.
1480
+ const l = e.ovl && i === e.pb;
1481
+ if ((n === "constructor" || n === "__proto__" || n === "prototype") && !hasOwn.call(i, n) && !(l && hasOwn.call(e.v, n))) return undefined;
1482
+ let u = i[n];
1483
+ if (u === undefined ? !hasOwn.call(i, n) && !(l && hasOwn.call(e.v, n)) : false) {
1484
+ // Inherited: prototype getters/methods run with the proxy receiver.
1485
+ u = Reflect.get(i, n, t);
1486
+ if (typeof u === "function") return u;
1487
+ // proto methods untracked
1488
+ // Read-through (§7b): the inner store answers an absent key too — the
1489
+ // outer's node is a subscription point unless it carries a guess.
1490
+ if (e.ch && i === e.v) return serveDataKey(e, n, u, i, r, o);
1491
+ // Reading a currently-absent own key subscribes to it (R12) — for any
1492
+ // target OUTSIDE its own draft scope, even mid-setter (#3037, above).
1493
+ if (u === undefined && !inDraft(e)) {
1494
+ if (getObserver() !== null) read(getNode(e, n, o));
1495
+ const t = e.n?.[n];
1496
+ if (t) {
1497
+ const r = nodeValue(t, undefined, e);
1498
+ if (e.s) return serveShallow(e, n, r);
1499
+ return isWrappable(r) ? draftServe(e, wrap(r, e, n)) : r;
1500
+ }
1501
+ }
1502
+ if (e.s) return serveShallow(e, n, u);
1503
+ return isWrappable(u) ? draftServe(e, wrap(u, e, n)) : u;
1504
+ }
1505
+ if (typeof u === "function" && !hasOwn.call(i, n) && !(l && hasOwn.call(e.v, n))) return u;
1506
+ // proto method
1507
+ return serveDataKey(e, n, u, i, r, o);
1508
+ },
1509
+ has(e, n) {
1510
+ if (n === $TARGET || n === $PROXY || n === $TRACK) return true;
1511
+ if (n === $OWNER || n === $RECORD) return false;
1512
+ // (The witness before the pull: a mark on an uninitialized derived
1513
+ // store is witnessed, then the pull throws — loading, declared pending.)
1514
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.witness(e, n);
1515
+ if (e.fam !== null) pullFamily(e);
1516
+ const t = readSource(e, n);
1517
+ // A tracked reader's presence node answers (born from the two frames;
1518
+ // core decides which it sees); everyone else reads the backing. A
1519
+ // chained target's node is a subscription point (§7b): the inner store
1520
+ // answers through the backing — unless the outer node carries a guess.
1521
+ if (!inDraft(e) && !writeOverride) {
1522
+ if (getObserver() !== null) {
1523
+ const r = getHasNode(e, n);
1524
+ const i = read(r);
1525
+ if (!e.ch || t !== e.v) return !!i;
1526
+ const o = n in t;
1527
+ r.pe = o;
1528
+ // the link follows the inner store (S4)
1529
+ if (r.u & CONFIG_OVERRIDE) return !!i;
1530
+ return o;
1531
+ } else if (optRead(e)) {
1532
+ const t = optHooks.has(e, n);
1533
+ if (t !== undefined) return t;
1534
+ }
1535
+ }
1536
+ return n in t && !(e.del !== null && t === e.pb && e.del.has(n));
1537
+ },
1538
+ ownKeys(e) {
1539
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.witness(e, undefined);
1540
+ if (e.fam !== null) pullFamily(e);
1541
+ return visibleKeys(e, enumerationSource(e));
1542
+ },
1543
+ getOwnPropertyDescriptor(e, n) {
1544
+ if (n === $OWNER || n === $RECORD) return undefined;
1545
+ const t = getObserver();
1546
+ if (verdict !== null && affectsHooks !== null && t === null) affectsHooks.witness(e, n);
1547
+ if (e.fam !== null) pullFamily(e);
1548
+ // An enumerator (spread, Object.entries) already holds the container
1549
+ // node and reads its frame; a descriptor read on its own tracks
1550
+ // presence (R13).
1551
+ const r = t !== null && !inDraft(e) && !writeOverride;
1552
+ const i = r && observerHoldsContainer(e, t);
1553
+ let o;
1554
+ if (i) o = enumerationSource(e); else if (r) {
1555
+ // The presence node's frame (core decides which) is the one described
1556
+ // (a chained target's inner store answers through the backing, §7b);
1557
+ // the descriptor itself comes from the backing the container's frame
1558
+ // serves this reader (`readSource`: a held container's changed key —
1559
+ // enumerability, accessor-ness — reads committed for a stale reader,
1560
+ // the staging for a member; #3706 contrast).
1561
+ const t = getHasNode(e, n);
1562
+ const r = read(t);
1563
+ o = readSource(e, n, true);
1564
+ if (!r && (!e.ch || t.u & CONFIG_OVERRIDE || !(n in o))) return undefined;
1565
+ } else o = readSource(e, n, true);
1566
+ let l;
1567
+ if (!i && optRead(e)) {
1568
+ const t = optHooks.descriptor(e, n);
1569
+ if (t === null) return undefined;
1570
+ l = t;
1571
+ }
1572
+ l ??= visibleDescriptor(e, o, n);
1573
+ if (l === undefined) return undefined;
1574
+ if (!(n === "length" && Array.isArray(e))) l.configurable = true;
1575
+ return l;
1576
+ },
1577
+ set(e, n, t) {
1578
+ const r = inDraft(e);
1579
+ const i = !r && writeOverride;
1580
+ if (!r && !i) return true;
1581
+ if (n === "__proto__") return true;
1582
+ // pollution guard (core R30)
1583
+ const o = e.s ? t : unwrapValue(t);
1584
+ const l = ensurePB(e);
1585
+ pendingNotify.add(e);
1586
+ if (Array.isArray(l)) {
1587
+ if (n === "length") e.wk = WK_ALL; else if (e.wk !== WK_ALL) {
1588
+ const t = e.wk ??= new Set;
1589
+ t.add(n);
1590
+ t.add("length");
1591
+ }
1592
+ } else {
1593
+ if (e.wk !== WK_ALL) (e.wk ??= new Set).add(n);
1594
+ if (!(n in l)) e.kc++;
1595
+ }
1596
+ if (UNSAFE_KEYS.has(n)) {
1597
+ Object.defineProperty(l, n, {
1598
+ value: o,
1599
+ writable: true,
1600
+ enumerable: true,
1601
+ configurable: true
1602
+ });
1603
+ if (e.del !== null) e.del.delete(n);
1604
+ return true;
1605
+ }
1606
+ if (e.ovl && e.sc !== 2 && !hasOwn.call(l, n)) {
1607
+ Object.defineProperty(l, n, {
1608
+ value: o,
1609
+ writable: true,
1610
+ enumerable: true,
1611
+ configurable: true
1612
+ });
1613
+ } else l[n] = o;
1614
+ if (e.del !== null) e.del.delete(n);
1615
+ if (e.s && o !== null && typeof o === "object") markRawOne(o);
1616
+ if (i) notifyWrites(e);
1617
+ return true;
1618
+ },
1619
+ defineProperty(e, n, t) {
1620
+ const r = inDraft(e);
1621
+ const i = !r && writeOverride;
1622
+ if (!r && !i) return true;
1623
+ if (n === "__proto__") return true;
1624
+ if (t.get || t.set) e.a = true;
1625
+ if ("value" in t) t = {
1626
+ ...t,
1627
+ value: unwrapValue(t.value)
1628
+ };
1629
+ const o = ensurePB(e);
1630
+ if (e.a || !(t.enumerable && t.writable && t.configurable)) e.sc = 1;
1631
+ pendingNotify.add(e);
1632
+ if (e.wk !== WK_ALL) (e.wk ??= new Set).add(n);
1633
+ Object.defineProperty(o, n, t);
1634
+ if (e.del !== null) e.del.delete(n);
1635
+ if (i) notifyWrites(e);
1636
+ return true;
1637
+ },
1638
+ deleteProperty(e, n) {
1639
+ const t = inDraft(e);
1640
+ const r = !t && writeOverride;
1641
+ if (!t && !r) return true;
1642
+ const i = ensurePB(e);
1643
+ pendingNotify.add(e);
1644
+ if (e.wk !== WK_ALL) (e.wk ??= new Set).add(n);
1645
+ delete i[n];
1646
+ if (e.ovl && hasOwn.call(e.v, n)) (e.del ??= new Set).add(n);
1647
+ if (r) notifyWrites(e);
1648
+ return true;
1649
+ }
1650
+ };
1651
+
1652
+ /** Is `obs` subscribed to the container BY THIS PASS (its enumerator holds
1653
+ * it)? A link left from a previous pass is stale — the reader must subscribe
1654
+ * to the key's presence node or a later add never re-runs it. */ function observerHoldsContainer(e, n) {
1655
+ const t = e.k;
1656
+ if (t === null) return false;
1657
+ const r = t.qe;
1658
+ return r !== null && r.o === n && (!(n.R & REACTIVE_RECOMPUTING_DEPS) || r.Ue === n.Ge);
1659
+ }
1660
+
1661
+ function storeSetter(e, n, t = true) {
1662
+ const r = e[$TARGET];
1663
+ const i = writeScopes;
1664
+ writeScopes = new Set;
1665
+ writeScopes.add(scopeKey(r));
1666
+ const o = userWrite;
1667
+ userWrite = t;
1668
+ writing++;
1669
+ let l;
1670
+ try {
1671
+ // No untrack: the writing flag already disables store-node linking
1672
+ // (draft reads never self-track, proj R2), while EXTERNAL reads (signals
1673
+ // inside a projection derive) must keep tracking — they are the derive's
1674
+ // dependencies.
1675
+ l = n(e);
1676
+ } finally {
1677
+ writing--;
1678
+ writeScopes = i;
1679
+ // Outermost setter exit: emit write-time notifications (one node write
1680
+ // per changed observed key) so holds and lanes engage now.
1681
+ if (writing === 0 && pendingNotify.size) {
1682
+ const e = [ ...pendingNotify ];
1683
+ pendingNotify.clear();
1684
+ for (const n of e) notifyWrites(n);
1685
+ // A direct commit (a projection's creation run) folds at the setter's
1686
+ // exit, unqueued: the backing is the pre-batch one (nothing swapped).
1687
+ if (directCommit) for (const n of e) if (n.pb !== null) foldTarget(n, n.v);
1688
+ }
1689
+ userWrite = o;
1690
+ }
1691
+ if (l !== undefined && l !== e && isWrappable(l)) {
1692
+ // A returned replacement: on an optimistic family a user's is itself an
1693
+ // optimistic edit — diffed against the view as guesses (`next` parity);
1694
+ // otherwise it is adopted as the incoming truth.
1695
+ if (t && r.fam?.opt === true && !writeOverride) optHooks.writes(r, unwrapValue(l)); else adoptPB(r, unwrapValue(l));
1696
+ }
1697
+ // A34 (3), #3612 — the derived store's twin of `setMemo`: a user write to
1698
+ // a derivation another transaction holds is nobody's proposal; it becomes
1699
+ // the draft's prior state and the hold re-derives over it (the writes
1700
+ // joined the hold through `setSignal`; the derive re-runs under it).
1701
+ if (t && heldDerivationHit) {
1702
+ heldDerivationHit = false;
1703
+ const e = r.fam.node;
1704
+ if (!(e.R & REACTIVE_DIRTY)) {
1705
+ e.R = e.R & ~REACTIVE_CHECK | REACTIVE_DIRTY;
1706
+ insertIntoHeap(e, dirtyQueue);
1707
+ schedule();
1708
+ }
1709
+ }
1710
+ }
1711
+
1712
+ function createStore(e, n) {
1713
+ const t = n?.shallow === true;
1714
+ const r = wrap(e);
1715
+ if (t) {
1716
+ r[$TARGET].s = true;
1717
+ markRawIngest(e);
1718
+ }
1719
+ const setter = e => storeSetter(r, e);
1720
+ return [ r, setter ];
1721
+ }
1722
+
1723
+ /** True when `proxy` is a SHALLOW store (children served verbatim, slots
1724
+ * replaced by reference — #2932). The list driver uses this to choose the
1725
+ * slot-patch channel (collected row bodies) over per-record registration. */ function storeIsShallow(e) {
1726
+ const n = e?.[$TARGET];
1727
+ return n !== undefined && n.s === true;
1728
+ }
1729
+
1730
+ /** True when `proxy` belongs to a projection family (S3). */ function storeHasFamily(e) {
1731
+ const n = e?.[$TARGET];
1732
+ return n !== undefined && n.fam !== null;
1733
+ }
1734
+
1735
+ /** True when `proxy` belongs to an OPTIMISTIC family (S4). */ function storeHasOptimisticFamily(e) {
1736
+ const n = e?.[$TARGET];
1737
+ return n !== undefined && n.fam?.opt === true;
1738
+ }
1739
+
1740
+ // ---------------------------------------------------------------------------
1741
+ // enumeration views (one visibility rule for the traps AND the deep walk)
1742
+ function visibleKeys(e, n) {
1743
+ let t;
1744
+ if (e.ovl && n === e.pb) {
1745
+ t = Reflect.ownKeys(e.v);
1746
+ const r = e.del;
1747
+ if (r !== null && r.size !== 0) t = t.filter(e => !r.has(e));
1748
+ for (const r of Reflect.ownKeys(n)) {
1749
+ if (!hasOwn.call(e.v, r)) t.push(r);
1750
+ }
1751
+ } else t = Reflect.ownKeys(n);
1752
+ // The ownership stamp is the last symbol on an owned backing: hide it.
1753
+ for (let e = t.length - 1; e >= 0 && typeof t[e] === "symbol"; e--) {
1754
+ if (t[e] === $OWNER) {
1755
+ t.splice(e, 1);
1756
+ break;
1757
+ }
1758
+ }
1759
+ return t;
1760
+ }
1761
+
1762
+ function visibleDescriptor(e, n, t) {
1763
+ let r = Object.getOwnPropertyDescriptor(n, t);
1764
+ if (e.ovl && n === e.pb) {
1765
+ if (e.del !== null && e.del.has(t)) return undefined;
1766
+ if (r === undefined) r = Object.getOwnPropertyDescriptor(e.v, t);
1767
+ }
1768
+ return r;
1769
+ }
1770
+
1771
+ // ---------------------------------------------------------------------------
1772
+ // deep / snapshot
1773
+ /** Tracking snapshot: subscribes every reachable record's container and deep
1774
+ * witness (one node per record, not one per path), then snapshots. */ function deep(e) {
1775
+ const n = e?.[$TARGET];
1776
+ if (n === undefined || n.px !== e) return e;
1777
+ const t = new Set;
1778
+ const childTarget = (e, n, t) => {
1779
+ let r = lookupTarget(n, e.fam);
1780
+ if (r === undefined) {
1781
+ if (!isWrappable(n)) return undefined;
1782
+ wrap(n, e, t);
1783
+ r = lookupTarget(n, e.fam) ?? n[$TARGET];
1784
+ }
1785
+ return r;
1786
+ };
1787
+ const walkT = e => {
1788
+ if (e.fam !== null) pullFamily(e);
1789
+ const n = enumerationSource(e);
1790
+ if (t.has(n)) return;
1791
+ t.add(n);
1792
+ read(getDeepNode(e));
1793
+ // Through a chain (#3323): every inner record's container AND deep
1794
+ // witness — base writes bump the inner witnesses.
1795
+ for (let n = e; n.ch; ) {
1796
+ n = n.v[$TARGET];
1797
+ read(getContainerNode(n));
1798
+ read(getDeepNode(n));
1799
+ }
1800
+ for (const t of visibleKeys(e, n)) {
1801
+ const r = visibleDescriptor(e, n, t);
1802
+ if (r === undefined) continue;
1803
+ if (r.get || r.set) {
1804
+ e.a = true;
1805
+ continue;
1806
+ // accessors track through their own reads when invoked
1807
+ }
1808
+ let i = r.value;
1809
+ if (i === null || typeof i !== "object") continue;
1810
+ if (e.ch && i[$TARGET] === undefined) i = resolveChainedRaw(e, t, i);
1811
+ const o = childTarget(e, i, t);
1812
+ if (o === undefined) continue;
1813
+ // raw-marked: leaf by contract
1814
+ walkT(o);
1815
+ }
1816
+ };
1817
+ walkT(n);
1818
+ return snapshot(e);
1819
+ }
1820
+
1821
+ /** Non-tracking snapshot: source identity for unowned subtrees (zero copy),
1822
+ * a copy for owned ones (they are by definition modified relative to the
1823
+ * source). Sees the pending backing (R27). */ function snapshot(e) {
1824
+ const n = e?.[$TARGET];
1825
+ return snapshotWalk(e, new Map, n?.fam ?? null);
1826
+ }
1827
+
1828
+ function snapshotWalk(e, n, t) {
1829
+ if (e === null || typeof e !== "object") return e;
1830
+ let r = e;
1831
+ for (let e = true; ;e = false) {
1832
+ const n = r?.[$TARGET]?.v !== undefined;
1833
+ let i = n ? r[$TARGET] : undefined;
1834
+ if (i === undefined && t !== null) i = lookupTarget(r, t);
1835
+ if (i === undefined) i = lookupTarget(r, null);
1836
+ if (i === undefined) break;
1837
+ if (e && !n && t !== null && i.fam !== t) {
1838
+ const e = t.map.get(i.px);
1839
+ if (e !== undefined) i = e;
1840
+ }
1841
+ if (i.fam !== null) t = i.fam;
1842
+ // R27: a snapshot sees the batch's pending backing — a held one (another
1843
+ // transaction's future) only from its own draft.
1844
+ let o = i.v;
1845
+ if (i.pb !== null && (inDraft(i) || writeOverride || i.k === null || !(i.k.u & CONFIG_HELD))) {
1846
+ if (i.ovl) materializePB(i);
1847
+ o = i.pb;
1848
+ }
1849
+ if (optRead(i)) o = optHooks.view(i, o);
1850
+ if (o === r) break;
1851
+ r = o;
1852
+ }
1853
+ if (!isWrappable(r)) return r;
1854
+ const i = n.get(r);
1855
+ if (i !== undefined) return i;
1856
+ if (isOwned(r)) {
1857
+ const e = Array.isArray(r);
1858
+ const i = e ? [] : Object.create(Object.getPrototypeOf(r));
1859
+ n.set(r, i);
1860
+ for (const o of Reflect.ownKeys(r)) {
1861
+ if (e && o === "length" || o === $OWNER) continue;
1862
+ const l = Object.getOwnPropertyDescriptor(r, o);
1863
+ if (typeof o === "symbol" && !l.enumerable) continue;
1864
+ if (l.get || l.set) {
1865
+ Object.defineProperty(i, o, l);
1866
+ continue;
1867
+ }
1868
+ const u = l.value;
1869
+ const f = u !== null && typeof u === "object" ? snapshotWalk(u, n, t) : u;
1870
+ if (l.enumerable && l.writable && l.configurable) i[o] = f; else Object.defineProperty(i, o, {
1871
+ ...l,
1872
+ value: f
1873
+ });
1874
+ }
1875
+ if (e && i.length !== r.length) i.length = r.length;
1876
+ return i;
1877
+ }
1878
+ n.set(r, r);
1879
+ let o = null;
1880
+ for (const e of Reflect.ownKeys(r)) {
1881
+ const i = Object.getOwnPropertyDescriptor(r, e);
1882
+ if (!i || i.get || i.set) continue;
1883
+ const l = i.value;
1884
+ if (l === null || typeof l !== "object") continue;
1885
+ const u = snapshotWalk(l, n, t);
1886
+ if (u !== l) {
1887
+ if (o === null) {
1888
+ o = Array.isArray(r) ? [ ...r ] : Object.create(Object.getPrototypeOf(r), Object.getOwnPropertyDescriptors(r));
1889
+ n.set(r, o);
1890
+ }
1891
+ o[e] = u;
1892
+ }
1893
+ }
1894
+ return o ?? r;
1895
+ }
1896
+
1897
+ export { $PROXY, $TARGET, $TRACK, LaneView, adoptPB, affectsHooks, arrayStructureChanged, asBacking, bumpDeep, cloneRaw, committed, containerEquals, createStore, deep, getContainerNode, getHasNode, getNode, hasAccessorFlag, installAffectsHooks, installOptHooks, materializePB, membershipChanged, notifyFold, notifyFoldTail, notifyKeyDiff, notifyKeyValue, optHooks, resolveChainedRaw, runDirect, sameKey, snapshot, storeHasFamily, storeHasOptimisticFamily, storeIsShallow, storeSetter, targetsEqual, unwrapValue, userWriting, wrap };