@agentshouse/mdmodel 0.1.0-c669bdbad8a03 → 0.1.0-c679f403bf3fb

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/dist/validate.js CHANGED
@@ -60,8 +60,8 @@ function itemHrefs(content, items, uses) {
60
60
  }
61
61
  return hrefs;
62
62
  }
63
- export function verify(schema) {
64
- const read = readSchema(schema);
63
+ export function verify(schema, provided) {
64
+ const read = readSchema(schema, provided);
65
65
  if ('types' in read)
66
66
  return { verdict: 'accept', model: read };
67
67
  return {
@@ -120,7 +120,8 @@ function definitionOf(document, model) {
120
120
  const kind = instanceType(document);
121
121
  return kind === null || !Object.hasOwn(model.types, kind) ? undefined : model.types[kind];
122
122
  }
123
- export function checkDocument(file, content, model) {
123
+ export function checkDocument(file, content, model, context = {}) {
124
+ const anyRoom = context.anyRoom === true;
124
125
  const parsed = parse(content, file);
125
126
  if (!parsed.ok)
126
127
  return { ok: false };
@@ -137,8 +138,8 @@ export function checkDocument(file, content, model) {
137
138
  type,
138
139
  typed: true,
139
140
  references: [
140
- ...references(document, definition).flatMap(({ value, to, crossRoom }) => crossing(file, value, crossRoom) ? [] : [reached(file, value, to)]),
141
- ...read.sections.flatMap(({ to, crossRoom, hrefs }) => hrefs.flatMap((href) => (crossing(file, href, crossRoom) ? [] : [reached(file, href, to)]))),
141
+ ...references(document, definition).flatMap(({ value, to, crossRoom }) => crossing(file, value, anyRoom || crossRoom) ? [] : [reached(file, value, to)]),
142
+ ...read.sections.flatMap(({ to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, anyRoom || crossRoom) ? [] : [reached(file, href, to)])),
142
143
  ],
143
144
  };
144
145
  }
@@ -180,90 +181,159 @@ export function roomReferences(file, content, definition) {
180
181
  if (!parsed.ok)
181
182
  return found;
182
183
  for (const { pointer, value, to, crossRoom } of references(parsed.document, definition)) {
183
- const room = crossRoom === true ? roomTarget(value) : null;
184
+ const room = roomTarget(value);
184
185
  if (room !== null)
185
- found.push({ kind: 'field', pointer, value, to, ...room });
186
+ found.push({
187
+ kind: 'field',
188
+ pointer,
189
+ value,
190
+ to,
191
+ ...room,
192
+ ...(crossRoom ? { crossRoom } : {}),
193
+ });
186
194
  }
187
195
  const uses = usesIn(content);
188
196
  for (const span of headingSpans(content, bodyStartOf(content))) {
189
197
  const spec = Object.hasOwn(definition.sections, span.name)
190
198
  ? definition.sections[span.name]
191
199
  : undefined;
192
- if (spec?.kind !== 'list' || spec.items.reference === undefined || !spec.items.crossRoom)
200
+ if (spec?.kind !== 'list' || spec.items.reference === undefined)
193
201
  continue;
194
202
  const listed = listItems(content, span.bodyStart, span.bodyEnd);
195
203
  for (const href of (listed === null ? null : itemHrefs(content, listed, uses)) ?? []) {
196
204
  const room = roomTarget(href);
197
205
  if (room !== null)
198
- found.push({ kind: 'item', section: span.name, href, to: spec.items.reference, ...room });
206
+ found.push({
207
+ kind: 'item',
208
+ section: span.name,
209
+ href,
210
+ to: spec.items.reference,
211
+ ...room,
212
+ ...(spec.items.crossRoom === true ? { crossRoom: true } : {}),
213
+ });
199
214
  }
200
215
  }
201
216
  return found;
202
217
  }
218
+ function least(refused, file, verdict) {
219
+ return refused === undefined || file < refused.file ? { file, verdict } : refused;
220
+ }
221
+ export function assess(file, content, binary, model = { types: {} }, context = {}) {
222
+ const unread = { file, type: undefined, targets: [], structured: null, fields: [], sections: [] };
223
+ if (binary)
224
+ return { ...unread, read: refuse(file, 'binary-target-document'), linked: true };
225
+ const parsed = parse(content, file);
226
+ if (!parsed.ok)
227
+ return { ...unread, read: { verdict: 'refuse', ...parsed.refusal }, linked: true };
228
+ const document = parsed.document;
229
+ const type = document.type ?? BUILT_IN_TYPE;
230
+ const typed = { file, type, read: null, structured: null };
231
+ if (instanceType(document) === BUILT_IN_TYPE) {
232
+ return { ...typed, targets: [], fields: [], linked: true, sections: [] };
233
+ }
234
+ const definition = definitionOf(document, model);
235
+ const read = structure(file, content, document, definition);
236
+ if ('refusal' in read || definition === undefined) {
237
+ return {
238
+ ...typed,
239
+ targets: [],
240
+ structured: 'refusal' in read ? read.refusal : null,
241
+ fields: [],
242
+ linked: true,
243
+ sections: [],
244
+ };
245
+ }
246
+ const anyRoom = context.anyRoom === true;
247
+ const fields = references(document, definition).flatMap(({ pointer, value, to, crossRoom }) => crossing(file, value, anyRoom || crossRoom)
248
+ ? []
249
+ : [{ reference: reached(file, value, to), location: pointer }]);
250
+ const sections = read.sections.flatMap(({ section, to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, anyRoom || crossRoom)
251
+ ? []
252
+ : [{ reference: reached(file, href, to), location: section }]));
253
+ const targets = new Set();
254
+ for (const { reference } of [...fields, ...sections]) {
255
+ if (reference.target !== null)
256
+ targets.add(reference.target);
257
+ }
258
+ return { ...typed, targets: [...targets], fields, linked: linked(file, content), sections };
259
+ }
260
+ export function verdicts() {
261
+ let read;
262
+ let structured;
263
+ let referred;
264
+ let missed;
265
+ const unreached = (placed, typeOf, binary) => placed.find(({ reference }) => !reaches(reference, typeOf, binary));
266
+ return {
267
+ add(assessment, typeOf, binary) {
268
+ const { file } = assessment;
269
+ if (assessment.read !== null) {
270
+ read = least(read, file, assessment.read);
271
+ return 0;
272
+ }
273
+ if (assessment.structured !== null) {
274
+ structured = least(structured, file, assessment.structured);
275
+ return 0;
276
+ }
277
+ const field = unreached(assessment.fields, typeOf, binary);
278
+ if (field !== undefined) {
279
+ referred = least(referred, file, refuse(file, 'unresolved-reference', { location: field.location }));
280
+ return 0;
281
+ }
282
+ if (!assessment.linked) {
283
+ referred = least(referred, file, refuse(file, 'unresolved-link'));
284
+ return 0;
285
+ }
286
+ const section = unreached(assessment.sections, typeOf, binary);
287
+ if (section !== undefined) {
288
+ referred = least(referred, file, refuse(file, 'unresolved-reference', { location: section.location }));
289
+ return 0;
290
+ }
291
+ const all = [...assessment.fields, ...assessment.sections];
292
+ for (const { reference } of all) {
293
+ if (!missing(reference, typeOf, binary))
294
+ continue;
295
+ const path = reference.target;
296
+ if (missed === undefined || path < missed.path)
297
+ missed = { path, referrers: [] };
298
+ if (path === missed.path && missed.referrers.at(-1) !== file)
299
+ missed.referrers.push(file);
300
+ }
301
+ return all.length;
302
+ },
303
+ verdict() {
304
+ const refused = read ?? structured ?? referred;
305
+ if (refused !== undefined)
306
+ return refused.verdict;
307
+ if (missed !== undefined) {
308
+ return refuse(missed.path, 'referenced-missing-file', {
309
+ referrers: [...missed.referrers].sort(),
310
+ });
311
+ }
312
+ return { verdict: 'accept' };
313
+ },
314
+ };
315
+ }
203
316
  export function* validating(library, context = { binaryTargets: [] }, model = { types: {} }) {
204
317
  const binaryTargets = new Set(context.binaryTargets);
205
318
  const binary = (path) => binaryTargets.has(path);
206
- const files = Object.keys(library).sort();
207
319
  const types = new Map();
208
- const typeOf = (path) => types.get(path);
209
- const documents = new Map();
210
- for (const file of files) {
211
- if (binaryTargets.has(file))
212
- return refuse(file, 'binary-target-document');
213
- const parsed = parse(library[file], file);
214
- if (!parsed.ok)
215
- return { verdict: 'refuse', ...parsed.refusal };
216
- types.set(file, parsed.document.type ?? BUILT_IN_TYPE);
217
- if (instanceType(parsed.document) !== BUILT_IN_TYPE)
218
- documents.set(file, parsed.document);
219
- yield file;
320
+ const assessments = [];
321
+ for (const [file, content] of library) {
322
+ const assessment = assess(file, content, binary(file), model, context);
323
+ if (assessment.type !== undefined)
324
+ types.set(file, assessment.type);
325
+ assessments.push(assessment);
326
+ yield { path: file, resolved: 0 };
220
327
  }
221
- const sectionReferences = new Map();
222
- for (const [file, document] of documents) {
223
- const read = structure(file, library[file], document, definitionOf(document, model));
224
- if ('refusal' in read)
225
- return read.refusal;
226
- sectionReferences.set(file, read.sections);
227
- yield file;
228
- }
229
- let least;
230
- for (const [file, document] of documents) {
231
- const definition = definitionOf(document, model);
232
- const fields = references(document, definition).flatMap(({ pointer, value, to, crossRoom }) => crossing(file, value, crossRoom)
233
- ? []
234
- : [{ reference: reached(file, value, to), location: pointer }]);
235
- for (const { reference, location } of fields) {
236
- if (!reaches(reference, typeOf, binary))
237
- return refuse(file, 'unresolved-reference', { location });
238
- }
239
- if (!linked(file, library[file]))
240
- return refuse(file, 'unresolved-link');
241
- const sections = sectionReferences
242
- .get(file)
243
- .flatMap(({ section, to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, crossRoom)
244
- ? []
245
- : [{ reference: reached(file, href, to), location: section }]));
246
- for (const { reference, location } of sections) {
247
- if (!reaches(reference, typeOf, binary))
248
- return refuse(file, 'unresolved-reference', { location });
249
- }
250
- for (const { reference } of [...fields, ...sections]) {
251
- if (!missing(reference, typeOf, binary))
252
- continue;
253
- const path = reference.target;
254
- if (least === undefined || path < least.path)
255
- least = { path, referrers: [] };
256
- if (path === least.path && least.referrers.at(-1) !== file)
257
- least.referrers.push(file);
258
- }
259
- yield file;
328
+ const judged = verdicts();
329
+ for (const assessment of assessments) {
330
+ const resolved = judged.add(assessment, (path) => types.get(path), binary);
331
+ yield { path: assessment.file, resolved };
260
332
  }
261
- if (least !== undefined)
262
- return refuse(least.path, 'referenced-missing-file', { referrers: least.referrers });
263
- return { verdict: 'accept' };
333
+ return judged.verdict();
264
334
  }
265
335
  export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
266
- const steps = validating(library, context, model);
336
+ const steps = validating(new Map(Object.entries(library)), context, model);
267
337
  let step = steps.next();
268
338
  while (step.done !== true)
269
339
  step = steps.next();
@@ -51,8 +51,8 @@ change to a value that is not prose leaves the projection alone.
51
51
 
52
52
  `division.json` exercises the public `divide` seam separately from the
53
53
  validation Libraries. It carries `schema` and `cases` like `projection.json`,
54
- and each case adds the `budget` the division runs under. Parse the source, call
55
- `divide(document, file, context)` with `{ documentTargets, definition, budget }`,
54
+ and each case adds the `bound` the division runs under. Parse the source, call
55
+ `divide(document, file, context)` with `{ documentTargets, definition, bound }`,
56
56
  and compare against `expected`: either `{ "ok": true, "spans": [...] }`, where
57
57
  each span carries the exact `text` to embed and the exact `source` the returned
58
58
  `start` and `end` address in that case's source, or `{ "ok": false, "refusal": ... }`. Then
@@ -107,6 +107,7 @@ schema text. The model's own rules:
107
107
  | `frontmatter-fence` | A present frontmatter fence does not parse as the accepted YAML subset |
108
108
  | `schema-yaml` | A schema text is not a mapping in the accepted YAML subset |
109
109
  | `schema-type-name` | A type name is empty, starts with `$`, or is `any` or `attachment` |
110
+ | `provided-type` | A schema names a type its caller provides with a key but its two role keywords |
110
111
  | `schema` | A type or subschema is neither a mapping nor a boolean |
111
112
  | `schema-keyword` | A schema carries a key that is no JSON Schema 2020-12 keyword the model admits |
112
113
  | `$defs` | `$defs` is not a mapping |
@@ -122,9 +123,9 @@ schema text. The model's own rules:
122
123
  | `structured-section` | A declared list section is not a list of values of its item schema |
123
124
  | `referenced-missing-file` | Remaining files still reference an absent path |
124
125
  | `unwritable-link` | No Markdown destination form can carry a new link destination |
125
- | `division-budget` | A division budget is not a whole number of at least 2 |
126
+ | `division-bound` | A division bound is not a whole number of at least 2 |
126
127
 
127
- `unwritable-link` is refused by `apply` and `division-budget` by `divide`, not
128
+ `unwritable-link` is refused by `apply` and `division-bound` by `divide`, not
128
129
  by `validate`, so no case under `cases/` carries either.
129
130
 
130
131
  A reference that admits `attachment` reaches any path in the same Library, so
@@ -2,12 +2,12 @@
2
2
  "schema": "task:\n type: object\n properties:\n name: { type: string }\n status: { enum: [open, done] }\n required: [status]\n sections:\n Description: { kind: text, required: true }\n Acceptance: { kind: list, items: { type: string, reference: person } }\nperson: {}\n",
3
3
  "cases": [
4
4
  {
5
- "name": "a Document whose whole input fits the budget is one Span carrying its projection",
5
+ "name": "a Document whose whole input fits the bound is one Span carrying its projection",
6
6
  "file": "tasks/one.md",
7
7
  "source": "---\ntype: task\nname: Ship the release\nstatus: open\n---\n\nPreamble prose.\n\n## Description\n\nBody of the description.\n\n## Acceptance\n\n- people/ana.md\n",
8
8
  "documentTargets": [],
9
9
  "definition": "task",
10
- "budget": 4096,
10
+ "bound": 4096,
11
11
  "expected": {
12
12
  "ok": true,
13
13
  "spans": [
@@ -19,12 +19,12 @@
19
19
  }
20
20
  },
21
21
  {
22
- "name": "an input over the budget descends to whole heading nodes and packs adjacent siblings",
22
+ "name": "an input over the bound descends to whole heading nodes and packs adjacent siblings",
23
23
  "file": "tasks/one.md",
24
24
  "source": "---\ntype: task\nname: Ship the release\nstatus: open\n---\n\nPreamble prose.\n\n## Description\n\nBody of the description.\n\n## Notes\n\nNotes prose.\n\n## Acceptance\n\n- people/ana.md\n",
25
25
  "documentTargets": [],
26
26
  "definition": "task",
27
- "budget": 45,
27
+ "bound": 45,
28
28
  "expected": {
29
29
  "ok": true,
30
30
  "spans": [
@@ -44,12 +44,12 @@
44
44
  }
45
45
  },
46
46
  {
47
- "name": "a heading node over the budget divides into adjacent blocks carrying its own heading",
47
+ "name": "a heading node over the bound divides into adjacent blocks carrying its own heading",
48
48
  "file": "tasks/one.md",
49
49
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\n## Overview\n\nFirst paragraph of the overview.\n\nSecond paragraph of the overview.\n\n## Notes\n\nShort note.\n",
50
50
  "documentTargets": [],
51
51
  "definition": "task",
52
- "budget": 50,
52
+ "bound": 50,
53
53
  "expected": {
54
54
  "ok": true,
55
55
  "spans": [
@@ -78,7 +78,7 @@
78
78
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\n## Overview\n\nFirst paragraph of the overview.\n\nSecond paragraph of the overview.\n\n## Notes\n\nShort note.\n",
79
79
  "documentTargets": [],
80
80
  "definition": "task",
81
- "budget": 75,
81
+ "bound": 75,
82
82
  "expected": {
83
83
  "ok": true,
84
84
  "spans": [
@@ -107,7 +107,7 @@
107
107
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\n## Overview\n\nIntro line.\n\n### Detail\n\nDetail paragraph one.\n\nDetail paragraph two.\n",
108
108
  "documentTargets": [],
109
109
  "definition": "task",
110
- "budget": 40,
110
+ "bound": 40,
111
111
  "expected": {
112
112
  "ok": true,
113
113
  "spans": [
@@ -131,12 +131,12 @@
131
131
  }
132
132
  },
133
133
  {
134
- "name": "a block over the budget divides into sentences that repeat their predecessor",
134
+ "name": "a block over the bound divides into sentences that repeat their predecessor",
135
135
  "file": "tasks/one.md",
136
136
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\n## Overview\n\nOne. Two. Three. Four.\n",
137
137
  "documentTargets": [],
138
138
  "definition": "task",
139
- "budget": 30,
139
+ "bound": 30,
140
140
  "expected": {
141
141
  "ok": true,
142
142
  "spans": [
@@ -156,12 +156,12 @@
156
156
  }
157
157
  },
158
158
  {
159
- "name": "a single sentence over the budget divides at a bound cut inside it",
159
+ "name": "a single sentence over the bound divides at a bound cut inside it",
160
160
  "file": "tasks/one.md",
161
161
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\n## Overview\n\naaaa bbbb cccc dddd eeee\n",
162
162
  "documentTargets": [],
163
163
  "definition": "task",
164
- "budget": 20,
164
+ "bound": 20,
165
165
  "expected": {
166
166
  "ok": true,
167
167
  "spans": [
@@ -198,7 +198,7 @@
198
198
  "source": "# Title\n\nIntro paragraph about the whole thing.\n\n## Alpha\n\nAlpha one. Alpha two.\n\n### Deep\n\nDeep prose here.\n\n## Beta\n\nBeta prose.\n",
199
199
  "documentTargets": [],
200
200
  "definition": null,
201
- "budget": 60,
201
+ "bound": 60,
202
202
  "expected": {
203
203
  "ok": true,
204
204
  "spans": [
@@ -227,7 +227,7 @@
227
227
  "source": "---\ntype: any\ntitle: Note\n---\n\n## Notes\n\nRead it.\n",
228
228
  "documentTargets": [],
229
229
  "definition": null,
230
- "budget": 4096,
230
+ "bound": 4096,
231
231
  "expected": {
232
232
  "ok": true,
233
233
  "spans": [
@@ -246,7 +246,7 @@
246
246
  "plans/q3.md"
247
247
  ],
248
248
  "definition": "task",
249
- "budget": 50,
249
+ "bound": 50,
250
250
  "expected": {
251
251
  "ok": true,
252
252
  "spans": [
@@ -271,7 +271,7 @@
271
271
  "source": "---\ntype: task\nname: Task\nstatus: open\n---\n\nPreamble prose.\n\n## Acceptance\n\n- people/ana.md\n\n## Notes\n\nShort note.\n",
272
272
  "documentTargets": [],
273
273
  "definition": "task",
274
- "budget": 4096,
274
+ "bound": 4096,
275
275
  "expected": {
276
276
  "ok": true,
277
277
  "spans": [
@@ -288,7 +288,7 @@
288
288
  "source": "---\ntype: task\nname: Ship the release\nstatus: open\n---\n\nPreamble prose.\n\n## Description\n\nBody of the description.\n\n## Acceptance\n\n- people/ana.md\n",
289
289
  "documentTargets": [],
290
290
  "definition": null,
291
- "budget": 4096,
291
+ "bound": 4096,
292
292
  "expected": {
293
293
  "ok": false,
294
294
  "refusal": {
@@ -299,17 +299,17 @@
299
299
  }
300
300
  },
301
301
  {
302
- "name": "a budget that cannot hold one whole character is refused before any division",
302
+ "name": "a bound that cannot hold one whole character is refused before any division",
303
303
  "file": "notes/one.md",
304
304
  "source": "## Notes\n\nShort note.\n",
305
305
  "documentTargets": [],
306
306
  "definition": null,
307
- "budget": 1,
307
+ "bound": 1,
308
308
  "expected": {
309
309
  "ok": false,
310
310
  "refusal": {
311
311
  "file": "notes/one.md",
312
- "rule": "division-budget"
312
+ "rule": "division-bound"
313
313
  }
314
314
  }
315
315
  },
@@ -319,7 +319,7 @@
319
319
  "source": "---\ntype: task\nname: \"Ship: the release\"\nstatus: open\n---\n\nBody prose.\n",
320
320
  "documentTargets": [],
321
321
  "definition": "task",
322
- "budget": 4096,
322
+ "bound": 4096,
323
323
  "expected": {
324
324
  "ok": true,
325
325
  "spans": [
@@ -331,12 +331,12 @@
331
331
  }
332
332
  },
333
333
  {
334
- "name": "sentence division reads every terminator and closer and drops a carry the budget cannot hold",
334
+ "name": "sentence division reads every terminator and closer and drops a carry the bound cannot hold",
335
335
  "file": "notes/three.md",
336
336
  "source": "## Ask\n\nReady? Go! He said \"no.\" Then (silence.) Done.\n",
337
337
  "documentTargets": [],
338
338
  "definition": null,
339
- "budget": 30,
339
+ "bound": 30,
340
340
  "expected": {
341
341
  "ok": true,
342
342
  "spans": [
@@ -361,7 +361,7 @@
361
361
  "source": "## Part\n\n### Alpha\n\nAlpha prose that is long enough to matter.\n\n### Beta\n\nBeta prose.\n",
362
362
  "documentTargets": [],
363
363
  "definition": null,
364
- "budget": 70,
364
+ "bound": 70,
365
365
  "expected": {
366
366
  "ok": true,
367
367
  "spans": [
@@ -377,12 +377,12 @@
377
377
  }
378
378
  },
379
379
  {
380
- "name": "a heading the budget cannot hold beside any text is not repeated",
380
+ "name": "a heading the bound cannot hold beside any text is not repeated",
381
381
  "file": "notes/five.md",
382
382
  "source": "## An overview of the release\n\nFirst paragraph here.\n\nSecond paragraph here.\n",
383
383
  "documentTargets": [],
384
384
  "definition": null,
385
- "budget": 25,
385
+ "bound": 25,
386
386
  "expected": {
387
387
  "ok": true,
388
388
  "spans": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/mdmodel",
3
- "version": "0.1.0-c669bdbad8a03",
3
+ "version": "0.1.0-c679f403bf3fb",
4
4
  "description": "MD Type language, parser, validator, structured change application, Markdown syntax profile, and conformance fixtures",
5
5
  "license": "MIT",
6
6
  "type": "module",