@delurio/cli 0.8.0 → 0.9.0

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/model.js CHANGED
@@ -1,6 +1,23 @@
1
- import { alternatives } from './graph.js';
1
+ import { alternatives, placed, transitionPath } from './graph.js';
2
2
  import { parseExpr } from './expr.js';
3
+ // a flow that cannot be explored; `place` says where in flow.yaml, when it is known (SPEC.md §10)
3
4
  export class ModelError extends Error {
5
+ place;
6
+ constructor(message, place) {
7
+ super(message);
8
+ this.place = place;
9
+ }
10
+ }
11
+ const inFlow = (path) => ({ file: 'flow.yaml', path });
12
+ // an expression read, or a ModelError that says why it cannot be: a guard or an assertion that does not parse keeps
13
+ // the flow from loading, as a name that is not one does
14
+ function parsed(src, place) {
15
+ try {
16
+ return parseExpr(src);
17
+ }
18
+ catch (err) {
19
+ throw new ModelError(err.message, place);
20
+ }
4
21
  }
5
22
  // The model explores millions of (state, context) pairs on a large flow (QAuth's consent: 5.4 million), so every guard,
6
23
  // assertion and effect is read once and kept ready, instead of being looked up by name at every pair.
@@ -19,7 +36,8 @@ export class Model {
19
36
  this.at = new Map(this.vars.map((v, i) => [v, i]));
20
37
  }
21
38
  // what evaluate(parseExpr(src), lookup) gave, with the same errors, raised only when the name is reached
22
- compile(e, depth) {
39
+ // `where`: the named guard being read, so that a name in it that is not one is placed there
40
+ compile(e, depth, where) {
23
41
  switch (e.k) {
24
42
  case 'lit': {
25
43
  const v = e.v;
@@ -35,36 +53,37 @@ export class Model {
35
53
  const guard = this.flow.guards[name];
36
54
  if (guard === undefined)
37
55
  return (state) => {
38
- throw new ModelError(`unknown name "${name}" (state ${state})`);
56
+ throw new ModelError(`unknown name "${name}" (state ${state})`, where);
39
57
  };
40
58
  if (depth > 8)
41
59
  return () => {
42
- throw new ModelError(`guard ${name} nests too deep`);
60
+ throw new ModelError(`guard ${name} nests too deep`, where);
43
61
  };
62
+ const at = inFlow(['guards', name]);
44
63
  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);
64
+ return (state, ctx) => (inner ??= this.compile(parsed(guard, at), depth + 1, at))(state, ctx);
46
65
  }
47
66
  case 'list': {
48
- const items = e.items.map((x) => this.compile(x, depth));
67
+ const items = e.items.map((x) => this.compile(x, depth, where));
49
68
  return (state, ctx) => items.map((f) => f(state, ctx));
50
69
  }
51
70
  case 'not': {
52
- const f = this.compile(e.e, depth);
71
+ const f = this.compile(e.e, depth, where);
53
72
  return (state, ctx) => !f(state, ctx);
54
73
  }
55
74
  case 'and': {
56
- const l = this.compile(e.l, depth);
57
- const r = this.compile(e.r, depth);
75
+ const l = this.compile(e.l, depth, where);
76
+ const r = this.compile(e.r, depth, where);
58
77
  return (state, ctx) => Boolean(l(state, ctx)) && Boolean(r(state, ctx));
59
78
  }
60
79
  case 'or': {
61
- const l = this.compile(e.l, depth);
62
- const r = this.compile(e.r, depth);
80
+ const l = this.compile(e.l, depth, where);
81
+ const r = this.compile(e.r, depth, where);
63
82
  return (state, ctx) => Boolean(l(state, ctx)) || Boolean(r(state, ctx));
64
83
  }
65
84
  case 'cmp': {
66
- const l = this.compile(e.l, depth);
67
- const r = this.compile(e.r, depth);
85
+ const l = this.compile(e.l, depth, where);
86
+ const r = this.compile(e.r, depth, where);
68
87
  if (e.op === '==')
69
88
  return (state, ctx) => l(state, ctx) === r(state, ctx);
70
89
  if (e.op === '!=')
@@ -92,7 +111,7 @@ export class Model {
92
111
  expr(src) {
93
112
  let f = this.compiled.get(src);
94
113
  if (f === undefined) {
95
- f = this.compile(parseExpr(src), 0);
114
+ f = this.compile(parsed(src), 0);
96
115
  this.compiled.set(src, f);
97
116
  }
98
117
  return f;
@@ -248,6 +267,7 @@ export class Model {
248
267
  guard: a.guard,
249
268
  effect: a.effect,
250
269
  taken: `${state}|${event ?? 'always'}|${i}`,
270
+ path: transitionPath(state, st, event, i),
251
271
  });
252
272
  w =
253
273
  st.type === 'final'
@@ -273,10 +293,33 @@ export class Model {
273
293
  }
274
294
  }
275
295
  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]])))}`);
296
+ throw new ModelError(`\`always\` in ${state} is not exhaustive for ${JSON.stringify(Object.fromEntries(this.vars.map((v, i) => [v, ctx[i]])))}`, inFlow(['states', state, 'always']));
277
297
  }
278
298
  return out;
279
299
  }
300
+ /**
301
+ * `err`, thrown by `steps(state, ctx)`, given the place in flow.yaml of the transition that threw it when it has
302
+ * none: the ways are tried again as `steps` tries them, and the one whose guard or effect throws is the place; else
303
+ * the state. Read only once a step has thrown, so the exploration itself pays nothing for it.
304
+ */
305
+ placeError(err, state, ctx) {
306
+ if (!(err instanceof Error) || err.place !== undefined || this.flow.states[state] === undefined)
307
+ return err;
308
+ for (const alts of this.waysOf(state).groups) {
309
+ for (const a of alts) {
310
+ try {
311
+ if (a.guard !== undefined && !this.test(a.guard, state, ctx))
312
+ continue;
313
+ this.apply(a.effect, ctx);
314
+ break;
315
+ }
316
+ catch {
317
+ return placed(err, inFlow(a.path));
318
+ }
319
+ }
320
+ }
321
+ return placed(err, inFlow(['states', state]));
322
+ }
280
323
  initials() {
281
324
  // every combination of the starting values, the first variable slowest: the order the nested lists gave
282
325
  const choices = this.vars.map((v, i) => {
@@ -360,24 +403,31 @@ export function explore(m, from = m.initials(), keep) {
360
403
  for (const c of from)
361
404
  push(m.flow.initial, c, -1, null);
362
405
  const starts = configs.length;
363
- for (let i = 0; i < configs.length; i++) {
364
- const { state, ctx } = configs[i];
365
- atState.set(state, (atState.get(state) ?? 0) + 1);
366
- const steps = m.steps(state, ctx);
367
- for (const s of steps) {
368
- if (keep !== undefined && !keep(configs[i], s))
369
- continue;
370
- taken.add(s.taken);
371
- push(s.target, s.ctx, i, s.event);
372
- }
373
- const always = m.flow.states[state].always;
374
- if (always !== undefined && steps.length > 0) {
375
- const counts = branchCounts.get(state) ?? always.map(() => 0);
376
- const taken0 = steps[0].taken;
377
- counts[Number(taken0.slice(taken0.lastIndexOf('|') + 1))] += 1;
378
- branchCounts.set(state, counts);
406
+ let i = 0;
407
+ try {
408
+ for (; i < configs.length; i++) {
409
+ const { state, ctx } = configs[i];
410
+ atState.set(state, (atState.get(state) ?? 0) + 1);
411
+ const steps = m.steps(state, ctx);
412
+ for (const s of steps) {
413
+ if (keep !== undefined && !keep(configs[i], s))
414
+ continue;
415
+ taken.add(s.taken);
416
+ push(s.target, s.ctx, i, s.event);
417
+ }
418
+ const always = m.flow.states[state].always;
419
+ if (always !== undefined && steps.length > 0) {
420
+ const counts = branchCounts.get(state) ?? always.map(() => 0);
421
+ const taken0 = steps[0].taken;
422
+ counts[Number(taken0.slice(taken0.lastIndexOf('|') + 1))] += 1;
423
+ branchCounts.set(state, counts);
424
+ }
379
425
  }
380
426
  }
427
+ catch (err) {
428
+ // where in flow.yaml the step that threw is written (SPEC.md §10)
429
+ throw i < configs.length ? m.placeError(err, configs[i].state, configs[i].ctx) : err;
430
+ }
381
431
  return { configs, starts, taken, atState, branchCounts };
382
432
  }
383
433
  export function pathTo(m, ex, index) {
@@ -428,8 +478,14 @@ export function checkAssertions(m, ex) {
428
478
  }
429
479
  return groups;
430
480
  };
431
- return (m.flow.assertions ?? []).map((a) => {
432
- const hit = firstHit(m, ex, a.never, byState);
481
+ return (m.flow.assertions ?? []).map((a, i) => {
482
+ let hit;
483
+ try {
484
+ hit = firstHit(m, ex, a.never, byState);
485
+ }
486
+ catch (err) {
487
+ throw placed(err, inFlow(['assertions', String(i), 'never']));
488
+ }
433
489
  if (hit < 0)
434
490
  return { id: a.id, never: a.never, say: a.say };
435
491
  return { id: a.id, never: a.never, say: a.say, violation: { ...pathTo(m, ex, hit), state: ex.configs[hit].state } };
package/dist/nav-page.js CHANGED
@@ -28,6 +28,16 @@ function pathWords(entries) {
28
28
  .map((e) => e.label)
29
29
  .join(' → ');
30
30
  }
31
+ // the way to a page in words: its labels from start, or from a page opened by its address (SPEC.md §6)
32
+ function wayWords(g, v, page) {
33
+ const r = v.reach.get(page);
34
+ const steps = r === undefined ? '' : pathWords(pathTo(v.reach, page));
35
+ if (r?.byAddress === undefined || r.byAddress === null)
36
+ return steps === '' ? 'automatic' : steps;
37
+ if (r.byAddress === page)
38
+ return 'by its address';
39
+ return `by the address of ${g.pages[r.byAddress].title}${steps === '' ? '' : `, then ${steps}`}`;
40
+ }
31
41
  function landingStats(g, v, rows, index) {
32
42
  if (v.landing === null)
33
43
  return '';
@@ -95,15 +105,15 @@ function reachTable(g, views) {
95
105
  continue;
96
106
  }
97
107
  const r = v.reach.get(p.id);
98
- const steps = r === undefined ? '' : pathWords(pathTo(v.reach, p.id));
99
108
  const mark = st === 'refused' ? ' <span class="c-refused">⛔</span>' : st === 'placeholder' ? ' <span class="c-placeholder">◌</span>' : st === 'missing' ? ' <span class="c-missing">✗</span>' : '';
100
- out.push(`<td class="reach"><b>${r?.clicks ?? 0}</b>${mark}${r?.usesData === true ? ' †' : ''}<span class="path">${esc(steps === '' ? 'automatic' : steps)}</span></td>`);
109
+ out.push(`<td class="reach"><b>${r?.clicks ?? 0}</b>${mark}${r?.usesData === true ? ' †' : ''}<span class="path">${esc(wayWords(g, v, p.id))}</span></td>`);
101
110
  }
102
111
  out.push('</tr>');
103
112
  }
104
113
  }
105
114
  out.push('</tbody></table></div>');
106
- out.push('<p class="legend-row"><span>number: fewest clicks from the moment the person signs in and “/” has sent them on</span><span>⛔ the page refuses them</span><span>◌ the page is not built</span><span>† needs an item in a list (a row to pick, a card to open)</span><span>–: no way there</span></p>');
115
+ const typed = Object.values(g.pages).some((pg) => pg.byAddress) ? '<span>by its address: opened by typing its address or by a link from outside the app, such as one in an e-mail, and counted from there</span>' : '';
116
+ out.push(`<p class="legend-row"><span>number: fewest clicks from the moment the person signs in and “/” has sent them on</span><span>⛔ the page refuses them</span><span>◌ the page is not built</span><span>† needs an item in a list (a row to pick, a card to open)</span>${typed}<span>–: no way there</span></p>`);
107
117
  return out.join('');
108
118
  }
109
119
  function goalsTable(g, goals) {
@@ -130,6 +140,8 @@ function siteMap(g) {
130
140
  tags.push(p.kind === 'placeholder' ? 'not built' : p.kind === 'missing' ? 'does not exist' : p.kind);
131
141
  if (p.opensFor !== null)
132
142
  tags.push(`opens only when ${p.opensFor}`);
143
+ if (p.byAddress)
144
+ tags.push('by its address');
133
145
  const notes = [p.opensFor !== null && p.refusal !== null ? `Otherwise ${p.refusal}.` : null, p.evidence].filter((n) => n !== null);
134
146
  out.push(`<li>${esc(p.title)}<span class="route">${code(p.route)}</span> ${tags.map((t) => `<span class="tag">${esc(t)}</span>`).join(' ')}${notes.map((n) => `<br><span class="muted">${esc(n)}</span>`).join('')}</li>`);
135
147
  }
package/dist/nav.d.ts CHANGED
@@ -30,6 +30,23 @@ export interface ExtractCfg {
30
30
  names?: Record<string, string>;
31
31
  titles?: Record<string, string>;
32
32
  locales?: string[];
33
+ entries?: Record<string, {
34
+ when?: string;
35
+ data?: boolean;
36
+ label?: string;
37
+ how?: string;
38
+ back?: boolean;
39
+ clicks?: number;
40
+ skip?: string;
41
+ }>;
42
+ }
43
+ export interface NavSource {
44
+ repo?: string;
45
+ commit?: string;
46
+ app?: string;
47
+ menu?: string;
48
+ locales?: string | string[];
49
+ [file: string]: string | string[] | undefined;
33
50
  }
34
51
  export interface Page {
35
52
  id: string;
@@ -43,6 +60,7 @@ export interface Page {
43
60
  pageCodes: string | null;
44
61
  evidence: string | null;
45
62
  refusal: string | null;
63
+ byAddress: boolean;
46
64
  }
47
65
  export interface Entry {
48
66
  id: string;
@@ -82,7 +100,7 @@ export interface NavGraph {
82
100
  status: string;
83
101
  summary: string;
84
102
  locale: string;
85
- source: Record<string, string>;
103
+ source: NavSource;
86
104
  facts: Record<string, FactSpec>;
87
105
  guards: Record<string, string>;
88
106
  pages: Record<string, Page>;
@@ -106,6 +124,7 @@ export interface Reach {
106
124
  via: Entry | null;
107
125
  prev: string | null;
108
126
  usesData: boolean;
127
+ byAddress: string | null;
109
128
  }
110
129
  export declare function walk(g: NavGraph, p: Persona): Map<string, Reach>;
111
130
  export declare function pathTo(reach: Map<string, Reach>, page: string): Entry[];
package/dist/nav.js CHANGED
@@ -93,6 +93,7 @@ export function loadNav(dir, locale, read = (file) => readFileSync(join(dir, fil
93
93
  pageCodes: p.pageCodes ?? null,
94
94
  evidence: p.evidence ?? null,
95
95
  refusal: p.refusal ?? null,
96
+ byAddress: p.byAddress === true,
96
97
  };
97
98
  }
98
99
  if (pages.start === undefined)
@@ -245,36 +246,47 @@ export const refusedFor = (g, p, page) => {
245
246
  return guard !== null && !holds(g, p, guard);
246
247
  };
247
248
  // Fewest clicks from "start"; ties go to the entry written first. Around a page that refuses the person only the frame's own entries work.
249
+ // A page opened by its address (byAddress) that no way from start reaches is reached so, with no clicks, and what it
250
+ // leads to that no way from start reaches is counted from it; every way from start stays as it is (SPEC.md §6).
248
251
  export function walk(g, p) {
249
252
  const best = new Map();
250
253
  const done = new Set();
251
254
  const index = (id) => Object.keys(g.pages).indexOf(id);
252
- best.set('start', { page: 'start', clicks: 0, via: null, prev: null, usesData: false });
253
- for (;;) {
254
- let pick = null;
255
- for (const r of best.values()) {
256
- if (done.has(r.page))
257
- continue;
258
- if (pick === null || r.clicks < pick.clicks || (r.clicks === pick.clicks && index(r.page) < index(pick.page)))
259
- pick = r;
260
- }
261
- if (pick === null)
262
- break;
263
- done.add(pick.page);
264
- const refused = refusedFor(g, p, pick.page);
265
- for (const e of g.entries) {
266
- if (e.from !== pick.page)
267
- continue;
268
- if (refused && !frameLevel(e))
269
- continue;
270
- if (!holds(g, p, e.when))
271
- continue;
272
- const clicks = pick.clicks + e.clicks;
273
- const held = best.get(e.to);
274
- if (held === undefined || clicks < held.clicks)
275
- best.set(e.to, { page: e.to, clicks, via: e, prev: pick.page, usesData: pick.usesData || e.data });
255
+ const settle = (fixed) => {
256
+ for (;;) {
257
+ let pick = null;
258
+ for (const r of best.values()) {
259
+ if (done.has(r.page))
260
+ continue;
261
+ if (pick === null || r.clicks < pick.clicks || (r.clicks === pick.clicks && index(r.page) < index(pick.page)))
262
+ pick = r;
263
+ }
264
+ if (pick === null)
265
+ break;
266
+ done.add(pick.page);
267
+ const refused = refusedFor(g, p, pick.page);
268
+ for (const e of g.entries) {
269
+ if (e.from !== pick.page || fixed.has(e.to))
270
+ continue;
271
+ if (refused && !frameLevel(e))
272
+ continue;
273
+ if (!holds(g, p, e.when))
274
+ continue;
275
+ const clicks = pick.clicks + e.clicks;
276
+ const held = best.get(e.to);
277
+ if (held === undefined || clicks < held.clicks)
278
+ best.set(e.to, { page: e.to, clicks, via: e, prev: pick.page, usesData: pick.usesData || e.data, byAddress: pick.byAddress });
279
+ }
276
280
  }
277
- }
281
+ };
282
+ best.set('start', { page: 'start', clicks: 0, via: null, prev: null, usesData: false, byAddress: null });
283
+ settle(new Set());
284
+ const fromStart = new Set(best.keys());
285
+ for (const pg of Object.values(g.pages))
286
+ if (pg.byAddress && !best.has(pg.id))
287
+ best.set(pg.id, { page: pg.id, clicks: 0, via: null, prev: null, usesData: false, byAddress: pg.id });
288
+ if (best.size > fromStart.size)
289
+ settle(fromStart);
278
290
  return best;
279
291
  }
280
292
  export function pathTo(reach, page) {
@@ -0,0 +1,50 @@
1
+ export declare const NEEDS_FILE = "needs.yaml";
2
+ export declare const GAPS_FILE = "needs.gaps.yaml";
3
+ export interface Evidence {
4
+ source?: string;
5
+ test?: string;
6
+ name?: string;
7
+ scenario?: string;
8
+ binding?: string;
9
+ state?: string;
10
+ node?: string;
11
+ setting?: string;
12
+ in?: string;
13
+ }
14
+ export interface Need {
15
+ id: string;
16
+ need: string;
17
+ evidence?: Evidence[];
18
+ missing?: string;
19
+ note?: string;
20
+ }
21
+ export interface Needs {
22
+ schema?: string;
23
+ kind: 'needs';
24
+ id: string;
25
+ status?: string;
26
+ needs: Need[];
27
+ }
28
+ export type NeedRule = 'evidence-unresolved' | 'need-no-evidence' | 'need-unlisted' | 'need-gap' | 'need-twice' | 'needs-place' | 'gap-met' | 'gap-unknown';
29
+ export interface NeedProblem {
30
+ rule: NeedRule;
31
+ file: string;
32
+ line: number;
33
+ column: number;
34
+ message: string;
35
+ graph: string;
36
+ element: string;
37
+ }
38
+ export interface NeedsCheck {
39
+ problems: NeedProblem[];
40
+ files: number;
41
+ needs: number;
42
+ gaps: number;
43
+ }
44
+ export declare function readNeeds(dir: string): Needs | null;
45
+ export interface Planned {
46
+ states: Map<string, string[]>;
47
+ nodes: Map<string, string[]>;
48
+ }
49
+ export declare function planned(needs: Needs | null): Planned;
50
+ export declare function checkNeeds(files: string[], app: string): NeedsCheck;