@yutaura/live-spec 0.3.0 → 0.3.1

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "live-spec",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "AI が書き、人が見て理解できる「生きた仕様書」を作るツール。素の TypeScript で書いた仕様を実行し、状態遷移図・画面・できる/できない理由・検査結果をまとめた仕様書を生成する",
5
5
  "keywords": [
6
6
  "specification",
package/dist/cli.js CHANGED
@@ -154,9 +154,9 @@ const runCheck = async (argv, io) => {
154
154
  for (const { spec, graph } of resolveLinks(loaded)) {
155
155
  const labels = new Map(graph.states.map((s) => [s.id, s.label]));
156
156
  const labelOf = (id) => labels.get(id) ?? id;
157
- const pending = reviewItems(spec, graph).pending;
157
+ const { pending, onHold } = reviewItems(spec, graph);
158
158
  if (graph.findings.length + pending.length === 0) {
159
- io.stdout(`✓ ${spec.name}\n`);
159
+ io.stdout(`✓ ${spec.name}${onHold.length === 0 ? "" : `(判断待ち ${onHold.length} 件)`}\n`);
160
160
  continue;
161
161
  }
162
162
  failed = true;
@@ -1,3 +1,9 @@
1
1
  import type { Spec } from "./load.js";
2
2
  import type { Finding } from "./model.js";
3
+ interface Location {
4
+ readonly path: string;
5
+ readonly last: number;
6
+ }
7
+ export declare const evidenceLocations: (text: string) => Array<Location>;
3
8
  export declare const checkEvidence: (spec: Spec, specPath: string) => Array<Finding>;
9
+ export {};
package/dist/evidence.js CHANGED
@@ -1,8 +1,35 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { existsSync, readFileSync } from "node:fs";
3
3
  import { dirname, join, parse, resolve } from "node:path";
4
- import { splitEvidence } from "./render/markdown.js";
5
- const LOCATION = /^(.+?)(?::(\d+)(?:-(\d+))?)?$/;
4
+ const LOCATION = /^(.*?)(?::(\d+)(?:-(\d+))?)?$/;
5
+ // 1 行に並べた複数の場所を読む(「a.ts:1-2、b.ts:4」「a.ts:186、:877-879」)。補足の括弧の中に「、」や
6
+ // 「server.listen(port, ...)」のような文字列があっても場所と取り違えないよう、括弧を取り除いてから区切る。
7
+ // 2 つ目以降は、場所らしいもの(「:数字」の省略形か、/ や拡張子を含むもの)だけを読む。
8
+ export const evidenceLocations = (text) => {
9
+ let body = text;
10
+ for (let previous = ""; previous !== body;) {
11
+ previous = body;
12
+ body = body.replace(/([^()]*)/g, " ").replace(/\([^()]*\)/g, " ");
13
+ }
14
+ const locations = [];
15
+ let currentPath = null;
16
+ for (const [i, segment] of body.split(/[、,]/).entries()) {
17
+ const token = segment.trim().split(/\s+/)[0] ?? "";
18
+ const looksLikeLocation = /^:\d/.test(token) || token.includes("/") || /\.[A-Za-z][A-Za-z0-9]*(?::\d|$)/.test(token);
19
+ if (token === "" || (i > 0 && !looksLikeLocation))
20
+ continue;
21
+ const m = LOCATION.exec(token);
22
+ if (m === null)
23
+ continue;
24
+ const [, rawPath, start, end] = m;
25
+ const path = rawPath === "" ? currentPath : (rawPath ?? null);
26
+ if (path === null)
27
+ continue;
28
+ currentPath = path;
29
+ locations.push({ path, last: Number(end ?? start ?? 0) });
30
+ }
31
+ return locations;
32
+ };
6
33
  const gitRoot = (dir) => {
7
34
  try {
8
35
  return execFileSync("git", ["-C", dir, "rev-parse", "--show-toplevel"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
@@ -29,18 +56,13 @@ const rootsFor = (spec, specPath) => {
29
56
  const lineCount = (path) => readFileSync(path, "utf8").replace(/\n$/, "").split("\n").length;
30
57
  export const checkEvidence = (spec, specPath) => {
31
58
  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;
59
+ return Object.entries(spec.evidence).flatMap(([action, items]) => items.flatMap((evidence) => evidenceLocations(evidence).flatMap(({ path, last }) => {
37
60
  const file = roots.map((root) => join(root, path)).find((p) => existsSync(p));
38
61
  if (file === undefined)
39
62
  return [{ kind: "evidence-missing", action, evidence }];
40
- const last = Number(end ?? start ?? 0);
41
63
  if (last === 0)
42
64
  return [];
43
65
  const lines = lineCount(file);
44
66
  return last > lines ? [{ kind: "evidence-out-of-range", action, evidence, lines }] : [];
45
- }));
67
+ })));
46
68
  };
package/dist/model.d.ts CHANGED
@@ -137,6 +137,8 @@ export declare const OmissionHint: Schema.Struct<{
137
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
138
  readonly action: Schema.NullOr<Schema.String>;
139
139
  readonly field: Schema.NullOr<Schema.String>;
140
+ /** 状態についての候補(袋小路の入口など)の、その状態の id */
141
+ readonly state: Schema.optionalKey<Schema.String>;
140
142
  readonly message: Schema.String;
141
143
  }>;
142
144
  export type OmissionHint = typeof OmissionHint.Type;
@@ -303,6 +305,8 @@ export declare const Graph: Schema.Struct<{
303
305
  readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
304
306
  readonly action: Schema.NullOr<Schema.String>;
305
307
  readonly field: Schema.NullOr<Schema.String>;
308
+ /** 状態についての候補(袋小路の入口など)の、その状態の id */
309
+ readonly state: Schema.optionalKey<Schema.String>;
306
310
  readonly message: Schema.String;
307
311
  }>>>;
308
312
  readonly scenarios: Schema.$Array<Schema.Struct<{
@@ -485,6 +489,8 @@ export declare const SpecModel: Schema.Struct<{
485
489
  readonly kind: Schema.Literals<readonly ["anyone-can", "anytime", "ignores-input", "same-for-similar-actors", "indistinguishable-example", "indistinguishable-actor", "unused-field", "cannot-reach-goal"]>;
486
490
  readonly action: Schema.NullOr<Schema.String>;
487
491
  readonly field: Schema.NullOr<Schema.String>;
492
+ /** 状態についての候補(袋小路の入口など)の、その状態の id */
493
+ readonly state: Schema.optionalKey<Schema.String>;
488
494
  readonly message: Schema.String;
489
495
  }>>>;
490
496
  readonly scenarios: Schema.$Array<Schema.Struct<{
package/dist/model.js CHANGED
@@ -97,6 +97,8 @@ export const OmissionHint = Schema.Struct({
97
97
  ]),
98
98
  action: Schema.NullOr(Schema.String),
99
99
  field: Schema.NullOr(Schema.String),
100
+ /** 状態についての候補(袋小路の入口など)の、その状態の id */
101
+ state: Schema.optionalKey(Schema.String),
100
102
  message: Schema.String,
101
103
  });
102
104
  export const Graph = Schema.Struct({
package/dist/omission.js CHANGED
@@ -157,23 +157,26 @@ const deadEnds = (spec, graph) => {
157
157
  }
158
158
  }
159
159
  }
160
+ // 遷移の一覧ではなく入力ごとの可否からたどる理由: 同じ操作でも入力の例によって行き先が違うことがあり、
161
+ // 手順に入力の例の名前が無いと、そのままシナリオに起こしたときに別の状態へ行ってしまう。
162
+ const steps = graph.availability.filter((r) => r.enabled && r.to !== null && r.to !== r.state);
160
163
  const parent = new Map();
161
164
  const queue = graph.states.filter((s) => s.initial).map((s) => s.id);
162
165
  const seen = new Set(queue);
163
166
  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))
167
+ for (const r of steps.filter((x) => x.state === queue[i])) {
168
+ if (seen.has(r.to))
166
169
  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
+ seen.add(r.to);
171
+ parent.set(r.to, { from: r.state, step: `${r.action}(${r.input === null ? r.actor : `${r.actor}、${r.input}`})` });
172
+ queue.push(r.to);
170
173
  }
171
174
  }
172
175
  const pathTo = (id) => {
173
- const steps = [];
176
+ const path = [];
174
177
  for (let p = parent.get(id); p !== undefined; p = parent.get(p.from))
175
- steps.unshift(`${p.action}(${p.actor})`);
176
- return steps.join(" → ");
178
+ path.unshift(p.step);
179
+ return path.join(" → ");
177
180
  };
178
181
  const goalNames = spec.goals.map(([name]) => name).join("、");
179
182
  return graph.states
@@ -184,6 +187,7 @@ const deadEnds = (spec, graph) => {
184
187
  kind: "cannot-reach-goal",
185
188
  action: null,
186
189
  field: null,
190
+ state: s.id,
187
191
  message: `状態「${s.label}」は終端ではないのに、目指す状態(${goalNames})のどれにもたどり着けない。入る手順: ${s.initial ? "(初期状態)" : pathTo(s.id)}`,
188
192
  }));
189
193
  };
@@ -327,14 +327,17 @@ const edgeSvg = (e, markerId) => {
327
327
  // 確かめてほしい点の文は Markdown と共通なので、バッククォートで囲んだ部分を HTML ではコードとして出す。
328
328
  const withCode = (text) => esc(text).replace(/`([^`]+)`/g, "<code>$1</code>");
329
329
  const reviewHtml = (spec, graph) => {
330
- const { pending, acknowledged } = reviewItems(spec, graph);
331
- if (pending.length + acknowledged.length === 0)
330
+ const { pending, onHold, acknowledged } = reviewItems(spec, graph);
331
+ if (pending.length + onHold.length + acknowledged.length === 0)
332
332
  return "";
333
333
  return `<section class="questions review${pending.length === 0 ? " done" : ""}" aria-label="${REVIEW_TITLE}">
334
334
  <h2>${REVIEW_TITLE}</h2>
335
335
  ${pending.length === 0
336
336
  ? '<p class="note">確かめてほしい点はありません。</p>'
337
337
  : `<p class="note">${withCode(REVIEW_LEAD)}</p><ol>${pending.map((i) => `<li><span class="ask">${withCode(i.message)}</span> <code class="review-id">${esc(i.id)}</code></li>`).join("")}</ol>`}
338
+ ${onHold.length === 0
339
+ ? ""
340
+ : `<details class="acknowledged" open><summary>判断待ち(未決事項、${onHold.length} 件)</summary><ul>${onHold.map((i) => `<li>${withCode(i.message)} <code class="review-id">${esc(i.id)}</code><span class="muted"> — 未決事項 ${i.question}「${esc(i.ask)}」</span></li>`).join("")}</ul></details>`}
338
341
  ${acknowledged.length === 0
339
342
  ? ""
340
343
  : `<details class="acknowledged"><summary>確認済み(${acknowledged.length} 件)</summary><ul>${acknowledged.map((i) => `<li><code class="review-id">${esc(i.id)}</code> ${esc(i.note)}</li>`).join("")}</ul></details>`}
@@ -94,14 +94,17 @@ export const renderMarkdown = (spec, graph, objects = [{ spec, graph }]) => {
94
94
  };
95
95
  const ensureLines = (ensures, reviewed) => ensures.length === 0 ? [] : ["- **条件**:", ...ensures.map((e) => ` - ${describeEnsure(e, reviewed[unguardedId(e)] !== undefined)}`)];
96
96
  const reviewSection = (spec, graph) => {
97
- const { pending, acknowledged } = reviewItems(spec, graph);
98
- if (pending.length + acknowledged.length === 0)
97
+ const { pending, onHold, acknowledged } = reviewItems(spec, graph);
98
+ if (pending.length + onHold.length + acknowledged.length === 0)
99
99
  return [];
100
100
  return [
101
101
  `## ${REVIEW_TITLE}`,
102
102
  "",
103
103
  ...(pending.length === 0 ? ["確かめてほしい点はありません。"] : [REVIEW_LEAD, "", ...pending.map((i) => `- ${i.message}(\`${i.id}\`)`)]),
104
104
  "",
105
+ ...(onHold.length === 0
106
+ ? []
107
+ : ["### 判断待ち(未決事項)", "", ...onHold.map((i) => `- ${i.message}(\`${i.id}\`)— 未決事項 ${i.question}「${i.ask}」`), ""]),
105
108
  ...(acknowledged.length === 0 ? [] : ["### 確認済み", "", ...acknowledged.map((i) => `- \`${i.id}\`: ${i.note}`), ""]),
106
109
  ];
107
110
  };
@@ -3,15 +3,29 @@ import type { EnsureCheck, Graph } from "../model.js";
3
3
  export interface ReviewItem {
4
4
  readonly id: string;
5
5
  readonly message: string;
6
+ /** 対象の操作(未決事項の about と照らすのに使う) */
7
+ readonly action: string | null;
8
+ /** 対象の状態の id(未決事項の about と照らすのに使う) */
9
+ readonly state: string | null;
6
10
  }
7
11
  export declare const unguardedId: (e: Pick<EnsureCheck, "action" | "reason">) => string;
8
12
  export declare const REVIEW_TITLE = "\u78BA\u304B\u3081\u3066\u307B\u3057\u3044\u70B9";
9
13
  export declare const REVIEW_LEAD = "spec \u3092\u5B9F\u884C\u3057\u305F\u7D50\u679C\u304B\u3089\u3001\u6761\u4EF6\u306E\u66F8\u304D\u5FD8\u308C\u30FB\u66F8\u304D\u9593\u9055\u3044\u306E\u5019\u88DC\u3092\u6319\u3052\u3066\u3044\u307E\u3059\u3002\u76F4\u3059\u304B\u3001\u610F\u56F3\u3069\u304A\u308A\u306A\u3089 `export const reviewed = { \"ID\": \"\u7406\u7531\" }` \u306B\u66F8\u3044\u3066\u304F\u3060\u3055\u3044\u3002";
10
14
  export declare const reviewItems: (spec: Spec, graph: Graph) => {
11
15
  pending: ReviewItem[];
16
+ onHold: {
17
+ id: string;
18
+ message: string;
19
+ action: string | null;
20
+ state: string | null;
21
+ question: number;
22
+ ask: string;
23
+ }[];
12
24
  acknowledged: {
13
25
  id: string;
14
26
  message: string;
27
+ action: string | null;
28
+ state: string | null;
15
29
  note: string;
16
30
  }[];
17
31
  };
@@ -7,11 +7,19 @@ export const reviewItems = (spec, graph) => {
7
7
  const items = [
8
8
  ...(graph.ensures ?? [])
9
9
  .filter((e) => e.verdict === "unguarded")
10
- .map((e) => ({ id: unguardedId(e), message: `\`${e.action}\` の${describeEnsure(e).replace(/^⚠ /, "")}` })),
11
- ...(graph.omissions ?? []).map((h) => ({ id: h.id, message: h.message })),
10
+ .map((e) => ({ id: unguardedId(e), message: `\`${e.action}\` の${describeEnsure(e).replace(/^⚠ /, "")}`, action: e.action, state: null })),
11
+ ...(graph.omissions ?? []).map((h) => ({ id: h.id, message: h.message, action: h.action, state: h.state ?? null })),
12
12
  ];
13
+ // 未決事項が指している候補を「判断待ち」に分ける理由: 判断がつくまでは意図どおりとも言えず(reviewed には書けない)、
14
+ // かといって残すと check が失敗し続ける。未決事項は人の判断待ちの正常な状態なので、その候補も同じ扱いにする。
15
+ const questionOf = (i) => graph.questions.findIndex((q) => (i.action !== null && q.action === i.action) || (i.state !== null && q.states.includes(i.state)));
16
+ const open = items.filter((i) => spec.reviewed[i.id] === undefined);
13
17
  return {
14
- pending: items.filter((i) => spec.reviewed[i.id] === undefined),
18
+ pending: open.filter((i) => questionOf(i) === -1),
19
+ onHold: open.flatMap((i) => {
20
+ const q = questionOf(i);
21
+ return q === -1 ? [] : [{ ...i, question: q + 1, ask: graph.questions[q].ask }];
22
+ }),
15
23
  acknowledged: items.flatMap((i) => (spec.reviewed[i.id] === undefined ? [] : [{ ...i, note: spec.reviewed[i.id] }])),
16
24
  };
17
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yutaura/live-spec",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "AI が書き、人が見て理解できる「生きた仕様書」を作るツール。素の TypeScript で書いた仕様を実行し、状態遷移図・画面・できる/できない理由・検査結果をまとめた仕様書を生成する",
@@ -192,7 +192,7 @@ live-spec は spec を実行した結果から、条件の書き忘れ・書き
192
192
  1. **要件・コードと照らして、本当に漏れか**を確かめる。漏れなら spec を直す
193
193
  2. 意図どおりなら、**その意図をもう 1 か所に書けないか**を考える。状態についての性質なら不変条件、入力や「誰が」についてなら `denied` の手順を持つシナリオ(`unguarded` はこれで消える)
194
194
  3. それでも意図どおりで、書き足す意味のある性質がないなら、`export const reviewed = { "ID": "理由" }` に理由を書く。仕様書では「確認済み」として理由つきで残る
195
- 4. 判断できないなら、未決事項(`questions`)にする
195
+ 4. 判断できないなら、未決事項(`questions`)にする。`about` でその候補の状態か操作を指すと、候補は「判断待ち(未決事項)」に移り、`live-spec check` の失敗にも数えなくなる。未決事項を消す(判断する)と、また候補として出る
196
196
 
197
197
  候補を消すためだけに、中身のない不変条件やシナリオを書かない。`reviewed` の理由も「意図どおり」だけで済ませず、なぜそれでよいかを要件の言葉で書く(「カードはどの列からでも動かせる。アーカイブも API では動かせる(未決事項 6)」)。`reviewed` の ID が候補のどれにも当たらなくなると検査結果に出るので、そのときは消す。
198
198
 
@@ -32,7 +32,7 @@ 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 は根拠のファイルが無いとき・行がファイルの範囲の外にあるときに検査結果に出すので、コードが移動したり行がずれたりしたら、根拠を直す。
35
+ 1 つの条件の根拠が複数の場所にあるときは、「、」で並べてよい(`a.ts:139-146、b.ts:578-585`)。同じファイルの別の行は `:877-879` と省略できる。補足は括弧()に入れる。根拠のパスは、spec のある git リポジトリのルートからの相対で書く。spec を対象のリポジトリの外(別のリポジトリなど)に置くときは、`export const evidenceRoot = "../my-app"`(spec のファイルからの相対)で起点を指定する。live-spec は根拠のファイルが無いとき・行がファイルの範囲の外にあるときに検査結果に出すので、コードが移動したり行がずれたりしたら、根拠を直す。
36
36
 
37
37
  ```ts
38
38
  /** 注文を確定する */