sdd-forge 0.1.0-alpha.23 → 0.1.0-alpha.25

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.
@@ -2,26 +2,260 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- <!-- {{text: Describe the purpose of this chapter in 1–2 sentences. Cover the project structure, module dependency direction, and key processing flows.}} -->
5
+ <!-- {{text: Describe the purpose of this chapter in 1–2 sentences. Cover the project structure, direction of module dependencies, and key processing flows.}} -->
6
+
7
+ This chapter describes the internal architecture of sdd-forge, including its directory layout, module responsibilities, and the direction of dependencies across the three-layer dispatch structure. It also covers the key processing flows that connect the CLI entry point through to AI invocation and file output.
6
8
 
7
9
  ## Contents
8
10
 
9
11
  ### Project Structure
10
12
 
11
- <!-- {{text: Describe the directory structure of this project in a tree-format code block. Include role comments for major directories and files. Cover the dispatchers directly under src/ (sdd-forge.js, docs.js, spec.js, flow.js), docs/commands/ (subcommand implementations), docs/lib/ (document generation library), lib/ (shared utilities), presets/ (preset definitions), and templates/ (bundled templates).}} -->
13
+ <!-- {{text: Describe the directory structure of this project in a tree-format code block. Include role comments for major directories and files. Cover the top-level dispatchers under src/ (sdd-forge.js, docs.js, spec.js, flow.js), docs/commands/ (subcommand implementations), docs/lib/ (document generation libraries), lib/ (shared utilities), presets/ (preset definitions), and templates/ (bundled templates).}} -->
14
+
15
+ ```
16
+ sdd-forge/
17
+ ├── package.json ← Package manifest; bin entry: ./src/sdd-forge.js
18
+ └── src/
19
+ ├── sdd-forge.js ← Top-level CLI entry point; routes to dispatchers
20
+ ├── docs.js ← Dispatcher for all docs subcommands
21
+ ├── spec.js ← Dispatcher for spec/gate subcommands
22
+ ├── flow.js ← SDD flow automation (DIRECT_COMMAND)
23
+ ├── presets-cmd.js ← Presets listing command (DIRECT_COMMAND)
24
+ ├── help.js ← Help text output
25
+ ├── docs/
26
+ │ ├── commands/ ← Subcommand implementations
27
+ │ │ ├── scan.js ← Source analysis → analysis.json / summary.json
28
+ │ │ ├── init.js ← Initialize docs/ from templates
29
+ │ │ ├── data.js ← Resolve {{data}} directives
30
+ │ │ ├── text.js ← Resolve {{text}} directives via AI
31
+ │ │ ├── readme.js ← Auto-generate README.md
32
+ │ │ ├── forge.js ← Iterative docs improvement via AI
33
+ │ │ ├── review.js ← Docs quality check via AI
34
+ │ │ ├── agents.js ← Regenerate AGENTS.md sections
35
+ │ │ ├── changelog.js ← Generate change_log.md from specs/
36
+ │ │ ├── setup.js ← Project registration + config generation
37
+ │ │ └── ... ← upgrade, translate, default-project, etc.
38
+ │ └── lib/ ← Document generation library modules
39
+ │ ├── scanner.js ← File discovery and PHP/JS/YAML parsing
40
+ │ ├── directive-parser.js ← Parse {{data}} / {{text}} / @block / @extends
41
+ │ ├── template-merger.js ← Resolve template inheritance (@extends / @block)
42
+ │ ├── data-source.js ← DataSource base class
43
+ │ ├── data-source-loader.js ← Dynamic DataSource loader per preset
44
+ │ ├── resolver-factory.js ← createResolver() factory for data command
45
+ │ ├── forge-prompts.js ← Prompt builders for forge/agents; summaryToText()
46
+ │ ├── text-prompts.js ← Prompt builders for text command
47
+ │ ├── review-parser.js ← Parse AI review output
48
+ │ ├── renderers.js ← Markdown rendering utilities
49
+ │ ├── scan-source.js ← Scan configuration loader
50
+ │ ├── concurrency.js ← Parallel file processing utilities
51
+ │ └── ... ← composer-utils.js, php-array-parser.js
52
+ ├── specs/
53
+ │ └── commands/
54
+ │ ├── init.js ← spec.md creation + feature branch setup
55
+ │ └── gate.js ← Spec gate check (pre/post phase)
56
+ ├── lib/ ← Shared utilities used across all layers
57
+ │ ├── agent.js ← AI agent invocation (sync/async)
58
+ │ ├── cli.js ← Argument parsing, path resolution utilities
59
+ │ ├── config.js ← Config loading, path utilities (.sdd-forge/)
60
+ │ ├── flow-state.js ← SDD flow state management (current-spec)
61
+ │ ├── presets.js ← Preset auto-discovery and lookup
62
+ │ ├── i18n.js ← Internationalization utilities
63
+ │ ├── types.js ← Type aliases and project type definitions
64
+ │ └── ... ← agents-md.js, process.js, projects.js, entrypoint.js
65
+ ├── presets/ ← Preset definitions per project type
66
+ │ ├── base/ ← Base preset (arch layer)
67
+ │ │ ├── preset.json
68
+ │ │ └── templates/ ← Doc templates (ja/ and en/)
69
+ │ ├── webapp/, cli/, library/ ← Architecture-layer presets
70
+ │ ├── cakephp2/, laravel/, symfony/ ← Framework-specific presets
71
+ │ └── node-cli/ ← Node.js CLI preset
72
+ └── templates/ ← Bundled templates (config.example.json, review-checklist.md, skills/)
73
+ ```
12
74
 
13
75
  ### Module Overview
14
76
 
15
77
  <!-- {{text: List the major modules in a table format. Include module name, file path, and responsibility. Cover the dispatcher layer (sdd-forge.js, docs.js, spec.js), command layer (docs/commands/*.js, specs/commands/*.js), library layer (lib/agent.js, lib/cli.js, lib/config.js, lib/flow-state.js, lib/presets.js, lib/i18n.js), and document generation layer (docs/lib/scanner.js, directive-parser.js, template-merger.js, forge-prompts.js, text-prompts.js, review-parser.js, data-source.js, resolver-factory.js).}} -->
16
78
 
79
+ | Layer | Module | File Path | Responsibility |
80
+ |---|---|---|---|
81
+ | **Dispatcher** | CLI Entry Point | `src/sdd-forge.js` | Parses top-level subcommand; resolves project context via env vars; routes to dispatcher or direct command |
82
+ | **Dispatcher** | Docs Dispatcher | `src/docs.js` | Routes all docs-related subcommands (`build`, `scan`, `init`, `data`, `text`, `readme`, `forge`, `review`, `agents`, `changelog`, `setup`, etc.) |
83
+ | **Dispatcher** | Spec Dispatcher | `src/spec.js` | Routes `spec` and `gate` subcommands |
84
+ | **Command** | scan | `src/docs/commands/scan.js` | Analyzes source files; writes `analysis.json` and `summary.json` |
85
+ | **Command** | data | `src/docs/commands/data.js` | Resolves `{{data}}` directives in docs using analysis data |
86
+ | **Command** | text | `src/docs/commands/text.js` | Resolves `{{text}}` directives by invoking an AI agent |
87
+ | **Command** | forge | `src/docs/commands/forge.js` | Iteratively improves existing docs content via AI |
88
+ | **Command** | review | `src/docs/commands/review.js` | Runs AI-based quality check against the review checklist |
89
+ | **Command** | readme | `src/docs/commands/readme.js` | Auto-generates `README.md` from docs and analysis data |
90
+ | **Command** | agents | `src/docs/commands/agents.js` | Regenerates SDD and PROJECT sections of `AGENTS.md` |
91
+ | **Command** | spec init | `src/specs/commands/init.js` | Creates a new spec file and feature branch |
92
+ | **Command** | gate | `src/specs/commands/gate.js` | Validates a spec file against gate criteria (pre/post phase) |
93
+ | **Library** | agent | `src/lib/agent.js` | Synchronous and asynchronous AI agent invocation; prompt injection; timeout management |
94
+ | **Library** | cli | `src/lib/cli.js` | `parseArgs()`, `repoRoot()`, `sourceRoot()`, `isInsideWorktree()`, `PKG_DIR` constant |
95
+ | **Library** | config | `src/lib/config.js` | Loads and validates `.sdd-forge/config.json`; manages `.sdd-forge/` path helpers |
96
+ | **Library** | flow-state | `src/lib/flow-state.js` | Reads and writes `.sdd-forge/current-spec` to track SDD flow progress |
97
+ | **Library** | presets | `src/lib/presets.js` | Auto-discovers `preset.json` files under `src/presets/`; exposes `PRESETS` constant and lookup helpers |
98
+ | **Library** | i18n | `src/lib/i18n.js` | Locale string loading and translation utilities |
99
+ | **Doc Gen** | scanner | `src/docs/lib/scanner.js` | File discovery; PHP/JS/YAML parsing utilities; `genericScan()` |
100
+ | **Doc Gen** | directive-parser | `src/docs/lib/directive-parser.js` | Parses `{{data}}`, `{{text}}`, `@block`, and `@extends` directives from markdown files |
101
+ | **Doc Gen** | template-merger | `src/docs/lib/template-merger.js` | Resolves template inheritance (`@extends` / `@block`) before directive processing |
102
+ | **Doc Gen** | forge-prompts | `src/docs/lib/forge-prompts.js` | Builds prompts for the `forge` and `agents` commands; provides `summaryToText()` |
103
+ | **Doc Gen** | text-prompts | `src/docs/lib/text-prompts.js` | Builds per-directive prompts for the `text` command |
104
+ | **Doc Gen** | review-parser | `src/docs/lib/review-parser.js` | Parses structured AI output from the `review` command into pass/fail results |
105
+ | **Doc Gen** | data-source | `src/docs/lib/data-source.js` | Base class for all `{{data}}` resolver implementations |
106
+ | **Doc Gen** | resolver-factory | `src/docs/lib/resolver-factory.js` | `createResolver()` factory that instantiates the correct DataSource for a given preset |
107
+
17
108
  ### Module Dependencies
18
109
 
19
- <!-- {{text: Generate a mermaid graph showing inter-module dependencies. Reflect the three-layer dispatch structure and show the dependency direction from dispatcher → command → library. Output only the mermaid code block.}} -->
110
+ <!-- {{text: Generate a mermaid graph showing the dependencies between modules. Reflect the three-layer dispatch structure and show the dependency direction from dispatcher → command → library. Output only the mermaid code block.}} -->
111
+
112
+ ```mermaid
113
+ graph TD
114
+ CLI["sdd-forge.js\n(Entry Point)"]
115
+
116
+ subgraph Dispatchers
117
+ DOCS["docs.js"]
118
+ SPEC["spec.js"]
119
+ FLOW["flow.js"]
120
+ end
121
+
122
+ subgraph Commands_Docs["docs/commands/"]
123
+ SCAN["scan.js"]
124
+ DATA["data.js"]
125
+ TEXT["text.js"]
126
+ FORGE["forge.js"]
127
+ REVIEW["review.js"]
128
+ README["readme.js"]
129
+ AGENTS["agents.js"]
130
+ INIT_D["init.js"]
131
+ end
132
+
133
+ subgraph Commands_Spec["specs/commands/"]
134
+ SPEC_INIT["init.js"]
135
+ GATE["gate.js"]
136
+ end
137
+
138
+ subgraph Lib["lib/"]
139
+ AGENT["agent.js"]
140
+ CLI_LIB["cli.js"]
141
+ CONFIG["config.js"]
142
+ FLOW_STATE["flow-state.js"]
143
+ PRESETS["presets.js"]
144
+ I18N["i18n.js"]
145
+ end
146
+
147
+ subgraph DocLib["docs/lib/"]
148
+ SCANNER["scanner.js"]
149
+ DIRECTIVE["directive-parser.js"]
150
+ MERGER["template-merger.js"]
151
+ FORGE_P["forge-prompts.js"]
152
+ TEXT_P["text-prompts.js"]
153
+ REVIEW_P["review-parser.js"]
154
+ DS["data-source.js"]
155
+ RESOLVER["resolver-factory.js"]
156
+ end
157
+
158
+ CLI --> DOCS
159
+ CLI --> SPEC
160
+ CLI --> FLOW
161
+
162
+ DOCS --> SCAN
163
+ DOCS --> DATA
164
+ DOCS --> TEXT
165
+ DOCS --> FORGE
166
+ DOCS --> REVIEW
167
+ DOCS --> README
168
+ DOCS --> AGENTS
169
+ DOCS --> INIT_D
170
+
171
+ SPEC --> SPEC_INIT
172
+ SPEC --> GATE
173
+
174
+ SCAN --> SCANNER
175
+ DATA --> DIRECTIVE
176
+ DATA --> RESOLVER
177
+ DATA --> DS
178
+ TEXT --> DIRECTIVE
179
+ TEXT --> TEXT_P
180
+ TEXT --> AGENT
181
+ FORGE --> FORGE_P
182
+ FORGE --> AGENT
183
+ REVIEW --> REVIEW_P
184
+ AGENTS --> FORGE_P
185
+ AGENTS --> AGENT
186
+
187
+ RESOLVER --> DS
188
+ TEXT_P --> CONFIG
189
+ FORGE_P --> CONFIG
190
+ SPEC_INIT --> FLOW_STATE
191
+ GATE --> FLOW_STATE
192
+ FLOW --> FLOW_STATE
193
+
194
+ SCAN --> CLI_LIB
195
+ DATA --> CLI_LIB
196
+ TEXT --> CLI_LIB
197
+ FORGE --> CLI_LIB
198
+ REVIEW --> CLI_LIB
199
+
200
+ CLI_LIB --> CONFIG
201
+ AGENT --> CONFIG
202
+ PRESETS --> CLI_LIB
203
+ ```
20
204
 
21
205
  ### Key Processing Flows
22
206
 
23
- <!-- {{text: Explain the data and control flow between modules when a representative command (build or forge) is executed, using numbered steps. Include the sequence: entry point → dispatch → config loading → analysis data preparation → AI invocation → file writing.}} -->
207
+ <!-- {{text: Explain the inter-module data and control flow when a representative command (build or forge) is executed, using numbered steps. Include the flow from entry point → dispatch → config loading → analysis data preparation → AI invocation → file writing.}} -->
208
+
209
+ The following steps describe the control and data flow for the `sdd-forge forge` command, which represents a complete end-to-end AI-assisted documentation update.
210
+
211
+ 1. **Entry point** — `sdd-forge.js` receives `forge` as the subcommand. It resolves the project context (from `--project`, `.sdd-forge/projects.json`, or the current directory), sets `SDD_SOURCE_ROOT` and `SDD_WORK_ROOT` environment variables, and forwards control to `docs.js`.
212
+ 2. **Dispatch** — `docs.js` matches `forge` and dynamically imports `docs/commands/forge.js`, passing the parsed arguments.
213
+ 3. **Config loading** — `forge.js` calls `loadConfig()` from `lib/config.js` to read `.sdd-forge/config.json`. The active language, AI agent settings, and document style options are extracted from the config.
214
+ 4. **Analysis data preparation** — `forge.js` reads `.sdd-forge/output/summary.json` (falling back to `analysis.json` if absent). The `summaryToText()` function from `docs/lib/forge-prompts.js` converts the JSON structure into a condensed text representation suitable for inclusion in an AI prompt.
215
+ 5. **Prompt construction** — `forge-prompts.js` assembles the full system prompt and user prompt, incorporating the analysis summary, the existing docs content, and any `documentStyle` configuration (tone, purpose, custom instructions).
216
+ 6. **AI invocation** — `lib/agent.js` is called with the constructed prompt. `callAgentAsync()` spawns the configured AI agent process (via `spawn` + `stdin: "ignore"`) and streams the response back through a callback.
217
+ 7. **Response parsing** — The AI-generated output is parsed and validated. For the `forge` command, the response contains updated markdown content for one or more docs files.
218
+ 8. **File writing** — Updated content is written back to the appropriate files under `docs/`. Directive markers (`{{data}}` / `{{text}}` blocks) in the output are preserved so that future automated runs can re-process them.
219
+ 9. **Review (optional)** — After `forge`, the SDD workflow recommends running `sdd-forge review`, which follows a similar flow but uses `review-parser.js` to parse structured pass/fail results from the AI response.
220
+
221
+ For the `build` command, the same pipeline is orchestrated sequentially: `scan` → `init` → `data` → `text` → `readme`, with each step producing output consumed by the next.
24
222
 
25
223
  ### Extension Points
26
224
 
27
- <!-- {{text: Explain what needs to change and the extension patterns when adding new commands or features. Cover each of the following: (1) adding a new docs subcommand, (2) adding a new spec subcommand, (3) adding a new preset, (4) adding a new DataSource ({{data}} resolver), and (5) adding a new AI prompt.}} -->
225
+ <!-- {{text: Explain where changes are needed and the extension patterns when adding new commands or features. Provide steps for each of the following: (1) adding a new docs subcommand, (2) adding a new spec subcommand, (3) adding a new preset, (4) adding a new DataSource ({{data}} resolver), and (5) adding a new AI prompt.}} -->
226
+
227
+ **1. Adding a new docs subcommand**
228
+
229
+ 1. Create `src/docs/commands/<name>.js`. Export a `main(args)` function (or call `main()` at the bottom for direct-run support).
230
+ 2. Open `src/docs.js` and add a case for the new subcommand name in the dispatch map, pointing to the new file path.
231
+ 3. Add an entry to the command table in `src/help.js` so the new command appears in `sdd-forge help` output.
232
+ 4. Add corresponding test files under `tests/docs/commands/`.
233
+
234
+ **2. Adding a new spec subcommand**
235
+
236
+ 1. Create `src/specs/commands/<name>.js` with a `main(args)` function.
237
+ 2. Register the new subcommand in `src/spec.js` by adding a dispatch case.
238
+ 3. Update `src/help.js` to include the new command description.
239
+ 4. Add tests under `tests/specs/commands/`.
240
+
241
+ **3. Adding a new preset**
242
+
243
+ 1. Create a directory under `src/presets/<key>/` containing a `preset.json` file. Set the required fields: `type`, `arch`, and `chapters` (the list of doc template files to include).
244
+ 2. Place doc templates under `src/presets/<key>/templates/{ja,en}/` using the filenames declared in `chapters`.
245
+ 3. If the preset requires custom source scanning logic, add an analyzer module and reference it from `preset.json`. Implement scan logic following the pattern in existing presets (e.g., `cakephp2`).
246
+ 4. The preset is automatically discovered by `src/lib/presets.js` via recursive `preset.json` discovery — no manual registration is required.
247
+ 5. Add type aliases in `src/lib/types.js` if the preset should be reachable by shorthand names.
248
+
249
+ **4. Adding a new DataSource (`{{data}}` resolver)**
250
+
251
+ 1. Create a class that extends `DataSource` from `src/docs/lib/data-source.js`. Implement the required `resolve(key, args)` method to return the data value for a given directive key.
252
+ 2. Register the new DataSource in `src/docs/lib/data-source-loader.js` by mapping it to the appropriate preset type(s).
253
+ 3. Ensure `resolver-factory.js` can instantiate the DataSource via `createResolver()` — add a case if the factory uses explicit type matching.
254
+ 4. Write tests that verify the resolved output for representative directive keys.
255
+
256
+ **5. Adding a new AI prompt**
257
+
258
+ 1. Determine which command the prompt belongs to. For docs improvement prompts, add a builder function in `src/docs/lib/forge-prompts.js`. For `{{text}}` directive prompts, add to `src/docs/lib/text-prompts.js`.
259
+ 2. The prompt builder should accept the relevant config and analysis data as arguments and return a `{ system, user }` object.
260
+ 3. Invoke the prompt builder from the corresponding command file (`forge.js`, `text.js`, etc.) and pass the result to `callAgent()` or `callAgentAsync()` in `lib/agent.js`.
261
+ 4. If the prompt requires a custom system prompt file (for `--system-prompt-file`), ensure `ensureAgentWorkDir()` is called and the temporary file is cleaned up after invocation.
@@ -0,0 +1,114 @@
1
+ # 01. システム概要
2
+
3
+ ## 説明
4
+
5
+ <!-- {{text: Write a 1-2 sentence overview of this chapter. Include the project's architecture and whether it integrates with external systems.}} -->
6
+
7
+ 本章では、ソースコード解析によるドキュメント自動生成と、Spec-Driven Development(SDD)ワークフローによる機能開発を推進する Node.js CLI ツール「sdd-forge」の構造概要を説明する。本ツールは3層のコマンドディスパッチアーキテクチャを採用し、ドキュメントテキストの生成・改善のために外部 AI エージェント(Claude CLI など)と連携する。
8
+
9
+ ## 内容
10
+
11
+ ### アーキテクチャ図
12
+
13
+ <!-- {{text: Generate a mermaid flowchart showing the project architecture. Include data flows between major components. Output only the mermaid code block.}} -->
14
+
15
+ ```mermaid
16
+ flowchart TD
17
+ User["ユーザー (CLI)"]
18
+
19
+ subgraph Entry["エントリ層"]
20
+ SDF["sdd-forge.js\n(エントリポイント)"]
21
+ end
22
+
23
+ subgraph Dispatch["ディスパッチャー層"]
24
+ DOCS["docs.js"]
25
+ SPEC["spec.js"]
26
+ FLOW["flow.js"]
27
+ HELP["help.js"]
28
+ end
29
+
30
+ subgraph Commands["コマンド層"]
31
+ SCAN["scan"]
32
+ DATA["data"]
33
+ TEXT["text"]
34
+ FORGE["forge"]
35
+ REVIEW["review"]
36
+ SPEC_INIT["spec init"]
37
+ GATE["gate"]
38
+ OTHER["readme / agents / changelog / …"]
39
+ end
40
+
41
+ subgraph Lib["コアライブラリ"]
42
+ AGENT["agent.js\n(AI エージェント呼び出し)"]
43
+ CONFIG["config.js\n(設定・パス管理)"]
44
+ SCANNER["scanner.js\n(ソース解析)"]
45
+ PARSER["directive-parser.js"]
46
+ end
47
+
48
+ subgraph Storage["ファイルシステム"]
49
+ SRC["ソースコード\n(プロジェクトルート)"]
50
+ ANALYSIS[".sdd-forge/output/\nanalysis.json / summary.json"]
51
+ DOCFILES["docs/*.md\n(生成ドキュメント)"]
52
+ SPECS["specs/NNN-xxx/spec.md"]
53
+ CFGFILE[".sdd-forge/config.json"]
54
+ end
55
+
56
+ AI["外部 AI エージェント\n(例: Claude CLI)"]
57
+
58
+ User --> SDF
59
+ SDF --> DOCS & SPEC & FLOW & HELP
60
+ DOCS --> SCAN & DATA & TEXT & FORGE & REVIEW & OTHER
61
+ SPEC --> SPEC_INIT & GATE
62
+ SCAN --> SCANNER --> SRC
63
+ SCANNER --> ANALYSIS
64
+ DATA --> PARSER --> DOCFILES
65
+ TEXT --> AGENT --> AI
66
+ FORGE --> AGENT
67
+ REVIEW --> AGENT
68
+ AGENT --> DOCFILES
69
+ CONFIG --> CFGFILE
70
+ SPEC_INIT --> SPECS
71
+ GATE --> SPECS
72
+ ```
73
+
74
+ ### コンポーネントの責務
75
+
76
+ <!-- {{text: Describe the major components with their location, responsibilities, and I/O in table format.}} -->
77
+
78
+ | コンポーネント | 場所 | 責務 | 入力 | 出力 |
79
+ |---|---|---|---|---|
80
+ | CLI エントリポイント | `src/sdd-forge.js` | プロジェクトコンテキストを解決し、サブコマンドを適切なディスパッチャーへルーティングする | CLI 引数、環境変数(`SDD_SOURCE_ROOT`、`SDD_WORK_ROOT`) | ディスパッチされたコマンド実行 |
81
+ | ドキュメントディスパッチャー | `src/docs.js` | `build`、`scan`、`init`、`data`、`text`、`forge`、`review` および関連サブコマンドをルーティングする | サブコマンド名+フラグ | `docs/commands/*.js` へ委譲 |
82
+ | スペックディスパッチャー | `src/spec.js` | `spec` および `gate` サブコマンドをルーティングする | サブコマンド名+フラグ | `specs/commands/*.js` へ委譲 |
83
+ | SDD フローランナー | `src/flow.js` | SDD サイクル全体をエンドツーエンドで自動実行する | `--request` プロンプト文字列 | spec → gate → 実装 → forge → review のオーケストレーション |
84
+ | ソーススキャナー | `src/docs/lib/scanner.js` | プロジェクトのソースファイルを解析し、モジュール・ルート・構造を抽出する | ソースルートディレクトリ | `.sdd-forge/output/analysis.json`、`summary.json` |
85
+ | ディレクティブパーサー | `src/docs/lib/directive-parser.js` | Markdown テンプレート内の `{{data}}` および `{{text}}` ディレクティブを解析する | `.md` テンプレートファイル | リゾルバー向けディレクティブ AST |
86
+ | データリゾルバー | `src/docs/lib/resolver-factory.js` | 構造化された解析データを `{{data}}` ディレクティブに注入する | `analysis.json`、ディレクティブ AST | 解決済み Markdown セクション |
87
+ | AI エージェント呼び出し | `src/lib/agent.js` | 外部 AI エージェントプロセスを同期または非同期で呼び出す | プロンプト文字列、エージェント設定 | AI 生成テキスト(stdout) |
88
+ | 設定マネージャー | `src/lib/config.js` | `.sdd-forge/config.json` を読み込みバリデーションし、標準パスを解決する | `.sdd-forge/` ディレクトリ | 型付き設定オブジェクト、ファイルパス |
89
+ | スペックゲート | `src/specs/commands/gate.js` | 実装前後のチェックリストに基づいてスペックを検証する | `spec.md` パス、`--phase` フラグ | PASS / FAIL レポート |
90
+ | フォージエンジン | `src/docs/commands/forge.js` | AI エージェントへのプロンプトにより `docs/` を反復改善する | 変更内容サマリープロンプト、`spec.md` パス | 更新された `docs/*.md` ファイル |
91
+
92
+ ### 外部連携
93
+
94
+ <!-- {{text: If there are external system integrations, describe their purpose and connection method in table format.}} -->
95
+
96
+ | システム | 目的 | 接続方法 | 設定 |
97
+ |---|---|---|---|
98
+ | AI エージェント(例: Claude CLI) | `{{text}}` ディレクティブのドキュメントテキスト生成・改善、および `forge`、`review`、`agents`、`text` コマンドの実行を担う | `src/lib/agent.js` 内で `execFileSync`(同期)または `spawn`(非同期ストリーミング)によって子プロセスとして起動される | `.sdd-forge/config.json` の `providers` に定義し、`defaultAgent` キーで選択する |
99
+
100
+ AI エージェントが唯一の外部連携である。ファイルスキャン、ディレクティブ解析、テンプレートマージ、スペック管理など、その他の処理はすべて Node.js 組み込みモジュール(`fs`、`path`、`child_process`、`os`)のみに依存する。エージェントのコマンド、引数、タイムアウト、システムプロンプトの渡し方(`--system-prompt` または `--system-prompt-file`)はプロジェクトごとに設定可能である。
101
+
102
+ ### 環境別の差異
103
+
104
+ <!-- {{text: Describe the configuration differences across environments (local/staging/production).}} -->
105
+
106
+ sdd-forge はローカル CLI ツールであるため、従来の意味での独立したデプロイ環境は存在しない。環境による設定の違いは、プロジェクトごとの `.sdd-forge/config.json` 設定と環境変数によって表現される。
107
+
108
+ | 観点 | ローカル開発 | CI / 自動化パイプライン | マルチプロジェクト構成 |
109
+ |---|---|---|---|
110
+ | プロジェクト解決 | インタラクティブ;`--project` フラグまたは `.sdd-forge/projects.json` の `default` キーを使用 | ジョブごとに `SDD_SOURCE_ROOT` と `SDD_WORK_ROOT` 環境変数を明示的に設定 | `.sdd-forge/projects.json` に名前付きプロジェクトを登録し、`--project <name>` でターゲットを選択 |
111
+ | AI エージェント | `config.json` の `providers` + `defaultAgent` で設定;インタラクティブに動作 | 同設定を使用;非 TTY 環境でのハングを防ぐため `stdin: "ignore"` を利用(`callAgentAsync`) | プロジェクトごとに独立したエージェント設定を持つ `config.json` を保持 |
112
+ | 言語 / 出力 | `lang` と `output.languages` で CLI 言語とドキュメント出力ロケールを制御 | 同設定値が適用される;個別のオーバーライド機構はなし | プロジェクトごとの `config.json` により言語を個別に設定可能 |
113
+ | 並列処理 | `limits.concurrency`(デフォルト: 5)でファイル並列処理数を制御 | 高コアマシンでは値を上げて高速化可能 | プロジェクトごとに独立して調整可能 |
114
+ | タイムアウト | `limits.designTimeoutMs` / プロバイダーごとの `timeoutMs` | 低速な CI エージェントでは値を増やすべき;デフォルト定数: 120 秒 / 180 秒 / 300 秒 | プロジェクトごとに設定可能 |
@@ -0,0 +1,270 @@
1
+ # 02. CLIコマンドリファレンス
2
+
3
+ ## 説明
4
+
5
+ <!-- {{text: Write a 1–2 sentence overview of this chapter. Cover the total number of commands, whether global options exist, and the subcommand structure.}} -->
6
+
7
+ 本章では `sdd-forge` で利用可能な全18サブコマンドを解説します。ドキュメント生成コマンド(`docs.js` 経由でルーティング)、specワークフローコマンド(`spec.js` 経由でルーティング)、および直接実行コマンド(`flow`、`presets`)に分類されます。すべてのコマンドは共通のグローバルオプションを持ち、ほとんどのコマンドは実行前にプロジェクトコンテキストの解決が必要です。
8
+
9
+ ## 目次
10
+
11
+ ### コマンド一覧
12
+
13
+ <!-- {{text: List all commands in a table format. Include command name, description, and main options. Commands can be identified from the modules list in the analysis data. Include both commands routed through dispatchers (docs.js, spec.js) and directly executed commands (flow, presets). Note that build is a composite command (scan → init → data → text → readme → agents → translate pipeline). Be sure to include translate (multilingual translation), upgrade (template update), and default (default project configuration).}} -->
14
+
15
+ | コマンド | 説明 | 主なオプション |
16
+ |---|---|---|
17
+ | `build` | ドキュメント生成パイプライン全体(`scan → init → data → text → readme → agents → translate`)を一括実行する | `--project` |
18
+ | `scan` | ソースコードを解析し、`analysis.json` と `summary.json` を生成する | `--project` |
19
+ | `init` | プリセットテンプレートから `docs/` を初期化する | `--project` |
20
+ | `data` | 解析出力を使用して `{{data: ...}}` ディレクティブを解決する | `--project` |
21
+ | `text` | 設定済みAIエージェントを使用して `{{text: ...}}` ディレクティブを解決する | `--file`、`--agent`、`--project` |
22
+ | `readme` | docsの内容をもとにAIで `README.md` を自動生成する | `--project` |
23
+ | `forge` | プロンプトに基づき、AIを使ってdocsを反復的に改善する | `--prompt`、`--spec`、`--project` |
24
+ | `review` | docsのAI品質チェックを実行し、PASS/FAILを報告する | `--project` |
25
+ | `changelog` | `specs/` 配下の全specファイルから `change_log.md` を生成する | `--project` |
26
+ | `agents` | 最新のSDDテンプレートとPROJECTセクションで `AGENTS.md` を更新する | `--sdd`、`--project-only`、`--dry-run` |
27
+ | `translate` | configで定義された追加出力言語へdocsを翻訳する | `--lang`、`--force`、`--dry-run` |
28
+ | `upgrade` | インストール済みパッケージバージョンに合わせてSDDマネージドテンプレートセクションを更新する | `--project` |
29
+ | `setup` | プロジェクトを登録し、初期 `.sdd-forge/config.json` を生成する | — |
30
+ | `default` | `projects.json` のデフォルトプロジェクトを設定または表示する | — |
31
+ | `spec` | 新しいspecファイルを作成し、オプションでfeatureブランチを作成する | `--title`、`--no-branch` |
32
+ | `gate` | 実装前または実装後にspecを受入基準に照らして検証する | `--spec`、`--phase` |
33
+ | `flow` | 自然言語のリクエストからSDDフロー全体を自動実行する | `--request` |
34
+ | `presets` | バンドル済みのプロジェクトタイププリセット一覧を表示する | — |
35
+ | `help` | 利用可能なサブコマンドと説明を表示する | `-h`、`--help` |
36
+
37
+ ### グローバルオプション
38
+
39
+ <!-- {{text: List global options common to all commands in a table format. Include --project, --help/-h, and --version/-v/-V. Also note that setup, default, help, and presets skip project context resolution.}} -->
40
+
41
+ | オプション | エイリアス | 説明 |
42
+ |---|---|---|
43
+ | `--project <name>` | — | `.sdd-forge/projects.json` に登録されたプロジェクト名で対象プロジェクトを指定する。省略した場合は `projects.json` の `default` に設定されたプロジェクトが使用される。 |
44
+ | `--help` | `-h` | 指定したサブコマンドのヘルプテキストを表示して終了する。 |
45
+ | `--version` | `-v`、`-V` | `package.json` から現在の `sdd-forge` バージョンを表示して終了する。 |
46
+
47
+ > **注意:** `setup`、`default`、`help`、`presets` コマンドはプロジェクトコンテキストの解決を完全にスキップします。これらのコマンドは登録済みプロジェクトを必要とせず、`--project` フラグを無視します。
48
+
49
+ ### コマンド詳細
50
+
51
+ <!-- {{text: Describe the usage, options, and examples for each command in detail. Create a #### subsection for each command. For the build pipeline, list all steps: scan → init → data → text → readme → agents → translate. The translate command has --lang, --force, and --dry-run options.}} -->
52
+
53
+ 以下の各コマンドについて、目的・利用可能なオプション・代表的な使用例を解説します。AIを使って生成するコマンドには、`.sdd-forge/config.json` に有効な `defaultAgent` または `providers` エントリが必要です。ディスクへの書き込みを行うコマンドは、特記がない限り解決済みプロジェクトの `docs/` ディレクトリ内で動作します。
54
+
55
+ #### build
56
+
57
+ ドキュメント生成パイプライン全体を一括実行します。パイプラインは `scan → init → data → text → readme → agents → translate` の順で各ステージを実行します。プロジェクトのdocs初回作成や完全な再生成の標準的なエントリポイントです。
58
+
59
+ ```bash
60
+ sdd-forge build
61
+ sdd-forge build --project myproject
62
+ ```
63
+
64
+ #### scan
65
+
66
+ 設定済みプリセット(例: `cli/node-cli`、`webapp/cakephp2`)に従ってプロジェクトのソースコードを解析し、`.sdd-forge/output/analysis.json` と `.sdd-forge/output/summary.json` に結果を書き込みます。`summary.json` はAI向けの軽量版です。
67
+
68
+ ```bash
69
+ sdd-forge scan
70
+ ```
71
+
72
+ #### init
73
+
74
+ プリセットテンプレートをコピーして `docs/` ディレクトリ構造を初期化します。テンプレートは `src/presets/{preset}/templates/{lang}/` から解決されます。強制上書きを指定しない限り、既存ファイルは上書きされません。
75
+
76
+ ```bash
77
+ sdd-forge init
78
+ ```
79
+
80
+ #### data
81
+
82
+ `docs/` ファイル内のすべての `{{data: ...}}` ディレクティブを解決し、`analysis.json` から抽出した構造化データに置き換えます。テンプレート境界の外にあるディレクティブはそのまま保持されます。
83
+
84
+ ```bash
85
+ sdd-forge data
86
+ ```
87
+
88
+ #### text
89
+
90
+ 設定済みAIエージェントを使用して `docs/` ファイル内の `{{text: ...}}` ディレクティブを解決します。`limits.concurrency` の設定に従ってファイルを並列処理します。特定のファイルのみを対象にすることもできます。
91
+
92
+ | オプション | 説明 |
93
+ |---|---|
94
+ | `--file <path>` | 指定したdocsファイルのみを処理する |
95
+ | `--agent <name>` | 使用するAIエージェントを上書きする |
96
+
97
+ ```bash
98
+ sdd-forge text
99
+ sdd-forge text --file docs/02_cli_commands.md
100
+ ```
101
+
102
+ #### readme
103
+
104
+ `docs/` の内容をソースとして、AIエージェントを使ってプロジェクトルートに `README.md` を自動生成します。使用するAIエージェントはプロジェクトconfigで定義します。
105
+
106
+ ```bash
107
+ sdd-forge readme
108
+ ```
109
+
110
+ #### forge
111
+
112
+ 最近の変更内容を説明するプロンプトに基づき、AIを使って1つ以上のdocsファイルを反復的に改善します。`--spec` を指定すると、specファイルがAIへの追加コンテキストとして含まれます。
113
+
114
+ | オプション | 説明 |
115
+ |---|---|
116
+ | `--prompt <text>` | docsに反映する変更内容の説明(必須) |
117
+ | `--spec <path>` | 追加コンテキストとして使用するspecファイルのパス |
118
+
119
+ ```bash
120
+ sdd-forge forge --prompt "translateコマンドに--langオプションを追加"
121
+ sdd-forge forge --prompt "scannerをリファクタリング" --spec specs/010-refactor/spec.md
122
+ ```
123
+
124
+ #### review
125
+
126
+ AIを使って生成済みdocsの品質チェックを実行し、完全性・正確性・ソースコードとの整合性を検証します。実行可能なフィードバックとともにPASSまたはFAILの結果を出力します。
127
+
128
+ ```bash
129
+ sdd-forge review
130
+ ```
131
+
132
+ #### changelog
133
+
134
+ `specs/` 配下のすべてのspecファイルを集約して `change_log.md` を生成します。各specが表す機能または修正の説明としてchangelogエントリを提供します。
135
+
136
+ ```bash
137
+ sdd-forge changelog
138
+ ```
139
+
140
+ #### agents
141
+
142
+ 最新のSDDインストラクションテンプレートと `analysis.json` / `summary.json` から新たに生成したPROJECTセクションで `AGENTS.md` を更新します。フラグで部分的な更新も可能です。
143
+
144
+ | オプション | 説明 |
145
+ |---|---|
146
+ | `--sdd` | `<!-- SDD:START/END -->` セクションのみを更新する |
147
+ | `--project` | `<!-- PROJECT:START/END -->` セクションのみを更新する |
148
+ | `--dry-run` | ディスクへの書き込みを行わずに出力をプレビューする |
149
+
150
+ ```bash
151
+ sdd-forge agents
152
+ sdd-forge agents --sdd
153
+ sdd-forge agents --dry-run
154
+ ```
155
+
156
+ #### translate
157
+
158
+ プロジェクトconfigの `output.languages` で指定された1つ以上の追加出力言語へ生成済みdocsを翻訳します。翻訳戦略の決定には `output.mode`(`translate` または `generate`)が使用されます。
159
+
160
+ | オプション | 説明 |
161
+ |---|---|
162
+ | `--lang <code>` | 翻訳先の言語コード(例: `en`、`ja`) |
163
+ | `--force` | 既存の翻訳済みファイルを上書きする |
164
+ | `--dry-run` | ディスクへの書き込みを行わずにプレビューする |
165
+
166
+ ```bash
167
+ sdd-forge translate --lang en
168
+ sdd-forge translate --lang en --force
169
+ ```
170
+
171
+ #### upgrade
172
+
173
+ `docs/` テンプレートのSDDマネージドセクションを、インストール済みの `sdd-forge` パッケージに同梱された最新バージョンに更新します。ディレクティブ境界の外にあるコンテンツは保持されます。
174
+
175
+ ```bash
176
+ sdd-forge upgrade
177
+ ```
178
+
179
+ #### setup
180
+
181
+ 新しいプロジェクトを `sdd-forge` に登録し、初期 `.sdd-forge/config.json` を生成します。プロジェクトタイプのプリセット選択と出力言語設定の構成をユーザーに案内します。`AGENTS.md` の作成と `CLAUDE.md` シンボリックリンクの作成も行います。
182
+
183
+ ```bash
184
+ sdd-forge setup
185
+ ```
186
+
187
+ #### default
188
+
189
+ 現在のツールインストールのデフォルトプロジェクトを設定または表示します。デフォルトプロジェクトは `.sdd-forge/projects.json` に保存され、`--project` が指定されていない場合に使用されます。
190
+
191
+ ```bash
192
+ sdd-forge default
193
+ sdd-forge default myproject
194
+ ```
195
+
196
+ #### spec
197
+
198
+ `specs/NNN-<slug>/spec.md` に新しいspecファイルを作成し、オプションでfeatureブランチを作成します。SDDワークフローにおいて新機能または修正の開始時に使用します。
199
+
200
+ | オプション | 説明 |
201
+ |---|---|
202
+ | `--title <name>` | specおよび機能のタイトル(必須) |
203
+ | `--no-branch` | ブランチ作成をスキップする(例: worktree内で作業している場合) |
204
+
205
+ ```bash
206
+ sdd-forge spec --title "translateコマンドを追加"
207
+ sdd-forge spec --title "scannerを修正" --no-branch
208
+ ```
209
+
210
+ #### gate
211
+
212
+ 実装前(`--phase pre`)または実装後(`--phase post`)に、定義された受入基準に照らしてspecファイルを検証します。各チェック項目についてPASSまたはFAILを報告します。
213
+
214
+ | オプション | 説明 |
215
+ |---|---|
216
+ | `--spec <path>` | 検証するspecファイルのパス(必須) |
217
+ | `--phase <pre\|post>` | ゲートフェーズ。デフォルトは `pre` |
218
+
219
+ ```bash
220
+ sdd-forge gate --spec specs/010-translate/spec.md
221
+ sdd-forge gate --spec specs/010-translate/spec.md --phase post
222
+ ```
223
+
224
+ #### flow
225
+
226
+ 自然言語のリクエストに対して、spec作成・ゲートチェック・実装スキャフォールディングを含むSDDフロー全体を自動実行します。手動でのステップ実行が現実的でないAIエージェント環境を想定しています。
227
+
228
+ | オプション | 説明 |
229
+ |---|---|
230
+ | `--request <text>` | 機能または修正の自然言語による説明 |
231
+
232
+ ```bash
233
+ sdd-forge flow --request "多言語出力のサポートを追加"
234
+ ```
235
+
236
+ #### presets
237
+
238
+ `sdd-forge` にバンドルされているすべてのプロジェクトタイププリセットを一覧表示します。アーキテクチャ層プリセット(`webapp`、`cli`、`library`)およびフレームワーク固有プリセット(`cakephp2`、`laravel`、`symfony`、`node-cli`)が含まれます。
239
+
240
+ ```bash
241
+ sdd-forge presets
242
+ ```
243
+
244
+ #### help
245
+
246
+ 利用可能なすべてのサブコマンドとその説明のまとめを表示します。引数なしで `sdd-forge` を実行した場合と同等です。
247
+
248
+ ```bash
249
+ sdd-forge help
250
+ sdd-forge -h
251
+ ```
252
+
253
+ ### 終了コードと出力
254
+
255
+ <!-- {{text: Describe exit code definitions (0=success, 1=general error, etc.) and the rules for stdout/stderr usage in a table format. Include the fact that gate and review PASS/FAIL results are printed to stdout.}} -->
256
+
257
+ | 終了コード | 意味 |
258
+ |---|---|
259
+ | `0` | コマンドが正常に完了した |
260
+ | `1` | 一般エラー(必須オプションの欠如、ファイルが見つからない、設定が無効など) |
261
+ | `2` | gateまたはreviewのチェックが失敗した(FAILの結果が報告される。詳細はstdoutを参照) |
262
+
263
+ **stdout / stderr の使い分け:**
264
+
265
+ | ストリーム | 内容 |
266
+ |---|---|
267
+ | `stdout` | 主要なコマンド出力: 生成テキスト、レンダリングされたテーブル、`gate` および `review` のPASS/FAIL結果、dry-runプレビュー |
268
+ | `stderr` | 進捗インジケーター、警告、デバッグ情報、エラーメッセージ |
269
+
270
+ `gate` と `review` は常にPASS/FAILの判定と各チェック項目の結果を **stdout** に出力するため、CIパイプラインでのキャプチャに適しています。コマンドが実行できない理由を示すエラーメッセージ(設定がない、引数が無効など)は **stderr** に書き込まれ、キャプチャしたstdoutには現れません。