@mfdaves/okf-mcp 0.9.1 → 0.12.0-rc.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 (97) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/README.md +172 -17
  3. package/bin/okf-mcp.js +6 -1
  4. package/okf/okf-mcp/policies/authoring-safety.md +11 -1
  5. package/package.json +17 -5
  6. package/server.json +2 -2
  7. package/src/assets.js +165 -73
  8. package/src/authoring/candidates.js +391 -0
  9. package/src/authoring/git-preflight.js +225 -0
  10. package/src/authoring/git-publish.js +776 -0
  11. package/src/authoring/live.js +789 -0
  12. package/src/authoring/paths.js +112 -0
  13. package/src/authoring/policy-hook.js +221 -0
  14. package/src/authoring/proposal-policy.js +137 -0
  15. package/src/authoring/receipts.js +329 -0
  16. package/src/authoring/render.js +38 -0
  17. package/src/authoring/shared.js +75 -0
  18. package/src/authoring/staging.js +216 -0
  19. package/src/authoring.js +386 -287
  20. package/src/cache/files.js +147 -0
  21. package/src/cache/lock.js +201 -0
  22. package/src/cache/paths.js +197 -0
  23. package/src/cache/store.js +703 -0
  24. package/src/catalog-text.js +19 -0
  25. package/src/cli.js +184 -151
  26. package/src/computation.js +99 -61
  27. package/src/context/assemble.js +567 -0
  28. package/src/context/cli-options.js +88 -0
  29. package/src/context/score.js +234 -0
  30. package/src/context/sections.js +274 -0
  31. package/src/embed/interface.js +160 -0
  32. package/src/embed/openai.js +715 -0
  33. package/src/embed/prepare.js +182 -0
  34. package/src/embed/units.js +96 -0
  35. package/src/git-source.js +112 -52
  36. package/src/graph.js +95 -37
  37. package/src/http-server.js +36 -17
  38. package/src/index.js +15 -0
  39. package/src/indexer/assemble.js +566 -0
  40. package/src/indexer/build.js +290 -0
  41. package/src/indexer/cache.js +81 -0
  42. package/src/indexer/edges.js +430 -0
  43. package/src/indexer/index.js +44 -0
  44. package/src/indexer/paths.js +178 -0
  45. package/src/indexer/resolve.js +282 -0
  46. package/src/indexer/update.js +720 -0
  47. package/src/indexer.js +3 -1068
  48. package/src/live-authoring.js +4 -2329
  49. package/src/markdown.js +21 -16
  50. package/src/mcp/registry.js +328 -0
  51. package/src/mcp/resources.js +45 -0
  52. package/src/mcp/server.js +472 -0
  53. package/src/mcp/tools/catalog.js +244 -0
  54. package/src/mcp/tools/computation.js +83 -0
  55. package/src/mcp/tools/context.js +133 -0
  56. package/src/mcp/tools/graph.js +167 -0
  57. package/src/mcp/tools/live-authoring.js +316 -0
  58. package/src/mcp/tools/migration.js +86 -0
  59. package/src/mcp/tools/producers.js +79 -0
  60. package/src/mcp/tools/proposals.js +213 -0
  61. package/src/mcp/tools/provenance.js +87 -0
  62. package/src/mcp/tools/remote.js +79 -0
  63. package/src/mcp/tools/search.js +242 -0
  64. package/src/mcp-server.js +3 -1611
  65. package/src/migration.js +431 -253
  66. package/src/parser.js +245 -130
  67. package/src/plugins.js +71 -48
  68. package/src/policy/write-policy.js +713 -0
  69. package/src/producer-publisher.js +195 -77
  70. package/src/producers.js +265 -101
  71. package/src/project.js +277 -93
  72. package/src/remote.js +96 -61
  73. package/src/search/cli-options.js +107 -0
  74. package/src/search/confidence.js +61 -0
  75. package/src/search/dense.js +304 -0
  76. package/src/search/embedding-cli.js +57 -0
  77. package/src/search/embedding-config.js +217 -0
  78. package/src/search/fusion.js +249 -0
  79. package/src/search/hybrid.js +526 -0
  80. package/src/search/languages.js +200 -0
  81. package/src/search/lexical.js +695 -0
  82. package/src/search/query.js +620 -0
  83. package/src/search/stem/among.js +43 -0
  84. package/src/search/stem/base-stemmer.js +311 -0
  85. package/src/search/stem/en-regions.js +456 -0
  86. package/src/search/stem/en-suffixes.js +664 -0
  87. package/src/search/stem/en-tables.js +176 -0
  88. package/src/search/stem/en.js +238 -0
  89. package/src/search/stem/it-regions.js +459 -0
  90. package/src/search/stem/it-suffixes.js +471 -0
  91. package/src/search/stem/it-tables.js +257 -0
  92. package/src/search/stem/it.js +155 -0
  93. package/src/search-index.js +2 -81
  94. package/src/search.js +2 -252
  95. package/src/store.js +134 -81
  96. package/src/v02.js +301 -105
  97. package/src/validation.js +24 -20
@@ -0,0 +1,566 @@
1
+ "use strict";
2
+
3
+ const { buildAssetRegistry, collectAssetReferences, resolveBundleAssetPath } = require("../assets");
4
+ const { normalizeSlashes } = require("../parser");
5
+ const { validateIndex } = require("../validation");
6
+ const { assetUri, contractAssetsReady, documentAtPath, documentEdges } = require("./edges");
7
+ const { bundleExcludesPath, fileStat } = require("./paths");
8
+
9
+ // Build state that is not part of the index object: the inputs the index was
10
+ // built from and, per document entry, the derived state together with the
11
+ // lookup keys that derivation consulted. updateIndex reads it to recompute
12
+ // only what a change can reach. A WeakMap keeps it out of the index's
13
+ // enumerable shape, so deep-equality comparisons ignore it, and lets it die
14
+ // with the index.
15
+ //
16
+ // A record holds { bundleArgs, options, config, project, duplicateBundleErrors,
17
+ // bundles: [{ bundle, rootMissing, entries, remoteWarnings }], registration,
18
+ // assets, stats }. An entry holds { relativePath, absolutePath, doc,
19
+ // parseError, stat, derived }; derived holds the entry's edges, warnings,
20
+ // errors, external references, custom relation types, asset references and
21
+ // the dependsOn key set. Index order is bundle order, then entry order.
22
+ const INDEX_RECORDS = new WeakMap();
23
+
24
+ const CONTRACT_EDGE_KINDS = ["computation", "executor", "attester"];
25
+
26
+ function indexRecord(index) {
27
+ return (index && INDEX_RECORDS.get(index)) || null;
28
+ }
29
+
30
+ function pushAll(target, items) {
31
+ for (let position = 0; position < items.length; position += 1) {
32
+ target.push(items[position]);
33
+ }
34
+ }
35
+
36
+ function portableConceptKey(value) {
37
+ return normalizeSlashes(String(value || ""))
38
+ .replace(/^\/+/, "")
39
+ .split("#")[0];
40
+ }
41
+
42
+ function registerPortableConceptId(map, ambiguous, key, document) {
43
+ const normalized = portableConceptKey(key);
44
+ if (!normalized || ambiguous.has(normalized)) {
45
+ return;
46
+ }
47
+ const owner = map.get(normalized);
48
+ if (owner && owner !== document) {
49
+ map.delete(normalized);
50
+ ambiguous.add(normalized);
51
+ return;
52
+ }
53
+ map.set(normalized, document);
54
+ }
55
+
56
+ // Registers every document in index order. Conflict resolution is order
57
+ // dependent (the first document wins a canonical URI, a contested alias is
58
+ // withdrawn from both claimants), so registration always runs over the full
59
+ // list; it is a linear pass of map inserts.
60
+ function registerDocuments(documents) {
61
+ const byUri = new Map();
62
+ const byPathUri = new Map();
63
+ const byCanonicalUri = new Map();
64
+ const byConceptId = new Map();
65
+ const ambiguousConceptIds = new Set();
66
+ const ambiguousAliases = new Set();
67
+ const duplicateErrors = [];
68
+ const aliasErrors = [];
69
+ documents.forEach((doc) => {
70
+ if (!byPathUri.has(doc.pathUri)) {
71
+ byPathUri.set(doc.pathUri, doc);
72
+ }
73
+ if (byCanonicalUri.has(doc.uri)) {
74
+ duplicateErrors.push({
75
+ code: "duplicate_concept_id",
76
+ uri: doc.uri,
77
+ message: "Duplicate canonical OKF Concept ID.",
78
+ });
79
+ return;
80
+ }
81
+ byCanonicalUri.set(doc.uri, doc);
82
+ byUri.set(doc.uri, doc);
83
+ registerPortableConceptId(byConceptId, ambiguousConceptIds, doc.conceptId, doc);
84
+ registerPortableConceptId(byConceptId, ambiguousConceptIds, doc.path, doc);
85
+ });
86
+ const aliasOwners = new Map();
87
+ documents.forEach((doc) => {
88
+ (doc.uriAliases || [doc.pathUri]).forEach((alias) => {
89
+ const canonicalOwner = byCanonicalUri.get(alias);
90
+ if (canonicalOwner && canonicalOwner !== doc) {
91
+ aliasErrors.push({
92
+ code: "uri_alias_conflicts_canonical",
93
+ uri: alias,
94
+ bundle: doc.bundle,
95
+ path: doc.path,
96
+ message:
97
+ "Compatibility URI alias conflicts with another canonical Concept ID; the canonical target wins.",
98
+ });
99
+ return;
100
+ }
101
+ const aliasOwner = aliasOwners.get(alias);
102
+ if (aliasOwner && aliasOwner !== doc) {
103
+ ambiguousAliases.add(alias);
104
+ byUri.delete(alias);
105
+ aliasErrors.push({
106
+ code: "ambiguous_uri_alias",
107
+ uri: alias,
108
+ bundle: doc.bundle,
109
+ path: doc.path,
110
+ message: "Compatibility URI alias resolves to more than one document.",
111
+ });
112
+ return;
113
+ }
114
+ if (!ambiguousAliases.has(alias)) {
115
+ aliasOwners.set(alias, doc);
116
+ byUri.set(alias, doc);
117
+ }
118
+ });
119
+ });
120
+ return {
121
+ byUri,
122
+ byPathUri,
123
+ byCanonicalUri,
124
+ byConceptId,
125
+ ambiguousConceptIds,
126
+ ambiguousAliases,
127
+ duplicateErrors,
128
+ aliasErrors,
129
+ };
130
+ }
131
+
132
+ // Map-like views that record every key they are asked for. Edge derivation
133
+ // resolves targets only through these, so the recorded set is exactly the set
134
+ // of index keys whose change could alter the document's derived state.
135
+ function trackedLookups(maps, dependsOn) {
136
+ const tracked = (prefix, map) => ({
137
+ get(key) {
138
+ dependsOn.add(prefix + key);
139
+ return map.get(key);
140
+ },
141
+ });
142
+ return {
143
+ byPathUri: tracked("p:", maps.byPathUri),
144
+ byUri: tracked("u:", maps.byUri),
145
+ byConceptId: tracked("c:", maps.byConceptId),
146
+ byAssetKey: tracked("a:", maps.byAssetKey),
147
+ byAssetUri: tracked("au:", maps.byAssetUri),
148
+ };
149
+ }
150
+
151
+ // The lookup keys under which other documents can reach this one. Adding,
152
+ // replacing or removing the document dirties exactly these keys.
153
+ function documentKeys(doc) {
154
+ const keys = [`p:${doc.pathUri}`, `u:${doc.uri}`];
155
+ (doc.uriAliases || []).forEach((alias) => {
156
+ keys.push(`u:${alias}`);
157
+ });
158
+ [doc.conceptId, doc.path].forEach((value) => {
159
+ const key = portableConceptKey(value);
160
+ if (key) {
161
+ keys.push(`c:${key}`);
162
+ }
163
+ });
164
+ return keys;
165
+ }
166
+
167
+ function assetKeys(byAssetKey, byAssetUri) {
168
+ const keys = [];
169
+ byAssetKey.forEach((_asset, key) => {
170
+ keys.push(`a:${key}`);
171
+ });
172
+ byAssetUri.forEach((_asset, uri) => {
173
+ keys.push(`au:${uri}`);
174
+ });
175
+ return keys;
176
+ }
177
+
178
+ function buildLocalAssetRegistry(documents, bundles, config) {
179
+ const localIds = new Set(bundles.filter((bundle) => !bundle.remote).map((bundle) => bundle.id));
180
+ const registry = buildAssetRegistry(
181
+ documents.filter((doc) => localIds.has(doc.bundle)),
182
+ bundles,
183
+ {
184
+ maxAssetBytes: config.maxAssetBytes,
185
+ allowResolvedPath: (bundle, relativePath) => !bundleExcludesPath(bundle, relativePath),
186
+ skipResolvedPath: (_bundle, relativePath) => relativePath.toLowerCase().endsWith(".md"),
187
+ },
188
+ );
189
+ registry.assets.forEach((asset) => {
190
+ asset.uri = asset.uri || assetUri(asset.bundle, asset.path);
191
+ });
192
+ return registry;
193
+ }
194
+
195
+ // The index's asset list: local assets that are not also documents, then the
196
+ // remote bundles' assets. Both lookup maps are built from the final list.
197
+ function assetMaps(registry, byPathUri, bundles) {
198
+ const assets = registry.assets.filter(
199
+ (asset) => !documentAtPath(byPathUri, `okf://${asset.bundle}/${asset.path}`),
200
+ );
201
+ bundles
202
+ .filter((bundle) => bundle.remote)
203
+ .forEach((bundle) => {
204
+ (bundle.assets || []).forEach((asset) => {
205
+ assets.push(
206
+ Object.assign({}, asset, {
207
+ bundle: bundle.id,
208
+ uri: asset.uri || assetUri(bundle.id, asset.path),
209
+ remote: true,
210
+ }),
211
+ );
212
+ });
213
+ });
214
+ return {
215
+ assets,
216
+ byAssetKey: new Map(assets.map((asset) => [`${asset.bundle}\u0000${asset.path}`, asset])),
217
+ byAssetUri: new Map(assets.map((asset) => [asset.uri, asset])),
218
+ };
219
+ }
220
+
221
+ // The asset paths the local documents reference, each with the size and
222
+ // mtime found now (null where no regular file exists), so a refresh can tell
223
+ // whether a referenced asset was modified, removed or added without reading
224
+ // any of them. Documents and policy-excluded paths are left out: the first
225
+ // are tracked as documents, the second carry a fixed diagnostic.
226
+ function watchAssets(record) {
227
+ const watch = new Map();
228
+ record.bundles.forEach((bundleRecord) => {
229
+ const bundle = bundleRecord.bundle;
230
+ if (bundle.remote) {
231
+ return;
232
+ }
233
+ bundleRecord.entries.forEach((entry) => {
234
+ if (!entry.doc) {
235
+ return;
236
+ }
237
+ entry.derived.assetReferences.forEach((reference) => {
238
+ let resolved;
239
+ try {
240
+ resolved = resolveBundleAssetPath(bundle.root, reference.fromPath, reference.value);
241
+ } catch {
242
+ return;
243
+ }
244
+ if (
245
+ resolved.path.toLowerCase().endsWith(".md") ||
246
+ bundleExcludesPath(bundle, resolved.path) ||
247
+ watch.has(resolved.absolutePath)
248
+ ) {
249
+ return;
250
+ }
251
+ watch.set(resolved.absolutePath, fileStat(resolved.absolutePath));
252
+ });
253
+ });
254
+ });
255
+ return watch;
256
+ }
257
+
258
+ function lookupMaps(registration, assetState) {
259
+ return {
260
+ byPathUri: registration.byPathUri,
261
+ byUri: registration.byUri,
262
+ byConceptId: registration.byConceptId,
263
+ byAssetKey: assetState.byAssetKey,
264
+ byAssetUri: assetState.byAssetUri,
265
+ };
266
+ }
267
+
268
+ function deriveEntry(entry, bundle, maps, config) {
269
+ const doc = entry.doc;
270
+ const dependsOn = new Set();
271
+ const lookups = trackedLookups(maps, dependsOn);
272
+ const derived = documentEdges(doc, bundle, lookups, config.edgeConfig);
273
+ return {
274
+ parseWarnings: doc.warnings.map((warning) => Object.assign({ bundle: doc.bundle }, warning)),
275
+ edges: derived.edges,
276
+ warnings: derived.warnings,
277
+ errors: derived.errors,
278
+ externalReferences: derived.externalReferences,
279
+ customRelationTypes: derived.customRelationTypes,
280
+ assetReferences: collectAssetReferences(doc),
281
+ dependsOn,
282
+ byAssetUri: lookups.byAssetUri,
283
+ };
284
+ }
285
+
286
+ function contractReadiness(contract, contractEdges, byAssetUri) {
287
+ // A freshly parsed contract carries the parser's structural verdict in
288
+ // `ready`; once indexed, `ready` also folds in asset readiness, so the
289
+ // structural verdict is kept separately and read back from there.
290
+ const structuralReady = Object.hasOwn(contract, "structuralReady")
291
+ ? Boolean(contract.structuralReady)
292
+ : Boolean(contract.ready);
293
+ const assetsReady = contractAssetsReady(contract, contractEdges, byAssetUri);
294
+ const ready = Boolean(structuralReady && assetsReady);
295
+ return { structuralReady, assetsReady, ready, attestationReady: ready };
296
+ }
297
+
298
+ // Applies computed readiness to the entry's document. A document this
299
+ // operation parsed is updated in place; one shared with another index is
300
+ // replaced by a shallow clone so that index never changes under its holder.
301
+ function applyReadiness(entry, readiness, owned) {
302
+ const contract = entry.doc.signals.computation;
303
+ if (Object.keys(readiness).every((key) => contract[key] === readiness[key])) {
304
+ return false;
305
+ }
306
+ if (owned.has(entry)) {
307
+ Object.assign(contract, readiness);
308
+ return false;
309
+ }
310
+ entry.doc = Object.assign({}, entry.doc, {
311
+ signals: Object.assign({}, entry.doc.signals, {
312
+ computation: Object.assign({}, contract, readiness),
313
+ }),
314
+ });
315
+ return true;
316
+ }
317
+
318
+ // Readiness for the concept entries just derived. When two documents share a
319
+ // canonical URI (a duplicate_concept_id error) the contract edges of both are
320
+ // pooled in index order, as a full build sees them.
321
+ function applyReadinessPass(items, located, pooled, owned) {
322
+ let pool = null;
323
+ if (pooled) {
324
+ pool = new Map();
325
+ located.forEach((item) => {
326
+ const uri = item.entry.doc.uri;
327
+ const list = pool.get(uri);
328
+ if (list) {
329
+ pushAll(list, item.entry.derived.edges);
330
+ } else {
331
+ pool.set(uri, item.entry.derived.edges.slice());
332
+ }
333
+ });
334
+ }
335
+ let cloned = false;
336
+ items.forEach((item) => {
337
+ const doc = item.entry.doc;
338
+ if (doc.reserved || !doc.valid) {
339
+ return;
340
+ }
341
+ const contract = doc.signals?.computation;
342
+ if (!contract) {
343
+ return;
344
+ }
345
+ const edges = pool ? pool.get(doc.uri) || [] : item.entry.derived.edges;
346
+ const contractEdges = edges.filter((edge) => CONTRACT_EDGE_KINDS.includes(edge.kind));
347
+ const readiness = contractReadiness(contract, contractEdges, item.entry.derived.byAssetUri);
348
+ if (applyReadiness(item.entry, readiness, owned)) {
349
+ cloned = true;
350
+ }
351
+ });
352
+ return cloned;
353
+ }
354
+
355
+ function bundleMetadata(bundle, documents, assets) {
356
+ const rootIndex = documents.find((doc) => doc.bundle === bundle.id && doc.path === "index.md");
357
+ const okfVersion =
358
+ rootIndex?.frontmatter && Object.hasOwn(rootIndex.frontmatter, "okf_version")
359
+ ? rootIndex.frontmatter.okf_version
360
+ : null;
361
+ const version = okfVersion === null ? null : String(okfVersion);
362
+ return Object.assign({}, bundle, {
363
+ okfVersion: version,
364
+ versionStatus:
365
+ version === "0.2"
366
+ ? "understood"
367
+ : version === "0.1"
368
+ ? "legacy"
369
+ : version
370
+ ? "unsupported"
371
+ : "undeclared",
372
+ documentCount: documents.filter((doc) => doc.bundle === bundle.id).length,
373
+ assetCount: assets.filter((asset) => asset.bundle === bundle.id).length,
374
+ });
375
+ }
376
+
377
+ function applyProject(index, project) {
378
+ index.errors.unshift.apply(index.errors, project.errors || []);
379
+ index.project = {
380
+ name: project.project,
381
+ path: project.path,
382
+ root: project.root,
383
+ plugins: project.plugins,
384
+ producers: project.producers,
385
+ remoteBundles: project.remoteBundles,
386
+ strictLinks: project.strictLinks,
387
+ languages: project.languages,
388
+ autoFill: project.autoFill,
389
+ };
390
+ index.strictLinks = Boolean(index.strictLinks || project.strictLinks);
391
+ }
392
+
393
+ function attachValidation(index) {
394
+ const validation = validateIndex(index);
395
+ index.conformant = validation.conformant;
396
+ index.validForProject = validation.validForProject;
397
+ index.valid = validation.valid;
398
+ index.diagnostics = validation.diagnostics;
399
+ return index;
400
+ }
401
+
402
+ // Builds the index object from a record whose entries are all derived. Every
403
+ // list keeps the order a full build produces: documents in bundle then entry
404
+ // order; errors as duplicate bundle ids, then per bundle a missing root or its
405
+ // parse errors, then duplicate concept ids, alias conflicts and relation
406
+ // errors; warnings as parse warnings, asset diagnostics, remote bundle
407
+ // notices and link warnings.
408
+ function assembleIndex(record, documents, registration, assetState) {
409
+ const { config } = record;
410
+ const derivedList = [];
411
+ record.bundles.forEach((bundleRecord) => {
412
+ bundleRecord.entries.forEach((entry) => {
413
+ if (entry.doc) {
414
+ derivedList.push(entry.derived);
415
+ }
416
+ });
417
+ });
418
+ const concepts = [];
419
+ const reserved = [];
420
+ documents.forEach((doc) => {
421
+ if (doc.reserved) {
422
+ reserved.push(doc);
423
+ } else if (doc.valid) {
424
+ concepts.push(doc);
425
+ }
426
+ });
427
+
428
+ const errors = record.duplicateBundleErrors.slice();
429
+ record.bundles.forEach((bundleRecord) => {
430
+ if (bundleRecord.rootMissing) {
431
+ errors.push(bundleRecord.rootMissing);
432
+ return;
433
+ }
434
+ bundleRecord.entries.forEach((entry) => {
435
+ if (entry.parseError) {
436
+ errors.push(entry.parseError);
437
+ }
438
+ });
439
+ });
440
+ pushAll(errors, registration.duplicateErrors);
441
+ pushAll(errors, registration.aliasErrors);
442
+ derivedList.forEach((derived) => {
443
+ pushAll(errors, derived.errors);
444
+ });
445
+
446
+ const warnings = [];
447
+ derivedList.forEach((derived) => {
448
+ pushAll(warnings, derived.parseWarnings);
449
+ });
450
+ pushAll(warnings, record.assets.registry.diagnostics);
451
+ record.bundles.forEach((bundleRecord) => {
452
+ if (bundleRecord.bundle.remote) {
453
+ pushAll(warnings, bundleRecord.remoteWarnings);
454
+ }
455
+ });
456
+ derivedList.forEach((derived) => {
457
+ pushAll(warnings, derived.warnings);
458
+ });
459
+
460
+ const edges = [];
461
+ derivedList.forEach((derived) => {
462
+ pushAll(edges, derived.edges);
463
+ });
464
+
465
+ const externalReferences = new Map();
466
+ derivedList.forEach((derived) => {
467
+ derived.externalReferences.forEach((uri) => {
468
+ if (!externalReferences.has(uri)) {
469
+ externalReferences.set(uri, { uri, kind: "external" });
470
+ }
471
+ });
472
+ });
473
+
474
+ const relationTypes = new Set(config.edgeConfig.allowedRelationTypes);
475
+ derivedList.forEach((derived) => {
476
+ derived.customRelationTypes.forEach((type) => {
477
+ relationTypes.add(type);
478
+ });
479
+ });
480
+
481
+ const index = {
482
+ bundles: record.bundles.map((bundleRecord) =>
483
+ bundleMetadata(bundleRecord.bundle, documents, assetState.assets),
484
+ ),
485
+ documents,
486
+ concepts,
487
+ reserved,
488
+ relationTypes: Array.from(relationTypes),
489
+ externalReferences: Array.from(externalReferences.values()),
490
+ assets: assetState.assets,
491
+ warnings,
492
+ errors,
493
+ edges,
494
+ strictLinks: config.strictLinks,
495
+ byUri: registration.byUri,
496
+ byPathUri: registration.byPathUri,
497
+ byCanonicalUri: registration.byCanonicalUri,
498
+ byConceptId: registration.byConceptId,
499
+ byAssetKey: assetState.byAssetKey,
500
+ byAssetUri: assetState.byAssetUri,
501
+ ambiguousAliases: registration.ambiguousAliases,
502
+ ambiguousConceptIds: registration.ambiguousConceptIds,
503
+ };
504
+ if (record.project) {
505
+ applyProject(index, record.project);
506
+ }
507
+ INDEX_RECORDS.set(index, record);
508
+ return attachValidation(index);
509
+ }
510
+
511
+ // Finishes an index from a record whose entries are loaded: registers the
512
+ // documents, settles the asset registry, derives the entries the operation
513
+ // selects (all of them for a build), applies computation readiness, and
514
+ // assembles the index object. `operation` carries `owned` (entries whose
515
+ // documents this operation parsed and may mutate), an optional carried
516
+ // `assets` state (registry and watch list), an optional `selectAffected`
517
+ // narrowing derivation, and `pooledReadiness` when duplicate canonical URIs
518
+ // exist on either side.
519
+ function completeIndex(record, operation) {
520
+ const bundles = record.bundles.map((bundleRecord) => bundleRecord.bundle);
521
+ const located = [];
522
+ record.bundles.forEach((bundleRecord) => {
523
+ bundleRecord.entries.forEach((entry, position) => {
524
+ if (entry.doc) {
525
+ located.push({ entry, bundleRecord, position });
526
+ }
527
+ });
528
+ });
529
+ let documents = located.map((item) => item.entry.doc);
530
+ let registration = registerDocuments(documents);
531
+ const registry = operation.assets
532
+ ? operation.assets.registry
533
+ : buildLocalAssetRegistry(documents, bundles, record.config);
534
+ let assetState = assetMaps(registry, registration.byPathUri, bundles);
535
+ const maps = lookupMaps(registration, assetState);
536
+ const affected = operation.selectAffected
537
+ ? operation.selectAffected({ registration, assetState, located })
538
+ : located;
539
+ affected.forEach((item) => {
540
+ item.entry.derived = deriveEntry(item.entry, item.bundleRecord.bundle, maps, record.config);
541
+ });
542
+ const pooled = registration.duplicateErrors.length > 0 || Boolean(operation.pooledReadiness);
543
+ if (applyReadinessPass(affected, located, pooled, operation.owned)) {
544
+ // A shared document was replaced by a clone; the maps must point at it.
545
+ documents = located.map((item) => item.entry.doc);
546
+ registration = registerDocuments(documents);
547
+ assetState = assetMaps(registry, registration.byPathUri, bundles);
548
+ }
549
+ record.registration = registration;
550
+ record.assets = Object.assign(
551
+ { registry, watch: operation.assets ? operation.assets.watch : watchAssets(record) },
552
+ assetState,
553
+ );
554
+ record.stats.documents = documents.length;
555
+ record.stats.derived = affected.length;
556
+ return assembleIndex(record, documents, registration, assetState);
557
+ }
558
+
559
+ module.exports = {
560
+ applyProject,
561
+ assetKeys,
562
+ attachValidation,
563
+ completeIndex,
564
+ documentKeys,
565
+ indexRecord,
566
+ };