@mfdaves/okf-mcp 0.5.0 → 0.6.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/README.md +86 -20
- package/okf/bundles/okf-mcp/interfaces/mcp-tools.md +7 -3
- package/okf/bundles/okf-mcp/overview/okf-mcp.md +1 -1
- package/okf/bundles/okf-mcp/policies/authoring-safety.md +7 -1
- package/okf/bundles/okf-mcp/runtime/file-concept-store.md +4 -2
- package/okf/bundles/okf-mcp/runtime/mcp-server.md +1 -1
- package/okf/bundles/okf-mcp/workflows/concept-authoring.md +9 -3
- package/package.json +3 -2
- package/server.json +2 -2
- package/src/cli.js +63 -1
- package/src/index.js +1 -0
- package/src/indexer.js +40 -6
- package/src/live-authoring.js +945 -0
- package/src/mcp-server.js +171 -2
- package/src/search-index.js +83 -0
- package/src/search.js +35 -42
- package/src/store.js +10 -1
- package/src/v02.js +1 -0
|
@@ -0,0 +1,945 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { spawnSync: nodeSpawnSync } = require("node:child_process");
|
|
7
|
+
const { isDeepStrictEqual } = require("node:util");
|
|
8
|
+
|
|
9
|
+
const { normalizeConceptPath, renderConceptMarkdown, slug } = require("./authoring");
|
|
10
|
+
const {
|
|
11
|
+
attachProject,
|
|
12
|
+
buildIndex,
|
|
13
|
+
bundleAllowsPath,
|
|
14
|
+
resolveConcept,
|
|
15
|
+
validateIndex,
|
|
16
|
+
} = require("./indexer");
|
|
17
|
+
const { validActor } = require("./v02");
|
|
18
|
+
|
|
19
|
+
const MAX_CHANGES = 100;
|
|
20
|
+
const MANAGED_METADATA_KEYS = new Set([
|
|
21
|
+
"id",
|
|
22
|
+
"type",
|
|
23
|
+
"title",
|
|
24
|
+
"description",
|
|
25
|
+
"tags",
|
|
26
|
+
"sources",
|
|
27
|
+
"relations",
|
|
28
|
+
"generated",
|
|
29
|
+
"generated_file",
|
|
30
|
+
"generatedFile",
|
|
31
|
+
"runtime",
|
|
32
|
+
"parameters",
|
|
33
|
+
"computation",
|
|
34
|
+
"executor",
|
|
35
|
+
"attester",
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
function isPlainObject(value) {
|
|
39
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const prototype = Object.getPrototypeOf(value);
|
|
43
|
+
return prototype === Object.prototype || prototype === null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hasOwn(value, key) {
|
|
47
|
+
return Boolean(value && Object.prototype.hasOwnProperty.call(value, key));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function nonEmptyString(value, field) {
|
|
51
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
52
|
+
throw new Error(`${field} must be a non-empty string.`);
|
|
53
|
+
}
|
|
54
|
+
return value.trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function normalizeBody(value) {
|
|
58
|
+
return String(value || "").replace(/\r\n/g, "\n").trim();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function canonicalValue(value) {
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
return value.map(canonicalValue);
|
|
64
|
+
}
|
|
65
|
+
if (isPlainObject(value)) {
|
|
66
|
+
return Object.fromEntries(
|
|
67
|
+
Object.keys(value).sort().map((key) => [key, canonicalValue(value[key])]),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function exactKey(value) {
|
|
74
|
+
return JSON.stringify(canonicalValue(value));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function uniqueStrings(values, field) {
|
|
78
|
+
if (values === undefined) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(values)) {
|
|
82
|
+
throw new Error(`${field} must be an array.`);
|
|
83
|
+
}
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
const normalized = [];
|
|
86
|
+
values.forEach((value, index) => {
|
|
87
|
+
const item = nonEmptyString(value, `${field}[${index}]`);
|
|
88
|
+
if (!seen.has(item)) {
|
|
89
|
+
seen.add(item);
|
|
90
|
+
normalized.push(item);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return normalized;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function normalizeSource(value, field) {
|
|
97
|
+
if (typeof value === "string") {
|
|
98
|
+
return { resource: nonEmptyString(value, field) };
|
|
99
|
+
}
|
|
100
|
+
if (!isPlainObject(value)) {
|
|
101
|
+
throw new Error(`${field} must be a resource string or source object.`);
|
|
102
|
+
}
|
|
103
|
+
return Object.assign({}, value);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function sourceIdentity(source) {
|
|
107
|
+
if (typeof source === "string" && source.trim()) {
|
|
108
|
+
return `resource:${source.trim()}`;
|
|
109
|
+
}
|
|
110
|
+
if (isPlainObject(source) && typeof source.id === "string" && source.id.trim()) {
|
|
111
|
+
return `id:${source.id.trim()}`;
|
|
112
|
+
}
|
|
113
|
+
if (isPlainObject(source) && typeof source.resource === "string" && source.resource.trim()) {
|
|
114
|
+
return `resource:${source.resource.trim()}`;
|
|
115
|
+
}
|
|
116
|
+
return `exact:${exactKey(source)}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function sourceMatches(source, selector) {
|
|
120
|
+
const sourceId = isPlainObject(source) && typeof source.id === "string"
|
|
121
|
+
? source.id.trim()
|
|
122
|
+
: "";
|
|
123
|
+
const sourceResource = typeof source === "string"
|
|
124
|
+
? source.trim()
|
|
125
|
+
: isPlainObject(source) && typeof source.resource === "string"
|
|
126
|
+
? source.resource.trim()
|
|
127
|
+
: "";
|
|
128
|
+
if (typeof selector === "string") {
|
|
129
|
+
const value = selector.trim();
|
|
130
|
+
return sourceId === value || sourceResource === value;
|
|
131
|
+
}
|
|
132
|
+
if (!isPlainObject(selector)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (typeof selector.id === "string" && selector.id.trim()) {
|
|
136
|
+
return sourceId === selector.id.trim();
|
|
137
|
+
}
|
|
138
|
+
if (typeof selector.resource === "string" && selector.resource.trim()) {
|
|
139
|
+
return sourceResource === selector.resource.trim();
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function validateSourceSelector(selector, field) {
|
|
145
|
+
if (typeof selector === "string") {
|
|
146
|
+
nonEmptyString(selector, field);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (!isPlainObject(selector)) {
|
|
150
|
+
throw new Error(`${field} must select a source by id or resource.`);
|
|
151
|
+
}
|
|
152
|
+
const hasId = typeof selector.id === "string" && selector.id.trim();
|
|
153
|
+
const hasResource = typeof selector.resource === "string" && selector.resource.trim();
|
|
154
|
+
if (!hasId && !hasResource) {
|
|
155
|
+
throw new Error(`${field} must select a source by id or resource.`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function applySourcePatch(existing, patch, field) {
|
|
160
|
+
if (!isPlainObject(patch)) {
|
|
161
|
+
throw new Error(`${field} must be an object containing add and/or remove arrays.`);
|
|
162
|
+
}
|
|
163
|
+
const remove = patch.remove === undefined ? [] : patch.remove;
|
|
164
|
+
if (!Array.isArray(remove)) {
|
|
165
|
+
throw new Error(`${field}.remove must be an array.`);
|
|
166
|
+
}
|
|
167
|
+
remove.forEach((selector, index) => validateSourceSelector(selector, `${field}.remove[${index}]`));
|
|
168
|
+
let result = Array.isArray(existing) ? existing.slice() : [];
|
|
169
|
+
result = result.filter((source) => !remove.some((selector) => sourceMatches(source, selector)));
|
|
170
|
+
const additions = patch.add === undefined ? [] : patch.add;
|
|
171
|
+
if (!Array.isArray(additions)) {
|
|
172
|
+
throw new Error(`${field}.add must be an array.`);
|
|
173
|
+
}
|
|
174
|
+
additions.map((source, index) => normalizeSource(source, `${field}.add[${index}]`)).forEach((source) => {
|
|
175
|
+
const identity = sourceIdentity(source);
|
|
176
|
+
const existingIndex = result.findIndex((candidate) => sourceIdentity(candidate) === identity);
|
|
177
|
+
if (existingIndex === -1) {
|
|
178
|
+
result.push(source);
|
|
179
|
+
} else if (!isDeepStrictEqual(result[existingIndex], source)) {
|
|
180
|
+
result[existingIndex] = source;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const seen = new Set();
|
|
184
|
+
return result.filter((source) => {
|
|
185
|
+
const identity = sourceIdentity(source);
|
|
186
|
+
if (seen.has(identity)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
seen.add(identity);
|
|
190
|
+
return true;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function relationParts(value) {
|
|
195
|
+
if (typeof value === "string") {
|
|
196
|
+
const parts = value.trim().split(/\s+/);
|
|
197
|
+
return {
|
|
198
|
+
type: parts.length > 1 ? parts.shift() : "related_to",
|
|
199
|
+
target: parts.join(" ") || value.trim(),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (isPlainObject(value)) {
|
|
203
|
+
return {
|
|
204
|
+
type: String(value.type || value.kind || "related_to").trim(),
|
|
205
|
+
target: String(value.target || value.to || "").trim(),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return { type: "", target: "" };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function relationKey(value) {
|
|
212
|
+
const relation = relationParts(value);
|
|
213
|
+
return `${relation.type}\u0000${relation.target}`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function normalizeRelation(value, field) {
|
|
217
|
+
if (!isPlainObject(value)) {
|
|
218
|
+
throw new Error(`${field} must be an object.`);
|
|
219
|
+
}
|
|
220
|
+
const type = nonEmptyString(value.type, `${field}.type`);
|
|
221
|
+
const target = nonEmptyString(value.target, `${field}.target`);
|
|
222
|
+
const relation = { type, target };
|
|
223
|
+
if (hasOwn(value, "label")) {
|
|
224
|
+
relation.label = String(value.label);
|
|
225
|
+
}
|
|
226
|
+
if (hasOwn(value, "description")) {
|
|
227
|
+
relation.description = String(value.description);
|
|
228
|
+
}
|
|
229
|
+
return relation;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function applyRelationPatch(existing, patch, field) {
|
|
233
|
+
if (!isPlainObject(patch)) {
|
|
234
|
+
throw new Error(`${field} must be an object containing add and/or remove arrays.`);
|
|
235
|
+
}
|
|
236
|
+
const remove = patch.remove === undefined ? [] : patch.remove;
|
|
237
|
+
if (!Array.isArray(remove)) {
|
|
238
|
+
throw new Error(`${field}.remove must be an array.`);
|
|
239
|
+
}
|
|
240
|
+
const removeKeys = new Set(remove.map((relation, index) => (
|
|
241
|
+
relationKey(normalizeRelation(relation, `${field}.remove[${index}]`))
|
|
242
|
+
)));
|
|
243
|
+
let result = (Array.isArray(existing) ? existing : [])
|
|
244
|
+
.filter((relation) => !removeKeys.has(relationKey(relation)));
|
|
245
|
+
const additions = patch.add === undefined ? [] : patch.add;
|
|
246
|
+
if (!Array.isArray(additions)) {
|
|
247
|
+
throw new Error(`${field}.add must be an array.`);
|
|
248
|
+
}
|
|
249
|
+
additions.map((relation, index) => normalizeRelation(relation, `${field}.add[${index}]`)).forEach((relation) => {
|
|
250
|
+
const key = relationKey(relation);
|
|
251
|
+
const existingIndex = result.findIndex((candidate) => relationKey(candidate) === key);
|
|
252
|
+
if (existingIndex === -1) {
|
|
253
|
+
result.push(relation);
|
|
254
|
+
} else if (!isDeepStrictEqual(result[existingIndex], relation)) {
|
|
255
|
+
result[existingIndex] = relation;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
const seen = new Set();
|
|
259
|
+
return result.filter((relation) => {
|
|
260
|
+
const key = relationKey(relation);
|
|
261
|
+
if (seen.has(key)) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
seen.add(key);
|
|
265
|
+
return true;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function applyTagPatch(existing, patch, field) {
|
|
270
|
+
if (!isPlainObject(patch)) {
|
|
271
|
+
throw new Error(`${field} must be an object containing add and/or remove arrays.`);
|
|
272
|
+
}
|
|
273
|
+
const additions = uniqueStrings(patch.add, `${field}.add`);
|
|
274
|
+
const removals = new Set(uniqueStrings(patch.remove, `${field}.remove`));
|
|
275
|
+
const seen = new Set();
|
|
276
|
+
return (Array.isArray(existing) ? existing : [])
|
|
277
|
+
.filter((tag) => !removals.has(tag))
|
|
278
|
+
.concat(additions)
|
|
279
|
+
.filter((tag) => {
|
|
280
|
+
if (seen.has(tag)) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
seen.add(tag);
|
|
284
|
+
return true;
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function validateMetadata(metadata, field) {
|
|
289
|
+
if (metadata === undefined) {
|
|
290
|
+
return {};
|
|
291
|
+
}
|
|
292
|
+
if (!isPlainObject(metadata)) {
|
|
293
|
+
throw new Error(`${field} must be an object.`);
|
|
294
|
+
}
|
|
295
|
+
Object.keys(metadata).forEach((key) => {
|
|
296
|
+
if (MANAGED_METADATA_KEYS.has(key)) {
|
|
297
|
+
throw new Error(`${field}.${key} is server-managed; use its dedicated field instead.`);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
return Object.assign({}, metadata);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function validateRemovalKeys(keys, field) {
|
|
304
|
+
const values = uniqueStrings(keys, field);
|
|
305
|
+
values.forEach((key) => {
|
|
306
|
+
if (MANAGED_METADATA_KEYS.has(key)) {
|
|
307
|
+
throw new Error(`${field} cannot remove server-managed field: ${key}`);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
return values;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function actorValue(value) {
|
|
314
|
+
const actor = nonEmptyString(value, "actor");
|
|
315
|
+
if (!validActor(actor)) {
|
|
316
|
+
throw new Error("actor must use human:<id>, process:<id>, or provider/model syntax.");
|
|
317
|
+
}
|
|
318
|
+
return actor;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function isInside(root, candidate) {
|
|
322
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
323
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function generatedOwners(document) {
|
|
327
|
+
const owners = [];
|
|
328
|
+
const generated = document && document.frontmatter && document.frontmatter.generated;
|
|
329
|
+
if (isPlainObject(generated) && typeof generated.by === "string") {
|
|
330
|
+
owners.push(generated.by);
|
|
331
|
+
}
|
|
332
|
+
const normalized = document && document.signals && document.signals.generated;
|
|
333
|
+
if (isPlainObject(normalized) && typeof normalized.by === "string") {
|
|
334
|
+
owners.push(normalized.by);
|
|
335
|
+
}
|
|
336
|
+
return owners;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function commandOutput(result) {
|
|
340
|
+
return String((result && (result.stderr || result.stdout)) || "").trim().slice(0, 4000);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function commandFailure(result, fallback) {
|
|
344
|
+
return commandOutput(result)
|
|
345
|
+
|| (result && result.error && result.error.message)
|
|
346
|
+
|| fallback;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function revisionFor(text) {
|
|
350
|
+
return `sha256:${crypto.createHash("sha256").update(text).digest("hex")}`;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function temporaryPath(target) {
|
|
354
|
+
return path.join(
|
|
355
|
+
path.dirname(target),
|
|
356
|
+
`.${path.basename(target)}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function restoreFile(target, text, mode) {
|
|
361
|
+
const temporary = temporaryPath(target);
|
|
362
|
+
try {
|
|
363
|
+
fs.writeFileSync(temporary, text, { encoding: "utf8", flag: "wx" });
|
|
364
|
+
if (mode !== null && mode !== undefined) {
|
|
365
|
+
fs.chmodSync(temporary, mode);
|
|
366
|
+
}
|
|
367
|
+
fs.renameSync(temporary, target);
|
|
368
|
+
} finally {
|
|
369
|
+
if (fs.existsSync(temporary)) {
|
|
370
|
+
fs.unlinkSync(temporary);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
class LiveAuthoringService {
|
|
376
|
+
constructor(store, options) {
|
|
377
|
+
const config = options || {};
|
|
378
|
+
this.store = store;
|
|
379
|
+
this.actor = actorValue(config.actor);
|
|
380
|
+
this.gitCommit = Boolean(config.gitCommit);
|
|
381
|
+
this.spawnSync = config.spawnSync || nodeSpawnSync;
|
|
382
|
+
this.now = config.now || (() => new Date());
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
getBundle(id) {
|
|
386
|
+
const bundles = this.store.getBundles();
|
|
387
|
+
const bundle = id
|
|
388
|
+
? bundles.find((entry) => entry.id === id)
|
|
389
|
+
: bundles.length === 1
|
|
390
|
+
? bundles[0]
|
|
391
|
+
: null;
|
|
392
|
+
if (!bundle) {
|
|
393
|
+
throw new Error(id
|
|
394
|
+
? `Unknown writable OKF bundle: ${id}`
|
|
395
|
+
: "A bundle id is required unless exactly one writable OKF root is configured.");
|
|
396
|
+
}
|
|
397
|
+
return bundle;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
buildIndex(additionalBundles, documentOverrides) {
|
|
401
|
+
const index = buildIndex(this.store.getBundles().concat(additionalBundles || []), {
|
|
402
|
+
relationTypes: this.store.getRelationTypes(),
|
|
403
|
+
strictLinks: this.store.strictLinks,
|
|
404
|
+
allowCustomRelationTypes: this.store.allowCustomRelationTypes,
|
|
405
|
+
documentOverrides,
|
|
406
|
+
});
|
|
407
|
+
return this.store.project ? attachProject(index, this.store.project) : index;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
generatedOutputDirectories() {
|
|
411
|
+
const project = this.store.project;
|
|
412
|
+
if (!project || !project.root) {
|
|
413
|
+
return [];
|
|
414
|
+
}
|
|
415
|
+
return (project.plugins || [])
|
|
416
|
+
.filter((plugin) => plugin && plugin.output)
|
|
417
|
+
.map((plugin) => path.resolve(project.root, String(plugin.output)));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
assertWritableTarget(bundle, conceptPath, existing) {
|
|
421
|
+
const target = this.store.resolveConceptFile(bundle.id, conceptPath);
|
|
422
|
+
if (this.generatedOutputDirectories().some((output) => isInside(output, target.absolutePath))) {
|
|
423
|
+
throw new Error(`Concept is inside a configured generator output and must be changed through its generator: ${conceptPath}`);
|
|
424
|
+
}
|
|
425
|
+
if (existing) {
|
|
426
|
+
const frontmatter = existing.frontmatter || {};
|
|
427
|
+
if (existing.generatedFile === true
|
|
428
|
+
|| existing.isGenerated === true
|
|
429
|
+
|| frontmatter.generated_file === true
|
|
430
|
+
|| frontmatter.generatedFile === true) {
|
|
431
|
+
throw new Error(`Generated concept must be changed through its generator: ${existing.uri}`);
|
|
432
|
+
}
|
|
433
|
+
if (generatedOwners(existing).some((owner) => owner.startsWith("process:"))) {
|
|
434
|
+
throw new Error(`Process-generated concept must be changed through its generator: ${existing.uri}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return target;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
createCandidate(change, bundle, generated) {
|
|
441
|
+
const type = nonEmptyString(change.type, "changes[].type");
|
|
442
|
+
if (type === "Attested Computation") {
|
|
443
|
+
throw new Error("Live concept authoring cannot create Attested Computation contracts.");
|
|
444
|
+
}
|
|
445
|
+
const title = nonEmptyString(change.title, "changes[].title");
|
|
446
|
+
const conceptPath = normalizeConceptPath(
|
|
447
|
+
change.path || `${slug(type, "concept")}/${slug(title, "concept")}.md`,
|
|
448
|
+
);
|
|
449
|
+
if (!bundleAllowsPath(bundle, conceptPath)) {
|
|
450
|
+
throw new Error(`Concept path is excluded by the bundle policy: ${conceptPath}`);
|
|
451
|
+
}
|
|
452
|
+
const target = this.assertWritableTarget(bundle, conceptPath, null);
|
|
453
|
+
if (fs.existsSync(target.absolutePath)) {
|
|
454
|
+
throw new Error(`Concept file already exists: ${conceptPath}`);
|
|
455
|
+
}
|
|
456
|
+
const metadata = validateMetadata(change.metadata, "changes[].metadata");
|
|
457
|
+
const frontmatter = Object.assign({ type, title }, metadata);
|
|
458
|
+
if (hasOwn(change, "description")) {
|
|
459
|
+
frontmatter.description = String(change.description);
|
|
460
|
+
}
|
|
461
|
+
const tags = uniqueStrings(change.tags, "changes[].tags");
|
|
462
|
+
if (change.sources !== undefined && !Array.isArray(change.sources)) {
|
|
463
|
+
throw new Error("changes[].sources must be an array.");
|
|
464
|
+
}
|
|
465
|
+
if (change.relations !== undefined && !Array.isArray(change.relations)) {
|
|
466
|
+
throw new Error("changes[].relations must be an array.");
|
|
467
|
+
}
|
|
468
|
+
const sources = (change.sources || [])
|
|
469
|
+
.map((source, index) => normalizeSource(source, `changes[].sources[${index}]`));
|
|
470
|
+
const relations = (change.relations || [])
|
|
471
|
+
.map((relation, index) => normalizeRelation(relation, `changes[].relations[${index}]`));
|
|
472
|
+
if (tags.length) frontmatter.tags = tags;
|
|
473
|
+
if (sources.length) frontmatter.sources = applySourcePatch([], { add: sources }, "changes[].sources");
|
|
474
|
+
if (relations.length) frontmatter.relations = applyRelationPatch([], { add: relations }, "changes[].relations");
|
|
475
|
+
frontmatter.generated = generated;
|
|
476
|
+
const body = hasOwn(change, "body") ? String(change.body || "") : `# ${title}`;
|
|
477
|
+
const markdown = renderConceptMarkdown({ path: conceptPath, frontmatter, body });
|
|
478
|
+
return {
|
|
479
|
+
op: "create",
|
|
480
|
+
bundle: bundle.id,
|
|
481
|
+
path: conceptPath,
|
|
482
|
+
uri: `okf://${bundle.id}/${conceptPath.replace(/\.md$/i, "")}`,
|
|
483
|
+
title,
|
|
484
|
+
frontmatter,
|
|
485
|
+
body,
|
|
486
|
+
markdown,
|
|
487
|
+
target,
|
|
488
|
+
baseRevision: null,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
updateCandidate(change, bundle, currentIndex, generated) {
|
|
493
|
+
const uri = nonEmptyString(change.uri, "changes[].uri");
|
|
494
|
+
const existing = resolveConcept(currentIndex, uri);
|
|
495
|
+
if (!existing || !existing.valid || existing.reserved) {
|
|
496
|
+
throw new Error(`Unknown valid OKF concept: ${uri}`);
|
|
497
|
+
}
|
|
498
|
+
if (existing.bundle !== bundle.id) {
|
|
499
|
+
throw new Error(`All changes must target writable bundle ${bundle.id}: ${uri}`);
|
|
500
|
+
}
|
|
501
|
+
if (existing.type === "Attested Computation") {
|
|
502
|
+
throw new Error("Live concept authoring cannot modify Attested Computation contracts or code.");
|
|
503
|
+
}
|
|
504
|
+
const target = this.assertWritableTarget(bundle, existing.path, existing);
|
|
505
|
+
const originalText = fs.readFileSync(target.absolutePath, "utf8");
|
|
506
|
+
const originalMode = fs.statSync(target.absolutePath).mode;
|
|
507
|
+
const indexedRevision = revisionFor(existing.text);
|
|
508
|
+
const diskRevision = revisionFor(originalText);
|
|
509
|
+
if (indexedRevision !== diskRevision) {
|
|
510
|
+
throw new Error(`Concept changed after it was indexed; retry the update: ${existing.uri}`);
|
|
511
|
+
}
|
|
512
|
+
const frontmatter = Object.assign({}, existing.frontmatter);
|
|
513
|
+
Object.assign(frontmatter, validateMetadata(change.metadata, "changes[].metadata"));
|
|
514
|
+
validateRemovalKeys(change.removeMetadataKeys, "changes[].removeMetadataKeys").forEach((key) => {
|
|
515
|
+
delete frontmatter[key];
|
|
516
|
+
});
|
|
517
|
+
if (hasOwn(change, "title")) {
|
|
518
|
+
frontmatter.title = nonEmptyString(change.title, "changes[].title");
|
|
519
|
+
}
|
|
520
|
+
if (hasOwn(change, "description")) {
|
|
521
|
+
frontmatter.description = String(change.description);
|
|
522
|
+
}
|
|
523
|
+
if (hasOwn(change, "tags")) {
|
|
524
|
+
frontmatter.tags = applyTagPatch(frontmatter.tags, change.tags, "changes[].tags");
|
|
525
|
+
}
|
|
526
|
+
if (hasOwn(change, "sources")) {
|
|
527
|
+
frontmatter.sources = applySourcePatch(frontmatter.sources, change.sources, "changes[].sources");
|
|
528
|
+
}
|
|
529
|
+
if (hasOwn(change, "relations")) {
|
|
530
|
+
frontmatter.relations = applyRelationPatch(frontmatter.relations, change.relations, "changes[].relations");
|
|
531
|
+
}
|
|
532
|
+
const body = hasOwn(change, "body") ? String(change.body || "") : existing.body;
|
|
533
|
+
if (isDeepStrictEqual(frontmatter, existing.frontmatter)
|
|
534
|
+
&& normalizeBody(body) === normalizeBody(existing.body)) {
|
|
535
|
+
throw new Error(`Update has no effective changes: ${existing.uri}`);
|
|
536
|
+
}
|
|
537
|
+
frontmatter.generated = Object.assign(
|
|
538
|
+
{},
|
|
539
|
+
isPlainObject(existing.frontmatter.generated) ? existing.frontmatter.generated : {},
|
|
540
|
+
generated,
|
|
541
|
+
);
|
|
542
|
+
const markdown = renderConceptMarkdown({ path: existing.path, frontmatter, body });
|
|
543
|
+
return {
|
|
544
|
+
op: "update",
|
|
545
|
+
bundle: bundle.id,
|
|
546
|
+
path: existing.path,
|
|
547
|
+
uri: existing.uri,
|
|
548
|
+
title: frontmatter.title || existing.title,
|
|
549
|
+
frontmatter,
|
|
550
|
+
body,
|
|
551
|
+
markdown,
|
|
552
|
+
target,
|
|
553
|
+
baseRevision: diskRevision,
|
|
554
|
+
originalText,
|
|
555
|
+
originalMode,
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
validationResult(index, candidates) {
|
|
560
|
+
const validation = validateIndex(index);
|
|
561
|
+
const candidateDiagnostics = [];
|
|
562
|
+
candidates.forEach((candidate) => {
|
|
563
|
+
const document = index.byPathUri.get(`okf://${candidate.bundle}/${candidate.path}`);
|
|
564
|
+
if (!document || document.reserved || !document.valid) {
|
|
565
|
+
candidateDiagnostics.push({
|
|
566
|
+
code: "candidate_not_indexed",
|
|
567
|
+
severity: "error",
|
|
568
|
+
layer: "project",
|
|
569
|
+
bundle: candidate.bundle,
|
|
570
|
+
path: candidate.path,
|
|
571
|
+
message: "Candidate did not become a valid concept in the future graph.",
|
|
572
|
+
});
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
(document.warnings || []).filter((entry) => entry.layer === "v0.2").forEach((entry) => {
|
|
576
|
+
candidateDiagnostics.push(Object.assign({}, entry, {
|
|
577
|
+
severity: "error",
|
|
578
|
+
bundle: candidate.bundle,
|
|
579
|
+
path: candidate.path,
|
|
580
|
+
}));
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
const diagnostics = validation.diagnostics.concat(candidateDiagnostics);
|
|
584
|
+
return {
|
|
585
|
+
valid: validation.valid && candidateDiagnostics.length === 0,
|
|
586
|
+
conformant: validation.conformant,
|
|
587
|
+
validForProject: validation.validForProject && candidateDiagnostics.length === 0,
|
|
588
|
+
diagnostics,
|
|
589
|
+
errors: validation.errors,
|
|
590
|
+
warnings: validation.warnings,
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
runGit(directory, args) {
|
|
595
|
+
const environment = Object.assign({}, process.env);
|
|
596
|
+
Object.keys(environment).filter((key) => key.startsWith("GIT_")).forEach((key) => {
|
|
597
|
+
delete environment[key];
|
|
598
|
+
});
|
|
599
|
+
environment.GIT_LITERAL_PATHSPECS = "1";
|
|
600
|
+
environment.GIT_TERMINAL_PROMPT = "0";
|
|
601
|
+
return this.spawnSync("git", [
|
|
602
|
+
"-C",
|
|
603
|
+
directory,
|
|
604
|
+
"-c",
|
|
605
|
+
"core.hooksPath=/dev/null",
|
|
606
|
+
"-c",
|
|
607
|
+
"commit.gpgSign=false",
|
|
608
|
+
].concat(args), {
|
|
609
|
+
encoding: "utf8",
|
|
610
|
+
shell: false,
|
|
611
|
+
maxBuffer: 1024 * 1024,
|
|
612
|
+
timeout: 15000,
|
|
613
|
+
env: environment,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
prepareGit(bundle) {
|
|
618
|
+
if (!this.gitCommit) {
|
|
619
|
+
return { enabled: false, repository: null };
|
|
620
|
+
}
|
|
621
|
+
const discovered = this.runGit(bundle.root, ["rev-parse", "--show-toplevel"]);
|
|
622
|
+
if (discovered.error) {
|
|
623
|
+
throw new Error(`Git executable is unavailable: ${discovered.error.message}`);
|
|
624
|
+
}
|
|
625
|
+
if (discovered.status !== 0) {
|
|
626
|
+
const detail = commandOutput(discovered);
|
|
627
|
+
if (/not a git repository/i.test(detail)) {
|
|
628
|
+
return { enabled: true, repository: null };
|
|
629
|
+
}
|
|
630
|
+
throw new Error(`Could not inspect Git repository: ${detail || `git exited ${discovered.status}`}`);
|
|
631
|
+
}
|
|
632
|
+
const repository = String(discovered.stdout || "").trim();
|
|
633
|
+
const status = this.runGit(repository, ["status", "--porcelain=v1", "--untracked-files=all"]);
|
|
634
|
+
if (status.error || status.status !== 0) {
|
|
635
|
+
throw new Error(`Could not inspect Git status: ${commandFailure(status, "git status failed")}`);
|
|
636
|
+
}
|
|
637
|
+
if (String(status.stdout || "").trim()) {
|
|
638
|
+
throw new Error("Git worktree must be completely clean before an automatic OKF commit.");
|
|
639
|
+
}
|
|
640
|
+
for (const key of ["user.name", "user.email"]) {
|
|
641
|
+
const configured = this.runGit(repository, ["config", "--get", key]);
|
|
642
|
+
if (configured.error || configured.status !== 0 || !String(configured.stdout || "").trim()) {
|
|
643
|
+
throw new Error(`Git ${key} must be configured before an automatic OKF commit.`);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return { enabled: true, repository };
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
commitGit(preflight, candidates, message) {
|
|
650
|
+
if (!preflight.enabled) {
|
|
651
|
+
return { enabled: false, committed: false };
|
|
652
|
+
}
|
|
653
|
+
if (!preflight.repository) {
|
|
654
|
+
return { enabled: true, repository: false, committed: false, reason: "not_git_repository" };
|
|
655
|
+
}
|
|
656
|
+
const files = candidates.map((candidate) => (
|
|
657
|
+
path.relative(preflight.repository, candidate.target.absolutePath).replace(/\\/g, "/")
|
|
658
|
+
));
|
|
659
|
+
if (files.some((file) => !file || file === ".." || file.startsWith("../"))) {
|
|
660
|
+
return {
|
|
661
|
+
enabled: true,
|
|
662
|
+
repository: true,
|
|
663
|
+
committed: false,
|
|
664
|
+
error: "An affected concept path is outside the detected Git repository.",
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
const add = this.runGit(preflight.repository, ["add", "--"].concat(files));
|
|
668
|
+
if (add.error || add.status !== 0) {
|
|
669
|
+
return {
|
|
670
|
+
enabled: true,
|
|
671
|
+
repository: true,
|
|
672
|
+
committed: false,
|
|
673
|
+
error: commandFailure(add, "git add failed"),
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
const commit = this.runGit(preflight.repository, ["commit", "-m", message, "--"].concat(files));
|
|
677
|
+
if (commit.error || commit.status !== 0) {
|
|
678
|
+
return {
|
|
679
|
+
enabled: true,
|
|
680
|
+
repository: true,
|
|
681
|
+
committed: false,
|
|
682
|
+
error: commandFailure(commit, "git commit failed"),
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
const revision = this.runGit(preflight.repository, ["rev-parse", "HEAD"]);
|
|
686
|
+
return {
|
|
687
|
+
enabled: true,
|
|
688
|
+
repository: true,
|
|
689
|
+
committed: true,
|
|
690
|
+
commitSha: revision.status === 0 ? String(revision.stdout || "").trim() : null,
|
|
691
|
+
...(revision.status === 0 ? {} : { warning: commandFailure(revision, "Commit succeeded but its SHA could not be read.") }),
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
defaultMessage(candidates) {
|
|
696
|
+
if (candidates.length === 1) {
|
|
697
|
+
return `docs(okf): ${candidates[0].op === "create" ? "add" : "update"} ${candidates[0].title}`;
|
|
698
|
+
}
|
|
699
|
+
return `docs(okf): apply ${candidates.length} concept changes`;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
ensureParentDirectories(candidate, createdDirectories) {
|
|
703
|
+
const bundleRoot = this.store.resolveWritableBundleRoot(this.store.getBundle(candidate.bundle));
|
|
704
|
+
const parent = path.dirname(candidate.target.absolutePath);
|
|
705
|
+
const relative = path.relative(bundleRoot, parent);
|
|
706
|
+
let current = bundleRoot;
|
|
707
|
+
for (const segment of relative.split(path.sep).filter(Boolean)) {
|
|
708
|
+
current = path.join(current, segment);
|
|
709
|
+
if (fs.existsSync(current)) {
|
|
710
|
+
const stat = fs.lstatSync(current);
|
|
711
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
712
|
+
throw new Error(`Concept parent is not a safe directory: ${current}`);
|
|
713
|
+
}
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
fs.mkdirSync(current);
|
|
717
|
+
createdDirectories.push(current);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
cleanupDirectories(createdDirectories) {
|
|
722
|
+
createdDirectories.slice().sort((left, right) => right.length - left.length).forEach((directory) => {
|
|
723
|
+
try {
|
|
724
|
+
fs.rmdirSync(directory);
|
|
725
|
+
} catch (error) {
|
|
726
|
+
if (!error || !["ENOENT", "ENOTEMPTY"].includes(error.code)) {
|
|
727
|
+
throw error;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
stageCandidates(candidates) {
|
|
734
|
+
const staged = [];
|
|
735
|
+
const createdDirectories = [];
|
|
736
|
+
try {
|
|
737
|
+
candidates.forEach((candidate) => {
|
|
738
|
+
this.ensureParentDirectories(candidate, createdDirectories);
|
|
739
|
+
const temporary = temporaryPath(candidate.target.absolutePath);
|
|
740
|
+
fs.writeFileSync(temporary, candidate.markdown, { encoding: "utf8", flag: "wx" });
|
|
741
|
+
const stagedCandidate = Object.assign({}, candidate, { temporary });
|
|
742
|
+
staged.push(stagedCandidate);
|
|
743
|
+
if (candidate.op === "update") {
|
|
744
|
+
fs.chmodSync(temporary, candidate.originalMode);
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
return { staged, createdDirectories };
|
|
748
|
+
} catch (error) {
|
|
749
|
+
staged.forEach((entry) => {
|
|
750
|
+
if (fs.existsSync(entry.temporary)) fs.unlinkSync(entry.temporary);
|
|
751
|
+
});
|
|
752
|
+
this.cleanupDirectories(createdDirectories);
|
|
753
|
+
throw error;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
assertRevisions(staged) {
|
|
758
|
+
staged.forEach((candidate) => {
|
|
759
|
+
const exists = fs.existsSync(candidate.target.absolutePath);
|
|
760
|
+
if (candidate.op === "create" && exists) {
|
|
761
|
+
throw new Error(`Concept file appeared during batch preparation: ${candidate.path}`);
|
|
762
|
+
}
|
|
763
|
+
if (candidate.op === "update") {
|
|
764
|
+
if (!exists) {
|
|
765
|
+
throw new Error(`Concept file disappeared during batch preparation: ${candidate.path}`);
|
|
766
|
+
}
|
|
767
|
+
const currentRevision = revisionFor(fs.readFileSync(candidate.target.absolutePath));
|
|
768
|
+
if (currentRevision !== candidate.baseRevision) {
|
|
769
|
+
throw new Error(`Concept changed during batch preparation: ${candidate.path}`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
rollback(applied, staged, createdDirectories) {
|
|
776
|
+
const failures = [];
|
|
777
|
+
staged.forEach((candidate) => {
|
|
778
|
+
if (fs.existsSync(candidate.temporary)) {
|
|
779
|
+
try {
|
|
780
|
+
fs.unlinkSync(candidate.temporary);
|
|
781
|
+
} catch (error) {
|
|
782
|
+
failures.push(error);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
applied.slice().reverse().forEach((candidate) => {
|
|
787
|
+
try {
|
|
788
|
+
if (candidate.op === "create") {
|
|
789
|
+
if (fs.existsSync(candidate.target.absolutePath)) fs.unlinkSync(candidate.target.absolutePath);
|
|
790
|
+
} else {
|
|
791
|
+
restoreFile(candidate.target.absolutePath, candidate.originalText, candidate.originalMode);
|
|
792
|
+
}
|
|
793
|
+
} catch (error) {
|
|
794
|
+
failures.push(error);
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
try {
|
|
798
|
+
this.cleanupDirectories(createdDirectories || []);
|
|
799
|
+
} catch (error) {
|
|
800
|
+
failures.push(error);
|
|
801
|
+
}
|
|
802
|
+
if (failures.length) {
|
|
803
|
+
throw new Error(`OKF batch rollback failed: ${failures[0].message}`);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
async applyChanges(input, context) {
|
|
808
|
+
return this.store.withWriteLock(() => this.applyChangesUnlocked(input, context));
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
async applyChangesUnlocked(input, context) {
|
|
812
|
+
if (!isPlainObject(input)) {
|
|
813
|
+
throw new Error("okf_apply_changes requires an object argument.");
|
|
814
|
+
}
|
|
815
|
+
if (!Array.isArray(input.changes) || input.changes.length < 1 || input.changes.length > MAX_CHANGES) {
|
|
816
|
+
throw new Error(`changes must contain from 1 through ${MAX_CHANGES} operations.`);
|
|
817
|
+
}
|
|
818
|
+
const bundle = this.getBundle(input.bundle);
|
|
819
|
+
const timestampValue = this.now();
|
|
820
|
+
const generated = {
|
|
821
|
+
by: this.actor,
|
|
822
|
+
at: timestampValue instanceof Date
|
|
823
|
+
? timestampValue.toISOString()
|
|
824
|
+
: new Date(timestampValue).toISOString(),
|
|
825
|
+
};
|
|
826
|
+
const additionalBundles = context && Array.isArray(context.additionalBundles)
|
|
827
|
+
? context.additionalBundles
|
|
828
|
+
: [];
|
|
829
|
+
const currentIndex = this.buildIndex(additionalBundles);
|
|
830
|
+
const candidates = input.changes.map((change, index) => {
|
|
831
|
+
if (!isPlainObject(change)) {
|
|
832
|
+
throw new Error(`changes[${index}] must be an object.`);
|
|
833
|
+
}
|
|
834
|
+
if (change.op === "create") {
|
|
835
|
+
return this.createCandidate(change, bundle, generated);
|
|
836
|
+
}
|
|
837
|
+
if (change.op === "update") {
|
|
838
|
+
return this.updateCandidate(change, bundle, currentIndex, generated);
|
|
839
|
+
}
|
|
840
|
+
throw new Error(`changes[${index}].op must be create or update.`);
|
|
841
|
+
});
|
|
842
|
+
const targetPaths = new Set();
|
|
843
|
+
candidates.forEach((candidate) => {
|
|
844
|
+
const key = path.resolve(candidate.target.absolutePath);
|
|
845
|
+
if (targetPaths.has(key)) {
|
|
846
|
+
throw new Error(`A batch cannot target the same concept more than once: ${candidate.path}`);
|
|
847
|
+
}
|
|
848
|
+
targetPaths.add(key);
|
|
849
|
+
});
|
|
850
|
+
const overrides = new Map(candidates.map((candidate) => [
|
|
851
|
+
`${candidate.bundle}\u0000${candidate.path}`,
|
|
852
|
+
candidate.markdown,
|
|
853
|
+
]));
|
|
854
|
+
const futureIndex = this.buildIndex(additionalBundles, overrides);
|
|
855
|
+
const validation = this.validationResult(futureIndex, candidates);
|
|
856
|
+
if (!validation.valid) {
|
|
857
|
+
return {
|
|
858
|
+
applied: false,
|
|
859
|
+
status: "rejected",
|
|
860
|
+
generated,
|
|
861
|
+
validation,
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
const gitPreflight = this.prepareGit(bundle);
|
|
865
|
+
const transaction = this.stageCandidates(candidates);
|
|
866
|
+
const staged = transaction.staged;
|
|
867
|
+
const applied = [];
|
|
868
|
+
try {
|
|
869
|
+
this.assertRevisions(staged);
|
|
870
|
+
staged.forEach((candidate) => {
|
|
871
|
+
if (candidate.op === "create") {
|
|
872
|
+
fs.linkSync(candidate.temporary, candidate.target.absolutePath);
|
|
873
|
+
applied.push(candidate);
|
|
874
|
+
fs.unlinkSync(candidate.temporary);
|
|
875
|
+
} else {
|
|
876
|
+
const currentRevision = revisionFor(fs.readFileSync(candidate.target.absolutePath));
|
|
877
|
+
if (currentRevision !== candidate.baseRevision) {
|
|
878
|
+
throw new Error(`Concept changed immediately before publication: ${candidate.path}`);
|
|
879
|
+
}
|
|
880
|
+
fs.renameSync(candidate.temporary, candidate.target.absolutePath);
|
|
881
|
+
applied.push(candidate);
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
} catch (error) {
|
|
885
|
+
this.rollback(applied, staged, transaction.createdDirectories);
|
|
886
|
+
throw error;
|
|
887
|
+
}
|
|
888
|
+
let persistedValidation;
|
|
889
|
+
try {
|
|
890
|
+
persistedValidation = this.validationResult(
|
|
891
|
+
this.buildIndex(additionalBundles),
|
|
892
|
+
candidates,
|
|
893
|
+
);
|
|
894
|
+
} catch (error) {
|
|
895
|
+
this.rollback(applied, staged, transaction.createdDirectories);
|
|
896
|
+
throw error;
|
|
897
|
+
}
|
|
898
|
+
if (!persistedValidation.valid) {
|
|
899
|
+
this.rollback(applied, staged, transaction.createdDirectories);
|
|
900
|
+
return {
|
|
901
|
+
applied: false,
|
|
902
|
+
status: "rolled_back",
|
|
903
|
+
generated,
|
|
904
|
+
validation: persistedValidation,
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
const message = hasOwn(input, "message") && String(input.message).trim()
|
|
908
|
+
? String(input.message).trim()
|
|
909
|
+
: this.defaultMessage(candidates);
|
|
910
|
+
let git;
|
|
911
|
+
try {
|
|
912
|
+
git = this.commitGit(gitPreflight, candidates, message);
|
|
913
|
+
} catch (error) {
|
|
914
|
+
git = {
|
|
915
|
+
enabled: gitPreflight.enabled,
|
|
916
|
+
repository: Boolean(gitPreflight.repository),
|
|
917
|
+
committed: false,
|
|
918
|
+
error: error && error.message ? error.message : String(error),
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
const summaries = candidates.map((candidate) => ({
|
|
922
|
+
op: candidate.op,
|
|
923
|
+
bundle: candidate.bundle,
|
|
924
|
+
path: candidate.path,
|
|
925
|
+
uri: candidate.uri,
|
|
926
|
+
title: candidate.title,
|
|
927
|
+
}));
|
|
928
|
+
return {
|
|
929
|
+
applied: true,
|
|
930
|
+
status: git.enabled && git.repository && !git.committed
|
|
931
|
+
? "applied_uncommitted"
|
|
932
|
+
: "applied",
|
|
933
|
+
generated,
|
|
934
|
+
changes: summaries,
|
|
935
|
+
validation,
|
|
936
|
+
git,
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
module.exports = {
|
|
942
|
+
LiveAuthoringService,
|
|
943
|
+
MAX_CHANGES,
|
|
944
|
+
MANAGED_METADATA_KEYS,
|
|
945
|
+
};
|