@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/v02.js
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { itemLiteral, markdownStructure, sectionNodes } = require("./markdown");
|
|
4
|
+
|
|
5
|
+
function isPlainObject(value) {
|
|
6
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const prototype = Object.getPrototypeOf(value);
|
|
10
|
+
return prototype === Object.prototype || prototype === null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function hasOwn(value, key) {
|
|
14
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function nonEmptyString(value) {
|
|
18
|
+
return typeof value === "string" && value.trim() !== "";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function validActor(value) {
|
|
22
|
+
if (!nonEmptyString(value) || /\s/.test(value)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return /^human:.+/.test(value)
|
|
26
|
+
|| /^process:.+/.test(value)
|
|
27
|
+
|| /^[^/]+\/[^/]+$/.test(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function diagnostic(code, field, message) {
|
|
31
|
+
return {
|
|
32
|
+
code,
|
|
33
|
+
field,
|
|
34
|
+
severity: "warning",
|
|
35
|
+
layer: "v0.2",
|
|
36
|
+
message,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function validIsoDate(value) {
|
|
41
|
+
if (typeof value !== "string") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
45
|
+
if (!match) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const year = Number(match[1]);
|
|
49
|
+
const month = Number(match[2]);
|
|
50
|
+
const day = Number(match[3]);
|
|
51
|
+
if (month < 1 || month > 12 || day < 1) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
55
|
+
const monthDays = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
56
|
+
return day <= monthDays[month - 1];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function validIsoDateTime(value) {
|
|
60
|
+
if (typeof value !== "string") {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const match = value.match(
|
|
64
|
+
/^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.\d{1,9})?)?(Z|[+-](\d{2}):(\d{2}))$/,
|
|
65
|
+
);
|
|
66
|
+
if (!match || !validIsoDate(match[1])) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const hour = Number(match[2]);
|
|
70
|
+
const minute = Number(match[3]);
|
|
71
|
+
const second = match[4] === undefined ? 0 : Number(match[4]);
|
|
72
|
+
const offsetHour = match[6] === undefined ? 0 : Number(match[6]);
|
|
73
|
+
const offsetMinute = match[7] === undefined ? 0 : Number(match[7]);
|
|
74
|
+
return hour <= 23
|
|
75
|
+
&& minute <= 59
|
|
76
|
+
&& second <= 59
|
|
77
|
+
&& offsetHour <= 23
|
|
78
|
+
&& offsetMinute <= 59
|
|
79
|
+
&& Number.isFinite(Date.parse(value));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function normalizeAsOf(value) {
|
|
83
|
+
if (value === undefined || value === null || value === "") {
|
|
84
|
+
return new Date().toISOString().slice(0, 10);
|
|
85
|
+
}
|
|
86
|
+
if (value instanceof Date) {
|
|
87
|
+
if (!Number.isFinite(value.getTime())) {
|
|
88
|
+
throw new TypeError("asOf must be a valid Date, ISO date, or ISO datetime.");
|
|
89
|
+
}
|
|
90
|
+
return value.toISOString().slice(0, 10);
|
|
91
|
+
}
|
|
92
|
+
if (validIsoDate(value)) {
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
if (validIsoDateTime(value)) {
|
|
96
|
+
return new Date(value).toISOString().slice(0, 10);
|
|
97
|
+
}
|
|
98
|
+
throw new TypeError("asOf must be a valid Date, ISO date, or ISO datetime.");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function normalizeUsageWindow(value, field, diagnostics) {
|
|
102
|
+
if (value === undefined) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (!isPlainObject(value) || !validIsoDate(value.from) || !validIsoDate(value.to) || value.from > value.to) {
|
|
106
|
+
diagnostics.push(diagnostic(
|
|
107
|
+
"invalid_usage_window",
|
|
108
|
+
field,
|
|
109
|
+
"usage_window must contain valid from and to ISO dates with from not later than to.",
|
|
110
|
+
));
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return Object.assign({}, value, {
|
|
114
|
+
from: value.from,
|
|
115
|
+
to: value.to,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function extractLegacyCitations(body) {
|
|
120
|
+
const structure = markdownStructure(body);
|
|
121
|
+
const citations = [];
|
|
122
|
+
structure.headings
|
|
123
|
+
.filter((heading) => heading.level === 1 && heading.text.toLowerCase() === "citations")
|
|
124
|
+
.forEach((heading) => {
|
|
125
|
+
const lists = new Set(sectionNodes(heading, { stopAtAnyHeading: true }).filter((node) => node.type === "list"));
|
|
126
|
+
structure.items
|
|
127
|
+
.filter((item) => lists.has(item.node.parent))
|
|
128
|
+
.forEach((item) => {
|
|
129
|
+
const raw = itemLiteral(structure, item);
|
|
130
|
+
if (!raw) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const markdownLink = raw.match(/^\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)$/);
|
|
134
|
+
citations.push({
|
|
135
|
+
...(markdownLink ? { title: markdownLink[1].trim() } : {}),
|
|
136
|
+
resource: markdownLink ? markdownLink[2].trim() : raw.replace(/^`|`$/g, ""),
|
|
137
|
+
legacy: true,
|
|
138
|
+
valid: true,
|
|
139
|
+
raw,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return citations;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function normalizeSources(frontmatter, body, diagnostics) {
|
|
148
|
+
const native = hasOwn(frontmatter, "sources");
|
|
149
|
+
const usageWindow = normalizeUsageWindow(frontmatter.usage_window, "usage_window", diagnostics);
|
|
150
|
+
if (!native) {
|
|
151
|
+
const legacy = extractLegacyCitations(body);
|
|
152
|
+
return {
|
|
153
|
+
sources: legacy,
|
|
154
|
+
origin: legacy.length ? "legacy_citations" : "none",
|
|
155
|
+
usageWindow,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (!Array.isArray(frontmatter.sources)) {
|
|
159
|
+
diagnostics.push(diagnostic("invalid_sources", "sources", "sources must be a list when present."));
|
|
160
|
+
return { sources: [], origin: "native", usageWindow };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const sources = frontmatter.sources.map((entry, index) => {
|
|
164
|
+
const field = `sources[${index}]`;
|
|
165
|
+
if (!isPlainObject(entry)) {
|
|
166
|
+
diagnostics.push(diagnostic("invalid_source_entry", field, "Each sources entry must be a mapping."));
|
|
167
|
+
return { raw: entry, legacy: false, valid: false };
|
|
168
|
+
}
|
|
169
|
+
let valid = true;
|
|
170
|
+
if (!nonEmptyString(entry.resource)) {
|
|
171
|
+
diagnostics.push(diagnostic("missing_source_resource", `${field}.resource`, "A source entry requires a non-empty resource."));
|
|
172
|
+
valid = false;
|
|
173
|
+
}
|
|
174
|
+
if (hasOwn(entry, "id") && !nonEmptyString(entry.id)) {
|
|
175
|
+
diagnostics.push(diagnostic("invalid_source_id", `${field}.id`, "A source id must be a non-empty scalar."));
|
|
176
|
+
valid = false;
|
|
177
|
+
}
|
|
178
|
+
if (hasOwn(entry, "author") && !nonEmptyString(entry.author)) {
|
|
179
|
+
diagnostics.push(diagnostic("invalid_source_author", `${field}.author`, "A source author must be a non-empty actor scalar."));
|
|
180
|
+
valid = false;
|
|
181
|
+
}
|
|
182
|
+
if (hasOwn(entry, "usage_count") && (
|
|
183
|
+
typeof entry.usage_count !== "number"
|
|
184
|
+
|| !Number.isFinite(entry.usage_count)
|
|
185
|
+
|| entry.usage_count < 0
|
|
186
|
+
)) {
|
|
187
|
+
diagnostics.push(diagnostic("invalid_source_usage_count", `${field}.usage_count`, "A source usage_count must be a non-negative finite number."));
|
|
188
|
+
valid = false;
|
|
189
|
+
}
|
|
190
|
+
if (hasOwn(entry, "last_modified") && !validIsoDate(entry.last_modified)) {
|
|
191
|
+
diagnostics.push(diagnostic("invalid_source_last_modified", `${field}.last_modified`, "A source last_modified value must be an ISO date."));
|
|
192
|
+
valid = false;
|
|
193
|
+
}
|
|
194
|
+
const localDiagnosticsBefore = diagnostics.length;
|
|
195
|
+
const entryUsageWindow = normalizeUsageWindow(entry.usage_window, `${field}.usage_window`, diagnostics);
|
|
196
|
+
if (diagnostics.length > localDiagnosticsBefore) {
|
|
197
|
+
valid = false;
|
|
198
|
+
}
|
|
199
|
+
return Object.assign({}, entry, {
|
|
200
|
+
legacy: false,
|
|
201
|
+
valid,
|
|
202
|
+
usageWindow: entryUsageWindow || usageWindow,
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
return { sources, origin: "native", usageWindow };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function normalizeGenerated(frontmatter, diagnostics) {
|
|
209
|
+
if (hasOwn(frontmatter, "generated")) {
|
|
210
|
+
const value = frontmatter.generated;
|
|
211
|
+
if (!isPlainObject(value)) {
|
|
212
|
+
diagnostics.push(diagnostic("invalid_generated", "generated", "generated must be a mapping when present."));
|
|
213
|
+
return { generated: null, origin: "native" };
|
|
214
|
+
}
|
|
215
|
+
let valid = true;
|
|
216
|
+
if (!validActor(value.by)) {
|
|
217
|
+
diagnostics.push(diagnostic("missing_generated_by", "generated.by", "generated.by requires a non-empty actor."));
|
|
218
|
+
valid = false;
|
|
219
|
+
}
|
|
220
|
+
if (hasOwn(value, "at") && !validIsoDateTime(value.at)) {
|
|
221
|
+
diagnostics.push(diagnostic("invalid_generated_at", "generated.at", "generated.at must be an ISO datetime when present."));
|
|
222
|
+
valid = false;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
generated: Object.assign({}, value, {
|
|
226
|
+
by: validActor(value.by) ? value.by : null,
|
|
227
|
+
at: hasOwn(value, "at") && validIsoDateTime(value.at) ? value.at : null,
|
|
228
|
+
legacy: false,
|
|
229
|
+
valid,
|
|
230
|
+
}),
|
|
231
|
+
origin: "native",
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (hasOwn(frontmatter, "timestamp")) {
|
|
235
|
+
if (!validIsoDateTime(frontmatter.timestamp)) {
|
|
236
|
+
diagnostics.push(diagnostic("invalid_legacy_timestamp", "timestamp", "Legacy timestamp must be an ISO datetime."));
|
|
237
|
+
return { generated: null, origin: "legacy_timestamp" };
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
generated: {
|
|
241
|
+
by: null,
|
|
242
|
+
at: frontmatter.timestamp,
|
|
243
|
+
legacy: true,
|
|
244
|
+
valid: true,
|
|
245
|
+
},
|
|
246
|
+
origin: "legacy_timestamp",
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return { generated: null, origin: "none" };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function normalizeVerified(value, present, diagnostics) {
|
|
253
|
+
if (!present) {
|
|
254
|
+
return { events: [], valid: true };
|
|
255
|
+
}
|
|
256
|
+
const candidates = isPlainObject(value) ? [value] : value;
|
|
257
|
+
if (!Array.isArray(candidates)) {
|
|
258
|
+
diagnostics.push(diagnostic("invalid_verified", "verified", "verified must be a mapping or a list of mappings."));
|
|
259
|
+
return { events: [], valid: false };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const events = [];
|
|
263
|
+
let valid = true;
|
|
264
|
+
candidates.forEach((entry, index) => {
|
|
265
|
+
const field = `verified[${index}]`;
|
|
266
|
+
if (!isPlainObject(entry)) {
|
|
267
|
+
diagnostics.push(diagnostic("invalid_verified_event", field, "Each verification event must be a mapping."));
|
|
268
|
+
valid = false;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (!validActor(entry.by)) {
|
|
272
|
+
diagnostics.push(diagnostic("invalid_verified_actor", `${field}.by`, "A verification event requires a non-empty actor."));
|
|
273
|
+
valid = false;
|
|
274
|
+
}
|
|
275
|
+
if (!validIsoDateTime(entry.at)) {
|
|
276
|
+
diagnostics.push(diagnostic("invalid_verified_at", `${field}.at`, "A verification event requires an ISO datetime."));
|
|
277
|
+
valid = false;
|
|
278
|
+
}
|
|
279
|
+
events.push(Object.assign({}, entry, {
|
|
280
|
+
by: validActor(entry.by) ? entry.by : null,
|
|
281
|
+
at: validIsoDateTime(entry.at) ? entry.at : null,
|
|
282
|
+
}));
|
|
283
|
+
});
|
|
284
|
+
return valid ? { events, valid: true } : { events: [], valid: false };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function deriveTrustTier(events, valid) {
|
|
288
|
+
if (!valid || !Array.isArray(events) || events.length === 0) {
|
|
289
|
+
return "unverified";
|
|
290
|
+
}
|
|
291
|
+
return events.some((entry) => /^human:.+/.test(String(entry.by || "")))
|
|
292
|
+
? "human-reviewed"
|
|
293
|
+
: "machine-confirmed";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function normalizeLifecycle(frontmatter, asOf, diagnostics) {
|
|
297
|
+
let status = "stable";
|
|
298
|
+
if (hasOwn(frontmatter, "status")) {
|
|
299
|
+
if (typeof frontmatter.status === "string" && ["draft", "stable", "deprecated"].includes(frontmatter.status)) {
|
|
300
|
+
status = frontmatter.status;
|
|
301
|
+
} else {
|
|
302
|
+
status = null;
|
|
303
|
+
diagnostics.push(diagnostic("invalid_status", "status", "status must be draft, stable, or deprecated."));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let staleAfter = null;
|
|
308
|
+
let freshness = "unspecified";
|
|
309
|
+
if (hasOwn(frontmatter, "stale_after")) {
|
|
310
|
+
if (validIsoDate(frontmatter.stale_after)) {
|
|
311
|
+
staleAfter = frontmatter.stale_after;
|
|
312
|
+
freshness = asOf >= staleAfter ? "stale" : "fresh";
|
|
313
|
+
} else {
|
|
314
|
+
freshness = "invalid";
|
|
315
|
+
diagnostics.push(diagnostic("invalid_stale_after", "stale_after", "stale_after must be an ISO date."));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return { status, staleAfter, freshness, asOf };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function extractInlineComputation(body) {
|
|
322
|
+
const structure = markdownStructure(body);
|
|
323
|
+
const blocks = [];
|
|
324
|
+
const sections = structure.headings.filter((heading) => (
|
|
325
|
+
heading.level === 1 && heading.text.toLowerCase() === "computation"
|
|
326
|
+
));
|
|
327
|
+
sections.forEach((heading) => {
|
|
328
|
+
const nodes = new Set(sectionNodes(heading, { stopAtAnyHeading: true }));
|
|
329
|
+
structure.codeBlocks
|
|
330
|
+
.filter((block) => nodes.has(block.node))
|
|
331
|
+
.forEach((block) => {
|
|
332
|
+
blocks.push({
|
|
333
|
+
language: block.language,
|
|
334
|
+
content: block.content,
|
|
335
|
+
closed: Boolean(block.fenced && block.closed),
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
sectionCount: sections.length,
|
|
341
|
+
blocks,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function normalizeParameters(value, diagnostics) {
|
|
346
|
+
if (value === undefined) {
|
|
347
|
+
return [];
|
|
348
|
+
}
|
|
349
|
+
if (!Array.isArray(value)) {
|
|
350
|
+
diagnostics.push(diagnostic("invalid_computation_parameters", "parameters", "parameters must be a list when present."));
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
let valid = true;
|
|
354
|
+
const seen = new Set();
|
|
355
|
+
const parameters = value.map((entry, index) => {
|
|
356
|
+
const field = `parameters[${index}]`;
|
|
357
|
+
if (!isPlainObject(entry)
|
|
358
|
+
|| !nonEmptyString(entry.name)
|
|
359
|
+
|| !nonEmptyString(entry.type)
|
|
360
|
+
|| typeof entry.required !== "boolean") {
|
|
361
|
+
diagnostics.push(diagnostic(
|
|
362
|
+
"invalid_computation_parameter",
|
|
363
|
+
field,
|
|
364
|
+
"Each parameter requires non-empty name and type scalars plus a boolean required field.",
|
|
365
|
+
));
|
|
366
|
+
valid = false;
|
|
367
|
+
return isPlainObject(entry) ? Object.assign({}, entry, { valid: false }) : { raw: entry, valid: false };
|
|
368
|
+
}
|
|
369
|
+
const name = String(entry.name);
|
|
370
|
+
if (seen.has(name)) {
|
|
371
|
+
diagnostics.push(diagnostic("duplicate_computation_parameter", `${field}.name`, `Duplicate computation parameter: ${name}`));
|
|
372
|
+
valid = false;
|
|
373
|
+
}
|
|
374
|
+
seen.add(name);
|
|
375
|
+
return Object.assign({}, entry, {
|
|
376
|
+
name,
|
|
377
|
+
type: String(entry.type),
|
|
378
|
+
valid: true,
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
return valid ? parameters : null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function normalizeExecutor(value, diagnostics) {
|
|
385
|
+
if (value === undefined) {
|
|
386
|
+
diagnostics.push(diagnostic("missing_computation_executor", "executor", "An attestable computation requires an executor contract."));
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
if (!isPlainObject(value)) {
|
|
390
|
+
diagnostics.push(diagnostic("invalid_computation_executor", "executor", "executor must be a mapping."));
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
let valid = true;
|
|
394
|
+
if (!nonEmptyString(value.resource)) {
|
|
395
|
+
diagnostics.push(diagnostic("missing_executor_resource", "executor.resource", "executor.resource must be a non-empty path or URI."));
|
|
396
|
+
valid = false;
|
|
397
|
+
}
|
|
398
|
+
if (!Array.isArray(value.receipt)
|
|
399
|
+
|| value.receipt.length === 0
|
|
400
|
+
|| value.receipt.some((entry) => !nonEmptyString(entry))) {
|
|
401
|
+
diagnostics.push(diagnostic("invalid_executor_receipt", "executor.receipt", "executor.receipt must be a non-empty list of field names."));
|
|
402
|
+
valid = false;
|
|
403
|
+
}
|
|
404
|
+
const receipt = Array.isArray(value.receipt) ? value.receipt.map(String) : [];
|
|
405
|
+
if (new Set(receipt).size !== receipt.length) {
|
|
406
|
+
diagnostics.push(diagnostic("duplicate_executor_receipt_field", "executor.receipt", "executor.receipt field names must be unique."));
|
|
407
|
+
valid = false;
|
|
408
|
+
}
|
|
409
|
+
return Object.assign({}, value, {
|
|
410
|
+
resource: nonEmptyString(value.resource) ? value.resource : null,
|
|
411
|
+
receipt,
|
|
412
|
+
valid,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function normalizeAttester(value, diagnostics) {
|
|
417
|
+
if (value === undefined) {
|
|
418
|
+
diagnostics.push(diagnostic("missing_computation_attester", "attester", "An attestable computation requires an attester contract."));
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
if (!isPlainObject(value)) {
|
|
422
|
+
diagnostics.push(diagnostic("invalid_computation_attester", "attester", "attester must be a mapping."));
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
const valid = nonEmptyString(value.resource);
|
|
426
|
+
if (!valid) {
|
|
427
|
+
diagnostics.push(diagnostic("missing_attester_resource", "attester.resource", "attester.resource must be a non-empty path or URI."));
|
|
428
|
+
}
|
|
429
|
+
return Object.assign({}, value, {
|
|
430
|
+
resource: valid ? value.resource : null,
|
|
431
|
+
valid,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function normalizeAttestedComputation(frontmatter, body, diagnostics) {
|
|
436
|
+
if (frontmatter.type !== "Attested Computation") {
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
const start = diagnostics.length;
|
|
440
|
+
let runtime = null;
|
|
441
|
+
if (!hasOwn(frontmatter, "runtime")) {
|
|
442
|
+
diagnostics.push(diagnostic("missing_computation_runtime", "runtime", "An Attested Computation requires runtime."));
|
|
443
|
+
} else if (!nonEmptyString(frontmatter.runtime)) {
|
|
444
|
+
diagnostics.push(diagnostic("invalid_computation_runtime", "runtime", "runtime must be a non-empty scalar."));
|
|
445
|
+
} else {
|
|
446
|
+
runtime = String(frontmatter.runtime);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const parameters = normalizeParameters(frontmatter.parameters, diagnostics);
|
|
450
|
+
const inline = extractInlineComputation(body);
|
|
451
|
+
const hasFileField = hasOwn(frontmatter, "computation");
|
|
452
|
+
const filePath = hasFileField && nonEmptyString(frontmatter.computation)
|
|
453
|
+
? frontmatter.computation
|
|
454
|
+
: null;
|
|
455
|
+
if (hasFileField && !filePath) {
|
|
456
|
+
diagnostics.push(diagnostic("invalid_computation_path", "computation", "computation must be a non-empty path when present."));
|
|
457
|
+
}
|
|
458
|
+
if (inline.sectionCount > 1
|
|
459
|
+
|| inline.blocks.some((block) => !block.closed || !block.content.trim())
|
|
460
|
+
|| inline.blocks.length > 1) {
|
|
461
|
+
diagnostics.push(diagnostic(
|
|
462
|
+
"invalid_inline_computation",
|
|
463
|
+
"body.# Computation",
|
|
464
|
+
"Inline computation requires one # Computation section containing exactly one closed fenced code block.",
|
|
465
|
+
));
|
|
466
|
+
}
|
|
467
|
+
if (hasFileField && inline.blocks.length > 0) {
|
|
468
|
+
diagnostics.push(diagnostic(
|
|
469
|
+
"conflicting_computation_sources",
|
|
470
|
+
"computation",
|
|
471
|
+
"Use either computation file path or an inline fenced computation, not both.",
|
|
472
|
+
));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
let computation = null;
|
|
476
|
+
if (filePath && inline.blocks.length === 0) {
|
|
477
|
+
computation = { mode: "file", path: filePath };
|
|
478
|
+
} else if (!hasFileField && inline.sectionCount === 1 && inline.blocks.length === 1 && inline.blocks[0].closed) {
|
|
479
|
+
computation = Object.assign({ mode: "inline" }, inline.blocks[0]);
|
|
480
|
+
} else if (!hasFileField && inline.blocks.length === 0) {
|
|
481
|
+
diagnostics.push(diagnostic(
|
|
482
|
+
"missing_computation",
|
|
483
|
+
"body.# Computation",
|
|
484
|
+
"Provide computation as a file path or one fenced block under # Computation.",
|
|
485
|
+
));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const executor = normalizeExecutor(frontmatter.executor, diagnostics);
|
|
489
|
+
const attester = normalizeAttester(frontmatter.attester, diagnostics);
|
|
490
|
+
return {
|
|
491
|
+
runtime,
|
|
492
|
+
parameters,
|
|
493
|
+
computation,
|
|
494
|
+
executor,
|
|
495
|
+
attester,
|
|
496
|
+
inline,
|
|
497
|
+
ready: diagnostics.length === start,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function normalizeV02Signals(documentOrFrontmatter, options) {
|
|
502
|
+
const input = documentOrFrontmatter || {};
|
|
503
|
+
const document = isPlainObject(input) && isPlainObject(input.frontmatter) ? input : null;
|
|
504
|
+
const frontmatter = document ? input.frontmatter : (isPlainObject(input) ? input : {});
|
|
505
|
+
const config = options || {};
|
|
506
|
+
const body = hasOwn(config, "body") ? config.body : document && hasOwn(document, "body") ? document.body : "";
|
|
507
|
+
const asOf = normalizeAsOf(config.asOf);
|
|
508
|
+
const diagnostics = [];
|
|
509
|
+
|
|
510
|
+
let resource = null;
|
|
511
|
+
if (hasOwn(frontmatter, "resource")) {
|
|
512
|
+
if (nonEmptyString(frontmatter.resource)) {
|
|
513
|
+
resource = frontmatter.resource;
|
|
514
|
+
} else {
|
|
515
|
+
diagnostics.push(diagnostic("invalid_resource", "resource", "resource must be a non-empty path or URI when present."));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const provenance = normalizeSources(frontmatter, body, diagnostics);
|
|
519
|
+
const generation = normalizeGenerated(frontmatter, diagnostics);
|
|
520
|
+
const verification = normalizeVerified(frontmatter.verified, hasOwn(frontmatter, "verified"), diagnostics);
|
|
521
|
+
const lifecycle = normalizeLifecycle(frontmatter, asOf, diagnostics);
|
|
522
|
+
const computation = normalizeAttestedComputation(frontmatter, body, diagnostics);
|
|
523
|
+
|
|
524
|
+
return {
|
|
525
|
+
raw: frontmatter,
|
|
526
|
+
resource,
|
|
527
|
+
sources: provenance.sources,
|
|
528
|
+
sourcesOrigin: provenance.origin,
|
|
529
|
+
usageWindow: provenance.usageWindow,
|
|
530
|
+
generated: generation.generated,
|
|
531
|
+
generatedOrigin: generation.origin,
|
|
532
|
+
verifiedEvents: verification.events,
|
|
533
|
+
trustTier: deriveTrustTier(verification.events, verification.valid),
|
|
534
|
+
status: lifecycle.status,
|
|
535
|
+
staleAfter: lifecycle.staleAfter,
|
|
536
|
+
freshness: lifecycle.freshness,
|
|
537
|
+
asOf: lifecycle.asOf,
|
|
538
|
+
computation,
|
|
539
|
+
diagnostics,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
module.exports = {
|
|
544
|
+
normalizeV02Signals,
|
|
545
|
+
};
|
package/src/validation.js
CHANGED
|
@@ -6,9 +6,9 @@ const CONFORMANCE_WARNING_CODES = new Set([
|
|
|
6
6
|
]);
|
|
7
7
|
|
|
8
8
|
const PROJECT_INVALID_WARNING_CODES = new Set([
|
|
9
|
-
"broken_link",
|
|
10
9
|
"invalid_id",
|
|
11
10
|
"link_outside_root",
|
|
11
|
+
"broken_semantic_reference",
|
|
12
12
|
]);
|
|
13
13
|
|
|
14
14
|
function inBundle(entry, bundle) {
|
|
@@ -39,6 +39,7 @@ function normalizeDiagnostic(entry, defaults) {
|
|
|
39
39
|
|
|
40
40
|
function validateIndex(index, bundle) {
|
|
41
41
|
const source = index || {};
|
|
42
|
+
const strictLinks = Boolean(source.strictLinks || (source.project && source.project.strictLinks));
|
|
42
43
|
const errors = (source.errors || []).filter((entry) => inBundle(entry, bundle));
|
|
43
44
|
const warnings = (source.warnings || []).filter((entry) => inBundle(entry, bundle));
|
|
44
45
|
const documents = (source.documents || []).filter((doc) => !bundle || doc.bundle === bundle);
|
|
@@ -87,7 +88,8 @@ function validateIndex(index, bundle) {
|
|
|
87
88
|
add(entry, {
|
|
88
89
|
severity: "warning",
|
|
89
90
|
layer: "project",
|
|
90
|
-
invalidatesProject: PROJECT_INVALID_WARNING_CODES.has(entry.code)
|
|
91
|
+
invalidatesProject: PROJECT_INVALID_WARNING_CODES.has(entry.code)
|
|
92
|
+
|| (entry.code === "broken_link" && strictLinks),
|
|
91
93
|
});
|
|
92
94
|
});
|
|
93
95
|
|
|
@@ -95,6 +97,8 @@ function validateIndex(index, bundle) {
|
|
|
95
97
|
entry.layer === "conformance" && entry.severity === "error"
|
|
96
98
|
));
|
|
97
99
|
const projectDiagnostics = diagnostics.filter((entry) => entry.layer === "project");
|
|
100
|
+
const advisories = diagnostics.filter((entry) => entry.layer === "v0.2");
|
|
101
|
+
const computations = documents.filter((doc) => doc.signals && doc.signals.computation);
|
|
98
102
|
const conformant = conformanceErrors.length === 0;
|
|
99
103
|
const validForProject = conformant
|
|
100
104
|
&& errors.length === 0
|
|
@@ -104,9 +108,16 @@ function validateIndex(index, bundle) {
|
|
|
104
108
|
conformant,
|
|
105
109
|
validForProject,
|
|
106
110
|
valid: validForProject,
|
|
111
|
+
strictLinks,
|
|
107
112
|
diagnostics,
|
|
108
113
|
conformanceErrors,
|
|
109
114
|
projectDiagnostics,
|
|
115
|
+
advisories,
|
|
116
|
+
readiness: {
|
|
117
|
+
attestedComputations: computations.length,
|
|
118
|
+
attestationReady: computations.filter((doc) => doc.signals.computation.attestationReady).length,
|
|
119
|
+
attestationNotReady: computations.filter((doc) => !doc.signals.computation.attestationReady).map((doc) => doc.uri),
|
|
120
|
+
},
|
|
110
121
|
errors,
|
|
111
122
|
warnings,
|
|
112
123
|
};
|