@yutaura/live-spec 0.2.0 → 0.3.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/.claude-plugin/plugin.json +1 -1
- package/README.md +3 -0
- package/dist/check.d.ts +5 -0
- package/dist/check.js +40 -0
- package/dist/cli.js +69 -2
- package/dist/evidence.d.ts +3 -0
- package/dist/evidence.js +46 -0
- package/dist/explore.js +4 -0
- package/dist/generate.d.ts +1 -0
- package/dist/generate.js +3 -2
- package/dist/load.d.ts +4 -0
- package/dist/load.js +4 -0
- package/dist/model.d.ts +45 -3
- package/dist/model.js +6 -0
- package/dist/mutation.js +2 -2
- package/dist/omission.js +58 -0
- package/dist/render/ensures.js +1 -1
- package/dist/render/markdown.js +6 -0
- package/dist/static.d.ts +2 -0
- package/dist/static.js +42 -2
- package/package.json +1 -1
- package/skills/live-spec-authoring/SKILL.md +28 -6
- package/skills/live-spec-from-code/SKILL.md +2 -0
package/README.md
CHANGED
|
@@ -129,6 +129,7 @@ npx live-spec specs/ --out docs/spec/ # ドメインオブジ
|
|
|
129
129
|
npx live-spec specs/ --serve # ブラウザで見ながら書く(保存すると自動で再読み込み)
|
|
130
130
|
npx live-spec specs/order.spec.ts # spec が 1 つなら Markdown を標準出力に出す
|
|
131
131
|
npx live-spec specs/ --json # 正規化モデル(差分レビュー用)
|
|
132
|
+
npx live-spec check specs/ # 型チェックと検査。問題があれば終了コード 1(CI 向け)
|
|
132
133
|
npx live-spec diff specs/ --base main --head feature # ブランチ間で、仕様の振る舞いの変化を出す(--head を省くと作業ツリー)
|
|
133
134
|
npx live-spec diff specs/ --base main --html --out diff.html # 重ね合わせた状態遷移図と、画面の変更前・変更後を並べた HTML
|
|
134
135
|
```
|
|
@@ -146,6 +147,8 @@ spec ファイルは素の TypeScript です。export の名前で役割が決
|
|
|
146
147
|
| 上記以外の関数 | | 操作。`(状態, 登場人物, ...入力) => 次の状態`。できないときは `ensure(条件, "理由")` で止める |
|
|
147
148
|
| `invariants` | | 不変条件。`{ 説明: (状態) => boolean }` |
|
|
148
149
|
| `inputs` | | 3 つ目以降の引数の例。`{ 操作名: { 例の名前: [引数...] } }` |
|
|
150
|
+
| `evidenceRoot` | | 根拠(`// 根拠: path:line`)のパスを解決する起点。spec のファイルからの相対。書かなければ git のルートなどから探す。根拠のファイルが無い・行が範囲の外なら検査結果に出る |
|
|
151
|
+
| `goals` | | 目指す状態。`{ 名前: (状態) => boolean }`。書くと、終端ではないのにどの目指す状態にもたどり着けない状態(袋小路)の入口を「確かめてほしい点」に出す |
|
|
149
152
|
| `reviewed` | | 確かめてほしい点のうち、意図どおりと判断したもの。`{ "候補の ID": "理由" }` |
|
|
150
153
|
| `scenarios` | | シナリオ。`{ 名前: [{ do: 操作の関数, by: "登場人物", input?: "入力の例の名前", expect?: { 項目: 値 }, denied?: "理由" }] }`。初期状態から順に実行し、止まったり期待と違ったりしたら検査結果に出る。`denied` を付けた手順は「断られるはず」で、実行できてしまうと失敗する |
|
|
151
154
|
| `questions` | | 未決事項。`[{ ask: "質問", about?: 操作の関数 \| { 項目: 値 }, note?: "補足" }]`。仕様書の上のほうと、図・操作カードに印が出る |
|
package/dist/check.d.ts
ADDED
package/dist/check.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
// spec はほかのファイルと別に、単独で型を確かめる(利用側の tsconfig.json の設定に左右されないため)。
|
|
6
|
+
// --allowImportingTsExtensions を付ける理由: spec は Node で直接実行するので、相対 import に .ts の拡張子を書くことがある。
|
|
7
|
+
const TSC_FLAGS = [
|
|
8
|
+
"--noEmit",
|
|
9
|
+
"--strict",
|
|
10
|
+
"--module",
|
|
11
|
+
"nodenext",
|
|
12
|
+
"--moduleResolution",
|
|
13
|
+
"nodenext",
|
|
14
|
+
"--target",
|
|
15
|
+
"es2024",
|
|
16
|
+
"--skipLibCheck",
|
|
17
|
+
"--allowImportingTsExtensions",
|
|
18
|
+
];
|
|
19
|
+
// live-spec に typescript を同梱しない理由: 利用側の typescript の版と型の解決(@types など)に合わせるため。
|
|
20
|
+
export const findTsc = (cwd) => {
|
|
21
|
+
try {
|
|
22
|
+
const pkgPath = createRequire(join(cwd, "package.json")).resolve("typescript/package.json");
|
|
23
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
24
|
+
const bin = typeof pkg.bin === "string" ? pkg.bin : pkg.bin?.tsc;
|
|
25
|
+
return bin === undefined ? null : join(dirname(pkgPath), bin);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export const typecheck = (tsc, files) => {
|
|
32
|
+
const run = (extra) => spawnSync(process.execPath, [tsc, ...TSC_FLAGS, ...extra, ...files], { encoding: "utf8" });
|
|
33
|
+
// --ignoreConfig は、ファイルを指定しても tsconfig.json を読もうとする TypeScript 6 以降で要る。
|
|
34
|
+
// 5 系はこのオプションを知らないので、知らないと言われたら外して実行し直す。
|
|
35
|
+
let result = run(["--ignoreConfig"]);
|
|
36
|
+
const text = `${result.stdout}${result.stderr}`;
|
|
37
|
+
if (/ignoreConfig/.test(text) && /unknown/i.test(text))
|
|
38
|
+
result = run([]);
|
|
39
|
+
return { ok: result.status === 0, output: `${result.stdout}${result.stderr}`.trim() };
|
|
40
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
import { realpathSync } from "node:fs";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
|
-
import {
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
import { findTsc, typecheck } from "./check.js";
|
|
7
|
+
import { expandSpecPaths, generateProject, loadObject, resolveLinks, specNameOf, writeOutput, writeProjectOutput } from "./generate.js";
|
|
8
|
+
import { describeFinding } from "./render/markdown.js";
|
|
9
|
+
import { reviewItems } from "./render/review.js";
|
|
6
10
|
import { diffProject } from "./project-diff.js";
|
|
7
11
|
import { renderDiffHtml } from "./render/diff-html.js";
|
|
8
12
|
import { renderDiffMarkdown } from "./render/diff-markdown.js";
|
|
@@ -103,6 +107,67 @@ const runDiff = async (argv, io) => {
|
|
|
103
107
|
await writeOutput(parsed.values.out, output);
|
|
104
108
|
return 0;
|
|
105
109
|
};
|
|
110
|
+
const CHECK_USAGE = "usage: live-spec check <file.spec.ts | dir>... [--cwd <dir>](typescript を探すディレクトリ。既定は今のディレクトリ)";
|
|
111
|
+
const indent = (text) => text
|
|
112
|
+
.split("\n")
|
|
113
|
+
.map((l) => ` ${l}`)
|
|
114
|
+
.join("\n");
|
|
115
|
+
// CI で使うために、問題が 1 つでもあれば 1 で終わる。未決事項(questions)は人の判断待ちで、正常な状態なので失敗にしない。
|
|
116
|
+
const runCheck = async (argv, io) => {
|
|
117
|
+
if (wantsHelp(argv)) {
|
|
118
|
+
io.stdout(`${CHECK_USAGE}\n`);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
let parsed;
|
|
122
|
+
try {
|
|
123
|
+
parsed = parseArgs({ args: [...argv], allowPositionals: true, options: { cwd: { type: "string" } } });
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
io.stderr(`${messageOf(e)}\n${CHECK_USAGE}\n`);
|
|
127
|
+
return 2;
|
|
128
|
+
}
|
|
129
|
+
const paths = expandSpecPaths(parsed.positionals);
|
|
130
|
+
if (paths.length === 0) {
|
|
131
|
+
io.stderr(`spec ファイル(*.spec.ts)を指定してください\n${CHECK_USAGE}\n`);
|
|
132
|
+
return 2;
|
|
133
|
+
}
|
|
134
|
+
let failed = false;
|
|
135
|
+
const tsc = findTsc(parsed.values.cwd ?? process.cwd());
|
|
136
|
+
if (tsc === null) {
|
|
137
|
+
io.stderr("typescript が見つからないので、型チェックを飛ばしました(--cwd で typescript を入れたディレクトリを指定できます)\n");
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const result = typecheck(tsc, paths.map((p) => resolve(p)));
|
|
141
|
+
failed ||= !result.ok;
|
|
142
|
+
io.stdout(result.ok ? "✓ 型チェック\n" : `✗ 型チェック\n${indent(result.output)}\n`);
|
|
143
|
+
}
|
|
144
|
+
const loaded = [];
|
|
145
|
+
for (const path of paths) {
|
|
146
|
+
try {
|
|
147
|
+
loaded.push(await loadObject(path));
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
failed = true;
|
|
151
|
+
io.stdout(`✗ ${specNameOf(path)}: 読み込めない: ${messageOf(e)}\n`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const { spec, graph } of resolveLinks(loaded)) {
|
|
155
|
+
const labels = new Map(graph.states.map((s) => [s.id, s.label]));
|
|
156
|
+
const labelOf = (id) => labels.get(id) ?? id;
|
|
157
|
+
const pending = reviewItems(spec, graph).pending;
|
|
158
|
+
if (graph.findings.length + pending.length === 0) {
|
|
159
|
+
io.stdout(`✓ ${spec.name}\n`);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
failed = true;
|
|
163
|
+
io.stdout([
|
|
164
|
+
`✗ ${spec.name}: 検査結果 ${graph.findings.length} 件、確かめてほしい点 ${pending.length} 件`,
|
|
165
|
+
...graph.findings.map((f) => ` - ${describeFinding(f, labelOf)}`),
|
|
166
|
+
...pending.map((i) => ` - ${i.message}(${i.id})`),
|
|
167
|
+
].join("\n") + "\n");
|
|
168
|
+
}
|
|
169
|
+
return failed ? 1 : 0;
|
|
170
|
+
};
|
|
106
171
|
const SKILLS_USAGE = "usage: live-spec skills install [--dir <path>](既定は .claude/skills) [--force]";
|
|
107
172
|
const runSkills = (argv, io) => {
|
|
108
173
|
if (wantsHelp(argv)) {
|
|
@@ -137,8 +202,10 @@ export const runCli = async (argv, io) => {
|
|
|
137
202
|
return runDiff(argv.slice(1), io);
|
|
138
203
|
if (argv[0] === "skills")
|
|
139
204
|
return runSkills(argv.slice(1), io);
|
|
205
|
+
if (argv[0] === "check")
|
|
206
|
+
return runCheck(argv.slice(1), io);
|
|
140
207
|
if (wantsHelp(argv)) {
|
|
141
|
-
io.stdout(`${[USAGE, DIFF_USAGE, SKILLS_USAGE].join("\n")}\n`);
|
|
208
|
+
io.stdout(`${[USAGE, DIFF_USAGE, CHECK_USAGE, SKILLS_USAGE].join("\n")}\n`);
|
|
142
209
|
return 0;
|
|
143
210
|
}
|
|
144
211
|
let options;
|
package/dist/evidence.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, parse, resolve } from "node:path";
|
|
4
|
+
import { splitEvidence } from "./render/markdown.js";
|
|
5
|
+
const LOCATION = /^(.+?)(?::(\d+)(?:-(\d+))?)?$/;
|
|
6
|
+
const gitRoot = (dir) => {
|
|
7
|
+
try {
|
|
8
|
+
return execFileSync("git", ["-C", dir, "rev-parse", "--show-toplevel"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
// 起点の候補を順に試す理由: 既存のコードから書き起こした spec は、根拠のコードとは別の場所(別のリポジトリなど)に
|
|
15
|
+
// 置かれることがある。evidenceRoot を書けばそこを、書かなければ spec のある git のルート、spec の上のディレクトリを探す。
|
|
16
|
+
const rootsFor = (spec, specPath) => {
|
|
17
|
+
const specDir = dirname(resolve(specPath));
|
|
18
|
+
const ancestors = [];
|
|
19
|
+
for (let dir = specDir;; dir = dirname(dir)) {
|
|
20
|
+
ancestors.push(dir);
|
|
21
|
+
if (dir === parse(dir).root)
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (spec.evidenceRoot !== null)
|
|
25
|
+
return [resolve(specDir, spec.evidenceRoot)];
|
|
26
|
+
const git = gitRoot(specDir);
|
|
27
|
+
return git === null ? ancestors : [git, ...ancestors];
|
|
28
|
+
};
|
|
29
|
+
const lineCount = (path) => readFileSync(path, "utf8").replace(/\n$/, "").split("\n").length;
|
|
30
|
+
export const checkEvidence = (spec, specPath) => {
|
|
31
|
+
const roots = rootsFor(spec, specPath);
|
|
32
|
+
return Object.entries(spec.evidence).flatMap(([action, items]) => items.flatMap((evidence) => {
|
|
33
|
+
const m = LOCATION.exec(splitEvidence(evidence).path);
|
|
34
|
+
if (m === null)
|
|
35
|
+
return [];
|
|
36
|
+
const [, path, start, end] = m;
|
|
37
|
+
const file = roots.map((root) => join(root, path)).find((p) => existsSync(p));
|
|
38
|
+
if (file === undefined)
|
|
39
|
+
return [{ kind: "evidence-missing", action, evidence }];
|
|
40
|
+
const last = Number(end ?? start ?? 0);
|
|
41
|
+
if (last === 0)
|
|
42
|
+
return [];
|
|
43
|
+
const lines = lineCount(file);
|
|
44
|
+
return last > lines ? [{ kind: "evidence-out-of-range", action, evidence, lines }] : [];
|
|
45
|
+
}));
|
|
46
|
+
};
|
package/dist/explore.js
CHANGED
|
@@ -122,6 +122,10 @@ export const explore = (spec, options = {}) => {
|
|
|
122
122
|
findings.push({ kind: "unreachable-value", field, value });
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
|
+
for (const [goal, holds] of spec.goals) {
|
|
126
|
+
if (!states.some((s) => invariantHolds(holds, s.values)))
|
|
127
|
+
findings.push({ kind: "goal-unreachable", goal });
|
|
128
|
+
}
|
|
125
129
|
for (const action of missingInputs)
|
|
126
130
|
findings.push({ kind: "missing-inputs", action });
|
|
127
131
|
for (const action of Object.keys(spec.inputs)) {
|
package/dist/generate.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface LoadObjectOptions {
|
|
|
15
15
|
}
|
|
16
16
|
export declare const loadObject: (path: string, options?: LoadObjectOptions) => Promise<DomainObject>;
|
|
17
17
|
export declare const generateProject: (paths: ReadonlyArray<string>, format: OutputFormat, title?: string) => Promise<Output>;
|
|
18
|
+
export declare const resolveLinks: (objects: ReadonlyArray<DomainObject>) => Array<DomainObject>;
|
|
18
19
|
export declare const failureOutput: (format: OutputFormat, paths: ReadonlyArray<string>, error: unknown) => Output;
|
|
19
20
|
export declare const generate: (path: string, format: OutputFormat) => Promise<string>;
|
|
20
21
|
export declare const writeOutput: (out: string, content: string) => Promise<void>;
|
package/dist/generate.js
CHANGED
|
@@ -11,6 +11,7 @@ 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 { checkEvidence } from "./evidence.js";
|
|
14
15
|
import { checkEnsures } from "./mutation.js";
|
|
15
16
|
import { findOmissionHints } from "./omission.js";
|
|
16
17
|
import { unguardedId } from "./render/review.js";
|
|
@@ -54,7 +55,7 @@ export const loadObject = async (path, options = {}) => {
|
|
|
54
55
|
const unmatched = Object.keys(spec.reviewed)
|
|
55
56
|
.filter((id) => !known.has(id))
|
|
56
57
|
.map((id) => ({ kind: "reviewed-unmatched", id }));
|
|
57
|
-
return { spec, graph: { ...graph, ensures, omissions, findings: [...graph.findings, ...unmatched] } };
|
|
58
|
+
return { spec, graph: { ...graph, ensures, omissions, findings: [...graph.findings, ...checkEvidence(spec, path), ...unmatched] } };
|
|
58
59
|
};
|
|
59
60
|
export const generateProject = async (paths, format, title) => {
|
|
60
61
|
const objects = resolveLinks(await Promise.all(paths.map((p) => loadObject(p))));
|
|
@@ -81,7 +82,7 @@ export const generateProject = async (paths, format, title) => {
|
|
|
81
82
|
};
|
|
82
83
|
// つながりの名前の誤りをプロジェクトを読み込んだ時点で調べる理由: 相手のドメインオブジェクトは別の spec にあり、
|
|
83
84
|
// 1 つの spec を探索するだけでは確かめられないため。spec が 1 つのときは相手がいないので調べない。
|
|
84
|
-
const resolveLinks = (objects) => objects.map((o) => {
|
|
85
|
+
export const resolveLinks = (objects) => objects.map((o) => {
|
|
85
86
|
if (objects.length < 2)
|
|
86
87
|
return o;
|
|
87
88
|
const unresolved = o.graph.links.filter((l) => {
|
package/dist/load.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export interface Spec {
|
|
|
35
35
|
readonly actors: ReadonlyArray<readonly [name: string, actor: unknown]>;
|
|
36
36
|
readonly actions: ReadonlyArray<readonly [name: string, fn: ActionFn]>;
|
|
37
37
|
readonly invariants: ReadonlyArray<readonly [name: string, fn: InvariantFn]>;
|
|
38
|
+
/** 目指す状態。たどり着けない状態(袋小路)を見つけるのに使う */
|
|
39
|
+
readonly goals: ReadonlyArray<readonly [name: string, fn: InvariantFn]>;
|
|
38
40
|
readonly inputs: Readonly<Record<string, ReadonlyArray<readonly [label: string, args: ReadonlyArray<unknown>]>>>;
|
|
39
41
|
readonly surface: SurfaceFn | null;
|
|
40
42
|
readonly scenarios: ReadonlyArray<readonly [name: string, steps: ReadonlyArray<ScenarioStep>]>;
|
|
@@ -44,6 +46,8 @@ export interface Spec {
|
|
|
44
46
|
readonly docs: Readonly<Record<string, string>>;
|
|
45
47
|
readonly fieldDocs: Readonly<Record<string, string>>;
|
|
46
48
|
readonly evidence: Readonly<Record<string, ReadonlyArray<string>>>;
|
|
49
|
+
/** 根拠のパスを解決する起点(spec のファイルからの相対)。無ければ git のルートなどから探す */
|
|
50
|
+
readonly evidenceRoot: string | null;
|
|
47
51
|
/** 操作ごとの ensure(静的解析で読んだもの。ソースがなければ空) */
|
|
48
52
|
readonly ensures: Readonly<Record<string, ReadonlyArray<EnsureCall>>>;
|
|
49
53
|
readonly glossary: Readonly<Record<string, string>>;
|
package/dist/load.js
CHANGED
|
@@ -11,6 +11,8 @@ const RESERVED = new Set([
|
|
|
11
11
|
"glossary",
|
|
12
12
|
"decisions",
|
|
13
13
|
"reviewed",
|
|
14
|
+
"goals",
|
|
15
|
+
"evidenceRoot",
|
|
14
16
|
]);
|
|
15
17
|
const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
16
18
|
export const loadSpec = (name, mod, options = {}) => {
|
|
@@ -28,6 +30,7 @@ export const loadSpec = (name, mod, options = {}) => {
|
|
|
28
30
|
invariants: isRecord(mod.invariants)
|
|
29
31
|
? Object.entries(mod.invariants).filter((e) => typeof e[1] === "function")
|
|
30
32
|
: [],
|
|
33
|
+
goals: isRecord(mod.goals) ? Object.entries(mod.goals).filter((e) => typeof e[1] === "function") : [],
|
|
31
34
|
inputs: inputsOf(mod.inputs),
|
|
32
35
|
surface: typeof mod.surface === "function" ? mod.surface : null,
|
|
33
36
|
questions: Array.isArray(mod.questions)
|
|
@@ -45,6 +48,7 @@ export const loadSpec = (name, mod, options = {}) => {
|
|
|
45
48
|
docs: info?.docs ?? {},
|
|
46
49
|
fieldDocs: info?.fieldDocs ?? {},
|
|
47
50
|
evidence: info?.evidence ?? {},
|
|
51
|
+
evidenceRoot: typeof mod.evidenceRoot === "string" ? mod.evidenceRoot : null,
|
|
48
52
|
ensures: info?.ensures ?? {},
|
|
49
53
|
reviewed: isRecord(mod.reviewed)
|
|
50
54
|
? Object.fromEntries(Object.entries(mod.reviewed).filter((e) => typeof e[1] === "string"))
|
package/dist/model.d.ts
CHANGED
|
@@ -90,6 +90,18 @@ export declare const Finding: Schema.Union<readonly [Schema.Struct<{
|
|
|
90
90
|
}>, Schema.Struct<{
|
|
91
91
|
readonly kind: Schema.Literal<"reviewed-unmatched">;
|
|
92
92
|
readonly id: Schema.String;
|
|
93
|
+
}>, Schema.Struct<{
|
|
94
|
+
readonly kind: Schema.Literal<"goal-unreachable">;
|
|
95
|
+
readonly goal: Schema.String;
|
|
96
|
+
}>, Schema.Struct<{
|
|
97
|
+
readonly kind: Schema.Literal<"evidence-missing">;
|
|
98
|
+
readonly action: Schema.String;
|
|
99
|
+
readonly evidence: Schema.String;
|
|
100
|
+
}>, Schema.Struct<{
|
|
101
|
+
readonly kind: Schema.Literal<"evidence-out-of-range">;
|
|
102
|
+
readonly action: Schema.String;
|
|
103
|
+
readonly evidence: Schema.String;
|
|
104
|
+
readonly lines: Schema.Number;
|
|
93
105
|
}>, Schema.Struct<{
|
|
94
106
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
95
107
|
readonly invariant: Schema.String;
|
|
@@ -116,11 +128,13 @@ export declare const EnsureCheck: Schema.Struct<{
|
|
|
116
128
|
readonly newStates: Schema.$Array<Schema.String>;
|
|
117
129
|
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
118
130
|
readonly passes: Schema.String;
|
|
131
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
132
|
+
readonly narrowing: Schema.Boolean;
|
|
119
133
|
}>;
|
|
120
134
|
export type EnsureCheck = typeof EnsureCheck.Type;
|
|
121
135
|
export declare const OmissionHint: Schema.Struct<{
|
|
122
136
|
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"]>;
|
|
137
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
124
138
|
readonly action: Schema.NullOr<Schema.String>;
|
|
125
139
|
readonly field: Schema.NullOr<Schema.String>;
|
|
126
140
|
readonly message: Schema.String;
|
|
@@ -217,6 +231,18 @@ export declare const Graph: Schema.Struct<{
|
|
|
217
231
|
}>, Schema.Struct<{
|
|
218
232
|
readonly kind: Schema.Literal<"reviewed-unmatched">;
|
|
219
233
|
readonly id: Schema.String;
|
|
234
|
+
}>, Schema.Struct<{
|
|
235
|
+
readonly kind: Schema.Literal<"goal-unreachable">;
|
|
236
|
+
readonly goal: Schema.String;
|
|
237
|
+
}>, Schema.Struct<{
|
|
238
|
+
readonly kind: Schema.Literal<"evidence-missing">;
|
|
239
|
+
readonly action: Schema.String;
|
|
240
|
+
readonly evidence: Schema.String;
|
|
241
|
+
}>, Schema.Struct<{
|
|
242
|
+
readonly kind: Schema.Literal<"evidence-out-of-range">;
|
|
243
|
+
readonly action: Schema.String;
|
|
244
|
+
readonly evidence: Schema.String;
|
|
245
|
+
readonly lines: Schema.Number;
|
|
220
246
|
}>, Schema.Struct<{
|
|
221
247
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
222
248
|
readonly invariant: Schema.String;
|
|
@@ -269,10 +295,12 @@ export declare const Graph: Schema.Struct<{
|
|
|
269
295
|
readonly newStates: Schema.$Array<Schema.String>;
|
|
270
296
|
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
271
297
|
readonly passes: Schema.String;
|
|
298
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
299
|
+
readonly narrowing: Schema.Boolean;
|
|
272
300
|
}>>>;
|
|
273
301
|
readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
274
302
|
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"]>;
|
|
303
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
276
304
|
readonly action: Schema.NullOr<Schema.String>;
|
|
277
305
|
readonly field: Schema.NullOr<Schema.String>;
|
|
278
306
|
readonly message: Schema.String;
|
|
@@ -385,6 +413,18 @@ export declare const SpecModel: Schema.Struct<{
|
|
|
385
413
|
}>, Schema.Struct<{
|
|
386
414
|
readonly kind: Schema.Literal<"reviewed-unmatched">;
|
|
387
415
|
readonly id: Schema.String;
|
|
416
|
+
}>, Schema.Struct<{
|
|
417
|
+
readonly kind: Schema.Literal<"goal-unreachable">;
|
|
418
|
+
readonly goal: Schema.String;
|
|
419
|
+
}>, Schema.Struct<{
|
|
420
|
+
readonly kind: Schema.Literal<"evidence-missing">;
|
|
421
|
+
readonly action: Schema.String;
|
|
422
|
+
readonly evidence: Schema.String;
|
|
423
|
+
}>, Schema.Struct<{
|
|
424
|
+
readonly kind: Schema.Literal<"evidence-out-of-range">;
|
|
425
|
+
readonly action: Schema.String;
|
|
426
|
+
readonly evidence: Schema.String;
|
|
427
|
+
readonly lines: Schema.Number;
|
|
388
428
|
}>, Schema.Struct<{
|
|
389
429
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
390
430
|
readonly invariant: Schema.String;
|
|
@@ -437,10 +477,12 @@ export declare const SpecModel: Schema.Struct<{
|
|
|
437
477
|
readonly newStates: Schema.$Array<Schema.String>;
|
|
438
478
|
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
439
479
|
readonly passes: Schema.String;
|
|
480
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
481
|
+
readonly narrowing: Schema.Boolean;
|
|
440
482
|
}>>>;
|
|
441
483
|
readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
442
484
|
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"]>;
|
|
485
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
444
486
|
readonly action: Schema.NullOr<Schema.String>;
|
|
445
487
|
readonly field: Schema.NullOr<Schema.String>;
|
|
446
488
|
readonly message: Schema.String;
|
package/dist/model.js
CHANGED
|
@@ -56,6 +56,9 @@ export const Finding = Schema.Union([
|
|
|
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
58
|
Schema.Struct({ kind: Schema.Literal("reviewed-unmatched"), id: Schema.String }),
|
|
59
|
+
Schema.Struct({ kind: Schema.Literal("goal-unreachable"), goal: Schema.String }),
|
|
60
|
+
Schema.Struct({ kind: Schema.Literal("evidence-missing"), action: Schema.String, evidence: Schema.String }),
|
|
61
|
+
Schema.Struct({ kind: Schema.Literal("evidence-out-of-range"), action: Schema.String, evidence: Schema.String, lines: Schema.Number }),
|
|
59
62
|
Schema.Struct({
|
|
60
63
|
kind: Schema.Literal("invariant-violation"),
|
|
61
64
|
invariant: Schema.String,
|
|
@@ -76,6 +79,8 @@ export const EnsureCheck = Schema.Struct({
|
|
|
76
79
|
newStates: Schema.Array(Schema.String),
|
|
77
80
|
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
78
81
|
passes: Schema.String,
|
|
82
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
83
|
+
narrowing: Schema.Boolean,
|
|
79
84
|
});
|
|
80
85
|
// spec を実行した結果から見つけた、書き忘れていそうな条件の候補。id は reviewed で「意図どおり」と答えるときのキー。
|
|
81
86
|
export const OmissionHint = Schema.Struct({
|
|
@@ -88,6 +93,7 @@ export const OmissionHint = Schema.Struct({
|
|
|
88
93
|
"indistinguishable-example",
|
|
89
94
|
"indistinguishable-actor",
|
|
90
95
|
"unused-field",
|
|
96
|
+
"cannot-reach-goal",
|
|
91
97
|
]),
|
|
92
98
|
action: Schema.NullOr(Schema.String),
|
|
93
99
|
field: Schema.NullOr(Schema.String),
|
package/dist/mutation.js
CHANGED
|
@@ -25,9 +25,9 @@ const caughtLabel = (f) => {
|
|
|
25
25
|
const targetsOf = (spec, graph) => spec.actions.flatMap(([action]) => {
|
|
26
26
|
const found = spec.ensures[action];
|
|
27
27
|
if (found !== undefined)
|
|
28
|
-
return found.map((e) => ({ action, reason: e.reason, line: e.line }));
|
|
28
|
+
return found.map((e) => ({ action, reason: e.reason, line: e.line, narrowing: e.narrowing === true }));
|
|
29
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 }));
|
|
30
|
+
return reasons.map((reason) => ({ action, reason, line: null, narrowing: false }));
|
|
31
31
|
});
|
|
32
32
|
export const checkEnsures = (spec, graph) => {
|
|
33
33
|
const original = normalize(spec, graph);
|
package/dist/omission.js
CHANGED
|
@@ -127,8 +127,66 @@ export const findOmissionHints = (spec, graph, options = {}) => {
|
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
129
|
hints.push(...unusedFields(spec, graph, options));
|
|
130
|
+
hints.push(...deadEnds(spec, graph));
|
|
130
131
|
return hints;
|
|
131
132
|
};
|
|
133
|
+
const holds = (fn, values) => {
|
|
134
|
+
try {
|
|
135
|
+
return Boolean(fn(structuredClone(values)));
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
// 袋小路の中の状態をすべて出さずに入口だけを出す理由: 1 つの原因で入った袋小路の中には状態がいくつもあり、
|
|
142
|
+
// 全部を並べると同じ指摘が何件も出る。入口(たどり着ける状態から 1 手で入る状態)を直せば中も解消する。
|
|
143
|
+
const deadEnds = (spec, graph) => {
|
|
144
|
+
if (spec.goals.length === 0)
|
|
145
|
+
return [];
|
|
146
|
+
const goals = new Set(graph.states.filter((s) => spec.goals.some(([, fn]) => holds(fn, s.values))).map((s) => s.id));
|
|
147
|
+
if (goals.size === 0)
|
|
148
|
+
return [];
|
|
149
|
+
const moves = graph.transitions.filter((t) => t.from !== t.to);
|
|
150
|
+
const live = new Set(goals);
|
|
151
|
+
for (let changed = true; changed;) {
|
|
152
|
+
changed = false;
|
|
153
|
+
for (const t of moves) {
|
|
154
|
+
if (live.has(t.to) && !live.has(t.from)) {
|
|
155
|
+
live.add(t.from);
|
|
156
|
+
changed = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const parent = new Map();
|
|
161
|
+
const queue = graph.states.filter((s) => s.initial).map((s) => s.id);
|
|
162
|
+
const seen = new Set(queue);
|
|
163
|
+
for (let i = 0; i < queue.length; i++) {
|
|
164
|
+
for (const t of moves.filter((m) => m.from === queue[i])) {
|
|
165
|
+
if (seen.has(t.to))
|
|
166
|
+
continue;
|
|
167
|
+
seen.add(t.to);
|
|
168
|
+
parent.set(t.to, { from: t.from, action: t.action, actor: t.actors[0] ?? "" });
|
|
169
|
+
queue.push(t.to);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const pathTo = (id) => {
|
|
173
|
+
const steps = [];
|
|
174
|
+
for (let p = parent.get(id); p !== undefined; p = parent.get(p.from))
|
|
175
|
+
steps.unshift(`${p.action}(${p.actor})`);
|
|
176
|
+
return steps.join(" → ");
|
|
177
|
+
};
|
|
178
|
+
const goalNames = spec.goals.map(([name]) => name).join("、");
|
|
179
|
+
return graph.states
|
|
180
|
+
.filter((s) => !s.terminal && !live.has(s.id))
|
|
181
|
+
.filter((s) => s.initial || moves.some((t) => t.to === s.id && live.has(t.from)))
|
|
182
|
+
.map((s) => ({
|
|
183
|
+
id: `cannot-reach-goal:${s.label}`,
|
|
184
|
+
kind: "cannot-reach-goal",
|
|
185
|
+
action: null,
|
|
186
|
+
field: null,
|
|
187
|
+
message: `状態「${s.label}」は終端ではないのに、目指す状態(${goalNames})のどれにもたどり着けない。入る手順: ${s.initial ? "(初期状態)" : pathTo(s.id)}`,
|
|
188
|
+
}));
|
|
189
|
+
};
|
|
132
190
|
const MAX_PERTURBED_STATES = 50;
|
|
133
191
|
// 項目の値を 1 つだけ差し替えた状態で操作を実行し直し、可否が変わるかを見る(Leveson の入力の完全性)。
|
|
134
192
|
// 差し替えた状態は到達できないこともあるが、ここで知りたいのは「条件がその項目を見ているか」なので構わない。
|
package/dist/render/ensures.js
CHANGED
|
@@ -13,7 +13,7 @@ const effectOf = (e, acknowledged) => {
|
|
|
13
13
|
case "unguarded":
|
|
14
14
|
return `外すと、${effectText(e)}。気づくものがない${acknowledged ? "(確認済み)" : ""}`;
|
|
15
15
|
case "no-effect":
|
|
16
|
-
return "外しても振る舞いが変わらない(ほかの条件と重なっている)";
|
|
16
|
+
return e.narrowing ? "外しても振る舞いが変わらない(後の条件のために型を絞る条件)" : "外しても振る舞いが変わらない(ほかの条件と重なっている)";
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
19
|
// 理由の文と「実際に通るとき」を並べる理由: 理由の文は書き手の意図をもう一度書いたものなので、
|
package/dist/render/markdown.js
CHANGED
|
@@ -224,6 +224,12 @@ export const describeFinding = (f, labelOf) => {
|
|
|
224
224
|
return `操作 ${f.action} は 3 つ目以降の引数をとるが、inputs に例がないので実行していない`;
|
|
225
225
|
case "unknown-inputs":
|
|
226
226
|
return `inputs に書かれた ${f.action} という操作は存在しない`;
|
|
227
|
+
case "evidence-missing":
|
|
228
|
+
return `${f.action} の根拠「${f.evidence}」のファイルが見つからない(移動・改名されたか、起点が違う。別の場所のコードなら export const evidenceRoot を書く)`;
|
|
229
|
+
case "evidence-out-of-range":
|
|
230
|
+
return `${f.action} の根拠「${f.evidence}」の行が、ファイルの行数(${f.lines} 行)を超えている(コードが変わって行がずれた)`;
|
|
231
|
+
case "goal-unreachable":
|
|
232
|
+
return `目指す状態「${f.goal}」には、どの状態からもたどり着けない`;
|
|
227
233
|
case "reviewed-unmatched":
|
|
228
234
|
return `reviewed に書かれた ${f.id} は、確かめてほしい点のどれにも当たらない(ID の書き間違いか、もう出なくなった候補)`;
|
|
229
235
|
case "surface-action-not-allowed":
|
package/dist/static.d.ts
CHANGED
|
@@ -11,5 +11,7 @@ export interface StaticInfo {
|
|
|
11
11
|
export interface EnsureCall {
|
|
12
12
|
readonly reason: string;
|
|
13
13
|
readonly line: number;
|
|
14
|
+
/** 後ろの条件のために型を絞る ensure(判別の項目を比べ、後ろで同じ値の別の項目を読む) */
|
|
15
|
+
readonly narrowing?: true;
|
|
14
16
|
}
|
|
15
17
|
export declare const analyze: (source: string) => StaticInfo;
|
package/dist/static.js
CHANGED
|
@@ -75,8 +75,9 @@ const ensureCallsIn = (root) => {
|
|
|
75
75
|
: arg?.type === "TemplateLiteral" && arg.expressions.length === 0
|
|
76
76
|
? arg.quasis.map((q) => q.value.cooked ?? "").join("")
|
|
77
77
|
: null;
|
|
78
|
-
if (reason !== null && n.loc != null)
|
|
79
|
-
calls.push({ reason, line: n.loc.start.line });
|
|
78
|
+
if (reason !== null && n.loc != null) {
|
|
79
|
+
calls.push({ reason, line: n.loc.start.line, ...(narrowsType(root, n) ? { narrowing: true } : {}) });
|
|
80
|
+
}
|
|
80
81
|
}
|
|
81
82
|
for (const [key, value] of Object.entries(n)) {
|
|
82
83
|
if (key === "loc" || key.endsWith("Comments"))
|
|
@@ -87,6 +88,45 @@ const ensureCallsIn = (root) => {
|
|
|
87
88
|
visit(root);
|
|
88
89
|
return calls;
|
|
89
90
|
};
|
|
91
|
+
// 判別 union の登場人物({ kind: "human"; via: … } | { kind: "worker" })では、via を読むために先に kind で型を絞る。
|
|
92
|
+
// この ensure は外しても振る舞いが変わらないことが多いが、消すと型が通らないので「重なり」とは別に扱う。
|
|
93
|
+
// 型チェッカーを使わず構文で見分けるので、比べた値と同じ名前の値の、別の項目を後ろで読んでいるかだけを見る。
|
|
94
|
+
const narrowsType = (root, call) => {
|
|
95
|
+
const cond = call.arguments[0];
|
|
96
|
+
if (cond?.type !== "BinaryExpression" || (cond.operator !== "===" && cond.operator !== "!=="))
|
|
97
|
+
return false;
|
|
98
|
+
const member = cond.right.type === "StringLiteral" ? cond.left : cond.left.type === "StringLiteral" ? cond.right : null;
|
|
99
|
+
if (member?.type !== "MemberExpression" || member.object.type !== "Identifier" || member.property.type !== "Identifier")
|
|
100
|
+
return false;
|
|
101
|
+
const object = member.object.name;
|
|
102
|
+
const discriminant = member.property.name;
|
|
103
|
+
let found = false;
|
|
104
|
+
const visit = (node) => {
|
|
105
|
+
if (found)
|
|
106
|
+
return;
|
|
107
|
+
if (Array.isArray(node))
|
|
108
|
+
return node.forEach(visit);
|
|
109
|
+
if (typeof node !== "object" || node === null || !("type" in node))
|
|
110
|
+
return;
|
|
111
|
+
const n = node;
|
|
112
|
+
if (n.type === "MemberExpression" &&
|
|
113
|
+
n.start > call.end &&
|
|
114
|
+
n.object.type === "Identifier" &&
|
|
115
|
+
n.object.name === object &&
|
|
116
|
+
n.property.type === "Identifier" &&
|
|
117
|
+
n.property.name !== discriminant) {
|
|
118
|
+
found = true;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
for (const [key, value] of Object.entries(n)) {
|
|
122
|
+
if (key === "loc" || key.endsWith("Comments"))
|
|
123
|
+
continue;
|
|
124
|
+
visit(value);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
visit(root);
|
|
128
|
+
return found;
|
|
129
|
+
};
|
|
90
130
|
const valueNamesOf = (decl) => {
|
|
91
131
|
if (decl.type === "FunctionDeclaration" && decl.id != null)
|
|
92
132
|
return [[decl.id.name, null]];
|
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ live-spec は npm パッケージ `@yutaura/live-spec`(`npm i -D @yutaura/live
|
|
|
17
17
|
| HTML を書き出す | `npx live-spec specs/ --html --out spec.html` |
|
|
18
18
|
| ブラウザで見ながら書く | `npx live-spec specs/ --serve` |
|
|
19
19
|
| 変更前と比べる | `npx live-spec diff specs/ [--base <リビジョン>] [--head <リビジョン>] [--html --out <file>]` |
|
|
20
|
+
| 型チェックと検査(CI 向け) | `npx live-spec check specs/`(型の誤り・検査結果・未確認の確かめてほしい点があれば終了コード 1) |
|
|
20
21
|
|
|
21
22
|
(live-spec リポジトリ自身の中では `pnpm spec ...`)
|
|
22
23
|
|
|
@@ -27,11 +28,7 @@ live-spec は npm パッケージ `@yutaura/live-spec`(`npm i -D @yutaura/live
|
|
|
27
28
|
|
|
28
29
|
spec からは `import { ensure, ui, api, cli } from "@yutaura/live-spec"` で読み込む(live-spec リポジトリ自身の `specs/` と `examples/` だけは `../src/index.ts` から読み込む)。
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
```sh
|
|
33
|
-
npx tsc --noEmit --ignoreConfig --strict --module nodenext --moduleResolution nodenext --target es2024 --skipLibCheck <file.spec.ts>
|
|
34
|
-
```
|
|
31
|
+
仕様書の生成(`live-spec specs/`)は型を消して実行するだけなので、型の誤りでは止まらない。書き終えたら `npx live-spec check specs/` で型チェックと検査をまとめて行う。型チェックには、実行したディレクトリから見える typescript を使う(monorepo で typescript が別の package にあるなら `--cwd <その package>`)。
|
|
35
32
|
|
|
36
33
|
## spec の形
|
|
37
34
|
|
|
@@ -116,6 +113,30 @@ export const actors = {
|
|
|
116
113
|
|
|
117
114
|
送料・割引・料金区分のように「入力の組み合わせ → 結果」で決まるルールは、状態遷移ではなく `decisions` に書く。判断の関数(`decide`)と入力の例(`cases`)を書くと、live-spec が実行して表にする。境界の値(4999 円と 5000 円など)を例に入れ、`expect` を付けておくと、期待と違うときに検査結果に出る。
|
|
118
115
|
|
|
116
|
+
### 6.5 上限つきの回数は、振る舞いに効く区分だけを持つ
|
|
117
|
+
|
|
118
|
+
再試行の上限(既定 3 回)のような回数を `attempt: 0 | 1 | 2 | 3` のまま状態に入れると、ほかの項目と掛け合わさって状態が増え、図が読めなくなる。振る舞いに効くのが「まだ残っている/いまが最後/使い切った」だけなら、その区分だけを状態に持ち、「次が最後になるか」は入力で与える。
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
export type Run = {
|
|
122
|
+
status: "queued" | "running" | "failure";
|
|
123
|
+
/** 試行の残り。回数そのものではなく、振る舞いに効く区分だけを持つ */
|
|
124
|
+
retries: "残りあり" | "最後" | "使い切った";
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export const inputs = { reserve: { まだ残る: [false], 次が最後: [true] } };
|
|
128
|
+
|
|
129
|
+
/** 試行を 1 回分予約する */
|
|
130
|
+
export function reserve(run: Run, worker: Worker, nextIsLast: boolean): Run {
|
|
131
|
+
ensure(run.retries !== "使い切った", "試行が残っているときだけ");
|
|
132
|
+
return { ...run, retries: run.retries === "最後" ? "使い切った" : nextIsLast ? "最後" : "残りあり" };
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
この形では「まだ残る」を何度でも選べるので、spec は実際より多くの道筋を探索する(4 回目も「まだ残る」を選べる)。実際にありうる道筋は取りこぼさない、安全側の近似なので、袋小路や不変条件の違反を見落とすことはない。ただ、実際には起きない道筋も図に出る。上限に届くまでの流れを見せたいときは、シナリオに入力を明示して書く(「まだ残る」→「次が最後」→ 失敗)。
|
|
137
|
+
|
|
138
|
+
回数そのものが振る舞いを変える(3 回目だけ人に知らせる、など)なら、回数を状態に入れる。そのときは、ほかの関心を別のドメインオブジェクトに分けて、状態の掛け合わせを減らす。
|
|
139
|
+
|
|
119
140
|
### 7. シナリオで業務の流れを書く
|
|
120
141
|
|
|
121
142
|
状態の一覧だけでは業務の流れが読めない。代表的な流れ(正常系 1〜2 本と、差し戻し・やり直しのような分岐 1〜2 本)を `scenarios` に書く。各段階には操作の**関数**を渡し、要所に `expect` を書く。シナリオは初期状態から実行され、止まったり期待と違ったりすると検査結果に出るので、受け入れテストを兼ねる。シナリオ名は業務の言葉で(「書いて公開するまで」)。
|
|
@@ -164,6 +185,7 @@ live-spec は spec を実行した結果から、条件の書き忘れ・書き
|
|
|
164
185
|
| 入力を見ていない(`ignores-input`) | 入力の検証の書き忘れ |
|
|
165
186
|
| 本人と別人を区別しない(`same-for-similar-actors`) | 「自分の〜だけ」の書き忘れ |
|
|
166
187
|
| 結果がまったく同じ入力の例・登場人物(`indistinguishable-*`) | 例や人を分けるはずの条件の書き忘れ。または、違いを状態として表していない |
|
|
188
|
+
| 目指す状態にたどり着けない(`cannot-reach-goal`) | 終端ではないのに抜け出せない袋小路。`export const goals = { 成功: (s) => s.status === "success" }` を書いたときだけ出る。書き起こしの spec では、実装が気づいていない袋小路であることが多い |
|
|
167
189
|
| どの可否にも影響しない項目(`unused-field`) | その項目を使う条件の書き忘れ。または、振る舞いに関係しない値を状態に入れている |
|
|
168
190
|
| 意図がほかに書かれていない条件(`unguarded`) | 外すと振る舞いが変わるのに、不変条件・シナリオ・画面のボタンのどれも気づかない条件 |
|
|
169
191
|
|
|
@@ -178,7 +200,7 @@ live-spec は spec を実行した結果から、条件の書き忘れ・書き
|
|
|
178
200
|
|
|
179
201
|
操作の節の「条件」には、`ensure` ごとに「理由の文 — 通るのは〜のとき」が並ぶ。「通るのは」は、その `ensure` だけを効かせて実行して求めた事実。理由の文と食い違っていれば、条件か理由のどちらかが間違っている(「実行中のときだけ」— 通るのは status が queued のとき)。書き終えたら全部の条件について読み比べる。状態の取り違えのような書き間違いは、ほかの検査ではほとんど見つからない。
|
|
180
202
|
|
|
181
|
-
|
|
203
|
+
「外しても振る舞いが変わらない(ほかの条件と重なっている)」条件は、できない理由の文言を分けたくて重ねているなら残してよい。そうでなければ消す。「(後の条件のために型を絞る条件)」と出るもの(判別 union の `actor.kind === "human"` のあとで `actor.via` を読む、など)は、消すと型が通らないので残す。
|
|
182
204
|
|
|
183
205
|
## 人への渡し方
|
|
184
206
|
|
|
@@ -32,6 +32,8 @@ spec の書き方の規約は [live-spec-authoring](../live-spec-authoring/SKILL
|
|
|
32
32
|
|
|
33
33
|
spec の各操作と ensure に、根拠となるコードの場所を `// 根拠: path:line(補足)` の形のコメントで残す。レビューする人が spec とコードを突き合わせられるようにするため。操作の関数の中に書いた根拠は、仕様書の操作カードにも「根拠」として表示される(絶対パスは書かない。共有したときに手元の環境が漏れるため)。
|
|
34
34
|
|
|
35
|
+
根拠のパスは、spec のある git リポジトリのルートからの相対で書く。spec を対象のリポジトリの外(別のリポジトリなど)に置くときは、`export const evidenceRoot = "../my-app"`(spec のファイルからの相対)で起点を指定する。live-spec は根拠のファイルが無いとき・行がファイルの範囲の外にあるときに検査結果に出すので、コードが移動したり行がずれたりしたら、根拠を直す。
|
|
36
|
+
|
|
35
37
|
```ts
|
|
36
38
|
/** 注文を確定する */
|
|
37
39
|
export function confirm(order: Order, user: User): Order {
|