narraeon 0.1.1 → 0.3.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/dist/node/protocol/modelConnections.js +49 -0
- package/dist/node/protocol/modelUsage.js +39 -0
- package/dist/node/protocol/v1.js +35 -0
- package/dist/node/runtime/FileNativePhysicalClone.js +86 -0
- package/dist/node/runtime/V1Runtime.js +66 -29
- package/dist/node/runtime/model/FileNativeModelAdapters.js +730 -287
- package/dist/node/runtime/model/ModelConnectionStore.js +123 -2
- package/dist/node/runtime/model/ModelHost.js +81 -9
- package/dist/node/runtime/model/ModelHostStream.js +61 -1
- package/dist/node/runtime/model/SettingAuthorStream.js +16 -113
- package/dist/node/runtime/play/FileNativePlayAdvanceStore.js +269 -0
- package/dist/node/runtime/play/FileNativePlayTimelineStore.js +1128 -0
- package/dist/node/runtime/play/PlayCallChain.js +904 -369
- package/dist/node/runtime/play/PlayDocumentTools.js +35 -0
- package/dist/node/runtime/play/PlayFollowupRequests.js +43 -26
- package/dist/node/runtime/prompt/FileNativePromptCompiler.js +50 -35
- package/dist/node/runtime/prompt/FileNativeToolRegistry.js +55 -29
- package/dist/node/runtime/setting/DocumentCandidateSettingImprovement.js +9 -4
- package/dist/node/runtime/world/FileNativeAuthorityV3.js +971 -0
- package/dist/node/runtime/world/FileNativeHistoryProjection.js +39 -0
- package/dist/node/runtime/world/FileNativeWorldStorageMigrator.js +1067 -0
- package/dist/node/runtime/world/FileNativeWorldStore.js +615 -784
- package/dist/node/server/createServer.js +10 -2
- package/dist/web/assets/index-D5VqDvjY.js +1192 -0
- package/dist/web/assets/index-Xa0s35CJ.css +1 -0
- package/dist/web/index.html +2 -2
- package/package.json +1 -1
- package/dist/web/assets/index-CRGTZ9Ak.css +0 -1
- package/dist/web/assets/index-DDx1itzT.js +0 -1192
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { link, mkdir, open, readFile, readdir, rename, rm, writeFile, } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { isDeepStrictEqual } from "node:util";
|
|
5
|
+
import { authorityV3Directory, continuityHeadFile, FileNativeAuthorityV3, } from "./FileNativeAuthorityV3.js";
|
|
6
|
+
import { projectFileNativeHistorySurface } from "./FileNativeHistoryProjection.js";
|
|
7
|
+
const currentAuthorityHeadFile = "play-authority-head.json";
|
|
8
|
+
const releasedAuthorityFile = "play-authority.json";
|
|
9
|
+
const releasedCallChainFile = "play-call-chain.json";
|
|
10
|
+
/**
|
|
11
|
+
* One-way upgrade seam for every cumulative storage shape shipped through
|
|
12
|
+
* v0.1.1. Callers serialize this module per world. It writes all new facts and
|
|
13
|
+
* projections first and publishes the small Authority head last, so that head
|
|
14
|
+
* is also the durable marker for the whole current layout.
|
|
15
|
+
*/
|
|
16
|
+
export class FileNativeWorldStorageMigrator {
|
|
17
|
+
#worldsRoot;
|
|
18
|
+
#operationsRoot;
|
|
19
|
+
#timeline;
|
|
20
|
+
constructor(dataRoot, timeline) {
|
|
21
|
+
const root = resolve(dataRoot);
|
|
22
|
+
this.#worldsRoot = join(root, "worlds-file-native");
|
|
23
|
+
this.#operationsRoot = join(root, "operations");
|
|
24
|
+
this.#timeline = timeline;
|
|
25
|
+
}
|
|
26
|
+
async migrate(worldId) {
|
|
27
|
+
assertIdentity(worldId, "World ID");
|
|
28
|
+
const worldRoot = join(this.#worldsRoot, worldId);
|
|
29
|
+
const runtimeRoot = join(worldRoot, "runtime");
|
|
30
|
+
const currentAuthority = new FileNativeAuthorityV3(worldRoot);
|
|
31
|
+
if (await currentAuthority.exists()) {
|
|
32
|
+
const head = await currentAuthority.readHead();
|
|
33
|
+
return {
|
|
34
|
+
outcome: "already_current",
|
|
35
|
+
authorityCommits: head.sequence,
|
|
36
|
+
callChainContexts: 0,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const current = await readOptionalJson(join(runtimeRoot, currentAuthorityHeadFile));
|
|
40
|
+
if (current !== null) {
|
|
41
|
+
assertCurrentAuthorityHead(current);
|
|
42
|
+
const commits = await readCurrentAuthority(worldRoot, current);
|
|
43
|
+
if ((commits.at(-1)?.operationId ?? null) !== current.operationId)
|
|
44
|
+
throw new Error("Current Authority head operation does not match its immutable tip");
|
|
45
|
+
const contexts = await this.#timeline.readAllContexts(worldId);
|
|
46
|
+
assertTimelineAuthorityRefs(contexts, commits);
|
|
47
|
+
const checkpoint = await migrateMaterializedCheckpoint(runtimeRoot, commits);
|
|
48
|
+
return this.#migrateCurrentAuthority({
|
|
49
|
+
worldId,
|
|
50
|
+
worldRoot,
|
|
51
|
+
commits,
|
|
52
|
+
checkpoint,
|
|
53
|
+
callChainContexts: contexts.length,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const publication = await readJson(join(worldRoot, "publication.json"));
|
|
57
|
+
if (!isRecord(publication) ||
|
|
58
|
+
publication.schemaVersion !== 1 ||
|
|
59
|
+
publication.worldId !== worldId)
|
|
60
|
+
throw new Error("Released world publication has an invalid identity");
|
|
61
|
+
const genesis = await readJson(join(runtimeRoot, "genesis.json"));
|
|
62
|
+
const genesisHistory = releasedGenesisHistory(genesis, worldId);
|
|
63
|
+
const releasedAuthority = await readReleasedAuthority(worldRoot);
|
|
64
|
+
const currentCommits = convertReleasedCommits(releasedAuthority.commits);
|
|
65
|
+
await publishJson(join(runtimeRoot, "play-genesis-timeline.json"), {
|
|
66
|
+
schemaVersion: 1,
|
|
67
|
+
worldId,
|
|
68
|
+
history: genesisHistory,
|
|
69
|
+
});
|
|
70
|
+
const callChainSource = await readOptionalText(join(runtimeRoot, releasedCallChainFile));
|
|
71
|
+
let callChainContexts = 0;
|
|
72
|
+
if (callChainSource !== null) {
|
|
73
|
+
const value = parseJson(callChainSource, "Released play call chain");
|
|
74
|
+
callChainContexts =
|
|
75
|
+
isRecord(value) && Array.isArray(value.previousContexts)
|
|
76
|
+
? value.previousContexts.length + 1
|
|
77
|
+
: 1;
|
|
78
|
+
await this.#timeline.migrateReleasedRecord(worldId, value, sha256Hex(callChainSource));
|
|
79
|
+
}
|
|
80
|
+
crashAtStorageMigrationEdge("after_timeline");
|
|
81
|
+
const migratedContexts = await this.#timeline.readAllContexts(worldId);
|
|
82
|
+
if (migratedContexts.length !== callChainContexts)
|
|
83
|
+
throw new Error("Released play timeline migration changed its context closure");
|
|
84
|
+
assertTimelineAuthorityRefs(migratedContexts, currentCommits);
|
|
85
|
+
for (const commit of currentCommits)
|
|
86
|
+
await publishImmutableJson(join(runtimeRoot, "play-commits", `${currentCommitDigest(commit)}.json`), commit);
|
|
87
|
+
const checkpoint = await migrateMaterializedCheckpoint(runtimeRoot, currentCommits);
|
|
88
|
+
await publishJson(join(runtimeRoot, "materialized-head.json"), checkpoint);
|
|
89
|
+
await mkdir(this.#operationsRoot, { recursive: true, mode: 0o700 });
|
|
90
|
+
for (const commit of currentCommits) {
|
|
91
|
+
const outcome = {
|
|
92
|
+
outcome: checkpoint.sequence >= commit.sequence
|
|
93
|
+
? "committed"
|
|
94
|
+
: "committed_materialization_pending",
|
|
95
|
+
worldId,
|
|
96
|
+
parentHead: commit.parentHead,
|
|
97
|
+
head: commit.head,
|
|
98
|
+
commitDigest: currentCommitDigest(commit),
|
|
99
|
+
historyAppend: commit.historyAppend.map(({ role, exactText }) => ({
|
|
100
|
+
role,
|
|
101
|
+
exactText,
|
|
102
|
+
})),
|
|
103
|
+
nextAdditionalMaterials: structuredClone(commit.nextAdditionalMaterials),
|
|
104
|
+
mode: commit.mode,
|
|
105
|
+
};
|
|
106
|
+
await publishJson(operationOutcomePath(this.#operationsRoot, commit.operationId), outcome);
|
|
107
|
+
}
|
|
108
|
+
crashAtStorageMigrationEdge("after_current_facts");
|
|
109
|
+
await verifyCurrentFacts(runtimeRoot, currentCommits, checkpoint);
|
|
110
|
+
crashAtStorageMigrationEdge("before_current_head");
|
|
111
|
+
const tip = currentCommits.at(-1);
|
|
112
|
+
const head = tip === undefined
|
|
113
|
+
? {
|
|
114
|
+
schemaVersion: 2,
|
|
115
|
+
head: "genesis",
|
|
116
|
+
sequence: 0,
|
|
117
|
+
commitDigest: null,
|
|
118
|
+
operationId: null,
|
|
119
|
+
}
|
|
120
|
+
: {
|
|
121
|
+
schemaVersion: 2,
|
|
122
|
+
head: tip.head,
|
|
123
|
+
sequence: tip.sequence,
|
|
124
|
+
commitDigest: currentCommitDigest(tip),
|
|
125
|
+
operationId: tip.operationId,
|
|
126
|
+
};
|
|
127
|
+
await publishJson(join(runtimeRoot, currentAuthorityHeadFile), head);
|
|
128
|
+
crashAtStorageMigrationEdge("after_current_head");
|
|
129
|
+
return this.#migrateCurrentAuthority({
|
|
130
|
+
worldId,
|
|
131
|
+
worldRoot,
|
|
132
|
+
commits: currentCommits,
|
|
133
|
+
checkpoint,
|
|
134
|
+
callChainContexts,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async #migrateCurrentAuthority(input) {
|
|
138
|
+
return migrateCurrentAuthorityToV3({
|
|
139
|
+
...input,
|
|
140
|
+
operationsRoot: this.#operationsRoot,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function migrateCurrentAuthorityToV3(input) {
|
|
145
|
+
const runtimeRoot = join(input.worldRoot, "runtime");
|
|
146
|
+
const genesis = await readCurrentGenesis(input.worldRoot, input.worldId);
|
|
147
|
+
const normalized = normalizeAuthorityIdentity(genesis.history, input.commits);
|
|
148
|
+
const stagingWorld = join(runtimeRoot, ".authority-v3-migration");
|
|
149
|
+
const stagedRuntime = join(stagingWorld, "runtime");
|
|
150
|
+
const targetAuthority = join(runtimeRoot, authorityV3Directory);
|
|
151
|
+
await rm(stagingWorld, { recursive: true, force: true });
|
|
152
|
+
const staged = new FileNativeAuthorityV3(stagingWorld);
|
|
153
|
+
try {
|
|
154
|
+
const genesisMaterials = normalizeMaterials(genesis.additionalMaterials, normalized.messageIds, input.worldId);
|
|
155
|
+
await staged.initialize({
|
|
156
|
+
operationId: genesis.operationId,
|
|
157
|
+
state: genesis.state,
|
|
158
|
+
history: normalized.genesisHistory,
|
|
159
|
+
additionalMaterials: genesisMaterials,
|
|
160
|
+
});
|
|
161
|
+
const expectedEndpoints = new Map([
|
|
162
|
+
[
|
|
163
|
+
"genesis",
|
|
164
|
+
{
|
|
165
|
+
state: structuredClone(genesis.state),
|
|
166
|
+
history: structuredClone(normalized.genesisHistory),
|
|
167
|
+
additionalMaterials: structuredClone(genesisMaterials),
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
]);
|
|
171
|
+
const migrated = [];
|
|
172
|
+
for (const source of input.commits) {
|
|
173
|
+
const historyAppend = source.historyAppend.map((message) => normalizeHistoryMessage(message, normalized.messageIds));
|
|
174
|
+
const nextMaterials = normalizeMaterials(source.nextAdditionalMaterials, normalized.messageIds, input.worldId);
|
|
175
|
+
const basisHead = source.timelineRevision?.restoresHead ?? source.parentHead;
|
|
176
|
+
const basis = expectedEndpoints.get(basisHead);
|
|
177
|
+
if (basis === undefined)
|
|
178
|
+
throw new Error("Released Authority transition references an unknown result basis");
|
|
179
|
+
if (source.timelineRevision !== undefined) {
|
|
180
|
+
const restored = await staged.recover(source.timelineRevision.restoresHead);
|
|
181
|
+
const expectedHistory = source.timelineRevision.replacementHistory.map((message) => normalizeHistoryMessage(message, normalized.messageIds));
|
|
182
|
+
if (!isDeepStrictEqual(canonicalState(restored.state), canonicalLegacyState(source.timelineRevision.replacementState)) ||
|
|
183
|
+
!isDeepStrictEqual(restored.history, expectedHistory) ||
|
|
184
|
+
!isDeepStrictEqual(restored.additionalMaterials, nextMaterials))
|
|
185
|
+
throw new Error("Released timeline revision does not exactly restore its logical parent");
|
|
186
|
+
}
|
|
187
|
+
const prepared = await staged.prepareAppend({
|
|
188
|
+
operationId: source.operationId,
|
|
189
|
+
parentHead: source.parentHead,
|
|
190
|
+
...(source.timelineRevision === undefined
|
|
191
|
+
? {}
|
|
192
|
+
: { timelineParentHead: source.timelineRevision.restoresHead }),
|
|
193
|
+
mode: source.mode,
|
|
194
|
+
historyAppend: historyAppend.map(({ role, exactText }) => ({
|
|
195
|
+
role,
|
|
196
|
+
exactText,
|
|
197
|
+
})),
|
|
198
|
+
stateChanges: source.stateChanges,
|
|
199
|
+
nextMaterials,
|
|
200
|
+
...(source.correctionTargets === undefined
|
|
201
|
+
? {}
|
|
202
|
+
: { correctionTargets: source.correctionTargets }),
|
|
203
|
+
...(source.corrects === undefined ? {} : { corrects: source.corrects }),
|
|
204
|
+
...(source.timelineRevision === undefined
|
|
205
|
+
? {}
|
|
206
|
+
: {
|
|
207
|
+
timelineRevision: {
|
|
208
|
+
restoresHead: source.timelineRevision.restoresHead,
|
|
209
|
+
replacesHead: source.timelineRevision.replacesHead,
|
|
210
|
+
requestFingerprint: source.timelineRevision.requestFingerprint,
|
|
211
|
+
},
|
|
212
|
+
}),
|
|
213
|
+
});
|
|
214
|
+
if (!isDeepStrictEqual(prepared.commit.historyAppend, historyAppend) ||
|
|
215
|
+
prepared.commit.head !== source.head)
|
|
216
|
+
throw new Error("Migrated Authority fact changed released semantics");
|
|
217
|
+
await staged.publishPrepared(prepared);
|
|
218
|
+
const expected = applyReleasedTransition(basis, source, historyAppend, nextMaterials);
|
|
219
|
+
const recovered = await staged.recover(source.head);
|
|
220
|
+
if (!isDeepStrictEqual(canonicalState(recovered.state), canonicalState(expected.state)) ||
|
|
221
|
+
!isDeepStrictEqual(recovered.history, expected.history) ||
|
|
222
|
+
!isDeepStrictEqual(recovered.additionalMaterials, expected.additionalMaterials))
|
|
223
|
+
throw new Error(`Migrated Authority endpoint changed released semantics: ${source.head}`);
|
|
224
|
+
expectedEndpoints.set(source.head, expected);
|
|
225
|
+
migrated.push({
|
|
226
|
+
source,
|
|
227
|
+
fact: prepared.commit,
|
|
228
|
+
digest: prepared.commitDigest,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const finalHead = await staged.readHead();
|
|
232
|
+
if (finalHead.sequence !== input.commits.length ||
|
|
233
|
+
finalHead.head !== (input.commits.at(-1)?.head ?? "genesis"))
|
|
234
|
+
throw new Error("Migrated Authority head does not match released history");
|
|
235
|
+
await staged.readHistory();
|
|
236
|
+
const checkpointFact = input.checkpoint.sequence === 0
|
|
237
|
+
? null
|
|
238
|
+
: migrated[input.checkpoint.sequence - 1];
|
|
239
|
+
if ((input.checkpoint.sequence === 0 &&
|
|
240
|
+
input.checkpoint.head !== "genesis") ||
|
|
241
|
+
(input.checkpoint.sequence > 0 &&
|
|
242
|
+
checkpointFact?.fact.head !== input.checkpoint.head))
|
|
243
|
+
throw new Error("Released materialized checkpoint is not in migrated Authority");
|
|
244
|
+
const checkpointEndpoint = await staged.recover(input.checkpoint.head);
|
|
245
|
+
await rm(targetAuthority, { recursive: true, force: true });
|
|
246
|
+
await rename(join(stagedRuntime, authorityV3Directory), targetAuthority);
|
|
247
|
+
await syncDirectory(runtimeRoot);
|
|
248
|
+
await publishJson(join(runtimeRoot, "play-genesis-timeline.json"), {
|
|
249
|
+
schemaVersion: 1,
|
|
250
|
+
worldId: input.worldId,
|
|
251
|
+
history: normalized.genesisHistory,
|
|
252
|
+
});
|
|
253
|
+
// V2 filenames hash world-scoped message IDs. Rebuild this derived surface
|
|
254
|
+
// from the normalized checkpoint before publishing the V3 checkpoint/head.
|
|
255
|
+
await replaceMaterializedHistory(join(input.worldRoot, "history"), projectFileNativeHistorySurface(checkpointEndpoint.history, (messageId) => {
|
|
256
|
+
throw new Error(`Migrated history-message identity cannot form a materialization path: ${messageId}`);
|
|
257
|
+
}));
|
|
258
|
+
await publishJson(join(runtimeRoot, "additional-materials.json"), {
|
|
259
|
+
head: input.checkpoint.head,
|
|
260
|
+
items: checkpointEndpoint.additionalMaterials,
|
|
261
|
+
});
|
|
262
|
+
await publishJson(join(runtimeRoot, "materialized-head.json"), {
|
|
263
|
+
schemaVersion: 3,
|
|
264
|
+
head: input.checkpoint.head,
|
|
265
|
+
sequence: input.checkpoint.sequence,
|
|
266
|
+
commitDigest: checkpointFact?.digest ?? null,
|
|
267
|
+
});
|
|
268
|
+
await mkdir(input.operationsRoot, { recursive: true, mode: 0o700 });
|
|
269
|
+
for (const { source, fact, digest } of migrated) {
|
|
270
|
+
await publishJson(operationOutcomePath(input.operationsRoot, source.operationId), {
|
|
271
|
+
outcome: input.checkpoint.sequence >= fact.sequence
|
|
272
|
+
? "committed"
|
|
273
|
+
: "committed_materialization_pending",
|
|
274
|
+
worldId: input.worldId,
|
|
275
|
+
parentHead: fact.auditParent.head,
|
|
276
|
+
head: fact.head,
|
|
277
|
+
commitDigest: digest,
|
|
278
|
+
historyAppend: fact.historyAppend.map(({ role, exactText }) => ({
|
|
279
|
+
role,
|
|
280
|
+
exactText,
|
|
281
|
+
})),
|
|
282
|
+
nextAdditionalMaterials: fact.nextAdditionalMaterials,
|
|
283
|
+
mode: fact.mode,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
await rm(stagingWorld, { recursive: true, force: true });
|
|
287
|
+
crashAtStorageMigrationEdge("before_authority_v3_head");
|
|
288
|
+
await publishJson(join(runtimeRoot, continuityHeadFile), finalHead);
|
|
289
|
+
crashAtStorageMigrationEdge("after_authority_v3_head");
|
|
290
|
+
return {
|
|
291
|
+
outcome: "migrated",
|
|
292
|
+
authorityCommits: migrated.length,
|
|
293
|
+
callChainContexts: input.callChainContexts,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
finally {
|
|
297
|
+
await rm(stagingWorld, { recursive: true, force: true }).catch(() => undefined);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function applyReleasedTransition(basis, source, historyAppend, nextMaterials) {
|
|
301
|
+
const state = new Map(basis.state.map(({ path, contents }) => [path, contents]));
|
|
302
|
+
for (const change of source.stateChanges) {
|
|
303
|
+
const previous = state.get(change.relativePath);
|
|
304
|
+
if ((previous === undefined ? null : `sha256:${sha256Hex(previous)}`) !==
|
|
305
|
+
change.expectedPreviousHash ||
|
|
306
|
+
`sha256:${sha256Hex(change.canonicalNextBytes)}` !== change.nextHash)
|
|
307
|
+
throw new Error(`Released state transition hash conflicts: ${change.relativePath}`);
|
|
308
|
+
state.set(change.relativePath, change.canonicalNextBytes);
|
|
309
|
+
}
|
|
310
|
+
return {
|
|
311
|
+
state: [...state].map(([path, contents]) => ({ path, contents })),
|
|
312
|
+
history: [
|
|
313
|
+
...structuredClone(basis.history),
|
|
314
|
+
...structuredClone(historyAppend),
|
|
315
|
+
],
|
|
316
|
+
additionalMaterials: structuredClone([...nextMaterials]),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function assertTimelineAuthorityRefs(contexts, commits) {
|
|
320
|
+
const heads = new Set(["genesis", ...commits.map(({ head }) => head)]);
|
|
321
|
+
for (const context of contexts) {
|
|
322
|
+
if (!heads.has(context.baselineHead) || !heads.has(context.parentHead))
|
|
323
|
+
throw new Error("Released play timeline references an unknown Authority endpoint");
|
|
324
|
+
for (const event of context.events) {
|
|
325
|
+
if ("committedHead" in event &&
|
|
326
|
+
event.committedHead !== undefined &&
|
|
327
|
+
!heads.has(event.committedHead))
|
|
328
|
+
throw new Error("Released play event references an unknown Authority endpoint");
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
async function readCurrentAuthority(worldRoot, head) {
|
|
333
|
+
const reverse = [];
|
|
334
|
+
let digest = head.commitDigest;
|
|
335
|
+
let expectedHead = head.head;
|
|
336
|
+
let expectedSequence = head.sequence;
|
|
337
|
+
while (digest !== null) {
|
|
338
|
+
const value = await readJson(join(worldRoot, "runtime", "play-commits", `${digest}.json`));
|
|
339
|
+
assertCurrentPlayCommit(value);
|
|
340
|
+
if (currentCommitDigest(value) !== digest ||
|
|
341
|
+
value.head !== expectedHead ||
|
|
342
|
+
value.sequence !== expectedSequence)
|
|
343
|
+
throw new Error("Current Authority endpoint does not match its commit");
|
|
344
|
+
reverse.push(value);
|
|
345
|
+
digest = value.parentCommitDigest;
|
|
346
|
+
expectedHead = value.parentHead;
|
|
347
|
+
expectedSequence -= 1;
|
|
348
|
+
}
|
|
349
|
+
if (expectedHead !== "genesis" || expectedSequence !== 0)
|
|
350
|
+
throw new Error("Current Authority chain does not terminate at genesis");
|
|
351
|
+
return reverse.reverse();
|
|
352
|
+
}
|
|
353
|
+
async function readCurrentGenesis(worldRoot, worldId) {
|
|
354
|
+
const value = await readJson(join(worldRoot, "runtime", "genesis.json"));
|
|
355
|
+
if (!isRecord(value) ||
|
|
356
|
+
value.schemaVersion !== 1 ||
|
|
357
|
+
value.type !== "file_native_genesis" ||
|
|
358
|
+
value.worldId !== worldId ||
|
|
359
|
+
(typeof value.operationId !== "string" && value.operationId !== null) ||
|
|
360
|
+
!Array.isArray(value.state) ||
|
|
361
|
+
!Array.isArray(value.history) ||
|
|
362
|
+
!value.history.every(isReleasedHistoryMessage) ||
|
|
363
|
+
!Array.isArray(value.additionalMaterials) ||
|
|
364
|
+
!value.additionalMaterials.every(isMaterialSelection))
|
|
365
|
+
throw new Error("Current world genesis has an invalid shape");
|
|
366
|
+
const state = value.state.map((file) => {
|
|
367
|
+
if (!isRecord(file) ||
|
|
368
|
+
typeof file.path !== "string" ||
|
|
369
|
+
!validRelativePath(file.path) ||
|
|
370
|
+
typeof file.sha256 !== "string" ||
|
|
371
|
+
typeof file.canonicalBytes !== "string" ||
|
|
372
|
+
`sha256:${sha256Hex(file.canonicalBytes)}` !== file.sha256)
|
|
373
|
+
throw new Error("Current world genesis state is corrupt");
|
|
374
|
+
return { path: file.path, contents: file.canonicalBytes };
|
|
375
|
+
});
|
|
376
|
+
return {
|
|
377
|
+
operationId: value.operationId,
|
|
378
|
+
state,
|
|
379
|
+
history: structuredClone(value.history),
|
|
380
|
+
additionalMaterials: structuredClone(value.additionalMaterials),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function normalizeAuthorityIdentity(genesis, commits) {
|
|
384
|
+
const messageIds = new Map();
|
|
385
|
+
const bind = (source, target) => {
|
|
386
|
+
if (messageIds.has(source) && messageIds.get(source) !== target)
|
|
387
|
+
throw new Error("Released Authority reuses one message identity");
|
|
388
|
+
messageIds.set(source, target);
|
|
389
|
+
messageIds.set(target, target);
|
|
390
|
+
};
|
|
391
|
+
for (const [index, message] of genesis.entries())
|
|
392
|
+
bind(message.messageId, index === 0 && message.role === "narrator"
|
|
393
|
+
? "message.genesis.narrator"
|
|
394
|
+
: `message.genesis.${index + 1}.${message.role}`);
|
|
395
|
+
for (const commit of commits)
|
|
396
|
+
for (const [index, message] of commit.historyAppend.entries())
|
|
397
|
+
bind(message.messageId, `message.${commit.sequence}.${index + 1}.${message.role}`);
|
|
398
|
+
return {
|
|
399
|
+
genesisHistory: genesis.map((message) => normalizeHistoryMessage(message, messageIds)),
|
|
400
|
+
messageIds,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function normalizeHistoryMessage(message, messageIds) {
|
|
404
|
+
const messageId = messageIds.get(message.messageId);
|
|
405
|
+
if (messageId === undefined)
|
|
406
|
+
throw new Error(`Released Authority references an unknown history message: ${message.messageId}`);
|
|
407
|
+
return { ...structuredClone(message), messageId };
|
|
408
|
+
}
|
|
409
|
+
function normalizeMaterials(materials, messageIds, worldId) {
|
|
410
|
+
return materials.map((material) => {
|
|
411
|
+
if (material.kind === "history_message") {
|
|
412
|
+
const message = messageIds.get(material.message);
|
|
413
|
+
if (message === undefined)
|
|
414
|
+
throw new Error(`Released material references an unknown history message: ${material.message}`);
|
|
415
|
+
return { ...material, message };
|
|
416
|
+
}
|
|
417
|
+
if (material.kind === "history_commit") {
|
|
418
|
+
const prefix = `${worldId}.`;
|
|
419
|
+
const commit = material.commit.startsWith(prefix)
|
|
420
|
+
? material.commit.slice(prefix.length)
|
|
421
|
+
: material.commit;
|
|
422
|
+
if (!/^(?:genesis|commit:[1-9][0-9]*)$/u.test(commit))
|
|
423
|
+
throw new Error(`Released material references a foreign Authority endpoint: ${material.commit}`);
|
|
424
|
+
return {
|
|
425
|
+
...material,
|
|
426
|
+
commit,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return structuredClone(material);
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
function canonicalState(files) {
|
|
433
|
+
return files
|
|
434
|
+
.map(({ path, contents }) => ({
|
|
435
|
+
path,
|
|
436
|
+
sha256: `sha256:${sha256Hex(contents)}`,
|
|
437
|
+
canonicalBytes: contents,
|
|
438
|
+
}))
|
|
439
|
+
.sort((left, right) => left.path.localeCompare(right.path));
|
|
440
|
+
}
|
|
441
|
+
function canonicalLegacyState(files) {
|
|
442
|
+
return [...structuredClone(files)].sort((left, right) => left.path.localeCompare(right.path));
|
|
443
|
+
}
|
|
444
|
+
async function readReleasedAuthority(worldRoot) {
|
|
445
|
+
const runtimeRoot = join(worldRoot, "runtime");
|
|
446
|
+
const published = await readOptionalJson(join(runtimeRoot, releasedAuthorityFile));
|
|
447
|
+
if (published !== null) {
|
|
448
|
+
assertReleasedAuthority(published);
|
|
449
|
+
for (const commit of published.commits) {
|
|
450
|
+
const digest = releasedCommitDigest(commit);
|
|
451
|
+
const immutable = await readJson(join(runtimeRoot, "play-commits", `${digest}.json`));
|
|
452
|
+
assertReleasedCommit(immutable);
|
|
453
|
+
if (!isDeepStrictEqual(immutable, commit))
|
|
454
|
+
throw new Error(`Released Authority endpoint does not match immutable commit ${commit.head}`);
|
|
455
|
+
}
|
|
456
|
+
return structuredClone(published);
|
|
457
|
+
}
|
|
458
|
+
const names = await readDirectoryFileNames(join(runtimeRoot, "play-commits"));
|
|
459
|
+
const remaining = [];
|
|
460
|
+
for (const name of names) {
|
|
461
|
+
const value = await readJson(join(runtimeRoot, "play-commits", name));
|
|
462
|
+
if (isRecord(value) && value.schemaVersion === 2)
|
|
463
|
+
continue;
|
|
464
|
+
assertReleasedCommit(value);
|
|
465
|
+
if (name !== `${releasedCommitDigest(value)}.json`)
|
|
466
|
+
throw new Error("Released immutable commit does not match its durable identity");
|
|
467
|
+
remaining.push(value);
|
|
468
|
+
}
|
|
469
|
+
if (remaining.length === 0)
|
|
470
|
+
return { schemaVersion: 1, head: "genesis", commits: [] };
|
|
471
|
+
const commits = [];
|
|
472
|
+
let parent = "genesis";
|
|
473
|
+
while (remaining.length > 0) {
|
|
474
|
+
const matches = remaining.filter((commit) => commit.parentHead === parent);
|
|
475
|
+
if (matches.length !== 1)
|
|
476
|
+
throw new Error("Released immutable commits cannot rebuild one unique Authority chain");
|
|
477
|
+
const next = matches[0];
|
|
478
|
+
commits.push(next);
|
|
479
|
+
remaining.splice(remaining.indexOf(next), 1);
|
|
480
|
+
parent = next.head;
|
|
481
|
+
}
|
|
482
|
+
const rebuilt = {
|
|
483
|
+
schemaVersion: 1,
|
|
484
|
+
head: parent,
|
|
485
|
+
commits,
|
|
486
|
+
};
|
|
487
|
+
assertReleasedAuthority(rebuilt);
|
|
488
|
+
return rebuilt;
|
|
489
|
+
}
|
|
490
|
+
function convertReleasedCommits(commits) {
|
|
491
|
+
const converted = [];
|
|
492
|
+
let parentCommitDigest = null;
|
|
493
|
+
for (const [index, source] of commits.entries()) {
|
|
494
|
+
const commit = {
|
|
495
|
+
schemaVersion: 2,
|
|
496
|
+
sequence: index + 1,
|
|
497
|
+
operationId: source.operationId,
|
|
498
|
+
parentHead: source.parentHead,
|
|
499
|
+
parentCommitDigest,
|
|
500
|
+
head: source.head,
|
|
501
|
+
mode: source.mode,
|
|
502
|
+
historyAppend: structuredClone(source.historyAppend),
|
|
503
|
+
stateChanges: structuredClone(source.stateChanges),
|
|
504
|
+
nextAdditionalMaterials: structuredClone(source.nextAdditionalMaterials),
|
|
505
|
+
...(source.correctionTargets === undefined
|
|
506
|
+
? {}
|
|
507
|
+
: { correctionTargets: structuredClone(source.correctionTargets) }),
|
|
508
|
+
...(source.corrects === undefined ? {} : { corrects: source.corrects }),
|
|
509
|
+
...(source.timelineRevision === undefined
|
|
510
|
+
? {}
|
|
511
|
+
: { timelineRevision: structuredClone(source.timelineRevision) }),
|
|
512
|
+
};
|
|
513
|
+
converted.push(commit);
|
|
514
|
+
parentCommitDigest = currentCommitDigest(commit);
|
|
515
|
+
}
|
|
516
|
+
return converted;
|
|
517
|
+
}
|
|
518
|
+
async function migrateMaterializedCheckpoint(runtimeRoot, commits) {
|
|
519
|
+
const value = await readOptionalJson(join(runtimeRoot, "materialized-head.json"));
|
|
520
|
+
if (value === null)
|
|
521
|
+
return {
|
|
522
|
+
schemaVersion: 2,
|
|
523
|
+
head: "genesis",
|
|
524
|
+
sequence: 0,
|
|
525
|
+
commitDigest: null,
|
|
526
|
+
};
|
|
527
|
+
if (isRecord(value) &&
|
|
528
|
+
hasExactKeys(value, [
|
|
529
|
+
"schemaVersion",
|
|
530
|
+
"head",
|
|
531
|
+
"sequence",
|
|
532
|
+
"commitDigest",
|
|
533
|
+
]) &&
|
|
534
|
+
value.schemaVersion === 3 &&
|
|
535
|
+
typeof value.head === "string" &&
|
|
536
|
+
Number.isSafeInteger(value.sequence) &&
|
|
537
|
+
Number(value.sequence) >= 0 &&
|
|
538
|
+
(value.commitDigest === null ||
|
|
539
|
+
(typeof value.commitDigest === "string" &&
|
|
540
|
+
/^[a-f0-9]{64}$/u.test(value.commitDigest)))) {
|
|
541
|
+
const sequence = Number(value.sequence);
|
|
542
|
+
const expected = sequence === 0 ? undefined : commits[sequence - 1];
|
|
543
|
+
if ((sequence === 0 &&
|
|
544
|
+
(value.head !== "genesis" || value.commitDigest !== null)) ||
|
|
545
|
+
(sequence > 0 && expected?.head !== value.head))
|
|
546
|
+
throw new Error("Unmarked V3 materialized checkpoint is not in released Authority");
|
|
547
|
+
return {
|
|
548
|
+
schemaVersion: 2,
|
|
549
|
+
head: value.head,
|
|
550
|
+
sequence,
|
|
551
|
+
commitDigest: expected === undefined ? null : currentCommitDigest(expected),
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
if (isCurrentMaterializedCheckpoint(value)) {
|
|
555
|
+
const expected = value.sequence === 0 ? undefined : commits[value.sequence - 1];
|
|
556
|
+
if ((value.sequence === 0 &&
|
|
557
|
+
(value.head !== "genesis" || value.commitDigest !== null)) ||
|
|
558
|
+
(value.sequence > 0 &&
|
|
559
|
+
(expected?.head !== value.head ||
|
|
560
|
+
currentCommitDigest(expected) !== value.commitDigest)))
|
|
561
|
+
throw new Error("Partially migrated materialized checkpoint is not in released Authority");
|
|
562
|
+
return structuredClone(value);
|
|
563
|
+
}
|
|
564
|
+
if (!isRecord(value) ||
|
|
565
|
+
!hasExactKeys(value, ["head"]) ||
|
|
566
|
+
typeof value.head !== "string")
|
|
567
|
+
throw new Error("Released materialized checkpoint has an invalid shape");
|
|
568
|
+
if (value.head === "genesis")
|
|
569
|
+
return {
|
|
570
|
+
schemaVersion: 2,
|
|
571
|
+
head: "genesis",
|
|
572
|
+
sequence: 0,
|
|
573
|
+
commitDigest: null,
|
|
574
|
+
};
|
|
575
|
+
const commit = commits.find(({ head }) => head === value.head);
|
|
576
|
+
if (commit === undefined)
|
|
577
|
+
throw new Error("Released materialized checkpoint is not in accepted Authority");
|
|
578
|
+
return {
|
|
579
|
+
schemaVersion: 2,
|
|
580
|
+
head: commit.head,
|
|
581
|
+
sequence: commit.sequence,
|
|
582
|
+
commitDigest: currentCommitDigest(commit),
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
async function verifyCurrentFacts(runtimeRoot, commits, checkpoint) {
|
|
586
|
+
let parentDigest = null;
|
|
587
|
+
for (const commit of commits) {
|
|
588
|
+
if (commit.parentCommitDigest !== parentDigest)
|
|
589
|
+
throw new Error("Migrated Authority parent digest is inconsistent");
|
|
590
|
+
const digest = currentCommitDigest(commit);
|
|
591
|
+
const durable = await readJson(join(runtimeRoot, "play-commits", `${digest}.json`));
|
|
592
|
+
if (!isDeepStrictEqual(durable, commit))
|
|
593
|
+
throw new Error("Migrated Authority commit verification failed");
|
|
594
|
+
parentDigest = digest;
|
|
595
|
+
}
|
|
596
|
+
const durableCheckpoint = await readJson(join(runtimeRoot, "materialized-head.json"));
|
|
597
|
+
if (!isDeepStrictEqual(durableCheckpoint, checkpoint))
|
|
598
|
+
throw new Error("Migrated materialized checkpoint verification failed");
|
|
599
|
+
}
|
|
600
|
+
function releasedGenesisHistory(value, worldId) {
|
|
601
|
+
if (!isRecord(value) ||
|
|
602
|
+
value.schemaVersion !== 1 ||
|
|
603
|
+
value.type !== "file_native_genesis" ||
|
|
604
|
+
value.worldId !== worldId ||
|
|
605
|
+
!Array.isArray(value.history) ||
|
|
606
|
+
!value.history.every(isReleasedHistoryMessage))
|
|
607
|
+
throw new Error("Released world genesis has an invalid identity");
|
|
608
|
+
return structuredClone(value.history);
|
|
609
|
+
}
|
|
610
|
+
function assertReleasedAuthority(value) {
|
|
611
|
+
if (!isRecord(value) ||
|
|
612
|
+
!hasExactKeys(value, ["schemaVersion", "head", "commits"]) ||
|
|
613
|
+
value.schemaVersion !== 1 ||
|
|
614
|
+
typeof value.head !== "string" ||
|
|
615
|
+
!Array.isArray(value.commits))
|
|
616
|
+
throw new Error("Released Play Authority has an invalid shape");
|
|
617
|
+
const operationIds = new Set();
|
|
618
|
+
const heads = new Set();
|
|
619
|
+
const commitsByHead = new Map();
|
|
620
|
+
let parent = "genesis";
|
|
621
|
+
for (const [index, commit] of value.commits.entries()) {
|
|
622
|
+
assertReleasedCommit(commit);
|
|
623
|
+
const revision = commit.timelineRevision;
|
|
624
|
+
const replaced = revision === undefined
|
|
625
|
+
? undefined
|
|
626
|
+
: commitsByHead.get(revision.replacesHead);
|
|
627
|
+
const replacedLogicalParent = replaced?.mode === "timeline_revision"
|
|
628
|
+
? replaced.timelineRevision.restoresHead
|
|
629
|
+
: replaced?.parentHead;
|
|
630
|
+
if (commit.parentHead !== parent ||
|
|
631
|
+
commit.head !== `commit:${index + 1}` ||
|
|
632
|
+
operationIds.has(commit.operationId) ||
|
|
633
|
+
heads.has(commit.head) ||
|
|
634
|
+
(commit.mode === "timeline_revision" &&
|
|
635
|
+
((revision.restoresHead !== "genesis" &&
|
|
636
|
+
!heads.has(revision.restoresHead)) ||
|
|
637
|
+
replaced === undefined ||
|
|
638
|
+
!replaced.historyAppend.some(({ role }) => role === "player") ||
|
|
639
|
+
replacedLogicalParent !== revision.restoresHead)))
|
|
640
|
+
throw new Error("Released Play Authority commit chain is invalid");
|
|
641
|
+
operationIds.add(commit.operationId);
|
|
642
|
+
heads.add(commit.head);
|
|
643
|
+
commitsByHead.set(commit.head, commit);
|
|
644
|
+
parent = commit.head;
|
|
645
|
+
}
|
|
646
|
+
if (value.head !== parent)
|
|
647
|
+
throw new Error("Released Play Authority head is invalid");
|
|
648
|
+
}
|
|
649
|
+
function assertReleasedCommit(value) {
|
|
650
|
+
if (!isRecord(value))
|
|
651
|
+
throw new Error("Released play commit has an invalid shape");
|
|
652
|
+
const correction = value.mode === "correction";
|
|
653
|
+
const timelineRevision = value.mode === "timeline_revision";
|
|
654
|
+
if (!hasExactKeys(value, [
|
|
655
|
+
"schemaVersion",
|
|
656
|
+
"operationId",
|
|
657
|
+
"parentHead",
|
|
658
|
+
"head",
|
|
659
|
+
"mode",
|
|
660
|
+
"historyAppend",
|
|
661
|
+
"stateChanges",
|
|
662
|
+
"nextAdditionalMaterials",
|
|
663
|
+
...(correction ? ["correctionTargets", "corrects"] : []),
|
|
664
|
+
...(timelineRevision ? ["timelineRevision"] : []),
|
|
665
|
+
]) ||
|
|
666
|
+
value.schemaVersion !== 1 ||
|
|
667
|
+
typeof value.operationId !== "string" ||
|
|
668
|
+
value.operationId.trim() === "" ||
|
|
669
|
+
typeof value.parentHead !== "string" ||
|
|
670
|
+
!/^(?:genesis|commit:[1-9][0-9]*)$/u.test(value.parentHead) ||
|
|
671
|
+
typeof value.head !== "string" ||
|
|
672
|
+
!/^commit:[1-9][0-9]*$/u.test(value.head) ||
|
|
673
|
+
(value.mode !== "play" &&
|
|
674
|
+
value.mode !== "correction" &&
|
|
675
|
+
value.mode !== "timeline_revision") ||
|
|
676
|
+
!Array.isArray(value.historyAppend) ||
|
|
677
|
+
(correction
|
|
678
|
+
? value.historyAppend.length !== 0
|
|
679
|
+
: timelineRevision
|
|
680
|
+
? value.historyAppend.length !== 1 ||
|
|
681
|
+
!isRecord(value.historyAppend[0]) ||
|
|
682
|
+
value.historyAppend[0].role !== "player"
|
|
683
|
+
: value.historyAppend.length === 0 &&
|
|
684
|
+
(!Array.isArray(value.stateChanges) ||
|
|
685
|
+
value.stateChanges.length === 0)) ||
|
|
686
|
+
!value.historyAppend.every(isReleasedHistoryMessage) ||
|
|
687
|
+
!Array.isArray(value.stateChanges) ||
|
|
688
|
+
!value.stateChanges.every(isReleasedStateChange) ||
|
|
689
|
+
!Array.isArray(value.nextAdditionalMaterials) ||
|
|
690
|
+
!value.nextAdditionalMaterials.every(isMaterialSelection))
|
|
691
|
+
throw new Error("Released play commit has an invalid shape");
|
|
692
|
+
if (correction &&
|
|
693
|
+
(!Array.isArray(value.correctionTargets) ||
|
|
694
|
+
!value.correctionTargets.every((target) => typeof target === "string" && target.trim() !== "") ||
|
|
695
|
+
typeof value.corrects !== "string" ||
|
|
696
|
+
value.corrects !== value.parentHead))
|
|
697
|
+
throw new Error("Released correction commit has an invalid shape");
|
|
698
|
+
if (timelineRevision &&
|
|
699
|
+
(value.stateChanges.length !== 0 ||
|
|
700
|
+
!isReleasedTimelineRevision(value.timelineRevision)))
|
|
701
|
+
throw new Error("Released timeline-revision commit has an invalid shape");
|
|
702
|
+
}
|
|
703
|
+
function isReleasedTimelineRevision(value) {
|
|
704
|
+
if (!isRecord(value) ||
|
|
705
|
+
!hasExactKeys(value, [
|
|
706
|
+
"restoresHead",
|
|
707
|
+
"replacesHead",
|
|
708
|
+
"requestFingerprint",
|
|
709
|
+
"replacementState",
|
|
710
|
+
"replacementHistory",
|
|
711
|
+
]) ||
|
|
712
|
+
typeof value.restoresHead !== "string" ||
|
|
713
|
+
!/^(?:genesis|commit:[1-9][0-9]*)$/u.test(value.restoresHead) ||
|
|
714
|
+
typeof value.replacesHead !== "string" ||
|
|
715
|
+
!/^commit:[1-9][0-9]*$/u.test(value.replacesHead) ||
|
|
716
|
+
typeof value.requestFingerprint !== "string" ||
|
|
717
|
+
!/^sha256:[a-f0-9]{64}$/u.test(value.requestFingerprint) ||
|
|
718
|
+
!Array.isArray(value.replacementState) ||
|
|
719
|
+
!Array.isArray(value.replacementHistory) ||
|
|
720
|
+
!value.replacementHistory.every(isReleasedHistoryMessage))
|
|
721
|
+
return false;
|
|
722
|
+
return value.replacementState.every((file) => isRecord(file) &&
|
|
723
|
+
hasExactKeys(file, ["path", "sha256", "canonicalBytes"]) &&
|
|
724
|
+
typeof file.path === "string" &&
|
|
725
|
+
validRelativePath(file.path) &&
|
|
726
|
+
typeof file.sha256 === "string" &&
|
|
727
|
+
typeof file.canonicalBytes === "string" &&
|
|
728
|
+
`sha256:${sha256Hex(file.canonicalBytes)}` === file.sha256);
|
|
729
|
+
}
|
|
730
|
+
function isReleasedHistoryMessage(value) {
|
|
731
|
+
return (isRecord(value) &&
|
|
732
|
+
hasExactKeys(value, ["messageId", "role", "exactText"]) &&
|
|
733
|
+
typeof value.messageId === "string" &&
|
|
734
|
+
value.messageId.trim() !== "" &&
|
|
735
|
+
(value.role === "player" || value.role === "narrator") &&
|
|
736
|
+
typeof value.exactText === "string");
|
|
737
|
+
}
|
|
738
|
+
function isReleasedStateChange(value) {
|
|
739
|
+
return (isRecord(value) &&
|
|
740
|
+
hasExactKeys(value, [
|
|
741
|
+
"kind",
|
|
742
|
+
"documentId",
|
|
743
|
+
"stableShortRef",
|
|
744
|
+
"relativePath",
|
|
745
|
+
"codec",
|
|
746
|
+
"expectedPreviousHash",
|
|
747
|
+
"nextHash",
|
|
748
|
+
"canonicalNextBytes",
|
|
749
|
+
]) &&
|
|
750
|
+
(value.kind === "create" || value.kind === "replace") &&
|
|
751
|
+
typeof value.documentId === "string" &&
|
|
752
|
+
value.documentId.trim() !== "" &&
|
|
753
|
+
typeof value.stableShortRef === "string" &&
|
|
754
|
+
value.stableShortRef.trim() !== "" &&
|
|
755
|
+
typeof value.relativePath === "string" &&
|
|
756
|
+
validRelativePath(value.relativePath) &&
|
|
757
|
+
(value.codec === "yaml" || value.codec === "markdown") &&
|
|
758
|
+
(value.expectedPreviousHash === null ||
|
|
759
|
+
(typeof value.expectedPreviousHash === "string" &&
|
|
760
|
+
/^sha256:[a-f0-9]{64}$/u.test(value.expectedPreviousHash))) &&
|
|
761
|
+
typeof value.nextHash === "string" &&
|
|
762
|
+
/^sha256:[a-f0-9]{64}$/u.test(value.nextHash) &&
|
|
763
|
+
typeof value.canonicalNextBytes === "string" &&
|
|
764
|
+
`sha256:${sha256Hex(value.canonicalNextBytes)}` === value.nextHash);
|
|
765
|
+
}
|
|
766
|
+
function isMaterialSelection(value) {
|
|
767
|
+
if (!isRecord(value))
|
|
768
|
+
return false;
|
|
769
|
+
if (value.kind === "document")
|
|
770
|
+
return (hasExactKeys(value, ["kind", "document"]) &&
|
|
771
|
+
typeof value.document === "string");
|
|
772
|
+
if (value.kind === "node")
|
|
773
|
+
return (hasExactKeys(value, ["kind", "document", "locator"]) &&
|
|
774
|
+
typeof value.document === "string" &&
|
|
775
|
+
isMaterialLocator(value.locator));
|
|
776
|
+
if (value.kind === "history_message")
|
|
777
|
+
return (hasExactKeys(value, ["kind", "message"]) &&
|
|
778
|
+
typeof value.message === "string");
|
|
779
|
+
return (value.kind === "history_commit" &&
|
|
780
|
+
hasExactKeys(value, ["kind", "commit"]) &&
|
|
781
|
+
typeof value.commit === "string");
|
|
782
|
+
}
|
|
783
|
+
function isMaterialLocator(value) {
|
|
784
|
+
return (isRecord(value) &&
|
|
785
|
+
hasRequiredAndOptionalKeys(value, [], ["yaml", "markdown"]) &&
|
|
786
|
+
(value.yaml === undefined ||
|
|
787
|
+
(Array.isArray(value.yaml) &&
|
|
788
|
+
value.yaml.every((segment) => typeof segment === "string" || Number.isSafeInteger(segment)))) &&
|
|
789
|
+
(value.markdown === undefined ||
|
|
790
|
+
(Array.isArray(value.markdown) &&
|
|
791
|
+
value.markdown.every((segment) => typeof segment === "string"))));
|
|
792
|
+
}
|
|
793
|
+
function assertCurrentAuthorityHead(value) {
|
|
794
|
+
if (!isRecord(value) ||
|
|
795
|
+
!hasExactKeys(value, [
|
|
796
|
+
"schemaVersion",
|
|
797
|
+
"head",
|
|
798
|
+
"sequence",
|
|
799
|
+
"commitDigest",
|
|
800
|
+
"operationId",
|
|
801
|
+
]) ||
|
|
802
|
+
value.schemaVersion !== 2 ||
|
|
803
|
+
typeof value.head !== "string" ||
|
|
804
|
+
!Number.isSafeInteger(value.sequence) ||
|
|
805
|
+
Number(value.sequence) < 0 ||
|
|
806
|
+
(value.commitDigest !== null &&
|
|
807
|
+
(typeof value.commitDigest !== "string" ||
|
|
808
|
+
!/^[a-f0-9]{64}$/u.test(value.commitDigest))) ||
|
|
809
|
+
(value.operationId !== null && typeof value.operationId !== "string") ||
|
|
810
|
+
(Number(value.sequence) === 0
|
|
811
|
+
? value.head !== "genesis" ||
|
|
812
|
+
value.commitDigest !== null ||
|
|
813
|
+
value.operationId !== null
|
|
814
|
+
: value.head !== `commit:${String(value.sequence)}` ||
|
|
815
|
+
value.commitDigest === null ||
|
|
816
|
+
value.operationId === null))
|
|
817
|
+
throw new Error("Current Authority head has an invalid shape");
|
|
818
|
+
}
|
|
819
|
+
function assertCurrentPlayCommit(value) {
|
|
820
|
+
if (!isRecord(value))
|
|
821
|
+
throw new Error("Current play commit has an invalid shape");
|
|
822
|
+
const correction = value.mode === "correction";
|
|
823
|
+
const timelineRevision = value.mode === "timeline_revision";
|
|
824
|
+
if (!hasExactKeys(value, [
|
|
825
|
+
"schemaVersion",
|
|
826
|
+
"sequence",
|
|
827
|
+
"operationId",
|
|
828
|
+
"parentHead",
|
|
829
|
+
"parentCommitDigest",
|
|
830
|
+
"head",
|
|
831
|
+
"mode",
|
|
832
|
+
"historyAppend",
|
|
833
|
+
"stateChanges",
|
|
834
|
+
"nextAdditionalMaterials",
|
|
835
|
+
...(correction ? ["correctionTargets", "corrects"] : []),
|
|
836
|
+
...(timelineRevision ? ["timelineRevision"] : []),
|
|
837
|
+
]) ||
|
|
838
|
+
value.schemaVersion !== 2 ||
|
|
839
|
+
!Number.isSafeInteger(value.sequence) ||
|
|
840
|
+
Number(value.sequence) < 1 ||
|
|
841
|
+
typeof value.operationId !== "string" ||
|
|
842
|
+
value.operationId.trim() === "" ||
|
|
843
|
+
typeof value.parentHead !== "string" ||
|
|
844
|
+
!/^(?:genesis|commit:[1-9][0-9]*)$/u.test(value.parentHead) ||
|
|
845
|
+
(value.parentCommitDigest !== null &&
|
|
846
|
+
(typeof value.parentCommitDigest !== "string" ||
|
|
847
|
+
!/^[a-f0-9]{64}$/u.test(value.parentCommitDigest))) ||
|
|
848
|
+
typeof value.head !== "string" ||
|
|
849
|
+
value.head !== `commit:${String(value.sequence)}` ||
|
|
850
|
+
(value.mode !== "play" &&
|
|
851
|
+
value.mode !== "correction" &&
|
|
852
|
+
value.mode !== "timeline_revision") ||
|
|
853
|
+
!Array.isArray(value.historyAppend) ||
|
|
854
|
+
!value.historyAppend.every(isReleasedHistoryMessage) ||
|
|
855
|
+
!Array.isArray(value.stateChanges) ||
|
|
856
|
+
!value.stateChanges.every(isReleasedStateChange) ||
|
|
857
|
+
!Array.isArray(value.nextAdditionalMaterials) ||
|
|
858
|
+
!value.nextAdditionalMaterials.every(isMaterialSelection))
|
|
859
|
+
throw new Error("Current play commit has an invalid shape");
|
|
860
|
+
if (correction &&
|
|
861
|
+
(!Array.isArray(value.correctionTargets) ||
|
|
862
|
+
!value.correctionTargets.every((target) => typeof target === "string" && target.trim() !== "") ||
|
|
863
|
+
typeof value.corrects !== "string" ||
|
|
864
|
+
value.corrects !== value.parentHead))
|
|
865
|
+
throw new Error("Current correction commit has an invalid shape");
|
|
866
|
+
if (timelineRevision &&
|
|
867
|
+
(value.stateChanges.length !== 0 ||
|
|
868
|
+
!isReleasedTimelineRevision(value.timelineRevision)))
|
|
869
|
+
throw new Error("Current timeline revision has an invalid shape");
|
|
870
|
+
}
|
|
871
|
+
function isCurrentMaterializedCheckpoint(value) {
|
|
872
|
+
return (isRecord(value) &&
|
|
873
|
+
hasExactKeys(value, [
|
|
874
|
+
"schemaVersion",
|
|
875
|
+
"head",
|
|
876
|
+
"sequence",
|
|
877
|
+
"commitDigest",
|
|
878
|
+
]) &&
|
|
879
|
+
value.schemaVersion === 2 &&
|
|
880
|
+
typeof value.head === "string" &&
|
|
881
|
+
Number.isSafeInteger(value.sequence) &&
|
|
882
|
+
Number(value.sequence) >= 0 &&
|
|
883
|
+
(value.commitDigest === null ||
|
|
884
|
+
(typeof value.commitDigest === "string" &&
|
|
885
|
+
/^[a-f0-9]{64}$/u.test(value.commitDigest))));
|
|
886
|
+
}
|
|
887
|
+
function currentCommitDigest(commit) {
|
|
888
|
+
return sha256Hex(JSON.stringify(commit));
|
|
889
|
+
}
|
|
890
|
+
function releasedCommitDigest(commit) {
|
|
891
|
+
return sha256Hex(JSON.stringify(commit));
|
|
892
|
+
}
|
|
893
|
+
function operationOutcomePath(root, operationId) {
|
|
894
|
+
return join(root, `outcome-${sha256Hex(operationId)}.json`);
|
|
895
|
+
}
|
|
896
|
+
function validRelativePath(path) {
|
|
897
|
+
return (path.length > 0 &&
|
|
898
|
+
!path.startsWith("/") &&
|
|
899
|
+
!path.includes("\\") &&
|
|
900
|
+
!path
|
|
901
|
+
.split("/")
|
|
902
|
+
.some((part) => part === "" || part === "." || part === ".."));
|
|
903
|
+
}
|
|
904
|
+
function assertIdentity(value, label) {
|
|
905
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/u.test(value))
|
|
906
|
+
throw new TypeError(`${label} is invalid`);
|
|
907
|
+
}
|
|
908
|
+
function isRecord(value) {
|
|
909
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
910
|
+
}
|
|
911
|
+
function hasExactKeys(value, keys) {
|
|
912
|
+
return (Object.keys(value).length === keys.length &&
|
|
913
|
+
keys.every((key) => Object.hasOwn(value, key)));
|
|
914
|
+
}
|
|
915
|
+
function hasRequiredAndOptionalKeys(value, required, optional) {
|
|
916
|
+
const allowed = new Set([...required, ...optional]);
|
|
917
|
+
return (required.every((key) => Object.hasOwn(value, key)) &&
|
|
918
|
+
Object.keys(value).every((key) => allowed.has(key)));
|
|
919
|
+
}
|
|
920
|
+
function parseJson(source, label) {
|
|
921
|
+
try {
|
|
922
|
+
return JSON.parse(source);
|
|
923
|
+
}
|
|
924
|
+
catch (error) {
|
|
925
|
+
throw new Error(`${label} is not valid JSON`, { cause: error });
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
function sha256Hex(value) {
|
|
929
|
+
return createHash("sha256").update(value).digest("hex");
|
|
930
|
+
}
|
|
931
|
+
function isNodeError(value) {
|
|
932
|
+
return value instanceof Error && "code" in value;
|
|
933
|
+
}
|
|
934
|
+
async function readJson(path) {
|
|
935
|
+
return parseJson(await readFile(path, "utf8"), path);
|
|
936
|
+
}
|
|
937
|
+
async function readOptionalJson(path) {
|
|
938
|
+
try {
|
|
939
|
+
return await readJson(path);
|
|
940
|
+
}
|
|
941
|
+
catch (error) {
|
|
942
|
+
if (isNodeError(error) && error.code === "ENOENT")
|
|
943
|
+
return null;
|
|
944
|
+
throw error;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
async function readOptionalText(path) {
|
|
948
|
+
try {
|
|
949
|
+
return await readFile(path, "utf8");
|
|
950
|
+
}
|
|
951
|
+
catch (error) {
|
|
952
|
+
if (isNodeError(error) && error.code === "ENOENT")
|
|
953
|
+
return null;
|
|
954
|
+
throw error;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
async function readDirectoryFileNames(root) {
|
|
958
|
+
try {
|
|
959
|
+
return (await readdir(root, { withFileTypes: true }))
|
|
960
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
|
|
961
|
+
.map(({ name }) => name)
|
|
962
|
+
.sort();
|
|
963
|
+
}
|
|
964
|
+
catch (error) {
|
|
965
|
+
if (isNodeError(error) && error.code === "ENOENT")
|
|
966
|
+
return [];
|
|
967
|
+
throw error;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
async function replaceMaterializedHistory(root, files) {
|
|
971
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
972
|
+
const entries = await readdir(root, { withFileTypes: true });
|
|
973
|
+
if (entries.some((entry) => !entry.isFile()))
|
|
974
|
+
throw new Error("Materialized history projection must be a flat file tree");
|
|
975
|
+
const current = new Map(await Promise.all(entries.map(async (entry) => [entry.name, await readFile(join(root, entry.name), "utf8")])));
|
|
976
|
+
const expected = new Set();
|
|
977
|
+
for (const file of files) {
|
|
978
|
+
if (!validRelativePath(file.path) || file.path.includes("/"))
|
|
979
|
+
throw new Error(`Migrated history projection has an invalid path: ${file.path}`);
|
|
980
|
+
if (expected.has(file.path))
|
|
981
|
+
throw new Error(`Migrated history projection has a duplicate path: ${file.path}`);
|
|
982
|
+
expected.add(file.path);
|
|
983
|
+
if (current.get(file.path) === file.contents)
|
|
984
|
+
continue;
|
|
985
|
+
const temporary = join(root, `${file.path}.${randomUUID()}.tmp`);
|
|
986
|
+
await writeFile(temporary, file.contents, {
|
|
987
|
+
encoding: "utf8",
|
|
988
|
+
mode: 0o600,
|
|
989
|
+
});
|
|
990
|
+
await syncFile(temporary);
|
|
991
|
+
await rename(temporary, join(root, file.path));
|
|
992
|
+
await syncDirectory(root);
|
|
993
|
+
}
|
|
994
|
+
for (const entry of entries) {
|
|
995
|
+
if (expected.has(entry.name))
|
|
996
|
+
continue;
|
|
997
|
+
await rm(join(root, entry.name), { force: true });
|
|
998
|
+
await syncDirectory(root);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
async function publishJson(path, value) {
|
|
1002
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
1003
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
1004
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, {
|
|
1005
|
+
encoding: "utf8",
|
|
1006
|
+
mode: 0o600,
|
|
1007
|
+
});
|
|
1008
|
+
await syncFile(temporary);
|
|
1009
|
+
await rename(temporary, path);
|
|
1010
|
+
await syncDirectory(dirname(path));
|
|
1011
|
+
}
|
|
1012
|
+
async function publishImmutableJson(path, value) {
|
|
1013
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
1014
|
+
const contents = `${JSON.stringify(value, null, 2)}\n`;
|
|
1015
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
1016
|
+
try {
|
|
1017
|
+
await writeFile(temporary, contents, {
|
|
1018
|
+
encoding: "utf8",
|
|
1019
|
+
flag: "wx",
|
|
1020
|
+
mode: 0o600,
|
|
1021
|
+
});
|
|
1022
|
+
await syncFile(temporary);
|
|
1023
|
+
try {
|
|
1024
|
+
await link(temporary, path);
|
|
1025
|
+
await syncDirectory(dirname(path));
|
|
1026
|
+
}
|
|
1027
|
+
catch (error) {
|
|
1028
|
+
if (!isNodeError(error) || error.code !== "EEXIST")
|
|
1029
|
+
throw error;
|
|
1030
|
+
if ((await readFile(path, "utf8")) !== contents)
|
|
1031
|
+
throw new Error("Migrated immutable file conflicts with existing data", {
|
|
1032
|
+
cause: error,
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
finally {
|
|
1037
|
+
await rm(temporary, { force: true });
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
async function syncFile(path) {
|
|
1041
|
+
const handle = await open(path, "r");
|
|
1042
|
+
try {
|
|
1043
|
+
await handle.sync();
|
|
1044
|
+
}
|
|
1045
|
+
finally {
|
|
1046
|
+
await handle.close();
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
async function syncDirectory(path) {
|
|
1050
|
+
let handle;
|
|
1051
|
+
try {
|
|
1052
|
+
handle = await open(path, "r");
|
|
1053
|
+
await handle.sync();
|
|
1054
|
+
}
|
|
1055
|
+
catch (error) {
|
|
1056
|
+
if (!isNodeError(error) || error.code !== "EINVAL")
|
|
1057
|
+
throw error;
|
|
1058
|
+
}
|
|
1059
|
+
finally {
|
|
1060
|
+
await handle?.close();
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
function crashAtStorageMigrationEdge(edge) {
|
|
1064
|
+
if (process.env.NODE_ENV === "test" &&
|
|
1065
|
+
process.env.NARRAEON_INTERNAL_TEST_CRASH_AT_STORAGE_MIGRATION_EDGE === edge)
|
|
1066
|
+
process.kill(process.pid, "SIGKILL");
|
|
1067
|
+
}
|