foottrace 0.0.0-stage → 1.0.1
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.
- package/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/lib/capture/freeze.d.ts +91 -0
- package/dist/esm/lib/capture/freeze.js +313 -0
- package/dist/esm/lib/capture/ownData.d.ts +18 -0
- package/dist/esm/lib/capture/ownData.js +35 -0
- package/dist/esm/lib/capture/valueKinds.d.ts +57 -0
- package/dist/esm/lib/capture/valueKinds.js +141 -0
- package/dist/esm/lib/ids/runtimeStageId.d.ts +173 -0
- package/dist/esm/lib/ids/runtimeStageId.js +193 -0
- package/dist/esm/lib/memory/EventLog.d.ts +105 -0
- package/dist/esm/lib/memory/EventLog.js +147 -0
- package/dist/esm/lib/memory/RecordFrame.d.ts +232 -0
- package/dist/esm/lib/memory/RecordFrame.js +295 -0
- package/dist/esm/lib/memory/SharedMemory.d.ts +46 -0
- package/dist/esm/lib/memory/SharedMemory.js +87 -0
- package/dist/esm/lib/memory/TransactionBuffer.d.ts +521 -0
- package/dist/esm/lib/memory/TransactionBuffer.js +946 -0
- package/dist/esm/lib/memory/admission.d.ts +81 -0
- package/dist/esm/lib/memory/admission.js +152 -0
- package/dist/esm/lib/memory/backtrack.d.ts +292 -0
- package/dist/esm/lib/memory/backtrack.js +466 -0
- package/dist/esm/lib/memory/commitLogUtils.d.ts +217 -0
- package/dist/esm/lib/memory/commitLogUtils.js +383 -0
- package/dist/esm/lib/memory/deltaEncoding.d.ts +66 -0
- package/dist/esm/lib/memory/deltaEncoding.js +121 -0
- package/dist/esm/lib/memory/equality.d.ts +93 -0
- package/dist/esm/lib/memory/equality.js +261 -0
- package/dist/esm/lib/memory/eventPosition.d.ts +18 -0
- package/dist/esm/lib/memory/eventPosition.js +1 -0
- package/dist/esm/lib/memory/honesty.d.ts +93 -0
- package/dist/esm/lib/memory/honesty.js +91 -0
- package/dist/esm/lib/memory/keyPaths.d.ts +125 -0
- package/dist/esm/lib/memory/keyPaths.js +226 -0
- package/dist/esm/lib/memory/logModel.d.ts +76 -0
- package/dist/esm/lib/memory/logModel.js +363 -0
- package/dist/esm/lib/memory/merge.d.ts +26 -0
- package/dist/esm/lib/memory/merge.js +51 -0
- package/dist/esm/lib/memory/pathOps.d.ts +118 -0
- package/dist/esm/lib/memory/pathOps.js +292 -0
- package/dist/esm/lib/memory/paths.d.ts +56 -0
- package/dist/esm/lib/memory/paths.js +60 -0
- package/dist/esm/lib/memory/placeholders.d.ts +39 -0
- package/dist/esm/lib/memory/placeholders.js +39 -0
- package/dist/esm/lib/memory/recordCommit.d.ts +45 -0
- package/dist/esm/lib/memory/recordCommit.js +60 -0
- package/dist/esm/lib/memory/scrub.d.ts +52 -0
- package/dist/esm/lib/memory/scrub.js +72 -0
- package/dist/esm/lib/memory/types.d.ts +209 -0
- package/dist/esm/lib/memory/types.js +9 -0
- package/dist/esm/lib/memory/utils.d.ts +73 -0
- package/dist/esm/lib/memory/utils.js +150 -0
- package/dist/esm/lib/memory/verbs.d.ts +333 -0
- package/dist/esm/lib/memory/verbs.js +426 -0
- package/dist/esm/lib/recorder/CommitRangeIndex.d.ts +147 -0
- package/dist/esm/lib/recorder/CommitRangeIndex.js +206 -0
- package/dist/esm/lib/slice/elementProvenance.d.ts +81 -0
- package/dist/esm/lib/slice/elementProvenance.js +189 -0
- package/dist/esm/lib/slice/forwardSliceForKey.d.ts +88 -0
- package/dist/esm/lib/slice/forwardSliceForKey.js +242 -0
- package/dist/esm/lib/slice/index.d.ts +26 -0
- package/dist/esm/lib/slice/index.js +25 -0
- package/dist/esm/lib/slice/keyIndex.d.ts +137 -0
- package/dist/esm/lib/slice/keyIndex.js +292 -0
- package/dist/esm/lib/slice/keyTimeline.d.ts +49 -0
- package/dist/esm/lib/slice/keyTimeline.js +112 -0
- package/dist/esm/lib/slice/keysReadSources.d.ts +51 -0
- package/dist/esm/lib/slice/keysReadSources.js +94 -0
- package/dist/esm/lib/slice/serialize.d.ts +78 -0
- package/dist/esm/lib/slice/serialize.js +277 -0
- package/dist/esm/lib/slice/sliceForKey.d.ts +40 -0
- package/dist/esm/lib/slice/sliceForKey.js +80 -0
- package/dist/esm/lib/slice/types.d.ts +567 -0
- package/dist/esm/lib/slice/types.js +24 -0
- package/dist/esm/lib/time-travel/axis.d.ts +119 -0
- package/dist/esm/lib/time-travel/axis.js +178 -0
- package/dist/esm/lib/time-travel/bundles.d.ts +77 -0
- package/dist/esm/lib/time-travel/bundles.js +135 -0
- package/dist/esm/lib/time-travel/chain.d.ts +93 -0
- package/dist/esm/lib/time-travel/chain.js +187 -0
- package/dist/esm/lib/time-travel/commitStops.d.ts +55 -0
- package/dist/esm/lib/time-travel/commitStops.js +201 -0
- package/dist/esm/lib/time-travel/index.d.ts +22 -0
- package/dist/esm/lib/time-travel/index.js +20 -0
- package/dist/esm/lib/time-travel/stateAt.d.ts +102 -0
- package/dist/esm/lib/time-travel/stateAt.js +152 -0
- package/dist/esm/lib/time-travel/tagStops.d.ts +72 -0
- package/dist/esm/lib/time-travel/tagStops.js +101 -0
- package/dist/esm/lib/time-travel/timeTravel.d.ts +70 -0
- package/dist/esm/lib/time-travel/timeTravel.js +359 -0
- package/dist/esm/lib/time-travel/types.d.ts +470 -0
- package/dist/esm/lib/time-travel/types.js +22 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/paths.d.ts +4 -0
- package/dist/esm/paths.js +4 -0
- package/dist/esm/write.d.ts +49 -0
- package/dist/esm/write.js +47 -0
- package/dist/index.js +67 -0
- package/dist/lib/capture/freeze.js +319 -0
- package/dist/lib/capture/ownData.js +40 -0
- package/dist/lib/capture/valueKinds.js +145 -0
- package/dist/lib/ids/runtimeStageId.js +209 -0
- package/dist/lib/memory/EventLog.js +151 -0
- package/dist/lib/memory/RecordFrame.js +299 -0
- package/dist/lib/memory/SharedMemory.js +91 -0
- package/dist/lib/memory/TransactionBuffer.js +950 -0
- package/dist/lib/memory/admission.js +158 -0
- package/dist/lib/memory/backtrack.js +473 -0
- package/dist/lib/memory/commitLogUtils.js +398 -0
- package/dist/lib/memory/deltaEncoding.js +127 -0
- package/dist/lib/memory/equality.js +265 -0
- package/dist/lib/memory/eventPosition.js +2 -0
- package/dist/lib/memory/honesty.js +94 -0
- package/dist/lib/memory/keyPaths.js +242 -0
- package/dist/lib/memory/logModel.js +371 -0
- package/dist/lib/memory/merge.js +55 -0
- package/dist/lib/memory/pathOps.js +308 -0
- package/dist/lib/memory/paths.js +66 -0
- package/dist/lib/memory/placeholders.js +42 -0
- package/dist/lib/memory/recordCommit.js +64 -0
- package/dist/lib/memory/scrub.js +77 -0
- package/dist/lib/memory/types.js +10 -0
- package/dist/lib/memory/utils.js +169 -0
- package/dist/lib/memory/verbs.js +443 -0
- package/dist/lib/recorder/CommitRangeIndex.js +210 -0
- package/dist/lib/slice/elementProvenance.js +194 -0
- package/dist/lib/slice/forwardSliceForKey.js +246 -0
- package/dist/lib/slice/index.js +42 -0
- package/dist/lib/slice/keyIndex.js +312 -0
- package/dist/lib/slice/keyTimeline.js +116 -0
- package/dist/lib/slice/keysReadSources.js +100 -0
- package/dist/lib/slice/serialize.js +285 -0
- package/dist/lib/slice/sliceForKey.js +84 -0
- package/dist/lib/slice/types.js +25 -0
- package/dist/lib/time-travel/axis.js +183 -0
- package/dist/lib/time-travel/bundles.js +143 -0
- package/dist/lib/time-travel/chain.js +192 -0
- package/dist/lib/time-travel/commitStops.js +205 -0
- package/dist/lib/time-travel/index.js +31 -0
- package/dist/lib/time-travel/stateAt.js +159 -0
- package/dist/lib/time-travel/tagStops.js +105 -0
- package/dist/lib/time-travel/timeTravel.js +363 -0
- package/dist/lib/time-travel/types.js +23 -0
- package/dist/paths.js +14 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/lib/capture/freeze.d.ts +91 -0
- package/dist/types/lib/capture/ownData.d.ts +18 -0
- package/dist/types/lib/capture/valueKinds.d.ts +57 -0
- package/dist/types/lib/ids/runtimeStageId.d.ts +173 -0
- package/dist/types/lib/memory/EventLog.d.ts +105 -0
- package/dist/types/lib/memory/RecordFrame.d.ts +232 -0
- package/dist/types/lib/memory/SharedMemory.d.ts +46 -0
- package/dist/types/lib/memory/TransactionBuffer.d.ts +521 -0
- package/dist/types/lib/memory/admission.d.ts +81 -0
- package/dist/types/lib/memory/backtrack.d.ts +292 -0
- package/dist/types/lib/memory/commitLogUtils.d.ts +217 -0
- package/dist/types/lib/memory/deltaEncoding.d.ts +66 -0
- package/dist/types/lib/memory/equality.d.ts +93 -0
- package/dist/types/lib/memory/eventPosition.d.ts +18 -0
- package/dist/types/lib/memory/honesty.d.ts +93 -0
- package/dist/types/lib/memory/keyPaths.d.ts +125 -0
- package/dist/types/lib/memory/logModel.d.ts +76 -0
- package/dist/types/lib/memory/merge.d.ts +26 -0
- package/dist/types/lib/memory/pathOps.d.ts +118 -0
- package/dist/types/lib/memory/paths.d.ts +56 -0
- package/dist/types/lib/memory/placeholders.d.ts +39 -0
- package/dist/types/lib/memory/recordCommit.d.ts +45 -0
- package/dist/types/lib/memory/scrub.d.ts +52 -0
- package/dist/types/lib/memory/types.d.ts +209 -0
- package/dist/types/lib/memory/utils.d.ts +73 -0
- package/dist/types/lib/memory/verbs.d.ts +333 -0
- package/dist/types/lib/recorder/CommitRangeIndex.d.ts +147 -0
- package/dist/types/lib/slice/elementProvenance.d.ts +81 -0
- package/dist/types/lib/slice/forwardSliceForKey.d.ts +88 -0
- package/dist/types/lib/slice/index.d.ts +26 -0
- package/dist/types/lib/slice/keyIndex.d.ts +137 -0
- package/dist/types/lib/slice/keyTimeline.d.ts +49 -0
- package/dist/types/lib/slice/keysReadSources.d.ts +51 -0
- package/dist/types/lib/slice/serialize.d.ts +78 -0
- package/dist/types/lib/slice/sliceForKey.d.ts +40 -0
- package/dist/types/lib/slice/types.d.ts +567 -0
- package/dist/types/lib/time-travel/axis.d.ts +119 -0
- package/dist/types/lib/time-travel/bundles.d.ts +77 -0
- package/dist/types/lib/time-travel/chain.d.ts +93 -0
- package/dist/types/lib/time-travel/commitStops.d.ts +55 -0
- package/dist/types/lib/time-travel/index.d.ts +22 -0
- package/dist/types/lib/time-travel/stateAt.d.ts +102 -0
- package/dist/types/lib/time-travel/tagStops.d.ts +72 -0
- package/dist/types/lib/time-travel/timeTravel.d.ts +70 -0
- package/dist/types/lib/time-travel/types.d.ts +470 -0
- package/dist/types/paths.d.ts +4 -0
- package/dist/types/write.d.ts +49 -0
- package/dist/write.js +56 -0
- package/package.json +48 -4
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RecordFrame — the record half of one stage's frame (C3, L3).
|
|
3
|
+
*
|
|
4
|
+
* What a stage's frame needs to READ and WRITE the record, and nothing about the stage inside a run:
|
|
5
|
+
* where it writes (its address), what it read first (the first-touch base), what it staged (the lazy
|
|
6
|
+
* transaction buffer) and which of it the log must not show (the scrub of each write), which keys it
|
|
7
|
+
* read (the readKeys list), and where a commit lands (live state, the redacted mirror, the log —
|
|
8
|
+
* through `recordCommit`). The engine's frame (`StageContext`, L4) composes one and keeps the rest:
|
|
9
|
+
* retention, diagnostics, the dev-mode warnings, the commit observer and the redaction DECISION —
|
|
10
|
+
* which value is secret (`redaction.ts · decideWrite`); the frame writes the bytes it is handed (C4).
|
|
11
|
+
*
|
|
12
|
+
* THE LAW:
|
|
13
|
+
* 1. ADDRESS. The frame reads and writes at its address, a path prefix the engine computed and hands
|
|
14
|
+
* over as data (C2: `['runs', <id>]` or `[]`, the root). It moves only before the first write:
|
|
15
|
+
* the buffer keeps the address it was built at (the engine refuses a later move).
|
|
16
|
+
* 2. FIRST TOUCH. The committed generation the frame first touched — its first read OR first write —
|
|
17
|
+
* is held by reference, never cloned: the read view before the first write and the buffer's
|
|
18
|
+
* net-change diff base after it.
|
|
19
|
+
* 3. TWO TIERS. A read is served from the stage's own view (the buffer's working copy once the stage
|
|
20
|
+
* wrote, else the first-touch base); a key absent there is served from LIVE state (at the
|
|
21
|
+
* address, then the root). After the first write the buffer's diff base is first given a private
|
|
22
|
+
* copy at that path (`TransactionBuffer · detachBase`).
|
|
23
|
+
* 4. LAZY BUFFER. The buffer is built at the first write — on the first-touch base, at the address,
|
|
24
|
+
* under the encoding. A frame that never writes builds none, and its commit is the empty bundle.
|
|
25
|
+
* 5. READ KEYS. Under `writeProvenance: 'reads-prefix'` every noted read puts its user-level key
|
|
26
|
+
* (dotted for a nested path) on the frame's list, once, in order; each staged row carries a copy
|
|
27
|
+
* of the list as it stands when the row is staged.
|
|
28
|
+
* 6. COMMIT, RELEASE, DISCARD. `commit` takes the buffer's payload (none = the empty commit), then
|
|
29
|
+
* the names, and hands both to `recordCommit`. `release` drops the buffer and the base; a frame
|
|
30
|
+
* touched again re-anchors on the state as it stands then. The readKeys list survives a release,
|
|
31
|
+
* and goes with `discard` (a failed attempt never happened).
|
|
32
|
+
* 7. WRITE. `write(path, value, verb, scrub?)` is the one way to stage: `set`, `merge` or `delete`
|
|
33
|
+
* at an absolute path (the address already joined — `at`), with the bytes of a redaction
|
|
34
|
+
* verdict. `whole` → the log and the mirror carry `LOG_PLACEHOLDER` at the path; `fields` → at
|
|
35
|
+
* each field inside the value (a literal key and, when dotted, the nested path) — wherever it
|
|
36
|
+
* holds a value at commit (`scrub.ts`). No scrub → the value as it is. The frame never decides
|
|
37
|
+
* a scrub, and nothing outside it reaches its buffer.
|
|
38
|
+
*/
|
|
39
|
+
import { nativeGet } from './pathOps.js';
|
|
40
|
+
import { recordCommit } from './recordCommit.js';
|
|
41
|
+
import { TransactionBuffer } from './TransactionBuffer.js';
|
|
42
|
+
export class RecordFrame {
|
|
43
|
+
/** Live committed state — the heap every frame of the run shares. */
|
|
44
|
+
state;
|
|
45
|
+
/** The log the frame's commits land on, when the run keeps one. */
|
|
46
|
+
log;
|
|
47
|
+
/** The redacted mirror the frame's commits land on, when the run keeps one ({@link useMirror}). */
|
|
48
|
+
_mirror;
|
|
49
|
+
/** Where the frame reads and writes (law 1). */
|
|
50
|
+
_address;
|
|
51
|
+
/** The two dials (law 4, 5); absent → the record's defaults. */
|
|
52
|
+
encoding;
|
|
53
|
+
/**
|
|
54
|
+
* The committed generation at the frame's FIRST touch (law 2) — held by REFERENCE, never cloned.
|
|
55
|
+
* See {@link firstTouch} for the algorithm and the immutability invariant that makes a bare
|
|
56
|
+
* reference safe.
|
|
57
|
+
*/
|
|
58
|
+
base;
|
|
59
|
+
/** The stage's staged writes — built at its first write (law 4), released at commit. */
|
|
60
|
+
buffer;
|
|
61
|
+
/**
|
|
62
|
+
* Keys tracked-read so far, in order (law 5) — the source of each staged row's `readKeys`. Only
|
|
63
|
+
* allocated under `'reads-prefix'`; insertion-ordered (a Set) and monotonic, which is what makes
|
|
64
|
+
* "last write's prefix == union" hold in delta mode.
|
|
65
|
+
*/
|
|
66
|
+
readKeys;
|
|
67
|
+
constructor(state, log, address = []) {
|
|
68
|
+
this.state = state;
|
|
69
|
+
this.log = log;
|
|
70
|
+
this._address = address;
|
|
71
|
+
}
|
|
72
|
+
/** Where the frame reads and writes (law 1). */
|
|
73
|
+
get address() {
|
|
74
|
+
return this._address;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Read and write at `address` from now on — before the frame's first write (law 1): the buffer keeps
|
|
78
|
+
* the address it was built at. The engine's one caller, `StageContext · useAddressOf`, refuses a move
|
|
79
|
+
* once the frame has staged.
|
|
80
|
+
*/
|
|
81
|
+
useAddress(address) {
|
|
82
|
+
this._address = address;
|
|
83
|
+
}
|
|
84
|
+
/** The two dials the frame encodes under from its next buffer and its next read on (law 4, 5). */
|
|
85
|
+
useEncoding(encoding) {
|
|
86
|
+
this.encoding = encoding;
|
|
87
|
+
}
|
|
88
|
+
/** The redacted mirror, if the run keeps one. */
|
|
89
|
+
get mirror() {
|
|
90
|
+
return this._mirror;
|
|
91
|
+
}
|
|
92
|
+
/** Land every commit from now on on `mirror` too, with the scrubbed rows the log takes (`recordCommit`). */
|
|
93
|
+
useMirror(mirror) {
|
|
94
|
+
this._mirror = mirror;
|
|
95
|
+
}
|
|
96
|
+
/** The absolute path of `key` under `path`: the frame's address, then the path, then the key. */
|
|
97
|
+
at(path, key) {
|
|
98
|
+
// Every read and write builds one. The root's is the path and the key; under an address, an
|
|
99
|
+
// index loop — the double spread `[...address, ...path, key]` costs a third more per call
|
|
100
|
+
// (as `utils · pathUnder` found for the address and the path).
|
|
101
|
+
const address = this._address;
|
|
102
|
+
if (address.length === 0)
|
|
103
|
+
return [...path, key];
|
|
104
|
+
const out = new Array(address.length + path.length + 1);
|
|
105
|
+
for (let i = 0; i < address.length; i++)
|
|
106
|
+
out[i] = address[i];
|
|
107
|
+
for (let i = 0; i < path.length; i++)
|
|
108
|
+
out[address.length + i] = path[i];
|
|
109
|
+
out[out.length - 1] = key;
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* ── The first-touch base (#13) ───────────────────────────────────────────
|
|
114
|
+
*
|
|
115
|
+
* WHAT: returns the committed shared state as it was at this frame's FIRST touch (first read or
|
|
116
|
+
* first write), capturing the reference on first call. Serves two consumers: reads before the first
|
|
117
|
+
* write ({@link read}) and the transaction buffer's diff base ({@link getTransactionBuffer}).
|
|
118
|
+
*
|
|
119
|
+
* WHY A BARE REFERENCE IS SAFE — the invariant this rests on: committed state is
|
|
120
|
+
* immutable-after-swap. Every write to `SharedMemory` builds the NEXT generation and swaps it in
|
|
121
|
+
* (copy-on-write, 9.29.0: `applyPatch` via `nextGeneration`, and `setValue`/`updateValue` too) — it
|
|
122
|
+
* copies the root and the containers on each written path, shares the rest, and never edits a
|
|
123
|
+
* container of the generation a stage captured here. Holding the reference therefore gives this
|
|
124
|
+
* stage a stable snapshot at zero cost — no clone, which is the entire point of #13. The transaction
|
|
125
|
+
* buffer's net-change diff base rests on the same guarantee: it IS this view.
|
|
126
|
+
*
|
|
127
|
+
* WHY FIRST TOUCH, not first write: the pre-#13 eager engine cloned the state into the buffer at
|
|
128
|
+
* the stage's first ACCESS, anchoring both its snapshot reads and its commit baseline (the
|
|
129
|
+
* net-change diff base) there. #13's first cut anchored the lazy buffer at first WRITE — observably
|
|
130
|
+
* different when something else commits in the gap between this stage's first read and its first
|
|
131
|
+
* write. That gap is REACHABLE: fork siblings are namespace-isolated for run-scoped keys (each child
|
|
132
|
+
* writes under `runs/<childId>/`), but ROOT-level keys are shared — written via `setGlobal` from
|
|
133
|
+
* consumer scope code and, critically, by `SubflowInputMapper`'s output mapping
|
|
134
|
+
* (`parentContext.setGlobal`), which is exactly what runs when a subflow is a fork branch. A
|
|
135
|
+
* sibling's root-key commit landing in the gap would shift this stage's diff base, making its
|
|
136
|
+
* CommitBundle record a phantom change (or swallow a real one) relative to the eager engine.
|
|
137
|
+
* Anchoring the view at first touch restores the EXACT eager semantics — sequential AND parallel —
|
|
138
|
+
* at zero clone cost.
|
|
139
|
+
*
|
|
140
|
+
* Read visibility is two-tier, matching eager byte-for-byte: keys present in the view at first
|
|
141
|
+
* touch read repeatably from it; keys ABSENT from it fall back to LIVE state (the eager engine's
|
|
142
|
+
* exact fallback — a mid-flight sibling root-key write was always visible to reads, and stays
|
|
143
|
+
* visible; only the DIFF BASE is pinned).
|
|
144
|
+
*/
|
|
145
|
+
firstTouch() {
|
|
146
|
+
if (!this.base) {
|
|
147
|
+
this.base = this.state.getState();
|
|
148
|
+
}
|
|
149
|
+
return this.base;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The transaction buffer, created lazily on the frame's FIRST WRITE (#13).
|
|
153
|
+
*
|
|
154
|
+
* Reads NEVER construct it: read-your-writes only matters once a staged write exists, so before that
|
|
155
|
+
* {@link read} serves from the first-touch base and {@link commit} records an empty bundle — all with
|
|
156
|
+
* ZERO `structuredClone`s of the shared state.
|
|
157
|
+
*
|
|
158
|
+
* The buffer's base is the FIRST-TOUCH view, NOT the live state at write time: under parallel forks
|
|
159
|
+
* a sibling may have committed between this frame's first read and this write, and the net-change
|
|
160
|
+
* diff base must stay anchored at first touch to match the eager engine — see {@link firstTouch}.
|
|
161
|
+
*
|
|
162
|
+
* Private since C4: every write goes through {@link write}, so the scrub travels with the op.
|
|
163
|
+
*/
|
|
164
|
+
getTransactionBuffer() {
|
|
165
|
+
if (!this.buffer) {
|
|
166
|
+
// Per-write provenance (#P1): hand the buffer a live view of this frame's read prefix —
|
|
167
|
+
// evaluated AT EACH WRITE, so each staged op captures exactly the reads that preceded it.
|
|
168
|
+
const readKeysProvider = this.encoding?.writeProvenance === 'reads-prefix' ? () => [...(this.readKeys ?? [])] : undefined;
|
|
169
|
+
// The frame's address (9.30.0: the admitted record reads the containers there as where the
|
|
170
|
+
// stage writes, never as a value it read).
|
|
171
|
+
this.buffer = new TransactionBuffer(this.firstTouch(), this.encoding?.commitValues, readKeysProvider, this._address);
|
|
172
|
+
}
|
|
173
|
+
return this.buffer;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Stage one write (law 7): `verb` at `path` — an absolute path, the address already joined
|
|
177
|
+
* ({@link at}) — and the bytes of its redaction verdict. The first write builds the buffer (law 4).
|
|
178
|
+
*
|
|
179
|
+
* The scrub registers paths, never values: `whole` marks the op's own path, `fields` each field
|
|
180
|
+
* inside the value (a literal key and, when dotted, the nested path); `recordCommit` puts the
|
|
181
|
+
* log's placeholder wherever one of them holds a value at commit, for the mirror and the log only
|
|
182
|
+
* (`scrub.ts`). Live state takes the value as it is. A path marked by a write that the net-change
|
|
183
|
+
* filter drops goes with it.
|
|
184
|
+
*/
|
|
185
|
+
write(path, value, verb, scrub) {
|
|
186
|
+
const buffer = this.getTransactionBuffer();
|
|
187
|
+
const whole = scrub?.whole === true;
|
|
188
|
+
if (verb === 'merge')
|
|
189
|
+
buffer.merge(path, value, whole);
|
|
190
|
+
else if (verb === 'delete')
|
|
191
|
+
buffer.delete(path, whole);
|
|
192
|
+
else
|
|
193
|
+
buffer.set(path, value, whole);
|
|
194
|
+
if (scrub?.fields !== undefined)
|
|
195
|
+
buffer.markRedactedFields(path, scrub.fields);
|
|
196
|
+
}
|
|
197
|
+
/** Has the frame staged a write since its last release — does its buffer exist? */
|
|
198
|
+
get hasStaged() {
|
|
199
|
+
return this.buffer !== undefined;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The frame's read, mirroring the eager engine's read order byte-for-byte (law 3):
|
|
203
|
+
*
|
|
204
|
+
* 1. staged writes + first-touch snapshot — `buffer.get` over its workingCopy when the buffer
|
|
205
|
+
* exists, else `nativeGet` over the zero-clone first-touch base (the buffer's base IS that
|
|
206
|
+
* view, so the two tiers agree on content);
|
|
207
|
+
* 2. LIVE state via `SharedMemory · getValue` for keys absent from the snapshot — including its
|
|
208
|
+
* address→root fallback. The eager engine had this exact live fallback for snapshot-missing
|
|
209
|
+
* keys; byte-identity over purity. After the frame's first write such a value can be the very
|
|
210
|
+
* container the buffer's diff base holds at the path (the stage deleted or unset it, or
|
|
211
|
+
* replaced a container above it), so the base is detached there first
|
|
212
|
+
* (`TransactionBuffer · detachBase`): an in-place edit of the value (out of contract) written
|
|
213
|
+
* back is recorded, as on 9.28.0, whose base was a clone taken at the first write.
|
|
214
|
+
*
|
|
215
|
+
* Reads never construct the buffer (#13): a frame that never writes performs zero clones of the
|
|
216
|
+
* shared state.
|
|
217
|
+
*/
|
|
218
|
+
read(path, key) {
|
|
219
|
+
const at = this.at(path, key);
|
|
220
|
+
const fromSnapshot = this.buffer ? this.buffer.get(at) : nativeGet(this.firstTouch(), at);
|
|
221
|
+
if (typeof fromSnapshot !== 'undefined')
|
|
222
|
+
return fromSnapshot;
|
|
223
|
+
const live = this.state.getValue(this._address, path, key);
|
|
224
|
+
// Tier 2 after the first write: keep the diff base exact (see above).
|
|
225
|
+
if (this.buffer && live !== null && typeof live === 'object')
|
|
226
|
+
this.buffer.detachBase(at);
|
|
227
|
+
return live;
|
|
228
|
+
}
|
|
229
|
+
/** Put a tracked read's user-level key on the readKeys list (law 5) — a no-op unless the frame keeps one. */
|
|
230
|
+
noteRead(path, key) {
|
|
231
|
+
if (this.encoding?.writeProvenance === 'reads-prefix') {
|
|
232
|
+
(this.readKeys ??= new Set()).add(path.length > 0 ? [...path, key].join('.') : key);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Live committed state at `key`: at the frame's address, else at the root — never the stage's own
|
|
237
|
+
* view. Typed as `SharedMemory · getValue` types it, which `StageContext · getRoot` hands on.
|
|
238
|
+
*/
|
|
239
|
+
readLive(key) {
|
|
240
|
+
return this.state.getValue(this._address, [], key);
|
|
241
|
+
}
|
|
242
|
+
/** Live committed state at `key` at the root — typed as `SharedMemory · getValue` (`StageContext · getGlobal`). */
|
|
243
|
+
readGlobal(key) {
|
|
244
|
+
return this.state.getValue([], [], key);
|
|
245
|
+
}
|
|
246
|
+
/** Did a staged op touch `path` — on it, above it or below it? (A report's question; never the hot path.) */
|
|
247
|
+
wasStaged(path) {
|
|
248
|
+
return this.buffer !== undefined && this.buffer.wasStaged(path);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* The value at `path` in what the stage has seen — its working copy once it wrote, else its
|
|
252
|
+
* first-touch base — WITHOUT taking a private copy: for a report that only compares.
|
|
253
|
+
*/
|
|
254
|
+
peek(path) {
|
|
255
|
+
return this.buffer ? this.buffer.peek(path) : nativeGet(this.firstTouch(), path);
|
|
256
|
+
}
|
|
257
|
+
/** The value at `path` in the first-touch base — committed state as the frame first touched it. */
|
|
258
|
+
baseAt(path) {
|
|
259
|
+
return nativeGet(this.firstTouch(), path);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Record what the frame staged (law 6): the buffer's payload first — none, the empty commit, when
|
|
263
|
+
* it staged nothing (#13: zero clones) — then the names (`stampOf()`), read once the payload is
|
|
264
|
+
* built, and both to `recordCommit`. User code can run while the payload is built (a getter on a
|
|
265
|
+
* staged value, which the net-change compare and the clone invoke); what it adds to the names —
|
|
266
|
+
* an untracked read, say — is on the bundle.
|
|
267
|
+
*/
|
|
268
|
+
commit(stampOf) {
|
|
269
|
+
const payload = this.buffer?.commit();
|
|
270
|
+
recordCommit(payload, stampOf(), { state: this.state, mirror: this._mirror, log: this.log });
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* End the frame's hold on state (#13b, law 6): drop the buffer (its working copy and whatever private
|
|
274
|
+
* copies its reads took) and the first-touch base (a reference that pins one committed-state
|
|
275
|
+
* GENERATION). Both re-create lazily: a later read re-anchors on the CURRENT committed state, and a
|
|
276
|
+
* later write builds a fresh buffer on it — so a second commit diffs against the state after the
|
|
277
|
+
* first. Without the release, the execution tree — which retains every frame for the lifetime of
|
|
278
|
+
* the run — retains one state generation per executed stage (measured O(N²) before copy-on-write;
|
|
279
|
+
* a 500-iteration agent OOMed a default Node heap, backlog #18).
|
|
280
|
+
*/
|
|
281
|
+
release() {
|
|
282
|
+
this.buffer = undefined;
|
|
283
|
+
this.base = undefined;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Throw away everything the frame staged and read (law 6): the buffer, the first-touch base (the next
|
|
287
|
+
* attempt re-anchors on committed state as it stands NOW) and the readKeys list — a discarded
|
|
288
|
+
* attempt's reads must never reach the next attempt's `TraceEntry.readKeys`, or a backward slice
|
|
289
|
+
* would follow an edge that no committed write ever had. Nothing is recorded.
|
|
290
|
+
*/
|
|
291
|
+
discard() {
|
|
292
|
+
this.release();
|
|
293
|
+
this.readKeys = undefined;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SharedMemory — The shared state container for all flowchart execution
|
|
3
|
+
*
|
|
4
|
+
* Like a runtime heap with namespace isolation:
|
|
5
|
+
* - A value is written at an ADDRESS — a path prefix the caller computed
|
|
6
|
+
* (C2: the engine's frame decides it, `runAddress.ts · runAddress`) — and read
|
|
7
|
+
* there first, then at the root
|
|
8
|
+
* - Default values seed the root, and the container at an address when a
|
|
9
|
+
* write creates it
|
|
10
|
+
* - Accepts commit bundles from TransactionBuffer
|
|
11
|
+
*
|
|
12
|
+
* COPY-ON-WRITE (9.29.0 — docs/design/2026-10-copy-on-write-commit.md):
|
|
13
|
+
* `context` is a GENERATION, and a generation is never edited. Every write —
|
|
14
|
+
* `applyPatch` (a stage commit), `setValue`, `updateValue` — builds the next
|
|
15
|
+
* generation by copying the root and the containers on each written path,
|
|
16
|
+
* shares every other subtree with the generation before it, and swaps. So a
|
|
17
|
+
* generation a stage captured (its first-touch view, its transaction
|
|
18
|
+
* buffer's diff base) stays exactly what it was, and a write costs O(what it
|
|
19
|
+
* wrote), not O(state).
|
|
20
|
+
*/
|
|
21
|
+
import type { MemoryPatch, TraceEntry } from './types.js';
|
|
22
|
+
export declare class SharedMemory {
|
|
23
|
+
private context;
|
|
24
|
+
private _defaultValues?;
|
|
25
|
+
constructor(defaultValues?: unknown, initialContext?: unknown);
|
|
26
|
+
/** Gets a clone of the default values. */
|
|
27
|
+
getDefaultValues(): {} | undefined;
|
|
28
|
+
/** Updates a value at `address` + `path` using merge semantics, as a new generation (path copy + swap). */
|
|
29
|
+
updateValue(address: readonly string[], path: string[], key: string, value: unknown): void;
|
|
30
|
+
/** Sets a value at `address` + `path` using overwrite semantics, as a new generation (path copy + swap). */
|
|
31
|
+
setValue(address: readonly string[], path: string[], key: string, value: unknown): void;
|
|
32
|
+
/**
|
|
33
|
+
* A copy of the current generation's root whose containers on the way to
|
|
34
|
+
* `key` are owned — the in-place helpers above then edit only copies.
|
|
35
|
+
*/
|
|
36
|
+
private ownedPathTo;
|
|
37
|
+
/**
|
|
38
|
+
* Reads a value from the store.
|
|
39
|
+
* Looks up at `address` first, falls back to the root.
|
|
40
|
+
*/
|
|
41
|
+
getValue(address?: readonly string[], path?: string[], key?: string): any;
|
|
42
|
+
/** Gets the entire state as a JSON object — the current generation, by reference. Never mutate it. */
|
|
43
|
+
getState(): Record<string, unknown>;
|
|
44
|
+
/** Applies a commit bundle from TransactionBuffer: builds the next generation and swaps it in. */
|
|
45
|
+
applyPatch(overwrite: MemoryPatch, updates: MemoryPatch, trace: TraceEntry[]): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SharedMemory — The shared state container for all flowchart execution
|
|
3
|
+
*
|
|
4
|
+
* Like a runtime heap with namespace isolation:
|
|
5
|
+
* - A value is written at an ADDRESS — a path prefix the caller computed
|
|
6
|
+
* (C2: the engine's frame decides it, `runAddress.ts · runAddress`) — and read
|
|
7
|
+
* there first, then at the root
|
|
8
|
+
* - Default values seed the root, and the container at an address when a
|
|
9
|
+
* write creates it
|
|
10
|
+
* - Accepts commit bundles from TransactionBuffer
|
|
11
|
+
*
|
|
12
|
+
* COPY-ON-WRITE (9.29.0 — docs/design/2026-10-copy-on-write-commit.md):
|
|
13
|
+
* `context` is a GENERATION, and a generation is never edited. Every write —
|
|
14
|
+
* `applyPatch` (a stage commit), `setValue`, `updateValue` — builds the next
|
|
15
|
+
* generation by copying the root and the containers on each written path,
|
|
16
|
+
* shares every other subtree with the generation before it, and swaps. So a
|
|
17
|
+
* generation a stage captured (its first-touch view, its transaction
|
|
18
|
+
* buffer's diff base) stays exactly what it was, and a write costs O(what it
|
|
19
|
+
* wrote), not O(state).
|
|
20
|
+
*/
|
|
21
|
+
import { mergeContextWins, ownedRootOf, ownSpine } from './pathOps.js';
|
|
22
|
+
import { getNestedValue, getRunAndGlobalPaths, isDeniedWritePath, nextGeneration, setNestedValue, updateNestedValue, } from './utils.js';
|
|
23
|
+
export class SharedMemory {
|
|
24
|
+
context = {};
|
|
25
|
+
_defaultValues;
|
|
26
|
+
constructor(defaultValues, initialContext) {
|
|
27
|
+
this._defaultValues = defaultValues;
|
|
28
|
+
const seed = mergeContextWins(initialContext || {}, defaultValues || {});
|
|
29
|
+
// Detached ONCE, here: `mergeContextWins` copies only the top level, so
|
|
30
|
+
// the seed's nested values are the caller's own objects. Before 9.29.0
|
|
31
|
+
// the first commit's whole-state clone detached them (and until then a
|
|
32
|
+
// caller mutating its `initialContext` changed live state with no row);
|
|
33
|
+
// nothing re-clones the state any more, so the seed is detached at
|
|
34
|
+
// construction — one clone per runtime, not one per commit.
|
|
35
|
+
this.context = Object.keys(seed).length > 0 ? structuredClone(seed) : seed;
|
|
36
|
+
}
|
|
37
|
+
/** Gets a clone of the default values. */
|
|
38
|
+
getDefaultValues() {
|
|
39
|
+
return this._defaultValues ? structuredClone(this._defaultValues) : undefined;
|
|
40
|
+
}
|
|
41
|
+
/** Updates a value at `address` + `path` using merge semantics, as a new generation (path copy + swap). */
|
|
42
|
+
updateValue(address, path, key, value) {
|
|
43
|
+
const next = this.ownedPathTo(address, path, key);
|
|
44
|
+
if (next === undefined)
|
|
45
|
+
return;
|
|
46
|
+
updateNestedValue(next, address, path, key, value, this.getDefaultValues());
|
|
47
|
+
this.context = next;
|
|
48
|
+
}
|
|
49
|
+
/** Sets a value at `address` + `path` using overwrite semantics, as a new generation (path copy + swap). */
|
|
50
|
+
setValue(address, path, key, value) {
|
|
51
|
+
const next = this.ownedPathTo(address, path, key);
|
|
52
|
+
if (next === undefined)
|
|
53
|
+
return;
|
|
54
|
+
setNestedValue(next, address, path, key, value, this.getDefaultValues());
|
|
55
|
+
this.context = next;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A copy of the current generation's root whose containers on the way to
|
|
59
|
+
* `key` are owned — the in-place helpers above then edit only copies.
|
|
60
|
+
*/
|
|
61
|
+
ownedPathTo(address, path, key) {
|
|
62
|
+
if (isDeniedWritePath(address, path, key))
|
|
63
|
+
return undefined;
|
|
64
|
+
const owned = new WeakSet();
|
|
65
|
+
const root = ownedRootOf(this.context, owned);
|
|
66
|
+
const { runPath, globalPath } = getRunAndGlobalPaths(address, path);
|
|
67
|
+
ownSpine(root, [...(runPath || globalPath), key], owned);
|
|
68
|
+
return root;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Reads a value from the store.
|
|
72
|
+
* Looks up at `address` first, falls back to the root.
|
|
73
|
+
*/
|
|
74
|
+
getValue(address, path, key) {
|
|
75
|
+
const { globalPath, runPath } = getRunAndGlobalPaths(address, path);
|
|
76
|
+
const value = runPath ? getNestedValue(this.context, runPath, key) : undefined;
|
|
77
|
+
return typeof value !== 'undefined' ? value : getNestedValue(this.context, globalPath, key);
|
|
78
|
+
}
|
|
79
|
+
/** Gets the entire state as a JSON object — the current generation, by reference. Never mutate it. */
|
|
80
|
+
getState() {
|
|
81
|
+
return this.context;
|
|
82
|
+
}
|
|
83
|
+
/** Applies a commit bundle from TransactionBuffer: builds the next generation and swaps it in. */
|
|
84
|
+
applyPatch(overwrite, updates, trace) {
|
|
85
|
+
this.context = nextGeneration(this.context, updates, overwrite, trace);
|
|
86
|
+
}
|
|
87
|
+
}
|