@yutaura/live-spec 0.0.0-stage → 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.
- package/.claude-plugin/plugin.json +12 -0
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/dist/cli.d.ts +20 -0
- package/dist/cli.js +219 -0
- package/dist/diff.d.ts +123 -0
- package/dist/diff.js +178 -0
- package/dist/ensure-scope.d.ts +9 -0
- package/dist/ensure-scope.js +81 -0
- package/dist/ensure.d.ts +9 -0
- package/dist/ensure.js +35 -0
- package/dist/explore.d.ts +25 -0
- package/dist/explore.js +292 -0
- package/dist/generate.d.ts +21 -0
- package/dist/generate.js +159 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/load.d.ts +58 -0
- package/dist/load.js +79 -0
- package/dist/model.d.ts +487 -0
- package/dist/model.js +209 -0
- package/dist/mutation.d.ts +3 -0
- package/dist/mutation.js +51 -0
- package/dist/omission.d.ts +6 -0
- package/dist/omission.js +177 -0
- package/dist/project-diff.d.ts +24 -0
- package/dist/project-diff.js +76 -0
- package/dist/render/collapse.d.ts +4 -0
- package/dist/render/collapse.js +22 -0
- package/dist/render/diff-html.d.ts +2 -0
- package/dist/render/diff-html.js +148 -0
- package/dist/render/diff-markdown.d.ts +11 -0
- package/dist/render/diff-markdown.js +70 -0
- package/dist/render/ensures.d.ts +2 -0
- package/dist/render/ensures.js +21 -0
- package/dist/render/escape.d.ts +1 -0
- package/dist/render/escape.js +2 -0
- package/dist/render/html.d.ts +13 -0
- package/dist/render/html.js +717 -0
- package/dist/render/layout.d.ts +39 -0
- package/dist/render/layout.js +74 -0
- package/dist/render/markdown.d.ts +20 -0
- package/dist/render/markdown.js +248 -0
- package/dist/render/review.d.ts +17 -0
- package/dist/render/review.js +17 -0
- package/dist/render/summary.d.ts +62 -0
- package/dist/render/summary.js +119 -0
- package/dist/render/union.d.ts +23 -0
- package/dist/render/union.js +43 -0
- package/dist/render/wireframe.d.ts +3 -0
- package/dist/render/wireframe.js +62 -0
- package/dist/serve.d.ts +8 -0
- package/dist/serve.js +71 -0
- package/dist/skills.d.ts +8 -0
- package/dist/skills.js +20 -0
- package/dist/spec-loader.d.ts +2 -0
- package/dist/spec-loader.js +24 -0
- package/dist/static.d.ts +15 -0
- package/dist/static.js +151 -0
- package/dist/surface.d.ts +59 -0
- package/dist/surface.js +44 -0
- package/dist/watch.d.ts +10 -0
- package/dist/watch.js +47 -0
- package/package.json +60 -4
- package/skills/live-spec-authoring/SKILL.md +190 -0
- package/skills/live-spec-from-code/SKILL.md +93 -0
- package/skills/live-spec-review/SKILL.md +71 -0
package/dist/model.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export const ExploredState = Schema.Struct({
|
|
3
|
+
id: Schema.String,
|
|
4
|
+
label: Schema.String,
|
|
5
|
+
values: Schema.Record(Schema.String, Schema.Unknown),
|
|
6
|
+
initial: Schema.Boolean,
|
|
7
|
+
terminal: Schema.Boolean,
|
|
8
|
+
});
|
|
9
|
+
export const Transition = Schema.Struct({
|
|
10
|
+
from: Schema.String,
|
|
11
|
+
to: Schema.String,
|
|
12
|
+
action: Schema.String,
|
|
13
|
+
actors: Schema.Array(Schema.String),
|
|
14
|
+
});
|
|
15
|
+
export const Availability = Schema.Struct({
|
|
16
|
+
state: Schema.String,
|
|
17
|
+
action: Schema.String,
|
|
18
|
+
actor: Schema.String,
|
|
19
|
+
input: Schema.NullOr(Schema.String),
|
|
20
|
+
enabled: Schema.Boolean,
|
|
21
|
+
to: Schema.NullOr(Schema.String),
|
|
22
|
+
reason: Schema.NullOr(Schema.String),
|
|
23
|
+
});
|
|
24
|
+
const FieldValue = Schema.Union([Schema.String, Schema.Boolean]);
|
|
25
|
+
export const Finding = Schema.Union([
|
|
26
|
+
Schema.Struct({ kind: Schema.Literal("unreachable-value"), field: Schema.String, value: FieldValue }),
|
|
27
|
+
Schema.Struct({ kind: Schema.Literal("never-enabled-action"), action: Schema.String }),
|
|
28
|
+
Schema.Struct({
|
|
29
|
+
kind: Schema.Literal("action-error"),
|
|
30
|
+
action: Schema.String,
|
|
31
|
+
actor: Schema.String,
|
|
32
|
+
state: Schema.String,
|
|
33
|
+
message: Schema.String,
|
|
34
|
+
}),
|
|
35
|
+
Schema.Struct({ kind: Schema.Literal("invalid-return"), action: Schema.String, actor: Schema.String, state: Schema.String }),
|
|
36
|
+
Schema.Struct({ kind: Schema.Literal("state-limit"), maxStates: Schema.Number }),
|
|
37
|
+
Schema.Struct({ kind: Schema.Literal("missing-inputs"), action: Schema.String }),
|
|
38
|
+
Schema.Struct({ kind: Schema.Literal("question-unmatched"), ask: Schema.String }),
|
|
39
|
+
Schema.Struct({
|
|
40
|
+
kind: Schema.Literal("decision-mismatch"),
|
|
41
|
+
decision: Schema.String,
|
|
42
|
+
row: Schema.Number,
|
|
43
|
+
expected: Schema.String,
|
|
44
|
+
actual: Schema.String,
|
|
45
|
+
}),
|
|
46
|
+
Schema.Struct({ kind: Schema.Literal("decision-error"), decision: Schema.String, row: Schema.Number, message: Schema.String }),
|
|
47
|
+
Schema.Struct({ kind: Schema.Literal("link-unresolved"), to: Schema.String, action: Schema.NullOr(Schema.String) }),
|
|
48
|
+
Schema.Struct({ kind: Schema.Literal("scenario-failed"), scenario: Schema.String, step: Schema.Number, reason: Schema.String }),
|
|
49
|
+
Schema.Struct({
|
|
50
|
+
kind: Schema.Literal("surface-action-not-allowed"),
|
|
51
|
+
state: Schema.String,
|
|
52
|
+
actor: Schema.String,
|
|
53
|
+
action: Schema.String,
|
|
54
|
+
}),
|
|
55
|
+
Schema.Struct({ kind: Schema.Literal("surface-action-missing"), state: Schema.String, actor: Schema.String, action: Schema.String }),
|
|
56
|
+
Schema.Struct({ kind: Schema.Literal("surface-error"), state: Schema.String, actor: Schema.String, message: Schema.String }),
|
|
57
|
+
Schema.Struct({ kind: Schema.Literal("unknown-inputs"), action: Schema.String }),
|
|
58
|
+
Schema.Struct({ kind: Schema.Literal("reviewed-unmatched"), id: Schema.String }),
|
|
59
|
+
Schema.Struct({
|
|
60
|
+
kind: Schema.Literal("invariant-violation"),
|
|
61
|
+
invariant: Schema.String,
|
|
62
|
+
state: Schema.String,
|
|
63
|
+
path: Schema.Array(Schema.Struct({ action: Schema.String, actor: Schema.String })),
|
|
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
|
+
});
|
|
96
|
+
export const Graph = Schema.Struct({
|
|
97
|
+
states: Schema.Array(ExploredState),
|
|
98
|
+
transitions: Schema.Array(Transition),
|
|
99
|
+
availability: Schema.Array(Availability),
|
|
100
|
+
findings: Schema.Array(Finding),
|
|
101
|
+
surfaces: Schema.Array(Schema.Struct({ state: Schema.String, actor: Schema.String, surface: Schema.Unknown })),
|
|
102
|
+
decisions: Schema.Array(Schema.Struct({
|
|
103
|
+
name: Schema.String,
|
|
104
|
+
columns: Schema.Array(Schema.String),
|
|
105
|
+
rows: Schema.Array(Schema.Struct({ input: Schema.Unknown, result: Schema.Unknown, expect: Schema.Unknown, ok: Schema.Boolean })),
|
|
106
|
+
})),
|
|
107
|
+
links: Schema.Array(Schema.Struct({ from: Schema.NullOr(Schema.String), to: Schema.String, action: Schema.NullOr(Schema.String), label: Schema.String })),
|
|
108
|
+
questions: Schema.Array(Schema.Struct({
|
|
109
|
+
ask: Schema.String,
|
|
110
|
+
note: Schema.NullOr(Schema.String),
|
|
111
|
+
action: Schema.NullOr(Schema.String),
|
|
112
|
+
states: Schema.Array(Schema.String),
|
|
113
|
+
})),
|
|
114
|
+
ensures: Schema.optionalKey(Schema.Array(EnsureCheck)),
|
|
115
|
+
omissions: Schema.optionalKey(Schema.Array(OmissionHint)),
|
|
116
|
+
scenarios: Schema.Array(Schema.Struct({
|
|
117
|
+
name: Schema.String,
|
|
118
|
+
passed: Schema.Boolean,
|
|
119
|
+
steps: Schema.Array(Schema.Struct({
|
|
120
|
+
action: Schema.String,
|
|
121
|
+
actor: Schema.String,
|
|
122
|
+
input: Schema.NullOr(Schema.String),
|
|
123
|
+
from: Schema.NullOr(Schema.String),
|
|
124
|
+
to: Schema.NullOr(Schema.String),
|
|
125
|
+
ok: Schema.Boolean,
|
|
126
|
+
reason: Schema.NullOr(Schema.String),
|
|
127
|
+
denied: Schema.Boolean,
|
|
128
|
+
})),
|
|
129
|
+
})),
|
|
130
|
+
});
|
|
131
|
+
// 正規化モデルでは availability を「状態 × 操作」ごとにまとめる理由: 1 件ずつ書き出すと
|
|
132
|
+
// 状態 × 操作 × 登場人物 × 入力の数だけ並び、入力の例が多い spec では JSON が 1MB を超える。
|
|
133
|
+
// 結果ごとにまとめ、さらに登場人物ごとに「その人の入力すべて("*")」か「入力の名前の一覧」を書く。
|
|
134
|
+
// 全員が同じ結果なら cases そのものを "*" にする。
|
|
135
|
+
export const CompactAvailability = Schema.Struct({
|
|
136
|
+
state: Schema.String,
|
|
137
|
+
action: Schema.String,
|
|
138
|
+
outcomes: Schema.Array(Schema.Struct({
|
|
139
|
+
enabled: Schema.Boolean,
|
|
140
|
+
to: Schema.NullOr(Schema.String),
|
|
141
|
+
reason: Schema.NullOr(Schema.String),
|
|
142
|
+
cases: Schema.Union([
|
|
143
|
+
Schema.Literal("*"),
|
|
144
|
+
Schema.Record(Schema.String, Schema.Union([Schema.Literal("*"), Schema.Array(Schema.String)])),
|
|
145
|
+
]),
|
|
146
|
+
})),
|
|
147
|
+
});
|
|
148
|
+
export const SpecModel = Schema.Struct({
|
|
149
|
+
name: Schema.String,
|
|
150
|
+
summary: Schema.NullOr(Schema.String),
|
|
151
|
+
fields: Schema.Record(Schema.String, Schema.Array(FieldValue)),
|
|
152
|
+
actors: Schema.Array(Schema.Struct({ name: Schema.String, value: Schema.Unknown })),
|
|
153
|
+
actions: Schema.Array(Schema.Struct({ name: Schema.String, summary: Schema.NullOr(Schema.String) })),
|
|
154
|
+
inputs: Schema.Record(Schema.String, Schema.Array(Schema.String)),
|
|
155
|
+
...Graph.fields,
|
|
156
|
+
availability: Schema.Array(CompactAvailability),
|
|
157
|
+
});
|
|
158
|
+
export const normalize = (spec, graph) => ({
|
|
159
|
+
name: spec.name,
|
|
160
|
+
summary: spec.stateDoc,
|
|
161
|
+
fields: canonical(spec.fields),
|
|
162
|
+
actors: spec.actors.map(([name, value]) => ({ name, value: canonical(value) })),
|
|
163
|
+
actions: spec.actions.map(([name]) => ({ name, summary: spec.docs[name] ?? null })),
|
|
164
|
+
inputs: Object.fromEntries(Object.entries(spec.inputs).map(([action, examples]) => [action, examples.map(([label]) => label)])),
|
|
165
|
+
...graph,
|
|
166
|
+
availability: compactAvailability(graph.availability),
|
|
167
|
+
});
|
|
168
|
+
const compactAvailability = (availability) => {
|
|
169
|
+
const byStateAction = new Map();
|
|
170
|
+
for (const a of availability) {
|
|
171
|
+
const key = JSON.stringify([a.state, a.action]);
|
|
172
|
+
byStateAction.set(key, [...(byStateAction.get(key) ?? []), a]);
|
|
173
|
+
}
|
|
174
|
+
return [...byStateAction.values()].map((entries) => {
|
|
175
|
+
const inputsOf = (actor) => entries.filter((a) => a.actor === actor).map((a) => a.input);
|
|
176
|
+
const outcomes = new Map();
|
|
177
|
+
for (const a of entries) {
|
|
178
|
+
const key = JSON.stringify([a.enabled, a.to, a.reason]);
|
|
179
|
+
const o = outcomes.get(key) ?? { enabled: a.enabled, to: a.to, reason: a.reason, entries: [] };
|
|
180
|
+
o.entries.push(a);
|
|
181
|
+
outcomes.set(key, o);
|
|
182
|
+
}
|
|
183
|
+
const list = [...outcomes.values()];
|
|
184
|
+
return {
|
|
185
|
+
state: entries[0].state,
|
|
186
|
+
action: entries[0].action,
|
|
187
|
+
outcomes: list.map(({ entries: mine, ...o }) => {
|
|
188
|
+
if (list.length === 1)
|
|
189
|
+
return { ...o, cases: "*" };
|
|
190
|
+
const cases = {};
|
|
191
|
+
for (const actor of [...new Set(mine.map((a) => a.actor))]) {
|
|
192
|
+
const inputs = mine.filter((a) => a.actor === actor).map((a) => a.input);
|
|
193
|
+
cases[actor] = inputs.length === inputsOf(actor).length ? "*" : inputs.map((i) => i ?? "");
|
|
194
|
+
}
|
|
195
|
+
return { ...o, cases };
|
|
196
|
+
}),
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
// キーを並べ替えて正規化する理由: spec の書き手(主に AI)がオブジェクトの
|
|
201
|
+
// キーの順序を変えただけで、意味の変わらない差分が出ないようにするため。
|
|
202
|
+
export const canonical = (u) => {
|
|
203
|
+
if (Array.isArray(u))
|
|
204
|
+
return u.map(canonical);
|
|
205
|
+
if (typeof u === "object" && u !== null) {
|
|
206
|
+
return Object.fromEntries(Object.keys(u).sort().map((k) => [k, canonical(u[k])]));
|
|
207
|
+
}
|
|
208
|
+
return u;
|
|
209
|
+
};
|
package/dist/mutation.js
ADDED
|
@@ -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>;
|
package/dist/omission.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { runAction } from "./explore.js";
|
|
2
|
+
const groupBy = (rows, keyOf) => {
|
|
3
|
+
const groups = new Map();
|
|
4
|
+
for (const r of rows) {
|
|
5
|
+
const key = JSON.stringify(keyOf(r));
|
|
6
|
+
groups.set(key, [...(groups.get(key) ?? []), r]);
|
|
7
|
+
}
|
|
8
|
+
return [...groups.values()];
|
|
9
|
+
};
|
|
10
|
+
const enabledOf = (r) => String(r.enabled);
|
|
11
|
+
// 行き先まで含める理由: 入力や登場人物によって行き先が変わる操作は、可否が同じでも、その入力・登場人物を見ている。
|
|
12
|
+
const outcomeOf = (r) => (r.enabled ? `ok:${r.to}` : "no");
|
|
13
|
+
const invariantAcross = (rows, rest, result) => groupBy(rows, rest).every((g) => new Set(g.map(result)).size === 1);
|
|
14
|
+
const signatures = (rows, valueOf, rest) => {
|
|
15
|
+
const byValue = new Map();
|
|
16
|
+
for (const r of rows)
|
|
17
|
+
byValue.set(valueOf(r), [...(byValue.get(valueOf(r)) ?? []), r]);
|
|
18
|
+
return new Map([...byValue].map(([value, rs]) => [
|
|
19
|
+
value,
|
|
20
|
+
JSON.stringify(rs.map((r) => [JSON.stringify(rest(r)), outcomeOf(r)]).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))),
|
|
21
|
+
]));
|
|
22
|
+
};
|
|
23
|
+
const sameGroups = (sigs) => {
|
|
24
|
+
const bySig = new Map();
|
|
25
|
+
for (const [value, sig] of sigs)
|
|
26
|
+
bySig.set(sig, [...(bySig.get(sig) ?? []), value]);
|
|
27
|
+
return [...bySig.values()].filter((g) => g.length > 1);
|
|
28
|
+
};
|
|
29
|
+
const isPlainObject = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
30
|
+
// 1 つの属性だけが違う 2 人の組。「別の著者bob」と「著者alice」のように id だけが違う組は、
|
|
31
|
+
// 「自分の〜だけ」の条件があるかどうかで扱いが分かれるはずの組なので、区別の有無を比べる対象にする。
|
|
32
|
+
const similarPairs = (spec) => {
|
|
33
|
+
const pairs = [];
|
|
34
|
+
for (const [i, [p, pv]] of spec.actors.entries()) {
|
|
35
|
+
for (const [q, qv] of spec.actors.slice(i + 1)) {
|
|
36
|
+
if (!isPlainObject(pv) || !isPlainObject(qv))
|
|
37
|
+
continue;
|
|
38
|
+
const keys = [...new Set([...Object.keys(pv), ...Object.keys(qv)])];
|
|
39
|
+
const differing = keys.filter((k) => JSON.stringify(pv[k]) !== JSON.stringify(qv[k]));
|
|
40
|
+
if (differing.length === 1)
|
|
41
|
+
pairs.push({ p, q, key: differing[0] });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return pairs;
|
|
45
|
+
};
|
|
46
|
+
const distinguishes = (rows, p, q) => groupBy(rows.filter((r) => r.actor === p || r.actor === q), (r) => [r.state, r.input]).some((g) => new Set(g.map((r) => r.enabled)).size > 1);
|
|
47
|
+
export const findOmissionHints = (spec, graph, options = {}) => {
|
|
48
|
+
const hints = [];
|
|
49
|
+
const rowsOf = (action) => graph.availability.filter((r) => r.action === action);
|
|
50
|
+
const distinctActors = new Set(spec.actors.map(([, v]) => JSON.stringify(v))).size;
|
|
51
|
+
const pairs = similarPairs(spec);
|
|
52
|
+
for (const [action] of spec.actions) {
|
|
53
|
+
const rows = rowsOf(action);
|
|
54
|
+
if (!rows.some((r) => r.enabled))
|
|
55
|
+
continue;
|
|
56
|
+
if (distinctActors > 1 && invariantAcross(rows, (r) => [r.state, r.input], outcomeOf)) {
|
|
57
|
+
hints.push({
|
|
58
|
+
id: `anyone-can:${action}`,
|
|
59
|
+
kind: "anyone-can",
|
|
60
|
+
action,
|
|
61
|
+
field: null,
|
|
62
|
+
message: `${action} は、誰が実行しても可否が変わらない(誰でもできる)。「〜だけ」の条件を書き忘れていないか`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (graph.states.length > 1 && invariantAcross(rows, (r) => [r.actor, r.input], enabledOf)) {
|
|
66
|
+
hints.push({
|
|
67
|
+
id: `anytime:${action}`,
|
|
68
|
+
kind: "anytime",
|
|
69
|
+
action,
|
|
70
|
+
field: null,
|
|
71
|
+
message: `${action} は、どの状態でも可否が変わらない(いつでもできる)。状態の条件を書き忘れていないか`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const inputCount = spec.inputs[action]?.length ?? 0;
|
|
75
|
+
const ignoresInput = inputCount > 1 && invariantAcross(rows, (r) => [r.state, r.actor], outcomeOf);
|
|
76
|
+
if (ignoresInput) {
|
|
77
|
+
hints.push({
|
|
78
|
+
id: `ignores-input:${action}`,
|
|
79
|
+
kind: "ignores-input",
|
|
80
|
+
action,
|
|
81
|
+
field: null,
|
|
82
|
+
message: `${action} は、入力の例を変えても結果が変わらない。入力の検証を書き忘れていないか`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// 断られる例同士が同じ結果なのは、境目の値を確かめる書き方なので候補にしない。
|
|
86
|
+
// 実行できる例同士が同じ結果なのは、違う結果を期待して選んだ例が、条件の書き忘れで同じになった可能性がある。
|
|
87
|
+
if (inputCount > 2 && !ignoresInput) {
|
|
88
|
+
const accepted = new Set(rows.filter((r) => r.enabled).map((r) => r.input ?? ""));
|
|
89
|
+
for (const group of sameGroups(signatures(rows, (r) => r.input ?? "", (r) => [r.state, r.actor]))) {
|
|
90
|
+
if (!group.every((g) => accepted.has(g)))
|
|
91
|
+
continue;
|
|
92
|
+
hints.push({
|
|
93
|
+
id: `indistinguishable-example:${action}:${group.join(",")}`,
|
|
94
|
+
kind: "indistinguishable-example",
|
|
95
|
+
action,
|
|
96
|
+
field: null,
|
|
97
|
+
message: `${action} の入力の例「${group.join("」「")}」は、結果がまったく同じ。違う結果を期待していなかったか(条件の書き忘れ)`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const blind = pairs.filter(({ p, q }) => rows.some((r) => r.enabled && (r.actor === p || r.actor === q)) && !distinguishes(rows, p, q));
|
|
102
|
+
const reported = blind
|
|
103
|
+
.map(({ p, q, key }) => ({ p, q, key, others: spec.actions.map(([n]) => n).filter((n) => n !== action && distinguishes(rowsOf(n), p, q)) }))
|
|
104
|
+
.filter((b) => b.others.length > 0);
|
|
105
|
+
if (reported.length > 0) {
|
|
106
|
+
const others = [...new Set(reported.flatMap((b) => b.others))];
|
|
107
|
+
hints.push({
|
|
108
|
+
id: `same-for-similar-actors:${action}`,
|
|
109
|
+
kind: "same-for-similar-actors",
|
|
110
|
+
action,
|
|
111
|
+
field: null,
|
|
112
|
+
message: `${action} は、${reported.map((b) => `${b.p} と ${b.q}(${b.key} だけが違う)`).join("、")}を区別しない。${others.join(", ")} は区別している。「自分の〜だけ」の条件を書き忘れていないか`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// 登場人物は、違う振る舞いを確かめるために書き手が選んだもの。すべての操作で結果が同じ 2 人は、
|
|
117
|
+
// 2 人を分けるはずの条件が書き忘れられている可能性がある。
|
|
118
|
+
for (const group of sameGroups(signatures(graph.availability, (r) => r.actor, (r) => [r.state, r.action, r.input]))) {
|
|
119
|
+
if (!graph.availability.some((r) => r.enabled && group.includes(r.actor)))
|
|
120
|
+
continue;
|
|
121
|
+
hints.push({
|
|
122
|
+
id: `indistinguishable-actor:${group.join(",")}`,
|
|
123
|
+
kind: "indistinguishable-actor",
|
|
124
|
+
action: null,
|
|
125
|
+
field: null,
|
|
126
|
+
message: `登場人物「${group.join("」「")}」は、どの状態・操作でも結果がまったく同じ。2 人を分けるはずの条件を書き忘れていないか`,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
hints.push(...unusedFields(spec, graph, options));
|
|
130
|
+
return hints;
|
|
131
|
+
};
|
|
132
|
+
const MAX_PERTURBED_STATES = 50;
|
|
133
|
+
// 項目の値を 1 つだけ差し替えた状態で操作を実行し直し、可否が変わるかを見る(Leveson の入力の完全性)。
|
|
134
|
+
// 差し替えた状態は到達できないこともあるが、ここで知りたいのは「条件がその項目を見ているか」なので構わない。
|
|
135
|
+
const unusedFields = (spec, graph, options) => {
|
|
136
|
+
const states = graph.states.slice(0, MAX_PERTURBED_STATES);
|
|
137
|
+
const fields = [...new Set(states.flatMap((s) => Object.keys(s.values)))];
|
|
138
|
+
// 文字列の項目には、登場人物の属性の値も候補に入れる。authorId のように登場人物と比べる項目は、
|
|
139
|
+
// 到達できる状態では 1 つの値("alice")しかとらないことがあり、それだけでは差し替えられないため。
|
|
140
|
+
const actorStrings = spec.actors.flatMap(([, v]) => (isPlainObject(v) ? Object.values(v).filter((x) => typeof x === "string") : []));
|
|
141
|
+
const enabledAt = new Map(graph.availability.map((r) => [JSON.stringify([r.state, r.action, r.actor, r.input]), r.enabled]));
|
|
142
|
+
const bypassFor = (action) => (options.bypassEnsure?.action === action ? options.bypassEnsure.reason : null);
|
|
143
|
+
const NO_INPUT = [[null, []]];
|
|
144
|
+
const used = (field) => {
|
|
145
|
+
const seen = states.map((s) => s.values[field]);
|
|
146
|
+
const domain = [...new Set([...seen, ...(spec.fields[field] ?? []), ...(seen.some((v) => typeof v === "string") ? actorStrings : [])].map((v) => JSON.stringify(v)))].map((v) => JSON.parse(v));
|
|
147
|
+
for (const s of states) {
|
|
148
|
+
for (const value of domain) {
|
|
149
|
+
if (JSON.stringify(value) === JSON.stringify(s.values[field]))
|
|
150
|
+
continue;
|
|
151
|
+
const perturbed = { ...s.values, [field]: value };
|
|
152
|
+
for (const [action, fn] of spec.actions) {
|
|
153
|
+
for (const [actor, actorValue] of spec.actors) {
|
|
154
|
+
for (const [input, args] of spec.inputs[action] ?? NO_INPUT) {
|
|
155
|
+
const before = enabledAt.get(JSON.stringify([s.id, action, actor, input]));
|
|
156
|
+
if (before === undefined)
|
|
157
|
+
continue;
|
|
158
|
+
const after = runAction(fn, perturbed, actorValue, args, bypassFor(action)).kind === "ok";
|
|
159
|
+
if (before !== after)
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
};
|
|
168
|
+
return fields
|
|
169
|
+
.filter((f) => !used(f))
|
|
170
|
+
.map((field) => ({
|
|
171
|
+
id: `unused-field:${field}`,
|
|
172
|
+
kind: "unused-field",
|
|
173
|
+
action: null,
|
|
174
|
+
field,
|
|
175
|
+
message: `状態の項目 ${field} は、値を変えても、どの操作の可否も変わらない。この項目を使う条件を書き忘れていないか`,
|
|
176
|
+
}));
|
|
177
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ModelDiff } from "./diff.js";
|
|
2
|
+
import type { DomainObject } from "./render/html.js";
|
|
3
|
+
export interface ProjectDiffOptions {
|
|
4
|
+
readonly cwd?: string;
|
|
5
|
+
readonly paths: ReadonlyArray<string>;
|
|
6
|
+
readonly base: string;
|
|
7
|
+
/** 省くと作業ツリーと比べる */
|
|
8
|
+
readonly head?: string;
|
|
9
|
+
}
|
|
10
|
+
export type ObjectStatus = "added" | "removed" | "changed" | "unchanged";
|
|
11
|
+
export interface ObjectDiff {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly status: ObjectStatus;
|
|
14
|
+
readonly before: DomainObject | null;
|
|
15
|
+
readonly after: DomainObject | null;
|
|
16
|
+
readonly diff: ModelDiff;
|
|
17
|
+
}
|
|
18
|
+
export interface ProjectDiff {
|
|
19
|
+
readonly base: string;
|
|
20
|
+
readonly head: string;
|
|
21
|
+
readonly objects: ReadonlyArray<ObjectDiff>;
|
|
22
|
+
}
|
|
23
|
+
export declare const WORKING_TREE = "\u4F5C\u696D\u30C4\u30EA\u30FC";
|
|
24
|
+
export declare const diffProject: (options: ProjectDiffOptions) => Promise<ProjectDiff>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { diffModels, EMPTY_MODEL, isEmptyDiff } from "./diff.js";
|
|
5
|
+
import { expandSpecPaths, loadObject, specNameOf } from "./generate.js";
|
|
6
|
+
import { normalize } from "./model.js";
|
|
7
|
+
export const WORKING_TREE = "作業ツリー";
|
|
8
|
+
const git = (root, args) => execFileSync("git", ["-C", root, ...args], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
9
|
+
const isSpec = (path) => path.endsWith(".spec.ts") && !basename(path).startsWith(".");
|
|
10
|
+
// git のリビジョンから取り出した spec を、元の spec と同じディレクトリの一時ファイルに書いて読み込む理由:
|
|
11
|
+
// spec が import する live-spec を、利用者のプロジェクトの node_modules から解決させるため。
|
|
12
|
+
// そのため spec が相対パスで import している別ファイルは、作業ツリーの版が使われる(git から取り出さない)。
|
|
13
|
+
const loadAtRevision = async (root, rel, ref, tag) => {
|
|
14
|
+
const source = git(root, ["show", `${ref}:${rel}`]);
|
|
15
|
+
const target = join(root, rel);
|
|
16
|
+
let created = null;
|
|
17
|
+
for (let dir = dirname(target); !existsSync(dir); dir = dirname(dir))
|
|
18
|
+
created = dir;
|
|
19
|
+
if (created !== null)
|
|
20
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
21
|
+
const tmp = join(dirname(target), `.${basename(rel).replace(/\.ts$/, "")}.live-spec-${tag}.ts`);
|
|
22
|
+
writeFileSync(tmp, source);
|
|
23
|
+
try {
|
|
24
|
+
return await loadObject(tmp, { name: specNameOf(rel), source });
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
rmSync(tmp, { force: true });
|
|
28
|
+
if (created !== null)
|
|
29
|
+
rmSync(created, { recursive: true, force: true });
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export const diffProject = async (options) => {
|
|
33
|
+
const cwd = resolve(options.cwd ?? ".");
|
|
34
|
+
const root = git(cwd, ["rev-parse", "--show-toplevel"]).trim();
|
|
35
|
+
for (const ref of [options.base, options.head]) {
|
|
36
|
+
if (ref === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
try {
|
|
39
|
+
git(cwd, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new Error(`リビジョン ${ref} が見つかりません`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// パスを git に cwd から相対で解釈させ、--full-name でリポジトリのルートからのパスを受け取る理由:
|
|
46
|
+
// 自分で relative(root, path) を計算すると、macOS の /var → /private/var のようなシンボリックリンクで
|
|
47
|
+
// パスがリポジトリの外に見えてしまうため。
|
|
48
|
+
const listAt = (ref) => git(cwd, ["ls-tree", "-r", "--name-only", "--full-name", ref, "--", ...options.paths])
|
|
49
|
+
.split("\n")
|
|
50
|
+
.filter(isSpec);
|
|
51
|
+
const baseFiles = listAt(options.base);
|
|
52
|
+
const headFiles = options.head === undefined
|
|
53
|
+
? expandSpecPaths(options.paths.map((p) => resolve(cwd, p)))
|
|
54
|
+
.map((p) => relative(root, realpathSync(p)))
|
|
55
|
+
.filter(isSpec)
|
|
56
|
+
: listAt(options.head);
|
|
57
|
+
const load = async (files, ref, tag) => {
|
|
58
|
+
const objects = new Map();
|
|
59
|
+
for (const rel of files) {
|
|
60
|
+
const o = ref === undefined ? await loadObject(join(root, rel)) : await loadAtRevision(root, rel, ref, tag);
|
|
61
|
+
objects.set(o.spec.name, o);
|
|
62
|
+
}
|
|
63
|
+
return objects;
|
|
64
|
+
};
|
|
65
|
+
const before = await load(baseFiles, options.base, "base");
|
|
66
|
+
const after = await load(headFiles, options.head, "head");
|
|
67
|
+
const names = [...new Set([...before.keys(), ...after.keys()])].sort();
|
|
68
|
+
const objects = names.map((name) => {
|
|
69
|
+
const b = before.get(name) ?? null;
|
|
70
|
+
const a = after.get(name) ?? null;
|
|
71
|
+
const diff = diffModels(b === null ? EMPTY_MODEL : normalize(b.spec, b.graph), a === null ? EMPTY_MODEL : normalize(a.spec, a.graph));
|
|
72
|
+
const status = b === null ? "added" : a === null ? "removed" : isEmptyDiff(diff) ? "unchanged" : "changed";
|
|
73
|
+
return { name, status, before: b, after: a, diff };
|
|
74
|
+
});
|
|
75
|
+
return { base: options.base, head: options.head ?? WORKING_TREE, objects };
|
|
76
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const ANY = "*";
|
|
2
|
+
export const ANY_LABEL = "どの状態からでも";
|
|
3
|
+
const MIN_STATES = 3;
|
|
4
|
+
// すべての状態から同じ行き先に移る遷移を 1 本にまとめる理由: 保存やリセットのような「いつでもできる」操作は、
|
|
5
|
+
// 状態の数だけ線が引かれて図が埋まり、一覧も状態名の羅列になる。ステートチャートで親の状態から
|
|
6
|
+
// まとめて書くのと同じく、「どの状態からでも」の 1 本で表す。状態が少ないときはまとめない(かえって分かりにくい)。
|
|
7
|
+
export const collapseTransitions = (graph) => {
|
|
8
|
+
if (graph.states.length < MIN_STATES)
|
|
9
|
+
return graph.transitions;
|
|
10
|
+
const all = graph.states.map((s) => s.id);
|
|
11
|
+
const groups = new Map();
|
|
12
|
+
for (const t of graph.transitions) {
|
|
13
|
+
const key = JSON.stringify([t.action, t.to]);
|
|
14
|
+
groups.set(key, [...(groups.get(key) ?? []), t]);
|
|
15
|
+
}
|
|
16
|
+
return [...groups.values()].flatMap((ts) => {
|
|
17
|
+
const froms = new Set(ts.map((t) => t.from));
|
|
18
|
+
if (!all.every((id) => froms.has(id)))
|
|
19
|
+
return ts;
|
|
20
|
+
return [{ from: ANY, to: ts[0].to, action: ts[0].action, actors: [...new Set(ts.flatMap((t) => t.actors))] }];
|
|
21
|
+
});
|
|
22
|
+
};
|