@mfdaves/okf-mcp 0.3.3 → 0.5.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.
- package/.agents/skills/okf-v02-migration/SKILL.md +37 -0
- package/.agents/skills/okf-v02-migration/agents/openai.yaml +6 -0
- package/.agents/skills/okf-v02-migration/references/field-mapping.md +26 -0
- package/README.md +217 -79
- package/okf/bundles/okf-mcp/distribution/reference-bundle.md +2 -2
- package/okf/bundles/okf-mcp/index.md +7 -0
- package/okf/bundles/okf-mcp/interfaces/http-authoring-api.md +2 -2
- package/okf/bundles/okf-mcp/interfaces/mcp-tools.md +9 -5
- package/okf/bundles/okf-mcp/overview/okf-mcp.md +2 -2
- package/okf/bundles/okf-mcp/policies/authoring-safety.md +5 -3
- package/okf/bundles/okf-mcp/runtime/file-concept-store.md +7 -5
- package/okf/bundles/okf-mcp/runtime/indexer.md +6 -4
- package/okf/bundles/okf-mcp/runtime/mcp-server.md +7 -7
- package/okf/bundles/okf-mcp/specs/attested-computation.md +24 -0
- package/okf/bundles/okf-mcp/specs/concept-format.md +5 -3
- package/okf/bundles/okf-mcp/specs/git-sources.md +23 -0
- package/okf/bundles/okf-mcp/workflows/v02-migration.md +24 -0
- package/package.json +10 -3
- package/server.json +5 -5
- package/src/assets.js +469 -0
- package/src/authoring.js +492 -20
- package/src/cli.js +468 -37
- package/src/computation.js +366 -0
- package/src/git-source.js +551 -0
- package/src/graph.js +71 -18
- package/src/http-server.js +8 -1
- package/src/index.js +5 -0
- package/src/indexer.js +428 -24
- package/src/markdown.js +129 -0
- package/src/mcp-server.js +450 -315
- package/src/migration.js +687 -0
- package/src/parser.js +135 -54
- package/src/plugins.js +30 -50
- package/src/project.js +74 -17
- package/src/remote.js +232 -17
- package/src/search-index.js +83 -0
- package/src/search.js +103 -47
- package/src/store.js +421 -25
- package/src/v02.js +545 -0
- package/src/validation.js +13 -2
- package/src/mcp-protocol.js +0 -283
package/src/store.js
CHANGED
|
@@ -4,7 +4,7 @@ const crypto = require("crypto");
|
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const path = require("path");
|
|
6
6
|
const { buildIndex } = require("./indexer");
|
|
7
|
-
const { loadProjectConfig } = require("./project");
|
|
7
|
+
const { DEFAULT_RELATION_TYPES, loadProjectConfig } = require("./project");
|
|
8
8
|
const { normalizeConceptPath } = require("./authoring");
|
|
9
9
|
|
|
10
10
|
function nowIso() {
|
|
@@ -22,21 +22,52 @@ function ensureDir(dir) {
|
|
|
22
22
|
fs.mkdirSync(dir, { recursive: true });
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
function
|
|
25
|
+
function normalizeProposalId(id) {
|
|
26
26
|
const clean = String(id || "").trim();
|
|
27
|
-
if (!/^[
|
|
27
|
+
if (!/^[a-z0-9_.-]+$/.test(clean)) {
|
|
28
28
|
throw new Error(`Invalid proposal id: ${id || "<missing>"}`);
|
|
29
29
|
}
|
|
30
|
+
return clean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function safeProposalFile(root, id) {
|
|
34
|
+
const clean = normalizeProposalId(id);
|
|
30
35
|
return path.join(root, `${clean}.json`);
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
function readJson(filePath) {
|
|
34
|
-
|
|
39
|
+
const stat = fs.lstatSync(filePath);
|
|
40
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
41
|
+
throw new Error("Proposal records must be regular files and cannot be symbolic links.");
|
|
42
|
+
}
|
|
43
|
+
let descriptor;
|
|
44
|
+
try {
|
|
45
|
+
descriptor = fs.openSync(filePath, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
|
|
46
|
+
return JSON.parse(fs.readFileSync(descriptor, "utf8"));
|
|
47
|
+
} finally {
|
|
48
|
+
if (descriptor !== undefined) {
|
|
49
|
+
fs.closeSync(descriptor);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
35
52
|
}
|
|
36
53
|
|
|
37
54
|
function writeJson(filePath, value) {
|
|
38
55
|
ensureDir(path.dirname(filePath));
|
|
39
|
-
fs.
|
|
56
|
+
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isSymbolicLink()) {
|
|
57
|
+
throw new Error("Proposal records cannot be symbolic links.");
|
|
58
|
+
}
|
|
59
|
+
const temporaryPath = path.join(
|
|
60
|
+
path.dirname(filePath),
|
|
61
|
+
`.${path.basename(filePath)}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`,
|
|
62
|
+
);
|
|
63
|
+
try {
|
|
64
|
+
fs.writeFileSync(temporaryPath, JSON.stringify(value, null, 2) + "\n", { encoding: "utf8", flag: "wx" });
|
|
65
|
+
fs.renameSync(temporaryPath, filePath);
|
|
66
|
+
} finally {
|
|
67
|
+
if (fs.existsSync(temporaryPath)) {
|
|
68
|
+
fs.unlinkSync(temporaryPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
40
71
|
}
|
|
41
72
|
|
|
42
73
|
function revisionFor(text) {
|
|
@@ -76,17 +107,34 @@ function isInside(root, candidate) {
|
|
|
76
107
|
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
77
108
|
}
|
|
78
109
|
|
|
79
|
-
function
|
|
110
|
+
function normalizeBundleFilePath(value) {
|
|
111
|
+
const raw = String(value || "").trim().replace(/\\/g, "/");
|
|
112
|
+
if (!raw || raw.startsWith("/") || raw.includes("\0")) {
|
|
113
|
+
throw new Error("Bundle file path must be a safe relative path.");
|
|
114
|
+
}
|
|
115
|
+
const normalized = path.posix.normalize(raw);
|
|
116
|
+
if (!normalized || normalized === "." || normalized === ".." || normalized.startsWith("../")) {
|
|
117
|
+
throw new Error("Bundle file path must stay inside the bundle.");
|
|
118
|
+
}
|
|
119
|
+
return normalized;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function rejectSymlinkTraversal(realRoot, relativePath, label) {
|
|
80
123
|
const segments = relativePath.split("/");
|
|
81
124
|
let current = realRoot;
|
|
82
125
|
segments.forEach((segment, index) => {
|
|
83
126
|
current = path.join(current, segment);
|
|
84
|
-
|
|
85
|
-
|
|
127
|
+
let stat;
|
|
128
|
+
try {
|
|
129
|
+
stat = fs.lstatSync(current);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error && error.code === "ENOENT") {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
throw error;
|
|
86
135
|
}
|
|
87
|
-
const stat = fs.lstatSync(current);
|
|
88
136
|
if (stat.isSymbolicLink()) {
|
|
89
|
-
throw new Error(
|
|
137
|
+
throw new Error(`${label || "Concept path"} cannot traverse a symbolic link: ${segments.slice(0, index + 1).join("/")}`);
|
|
90
138
|
}
|
|
91
139
|
if (index < segments.length - 1 && !stat.isDirectory()) {
|
|
92
140
|
throw new Error(`Concept path parent is not a directory: ${segments.slice(0, index + 1).join("/")}`);
|
|
@@ -103,15 +151,68 @@ class FileConceptStore {
|
|
|
103
151
|
this.project = options.project || null;
|
|
104
152
|
this.bundles = (options.bundles || []).filter((bundle) => !bundle.remote);
|
|
105
153
|
this.relationTypes = options.relationTypes || [];
|
|
106
|
-
this.
|
|
154
|
+
this.allowCustomRelationTypes = Boolean(options.allowCustomRelationTypes);
|
|
155
|
+
this.strictLinks = Boolean(options.strictLinks || (this.project && this.project.strictLinks));
|
|
156
|
+
this.projectRoot = this.project ? fs.realpathSync(this.project.root) : null;
|
|
157
|
+
const configuredProposalRoot = options.proposalRoot || ".okf-proposals";
|
|
158
|
+
this.proposalRoot = path.resolve(
|
|
159
|
+
this.projectRoot || process.cwd(),
|
|
160
|
+
configuredProposalRoot,
|
|
161
|
+
);
|
|
162
|
+
if (this.projectRoot) {
|
|
163
|
+
const relative = path.relative(this.projectRoot, this.proposalRoot);
|
|
164
|
+
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
165
|
+
throw new Error("Proposal root must be a dedicated directory inside the project root.");
|
|
166
|
+
}
|
|
167
|
+
rejectSymlinkTraversal(this.projectRoot, relative.replace(/\\/g, "/"), "Proposal root");
|
|
168
|
+
} else if (fs.existsSync(this.proposalRoot) && fs.lstatSync(this.proposalRoot).isSymbolicLink()) {
|
|
169
|
+
throw new Error("Proposal root cannot be a symbolic link.");
|
|
170
|
+
}
|
|
171
|
+
this.proposalLocks = new Map();
|
|
107
172
|
}
|
|
108
173
|
|
|
109
174
|
static fromProject(projectPath, options) {
|
|
110
175
|
const project = loadProjectConfig(projectPath);
|
|
176
|
+
if (project.errors.length) {
|
|
177
|
+
throw new Error("Cannot configure writable concept storage from an invalid project configuration.");
|
|
178
|
+
}
|
|
111
179
|
return new FileConceptStore({
|
|
112
180
|
project,
|
|
113
181
|
bundles: project.bundles,
|
|
114
182
|
relationTypes: project.relationTypes,
|
|
183
|
+
strictLinks: project.strictLinks,
|
|
184
|
+
proposalRoot: options && options.proposalRoot,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static fromRoot(rootPath, options) {
|
|
189
|
+
const requestedRoot = path.resolve(rootPath || ".");
|
|
190
|
+
const root = fs.realpathSync(requestedRoot);
|
|
191
|
+
if (!fs.statSync(root).isDirectory()) {
|
|
192
|
+
throw new Error("Writable OKF root must be an existing directory.");
|
|
193
|
+
}
|
|
194
|
+
const id = String((options && options.id) || path.basename(root) || "bundle")
|
|
195
|
+
.replace(/[^A-Za-z0-9_.-]+/g, "-")
|
|
196
|
+
.replace(/^-+|-+$/g, "") || "bundle";
|
|
197
|
+
const bundles = [{ id, root, include: [], exclude: [] }];
|
|
198
|
+
const project = {
|
|
199
|
+
project: id,
|
|
200
|
+
root,
|
|
201
|
+
path: null,
|
|
202
|
+
bundles,
|
|
203
|
+
remoteBundles: [],
|
|
204
|
+
relationTypes: DEFAULT_RELATION_TYPES.slice(),
|
|
205
|
+
plugins: [],
|
|
206
|
+
strictLinks: Boolean(options && options.strictLinks),
|
|
207
|
+
errors: [],
|
|
208
|
+
rootMode: true,
|
|
209
|
+
};
|
|
210
|
+
return new FileConceptStore({
|
|
211
|
+
project,
|
|
212
|
+
bundles,
|
|
213
|
+
relationTypes: project.relationTypes,
|
|
214
|
+
allowCustomRelationTypes: true,
|
|
215
|
+
strictLinks: project.strictLinks,
|
|
115
216
|
proposalRoot: options && options.proposalRoot,
|
|
116
217
|
});
|
|
117
218
|
}
|
|
@@ -125,7 +226,11 @@ class FileConceptStore {
|
|
|
125
226
|
}
|
|
126
227
|
|
|
127
228
|
getIndex() {
|
|
128
|
-
return buildIndex(this.bundles, {
|
|
229
|
+
return buildIndex(this.bundles, {
|
|
230
|
+
relationTypes: this.relationTypes,
|
|
231
|
+
strictLinks: this.strictLinks,
|
|
232
|
+
allowCustomRelationTypes: this.allowCustomRelationTypes,
|
|
233
|
+
});
|
|
129
234
|
}
|
|
130
235
|
|
|
131
236
|
getBundle(id) {
|
|
@@ -136,10 +241,29 @@ class FileConceptStore {
|
|
|
136
241
|
return bundle;
|
|
137
242
|
}
|
|
138
243
|
|
|
244
|
+
resolveWritableBundleRoot(bundle) {
|
|
245
|
+
const configuredRoot = path.resolve(bundle.root);
|
|
246
|
+
if (this.projectRoot) {
|
|
247
|
+
if (!isInside(this.projectRoot, configuredRoot)) {
|
|
248
|
+
throw new Error("Writable bundle root resolves outside the project root.");
|
|
249
|
+
}
|
|
250
|
+
const relative = path.relative(this.projectRoot, configuredRoot).replace(/\\/g, "/");
|
|
251
|
+
rejectSymlinkTraversal(this.projectRoot, relative, "Writable bundle root");
|
|
252
|
+
}
|
|
253
|
+
const realRoot = fs.realpathSync(configuredRoot);
|
|
254
|
+
if (!fs.statSync(realRoot).isDirectory()) {
|
|
255
|
+
throw new Error("Writable bundle root must be an existing directory.");
|
|
256
|
+
}
|
|
257
|
+
if (this.projectRoot && !isInside(this.projectRoot, realRoot)) {
|
|
258
|
+
throw new Error("Writable bundle root resolves outside the project root.");
|
|
259
|
+
}
|
|
260
|
+
return realRoot;
|
|
261
|
+
}
|
|
262
|
+
|
|
139
263
|
resolveConceptFile(bundleId, conceptPath) {
|
|
140
264
|
const bundle = this.getBundle(bundleId);
|
|
141
265
|
const relativePath = normalizeConceptPath(conceptPath);
|
|
142
|
-
const realRoot =
|
|
266
|
+
const realRoot = this.resolveWritableBundleRoot(bundle);
|
|
143
267
|
const absolutePath = path.resolve(realRoot, ...relativePath.split("/"));
|
|
144
268
|
if (!isInside(realRoot, absolutePath)) {
|
|
145
269
|
throw new Error("Concept path resolves outside bundle root.");
|
|
@@ -148,6 +272,18 @@ class FileConceptStore {
|
|
|
148
272
|
return { relativePath, absolutePath };
|
|
149
273
|
}
|
|
150
274
|
|
|
275
|
+
resolveBundleFile(bundleId, filePath) {
|
|
276
|
+
const bundle = this.getBundle(bundleId);
|
|
277
|
+
const relativePath = normalizeBundleFilePath(filePath);
|
|
278
|
+
const realRoot = this.resolveWritableBundleRoot(bundle);
|
|
279
|
+
const absolutePath = path.resolve(realRoot, ...relativePath.split("/"));
|
|
280
|
+
if (!isInside(realRoot, absolutePath)) {
|
|
281
|
+
throw new Error("Bundle file path resolves outside bundle root.");
|
|
282
|
+
}
|
|
283
|
+
rejectSymlinkTraversal(realRoot, relativePath);
|
|
284
|
+
return { relativePath, absolutePath };
|
|
285
|
+
}
|
|
286
|
+
|
|
151
287
|
getConceptRevision(bundleId, conceptPath) {
|
|
152
288
|
const { relativePath, absolutePath } = this.resolveConceptFile(bundleId, conceptPath);
|
|
153
289
|
if (!fs.existsSync(absolutePath)) {
|
|
@@ -160,14 +296,76 @@ class FileConceptStore {
|
|
|
160
296
|
return revisionFor(text);
|
|
161
297
|
}
|
|
162
298
|
|
|
299
|
+
proposalDirectory(relativeDirectory, create) {
|
|
300
|
+
const relative = String(relativeDirectory || "").replace(/\\/g, "/");
|
|
301
|
+
if (relative && !/^[A-Za-z0-9_.-]+$/.test(relative)) {
|
|
302
|
+
throw new Error("Invalid proposal directory.");
|
|
303
|
+
}
|
|
304
|
+
const directory = relative ? path.join(this.proposalRoot, relative) : this.proposalRoot;
|
|
305
|
+
if (this.projectRoot) {
|
|
306
|
+
const fromProject = path.relative(this.projectRoot, directory);
|
|
307
|
+
if (!fromProject || fromProject.startsWith("..") || path.isAbsolute(fromProject)) {
|
|
308
|
+
throw new Error("Proposal directory resolves outside the project root.");
|
|
309
|
+
}
|
|
310
|
+
rejectSymlinkTraversal(this.projectRoot, fromProject.replace(/\\/g, "/"), "Proposal path");
|
|
311
|
+
} else {
|
|
312
|
+
const fromProposalRoot = path.relative(this.proposalRoot, directory);
|
|
313
|
+
if (fromProposalRoot.startsWith("..") || path.isAbsolute(fromProposalRoot)) {
|
|
314
|
+
throw new Error("Proposal directory resolves outside the configured proposal root.");
|
|
315
|
+
}
|
|
316
|
+
if (fs.existsSync(this.proposalRoot) && fs.lstatSync(this.proposalRoot).isSymbolicLink()) {
|
|
317
|
+
throw new Error("Proposal root cannot be a symbolic link.");
|
|
318
|
+
}
|
|
319
|
+
if (relative) {
|
|
320
|
+
rejectSymlinkTraversal(this.proposalRoot, relative, "Proposal path");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (create) {
|
|
324
|
+
ensureDir(directory);
|
|
325
|
+
if (this.projectRoot) {
|
|
326
|
+
const fromProject = path.relative(this.projectRoot, directory).replace(/\\/g, "/");
|
|
327
|
+
rejectSymlinkTraversal(this.projectRoot, fromProject, "Proposal path");
|
|
328
|
+
if (!isInside(this.projectRoot, fs.realpathSync(directory))) {
|
|
329
|
+
throw new Error("Proposal directory resolves outside the project root.");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return directory;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
proposalFile(id, relativeDirectory, createDirectory) {
|
|
337
|
+
return safeProposalFile(this.proposalDirectory(relativeDirectory, createDirectory), id);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async withProposalLock(id, operation) {
|
|
341
|
+
const key = normalizeProposalId(id);
|
|
342
|
+
const previous = this.proposalLocks.get(key) || Promise.resolve();
|
|
343
|
+
let release;
|
|
344
|
+
const gate = new Promise((resolve) => {
|
|
345
|
+
release = resolve;
|
|
346
|
+
});
|
|
347
|
+
const tail = previous.then(() => gate);
|
|
348
|
+
this.proposalLocks.set(key, tail);
|
|
349
|
+
await previous;
|
|
350
|
+
try {
|
|
351
|
+
return await operation(key);
|
|
352
|
+
} finally {
|
|
353
|
+
release();
|
|
354
|
+
if (this.proposalLocks.get(key) === tail) {
|
|
355
|
+
this.proposalLocks.delete(key);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
163
360
|
listProposalFiles() {
|
|
164
|
-
|
|
361
|
+
const root = this.proposalDirectory("", false);
|
|
362
|
+
if (!fs.existsSync(root)) {
|
|
165
363
|
return [];
|
|
166
364
|
}
|
|
167
|
-
return fs.readdirSync(
|
|
365
|
+
return fs.readdirSync(root)
|
|
168
366
|
.filter((name) => name.endsWith(".json"))
|
|
169
367
|
.sort()
|
|
170
|
-
.map((name) => path.join(
|
|
368
|
+
.map((name) => path.join(root, name));
|
|
171
369
|
}
|
|
172
370
|
|
|
173
371
|
async saveProposal(input) {
|
|
@@ -189,10 +387,14 @@ class FileConceptStore {
|
|
|
189
387
|
markdown: input.markdown || "",
|
|
190
388
|
message: input.message || "",
|
|
191
389
|
validation: input.validation || null,
|
|
390
|
+
assetFiles: Array.isArray(input.assetFiles) ? input.assetFiles : [],
|
|
391
|
+
dependencyRevisions: Array.isArray(input.dependencyRevisions) ? input.dependencyRevisions : [],
|
|
392
|
+
migrationId: input.migrationId || null,
|
|
393
|
+
prerequisites: Array.isArray(input.prerequisites) ? input.prerequisites : [],
|
|
192
394
|
createdAt: timestamp,
|
|
193
395
|
updatedAt: timestamp,
|
|
194
396
|
};
|
|
195
|
-
writeJson(
|
|
397
|
+
writeJson(this.proposalFile(id, "", true), proposal);
|
|
196
398
|
return proposal;
|
|
197
399
|
}
|
|
198
400
|
|
|
@@ -215,13 +417,15 @@ class FileConceptStore {
|
|
|
215
417
|
path: proposal.path,
|
|
216
418
|
uri: proposal.uri,
|
|
217
419
|
message: proposal.message,
|
|
420
|
+
assetFileCount: Array.isArray(proposal.assetFiles) ? proposal.assetFiles.length : 0,
|
|
421
|
+
migrationId: proposal.migrationId || null,
|
|
218
422
|
createdAt: proposal.createdAt,
|
|
219
423
|
updatedAt: proposal.updatedAt,
|
|
220
424
|
}));
|
|
221
425
|
}
|
|
222
426
|
|
|
223
427
|
async getProposal(id) {
|
|
224
|
-
const filePath =
|
|
428
|
+
const filePath = this.proposalFile(id, "", false);
|
|
225
429
|
if (!fs.existsSync(filePath)) {
|
|
226
430
|
throw new Error(`Unknown OKF proposal: ${id || "<missing>"}`);
|
|
227
431
|
}
|
|
@@ -230,7 +434,7 @@ class FileConceptStore {
|
|
|
230
434
|
|
|
231
435
|
saveExistingProposal(proposal) {
|
|
232
436
|
proposal.updatedAt = nowIso();
|
|
233
|
-
writeJson(
|
|
437
|
+
writeJson(this.proposalFile(proposal.id, "", true), proposal);
|
|
234
438
|
return proposal;
|
|
235
439
|
}
|
|
236
440
|
|
|
@@ -249,18 +453,64 @@ class FileConceptStore {
|
|
|
249
453
|
};
|
|
250
454
|
}
|
|
251
455
|
|
|
252
|
-
async acceptProposal(id, authoringService) {
|
|
456
|
+
async acceptProposal(id, authoringService, options) {
|
|
457
|
+
return this.withProposalLock(id, (canonicalId) => this.acceptProposalUnlocked(canonicalId, authoringService, options));
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async acceptProposalUnlocked(id, authoringService, options) {
|
|
253
461
|
const proposal = await this.getProposal(id);
|
|
254
462
|
if (proposal.status !== "proposed") {
|
|
255
463
|
throw new Error(`Only proposed concepts can be accepted. Current status: ${proposal.status}`);
|
|
256
464
|
}
|
|
257
465
|
const op = proposal.op || "create";
|
|
258
|
-
if (
|
|
466
|
+
if (!["create", "update", "create_computation", "migration_index"].includes(op)) {
|
|
259
467
|
throw new Error(`Unsupported proposal operation: ${op}`);
|
|
260
468
|
}
|
|
469
|
+
if (op === "create_computation" && !(options && options.allowComputation)) {
|
|
470
|
+
throw new Error("Accepting an Attested Computation proposal requires the computation-authoring capability.");
|
|
471
|
+
}
|
|
472
|
+
if (op === "migration_index") {
|
|
473
|
+
for (const prerequisite of proposal.prerequisites || []) {
|
|
474
|
+
const required = await this.getProposal(prerequisite);
|
|
475
|
+
if (required.status !== "accepted") {
|
|
476
|
+
throw new Error(`Migration index proposal requires accepted child proposal: ${prerequisite}`);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const target = this.resolveBundleFile(proposal.bundle, proposal.path);
|
|
480
|
+
if (!fs.existsSync(target.absolutePath)) {
|
|
481
|
+
throw new Error(`Reserved index does not exist: ${proposal.path}`);
|
|
482
|
+
}
|
|
483
|
+
const currentRevision = revisionFor(fs.readFileSync(target.absolutePath));
|
|
484
|
+
if (!proposal.baseRevision || currentRevision !== proposal.baseRevision) {
|
|
485
|
+
return this.markProposalConflict(proposal, currentRevision);
|
|
486
|
+
}
|
|
487
|
+
const validation = authoringService.validateReservedIndexMigration(proposal);
|
|
488
|
+
if (!validation.valid) {
|
|
489
|
+
proposal.validation = validation;
|
|
490
|
+
this.saveExistingProposal(proposal);
|
|
491
|
+
return { accepted: false, proposal, validation };
|
|
492
|
+
}
|
|
493
|
+
const originalText = fs.readFileSync(target.absolutePath, "utf8");
|
|
494
|
+
const write = writeFileAtomic(target.absolutePath, proposal.markdown, proposal.baseRevision);
|
|
495
|
+
if (!write.written) {
|
|
496
|
+
return this.markProposalConflict(proposal, write.currentRevision);
|
|
497
|
+
}
|
|
498
|
+
proposal.status = "accepted";
|
|
499
|
+
proposal.acceptedAt = nowIso();
|
|
500
|
+
proposal.validation = validation;
|
|
501
|
+
try {
|
|
502
|
+
this.saveExistingProposal(proposal);
|
|
503
|
+
} catch (error) {
|
|
504
|
+
writeFileAtomic(target.absolutePath, originalText, revisionFor(proposal.markdown));
|
|
505
|
+
proposal.status = "proposed";
|
|
506
|
+
delete proposal.acceptedAt;
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
return { accepted: true, created: false, updated: true, proposal, validation };
|
|
510
|
+
}
|
|
261
511
|
const { relativePath, absolutePath } = this.resolveConceptFile(proposal.bundle, proposal.path);
|
|
262
512
|
const exists = fs.existsSync(absolutePath);
|
|
263
|
-
if (op === "create" && exists) {
|
|
513
|
+
if ((op === "create" || op === "create_computation") && exists) {
|
|
264
514
|
throw new Error(`Concept file already exists: ${relativePath}`);
|
|
265
515
|
}
|
|
266
516
|
if (op === "update" && !exists) {
|
|
@@ -289,34 +539,147 @@ class FileConceptStore {
|
|
|
289
539
|
};
|
|
290
540
|
const validation = op === "update"
|
|
291
541
|
? authoringService.validateUpdateCandidate(candidate, existing)
|
|
292
|
-
:
|
|
542
|
+
: op === "create_computation"
|
|
543
|
+
? authoringService.validateAttestedComputation(candidate, {
|
|
544
|
+
assetFiles: proposal.assetFiles || [],
|
|
545
|
+
accepting: true,
|
|
546
|
+
})
|
|
547
|
+
: authoringService.validateConcept(candidate);
|
|
293
548
|
if (!validation.valid) {
|
|
294
549
|
proposal.validation = validation;
|
|
295
550
|
this.saveExistingProposal(proposal);
|
|
296
551
|
return { accepted: false, proposal, validation };
|
|
297
552
|
}
|
|
553
|
+
if (op === "create_computation") {
|
|
554
|
+
const normalizeDependencies = (entries) => (Array.isArray(entries) ? entries : [])
|
|
555
|
+
.map((entry) => ({
|
|
556
|
+
field: entry.field,
|
|
557
|
+
bundle: entry.bundle,
|
|
558
|
+
path: entry.path,
|
|
559
|
+
bytes: entry.bytes,
|
|
560
|
+
revision: entry.revision,
|
|
561
|
+
}))
|
|
562
|
+
.sort((left, right) => `${left.field}\u0000${left.bundle}\u0000${left.path}`.localeCompare(`${right.field}\u0000${right.bundle}\u0000${right.path}`));
|
|
563
|
+
const expectedDependencies = normalizeDependencies(proposal.dependencyRevisions);
|
|
564
|
+
const actualDependencies = normalizeDependencies(validation.dependencyRevisions);
|
|
565
|
+
if (JSON.stringify(expectedDependencies) !== JSON.stringify(actualDependencies)) {
|
|
566
|
+
proposal.conflict = {
|
|
567
|
+
code: "computation_dependency_changed",
|
|
568
|
+
expectedDependencies,
|
|
569
|
+
actualDependencies,
|
|
570
|
+
detectedAt: nowIso(),
|
|
571
|
+
};
|
|
572
|
+
proposal.validation = validation;
|
|
573
|
+
this.saveExistingProposal(proposal);
|
|
574
|
+
return {
|
|
575
|
+
accepted: false,
|
|
576
|
+
conflict: true,
|
|
577
|
+
message: "A referenced computation dependency changed after review. Create a new proposal from the current artifacts.",
|
|
578
|
+
proposal,
|
|
579
|
+
validation,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
let rollbackAcceptance = null;
|
|
298
584
|
if (op === "update") {
|
|
299
585
|
const currentRevision = this.getConceptRevision(proposal.bundle, proposal.path);
|
|
300
586
|
if (currentRevision !== proposal.baseRevision) {
|
|
301
587
|
return this.markProposalConflict(proposal, currentRevision);
|
|
302
588
|
}
|
|
303
589
|
const safeTarget = this.resolveConceptFile(proposal.bundle, proposal.path);
|
|
590
|
+
const originalText = fs.readFileSync(safeTarget.absolutePath, "utf8");
|
|
304
591
|
const write = writeFileAtomic(safeTarget.absolutePath, validation.markdown, proposal.baseRevision);
|
|
305
592
|
if (!write.written) {
|
|
306
593
|
return this.markProposalConflict(proposal, write.currentRevision);
|
|
307
594
|
}
|
|
595
|
+
rollbackAcceptance = () => {
|
|
596
|
+
const rollback = writeFileAtomic(
|
|
597
|
+
safeTarget.absolutePath,
|
|
598
|
+
originalText,
|
|
599
|
+
revisionFor(validation.markdown),
|
|
600
|
+
);
|
|
601
|
+
if (!rollback.written) {
|
|
602
|
+
throw new Error("Could not roll back an update after proposal status persistence failed.");
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
} else if (op === "create_computation") {
|
|
606
|
+
const assetTargets = (proposal.assetFiles || []).map((asset) => {
|
|
607
|
+
const target = this.resolveBundleFile(proposal.bundle, asset.path);
|
|
608
|
+
if (fs.existsSync(target.absolutePath)) {
|
|
609
|
+
throw new Error(`Computation asset already exists: ${target.relativePath}`);
|
|
610
|
+
}
|
|
611
|
+
return Object.assign({}, asset, target);
|
|
612
|
+
});
|
|
613
|
+
const staged = [];
|
|
614
|
+
const written = [];
|
|
615
|
+
const allTargets = assetTargets.concat([{ relativePath, absolutePath, content: validation.markdown }]);
|
|
616
|
+
const uniqueTargets = new Set(allTargets.map((target) => path.resolve(target.absolutePath)));
|
|
617
|
+
if (uniqueTargets.size !== allTargets.length) {
|
|
618
|
+
throw new Error("Computation proposal targets must resolve to distinct files.");
|
|
619
|
+
}
|
|
620
|
+
try {
|
|
621
|
+
allTargets.forEach((target) => {
|
|
622
|
+
ensureDir(path.dirname(target.absolutePath));
|
|
623
|
+
const temporaryPath = path.join(
|
|
624
|
+
path.dirname(target.absolutePath),
|
|
625
|
+
`.${path.basename(target.absolutePath)}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`,
|
|
626
|
+
);
|
|
627
|
+
fs.writeFileSync(temporaryPath, target.content, { encoding: "utf8", flag: "wx" });
|
|
628
|
+
staged.push({ temporaryPath, absolutePath: target.absolutePath });
|
|
629
|
+
});
|
|
630
|
+
staged.forEach((target) => {
|
|
631
|
+
fs.renameSync(target.temporaryPath, target.absolutePath);
|
|
632
|
+
written.push(target.absolutePath);
|
|
633
|
+
});
|
|
634
|
+
rollbackAcceptance = () => {
|
|
635
|
+
written.slice().reverse().forEach((target) => {
|
|
636
|
+
if (fs.existsSync(target)) {
|
|
637
|
+
fs.unlinkSync(target);
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
};
|
|
641
|
+
} catch (error) {
|
|
642
|
+
staged.forEach((target) => {
|
|
643
|
+
if (fs.existsSync(target.temporaryPath)) {
|
|
644
|
+
fs.unlinkSync(target.temporaryPath);
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
written.forEach((target) => {
|
|
648
|
+
if (fs.existsSync(target)) {
|
|
649
|
+
fs.unlinkSync(target);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
throw error;
|
|
653
|
+
}
|
|
308
654
|
} else {
|
|
309
655
|
ensureDir(path.dirname(absolutePath));
|
|
310
656
|
const safeTarget = this.resolveConceptFile(proposal.bundle, proposal.path);
|
|
311
657
|
fs.writeFileSync(safeTarget.absolutePath, validation.markdown, { encoding: "utf8", flag: "wx" });
|
|
658
|
+
rollbackAcceptance = () => {
|
|
659
|
+
if (fs.existsSync(safeTarget.absolutePath)) {
|
|
660
|
+
fs.unlinkSync(safeTarget.absolutePath);
|
|
661
|
+
}
|
|
662
|
+
};
|
|
312
663
|
}
|
|
313
664
|
proposal.status = "accepted";
|
|
314
665
|
proposal.acceptedAt = nowIso();
|
|
315
666
|
proposal.validation = validation;
|
|
316
|
-
|
|
667
|
+
try {
|
|
668
|
+
this.saveExistingProposal(proposal);
|
|
669
|
+
} catch (error) {
|
|
670
|
+
try {
|
|
671
|
+
if (rollbackAcceptance) {
|
|
672
|
+
rollbackAcceptance();
|
|
673
|
+
}
|
|
674
|
+
} finally {
|
|
675
|
+
proposal.status = "proposed";
|
|
676
|
+
delete proposal.acceptedAt;
|
|
677
|
+
}
|
|
678
|
+
throw error;
|
|
679
|
+
}
|
|
317
680
|
return {
|
|
318
681
|
accepted: true,
|
|
319
|
-
created: op === "create",
|
|
682
|
+
created: op === "create" || op === "create_computation",
|
|
320
683
|
updated: op === "update",
|
|
321
684
|
proposal,
|
|
322
685
|
validation,
|
|
@@ -324,6 +687,10 @@ class FileConceptStore {
|
|
|
324
687
|
}
|
|
325
688
|
|
|
326
689
|
async rejectProposal(id, reason) {
|
|
690
|
+
return this.withProposalLock(id, (canonicalId) => this.rejectProposalUnlocked(canonicalId, reason));
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
async rejectProposalUnlocked(id, reason) {
|
|
327
694
|
const proposal = await this.getProposal(id);
|
|
328
695
|
if (proposal.status !== "proposed") {
|
|
329
696
|
throw new Error(`Only proposed concepts can be rejected. Current status: ${proposal.status}`);
|
|
@@ -334,8 +701,37 @@ class FileConceptStore {
|
|
|
334
701
|
this.saveExistingProposal(proposal);
|
|
335
702
|
return proposal;
|
|
336
703
|
}
|
|
704
|
+
|
|
705
|
+
async saveMigrationManifest(input) {
|
|
706
|
+
const id = input.id || proposalId();
|
|
707
|
+
const timestamp = nowIso();
|
|
708
|
+
const manifest = Object.assign({
|
|
709
|
+
id,
|
|
710
|
+
status: "proposed",
|
|
711
|
+
kind: "okf-v0.2-stage-a",
|
|
712
|
+
createdAt: timestamp,
|
|
713
|
+
updatedAt: timestamp,
|
|
714
|
+
}, input, { id });
|
|
715
|
+
writeJson(this.proposalFile(id, "migrations", true), manifest);
|
|
716
|
+
return manifest;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
removeProposal(id) {
|
|
720
|
+
const filePath = this.proposalFile(id, "", false);
|
|
721
|
+
if (fs.existsSync(filePath)) {
|
|
722
|
+
fs.unlinkSync(filePath);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
removeMigrationManifest(id) {
|
|
727
|
+
const filePath = this.proposalFile(id, "migrations", false);
|
|
728
|
+
if (fs.existsSync(filePath)) {
|
|
729
|
+
fs.unlinkSync(filePath);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
337
732
|
}
|
|
338
733
|
|
|
339
734
|
module.exports = {
|
|
340
735
|
FileConceptStore,
|
|
736
|
+
normalizeBundleFilePath,
|
|
341
737
|
};
|