@crubal/engine 0.0.0-stage → 0.4.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 +202 -0
- package/README.md +97 -2
- package/dist/bin/crubal.d.ts +2 -0
- package/dist/bin/crubal.js +103 -0
- package/dist/bin/crubal.js.map +1 -0
- package/dist/src/access.d.ts +149 -0
- package/dist/src/access.js +584 -0
- package/dist/src/access.js.map +1 -0
- package/dist/src/acts.d.ts +531 -0
- package/dist/src/acts.js +1466 -0
- package/dist/src/acts.js.map +1 -0
- package/dist/src/address.d.ts +29 -0
- package/dist/src/address.js +59 -0
- package/dist/src/address.js.map +1 -0
- package/dist/src/arrival.d.ts +55 -0
- package/dist/src/arrival.js +127 -0
- package/dist/src/arrival.js.map +1 -0
- package/dist/src/browser.d.ts +10 -0
- package/dist/src/browser.js +13 -0
- package/dist/src/browser.js.map +1 -0
- package/dist/src/bundle.d.ts +74 -0
- package/dist/src/bundle.js +1108 -0
- package/dist/src/bundle.js.map +1 -0
- package/dist/src/canonical.d.ts +6 -0
- package/dist/src/canonical.js +47 -0
- package/dist/src/canonical.js.map +1 -0
- package/dist/src/carriers.d.ts +7 -0
- package/dist/src/carriers.js +33 -0
- package/dist/src/carriers.js.map +1 -0
- package/dist/src/conditions.d.ts +28 -0
- package/dist/src/conditions.js +227 -0
- package/dist/src/conditions.js.map +1 -0
- package/dist/src/corpus.d.ts +8 -0
- package/dist/src/corpus.js +1207 -0
- package/dist/src/corpus.js.map +1 -0
- package/dist/src/defs.d.ts +174 -0
- package/dist/src/defs.js +690 -0
- package/dist/src/defs.js.map +1 -0
- package/dist/src/dictionary.d.ts +77 -0
- package/dist/src/dictionary.js +629 -0
- package/dist/src/dictionary.js.map +1 -0
- package/dist/src/exchange.d.ts +259 -0
- package/dist/src/exchange.js +1088 -0
- package/dist/src/exchange.js.map +1 -0
- package/dist/src/forms.d.ts +16 -0
- package/dist/src/forms.js +181 -0
- package/dist/src/forms.js.map +1 -0
- package/dist/src/index.d.ts +32 -0
- package/dist/src/index.js +34 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lex.d.ts +21 -0
- package/dist/src/lex.js +142 -0
- package/dist/src/lex.js.map +1 -0
- package/dist/src/lifecycle.d.ts +92 -0
- package/dist/src/lifecycle.js +451 -0
- package/dist/src/lifecycle.js.map +1 -0
- package/dist/src/log.d.ts +91 -0
- package/dist/src/log.js +629 -0
- package/dist/src/log.js.map +1 -0
- package/dist/src/migrate.d.ts +129 -0
- package/dist/src/migrate.js +415 -0
- package/dist/src/migrate.js.map +1 -0
- package/dist/src/model.d.ts +81 -0
- package/dist/src/model.js +188 -0
- package/dist/src/model.js.map +1 -0
- package/dist/src/packages.d.ts +372 -0
- package/dist/src/packages.js +1158 -0
- package/dist/src/packages.js.map +1 -0
- package/dist/src/print-text.d.ts +7 -0
- package/dist/src/print-text.js +223 -0
- package/dist/src/print-text.js.map +1 -0
- package/dist/src/publish.d.ts +27 -0
- package/dist/src/publish.js +279 -0
- package/dist/src/publish.js.map +1 -0
- package/dist/src/read-text.d.ts +10 -0
- package/dist/src/read-text.js +400 -0
- package/dist/src/read-text.js.map +1 -0
- package/dist/src/records.d.ts +32 -0
- package/dist/src/records.js +104 -0
- package/dist/src/records.js.map +1 -0
- package/dist/src/review.d.ts +22 -0
- package/dist/src/review.js +198 -0
- package/dist/src/review.js.map +1 -0
- package/dist/src/rules.d.ts +101 -0
- package/dist/src/rules.js +670 -0
- package/dist/src/rules.js.map +1 -0
- package/dist/src/schedule.d.ts +24 -0
- package/dist/src/schedule.js +126 -0
- package/dist/src/schedule.js.map +1 -0
- package/dist/src/std-packages.d.ts +9 -0
- package/dist/src/std-packages.js +268 -0
- package/dist/src/std-packages.js.map +1 -0
- package/dist/src/store.d.ts +191 -0
- package/dist/src/store.js +671 -0
- package/dist/src/store.js.map +1 -0
- package/dist/src/surface.d.ts +47 -0
- package/dist/src/surface.js +92 -0
- package/dist/src/surface.js.map +1 -0
- package/dist/src/tree.d.ts +35 -0
- package/dist/src/tree.js +159 -0
- package/dist/src/tree.js.map +1 -0
- package/dist/src/units-table.d.ts +1 -0
- package/dist/src/units-table.js +200 -0
- package/dist/src/units-table.js.map +1 -0
- package/dist/src/units.d.ts +42 -0
- package/dist/src/units.js +98 -0
- package/dist/src/units.js.map +1 -0
- package/dist/src/versions.d.ts +10 -0
- package/dist/src/versions.js +27 -0
- package/dist/src/versions.js.map +1 -0
- package/dist/src/views.d.ts +372 -0
- package/dist/src/views.js +1170 -0
- package/dist/src/views.js.map +1 -0
- package/package.json +60 -4
package/dist/src/log.js
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
// log.ts: the log as effects (spec 8.5) and rebuild (8.5.3). An entry says exactly what changed in the
|
|
2
|
+
// stored state: value writes with their source and where they landed, envelope writes, observations,
|
|
3
|
+
// bindings, review decisions, instructions, runs, installs and (under an emergency edge) reads. Rebuilding
|
|
4
|
+
// applies the entries in order to an empty store and needs no rules, so it gives back exactly the state the
|
|
5
|
+
// store had. Redacted entries are skipped, and only value, observation and read writes may be redacted.
|
|
6
|
+
import { serialize } from "./canonical.js";
|
|
7
|
+
import { MemoryRecords } from "./records.js";
|
|
8
|
+
import { writeHash } from "./store.js";
|
|
9
|
+
import { CrubalError } from "./tree.js";
|
|
10
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
11
|
+
export function emptyState() {
|
|
12
|
+
return { records: new MemoryRecords(), edges: new Map(), bindings: new Map(), instructions: new Map(), observations: [], reviews: [], runs: new Map(), installed: [], reads: [], definitions: new Map() };
|
|
13
|
+
}
|
|
14
|
+
// A filter names its facet in a `facet` field, a view its `shape`, a contract its `kind`: the member kinds
|
|
15
|
+
// that carry another kind's word are read first, so `{filter, facet}` is a filter and never a facet.
|
|
16
|
+
const MEMBERS = ["filter", "view", "contract", "facet", "class", "rule", "kind", "shape", "display", "print", "permissions", "ownership", "units", "utility", "lane", "flow", "act"];
|
|
17
|
+
/** The key of a `define` write's member: which kind of member, and its name. */
|
|
18
|
+
function memberKey(def) {
|
|
19
|
+
for (const m of MEMBERS)
|
|
20
|
+
if (typeof def[m] === "string")
|
|
21
|
+
return `${m}:${def[m]}`;
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
/** The source a value write carries: the write's own, else the entry's default, with `via` the entry. */
|
|
25
|
+
function sourceOf(entry, write) {
|
|
26
|
+
const base = isObj(entry.source) ? entry.source : {};
|
|
27
|
+
const own = isObj(write.source) ? write.source : {};
|
|
28
|
+
const merged = { ...base, ...own };
|
|
29
|
+
// A copied value keeps the source of where it came from (1.12, 1.7), so a write may name its own `via`.
|
|
30
|
+
const s = { origin: merged.origin ?? "reported", via: merged.via ?? String(entry.log) };
|
|
31
|
+
if (merged.owner)
|
|
32
|
+
s.owner = merged.owner;
|
|
33
|
+
if (merged.adopted)
|
|
34
|
+
s.adopted = true;
|
|
35
|
+
// The terms a value was taken under (7.8) ride on the write and come back with it.
|
|
36
|
+
if (typeof merged.until === "string")
|
|
37
|
+
s.until = merged.until;
|
|
38
|
+
if (merged.reshare === false)
|
|
39
|
+
s.reshare = false;
|
|
40
|
+
if (merged.revocable === true)
|
|
41
|
+
s.revocable = true;
|
|
42
|
+
return s;
|
|
43
|
+
}
|
|
44
|
+
/** Set or remove the value at a top-level key or `parts[row].key` address. */
|
|
45
|
+
function place(r, address, value, source) {
|
|
46
|
+
const part = address.match(/^parts\[([^\]]+)\]\.(\w+)$/);
|
|
47
|
+
if (part) {
|
|
48
|
+
const parts = (r.parts ?? []).filter(isObj);
|
|
49
|
+
let p = parts.find((x) => String(x.$row) === part[1]);
|
|
50
|
+
if (!p) {
|
|
51
|
+
p = { $row: part[1], values: {} };
|
|
52
|
+
parts.push(p);
|
|
53
|
+
}
|
|
54
|
+
const values = isObj(p.values) ? { ...p.values } : {};
|
|
55
|
+
if (value === undefined)
|
|
56
|
+
delete values[part[2]];
|
|
57
|
+
else
|
|
58
|
+
values[part[2]] = value;
|
|
59
|
+
p.values = values;
|
|
60
|
+
r.parts = parts;
|
|
61
|
+
}
|
|
62
|
+
else if (value === undefined)
|
|
63
|
+
delete r.values[address];
|
|
64
|
+
else
|
|
65
|
+
r.values[address] = value;
|
|
66
|
+
if (source) {
|
|
67
|
+
r.sources = { ...(r.sources ?? {}) };
|
|
68
|
+
if (value === undefined)
|
|
69
|
+
delete r.sources[address];
|
|
70
|
+
else
|
|
71
|
+
r.sources[address] = source;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function need(state, id, what) {
|
|
75
|
+
const r = state.records.get(id);
|
|
76
|
+
if (!r)
|
|
77
|
+
throw new CrubalError(`${what} names ${id}, a record the log hasn't created`);
|
|
78
|
+
// A record a working set shares between copies is frozen: the entry changes its own copy, set back.
|
|
79
|
+
if (Object.isFrozen(r)) {
|
|
80
|
+
const copy = structuredClone(r);
|
|
81
|
+
state.records.set(id, copy);
|
|
82
|
+
return copy;
|
|
83
|
+
}
|
|
84
|
+
return r;
|
|
85
|
+
}
|
|
86
|
+
/** Apply one entry's effects to the state. */
|
|
87
|
+
export function applyEntry(state, entry) {
|
|
88
|
+
if (typeof entry.redacted === "string")
|
|
89
|
+
return;
|
|
90
|
+
const writes = Array.isArray(entry.writes) ? entry.writes.filter(isObj) : [];
|
|
91
|
+
for (const w of writes) {
|
|
92
|
+
// A redacted write (8.5.2) is skipped: the state is rebuilt without what it held.
|
|
93
|
+
if (typeof w.redacted === "string")
|
|
94
|
+
continue;
|
|
95
|
+
if (typeof w.envelope === "string") {
|
|
96
|
+
if (typeof w.address === "string" && /^parts\[[^\]]+\]$/.test(w.address))
|
|
97
|
+
applyPartRow(state, entry, w);
|
|
98
|
+
else
|
|
99
|
+
applyEnvelope(state, entry, w);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (isObj(w.edge)) {
|
|
103
|
+
state.edges.set(String(w.edge.edge), w.edge);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (typeof w.edge === "string" && w.removed === true) {
|
|
107
|
+
state.edges.delete(w.edge);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (isObj(w.binding)) {
|
|
111
|
+
const b = w.binding;
|
|
112
|
+
const key = `${b.contract}|${b.ref}`;
|
|
113
|
+
if (b.removed === true)
|
|
114
|
+
state.bindings.delete(key);
|
|
115
|
+
else
|
|
116
|
+
state.bindings.set(key, String(b.record));
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (isObj(w.instruction)) {
|
|
120
|
+
const i = w.instruction;
|
|
121
|
+
const issuer = String(i.issuer);
|
|
122
|
+
state.instructions.set(issuer, [...(state.instructions.get(issuer) ?? []), i]);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (isObj(w.observation)) {
|
|
126
|
+
state.observations.push(w.observation);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (isObj(w.review)) {
|
|
130
|
+
state.reviews.push(w.review);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (typeof w.run === "string") {
|
|
134
|
+
state.runs.set(w.run, { ...(state.runs.get(w.run) ?? {}), ...w });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (isObj(w.install)) {
|
|
138
|
+
state.installed.push(w.install);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (isObj(w.define)) {
|
|
142
|
+
// A member of the working package as it now stands, or retired (8.5, 11.4).
|
|
143
|
+
const key = memberKey(w.define);
|
|
144
|
+
if (!key)
|
|
145
|
+
throw new CrubalError(`entry ${entry.log}: a define write names a ${MEMBERS.join(", ")}`);
|
|
146
|
+
if (w.define.retired === true)
|
|
147
|
+
state.definitions.delete(key);
|
|
148
|
+
else
|
|
149
|
+
state.definitions.set(key, w.define);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (typeof w.read === "string") {
|
|
153
|
+
state.reads.push({ record: w.read, log: String(entry.log), by: (entry.by ?? "") });
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (typeof w.record === "string" && typeof w.address === "string") {
|
|
157
|
+
applyValue(state, entry, w);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
throw new CrubalError(`entry ${entry.log} holds a write of no known kind: ${serialize(w)}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function applyValue(state, entry, w) {
|
|
164
|
+
const r = need(state, String(w.record), `entry ${entry.log}`);
|
|
165
|
+
const address = String(w.address);
|
|
166
|
+
const as = typeof w.as === "string" ? w.as : "current";
|
|
167
|
+
const source = sourceOf(entry, w);
|
|
168
|
+
const removed = w.removed === true;
|
|
169
|
+
if (typeof entry.at === "string")
|
|
170
|
+
r.updated = entry.at;
|
|
171
|
+
if (as === "current") {
|
|
172
|
+
place(r, address, removed ? undefined : w.value, source);
|
|
173
|
+
// A value made current is no longer held there.
|
|
174
|
+
if (r.held?.[address]) {
|
|
175
|
+
const { [address]: _h, ...rest } = r.held;
|
|
176
|
+
if (Object.keys(rest).length)
|
|
177
|
+
r.held = rest;
|
|
178
|
+
else
|
|
179
|
+
delete r.held;
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (as === "held" && removed) {
|
|
184
|
+
// A hold let go by a decision (10.4): nothing comes back.
|
|
185
|
+
const held = { ...(r.held ?? {}) };
|
|
186
|
+
delete held[address];
|
|
187
|
+
if (Object.keys(held).length)
|
|
188
|
+
r.held = held;
|
|
189
|
+
else
|
|
190
|
+
delete r.held;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (as === "held") {
|
|
194
|
+
// Held: the value leaves the current state and waits, with the rule that held it (6.7).
|
|
195
|
+
const held = { ...(r.held ?? {}) };
|
|
196
|
+
const value = w.value ?? r.values[address];
|
|
197
|
+
if (value === undefined)
|
|
198
|
+
throw new CrubalError(`entry ${entry.log}: nothing at ${address} to hold`);
|
|
199
|
+
const kept = isObj(w.source) ? w.source : undefined;
|
|
200
|
+
held[address] = { value, ...(typeof w.rule === "string" ? { rule: w.rule } : {}), ...(typeof w.instruction === "string" ? { instruction: w.instruction } : {}), ...(kept ? { source: kept } : {}) };
|
|
201
|
+
r.held = held;
|
|
202
|
+
place(r, address, undefined, source);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const value = w.value;
|
|
206
|
+
if (value === undefined && !removed)
|
|
207
|
+
throw new CrubalError(`entry ${entry.log}: a value landing as ${as} needs its value`);
|
|
208
|
+
if (as === "alternative" || as === "suggestion") {
|
|
209
|
+
const bag = { ...(r[as === "alternative" ? "alternatives" : "suggestions"] ?? {}) };
|
|
210
|
+
const item = { value: value, origin: source.origin, via: source.via, ...(source.owner ? { owner: source.owner } : {}) };
|
|
211
|
+
bag[address] = removed ? (bag[address] ?? []).filter((x) => serialize(x.value) !== serialize(value)) : [...(bag[address] ?? []), item];
|
|
212
|
+
if (!bag[address]?.length)
|
|
213
|
+
delete bag[address];
|
|
214
|
+
if (as === "alternative") {
|
|
215
|
+
if (Object.keys(bag).length)
|
|
216
|
+
r.alternatives = bag;
|
|
217
|
+
else
|
|
218
|
+
delete r.alternatives;
|
|
219
|
+
}
|
|
220
|
+
else if (Object.keys(bag).length)
|
|
221
|
+
r.suggestions = bag;
|
|
222
|
+
else
|
|
223
|
+
delete r.suggestions;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
throw new CrubalError(`entry ${entry.log}: a value lands as current, alternative, suggestion or held, not ${as}`);
|
|
227
|
+
}
|
|
228
|
+
/** A part row's envelope (5.6): `wears` sets the row's classes, making the row when it is new; `row` removed drops it. */
|
|
229
|
+
function applyPartRow(state, entry, w) {
|
|
230
|
+
const r = need(state, String(w.record), `entry ${entry.log}`);
|
|
231
|
+
const row = String(w.address).slice("parts[".length, -1);
|
|
232
|
+
const parts = (r.parts ?? []).filter(isObj);
|
|
233
|
+
const member = String(w.envelope);
|
|
234
|
+
if (typeof entry.at === "string")
|
|
235
|
+
r.updated = entry.at;
|
|
236
|
+
if (member === "row" && w.removed === true) {
|
|
237
|
+
const left = parts.filter((p) => String(p.$row) !== row);
|
|
238
|
+
if (left.length)
|
|
239
|
+
r.parts = left;
|
|
240
|
+
else
|
|
241
|
+
delete r.parts;
|
|
242
|
+
if (r.sources)
|
|
243
|
+
for (const k of Object.keys(r.sources))
|
|
244
|
+
if (k.startsWith(`parts[${row}].`))
|
|
245
|
+
delete r.sources[k];
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (member !== "wears")
|
|
249
|
+
throw new CrubalError(`entry ${entry.log}: no part envelope member ${member}`);
|
|
250
|
+
// A row with no classes of its own (a unit's copy of a mirrored row, 1.6) carries no `wears` member at all.
|
|
251
|
+
const wears = Array.isArray(w.value) ? w.value.map(String) : [];
|
|
252
|
+
const have = parts.find((p) => String(p.$row) === row);
|
|
253
|
+
if (have) {
|
|
254
|
+
if (wears.length)
|
|
255
|
+
have.wears = wears;
|
|
256
|
+
else
|
|
257
|
+
delete have.wears;
|
|
258
|
+
}
|
|
259
|
+
else
|
|
260
|
+
parts.push({ $row: row, ...(wears.length ? { wears } : {}), values: {} });
|
|
261
|
+
r.parts = parts;
|
|
262
|
+
}
|
|
263
|
+
function applyEnvelope(state, entry, w) {
|
|
264
|
+
const id = String(w.record);
|
|
265
|
+
const member = String(w.envelope);
|
|
266
|
+
if (member === "created") {
|
|
267
|
+
const v = isObj(w.value) ? w.value : {};
|
|
268
|
+
const r = { id, kind: String(v.kind), level: String(v.level), owner: String(v.owner ?? entry.owner ?? ""), values: {} };
|
|
269
|
+
if (typeof v.of === "string")
|
|
270
|
+
r.of = v.of;
|
|
271
|
+
if (typeof v.within === "string")
|
|
272
|
+
r.within = v.within;
|
|
273
|
+
if (Array.isArray(v.wears))
|
|
274
|
+
r.wears = v.wears.map(String);
|
|
275
|
+
if (isObj(v.carriers) && Object.keys(v.carriers).length)
|
|
276
|
+
r.carriers = carriersOf(v.carriers);
|
|
277
|
+
if (typeof entry.at === "string") {
|
|
278
|
+
r.created = entry.at;
|
|
279
|
+
r.updated = entry.at;
|
|
280
|
+
}
|
|
281
|
+
state.records.set(id, r);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const r = need(state, id, `entry ${entry.log}`);
|
|
285
|
+
if (typeof entry.at === "string")
|
|
286
|
+
r.updated = entry.at;
|
|
287
|
+
if (member === "of" || member === "within") {
|
|
288
|
+
if (w.removed === true)
|
|
289
|
+
delete r[member];
|
|
290
|
+
else
|
|
291
|
+
r[member] = String(w.value);
|
|
292
|
+
}
|
|
293
|
+
else if (member === "wears")
|
|
294
|
+
r.wears = Array.isArray(w.value) ? w.value.map(String) : [];
|
|
295
|
+
else if (member === "carriers") {
|
|
296
|
+
const c = isObj(w.value) ? carriersOf(w.value) : {};
|
|
297
|
+
if (Object.keys(c).length)
|
|
298
|
+
r.carriers = c;
|
|
299
|
+
else
|
|
300
|
+
delete r.carriers;
|
|
301
|
+
}
|
|
302
|
+
else if (member === "status") {
|
|
303
|
+
const s = isObj(w.value) ? w.value : {};
|
|
304
|
+
if (s.state === "archived") {
|
|
305
|
+
const st = { state: "archived" };
|
|
306
|
+
if (typeof s.reason === "string")
|
|
307
|
+
st.reason = s.reason;
|
|
308
|
+
if (typeof s.into === "string")
|
|
309
|
+
st.into = s.into;
|
|
310
|
+
r.status = st;
|
|
311
|
+
}
|
|
312
|
+
else
|
|
313
|
+
delete r.status;
|
|
314
|
+
}
|
|
315
|
+
else if (member === "applied") {
|
|
316
|
+
const v = isObj(w.value) ? w.value : {};
|
|
317
|
+
r.applied = { ...(r.applied ?? {}), ...Object.fromEntries(Object.entries(v).map(([k, n]) => [k, Number(n)])) };
|
|
318
|
+
}
|
|
319
|
+
else
|
|
320
|
+
throw new CrubalError(`entry ${entry.log}: no envelope member ${member}`);
|
|
321
|
+
}
|
|
322
|
+
/** A carriers envelope as the engine holds it (5.5, revision 18): each class's carriers, `by` and `version`. */
|
|
323
|
+
function carriersOf(v) {
|
|
324
|
+
const out = {};
|
|
325
|
+
for (const [cls, list] of Object.entries(v)) {
|
|
326
|
+
if (!Array.isArray(list))
|
|
327
|
+
continue;
|
|
328
|
+
const cs = list.filter(isObj).filter((c) => typeof c.by === "string").map((c) => ({ by: String(c.by), ...(typeof c.version === "string" ? { version: c.version } : {}) }));
|
|
329
|
+
if (cs.length)
|
|
330
|
+
out[cls] = cs;
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
/** Rebuild a store's state from its log alone (8.5.3). */
|
|
335
|
+
export function rebuild(entries) {
|
|
336
|
+
const state = emptyState();
|
|
337
|
+
for (const e of entries)
|
|
338
|
+
applyEntry(state, e);
|
|
339
|
+
return state;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Revision 50: one record as it stood. The entries are the log's, in order; only this record's own writes are
|
|
343
|
+
* applied, and only those of entries at or before `until` (every one when it is not said). Answers the record
|
|
344
|
+
* as it then stood (nothing when it was not yet made) and, for each address of its values, when it last
|
|
345
|
+
* changed: the moment of the last entry that left it other than it was. An entry or a write redacted is
|
|
346
|
+
* passed over as a rebuild passes it over, so what it held is not answered.
|
|
347
|
+
*/
|
|
348
|
+
export function recordAt(entries, id, until) {
|
|
349
|
+
const state = emptyState();
|
|
350
|
+
const changed = {};
|
|
351
|
+
for (const e of entries) {
|
|
352
|
+
if (until !== undefined && typeof e.at === "string" && e.at > until)
|
|
353
|
+
continue;
|
|
354
|
+
const writes = (Array.isArray(e.writes) ? e.writes.filter(isObj) : []).filter((w) => w.record === id && (typeof w.address === "string" || typeof w.envelope === "string"));
|
|
355
|
+
if (!writes.length)
|
|
356
|
+
continue;
|
|
357
|
+
const before = { ...(state.records.get(id)?.values ?? {}) };
|
|
358
|
+
try {
|
|
359
|
+
applyEntry(state, { ...e, writes });
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
// A write on a record whose making was redacted: nothing of it is answered.
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
const after = state.records.get(id)?.values ?? {};
|
|
366
|
+
if (typeof e.at !== "string")
|
|
367
|
+
continue;
|
|
368
|
+
for (const k of new Set([...Object.keys(before), ...Object.keys(after)]))
|
|
369
|
+
if (serialize((before[k] ?? null)) !== serialize((after[k] ?? null)) || (k in before) !== (k in after))
|
|
370
|
+
changed[k] = e.at;
|
|
371
|
+
}
|
|
372
|
+
const record = state.records.get(id);
|
|
373
|
+
return { ...(record ? { record } : {}), changed };
|
|
374
|
+
}
|
|
375
|
+
/** The state as JSON, for comparing and for a dump. */
|
|
376
|
+
export function stateJson(state) {
|
|
377
|
+
const out = { records: [...state.records.values()] };
|
|
378
|
+
if (state.edges.size)
|
|
379
|
+
out.edges = [...state.edges.values()];
|
|
380
|
+
if (state.bindings.size)
|
|
381
|
+
out.bindings = Object.fromEntries(state.bindings);
|
|
382
|
+
if (state.instructions.size)
|
|
383
|
+
out.instructions = Object.fromEntries([...state.instructions].map(([k, v]) => [k, v]));
|
|
384
|
+
if (state.observations.length)
|
|
385
|
+
out.observations = state.observations;
|
|
386
|
+
if (state.reviews.length)
|
|
387
|
+
out.reviews = state.reviews;
|
|
388
|
+
if (state.runs.size)
|
|
389
|
+
out.runs = [...state.runs.values()];
|
|
390
|
+
if (state.installed.length)
|
|
391
|
+
out.installed = state.installed;
|
|
392
|
+
if (state.reads.length)
|
|
393
|
+
out.reads = state.reads;
|
|
394
|
+
if (state.definitions.size)
|
|
395
|
+
out.definitions = [...state.definitions.values()];
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
function sourceAt(r, key) {
|
|
399
|
+
return r.sources?.[key] ?? r.sources?.["*"];
|
|
400
|
+
}
|
|
401
|
+
const sameJson = (a, b) => serialize((a ?? "∅")) === serialize((b ?? "∅"));
|
|
402
|
+
/** The writes that take `before` to `after`: exactly the effects, nothing computed (8.5). */
|
|
403
|
+
export function diffWrites(before, after) {
|
|
404
|
+
const writes = [];
|
|
405
|
+
for (const [id, r] of after.records) {
|
|
406
|
+
const was = before.records.get(id);
|
|
407
|
+
const now = r;
|
|
408
|
+
if (!was) {
|
|
409
|
+
const v = { kind: now.kind, level: now.level, owner: now.owner };
|
|
410
|
+
if (now.of)
|
|
411
|
+
v.of = now.of;
|
|
412
|
+
if (now.within)
|
|
413
|
+
v.within = now.within;
|
|
414
|
+
if (now.wears)
|
|
415
|
+
v.wears = now.wears;
|
|
416
|
+
if (now.carriers && Object.keys(now.carriers).length)
|
|
417
|
+
v.carriers = now.carriers;
|
|
418
|
+
writes.push({ record: id, envelope: "created", value: v });
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
for (const m of ["of", "within"]) {
|
|
422
|
+
if (was[m] === now[m])
|
|
423
|
+
continue;
|
|
424
|
+
writes.push(now[m] ? { record: id, envelope: m, value: now[m] } : { record: id, envelope: m, removed: true });
|
|
425
|
+
}
|
|
426
|
+
if (!sameJson(was.wears ?? [], now.wears ?? []))
|
|
427
|
+
writes.push({ record: id, envelope: "wears", value: now.wears ?? [] });
|
|
428
|
+
if (!sameJson((was.carriers ?? {}), (now.carriers ?? {})))
|
|
429
|
+
writes.push({ record: id, envelope: "carriers", value: (now.carriers ?? {}) });
|
|
430
|
+
}
|
|
431
|
+
if (!sameJson((was?.status ?? {}), (now.status ?? {})))
|
|
432
|
+
writes.push({ record: id, envelope: "status", value: (now.status ?? { state: "active" }) });
|
|
433
|
+
if (!sameJson(was?.applied ?? {}, now.applied ?? {}))
|
|
434
|
+
writes.push({ record: id, envelope: "applied", value: now.applied ?? {} });
|
|
435
|
+
const heldWas = was?.held ?? {};
|
|
436
|
+
const heldNow = now.held ?? {};
|
|
437
|
+
const newlyHeld = new Set(Object.keys(heldNow).filter((k) => !sameJson(heldWas[k], heldNow[k])));
|
|
438
|
+
for (const k of newlyHeld) {
|
|
439
|
+
const h = heldNow[k];
|
|
440
|
+
// The value's source rides with the hold, so a release gives it back as it was (6.7).
|
|
441
|
+
const src = isObj(h.source) ? h.source : undefined;
|
|
442
|
+
writes.push({ record: id, address: k, as: "held", value: h.value, ...(typeof h.rule === "string" ? { rule: h.rule } : {}), ...(typeof h.instruction === "string" ? { instruction: h.instruction } : {}), ...(src ? { source: src } : {}) });
|
|
443
|
+
}
|
|
444
|
+
for (const k of Object.keys(heldWas))
|
|
445
|
+
if (heldNow[k] === undefined && now.values[k] === undefined)
|
|
446
|
+
writes.push({ record: id, address: k, as: "held", removed: true });
|
|
447
|
+
for (const [k, v] of Object.entries(now.values)) {
|
|
448
|
+
const s = sourceAt(now, k);
|
|
449
|
+
if (was && sameJson(was.values[k], v) && sameJson((sourceAt(was, k) ?? ""), (s ?? "")))
|
|
450
|
+
continue;
|
|
451
|
+
writes.push({ record: id, address: k, value: v, ...(s ? { source: s } : {}) });
|
|
452
|
+
}
|
|
453
|
+
for (const k of Object.keys(was?.values ?? {}))
|
|
454
|
+
if (now.values[k] === undefined && !newlyHeld.has(k))
|
|
455
|
+
writes.push({ record: id, address: k, removed: true });
|
|
456
|
+
// Parts (5.6): a row's classes as an envelope write at `parts[row]`, its values as value writes at
|
|
457
|
+
// `parts[row].key` with their sources, a row gone as a removed row. Without these a draft that landed a
|
|
458
|
+
// part failed the log's own check, "the log doesn't describe the change" (2026-10-27).
|
|
459
|
+
const partsWas = new Map((was?.parts ?? []).filter(isObj).map((p) => [String(p.$row), p]));
|
|
460
|
+
const partsNow = new Map((now.parts ?? []).filter(isObj).map((p) => [String(p.$row), p]));
|
|
461
|
+
for (const [row, p] of partsNow) {
|
|
462
|
+
const q = partsWas.get(row);
|
|
463
|
+
const at = `parts[${row}]`;
|
|
464
|
+
const wearsNow = Array.isArray(p.wears) ? p.wears : [];
|
|
465
|
+
if (!q || !sameJson(Array.isArray(q.wears) ? q.wears : [], wearsNow))
|
|
466
|
+
writes.push({ record: id, address: at, envelope: "wears", value: wearsNow });
|
|
467
|
+
const vNow = isObj(p.values) ? p.values : {};
|
|
468
|
+
const vWas = q && isObj(q.values) ? q.values : {};
|
|
469
|
+
for (const [k, v] of Object.entries(vNow)) {
|
|
470
|
+
const s = sourceAt(now, `${at}.${k}`);
|
|
471
|
+
if (q && sameJson(vWas[k], v) && sameJson((sourceAt(was, `${at}.${k}`) ?? ""), (s ?? "")))
|
|
472
|
+
continue;
|
|
473
|
+
writes.push({ record: id, address: `${at}.${k}`, value: v, ...(s ? { source: s } : {}) });
|
|
474
|
+
}
|
|
475
|
+
for (const k of Object.keys(vWas))
|
|
476
|
+
if (vNow[k] === undefined)
|
|
477
|
+
writes.push({ record: id, address: `${at}.${k}`, removed: true });
|
|
478
|
+
}
|
|
479
|
+
for (const row of partsWas.keys())
|
|
480
|
+
if (!partsNow.has(row))
|
|
481
|
+
writes.push({ record: id, address: `parts[${row}]`, envelope: "row", removed: true });
|
|
482
|
+
for (const bag of ["alternatives", "suggestions"]) {
|
|
483
|
+
const bw = was?.[bag] ?? {};
|
|
484
|
+
// Items gone from the bag: accepted or denied (14.7), kept in the history as removed.
|
|
485
|
+
for (const [addr, olds] of Object.entries(bw)) {
|
|
486
|
+
const keep = (now[bag]?.[addr] ?? []).map((x) => serialize(x));
|
|
487
|
+
for (const it of olds)
|
|
488
|
+
if (!keep.includes(serialize(it)))
|
|
489
|
+
writes.push({ record: id, address: addr, as: bag === "alternatives" ? "alternative" : "suggestion", value: it.value, removed: true });
|
|
490
|
+
}
|
|
491
|
+
for (const [addr, items] of Object.entries(now[bag] ?? {})) {
|
|
492
|
+
const old = (bw[addr] ?? []).map((x) => serialize(x));
|
|
493
|
+
for (const it of items) {
|
|
494
|
+
if (old.includes(serialize(it)))
|
|
495
|
+
continue;
|
|
496
|
+
const src = { origin: it.origin, via: it.via, ...(it.owner ? { owner: it.owner } : {}) };
|
|
497
|
+
writes.push({ record: id, address: addr, as: bag === "alternatives" ? "alternative" : "suggestion", value: it.value, source: src });
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
for (const [key, record] of after.bindings) {
|
|
503
|
+
if (before.bindings.get(key) === record)
|
|
504
|
+
continue;
|
|
505
|
+
const at = key.indexOf("|");
|
|
506
|
+
writes.push({ binding: { contract: key.slice(0, at), ref: key.slice(at + 1), record } });
|
|
507
|
+
}
|
|
508
|
+
for (const [issuer, list] of after.instructions) {
|
|
509
|
+
const have = before.instructions.get(issuer)?.length ?? 0;
|
|
510
|
+
for (const i of list.slice(have))
|
|
511
|
+
writes.push({ instruction: { issuer, ...i } });
|
|
512
|
+
}
|
|
513
|
+
for (const [id, e] of after.edges)
|
|
514
|
+
if (!sameJson(before.edges.get(id), e))
|
|
515
|
+
writes.push({ edge: e });
|
|
516
|
+
for (const id of before.edges.keys())
|
|
517
|
+
if (!after.edges.has(id))
|
|
518
|
+
writes.push({ edge: id, removed: true });
|
|
519
|
+
for (const item of after.review.slice(before.reviews.length))
|
|
520
|
+
writes.push({ review: item });
|
|
521
|
+
return writes;
|
|
522
|
+
}
|
|
523
|
+
/** A record with its sources spelled per key and its times dropped, so a draft and a rebuilt state compare. */
|
|
524
|
+
function normal(r) {
|
|
525
|
+
const x = r;
|
|
526
|
+
const sources = {};
|
|
527
|
+
for (const k of Object.keys(r.values)) {
|
|
528
|
+
const s = sourceAt(r, k);
|
|
529
|
+
if (s)
|
|
530
|
+
sources[k] = s;
|
|
531
|
+
}
|
|
532
|
+
const { created: _c, updated: _u, sources: _s, ...rest } = x;
|
|
533
|
+
return { ...rest, sources };
|
|
534
|
+
}
|
|
535
|
+
/** Where a draft and a state disagree, or null. */
|
|
536
|
+
export function differs(state, draft) {
|
|
537
|
+
for (const [id, r] of draft.records) {
|
|
538
|
+
const s = state.records.get(id);
|
|
539
|
+
if (!s)
|
|
540
|
+
return `${id} is in the draft but not the state`;
|
|
541
|
+
if (serialize(normal(s)) !== serialize(normal(r)))
|
|
542
|
+
return `${id}: draft ${serialize(normal(r))} state ${serialize(normal(s))}`;
|
|
543
|
+
}
|
|
544
|
+
for (const id of state.records.keys())
|
|
545
|
+
if (!draft.records.has(id))
|
|
546
|
+
return `${id} is in the state but not the draft`;
|
|
547
|
+
for (const [k, v] of draft.bindings)
|
|
548
|
+
if (state.bindings.get(k) !== v)
|
|
549
|
+
return `binding ${k}`;
|
|
550
|
+
for (const [k, v] of draft.instructions)
|
|
551
|
+
if ((state.instructions.get(k)?.length ?? 0) !== v.length)
|
|
552
|
+
return `instructions of ${k}`;
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* The journal (8.5): operations work on a draft, every change becomes an entry, and the state is only ever
|
|
557
|
+
* what applying the entries gives. A commit whose entries don't describe the whole change is refused, so
|
|
558
|
+
* the log can never fall behind the state.
|
|
559
|
+
*/
|
|
560
|
+
export class Journal {
|
|
561
|
+
state = emptyState();
|
|
562
|
+
entries = [];
|
|
563
|
+
draft;
|
|
564
|
+
owner;
|
|
565
|
+
n = 0;
|
|
566
|
+
constructor(owner, records = [], at) {
|
|
567
|
+
this.owner = owner;
|
|
568
|
+
this.draft = { records: new MemoryRecords(), bindings: new Map(), instructions: new Map(), edges: new Map(), review: [] };
|
|
569
|
+
for (const r of records)
|
|
570
|
+
this.draft.records.set(r.id, structuredClone(r));
|
|
571
|
+
if (records.length)
|
|
572
|
+
this.commit({ migration: "backfill" }, () => { }, at);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Run an operation on the draft, log what it changed, and apply the entry to the state. `extra` carries
|
|
576
|
+
* writes the diff can't see (a run's state, a read under an emergency edge, a define).
|
|
577
|
+
*/
|
|
578
|
+
commit(by, mutate, at, extra = []) {
|
|
579
|
+
mutate(this.draft);
|
|
580
|
+
const writes = [...diffWrites(this.state, this.draft), ...extra];
|
|
581
|
+
if (!writes.length)
|
|
582
|
+
return null;
|
|
583
|
+
this.n += 1;
|
|
584
|
+
const entry = { log: `J-${this.n}`, owner: this.owner, at: at ?? new Date().toISOString().replace(/\.\d{3}Z$/, "Z"), by, writes };
|
|
585
|
+
applyEntry(this.state, entry);
|
|
586
|
+
this.entries.push(entry);
|
|
587
|
+
const problem = differs(this.state, this.draft);
|
|
588
|
+
if (problem)
|
|
589
|
+
throw new CrubalError(`the log doesn't describe the change: ${problem}`);
|
|
590
|
+
return entry;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Can this whole entry be redacted (8.5.2)? Only when it holds nothing but value, observation and read
|
|
595
|
+
* writes and isn't witnessed; otherwise it is redacted write by write. Returns the reason it can't, or null.
|
|
596
|
+
*/
|
|
597
|
+
export function redactable(entry) {
|
|
598
|
+
if (entry.witnessed === true)
|
|
599
|
+
return "a witnessed entry is never redacted";
|
|
600
|
+
const writes = Array.isArray(entry.writes) ? entry.writes.filter(isObj) : [];
|
|
601
|
+
for (const w of writes)
|
|
602
|
+
if (redactableWrite(entry, w))
|
|
603
|
+
return "an entry holding a write that is never redacted is redacted write by write, never whole";
|
|
604
|
+
return null;
|
|
605
|
+
}
|
|
606
|
+
/** Can this write be redacted (8.5.2)? Null when it can, else why not. */
|
|
607
|
+
export function redactableWrite(entry, w) {
|
|
608
|
+
if (entry.witnessed === true)
|
|
609
|
+
return "a witnessed entry is never redacted";
|
|
610
|
+
if (typeof w.redacted === "string")
|
|
611
|
+
return null;
|
|
612
|
+
const value = typeof w.record === "string" && typeof w.address === "string";
|
|
613
|
+
if (value || isObj(w.observation) || typeof w.read === "string")
|
|
614
|
+
return null;
|
|
615
|
+
return "a write that created a record, changed its envelope or a definition, or wrote an edge, binding, review, instruction, run or install is never redacted";
|
|
616
|
+
}
|
|
617
|
+
/** Redact one write of an entry (8.5.2): it becomes its own hash, and the entry's hash stays where it was. */
|
|
618
|
+
export function redactWrite(entry, index) {
|
|
619
|
+
const writes = Array.isArray(entry.writes) ? [...entry.writes] : [];
|
|
620
|
+
const w = writes[index];
|
|
621
|
+
if (!isObj(w))
|
|
622
|
+
throw new Error(`no write ${index}`);
|
|
623
|
+
const why = redactableWrite(entry, w);
|
|
624
|
+
if (why)
|
|
625
|
+
throw new Error(why);
|
|
626
|
+
writes[index] = { redacted: `sha256:${writeHash(w)}` };
|
|
627
|
+
return { ...entry, writes };
|
|
628
|
+
}
|
|
629
|
+
//# sourceMappingURL=log.js.map
|