specrails-core 3.6.0 → 3.8.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.
@@ -0,0 +1,1637 @@
1
+ # Enrich: Agent Workflow System
2
+
3
+ Interactive wizard to configure the full agent workflow system for this repository. Analyzes the codebase, discovers target users, generates VPC personas, and creates all agents, commands, rules, and configuration adapted to this project. Supports config-driven mode for direct installation from a pre-built config file.
4
+
5
+ **Prerequisites:** Run `specrails/install.sh` first to install templates.
6
+
7
+ ### Hub Checkpoint Protocol
8
+
9
+ When running inside specrails-hub, emit checkpoint markers at key transitions so the hub `CheckpointTracker` can display progress. Each checkpoint is a single line printed to stdout:
10
+
11
+ ```
12
+ [checkpoint:phase_1_analysis] Codebase analysis complete
13
+ [checkpoint:phase_2_personas] VPC personas generated
14
+ [checkpoint:phase_3_commands] Commands and rules generated
15
+ [checkpoint:phase_4_agents] Agent files generated
16
+ [checkpoint:phase_5_cleanup] Cleanup and summary complete
17
+ ```
18
+
19
+ The hub parses these via `detectCheckpointFromText()` regex patterns. Always emit checkpoints at the end of each phase, even in `--from-config` and `--quick` modes.
20
+
21
+ ---
22
+
23
+ ## Mode Detection
24
+
25
+ Check `$ARGUMENTS` in this order:
26
+
27
+ 1. If `--update` is present → execute **Update Mode** (below), then stop.
28
+ 2. If `--from-config` is present → execute **From-Config Mode** (below), then stop.
29
+ 3. If `--quick` or `--lite` is present → execute **Quick Mode** (below), then stop.
30
+ 4. Otherwise (no flags) → skip directly to **Phase 1** and execute the full 5-phase wizard.
31
+
32
+ **Default is the full wizard.** Quick Mode only runs when `--quick` or `--lite` is explicitly passed.
33
+
34
+ ---
35
+
36
+ ## From-Config Mode
37
+
38
+ When `--from-config` is passed, execute a fully automated installation using `.specrails/install-config.yaml`. No interactive prompts — all decisions come from the config file.
39
+
40
+ ### FC1: Read and Validate Config
41
+
42
+ Read `.specrails/install-config.yaml`. If it does not exist, display:
43
+ > "`.specrails/install-config.yaml` not found. Run the TUI installer first (`npx specrails-core@latest init`) or use `/specrails:enrich` without `--from-config` for the interactive wizard."
44
+ Then stop.
45
+
46
+ Parse the YAML and extract:
47
+ - `config.version` — must be `1`; if not, warn and proceed
48
+ - `config.provider` — `claude` or `codex`; default `claude`
49
+ - `config.tier` — `full` or `quick`; default `full`
50
+ - `config.agents.selected` — list of agent names to install
51
+ - `config.agents.excluded` — list of agent names to skip (for reference only)
52
+ - `config.models.preset` — `balanced`, `budget`, or `max` (see Model Presets below)
53
+ - `config.models.defaults.model` — default model override (overrides preset)
54
+ - `config.models.overrides` — per-agent model overrides (highest priority)
55
+ - `config.quick_context.product_description` — product description seed
56
+ - `config.quick_context.target_users` — target users seed
57
+ - `config.agent_teams` — boolean; whether to install team-review/team-debug commands
58
+
59
+ Store all values in variables prefixed `FC_`.
60
+
61
+ **Model Presets** (applied when `config.models.defaults` is not set):
62
+ - `balanced` (default): sr-architect=opus, sr-product-manager=opus, all others=sonnet
63
+ - `budget`: all agents=haiku
64
+ - `max`: all agents=opus
65
+
66
+ To resolve the model for any given agent:
67
+ 1. Check `config.models.overrides.<agent-name>` — if present, use it
68
+ 2. Check `config.models.defaults.model` — if present, use it
69
+ 3. Apply preset defaults based on `config.models.preset`
70
+ 4. Fallback to model in template frontmatter
71
+
72
+ ### FC2: Provider Setup
73
+
74
+ Read `.specrails/setup-templates/.provider-detection.json` if present, to get `cli_provider` and `specrails_dir`. If `FC_provider` differs from the detected value, use `FC_provider` (config takes precedence).
75
+
76
+ Set `SPECRAILS_DIR` and `CLI_PROVIDER` from resolved provider.
77
+
78
+ ### FC3: If tier is `quick` — run Quick Mode with config context
79
+
80
+ If `FC_tier == "quick"`:
81
+ - Set `QS_PROJECT_DESCRIPTION = FC_quick_context.product_description`
82
+ - Set `QS_TARGET_USERS = FC_quick_context.target_users`
83
+ - Set `FC_AGENTS_SELECTED` as the list of agents to generate (instead of defaults)
84
+ - Execute Quick Mode (QS1–QS4) with these values injected, generating all `FC_AGENTS_SELECTED` agents (not just defaults)
85
+ - Then stop.
86
+
87
+ ### FC4: If tier is `full` — run automated full wizard
88
+
89
+ **Phase 1 (Silent):** Run the full Phase 1 analysis (1.1–1.4) without any user prompts. Store all detected values internally.
90
+
91
+ **Phase 2 (Seeded VPC Discovery):** Run VPC persona generation using `FC_quick_context` as seeds:
92
+ - Treat `FC_quick_context.product_description` as the answer to "Describe your product"
93
+ - Treat `FC_quick_context.target_users` as the answer to "Who are your users"
94
+ - Use AI to expand these seeds into full VPC personas (3 primary, 3 secondary)
95
+ - Do not ask the user any questions — infer all answers from seeds + codebase analysis
96
+
97
+ **Phase 3 (Silent):** Run Phase 3 normally without user prompts.
98
+
99
+ **Phase 4 (Config-Driven Agent Generation):**
100
+ - Generate only agents in `FC_AGENTS_SELECTED` (skip any not in the list)
101
+ - Apply model resolution (FC1 preset/override logic) to each agent's frontmatter
102
+ - Run all Phase 4 sub-phases (4.1, 4.2, 4.3, 4.4) normally but without interactive prompts
103
+ - If `FC_agent_teams == true`, also install team-review and team-debug commands
104
+
105
+ **Phase 5 (Normal):** Run Phase 5 cleanup normally.
106
+
107
+ ### FC5: Completion
108
+
109
+ Display:
110
+ ```
111
+ ✅ Enrich complete (from-config).
112
+
113
+ Agents generated: <count> of <total selected>
114
+ Models applied:
115
+ sr-architect: <resolved-model>
116
+ sr-developer: <resolved-model>
117
+ [... one line per generated agent ...]
118
+
119
+ Next steps:
120
+ > /specrails:implement <feature>
121
+ ```
122
+
123
+ ---
124
+
125
+ ## Update Mode
126
+
127
+ When `--update` is passed, execute this streamlined flow instead of the full wizard. Do not run any phases from the full wizard. When Phase U7 is complete, stop.
128
+
129
+ ### Phase U1: Read Update Context
130
+
131
+ Read the following files to understand the current installation state:
132
+
133
+ 1. Read `.specrails/specrails-manifest.json` — contains agent template checksums from the last install/update. Structure:
134
+ ```json
135
+ {
136
+ "version": "0.2.0",
137
+ "installed_at": "2025-01-15T10:00:00Z",
138
+ "artifacts": {
139
+ "templates/agents/architect.md": "sha256:<checksum>",
140
+ "templates/agents/developer.md": "sha256:<checksum>",
141
+ "templates/agents/reviewer.md": "sha256:<checksum>"
142
+ }
143
+ }
144
+ ```
145
+ If this file does not exist, inform the user:
146
+ > "No `.specrails/specrails-manifest.json` found. This looks like a pre-versioning installation. Run `update.sh` first to initialize the manifest, then re-run `/specrails:enrich --update`."
147
+ Then stop.
148
+
149
+ 2. Read `.specrails/specrails-version` — contains the current version string (e.g., `0.2.0`). If it does not exist, treat version as `0.1.0 (legacy)`.
150
+
151
+ 3. Determine `$SPECRAILS_DIR` by reading `.specrails/setup-templates/.provider-detection.json`. Extract `cli_provider` and `specrails_dir`. If not found, default to `cli_provider = "claude"`, `specrails_dir = ".claude"`.
152
+
153
+ 4. List all template files in `.specrails/setup-templates/agents/` — these are the NEW agent templates from the update:
154
+ ```bash
155
+ ls .specrails/setup-templates/agents/
156
+ ```
157
+ Template files are named with `sr-` prefix (e.g., `sr-architect.md`, `sr-developer.md`).
158
+
159
+ 5. List all template files in `.specrails/setup-templates/commands/specrails/` — these are the NEW command templates from the update:
160
+ ```bash
161
+ ls .specrails/setup-templates/commands/specrails/
162
+ ```
163
+ Command template files include `implement.md`, `batch-implement.md`, `compat-check.md`, `refactor-recommender.md`, `why.md`, `get-backlog-specs.md`, `auto-propose-backlog-specs.md`.
164
+ If this directory does not exist, skip command template checking for this update.
165
+
166
+ 6. Read `.specrails/backlog-config.json` if it exists — contains stored provider configuration needed for command placeholder substitution.
167
+
168
+ 7. Read `.specrails/agents.yaml` if it exists — contains agent model configuration. Validate all `model:` values (only `opus`, `sonnet`, `haiku` are valid). Store as `AGENTS_CONFIG` for use in Phase U4. If the file does not exist, set `AGENTS_CONFIG = null`.
169
+
170
+ ### Phase U2: Quick Codebase Re-Analysis
171
+
172
+ Perform the same analysis as Phase 1 of the full setup wizard, but silently — do not prompt the user and do not show the findings table. Just execute and store results internally.
173
+
174
+ Detect:
175
+ - **Languages**: Check for `*.py`, `*.ts`, `*.tsx`, `*.go`, `*.rs`, `*.java`, `*.kt`, `*.rb`, `*.cs`
176
+ - **Frameworks**: Search for imports (`fastapi`, `express`, `react`, `vue`, `angular`, `django`, `spring`, `gin`, `actix`, `rails`)
177
+ - **Directory structure**: Identify backend/frontend/core/test directories
178
+ - **Database**: Check for SQL files, ORM configs, migration directories
179
+ - **CI/CD**: Parse `.github/workflows/*.yml` for lint/test/build commands
180
+ - **Naming conventions**: Read 2-3 source files per detected layer
181
+
182
+ Read:
183
+ - `README.md` (if exists)
184
+ - `package.json` / `pyproject.toml` / `Cargo.toml` / `go.mod` / `pom.xml` (detect stack)
185
+ - `.github/workflows/*.yml` (detect CI commands)
186
+
187
+ Store all results for use in Phases U4 and U5.
188
+
189
+ ### Phase U3: Identify What Needs Regeneration
190
+
191
+ **Agent templates:** For each agent template, find its entry in the manifest's `artifacts` map (keyed as `templates/agents/sr-<name>.md`). Compute the SHA-256 checksum of the corresponding file in `.specrails/setup-templates/agents/`:
192
+
193
+ ```bash
194
+ sha256sum .specrails/setup-templates/agents/sr-<name>.md
195
+ ```
196
+
197
+ Build three lists for agents:
198
+
199
+ 1. **Changed agents**: agent name exists in manifest AND the current template checksum differs from the manifest checksum → mark for regeneration
200
+ 2. **New agents**: template file exists in `.specrails/setup-templates/agents/` but the agent name is NOT in the manifest → mark for evaluation
201
+ 3. **Unchanged agents**: agent name exists in manifest AND checksum matches → skip
202
+
203
+ **Command templates:** If `.specrails/setup-templates/commands/specrails/` exists, for each command template file, find its entry in the manifest's `artifacts` map (keyed as `templates/commands/specrails/<name>.md`). Compute the SHA-256 checksum of the corresponding file in `.specrails/setup-templates/commands/specrails/`:
204
+
205
+ ```bash
206
+ sha256sum .specrails/setup-templates/commands/specrails/<name>.md
207
+ ```
208
+
209
+ Build three lists for commands:
210
+
211
+ 1. **Changed commands**: command name exists in manifest AND the current template checksum differs from the manifest checksum → mark for update
212
+ 2. **New commands**: template file exists in `.specrails/setup-templates/commands/specrails/` but the command name is NOT in the manifest → mark for evaluation
213
+ 3. **Unchanged commands**: command name exists in manifest AND checksum matches → skip
214
+
215
+ Display the combined analysis to the user:
216
+
217
+ ```
218
+ ## Update Analysis
219
+
220
+ ### Agents — Changed Templates (will be regenerated)
221
+ - sr-architect.md (template modified)
222
+ - sr-developer.md (template modified)
223
+
224
+ ### Agents — New Templates Available
225
+ - sr-frontend-developer.md
226
+ - sr-backend-developer.md
227
+
228
+ ### Agents — Unchanged (keeping current)
229
+ - sr-reviewer.md
230
+ - sr-product-manager.md
231
+
232
+ ### Commands — Changed Templates (will be updated)
233
+ - implement.md (template modified)
234
+
235
+ ### Commands — New Templates Available
236
+ - refactor-recommender.md
237
+
238
+ ### Commands — Unchanged (keeping current)
239
+ - compat-check.md
240
+ - why.md
241
+ ```
242
+
243
+ If there are no changed agents, no new agents, no changed commands, and no new commands, display:
244
+ ```
245
+ All agents and commands are already up to date. Nothing to regenerate.
246
+ ```
247
+ Then jump to Phase U7.
248
+
249
+ ### Phase U4: Regenerate Changed Agents
250
+
251
+ For each agent in the "changed" list:
252
+
253
+ 1. Read the NEW template from `.specrails/setup-templates/agents/sr-<name>.md`
254
+ 2. Use the codebase analysis from Phase U2 to fill in all `{{PLACEHOLDER}}` values, using the same substitution rules as Phase 4.1 of the full setup:
255
+ - `{{PROJECT_NAME}}` → project name (from README.md or directory name)
256
+ - `{{ARCHITECTURE_DIAGRAM}}` → detected architecture layers
257
+ - `{{LAYER_TAGS}}` → detected layer tags (e.g., `[backend]`, `[frontend]`, `[api]`)
258
+ - `{{CI_COMMANDS_BACKEND}}` → backend CI commands
259
+ - `{{CI_COMMANDS_FRONTEND}}` → frontend CI commands
260
+ - `{{LAYER_CONVENTIONS}}` → detected conventions per layer
261
+ - `{{PERSONA_NAMES}}` → read existing persona names from `$SPECRAILS_DIR/agents/personas/` filenames
262
+ - `{{PERSONA_FILES}}` → paths to existing persona files in `$SPECRAILS_DIR/agents/personas/`
263
+ - `{{DOMAIN_EXPERTISE}}` → infer from detected stack and README
264
+ - `{{KEY_FILE_PATHS}}` → important file paths detected in Phase U2
265
+ - `{{WARNINGS}}` → read from existing `CLAUDE.md` if present
266
+ - `{{MEMORY_PATH}}` → `$SPECRAILS_DIR/agent-memory/sr-<agent-name>/`
267
+ 3. Resolve the agent's model using `AGENTS_CONFIG` (loaded in Phase U1, step 7):
268
+ - Check `AGENTS_CONFIG.agents.<agent-name>.model` (per-agent override)
269
+ - If not present, check `AGENTS_CONFIG.defaults.model` (global default)
270
+ - If `AGENTS_CONFIG` is null, use the model from the template frontmatter
271
+ - Replace the `model:` field in the YAML frontmatter with the resolved value before writing
272
+ 4. Write the adapted agent using the format for the active provider (same dual-format rules as Phase 4.1):
273
+ - `cli_provider == "claude"`: write to `$SPECRAILS_DIR/agents/sr-<name>.md` (Markdown with YAML frontmatter)
274
+ - `cli_provider == "codex"`: write to `$SPECRAILS_DIR/agents/sr-<name>.toml` (TOML format with `name`, `description`, `model`, `prompt` fields)
275
+ 5. Show: `✓ Regenerated sr-<name>`
276
+
277
+ After regenerating all changed agents, verify no unresolved placeholders remain:
278
+ ```bash
279
+ # Claude Code
280
+ grep -r '{{[A-Z_]*}}' .claude/agents/sr-*.md 2>/dev/null || echo "OK: no broken placeholders"
281
+ # Codex
282
+ grep -r '{{[A-Z_]*}}' .codex/agents/sr-*.toml 2>/dev/null || echo "OK: no broken placeholders"
283
+ ```
284
+
285
+ ### Phase U4b: Update Changed Commands
286
+
287
+ For each command in the "changed commands" list from Phase U3:
288
+
289
+ 1. Read the NEW template:
290
+ - If `cli_provider == "claude"`: from `.specrails/setup-templates/commands/specrails/<name>.md`
291
+ - If `cli_provider == "codex"`: from `.specrails/setup-templates/skills/sr-<name>/SKILL.md`
292
+ 2. Read stored backlog configuration from `.specrails/backlog-config.json` (if it exists) to resolve provider-specific placeholders:
293
+ - `BACKLOG_PROVIDER` → `provider` field (`github`, `jira`, or `none`)
294
+ - `BACKLOG_WRITE` → `write_access` field
295
+ - `JIRA_BASE_URL` → `jira_base_url` field
296
+ - `JIRA_PROJECT_KEY` → `jira_project_key` field
297
+ 3. Substitute all `{{PLACEHOLDER}}` values using the same rules as Phase 4.3 of the full setup:
298
+ - `{{CI_COMMANDS_BACKEND}}` → backend CI commands detected in Phase U2
299
+ - `{{CI_COMMANDS_FRONTEND}}` → frontend CI commands detected in Phase U2
300
+ - `{{DEPENDENCY_CHECK_COMMANDS}}` → stack-specific dependency check commands from Phase U2
301
+ - `{{TEST_RUNNER_CHECK}}` → test runner commands from Phase U2
302
+ - `{{BACKLOG_FETCH_CMD}}` → provider-specific fetch command from backlog config
303
+ - `{{BACKLOG_CREATE_CMD}}` → provider-specific create command from backlog config
304
+ - `{{BACKLOG_VIEW_CMD}}` → provider-specific view command from backlog config
305
+ - `{{BACKLOG_PREFLIGHT}}` → provider-specific preflight check from backlog config
306
+ - Any other `{{PLACEHOLDER}}` values → use Phase U2 analysis data
307
+ 4. Write the updated file:
308
+ - If `cli_provider == "claude"`: to `.claude/commands/specrails/<name>.md`
309
+ - If `cli_provider == "codex"`: to `.agents/skills/sr-<name>/SKILL.md`
310
+ 5. Show:
311
+ - If `cli_provider == "claude"`: `✓ Updated /specrails:<name>`
312
+ - If `cli_provider == "codex"`: `✓ Updated $sr-<name>`
313
+
314
+ After updating all changed commands/skills, verify no unresolved placeholders remain:
315
+ ```bash
316
+ # If cli_provider == "claude":
317
+ grep -l '{{[A-Z_]*}}' .claude/commands/specrails/*.md 2>/dev/null || echo "OK: no broken placeholders"
318
+ # If cli_provider == "codex":
319
+ grep -rl '{{[A-Z_]*}}' .agents/skills/sr-*/SKILL.md 2>/dev/null || echo "OK: no broken placeholders"
320
+ ```
321
+ If any placeholders remain unresolved, warn the user:
322
+ > "⚠ Some placeholders in `<filename>` could not be resolved automatically. Please review the file and fill them in manually."
323
+
324
+ ### Phase U5: Evaluate New Agents
325
+
326
+ For each agent in the "new" list:
327
+
328
+ 1. Read the template from `.specrails/setup-templates/agents/sr-<name>.md` to understand what stack or layer it targets (read its description and any layer-specific comments)
329
+ 2. Match against the codebase detected in Phase U2:
330
+ - If the template targets a layer/stack that IS present (e.g., `sr-frontend-developer` and React was detected), prompt:
331
+ > "New agent available: `sr-<name>` — your project uses [detected tech]. Add it? [Y/n]"
332
+ - If the template targets a layer/stack that is NOT present (e.g., `sr-backend-developer` and no backend was detected), prompt:
333
+ > "New agent available: `sr-<name>` — no [layer] detected in your project. Skip? [Y/n]"
334
+ 3. If the user accepts (or presses Enter on a pre-selected default):
335
+ - Generate the agent using the same template adaptation as Phase U4
336
+ - Create memory directory if it does not exist: `$SPECRAILS_DIR/agent-memory/sr-<name>/`
337
+ - Show: `✓ Added sr-<name>`
338
+ 4. If the user declines:
339
+ - Show: `→ Skipped sr-<name>`
340
+
341
+ For each command in the "new commands" list from Phase U3:
342
+
343
+ 1. Read the template:
344
+ - If `cli_provider == "claude"`: from `.specrails/setup-templates/commands/specrails/<name>.md`
345
+ - If `cli_provider == "codex"`: from `.specrails/setup-templates/skills/sr-<name>/SKILL.md`
346
+ 2. Prompt the user:
347
+ - If `cli_provider == "claude"`: `"New command available: /specrails:<name> — [one-line description]. Install it? [Y/n]"`
348
+ - If `cli_provider == "codex"`: `"New skill available: $sr-<name> — [one-line description]. Install it? [Y/n]"`
349
+ 3. If the user accepts (or presses Enter):
350
+ - Apply placeholder substitution using the same rules as Phase U4b (backlog config + codebase analysis)
351
+ - If `cli_provider == "claude"`: write to `.claude/commands/specrails/<name>.md` — show `✓ Added /specrails:<name>`
352
+ - If `cli_provider == "codex"`: write to `.agents/skills/sr-<name>/SKILL.md` — show `✓ Added $sr-<name>`
353
+ 4. If the user declines:
354
+ - If `cli_provider == "claude"`: show `→ Skipped /specrails:<name>`
355
+ - If `cli_provider == "codex"`: show `→ Skipped $sr-<name>`
356
+
357
+ ### Phase U6: Update Workflow Commands
358
+
359
+ If any new agents were added in Phase U5:
360
+
361
+ 1. Read the implement command/skill:
362
+ - If `cli_provider == "claude"`: `.claude/commands/specrails/implement.md`
363
+ - If `cli_provider == "codex"`: `.agents/skills/sr-implement/SKILL.md`
364
+ 2. Check if the file references agent names in its orchestration steps (look for `sr-architect`, `sr-developer`, `sr-reviewer` etc.)
365
+ 3. If newly added agents belong in the implementation pipeline (i.e., they are layer-specific developers such as `sr-frontend-developer` or `sr-backend-developer`), add them to the appropriate step in the implement command — specifically where parallel developer agents are launched
366
+ 4. Write the updated file if any changes were made:
367
+ - If `cli_provider == "claude"`: `.claude/commands/specrails/implement.md`
368
+ - If `cli_provider == "codex"`: `.agents/skills/sr-implement/SKILL.md`
369
+ 5. Show which commands were updated, or "No command updates needed" if nothing changed
370
+
371
+ This is a lightweight check — only update commands where the sr- agent clearly belongs. Do not restructure the entire command.
372
+
373
+ ### Phase U7: Summary
374
+
375
+ Display the final summary and stop. Do not continue to Phase 1 of the full setup wizard.
376
+
377
+ ```
378
+ ## Update Complete
379
+
380
+ specrails updated from v<previous> to v<new>.
381
+
382
+ | Action | Count |
383
+ |---------------------|-------|
384
+ | Agents regenerated | N |
385
+ | Agents added | N |
386
+ | Agents skipped | N |
387
+ | Commands updated | N |
388
+ | Commands added | N |
389
+ | Commands skipped | N |
390
+
391
+ All agents and commands are now up to date.
392
+
393
+ ### Agents Regenerated
394
+ [list agent names, or "(none)"]
395
+
396
+ ### Agents Added
397
+ [list agent names, or "(none)"]
398
+
399
+ ### Agents Skipped
400
+ [list agent names, or "(none)"]
401
+
402
+ ### Commands Updated
403
+ [list command names, or "(none)"]
404
+
405
+ ### Commands Added
406
+ [list command names, or "(none)"]
407
+
408
+ ### Commands Skipped
409
+ [list command names, or "(none)"]
410
+ ```
411
+
412
+ Update `.specrails/specrails-manifest.json` to reflect the new checksums for all regenerated/updated and added agents and commands:
413
+ - For each regenerated agent: update its checksum entry to the new template's checksum (keyed as `templates/agents/sr-<name>.md`)
414
+ - For each added agent: add a new entry with its checksum
415
+ - For each updated command: update its checksum entry to the new template's checksum (keyed as `templates/commands/specrails/<name>.md`)
416
+ - For each added command: add a new entry with its checksum
417
+ - Update the `version` field to the version read from `.specrails/specrails-version`
418
+
419
+ ---
420
+
421
+ ## Quick Mode
422
+
423
+ When `--quick` or `--lite` is passed, run this streamlined 3-question setup. Do NOT run Phase 1–5. When QS4 is complete, stop.
424
+
425
+ ### QS1: Ask the 3 questions
426
+
427
+ Display the following prompt EXACTLY ONCE and then wait for the user's responses. Do NOT repeat the questions — output them a single time only.
428
+
429
+ Welcome to specrails! Let's get your AI agent team set up in 3 quick questions.
430
+
431
+ 1. What is this project? (one sentence)
432
+ 2. Who are the target users?
433
+ 3. Git access for agents — read-only or read-write?
434
+ (read-only = agents can read and suggest; read-write = agents can commit)
435
+
436
+ Store the answers as:
437
+ - `QS_PROJECT_DESCRIPTION` — answer to question 1
438
+ - `QS_TARGET_USERS` — answer to question 2
439
+ - `QS_GIT_ACCESS` — "read-only" or "read-write" (normalize if user types "ro", "rw", "readonly", etc.)
440
+
441
+ ### QS2: Apply opinionated defaults
442
+
443
+ Use these defaults for all configuration not asked in QS1:
444
+
445
+ | Setting | Quick Mode Default |
446
+ |---------|------------------|
447
+ | Agents enabled | sr-architect, sr-developer, sr-reviewer, sr-product-manager |
448
+ | Git mode | Derived from QS_GIT_ACCESS |
449
+ | CLAUDE.md template | `templates/CLAUDE-quickstart.md` |
450
+ | OpenSpec enabled | Yes if `openspec` CLI is detected in PATH, No otherwise |
451
+ | Telemetry | Not configured (deferred to PRD-002) |
452
+ | Backlog provider | local (lightweight JSON-based, no external tools needed) |
453
+
454
+ Detect whether this is an existing codebase or new project:
455
+ - **Existing codebase**: `package.json`, `Gemfile`, `pyproject.toml`, `go.mod`, or `pom.xml` found in the repo root
456
+ - **New project**: none of the above found
457
+
458
+ Store as `QS_IS_EXISTING_CODEBASE=true/false`.
459
+
460
+ ### QS2.5: Re-run Detection
461
+
462
+ Before generating files, check if this is a re-run:
463
+
464
+ 1. Check if commands/skills already exist:
465
+ - If `cli_provider == "claude"`: check if `.claude/commands/specrails/` directory exists with any `.md` files:
466
+ ```bash
467
+ ls .claude/commands/specrails/*.md 2>/dev/null
468
+ ```
469
+ - If `cli_provider == "codex"`: check if `.agents/skills/sr-*/SKILL.md` files exist:
470
+ ```bash
471
+ ls .agents/skills/sr-*/SKILL.md 2>/dev/null
472
+ ```
473
+ 2. If files are found → this is a **re-run**. Store `QS_IS_RERUN=true`.
474
+ 3. If the directory does not exist or is empty → this is a **fresh install**. Store `QS_IS_RERUN=false`.
475
+
476
+ In re-run mode, QS3 executes in **gap-fill mode** for command/skill files:
477
+ - For each command in the list, check if it already exists:
478
+ - If `cli_provider == "claude"`: at `.claude/commands/specrails/<name>.md`
479
+ - If `cli_provider == "codex"`: at `.agents/skills/sr-<name>/SKILL.md`
480
+ - If it exists: skip it and show:
481
+ - Claude: `✓ Already installed: /specrails:<name>`
482
+ - Codex: `✓ Already installed: $sr-<name>`
483
+ - If it does NOT exist: install it and show:
484
+ - Claude: `✓ Added /specrails:<name> (was missing)`
485
+ - Codex: `✓ Added $sr-<name> (was missing)`
486
+ - Do NOT prompt the user for confirmation on missing files — install them automatically
487
+
488
+ For CLAUDE.md/AGENTS.md and agent files, the existing per-file prompts already handle re-runs (user is asked before overwriting). No change needed there.
489
+
490
+ ### QS3: Generate files
491
+
492
+ Generate files using the Quick Mode defaults.
493
+
494
+ **1. CLAUDE.md**
495
+
496
+ Read `.specrails/setup-templates/claude-md/CLAUDE-quickstart.md` (or fall back to `.specrails/setup-templates/claude-md/default.md` if quickstart template is not found).
497
+
498
+ Replace placeholders:
499
+ - `{{PROJECT_NAME}}` → derive from directory name or README.md first heading
500
+ - `{{PROJECT_DESCRIPTION}}` → `QS_PROJECT_DESCRIPTION`
501
+ - `{{TARGET_USERS}}` → `QS_TARGET_USERS`
502
+ - `{{GIT_ACCESS}}` → `QS_GIT_ACCESS`
503
+
504
+ Write to `CLAUDE.md` in the repo root. If `CLAUDE.md` already exists, ask:
505
+ > "CLAUDE.md already exists. Overwrite? [Y/n]"
506
+ Skip if user says no.
507
+
508
+ **2. Agent files**
509
+
510
+ For each default agent (sr-architect, sr-developer, sr-reviewer, sr-product-manager), read the template from `.specrails/setup-templates/agents/<name>.md` and generate the adapted agent file using the dual-format rules from Phase 4.1:
511
+ - `cli_provider == "claude"`: write to `.claude/agents/<name>.md` (Markdown with frontmatter)
512
+ - `cli_provider == "codex"`: write to `.codex/agents/<name>.toml` (TOML format)
513
+
514
+ If `.specrails/agents.yaml` exists, read it and apply model resolution (per-agent override → defaults → template value) before writing each agent file.
515
+
516
+ Fill placeholders with best-effort values from the limited context available:
517
+ - `{{PROJECT_NAME}}` → directory name or README first heading
518
+ - `{{PROJECT_DESCRIPTION}}` → `QS_PROJECT_DESCRIPTION`
519
+ - `{{TARGET_USERS}}` → `QS_TARGET_USERS`
520
+ - `{{GIT_ACCESS}}` → `QS_GIT_ACCESS`
521
+ - `{{ARCHITECTURE_DIAGRAM}}` → "(Quick Mode — run `/specrails:enrich` for full architecture analysis)"
522
+ - `{{TECH_EXPERTISE}}` → "(Quick Mode — run `/specrails:enrich` for codebase-specific expertise)"
523
+ - `{{LAYER_TAGS}}` → detect from package.json / Gemfile / go.mod if present; otherwise leave empty
524
+ - All other placeholders → "(not configured — run `/specrails:enrich`)"
525
+
526
+ Create memory directories: `$SPECRAILS_DIR/agent-memory/sr-<name>/`
527
+
528
+ **3. Command files**
529
+
530
+ Core commands (always install if missing):
531
+ - `implement.md`
532
+ - `batch-implement.md`
533
+ - `propose-spec.md`
534
+ - `compat-check.md`
535
+ - `why.md`
536
+ - `get-backlog-specs.md`
537
+ - `auto-propose-backlog-specs.md`
538
+
539
+ **Initialize local ticket storage** (backlog provider defaults to `local`):
540
+ 1. Copy `templates/local-tickets-schema.json` to `.specrails/local-tickets.json` and set `last_updated` to the current ISO-8601 timestamp. Skip if the file already exists.
541
+ 2. Write `.specrails/backlog-config.json` (skip if already exists):
542
+ ```json
543
+ {
544
+ "provider": "local",
545
+ "write_access": true,
546
+ "git_auto": true
547
+ }
548
+ ```
549
+
550
+ **If `cli_provider == "claude"`:**
551
+
552
+ If `QS_IS_RERUN=false` (fresh install): for each core command, read the template from `.specrails/setup-templates/commands/specrails/<name>.md`, substitute the backlog placeholders with local values (using the same table as Phase 4.3 "Local Tickets"), stub all persona placeholders with `(Quick Mode — run /specrails:enrich to configure personas)`, then write to `.claude/commands/specrails/<name>.md`.
553
+
554
+ If `QS_IS_RERUN=true` (gap-fill mode): for each command in the list above, check if `.claude/commands/specrails/<name>.md` already exists:
555
+ - If it exists: skip it — show `✓ Already installed: /specrails:<name>`
556
+ - If it does NOT exist: read template, substitute placeholders as above, write to `.claude/commands/specrails/<name>.md` — show `✓ Added /specrails:<name> (was missing)`
557
+
558
+ **If `cli_provider == "codex"`:**
559
+
560
+ If `QS_IS_RERUN=false` (fresh install): for each core command, read the corresponding skill template from `.specrails/setup-templates/skills/sr-<name>/SKILL.md`, substitute the backlog placeholders with local values and stub persona placeholders with `(Quick Mode — run /specrails:enrich to configure personas)`, then write to `.agents/skills/sr-<name>/SKILL.md` (create the directory first).
561
+
562
+ If `QS_IS_RERUN=true` (gap-fill mode): for each command in the list above, check if `.agents/skills/sr-<name>/SKILL.md` already exists:
563
+ - If it exists: skip it — show `✓ Already installed: $sr-<name>`
564
+ - If it does NOT exist: read template, substitute placeholders as above, write to `.agents/skills/sr-<name>/SKILL.md` — show `✓ Added $sr-<name> (was missing)`
565
+
566
+ **4. Cleanup**
567
+
568
+ Remove `.specrails/setup-templates/` (same as full wizard cleanup in Phase 5).
569
+
570
+ Remove `commands/enrich.md` from `.claude/commands/` if it was copied there by the installer.
571
+
572
+ ### QS4: First Task Prompt
573
+
574
+ After generating all files, display the setup complete message.
575
+
576
+ Then, based on `QS_IS_EXISTING_CODEBASE`:
577
+ - **Existing codebase** (`true`): recommend `/specrails:refactor-recommender`
578
+ - **New project** (`false`): recommend `/specrails:get-backlog-specs`
579
+
580
+ If `QS_IS_RERUN=false`, display:
581
+ ```
582
+ ✅ Setup complete.
583
+
584
+ Try your first command:
585
+ > /specrails:get-backlog-specs
586
+ ```
587
+ (Replace `/specrails:get-backlog-specs` with `/specrails:refactor-recommender` for existing codebases.)
588
+
589
+ If `QS_IS_RERUN=true`, display the gap-fill summary and stop:
590
+ ```
591
+ ✅ Re-run complete.
592
+
593
+ Commands status:
594
+ ✓ Already installed: /specrails:<name>
595
+ ✓ Added /specrails:<name> (was missing)
596
+ [... one line per command ...]
597
+
598
+ All commands are up to date.
599
+ ```
600
+ If all commands were already present, display:
601
+ ```
602
+ ✅ Re-run complete. All commands already installed — nothing to add.
603
+ ```
604
+
605
+ Then stop. Do not execute Phase 1.
606
+
607
+ ---
608
+
609
+ ## Phase 1: Codebase Analysis
610
+
611
+ Analyze the repository to understand its architecture, stack, and conventions.
612
+
613
+ ### 1.1 Read project structure
614
+
615
+ ```bash
616
+ # Get the repo root and basic info
617
+ git rev-parse --show-toplevel
618
+ ls -la
619
+ ```
620
+
621
+ Read the following to understand the project:
622
+ - `README.md` (if exists)
623
+ - `CLAUDE.md` (if exists — don't overwrite, merge later)
624
+ - `package.json` or `pyproject.toml` or `Cargo.toml` or `go.mod` or `pom.xml` (detect stack)
625
+ - `.github/workflows/*.yml` (detect CI commands)
626
+ - `docker-compose.yml` or `Dockerfile` (detect infra)
627
+
628
+ ### 1.2 Detect architecture layers
629
+
630
+ Use Glob and Grep to identify:
631
+
632
+ 1. **Languages**: Check for `*.py`, `*.ts`, `*.tsx`, `*.go`, `*.rs`, `*.java`, `*.kt`, `*.rb`, `*.cs`
633
+ 2. **Frameworks**: Search for imports (`fastapi`, `express`, `react`, `vue`, `angular`, `django`, `spring`, `gin`, `actix`, `rails`)
634
+ 3. **Directory structure**: Identify backend/frontend/core/test directories
635
+ 4. **Database**: Check for SQL files, ORM configs, migration directories
636
+ 5. **CI/CD**: Parse workflow files for lint/test/build commands
637
+
638
+ ### 1.3 Infer conventions
639
+
640
+ Read 3-5 representative source files from each detected layer to understand:
641
+ - Naming conventions (camelCase, snake_case, PascalCase)
642
+ - Import patterns
643
+ - Error handling patterns
644
+ - Testing patterns (framework, structure, mocking approach)
645
+ - API patterns (REST, GraphQL, tRPC)
646
+
647
+ ### 1.4 Present findings
648
+
649
+ Display the detected architecture to the user:
650
+
651
+ ```
652
+ ## Codebase Analysis
653
+
654
+ | Layer | Tech | Path |
655
+ |-------------|---------------------|---------------|
656
+ | Backend | FastAPI (Python) | backend/ |
657
+ | Frontend | React + TypeScript | frontend/ |
658
+ | Core | Python package | src/ |
659
+ | Tests | pytest | tests/ |
660
+ | Database | PostgreSQL | migrations/ |
661
+
662
+ ### CI Commands Detected
663
+ - Lint: `ruff check .`
664
+ - Format: `ruff format --check .`
665
+ - Test: `pytest tests/ -q`
666
+ - Frontend lint: `npm run lint`
667
+ - Frontend build: `npx tsc --noEmit`
668
+
669
+ ### Conventions Detected
670
+ - Python: snake_case, type hints, Pydantic models
671
+ - TypeScript: strict mode, functional components
672
+ - Testing: pytest fixtures with scope="function"
673
+
674
+ ### OSS Project Detection
675
+
676
+ Read `.specrails/setup-templates/.oss-detection.json` if it exists.
677
+
678
+ | Signal | Status |
679
+ |--------|--------|
680
+ | Public repository | [Yes / No / Unknown] |
681
+ | CI workflows (.github/workflows/) | [Yes / No] |
682
+ | CONTRIBUTING.md | [Yes / No] |
683
+ | **Result** | **OSS detected / Not detected / Could not check** |
684
+
685
+ If `is_oss: false` but at least one signal is `true`:
686
+ > "Some OSS signals were found but not all three. Is this an open-source project? (yes/no)"
687
+
688
+ If `.oss-detection.json` does not exist:
689
+ > "Is this an open-source project? (yes/no)"
690
+
691
+ When `IS_OSS=false` and no signals are present, skip OSS output entirely to avoid cluttering the display for non-OSS projects.
692
+
693
+ Store the final OSS determination as `IS_OSS` for use throughout the rest of setup.
694
+
695
+ [Confirm] [Modify] [Rescan]
696
+ ```
697
+
698
+ Wait for user confirmation. If they want to modify, ask what to change.
699
+
700
+ ---
701
+
702
+ ## Phase 2: User Personas & Product Discovery
703
+
704
+ ### 2.1 Ask about target users
705
+
706
+ Ask the user:
707
+
708
+ > If IS_OSS=true, prepend:
709
+ > "This is an OSS project. The **Maintainer** persona (Kai) is automatically included —
710
+ > you do not need to add 'open-source maintainers' to your list.
711
+ > Describe your other target user types below."
712
+
713
+ > **Who are the target users of your software?**
714
+ >
715
+ > Describe them in natural language. Examples:
716
+ > - "Developers who manage Kubernetes clusters"
717
+ > - "Small business owners tracking inventory and sales"
718
+ > - "Gamers who collect and trade digital items"
719
+ >
720
+ > I'll research the competitive landscape and create detailed personas
721
+ > with Value Proposition Canvas profiles for each user type.
722
+ >
723
+ > **How many distinct user types do you have?** (typically 2-3)
724
+
725
+ Wait for the user's response.
726
+
727
+ ### 2.2 Research competitive landscape
728
+
729
+ For each user type described, use WebSearch to research:
730
+
731
+ 1. **Existing tools** they use today (competitors)
732
+ 2. **Common pain points** reported in forums, Reddit, product reviews
733
+ 3. **Feature gaps** in current tools
734
+ 4. **Unmet needs** and workflow frustrations
735
+
736
+ Search queries to use (adapt to the domain):
737
+ - `"[domain] [user type] best tools 2025"`
738
+ - `"[domain] [user type] pain points frustrations"`
739
+ - `"[competitor name] missing features complaints"`
740
+ - `"[domain] management app feature comparison"`
741
+ - `site:reddit.com "[domain] [user type] what tool do you use"`
742
+
743
+ ### 2.3 Generate VPC personas
744
+
745
+ For each user type, generate a full Value Proposition Canvas persona file following the template at `.specrails/setup-templates/personas/persona.md`.
746
+
747
+ Each persona must include:
748
+ - **Profile**: Demographics, behaviors, tools used, spending patterns
749
+ - **Customer Jobs**: Functional, social, emotional (6-8 jobs)
750
+ - **Pains**: Graded by severity (Critical > High > Medium > Low) with 6-8 entries
751
+ - **Gains**: Graded by impact (High > Medium > Low) with 6-8 entries
752
+ - **Key Insight**: The #1 unmet need that this project can address
753
+ - **Sources**: Links to competitive analysis, forums, reviews used in research
754
+
755
+ ### 2.4 Present personas
756
+
757
+ Display each generated persona to the user:
758
+
759
+ ```
760
+ ## Generated Personas
761
+
762
+ ### Persona 1: "[Nickname]" — The [Role]
763
+ - Age: X-Y
764
+ - Key pain: [Critical pain]
765
+ - Key insight: [Main unmet need]
766
+
767
+ ### Persona 2: "[Nickname]" — The [Role]
768
+ - Age: X-Y
769
+ - Key pain: [Critical pain]
770
+ - Key insight: [Main unmet need]
771
+
772
+ [Accept] [Edit] [Regenerate]
773
+ ```
774
+
775
+ Wait for confirmation. If the user wants edits, apply them.
776
+
777
+ ### 2.5 Initialize agent config
778
+
779
+ Check for `.specrails/agents.yaml`:
780
+
781
+ 1. If the file **exists**:
782
+ - Read it
783
+ - Validate all `model:` values — only `opus`, `sonnet`, and `haiku` are valid
784
+ - If any value is invalid, warn the user and fall back to the template default for that agent
785
+ - Store as `AGENTS_CONFIG` for use in Phase 4
786
+
787
+ 2. If the file **does not exist**:
788
+ - Generate it with the default model assignments matching the template hard-coded values:
789
+
790
+ ```yaml
791
+ # specrails agent configuration
792
+ # Modify model assignments and other agent settings
793
+ # Valid models: opus, sonnet, haiku
794
+
795
+ defaults:
796
+ model: sonnet
797
+
798
+ agents:
799
+ sr-architect:
800
+ model: sonnet
801
+ sr-developer:
802
+ model: sonnet
803
+ sr-reviewer:
804
+ model: sonnet
805
+ sr-product-manager:
806
+ model: opus
807
+ sr-product-analyst:
808
+ model: haiku
809
+ sr-test-writer:
810
+ model: sonnet
811
+ sr-security-reviewer:
812
+ model: sonnet
813
+ sr-backend-developer:
814
+ model: sonnet
815
+ sr-frontend-developer:
816
+ model: sonnet
817
+ sr-backend-reviewer:
818
+ model: sonnet
819
+ sr-frontend-reviewer:
820
+ model: sonnet
821
+ sr-doc-sync:
822
+ model: sonnet
823
+ sr-merge-resolver:
824
+ model: sonnet
825
+ sr-performance-reviewer:
826
+ model: sonnet
827
+ ```
828
+
829
+ - Write this file to `.specrails/agents.yaml`
830
+ - Store as `AGENTS_CONFIG` for use in Phase 4
831
+ - Log: "Generated `.specrails/agents.yaml` with default model assignments"
832
+
833
+ ---
834
+
835
+ ## Phase 3: Configuration
836
+
837
+ ### 3.1 Agents to install
838
+
839
+ Present the available agents and let the user choose:
840
+
841
+ ```
842
+ ## Agent Selection
843
+
844
+ Which agents do you want to install?
845
+
846
+ | Agent | Purpose | Model | Required |
847
+ |-------|---------|-------|----------|
848
+ | sr-architect | Design features, create implementation plans | Sonnet | Yes |
849
+ | sr-developer (full-stack) | Implement features across all layers | Sonnet | Yes |
850
+ | sr-reviewer | CI/CD quality gate, fix issues | Sonnet | Yes |
851
+ | sr-test-writer | Generate unit, integration, and edge-case tests after implementation | Sonnet | Yes |
852
+ | sr-security-reviewer | Scan for secrets, OWASP vulnerabilities, hardcoded credentials | Sonnet | Yes |
853
+ | sr-product-manager | Product discovery, ideation, VPC evaluation | Opus | Recommended |
854
+ | sr-product-analyst | Read-only backlog analysis | Haiku | Recommended |
855
+ | sr-backend-developer | Specialized backend implementation | Sonnet | If backend layer exists |
856
+ | sr-frontend-developer | Specialized frontend implementation | Sonnet | If frontend layer exists |
857
+
858
+ [All] [Required only] [Custom selection]
859
+ ```
860
+
861
+ ### 3.2 Backlog provider
862
+
863
+ Ask the user how they want to manage their product backlog. Default is local — no external tools or accounts required:
864
+
865
+ ```
866
+ ## Backlog Provider
867
+
868
+ Use local ticket management or connect an external provider?
869
+
870
+ 1. **Local tickets** (default, recommended) — lightweight JSON-based ticket management built into the project.
871
+ No external tools or accounts required. Tickets stored in `.specrails/local-tickets.json`, version-controlled and diffable.
872
+ 2. **External provider** — connect GitHub Issues, JIRA, or disable backlog commands
873
+ ```
874
+
875
+ If the user selects **1** or presses Enter without typing anything: set `BACKLOG_PROVIDER=local` and proceed directly to **If Local Tickets** below. Do NOT ask about GitHub CLI, JIRA credentials, or any external provider configuration.
876
+
877
+ If the user selects **2**: display the secondary menu:
878
+
879
+ ```
880
+ ## External Backlog Provider
881
+
882
+ Which external provider?
883
+
884
+ 1. **Local tickets** (recommended) — lightweight JSON-based ticket management built into the project.
885
+ No external tools required. Tickets stored in `.specrails/local-tickets.json`, version-controlled and diffable.
886
+ 2. **GitHub Issues** — uses `gh` CLI to read/create issues with labels and VPC scores
887
+ 3. **JIRA** — uses JIRA CLI or REST API to read/create tickets in a JIRA project
888
+ 4. **None** — skip backlog commands (you can still use /implement with text descriptions)
889
+ ```
890
+
891
+ Wait for the user's choice. Set `BACKLOG_PROVIDER` to `local`, `github`, `jira`, or `none`.
892
+
893
+ #### If Local Tickets
894
+
895
+ No external tools or credentials required. Initialize the storage file:
896
+
897
+ 1. Copy `templates/local-tickets-schema.json` to `.specrails/local-tickets.json`
898
+ 2. Set `last_updated` to the current ISO-8601 timestamp
899
+
900
+ Store configuration in `.specrails/backlog-config.json`:
901
+ ```json
902
+ {
903
+ "provider": "local",
904
+ "write_access": true,
905
+ "git_auto": true
906
+ }
907
+ ```
908
+
909
+ Local tickets are always read-write — there is no "read only" mode since the file is local.
910
+
911
+ **Ticket schema** — each entry in the `tickets` map has these fields:
912
+
913
+ ```json
914
+ {
915
+ "id": 1,
916
+ "title": "Feature title",
917
+ "description": "Markdown description",
918
+ "status": "todo",
919
+ "priority": "medium",
920
+ "labels": ["area:frontend", "effort:medium"],
921
+ "assignee": null,
922
+ "prerequisites": [],
923
+ "metadata": {
924
+ "vpc_scores": {},
925
+ "effort_level": "Medium",
926
+ "user_story": "",
927
+ "area": ""
928
+ },
929
+ "comments": [],
930
+ "created_at": "<ISO-8601>",
931
+ "updated_at": "<ISO-8601>",
932
+ "created_by": "user",
933
+ "source": "manual"
934
+ }
935
+ ```
936
+
937
+ **Status values:** `todo`, `in_progress`, `done`, `cancelled`
938
+ **Priority values:** `critical`, `high`, `medium`, `low`
939
+ **Labels:** Freeform strings following the `area:*` and `effort:*` convention
940
+ **Source values:** `manual`, `get-backlog-specs`, `propose-spec`
941
+
942
+ **Advisory file locking protocol** (CLI agents and hub server must both follow this):
943
+
944
+ The `revision` counter in the JSON root enables optimistic concurrency — increment it on **every** write. The lock file prevents concurrent corruption:
945
+
946
+ 1. **Acquire lock:** Check for `.specrails/local-tickets.json.lock`
947
+ - If the file exists and its `timestamp` is less than 30 seconds old: wait 500ms and retry (max 5 attempts before aborting with an error)
948
+ - If the file exists and its `timestamp` is 30+ seconds old (stale): delete it and proceed
949
+ - If no lock file exists: proceed immediately
950
+ 2. **Create lock file:** Write `{"agent": "<agent-name-or-process>", "timestamp": "<ISO-8601>"}` to `.specrails/local-tickets.json.lock`
951
+ 3. **Minimal lock window:** Read the JSON → modify in memory → write back → release
952
+ 4. **Release lock:** Delete `.specrails/local-tickets.json.lock`
953
+ 5. **Always increment `revision`** by 1 and update `last_updated` on every successful write
954
+
955
+ The hub server uses `proper-lockfile` (or equivalent) to honor the same protocol via the `.lock` file path.
956
+
957
+ #### If GitHub Issues
958
+
959
+ - Verify `gh auth status` works. If not, warn and offer to skip.
960
+ - Ask about **access mode**:
961
+
962
+ ```
963
+ ## GitHub Issues — Access Mode
964
+
965
+ How should we interact with GitHub Issues?
966
+
967
+ 1. **Read & Write** (default) — read backlog, create new issues from product discovery,
968
+ close resolved issues, add comments on partial progress
969
+ 2. **Read only** — read backlog for prioritization, but don't create or modify issues.
970
+ Product discovery will propose ideas as output but won't sync them to GitHub.
971
+ ```
972
+
973
+ Set `BACKLOG_WRITE=true/false`.
974
+
975
+ - If write mode, ask if they want to create labels:
976
+ - `product-driven-backlog` (purple) — product feature ideas
977
+ - `area:*` labels for each detected layer/area
978
+ - `enhancement`, `bug`, `tech-debt`
979
+
980
+ #### If JIRA
981
+
982
+ First, check if JIRA CLI is installed:
983
+
984
+ ```bash
985
+ command -v jira &> /dev/null
986
+ ```
987
+
988
+ If not installed, offer to install it:
989
+
990
+ > JIRA CLI is not installed. There are several options:
991
+ >
992
+ > 1. **go-jira** (recommended) — lightweight CLI
993
+ > - macOS: `brew install go-jira`
994
+ > - Linux/other: `go install github.com/go-jira/jira/cmd/jira@latest`
995
+ > 2. **Atlassian CLI** — official but heavier
996
+ > - `npm install -g @atlassian/cli`
997
+ > 3. **Skip CLI, use REST API** — no CLI needed, uses `curl` with API token
998
+ >
999
+ > Which option? (1/2/3)
1000
+
1001
+ If the user chooses option 1 or 2, run the install command. If option 3, proceed with REST API mode.
1002
+
1003
+ Then ask for JIRA configuration:
1004
+
1005
+ > To connect to JIRA, I need:
1006
+ >
1007
+ > 1. **JIRA base URL** (e.g., `https://your-company.atlassian.net`)
1008
+ > 2. **Project key** (e.g., `PROJ`, `DECK`, `MYAPP`)
1009
+ > 3. **Authentication method**:
1010
+ > - **JIRA CLI** (`jira` command) — if already configured
1011
+ > - **API token** — stored in `.env` as `JIRA_API_TOKEN` and `JIRA_USER_EMAIL`
1012
+ >
1013
+ > Optional:
1014
+ > - **Custom issue type** for backlog items (default: "Story")
1015
+ > - **Custom fields** for VPC scores (or use labels/description)
1016
+
1017
+ Then ask about **access mode**:
1018
+
1019
+ ```
1020
+ ## JIRA — Access Mode
1021
+
1022
+ How should we interact with JIRA?
1023
+
1024
+ 1. **Read & Write** — read tickets for implementation context, create new tickets
1025
+ from product discovery, add a comment to tickets when implementation is complete
1026
+ 2. **Read only** — read tickets for implementation context, but never create or
1027
+ modify tickets. Product discovery will propose ideas as output only. After
1028
+ implementation, the pipeline will show what to update manually but won't
1029
+ touch JIRA.
1030
+ ```
1031
+
1032
+ Set `BACKLOG_WRITE=true/false`.
1033
+
1034
+ <!-- no separate template — this file IS the source (install.sh copies commands/setup.md directly) -->
1035
+
1036
+ #### Project Label
1037
+
1038
+ After the access mode selection, ask:
1039
+
1040
+ > **Project Label (optional but recommended)**
1041
+ >
1042
+ > JIRA teams often tag all tickets for a product with a project label
1043
+ > (e.g., `PROJECT-specrails`, `PLATFORM`, `MOBILE`). This label is applied
1044
+ > to every ticket the backlog pipeline creates — making it easy to filter all
1045
+ > AI-generated backlog items across JIRA.
1046
+ >
1047
+ > Enter a project label, or press Enter to skip:
1048
+
1049
+ If the user enters a label: set `PROJECT_LABEL=<value>`.
1050
+ If the user skips: set `PROJECT_LABEL=""`.
1051
+
1052
+ #### Epic Link Field
1053
+
1054
+ Ask:
1055
+
1056
+ > **Epic Link Field (optional — advanced)**
1057
+ >
1058
+ > JIRA Next-Gen (team-managed) projects link stories to epics using the `parent`
1059
+ > field. JIRA Classic (company-managed) projects use `Epic Link` (customfield_10014).
1060
+ >
1061
+ > Which does your project use?
1062
+ > 1. `parent` — Next-Gen / team-managed **(default)**
1063
+ > 2. `customfield_10014` — Classic / company-managed
1064
+
1065
+ Set `EPIC_LINK_FIELD` to `parent` or `customfield_10014`. Default: `parent`.
1066
+
1067
+ Store the full configuration in `.specrails/backlog-config.json`:
1068
+ ```json
1069
+ {
1070
+ "provider": "jira",
1071
+ "write_access": true,
1072
+ "jira_base_url": "https://your-company.atlassian.net",
1073
+ "jira_project_key": "PROJ",
1074
+ "issue_type": "Story",
1075
+ "auth_method": "api_token",
1076
+ "cli_installed": true,
1077
+ "project_label": "<PROJECT_LABEL or empty string>",
1078
+ "epic_link_field": "parent",
1079
+ "epic_mapping": {}
1080
+ }
1081
+ ```
1082
+
1083
+ #### If None
1084
+
1085
+ - Skip `/specrails:get-backlog-specs` and `/specrails:auto-propose-backlog-specs` commands.
1086
+ - The `/specrails:implement` command will still work with text descriptions.
1087
+
1088
+ ### 3.3 Git & shipping workflow
1089
+
1090
+ Ask how the user wants to handle git operations after implementation:
1091
+
1092
+ ```
1093
+ ## Git & Shipping
1094
+
1095
+ After implementation is complete, how should we handle shipping?
1096
+
1097
+ 1. **Automatic** (default) — create branch, commit changes, push, and open a PR
1098
+ (requires GitHub CLI for PRs, otherwise prints a compare URL)
1099
+ 2. **Manual** — stop after implementation and review. You handle branching,
1100
+ committing, and PR creation yourself. The pipeline will show a summary
1101
+ of all changes but won't touch git.
1102
+ ```
1103
+
1104
+ Set `GIT_AUTO=true/false`.
1105
+
1106
+ If automatic, also check if `gh` is authenticated (for PR creation). If not, warn that PRs will be skipped but commits and push will still work.
1107
+
1108
+ ### 3.4 Commands to install
1109
+
1110
+ ```
1111
+ ## Command Selection
1112
+
1113
+ | Command | Purpose | Requires |
1114
+ |---------|---------|----------|
1115
+ | /specrails:implement | Full pipeline: sr-architect → sr-developer → sr-reviewer → ship | sr-architect + sr-developer + sr-reviewer |
1116
+ | /specrails:batch-implement | Orchestrate multiple features in dependency-aware waves | sr-architect + sr-developer + sr-reviewer |
1117
+ | /specrails:propose-spec | Interactively propose and refine a feature spec, then create a GitHub issue | GitHub CLI |
1118
+ | /specrails:get-backlog-specs | View prioritized backlog with VPC scores | sr-product-analyst + Backlog provider |
1119
+ | /specrails:auto-propose-backlog-specs | Generate new feature ideas via product discovery | sr-product-manager + Backlog provider |
1120
+ | /specrails:compat-check | Snapshot API surface and detect breaking changes | None |
1121
+ | /specrails:refactor-recommender | Scan for refactoring opportunities ranked by impact/effort | None |
1122
+ | /specrails:why | Search past architectural decisions from agent memory | None |
1123
+
1124
+ [All] [Custom selection]
1125
+ ```
1126
+
1127
+ Note: If `BACKLOG_PROVIDER=none`, the backlog commands are not offered.
1128
+
1129
+ ### 3.5 Confirm configuration
1130
+
1131
+ Display the full configuration summary including access modes:
1132
+
1133
+ ```
1134
+ ## Configuration Summary
1135
+
1136
+ | Setting | Value |
1137
+ |---------|-------|
1138
+ | Backlog provider | GitHub Issues / JIRA / None |
1139
+ | Backlog access | Read & Write / Read only |
1140
+ | Project label (JIRA) | PROJECT-specrails / (none) |
1141
+ | Epic link field (JIRA) | parent / customfield_10014 |
1142
+ | Git workflow | Automatic / Manual |
1143
+ | Agents | [list] |
1144
+ | Commands | [list] |
1145
+ | Personas | [count] personas |
1146
+
1147
+ Note: The `Project label (JIRA)` and `Epic link field (JIRA)` rows are only shown when `BACKLOG_PROVIDER=jira`.
1148
+
1149
+ [Confirm] [Modify]
1150
+ ```
1151
+
1152
+ Wait for final confirmation.
1153
+
1154
+ ---
1155
+
1156
+ ## Phase 4: Generate Files
1157
+
1158
+ Read each template from `.specrails/setup-templates/` and generate the final files adapted to this project. Use the codebase analysis from Phase 1, personas from Phase 2, and configuration from Phase 3.
1159
+
1160
+ **Provider detection (required before any file generation):** Read `.specrails/setup-templates/.provider-detection.json` to determine `cli_provider` (`"claude"` or `"codex"`) and `specrails_dir` (`.claude` or `.codex`). All output paths in Phase 4 use `$SPECRAILS_DIR` as the base directory. If the file is missing, fall back to `cli_provider = "claude"` and `specrails_dir = ".claude"`.
1161
+
1162
+ ### 4.1 Generate agents
1163
+
1164
+ For each selected agent, read the template and generate the adapted version.
1165
+
1166
+ **Template → Output mapping:**
1167
+
1168
+ **If `cli_provider == "claude"` (default):**
1169
+ - `.specrails/setup-templates/agents/sr-architect.md` → `.claude/agents/sr-architect.md`
1170
+ - `.specrails/setup-templates/agents/sr-developer.md` → `.claude/agents/sr-developer.md`
1171
+ - `.specrails/setup-templates/agents/sr-reviewer.md` → `.claude/agents/sr-reviewer.md`
1172
+ - `.specrails/setup-templates/agents/sr-test-writer.md` → `.claude/agents/sr-test-writer.md`
1173
+ - `.specrails/setup-templates/agents/sr-security-reviewer.md` → `.claude/agents/sr-security-reviewer.md`
1174
+ - `.specrails/setup-templates/agents/sr-product-manager.md` → `.claude/agents/sr-product-manager.md`
1175
+ - `.specrails/setup-templates/agents/sr-product-analyst.md` → `.claude/agents/sr-product-analyst.md`
1176
+ - `.specrails/setup-templates/agents/sr-backend-developer.md` → `.claude/agents/sr-backend-developer.md` (if backend layer)
1177
+ - `.specrails/setup-templates/agents/sr-frontend-developer.md` → `.claude/agents/sr-frontend-developer.md` (if frontend layer)
1178
+
1179
+ **If `cli_provider == "codex"`:**
1180
+ - `.specrails/setup-templates/agents/sr-architect.md` → `.codex/agents/sr-architect.toml`
1181
+ - `.specrails/setup-templates/agents/sr-developer.md` → `.codex/agents/sr-developer.toml`
1182
+ - `.specrails/setup-templates/agents/sr-reviewer.md` → `.codex/agents/sr-reviewer.toml`
1183
+ - `.specrails/setup-templates/agents/sr-test-writer.md` → `.codex/agents/sr-test-writer.toml`
1184
+ - `.specrails/setup-templates/agents/sr-security-reviewer.md` → `.codex/agents/sr-security-reviewer.toml`
1185
+ - `.specrails/setup-templates/agents/sr-product-manager.md` → `.codex/agents/sr-product-manager.toml`
1186
+ - `.specrails/setup-templates/agents/sr-product-analyst.md` → `.codex/agents/sr-product-analyst.toml`
1187
+ - `.specrails/setup-templates/agents/sr-backend-developer.md` → `.codex/agents/sr-backend-developer.toml` (if backend layer)
1188
+ - `.specrails/setup-templates/agents/sr-frontend-developer.md` → `.codex/agents/sr-frontend-developer.toml` (if frontend layer)
1189
+
1190
+ When generating each agent:
1191
+ 1. Read the template
1192
+ 2. Replace all `{{PLACEHOLDER}}` values with project-specific content:
1193
+ - `{{PROJECT_NAME}}` → project name
1194
+ - `{{ARCHITECTURE_DIAGRAM}}` → detected architecture
1195
+ - `{{LAYER_TAGS}}` → detected layer tags (e.g., `[backend]`, `[frontend]`, `[api]`, `[mobile]`)
1196
+ - `{{CI_COMMANDS_BACKEND}}` → backend CI commands from Phase 1
1197
+ - `{{CI_COMMANDS_FRONTEND}}` → frontend CI commands from Phase 1
1198
+ - `{{LAYER_CONVENTIONS}}` → detected conventions per layer
1199
+ - `{{PERSONA_NAMES}}` → names from generated personas
1200
+ - `{{PERSONA_FILES}}` → paths to persona files
1201
+ - `{{DOMAIN_EXPERTISE}}` → domain knowledge from Phase 2 research
1202
+ - `{{COMPETITIVE_LANDSCAPE}}` → competitors discovered in Phase 2
1203
+ - `{{KEY_FILE_PATHS}}` → important file paths detected in Phase 1
1204
+ - `{{WARNINGS}}` → project-specific warnings (from existing CLAUDE.md or detected)
1205
+ - `{{MEMORY_PATH}}` → agent memory directory path (e.g., `$SPECRAILS_DIR/agent-memory/sr-<agent-name>/`)
1206
+ - `{{TECH_EXPERTISE}}` → detected languages, frameworks, and test frameworks from Phase 1
1207
+ - `{{LAYER_CLAUDE_MD_PATHS}}` → comma-separated paths to per-layer rules files (e.g., `$SPECRAILS_DIR/rules/backend.md`, `$SPECRAILS_DIR/rules/frontend.md`)
1208
+ - `{{SECURITY_EXEMPTIONS_PATH}}` → `$SPECRAILS_DIR/security-exemptions.yaml`
1209
+ 3. Resolve the agent's model using `AGENTS_CONFIG` (loaded in Phase 2.5):
1210
+ - Check `AGENTS_CONFIG.agents.<agent-name>.model` (per-agent override)
1211
+ - If not present, check `AGENTS_CONFIG.defaults.model` (global default)
1212
+ - If `AGENTS_CONFIG` was not loaded (e.g., re-run without config), use the model from the template frontmatter (current behavior)
1213
+ - Replace the `model:` field in the YAML frontmatter with the resolved value before writing
1214
+ 4. Write the final file in the format for the active provider:
1215
+
1216
+ **If `cli_provider == "claude"`:** Write as Markdown with YAML frontmatter — the template file as-is (frontmatter preserved).
1217
+
1218
+ **If `cli_provider == "codex"`:** Convert to TOML format:
1219
+ - Extract YAML frontmatter fields: `name`, `description`, `model`
1220
+ - Extract the body content (everything after the closing `---` of the frontmatter)
1221
+ - Map the `model` field: `sonnet` → `codex-mini-latest`, `opus` → `o3`, `haiku` → `codex-mini-latest`
1222
+ - Write a `.toml` file with this structure:
1223
+ ```toml
1224
+ name = "<name from frontmatter>"
1225
+ description = "<description from frontmatter, escaped for TOML>"
1226
+ model = "codex-mini-latest"
1227
+ prompt = """
1228
+ <body content after placeholder substitution>
1229
+ """
1230
+ ```
1231
+
1232
+ ### 4.2 Generate personas
1233
+
1234
+ If IS_OSS=true:
1235
+ 1. Copy `.specrails/setup-templates/personas/the-maintainer.md` to `$SPECRAILS_DIR/agents/personas/the-maintainer.md`
1236
+ 2. Log: "Maintainer persona included"
1237
+ 3. Set MAINTAINER_INCLUDED=true for use in template substitution
1238
+ 4. Set `{{MAINTAINER_PERSONA_LINE}}` = `- \`$SPECRAILS_DIR/agents/personas/the-maintainer.md\` — "Kai" the Maintainer (open-source maintainer)`
1239
+ 5. Increment `{{PERSONA_COUNT}}` by 1 to account for the Maintainer
1240
+
1241
+ If IS_OSS=false:
1242
+ - Set `{{MAINTAINER_PERSONA_LINE}}` = *(empty string)*
1243
+
1244
+ Then for each user-defined VPC persona from Phase 2.3:
1245
+
1246
+ Write each persona to `$SPECRAILS_DIR/agents/personas/`:
1247
+ - Use the VPC personas generated in Phase 2
1248
+ - File naming: kebab-case of persona nickname (e.g., `the-developer.md`, `the-admin.md`)
1249
+
1250
+ ### 4.3 Generate commands / skills
1251
+
1252
+ For each selected command, read the template and adapt.
1253
+
1254
+ **If `cli_provider == "claude"` (default):**
1255
+ - `.specrails/setup-templates/commands/specrails/implement.md` → `.claude/commands/specrails/implement.md`
1256
+ - `.specrails/setup-templates/commands/specrails/batch-implement.md` → `.claude/commands/specrails/batch-implement.md`
1257
+ - `.specrails/setup-templates/commands/specrails/propose-spec.md` → `.claude/commands/specrails/propose-spec.md`
1258
+ - `.specrails/setup-templates/commands/specrails/get-backlog-specs.md` → `.claude/commands/specrails/get-backlog-specs.md` (if `BACKLOG_PROVIDER != none`)
1259
+ - `.specrails/setup-templates/commands/specrails/auto-propose-backlog-specs.md` → `.claude/commands/specrails/auto-propose-backlog-specs.md` (if `BACKLOG_PROVIDER != none`)
1260
+ - `.specrails/setup-templates/commands/specrails/compat-check.md` → `.claude/commands/specrails/compat-check.md`
1261
+ - `.specrails/setup-templates/commands/specrails/refactor-recommender.md` → `.claude/commands/specrails/refactor-recommender.md`
1262
+ - `.specrails/setup-templates/commands/specrails/why.md` → `.claude/commands/specrails/why.md`
1263
+ - `.specrails/setup-templates/commands/specrails/reconfig.md` → `.claude/commands/specrails/reconfig.md`
1264
+
1265
+ **If `cli_provider == "codex"`:**
1266
+ - `.specrails/setup-templates/skills/sr-implement/SKILL.md` → `.agents/skills/sr-implement/SKILL.md`
1267
+ - `.specrails/setup-templates/skills/sr-batch-implement/SKILL.md` → `.agents/skills/sr-batch-implement/SKILL.md`
1268
+ - `.specrails/setup-templates/commands/specrails/propose-spec.md` → `.agents/skills/sr-propose-spec/SKILL.md` (wrap with YAML frontmatter if no skill template exists)
1269
+ - `.specrails/setup-templates/commands/specrails/get-backlog-specs.md` → `.agents/skills/sr-get-backlog-specs/SKILL.md` (if `BACKLOG_PROVIDER != none`; wrap with frontmatter)
1270
+ - `.specrails/setup-templates/commands/specrails/auto-propose-backlog-specs.md` → `.agents/skills/sr-auto-propose-backlog-specs/SKILL.md` (if `BACKLOG_PROVIDER != none`; wrap with frontmatter)
1271
+ - `.specrails/setup-templates/skills/sr-compat-check/SKILL.md` → `.agents/skills/sr-compat-check/SKILL.md`
1272
+ - `.specrails/setup-templates/skills/sr-refactor-recommender/SKILL.md` → `.agents/skills/sr-refactor-recommender/SKILL.md`
1273
+ - `.specrails/setup-templates/skills/sr-why/SKILL.md` → `.agents/skills/sr-why/SKILL.md`
1274
+ - `.specrails/setup-templates/commands/specrails/reconfig.md` → `.agents/skills/sr-reconfig/SKILL.md` (wrap with YAML frontmatter)
1275
+
1276
+ **Codex skill frontmatter wrapping:** When a dedicated skill template does not exist in `.specrails/setup-templates/skills/` for a command, generate the `SKILL.md` by prepending YAML frontmatter to the command content:
1277
+ ```yaml
1278
+ ---
1279
+ name: sr-<name>
1280
+ description: "<one-line description from the command's first heading>"
1281
+ license: MIT
1282
+ compatibility: "Requires git."
1283
+ metadata:
1284
+ author: specrails
1285
+ version: "1.0"
1286
+ ---
1287
+ ```
1288
+
1289
+ For both providers, create the output directory before writing (`mkdir -p` for `.claude/commands/specrails/` or `.agents/skills/sr-<name>/`).
1290
+
1291
+ Adapt:
1292
+ - CI commands to match detected stack
1293
+ - **Persona references** to match generated personas (see substitution rules below)
1294
+ - File paths to match project structure
1295
+ - Layer tags to match detected layers
1296
+ - **Backlog provider commands** based on `BACKLOG_PROVIDER`:
1297
+
1298
+ #### Backlog command persona placeholder substitution
1299
+
1300
+ When adapting `auto-propose-backlog-specs.md` and `get-backlog-specs.md`, substitute the persona placeholders based on the full persona set (user-generated personas + Maintainer if `IS_OSS=true`):
1301
+
1302
+ | Placeholder | Substitution rule |
1303
+ |-------------|------------------|
1304
+ | `{{PERSONA_FILE_READ_LIST}}` | One bullet per persona file: `- Read \`$SPECRAILS_DIR/agents/personas/{name}.md\`` |
1305
+ | `{{PERSONA_SCORE_HEADERS}}` | Column headers for each persona nickname: e.g., `Alex \| Sara \| Kai` |
1306
+ | `{{PERSONA_SCORE_SEPARATORS}}` | One `------` separator per persona column |
1307
+ | `{{PERSONA_FIT_FORMAT}}` | Inline score display: e.g., `Alex: X/5, Sara: X/5, Kai: X/5` |
1308
+ | `{{PERSONA_VPC_SECTIONS}}` | One VPC section block per persona (see format below) |
1309
+ | `{{MAX_SCORE}}` | Total max score = 5 × number of personas (e.g., `15` for 3 personas) |
1310
+ | `{{PERSONA_NAMES_WITH_ROLES}}` | Comma-separated: e.g., `Alex (Lead Dev), Sara (Product Founder), Kai (OSS Maintainer)` |
1311
+
1312
+ **`{{PERSONA_VPC_SECTIONS}}` format** — repeat for each persona in order:
1313
+ ```
1314
+ ### "{Nickname}" — The {Role} (X/5)
1315
+ - **Jobs addressed**: {list}
1316
+ - **Pains relieved**: {list with severity}
1317
+ - **Gains created**: {list with impact}
1318
+ ```
1319
+
1320
+ **Kai inclusion rule**: When `IS_OSS=true`, Kai (`sr-the-maintainer.md`) is always the last entry in persona lists and the rightmost column in scoring tables. Kai uses the evaluation criteria defined in `.claude/agents/personas/sr-the-maintainer.md` — features score high (4-5/5) for Kai when they reduce async review burden, enforce project-specific conventions, or automate release/dependency coordination; features score low (0-1/5) when they add configuration complexity or require paid tiers.
1321
+
1322
+ **When `IS_OSS=false`**: All Kai-related persona references are omitted. `{{MAX_SCORE}}` reduces by 5. Tables and inline scores contain only user-generated personas.
1323
+
1324
+ #### Local Tickets (`BACKLOG_PROVIDER=local`)
1325
+
1326
+ For the local provider, backlog placeholders resolve to **inline file-operation instructions** embedded in the generated command markdown — not shell commands. Agents execute these by reading/writing `.specrails/local-tickets.json` directly using their file tools.
1327
+
1328
+ All write operations must follow the **advisory file locking protocol** defined in Phase 3.2. Always increment `revision` and update `last_updated` on every write.
1329
+
1330
+ | Placeholder | Substituted value |
1331
+ |-------------|-------------------|
1332
+ | `{{BACKLOG_PROVIDER_NAME}}` | `Local Tickets` |
1333
+ | `{{BACKLOG_PREFLIGHT}}` | `[[ -f ".specrails/local-tickets.json" ]] && echo "Local tickets storage: OK" \|\| echo "WARNING: .specrails/local-tickets.json not found — run /specrails:enrich to initialize"` |
1334
+ | `{{BACKLOG_FETCH_CMD}}` | Read `.specrails/local-tickets.json`. Parse the `tickets` map and return all entries where `status` is `"todo"` or `"in_progress"`. |
1335
+ | `{{BACKLOG_FETCH_ALL_CMD}}` | Read `.specrails/local-tickets.json`. Parse the `tickets` map and return all entries regardless of status. |
1336
+ | `{{BACKLOG_FETCH_CLOSED_CMD}}` | Read `.specrails/local-tickets.json`. Parse the `tickets` map and return all entries where `status` is `"done"` or `"cancelled"`. |
1337
+ | `{{BACKLOG_VIEW_CMD}}` | Read `.specrails/local-tickets.json`. Parse JSON and return the full ticket object at `tickets["{id}"]`, or an error if not found. |
1338
+ | `{{BACKLOG_CREATE_CMD}}` | Write to `.specrails/local-tickets.json` using the advisory locking protocol: acquire lock → read file → set `id = next_id`, increment `next_id`, set all ticket fields, set `created_at` and `updated_at` to now, bump `revision`, update `last_updated` → write → release lock. |
1339
+ | `{{BACKLOG_UPDATE_CMD}}` | Write to `.specrails/local-tickets.json` using the advisory locking protocol: acquire lock → read file → update fields in `tickets["{id}"]`, set `updated_at` to now, bump `revision`, update `last_updated` → write → release lock. |
1340
+ | `{{BACKLOG_DELETE_CMD}}` | Write to `.specrails/local-tickets.json` using the advisory locking protocol: acquire lock → read file → delete `tickets["{id}"]`, bump `revision`, update `last_updated` → write → release lock. |
1341
+ | `{{BACKLOG_COMMENT_CMD}}` | Write to `.specrails/local-tickets.json` using the advisory locking protocol: acquire lock → read file → append `{"author": "<agent-name>", "body": "<comment>", "created_at": "<ISO-8601>"}` to `tickets["{id}"].comments` (create the array if absent), set `updated_at` to now, bump `revision`, update `last_updated` → write → release lock. |
1342
+ | `{{BACKLOG_PARTIAL_COMMENT_CMD}}` | Same as `{{BACKLOG_COMMENT_CMD}}` but append `{"author": "<agent-name>", "body": "<comment>", "type": "progress", "created_at": "<ISO-8601>"}`. |
1343
+ | `{{BACKLOG_INIT_LABELS_CMD}}` | No label initialization required. Local tickets use freeform label strings. Standard label conventions: `area:frontend`, `area:backend`, `area:api`, `effort:low`, `effort:medium`, `effort:high`. |
1344
+
1345
+ #### GitHub Issues (`BACKLOG_PROVIDER=github`)
1346
+ - Issue fetch: `gh issue list --label "product-driven-backlog" --state open --limit 100 --json number,title,labels,body`
1347
+ - Issue create: `gh issue create --title "..." --label "..." --body "..."`
1348
+ - Issue view: `gh issue view {number} --json number,title,labels,body`
1349
+ - Issue label names to match project areas
1350
+ - Pre-flight check: `gh auth status`
1351
+
1352
+ #### JIRA (`BACKLOG_PROVIDER=jira`)
1353
+ - Issue fetch: `jira issue list --project {{JIRA_PROJECT_KEY}} --type Story --label get-backlog-specs --status "To Do" --plain` or equivalent JIRA REST API call via curl:
1354
+ ```bash
1355
+ curl -s -u "$JIRA_USER_EMAIL:$JIRA_API_TOKEN" \
1356
+ "{{JIRA_BASE_URL}}/rest/api/3/search?jql=project={{JIRA_PROJECT_KEY}} AND labels=get-backlog-specs AND status='To Do'&fields=summary,description,labels,priority"
1357
+ ```
1358
+ - Issue create: `jira issue create --project {{JIRA_PROJECT_KEY}} --type Story --summary "..." --label get-backlog-specs --description "..."` or equivalent REST API call
1359
+ - Issue view: `jira issue view {key}` or REST API
1360
+ - VPC scores stored in the issue description body (same markdown format, parsed from description)
1361
+ - Pre-flight check: `jira me` or test API connectivity
1362
+ - Store JIRA config in `.specrails/backlog-config.json`:
1363
+ ```json
1364
+ {
1365
+ "provider": "jira",
1366
+ "jira_base_url": "https://your-company.atlassian.net",
1367
+ "jira_project_key": "PROJ",
1368
+ "issue_type": "Story",
1369
+ "auth_method": "api_token"
1370
+ }
1371
+ ```
1372
+
1373
+ The command templates use `{{BACKLOG_FETCH_CMD}}`, `{{BACKLOG_CREATE_CMD}}`, `{{BACKLOG_VIEW_CMD}}`, `{{BACKLOG_PREFLIGHT}}`, and related placeholders that get filled with the provider-specific commands (for `local`) or instructions (for `github`, `jira`). The `{{BACKLOG_PROVIDER_NAME}}` placeholder is substituted with a human-readable provider label in all three cases.
1374
+
1375
+ ### 4.4 Generate rules
1376
+
1377
+ For each detected layer, read the layer rule template and generate a layer-specific rules file:
1378
+ - `.specrails/setup-templates/rules/layer.md` → `$SPECRAILS_DIR/rules/{layer-name}.md`
1379
+
1380
+ Each rule file must:
1381
+ - Have the correct `paths:` frontmatter matching the layer's directory
1382
+ - Contain conventions specific to that layer (from Phase 1 analysis)
1383
+ - Reference actual file paths and patterns from the codebase
1384
+
1385
+ ### 4.5 Generate root instructions file
1386
+
1387
+ **If `cli_provider == "claude"`:** If no `CLAUDE.md` exists, generate one from the template. If one already exists, **merge** — add the agent workflow sections without removing existing content.
1388
+
1389
+ **If `cli_provider == "codex"`:** If no `AGENTS.md` exists, generate one from the template. If one already exists, **merge** — add the agent workflow sections without removing existing content.
1390
+
1391
+ ### 4.6 Generate settings
1392
+
1393
+ Read `.specrails/setup-templates/.provider-detection.json` (written by `install.sh`) to determine `cli_provider` (`"claude"` or `"codex"`).
1394
+
1395
+ **If `cli_provider == "claude"` (default):**
1396
+
1397
+ Create or merge `.claude/settings.json` with permissions for:
1398
+ - All detected CI commands
1399
+ - Git operations
1400
+ - OpenSpec CLI (if installed)
1401
+ - GitHub CLI (if available)
1402
+ - Language-specific tools (python, npm, cargo, go, etc.)
1403
+
1404
+ **If `cli_provider == "codex"`:**
1405
+
1406
+ 1. Read `.specrails/setup-templates/settings/codex-config.toml`. Write it to `.codex/config.toml` as-is (no substitutions needed — the TOML is static).
1407
+
1408
+ 2. Read `.specrails/setup-templates/settings/codex-rules.star`. Replace `{{CODEX_SHELL_RULES}}` with Starlark `prefix_rule(...)` lines for each detected tool allowance:
1409
+
1410
+ | Detected tool/command | Starlark rule |
1411
+ |----------------------|---------------|
1412
+ | OpenSpec CLI (`openspec`) | `prefix_rule(pattern=["openspec"], decision="allow")` |
1413
+ | Python (`python`, `pip`) | `prefix_rule(pattern=["python"], decision="allow")`<br>`prefix_rule(pattern=["pip"], decision="allow")` |
1414
+ | npm (`npm`) | `prefix_rule(pattern=["npm"], decision="allow")` |
1415
+ | Cargo (`cargo`) | `prefix_rule(pattern=["cargo"], decision="allow")` |
1416
+ | Go (`go`) | `prefix_rule(pattern=["go"], decision="allow")` |
1417
+ | Any detected CI command | `prefix_rule(pattern=["<cmd>"], decision="allow")` |
1418
+
1419
+ Write the rendered file to `.codex/rules/default.rules`.
1420
+
1421
+ ```bash
1422
+ mkdir -p .codex/rules
1423
+ ```
1424
+
1425
+ If `cli_provider` cannot be determined (file missing), fall back to `"claude"` behavior.
1426
+
1427
+ ### 4.7 Initialize agent memory
1428
+
1429
+ Create memory directories for each installed agent using the provider-aware base directory:
1430
+
1431
+ ```bash
1432
+ mkdir -p $SPECRAILS_DIR/agent-memory/sr-{agent-name}/
1433
+ ```
1434
+
1435
+ Each gets an empty `MEMORY.md` that will be populated during usage.
1436
+
1437
+ ---
1438
+
1439
+ ## Phase 5: Cleanup & Summary
1440
+
1441
+ ### 5.1 Remove all scaffolding artifacts
1442
+
1443
+ The setup process installed temporary files that are only needed during installation. Remove them all now that the final files have been generated.
1444
+
1445
+ ```bash
1446
+ # 1. Remove setup templates (used as structural references during generation)
1447
+ rm -rf .specrails/setup-templates/
1448
+
1449
+ # 2. Remove the /specrails:enrich command itself — it's a one-time installer, not a permanent command
1450
+ rm -f .claude/commands/enrich.md
1451
+
1452
+ # 3. Remove the specrails/ directory from the repo if it exists at the root
1453
+ # (it was only needed for install.sh and templates — everything is now in .claude/)
1454
+ # NOTE: Only remove if it's inside this repo. Ask the user if unsure.
1455
+ ```
1456
+
1457
+ **What gets removed:**
1458
+ | Artifact | Why |
1459
+ |----------|-----|
1460
+ | `.specrails/setup-templates/` | Temporary — templates already rendered into final files |
1461
+ | `.claude/commands/enrich.md` | One-time installer — running it again would overwrite customized agents |
1462
+
1463
+ **What to do with `specrails/`:**
1464
+
1465
+ The `specrails/` directory should NOT be committed to the target repo — it's an installer tool, not part of the project. Always add it to `.gitignore`:
1466
+
1467
+ ```bash
1468
+ # Add specrails/ to .gitignore if not already there
1469
+ if ! grep -q '^specrails/' .gitignore 2>/dev/null; then
1470
+ echo '' >> .gitignore
1471
+ echo '# specrails installer (one-time setup tool, not part of the project)' >> .gitignore
1472
+ echo 'specrails/' >> .gitignore
1473
+ fi
1474
+ ```
1475
+
1476
+ Then ask the user:
1477
+
1478
+ > `specrails/` has been added to `.gitignore`. Do you also want to delete it?
1479
+ >
1480
+ > 1. **Keep it** (default) — stays locally in case you want to re-run setup or install in other repos
1481
+ > 2. **Delete it** — everything is installed, you don't need it anymore
1482
+
1483
+ Apply the user's choice.
1484
+
1485
+ ### 5.2 Verify clean state
1486
+
1487
+ After cleanup, verify that only the intended files remain:
1488
+
1489
+ ```bash
1490
+ # These should exist (the actual system) — use $SPECRAILS_DIR from .provider-detection.json:
1491
+ # If cli_provider == "claude":
1492
+ ls .claude/agents/sr-*.md
1493
+ ls .claude/agents/personas/*.md
1494
+ ls .claude/commands/specrails/*.md
1495
+ ls .claude/rules/*.md
1496
+ ls .claude/agent-memory/
1497
+
1498
+ # If cli_provider == "codex":
1499
+ ls .codex/agents/sr-*.toml
1500
+ ls .codex/agents/personas/*.md
1501
+ ls .agents/skills/sr-*/SKILL.md
1502
+ ls .codex/rules/*.md
1503
+ ls .codex/agent-memory/
1504
+
1505
+ # These should NOT exist (scaffolding):
1506
+ # .specrails/setup-templates/ — GONE
1507
+ # If cli_provider == "claude": $SPECRAILS_DIR/commands/enrich.md — GONE
1508
+ # If cli_provider == "codex": .agents/skills/setup/ — GONE (installer scaffold, not a generated sr-skill)
1509
+ ```
1510
+
1511
+ If any scaffolding artifact remains, remove it.
1512
+
1513
+ ### 5.3 Summary
1514
+
1515
+ Display the complete installation summary:
1516
+
1517
+ ```
1518
+ ## Setup Complete
1519
+
1520
+ ### Agents Installed
1521
+ | Agent | File | Model |
1522
+ |-------|------|-------|
1523
+ [If cli_provider == "claude":]
1524
+ | sr-architect | .claude/agents/sr-architect.md | Sonnet |
1525
+ | sr-developer | .claude/agents/sr-developer.md | Sonnet |
1526
+ | sr-reviewer | .claude/agents/sr-reviewer.md | Sonnet |
1527
+ | sr-test-writer | .claude/agents/sr-test-writer.md | Sonnet |
1528
+ | sr-security-reviewer | .claude/agents/sr-security-reviewer.md | Sonnet |
1529
+ | sr-product-manager | .claude/agents/sr-product-manager.md | Opus |
1530
+ [If cli_provider == "codex":]
1531
+ | sr-architect | .codex/agents/sr-architect.toml | codex-mini-latest |
1532
+ | sr-developer | .codex/agents/sr-developer.toml | codex-mini-latest |
1533
+ | sr-reviewer | .codex/agents/sr-reviewer.toml | codex-mini-latest |
1534
+ | sr-test-writer | .codex/agents/sr-test-writer.toml | codex-mini-latest |
1535
+ | sr-security-reviewer | .codex/agents/sr-security-reviewer.toml | codex-mini-latest |
1536
+ | sr-product-manager | .codex/agents/sr-product-manager.toml | o3 |
1537
+
1538
+ ### Personas Created
1539
+ | Persona | File | Source |
1540
+ |---------|------|--------|
1541
+ [If IS_OSS=true:]
1542
+ | "Kai" — The Maintainer | $SPECRAILS_DIR/agents/personas/sr-the-maintainer.md | Auto-included (OSS) |
1543
+ [For each user-generated persona:]
1544
+ | "[Name]" — The [Role] | $SPECRAILS_DIR/agents/personas/[name].md | Generated |
1545
+
1546
+ ### Commands / Skills Installed
1547
+ [If cli_provider == "claude":]
1548
+ | Command | File |
1549
+ |---------|------|
1550
+ | /specrails:implement | .claude/commands/specrails/implement.md |
1551
+ | /specrails:batch-implement | .claude/commands/specrails/batch-implement.md |
1552
+ | /specrails:propose-spec | .claude/commands/specrails/propose-spec.md |
1553
+ | /specrails:get-backlog-specs | .claude/commands/specrails/get-backlog-specs.md |
1554
+ | /specrails:auto-propose-backlog-specs | .claude/commands/specrails/auto-propose-backlog-specs.md |
1555
+ | /specrails:compat-check | .claude/commands/specrails/compat-check.md |
1556
+ | /specrails:refactor-recommender | .claude/commands/specrails/refactor-recommender.md |
1557
+ | /specrails:why | .claude/commands/specrails/why.md |
1558
+ [If cli_provider == "codex":]
1559
+ | Skill | File |
1560
+ |-------|------|
1561
+ | $sr-implement | .agents/skills/sr-implement/SKILL.md |
1562
+ | $sr-batch-implement | .agents/skills/sr-batch-implement/SKILL.md |
1563
+ | $sr-propose-spec | .agents/skills/sr-propose-spec/SKILL.md |
1564
+ | $sr-get-backlog-specs | .agents/skills/sr-get-backlog-specs/SKILL.md |
1565
+ | $sr-auto-propose-backlog-specs | .agents/skills/sr-auto-propose-backlog-specs/SKILL.md |
1566
+ | $sr-compat-check | .agents/skills/sr-compat-check/SKILL.md |
1567
+ | $sr-refactor-recommender | .agents/skills/sr-refactor-recommender/SKILL.md |
1568
+ | $sr-why | .agents/skills/sr-why/SKILL.md |
1569
+
1570
+ Note: Only commands/skills selected during setup are shown. Backlog commands are excluded if no backlog provider was configured.
1571
+
1572
+ ### Rules Created
1573
+ | Layer | File |
1574
+ |-------|------|
1575
+ | Backend | $SPECRAILS_DIR/rules/backend.md |
1576
+ | Frontend | $SPECRAILS_DIR/rules/frontend.md |
1577
+
1578
+ ### Scaffolding Removed
1579
+ | Artifact | Status |
1580
+ |----------|--------|
1581
+ | .specrails/setup-templates/ | Deleted |
1582
+ [If cli_provider == "claude":] | .claude/commands/enrich.md | Deleted |
1583
+ [If cli_provider == "codex":] | .agents/skills/setup/ | Deleted |
1584
+ | specrails/ | [User's choice] |
1585
+
1586
+ ### Next Steps
1587
+ [If cli_provider == "claude":]
1588
+ 1. Review the generated files in .claude/
1589
+ 2. Run `/specrails:get-backlog-specs` to see your backlog (if GitHub Issues exist)
1590
+ 3. Run `/specrails:auto-propose-backlog-specs` to generate feature ideas
1591
+ 4. Run `/specrails:implement #issue-number` to implement a feature
1592
+ 5. Commit the .claude/ directory to version control
1593
+ [If cli_provider == "codex":]
1594
+ 1. Review the generated files in .codex/ and .agents/skills/
1595
+ 2. Run `$sr-get-backlog-specs` to see your backlog (if GitHub Issues exist)
1596
+ 3. Run `$sr-auto-propose-backlog-specs` to generate feature ideas
1597
+ 4. Run `$sr-implement #issue-number` to implement a feature
1598
+ 5. Commit the .codex/ and .agents/ directories to version control
1599
+
1600
+ ### Quick Start
1601
+ [If cli_provider == "claude":]
1602
+ - `/specrails:implement "describe a feature"` — implement something right now
1603
+ - `/specrails:get-backlog-specs` — see prioritized feature ideas
1604
+ - `/specrails:auto-propose-backlog-specs` — discover new features using VPC
1605
+ [If cli_provider == "codex":]
1606
+ - `$sr-implement "describe a feature"` — implement something right now
1607
+ - `$sr-get-backlog-specs` — see prioritized feature ideas
1608
+ - `$sr-auto-propose-backlog-specs` — discover new features using VPC
1609
+ ```
1610
+
1611
+ ## First Task Prompt (Full Wizard)
1612
+
1613
+ After displaying the setup complete summary above, detect the project type and output:
1614
+
1615
+ **New project** (no `package.json`, `Gemfile`, `pyproject.toml`, `go.mod`, or `pom.xml` in root):
1616
+ ```
1617
+ ✅ Setup complete.
1618
+
1619
+ Try your first spec:
1620
+ [If cli_provider == "claude":]
1621
+ > /specrails:get-backlog-specs
1622
+ [If cli_provider == "codex":]
1623
+ > $sr-get-backlog-specs
1624
+ ```
1625
+
1626
+ **Existing codebase** (one or more of the above files found in root):
1627
+ ```
1628
+ ✅ Setup complete.
1629
+
1630
+ Try your first spec:
1631
+ [If cli_provider == "claude":]
1632
+ > /specrails:refactor-recommender
1633
+ [If cli_provider == "codex":]
1634
+ > $sr-refactor-recommender
1635
+ ```
1636
+
1637
+ Then stop.