@yutaura/live-spec 0.1.0 → 0.2.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.
Files changed (45) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/README.md +12 -2
  3. package/dist/cli.js +49 -10
  4. package/dist/diff.d.ts +92 -2
  5. package/dist/diff.js +101 -64
  6. package/dist/ensure-scope.d.ts +9 -0
  7. package/dist/ensure-scope.js +81 -0
  8. package/dist/ensure.d.ts +4 -0
  9. package/dist/ensure.js +27 -2
  10. package/dist/explore.d.ts +20 -1
  11. package/dist/explore.js +34 -12
  12. package/dist/generate.d.ts +6 -0
  13. package/dist/generate.js +16 -4
  14. package/dist/load.d.ts +7 -1
  15. package/dist/load.js +5 -0
  16. package/dist/model.d.ts +85 -0
  17. package/dist/model.js +34 -0
  18. package/dist/mutation.d.ts +3 -0
  19. package/dist/mutation.js +51 -0
  20. package/dist/omission.d.ts +6 -0
  21. package/dist/omission.js +177 -0
  22. package/dist/project-diff.d.ts +24 -0
  23. package/dist/project-diff.js +76 -0
  24. package/dist/render/diff-html.d.ts +2 -0
  25. package/dist/render/diff-html.js +148 -0
  26. package/dist/render/diff-markdown.d.ts +11 -0
  27. package/dist/render/diff-markdown.js +70 -0
  28. package/dist/render/ensures.d.ts +2 -0
  29. package/dist/render/ensures.js +21 -0
  30. package/dist/render/html.d.ts +1 -0
  31. package/dist/render/html.js +42 -10
  32. package/dist/render/layout.d.ts +2 -0
  33. package/dist/render/layout.js +1 -1
  34. package/dist/render/markdown.js +24 -3
  35. package/dist/render/review.d.ts +17 -0
  36. package/dist/render/review.js +17 -0
  37. package/dist/render/union.d.ts +23 -0
  38. package/dist/render/union.js +43 -0
  39. package/dist/spec-loader.d.ts +2 -0
  40. package/dist/spec-loader.js +24 -0
  41. package/dist/static.d.ts +5 -0
  42. package/dist/static.js +34 -0
  43. package/package.json +7 -1
  44. package/skills/live-spec-authoring/SKILL.md +32 -2
  45. package/skills/live-spec-review/SKILL.md +16 -10
package/dist/explore.d.ts CHANGED
@@ -1,6 +1,25 @@
1
- import type { Spec } from "./load.js";
1
+ import type { Spec, State } from "./load.js";
2
2
  import { type Graph } from "./model.js";
3
3
  export interface ExploreOptions {
4
4
  readonly maxStates?: number;
5
+ /** この操作の、この理由の ensure を外して探索する(ensure のミューテーション) */
6
+ readonly bypassEnsure?: {
7
+ readonly action: string;
8
+ readonly reason: string;
9
+ };
5
10
  }
11
+ type Outcome = {
12
+ readonly kind: "ok";
13
+ readonly next: State;
14
+ } | {
15
+ readonly kind: "not-allowed";
16
+ readonly reason: string;
17
+ } | {
18
+ readonly kind: "error";
19
+ readonly message: string;
20
+ } | {
21
+ readonly kind: "invalid-return";
22
+ };
23
+ export declare const runAction: (fn: Spec["actions"][number][1], state: State, actor: unknown, args: ReadonlyArray<unknown>, bypassReason: string | null) => Outcome;
6
24
  export declare const explore: (spec: Spec, options?: ExploreOptions) => Graph;
25
+ export {};
package/dist/explore.js CHANGED
@@ -1,8 +1,10 @@
1
- import { isNotAllowed } from "./ensure.js";
1
+ import { isNotAllowed, withEnsureBypassed } from "./ensure.js";
2
2
  import { canonical } from "./model.js";
3
3
  import { buttonsOf } from "./surface.js";
4
4
  const DEFAULT_MAX_STATES = 500;
5
- const run = (fn, state, actor, args) => {
5
+ export const runAction = (fn, state, actor, args, bypassReason) => {
6
+ if (bypassReason !== null)
7
+ return withEnsureBypassed(bypassReason, () => runAction(fn, state, actor, args, null));
6
8
  try {
7
9
  // structuredClone を渡す理由: 関数が引数を破壊的に変更しても、探索済みの状態が壊れないようにするため。
8
10
  const next = fn(structuredClone(state), structuredClone(actor), ...structuredClone(args));
@@ -28,6 +30,7 @@ const invariantHolds = (holds, values) => {
28
30
  };
29
31
  export const explore = (spec, options = {}) => {
30
32
  const maxStates = options.maxStates ?? DEFAULT_MAX_STATES;
33
+ const bypassFor = (action) => (options.bypassEnsure?.action === action ? options.bypassEnsure.reason : null);
31
34
  const ids = new Map();
32
35
  const valuesById = new Map();
33
36
  const queue = [];
@@ -77,7 +80,7 @@ export const explore = (spec, options = {}) => {
77
80
  for (const [action, fn] of runnable) {
78
81
  for (const [actor, actorValue] of spec.actors) {
79
82
  for (const [input, args] of spec.inputs[action] ?? NO_INPUT) {
80
- const outcome = run(fn, values, actorValue, args);
83
+ const outcome = runAction(fn, values, actorValue, args, bypassFor(action));
81
84
  if (outcome.kind !== "ok") {
82
85
  if (outcome.kind === "error")
83
86
  findings.push({ kind: "action-error", action, actor, state: from, message: outcome.message });
@@ -103,7 +106,7 @@ export const explore = (spec, options = {}) => {
103
106
  }
104
107
  processed.add(from);
105
108
  }
106
- const labelOf = labeler([...valuesById.values()]);
109
+ const labelOf = labeler([...valuesById.values()], Object.keys(spec.initial));
107
110
  const states = [...valuesById.entries()].map(([id, values]) => ({
108
111
  id,
109
112
  label: labelOf(values),
@@ -130,7 +133,7 @@ export const explore = (spec, options = {}) => {
130
133
  findings.push({ kind: "never-enabled-action", action });
131
134
  }
132
135
  const surfaces = spec.surface === null ? [] : checkSurfaces(spec.surface, spec, [...processed], valuesById, availability, findings);
133
- const scenarios = runScenarios(spec, (values) => ids.get(JSON.stringify(values)) ?? null, findings);
136
+ const scenarios = runScenarios(spec, (values) => ids.get(JSON.stringify(values)) ?? null, findings, bypassFor);
134
137
  const questions = spec.questions.map((q) => {
135
138
  const about = q.about;
136
139
  const action = typeof about === "function" ? about.name : null;
@@ -167,13 +170,13 @@ export const explore = (spec, options = {}) => {
167
170
  const show = (v) => (typeof v === "string" ? v : JSON.stringify(v));
168
171
  // シナリオは探索とは別に、初期状態から手順どおりに実行する。探索は「到達できるか」を確かめるが、
169
172
  // シナリオは「この業務の流れが、この順番で通るか」を名指しで確かめる(受け入れテストの役割)。
170
- const runScenarios = (spec, idOf, findings) => spec.scenarios.map(([name, steps]) => {
173
+ const runScenarios = (spec, idOf, findings, bypassFor) => spec.scenarios.map(([name, steps]) => {
171
174
  let values = canonical(spec.initial);
172
175
  const records = [];
173
176
  for (const [i, step] of steps.entries()) {
174
177
  const action = step.do.name;
175
178
  const fail = (reason) => {
176
- records.push({ action, actor: step.by, input: step.input ?? null, from: idOf(values), to: null, ok: false, reason });
179
+ records.push({ action, actor: step.by, input: step.input ?? null, from: idOf(values), to: null, ok: false, reason, denied: step.denied !== undefined });
177
180
  findings.push({ kind: "scenario-failed", scenario: name, step: i + 1, reason });
178
181
  };
179
182
  const actor = spec.actors.find(([n]) => n === step.by);
@@ -195,7 +198,24 @@ const runScenarios = (spec, idOf, findings) => spec.scenarios.map(([name, steps]
195
198
  fail("この操作には入力の例(input)を指定する");
196
199
  break;
197
200
  }
198
- const outcome = run(step.do, values, actor[1], args);
201
+ const outcome = runAction(step.do, values, actor[1], args, bypassFor(action));
202
+ if (step.denied !== undefined) {
203
+ if (outcome.kind === "ok") {
204
+ fail("断られるはずが、実行できた");
205
+ break;
206
+ }
207
+ if (outcome.kind !== "not-allowed") {
208
+ fail(outcome.kind === "error" ? `例外: ${outcome.message}` : "オブジェクトを返さなかった");
209
+ break;
210
+ }
211
+ if (typeof step.denied === "string" && outcome.reason !== step.denied) {
212
+ fail(`断られる理由が違う: 期待「${step.denied}」、実際「${outcome.reason}」`);
213
+ break;
214
+ }
215
+ const here = idOf(values);
216
+ records.push({ action, actor: step.by, input: step.input ?? null, from: here, to: here, ok: true, reason: outcome.reason, denied: true });
217
+ continue;
218
+ }
199
219
  if (outcome.kind !== "ok") {
200
220
  fail(outcome.kind === "not-allowed" ? outcome.reason : outcome.kind === "error" ? `例外: ${outcome.message}` : "オブジェクトを返さなかった");
201
221
  break;
@@ -206,7 +226,7 @@ const runScenarios = (spec, idOf, findings) => spec.scenarios.map(([name, steps]
206
226
  fail(`期待: ${k} = ${show(v)}、実際: ${k} = ${show(outcome.next[k])}`);
207
227
  break;
208
228
  }
209
- records.push({ action, actor: step.by, input: step.input ?? null, from: idOf(values), to: idOf(outcome.next), ok: true, reason: null });
229
+ records.push({ action, actor: step.by, input: step.input ?? null, from: idOf(values), to: idOf(outcome.next), ok: true, reason: null, denied: false });
210
230
  values = outcome.next;
211
231
  }
212
232
  return { name, passed: records.length === steps.length && records.every((r) => r.ok), steps: records };
@@ -251,10 +271,13 @@ const checkSurfaces = (surface, spec, states, valuesById, availability, findings
251
271
  // ラベルに「状態によって値が変わる項目」だけを使う理由: authorId のように
252
272
  // どの状態でも同じ値の項目まで並べると、状態を区別する情報が埋もれるため。
253
273
  // 文字列の項目を先に並べるのは、status のような主たる区分を先頭に見せるため。
254
- const labeler = (all) => {
274
+ // initial に書いた順に並べる理由: 書き手は、読み手に最初に見せたい項目を先に書く。
275
+ // 状態の値そのもの(canonical)はキーを並べ替えて持つので、その順番は使わない。
276
+ const labeler = (all, declared) => {
277
+ const position = (k) => (declared.includes(k) ? declared.indexOf(k) : declared.length);
255
278
  const keys = [...new Set(all.flatMap((v) => Object.keys(v)))]
256
279
  .filter((k) => new Set(all.map((v) => JSON.stringify(v[k]))).size > 1)
257
- .sort((a, b) => rank(all[0][a]) - rank(all[0][b]) || a.localeCompare(b));
280
+ .sort((a, b) => position(a) - position(b) || a.localeCompare(b));
258
281
  return (values) => {
259
282
  const parts = keys.flatMap((k) => {
260
283
  const v = values[k];
@@ -267,4 +290,3 @@ const labeler = (all) => {
267
290
  return parts.length > 0 ? parts.join(" · ") : "初期状態";
268
291
  };
269
292
  };
270
- const rank = (v) => (typeof v === "string" ? 0 : typeof v === "boolean" ? 1 : 2);
@@ -1,3 +1,4 @@
1
+ import { type DomainObject } from "./render/html.js";
1
2
  export type OutputFormat = "markdown" | "json" | "html";
2
3
  export type Output = {
3
4
  readonly kind: "text";
@@ -8,6 +9,11 @@ export type Output = {
8
9
  };
9
10
  export declare const specNameOf: (path: string) => string;
10
11
  export declare const expandSpecPaths: (paths: ReadonlyArray<string>) => Array<string>;
12
+ export interface LoadObjectOptions {
13
+ readonly name?: string;
14
+ readonly source?: string;
15
+ }
16
+ export declare const loadObject: (path: string, options?: LoadObjectOptions) => Promise<DomainObject>;
11
17
  export declare const generateProject: (paths: ReadonlyArray<string>, format: OutputFormat, title?: string) => Promise<Output>;
12
18
  export declare const failureOutput: (format: OutputFormat, paths: ReadonlyArray<string>, error: unknown) => Output;
13
19
  export declare const generate: (path: string, format: OutputFormat) => Promise<string>;
package/dist/generate.js CHANGED
@@ -11,6 +11,9 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
11
11
  import { basename, dirname, join, resolve } from "node:path";
12
12
  import { pathToFileURL } from "node:url";
13
13
  import { explore } from "./explore.js";
14
+ import { checkEnsures } from "./mutation.js";
15
+ import { findOmissionHints } from "./omission.js";
16
+ import { unguardedId } from "./render/review.js";
14
17
  import { loadSpec } from "./load.js";
15
18
  import { normalize } from "./model.js";
16
19
  import { renderErrorHtml, renderProjectHtml } from "./render/html.js";
@@ -37,15 +40,24 @@ export const expandSpecPaths = (paths) => paths.flatMap((p) => {
37
40
  // --watch で spec を読み直しても古い内容のまま生成されてしまう。クエリが違えば別モジュールとして読み込まれる。
38
41
  // spec から import しているファイル(共通の型など)は読み直されない点に注意。
39
42
  let importVersion = 0;
40
- const loadObject = async (path) => {
43
+ // 名前とソースを外から渡せるようにしている理由: 差分を取るとき、git から取り出した spec を一時ファイルに書いて
44
+ // 読み込むので、ファイル名からではなく元の spec の名前で扱う必要があるため。
45
+ export const loadObject = async (path, options = {}) => {
41
46
  const url = pathToFileURL(resolve(path));
42
47
  url.search = `?v=${++importVersion}`;
43
48
  const mod = (await import(__rewriteRelativeImportExtension(url.href)));
44
- const spec = loadSpec(specNameOf(path), mod, { source: await readFile(path, "utf8") });
45
- return { spec, graph: explore(spec) };
49
+ const spec = loadSpec(options.name ?? specNameOf(path), mod, { source: options.source ?? (await readFile(path, "utf8")) });
50
+ const graph = explore(spec);
51
+ const ensures = checkEnsures(spec, graph);
52
+ const omissions = findOmissionHints(spec, graph);
53
+ const known = new Set([...omissions.map((h) => h.id), ...ensures.filter((e) => e.verdict === "unguarded").map(unguardedId)]);
54
+ const unmatched = Object.keys(spec.reviewed)
55
+ .filter((id) => !known.has(id))
56
+ .map((id) => ({ kind: "reviewed-unmatched", id }));
57
+ return { spec, graph: { ...graph, ensures, omissions, findings: [...graph.findings, ...unmatched] } };
46
58
  };
47
59
  export const generateProject = async (paths, format, title) => {
48
- const objects = resolveLinks(await Promise.all(paths.map(loadObject)));
60
+ const objects = resolveLinks(await Promise.all(paths.map((p) => loadObject(p))));
49
61
  const names = objects.map((o) => o.spec.name);
50
62
  const duplicated = names.find((n, i) => names.indexOf(n) !== i);
51
63
  if (duplicated !== undefined)
package/dist/load.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type FieldValue } from "./static.js";
1
+ import { type EnsureCall, type FieldValue } from "./static.js";
2
2
  export type State = Readonly<Record<string, unknown>>;
3
3
  export type ActionFn = (state: never, actor: never) => unknown;
4
4
  export type InvariantFn = (state: never) => unknown;
@@ -26,6 +26,8 @@ export interface ScenarioStep {
26
26
  readonly by: string;
27
27
  readonly input?: string;
28
28
  readonly expect?: Readonly<Record<string, unknown>>;
29
+ /** この手順は断られるはず。文字列なら、その理由で断られるはず */
30
+ readonly denied?: string | true;
29
31
  }
30
32
  export interface Spec {
31
33
  readonly name: string;
@@ -42,7 +44,11 @@ export interface Spec {
42
44
  readonly docs: Readonly<Record<string, string>>;
43
45
  readonly fieldDocs: Readonly<Record<string, string>>;
44
46
  readonly evidence: Readonly<Record<string, ReadonlyArray<string>>>;
47
+ /** 操作ごとの ensure(静的解析で読んだもの。ソースがなければ空) */
48
+ readonly ensures: Readonly<Record<string, ReadonlyArray<EnsureCall>>>;
45
49
  readonly glossary: Readonly<Record<string, string>>;
50
+ /** 確かめてほしい点のうち、人や AI が「意図どおり」と答えたもの(キーは候補の id、値はその理由) */
51
+ readonly reviewed: Readonly<Record<string, string>>;
46
52
  readonly fields: Readonly<Record<string, ReadonlyArray<FieldValue>>>;
47
53
  readonly stateDoc: string | null;
48
54
  }
package/dist/load.js CHANGED
@@ -10,6 +10,7 @@ const RESERVED = new Set([
10
10
  "links",
11
11
  "glossary",
12
12
  "decisions",
13
+ "reviewed",
13
14
  ]);
14
15
  const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
15
16
  export const loadSpec = (name, mod, options = {}) => {
@@ -44,6 +45,10 @@ export const loadSpec = (name, mod, options = {}) => {
44
45
  docs: info?.docs ?? {},
45
46
  fieldDocs: info?.fieldDocs ?? {},
46
47
  evidence: info?.evidence ?? {},
48
+ ensures: info?.ensures ?? {},
49
+ reviewed: isRecord(mod.reviewed)
50
+ ? Object.fromEntries(Object.entries(mod.reviewed).filter((e) => typeof e[1] === "string"))
51
+ : {},
47
52
  glossary: isRecord(mod.glossary)
48
53
  ? Object.fromEntries(Object.entries(mod.glossary).filter((e) => typeof e[1] === "string"))
49
54
  : {},
package/dist/model.d.ts CHANGED
@@ -87,6 +87,9 @@ export declare const Finding: Schema.Union<readonly [Schema.Struct<{
87
87
  }>, Schema.Struct<{
88
88
  readonly kind: Schema.Literal<"unknown-inputs">;
89
89
  readonly action: Schema.String;
90
+ }>, Schema.Struct<{
91
+ readonly kind: Schema.Literal<"reviewed-unmatched">;
92
+ readonly id: Schema.String;
90
93
  }>, Schema.Struct<{
91
94
  readonly kind: Schema.Literal<"invariant-violation">;
92
95
  readonly invariant: Schema.String;
@@ -97,6 +100,32 @@ export declare const Finding: Schema.Union<readonly [Schema.Struct<{
97
100
  }>>;
98
101
  }>]>;
99
102
  export type Finding = typeof Finding.Type;
103
+ export declare const EnsureCheck: Schema.Struct<{
104
+ readonly action: Schema.String;
105
+ readonly reason: Schema.String;
106
+ readonly line: Schema.NullOr<Schema.Number>;
107
+ readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
108
+ readonly caughtBy: Schema.$Array<Schema.String>;
109
+ readonly effects: Schema.$Array<Schema.Struct<{
110
+ readonly state: Schema.String;
111
+ readonly action: Schema.String;
112
+ readonly actor: Schema.String;
113
+ readonly input: Schema.NullOr<Schema.String>;
114
+ readonly after: Schema.String;
115
+ }>>;
116
+ readonly newStates: Schema.$Array<Schema.String>;
117
+ /** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
118
+ readonly passes: Schema.String;
119
+ }>;
120
+ export type EnsureCheck = typeof EnsureCheck.Type;
121
+ export declare const OmissionHint: Schema.Struct<{
122
+ readonly id: Schema.String;
123
+ readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field"]>;
124
+ readonly action: Schema.NullOr<Schema.String>;
125
+ readonly field: Schema.NullOr<Schema.String>;
126
+ readonly message: Schema.String;
127
+ }>;
128
+ export type OmissionHint = typeof OmissionHint.Type;
100
129
  export declare const Graph: Schema.Struct<{
101
130
  readonly states: Schema.$Array<Schema.Struct<{
102
131
  readonly id: Schema.String;
@@ -185,6 +214,9 @@ export declare const Graph: Schema.Struct<{
185
214
  }>, Schema.Struct<{
186
215
  readonly kind: Schema.Literal<"unknown-inputs">;
187
216
  readonly action: Schema.String;
217
+ }>, Schema.Struct<{
218
+ readonly kind: Schema.Literal<"reviewed-unmatched">;
219
+ readonly id: Schema.String;
188
220
  }>, Schema.Struct<{
189
221
  readonly kind: Schema.Literal<"invariant-violation">;
190
222
  readonly invariant: Schema.String;
@@ -221,6 +253,30 @@ export declare const Graph: Schema.Struct<{
221
253
  readonly action: Schema.NullOr<Schema.String>;
222
254
  readonly states: Schema.$Array<Schema.String>;
223
255
  }>>;
256
+ readonly ensures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
257
+ readonly action: Schema.String;
258
+ readonly reason: Schema.String;
259
+ readonly line: Schema.NullOr<Schema.Number>;
260
+ readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
261
+ readonly caughtBy: Schema.$Array<Schema.String>;
262
+ readonly effects: Schema.$Array<Schema.Struct<{
263
+ readonly state: Schema.String;
264
+ readonly action: Schema.String;
265
+ readonly actor: Schema.String;
266
+ readonly input: Schema.NullOr<Schema.String>;
267
+ readonly after: Schema.String;
268
+ }>>;
269
+ readonly newStates: Schema.$Array<Schema.String>;
270
+ /** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
271
+ readonly passes: Schema.String;
272
+ }>>>;
273
+ readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
274
+ readonly id: Schema.String;
275
+ readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field"]>;
276
+ readonly action: Schema.NullOr<Schema.String>;
277
+ readonly field: Schema.NullOr<Schema.String>;
278
+ readonly message: Schema.String;
279
+ }>>>;
224
280
  readonly scenarios: Schema.$Array<Schema.Struct<{
225
281
  readonly name: Schema.String;
226
282
  readonly passed: Schema.Boolean;
@@ -232,6 +288,7 @@ export declare const Graph: Schema.Struct<{
232
288
  readonly to: Schema.NullOr<Schema.String>;
233
289
  readonly ok: Schema.Boolean;
234
290
  readonly reason: Schema.NullOr<Schema.String>;
291
+ readonly denied: Schema.Boolean;
235
292
  }>>;
236
293
  }>>;
237
294
  }>;
@@ -325,6 +382,9 @@ export declare const SpecModel: Schema.Struct<{
325
382
  }>, Schema.Struct<{
326
383
  readonly kind: Schema.Literal<"unknown-inputs">;
327
384
  readonly action: Schema.String;
385
+ }>, Schema.Struct<{
386
+ readonly kind: Schema.Literal<"reviewed-unmatched">;
387
+ readonly id: Schema.String;
328
388
  }>, Schema.Struct<{
329
389
  readonly kind: Schema.Literal<"invariant-violation">;
330
390
  readonly invariant: Schema.String;
@@ -361,6 +421,30 @@ export declare const SpecModel: Schema.Struct<{
361
421
  readonly action: Schema.NullOr<Schema.String>;
362
422
  readonly states: Schema.$Array<Schema.String>;
363
423
  }>>;
424
+ readonly ensures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
425
+ readonly action: Schema.String;
426
+ readonly reason: Schema.String;
427
+ readonly line: Schema.NullOr<Schema.Number>;
428
+ readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
429
+ readonly caughtBy: Schema.$Array<Schema.String>;
430
+ readonly effects: Schema.$Array<Schema.Struct<{
431
+ readonly state: Schema.String;
432
+ readonly action: Schema.String;
433
+ readonly actor: Schema.String;
434
+ readonly input: Schema.NullOr<Schema.String>;
435
+ readonly after: Schema.String;
436
+ }>>;
437
+ readonly newStates: Schema.$Array<Schema.String>;
438
+ /** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
439
+ readonly passes: Schema.String;
440
+ }>>>;
441
+ readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
442
+ readonly id: Schema.String;
443
+ readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field"]>;
444
+ readonly action: Schema.NullOr<Schema.String>;
445
+ readonly field: Schema.NullOr<Schema.String>;
446
+ readonly message: Schema.String;
447
+ }>>>;
364
448
  readonly scenarios: Schema.$Array<Schema.Struct<{
365
449
  readonly name: Schema.String;
366
450
  readonly passed: Schema.Boolean;
@@ -372,6 +456,7 @@ export declare const SpecModel: Schema.Struct<{
372
456
  readonly to: Schema.NullOr<Schema.String>;
373
457
  readonly ok: Schema.Boolean;
374
458
  readonly reason: Schema.NullOr<Schema.String>;
459
+ readonly denied: Schema.Boolean;
375
460
  }>>;
376
461
  }>>;
377
462
  readonly name: Schema.String;
package/dist/model.js CHANGED
@@ -55,6 +55,7 @@ export const Finding = Schema.Union([
55
55
  Schema.Struct({ kind: Schema.Literal("surface-action-missing"), state: Schema.String, actor: Schema.String, action: Schema.String }),
56
56
  Schema.Struct({ kind: Schema.Literal("surface-error"), state: Schema.String, actor: Schema.String, message: Schema.String }),
57
57
  Schema.Struct({ kind: Schema.Literal("unknown-inputs"), action: Schema.String }),
58
+ Schema.Struct({ kind: Schema.Literal("reviewed-unmatched"), id: Schema.String }),
58
59
  Schema.Struct({
59
60
  kind: Schema.Literal("invariant-violation"),
60
61
  invariant: Schema.String,
@@ -62,6 +63,36 @@ export const Finding = Schema.Union([
62
63
  path: Schema.Array(Schema.Struct({ action: Schema.String, actor: Schema.String })),
63
64
  }),
64
65
  ]);
66
+ // ensure を 1 つ外して探索し直した結果。caught: 外すと何か(不変条件・シナリオ・画面・実行時エラー)が気づく。
67
+ // unguarded: 振る舞いは変わるのに何も気づかない(その条件の意図が、ほかのどこにも書かれていない)。
68
+ // no-effect: 外しても振る舞いが変わらない(ほかの条件と重なっている)。
69
+ export const EnsureCheck = Schema.Struct({
70
+ action: Schema.String,
71
+ reason: Schema.String,
72
+ line: Schema.NullOr(Schema.Number),
73
+ verdict: Schema.Literals(["caught", "unguarded", "no-effect"]),
74
+ caughtBy: Schema.Array(Schema.String),
75
+ effects: Schema.Array(Schema.Struct({ state: Schema.String, action: Schema.String, actor: Schema.String, input: Schema.NullOr(Schema.String), after: Schema.String })),
76
+ newStates: Schema.Array(Schema.String),
77
+ /** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
78
+ passes: Schema.String,
79
+ });
80
+ // spec を実行した結果から見つけた、書き忘れていそうな条件の候補。id は reviewed で「意図どおり」と答えるときのキー。
81
+ export const OmissionHint = Schema.Struct({
82
+ id: Schema.String,
83
+ kind: Schema.Literals([
84
+ "anyone-can",
85
+ "anytime",
86
+ "ignores-input",
87
+ "same-for-similar-actors",
88
+ "indistinguishable-example",
89
+ "indistinguishable-actor",
90
+ "unused-field",
91
+ ]),
92
+ action: Schema.NullOr(Schema.String),
93
+ field: Schema.NullOr(Schema.String),
94
+ message: Schema.String,
95
+ });
65
96
  export const Graph = Schema.Struct({
66
97
  states: Schema.Array(ExploredState),
67
98
  transitions: Schema.Array(Transition),
@@ -80,6 +111,8 @@ export const Graph = Schema.Struct({
80
111
  action: Schema.NullOr(Schema.String),
81
112
  states: Schema.Array(Schema.String),
82
113
  })),
114
+ ensures: Schema.optionalKey(Schema.Array(EnsureCheck)),
115
+ omissions: Schema.optionalKey(Schema.Array(OmissionHint)),
83
116
  scenarios: Schema.Array(Schema.Struct({
84
117
  name: Schema.String,
85
118
  passed: Schema.Boolean,
@@ -91,6 +124,7 @@ export const Graph = Schema.Struct({
91
124
  to: Schema.NullOr(Schema.String),
92
125
  ok: Schema.Boolean,
93
126
  reason: Schema.NullOr(Schema.String),
127
+ denied: Schema.Boolean,
94
128
  })),
95
129
  })),
96
130
  });
@@ -0,0 +1,3 @@
1
+ import type { Spec } from "./load.js";
2
+ import { type EnsureCheck, type Graph } from "./model.js";
3
+ export declare const checkEnsures: (spec: Spec, graph: Graph) => Array<EnsureCheck>;
@@ -0,0 +1,51 @@
1
+ import { diffModels, groupAvailability } from "./diff.js";
2
+ import { ensureScope } from "./ensure-scope.js";
3
+ import { explore } from "./explore.js";
4
+ import { normalize } from "./model.js";
5
+ const caughtLabel = (f) => {
6
+ switch (f.kind) {
7
+ case "invariant-violation":
8
+ return `不変条件「${String(f.invariant)}」`;
9
+ case "scenario-failed":
10
+ return `シナリオ「${String(f.scenario)}」`;
11
+ case "surface-action-missing":
12
+ case "surface-action-not-allowed":
13
+ return "画面のボタン";
14
+ case "action-error":
15
+ case "invalid-return":
16
+ return "実行時エラー";
17
+ case "state-limit":
18
+ return "状態の数の上限";
19
+ default:
20
+ return String(f.kind);
21
+ }
22
+ };
23
+ // 静的解析で読めなかったとき(ソースがない spec)は、探索で実際に止めた理由から外す対象を決める。
24
+ // この場合、一度も止めなかった ensure は対象にできない。
25
+ const targetsOf = (spec, graph) => spec.actions.flatMap(([action]) => {
26
+ const found = spec.ensures[action];
27
+ if (found !== undefined)
28
+ return found.map((e) => ({ action, reason: e.reason, line: e.line }));
29
+ const reasons = [...new Set(graph.availability.filter((a) => a.action === action && a.reason !== null).map((a) => a.reason))];
30
+ return reasons.map((reason) => ({ action, reason, line: null }));
31
+ });
32
+ export const checkEnsures = (spec, graph) => {
33
+ const original = normalize(spec, graph);
34
+ const seen = new Set();
35
+ return targetsOf(spec, graph).flatMap((target) => {
36
+ // 同じ操作に同じ理由の ensure が 2 つあると、外すときに区別できないので 1 回だけ調べる。
37
+ const key = JSON.stringify([target.action, target.reason]);
38
+ if (seen.has(key))
39
+ return [];
40
+ seen.add(key);
41
+ const mutant = explore(spec, { bypassEnsure: target });
42
+ const d = diffModels(original, normalize(spec, mutant));
43
+ const effects = groupAvailability(d.availability)
44
+ .flipped.filter((a) => a.after.startsWith("✅"))
45
+ .map((a) => ({ state: a.state, action: a.action, actor: a.actor, input: a.input, after: a.after }));
46
+ const caughtBy = [...new Set(d.findings.added.map((f) => caughtLabel(f)))];
47
+ const changed = effects.length + d.states.added.length + d.transitions.added.length > 0;
48
+ const verdict = caughtBy.length > 0 ? "caught" : changed ? "unguarded" : "no-effect";
49
+ return [{ ...target, verdict, caughtBy, effects, newStates: d.states.added, passes: ensureScope(spec, graph, target.action, target.reason).passes }];
50
+ });
51
+ };
@@ -0,0 +1,6 @@
1
+ import { type ExploreOptions } from "./explore.js";
2
+ import type { Spec } from "./load.js";
3
+ import type { Graph, OmissionHint } from "./model.js";
4
+ export type { OmissionHint } from "./model.js";
5
+ export type OmissionKind = OmissionHint["kind"];
6
+ export declare const findOmissionHints: (spec: Spec, graph: Graph, options?: Pick<ExploreOptions, "bypassEnsure">) => Array<OmissionHint>;