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