@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
package/src/producers.js CHANGED
@@ -4,6 +4,7 @@ const fs = require("node:fs");
4
4
  const path = require("node:path");
5
5
  const { createRequire } = require("node:module");
6
6
 
7
+ const { evaluateProducerFiles, policyDiagnostics } = require("./authoring/policy-hook");
7
8
  const { attachProject, buildIndex } = require("./indexer");
8
9
  const { splitFrontmatter } = require("./parser");
9
10
  const {
@@ -50,17 +51,21 @@ function publicDiagnostic(entry) {
50
51
  const source = entry || {};
51
52
  const rawCode = typeof source.code === "string" ? source.code : "";
52
53
  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));
54
+ return Object.fromEntries(
55
+ [
56
+ ["code", /^[a-z][a-z0-9_]{0,63}$/.test(rawCode) ? rawCode : "producer_invalid"],
57
+ ["severity", source.severity || "error"],
58
+ ["layer", source.layer || "producer"],
59
+ ["path", source.path],
60
+ ["field", /^[A-Za-z0-9_.[\]-]{1,128}$/.test(rawField) ? rawField : undefined],
61
+ ].filter(([, value]) => value !== undefined),
62
+ );
60
63
  }
61
64
 
62
65
  function diagnostic(code, extra) {
63
- return publicDiagnostic(Object.assign({ code, severity: "error", layer: "producer" }, extra || {}));
66
+ return publicDiagnostic(
67
+ Object.assign({ code, severity: "error", layer: "producer" }, extra || {}),
68
+ );
64
69
  }
65
70
 
66
71
  function isInside(root, target) {
@@ -87,8 +92,7 @@ function moduleDirectories(projectRoot) {
87
92
  // node_modules directories. Node resolves symbolic links away, so the install
88
93
  // location is located lexically first and then tied back to the resolved entry.
89
94
  function locateInstalledPackage(projectRoot, projectPath, packageName) {
90
- const roots = moduleDirectories(projectRoot)
91
- .concat(moduleDirectories(path.dirname(projectPath)));
95
+ const roots = moduleDirectories(projectRoot).concat(moduleDirectories(path.dirname(projectPath)));
92
96
  const seen = new Set();
93
97
  for (const directory of roots) {
94
98
  if (seen.has(directory)) continue;
@@ -104,10 +108,16 @@ function loadInstalledProducer(project, packageName) {
104
108
  try {
105
109
  resolved = createRequire(project.path).resolve(packageName);
106
110
  } catch {
107
- throw new ProducerHostError("The configured producer package is not installed in the project.", "producer_package_not_installed");
111
+ throw new ProducerHostError(
112
+ "The configured producer package is not installed in the project.",
113
+ "producer_package_not_installed",
114
+ );
108
115
  }
109
116
  if (!path.isAbsolute(resolved)) {
110
- throw new ProducerHostError("The configured producer package must resolve to an installed npm package.", "invalid_producer_package");
117
+ throw new ProducerHostError(
118
+ "The configured producer package must resolve to an installed npm package.",
119
+ "invalid_producer_package",
120
+ );
111
121
  }
112
122
  let projectRoot;
113
123
  try {
@@ -118,7 +128,10 @@ function loadInstalledProducer(project, packageName) {
118
128
  const projectPath = path.resolve(project.path);
119
129
  const installDirectory = locateInstalledPackage(projectRoot, projectPath, packageName);
120
130
  if (!installDirectory) {
121
- throw new ProducerHostError("The configured producer package must be installed under a project node_modules directory.", "invalid_producer_package");
131
+ throw new ProducerHostError(
132
+ "The configured producer package must be installed under a project node_modules directory.",
133
+ "invalid_producer_package",
134
+ );
122
135
  }
123
136
  let realInstall;
124
137
  let realResolved;
@@ -126,14 +139,20 @@ function loadInstalledProducer(project, packageName) {
126
139
  realInstall = fs.realpathSync(installDirectory);
127
140
  realResolved = fs.realpathSync(resolved);
128
141
  } catch {
129
- throw new ProducerHostError("The configured producer package could not be resolved safely.", "invalid_producer_package");
142
+ throw new ProducerHostError(
143
+ "The configured producer package could not be resolved safely.",
144
+ "invalid_producer_package",
145
+ );
130
146
  }
131
147
  // A workspace or linked install points out of node_modules; that is an
132
148
  // install-tool detail, and anything able to write there can already run
133
149
  // install scripts. What must hold is that the entry node resolved belongs to
134
150
  // the package the project installed under that exact bare name.
135
151
  if (!isInside(realInstall, realResolved)) {
136
- throw new ProducerHostError("The configured producer package must resolve inside its installed package directory.", "invalid_producer_package");
152
+ throw new ProducerHostError(
153
+ "The configured producer package must resolve inside its installed package directory.",
154
+ "invalid_producer_package",
155
+ );
137
156
  }
138
157
  return require(realResolved);
139
158
  }
@@ -143,11 +162,19 @@ function inspectConfig(value, envNames, pathParts) {
143
162
  Object.entries(value).forEach(([key, child]) => {
144
163
  const normalized = key.toLowerCase().replace(/[^a-z0-9]/g, "");
145
164
  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(".") });
165
+ throw new ProducerHostError(
166
+ "Producer config contains a host-controlled or unsafe field.",
167
+ "unsafe_producer_config",
168
+ { field: pathParts.concat(key).join(".") },
169
+ );
147
170
  }
148
171
  if (/env$/i.test(key)) {
149
172
  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(".") });
173
+ throw new ProducerHostError(
174
+ "Producer secret references must be valid environment variable names.",
175
+ "invalid_producer_secret_reference",
176
+ { field: pathParts.concat(key).join(".") },
177
+ );
151
178
  }
152
179
  envNames.add(child);
153
180
  }
@@ -158,19 +185,33 @@ function inspectConfig(value, envNames, pathParts) {
158
185
  function validateDescriptor(instance, moduleValue, allowedRelationTypes) {
159
186
  const descriptor = moduleValue && moduleValue.okfProducer;
160
187
  if (!descriptor || typeof descriptor !== "object" || Array.isArray(descriptor)) {
161
- throw new ProducerHostError("Producer package must export okfProducer.", "invalid_producer_descriptor");
188
+ throw new ProducerHostError(
189
+ "Producer package must export okfProducer.",
190
+ "invalid_producer_descriptor",
191
+ );
162
192
  }
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");
193
+ if (
194
+ String(descriptor.apiVersion || "") !== "1" ||
195
+ String(descriptor.okfVersion || "") !== "0.2" ||
196
+ descriptor.id !== instance.type ||
197
+ typeof descriptor.version !== "string" ||
198
+ !descriptor.version.trim() ||
199
+ typeof descriptor.validateConfig !== "function" ||
200
+ typeof descriptor.generate !== "function"
201
+ ) {
202
+ throw new ProducerHostError(
203
+ "Producer descriptor is incompatible with this host.",
204
+ "incompatible_producer_descriptor",
205
+ );
170
206
  }
171
- const relationTypes = Array.isArray(descriptor.relationTypes) ? descriptor.relationTypes.map(String) : [];
207
+ const relationTypes = Array.isArray(descriptor.relationTypes)
208
+ ? descriptor.relationTypes.map(String)
209
+ : [];
172
210
  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");
211
+ throw new ProducerHostError(
212
+ "Producer declares a relation type that the project does not allow.",
213
+ "producer_relation_type_not_allowed",
214
+ );
174
215
  }
175
216
  return descriptor;
176
217
  }
@@ -179,12 +220,15 @@ function buildCandidate(project, bundles, remoteBundles, files, stalePaths) {
179
220
  const overrides = new Map();
180
221
  files.forEach((file) => overrides.set(`${file.bundle}\u0000${file.path}`, file.content));
181
222
  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);
223
+ return attachProject(
224
+ buildIndex(bundles.concat(remoteBundles || []), {
225
+ relationTypes: project.relationTypes,
226
+ strictLinks: project.strictLinks,
227
+ documentOverrides: overrides,
228
+ documentDeletions: deletions,
229
+ }),
230
+ project,
231
+ );
188
232
  }
189
233
 
190
234
  function validateCandidate(index, instance, outputPaths) {
@@ -193,24 +237,28 @@ function validateCandidate(index, instance, outputPaths) {
193
237
  if (!bundle || bundle.okfVersion !== "0.2") {
194
238
  diagnostics.push(diagnostic("producer_bundle_requires_okf_0_2", { path: "index.md" }));
195
239
  }
196
- const root = index.documents.find((doc) => doc.bundle === instance.bundle && doc.path === "index.md");
240
+ const root = index.documents.find(
241
+ (doc) => doc.bundle === instance.bundle && doc.path === "index.md",
242
+ );
197
243
  if (!root) {
198
244
  diagnostics.push(diagnostic("producer_missing_root_index", { path: "index.md" }));
199
245
  } else {
200
246
  try {
201
247
  const split = splitFrontmatter(root.text);
202
- if (!split.frontmatter
203
- || Object.keys(split.frontmatter).length !== 1
204
- || split.frontmatter.okf_version !== "0.2") {
248
+ if (
249
+ !split.frontmatter ||
250
+ Object.keys(split.frontmatter).length !== 1 ||
251
+ split.frontmatter.okf_version !== "0.2"
252
+ ) {
205
253
  diagnostics.push(diagnostic("producer_invalid_root_index", { path: "index.md" }));
206
254
  }
207
255
  } catch {
208
256
  diagnostics.push(diagnostic("producer_invalid_root_index", { path: "index.md" }));
209
257
  }
210
258
  }
211
- const documents = new Map(index.documents
212
- .filter((doc) => doc.bundle === instance.bundle)
213
- .map((doc) => [doc.path, doc]));
259
+ const documents = new Map(
260
+ index.documents.filter((doc) => doc.bundle === instance.bundle).map((doc) => [doc.path, doc]),
261
+ );
214
262
  outputPaths.forEach((relativePath) => {
215
263
  const doc = documents.get(relativePath);
216
264
  if (!doc) {
@@ -224,28 +272,44 @@ function validateCandidate(index, instance, outputPaths) {
224
272
  diagnostics.push(diagnostic(`producer_missing_${field}`, { path: relativePath, field }));
225
273
  }
226
274
  }
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" }));
275
+ if (
276
+ doc.signals.generatedOrigin !== "native" ||
277
+ !doc.signals.generated ||
278
+ doc.signals.generated.valid !== true
279
+ ) {
280
+ diagnostics.push(
281
+ diagnostic("producer_invalid_generated", { path: relativePath, field: "generated" }),
282
+ );
230
283
  }
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" }));
284
+ if (
285
+ doc.signals.sourcesOrigin !== "native" ||
286
+ !Array.isArray(doc.signals.sources) ||
287
+ doc.signals.sources.length === 0 ||
288
+ doc.signals.sources.some((source) => source.valid !== true)
289
+ ) {
290
+ diagnostics.push(
291
+ diagnostic("producer_invalid_sources", { path: relativePath, field: "sources" }),
292
+ );
235
293
  }
236
294
  (doc.v02Diagnostics || []).forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
237
295
  });
238
296
  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)));
297
+ (index.errors || [])
298
+ .filter(
299
+ (entry) =>
300
+ entry.bundle === instance.bundle &&
301
+ generatedPaths.has(entry.path) &&
302
+ ["broken_relation", "missing_relation_target", "parse_error"].includes(entry.code),
303
+ )
304
+ .forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
305
+ (index.warnings || [])
306
+ .filter(
307
+ (entry) =>
308
+ entry.bundle === instance.bundle &&
309
+ generatedPaths.has(entry.path) &&
310
+ ["broken_link", "link_outside_root", "broken_semantic_reference"].includes(entry.code),
311
+ )
312
+ .forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
249
313
  const projectValidation = index.validation || require("./validation").validateIndex(index);
250
314
  if (!index.conformant || !index.validForProject) {
251
315
  projectValidation.diagnostics.forEach((entry) => diagnostics.push(publicDiagnostic(entry)));
@@ -266,15 +330,23 @@ function countsFor(changes) {
266
330
 
267
331
  function normalizeSummary(value) {
268
332
  if (!value || typeof value !== "object" || Array.isArray(value)) {
269
- throw new ProducerHostError("Producer generate() must return a numeric summary.", "invalid_producer_summary");
333
+ throw new ProducerHostError(
334
+ "Producer generate() must return a numeric summary.",
335
+ "invalid_producer_summary",
336
+ );
270
337
  }
271
338
  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");
339
+ if (
340
+ entries.length > 50 ||
341
+ entries.some(
342
+ ([key, count]) =>
343
+ !/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key) || !Number.isSafeInteger(count) || count < 0,
344
+ )
345
+ ) {
346
+ throw new ProducerHostError(
347
+ "Producer summary entries must be bounded non-negative integers.",
348
+ "invalid_producer_summary",
349
+ );
278
350
  }
279
351
  return Object.fromEntries(entries.sort(([left], [right]) => left.localeCompare(right)));
280
352
  }
@@ -290,11 +362,16 @@ function producerReceipt(receipt, detail) {
290
362
  readyToApply: receipt.readyToApply === true,
291
363
  ...(receipt.applied !== undefined ? { applied: receipt.applied === true } : {}),
292
364
  ...(receipt.conformant !== undefined ? { conformant: receipt.conformant === true } : {}),
293
- ...(receipt.validForProject !== undefined ? { validForProject: receipt.validForProject === true } : {}),
365
+ ...(receipt.validForProject !== undefined
366
+ ? { validForProject: receipt.validForProject === true }
367
+ : {}),
294
368
  ...(receipt.generatedAt ? { generatedAt: receipt.generatedAt } : {}),
295
369
  counts: receipt.counts || { create: 0, update: 0, delete: 0, unchanged: 0 },
296
370
  summary: receipt.summary || {},
297
371
  diagnostics: Array.isArray(receipt.diagnostics) ? receipt.diagnostics.slice(0, 100) : [],
372
+ ...(Array.isArray(receipt.policyViolations)
373
+ ? { policyViolations: receipt.policyViolations.slice(0, 100) }
374
+ : {}),
298
375
  };
299
376
  if (detail === "full" && receipt.changes) {
300
377
  result.changes = {};
@@ -329,9 +406,15 @@ class ProducerService {
329
406
 
330
407
  getInstance(name) {
331
408
  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");
409
+ if (!instance)
410
+ throw new ProducerHostError("Unknown configured producer.", "producer_not_found");
411
+ if (
412
+ (this.project.errors || []).some((entry) => String(entry.code || "").includes("producer"))
413
+ ) {
414
+ throw new ProducerHostError(
415
+ "Project producer configuration is invalid.",
416
+ "invalid_producer_configuration",
417
+ );
335
418
  }
336
419
  return instance;
337
420
  }
@@ -347,7 +430,10 @@ class ProducerService {
347
430
  );
348
431
  } catch (error) {
349
432
  if (error instanceof ProducerHostError) throw error;
350
- throw new ProducerHostError("The configured producer package could not be loaded.", "producer_load_failed");
433
+ throw new ProducerHostError(
434
+ "The configured producer package could not be loaded.",
435
+ "producer_load_failed",
436
+ );
351
437
  }
352
438
  const envNames = new Set();
353
439
  inspectConfig(instance.config, envNames, ["config"]);
@@ -355,28 +441,42 @@ class ProducerService {
355
441
  try {
356
442
  configValidation = await descriptor.validateConfig(structuredClone(instance.config));
357
443
  } catch {
358
- throw new ProducerHostError("Producer configuration validation failed unexpectedly.", "producer_config_validation_failed");
444
+ throw new ProducerHostError(
445
+ "Producer configuration validation failed unexpectedly.",
446
+ "producer_config_validation_failed",
447
+ );
359
448
  }
360
449
  if (!configValidation || configValidation.valid !== true) {
361
450
  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
- ))
451
+ ? configValidation.diagnostics
452
+ .slice(0, 100)
453
+ .map((entry) =>
454
+ diagnostic(
455
+ typeof entry.code === "string" ? entry.code : "producer_config_invalid",
456
+ typeof entry.field === "string" ? { field: entry.field } : undefined,
457
+ ),
458
+ )
366
459
  : [diagnostic("producer_config_invalid")];
367
460
  return { instance, descriptor, valid: false, readyToApply: false, diagnostics: entries };
368
461
  }
369
- const normalizedConfig = configValidation.config === undefined
370
- ? structuredClone(instance.config)
371
- : configValidation.config;
462
+ const normalizedConfig =
463
+ configValidation.config === undefined
464
+ ? structuredClone(instance.config)
465
+ : configValidation.config;
372
466
  const accessedSecrets = [];
373
467
  const getSecret = (secretName) => {
374
468
  if (!envNames.has(secretName)) {
375
- throw new ProducerHostError("Producer requested a secret not referenced by its configuration.", "producer_secret_not_allowed");
469
+ throw new ProducerHostError(
470
+ "Producer requested a secret not referenced by its configuration.",
471
+ "producer_secret_not_allowed",
472
+ );
376
473
  }
377
474
  const value = this.env[secretName];
378
475
  if (typeof value !== "string" || value.length === 0) {
379
- throw new ProducerHostError("A configured producer secret is unavailable.", "producer_secret_unavailable");
476
+ throw new ProducerHostError(
477
+ "A configured producer secret is unavailable.",
478
+ "producer_secret_unavailable",
479
+ );
380
480
  }
381
481
  accessedSecrets.push(value);
382
482
  return value;
@@ -389,7 +489,7 @@ class ProducerService {
389
489
  config: structuredClone(normalizedConfig),
390
490
  generatedAt,
391
491
  getSecret,
392
- signal: options && options.signal || new AbortController().signal,
492
+ signal: (options && options.signal) || new AbortController().signal,
393
493
  });
394
494
  } catch (error) {
395
495
  if (error instanceof ProducerHostError) throw error;
@@ -402,48 +502,103 @@ class ProducerService {
402
502
  summary = normalizeSummary(generated && generated.summary);
403
503
  } catch (error) {
404
504
  if (error instanceof ProducerPublicationError || error instanceof ProducerHostError) {
405
- return { instance, descriptor, valid: false, readyToApply: false, diagnostics: [publicDiagnostic(error)] };
505
+ return {
506
+ instance,
507
+ descriptor,
508
+ valid: false,
509
+ readyToApply: false,
510
+ diagnostics: [publicDiagnostic(error)],
511
+ };
406
512
  }
407
513
  throw error;
408
514
  }
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")] };
515
+ if (
516
+ files.length > MAX_PRODUCER_FILES ||
517
+ files.reduce((total, file) => total + Buffer.byteLength(file.content), 0) > MAX_PRODUCER_BYTES
518
+ ) {
519
+ return {
520
+ instance,
521
+ descriptor,
522
+ valid: false,
523
+ readyToApply: false,
524
+ diagnostics: [diagnostic("producer_output_too_large")],
525
+ };
412
526
  }
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")] };
527
+ if (
528
+ accessedSecrets.some((secret) =>
529
+ files.some((file) => secret && file.content.includes(secret)),
530
+ )
531
+ ) {
532
+ return {
533
+ instance,
534
+ descriptor,
535
+ valid: false,
536
+ readyToApply: false,
537
+ diagnostics: [diagnostic("producer_secret_leak")],
538
+ };
415
539
  }
416
540
  let inspection;
417
541
  try {
418
- inspection = this.publisher.inspect(Object.assign({}, instance, { version: descriptor.version }), files);
542
+ inspection = this.publisher.inspect(
543
+ Object.assign({}, instance, { version: descriptor.version }),
544
+ files,
545
+ );
419
546
  } catch (error) {
420
547
  if (error instanceof ProducerPublicationError) {
421
- return { instance, descriptor, files, valid: false, readyToApply: false, diagnostics: [publicDiagnostic(error)] };
548
+ return {
549
+ instance,
550
+ descriptor,
551
+ files,
552
+ valid: false,
553
+ readyToApply: false,
554
+ diagnostics: [publicDiagnostic(error)],
555
+ };
422
556
  }
423
- throw new ProducerHostError("Producer publication preflight failed.", "producer_publication_preflight_failed");
557
+ throw new ProducerHostError(
558
+ "Producer publication preflight failed.",
559
+ "producer_publication_preflight_failed",
560
+ );
424
561
  }
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 }));
562
+ const candidateFiles = (inspection.effective || files).map((file) =>
563
+ Object.assign({ bundle: instance.bundle }, file),
564
+ );
565
+ const stalePaths = inspection.changes.delete.map((relativePath) => ({
566
+ bundle: instance.bundle,
567
+ path: relativePath,
568
+ }));
428
569
  const candidate = buildCandidate(
429
570
  this.project,
430
571
  this.bundles,
431
- options && options.additionalBundles || [],
572
+ (options && options.additionalBundles) || [],
432
573
  candidateFiles,
433
574
  stalePaths,
434
575
  );
435
- const validation = validateCandidate(candidate, instance, files.map((file) => file.path));
576
+ const validation = validateCandidate(
577
+ candidate,
578
+ instance,
579
+ files.map((file) => file.path),
580
+ );
581
+ // Opt-in only (writePolicy.applyToProducers): preview reports the
582
+ // violations read-only and run refuses before publication.
583
+ const policyViolations = evaluateProducerFiles(
584
+ this.project.writePolicy,
585
+ instance,
586
+ files,
587
+ inspection.changes,
588
+ );
589
+ const allowed = !policyViolations?.length;
436
590
  return {
437
591
  instance,
438
592
  descriptor,
439
593
  files,
440
594
  changes: inspection.changes,
441
595
  counts: countsFor(inspection.changes),
442
- valid: validation.valid,
443
- readyToApply: validation.valid,
596
+ valid: validation.valid && allowed,
597
+ readyToApply: validation.valid && allowed,
444
598
  conformant: validation.conformant,
445
599
  validForProject: validation.validForProject,
446
- diagnostics: validation.diagnostics,
600
+ diagnostics: validation.diagnostics.concat(policyDiagnostics(policyViolations || [])),
601
+ ...(policyViolations ? { policyViolations } : {}),
447
602
  summary,
448
603
  generatedAt,
449
604
  };
@@ -457,20 +612,29 @@ class ProducerService {
457
612
  return this.store.withWriteLock(async () => {
458
613
  const prepared = await this.prepare(name, options);
459
614
  if (!prepared.readyToApply) return Object.assign({}, prepared, { applied: false });
460
- const producer = Object.assign({}, prepared.instance, { version: prepared.descriptor.version });
615
+ const producer = Object.assign({}, prepared.instance, {
616
+ version: prepared.descriptor.version,
617
+ });
461
618
  let changes;
462
619
  try {
463
620
  changes = await this.publisher.publish(producer, prepared.files, async () => {
464
621
  const persisted = buildCandidate(
465
622
  this.project,
466
623
  this.bundles,
467
- options && options.additionalBundles || [],
624
+ (options && options.additionalBundles) || [],
468
625
  [],
469
626
  [],
470
627
  );
471
- const validation = validateCandidate(persisted, prepared.instance, prepared.files.map((file) => file.path));
628
+ const validation = validateCandidate(
629
+ persisted,
630
+ prepared.instance,
631
+ prepared.files.map((file) => file.path),
632
+ );
472
633
  if (!validation.valid) {
473
- throw new ProducerHostError("Persisted producer output failed validation.", "persisted_producer_validation_failed");
634
+ throw new ProducerHostError(
635
+ "Persisted producer output failed validation.",
636
+ "persisted_producer_validation_failed",
637
+ );
474
638
  }
475
639
  });
476
640
  } catch (error) {