@delurio/cli 0.4.0 → 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/CHANGELOG.md +11 -0
- package/README.md +2 -2
- package/SPEC.md +13 -5
- package/dist/changes.js +12 -8
- package/dist/check.js +33 -2
- package/dist/diff.d.ts +2 -0
- package/dist/diff.js +26 -11
- package/dist/domain.d.ts +25 -0
- package/dist/domain.js +135 -0
- package/dist/model.d.ts +21 -1
- package/dist/model.js +327 -57
- package/dist/schemas.js +5 -1
- package/package.json +1 -1
- package/schemas/domain.schema.json +7 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
While the version is 0.x, a release that can judge the same graphs differently (a new check, a stricter schema) bumps the minor; a fix that judges nothing differently bumps the patch.
|
|
4
4
|
|
|
5
|
+
## 0.5.1 (2026-11-03)
|
|
6
|
+
|
|
7
|
+
- `check`, `diff` and `page` explore a large flow about four times faster and in a third of the memory, and judge every graph exactly as before: the same situations in the same order, the same shortest paths and the same findings. On QAuth's consent flow (5.4 million situations from 580,608 starts) `check` took 122 s and 2.9 GB, and now takes 34 s and 1 GB. A situation is kept as a number instead of a string; guards, assertions and effects are read once instead of at every situation; and an assertion is not tested in the states where the state alone rules it out.
|
|
8
|
+
|
|
9
|
+
## 0.5.0 (2026-11-01)
|
|
10
|
+
|
|
11
|
+
- A domain may be split across the files of its directory (SPEC.md §5): every `*.yaml` there that says `kind: domain`, read as one, `domain.yaml` first and then the others by name. Maps merge key by key at any depth, `policies` and `invariants` join, and any other value is given in one file only. `check` (the `schema` and `changes` rules), `diff` and `page` read the merged domain; before, a domain file not named `domain.yaml` was read by none of them.
|
|
12
|
+
- New rules of `check`, which fail `--strict`: `domain-twice`, a value or an invariant given in two files of a domain, at the second one's line with a note at the first; `domain-place`, a file whose id is not its directory's domain, or a domain whose files sit in two directories.
|
|
13
|
+
- `diff` heads the domain's changes "The API it calls (its domain)", and names every file of it it compared.
|
|
14
|
+
- An entity may carry `examples` (SPEC.md §5): records of it, each an object of its fields, as JSON Schema's own keyword. The `schema` rule refused it before.
|
|
15
|
+
|
|
5
16
|
## 0.4.0 (2026-11-01)
|
|
6
17
|
|
|
7
18
|
- An exit (`type: final`) may be named in snake_case, optionally followed by its cause: `signed_out`, `invalid_token.dpop_bound` (SPEC.md §1, Text). The `schema` rule accepts these names on exits only; every other state stays camelCase. `flow.mmd` writes such an id as `invalid_token__dpop_bound`, since Mermaid ids take no dot.
|
package/README.md
CHANGED
|
@@ -74,7 +74,7 @@ The package ships a JSON Schema (2020-12) for each kind of graph file in `schema
|
|
|
74
74
|
"https://unpkg.com/@delurio/cli@0.2.0/schemas/flow.schema.json": "**/flow.yaml",
|
|
75
75
|
"https://unpkg.com/@delurio/cli@0.2.0/schemas/ui.schema.json": "**/ui.yaml",
|
|
76
76
|
"https://unpkg.com/@delurio/cli@0.2.0/schemas/nav.schema.json": ["**/nav.yaml", "**/nav.extracted.yaml"],
|
|
77
|
-
"https://unpkg.com/@delurio/cli@0.2.0/schemas/domain.schema.json": "**/domain.yaml"
|
|
77
|
+
"https://unpkg.com/@delurio/cli@0.2.0/schemas/domain.schema.json": ["**/domain.yaml", "**/domain/*.yaml"]
|
|
78
78
|
},
|
|
79
79
|
"json.schemas": [
|
|
80
80
|
{ "fileMatch": ["**/catalog/*.json"], "url": "https://unpkg.com/@delurio/cli@0.2.0/schemas/catalog.schema.json" }
|
|
@@ -82,7 +82,7 @@ The package ships a JSON Schema (2020-12) for each kind of graph file in `schema
|
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
With the package installed, `./node_modules/@delurio/cli/schemas/<kind>.schema.json` works the same, offline. `check` reports the same as the editor, as errors of the rule `schema` that fail `--strict`, and finds what a schema cannot see, such as a state that does not exist or a catalog key that is missing.
|
|
85
|
+
A domain may be split across the files of its directory (SPEC.md §5); `**/domain/*.yaml` covers them in a directory named `domain`, and a file elsewhere names its schema on its first line: `# yaml-language-server: $schema=<the domain schema's URL>`. With the package installed, `./node_modules/@delurio/cli/schemas/<kind>.schema.json` works the same, offline. `check` reports the same as the editor, as errors of the rule `schema` that fail `--strict`, and finds what a schema cannot see, such as a state that does not exist or a catalog key that is missing.
|
|
86
86
|
|
|
87
87
|
Next to the editor, keep the pages open and current:
|
|
88
88
|
|
package/SPEC.md
CHANGED
|
@@ -21,14 +21,14 @@ id: learning.enrollment
|
|
|
21
21
|
| `flow` | `flow.yaml` | the behaviour: states, events, transitions, a finite context, and guarantees the checker proves |
|
|
22
22
|
| `ui` | `ui.yaml` | what each state shows: the nodes of the screen, their roles, names, texts and the events they send |
|
|
23
23
|
| catalog | `catalog/<locale>.json` | the words of the UI, one file per locale |
|
|
24
|
-
| `domain` | `domain.yaml
|
|
24
|
+
| `domain` | `domain.yaml`, and any other file of the domain's directory (§5) | the data and the API: entities, actions, refusals, policies, invariants |
|
|
25
25
|
| `nav` | `nav.yaml` | the pages of an app, the ways between them, and who may open them |
|
|
26
26
|
| `binding` | `binding.yaml` | how a flow maps onto a running app, for replay and recording |
|
|
27
27
|
| `tutorial` | `tutorial.yaml` | an end-user video written against a flow |
|
|
28
28
|
| `kit` | `kit.yaml` | how the UI components look in one product |
|
|
29
29
|
| (generated) | `anchors.lock.yaml`, `viz/`, `video/` | written by the tools; never by hand |
|
|
30
30
|
|
|
31
|
-
A flow directory holds `flow.yaml`; a flow with screens also holds `ui.yaml` and the catalog that `ui.yaml` names. It may hold `domain.yaml`, `binding.yaml` and `tutorial.yaml`. A domain shared by several flows sits in its own directory and each flow names it by id (`domain: learning.api`). A navigation graph is a directory with `nav.yaml` and its catalog.
|
|
31
|
+
A flow directory holds `flow.yaml`; a flow with screens also holds `ui.yaml` and the catalog that `ui.yaml` names. It may hold `domain.yaml`, `binding.yaml` and `tutorial.yaml`. A domain shared by several flows sits in its own directory and each flow names it by id (`domain: learning.api`). A directory holds one domain and a domain sits in one directory, in `domain.yaml` or split across several files (§5). A navigation graph is a directory with `nav.yaml` and its catalog.
|
|
32
32
|
|
|
33
33
|
A flow with no screen (an HTTP endpoint, a webhook, an MCP tool, a CI job) has no `ui.yaml`: it loads and is checked without one, and its page draws no screens. What such a step answers the outside world is written on its state as `response` (§2).
|
|
34
34
|
|
|
@@ -219,7 +219,7 @@ invariants:
|
|
|
219
219
|
- { id: redirect-uri-exact, rule: "...", spec: RFC 6749 §3.1.2.3 }
|
|
220
220
|
```
|
|
221
221
|
|
|
222
|
-
**The data.** An entity's `fields` give each field as a JSON Schema (2020-12), and `required` lists the fields that must be present: an entity is the JSON Schema `{ type: object, properties: <fields>, required: <required> }`. `null` is a type like any other: an optional time is `{ type: [string, "null"], format: date-time }`, and an enum that may be empty lists `null` among its values. A `body` or a `result` written out is a JSON Schema too. delurio adds these keywords, which a JSON Schema validator leaves alone:
|
|
222
|
+
**The data.** An entity's `fields` give each field as a JSON Schema (2020-12), and `required` lists the fields that must be present: an entity is the JSON Schema `{ type: object, properties: <fields>, required: <required> }`. `null` is a type like any other: an optional time is `{ type: [string, "null"], format: date-time }`, and an enum that may be empty lists `null` among its values. A `body` or a `result` written out is a JSON Schema too. An entity may carry `examples`, JSON Schema's own keyword: records of it as the app keeps them, each an object of its fields (decided on 1 Nov 2026). delurio adds these keywords, which a JSON Schema validator leaves alone:
|
|
223
223
|
|
|
224
224
|
| Keyword | On | Meaning |
|
|
225
225
|
|---|---|---|
|
|
@@ -237,7 +237,15 @@ An action, a refusal and an invariant may carry `note`: the author's own words,
|
|
|
237
237
|
|
|
238
238
|
This form was decided on 22 Oct 2026. Graphs captured before it may still give fields as `{ kind: text }`; they are converted when a tool first reads fields. The OpenAPI export and import are [#8](https://github.com/delurio-labs/delurio/issues/8).
|
|
239
239
|
|
|
240
|
-
|
|
240
|
+
**Several files.** A domain may be split across the files of its directory: every `*.yaml` there that says `kind: domain` is a part of it (a file named for another kind, such as `flow.yaml`, never is). Each part starts with `schema`, `kind` and the domain's `id`, and the parts are read as one domain, `domain.yaml` first and then the others by name:
|
|
241
|
+
|
|
242
|
+
- maps merge key by key, at any depth: one file may give an entity's fields, and another add its actions or mark one of its fields `sensitivity: credential`;
|
|
243
|
+
- the lists `policies` and `invariants` join, in that order; an invariant's `id` is given once;
|
|
244
|
+
- any other value (a string, a number, `true`, a list such as `required` or `lifecycle`) is given in one file only.
|
|
245
|
+
|
|
246
|
+
A value given in two files is an error of `check` at the second one's line (`domain-twice`); so is a file whose id is not its directory's domain, and a domain whose files sit in two directories (`domain-place`). A split keeps a large domain one entity per file, and lets a part generated from the code ([#6](https://github.com/delurio-labs/delurio/issues/6)) and a part written by hand sit side by side; how a generator marks the files it owns is decided there. Decided on 1 Nov 2026.
|
|
247
|
+
|
|
248
|
+
`check` validates every file of a domain against its schema and reads its files as one, the `changes` rule (§4) reads its actions, the revision diff compares it, and anchors lock its `source:` keys. Loading it with the flow and checking policies, secrets and `invoke` names is [#5](https://github.com/delurio-labs/delurio/issues/5); generating it from Drizzle and Zod is [#6](https://github.com/delurio-labs/delurio/issues/6); a registry that every API operation and route is covered by a graph is [#7](https://github.com/delurio-labs/delurio/issues/7).
|
|
241
249
|
|
|
242
250
|
## 6. Navigation (`kind: nav`)
|
|
243
251
|
|
|
@@ -291,7 +299,7 @@ A kit says how the components of `ui.yaml` look in one product: `tokens` and `da
|
|
|
291
299
|
|---|---|
|
|
292
300
|
| 0 | the graph loads and, under `--strict`, nothing is wrong |
|
|
293
301
|
| 1 | the graph cannot be loaded: a YAML error, an unknown state, an unknown name in an expression, a non-exhaustive `always`, an unsupported construct |
|
|
294
|
-
| 2 | under `--strict`: an assertion is violated, a catalog key or a sample value is missing, a component has no renderer, the kit does not cover a component,
|
|
302
|
+
| 2 | under `--strict`: an assertion is violated, a catalog key or a sample value is missing, a component has no renderer, the kit does not cover a component, the main story cannot be walked, or a domain's files clash (§5) |
|
|
295
303
|
|
|
296
304
|
**As text.** A finding is one line in the GNU error format, which compilers, editors and most CI logs read:
|
|
297
305
|
|
package/dist/changes.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// `changes:` in ui.yaml (SPEC.md §4): a flow whose steps write something tells its users, in its own note, what it
|
|
2
2
|
// changes and what follows. An action writes when its route is not a GET, when a policy lets someone `write` with it, or
|
|
3
3
|
// when it is a step inside the service (no route) that returns an entity; all three are read from the flow's domain,
|
|
4
|
-
// looked up by id among the graphs beside this one. A flow with no screens, or
|
|
4
|
+
// looked up by id among the graphs beside this one, all its files read as one (SPEC.md §5). A flow with no screens, or
|
|
5
|
+
// whose domain is not found, is not checked.
|
|
5
6
|
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
6
7
|
import { basename, dirname, join } from 'node:path';
|
|
7
|
-
import {
|
|
8
|
+
import { inReadingOrder, isDomainText, mayBeDomain, mergeDomain } from './domain.js';
|
|
8
9
|
const SKIP = new Set(['viz', 'video', 'node_modules', '.git']);
|
|
9
10
|
// the root to search: the nearest folder called `graphs` above the graph, else the graph's parent
|
|
10
11
|
function rootOf(dir) {
|
|
@@ -14,15 +15,18 @@ function rootOf(dir) {
|
|
|
14
15
|
return dirname(dir);
|
|
15
16
|
}
|
|
16
17
|
function domainUnder(root, id, depth = 4) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const names = readdirSync(root);
|
|
19
|
+
const parts = inReadingOrder(names.filter((name) => mayBeDomain(name) && statSync(join(root, name)).isFile()))
|
|
20
|
+
.map((name) => ({ file: join(root, name), text: readFileSync(join(root, name), 'utf8') }))
|
|
21
|
+
.filter((p) => isDomainText(p.text));
|
|
22
|
+
if (parts.length > 0) {
|
|
23
|
+
const merged = mergeDomain(parts);
|
|
24
|
+
if (merged.id === id)
|
|
25
|
+
return merged.doc;
|
|
22
26
|
}
|
|
23
27
|
if (depth === 0)
|
|
24
28
|
return null;
|
|
25
|
-
for (const name of
|
|
29
|
+
for (const name of names) {
|
|
26
30
|
if (SKIP.has(name))
|
|
27
31
|
continue;
|
|
28
32
|
const sub = join(root, name);
|
package/dist/check.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// is found under --strict (and, with --baseline, new since that run).
|
|
5
5
|
import { createHash } from 'node:crypto';
|
|
6
6
|
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
7
|
-
import { join, relative, resolve } from 'node:path';
|
|
7
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
8
8
|
import { parse } from 'yaml';
|
|
9
9
|
import { GraphError, load } from './graph.js';
|
|
10
10
|
import { checkAssertions, explore, Model, ModelError } from './model.js';
|
|
@@ -15,6 +15,7 @@ import { checkChanges } from './changes.js';
|
|
|
15
15
|
import { KitError, resolveKit } from './kit.js';
|
|
16
16
|
import { checkGoals, findings, loadNav, viewOf } from './nav.js';
|
|
17
17
|
import { schemaKindOf, validateFile } from './schemas.js';
|
|
18
|
+
import { inReadingOrder, mergeDomain } from './domain.js';
|
|
18
19
|
export const CHECK_USAGE = [
|
|
19
20
|
' node src/cli.ts check <graphs folder> [--strict] [--baseline <sarif>] [--format sarif --output <file>]',
|
|
20
21
|
' checks every graph under the folder and writes nothing else: GNU error lines on stdout, or SARIF 2.1.0;',
|
|
@@ -26,6 +27,8 @@ const RULES = [
|
|
|
26
27
|
{ id: 'load', text: 'The graph cannot be loaded', level: 'error' },
|
|
27
28
|
{ id: 'format-newer', text: 'The graph is written for a newer format than this checker reads', level: 'error' },
|
|
28
29
|
{ id: 'schema', text: "A graph file does not follow its kind's schema (SPEC.md)", level: 'error' },
|
|
30
|
+
{ id: 'domain-twice', text: 'A value of a domain is given in two of its files (SPEC.md §5)', level: 'error' },
|
|
31
|
+
{ id: 'domain-place', text: 'A domain file is not with the rest of its domain (SPEC.md §1)', level: 'error' },
|
|
29
32
|
{ id: 'assertion-violated', text: 'A guarantee of the flow is broken by a reachable situation', level: 'error' },
|
|
30
33
|
{ id: 'main-story-unwalkable', text: 'The declared main story cannot be walked', level: 'error' },
|
|
31
34
|
{ id: 'catalog-key-missing', text: 'A catalog key the screens use is missing', level: 'error' },
|
|
@@ -151,6 +154,32 @@ function checkFlow(dir) {
|
|
|
151
154
|
out.push(fromWarning(dir, g.flow.id, w));
|
|
152
155
|
return out;
|
|
153
156
|
}
|
|
157
|
+
// the domains under root, each the domain files of one directory read as one (SPEC.md §5): a value given in two of
|
|
158
|
+
// them, a file whose id is not its directory's, and a domain whose files sit in two directories
|
|
159
|
+
function checkDomains(files) {
|
|
160
|
+
const byDir = new Map();
|
|
161
|
+
for (const f of files)
|
|
162
|
+
if (schemaKindOf(f) === 'domain')
|
|
163
|
+
byDir.set(dirname(f), [...(byDir.get(dirname(f)) ?? []), f]);
|
|
164
|
+
const out = [];
|
|
165
|
+
const dirsOf = new Map(); // a domain's id -> the directories that hold it
|
|
166
|
+
for (const [dir, list] of byDir) {
|
|
167
|
+
const merged = mergeDomain(inReadingOrder(list).map((file) => ({ file, text: readFileSync(file, 'utf8') })));
|
|
168
|
+
for (const p of merged.problems) {
|
|
169
|
+
const notes = p.first === undefined ? [] : [{ file: rel(p.first.file), line: p.first.line, column: p.first.column, message: 'given first here' }];
|
|
170
|
+
out.push(result(p.rule, p.file, p, p.message, merged.id ?? rel(dir), p.element, notes));
|
|
171
|
+
}
|
|
172
|
+
if (merged.id !== null)
|
|
173
|
+
dirsOf.set(merged.id, [...(dirsOf.get(merged.id) ?? []), dir]);
|
|
174
|
+
}
|
|
175
|
+
for (const [id, dirs] of dirsOf) {
|
|
176
|
+
for (const dir of dirs.slice(1)) {
|
|
177
|
+
const file = inReadingOrder(byDir.get(dir) ?? [])[0];
|
|
178
|
+
out.push(result('domain-place', file, placeOf(file, 'id:'), `the domain ${id} is in ${rel(dirs[0])}/ too: a domain sits in one directory, so every flow that names it reads all of it (SPEC.md §1)`, id, `${basename(file)}#/id`));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
154
183
|
function checkNav(dir) {
|
|
155
184
|
const file = join(dir, 'nav.yaml');
|
|
156
185
|
const { g, warnings } = loadNav(dir, undefined);
|
|
@@ -255,10 +284,12 @@ export function checkMain(args) {
|
|
|
255
284
|
}
|
|
256
285
|
}
|
|
257
286
|
// every graph file against its kind's schema: what an editor with the schemas marks, check reports
|
|
258
|
-
|
|
287
|
+
const files = graphFiles(root);
|
|
288
|
+
for (const file of files) {
|
|
259
289
|
for (const f of validateFile(file))
|
|
260
290
|
results.push(result('schema', file, f, f.message, f.graph ?? rel(file), f.element));
|
|
261
291
|
}
|
|
292
|
+
results.push(...checkDomains(files));
|
|
262
293
|
results.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.column - b.column || a.rule.localeCompare(b.rule) || a.message.localeCompare(b.message));
|
|
263
294
|
if (baseline !== undefined) {
|
|
264
295
|
const prints = baselinePrints(baseline);
|
package/dist/diff.d.ts
CHANGED
package/dist/diff.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { execFileSync } from 'node:child_process';
|
|
2
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
3
|
import { isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
|
|
4
|
-
import { parse } from 'yaml';
|
|
5
4
|
import { GraphError, humanize, load } from './graph.js';
|
|
6
5
|
import { loadNav } from './nav.js';
|
|
7
6
|
import { joinWords } from './compare.js';
|
|
8
7
|
import { flowBehaviour, flowSameBehaviour, flowStructure, runSide } from './diff-flow.js';
|
|
9
8
|
import { navBehaviour, navSameBehaviour, navStructure, runNav } from './diff-nav.js';
|
|
9
|
+
import { inReadingOrder, isDomainText, mayBeDomain, mergeDomain } from './domain.js';
|
|
10
10
|
// `node src/cli.ts diff`: what a revision of a graph changes, for a PR description. Each side is a graph directory on disk
|
|
11
11
|
// or the same directory at a git revision (read with `git show <commit>:<path>`, nothing is checked out). No model, no
|
|
12
12
|
// network: the same two revisions give the same bytes.
|
|
@@ -43,6 +43,11 @@ function dirSource(dir) {
|
|
|
43
43
|
texts.set(posix.normalize(file), text);
|
|
44
44
|
return text;
|
|
45
45
|
},
|
|
46
|
+
peek: (file) => readFileSync(join(dir, file), 'utf8'),
|
|
47
|
+
list: (sub) => {
|
|
48
|
+
const path = join(dir, sub);
|
|
49
|
+
return existsSync(path) && statSync(path).isDirectory() ? readdirSync(path).filter((n) => statSync(join(path, n)).isFile()).sort() : [];
|
|
50
|
+
},
|
|
46
51
|
};
|
|
47
52
|
}
|
|
48
53
|
function git(args, cwd) {
|
|
@@ -86,19 +91,29 @@ function gitSource(rev, dir) {
|
|
|
86
91
|
texts.set(posix.normalize(file), text);
|
|
87
92
|
return text;
|
|
88
93
|
},
|
|
94
|
+
peek: (file) => git(['show', `${commit}:${path(file)}`], top) ?? '',
|
|
95
|
+
list: (sub) => {
|
|
96
|
+
// blobs only: `ls-tree` names a directory a tree
|
|
97
|
+
const tree = git(['ls-tree', `${commit}:${path(sub)}`], top);
|
|
98
|
+
if (tree === null)
|
|
99
|
+
return [];
|
|
100
|
+
return tree.split('\n').filter((l) => /^\d+ blob /.test(l)).map((l) => l.slice(l.indexOf('\t') + 1)).sort();
|
|
101
|
+
},
|
|
89
102
|
};
|
|
90
103
|
}
|
|
91
|
-
// The API description of a flow: domain
|
|
92
|
-
//
|
|
104
|
+
// The API description of a flow: the domain beside it, else the shared ../domain/ of a capture layout, else
|
|
105
|
+
// ../../domain/ of graphs/ (graphs/<part>/<flow>); all its files read as one (SPEC.md §5). When the flow names its
|
|
106
|
+
// domain, the files must be that domain.
|
|
93
107
|
function findDomain(src, g) {
|
|
94
|
-
for (const
|
|
95
|
-
|
|
108
|
+
for (const dir of ['.', '../domain', '../../domain']) {
|
|
109
|
+
const files = inReadingOrder(src.list(dir).filter(mayBeDomain).map((n) => (dir === '.' ? n : `${dir}/${n}`))).filter((f) => isDomainText(src.peek(f)));
|
|
110
|
+
if (files.length === 0)
|
|
96
111
|
continue;
|
|
97
|
-
const doc =
|
|
112
|
+
const { id, doc } = mergeDomain(files.map((file) => ({ file, text: src.read(file) })));
|
|
98
113
|
const named = g.flow.domain;
|
|
99
|
-
if (typeof named === 'string' &&
|
|
100
|
-
throw new GraphError(`flow.yaml names the domain ${named}, but ${
|
|
101
|
-
return { file, doc };
|
|
114
|
+
if (typeof named === 'string' && id !== named)
|
|
115
|
+
throw new GraphError(`flow.yaml names the domain ${named}, but ${files.join(', ')} ${files.length === 1 ? 'is' : 'are'} ${String(id)}`);
|
|
116
|
+
return { file: files.join(', '), doc };
|
|
102
117
|
}
|
|
103
118
|
return null;
|
|
104
119
|
}
|
|
@@ -136,7 +151,7 @@ const HEADINGS = {
|
|
|
136
151
|
facts: 'Facts and conditions',
|
|
137
152
|
notModeled: 'Not modeled',
|
|
138
153
|
wording: 'Wording',
|
|
139
|
-
domain: 'The API it calls (domain
|
|
154
|
+
domain: 'The API it calls (its domain)',
|
|
140
155
|
other: 'Other keys',
|
|
141
156
|
citations: 'Source citations',
|
|
142
157
|
};
|
package/dist/domain.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface DomainPart {
|
|
2
|
+
file: string;
|
|
3
|
+
text: string;
|
|
4
|
+
}
|
|
5
|
+
export interface DomainPlace {
|
|
6
|
+
file: string;
|
|
7
|
+
line: number;
|
|
8
|
+
column: number;
|
|
9
|
+
}
|
|
10
|
+
export interface DomainProblem extends DomainPlace {
|
|
11
|
+
rule: 'domain-twice' | 'domain-place';
|
|
12
|
+
message: string;
|
|
13
|
+
element: string;
|
|
14
|
+
first?: DomainPlace;
|
|
15
|
+
}
|
|
16
|
+
export interface MergedDomain {
|
|
17
|
+
id: string | null;
|
|
18
|
+
doc: Record<string, unknown>;
|
|
19
|
+
problems: DomainProblem[];
|
|
20
|
+
}
|
|
21
|
+
export declare const OTHER_KINDS: Set<string>;
|
|
22
|
+
export declare const mayBeDomain: (name: string) => boolean;
|
|
23
|
+
export declare const isDomainText: (text: string) => boolean;
|
|
24
|
+
export declare function inReadingOrder(names: string[]): string[];
|
|
25
|
+
export declare function mergeDomain(parts: DomainPart[]): MergedDomain;
|
package/dist/domain.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// A domain (SPEC.md §1, §5) is every file in its directory that says `kind: domain`, read as one: `domain.yaml` first,
|
|
2
|
+
// then the others by name. `schema`, `kind` and `id` open every file and the id is the same in all; maps merge key by
|
|
3
|
+
// key at any depth; the top-level lists `policies` and `invariants` join in that order; any other value (a string, a
|
|
4
|
+
// number, a list) is given in one file only. A split lets a generated part and a hand-written part sit side by side.
|
|
5
|
+
import { isMap, isSeq, LineCounter, parse, parseDocument } from 'yaml';
|
|
6
|
+
// the files whose name says another kind (SPEC.md §1); only the others are read to see whether they are a domain
|
|
7
|
+
export const OTHER_KINDS = new Set(['flow.yaml', 'ui.yaml', 'nav.yaml', 'nav.extracted.yaml', 'binding.yaml', 'tutorial.yaml', 'kit.yaml', 'anchors.lock.yaml']);
|
|
8
|
+
const OWN = new Set(['schema', 'kind', 'id']);
|
|
9
|
+
const JOINED = new Set(['policies', 'invariants']);
|
|
10
|
+
// a file that may be part of a domain, by its name
|
|
11
|
+
export const mayBeDomain = (name) => name === 'domain.yaml' || (name.endsWith('.yaml') && !OTHER_KINDS.has(name));
|
|
12
|
+
// `kind: domain` at the top of the file; read from the text, so a file that is not YAML is still found and its own
|
|
13
|
+
// schema check says why
|
|
14
|
+
export const isDomainText = (text) => /^kind:[ \t]*(domain|'domain'|"domain")[ \t]*(#.*)?$/m.test(text);
|
|
15
|
+
// the reading order of a domain's files: domain.yaml, then the others by name
|
|
16
|
+
export function inReadingOrder(names) {
|
|
17
|
+
const base = (n) => n.slice(n.lastIndexOf('/') + 1);
|
|
18
|
+
return [...names].sort((a, b) => (base(a) === 'domain.yaml' ? -1 : base(b) === 'domain.yaml' ? 1 : a < b ? -1 : a > b ? 1 : 0));
|
|
19
|
+
}
|
|
20
|
+
const isObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
21
|
+
const keyOf = (pair) => String(pair.key?.value ?? pair.key);
|
|
22
|
+
const pointer = (path) => path.map((s) => `/${s.replace(/~/g, '~0').replace(/\//g, '~1')}`).join('');
|
|
23
|
+
const nameOf = (file) => file.slice(file.lastIndexOf('/') + 1);
|
|
24
|
+
// the 1-based line and column of the key at `path` in a YAML text (of the item, for a list index), else the file's start
|
|
25
|
+
function placeOf(text, path) {
|
|
26
|
+
const lineCounter = new LineCounter();
|
|
27
|
+
const doc = parseDocument(text, { lineCounter, prettyErrors: false });
|
|
28
|
+
let node = doc.contents;
|
|
29
|
+
let at;
|
|
30
|
+
for (const step of path) {
|
|
31
|
+
if (isMap(node)) {
|
|
32
|
+
const pair = node.items.find((p) => keyOf(p) === step);
|
|
33
|
+
if (pair === undefined)
|
|
34
|
+
break;
|
|
35
|
+
at = pair.key?.range?.[0] ?? at;
|
|
36
|
+
node = pair.value;
|
|
37
|
+
}
|
|
38
|
+
else if (isSeq(node)) {
|
|
39
|
+
node = node.items[Number(step)];
|
|
40
|
+
at = node?.range?.[0] ?? at;
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
const { line, col } = lineCounter.linePos(at ?? 0);
|
|
46
|
+
return { line, column: col };
|
|
47
|
+
}
|
|
48
|
+
// the files of one directory, read as one domain; a file that is not YAML, or not a map, is left to its schema check
|
|
49
|
+
export function mergeDomain(parts) {
|
|
50
|
+
const doc = {};
|
|
51
|
+
const problems = [];
|
|
52
|
+
const origin = new Map(); // the pointer of a value given -> the part that gave it
|
|
53
|
+
const invariants = new Map();
|
|
54
|
+
let id = null;
|
|
55
|
+
let idFrom = -1;
|
|
56
|
+
const where = (i, path) => ({ file: parts[i].file, ...placeOf(parts[i].text, path) });
|
|
57
|
+
// the part that gave the value at `path`, or the map it sits in
|
|
58
|
+
const giver = (path) => {
|
|
59
|
+
for (let n = path.length; n > 0; n--) {
|
|
60
|
+
const o = origin.get(pointer(path.slice(0, n)));
|
|
61
|
+
if (o !== undefined)
|
|
62
|
+
return o;
|
|
63
|
+
}
|
|
64
|
+
return 0;
|
|
65
|
+
};
|
|
66
|
+
const twice = (i, path, first, said) => {
|
|
67
|
+
const at = where(i, path);
|
|
68
|
+
const firstAt = where(first, path);
|
|
69
|
+
problems.push({ rule: 'domain-twice', ...at, first: firstAt, message: `${said} is given in ${nameOf(parts[first].file)}:${firstAt.line} too: a domain's files give each value once (SPEC.md §5)`, element: `${nameOf(parts[i].file)}#${pointer(path)}` });
|
|
70
|
+
};
|
|
71
|
+
const put = (target, key, value, path, i) => {
|
|
72
|
+
if (!(key in target)) {
|
|
73
|
+
target[key] = value;
|
|
74
|
+
origin.set(pointer(path), i);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const here = target[key];
|
|
78
|
+
if (isObject(here) && isObject(value)) {
|
|
79
|
+
for (const [k, v] of Object.entries(value))
|
|
80
|
+
put(here, k, v, [...path, k], i);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
twice(i, path, giver(path), path.join('.'));
|
|
84
|
+
};
|
|
85
|
+
parts.forEach((part, i) => {
|
|
86
|
+
let data;
|
|
87
|
+
try {
|
|
88
|
+
data = parse(part.text);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!isObject(data))
|
|
94
|
+
return;
|
|
95
|
+
if (typeof data.id === 'string') {
|
|
96
|
+
if (id === null) {
|
|
97
|
+
id = data.id;
|
|
98
|
+
idFrom = i;
|
|
99
|
+
}
|
|
100
|
+
else if (data.id !== id) {
|
|
101
|
+
problems.push({ rule: 'domain-place', ...where(i, ['id']), message: `the id is ${data.id}, and ${nameOf(parts[idFrom].file)} beside it is ${id}: a directory holds one domain, every file of it with its id (SPEC.md §1)`, element: `${nameOf(part.file)}#/id` });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
for (const [key, value] of Object.entries(data)) {
|
|
106
|
+
if (OWN.has(key)) {
|
|
107
|
+
doc[key] ??= value;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (JOINED.has(key) && Array.isArray(value) && (doc[key] === undefined || Array.isArray(doc[key]))) {
|
|
111
|
+
if (doc[key] === undefined)
|
|
112
|
+
origin.set(pointer([key]), i);
|
|
113
|
+
doc[key] = [...(doc[key] ?? []), ...value];
|
|
114
|
+
if (key !== 'invariants')
|
|
115
|
+
continue;
|
|
116
|
+
value.forEach((inv, index) => {
|
|
117
|
+
const name = isObject(inv) && typeof inv.id === 'string' ? inv.id : null;
|
|
118
|
+
if (name === null)
|
|
119
|
+
return;
|
|
120
|
+
const seen = invariants.get(name);
|
|
121
|
+
if (seen === undefined)
|
|
122
|
+
invariants.set(name, { part: i, index });
|
|
123
|
+
else if (seen.part !== i) {
|
|
124
|
+
const at = where(i, ['invariants', String(index), 'id']);
|
|
125
|
+
const firstAt = where(seen.part, ['invariants', String(seen.index), 'id']);
|
|
126
|
+
problems.push({ rule: 'domain-twice', ...at, first: firstAt, message: `the invariant ${name} is given in ${nameOf(parts[seen.part].file)}:${firstAt.line} too: a domain's files give each invariant once (SPEC.md §5)`, element: `${nameOf(part.file)}#/invariants/${index}` });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
put(doc, key, value, [key], i);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
return { id, doc, problems };
|
|
135
|
+
}
|
package/dist/model.d.ts
CHANGED
|
@@ -18,11 +18,31 @@ export declare class Model {
|
|
|
18
18
|
readonly flow: Flow;
|
|
19
19
|
readonly vars: string[];
|
|
20
20
|
readonly domains: Scalar[][];
|
|
21
|
+
private readonly at;
|
|
22
|
+
private readonly compiled;
|
|
23
|
+
private readonly ready;
|
|
24
|
+
private readonly ways;
|
|
21
25
|
constructor(flow: Flow);
|
|
22
|
-
private
|
|
26
|
+
private compile;
|
|
27
|
+
private expr;
|
|
23
28
|
test(expr: string, state: string, ctx: Scalar[]): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Whether evaluating `src` can never throw: it names only the state, variables, and guards that parse and nest no
|
|
31
|
+
* deeper than evaluation allows. Only then may its pairs be tested in another order than the exploration's.
|
|
32
|
+
*/
|
|
33
|
+
pure(src: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The value of `src` in `state` whatever the context holds, when the state alone decides it (`state in [...] and
|
|
36
|
+
* ...` outside those states); undefined when the context matters. Folds only what evaluation would compute the same
|
|
37
|
+
* way: literals, the state, and the side of an `and` / `or` that decides it before the other is read.
|
|
38
|
+
*/
|
|
39
|
+
inState(src: string, state: string): Value | undefined;
|
|
40
|
+
/** `test` for one expression at many pairs: read once, then run at each. */
|
|
41
|
+
tester(expr: string): (state: string, ctx: Scalar[]) => boolean;
|
|
24
42
|
value(expr: string, state: string, ctx: Scalar[]): Value;
|
|
43
|
+
private effect;
|
|
25
44
|
private apply;
|
|
45
|
+
private waysOf;
|
|
26
46
|
steps(state: string, ctx: Scalar[]): Step[];
|
|
27
47
|
initials(): Scalar[][];
|
|
28
48
|
scenario(ctx: Scalar[]): Record<string, Scalar>;
|
package/dist/model.js
CHANGED
|
@@ -1,61 +1,235 @@
|
|
|
1
1
|
import { alternatives } from './graph.js';
|
|
2
|
-
import {
|
|
2
|
+
import { parseExpr } from './expr.js';
|
|
3
3
|
export class ModelError extends Error {
|
|
4
4
|
}
|
|
5
|
+
// The model explores millions of (state, context) pairs on a large flow (QAuth's consent: 5.4 million), so every guard,
|
|
6
|
+
// assertion and effect is read once and kept ready, instead of being looked up by name at every pair.
|
|
5
7
|
export class Model {
|
|
6
8
|
flow;
|
|
7
9
|
vars;
|
|
8
10
|
domains;
|
|
11
|
+
at; // a variable's position in a context
|
|
12
|
+
compiled = new Map();
|
|
13
|
+
ready = new Map();
|
|
14
|
+
ways = new Map();
|
|
9
15
|
constructor(flow) {
|
|
10
16
|
this.flow = flow;
|
|
11
17
|
this.vars = Object.keys(flow.context);
|
|
12
18
|
this.domains = this.vars.map((v) => flow.context[v].values);
|
|
19
|
+
this.at = new Map(this.vars.map((v, i) => [v, i]));
|
|
13
20
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
// what evaluate(parseExpr(src), lookup) gave, with the same errors, raised only when the name is reached
|
|
22
|
+
compile(e, depth) {
|
|
23
|
+
switch (e.k) {
|
|
24
|
+
case 'lit': {
|
|
25
|
+
const v = e.v;
|
|
26
|
+
return () => v;
|
|
27
|
+
}
|
|
28
|
+
case 'id': {
|
|
29
|
+
const name = e.name;
|
|
30
|
+
if (name === 'state')
|
|
31
|
+
return (state) => state;
|
|
32
|
+
const i = this.at.get(name);
|
|
33
|
+
if (i !== undefined)
|
|
34
|
+
return (_, ctx) => ctx[i];
|
|
35
|
+
const guard = this.flow.guards[name];
|
|
36
|
+
if (guard === undefined)
|
|
37
|
+
return (state) => {
|
|
38
|
+
throw new ModelError(`unknown name "${name}" (state ${state})`);
|
|
39
|
+
};
|
|
40
|
+
if (depth > 8)
|
|
41
|
+
return () => {
|
|
42
|
+
throw new ModelError(`guard ${name} nests too deep`);
|
|
43
|
+
};
|
|
44
|
+
let inner = null; // compiled when first reached, as the lookup read it
|
|
45
|
+
return (state, ctx) => (inner ??= this.compile(parseExpr(guard), depth + 1))(state, ctx);
|
|
46
|
+
}
|
|
47
|
+
case 'list': {
|
|
48
|
+
const items = e.items.map((x) => this.compile(x, depth));
|
|
49
|
+
return (state, ctx) => items.map((f) => f(state, ctx));
|
|
50
|
+
}
|
|
51
|
+
case 'not': {
|
|
52
|
+
const f = this.compile(e.e, depth);
|
|
53
|
+
return (state, ctx) => !f(state, ctx);
|
|
54
|
+
}
|
|
55
|
+
case 'and': {
|
|
56
|
+
const l = this.compile(e.l, depth);
|
|
57
|
+
const r = this.compile(e.r, depth);
|
|
58
|
+
return (state, ctx) => Boolean(l(state, ctx)) && Boolean(r(state, ctx));
|
|
59
|
+
}
|
|
60
|
+
case 'or': {
|
|
61
|
+
const l = this.compile(e.l, depth);
|
|
62
|
+
const r = this.compile(e.r, depth);
|
|
63
|
+
return (state, ctx) => Boolean(l(state, ctx)) || Boolean(r(state, ctx));
|
|
64
|
+
}
|
|
65
|
+
case 'cmp': {
|
|
66
|
+
const l = this.compile(e.l, depth);
|
|
67
|
+
const r = this.compile(e.r, depth);
|
|
68
|
+
if (e.op === '==')
|
|
69
|
+
return (state, ctx) => l(state, ctx) === r(state, ctx);
|
|
70
|
+
if (e.op === '!=')
|
|
71
|
+
return (state, ctx) => l(state, ctx) !== r(state, ctx);
|
|
72
|
+
const negate = e.op === 'not in';
|
|
73
|
+
// a list of literals, the usual right side, is the same list every time: made once
|
|
74
|
+
const fixed = e.r.k === 'list' && e.r.items.every((x) => x.k === 'lit') ? e.r.items.map((x) => x.v) : null;
|
|
75
|
+
if (fixed !== null)
|
|
76
|
+
return (state, ctx) => {
|
|
77
|
+
const x = l(state, ctx);
|
|
78
|
+
let inList = false;
|
|
79
|
+
for (let j = 0; j < fixed.length && !inList; j++)
|
|
80
|
+
inList = fixed[j] === x;
|
|
81
|
+
return negate ? !inList : inList;
|
|
82
|
+
};
|
|
83
|
+
return (state, ctx) => {
|
|
84
|
+
const x = l(state, ctx);
|
|
85
|
+
const list = r(state, ctx);
|
|
86
|
+
const inList = Array.isArray(list) && list.some((y) => y === x);
|
|
87
|
+
return negate ? !inList : inList;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
expr(src) {
|
|
93
|
+
let f = this.compiled.get(src);
|
|
94
|
+
if (f === undefined) {
|
|
95
|
+
f = this.compile(parseExpr(src), 0);
|
|
96
|
+
this.compiled.set(src, f);
|
|
97
|
+
}
|
|
98
|
+
return f;
|
|
28
99
|
}
|
|
29
100
|
test(expr, state, ctx) {
|
|
30
|
-
return Boolean(this.
|
|
101
|
+
return Boolean(this.expr(expr)(state, ctx));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Whether evaluating `src` can never throw: it names only the state, variables, and guards that parse and nest no
|
|
105
|
+
* deeper than evaluation allows. Only then may its pairs be tested in another order than the exploration's.
|
|
106
|
+
*/
|
|
107
|
+
pure(src) {
|
|
108
|
+
const ok = (e, depth, seen) => {
|
|
109
|
+
switch (e.k) {
|
|
110
|
+
case 'lit':
|
|
111
|
+
return true;
|
|
112
|
+
case 'id': {
|
|
113
|
+
if (e.name === 'state' || this.at.has(e.name))
|
|
114
|
+
return true;
|
|
115
|
+
const guard = this.flow.guards[e.name];
|
|
116
|
+
if (guard === undefined || depth > 8 || seen.has(e.name))
|
|
117
|
+
return false;
|
|
118
|
+
try {
|
|
119
|
+
return ok(parseExpr(guard), depth + 1, new Set([...seen, e.name]));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
case 'list':
|
|
126
|
+
return e.items.every((x) => ok(x, depth, seen));
|
|
127
|
+
case 'not':
|
|
128
|
+
return ok(e.e, depth, seen);
|
|
129
|
+
default:
|
|
130
|
+
return ok(e.l, depth, seen) && ok(e.r, depth, seen);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
return ok(parseExpr(src), 0, new Set());
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The value of `src` in `state` whatever the context holds, when the state alone decides it (`state in [...] and
|
|
142
|
+
* ...` outside those states); undefined when the context matters. Folds only what evaluation would compute the same
|
|
143
|
+
* way: literals, the state, and the side of an `and` / `or` that decides it before the other is read.
|
|
144
|
+
*/
|
|
145
|
+
inState(src, state) {
|
|
146
|
+
const fold = (e) => {
|
|
147
|
+
switch (e.k) {
|
|
148
|
+
case 'lit':
|
|
149
|
+
return { v: e.v };
|
|
150
|
+
case 'id':
|
|
151
|
+
return e.name === 'state' ? { v: state } : undefined;
|
|
152
|
+
case 'list': {
|
|
153
|
+
const items = e.items.map(fold);
|
|
154
|
+
return items.every((x) => x !== undefined) ? { v: items.map((x) => x.v) } : undefined;
|
|
155
|
+
}
|
|
156
|
+
case 'not': {
|
|
157
|
+
const x = fold(e.e);
|
|
158
|
+
return x === undefined ? undefined : { v: !x.v };
|
|
159
|
+
}
|
|
160
|
+
case 'and':
|
|
161
|
+
case 'or': {
|
|
162
|
+
const l = fold(e.l);
|
|
163
|
+
if (l === undefined)
|
|
164
|
+
return undefined;
|
|
165
|
+
if (e.k === 'and' ? !l.v : Boolean(l.v))
|
|
166
|
+
return { v: e.k === 'or' };
|
|
167
|
+
const r = fold(e.r);
|
|
168
|
+
return r === undefined ? undefined : { v: Boolean(r.v) };
|
|
169
|
+
}
|
|
170
|
+
case 'cmp': {
|
|
171
|
+
const l = fold(e.l);
|
|
172
|
+
const r = fold(e.r);
|
|
173
|
+
if (l === undefined || r === undefined)
|
|
174
|
+
return undefined;
|
|
175
|
+
if (e.op === '==')
|
|
176
|
+
return { v: l.v === r.v };
|
|
177
|
+
if (e.op === '!=')
|
|
178
|
+
return { v: l.v !== r.v };
|
|
179
|
+
const inList = Array.isArray(r.v) && r.v.some((y) => y === l.v);
|
|
180
|
+
return { v: e.op === 'in' ? inList : !inList };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
return fold(parseExpr(src))?.v;
|
|
185
|
+
}
|
|
186
|
+
/** `test` for one expression at many pairs: read once, then run at each. */
|
|
187
|
+
tester(expr) {
|
|
188
|
+
const f = this.expr(expr);
|
|
189
|
+
return (state, ctx) => Boolean(f(state, ctx));
|
|
31
190
|
}
|
|
32
191
|
value(expr, state, ctx) {
|
|
33
|
-
return
|
|
192
|
+
return this.expr(expr)(state, ctx);
|
|
34
193
|
}
|
|
35
|
-
|
|
36
|
-
let
|
|
37
|
-
|
|
194
|
+
effect(name) {
|
|
195
|
+
let r = this.ready.get(name);
|
|
196
|
+
if (r === undefined) {
|
|
38
197
|
const effect = this.flow.effects[name];
|
|
39
198
|
if (effect === undefined)
|
|
40
199
|
throw new ModelError(`unknown effect ${name}`);
|
|
200
|
+
r = {
|
|
201
|
+
// a name that is not a variable set nothing a context holds
|
|
202
|
+
set: Object.entries(effect.set ?? {}).flatMap(([k, v]) => (this.at.has(k) ? [[this.at.get(k), v]] : [])),
|
|
203
|
+
havoc: (effect.havoc ?? []).map((h) => {
|
|
204
|
+
const i = this.vars.indexOf(h);
|
|
205
|
+
return [i, this.domains[i]];
|
|
206
|
+
}),
|
|
207
|
+
};
|
|
208
|
+
this.ready.set(name, r);
|
|
209
|
+
}
|
|
210
|
+
return r;
|
|
211
|
+
}
|
|
212
|
+
apply(names, ctx) {
|
|
213
|
+
let outs = [ctx];
|
|
214
|
+
for (const name of names ?? []) {
|
|
215
|
+
const { set, havoc } = this.effect(name);
|
|
41
216
|
const next = [];
|
|
42
217
|
for (const base of outs) {
|
|
43
218
|
const c = base.slice();
|
|
44
|
-
for (const [
|
|
45
|
-
c[
|
|
46
|
-
const havoc = effect.havoc ?? [];
|
|
219
|
+
for (const [i, v] of set)
|
|
220
|
+
c[i] = v;
|
|
47
221
|
if (havoc.length === 0) {
|
|
48
222
|
next.push(c);
|
|
49
223
|
continue;
|
|
50
224
|
}
|
|
225
|
+
// every combination of the havoc variables' values, the first variable slowest, as the nested lists gave them
|
|
51
226
|
let combos = [[]];
|
|
52
|
-
for (const
|
|
53
|
-
combos = combos.flatMap((p) =>
|
|
227
|
+
for (const [, values] of havoc)
|
|
228
|
+
combos = combos.flatMap((p) => values.map((v) => [...p, v]));
|
|
54
229
|
for (const combo of combos) {
|
|
55
230
|
const c2 = c.slice();
|
|
56
|
-
havoc.
|
|
57
|
-
c2[
|
|
58
|
-
});
|
|
231
|
+
for (let j = 0; j < havoc.length; j++)
|
|
232
|
+
c2[havoc[j][0]] = combo[j];
|
|
59
233
|
next.push(c2);
|
|
60
234
|
}
|
|
61
235
|
}
|
|
@@ -63,43 +237,64 @@ export class Model {
|
|
|
63
237
|
}
|
|
64
238
|
return outs;
|
|
65
239
|
}
|
|
240
|
+
// a state's ways out, read once: `always` as one group tried in order, else one group per event
|
|
241
|
+
waysOf(state) {
|
|
242
|
+
let w = this.ways.get(state);
|
|
243
|
+
if (w === undefined) {
|
|
244
|
+
const st = this.flow.states[state];
|
|
245
|
+
const way = (event, a, i) => ({
|
|
246
|
+
event,
|
|
247
|
+
target: a.target,
|
|
248
|
+
guard: a.guard,
|
|
249
|
+
effect: a.effect,
|
|
250
|
+
taken: `${state}|${event ?? 'always'}|${i}`,
|
|
251
|
+
});
|
|
252
|
+
w =
|
|
253
|
+
st.type === 'final'
|
|
254
|
+
? { always: false, groups: [] }
|
|
255
|
+
: st.always !== undefined
|
|
256
|
+
? { always: true, groups: [st.always.map((a, i) => way(null, a, i))] }
|
|
257
|
+
: { always: false, groups: Object.entries(st.on ?? {}).map(([event, spec]) => alternatives(spec).map((a, i) => way(event, a, i))) };
|
|
258
|
+
this.ways.set(state, w);
|
|
259
|
+
}
|
|
260
|
+
return w;
|
|
261
|
+
}
|
|
66
262
|
steps(state, ctx) {
|
|
67
|
-
const
|
|
263
|
+
const { always, groups } = this.waysOf(state);
|
|
68
264
|
const out = [];
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for (let i = 0; i < st.always.length; i++) {
|
|
73
|
-
const a = st.always[i];
|
|
265
|
+
for (const alts of groups) {
|
|
266
|
+
let chosen = false;
|
|
267
|
+
for (const a of alts) {
|
|
74
268
|
if (a.guard === undefined || this.test(a.guard, state, ctx)) {
|
|
75
269
|
for (const c of this.apply(a.effect, ctx))
|
|
76
|
-
out.push({ event:
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
throw new ModelError(`\`always\` in ${state} is not exhaustive for ${JSON.stringify(Object.fromEntries(this.vars.map((v, i) => [v, ctx[i]])))}`);
|
|
81
|
-
}
|
|
82
|
-
for (const [event, spec] of Object.entries(st.on ?? {})) {
|
|
83
|
-
const alts = alternatives(spec);
|
|
84
|
-
for (let i = 0; i < alts.length; i++) {
|
|
85
|
-
const a = alts[i];
|
|
86
|
-
if (a.guard === undefined || this.test(a.guard, state, ctx)) {
|
|
87
|
-
for (const c of this.apply(a.effect, ctx))
|
|
88
|
-
out.push({ event, target: a.target, ctx: c, taken: `${state}|${event}|${i}` });
|
|
270
|
+
out.push({ event: a.event, target: a.target, ctx: c, taken: a.taken });
|
|
271
|
+
chosen = true;
|
|
89
272
|
break;
|
|
90
273
|
}
|
|
91
274
|
}
|
|
275
|
+
if (always && !chosen)
|
|
276
|
+
throw new ModelError(`\`always\` in ${state} is not exhaustive for ${JSON.stringify(Object.fromEntries(this.vars.map((v, i) => [v, ctx[i]])))}`);
|
|
92
277
|
}
|
|
93
278
|
return out;
|
|
94
279
|
}
|
|
95
280
|
initials() {
|
|
96
|
-
|
|
97
|
-
this.vars.
|
|
281
|
+
// every combination of the starting values, the first variable slowest: the order the nested lists gave
|
|
282
|
+
const choices = this.vars.map((v, i) => {
|
|
98
283
|
const initial = this.flow.context[v].initial;
|
|
99
|
-
|
|
100
|
-
combos = combos.flatMap((p) => values.map((x) => [...p, x]));
|
|
284
|
+
return initial === 'any' ? this.domains[i] : [initial];
|
|
101
285
|
});
|
|
102
|
-
|
|
286
|
+
const total = choices.reduce((n, c) => n * c.length, 1);
|
|
287
|
+
const out = new Array(total);
|
|
288
|
+
for (let k = 0; k < total; k++) {
|
|
289
|
+
const c = new Array(choices.length);
|
|
290
|
+
let rest = k;
|
|
291
|
+
for (let i = choices.length - 1; i >= 0; i--) {
|
|
292
|
+
c[i] = choices[i][rest % choices[i].length];
|
|
293
|
+
rest = Math.floor(rest / choices[i].length);
|
|
294
|
+
}
|
|
295
|
+
out[k] = c;
|
|
296
|
+
}
|
|
297
|
+
return out;
|
|
103
298
|
}
|
|
104
299
|
scenario(ctx) {
|
|
105
300
|
const out = {};
|
|
@@ -110,19 +305,56 @@ export class Model {
|
|
|
110
305
|
return out;
|
|
111
306
|
}
|
|
112
307
|
}
|
|
308
|
+
// A (state, context) pair as one number: the state's position, then each variable's value by its position in the
|
|
309
|
+
// variable's values, as digits of a mixed radix. A Set keeps numbers far faster than the strings they replace.
|
|
310
|
+
// Two pairs are the same exactly when their old keys, `${state}|${ctx.join(',')}`, are: a value is placed by its text,
|
|
311
|
+
// as join wrote it. A flow whose pairs do not fit below 2^53, a value or state name that would make the old key
|
|
312
|
+
// ambiguous, or a value outside its variable's values, keeps the old key.
|
|
313
|
+
function keyer(m) {
|
|
314
|
+
const text = (state, ctx) => `${state}|${ctx.join(',')}`;
|
|
315
|
+
const written = (v) => (v === null || v === undefined ? '' : String(v));
|
|
316
|
+
const names = Object.keys(m.flow.states);
|
|
317
|
+
const size = m.domains.reduce((n, values) => n * Math.max(values.length, 1), Math.max(names.length, 1));
|
|
318
|
+
if (!Number.isSafeInteger(size) || names.some((s) => s.includes('|')) || m.domains.some((values) => values.some((v) => written(v).includes(','))))
|
|
319
|
+
return text;
|
|
320
|
+
const states = new Map(names.map((s, i) => [s, i]));
|
|
321
|
+
const byText = m.domains.map((values) => {
|
|
322
|
+
const d = new Map();
|
|
323
|
+
values.forEach((v, i) => {
|
|
324
|
+
if (!d.has(written(v)))
|
|
325
|
+
d.set(written(v), i);
|
|
326
|
+
});
|
|
327
|
+
return d;
|
|
328
|
+
});
|
|
329
|
+
const byValue = m.domains.map((values, i) => new Map(values.map((v) => [v, byText[i].get(written(v))])));
|
|
330
|
+
const radix = m.domains.map((values) => Math.max(values.length, 1));
|
|
331
|
+
return (state, ctx) => {
|
|
332
|
+
let key = states.get(state);
|
|
333
|
+
if (key === undefined || ctx.length !== radix.length)
|
|
334
|
+
return text(state, ctx);
|
|
335
|
+
for (let i = 0; i < radix.length; i++) {
|
|
336
|
+
const d = byValue[i].get(ctx[i]) ?? byText[i].get(written(ctx[i]));
|
|
337
|
+
if (d === undefined)
|
|
338
|
+
return text(state, ctx);
|
|
339
|
+
key = key * radix[i] + d;
|
|
340
|
+
}
|
|
341
|
+
return key;
|
|
342
|
+
};
|
|
343
|
+
}
|
|
113
344
|
// Every reachable (state, context) pair, breadth first, so the first hit of a state is a shortest path to it.
|
|
114
345
|
// `from` and `keep` narrow the search: the replay starts only from situations the app is bound to and takes only steps it can drive.
|
|
115
346
|
export function explore(m, from = m.initials(), keep) {
|
|
116
347
|
const index = new Set();
|
|
348
|
+
const key = keyer(m);
|
|
117
349
|
const configs = [];
|
|
118
350
|
const taken = new Set();
|
|
119
351
|
const atState = new Map();
|
|
120
352
|
const branchCounts = new Map();
|
|
121
353
|
const push = (state, ctx, parent, event) => {
|
|
122
|
-
const
|
|
123
|
-
if (index.has(
|
|
354
|
+
const k = key(state, ctx);
|
|
355
|
+
if (index.has(k))
|
|
124
356
|
return;
|
|
125
|
-
index.add(
|
|
357
|
+
index.add(k);
|
|
126
358
|
configs.push({ state, ctx, parent, event });
|
|
127
359
|
};
|
|
128
360
|
for (const c of from)
|
|
@@ -141,7 +373,8 @@ export function explore(m, from = m.initials(), keep) {
|
|
|
141
373
|
const always = m.flow.states[state].always;
|
|
142
374
|
if (always !== undefined && steps.length > 0) {
|
|
143
375
|
const counts = branchCounts.get(state) ?? always.map(() => 0);
|
|
144
|
-
|
|
376
|
+
const taken0 = steps[0].taken;
|
|
377
|
+
counts[Number(taken0.slice(taken0.lastIndexOf('|') + 1))] += 1;
|
|
145
378
|
branchCounts.set(state, counts);
|
|
146
379
|
}
|
|
147
380
|
}
|
|
@@ -157,9 +390,46 @@ export function pathTo(m, ex, index) {
|
|
|
157
390
|
}
|
|
158
391
|
return { events: events.reverse(), start: m.scenario(c.ctx) };
|
|
159
392
|
}
|
|
393
|
+
// the first pair, in exploration order, where `src` holds; -1 when none does. An assertion that cannot throw is tested
|
|
394
|
+
// only in the states where the state alone does not rule it out: the first hit of each such state, the earliest of them.
|
|
395
|
+
function firstHit(m, ex, src, byState) {
|
|
396
|
+
// read where the exploration reads it, so an expression that throws throws at the same pair as before
|
|
397
|
+
if (!m.pure(src))
|
|
398
|
+
return ex.configs.findIndex((c) => m.test(src, c.state, c.ctx));
|
|
399
|
+
const test = m.tester(src);
|
|
400
|
+
let hit = -1;
|
|
401
|
+
for (const [state, at] of byState()) {
|
|
402
|
+
const fixed = m.inState(src, state);
|
|
403
|
+
if (fixed !== undefined && !fixed)
|
|
404
|
+
continue;
|
|
405
|
+
for (const i of at) {
|
|
406
|
+
if (hit >= 0 && i > hit)
|
|
407
|
+
break;
|
|
408
|
+
if (fixed !== undefined || test(state, ex.configs[i].ctx)) {
|
|
409
|
+
hit = i;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return hit;
|
|
415
|
+
}
|
|
160
416
|
export function checkAssertions(m, ex) {
|
|
417
|
+
let groups = null;
|
|
418
|
+
const byState = () => {
|
|
419
|
+
if (groups === null) {
|
|
420
|
+
groups = new Map();
|
|
421
|
+
ex.configs.forEach((c, i) => {
|
|
422
|
+
const list = groups?.get(c.state);
|
|
423
|
+
if (list === undefined)
|
|
424
|
+
groups?.set(c.state, [i]);
|
|
425
|
+
else
|
|
426
|
+
list.push(i);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
return groups;
|
|
430
|
+
};
|
|
161
431
|
return (m.flow.assertions ?? []).map((a) => {
|
|
162
|
-
const hit =
|
|
432
|
+
const hit = firstHit(m, ex, a.never, byState);
|
|
163
433
|
if (hit < 0)
|
|
164
434
|
return { id: a.id, never: a.never, say: a.say };
|
|
165
435
|
return { id: a.id, never: a.never, say: a.say, violation: { ...pathTo(m, ex, hit), state: ex.configs[hit].state } };
|
package/dist/schemas.js
CHANGED
|
@@ -8,9 +8,11 @@ import { basename, dirname, join } from 'node:path';
|
|
|
8
8
|
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
9
9
|
import addFormatsModule from 'ajv-formats';
|
|
10
10
|
import { isMap, isSeq, LineCounter, parse, parseDocument } from 'yaml';
|
|
11
|
+
import { isDomainText, mayBeDomain } from './domain.js';
|
|
11
12
|
const FORMAT = 'delurio-graph/0';
|
|
12
13
|
const KINDS = ['flow', 'ui', 'domain', 'nav', 'catalog'];
|
|
13
|
-
// the kind of graph file, by its name
|
|
14
|
+
// the kind of graph file, by its name, and a domain also by its `kind` (a domain may be split across files, SPEC.md
|
|
15
|
+
// §5): null for a file no schema covers (binding, tutorial, kit, anchors.lock)
|
|
14
16
|
export function schemaKindOf(file) {
|
|
15
17
|
const name = basename(file);
|
|
16
18
|
if (name === 'flow.yaml' || name === 'ui.yaml' || name === 'domain.yaml')
|
|
@@ -19,6 +21,8 @@ export function schemaKindOf(file) {
|
|
|
19
21
|
return 'nav';
|
|
20
22
|
if (name.endsWith('.json') && basename(dirname(file)) === 'catalog')
|
|
21
23
|
return 'catalog';
|
|
24
|
+
if (mayBeDomain(name) && existsSync(file) && isDomainText(readFileSync(file, 'utf8')))
|
|
25
|
+
return 'domain';
|
|
22
26
|
return null;
|
|
23
27
|
}
|
|
24
28
|
let compiled = null;
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"additionalProperties": false,
|
|
9
9
|
"properties": {
|
|
10
10
|
"schema": { "$ref": "#/$defs/formatVersion" },
|
|
11
|
-
"kind": { "description": "The kind of graph file. A domain
|
|
11
|
+
"kind": { "description": "The kind of graph file. A domain file is `domain`: `domain.yaml`, or any other file of a domain split across its directory.", "const": "domain" },
|
|
12
12
|
"id": { "$ref": "#/$defs/graphId", "description": "The domain's id; a flow names it with `domain:`.", "examples": ["qauth.authorize", "learning.api"] },
|
|
13
13
|
"title": { "type": "string", "description": "The domain in plain words, for people." },
|
|
14
14
|
"summary": { "type": "string", "description": "What the domain covers, in a few plain sentences." },
|
|
@@ -80,6 +80,12 @@
|
|
|
80
80
|
"items": { "type": "string" },
|
|
81
81
|
"examples": [["none -> active: the first sign-in"]]
|
|
82
82
|
},
|
|
83
|
+
"examples": {
|
|
84
|
+
"type": "array",
|
|
85
|
+
"description": "Records of the entity as the app keeps them, each an object of its fields: JSON Schema's own `examples`. Pages, docs and generated tests show these instead of made-up values.",
|
|
86
|
+
"items": { "type": "object" },
|
|
87
|
+
"examples": [[{ "userId": "0199f9a0-11aa-7b22-8c33-4d44e55f6601", "clientId": "0199f9b2-3c4d-7e5f-8a6b-7c8d9e0f1a2b", "scopes": ["openid"] }]]
|
|
88
|
+
},
|
|
83
89
|
"source": { "$ref": "#/$defs/sourceAnchor" }
|
|
84
90
|
}
|
|
85
91
|
},
|