@mfdaves/okf-mcp 0.8.0 → 0.9.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 (31) hide show
  1. package/README.md +74 -14
  2. package/okf/{bundles/okf-mcp → okf-mcp}/distribution/reference-bundle.md +1 -1
  3. package/okf/{bundles/okf-mcp → okf-mcp}/index.md +3 -0
  4. package/okf/{bundles/okf-mcp → okf-mcp}/interfaces/mcp-tools.md +9 -1
  5. package/okf/{bundles/okf-mcp → okf-mcp}/overview/okf-mcp.md +5 -3
  6. package/okf/{bundles/okf-mcp → okf-mcp}/policies/authoring-safety.md +8 -0
  7. package/okf/{bundles/okf-mcp → okf-mcp}/runtime/file-concept-store.md +1 -1
  8. package/okf/{bundles/okf-mcp → okf-mcp}/runtime/mcp-server.md +6 -2
  9. package/okf/okf-mcp/runtime/producer-host.md +28 -0
  10. package/okf/okf-mcp/specs/producer-api.md +26 -0
  11. package/okf/okf-mcp/workflows/producer-publication.md +28 -0
  12. package/okf.project.yaml +1 -1
  13. package/package.json +2 -2
  14. package/server.json +11 -3
  15. package/src/authoring.js +10 -2
  16. package/src/cli.js +46 -4
  17. package/src/index.js +2 -0
  18. package/src/indexer.js +8 -1
  19. package/src/mcp-server.js +95 -0
  20. package/src/producer-publisher.js +462 -0
  21. package/src/producers.js +507 -0
  22. package/src/project.js +65 -0
  23. package/src/store.js +1 -0
  24. /package/okf/{bundles/okf-mcp → okf-mcp}/interfaces/http-authoring-api.md +0 -0
  25. /package/okf/{bundles/okf-mcp → okf-mcp}/runtime/indexer.md +0 -0
  26. /package/okf/{bundles/okf-mcp → okf-mcp}/specs/attested-computation.md +0 -0
  27. /package/okf/{bundles/okf-mcp → okf-mcp}/specs/concept-format.md +0 -0
  28. /package/okf/{bundles/okf-mcp → okf-mcp}/specs/git-sources.md +0 -0
  29. /package/okf/{bundles/okf-mcp → okf-mcp}/workflows/concept-authoring.md +0 -0
  30. /package/okf/{bundles/okf-mcp → okf-mcp}/workflows/concept-update.md +0 -0
  31. /package/okf/{bundles/okf-mcp → okf-mcp}/workflows/v02-migration.md +0 -0
@@ -0,0 +1,507 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+ const { createRequire } = require("node:module");
6
+
7
+ const { attachProject, buildIndex } = require("./indexer");
8
+ const { splitFrontmatter } = require("./parser");
9
+ const {
10
+ ProducerPublicationError,
11
+ ProducerPublisher,
12
+ normalizeProducedFiles,
13
+ } = require("./producer-publisher");
14
+
15
+ const MAX_PRODUCER_FILES = 10000;
16
+ const MAX_PRODUCER_BYTES = 64 * 1024 * 1024;
17
+ const UNSAFE_CONFIG_KEYS = new Set([
18
+ "catalogfile",
19
+ "catalogpath",
20
+ "connection",
21
+ "connectionstring",
22
+ "connectionuri",
23
+ "connectionurl",
24
+ "credentials",
25
+ "credential",
26
+ "databaseuri",
27
+ "databaseurl",
28
+ "dsn",
29
+ "dryrun",
30
+ "output",
31
+ "outputdir",
32
+ "outputpath",
33
+ "query",
34
+ "password",
35
+ "secret",
36
+ "sql",
37
+ "token",
38
+ ]);
39
+
40
+ class ProducerHostError extends Error {
41
+ constructor(message, code, details) {
42
+ super(message);
43
+ this.name = "ProducerHostError";
44
+ this.code = code;
45
+ if (details !== undefined) this.details = details;
46
+ }
47
+ }
48
+
49
+ function publicDiagnostic(entry) {
50
+ const source = entry || {};
51
+ const rawCode = typeof source.code === "string" ? source.code : "";
52
+ const rawField = typeof source.field === "string" ? source.field : "";
53
+ return Object.fromEntries([
54
+ ["code", /^[a-z][a-z0-9_]{0,63}$/.test(rawCode) ? rawCode : "producer_invalid"],
55
+ ["severity", source.severity || "error"],
56
+ ["layer", source.layer || "producer"],
57
+ ["path", source.path],
58
+ ["field", /^[A-Za-z0-9_.[\]-]{1,128}$/.test(rawField) ? rawField : undefined],
59
+ ].filter(([, value]) => value !== undefined));
60
+ }
61
+
62
+ function diagnostic(code, extra) {
63
+ return publicDiagnostic(Object.assign({ code, severity: "error", layer: "producer" }, extra || {}));
64
+ }
65
+
66
+ function isInside(root, target) {
67
+ const relative = path.relative(root, target);
68
+ return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
69
+ }
70
+
71
+ // Every node_modules directory node would consult for the project, nearest
72
+ // first. Hoisted, workspace, and store-backed installs all land in one of
73
+ // these even when the package directory itself is a symbolic link.
74
+ function moduleDirectories(projectRoot) {
75
+ const directories = [];
76
+ let current = path.resolve(projectRoot);
77
+ for (;;) {
78
+ directories.push(path.join(current, "node_modules"));
79
+ const parent = path.dirname(current);
80
+ if (parent === current) break;
81
+ current = parent;
82
+ }
83
+ return directories;
84
+ }
85
+
86
+ // The bare package name must actually be installed in one of the project's
87
+ // node_modules directories. Node resolves symbolic links away, so the install
88
+ // location is located lexically first and then tied back to the resolved entry.
89
+ function locateInstalledPackage(projectRoot, projectPath, packageName) {
90
+ const roots = moduleDirectories(projectRoot)
91
+ .concat(moduleDirectories(path.dirname(projectPath)));
92
+ const seen = new Set();
93
+ for (const directory of roots) {
94
+ if (seen.has(directory)) continue;
95
+ seen.add(directory);
96
+ const candidate = path.join(directory, ...packageName.split("/"));
97
+ if (fs.existsSync(path.join(candidate, "package.json"))) return candidate;
98
+ }
99
+ return null;
100
+ }
101
+
102
+ function loadInstalledProducer(project, packageName) {
103
+ let resolved;
104
+ try {
105
+ resolved = createRequire(project.path).resolve(packageName);
106
+ } catch {
107
+ throw new ProducerHostError("The configured producer package is not installed in the project.", "producer_package_not_installed");
108
+ }
109
+ if (!path.isAbsolute(resolved)) {
110
+ throw new ProducerHostError("The configured producer package must resolve to an installed npm package.", "invalid_producer_package");
111
+ }
112
+ let projectRoot;
113
+ try {
114
+ projectRoot = fs.realpathSync(project.root);
115
+ } catch {
116
+ projectRoot = path.resolve(project.root);
117
+ }
118
+ const projectPath = path.resolve(project.path);
119
+ const installDirectory = locateInstalledPackage(projectRoot, projectPath, packageName);
120
+ if (!installDirectory) {
121
+ throw new ProducerHostError("The configured producer package must be installed under a project node_modules directory.", "invalid_producer_package");
122
+ }
123
+ let realInstall;
124
+ let realResolved;
125
+ try {
126
+ realInstall = fs.realpathSync(installDirectory);
127
+ realResolved = fs.realpathSync(resolved);
128
+ } catch {
129
+ throw new ProducerHostError("The configured producer package could not be resolved safely.", "invalid_producer_package");
130
+ }
131
+ // A workspace or linked install points out of node_modules; that is an
132
+ // install-tool detail, and anything able to write there can already run
133
+ // install scripts. What must hold is that the entry node resolved belongs to
134
+ // the package the project installed under that exact bare name.
135
+ if (!isInside(realInstall, realResolved)) {
136
+ throw new ProducerHostError("The configured producer package must resolve inside its installed package directory.", "invalid_producer_package");
137
+ }
138
+ return require(realResolved);
139
+ }
140
+
141
+ function inspectConfig(value, envNames, pathParts) {
142
+ if (!value || typeof value !== "object") return;
143
+ Object.entries(value).forEach(([key, child]) => {
144
+ const normalized = key.toLowerCase().replace(/[^a-z0-9]/g, "");
145
+ if (UNSAFE_CONFIG_KEYS.has(normalized)) {
146
+ throw new ProducerHostError("Producer config contains a host-controlled or unsafe field.", "unsafe_producer_config", { field: pathParts.concat(key).join(".") });
147
+ }
148
+ if (/env$/i.test(key)) {
149
+ if (typeof child !== "string" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(child)) {
150
+ throw new ProducerHostError("Producer secret references must be valid environment variable names.", "invalid_producer_secret_reference", { field: pathParts.concat(key).join(".") });
151
+ }
152
+ envNames.add(child);
153
+ }
154
+ if (child && typeof child === "object") inspectConfig(child, envNames, pathParts.concat(key));
155
+ });
156
+ }
157
+
158
+ function validateDescriptor(instance, moduleValue, allowedRelationTypes) {
159
+ const descriptor = moduleValue && moduleValue.okfProducer;
160
+ if (!descriptor || typeof descriptor !== "object" || Array.isArray(descriptor)) {
161
+ throw new ProducerHostError("Producer package must export okfProducer.", "invalid_producer_descriptor");
162
+ }
163
+ if (String(descriptor.apiVersion || "") !== "1"
164
+ || String(descriptor.okfVersion || "") !== "0.2"
165
+ || descriptor.id !== instance.type
166
+ || typeof descriptor.version !== "string" || !descriptor.version.trim()
167
+ || typeof descriptor.validateConfig !== "function"
168
+ || typeof descriptor.generate !== "function") {
169
+ throw new ProducerHostError("Producer descriptor is incompatible with this host.", "incompatible_producer_descriptor");
170
+ }
171
+ const relationTypes = Array.isArray(descriptor.relationTypes) ? descriptor.relationTypes.map(String) : [];
172
+ if (relationTypes.some((type) => !allowedRelationTypes.has(type))) {
173
+ throw new ProducerHostError("Producer declares a relation type that the project does not allow.", "producer_relation_type_not_allowed");
174
+ }
175
+ return descriptor;
176
+ }
177
+
178
+ function buildCandidate(project, bundles, remoteBundles, files, stalePaths) {
179
+ const overrides = new Map();
180
+ files.forEach((file) => overrides.set(`${file.bundle}\u0000${file.path}`, file.content));
181
+ const deletions = new Set(stalePaths.map((entry) => `${entry.bundle}\u0000${entry.path}`));
182
+ return attachProject(buildIndex(bundles.concat(remoteBundles || []), {
183
+ relationTypes: project.relationTypes,
184
+ strictLinks: project.strictLinks,
185
+ documentOverrides: overrides,
186
+ documentDeletions: deletions,
187
+ }), project);
188
+ }
189
+
190
+ function validateCandidate(index, instance, outputPaths) {
191
+ const diagnostics = [];
192
+ const bundle = index.bundles.find((entry) => entry.id === instance.bundle);
193
+ if (!bundle || bundle.okfVersion !== "0.2") {
194
+ diagnostics.push(diagnostic("producer_bundle_requires_okf_0_2", { path: "index.md" }));
195
+ }
196
+ const root = index.documents.find((doc) => doc.bundle === instance.bundle && doc.path === "index.md");
197
+ if (!root) {
198
+ diagnostics.push(diagnostic("producer_missing_root_index", { path: "index.md" }));
199
+ } else {
200
+ try {
201
+ const split = splitFrontmatter(root.text);
202
+ if (!split.frontmatter
203
+ || Object.keys(split.frontmatter).length !== 1
204
+ || split.frontmatter.okf_version !== "0.2") {
205
+ diagnostics.push(diagnostic("producer_invalid_root_index", { path: "index.md" }));
206
+ }
207
+ } catch {
208
+ diagnostics.push(diagnostic("producer_invalid_root_index", { path: "index.md" }));
209
+ }
210
+ }
211
+ const documents = new Map(index.documents
212
+ .filter((doc) => doc.bundle === instance.bundle)
213
+ .map((doc) => [doc.path, doc]));
214
+ outputPaths.forEach((relativePath) => {
215
+ const doc = documents.get(relativePath);
216
+ if (!doc) {
217
+ diagnostics.push(diagnostic("producer_document_not_indexed", { path: relativePath }));
218
+ return;
219
+ }
220
+ if (doc.reserved) return;
221
+ const frontmatter = doc.frontmatter || {};
222
+ for (const field of ["type", "title", "description"]) {
223
+ if (typeof frontmatter[field] !== "string" || !frontmatter[field].trim()) {
224
+ diagnostics.push(diagnostic(`producer_missing_${field}`, { path: relativePath, field }));
225
+ }
226
+ }
227
+ if (doc.signals.generatedOrigin !== "native"
228
+ || !doc.signals.generated || doc.signals.generated.valid !== true) {
229
+ diagnostics.push(diagnostic("producer_invalid_generated", { path: relativePath, field: "generated" }));
230
+ }
231
+ if (doc.signals.sourcesOrigin !== "native"
232
+ || !Array.isArray(doc.signals.sources) || doc.signals.sources.length === 0
233
+ || doc.signals.sources.some((source) => source.valid !== true)) {
234
+ diagnostics.push(diagnostic("producer_invalid_sources", { path: relativePath, field: "sources" }));
235
+ }
236
+ (doc.v02Diagnostics || []).forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
237
+ });
238
+ const generatedPaths = new Set(outputPaths);
239
+ (index.errors || []).filter((entry) => (
240
+ entry.bundle === instance.bundle
241
+ && generatedPaths.has(entry.path)
242
+ && ["broken_relation", "missing_relation_target", "parse_error"].includes(entry.code)
243
+ )).forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
244
+ (index.warnings || []).filter((entry) => (
245
+ entry.bundle === instance.bundle
246
+ && generatedPaths.has(entry.path)
247
+ && ["broken_link", "link_outside_root", "broken_semantic_reference"].includes(entry.code)
248
+ )).forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
249
+ const projectValidation = index.validation || require("./validation").validateIndex(index);
250
+ if (!index.conformant || !index.validForProject) {
251
+ projectValidation.diagnostics.forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
252
+ }
253
+ const unique = new Map();
254
+ diagnostics.forEach((entry) => unique.set(JSON.stringify(entry), entry));
255
+ return {
256
+ valid: index.conformant === true && index.validForProject === true && unique.size === 0,
257
+ conformant: index.conformant === true,
258
+ validForProject: index.validForProject === true,
259
+ diagnostics: Array.from(unique.values()),
260
+ };
261
+ }
262
+
263
+ function countsFor(changes) {
264
+ return Object.fromEntries(Object.entries(changes).map(([key, values]) => [key, values.length]));
265
+ }
266
+
267
+ function normalizeSummary(value) {
268
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
269
+ throw new ProducerHostError("Producer generate() must return a numeric summary.", "invalid_producer_summary");
270
+ }
271
+ const entries = Object.entries(value);
272
+ if (entries.length > 50 || entries.some(([key, count]) => (
273
+ !/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key)
274
+ || !Number.isSafeInteger(count)
275
+ || count < 0
276
+ ))) {
277
+ throw new ProducerHostError("Producer summary entries must be bounded non-negative integers.", "invalid_producer_summary");
278
+ }
279
+ return Object.fromEntries(entries.sort(([left], [right]) => left.localeCompare(right)));
280
+ }
281
+
282
+ function producerReceipt(receipt, detail) {
283
+ const instance = receipt.instance || {};
284
+ const result = {
285
+ producer: instance.name,
286
+ type: instance.type,
287
+ bundle: instance.bundle,
288
+ okfVersion: "0.2",
289
+ valid: receipt.valid === true,
290
+ readyToApply: receipt.readyToApply === true,
291
+ ...(receipt.applied !== undefined ? { applied: receipt.applied === true } : {}),
292
+ ...(receipt.conformant !== undefined ? { conformant: receipt.conformant === true } : {}),
293
+ ...(receipt.validForProject !== undefined ? { validForProject: receipt.validForProject === true } : {}),
294
+ ...(receipt.generatedAt ? { generatedAt: receipt.generatedAt } : {}),
295
+ counts: receipt.counts || { create: 0, update: 0, delete: 0, unchanged: 0 },
296
+ summary: receipt.summary || {},
297
+ diagnostics: Array.isArray(receipt.diagnostics) ? receipt.diagnostics.slice(0, 100) : [],
298
+ };
299
+ if (detail === "full" && receipt.changes) {
300
+ result.changes = {};
301
+ Object.entries(receipt.changes).forEach(([kind, entries]) => {
302
+ result.changes[kind] = entries.slice(0, 100);
303
+ if (entries.length > 100) result.changes[`${kind}Omitted`] = entries.length - 100;
304
+ });
305
+ }
306
+ return result;
307
+ }
308
+
309
+ class ProducerService {
310
+ constructor(options) {
311
+ const config = options || {};
312
+ this.project = config.project;
313
+ this.store = config.store;
314
+ this.loader = config.loader || loadInstalledProducer;
315
+ this.env = config.env || process.env;
316
+ this.now = config.now || (() => new Date());
317
+ this.publisher = config.publisher || new ProducerPublisher(this.project, this.store);
318
+ this.bundles = config.bundles || this.project.bundles.filter((bundle) => !bundle.remote);
319
+ }
320
+
321
+ list() {
322
+ return (this.project.producers || []).map((producer) => ({
323
+ name: producer.name,
324
+ type: producer.type,
325
+ package: producer.package,
326
+ bundle: producer.bundle,
327
+ }));
328
+ }
329
+
330
+ getInstance(name) {
331
+ const instance = (this.project.producers || []).find((entry) => entry.name === name);
332
+ if (!instance) throw new ProducerHostError("Unknown configured producer.", "producer_not_found");
333
+ if ((this.project.errors || []).some((entry) => String(entry.code || "").includes("producer"))) {
334
+ throw new ProducerHostError("Project producer configuration is invalid.", "invalid_producer_configuration");
335
+ }
336
+ return instance;
337
+ }
338
+
339
+ async prepare(name, options) {
340
+ const instance = this.getInstance(name);
341
+ let descriptor;
342
+ try {
343
+ descriptor = validateDescriptor(
344
+ instance,
345
+ await this.loader(this.project, instance.package),
346
+ new Set(this.project.relationTypes || []),
347
+ );
348
+ } catch (error) {
349
+ if (error instanceof ProducerHostError) throw error;
350
+ throw new ProducerHostError("The configured producer package could not be loaded.", "producer_load_failed");
351
+ }
352
+ const envNames = new Set();
353
+ inspectConfig(instance.config, envNames, ["config"]);
354
+ let configValidation;
355
+ try {
356
+ configValidation = await descriptor.validateConfig(structuredClone(instance.config));
357
+ } catch {
358
+ throw new ProducerHostError("Producer configuration validation failed unexpectedly.", "producer_config_validation_failed");
359
+ }
360
+ if (!configValidation || configValidation.valid !== true) {
361
+ const entries = Array.isArray(configValidation && configValidation.diagnostics)
362
+ ? configValidation.diagnostics.slice(0, 100).map((entry) => diagnostic(
363
+ typeof entry.code === "string" ? entry.code : "producer_config_invalid",
364
+ typeof entry.field === "string" ? { field: entry.field } : undefined,
365
+ ))
366
+ : [diagnostic("producer_config_invalid")];
367
+ return { instance, descriptor, valid: false, readyToApply: false, diagnostics: entries };
368
+ }
369
+ const normalizedConfig = configValidation.config === undefined
370
+ ? structuredClone(instance.config)
371
+ : configValidation.config;
372
+ const accessedSecrets = [];
373
+ const getSecret = (secretName) => {
374
+ if (!envNames.has(secretName)) {
375
+ throw new ProducerHostError("Producer requested a secret not referenced by its configuration.", "producer_secret_not_allowed");
376
+ }
377
+ const value = this.env[secretName];
378
+ if (typeof value !== "string" || value.length === 0) {
379
+ throw new ProducerHostError("A configured producer secret is unavailable.", "producer_secret_unavailable");
380
+ }
381
+ accessedSecrets.push(value);
382
+ return value;
383
+ };
384
+ let generated;
385
+ const generatedAt = this.now().toISOString();
386
+ try {
387
+ generated = await descriptor.generate({
388
+ bundleId: instance.bundle,
389
+ config: structuredClone(normalizedConfig),
390
+ generatedAt,
391
+ getSecret,
392
+ signal: options && options.signal || new AbortController().signal,
393
+ });
394
+ } catch (error) {
395
+ if (error instanceof ProducerHostError) throw error;
396
+ throw new ProducerHostError("Producer execution failed.", "producer_execution_failed");
397
+ }
398
+ let files;
399
+ let summary;
400
+ try {
401
+ files = normalizeProducedFiles(generated && generated.files);
402
+ summary = normalizeSummary(generated && generated.summary);
403
+ } catch (error) {
404
+ if (error instanceof ProducerPublicationError || error instanceof ProducerHostError) {
405
+ return { instance, descriptor, valid: false, readyToApply: false, diagnostics: [publicDiagnostic(error)] };
406
+ }
407
+ throw error;
408
+ }
409
+ if (files.length > MAX_PRODUCER_FILES
410
+ || files.reduce((total, file) => total + Buffer.byteLength(file.content), 0) > MAX_PRODUCER_BYTES) {
411
+ return { instance, descriptor, valid: false, readyToApply: false, diagnostics: [diagnostic("producer_output_too_large")] };
412
+ }
413
+ if (accessedSecrets.some((secret) => files.some((file) => secret && file.content.includes(secret)))) {
414
+ return { instance, descriptor, valid: false, readyToApply: false, diagnostics: [diagnostic("producer_secret_leak")] };
415
+ }
416
+ let inspection;
417
+ try {
418
+ inspection = this.publisher.inspect(Object.assign({}, instance, { version: descriptor.version }), files);
419
+ } catch (error) {
420
+ if (error instanceof ProducerPublicationError) {
421
+ return { instance, descriptor, files, valid: false, readyToApply: false, diagnostics: [publicDiagnostic(error)] };
422
+ }
423
+ throw new ProducerHostError("Producer publication preflight failed.", "producer_publication_preflight_failed");
424
+ }
425
+ const candidateFiles = (inspection.effective || files)
426
+ .map((file) => Object.assign({ bundle: instance.bundle }, file));
427
+ const stalePaths = inspection.changes.delete.map((relativePath) => ({ bundle: instance.bundle, path: relativePath }));
428
+ const candidate = buildCandidate(
429
+ this.project,
430
+ this.bundles,
431
+ options && options.additionalBundles || [],
432
+ candidateFiles,
433
+ stalePaths,
434
+ );
435
+ const validation = validateCandidate(candidate, instance, files.map((file) => file.path));
436
+ return {
437
+ instance,
438
+ descriptor,
439
+ files,
440
+ changes: inspection.changes,
441
+ counts: countsFor(inspection.changes),
442
+ valid: validation.valid,
443
+ readyToApply: validation.valid,
444
+ conformant: validation.conformant,
445
+ validForProject: validation.validForProject,
446
+ diagnostics: validation.diagnostics,
447
+ summary,
448
+ generatedAt,
449
+ };
450
+ }
451
+
452
+ async preview(name, options) {
453
+ return this.prepare(name, options);
454
+ }
455
+
456
+ async run(name, options) {
457
+ return this.store.withWriteLock(async () => {
458
+ const prepared = await this.prepare(name, options);
459
+ if (!prepared.readyToApply) return Object.assign({}, prepared, { applied: false });
460
+ const producer = Object.assign({}, prepared.instance, { version: prepared.descriptor.version });
461
+ let changes;
462
+ try {
463
+ changes = await this.publisher.publish(producer, prepared.files, async () => {
464
+ const persisted = buildCandidate(
465
+ this.project,
466
+ this.bundles,
467
+ options && options.additionalBundles || [],
468
+ [],
469
+ [],
470
+ );
471
+ const validation = validateCandidate(persisted, prepared.instance, prepared.files.map((file) => file.path));
472
+ if (!validation.valid) {
473
+ throw new ProducerHostError("Persisted producer output failed validation.", "persisted_producer_validation_failed");
474
+ }
475
+ });
476
+ } catch (error) {
477
+ if (error instanceof ProducerPublicationError) {
478
+ return Object.assign({}, prepared, {
479
+ applied: false,
480
+ diagnostics: prepared.diagnostics.concat(publicDiagnostic(error)),
481
+ });
482
+ }
483
+ if (error instanceof ProducerHostError) throw error;
484
+ throw new ProducerHostError("Producer publication failed.", "producer_publication_failed");
485
+ }
486
+ return Object.assign({}, prepared, {
487
+ changes,
488
+ counts: countsFor(changes),
489
+ applied: true,
490
+ });
491
+ });
492
+ }
493
+ }
494
+
495
+ module.exports = {
496
+ MAX_PRODUCER_BYTES,
497
+ MAX_PRODUCER_FILES,
498
+ ProducerHostError,
499
+ ProducerService,
500
+ buildCandidate,
501
+ loadInstalledProducer,
502
+ normalizeSummary,
503
+ producerReceipt,
504
+ publicDiagnostic,
505
+ validateCandidate,
506
+ validateDescriptor,
507
+ };
package/src/project.js CHANGED
@@ -15,6 +15,9 @@ const DEFAULT_RELATION_TYPES = [
15
15
  "owned_by",
16
16
  "supersedes",
17
17
  "related_to",
18
+ "contains",
19
+ "contained_by",
20
+ "foreign_key_to",
18
21
  ];
19
22
 
20
23
  function configId(value, fallback) {
@@ -208,6 +211,65 @@ function validatePlugins(config, projectRoot, bundleIds, errors) {
208
211
  });
209
212
  }
210
213
 
214
+ function barePackageName(value) {
215
+ return typeof value === "string"
216
+ && /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(value);
217
+ }
218
+
219
+ function normalizeProducers(config, localBundleIds, errors) {
220
+ if (config.producers !== undefined && !Array.isArray(config.producers)) {
221
+ errors.push({ code: "invalid_producers", message: "producers must be an array." });
222
+ return [];
223
+ }
224
+ const names = new Set();
225
+ const bundles = new Set();
226
+ return (config.producers || []).map((producer, index) => {
227
+ const field = `producers[${index}]`;
228
+ if (!producer || typeof producer !== "object" || Array.isArray(producer)) {
229
+ errors.push({ code: "invalid_producer", producer: index, message: "Producer entries must be objects." });
230
+ return null;
231
+ }
232
+ const allowed = new Set(["name", "type", "package", "bundle", "config"]);
233
+ Object.keys(producer).filter((key) => !allowed.has(key)).forEach((key) => {
234
+ errors.push({ code: "unknown_producer_field", producer: producer.name || index, field: `${field}.${key}`, message: `Unknown producer field: ${key}` });
235
+ });
236
+ const name = typeof producer.name === "string" ? producer.name.trim() : "";
237
+ const type = typeof producer.type === "string" ? producer.type.trim() : "";
238
+ const packageName = typeof producer.package === "string" ? producer.package.trim() : "";
239
+ const bundle = typeof producer.bundle === "string" ? producer.bundle.trim() : "";
240
+ if (!name || !/^[A-Za-z0-9_.-]+$/.test(name)) {
241
+ errors.push({ code: "invalid_producer_name", producer: index, field: `${field}.name`, message: "Producer name must contain only letters, numbers, underscores, dots, and hyphens." });
242
+ } else if (names.has(name)) {
243
+ errors.push({ code: "duplicate_producer_name", producer: name, message: "Producer names must be unique." });
244
+ }
245
+ if (name) names.add(name);
246
+ if (!type || !/^[A-Za-z0-9_.-]+$/.test(type)) {
247
+ errors.push({ code: "invalid_producer_type", producer: name || index, field: `${field}.type`, message: "Producer type must contain only letters, numbers, underscores, dots, and hyphens." });
248
+ }
249
+ if (!barePackageName(packageName)) {
250
+ errors.push({ code: "invalid_producer_package", producer: name || index, field: `${field}.package`, message: "Producer package must be a bare npm package name without a subpath." });
251
+ }
252
+ if (!localBundleIds.has(bundle)) {
253
+ errors.push({ code: "unknown_producer_bundle", producer: name || index, bundle, message: "Producer bundle must name a local project bundle." });
254
+ } else if (bundles.has(bundle)) {
255
+ errors.push({ code: "duplicate_producer_bundle", producer: name || index, bundle, message: "Only one producer may manage a local bundle." });
256
+ }
257
+ if (bundle) bundles.add(bundle);
258
+ if (producer.config !== undefined && (!producer.config || typeof producer.config !== "object" || Array.isArray(producer.config))) {
259
+ errors.push({ code: "invalid_producer_config", producer: name || index, field: `${field}.config`, message: "Producer config must be an object." });
260
+ }
261
+ return {
262
+ name,
263
+ type,
264
+ package: packageName,
265
+ bundle,
266
+ config: producer.config && typeof producer.config === "object" && !Array.isArray(producer.config)
267
+ ? producer.config
268
+ : {},
269
+ };
270
+ }).filter(Boolean);
271
+ }
272
+
211
273
  function normalizeRemoteBundles(config, errors) {
212
274
  const remotes = Array.isArray(config.remoteBundles) ? config.remoteBundles : [];
213
275
  if (config.remoteBundles !== undefined && !Array.isArray(config.remoteBundles)) {
@@ -259,6 +321,7 @@ function loadProjectConfig(configPath) {
259
321
  const relationTypes = new Set(DEFAULT_RELATION_TYPES.concat(Array.isArray(config.relationTypes) ? config.relationTypes.map(String) : []));
260
322
  const bundles = normalizeBundles(config, resolvedPath, errors);
261
323
  const remoteBundles = normalizeRemoteBundles(config, errors);
324
+ const localBundleIds = new Set(bundles.map((bundle) => bundle.id));
262
325
  const allBundleIds = new Set(bundles.map((bundle) => bundle.id));
263
326
  remoteBundles.forEach((bundle) => {
264
327
  if (allBundleIds.has(bundle.id)) {
@@ -267,6 +330,7 @@ function loadProjectConfig(configPath) {
267
330
  allBundleIds.add(bundle.id);
268
331
  });
269
332
  validatePlugins(config, path.dirname(resolvedPath), allBundleIds, errors);
333
+ const producers = normalizeProducers(config, localBundleIds, errors);
270
334
  return {
271
335
  path: resolvedPath,
272
336
  root: path.dirname(resolvedPath),
@@ -275,6 +339,7 @@ function loadProjectConfig(configPath) {
275
339
  remoteBundles,
276
340
  relationTypes: Array.from(relationTypes),
277
341
  plugins: Array.isArray(config.plugins) ? config.plugins : [],
342
+ producers,
278
343
  strictLinks: Boolean(config.strictLinks),
279
344
  errors,
280
345
  raw: config,
package/src/store.js CHANGED
@@ -204,6 +204,7 @@ class FileConceptStore {
204
204
  remoteBundles: [],
205
205
  relationTypes: DEFAULT_RELATION_TYPES.slice(),
206
206
  plugins: [],
207
+ producers: [],
207
208
  strictLinks: Boolean(options && options.strictLinks),
208
209
  errors: [],
209
210
  rootMode: true,