@yutaura/live-spec 0.1.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 +15 -2
- package/dist/check.d.ts +5 -0
- package/dist/check.js +40 -0
- package/dist/cli.js +116 -10
- package/dist/diff.d.ts +92 -2
- package/dist/diff.js +101 -64
- package/dist/ensure-scope.d.ts +9 -0
- package/dist/ensure-scope.js +81 -0
- package/dist/ensure.d.ts +4 -0
- package/dist/ensure.js +27 -2
- package/dist/evidence.d.ts +3 -0
- package/dist/evidence.js +46 -0
- package/dist/explore.d.ts +20 -1
- package/dist/explore.js +38 -12
- package/dist/generate.d.ts +7 -0
- package/dist/generate.js +18 -5
- package/dist/load.d.ts +11 -1
- package/dist/load.js +9 -0
- package/dist/model.d.ts +127 -0
- package/dist/model.js +40 -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 +235 -0
- package/dist/project-diff.d.ts +24 -0
- package/dist/project-diff.js +76 -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/html.d.ts +1 -0
- package/dist/render/html.js +42 -10
- package/dist/render/layout.d.ts +2 -0
- package/dist/render/layout.js +1 -1
- package/dist/render/markdown.js +30 -3
- package/dist/render/review.d.ts +17 -0
- package/dist/render/review.js +17 -0
- package/dist/render/union.d.ts +23 -0
- package/dist/render/union.js +43 -0
- package/dist/spec-loader.d.ts +2 -0
- package/dist/spec-loader.js +24 -0
- package/dist/static.d.ts +7 -0
- package/dist/static.js +74 -0
- package/package.json +7 -1
- package/skills/live-spec-authoring/SKILL.md +59 -7
- package/skills/live-spec-from-code/SKILL.md +2 -0
- package/skills/live-spec-review/SKILL.md +16 -10
package/dist/model.d.ts
CHANGED
|
@@ -87,6 +87,21 @@ 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;
|
|
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;
|
|
90
105
|
}>, Schema.Struct<{
|
|
91
106
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
92
107
|
readonly invariant: Schema.String;
|
|
@@ -97,6 +112,34 @@ export declare const Finding: Schema.Union<readonly [Schema.Struct<{
|
|
|
97
112
|
}>>;
|
|
98
113
|
}>]>;
|
|
99
114
|
export type Finding = typeof Finding.Type;
|
|
115
|
+
export declare const EnsureCheck: Schema.Struct<{
|
|
116
|
+
readonly action: Schema.String;
|
|
117
|
+
readonly reason: Schema.String;
|
|
118
|
+
readonly line: Schema.NullOr<Schema.Number>;
|
|
119
|
+
readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
|
|
120
|
+
readonly caughtBy: Schema.$Array<Schema.String>;
|
|
121
|
+
readonly effects: Schema.$Array<Schema.Struct<{
|
|
122
|
+
readonly state: Schema.String;
|
|
123
|
+
readonly action: Schema.String;
|
|
124
|
+
readonly actor: Schema.String;
|
|
125
|
+
readonly input: Schema.NullOr<Schema.String>;
|
|
126
|
+
readonly after: Schema.String;
|
|
127
|
+
}>>;
|
|
128
|
+
readonly newStates: Schema.$Array<Schema.String>;
|
|
129
|
+
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
130
|
+
readonly passes: Schema.String;
|
|
131
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
132
|
+
readonly narrowing: Schema.Boolean;
|
|
133
|
+
}>;
|
|
134
|
+
export type EnsureCheck = typeof EnsureCheck.Type;
|
|
135
|
+
export declare const OmissionHint: Schema.Struct<{
|
|
136
|
+
readonly id: Schema.String;
|
|
137
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
138
|
+
readonly action: Schema.NullOr<Schema.String>;
|
|
139
|
+
readonly field: Schema.NullOr<Schema.String>;
|
|
140
|
+
readonly message: Schema.String;
|
|
141
|
+
}>;
|
|
142
|
+
export type OmissionHint = typeof OmissionHint.Type;
|
|
100
143
|
export declare const Graph: Schema.Struct<{
|
|
101
144
|
readonly states: Schema.$Array<Schema.Struct<{
|
|
102
145
|
readonly id: Schema.String;
|
|
@@ -185,6 +228,21 @@ export declare const Graph: Schema.Struct<{
|
|
|
185
228
|
}>, Schema.Struct<{
|
|
186
229
|
readonly kind: Schema.Literal<"unknown-inputs">;
|
|
187
230
|
readonly action: Schema.String;
|
|
231
|
+
}>, Schema.Struct<{
|
|
232
|
+
readonly kind: Schema.Literal<"reviewed-unmatched">;
|
|
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;
|
|
188
246
|
}>, Schema.Struct<{
|
|
189
247
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
190
248
|
readonly invariant: Schema.String;
|
|
@@ -221,6 +279,32 @@ export declare const Graph: Schema.Struct<{
|
|
|
221
279
|
readonly action: Schema.NullOr<Schema.String>;
|
|
222
280
|
readonly states: Schema.$Array<Schema.String>;
|
|
223
281
|
}>>;
|
|
282
|
+
readonly ensures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
283
|
+
readonly action: Schema.String;
|
|
284
|
+
readonly reason: Schema.String;
|
|
285
|
+
readonly line: Schema.NullOr<Schema.Number>;
|
|
286
|
+
readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
|
|
287
|
+
readonly caughtBy: Schema.$Array<Schema.String>;
|
|
288
|
+
readonly effects: Schema.$Array<Schema.Struct<{
|
|
289
|
+
readonly state: Schema.String;
|
|
290
|
+
readonly action: Schema.String;
|
|
291
|
+
readonly actor: Schema.String;
|
|
292
|
+
readonly input: Schema.NullOr<Schema.String>;
|
|
293
|
+
readonly after: Schema.String;
|
|
294
|
+
}>>;
|
|
295
|
+
readonly newStates: Schema.$Array<Schema.String>;
|
|
296
|
+
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
297
|
+
readonly passes: Schema.String;
|
|
298
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
299
|
+
readonly narrowing: Schema.Boolean;
|
|
300
|
+
}>>>;
|
|
301
|
+
readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
302
|
+
readonly id: Schema.String;
|
|
303
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
304
|
+
readonly action: Schema.NullOr<Schema.String>;
|
|
305
|
+
readonly field: Schema.NullOr<Schema.String>;
|
|
306
|
+
readonly message: Schema.String;
|
|
307
|
+
}>>>;
|
|
224
308
|
readonly scenarios: Schema.$Array<Schema.Struct<{
|
|
225
309
|
readonly name: Schema.String;
|
|
226
310
|
readonly passed: Schema.Boolean;
|
|
@@ -232,6 +316,7 @@ export declare const Graph: Schema.Struct<{
|
|
|
232
316
|
readonly to: Schema.NullOr<Schema.String>;
|
|
233
317
|
readonly ok: Schema.Boolean;
|
|
234
318
|
readonly reason: Schema.NullOr<Schema.String>;
|
|
319
|
+
readonly denied: Schema.Boolean;
|
|
235
320
|
}>>;
|
|
236
321
|
}>>;
|
|
237
322
|
}>;
|
|
@@ -325,6 +410,21 @@ export declare const SpecModel: Schema.Struct<{
|
|
|
325
410
|
}>, Schema.Struct<{
|
|
326
411
|
readonly kind: Schema.Literal<"unknown-inputs">;
|
|
327
412
|
readonly action: Schema.String;
|
|
413
|
+
}>, Schema.Struct<{
|
|
414
|
+
readonly kind: Schema.Literal<"reviewed-unmatched">;
|
|
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;
|
|
328
428
|
}>, Schema.Struct<{
|
|
329
429
|
readonly kind: Schema.Literal<"invariant-violation">;
|
|
330
430
|
readonly invariant: Schema.String;
|
|
@@ -361,6 +461,32 @@ export declare const SpecModel: Schema.Struct<{
|
|
|
361
461
|
readonly action: Schema.NullOr<Schema.String>;
|
|
362
462
|
readonly states: Schema.$Array<Schema.String>;
|
|
363
463
|
}>>;
|
|
464
|
+
readonly ensures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
465
|
+
readonly action: Schema.String;
|
|
466
|
+
readonly reason: Schema.String;
|
|
467
|
+
readonly line: Schema.NullOr<Schema.Number>;
|
|
468
|
+
readonly verdict: Schema.Literals<readonly ["caught", "unguarded", "no-effect"]>;
|
|
469
|
+
readonly caughtBy: Schema.$Array<Schema.String>;
|
|
470
|
+
readonly effects: Schema.$Array<Schema.Struct<{
|
|
471
|
+
readonly state: Schema.String;
|
|
472
|
+
readonly action: Schema.String;
|
|
473
|
+
readonly actor: Schema.String;
|
|
474
|
+
readonly input: Schema.NullOr<Schema.String>;
|
|
475
|
+
readonly after: Schema.String;
|
|
476
|
+
}>>;
|
|
477
|
+
readonly newStates: Schema.$Array<Schema.String>;
|
|
478
|
+
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
479
|
+
readonly passes: Schema.String;
|
|
480
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
481
|
+
readonly narrowing: Schema.Boolean;
|
|
482
|
+
}>>>;
|
|
483
|
+
readonly omissions: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
484
|
+
readonly id: Schema.String;
|
|
485
|
+
readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
|
|
486
|
+
readonly action: Schema.NullOr<Schema.String>;
|
|
487
|
+
readonly field: Schema.NullOr<Schema.String>;
|
|
488
|
+
readonly message: Schema.String;
|
|
489
|
+
}>>>;
|
|
364
490
|
readonly scenarios: Schema.$Array<Schema.Struct<{
|
|
365
491
|
readonly name: Schema.String;
|
|
366
492
|
readonly passed: Schema.Boolean;
|
|
@@ -372,6 +498,7 @@ export declare const SpecModel: Schema.Struct<{
|
|
|
372
498
|
readonly to: Schema.NullOr<Schema.String>;
|
|
373
499
|
readonly ok: Schema.Boolean;
|
|
374
500
|
readonly reason: Schema.NullOr<Schema.String>;
|
|
501
|
+
readonly denied: Schema.Boolean;
|
|
375
502
|
}>>;
|
|
376
503
|
}>>;
|
|
377
504
|
readonly name: Schema.String;
|
package/dist/model.js
CHANGED
|
@@ -55,6 +55,10 @@ 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 }),
|
|
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 }),
|
|
58
62
|
Schema.Struct({
|
|
59
63
|
kind: Schema.Literal("invariant-violation"),
|
|
60
64
|
invariant: Schema.String,
|
|
@@ -62,6 +66,39 @@ export const Finding = Schema.Union([
|
|
|
62
66
|
path: Schema.Array(Schema.Struct({ action: Schema.String, actor: Schema.String })),
|
|
63
67
|
}),
|
|
64
68
|
]);
|
|
69
|
+
// ensure を 1 つ外して探索し直した結果。caught: 外すと何か(不変条件・シナリオ・画面・実行時エラー)が気づく。
|
|
70
|
+
// unguarded: 振る舞いは変わるのに何も気づかない(その条件の意図が、ほかのどこにも書かれていない)。
|
|
71
|
+
// no-effect: 外しても振る舞いが変わらない(ほかの条件と重なっている)。
|
|
72
|
+
export const EnsureCheck = Schema.Struct({
|
|
73
|
+
action: Schema.String,
|
|
74
|
+
reason: Schema.String,
|
|
75
|
+
line: Schema.NullOr(Schema.Number),
|
|
76
|
+
verdict: Schema.Literals(["caught", "unguarded", "no-effect"]),
|
|
77
|
+
caughtBy: Schema.Array(Schema.String),
|
|
78
|
+
effects: Schema.Array(Schema.Struct({ state: Schema.String, action: Schema.String, actor: Schema.String, input: Schema.NullOr(Schema.String), after: Schema.String })),
|
|
79
|
+
newStates: Schema.Array(Schema.String),
|
|
80
|
+
/** 実際に通るのはどんなときか(その ensure だけを効かせて実行して求めたもの) */
|
|
81
|
+
passes: Schema.String,
|
|
82
|
+
/** 後ろの条件のために型を絞る ensure か */
|
|
83
|
+
narrowing: Schema.Boolean,
|
|
84
|
+
});
|
|
85
|
+
// spec を実行した結果から見つけた、書き忘れていそうな条件の候補。id は reviewed で「意図どおり」と答えるときのキー。
|
|
86
|
+
export const OmissionHint = Schema.Struct({
|
|
87
|
+
id: Schema.String,
|
|
88
|
+
kind: Schema.Literals([
|
|
89
|
+
"anyone-can",
|
|
90
|
+
"anytime",
|
|
91
|
+
"ignores-input",
|
|
92
|
+
"same-for-similar-actors",
|
|
93
|
+
"indistinguishable-example",
|
|
94
|
+
"indistinguishable-actor",
|
|
95
|
+
"unused-field",
|
|
96
|
+
"cannot-reach-goal",
|
|
97
|
+
]),
|
|
98
|
+
action: Schema.NullOr(Schema.String),
|
|
99
|
+
field: Schema.NullOr(Schema.String),
|
|
100
|
+
message: Schema.String,
|
|
101
|
+
});
|
|
65
102
|
export const Graph = Schema.Struct({
|
|
66
103
|
states: Schema.Array(ExploredState),
|
|
67
104
|
transitions: Schema.Array(Transition),
|
|
@@ -80,6 +117,8 @@ export const Graph = Schema.Struct({
|
|
|
80
117
|
action: Schema.NullOr(Schema.String),
|
|
81
118
|
states: Schema.Array(Schema.String),
|
|
82
119
|
})),
|
|
120
|
+
ensures: Schema.optionalKey(Schema.Array(EnsureCheck)),
|
|
121
|
+
omissions: Schema.optionalKey(Schema.Array(OmissionHint)),
|
|
83
122
|
scenarios: Schema.Array(Schema.Struct({
|
|
84
123
|
name: Schema.String,
|
|
85
124
|
passed: Schema.Boolean,
|
|
@@ -91,6 +130,7 @@ export const Graph = Schema.Struct({
|
|
|
91
130
|
to: Schema.NullOr(Schema.String),
|
|
92
131
|
ok: Schema.Boolean,
|
|
93
132
|
reason: Schema.NullOr(Schema.String),
|
|
133
|
+
denied: Schema.Boolean,
|
|
94
134
|
})),
|
|
95
135
|
})),
|
|
96
136
|
});
|
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, narrowing: e.narrowing === true }));
|
|
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, narrowing: false }));
|
|
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,235 @@
|
|
|
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
|
+
hints.push(...deadEnds(spec, graph));
|
|
131
|
+
return hints;
|
|
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
|
+
};
|
|
190
|
+
const MAX_PERTURBED_STATES = 50;
|
|
191
|
+
// 項目の値を 1 つだけ差し替えた状態で操作を実行し直し、可否が変わるかを見る(Leveson の入力の完全性)。
|
|
192
|
+
// 差し替えた状態は到達できないこともあるが、ここで知りたいのは「条件がその項目を見ているか」なので構わない。
|
|
193
|
+
const unusedFields = (spec, graph, options) => {
|
|
194
|
+
const states = graph.states.slice(0, MAX_PERTURBED_STATES);
|
|
195
|
+
const fields = [...new Set(states.flatMap((s) => Object.keys(s.values)))];
|
|
196
|
+
// 文字列の項目には、登場人物の属性の値も候補に入れる。authorId のように登場人物と比べる項目は、
|
|
197
|
+
// 到達できる状態では 1 つの値("alice")しかとらないことがあり、それだけでは差し替えられないため。
|
|
198
|
+
const actorStrings = spec.actors.flatMap(([, v]) => (isPlainObject(v) ? Object.values(v).filter((x) => typeof x === "string") : []));
|
|
199
|
+
const enabledAt = new Map(graph.availability.map((r) => [JSON.stringify([r.state, r.action, r.actor, r.input]), r.enabled]));
|
|
200
|
+
const bypassFor = (action) => (options.bypassEnsure?.action === action ? options.bypassEnsure.reason : null);
|
|
201
|
+
const NO_INPUT = [[null, []]];
|
|
202
|
+
const used = (field) => {
|
|
203
|
+
const seen = states.map((s) => s.values[field]);
|
|
204
|
+
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));
|
|
205
|
+
for (const s of states) {
|
|
206
|
+
for (const value of domain) {
|
|
207
|
+
if (JSON.stringify(value) === JSON.stringify(s.values[field]))
|
|
208
|
+
continue;
|
|
209
|
+
const perturbed = { ...s.values, [field]: value };
|
|
210
|
+
for (const [action, fn] of spec.actions) {
|
|
211
|
+
for (const [actor, actorValue] of spec.actors) {
|
|
212
|
+
for (const [input, args] of spec.inputs[action] ?? NO_INPUT) {
|
|
213
|
+
const before = enabledAt.get(JSON.stringify([s.id, action, actor, input]));
|
|
214
|
+
if (before === undefined)
|
|
215
|
+
continue;
|
|
216
|
+
const after = runAction(fn, perturbed, actorValue, args, bypassFor(action)).kind === "ok";
|
|
217
|
+
if (before !== after)
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
};
|
|
226
|
+
return fields
|
|
227
|
+
.filter((f) => !used(f))
|
|
228
|
+
.map((field) => ({
|
|
229
|
+
id: `unused-field:${field}`,
|
|
230
|
+
kind: "unused-field",
|
|
231
|
+
action: null,
|
|
232
|
+
field,
|
|
233
|
+
message: `状態の項目 ${field} は、値を変えても、どの操作の可否も変わらない。この項目を使う条件を書き忘れていないか`,
|
|
234
|
+
}));
|
|
235
|
+
};
|
|
@@ -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
|
+
};
|