foottrace 0.0.0-stage → 1.0.0
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 +286 -0
- package/dist/esm/lib/memory/backtrack.js +456 -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 +78 -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 +462 -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 +82 -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 +286 -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,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed utilities for querying the commit log.
|
|
3
|
+
*
|
|
4
|
+
* The commitLog is an ordered array of CommitBundle — one per stage commit.
|
|
5
|
+
* These helpers provide type-safe queries without (b: any) casts.
|
|
6
|
+
*/
|
|
7
|
+
import { queryWork, relation, rootOf, writesOnlyInside } from './keyPaths.js';
|
|
8
|
+
import { leavesStringAbove, logModel, memoisedModel } from './logModel.js';
|
|
9
|
+
import { nativeGet } from './pathOps.js';
|
|
10
|
+
import { DELIM } from './paths.js';
|
|
11
|
+
import { foldKey, isTotal, isVerb, UnknownVerbError } from './verbs.js';
|
|
12
|
+
// Every key query here follows the writer rule and the value rule of `keyPaths.ts` (F3, 9.33.0):
|
|
13
|
+
// a key is written by a row ON it, INSIDE it, or AROUND it when that changed it — never by an
|
|
14
|
+
// exact path match alone, which called every subflow seed and merge-back "never written". The
|
|
15
|
+
// read model that answers them at a cost proportional to the answer is `logModel.ts`; a frozen
|
|
16
|
+
// log's model is memoised, and a single question on a log that is not frozen first tries a
|
|
17
|
+
// backward scan that stops at the answer.
|
|
18
|
+
/**
|
|
19
|
+
* Find the first commit by `stageId`, optionally the first one that WROTE `key` — under the writer
|
|
20
|
+
* rule: a row on the key, inside it, or around it that changed it (see {@link writersOf}).
|
|
21
|
+
*/
|
|
22
|
+
export function findCommit(commitLog, stageId, key) {
|
|
23
|
+
if (!key)
|
|
24
|
+
return commitLog.find((b) => b.stageId === stageId);
|
|
25
|
+
const writers = new Set(logModel(commitLog).writersOf(key));
|
|
26
|
+
return commitLog.find((b, i) => b.stageId === stageId && writers.has(i));
|
|
27
|
+
}
|
|
28
|
+
/** Find all commits by stageId. */
|
|
29
|
+
export function findCommits(commitLog, stageId) {
|
|
30
|
+
return commitLog.filter((b) => b.stageId === stageId);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Find the last commit that WROTE `key` before `beforeIdx` (exclusive; default: the whole log) —
|
|
34
|
+
* the anchor of every backward question ("who made it this?").
|
|
35
|
+
*
|
|
36
|
+
* The writer rule (`keyPaths.ts`): a commit writes `key` when one of its rows is ON the key,
|
|
37
|
+
* INSIDE it (a subflow's input seed writes `cfg␟a`; an outputMapper merge-back writes `cfg␟b`;
|
|
38
|
+
* either is a write of `cfg`), or AROUND it with the value at the key different across the commit
|
|
39
|
+
* (a `set` of `cfg` writes `cfg␟a`; a `merge` of `cfg` that never reaches `a` does not). A write
|
|
40
|
+
* found through a row INSIDE the key wrote only PART of its value — the slice readers say so with
|
|
41
|
+
* the `'nested-rows'` note.
|
|
42
|
+
*
|
|
43
|
+
* @param key A DELIM-joined path (`normalisePath`); a top-level key is a one-segment path.
|
|
44
|
+
*/
|
|
45
|
+
export function findLastWriter(commitLog, key, beforeIdx) {
|
|
46
|
+
const end = Math.min(beforeIdx ?? commitLog.length, commitLog.length);
|
|
47
|
+
const model = memoisedModel(commitLog);
|
|
48
|
+
if (model !== undefined) {
|
|
49
|
+
const at = model.lastWriterBefore(key, end);
|
|
50
|
+
return at >= 0 ? commitLog[at] : undefined;
|
|
51
|
+
}
|
|
52
|
+
// Not frozen: scan back and stop at the first write on or inside the key. A row AROUND the key
|
|
53
|
+
// (or a nested `delete`, which cannot replace a string container) needs a verdict — the model's.
|
|
54
|
+
const nested = rootOf(key) !== key;
|
|
55
|
+
for (let i = end - 1; i >= 0; i--) {
|
|
56
|
+
let write = false;
|
|
57
|
+
let needsVerdict = false;
|
|
58
|
+
queryWork.units += commitLog[i].trace.length;
|
|
59
|
+
for (const t of commitLog[i].trace) {
|
|
60
|
+
const r = relation(t.path, key);
|
|
61
|
+
if (r === 'around')
|
|
62
|
+
needsVerdict = true;
|
|
63
|
+
else if (r !== undefined) {
|
|
64
|
+
if (t.verb !== 'delete' || !nested)
|
|
65
|
+
write = true;
|
|
66
|
+
else
|
|
67
|
+
needsVerdict = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (write && (!needsVerdict || !leavesStringAbove(commitLog[i], key)))
|
|
71
|
+
return commitLog[i];
|
|
72
|
+
if (needsVerdict) {
|
|
73
|
+
const at = logModel(commitLog).lastWriterBefore(key, end);
|
|
74
|
+
return at >= 0 ? commitLog[at] : undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The ARRAY positions (ascending) of every commit in `commitLog[0..end]` that WROTE `key` — the
|
|
81
|
+
* writer rule of `keyPaths.ts`, the one definition every key query shares (`logModel.ts` applies it).
|
|
82
|
+
*/
|
|
83
|
+
export function writersOf(commitLog, key, options = {}) {
|
|
84
|
+
const end = options.end ?? commitLog.length - 1;
|
|
85
|
+
return logModel(commitLog)
|
|
86
|
+
.writersOf(key)
|
|
87
|
+
.filter((i) => i <= end);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Every row under `key`'s TOP-LEVEL key in `commitLog[0..end]`, in commit order — the rows the value
|
|
91
|
+
* rule folds ({@link foldKey}), each with its {@link Touch.relation} to `key` (absent for a sibling
|
|
92
|
+
* under the same top-level key: applied, not observed) — found through the log's index. A row whose
|
|
93
|
+
* verb is not one of the four is refused with {@link UnknownVerbError} naming the row — whether or
|
|
94
|
+
* not the fold would have reached it: the answer must not depend on where an optimisation starts.
|
|
95
|
+
*/
|
|
96
|
+
export function rowsUnderRoot(commitLog, key, end) {
|
|
97
|
+
return logModel(commitLog).rowsUnderRoot(key, end);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Reconstruct the FULL value of `key` as of commit array index `idx`
|
|
101
|
+
* (inclusive) — the migration helper for the "read `bundle.overwrite[key]`
|
|
102
|
+
* as the full value written" pattern (#13c-B).
|
|
103
|
+
*
|
|
104
|
+
* The value rule (`keyPaths.ts`, 9.33.0): every row under the key's TOP-LEVEL
|
|
105
|
+
* key in `commitLog[0..idx]` is folded, anchored at that key's last `set` /
|
|
106
|
+
* `delete`, with the SAME step and clone discipline the replay uses
|
|
107
|
+
* (`verbs.ts` · `foldKey`), and the result is read at `key`. So the answer is
|
|
108
|
+
* the value `stateAt` gives at that path when it folds the log alone — rows
|
|
109
|
+
* INSIDE the key (a subflow seed, an outputMapper merge-back) and AROUND it (a
|
|
110
|
+
* `set` of the container) count; before 9.33.0 only rows on the exact path did,
|
|
111
|
+
* and a seeded `cfg` read as `undefined`. Under `commitValues: 'delta'`, an
|
|
112
|
+
* `append` row's `overwrite[key]` holds only the TAIL; the fold puts the array
|
|
113
|
+
* back together. Two `merge` rows of one bundle see one copy of the bundle's
|
|
114
|
+
* delta, as the replay's do.
|
|
115
|
+
*
|
|
116
|
+
* @param key A DELIM-joined path (`normalisePath`); a top-level key is a
|
|
117
|
+
* one-segment path.
|
|
118
|
+
* @param idx CommitBundle ARRAY index (the `bundle.idx` position),
|
|
119
|
+
* inclusive. NOT the executionIndex from a runtimeStageId.
|
|
120
|
+
* @returns The reconstructed value (a detached clone), or `undefined` when no
|
|
121
|
+
* row in `commitLog[0..idx]` is on, inside or around the key, or the fold
|
|
122
|
+
* leaves it absent (its last write was a delete). Caveat: a key that held a
|
|
123
|
+
* value in the run's INITIAL state folds from absent here — the log alone
|
|
124
|
+
* cannot see the pre-run base, so merges and nested writes onto a seeded key
|
|
125
|
+
* give only what they added. Since 9.17.0 the base TRAVELS with the log:
|
|
126
|
+
* `stateAt(snapshot, idx)` (foottrace) folds from
|
|
127
|
+
* `RuntimeSnapshot.initialState`, which this function never receives.
|
|
128
|
+
* @throws {@link UnknownVerbError} when a row under the key's top-level key
|
|
129
|
+
* carries a verb other than `set | merge | append | delete` — a foreign or
|
|
130
|
+
* corrupted log is refused, not folded as a `merge`. Engine-written logs
|
|
131
|
+
* never carry one.
|
|
132
|
+
* @see commitValueAtWithBasis — the same answer, with the codes that say WHY it is
|
|
133
|
+
* `undefined` or partial (and the `initialState` base this function never receives).
|
|
134
|
+
*/
|
|
135
|
+
export function commitValueAt(commitLog, idx, key) {
|
|
136
|
+
const end = Math.min(idx, commitLog.length - 1);
|
|
137
|
+
const model = memoisedModel(commitLog);
|
|
138
|
+
if (model !== undefined)
|
|
139
|
+
return model.valueAt(key, end);
|
|
140
|
+
return valueByScan(commitLog, end, key);
|
|
141
|
+
}
|
|
142
|
+
const VALUE_BASIS_ORDER = [
|
|
143
|
+
'never-written',
|
|
144
|
+
'deleted',
|
|
145
|
+
'nested-rows',
|
|
146
|
+
'from-initial-state',
|
|
147
|
+
'redacted',
|
|
148
|
+
];
|
|
149
|
+
/**
|
|
150
|
+
* {@link commitValueAt}, with the codes that say what the answer rests on. Without `options.initialState`
|
|
151
|
+
* the value IS `commitValueAt(commitLog, idx, key)` (pinned by a property); with it, a key that has no
|
|
152
|
+
* `set`/`delete` of itself or of a container around it in range folds from that base — the value
|
|
153
|
+
* `stateAt(snapshot, idx)` gives at the key. Refuses an unknown verb exactly as `commitValueAt` does.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const { value, basis } = commitValueAtWithBasis(snap.commitLog, i, 'cfg', { initialState: snap.initialState });
|
|
158
|
+
* for (const code of basis) console.log(code, HONESTY_CODES[code]);
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
export function commitValueAtWithBasis(commitLog, idx, key, options = {}) {
|
|
162
|
+
const end = Math.min(idx, commitLog.length - 1);
|
|
163
|
+
const model = logModel(commitLog);
|
|
164
|
+
const rows = model.rowsUnderRoot(key, end);
|
|
165
|
+
// The last `set`/`delete` ON the key or AROUND it decides the value alone; rows inside after it refine it.
|
|
166
|
+
let anchor = -1;
|
|
167
|
+
let inside = false;
|
|
168
|
+
let lastRelated;
|
|
169
|
+
for (let i = 0; i < rows.length; i++) {
|
|
170
|
+
const r = rows[i].relation;
|
|
171
|
+
if (r === undefined)
|
|
172
|
+
continue;
|
|
173
|
+
lastRelated = rows[i];
|
|
174
|
+
if (r === 'inside')
|
|
175
|
+
inside = true;
|
|
176
|
+
else if (isTotal(rows[i].verb)) {
|
|
177
|
+
anchor = i;
|
|
178
|
+
inside = false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const root = rootOf(key);
|
|
182
|
+
const base = options.initialState;
|
|
183
|
+
const baseHoldsRoot = base !== undefined && Object.prototype.hasOwnProperty.call(base, root);
|
|
184
|
+
const value = anchor === -1 && baseHoldsRoot
|
|
185
|
+
? foldKey(rows, key.split(DELIM), { anchored: true, start: structuredClone(base[root]) })
|
|
186
|
+
: model.valueAt(key, end);
|
|
187
|
+
const codes = new Set();
|
|
188
|
+
// The writer rule decides 'never-written' (as it does for `findLastWriter` and the slice layer), with two
|
|
189
|
+
// corrections only the base and the redaction list can make:
|
|
190
|
+
// - the run REMOVED a value the passed base held (a `delete` — full mode spells it a `set` — or a `set` of
|
|
191
|
+
// a container without the key): the log-only verdict sees absent → absent, the truth is 'deleted';
|
|
192
|
+
// - a redaction at or around the key HID the write (a placeholder string replaced the container): the
|
|
193
|
+
// log cannot say whether the key was written, so the answer says 'redacted' and nothing else about it.
|
|
194
|
+
const segs = key.split(DELIM);
|
|
195
|
+
const red = redactionsInRange(commitLog, key, anchor === -1 ? 0 : rows[anchor].commitIdx, end);
|
|
196
|
+
const deletedByRow = lastRelated !== undefined && lastRelated.relation !== 'inside' && lastRelated.verb === 'delete';
|
|
197
|
+
const baseHeldKey = base !== undefined && nativeGet(base, segs) !== undefined;
|
|
198
|
+
const removedFromBase = baseHeldKey && value === undefined && !red.any && (anchor !== -1 || deletedByRow);
|
|
199
|
+
const written = (end >= 0 && model.lastWriterBefore(key, end + 1) !== -1) || removedFromBase;
|
|
200
|
+
if (!written) {
|
|
201
|
+
if (!red.hidden)
|
|
202
|
+
codes.add('never-written');
|
|
203
|
+
}
|
|
204
|
+
else if (value === undefined && (deletedByRow || removedFromBase || !red.any))
|
|
205
|
+
codes.add('deleted');
|
|
206
|
+
if (anchor === -1 && inside)
|
|
207
|
+
codes.add('nested-rows');
|
|
208
|
+
if (anchor === -1 && (base === undefined || baseHoldsRoot))
|
|
209
|
+
codes.add('from-initial-state');
|
|
210
|
+
if (red.any)
|
|
211
|
+
codes.add('redacted');
|
|
212
|
+
return { value, basis: VALUE_BASIS_ORDER.filter((c) => codes.has(c)) };
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The redacted paths of the commits in `[from, end]` against `key`: `any` — one at, inside or around it;
|
|
216
|
+
* `hidden` — one AT or AROUND it (a placeholder replaced the key or its container, so the write is hidden).
|
|
217
|
+
*/
|
|
218
|
+
function redactionsInRange(commitLog, key, from, end) {
|
|
219
|
+
let any = false;
|
|
220
|
+
for (let c = from; c <= end; c++) {
|
|
221
|
+
for (const p of commitLog[c].redactedPaths ?? []) {
|
|
222
|
+
const r = relation(p, key);
|
|
223
|
+
if (r === undefined)
|
|
224
|
+
continue;
|
|
225
|
+
any = true;
|
|
226
|
+
if (r !== 'inside')
|
|
227
|
+
return { any, hidden: true };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { any, hidden: false };
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* {@link findLastWriter}, with the code that says why: `writer` is `findLastWriter(commitLog, key, beforeIdx)`;
|
|
234
|
+
* `basis` is `['never-written']` when there is none and `['nested-rows']` when that commit wrote the key
|
|
235
|
+
* only through rows inside it.
|
|
236
|
+
*/
|
|
237
|
+
export function findLastWriterWithBasis(commitLog, key, beforeIdx) {
|
|
238
|
+
const writer = findLastWriter(commitLog, key, beforeIdx);
|
|
239
|
+
if (writer === undefined) {
|
|
240
|
+
const end = Math.min(beforeIdx ?? commitLog.length, commitLog.length) - 1;
|
|
241
|
+
return { basis: [redactionsInRange(commitLog, key, 0, end).hidden ? 'redacted' : 'never-written'] };
|
|
242
|
+
}
|
|
243
|
+
return { writer, basis: writesOnlyInside(writer, key) ? ['nested-rows'] : [] };
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* {@link commitValueAt} on a log that is not frozen: refuse an unknown verb under the top-level key,
|
|
247
|
+
* then scan back from `end` to the last `set` ON the key, keeping the rows on and inside it — enough
|
|
248
|
+
* while nothing AROUND the key was written in between (and, for a nested key, no `delete` on it: a
|
|
249
|
+
* `delete` cannot replace a string container). Otherwise the model folds the top-level key.
|
|
250
|
+
*/
|
|
251
|
+
function valueByScan(commitLog, end, key) {
|
|
252
|
+
const root = rootOf(key);
|
|
253
|
+
for (let c = 0; c <= end; c++) {
|
|
254
|
+
const trace = commitLog[c].trace;
|
|
255
|
+
queryWork.units += trace.length;
|
|
256
|
+
for (let row = 0; row < trace.length; row++) {
|
|
257
|
+
const { path, verb } = trace[row];
|
|
258
|
+
if ((path === root || relation(path, root) === 'inside') && !isVerb(verb)) {
|
|
259
|
+
throw new UnknownVerbError(verb, { path, row, commit: c });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const nested = root !== key;
|
|
264
|
+
const rows = [];
|
|
265
|
+
let anchored = false;
|
|
266
|
+
for (let c = end; c >= 0 && !anchored; c--) {
|
|
267
|
+
const trace = commitLog[c].trace;
|
|
268
|
+
for (let row = trace.length - 1; row >= 0; row--) {
|
|
269
|
+
const { path, verb } = trace[row];
|
|
270
|
+
const r = relation(path, key);
|
|
271
|
+
if (r === undefined)
|
|
272
|
+
continue;
|
|
273
|
+
if (r === 'around' || (nested && r === 'exact' && verb === 'delete')) {
|
|
274
|
+
return logModel(commitLog).valueAt(key, end);
|
|
275
|
+
}
|
|
276
|
+
rows.push({ verb: verb, bundle: commitLog[c], commitIdx: c, path, relation: r });
|
|
277
|
+
if (r === 'exact' && (verb === 'set' || verb === 'delete')) {
|
|
278
|
+
anchored = true;
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (rows.length === 0)
|
|
284
|
+
return undefined;
|
|
285
|
+
return foldKey(rows.reverse(), key.split(DELIM));
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Position index over a commit log: `runtimeStageId` → the ARRAY INDEX of the
|
|
289
|
+
* FIRST bundle that stage committed. The map's insertion order is the order
|
|
290
|
+
* the stages first committed — execution order.
|
|
291
|
+
*
|
|
292
|
+
* "First" is the contract, and it is load-bearing. A stage normally commits
|
|
293
|
+
* exactly one bundle, but some executions commit more under one
|
|
294
|
+
* `runtimeStageId`: a subflow MOUNT (the merge-back, then the exit), a fork
|
|
295
|
+
* child (its own commit, then the fan-out's). Since 9.39.0 the WRITER names
|
|
296
|
+
* each continuation on the bundle (`CommitBundle.phase`: `'exit'` /
|
|
297
|
+
* `'repeat'`), and every continuation is written after the execution's own
|
|
298
|
+
* bundle — so "where does this stage start?" is the first bundle, and the
|
|
299
|
+
* grouping every reader's axis uses (`commitStops`) is this index. No reader
|
|
300
|
+
* tells a continuation from the log's shape.
|
|
301
|
+
*
|
|
302
|
+
* Build this ONCE when resolving many ids; use {@link commitIndexOf} for a
|
|
303
|
+
* single lookup.
|
|
304
|
+
*
|
|
305
|
+
* @param commitLog Ordered commit bundles — `getSnapshot().commitLog`.
|
|
306
|
+
* @returns A fresh Map, owned by the caller.
|
|
307
|
+
*/
|
|
308
|
+
export function buildCommitIndex(commitLog) {
|
|
309
|
+
const index = new Map();
|
|
310
|
+
for (let i = 0; i < commitLog.length; i++) {
|
|
311
|
+
const id = commitLog[i].runtimeStageId;
|
|
312
|
+
if (!index.has(id))
|
|
313
|
+
index.set(id, i);
|
|
314
|
+
}
|
|
315
|
+
return index;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Whether a log RECORDS phases — any bundle carries `phase` — i.e. it was
|
|
319
|
+
* written by 9.39.0 or later and holds at least one continuation. A log that
|
|
320
|
+
* does not either has no continuation at all, or predates the field (see
|
|
321
|
+
* {@link inferLegacyPhases}).
|
|
322
|
+
*/
|
|
323
|
+
export function recordsPhases(commitLog) {
|
|
324
|
+
for (const bundle of commitLog)
|
|
325
|
+
if (bundle.phase !== undefined)
|
|
326
|
+
return true;
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* THE LEGACY READER — the one place a continuation's phase is INFERRED, for a
|
|
331
|
+
* log written before 9.39.0 (which carries no `phase` anywhere). Returns the
|
|
332
|
+
* phase each bundle would have been stamped with, by the rule every reader
|
|
333
|
+
* applied through 9.38.0: a stage's bundle that IMMEDIATELY follows its first
|
|
334
|
+
* bundle is a mount's exit; any later bundle of the same `runtimeStageId` is
|
|
335
|
+
* a fork child's repeat; a first bundle is the stage's own (`undefined`).
|
|
336
|
+
*
|
|
337
|
+
* The rule is a guess, and the guess has a known miss — a single-child fork
|
|
338
|
+
* whose child is a leaf looks exactly like a mount — which is why a reader
|
|
339
|
+
* asks it only when the log records no phase AND it has no execution tree to
|
|
340
|
+
* ask instead (`commitStops`). A log that records phases is never inferred.
|
|
341
|
+
*/
|
|
342
|
+
export function inferLegacyPhases(commitLog) {
|
|
343
|
+
const phases = new Array(commitLog.length).fill(undefined);
|
|
344
|
+
const firstOf = new Map();
|
|
345
|
+
for (let i = 0; i < commitLog.length; i++) {
|
|
346
|
+
const id = commitLog[i].runtimeStageId;
|
|
347
|
+
if (!id)
|
|
348
|
+
continue;
|
|
349
|
+
const first = firstOf.get(id);
|
|
350
|
+
if (first === undefined)
|
|
351
|
+
firstOf.set(id, i);
|
|
352
|
+
else
|
|
353
|
+
phases[i] = i === first + 1 ? 'exit' : 'repeat';
|
|
354
|
+
}
|
|
355
|
+
return phases;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* The ARRAY INDEX of the first commit a given `runtimeStageId` produced, or
|
|
359
|
+
* `-1` when that stage is not in this log (`indexOf` semantics — the name's
|
|
360
|
+
* promise).
|
|
361
|
+
*
|
|
362
|
+
* This is the address translation every reader needs and nobody exported: a
|
|
363
|
+
* `runtimeStageId` is the id an event carries, a commit index is what
|
|
364
|
+
* `commitValueAt` / `stateAt` / `CommitRangeIndex` speak. O(n) — for many
|
|
365
|
+
* lookups build the map once with {@link buildCommitIndex}.
|
|
366
|
+
*
|
|
367
|
+
* A stage that ran inside a SUBFLOW is not in the run-level log (subflows
|
|
368
|
+
* commit to their own isolated log); look it up in that subflow's own
|
|
369
|
+
* `history` instead — see `getSubtreeSnapshot`.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```typescript
|
|
373
|
+
* const idx = commitIndexOf(snapshot.commitLog, 'score-risk#7');
|
|
374
|
+
* const before = idx > 0 ? commitValueAt(snapshot.commitLog, idx - 1, 'risk') : undefined;
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
export function commitIndexOf(commitLog, runtimeStageId) {
|
|
378
|
+
for (let i = 0; i < commitLog.length; i++) {
|
|
379
|
+
if (commitLog[i].runtimeStageId === runtimeStageId)
|
|
380
|
+
return i;
|
|
381
|
+
}
|
|
382
|
+
return -1;
|
|
383
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* deltaEncoding.ts — the delta encoding's pure helpers (`commitValues:
|
|
3
|
+
* 'delta'`, #13c-B; docs/design/13c-b-delta-commit-verb.md), out of
|
|
4
|
+
* `TransactionBuffer` since 9.30.0: the overlap families of rule 3 and the
|
|
5
|
+
* order rows are emitted in (which now also places the admitted record's
|
|
6
|
+
* re-encoded families), the append detection, and the row that carries a
|
|
7
|
+
* value. The verb CHOICE stays in `TransactionBuffer · deltaRows`; nothing
|
|
8
|
+
* here replays the verb law.
|
|
9
|
+
*/
|
|
10
|
+
import type { Rows } from './admission.js';
|
|
11
|
+
/** Op-level verbs staged into `opTrace`. `'delete'` is staged distinctly so
|
|
12
|
+
* delta-mode commits (#13c-B) can emit a real `delete` trace entry; under
|
|
13
|
+
* the default `'full'` mode it commits as `'set'` (of `undefined`) —
|
|
14
|
+
* byte-identical to the historical flattening. */
|
|
15
|
+
export type OpVerb = 'set' | 'merge' | 'delete';
|
|
16
|
+
/** A path that passed the net-change filter, with everything the delta
|
|
17
|
+
* encoder needs to decide its verb — resolved once, then reused by the
|
|
18
|
+
* overlap-family grouping (`toDeltaPayload` rule 3). */
|
|
19
|
+
export type Survivor = {
|
|
20
|
+
path: string;
|
|
21
|
+
segments: string[];
|
|
22
|
+
verbs: OpVerb[];
|
|
23
|
+
readKeys?: string[];
|
|
24
|
+
/** Index in `opTrace` of this path's last op (its last touch). */
|
|
25
|
+
last: number;
|
|
26
|
+
/** Value at this path when the stage began (the append/diff base). */
|
|
27
|
+
before: unknown;
|
|
28
|
+
/** Value at this path after every staged op (read-your-writes view). */
|
|
29
|
+
after: unknown;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A delta row carrying a VALUE: an `append` of the tail when `before` is a
|
|
33
|
+
* strict prefix of `value` ({@link isStrictArrayPrefix}), else a `set` of it.
|
|
34
|
+
* The value is cloned here — the one copy the record takes.
|
|
35
|
+
*
|
|
36
|
+
* `marked` — a redaction mark sits BELOW this path (`list.1.token`): its
|
|
37
|
+
* indices count from the start of the WHOLE array, and an `append` row holds
|
|
38
|
+
* only the tail, where `scrubPatch` would find no element 1 and leave the
|
|
39
|
+
* secret in the log and the redacted mirror. Such a path always takes the
|
|
40
|
+
* `set` of the whole value, where the mark lands (9.30.0; the same gap was
|
|
41
|
+
* open in 9.29.0 for a hard write of base + tail).
|
|
42
|
+
*/
|
|
43
|
+
export declare function pushValueRow(rows: Rows, path: string, segments: string[], before: unknown, value: unknown, prov: {
|
|
44
|
+
readKeys: string[];
|
|
45
|
+
} | undefined, marked?: boolean): void;
|
|
46
|
+
/**
|
|
47
|
+
* Group survivors into OVERLAP FAMILIES (`toDeltaPayload` rule 3): each path's
|
|
48
|
+
* family is keyed by its SHALLOWEST surviving ancestor — walking prefixes
|
|
49
|
+
* shallow first means the first hit is that root, and a depth-1 path (the
|
|
50
|
+
* common case) never enters the loop at all. Any two paths sharing a root
|
|
51
|
+
* are exactly the paths whose patch storage overlaps.
|
|
52
|
+
*/
|
|
53
|
+
export declare function groupIntoFamilies(survivors: Survivor[], survivingPaths: Set<string>): {
|
|
54
|
+
rootOf: Map<string, string>;
|
|
55
|
+
byRoot: Map<string, Survivor[]>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Emit survivors in last-touch order, with one exception: in an overlapping
|
|
59
|
+
* family the ROOT is emitted LAST — its whole-subtree set is what makes the
|
|
60
|
+
* replayed subtree exactly the family value (a descendant applied afterwards
|
|
61
|
+
* could only re-state a value already inside it, but would also leave behind
|
|
62
|
+
* the `key: undefined` shells 'full' mode never has). `before(touch)` runs
|
|
63
|
+
* ahead of each survivor with that survivor's last touch — where the
|
|
64
|
+
* re-encoded families of the admitted record take their places.
|
|
65
|
+
*/
|
|
66
|
+
export declare function emitInFamilyOrder(survivors: Survivor[], rootOf: Map<string, string>, byRoot: Map<string, Survivor[]>, emit: (s: Survivor) => void, before: (touch: number) => void): void;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* deltaEncoding.ts — the delta encoding's pure helpers (`commitValues:
|
|
3
|
+
* 'delta'`, #13c-B; docs/design/13c-b-delta-commit-verb.md), out of
|
|
4
|
+
* `TransactionBuffer` since 9.30.0: the overlap families of rule 3 and the
|
|
5
|
+
* order rows are emitted in (which now also places the admitted record's
|
|
6
|
+
* re-encoded families), the append detection, and the row that carries a
|
|
7
|
+
* value. The verb CHOICE stays in `TransactionBuffer · deltaRows`; nothing
|
|
8
|
+
* here replays the verb law.
|
|
9
|
+
*/
|
|
10
|
+
import { nativeSet as _set } from './pathOps.js';
|
|
11
|
+
import { deepEqual, DELIM } from './utils.js';
|
|
12
|
+
/**
|
|
13
|
+
* A delta row carrying a VALUE: an `append` of the tail when `before` is a
|
|
14
|
+
* strict prefix of `value` ({@link isStrictArrayPrefix}), else a `set` of it.
|
|
15
|
+
* The value is cloned here — the one copy the record takes.
|
|
16
|
+
*
|
|
17
|
+
* `marked` — a redaction mark sits BELOW this path (`list.1.token`): its
|
|
18
|
+
* indices count from the start of the WHOLE array, and an `append` row holds
|
|
19
|
+
* only the tail, where `scrubPatch` would find no element 1 and leave the
|
|
20
|
+
* secret in the log and the redacted mirror. Such a path always takes the
|
|
21
|
+
* `set` of the whole value, where the mark lands (9.30.0; the same gap was
|
|
22
|
+
* open in 9.29.0 for a hard write of base + tail).
|
|
23
|
+
*/
|
|
24
|
+
export function pushValueRow(rows, path, segments, before, value, prov, marked = false) {
|
|
25
|
+
if (!marked && isStrictArrayPrefix(before, value)) {
|
|
26
|
+
rows.trace.push({ path, verb: 'append', ...prov });
|
|
27
|
+
_set(rows.overwrite, segments, structuredClone(value.slice(before.length)));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
rows.trace.push({ path, verb: 'set', ...prov });
|
|
31
|
+
_set(rows.overwrite, segments, structuredClone(value));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Group survivors into OVERLAP FAMILIES (`toDeltaPayload` rule 3): each path's
|
|
36
|
+
* family is keyed by its SHALLOWEST surviving ancestor — walking prefixes
|
|
37
|
+
* shallow first means the first hit is that root, and a depth-1 path (the
|
|
38
|
+
* common case) never enters the loop at all. Any two paths sharing a root
|
|
39
|
+
* are exactly the paths whose patch storage overlaps.
|
|
40
|
+
*/
|
|
41
|
+
export function groupIntoFamilies(survivors, survivingPaths) {
|
|
42
|
+
const rootOf = new Map();
|
|
43
|
+
const byRoot = new Map();
|
|
44
|
+
for (const s of survivors) {
|
|
45
|
+
let root = s.path;
|
|
46
|
+
for (let i = 1; i < s.segments.length; i++) {
|
|
47
|
+
const ancestor = s.segments.slice(0, i).join(DELIM);
|
|
48
|
+
if (survivingPaths.has(ancestor)) {
|
|
49
|
+
root = ancestor;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
rootOf.set(s.path, root);
|
|
54
|
+
const family = byRoot.get(root);
|
|
55
|
+
if (family)
|
|
56
|
+
family.push(s);
|
|
57
|
+
else
|
|
58
|
+
byRoot.set(root, [s]);
|
|
59
|
+
}
|
|
60
|
+
return { rootOf, byRoot };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Emit survivors in last-touch order, with one exception: in an overlapping
|
|
64
|
+
* family the ROOT is emitted LAST — its whole-subtree set is what makes the
|
|
65
|
+
* replayed subtree exactly the family value (a descendant applied afterwards
|
|
66
|
+
* could only re-state a value already inside it, but would also leave behind
|
|
67
|
+
* the `key: undefined` shells 'full' mode never has). `before(touch)` runs
|
|
68
|
+
* ahead of each survivor with that survivor's last touch — where the
|
|
69
|
+
* re-encoded families of the admitted record take their places.
|
|
70
|
+
*/
|
|
71
|
+
export function emitInFamilyOrder(survivors, rootOf, byRoot, emit, before) {
|
|
72
|
+
for (const s of survivors) {
|
|
73
|
+
before(s.last);
|
|
74
|
+
const root = rootOf.get(s.path);
|
|
75
|
+
const family = byRoot.get(root);
|
|
76
|
+
if (family.length === 1) {
|
|
77
|
+
emit(s);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (s.path !== root)
|
|
81
|
+
emit(s);
|
|
82
|
+
if (family[family.length - 1] === s)
|
|
83
|
+
emit(family.find((m) => m.path === root));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Append-detection predicate (#13c-B §2.2): both values are arrays, the
|
|
88
|
+
* final is strictly longer, and the base is a structural prefix of the
|
|
89
|
+
* final. Element compares short-circuit on reference identity (`deepEqual`'s
|
|
90
|
+
* `===` fast path) before walking structure, and bail at the first mismatch
|
|
91
|
+
* — worst case one structural compare of the base array, strictly cheaper
|
|
92
|
+
* than the full-value `structuredClone` the fallback pays.
|
|
93
|
+
*
|
|
94
|
+
* `before === undefined` (first write) fails `Array.isArray` → `set`, which
|
|
95
|
+
* keeps the first write as the causal anchor for "who initialized this key".
|
|
96
|
+
*
|
|
97
|
+
* BOTH arrays must also be plain and dense ({@link isIndexOnly}): replay
|
|
98
|
+
* reconstructs an append as `[...current, ...tail]`, and that spread carries
|
|
99
|
+
* ONLY indexed elements — a named property parked on an array (a nested write
|
|
100
|
+
* like `set(['history','note'], …)`, which `nativeSet` happily hangs off the
|
|
101
|
+
* array object) or a hole would be silently dropped, where `'full'` mode
|
|
102
|
+
* stores the value whole and keeps it.
|
|
103
|
+
*/
|
|
104
|
+
function isStrictArrayPrefix(before, after) {
|
|
105
|
+
if (!Array.isArray(before) || !Array.isArray(after))
|
|
106
|
+
return false;
|
|
107
|
+
if (after.length <= before.length)
|
|
108
|
+
return false;
|
|
109
|
+
for (let i = 0; i < before.length; i++) {
|
|
110
|
+
if (!deepEqual(before[i], after[i]))
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return isIndexOnly(before) && isIndexOnly(after);
|
|
114
|
+
}
|
|
115
|
+
/** True when an array's own enumerable keys are exactly its indices — no
|
|
116
|
+
* extra named properties and no holes, i.e. the array survives a spread
|
|
117
|
+
* intact. One `Object.keys` pass, same order as the prefix compare above
|
|
118
|
+
* and paid only after it succeeds. */
|
|
119
|
+
function isIndexOnly(arr) {
|
|
120
|
+
return Object.keys(arr).length === arr.length;
|
|
121
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* equality.ts — structural equality of committed-state values: THE owner of
|
|
3
|
+
* "what counts as a change". What `TransactionBuffer`'s net-change filter, the
|
|
4
|
+
* admitted record's compare, the read model's writer rule and the
|
|
5
|
+
* borrowed-mutation guard all ask. Its one import is the classifier of value
|
|
6
|
+
* kinds (`capture/valueKinds.ts`, a leaf); re-exported from `utils.ts`, so no
|
|
7
|
+
* importer moved.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Structural deep equality for committed-state values.
|
|
11
|
+
*
|
|
12
|
+
* Used by {@link TransactionBuffer} to decide whether a stage actually CHANGED
|
|
13
|
+
* a path or merely re-wrote / reverted it to the value it already held (a
|
|
14
|
+
* "no-op write"), and by `borrowedMutation.firstDifferingPath` to find where a
|
|
15
|
+
* borrowed read moved. Committed state must survive `structuredClone`, so the
|
|
16
|
+
* values it can hold are the kinds the clone keeps, and two values are equal
|
|
17
|
+
* when a record holds the same thing for both. Every kind has ONE arm below
|
|
18
|
+
* (`equalPairs`' switch over `ValueKind` — a kind added to the union does not
|
|
19
|
+
* compile until it is compared). Before 9.22.0 a Date, Map or Set was compared
|
|
20
|
+
* as an object with no own keys, and until 9.44.2 so were a RegExp, an Error,
|
|
21
|
+
* a boxed primitive, an ArrayBuffer, a DataView and a Blob: any two of a kind
|
|
22
|
+
* were "equal", so `$setValue('re', /y/g)` over `/x/g` committed no row and
|
|
23
|
+
* live state kept the OLD value.
|
|
24
|
+
*
|
|
25
|
+
* Semantics:
|
|
26
|
+
* - reference / identical-primitive short-circuits first (cheap fast path)
|
|
27
|
+
* - `NaN` equals `NaN` (primitive compare falls back to `Object.is`)
|
|
28
|
+
* - two values of different KINDS are never equal (a `Date` is never `{}`,
|
|
29
|
+
* an array never `{ 0: … }`)
|
|
30
|
+
* - `Date`: same `getTime()` (two invalid dates are equal)
|
|
31
|
+
* - `RegExp`: same `source` and `flags` (the clone resets `lastIndex`)
|
|
32
|
+
* - `Error`: the same restored kind (one of the six built-in error names,
|
|
33
|
+
* else `Error`), own `message`, `stack` and own `cause` (deep) — what the
|
|
34
|
+
* clone keeps; any other own field is dropped by it, so it never counts
|
|
35
|
+
* (the admitted record's law)
|
|
36
|
+
* - a boxed primitive: the same wrapper type and the same primitive inside
|
|
37
|
+
* - an `ArrayBuffer`: length, resizability and bytes; a typed array or a
|
|
38
|
+
* `DataView`: its type and the bytes IT views — never the rest of its
|
|
39
|
+
* buffer (a Node `Buffer` views a slice of a shared pool, and the record
|
|
40
|
+
* keeps the view's bytes only: `capture/freeze.ts · freezeRecord`)
|
|
41
|
+
* - `Map`: same size AND, per key of `a`, `b` holds a deep-equal value —
|
|
42
|
+
* keys by `Map` identity (`SameValueZero`), values deep
|
|
43
|
+
* - `Set`: same size AND every member of `a` is deep-equal to SOME member
|
|
44
|
+
* of `b` (order-insensitive — a Set has no order to compare by)
|
|
45
|
+
* - an OPAQUE value (a Blob, a `DOMException` — content this library cannot
|
|
46
|
+
* read): by the caller's {@link OpaqueRule} — equal only to itself by
|
|
47
|
+
* default (replacing one is always a change), or, when both sides are
|
|
48
|
+
* copies of one record, equal to any other of its kind
|
|
49
|
+
* - arrays: equal length AND deep-equal element-wise (order-sensitive; an
|
|
50
|
+
* array's expando keys are out of contract and not compared)
|
|
51
|
+
* - objects: identical set of keys that HOLD a value AND deep-equal per key.
|
|
52
|
+
* An own key whose value is `undefined` counts as ABSENT: it is this
|
|
53
|
+
* library's other spelling of a deleted key (`'full'` mode flattens
|
|
54
|
+
* `delete` into `key: undefined`; `'delta'` mode's `delete` verb removes
|
|
55
|
+
* the key), and every consumer — JSON, the fold, `commitValueAt`, the
|
|
56
|
+
* mirror — reads the two spellings as one state. So must the net-change
|
|
57
|
+
* filter (9.19.1): `{}` and `{ k: undefined }` are not a change. Arrays
|
|
58
|
+
* are untouched — a slot holding `undefined` is still a slot.
|
|
59
|
+
*
|
|
60
|
+
* Cost & safety:
|
|
61
|
+
* - Allocates only transient `Object.keys` arrays, an error's property
|
|
62
|
+
* descriptors and a byte view per compared buffer — no clones (but one
|
|
63
|
+
* probe clone per class prototype, remembered: `capture/valueKinds.ts ·
|
|
64
|
+
* kindOf`). It is strictly cheaper than the `structuredClone` the commit
|
|
65
|
+
* already performs.
|
|
66
|
+
* - Primitive comparisons (the bulk of state) are O(1) via the `===` /
|
|
67
|
+
* `Object.is` fast paths; only nested objects/arrays incur a walk, bounded
|
|
68
|
+
* by the value's own size.
|
|
69
|
+
* - Terminates on CYCLIC values (9.18.1): a state value must survive
|
|
70
|
+
* `structuredClone`, and `structuredClone` preserves cycles — so a
|
|
71
|
+
* self-referencing value is a legal value, not an out-of-contract one. A
|
|
72
|
+
* pair of objects already under comparison is treated as equal (the
|
|
73
|
+
* structural answer, lodash `isEqual` semantics); acyclic inputs see no
|
|
74
|
+
* change. Dev mode still warns about cycles at write time
|
|
75
|
+
* (`ScopeFacade.setValue`) because they surprise narrative and JSON.
|
|
76
|
+
*/
|
|
77
|
+
export declare function deepEqual(a: any, b: any, opaque?: OpaqueRule): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* How {@link deepEqual} compares two OPAQUE values (a Blob, a `DOMException` —
|
|
80
|
+
* content this library cannot read):
|
|
81
|
+
*
|
|
82
|
+
* - `'identity'` (the default) — equal only to itself. The answer when one
|
|
83
|
+
* side is a value a stage handed in: the net-change filter
|
|
84
|
+
* (`TransactionBuffer`), the append check (`deltaEncoding`). A new object
|
|
85
|
+
* is a change, because nothing can say it is not.
|
|
86
|
+
* - `'copies'` — equal to any other of its kind. The answer when BOTH sides
|
|
87
|
+
* are copies of one recorded value — a fold's generations (`logModel`,
|
|
88
|
+
* `elementProvenance`), a retained read against live state
|
|
89
|
+
* (`borrowedMutation`), the record replayed against the read-back
|
|
90
|
+
* (`admission`): copies never share identity, so identity would call every
|
|
91
|
+
* opaque value changed.
|
|
92
|
+
*/
|
|
93
|
+
export type OpaqueRule = 'identity' | 'copies';
|