@delurio/cli 0.5.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 CHANGED
@@ -2,6 +2,10 @@
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
+
5
9
  ## 0.5.0 (2026-11-01)
6
10
 
7
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.
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 lookup;
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 { evaluate, parseExpr } from './expr.js';
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
- lookup(state, ctx, depth) {
15
- return (name) => {
16
- if (name === 'state')
17
- return state;
18
- const i = this.vars.indexOf(name);
19
- if (i >= 0)
20
- return ctx[i];
21
- const guard = this.flow.guards[name];
22
- if (guard === undefined)
23
- throw new ModelError(`unknown name "${name}" (state ${state})`);
24
- if (depth > 8)
25
- throw new ModelError(`guard ${name} nests too deep`);
26
- return evaluate(parseExpr(guard), this.lookup(state, ctx, depth + 1));
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.value(expr, state, ctx));
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 evaluate(parseExpr(expr), this.lookup(state, ctx, 0));
192
+ return this.expr(expr)(state, ctx);
34
193
  }
35
- apply(names, ctx) {
36
- let outs = [ctx];
37
- for (const name of names ?? []) {
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 [k, v] of Object.entries(effect.set ?? {}))
45
- c[this.vars.indexOf(k)] = v;
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 h of havoc)
53
- combos = combos.flatMap((p) => this.domains[this.vars.indexOf(h)].map((v) => [...p, v]));
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.forEach((h, j) => {
57
- c2[this.vars.indexOf(h)] = combo[j];
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 st = this.flow.states[state];
263
+ const { always, groups } = this.waysOf(state);
68
264
  const out = [];
69
- if (st.type === 'final')
70
- return out;
71
- if (st.always !== undefined) {
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: null, target: a.target, ctx: c, taken: `${state}|always|${i}` });
77
- return out;
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
- let combos = [[]];
97
- this.vars.forEach((v, i) => {
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
- const values = initial === 'any' ? this.domains[i] : [initial];
100
- combos = combos.flatMap((p) => values.map((x) => [...p, x]));
284
+ return initial === 'any' ? this.domains[i] : [initial];
101
285
  });
102
- return combos;
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 key = `${state}|${ctx.join(',')}`;
123
- if (index.has(key))
354
+ const k = key(state, ctx);
355
+ if (index.has(k))
124
356
  return;
125
- index.add(key);
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
- counts[Number(steps[0].taken.split('|')[2])] += 1;
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 = ex.configs.findIndex((c) => m.test(a.never, c.state, c.ctx));
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@delurio/cli",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "The open checker and page of delurio-graph/0: check every graph in a project's own CI, offline, with GNU error lines and SARIF.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",