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

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