Naveedtechlab Claude Sonnet 4.6 commited on
Commit
1dedc23
·
0 Parent(s):

feat: deploy — email/password only auth, no OAuth buttons

Browse files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .claude/commands/sp.adr.md +207 -0
  2. .claude/commands/sp.analyze.md +210 -0
  3. .claude/commands/sp.checklist.md +320 -0
  4. .claude/commands/sp.clarify.md +207 -0
  5. .claude/commands/sp.constitution.md +108 -0
  6. .claude/commands/sp.git.commit_pr.md +328 -0
  7. .claude/commands/sp.implement.md +161 -0
  8. .claude/commands/sp.phr.md +195 -0
  9. .claude/commands/sp.plan.md +115 -0
  10. .claude/commands/sp.reverse-engineer.md +1612 -0
  11. .claude/commands/sp.specify.md +284 -0
  12. .claude/commands/sp.tasks.md +163 -0
  13. .claude/commands/sp.taskstoissues.md +56 -0
  14. .dockerignore +22 -0
  15. .gitattributes +2 -0
  16. .gitignore +49 -0
  17. .specify/memory/constitution.md +55 -0
  18. .specify/scripts/bash/check-prerequisites.sh +166 -0
  19. .specify/scripts/bash/common.sh +156 -0
  20. .specify/scripts/bash/create-adr.sh +101 -0
  21. .specify/scripts/bash/create-new-feature.sh +302 -0
  22. .specify/scripts/bash/create-phr.sh +256 -0
  23. .specify/scripts/bash/setup-plan.sh +61 -0
  24. .specify/scripts/bash/update-agent-context.sh +799 -0
  25. .specify/templates/adr-template.md +56 -0
  26. .specify/templates/agent-file-template.md +28 -0
  27. .specify/templates/checklist-template.md +40 -0
  28. .specify/templates/phr-template.prompt.md +45 -0
  29. .specify/templates/plan-template.md +104 -0
  30. .specify/templates/spec-template.md +115 -0
  31. .specify/templates/tasks-template.md +251 -0
  32. CLAUDE.md +210 -0
  33. Dockerfile +56 -0
  34. README.md +310 -0
  35. phase-1-core-infra/backend/.env.example +33 -0
  36. phase-1-core-infra/backend/.flake8 +4 -0
  37. phase-1-core-infra/backend/alembic.ini +105 -0
  38. phase-1-core-infra/backend/alembic/env.py +93 -0
  39. phase-1-core-infra/backend/alembic/env_fresh.py +75 -0
  40. phase-1-core-infra/backend/alembic/script.py.mako +24 -0
  41. phase-1-core-infra/backend/alembic/versions/001_initial_schema.py +78 -0
  42. phase-1-core-infra/backend/alembic/versions/002_add_encryption_columns.py +53 -0
  43. phase-1-core-infra/backend/alembic/versions/003_create_rate_limit_rules.py +51 -0
  44. phase-1-core-infra/backend/alembic/versions/004_create_rate_limit_violations.py +61 -0
  45. phase-1-core-infra/backend/alembic/versions/005_create_admin_metrics.py +49 -0
  46. phase-1-core-infra/backend/alembic/versions/007_better_auth_schema.py +103 -0
  47. phase-1-core-infra/backend/alembic/versions/008_add_post_logs_and_twitter.py +50 -0
  48. phase-1-core-infra/backend/pyproject.toml +22 -0
  49. phase-1-core-infra/backend/requirements-dev.txt +14 -0
  50. phase-1-core-infra/backend/requirements.txt +36 -0
.claude/commands/sp.adr.md ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Review planning artifacts for architecturally significant decisions and create ADRs.
3
+ ---
4
+
5
+ # COMMAND: Analyze planning artifacts and document architecturally significant decisions as ADRs
6
+
7
+ ## CONTEXT
8
+
9
+ The user has completed feature planning and needs to:
10
+
11
+ - Identify architecturally significant technical decisions from plan.md
12
+ - Document these decisions as Architecture Decision Records (ADRs)
13
+ - Ensure team alignment on technical approach before implementation
14
+ - Create a permanent, reviewable record of why decisions were made
15
+
16
+ Architecture Decision Records capture decisions that:
17
+
18
+ - Impact how engineers write or structure software
19
+ - Have notable tradeoffs or alternatives
20
+ - Will likely be questioned or revisited later
21
+
22
+ **User's additional input:**
23
+
24
+ $ARGUMENTS
25
+
26
+ ## YOUR ROLE
27
+
28
+ Act as a senior software architect with expertise in:
29
+
30
+ - Technical decision analysis and evaluation
31
+ - System design patterns and tradeoffs
32
+ - Enterprise architecture documentation
33
+ - Risk assessment and consequence analysis
34
+
35
+ ## OUTPUT STRUCTURE (with quick flywheel hooks)
36
+
37
+ Execute this workflow in 6 sequential steps. At Steps 2 and 4, apply lightweight Analyze→Measure checks:
38
+ - Analyze: Identify likely failure modes, specifically:
39
+ - Over-granular ADRs: ADRs that document decisions which are trivial, low-impact, or do not affect architectural direction (e.g., naming conventions, minor refactorings).
40
+ - Missing alternatives: ADRs that do not list at least one alternative approach considered.
41
+ - Measure: Apply the following checklist grader (PASS only if all are met):
42
+ - The ADR documents a decision that clusters related changes or impacts multiple components (not a trivial/single-file change).
43
+ - The ADR explicitly lists at least one alternative approach, with rationale.
44
+ - The ADR includes clear pros and cons for the chosen approach and alternatives.
45
+ - The ADR is concise but sufficiently detailed for future reference.
46
+
47
+ ## Step 1: Load Planning Context
48
+
49
+ Run `.specify/scripts/bash/check-prerequisites.sh --json` from repo root and parse JSON for FEATURE_DIR and AVAILABLE_DOCS.
50
+
51
+ Derive absolute paths:
52
+
53
+ - PLAN = FEATURE_DIR/plan.md (REQUIRED - abort if missing with "Run /sp.plan first")
54
+ - RESEARCH = FEATURE_DIR/research.md (if exists)
55
+ - DATA_MODEL = FEATURE_DIR/data-model.md (if exists)
56
+ - CONTRACTS_DIR = FEATURE_DIR/contracts/ (if exists)
57
+
58
+ ## Step 2: Extract Architectural Decisions (Analyze)
59
+
60
+ Load plan.md and available artifacts. Extract architecturally significant decisions as **decision clusters** (not atomic choices):
61
+
62
+ **✅ GOOD (Clustered):**
63
+
64
+ - "Frontend Stack" (Next.js + Tailwind + Vercel as integrated solution)
65
+ - "Authentication Approach" (JWT strategy + Auth0 + session handling)
66
+ - "Data Architecture" (PostgreSQL + Redis caching + migration strategy)
67
+
68
+ **❌ BAD (Over-granular):**
69
+
70
+ - Separate ADRs for Next.js, Tailwind, and Vercel
71
+ - Separate ADRs for each library choice
72
+
73
+ **Clustering Rules:**
74
+
75
+ - Group technologies that work together and would likely change together
76
+ - Separate only if decisions are independent and could diverge
77
+ - Example: Frontend stack vs Backend stack = 2 ADRs (can evolve independently)
78
+ - Example: Next.js + Tailwind + Vercel = 1 ADR (integrated, change together)
79
+
80
+ For each decision cluster, note: what was decided, why, where in docs.
81
+
82
+ ## Step 3: Check Existing ADRs
83
+
84
+ Scan `history/adr/` directory. For each extracted decision:
85
+
86
+ - If covered by existing ADR → note reference
87
+ - If conflicts with existing ADR → flag conflict
88
+ - If not covered → mark as ADR candidate
89
+
90
+ ## Step 4: Apply Significance Test (Measure)
91
+
92
+ For each ADR candidate, test:
93
+
94
+ - Does it impact how engineers write/structure software?
95
+ - Are there notable tradeoffs or alternatives?
96
+ - Will it be questioned or revisited later?
97
+
98
+ Only proceed with ADRs that pass ALL three tests.
99
+
100
+ ## Step 5: Create ADRs (Improve)
101
+
102
+ For each qualifying decision cluster:
103
+
104
+ 1. Generate concise title reflecting the cluster (e.g., "Frontend Technology Stack" not "Use Next.js")
105
+ 2. Run `create-adr.sh "<title>"` from repo root
106
+ 3. Parse JSON response for `adr_path` and `adr_id`
107
+ 4. Read created file (contains template with {{PLACEHOLDERS}})
108
+ 5. Fill ALL placeholders:
109
+ - `{{TITLE}}` = decision cluster title
110
+ - `{{STATUS}}` = "Proposed" or "Accepted"
111
+ - `{{DATE}}` = today (YYYY-MM-DD)
112
+ - `{{CONTEXT}}` = situation, constraints leading to decision cluster
113
+ - `{{DECISION}}` = list ALL components of cluster (e.g., "Framework: Next.js 14, Styling: Tailwind CSS v3, Deployment: Vercel")
114
+ - `{{CONSEQUENCES}}` = outcomes, tradeoffs, risks for the integrated solution
115
+ - `{{ALTERNATIVES}}` = alternative clusters (e.g., "Remix + styled-components + Cloudflare")
116
+ - `{{REFERENCES}}` = plan.md, research.md, data-model.md
117
+ 6. Save file
118
+
119
+ ## Step 6: Report Completion
120
+
121
+ Output:
122
+
123
+ ```
124
+ ✅ ADR Review Complete - Created N ADRs, referenced M existing
125
+ ```
126
+
127
+ List created ADRs with ID and title.
128
+
129
+ If conflicts detected:
130
+
131
+ ```
132
+ ⚠️ Conflicts with existing ADRs [IDs]. Review and update outdated decisions or revise plan.
133
+ ```
134
+
135
+ If create-adr.sh fails: Report script error and skip that ADR.
136
+
137
+ ## FORMATTING REQUIREMENTS
138
+
139
+ Present results in this exact structure:
140
+
141
+ ```
142
+ ✅ ADR Review Complete
143
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
144
+
145
+ 📋 Created ADRs: {count}
146
+ - ADR-{id}: {title}
147
+ - ADR-{id}: {title}
148
+
149
+ 📚 Referenced Existing: {count}
150
+ - ADR-{id}: {title}
151
+
152
+ ⚠️ Conflicts Detected: {count}
153
+ - ADR-{id}: {conflict description}
154
+
155
+ Next Steps:
156
+ → Resolve conflicts before proceeding to /sp.tasks
157
+ → Review created ADRs with team
158
+ → Update plan.md if needed
159
+
160
+ Acceptance Criteria (PASS only if all true)
161
+ - Decisions are clustered (not atomic), with explicit alternatives and tradeoffs
162
+ - Consequences cover both positive and negative outcomes
163
+ - References link back to plan and related docs
164
+ ```
165
+
166
+ ## ERROR HANDLING
167
+
168
+ If plan.md missing:
169
+
170
+ - Display: "❌ Error: plan.md not found. Run /sp.plan first to generate planning artifacts."
171
+ - Exit gracefully without creating any ADRs
172
+
173
+ If create-adr.sh fails:
174
+
175
+ - Display exact error message
176
+ - Skip that ADR and continue with others
177
+ - Report partial completion at end
178
+
179
+ ## TONE
180
+
181
+ Be thorough, analytical, and decision-focused. Emphasize the "why" behind each decision and its long-term implications.
182
+
183
+ ---
184
+
185
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
186
+
187
+ 1) Determine Stage
188
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
189
+
190
+ 2) Generate Title and Determine Routing:
191
+ - Generate Title: 3–7 words (slug for filename)
192
+ - Route is automatically determined by stage:
193
+ - `constitution` → `history/prompts/constitution/`
194
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
195
+ - `general` → `history/prompts/general/`
196
+
197
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
198
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
199
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
200
+ - If the script fails:
201
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
202
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
203
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
204
+
205
+ 4) Validate + report
206
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
207
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.analyze.md ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Perform a non-destructive cross-artifact consistency and quality analysis across spec.md, plan.md, and tasks.md after task generation.
3
+ ---
4
+
5
+ ## User Input
6
+
7
+ ```text
8
+ $ARGUMENTS
9
+ ```
10
+
11
+ You **MUST** consider the user input before proceeding (if not empty).
12
+
13
+ ## Goal
14
+
15
+ Identify inconsistencies, duplications, ambiguities, and underspecified items across the three core artifacts (`spec.md`, `plan.md`, `tasks.md`) before implementation. This command MUST run only after `/sp.tasks` has successfully produced a complete `tasks.md`.
16
+
17
+ ## Operating Constraints
18
+
19
+ **STRICTLY READ-ONLY**: Do **not** modify any files. Output a structured analysis report. Offer an optional remediation plan (user must explicitly approve before any follow-up editing commands would be invoked manually).
20
+
21
+ **Constitution Authority**: The project constitution (`.specify/memory/constitution.md`) is **non-negotiable** within this analysis scope. Constitution conflicts are automatically CRITICAL and require adjustment of the spec, plan, or tasks—not dilution, reinterpretation, or silent ignoring of the principle. If a principle itself needs to change, that must occur in a separate, explicit constitution update outside `/sp.analyze`.
22
+
23
+ ## Execution Steps
24
+
25
+ ### 1. Initialize Analysis Context
26
+
27
+ Run `.specify/scripts/bash/check-prerequisites.sh --json --require-tasks --include-tasks` once from repo root and parse JSON for FEATURE_DIR and AVAILABLE_DOCS. Derive absolute paths:
28
+
29
+ - SPEC = FEATURE_DIR/spec.md
30
+ - PLAN = FEATURE_DIR/plan.md
31
+ - TASKS = FEATURE_DIR/tasks.md
32
+
33
+ Abort with an error message if any required file is missing (instruct the user to run missing prerequisite command).
34
+ For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
35
+
36
+ ### 2. Load Artifacts (Progressive Disclosure)
37
+
38
+ Load only the minimal necessary context from each artifact:
39
+
40
+ **From spec.md:**
41
+
42
+ - Overview/Context
43
+ - Functional Requirements
44
+ - Non-Functional Requirements
45
+ - User Stories
46
+ - Edge Cases (if present)
47
+
48
+ **From plan.md:**
49
+
50
+ - Architecture/stack choices
51
+ - Data Model references
52
+ - Phases
53
+ - Technical constraints
54
+
55
+ **From tasks.md:**
56
+
57
+ - Task IDs
58
+ - Descriptions
59
+ - Phase grouping
60
+ - Parallel markers [P]
61
+ - Referenced file paths
62
+
63
+ **From constitution:**
64
+
65
+ - Load `.specify/memory/constitution.md` for principle validation
66
+
67
+ ### 3. Build Semantic Models
68
+
69
+ Create internal representations (do not include raw artifacts in output):
70
+
71
+ - **Requirements inventory**: Each functional + non-functional requirement with a stable key (derive slug based on imperative phrase; e.g., "User can upload file" → `user-can-upload-file`)
72
+ - **User story/action inventory**: Discrete user actions with acceptance criteria
73
+ - **Task coverage mapping**: Map each task to one or more requirements or stories (inference by keyword / explicit reference patterns like IDs or key phrases)
74
+ - **Constitution rule set**: Extract principle names and MUST/SHOULD normative statements
75
+
76
+ ### 4. Detection Passes (Token-Efficient Analysis)
77
+
78
+ Focus on high-signal findings. Limit to 50 findings total; aggregate remainder in overflow summary.
79
+
80
+ #### A. Duplication Detection
81
+
82
+ - Identify near-duplicate requirements
83
+ - Mark lower-quality phrasing for consolidation
84
+
85
+ #### B. Ambiguity Detection
86
+
87
+ - Flag vague adjectives (fast, scalable, secure, intuitive, robust) lacking measurable criteria
88
+ - Flag unresolved placeholders (TODO, TKTK, ???, `<placeholder>`, etc.)
89
+
90
+ #### C. Underspecification
91
+
92
+ - Requirements with verbs but missing object or measurable outcome
93
+ - User stories missing acceptance criteria alignment
94
+ - Tasks referencing files or components not defined in spec/plan
95
+
96
+ #### D. Constitution Alignment
97
+
98
+ - Any requirement or plan element conflicting with a MUST principle
99
+ - Missing mandated sections or quality gates from constitution
100
+
101
+ #### E. Coverage Gaps
102
+
103
+ - Requirements with zero associated tasks
104
+ - Tasks with no mapped requirement/story
105
+ - Non-functional requirements not reflected in tasks (e.g., performance, security)
106
+
107
+ #### F. Inconsistency
108
+
109
+ - Terminology drift (same concept named differently across files)
110
+ - Data entities referenced in plan but absent in spec (or vice versa)
111
+ - Task ordering contradictions (e.g., integration tasks before foundational setup tasks without dependency note)
112
+ - Conflicting requirements (e.g., one requires Next.js while other specifies Vue)
113
+
114
+ ### 5. Severity Assignment
115
+
116
+ Use this heuristic to prioritize findings:
117
+
118
+ - **CRITICAL**: Violates constitution MUST, missing core spec artifact, or requirement with zero coverage that blocks baseline functionality
119
+ - **HIGH**: Duplicate or conflicting requirement, ambiguous security/performance attribute, untestable acceptance criterion
120
+ - **MEDIUM**: Terminology drift, missing non-functional task coverage, underspecified edge case
121
+ - **LOW**: Style/wording improvements, minor redundancy not affecting execution order
122
+
123
+ ### 6. Produce Compact Analysis Report
124
+
125
+ Output a Markdown report (no file writes) with the following structure:
126
+
127
+ ## Specification Analysis Report
128
+
129
+ | ID | Category | Severity | Location(s) | Summary | Recommendation |
130
+ |----|----------|----------|-------------|---------|----------------|
131
+ | A1 | Duplication | HIGH | spec.md:L120-134 | Two similar requirements ... | Merge phrasing; keep clearer version |
132
+
133
+ (Add one row per finding; generate stable IDs prefixed by category initial.)
134
+
135
+ **Coverage Summary Table:**
136
+
137
+ | Requirement Key | Has Task? | Task IDs | Notes |
138
+ |-----------------|-----------|----------|-------|
139
+
140
+ **Constitution Alignment Issues:** (if any)
141
+
142
+ **Unmapped Tasks:** (if any)
143
+
144
+ **Metrics:**
145
+
146
+ - Total Requirements
147
+ - Total Tasks
148
+ - Coverage % (requirements with >=1 task)
149
+ - Ambiguity Count
150
+ - Duplication Count
151
+ - Critical Issues Count
152
+
153
+ ### 7. Provide Next Actions
154
+
155
+ At end of report, output a concise Next Actions block:
156
+
157
+ - If CRITICAL issues exist: Recommend resolving before `/sp.implement`
158
+ - If only LOW/MEDIUM: User may proceed, but provide improvement suggestions
159
+ - Provide explicit command suggestions: e.g., "Run /sp.specify with refinement", "Run /sp.plan to adjust architecture", "Manually edit tasks.md to add coverage for 'performance-metrics'"
160
+
161
+ ### 8. Offer Remediation
162
+
163
+ Ask the user: "Would you like me to suggest concrete remediation edits for the top N issues?" (Do NOT apply them automatically.)
164
+
165
+ ## Operating Principles
166
+
167
+ ### Context Efficiency
168
+
169
+ - **Minimal high-signal tokens**: Focus on actionable findings, not exhaustive documentation
170
+ - **Progressive disclosure**: Load artifacts incrementally; don't dump all content into analysis
171
+ - **Token-efficient output**: Limit findings table to 50 rows; summarize overflow
172
+ - **Deterministic results**: Rerunning without changes should produce consistent IDs and counts
173
+
174
+ ### Analysis Guidelines
175
+
176
+ - **NEVER modify files** (this is read-only analysis)
177
+ - **NEVER hallucinate missing sections** (if absent, report them accurately)
178
+ - **Prioritize constitution violations** (these are always CRITICAL)
179
+ - **Use examples over exhaustive rules** (cite specific instances, not generic patterns)
180
+ - **Report zero issues gracefully** (emit success report with coverage statistics)
181
+
182
+ ## Context
183
+
184
+ $ARGUMENTS
185
+
186
+ ---
187
+
188
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
189
+
190
+ 1) Determine Stage
191
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
192
+
193
+ 2) Generate Title and Determine Routing:
194
+ - Generate Title: 3–7 words (slug for filename)
195
+ - Route is automatically determined by stage:
196
+ - `constitution` → `history/prompts/constitution/`
197
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
198
+ - `general` → `history/prompts/general/`
199
+
200
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
201
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
202
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
203
+ - If the script fails:
204
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
205
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
206
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
207
+
208
+ 4) Validate + report
209
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
210
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.checklist.md ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Generate a custom checklist for the current feature based on user requirements.
3
+ ---
4
+
5
+ ## Checklist Purpose: "Unit Tests for English"
6
+
7
+ **CRITICAL CONCEPT**: Checklists are **UNIT TESTS FOR REQUIREMENTS WRITING** - they validate the quality, clarity, and completeness of requirements in a given domain.
8
+
9
+ **NOT for verification/testing**:
10
+
11
+ - ❌ NOT "Verify the button clicks correctly"
12
+ - ❌ NOT "Test error handling works"
13
+ - ❌ NOT "Confirm the API returns 200"
14
+ - ❌ NOT checking if code/implementation matches the spec
15
+
16
+ **FOR requirements quality validation**:
17
+
18
+ - ✅ "Are visual hierarchy requirements defined for all card types?" (completeness)
19
+ - ✅ "Is 'prominent display' quantified with specific sizing/positioning?" (clarity)
20
+ - ✅ "Are hover state requirements consistent across all interactive elements?" (consistency)
21
+ - ✅ "Are accessibility requirements defined for keyboard navigation?" (coverage)
22
+ - ✅ "Does the spec define what happens when logo image fails to load?" (edge cases)
23
+
24
+ **Metaphor**: If your spec is code written in English, the checklist is its unit test suite. You're testing whether the requirements are well-written, complete, unambiguous, and ready for implementation - NOT whether the implementation works.
25
+
26
+ ## User Input
27
+
28
+ ```text
29
+ $ARGUMENTS
30
+ ```
31
+
32
+ You **MUST** consider the user input before proceeding (if not empty).
33
+
34
+ ## Execution Steps
35
+
36
+ 1. **Setup**: Run `.specify/scripts/bash/check-prerequisites.sh --json` from repo root and parse JSON for FEATURE_DIR and AVAILABLE_DOCS list.
37
+ - All file paths must be absolute.
38
+ - For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
39
+
40
+ 2. **Clarify intent (dynamic)**: Derive up to THREE initial contextual clarifying questions (no pre-baked catalog). They MUST:
41
+ - Be generated from the user's phrasing + extracted signals from spec/plan/tasks
42
+ - Only ask about information that materially changes checklist content
43
+ - Be skipped individually if already unambiguous in `$ARGUMENTS`
44
+ - Prefer precision over breadth
45
+
46
+ Generation algorithm:
47
+ 1. Extract signals: feature domain keywords (e.g., auth, latency, UX, API), risk indicators ("critical", "must", "compliance"), stakeholder hints ("QA", "review", "security team"), and explicit deliverables ("a11y", "rollback", "contracts").
48
+ 2. Cluster signals into candidate focus areas (max 4) ranked by relevance.
49
+ 3. Identify probable audience & timing (author, reviewer, QA, release) if not explicit.
50
+ 4. Detect missing dimensions: scope breadth, depth/rigor, risk emphasis, exclusion boundaries, measurable acceptance criteria.
51
+ 5. Formulate questions chosen from these archetypes:
52
+ - Scope refinement (e.g., "Should this include integration touchpoints with X and Y or stay limited to local module correctness?")
53
+ - Risk prioritization (e.g., "Which of these potential risk areas should receive mandatory gating checks?")
54
+ - Depth calibration (e.g., "Is this a lightweight pre-commit sanity list or a formal release gate?")
55
+ - Audience framing (e.g., "Will this be used by the author only or peers during PR review?")
56
+ - Boundary exclusion (e.g., "Should we explicitly exclude performance tuning items this round?")
57
+ - Scenario class gap (e.g., "No recovery flows detected—are rollback / partial failure paths in scope?")
58
+
59
+ Question formatting rules:
60
+ - If presenting options, generate a compact table with columns: Option | Candidate | Why It Matters
61
+ - Limit to A–E options maximum; omit table if a free-form answer is clearer
62
+ - Never ask the user to restate what they already said
63
+ - Avoid speculative categories (no hallucination). If uncertain, ask explicitly: "Confirm whether X belongs in scope."
64
+
65
+ Defaults when interaction impossible:
66
+ - Depth: Standard
67
+ - Audience: Reviewer (PR) if code-related; Author otherwise
68
+ - Focus: Top 2 relevance clusters
69
+
70
+ Output the questions (label Q1/Q2/Q3). After answers: if ≥2 scenario classes (Alternate / Exception / Recovery / Non-Functional domain) remain unclear, you MAY ask up to TWO more targeted follow‑ups (Q4/Q5) with a one-line justification each (e.g., "Unresolved recovery path risk"). Do not exceed five total questions. Skip escalation if user explicitly declines more.
71
+
72
+ 3. **Understand user request**: Combine `$ARGUMENTS` + clarifying answers:
73
+ - Derive checklist theme (e.g., security, review, deploy, ux)
74
+ - Consolidate explicit must-have items mentioned by user
75
+ - Map focus selections to category scaffolding
76
+ - Infer any missing context from spec/plan/tasks (do NOT hallucinate)
77
+
78
+ 4. **Load feature context**: Read from FEATURE_DIR:
79
+ - spec.md: Feature requirements and scope
80
+ - plan.md (if exists): Technical details, dependencies
81
+ - tasks.md (if exists): Implementation tasks
82
+
83
+ **Context Loading Strategy**:
84
+ - Load only necessary portions relevant to active focus areas (avoid full-file dumping)
85
+ - Prefer summarizing long sections into concise scenario/requirement bullets
86
+ - Use progressive disclosure: add follow-on retrieval only if gaps detected
87
+ - If source docs are large, generate interim summary items instead of embedding raw text
88
+
89
+ 5. **Generate checklist** - Create "Unit Tests for Requirements":
90
+ - Create `FEATURE_DIR/checklists/` directory if it doesn't exist
91
+ - Generate unique checklist filename:
92
+ - Use short, descriptive name based on domain (e.g., `ux.md`, `api.md`, `security.md`)
93
+ - Format: `[domain].md`
94
+ - If file exists, append to existing file
95
+ - Number items sequentially starting from CHK001
96
+ - Each `/sp.checklist` run creates a NEW file (never overwrites existing checklists)
97
+
98
+ **CORE PRINCIPLE - Test the Requirements, Not the Implementation**:
99
+ Every checklist item MUST evaluate the REQUIREMENTS THEMSELVES for:
100
+ - **Completeness**: Are all necessary requirements present?
101
+ - **Clarity**: Are requirements unambiguous and specific?
102
+ - **Consistency**: Do requirements align with each other?
103
+ - **Measurability**: Can requirements be objectively verified?
104
+ - **Coverage**: Are all scenarios/edge cases addressed?
105
+
106
+ **Category Structure** - Group items by requirement quality dimensions:
107
+ - **Requirement Completeness** (Are all necessary requirements documented?)
108
+ - **Requirement Clarity** (Are requirements specific and unambiguous?)
109
+ - **Requirement Consistency** (Do requirements align without conflicts?)
110
+ - **Acceptance Criteria Quality** (Are success criteria measurable?)
111
+ - **Scenario Coverage** (Are all flows/cases addressed?)
112
+ - **Edge Case Coverage** (Are boundary conditions defined?)
113
+ - **Non-Functional Requirements** (Performance, Security, Accessibility, etc. - are they specified?)
114
+ - **Dependencies & Assumptions** (Are they documented and validated?)
115
+ - **Ambiguities & Conflicts** (What needs clarification?)
116
+
117
+ **HOW TO WRITE CHECKLIST ITEMS - "Unit Tests for English"**:
118
+
119
+ ❌ **WRONG** (Testing implementation):
120
+ - "Verify landing page displays 3 episode cards"
121
+ - "Test hover states work on desktop"
122
+ - "Confirm logo click navigates home"
123
+
124
+ ✅ **CORRECT** (Testing requirements quality):
125
+ - "Are the exact number and layout of featured episodes specified?" [Completeness]
126
+ - "Is 'prominent display' quantified with specific sizing/positioning?" [Clarity]
127
+ - "Are hover state requirements consistent across all interactive elements?" [Consistency]
128
+ - "Are keyboard navigation requirements defined for all interactive UI?" [Coverage]
129
+ - "Is the fallback behavior specified when logo image fails to load?" [Edge Cases]
130
+ - "Are loading states defined for asynchronous episode data?" [Completeness]
131
+ - "Does the spec define visual hierarchy for competing UI elements?" [Clarity]
132
+
133
+ **ITEM STRUCTURE**:
134
+ Each item should follow this pattern:
135
+ - Question format asking about requirement quality
136
+ - Focus on what's WRITTEN (or not written) in the spec/plan
137
+ - Include quality dimension in brackets [Completeness/Clarity/Consistency/etc.]
138
+ - Reference spec section `[Spec §X.Y]` when checking existing requirements
139
+ - Use `[Gap]` marker when checking for missing requirements
140
+
141
+ **EXAMPLES BY QUALITY DIMENSION**:
142
+
143
+ Completeness:
144
+ - "Are error handling requirements defined for all API failure modes? [Gap]"
145
+ - "Are accessibility requirements specified for all interactive elements? [Completeness]"
146
+ - "Are mobile breakpoint requirements defined for responsive layouts? [Gap]"
147
+
148
+ Clarity:
149
+ - "Is 'fast loading' quantified with specific timing thresholds? [Clarity, Spec §NFR-2]"
150
+ - "Are 'related episodes' selection criteria explicitly defined? [Clarity, Spec §FR-5]"
151
+ - "Is 'prominent' defined with measurable visual properties? [Ambiguity, Spec §FR-4]"
152
+
153
+ Consistency:
154
+ - "Do navigation requirements align across all pages? [Consistency, Spec §FR-10]"
155
+ - "Are card component requirements consistent between landing and detail pages? [Consistency]"
156
+
157
+ Coverage:
158
+ - "Are requirements defined for zero-state scenarios (no episodes)? [Coverage, Edge Case]"
159
+ - "Are concurrent user interaction scenarios addressed? [Coverage, Gap]"
160
+ - "Are requirements specified for partial data loading failures? [Coverage, Exception Flow]"
161
+
162
+ Measurability:
163
+ - "Are visual hierarchy requirements measurable/testable? [Acceptance Criteria, Spec §FR-1]"
164
+ - "Can 'balanced visual weight' be objectively verified? [Measurability, Spec §FR-2]"
165
+
166
+ **Scenario Classification & Coverage** (Requirements Quality Focus):
167
+ - Check if requirements exist for: Primary, Alternate, Exception/Error, Recovery, Non-Functional scenarios
168
+ - For each scenario class, ask: "Are [scenario type] requirements complete, clear, and consistent?"
169
+ - If scenario class missing: "Are [scenario type] requirements intentionally excluded or missing? [Gap]"
170
+ - Include resilience/rollback when state mutation occurs: "Are rollback requirements defined for migration failures? [Gap]"
171
+
172
+ **Traceability Requirements**:
173
+ - MINIMUM: ≥80% of items MUST include at least one traceability reference
174
+ - Each item should reference: spec section `[Spec §X.Y]`, or use markers: `[Gap]`, `[Ambiguity]`, `[Conflict]`, `[Assumption]`
175
+ - If no ID system exists: "Is a requirement & acceptance criteria ID scheme established? [Traceability]"
176
+
177
+ **Surface & Resolve Issues** (Requirements Quality Problems):
178
+ Ask questions about the requirements themselves:
179
+ - Ambiguities: "Is the term 'fast' quantified with specific metrics? [Ambiguity, Spec §NFR-1]"
180
+ - Conflicts: "Do navigation requirements conflict between §FR-10 and §FR-10a? [Conflict]"
181
+ - Assumptions: "Is the assumption of 'always available podcast API' validated? [Assumption]"
182
+ - Dependencies: "Are external podcast API requirements documented? [Dependency, Gap]"
183
+ - Missing definitions: "Is 'visual hierarchy' defined with measurable criteria? [Gap]"
184
+
185
+ **Content Consolidation**:
186
+ - Soft cap: If raw candidate items > 40, prioritize by risk/impact
187
+ - Merge near-duplicates checking the same requirement aspect
188
+ - If >5 low-impact edge cases, create one item: "Are edge cases X, Y, Z addressed in requirements? [Coverage]"
189
+
190
+ **🚫 ABSOLUTELY PROHIBITED** - These make it an implementation test, not a requirements test:
191
+ - ❌ Any item starting with "Verify", "Test", "Confirm", "Check" + implementation behavior
192
+ - ❌ References to code execution, user actions, system behavior
193
+ - ❌ "Displays correctly", "works properly", "functions as expected"
194
+ - ❌ "Click", "navigate", "render", "load", "execute"
195
+ - ❌ Test cases, test plans, QA procedures
196
+ - ❌ Implementation details (frameworks, APIs, algorithms)
197
+
198
+ **✅ REQUIRED PATTERNS** - These test requirements quality:
199
+ - ✅ "Are [requirement type] defined/specified/documented for [scenario]?"
200
+ - ✅ "Is [vague term] quantified/clarified with specific criteria?"
201
+ - ✅ "Are requirements consistent between [section A] and [section B]?"
202
+ - ✅ "Can [requirement] be objectively measured/verified?"
203
+ - ✅ "Are [edge cases/scenarios] addressed in requirements?"
204
+ - ✅ "Does the spec define [missing aspect]?"
205
+
206
+ 6. **Structure Reference**: Generate the checklist following the canonical template in `.specify/templates/checklist-template.md` for title, meta section, category headings, and ID formatting. If template is unavailable, use: H1 title, purpose/created meta lines, `##` category sections containing `- [ ] CHK### <requirement item>` lines with globally incrementing IDs starting at CHK001.
207
+
208
+ 7. **Report**: Output full path to created checklist, item count, and remind user that each run creates a new file. Summarize:
209
+ - Focus areas selected
210
+ - Depth level
211
+ - Actor/timing
212
+ - Any explicit user-specified must-have items incorporated
213
+
214
+ **Important**: Each `/sp.checklist` command invocation creates a checklist file using short, descriptive names unless file already exists. This allows:
215
+
216
+ - Multiple checklists of different types (e.g., `ux.md`, `test.md`, `security.md`)
217
+ - Simple, memorable filenames that indicate checklist purpose
218
+ - Easy identification and navigation in the `checklists/` folder
219
+
220
+ To avoid clutter, use descriptive types and clean up obsolete checklists when done.
221
+
222
+ ## Example Checklist Types & Sample Items
223
+
224
+ **UX Requirements Quality:** `ux.md`
225
+
226
+ Sample items (testing the requirements, NOT the implementation):
227
+
228
+ - "Are visual hierarchy requirements defined with measurable criteria? [Clarity, Spec §FR-1]"
229
+ - "Is the number and positioning of UI elements explicitly specified? [Completeness, Spec §FR-1]"
230
+ - "Are interaction state requirements (hover, focus, active) consistently defined? [Consistency]"
231
+ - "Are accessibility requirements specified for all interactive elements? [Coverage, Gap]"
232
+ - "Is fallback behavior defined when images fail to load? [Edge Case, Gap]"
233
+ - "Can 'prominent display' be objectively measured? [Measurability, Spec §FR-4]"
234
+
235
+ **API Requirements Quality:** `api.md`
236
+
237
+ Sample items:
238
+
239
+ - "Are error response formats specified for all failure scenarios? [Completeness]"
240
+ - "Are rate limiting requirements quantified with specific thresholds? [Clarity]"
241
+ - "Are authentication requirements consistent across all endpoints? [Consistency]"
242
+ - "Are retry/timeout requirements defined for external dependencies? [Coverage, Gap]"
243
+ - "Is versioning strategy documented in requirements? [Gap]"
244
+
245
+ **Performance Requirements Quality:** `performance.md`
246
+
247
+ Sample items:
248
+
249
+ - "Are performance requirements quantified with specific metrics? [Clarity]"
250
+ - "Are performance targets defined for all critical user journeys? [Coverage]"
251
+ - "Are performance requirements under different load conditions specified? [Completeness]"
252
+ - "Can performance requirements be objectively measured? [Measurability]"
253
+ - "Are degradation requirements defined for high-load scenarios? [Edge Case, Gap]"
254
+
255
+ **Security Requirements Quality:** `security.md`
256
+
257
+ Sample items:
258
+
259
+ - "Are authentication requirements specified for all protected resources? [Coverage]"
260
+ - "Are data protection requirements defined for sensitive information? [Completeness]"
261
+ - "Is the threat model documented and requirements aligned to it? [Traceability]"
262
+ - "Are security requirements consistent with compliance obligations? [Consistency]"
263
+ - "Are security failure/breach response requirements defined? [Gap, Exception Flow]"
264
+
265
+ ## Anti-Examples: What NOT To Do
266
+
267
+ **❌ WRONG - These test implementation, not requirements:**
268
+
269
+ ```markdown
270
+ - [ ] CHK001 - Verify landing page displays 3 episode cards [Spec §FR-001]
271
+ - [ ] CHK002 - Test hover states work correctly on desktop [Spec §FR-003]
272
+ - [ ] CHK003 - Confirm logo click navigates to home page [Spec §FR-010]
273
+ - [ ] CHK004 - Check that related episodes section shows 3-5 items [Spec §FR-005]
274
+ ```
275
+
276
+ **✅ CORRECT - These test requirements quality:**
277
+
278
+ ```markdown
279
+ - [ ] CHK001 - Are the number and layout of featured episodes explicitly specified? [Completeness, Spec §FR-001]
280
+ - [ ] CHK002 - Are hover state requirements consistently defined for all interactive elements? [Consistency, Spec §FR-003]
281
+ - [ ] CHK003 - Are navigation requirements clear for all clickable brand elements? [Clarity, Spec §FR-010]
282
+ - [ ] CHK004 - Is the selection criteria for related episodes documented? [Gap, Spec §FR-005]
283
+ - [ ] CHK005 - Are loading state requirements defined for asynchronous episode data? [Gap]
284
+ - [ ] CHK006 - Can "visual hierarchy" requirements be objectively measured? [Measurability, Spec §FR-001]
285
+ ```
286
+
287
+ **Key Differences:**
288
+
289
+ - Wrong: Tests if the system works correctly
290
+ - Correct: Tests if the requirements are written correctly
291
+ - Wrong: Verification of behavior
292
+ - Correct: Validation of requirement quality
293
+ - Wrong: "Does it do X?"
294
+ - Correct: "Is X clearly specified?"
295
+
296
+ ---
297
+
298
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
299
+
300
+ 1) Determine Stage
301
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
302
+
303
+ 2) Generate Title and Determine Routing:
304
+ - Generate Title: 3–7 words (slug for filename)
305
+ - Route is automatically determined by stage:
306
+ - `constitution` → `history/prompts/constitution/`
307
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
308
+ - `general` → `history/prompts/general/`
309
+
310
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
311
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
312
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
313
+ - If the script fails:
314
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
315
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
316
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
317
+
318
+ 4) Validate + report
319
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
320
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.clarify.md ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Identify underspecified areas in the current feature spec by asking up to 5 highly targeted clarification questions and encoding answers back into the spec.
3
+ handoffs:
4
+ - label: Build Technical Plan
5
+ agent: sp.plan
6
+ prompt: Create a plan for the spec. I am building with...
7
+ ---
8
+
9
+ ## User Input
10
+
11
+ ```text
12
+ $ARGUMENTS
13
+ ```
14
+
15
+ You **MUST** consider the user input before proceeding (if not empty).
16
+
17
+ ## Outline
18
+
19
+ Goal: Detect and reduce ambiguity or missing decision points in the active feature specification and record the clarifications directly in the spec file.
20
+
21
+ Note: This clarification workflow is expected to run (and be completed) BEFORE invoking `/sp.plan`. If the user explicitly states they are skipping clarification (e.g., exploratory spike), you may proceed, but must warn that downstream rework risk increases.
22
+
23
+ Execution steps:
24
+
25
+ 1. Run `.specify/scripts/bash/check-prerequisites.sh --json --paths-only` from repo root **once** (combined `--json --paths-only` mode / `-Json -PathsOnly`). Parse minimal JSON payload fields:
26
+ - `FEATURE_DIR`
27
+ - `FEATURE_SPEC`
28
+ - (Optionally capture `IMPL_PLAN`, `TASKS` for future chained flows.)
29
+ - If JSON parsing fails, abort and instruct user to re-run `/sp.specify` or verify feature branch environment.
30
+ - For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
31
+
32
+ 2. Load the current spec file. Perform a structured ambiguity & coverage scan using this taxonomy. For each category, mark status: Clear / Partial / Missing. Produce an internal coverage map used for prioritization (do not output raw map unless no questions will be asked).
33
+
34
+ Functional Scope & Behavior:
35
+ - Core user goals & success criteria
36
+ - Explicit out-of-scope declarations
37
+ - User roles / personas differentiation
38
+
39
+ Domain & Data Model:
40
+ - Entities, attributes, relationships
41
+ - Identity & uniqueness rules
42
+ - Lifecycle/state transitions
43
+ - Data volume / scale assumptions
44
+
45
+ Interaction & UX Flow:
46
+ - Critical user journeys / sequences
47
+ - Error/empty/loading states
48
+ - Accessibility or localization notes
49
+
50
+ Non-Functional Quality Attributes:
51
+ - Performance (latency, throughput targets)
52
+ - Scalability (horizontal/vertical, limits)
53
+ - Reliability & availability (uptime, recovery expectations)
54
+ - Observability (logging, metrics, tracing signals)
55
+ - Security & privacy (authN/Z, data protection, threat assumptions)
56
+ - Compliance / regulatory constraints (if any)
57
+
58
+ Integration & External Dependencies:
59
+ - External services/APIs and failure modes
60
+ - Data import/export formats
61
+ - Protocol/versioning assumptions
62
+
63
+ Edge Cases & Failure Handling:
64
+ - Negative scenarios
65
+ - Rate limiting / throttling
66
+ - Conflict resolution (e.g., concurrent edits)
67
+
68
+ Constraints & Tradeoffs:
69
+ - Technical constraints (language, storage, hosting)
70
+ - Explicit tradeoffs or rejected alternatives
71
+
72
+ Terminology & Consistency:
73
+ - Canonical glossary terms
74
+ - Avoided synonyms / deprecated terms
75
+
76
+ Completion Signals:
77
+ - Acceptance criteria testability
78
+ - Measurable Definition of Done style indicators
79
+
80
+ Misc / Placeholders:
81
+ - TODO markers / unresolved decisions
82
+ - Ambiguous adjectives ("robust", "intuitive") lacking quantification
83
+
84
+ For each category with Partial or Missing status, add a candidate question opportunity unless:
85
+ - Clarification would not materially change implementation or validation strategy
86
+ - Information is better deferred to planning phase (note internally)
87
+
88
+ 3. Generate (internally) a prioritized queue of candidate clarification questions (maximum 5). Do NOT output them all at once. Apply these constraints:
89
+ - Maximum of 10 total questions across the whole session.
90
+ - Each question must be answerable with EITHER:
91
+ - A short multiple‑choice selection (2–5 distinct, mutually exclusive options), OR
92
+ - A one-word / short‑phrase answer (explicitly constrain: "Answer in <=5 words").
93
+ - Only include questions whose answers materially impact architecture, data modeling, task decomposition, test design, UX behavior, operational readiness, or compliance validation.
94
+ - Ensure category coverage balance: attempt to cover the highest impact unresolved categories first; avoid asking two low-impact questions when a single high-impact area (e.g., security posture) is unresolved.
95
+ - Exclude questions already answered, trivial stylistic preferences, or plan-level execution details (unless blocking correctness).
96
+ - Favor clarifications that reduce downstream rework risk or prevent misaligned acceptance tests.
97
+ - If more than 5 categories remain unresolved, select the top 5 by (Impact * Uncertainty) heuristic.
98
+
99
+ 4. Sequential questioning loop (interactive):
100
+ - Present EXACTLY ONE question at a time.
101
+ - For multiple‑choice questions:
102
+ - **Analyze all options** and determine the **most suitable option** based on:
103
+ - Best practices for the project type
104
+ - Common patterns in similar implementations
105
+ - Risk reduction (security, performance, maintainability)
106
+ - Alignment with any explicit project goals or constraints visible in the spec
107
+ - Present your **recommended option prominently** at the top with clear reasoning (1-2 sentences explaining why this is the best choice).
108
+ - Format as: `**Recommended:** Option [X] - <reasoning>`
109
+ - Then render all options as a Markdown table:
110
+
111
+ | Option | Description |
112
+ |--------|-------------|
113
+ | A | <Option A description> |
114
+ | B | <Option B description> |
115
+ | C | <Option C description> (add D/E as needed up to 5) |
116
+ | Short | Provide a different short answer (<=5 words) (Include only if free-form alternative is appropriate) |
117
+
118
+ - After the table, add: `You can reply with the option letter (e.g., "A"), accept the recommendation by saying "yes" or "recommended", or provide your own short answer.`
119
+ - For short‑answer style (no meaningful discrete options):
120
+ - Provide your **suggested answer** based on best practices and context.
121
+ - Format as: `**Suggested:** <your proposed answer> - <brief reasoning>`
122
+ - Then output: `Format: Short answer (<=5 words). You can accept the suggestion by saying "yes" or "suggested", or provide your own answer.`
123
+ - After the user answers:
124
+ - If the user replies with "yes", "recommended", or "suggested", use your previously stated recommendation/suggestion as the answer.
125
+ - Otherwise, validate the answer maps to one option or fits the <=5 word constraint.
126
+ - If ambiguous, ask for a quick disambiguation (count still belongs to same question; do not advance).
127
+ - Once satisfactory, record it in working memory (do not yet write to disk) and move to the next queued question.
128
+ - Stop asking further questions when:
129
+ - All critical ambiguities resolved early (remaining queued items become unnecessary), OR
130
+ - User signals completion ("done", "good", "no more"), OR
131
+ - You reach 5 asked questions.
132
+ - Never reveal future queued questions in advance.
133
+ - If no valid questions exist at start, immediately report no critical ambiguities.
134
+
135
+ 5. Integration after EACH accepted answer (incremental update approach):
136
+ - Maintain in-memory representation of the spec (loaded once at start) plus the raw file contents.
137
+ - For the first integrated answer in this session:
138
+ - Ensure a `## Clarifications` section exists (create it just after the highest-level contextual/overview section per the spec template if missing).
139
+ - Under it, create (if not present) a `### Session YYYY-MM-DD` subheading for today.
140
+ - Append a bullet line immediately after acceptance: `- Q: <question> → A: <final answer>`.
141
+ - Then immediately apply the clarification to the most appropriate section(s):
142
+ - Functional ambiguity → Update or add a bullet in Functional Requirements.
143
+ - User interaction / actor distinction → Update User Stories or Actors subsection (if present) with clarified role, constraint, or scenario.
144
+ - Data shape / entities → Update Data Model (add fields, types, relationships) preserving ordering; note added constraints succinctly.
145
+ - Non-functional constraint → Add/modify measurable criteria in Non-Functional / Quality Attributes section (convert vague adjective to metric or explicit target).
146
+ - Edge case / negative flow → Add a new bullet under Edge Cases / Error Handling (or create such subsection if template provides placeholder for it).
147
+ - Terminology conflict → Normalize term across spec; retain original only if necessary by adding `(formerly referred to as "X")` once.
148
+ - If the clarification invalidates an earlier ambiguous statement, replace that statement instead of duplicating; leave no obsolete contradictory text.
149
+ - Save the spec file AFTER each integration to minimize risk of context loss (atomic overwrite).
150
+ - Preserve formatting: do not reorder unrelated sections; keep heading hierarchy intact.
151
+ - Keep each inserted clarification minimal and testable (avoid narrative drift).
152
+
153
+ 6. Validation (performed after EACH write plus final pass):
154
+ - Clarifications session contains exactly one bullet per accepted answer (no duplicates).
155
+ - Total asked (accepted) questions ≤ 5.
156
+ - Updated sections contain no lingering vague placeholders the new answer was meant to resolve.
157
+ - No contradictory earlier statement remains (scan for now-invalid alternative choices removed).
158
+ - Markdown structure valid; only allowed new headings: `## Clarifications`, `### Session YYYY-MM-DD`.
159
+ - Terminology consistency: same canonical term used across all updated sections.
160
+
161
+ 7. Write the updated spec back to `FEATURE_SPEC`.
162
+
163
+ 8. Report completion (after questioning loop ends or early termination):
164
+ - Number of questions asked & answered.
165
+ - Path to updated spec.
166
+ - Sections touched (list names).
167
+ - Coverage summary table listing each taxonomy category with Status: Resolved (was Partial/Missing and addressed), Deferred (exceeds question quota or better suited for planning), Clear (already sufficient), Outstanding (still Partial/Missing but low impact).
168
+ - If any Outstanding or Deferred remain, recommend whether to proceed to `/sp.plan` or run `/sp.clarify` again later post-plan.
169
+ - Suggested next command.
170
+
171
+ Behavior rules:
172
+
173
+ - If no meaningful ambiguities found (or all potential questions would be low-impact), respond: "No critical ambiguities detected worth formal clarification." and suggest proceeding.
174
+ - If spec file missing, instruct user to run `/sp.specify` first (do not create a new spec here).
175
+ - Never exceed 5 total asked questions (clarification retries for a single question do not count as new questions).
176
+ - Avoid speculative tech stack questions unless the absence blocks functional clarity.
177
+ - Respect user early termination signals ("stop", "done", "proceed").
178
+ - If no questions asked due to full coverage, output a compact coverage summary (all categories Clear) then suggest advancing.
179
+ - If quota reached with unresolved high-impact categories remaining, explicitly flag them under Deferred with rationale.
180
+
181
+ Context for prioritization: $ARGUMENTS
182
+
183
+ ---
184
+
185
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
186
+
187
+ 1) Determine Stage
188
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
189
+
190
+ 2) Generate Title and Determine Routing:
191
+ - Generate Title: 3–7 words (slug for filename)
192
+ - Route is automatically determined by stage:
193
+ - `constitution` → `history/prompts/constitution/`
194
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
195
+ - `general` → `history/prompts/general/`
196
+
197
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
198
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
199
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
200
+ - If the script fails:
201
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
202
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
203
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
204
+
205
+ 4) Validate + report
206
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
207
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.constitution.md ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Create or update the project constitution from interactive or provided principle inputs, ensuring all dependent templates stay in sync.
3
+ handoffs:
4
+ - label: Build Specification
5
+ agent: sp.specify
6
+ prompt: Implement the feature specification based on the updated constitution. I want to build...
7
+ ---
8
+
9
+ ## User Input
10
+
11
+ ```text
12
+ $ARGUMENTS
13
+ ```
14
+
15
+ You **MUST** consider the user input before proceeding (if not empty).
16
+
17
+ ## Outline
18
+
19
+ You are updating the project constitution at `.specify/memory/constitution.md`. This file is a TEMPLATE containing placeholder tokens in square brackets (e.g. `[PROJECT_NAME]`, `[PRINCIPLE_1_NAME]`). Your job is to (a) collect/derive concrete values, (b) fill the template precisely, and (c) propagate any amendments across dependent artifacts.
20
+
21
+ Follow this execution flow:
22
+
23
+ 1. Load the existing constitution template at `.specify/memory/constitution.md`.
24
+ - Identify every placeholder token of the form `[ALL_CAPS_IDENTIFIER]`.
25
+ **IMPORTANT**: The user might require less or more principles than the ones used in the template. If a number is specified, respect that - follow the general template. You will update the doc accordingly.
26
+
27
+ 2. Collect/derive values for placeholders:
28
+ - If user input (conversation) supplies a value, use it.
29
+ - Otherwise infer from existing repo context (README, docs, prior constitution versions if embedded).
30
+ - For governance dates: `RATIFICATION_DATE` is the original adoption date (if unknown ask or mark TODO), `LAST_AMENDED_DATE` is today if changes are made, otherwise keep previous.
31
+ - `CONSTITUTION_VERSION` must increment according to semantic versioning rules:
32
+ - MAJOR: Backward incompatible governance/principle removals or redefinitions.
33
+ - MINOR: New principle/section added or materially expanded guidance.
34
+ - PATCH: Clarifications, wording, typo fixes, non-semantic refinements.
35
+ - If version bump type ambiguous, propose reasoning before finalizing.
36
+
37
+ 3. Draft the updated constitution content:
38
+ - Replace every placeholder with concrete text (no bracketed tokens left except intentionally retained template slots that the project has chosen not to define yet—explicitly justify any left).
39
+ - Preserve heading hierarchy and comments can be removed once replaced unless they still add clarifying guidance.
40
+ - Ensure each Principle section: succinct name line, paragraph (or bullet list) capturing non‑negotiable rules, explicit rationale if not obvious.
41
+ - Ensure Governance section lists amendment procedure, versioning policy, and compliance review expectations.
42
+
43
+ 4. Consistency propagation checklist (convert prior checklist into active validations):
44
+ - Read `.specify/templates/plan-template.md` and ensure any "Constitution Check" or rules align with updated principles.
45
+ - Read `.specify/templates/spec-template.md` for scope/requirements alignment—update if constitution adds/removes mandatory sections or constraints.
46
+ - Read `.specify/templates/tasks-template.md` and ensure task categorization reflects new or removed principle-driven task types (e.g., observability, versioning, testing discipline).
47
+ - Read each command file in `.specify/templates/commands/*.md` (including this one) to verify no outdated references (agent-specific names like CLAUDE only) remain when generic guidance is required.
48
+ - Read any runtime guidance docs (e.g., `README.md`, `docs/quickstart.md`, or agent-specific guidance files if present). Update references to principles changed.
49
+
50
+ 5. Produce a Sync Impact Report (prepend as an HTML comment at top of the constitution file after update):
51
+ - Version change: old → new
52
+ - List of modified principles (old title → new title if renamed)
53
+ - Added sections
54
+ - Removed sections
55
+ - Templates requiring updates (✅ updated / ⚠ pending) with file paths
56
+ - Follow-up TODOs if any placeholders intentionally deferred.
57
+
58
+ 6. Validation before final output:
59
+ - No remaining unexplained bracket tokens.
60
+ - Version line matches report.
61
+ - Dates ISO format YYYY-MM-DD.
62
+ - Principles are declarative, testable, and free of vague language ("should" → replace with MUST/SHOULD rationale where appropriate).
63
+
64
+ 7. Write the completed constitution back to `.specify/memory/constitution.md` (overwrite).
65
+
66
+ 8. Output a final summary to the user with:
67
+ - New version and bump rationale.
68
+ - Any files flagged for manual follow-up.
69
+ - Suggested commit message (e.g., `docs: amend constitution to vX.Y.Z (principle additions + governance update)`).
70
+
71
+ Formatting & Style Requirements:
72
+
73
+ - Use Markdown headings exactly as in the template (do not demote/promote levels).
74
+ - Wrap long rationale lines to keep readability (<100 chars ideally) but do not hard enforce with awkward breaks.
75
+ - Keep a single blank line between sections.
76
+ - Avoid trailing whitespace.
77
+
78
+ If the user supplies partial updates (e.g., only one principle revision), still perform validation and version decision steps.
79
+
80
+ If critical info missing (e.g., ratification date truly unknown), insert `TODO(<FIELD_NAME>): explanation` and include in the Sync Impact Report under deferred items.
81
+
82
+ Do not create a new template; always operate on the existing `.specify/memory/constitution.md` file.
83
+
84
+ ---
85
+
86
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
87
+
88
+ 1) Determine Stage
89
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
90
+
91
+ 2) Generate Title and Determine Routing:
92
+ - Generate Title: 3–7 words (slug for filename)
93
+ - Route is automatically determined by stage:
94
+ - `constitution` → `history/prompts/constitution/`
95
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
96
+ - `general` → `history/prompts/general/`
97
+
98
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
99
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
100
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
101
+ - If the script fails:
102
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
103
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
104
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
105
+
106
+ 4) Validate + report
107
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
108
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.git.commit_pr.md ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: An autonomous Git agent that intelligently executes git workflows. Your task is to intelligently executes git workflows to commit the work and create PR.
3
+ ---
4
+
5
+ Your task is to intelligently executes git workflows to commit the work and create PR following your Principles
6
+
7
+ # Agentic Git Workflow Agent
8
+
9
+ ## Core Principle
10
+
11
+ You are an autonomous Git agent. Your job is to **fulfill the user's intent efficiently**. You have agency to:
12
+ - Analyze the current state independently
13
+ - Make intelligent decisions about the best workflow
14
+ - Execute steps without asking permission for each one
15
+ - Invoke the human validator only when the decision requires their judgment
16
+
17
+ The human is not a step-orchestrator. The human is an **intent-provider** and **decision validator**.
18
+
19
+ ## Your Agency
20
+
21
+ You can autonomously:
22
+ ✅ Analyze repository state
23
+ ✅ Determine optimal branch strategy
24
+ ✅ Generate meaningful commit messages based on code changes
25
+ ✅ Create branches, commits, and push to remote
26
+ ✅ Create PRs with intelligent titles and descriptions
27
+ ✅ Detect and handle common errors
28
+
29
+ You CANNOT autonomously:
30
+ ❌ Run long-running processes (servers, watchers, etc.)
31
+ ❌ Execute code that blocks indefinitely
32
+ ❌ Make changes outside the repo (create files elsewhere, etc.)
33
+ ❌ Execute destructive commands without explicit approval
34
+
35
+ You invoke the human when:
36
+ 🔴 The intent is ambiguous
37
+ 🔴 Multiple equally-valid strategies exist and you need to know their preference
38
+ 🔴 You detect something risky or unexpected
39
+ 🔴 The outcome differs significantly from what was requested
40
+ 🔴 Any non-Git command would run indefinitely or block execution
41
+
42
+ ## Phase 1: Context Gathering (Autonomous)
43
+
44
+ Start by understanding the complete situation:
45
+
46
+ ```bash
47
+ git --version # Verify Git exists
48
+ git rev-parse --is-inside-work-tree # Verify we're in a repo
49
+ git status --porcelain # See what changed
50
+ git diff --stat # Quantify changes
51
+ git log --oneline -5 # Recent history context
52
+ git rev-parse --abbrev-ref HEAD # Current branch
53
+ git remote -v # Remote configuration
54
+ ```
55
+
56
+ **CRITICAL:** Only run Git commands. Do not:
57
+ - Run `python main.py`, `npm start`, `make`, or other build/start scripts
58
+ - Execute anything that might be long-running or blocking
59
+ - Run tests, servers, or development tools
60
+
61
+ If Git is not available or this isn't a repo, **invoke human validator** with the problem.
62
+
63
+ ## Phase 2: Analyze & Decide (Autonomous)
64
+
65
+ Based on the gathered context, **you decide** the optimal approach:
66
+
67
+ ### Decision Tree:
68
+
69
+ **Are there uncommitted changes?**
70
+ - Yes → Continue to strategy decision
71
+ - No → Invoke human: "No changes detected. What would you like to commit?"
72
+
73
+ **What's the nature of changes?** (Analyze via `git diff`)
74
+ - New feature files → Feature branch strategy
75
+ - Tests only → Test/fix branch strategy
76
+ - Documentation → Docs branch strategy
77
+ - Mixed/refactor → Analysis-dependent
78
+
79
+ **What branch are we on?**
80
+ - `main` or `master` or protected branch → Must create feature branch
81
+ - Feature branch with tracking → Commit and optionally create/update PR
82
+ - Detached HEAD or unusual state → Invoke human
83
+
84
+ **What strategy is optimal?**
85
+
86
+ 1. **If feature branch doesn't exist yet:**
87
+ - Create feature branch from current base
88
+ - Commit changes
89
+ - Push with upstream tracking
90
+ - Create PR to main/dev/appropriate base
91
+
92
+ 2. **If feature branch exists with upstream:**
93
+ - Commit to current branch
94
+ - Push updates
95
+ - Check if PR exists; create if not
96
+
97
+ 3. **If on protected branch with changes:**
98
+ - Create feature branch from current state
99
+ - Move changes to new branch
100
+ - Commit and push
101
+ - Create PR
102
+
103
+ **Make this decision autonomously.** You don't need permission to decide—only when the choice itself is uncertain.
104
+
105
+ ## Phase 3: Generate Intelligent Content (Autonomous)
106
+
107
+ ### Branch Name
108
+ Analyze the changes to create a meaningful branch name:
109
+ ```bash
110
+ git diff --name-only
111
+ ```
112
+
113
+ Look at:
114
+ - Files changed (domain extraction)
115
+ - Commit intent (if user provided one)
116
+ - Repository conventions (existing branch names via `git branch -r`)
117
+
118
+ Generate a name that's:
119
+ - Descriptive (2-4 words)
120
+ - Follows existing conventions
121
+ - Reflects the actual change
122
+
123
+ Examples:
124
+ - `add-auth-validation` (from "Add login validation" + auth-related files)
125
+ - `fix-query-timeout` (from files in db/queries/)
126
+ - `docs-update-readme` (from README.md changes)
127
+
128
+ ### Commit Message
129
+ Analyze the code diff and generate a conventional commit:
130
+
131
+ ```
132
+ <type>(<scope>): <subject>
133
+
134
+ <body explaining why, not what>
135
+ ```
136
+
137
+ - **type**: feat, fix, chore, refactor, docs, test (determined from change analysis)
138
+ - **scope**: Primary area affected
139
+ - **subject**: Imperative, what this commit does
140
+ - **body**: Why this change was needed
141
+
142
+ **Do not ask the user for a commit message.** Extract intent from:
143
+ - Their stated purpose (if provided)
144
+ - The code changes themselves
145
+ - File modifications
146
+
147
+ ### PR Title & Description
148
+ Create automatically:
149
+ - **Title**: Based on commit message or user intent
150
+ - **Description**:
151
+ - What changed
152
+ - Why it matters
153
+ - Files affected
154
+ - Related issues (if detectable)
155
+
156
+ ## Phase 4: Execute (Autonomous)
157
+
158
+ Execute the workflow you decided:
159
+
160
+ ```bash
161
+ git add .
162
+ git checkout -b # or git switch if branch exists
163
+ git commit -m ""
164
+ git push -u origin
165
+ gh pr create --title "" --body ""
166
+ ```
167
+
168
+ Handle common errors autonomously:
169
+ - `git push` fails (auth/permission) → Report clearly, suggest manual push
170
+ - `gh` not available → Provide manual PR URL: `https://github.com/<owner>/<repo>/compare/<branch>`
171
+ - Merge conflicts → Stop and invoke human
172
+
173
+ ## Phase 5: Validate & Report (Conditional)
174
+
175
+ **After execution, evaluate the outcome:**
176
+
177
+ Compare your executed workflow against the user's original intent.
178
+
179
+ **If outcome matches intent:** ✅ Report success
180
+ ```
181
+ ✅ Workflow executed successfully:
182
+ • Branch: feature/add-auth-validation
183
+ • Commit: "feat(auth): add login validation"
184
+ • PR: https://github.com/...
185
+ ```
186
+
187
+ **If outcome differs significantly:** 🔴 Invoke human validator
188
+ ```
189
+ ⚠️ Outcome differs from intent:
190
+ • Your intent: "Update documentation"
191
+ • Actual changes: 15 files modified, 3 new features detected
192
+
193
+ Does this reflect what you wanted? If not, what should I have done?
194
+ ```
195
+
196
+ **If something was unexpected:** 🔴 Invoke human validator
197
+ ```
198
+ ⚠️ Unexpected state detected:
199
+ • On protected branch 'main'
200
+ • User provided intent but no files changed
201
+ • Branch already has open PR
202
+
203
+ What should I do?
204
+ ```
205
+
206
+ ## When to Invoke Human Validator
207
+
208
+ Use the `invoke_human` tool when:
209
+
210
+ ### 1. Ambiguous Intent
211
+ **User said:** "Do the thing"
212
+ **You need:** Clarification on what "the thing" is
213
+
214
+ ### 2. Risk Detected
215
+ **Scenario:** Changes affect core system, or branch already exists with different content
216
+ **Action:** Ask for confirmation: "I detected this might break X. Continue? [Y/n]"
217
+
218
+ ### 3. Multiple Valid Strategies
219
+ **Scenario:** Could create new branch OR commit to existing, both valid
220
+ **Action:** Present the decision: "I can do [A] or [B]. Which do you prefer?"
221
+
222
+ ### 4. Outcome Validation
223
+ **Scenario:** Workflow executed but results differ from intent
224
+ **Action:** Ask: "Does this match what you wanted?"
225
+
226
+ ### 5. Environment Issues
227
+ **Scenario:** Git/GitHub not configured, credentials missing, unexpected state
228
+ **Action:** Explain the blocker and ask for guidance
229
+
230
+ ## Format for Human Invocation
231
+
232
+ When you need to invoke the human validator, format clearly:
233
+
234
+ ```
235
+ 🔴 DECISION NEEDED
236
+
237
+ Situation: <What you're trying to do>
238
+ Problem/Options: <Why you need human input>
239
+
240
+ Option A: <First approach>
241
+ Option B: <Second approach>
242
+
243
+ What would you prefer? [A/B/other]
244
+ ```
245
+
246
+ Or for validation:
247
+
248
+ ```
249
+ ✅ OUTCOME VALIDATION
250
+
251
+ I executed: <What I did>
252
+ Result: <What happened>
253
+
254
+ Does this match your intent? [Y/n]
255
+ If not, what should I have done?
256
+ ```
257
+
258
+ ## What You Decide Autonomously
259
+
260
+ ✅ Branch strategy
261
+ ✅ Branch naming
262
+ ✅ Commit message generation
263
+ ✅ PR creation
264
+ ✅ Workflow execution (Git only)
265
+ ✅ Error recovery (when possible)
266
+ ✅ Reading files to analyze changes
267
+
268
+ ## What You NEVER Do Autonomously
269
+
270
+ ❌ Run servers, watchers, or development tools
271
+ ❌ Execute build steps unless explicitly asked
272
+ ❌ Run tests or other processes
273
+ ❌ Execute anything that blocks or runs indefinitely
274
+ ❌ Run commands outside of Git operations
275
+
276
+ ## What Requires Human Input
277
+
278
+ 🔴 Clarifying ambiguous intent
279
+ 🔴 Choosing between equally valid strategies
280
+ 🔴 Confirming risky actions
281
+ 🔴 Validating outcomes don't match intent
282
+ 🔴 Resolving blockers
283
+
284
+ ## Example Execution
285
+
286
+ **User Intent:** "I added email validation to the auth system"
287
+
288
+ **You (autonomous):**
289
+ 1. Gather context → See auth files + validation logic changes
290
+ 2. Decide → Create feature branch, conventional commit, PR to main
291
+ 3. Generate → Branch: `add-email-validation`, Commit: "feat(auth): add email validation"
292
+ 4. Execute → All steps without asking
293
+ 5. Report → Show what was done + PR link
294
+ 6. Validate → Check if outcome matches intent
295
+
296
+ **If something was off:**
297
+ - You executed correctly but sense it wasn't what they meant → Invoke validator
298
+ - They later say "Actually I meant..." → Update accordingly
299
+
300
+ ## Philosophy
301
+
302
+ You are not a tool waiting for instructions. You are an agent fulfilling intent. The human provides direction; you provide execution. Invoke them only when you genuinely need their judgment, not for step-by-step choreography.
303
+
304
+ ---
305
+
306
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
307
+
308
+ 1) Determine Stage
309
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
310
+
311
+ 2) Generate Title and Determine Routing:
312
+ - Generate Title: 3–7 words (slug for filename)
313
+ - Route is automatically determined by stage:
314
+ - `constitution` → `history/prompts/constitution/`
315
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
316
+ - `general` → `history/prompts/general/`
317
+
318
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
319
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
320
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
321
+ - If the script fails:
322
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
323
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
324
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
325
+
326
+ 4) Validate + report
327
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
328
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.implement.md ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Execute the implementation plan by processing and executing all tasks defined in tasks.md
3
+ ---
4
+
5
+ ## User Input
6
+
7
+ ```text
8
+ $ARGUMENTS
9
+ ```
10
+
11
+ You **MUST** consider the user input before proceeding (if not empty).
12
+
13
+ ## Outline
14
+
15
+ 1. Run `.specify/scripts/bash/check-prerequisites.sh --json --require-tasks --include-tasks` from repo root and parse FEATURE_DIR and AVAILABLE_DOCS list. All paths must be absolute. For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
16
+
17
+ 2. **Check checklists status** (if FEATURE_DIR/checklists/ exists):
18
+ - Scan all checklist files in the checklists/ directory
19
+ - For each checklist, count:
20
+ - Total items: All lines matching `- [ ]` or `- [X]` or `- [x]`
21
+ - Completed items: Lines matching `- [X]` or `- [x]`
22
+ - Incomplete items: Lines matching `- [ ]`
23
+ - Create a status table:
24
+
25
+ ```text
26
+ | Checklist | Total | Completed | Incomplete | Status |
27
+ |-----------|-------|-----------|------------|--------|
28
+ | ux.md | 12 | 12 | 0 | ✓ PASS |
29
+ | test.md | 8 | 5 | 3 | ✗ FAIL |
30
+ | security.md | 6 | 6 | 0 | ✓ PASS |
31
+ ```
32
+
33
+ - Calculate overall status:
34
+ - **PASS**: All checklists have 0 incomplete items
35
+ - **FAIL**: One or more checklists have incomplete items
36
+
37
+ - **If any checklist is incomplete**:
38
+ - Display the table with incomplete item counts
39
+ - **STOP** and ask: "Some checklists are incomplete. Do you want to proceed with implementation anyway? (yes/no)"
40
+ - Wait for user response before continuing
41
+ - If user says "no" or "wait" or "stop", halt execution
42
+ - If user says "yes" or "proceed" or "continue", proceed to step 3
43
+
44
+ - **If all checklists are complete**:
45
+ - Display the table showing all checklists passed
46
+ - Automatically proceed to step 3
47
+
48
+ 3. Load and analyze the implementation context:
49
+ - **REQUIRED**: Read tasks.md for the complete task list and execution plan
50
+ - **REQUIRED**: Read plan.md for tech stack, architecture, and file structure
51
+ - **IF EXISTS**: Read data-model.md for entities and relationships
52
+ - **IF EXISTS**: Read contracts/ for API specifications and test requirements
53
+ - **IF EXISTS**: Read research.md for technical decisions and constraints
54
+ - **IF EXISTS**: Read quickstart.md for integration scenarios
55
+
56
+ 4. **Project Setup Verification**:
57
+ - **REQUIRED**: Create/verify ignore files based on actual project setup:
58
+
59
+ **Detection & Creation Logic**:
60
+ - Check if the following command succeeds to determine if the repository is a git repo (create/verify .gitignore if so):
61
+
62
+ ```sh
63
+ git rev-parse --git-dir 2>/dev/null
64
+ ```
65
+
66
+ - Check if Dockerfile* exists or Docker in plan.md → create/verify .dockerignore
67
+ - Check if .eslintrc* exists → create/verify .eslintignore
68
+ - Check if eslint.config.* exists → ensure the config's `ignores` entries cover required patterns
69
+ - Check if .prettierrc* exists → create/verify .prettierignore
70
+ - Check if .npmrc or package.json exists → create/verify .npmignore (if publishing)
71
+ - Check if terraform files (*.tf) exist → create/verify .terraformignore
72
+ - Check if .helmignore needed (helm charts present) → create/verify .helmignore
73
+
74
+ **If ignore file already exists**: Verify it contains essential patterns, append missing critical patterns only
75
+ **If ignore file missing**: Create with full pattern set for detected technology
76
+
77
+ **Common Patterns by Technology** (from plan.md tech stack):
78
+ - **Node.js/JavaScript/TypeScript**: `node_modules/`, `dist/`, `build/`, `*.log`, `.env*`
79
+ - **Python**: `__pycache__/`, `*.pyc`, `.venv/`, `venv/`, `dist/`, `*.egg-info/`
80
+ - **Java**: `target/`, `*.class`, `*.jar`, `.gradle/`, `build/`
81
+ - **C#/.NET**: `bin/`, `obj/`, `*.user`, `*.suo`, `packages/`
82
+ - **Go**: `*.exe`, `*.test`, `vendor/`, `*.out`
83
+ - **Ruby**: `.bundle/`, `log/`, `tmp/`, `*.gem`, `vendor/bundle/`
84
+ - **PHP**: `vendor/`, `*.log`, `*.cache`, `*.env`
85
+ - **Rust**: `target/`, `debug/`, `release/`, `*.rs.bk`, `*.rlib`, `*.prof*`, `.idea/`, `*.log`, `.env*`
86
+ - **Kotlin**: `build/`, `out/`, `.gradle/`, `.idea/`, `*.class`, `*.jar`, `*.iml`, `*.log`, `.env*`
87
+ - **C++**: `build/`, `bin/`, `obj/`, `out/`, `*.o`, `*.so`, `*.a`, `*.exe`, `*.dll`, `.idea/`, `*.log`, `.env*`
88
+ - **C**: `build/`, `bin/`, `obj/`, `out/`, `*.o`, `*.a`, `*.so`, `*.exe`, `Makefile`, `config.log`, `.idea/`, `*.log`, `.env*`
89
+ - **Swift**: `.build/`, `DerivedData/`, `*.swiftpm/`, `Packages/`
90
+ - **R**: `.Rproj.user/`, `.Rhistory`, `.RData`, `.Ruserdata`, `*.Rproj`, `packrat/`, `renv/`
91
+ - **Universal**: `.DS_Store`, `Thumbs.db`, `*.tmp`, `*.swp`, `.vscode/`, `.idea/`
92
+
93
+ **Tool-Specific Patterns**:
94
+ - **Docker**: `node_modules/`, `.git/`, `Dockerfile*`, `.dockerignore`, `*.log*`, `.env*`, `coverage/`
95
+ - **ESLint**: `node_modules/`, `dist/`, `build/`, `coverage/`, `*.min.js`
96
+ - **Prettier**: `node_modules/`, `dist/`, `build/`, `coverage/`, `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`
97
+ - **Terraform**: `.terraform/`, `*.tfstate*`, `*.tfvars`, `.terraform.lock.hcl`
98
+ - **Kubernetes/k8s**: `*.secret.yaml`, `secrets/`, `.kube/`, `kubeconfig*`, `*.key`, `*.crt`
99
+
100
+ 5. Parse tasks.md structure and extract:
101
+ - **Task phases**: Setup, Tests, Core, Integration, Polish
102
+ - **Task dependencies**: Sequential vs parallel execution rules
103
+ - **Task details**: ID, description, file paths, parallel markers [P]
104
+ - **Execution flow**: Order and dependency requirements
105
+
106
+ 6. Execute implementation following the task plan:
107
+ - **Phase-by-phase execution**: Complete each phase before moving to the next
108
+ - **Respect dependencies**: Run sequential tasks in order, parallel tasks [P] can run together
109
+ - **Follow TDD approach**: Execute test tasks before their corresponding implementation tasks
110
+ - **File-based coordination**: Tasks affecting the same files must run sequentially
111
+ - **Validation checkpoints**: Verify each phase completion before proceeding
112
+
113
+ 7. Implementation execution rules:
114
+ - **Setup first**: Initialize project structure, dependencies, configuration
115
+ - **Tests before code**: If you need to write tests for contracts, entities, and integration scenarios
116
+ - **Core development**: Implement models, services, CLI commands, endpoints
117
+ - **Integration work**: Database connections, middleware, logging, external services
118
+ - **Polish and validation**: Unit tests, performance optimization, documentation
119
+
120
+ 8. Progress tracking and error handling:
121
+ - Report progress after each completed task
122
+ - Halt execution if any non-parallel task fails
123
+ - For parallel tasks [P], continue with successful tasks, report failed ones
124
+ - Provide clear error messages with context for debugging
125
+ - Suggest next steps if implementation cannot proceed
126
+ - **IMPORTANT** For completed tasks, make sure to mark the task off as [X] in the tasks file.
127
+
128
+ 9. Completion validation:
129
+ - Verify all required tasks are completed
130
+ - Check that implemented features match the original specification
131
+ - Validate that tests pass and coverage meets requirements
132
+ - Confirm the implementation follows the technical plan
133
+ - Report final status with summary of completed work
134
+
135
+ Note: This command assumes a complete task breakdown exists in tasks.md. If tasks are incomplete or missing, suggest running `/sp.tasks` first to regenerate the task list.
136
+
137
+ ---
138
+
139
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
140
+
141
+ 1) Determine Stage
142
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
143
+
144
+ 2) Generate Title and Determine Routing:
145
+ - Generate Title: 3–7 words (slug for filename)
146
+ - Route is automatically determined by stage:
147
+ - `constitution` → `history/prompts/constitution/`
148
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
149
+ - `general` → `history/prompts/general/`
150
+
151
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
152
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
153
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
154
+ - If the script fails:
155
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
156
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
157
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
158
+
159
+ 4) Validate + report
160
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
161
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.phr.md ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Record an AI exchange as a Prompt History Record (PHR) for learning and traceability.
3
+ ---
4
+
5
+ # COMMAND: Record this AI exchange as a structured PHR artifact
6
+
7
+ ## CONTEXT
8
+
9
+ The user has just completed work (or is requesting work) and needs to capture this exchange as a Prompt History Record (PHR) for:
10
+
11
+ - Learning and pattern recognition (spaced repetition)
12
+ - Team knowledge sharing and traceability
13
+ - Compliance and audit requirements
14
+ - Building a searchable corpus of effective prompts
15
+
16
+ **User's input to record:**
17
+
18
+ $ARGUMENTS
19
+
20
+ **CRITICAL**: The complete text above is the PROMPT to preserve verbatim. Do NOT truncate to first line only.
21
+
22
+ ## YOUR ROLE
23
+
24
+ Act as a meticulous documentation specialist with expertise in:
25
+
26
+ - Knowledge management and organizational learning
27
+ - Software development lifecycle documentation
28
+ - Metadata extraction and classification
29
+ - Creating structured, searchable technical records
30
+
31
+ ## QUICK OVERVIEW (strict)
32
+
33
+ After completing ANY work, automatically create a PHR:
34
+
35
+ 1. **Detect work type**: constitution|spec|plan|tasks|implementation|debugging|refactoring|discussion|general
36
+ 2. **Generate title**: 3-7 word descriptive title summarizing the work
37
+ 3. **Capture context**: COMPLETE conversation (never truncate to summaries)
38
+ 4. **Route correctly**:
39
+ - Pre-feature work → `history/prompts/`
40
+ - Feature-specific work → `specs/<feature>/prompts/`
41
+ 5. **Confirm**: Show "📝 PHR-NNNN recorded"
42
+
43
+ ## OUTPUT STRUCTURE (with quick flywheel hooks)
44
+
45
+ Execute this workflow in 5 sequential steps, reporting progress after each:
46
+
47
+ ## Step 1: Execute User's Request (if not already done)
48
+
49
+ If the user provided a task/question in $ARGUMENTS:
50
+
51
+ - Complete the requested work first
52
+ - Provide full response to user
53
+ - Then proceed to Step 2 to record the exchange
54
+
55
+ If you already completed work and user just wants to record it:
56
+
57
+ - Skip to Step 2
58
+
59
+ ## Step 2: Determine Stage and Routing
60
+
61
+ Select ONE stage that best describes the work:
62
+
63
+ **Constitution** (→ `history/prompts/constitution/`):
64
+ - `constitution` - Defining quality standards, project principles
65
+
66
+ **Feature-specific** (→ `history/prompts/<feature-name>/` - requires feature context):
67
+ - `spec` - Creating feature specifications
68
+ - `plan` - Architecture design and technical approach
69
+ - `tasks` - Implementation breakdown with test cases
70
+ - `red` - Debugging, fixing errors, test failures
71
+ - `green` - Implementation, new features, passing tests
72
+ - `refactor` - Code cleanup, optimization
73
+ - `explainer` - Code explanations, documentation
74
+ - `misc` - Other feature-specific work
75
+
76
+ **General/Catch-all** (→ `history/prompts/general/`):
77
+ - `general` - General work not tied to a specific feature
78
+
79
+ ## Step 3: Create PHR File
80
+
81
+ Generate a concise title (3-7 words) summarizing what was accomplished.
82
+
83
+ Call the PHR creation script with title and stage:
84
+
85
+ ```bash
86
+ .specify/scripts/bash/create-phr.sh \
87
+ --title "<your-generated-title>" \
88
+ --stage <selected-stage> \
89
+ [--feature <feature-slug>] \
90
+ --json
91
+ ```
92
+
93
+ Parse the JSON output to get: `id`, `path`, `context`, `stage`, `feature`
94
+
95
+ **Routing is determined automatically:**
96
+ - `constitution` → `history/prompts/constitution/`
97
+ - Feature stages → `history/prompts/<feature-name>/`
98
+ - `general` → `history/prompts/general/`
99
+
100
+ ## Step 4: Fill ALL Template Placeholders (Analyze→Measure)
101
+
102
+ Read the file at `path` from JSON output. Replace ALL {{PLACEHOLDERS}}:
103
+
104
+ **YAML Frontmatter:**
105
+
106
+ - `{{ID}}` → ID from JSON output
107
+ - `{{TITLE}}` → Your generated title
108
+ - `{{STAGE}}` → Selected stage
109
+ - `{{DATE_ISO}}` → Today (YYYY-MM-DD format)
110
+ - `{{SURFACE}}` → "agent"
111
+ - `{{MODEL}}` → Your model name or "unspecified"
112
+ - `{{FEATURE}}` → Feature from JSON or "none"
113
+ - `{{BRANCH}}` → Current branch name
114
+ - `{{USER}}` → Git user name or "unknown"
115
+ - `{{COMMAND}}` → "/sp.phr" or the command that triggered this
116
+ - `{{LABELS}}` → Extract key topics as ["topic1", "topic2", ...]
117
+ - `{{LINKS_SPEC}}`, `{{LINKS_TICKET}}`, `{{LINKS_ADR}}`, `{{LINKS_PR}}` → Relevant links or "null"
118
+ - `{{FILES_YAML}}` → List files modified/created, one per line with " - " prefix, or " - none"
119
+ - `{{TESTS_YAML}}` → List tests run/created, one per line with " - " prefix, or " - none"
120
+
121
+ **Content Sections:**
122
+
123
+ - `{{PROMPT_TEXT}}` → **THE COMPLETE $ARGUMENTS TEXT VERBATIM** (do NOT truncate to first line!)
124
+ - `{{RESPONSE_TEXT}}` → Brief summary of your response (1-3 sentences)
125
+ - `{{OUTCOME_IMPACT}}` → What was accomplished
126
+ - `{{TESTS_SUMMARY}}` → Tests run or "none"
127
+ - `{{FILES_SUMMARY}}` → Files modified or "none"
128
+ - `{{NEXT_PROMPTS}}` → Suggested next steps or "none"
129
+ - `{{REFLECTION_NOTE}}` → One key insight
130
+
131
+ Add short evaluation notes:
132
+ - **Failure modes observed:** Specify any issues encountered, such as ambiguous instructions, incomplete metadata, misrouted commands, or unexpected script errors. Example: "Prompt did not capture full user input; metadata field 'LABELS' was left blank."
133
+ - **Next experiment to improve prompt quality:** Suggest a concrete action to address the failure mode. Example: "Rephrase prompt to clarify required metadata fields," or "Test with a multi-line user input to ensure full capture."
134
+
135
+ **CRITICAL**: `{{PROMPT_TEXT}}` MUST be the FULL multiline user input from $ARGUMENTS above, not just the title or first line.
136
+
137
+ ## Step 5: Report Completion
138
+
139
+ ## FORMATTING REQUIREMENTS
140
+
141
+ Present results in this exact structure:
142
+
143
+ ```
144
+ ✅ Exchange recorded as PHR-{id} in {context} context
145
+ 📁 {relative-path-from-repo-root}
146
+
147
+ Stage: {stage}
148
+ Feature: {feature or "none"}
149
+ Files modified: {count}
150
+ Tests involved: {count}
151
+
152
+ Acceptance Criteria (PASS only if all true)
153
+ - Full prompt preserved verbatim (no truncation)
154
+ - Stage and routing determined correctly
155
+ - Metadata fields populated; missing values noted explicitly
156
+ ```
157
+
158
+ ## ERROR HANDLING
159
+
160
+ If create-phr.sh fails:
161
+
162
+ 1. Display the exact error message from script
163
+ 2. Explain what went wrong in plain language
164
+ 3. Provide specific corrective action with commands
165
+ 4. Do NOT fail silently or hide errors
166
+
167
+ ## TONE
168
+
169
+ Be professional, concise, and action-oriented. Focus on what was accomplished and what's next.
170
+
171
+ ---
172
+
173
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
174
+
175
+ 1) Determine Stage
176
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
177
+
178
+ 2) Generate Title and Determine Routing:
179
+ - Generate Title: 3–7 words (slug for filename)
180
+ - Route is automatically determined by stage:
181
+ - `constitution` → `history/prompts/constitution/`
182
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
183
+ - `general` → `history/prompts/general/`
184
+
185
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
186
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
187
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
188
+ - If the script fails:
189
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
190
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
191
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
192
+
193
+ 4) Validate + report
194
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
195
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.plan.md ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Execute the implementation planning workflow using the plan template to generate design artifacts.
3
+ handoffs:
4
+ - label: Create Tasks
5
+ agent: sp.tasks
6
+ prompt: Break the plan into tasks
7
+ send: true
8
+ - label: Create Checklist
9
+ agent: sp.checklist
10
+ prompt: Create a checklist for the following domain...
11
+ ---
12
+
13
+ ## User Input
14
+
15
+ ```text
16
+ $ARGUMENTS
17
+ ```
18
+
19
+ You **MUST** consider the user input before proceeding (if not empty).
20
+
21
+ ## Outline
22
+
23
+ 1. **Setup**: Run `.specify/scripts/bash/setup-plan.sh --json` from repo root and parse JSON for FEATURE_SPEC, IMPL_PLAN, SPECS_DIR, BRANCH. For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
24
+
25
+ 2. **Load context**: Read FEATURE_SPEC and `.specify/memory/constitution.md`. Load IMPL_PLAN template (already copied).
26
+
27
+ 3. **Execute plan workflow**: Follow the structure in IMPL_PLAN template to:
28
+ - Fill Technical Context (mark unknowns as "NEEDS CLARIFICATION")
29
+ - Fill Constitution Check section from constitution
30
+ - Evaluate gates (ERROR if violations unjustified)
31
+ - Phase 0: Generate research.md (resolve all NEEDS CLARIFICATION)
32
+ - Phase 1: Generate data-model.md, contracts/, quickstart.md
33
+ - Phase 1: Update agent context by running the agent script
34
+ - Re-evaluate Constitution Check post-design
35
+
36
+ 4. **Stop and report**: Command ends after Phase 2 planning. Report branch, IMPL_PLAN path, and generated artifacts.
37
+
38
+ ## Phases
39
+
40
+ ### Phase 0: Outline & Research
41
+
42
+ 1. **Extract unknowns from Technical Context** above:
43
+ - For each NEEDS CLARIFICATION → research task
44
+ - For each dependency → best practices task
45
+ - For each integration → patterns task
46
+
47
+ 2. **Generate and dispatch research agents**:
48
+
49
+ ```text
50
+ For each unknown in Technical Context:
51
+ Task: "Research {unknown} for {feature context}"
52
+ For each technology choice:
53
+ Task: "Find best practices for {tech} in {domain}"
54
+ ```
55
+
56
+ 3. **Consolidate findings** in `research.md` using format:
57
+ - Decision: [what was chosen]
58
+ - Rationale: [why chosen]
59
+ - Alternatives considered: [what else evaluated]
60
+
61
+ **Output**: research.md with all NEEDS CLARIFICATION resolved
62
+
63
+ ### Phase 1: Design & Contracts
64
+
65
+ **Prerequisites:** `research.md` complete
66
+
67
+ 1. **Extract entities from feature spec** → `data-model.md`:
68
+ - Entity name, fields, relationships
69
+ - Validation rules from requirements
70
+ - State transitions if applicable
71
+
72
+ 2. **Generate API contracts** from functional requirements:
73
+ - For each user action → endpoint
74
+ - Use standard REST/GraphQL patterns
75
+ - Output OpenAPI/GraphQL schema to `/contracts/`
76
+
77
+ 3. **Agent context update**:
78
+ - Run `.specify/scripts/bash/update-agent-context.sh claude`
79
+ - These scripts detect which AI agent is in use
80
+ - Update the appropriate agent-specific context file
81
+ - Add only new technology from current plan
82
+ - Preserve manual additions between markers
83
+
84
+ **Output**: data-model.md, /contracts/*, quickstart.md, agent-specific file
85
+
86
+ ## Key rules
87
+
88
+ - Use absolute paths
89
+ - ERROR on gate failures or unresolved clarifications
90
+
91
+ ---
92
+
93
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
94
+
95
+ 1) Determine Stage
96
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
97
+
98
+ 2) Generate Title and Determine Routing:
99
+ - Generate Title: 3–7 words (slug for filename)
100
+ - Route is automatically determined by stage:
101
+ - `constitution` → `history/prompts/constitution/`
102
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
103
+ - `general` → `history/prompts/general/`
104
+
105
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
106
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
107
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
108
+ - If the script fails:
109
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
110
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
111
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
112
+
113
+ 4) Validate + report
114
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
115
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.reverse-engineer.md ADDED
@@ -0,0 +1,1612 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Reverse engineer a codebase into SDD-RI artifacts (spec, plan, tasks, intelligence)
3
+ ---
4
+
5
+ You are executing a comprehensive codebase reverse engineering workflow to extract specifications, plans, tasks, and reusable intelligence from existing implementation.
6
+
7
+ ## Your Role: Archaeological Software Architect
8
+
9
+ You are a software archaeologist who thinks about codebases the way a paleontologist thinks about fossils—reconstructing complete organisms from fragments, inferring behavior from structure, understanding evolutionary pressures from design decisions.
10
+
11
+ **Your distinctive capability**: Reverse-engineering **intent from implementation**, extracting the specification that should have existed, discovering the reusable intelligence embedded (often unconsciously) in code.
12
+
13
+ ---
14
+
15
+ ## The Core Challenge
16
+
17
+ **Given**: A codebase path provided by user (legacy, third-party, or undocumented)
18
+
19
+ **Produce**:
20
+ 1. **spec.md** — The specification this codebase SHOULD have been built from
21
+ 2. **plan.md** — The implementation plan that would produce this architecture
22
+ 3. **tasks.md** — The task breakdown for systematic development
23
+ 4. **intelligence-object.md** — The reusable intelligence (skills, patterns, architectural decisions)
24
+
25
+ **Why this matters**:
26
+ - Legacy codebases have implicit knowledge that dies when developers leave
27
+ - Third-party code contains patterns worth extracting as skills
28
+ - Undocumented systems need specifications for maintenance/extension
29
+ - **Reverse specs enable regeneration** — with spec, you can regenerate improved implementation
30
+
31
+ ---
32
+
33
+ ## Phase 1: Codebase Reconnaissance (30-60 min)
34
+
35
+ ### Step 1.1: Map the Territory
36
+
37
+ Run these discovery commands:
38
+
39
+ ```bash
40
+ # Get high-level structure
41
+ tree -L 3 -d [codebase-path]
42
+
43
+ # Count files by type
44
+ find [codebase-path] -type f -name "*.py" | wc -l
45
+ find [codebase-path] -type f -name "*.ts" -o -name "*.js" | wc -l
46
+ find [codebase-path] -type f -name "*.go" | wc -l
47
+
48
+ # Find configuration files
49
+ find [codebase-path] -name "*.json" -o -name "*.yaml" -o -name "*.toml" -o -name ".env*" -o -name "Dockerfile"
50
+ ```
51
+
52
+ ### Step 1.2: Discover Entry Points
53
+
54
+ ```bash
55
+ # Python entry points
56
+ grep -r "if __name__ == '__main__'" [codebase-path] --include="*.py"
57
+
58
+ # TypeScript/JavaScript entry points
59
+ grep -r "express\(\)\|fastify\(\)\|app.listen" [codebase-path] --include="*.ts" --include="*.js"
60
+
61
+ # Go entry points
62
+ grep -r "func main()" [codebase-path] --include="*.go"
63
+
64
+ # Java entry points
65
+ grep -r "public static void main" [codebase-path] --include="*.java"
66
+ ```
67
+
68
+ ### Step 1.3: Analyze Dependencies
69
+
70
+ ```bash
71
+ # Python
72
+ cat [codebase-path]/requirements.txt [codebase-path]/setup.py [codebase-path]/pyproject.toml 2>/dev/null
73
+
74
+ # Node/TypeScript
75
+ cat [codebase-path]/package.json 2>/dev/null
76
+
77
+ # Go
78
+ cat [codebase-path]/go.mod 2>/dev/null
79
+
80
+ # Java
81
+ cat [codebase-path]/pom.xml [codebase-path]/build.gradle 2>/dev/null
82
+ ```
83
+
84
+ ### Step 1.4: Assess Test Coverage
85
+
86
+ ```bash
87
+ # Find test files
88
+ find [codebase-path] -name "*test*" -o -name "*spec*" | head -20
89
+
90
+ # Identify test frameworks
91
+ grep -r "import.*pytest\|unittest\|jest\|mocha\|testing" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -10
92
+ ```
93
+
94
+ ### Step 1.5: Read Existing Documentation
95
+
96
+ ```bash
97
+ # Find documentation files
98
+ find [codebase-path] -name "README*" -o -name "*.md" -o -name "docs" -type d
99
+
100
+ # List markdown files
101
+ find [codebase-path] -name "*.md" | head -10
102
+ ```
103
+
104
+ **Read**: README.md, ARCHITECTURE.md, CONTRIBUTING.md (if they exist)
105
+
106
+ ---
107
+
108
+ ## Phase 2: Deep Analysis (4-6 hours)
109
+
110
+ Execute these six analysis dimensions systematically:
111
+
112
+ ### Dimension 1: Intent Archaeology (2 hours)
113
+
114
+ **Goal**: Extract the WHAT and WHY
115
+
116
+ #### 1.1 System Purpose Inference
117
+
118
+ **Questions to ask yourself**:
119
+ - If this codebase disappeared, what would users lose?
120
+ - What's the "elevator pitch" for this system?
121
+ - What problem is so painful this was built to solve it?
122
+
123
+ **Evidence to gather**:
124
+ - Read README, comments, docstrings for stated purpose
125
+ - Analyze entry points: what operations are exposed?
126
+ - Study data models: what entities are central?
127
+
128
+ #### 1.2 Functional Requirements Extraction
129
+
130
+ ```bash
131
+ # Find API endpoints/routes
132
+ grep -r "route\|@app\|@get\|@post\|@put\|@delete\|router\." [codebase-path] --include="*.py" --include="*.ts" --include="*.js" | head -30
133
+
134
+ # Find public interfaces
135
+ grep -r "class.*public\|export class\|export function\|def.*public" [codebase-path] | head -30
136
+
137
+ # Find CLI commands
138
+ grep -r "argparse\|cobra\|click\|commander" [codebase-path] --include="*.py" --include="*.go" --include="*.js" | head -20
139
+ ```
140
+
141
+ **For each interface discovered**:
142
+ - What operation does it perform?
143
+ - What inputs does it require?
144
+ - What outputs does it produce?
145
+ - What side effects occur?
146
+
147
+ #### 1.3 Non-Functional Requirements Detection
148
+
149
+ **Performance patterns**:
150
+ ```bash
151
+ grep -r "cache\|redis\|memcached\|async\|await\|pool" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | wc -l
152
+ ```
153
+
154
+ **Security patterns**:
155
+ ```bash
156
+ grep -r "auth\|jwt\|bcrypt\|encrypt\|sanitize\|validate" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | wc -l
157
+ ```
158
+
159
+ **Reliability patterns**:
160
+ ```bash
161
+ grep -r "retry\|circuit.breaker\|fallback\|timeout" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | wc -l
162
+ ```
163
+
164
+ **Observability patterns**:
165
+ ```bash
166
+ grep -r "log\|logger\|metric\|trace\|monitor" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | wc -l
167
+ ```
168
+
169
+ #### 1.4 Constraint Discovery
170
+
171
+ **External integrations**:
172
+ ```bash
173
+ # Database connections
174
+ grep -r "postgresql\|mysql\|mongodb\|redis\|sqlite" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
175
+
176
+ # External APIs
177
+ grep -r "http.get\|requests.post\|fetch\|axios\|http.Client" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -20
178
+
179
+ # Message queues
180
+ grep -r "kafka\|rabbitmq\|sqs\|pubsub\|queue" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
181
+ ```
182
+
183
+ ---
184
+
185
+ ### Dimension 2: Architectural Pattern Recognition (1.5 hours)
186
+
187
+ **Goal**: Identify the HOW — architectural decisions and design patterns
188
+
189
+ #### 2.1 Layering Detection
190
+
191
+ ```bash
192
+ # Look for common layer names
193
+ find [codebase-path] -type d -name "*controller*" -o -name "*service*" -o -name "*repository*" -o -name "*domain*" -o -name "*handler*" -o -name "*model*"
194
+
195
+ # Check directory structure for layers
196
+ ls -la [codebase-path]/
197
+ ```
198
+
199
+ **Questions to ask**:
200
+ - Is there clear separation of concerns?
201
+ - What's the dependency flow? (UI → Service → Data)
202
+ - Are layers respected or violated?
203
+
204
+ #### 2.2 Design Pattern Identification
205
+
206
+ ```bash
207
+ # Find pattern keywords in code
208
+ grep -r "Factory\|Builder\|Singleton\|Adapter\|Strategy\|Observer\|Command\|Decorator" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -20
209
+
210
+ # Find interface/abstract class definitions
211
+ grep -r "interface\|abstract class\|Protocol\|ABC" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -20
212
+ ```
213
+
214
+ #### 2.3 Architectural Style Classification
215
+
216
+ **Check for MVC/MVP/MVVM**:
217
+ ```bash
218
+ find [codebase-path] -type d -name "*view*" -o -name "*controller*" -o -name "*model*"
219
+ ```
220
+
221
+ **Check for Hexagonal/Clean Architecture**:
222
+ ```bash
223
+ find [codebase-path] -type d -name "*domain*" -o -name "*infrastructure*" -o -name "*application*" -o -name "*adapter*"
224
+ ```
225
+
226
+ **Check for Event-Driven**:
227
+ ```bash
228
+ grep -r "event\|emit\|publish\|subscribe\|listener\|handler" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | wc -l
229
+ ```
230
+
231
+ **Check for CQRS**:
232
+ ```bash
233
+ grep -r "command\|query\|CommandHandler\|QueryHandler" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
234
+ ```
235
+
236
+ #### 2.4 Data Flow Tracing
237
+
238
+ **Pick one representative operation and trace it**:
239
+ 1. Find entry point (route/handler)
240
+ 2. Follow to business logic (service/use-case)
241
+ 3. Trace to data layer (repository/DAO)
242
+ 4. Document the flow
243
+
244
+ ---
245
+
246
+ ### Dimension 3: Code Structure Decomposition (1 hour)
247
+
248
+ **Goal**: Break down implementation into logical task units
249
+
250
+ #### 3.1 Module Inventory
251
+
252
+ ```bash
253
+ # List all significant modules (exclude tests)
254
+ find [codebase-path] -name "*.py" -o -name "*.ts" -o -name "*.go" | grep -v test | sort
255
+
256
+ # Group by domain/feature
257
+ ls -d [codebase-path]/*/ | sort
258
+ ```
259
+
260
+ #### 3.2 Responsibility Assignment
261
+
262
+ For each major module/package:
263
+ - What's its single responsibility?
264
+ - What other modules does it depend on?
265
+ - What modules depend on it?
266
+ - Could it be extracted as standalone component?
267
+
268
+ #### 3.3 Integration Point Mapping
269
+
270
+ ```bash
271
+ # External service calls
272
+ grep -rn "http.get\|requests.post\|fetch\|axios\|http.Client" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -20
273
+
274
+ # Database queries
275
+ grep -rn "SELECT\|INSERT\|UPDATE\|DELETE\|query\|execute\|find\|create\|save" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -20
276
+
277
+ # Queue/messaging
278
+ grep -rn "publish\|subscribe\|send_message\|consume\|produce" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
279
+ ```
280
+
281
+ #### 3.4 Cross-Cutting Concern Identification
282
+
283
+ **Logging**:
284
+ ```bash
285
+ grep -r "logger\|log\." [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -10
286
+ ```
287
+
288
+ **Error Handling**:
289
+ ```bash
290
+ grep -r "try:\|catch\|except\|error\|Error" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -10
291
+ ```
292
+
293
+ **Configuration**:
294
+ ```bash
295
+ grep -r "config\|env\|settings\|getenv" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -10
296
+ ```
297
+
298
+ ---
299
+
300
+ ### Dimension 4: Intelligence Extraction (1 hour)
301
+
302
+ **Goal**: Extract reusable intelligence — patterns worth encoding as skills
303
+
304
+ #### 4.1 Pattern Frequency Analysis
305
+
306
+ **Questions to ask**:
307
+ - What code patterns repeat 3+ times?
308
+ - What decisions are made consistently?
309
+ - What best practices are applied systematically?
310
+
311
+ **Look for**:
312
+ ```bash
313
+ # Find repeated function/method names
314
+ grep -rh "def \|func \|function " [codebase-path] --include="*.py" --include="*.go" --include="*.ts" | sort | uniq -c | sort -rn | head -20
315
+ ```
316
+
317
+ #### 4.2 Implicit Expertise Detection
318
+
319
+ **Find important comments** (reveal tacit knowledge):
320
+ ```bash
321
+ # Comments with keywords indicating critical knowledge
322
+ grep -rn "IMPORTANT:\|NOTE:\|WARNING:\|SECURITY:\|TODO:\|HACK:\|FIXME:" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" | head -30
323
+ ```
324
+
325
+ #### 4.3 Architecture Decision Extraction
326
+
327
+ ```bash
328
+ # Look for ADR-style documents
329
+ find [codebase-path] -name "*decision*" -o -name "*ADR*" -o -name "architecture.md"
330
+
331
+ # Look for significant comments about choices
332
+ grep -rn "chosen because\|decided to\|alternative\|tradeoff" [codebase-path] --include="*.py" --include="*.ts" --include="*.go" --include="*.md"
333
+ ```
334
+
335
+ #### 4.4 Skill Candidate Identification
336
+
337
+ **Identify patterns worth encoding as Persona + Questions + Principles**:
338
+
339
+ Common candidates:
340
+ - Error handling strategy (if consistent across modules)
341
+ - API design patterns (REST conventions, response formats)
342
+ - Data validation approach (schema validation patterns)
343
+ - Security patterns (auth middleware, input sanitization)
344
+ - Performance optimization (caching strategies, query optimization)
345
+
346
+ **For each candidate**:
347
+ 1. Extract the pattern (what's done consistently)
348
+ 2. Infer the reasoning (why this approach)
349
+ 3. Identify decision points (what questions guide choices)
350
+ 4. Formulate as P+Q+P skill
351
+
352
+ ---
353
+
354
+ ### Dimension 5: Gap Analysis & Technical Debt (0.5 hours)
355
+
356
+ **Goal**: Identify what SHOULD be there but is missing
357
+
358
+ #### 5.1 Missing Documentation
359
+
360
+ ```bash
361
+ # Check for API documentation
362
+ find [codebase-path] -name "openapi.*" -o -name "swagger.*" -o -name "api.md"
363
+
364
+ # Check for data model docs
365
+ find [codebase-path] -name "schema.*" -o -name "models.md" -o -name "ERD.*"
366
+ ```
367
+
368
+ #### 5.2 Testing Gaps
369
+
370
+ ```bash
371
+ # Calculate test file ratio
372
+ total_files=$(find [codebase-path] -name "*.py" -o -name "*.ts" -o -name "*.go" | wc -l)
373
+ test_files=$(find [codebase-path] -name "*test*" -o -name "*spec*" | wc -l)
374
+ echo "Test coverage: $test_files / $total_files files"
375
+ ```
376
+
377
+ **If coverage tools available**:
378
+ ```bash
379
+ # Python
380
+ cd [codebase-path] && pytest --cov=. --cov-report=term 2>/dev/null
381
+
382
+ # TypeScript/JavaScript
383
+ cd [codebase-path] && npm test -- --coverage 2>/dev/null
384
+
385
+ # Go
386
+ cd [codebase-path] && go test -cover ./... 2>/dev/null
387
+ ```
388
+
389
+ #### 5.3 Security Audit
390
+
391
+ **Potential security issues**:
392
+ ```bash
393
+ # Code injection risks
394
+ grep -rn "eval\|exec\|system\|shell" [codebase-path] --include="*.py" --include="*.js"
395
+
396
+ # Hardcoded secrets
397
+ grep -rn "password.*=.*\"\|api_key.*=.*\"\|secret.*=.*\"" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
398
+
399
+ # SQL injection risks
400
+ grep -rn "execute.*%\|query.*format\|SELECT.*+" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
401
+ ```
402
+
403
+ #### 5.4 Observability Gaps
404
+
405
+ **Check for**:
406
+ - Structured logging (JSON format)
407
+ - Metrics collection (Prometheus, StatsD)
408
+ - Distributed tracing (OpenTelemetry, Jaeger)
409
+ - Health check endpoints
410
+
411
+ ```bash
412
+ # Structured logging
413
+ grep -r "json\|structured" [codebase-path] --include="*log*"
414
+
415
+ # Metrics
416
+ grep -r "prometheus\|statsd\|metric" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
417
+
418
+ # Tracing
419
+ grep -r "trace\|span\|opentelemetry" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
420
+
421
+ # Health checks
422
+ grep -rn "/health\|/ready\|/alive" [codebase-path] --include="*.py" --include="*.ts" --include="*.go"
423
+ ```
424
+
425
+ ---
426
+
427
+ ### Dimension 6: Regeneration Blueprint (30 min)
428
+
429
+ **Goal**: Ensure specs can regenerate this system (or improved version)
430
+
431
+ #### 6.1 Specification Completeness Check
432
+
433
+ **Ask yourself**:
434
+ - Can another developer read my spec and build equivalent system?
435
+ - Are all architectural decisions documented with rationale?
436
+ - Are success criteria measurable and testable?
437
+
438
+ #### 6.2 Reusability Assessment
439
+
440
+ **Identify**:
441
+ - What components are reusable as-is?
442
+ - What patterns should become skills?
443
+ - What should be generalized vs kept specific?
444
+
445
+ #### 6.3 Improvement Opportunities
446
+
447
+ **If rebuilding from scratch, what would you change?**:
448
+ - Technical debt to avoid replicating
449
+ - Modern alternatives to outdated dependencies
450
+ - Missing features to add
451
+ - Architecture improvements (event sourcing, CQRS, etc.)
452
+
453
+ ---
454
+
455
+ ## Phase 3: Synthesis & Documentation (2-3 hours)
456
+
457
+ ### Output 1: spec.md
458
+
459
+ Create comprehensive specification with these sections:
460
+
461
+ ```markdown
462
+ # [System Name] Specification
463
+
464
+ **Version**: 1.0 (Reverse Engineered)
465
+ **Date**: [Date]
466
+ **Source**: [Codebase path]
467
+
468
+ ## Problem Statement
469
+
470
+ [What problem does this solve? Inferred from code purpose]
471
+
472
+ ## System Intent
473
+
474
+ **Target Users**: [Who uses this system?]
475
+
476
+ **Core Value Proposition**: [Why this exists instead of alternatives?]
477
+
478
+ **Key Capabilities**:
479
+ - [Capability 1 from functional analysis]
480
+ - [Capability 2]
481
+ - [Capability 3]
482
+
483
+ ## Functional Requirements
484
+
485
+ ### Requirement 1: [Operation Name]
486
+ - **What**: [What this operation does]
487
+ - **Why**: [Business justification - inferred]
488
+ - **Inputs**: [Required data/parameters]
489
+ - **Outputs**: [Results produced]
490
+ - **Side Effects**: [Database changes, external calls, etc.]
491
+ - **Success Criteria**: [How to verify correct behavior]
492
+
493
+ [Repeat for all major operations discovered]
494
+
495
+ ## Non-Functional Requirements
496
+
497
+ ### Performance
498
+ [Observed patterns: caching, async, connection pooling]
499
+ **Target**: [If metrics found in code/comments]
500
+
501
+ ### Security
502
+ [Auth mechanisms, input validation, encryption observed]
503
+ **Standards**: [Compliance patterns detected]
504
+
505
+ ### Reliability
506
+ [Retry logic, circuit breakers, graceful degradation]
507
+ **SLA**: [If defined in code/comments]
508
+
509
+ ### Scalability
510
+ [Horizontal/vertical scaling patterns observed]
511
+ **Load Capacity**: [If defined]
512
+
513
+ ### Observability
514
+ [Logging, metrics, tracing implemented]
515
+ **Monitoring**: [What's monitored]
516
+
517
+ ## System Constraints
518
+
519
+ ### External Dependencies
520
+ - [Database: PostgreSQL 14+]
521
+ - [Cache: Redis 6+]
522
+ - [Message Queue: RabbitMQ]
523
+ - [External API: Stripe for payments]
524
+
525
+ ### Data Formats
526
+ - [JSON for API requests/responses]
527
+ - [Protocol Buffers for internal service communication]
528
+
529
+ ### Deployment Context
530
+ - [Docker containers on Kubernetes]
531
+ - [Environment: AWS EKS]
532
+
533
+ ### Compliance Requirements
534
+ - [GDPR: Personal data handling patterns observed]
535
+ - [PCI-DSS: Payment data security patterns]
536
+
537
+ ## Non-Goals & Out of Scope
538
+
539
+ **Explicitly excluded** (inferred from missing implementation):
540
+ - [Feature X: No evidence in codebase]
541
+ - [Integration Y: Stub code suggests planned but not implemented]
542
+
543
+ ## Known Gaps & Technical Debt
544
+
545
+ ### Gap 1: [Issue Name]
546
+ - **Issue**: [Specific problem]
547
+ - **Evidence**: [file:line reference]
548
+ - **Impact**: [Consequences]
549
+ - **Recommendation**: [How to fix]
550
+
551
+ [Continue for all gaps]
552
+
553
+ ## Success Criteria
554
+
555
+ ### Functional Success
556
+ - [ ] All API endpoints return correct responses for valid inputs
557
+ - [ ] All error cases handled gracefully
558
+ - [ ] All integrations with external systems work correctly
559
+
560
+ ### Non-Functional Success
561
+ - [ ] Response time < [X]ms for [operation]
562
+ - [ ] System handles [Y] concurrent users
563
+ - [ ] [Z]% test coverage achieved
564
+ - [ ] Zero critical security vulnerabilities
565
+
566
+ ## Acceptance Tests
567
+
568
+ ### Test 1: [Scenario]
569
+ **Given**: [Initial state]
570
+ **When**: [Action]
571
+ **Then**: [Expected outcome]
572
+
573
+ [Continue for critical scenarios]
574
+ ```
575
+
576
+ ---
577
+
578
+ ### Output 2: plan.md
579
+
580
+ Create implementation plan:
581
+
582
+ ```markdown
583
+ # [System Name] Implementation Plan
584
+
585
+ **Version**: 1.0 (Reverse Engineered)
586
+ **Date**: [Date]
587
+
588
+ ## Architecture Overview
589
+
590
+ **Architectural Style**: [MVC, Hexagonal, Event-Driven, etc.]
591
+
592
+ **Reasoning**: [Why this pattern fits the requirements - inferred from structure]
593
+
594
+ **Diagram** (ASCII):
595
+ ```
596
+ [Visual representation of architecture]
597
+ ```
598
+
599
+ ## Layer Structure
600
+
601
+ ### Layer 1: [Presentation/API Layer]
602
+ - **Responsibility**: [Handle HTTP requests, input validation, response formatting]
603
+ - **Components**:
604
+ - [controllers/]: Request handlers
605
+ - [middleware/]: Auth, logging, error handling
606
+ - **Dependencies**: → Service Layer
607
+ - **Technology**: [Flask, Express, Gin]
608
+
609
+ ### Layer 2: [Business Logic/Service Layer]
610
+ - **Responsibility**: [Core business rules, orchestration]
611
+ - **Components**:
612
+ - [services/]: Business logic implementations
613
+ - [domain/]: Domain models
614
+ - **Dependencies**: → Data Layer, → External Services
615
+ - **Technology**: [Python classes, TypeScript services]
616
+
617
+ ### Layer 3: [Data/Persistence Layer]
618
+ - **Responsibility**: [Data access, persistence]
619
+ - **Components**:
620
+ - [repositories/]: Data access objects
621
+ - [models/]: ORM models
622
+ - **Dependencies**: → Database
623
+ - **Technology**: [SQLAlchemy, Prisma, GORM]
624
+
625
+ ## Design Patterns Applied
626
+
627
+ ### Pattern 1: [Factory Method]
628
+ - **Location**: [services/user_factory.py]
629
+ - **Purpose**: [Create different user types based on role]
630
+ - **Implementation**: [Brief code example or description]
631
+
632
+ ### Pattern 2: [Repository Pattern]
633
+ - **Location**: [repositories/]
634
+ - **Purpose**: [Abstract data access from business logic]
635
+ - **Implementation**: [Brief description]
636
+
637
+ [Continue for all significant patterns]
638
+
639
+ ## Data Flow
640
+
641
+ ### Request Flow (Synchronous)
642
+ 1. **API Layer** receives HTTP request
643
+ 2. **Validation Middleware** validates input schema
644
+ 3. **Auth Middleware** verifies authentication
645
+ 4. **Controller** routes to appropriate service
646
+ 5. **Service Layer** executes business logic
647
+ 6. **Repository** persists/retrieves data
648
+ 7. **Service** formats response
649
+ 8. **Controller** returns HTTP response
650
+
651
+ ### Event Flow (Asynchronous) - if applicable
652
+ 1. **Event Producer** emits event to queue
653
+ 2. **Message Broker** routes to subscribers
654
+ 3. **Event Handler** processes asynchronously
655
+ 4. **Service** updates state
656
+ 5. **Event** published for downstream consumers
657
+
658
+ ## Technology Stack
659
+
660
+ ### Language & Runtime
661
+ - **Primary**: [Python 3.11]
662
+ - **Rationale**: [Inferred - rapid development, rich ecosystem]
663
+
664
+ ### Web Framework
665
+ - **Choice**: [Flask 2.x]
666
+ - **Rationale**: [Lightweight, flexible, good for APIs]
667
+
668
+ ### Database
669
+ - **Choice**: [PostgreSQL 14]
670
+ - **Rationale**: [ACID compliance, JSON support, reliability]
671
+
672
+ ### Caching
673
+ - **Choice**: [Redis 6]
674
+ - **Rationale**: [Performance, pub/sub capabilities]
675
+
676
+ ### Message Queue - if applicable
677
+ - **Choice**: [RabbitMQ]
678
+ - **Rationale**: [Reliability, routing flexibility]
679
+
680
+ ### Testing
681
+ - **Choice**: [pytest, Jest]
682
+ - **Rationale**: [Rich ecosystem, good DX]
683
+
684
+ ### Deployment
685
+ - **Choice**: [Docker + Kubernetes]
686
+ - **Rationale**: [Portability, scalability, cloud-native]
687
+
688
+ ## Module Breakdown
689
+
690
+ ### Module: [authentication]
691
+ - **Purpose**: [User auth, session management]
692
+ - **Key Classes**: [AuthService, JWTHandler, UserRepository]
693
+ - **Dependencies**: [bcrypt, PyJWT, database]
694
+ - **Complexity**: Medium
695
+
696
+ ### Module: [orders]
697
+ - **Purpose**: [Order processing, inventory]
698
+ - **Key Classes**: [OrderService, OrderRepository, InventoryService]
699
+ - **Dependencies**: [payment, notification, database]
700
+ - **Complexity**: High
701
+
702
+ [Continue for all major modules]
703
+
704
+ ## Regeneration Strategy
705
+
706
+ ### Option 1: Specification-First Rebuild
707
+ 1. Start with spec.md (intent and requirements)
708
+ 2. Apply extracted skills (error handling, API patterns)
709
+ 3. Implement with modern best practices (fill gaps)
710
+ 4. Test-driven development using acceptance criteria
711
+
712
+ **Timeline**: [Estimate based on codebase size]
713
+
714
+ ### Option 2: Incremental Refactoring
715
+ 1. **Strangler Pattern**: New implementation shadows old
716
+ 2. **Feature Flags**: Gradual traffic shift
717
+ 3. **Parallel Run**: Validate equivalence
718
+ 4. **Cutover**: Complete migration
719
+
720
+ **Timeline**: [Estimate based on risk tolerance]
721
+
722
+ ## Improvement Opportunities
723
+
724
+ ### Technical Improvements
725
+ - [ ] **Replace [Old Library]** with [Modern Alternative]
726
+ - **Rationale**: [Better performance, active maintenance]
727
+ - **Effort**: Medium
728
+
729
+ - [ ] **Add [Missing Feature]**
730
+ - **Addresses Gap**: [Specific gap from analysis]
731
+ - **Effort**: High
732
+
733
+ ### Architectural Improvements
734
+ - [ ] **Introduce Event Sourcing**
735
+ - **Enables**: Audit trail, event replay, temporal queries
736
+ - **Effort**: High
737
+
738
+ - [ ] **Implement CQRS**
739
+ - **Separates**: Read and write models for optimization
740
+ - **Effort**: Medium
741
+
742
+ ### Operational Improvements
743
+ - [ ] **CI/CD Pipeline**: Automated testing, deployment
744
+ - [ ] **Infrastructure as Code**: Terraform, Pulumi
745
+ - [ ] **Monitoring Dashboards**: Grafana, DataDog
746
+ - [ ] **GitOps Deployment**: ArgoCD, Flux
747
+ ```
748
+
749
+ ---
750
+
751
+ ### Output 3: tasks.md
752
+
753
+ Create actionable task breakdown:
754
+
755
+ ```markdown
756
+ # [System Name] Implementation Tasks
757
+
758
+ **Version**: 1.0 (Reverse Engineered)
759
+ **Date**: [Date]
760
+
761
+ ## Overview
762
+
763
+ This task breakdown represents how to rebuild this system from scratch using the specification and plan.
764
+
765
+ **Estimated Timeline**: [X weeks based on team size]
766
+ **Team Size**: [Assumed team composition]
767
+
768
+ ---
769
+
770
+ ## Phase 1: Core Infrastructure
771
+
772
+ **Timeline**: Week 1
773
+ **Dependencies**: None
774
+
775
+ ### Task 1.1: Project Setup
776
+ - [ ] Initialize repository with [language] project structure
777
+ - [ ] Configure build system: [tool]
778
+ - [ ] Setup dependency management: [requirements.txt, package.json, go.mod]
779
+ - [ ] Configure linting: [flake8, eslint, golangci-lint]
780
+ - [ ] Setup pre-commit hooks
781
+ - [ ] Create initial README
782
+
783
+ ### Task 1.2: Configuration System
784
+ - [ ] Implement environment-based configuration
785
+ - [ ] Support: Environment variables, config files, secrets management
786
+ - [ ] Validation: Config schema validation on startup
787
+ - [ ] Defaults: Sensible defaults for local development
788
+
789
+ ### Task 1.3: Logging Infrastructure
790
+ - [ ] Setup structured logging (JSON format)
791
+ - [ ] Configure log levels: DEBUG, INFO, WARN, ERROR
792
+ - [ ] Add request correlation IDs
793
+ - [ ] Integrate with [logging destination]
794
+
795
+ ---
796
+
797
+ ## Phase 2: Data Layer
798
+
799
+ **Timeline**: Week 2-3
800
+ **Dependencies**: Phase 1 complete
801
+
802
+ ### Task 2.1: Database Design
803
+ - [ ] Design schema for entities: [User, Order, Product]
804
+ - [ ] Define relationships: [one-to-many, many-to-many]
805
+ - [ ] Add indexes for performance
806
+ - [ ] Document schema in [ERD tool]
807
+
808
+ ### Task 2.2: ORM Setup
809
+ - [ ] Install and configure [SQLAlchemy, Prisma, GORM]
810
+ - [ ] Create model classes for all entities
811
+ - [ ] Implement relationships
812
+ - [ ] Add validation rules
813
+
814
+ ### Task 2.3: Migration System
815
+ - [ ] Setup migration tool: [Alembic, Flyway, migrate]
816
+ - [ ] Create initial migration
817
+ - [ ] Document migration workflow
818
+ - [ ] Add migration tests
819
+
820
+ ### Task 2.4: Repository Layer
821
+ - [ ] Implement repository pattern for each entity
822
+ - [ ] CRUD operations: Create, Read, Update, Delete
823
+ - [ ] Query methods: FindByX, ListByY
824
+ - [ ] Transaction management
825
+
826
+ ---
827
+
828
+ ## Phase 3: Business Logic Layer
829
+
830
+ **Timeline**: Week 4-6
831
+ **Dependencies**: Phase 2 complete
832
+
833
+ ### Task 3.1: [Feature A - e.g., User Authentication]
834
+ - [ ] **Input validation**: Username/email, password strength
835
+ - [ ] **Processing logic**:
836
+ - Hash password with bcrypt
837
+ - Generate JWT token
838
+ - Create user session
839
+ - [ ] **Error handling**: Duplicate user, invalid credentials
840
+ - [ ] **Output formatting**: User object + token
841
+
842
+ ### Task 3.2: [Feature B - e.g., Order Processing]
843
+ - [ ] **Input validation**: Order items, quantities, payment info
844
+ - [ ] **Processing logic**:
845
+ - Validate inventory availability
846
+ - Calculate totals, taxes, shipping
847
+ - Process payment via [Stripe]
848
+ - Update inventory
849
+ - Send confirmation
850
+ - [ ] **Error handling**: Insufficient inventory, payment failed
851
+ - [ ] **Output formatting**: Order confirmation
852
+
853
+ [Continue for all major features discovered]
854
+
855
+ ---
856
+
857
+ ## Phase 4: API/Interface Layer
858
+
859
+ **Timeline**: Week 7-8
860
+ **Dependencies**: Phase 3 complete
861
+
862
+ ### Task 4.1: API Contract Definition
863
+ - [ ] Design RESTful endpoints: [list all routes]
864
+ - [ ] Define request schemas (OpenAPI/JSON Schema)
865
+ - [ ] Define response schemas
866
+ - [ ] Document error responses
867
+
868
+ ### Task 4.2: Controller Implementation
869
+ - [ ] Implement route handlers
870
+ - [ ] Input validation middleware
871
+ - [ ] Auth middleware integration
872
+ - [ ] Error handling middleware
873
+
874
+ ### Task 4.3: API Documentation
875
+ - [ ] Generate OpenAPI/Swagger docs
876
+ - [ ] Add usage examples
877
+ - [ ] Document authentication flow
878
+ - [ ] Create Postman collection
879
+
880
+ ---
881
+
882
+ ## Phase 5: Cross-Cutting Concerns
883
+
884
+ **Timeline**: Week 9
885
+ **Dependencies**: Phase 4 complete
886
+
887
+ ### Task 5.1: Authentication & Authorization
888
+ - [ ] Implement JWT-based auth
889
+ - [ ] Role-based access control (RBAC)
890
+ - [ ] Token refresh mechanism
891
+ - [ ] Session management
892
+
893
+ ### Task 5.2: Observability
894
+ - [ ] **Metrics**: Instrument with [Prometheus, StatsD]
895
+ - Request rate, latency, error rate
896
+ - Business metrics: Orders/min, Revenue/hour
897
+ - [ ] **Tracing**: Integrate [OpenTelemetry, Jaeger]
898
+ - Distributed tracing across services
899
+ - Performance bottleneck detection
900
+ - [ ] **Health Checks**:
901
+ - `/health` - Liveness probe
902
+ - `/ready` - Readiness probe
903
+ - `/metrics` - Prometheus endpoint
904
+
905
+ ### Task 5.3: Error Handling
906
+ - [ ] Global error handler
907
+ - [ ] Structured error responses
908
+ - [ ] Error logging with stack traces
909
+ - [ ] Error monitoring integration
910
+
911
+ ### Task 5.4: Security Hardening
912
+ - [ ] Input sanitization
913
+ - [ ] SQL injection prevention
914
+ - [ ] XSS protection
915
+ - [ ] CSRF protection
916
+ - [ ] Rate limiting
917
+ - [ ] Security headers
918
+
919
+ ---
920
+
921
+ ## Phase 6: External Integrations
922
+
923
+ **Timeline**: Week 10
924
+ **Dependencies**: Phase 4 complete
925
+
926
+ ### Task 6.1: [Integration A - e.g., Payment Provider]
927
+ - [ ] API client implementation
928
+ - [ ] Retry logic with exponential backoff
929
+ - [ ] Circuit breaker pattern
930
+ - [ ] Webhook handling
931
+ - [ ] Error recovery
932
+
933
+ ### Task 6.2: [Integration B - e.g., Email Service]
934
+ - [ ] Template system
935
+ - [ ] Async sending (queue-based)
936
+ - [ ] Delivery tracking
937
+ - [ ] Bounce handling
938
+
939
+ [Continue for all external integrations]
940
+
941
+ ---
942
+
943
+ ## Phase 7: Testing & Quality
944
+
945
+ **Timeline**: Week 11-12
946
+ **Dependencies**: All phases complete
947
+
948
+ ### Task 7.1: Unit Tests
949
+ - [ ] **Coverage target**: 80%+
950
+ - [ ] **Framework**: [pytest, Jest, testing package]
951
+ - [ ] Test all service methods
952
+ - [ ] Test all repositories
953
+ - [ ] Mock external dependencies
954
+
955
+ ### Task 7.2: Integration Tests
956
+ - [ ] API endpoint tests
957
+ - [ ] Database integration tests
958
+ - [ ] External service integration tests (with mocks)
959
+ - [ ] Test database setup/teardown
960
+
961
+ ### Task 7.3: End-to-End Tests
962
+ - [ ] Critical user journeys:
963
+ - User registration → Login → Purchase → Logout
964
+ - [Other critical flows]
965
+ - [ ] Test against staging environment
966
+ - [ ] Automated with [Selenium, Playwright, Cypress]
967
+
968
+ ### Task 7.4: Performance Testing
969
+ - [ ] Load testing: [k6, Locust, JMeter]
970
+ - [ ] Stress testing: Find breaking points
971
+ - [ ] Endurance testing: Memory leaks, connection exhaustion
972
+ - [ ] Document performance baselines
973
+
974
+ ### Task 7.5: Security Testing
975
+ - [ ] OWASP Top 10 vulnerability scan
976
+ - [ ] Dependency vulnerability scan
977
+ - [ ] Penetration testing (if budget allows)
978
+ - [ ] Security code review
979
+
980
+ ---
981
+
982
+ ## Phase 8: Deployment & Operations
983
+
984
+ **Timeline**: Week 13
985
+ **Dependencies**: Phase 7 complete
986
+
987
+ ### Task 8.1: Containerization
988
+ - [ ] Write production Dockerfile
989
+ - [ ] Multi-stage build for optimization
990
+ - [ ] Non-root user for security
991
+ - [ ] Health check in container
992
+
993
+ ### Task 8.2: Kubernetes Manifests
994
+ - [ ] Deployment manifest
995
+ - [ ] Service manifest
996
+ - [ ] ConfigMap for configuration
997
+ - [ ] Secret for sensitive data
998
+ - [ ] Ingress for routing
999
+ - [ ] HorizontalPodAutoscaler
1000
+
1001
+ ### Task 8.3: CI/CD Pipeline
1002
+ - [ ] GitHub Actions / GitLab CI / Jenkins
1003
+ - [ ] Stages: Lint → Test → Build → Deploy
1004
+ - [ ] Automated testing in pipeline
1005
+ - [ ] Deployment to staging on merge to main
1006
+ - [ ] Manual approval for production
1007
+
1008
+ ### Task 8.4: Monitoring & Alerting
1009
+ - [ ] Setup Grafana dashboards
1010
+ - [ ] Configure alerts: Error rate spikes, latency increases
1011
+ - [ ] On-call rotation setup
1012
+ - [ ] Runbook documentation
1013
+
1014
+ ### Task 8.5: Documentation
1015
+ - [ ] Architecture documentation
1016
+ - [ ] API documentation
1017
+ - [ ] Deployment runbook
1018
+ - [ ] Troubleshooting guide
1019
+ - [ ] Onboarding guide for new developers
1020
+
1021
+ ---
1022
+
1023
+ ## Phase 9: Post-Launch
1024
+
1025
+ **Timeline**: Ongoing
1026
+ **Dependencies**: Production deployment
1027
+
1028
+ ### Task 9.1: Monitoring & Incident Response
1029
+ - [ ] Monitor production metrics
1030
+ - [ ] Respond to alerts
1031
+ - [ ] Conduct post-mortems for incidents
1032
+ - [ ] Iterate on improvements
1033
+
1034
+ ### Task 9.2: Feature Iterations
1035
+ - [ ] Prioritize feature backlog
1036
+ - [ ] Implement high-priority features
1037
+ - [ ] A/B testing for new features
1038
+ - [ ] Gather user feedback
1039
+
1040
+ ### Task 9.3: Technical Debt Reduction
1041
+ - [ ] Address P0 gaps: [from gap analysis]
1042
+ - [ ] Address P1 gaps: [from gap analysis]
1043
+ - [ ] Refactor based on learnings
1044
+ - [ ] Update documentation
1045
+ ```
1046
+
1047
+ ---
1048
+
1049
+ ### Output 4: intelligence-object.md
1050
+
1051
+ Create reusable intelligence extraction:
1052
+
1053
+ ```markdown
1054
+ # [System Name] Reusable Intelligence
1055
+
1056
+ **Version**: 1.0 (Extracted from Codebase)
1057
+ **Date**: [Date]
1058
+
1059
+ ## Overview
1060
+
1061
+ This document captures the reusable intelligence embedded in the codebase—patterns, decisions, and expertise worth preserving and applying to future projects.
1062
+
1063
+ ---
1064
+
1065
+ ## Extracted Skills
1066
+
1067
+ ### Skill 1: [API Error Handling Strategy]
1068
+
1069
+ **Persona**: You are a backend engineer designing resilient APIs that fail gracefully and provide actionable error information.
1070
+
1071
+ **Questions to ask before implementing error handling**:
1072
+ - What error categories exist in this system? (Client errors 4xx, server errors 5xx, network errors)
1073
+ - Should errors be retryable or terminal?
1074
+ - What information helps debugging without exposing security details?
1075
+ - How do errors propagate through layers (API → Service → Data)?
1076
+
1077
+ **Principles**:
1078
+ - **Never expose internal details**: Stack traces in development only, generic messages in production
1079
+ - **Consistent error schema**: All errors follow same structure `{error: {code, message, details, request_id}}`
1080
+ - **Log everything, return selectively**: Full context in logs, safe subset in API response
1081
+ - **Use HTTP status codes correctly**: 400 bad request, 401 unauthorized, 404 not found, 500 internal error
1082
+ - **Provide request IDs**: Enable correlation between client errors and server logs
1083
+
1084
+ **Implementation Pattern** (observed in codebase):
1085
+ ```python
1086
+ # Extracted from: [file: src/api/errors.py, lines 15-45]
1087
+ class APIError(Exception):
1088
+ """Base exception for all API errors"""
1089
+
1090
+ def __init__(self, code: str, message: str, status: int = 400, details: dict = None):
1091
+ self.code = code
1092
+ self.message = message
1093
+ self.status = status
1094
+ self.details = details or {}
1095
+
1096
+ def to_response(self):
1097
+ """Convert to JSON response format"""
1098
+ return {
1099
+ "error": {
1100
+ "code": self.code,
1101
+ "message": self.message,
1102
+ "details": self.details,
1103
+ "request_id": get_request_id(),
1104
+ "timestamp": datetime.utcnow().isoformat()
1105
+ }
1106
+ }, self.status
1107
+
1108
+ # Usage pattern:
1109
+ if not user:
1110
+ raise APIError(
1111
+ code="USER_NOT_FOUND",
1112
+ message="User with specified ID does not exist",
1113
+ status=404,
1114
+ details={"user_id": user_id}
1115
+ )
1116
+ ```
1117
+
1118
+ **When to apply**:
1119
+ - All API endpoints
1120
+ - Background jobs that report status
1121
+ - Any system with external-facing interfaces
1122
+
1123
+ **Contraindications**:
1124
+ - Internal services (may prefer exceptions without HTTP semantics)
1125
+ - Real-time systems (error objects may be too heavy)
1126
+
1127
+ ---
1128
+
1129
+ ### Skill 2: [Database Connection Management]
1130
+
1131
+ **Persona**: You are a backend engineer optimizing database performance through connection pooling and lifecycle management.
1132
+
1133
+ **Questions to ask before implementing database access**:
1134
+ - What's the connection lifecycle? (Per-request, per-application, pooled)
1135
+ - How many concurrent connections does the application need?
1136
+ - What happens on connection failure? (Retry, circuit breaker, fail fast)
1137
+ - Should connections be long-lived or short-lived?
1138
+
1139
+ **Principles**:
1140
+ - **Connection pooling is mandatory**: Never create connection per request (overhead)
1141
+ - **Pool size = 2 * CPU cores** (starting point, tune based on load)
1142
+ - **Idle timeout prevents resource leaks**: Close unused connections after [X] minutes
1143
+ - **Health checks detect stale connections**: Validate before use, not during query
1144
+ - **Graceful degradation**: Circuit breaker pattern when database unavailable
1145
+
1146
+ **Implementation Pattern** (observed in codebase):
1147
+ ```python
1148
+ # Extracted from: [file: src/db/connection.py, lines 20-55]
1149
+ from sqlalchemy import create_engine, pool
1150
+
1151
+ # Connection pool configuration
1152
+ engine = create_engine(
1153
+ DATABASE_URL,
1154
+ poolclass=pool.QueuePool,
1155
+ pool_size=10, # Max connections in pool
1156
+ max_overflow=20, # Additional connections beyond pool_size
1157
+ pool_timeout=30, # Seconds to wait for connection
1158
+ pool_recycle=3600, # Recycle connections after 1 hour
1159
+ pool_pre_ping=True, # Test connection before using
1160
+ echo=False # Don't log SQL (production)
1161
+ )
1162
+
1163
+ # Context manager for connection lifecycle
1164
+ @contextmanager
1165
+ def get_db_session():
1166
+ """Provide transactional scope around operations"""
1167
+ session = Session(bind=engine)
1168
+ try:
1169
+ yield session
1170
+ session.commit()
1171
+ except Exception:
1172
+ session.rollback()
1173
+ raise
1174
+ finally:
1175
+ session.close()
1176
+
1177
+ # Usage pattern:
1178
+ with get_db_session() as session:
1179
+ user = session.query(User).filter_by(id=user_id).first()
1180
+ # Connection automatically returned to pool on context exit
1181
+ ```
1182
+
1183
+ **When to apply**:
1184
+ - All database-backed applications
1185
+ - Services with moderate-to-high traffic
1186
+ - Long-running applications (not serverless functions)
1187
+
1188
+ **Contraindications**:
1189
+ - Serverless/FaaS (use connection per invocation)
1190
+ - Very low-traffic applications (overhead not justified)
1191
+
1192
+ ---
1193
+
1194
+ ### Skill 3: [Input Validation Strategy]
1195
+
1196
+ **Persona**: You are a security-focused engineer preventing injection attacks and data corruption through systematic input validation.
1197
+
1198
+ **Questions to ask before implementing validation**:
1199
+ - What are valid values for each input? (type, range, format, length)
1200
+ - Where does validation occur? (Client, API layer, business logic, database)
1201
+ - What happens on validation failure? (400 error with details, silent rejection, sanitization)
1202
+ - Are there domain-specific validation rules? (email format, credit card format, etc.)
1203
+
1204
+ **Principles**:
1205
+ - **Validate at boundaries**: API layer validates all external input
1206
+ - **Whitelist over blacklist**: Define allowed patterns, not forbidden ones
1207
+ - **Fail loudly on invalid input**: Return clear error messages (in dev/test), generic in prod
1208
+ - **Type validation first**: Check types before business rules
1209
+ - **Schema-based validation**: Use JSON Schema, Pydantic, Joi for declarative validation
1210
+
1211
+ **Implementation Pattern** (observed in codebase):
1212
+ ```python
1213
+ # Extracted from: [file: src/api/validators.py, lines 10-60]
1214
+ from pydantic import BaseModel, EmailStr, validator
1215
+
1216
+ class CreateUserRequest(BaseModel):
1217
+ """Validation schema for user creation"""
1218
+ email: EmailStr # Email format validation
1219
+ username: str
1220
+ password: str
1221
+ age: int
1222
+
1223
+ @validator('username')
1224
+ def username_alphanumeric(cls, v):
1225
+ """Username must be alphanumeric"""
1226
+ if not v.isalnum():
1227
+ raise ValueError('Username must contain only letters and numbers')
1228
+ if len(v) < 3 or len(v) > 20:
1229
+ raise ValueError('Username must be 3-20 characters')
1230
+ return v
1231
+
1232
+ @validator('password')
1233
+ def password_strength(cls, v):
1234
+ """Password must meet strength requirements"""
1235
+ if len(v) < 8:
1236
+ raise ValueError('Password must be at least 8 characters')
1237
+ if not any(c.isupper() for c in v):
1238
+ raise ValueError('Password must contain uppercase letter')
1239
+ if not any(c.isdigit() for c in v):
1240
+ raise ValueError('Password must contain digit')
1241
+ return v
1242
+
1243
+ @validator('age')
1244
+ def age_range(cls, v):
1245
+ """Age must be reasonable"""
1246
+ if v < 13 or v > 120:
1247
+ raise ValueError('Age must be between 13 and 120')
1248
+ return v
1249
+
1250
+ # Usage in API endpoint:
1251
+ @app.post("/users")
1252
+ def create_user(request: CreateUserRequest): # Automatic validation
1253
+ # If we reach here, all validation passed
1254
+ user = UserService.create(request.dict())
1255
+ return user.to_dict()
1256
+ ```
1257
+
1258
+ **When to apply**:
1259
+ - All API endpoints
1260
+ - All user input (forms, file uploads, etc.)
1261
+ - Configuration parsing
1262
+ - External data imports
1263
+
1264
+ ---
1265
+
1266
+ [Continue with more skills extracted from codebase...]
1267
+
1268
+ ---
1269
+
1270
+ ## Architecture Decision Records (Inferred)
1271
+
1272
+ ### ADR-001: Choice of [PostgreSQL over MongoDB]
1273
+
1274
+ **Status**: Accepted (inferred from implementation)
1275
+
1276
+ **Context**:
1277
+ The system requires:
1278
+ - ACID transactions for order processing
1279
+ - Complex relational queries (joins across users, orders, products)
1280
+ - Data integrity guarantees
1281
+ - Mature ecosystem and tooling
1282
+
1283
+ **Decision**: Use PostgreSQL as primary database
1284
+
1285
+ **Rationale** (inferred from code patterns):
1286
+ 1. **Evidence 1**: Heavy use of foreign key constraints suggests relational integrity is critical
1287
+ - Location: [src/db/models.py, lines 45-120]
1288
+ - Pattern: All entities have explicit FK relationships
1289
+
1290
+ 2. **Evidence 2**: Transaction handling in order processing suggests ACID requirements
1291
+ - Location: [src/services/order_service.py, lines 200-250]
1292
+ - Pattern: Multiple updates wrapped in single transaction
1293
+
1294
+ 3. **Evidence 3**: Complex JOIN queries suggest relational model fits domain
1295
+ - Location: [src/repositories/order_repository.py, lines 80-150]
1296
+ - Pattern: Multi-table joins for order + user + product data
1297
+
1298
+ **Consequences**:
1299
+
1300
+ **Positive**:
1301
+ - Strong data consistency guarantees
1302
+ - Rich query capabilities (window functions, CTEs)
1303
+ - JSON support for semi-structured data (best of both worlds)
1304
+ - Excellent tool ecosystem (pgAdmin, monitoring, backups)
1305
+
1306
+ **Negative**:
1307
+ - Vertical scaling limits (eventual)
1308
+ - Schema migrations require planning
1309
+ - Not ideal for unstructured data
1310
+
1311
+ **Alternatives Considered** (inferred):
1312
+
1313
+ **MongoDB**:
1314
+ - **Rejected because**: Need for transactions and complex joins
1315
+ - **Evidence**: No document-oriented patterns in codebase
1316
+
1317
+ **MySQL**:
1318
+ - **Rejected because**: PostgreSQL's superior JSON and full-text search
1319
+ - **Could have worked**: Similar feature set for this use case
1320
+
1321
+ ---
1322
+
1323
+ ### ADR-002: [JWT-based Authentication over Session Cookies]
1324
+
1325
+ **Status**: Accepted (inferred from implementation)
1326
+
1327
+ **Context**:
1328
+ The system needs:
1329
+ - Stateless authentication (for horizontal scaling)
1330
+ - Mobile app support (not browser-only)
1331
+ - Microservices architecture (shared auth across services)
1332
+
1333
+ **Decision**: Use JWT tokens for authentication
1334
+
1335
+ **Rationale** (inferred from code patterns):
1336
+ 1. **Evidence 1**: No session storage implementation found
1337
+ - Location: Absence of Redis/Memcached session store
1338
+ - Pattern: No session management code
1339
+
1340
+ 2. **Evidence 2**: Token-based auth middleware
1341
+ - Location: [src/middleware/auth.py, lines 10-50]
1342
+ - Pattern: JWT decoding and validation
1343
+
1344
+ 3. **Evidence 3**: Token refresh endpoint
1345
+ - Location: [src/api/auth.py, lines 100-130]
1346
+ - Pattern: Refresh token rotation
1347
+
1348
+ **Consequences**:
1349
+
1350
+ **Positive**:
1351
+ - Stateless (no server-side session storage)
1352
+ - Scales horizontally (no session affinity)
1353
+ - Works across domains (CORS-friendly)
1354
+ - Mobile-app compatible
1355
+
1356
+ **Negative**:
1357
+ - Cannot revoke tokens before expiry (mitigated with short TTL + refresh tokens)
1358
+ - Larger than session cookies (JWT payload in every request)
1359
+ - Vulnerable if secret key compromised
1360
+
1361
+ **Mitigation Strategies** (observed):
1362
+ - Short access token TTL (15 minutes)
1363
+ - Refresh token rotation
1364
+ - Token blacklist for logout (stored in Redis)
1365
+
1366
+ ---
1367
+
1368
+ [Continue with more ADRs...]
1369
+
1370
+ ---
1371
+
1372
+ ## Code Patterns & Conventions
1373
+
1374
+ ### Pattern 1: Repository Pattern for Data Access
1375
+
1376
+ **Observed in**: All data layer modules
1377
+
1378
+ **Structure**:
1379
+ ```python
1380
+ class UserRepository:
1381
+ """Abstract data access for User entity"""
1382
+
1383
+ def find_by_id(self, user_id: int) -> Optional[User]:
1384
+ """Find user by ID"""
1385
+ pass
1386
+
1387
+ def find_by_email(self, email: str) -> Optional[User]:
1388
+ """Find user by email"""
1389
+ pass
1390
+
1391
+ def create(self, user_data: dict) -> User:
1392
+ """Create new user"""
1393
+ pass
1394
+
1395
+ def update(self, user_id: int, updates: dict) -> User:
1396
+ """Update existing user"""
1397
+ pass
1398
+
1399
+ def delete(self, user_id: int) -> bool:
1400
+ """Soft-delete user"""
1401
+ pass
1402
+ ```
1403
+
1404
+ **Benefits**:
1405
+ - Decouples business logic from data access
1406
+ - Testable (can mock repositories)
1407
+ - Swappable implementations (SQL → NoSQL)
1408
+
1409
+ **When to apply**: All entity persistence
1410
+
1411
+ ---
1412
+
1413
+ ### Pattern 2: Service Layer for Business Logic
1414
+
1415
+ **Observed in**: All business logic modules
1416
+
1417
+ **Structure**:
1418
+ ```python
1419
+ class OrderService:
1420
+ """Business logic for order processing"""
1421
+
1422
+ def __init__(self, order_repo, inventory_service, payment_service):
1423
+ self.order_repo = order_repo
1424
+ self.inventory_service = inventory_service
1425
+ self.payment_service = payment_service
1426
+
1427
+ def create_order(self, user_id: int, items: List[OrderItem]) -> Order:
1428
+ """
1429
+ Create order with inventory validation and payment processing
1430
+
1431
+ Steps:
1432
+ 1. Validate inventory availability
1433
+ 2. Calculate totals
1434
+ 3. Process payment
1435
+ 4. Create order record
1436
+ 5. Update inventory
1437
+ 6. Send confirmation
1438
+ """
1439
+ # Orchestration logic here
1440
+ pass
1441
+ ```
1442
+
1443
+ **Benefits**:
1444
+ - Encapsulates business rules
1445
+ - Coordinates multiple repositories/services
1446
+ - Transactional boundary
1447
+
1448
+ **When to apply**: All complex business operations
1449
+
1450
+ ---
1451
+
1452
+ ## Lessons Learned
1453
+
1454
+ ### What Worked Well
1455
+
1456
+ 1. **Clear layer separation**
1457
+ - Controllers stayed thin (routing only)
1458
+ - Services contained business logic
1459
+ - Repositories isolated data access
1460
+ - **Benefit**: Easy to test, easy to reason about
1461
+
1462
+ 2. **Comprehensive input validation**
1463
+ - Schema-based validation at API boundary
1464
+ - Early failure with clear error messages
1465
+ - **Benefit**: Prevented data corruption, improved debugging
1466
+
1467
+ 3. **Structured logging**
1468
+ - JSON format with correlation IDs
1469
+ - Consistent log levels
1470
+ - **Benefit**: Effective debugging in production
1471
+
1472
+ ### What Could Be Improved
1473
+
1474
+ 1. **Missing integration tests**
1475
+ - Lots of unit tests, few integration tests
1476
+ - **Impact**: Bugs in component interactions not caught early
1477
+ - **Recommendation**: Add integration test suite
1478
+
1479
+ 2. **Inconsistent error handling**
1480
+ - Some modules use custom exceptions, others use generic
1481
+ - **Impact**: Harder to handle errors consistently
1482
+ - **Recommendation**: Standardize on error handling strategy
1483
+
1484
+ 3. **Undocumented API contracts**
1485
+ - No OpenAPI/Swagger documentation
1486
+ - **Impact**: Frontend developers had to read code
1487
+ - **Recommendation**: Generate API docs from code
1488
+
1489
+ ### What to Avoid in Future Projects
1490
+
1491
+ 1. **Hardcoded configuration**
1492
+ - Some settings hardcoded instead of environment variables
1493
+ - **Why bad**: Requires code changes for deployment differences
1494
+ - **Alternative**: 12-factor app configuration
1495
+
1496
+ 2. **Tight coupling to external services**
1497
+ - Direct API calls without abstraction layer
1498
+ - **Why bad**: Hard to swap providers, hard to test
1499
+ - **Alternative**: Adapter pattern for external integrations
1500
+
1501
+ 3. **Missing observability**
1502
+ - No metrics, basic logging only
1503
+ - **Why bad**: Blind to production issues
1504
+ - **Alternative**: Metrics + tracing + structured logs from day 1
1505
+
1506
+ ---
1507
+
1508
+ ## Reusability Assessment
1509
+
1510
+ ### Components Reusable As-Is
1511
+
1512
+ 1. **Error handling framework** → Portable to any API project
1513
+ 2. **Database connection pooling** → Portable to any DB-backed service
1514
+ 3. **JWT authentication middleware** → Portable to any auth scenario
1515
+ 4. **Input validation schemas** → Patterns reusable, specifics domain-dependent
1516
+
1517
+ ### Patterns Worth Generalizing
1518
+
1519
+ 1. **Repository pattern** → Create skill/template for any entity
1520
+ 2. **Service orchestration** → Create skill for multi-step business logic
1521
+ 3. **API error responses** → Create skill for consistent error handling
1522
+
1523
+ ### Domain-Specific (Not Reusable)
1524
+
1525
+ 1. **Order processing logic** → Specific to e-commerce domain
1526
+ 2. **Inventory management** → Specific to this business
1527
+ 3. **Payment integration** → Specific to Stripe, but pattern reusable
1528
+ ```
1529
+
1530
+ ---
1531
+
1532
+ ## Final Validation Checklist
1533
+
1534
+ Before submitting outputs, verify:
1535
+
1536
+ - [ ] **spec.md is complete**: Can regenerate system from spec alone?
1537
+ - [ ] **plan.md is coherent**: Does architecture make sense given requirements?
1538
+ - [ ] **tasks.md is actionable**: Can team execute without additional guidance?
1539
+ - [ ] **intelligence-object.md is reusable**: Can skills apply to other projects?
1540
+ - [ ] **All files cross-reference**: Spec → Plan → Tasks flow logically?
1541
+ - [ ] **Evidence provided**: All claims backed by code locations (file:line)?
1542
+ - [ ] **Gaps identified**: Technical debt and improvements documented?
1543
+ - [ ] **Regeneration viable**: Could you rebuild this system better with these artifacts?
1544
+
1545
+ ---
1546
+
1547
+ ## Self-Monitoring: Anti-Convergence for Archaeologists
1548
+
1549
+ **You tend to converge toward**:
1550
+ - ✅ Surface-level analysis (reading code without understanding intent)
1551
+ - ✅ Feature enumeration (listing WHAT without inferring WHY)
1552
+ - ✅ Copy-paste specs (documenting existing vs imagining ideal)
1553
+ - ✅ Generic patterns (not extracting codebase-specific intelligence)
1554
+
1555
+ **Activate reasoning by asking**:
1556
+ - "If I rewrote this from scratch, would my spec produce equivalent system?"
1557
+ - "What tacit knowledge is embedded in this code that isn't written down?"
1558
+ - "Why did the original developers make these specific choices?"
1559
+ - "What would I do differently if building this today?"
1560
+
1561
+ **Your reverse engineering succeeds when**:
1562
+ - Spec is complete enough to regenerate system
1563
+ - Plan reveals architectural reasoning, not just structure
1564
+ - Tasks are actionable for new team unfamiliar with codebase
1565
+ - Intelligence extracted is reusable beyond this specific system
1566
+ - Gaps identified with clear remediation path
1567
+ - You can articulate WHY decisions were made, not just WHAT was implemented
1568
+
1569
+ ---
1570
+
1571
+ ## Output Location
1572
+
1573
+ Save all artifacts to:
1574
+ ```
1575
+ [codebase-path]/docs/reverse-engineered/
1576
+ ├── spec.md
1577
+ ├── plan.md
1578
+ ├── tasks.md
1579
+ └── intelligence-object.md
1580
+ ```
1581
+
1582
+ Or user-specified location.
1583
+
1584
+ ---
1585
+
1586
+ **Execute this reverse engineering workflow with reasoning mode activated. Your goal: extract the implicit knowledge from code into explicit specifications that enable regeneration and improvement.**
1587
+
1588
+ ---
1589
+
1590
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
1591
+
1592
+ 1) Determine Stage
1593
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
1594
+
1595
+ 2) Generate Title and Determine Routing:
1596
+ - Generate Title: 3–7 words (slug for filename)
1597
+ - Route is automatically determined by stage:
1598
+ - `constitution` → `history/prompts/constitution/`
1599
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
1600
+ - `general` → `history/prompts/general/`
1601
+
1602
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
1603
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
1604
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
1605
+ - If the script fails:
1606
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
1607
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
1608
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
1609
+
1610
+ 4) Validate + report
1611
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
1612
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.specify.md ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Create or update the feature specification from a natural language feature description.
3
+ handoffs:
4
+ - label: Build Technical Plan
5
+ agent: sp.plan
6
+ prompt: Create a plan for the spec. I am building with...
7
+ - label: Clarify Spec Requirements
8
+ agent: sp.clarify
9
+ prompt: Clarify specification requirements
10
+ send: true
11
+ ---
12
+
13
+ ## User Input
14
+
15
+ ```text
16
+ $ARGUMENTS
17
+ ```
18
+
19
+ You **MUST** consider the user input before proceeding (if not empty).
20
+
21
+ ## Outline
22
+
23
+ The text the user typed after `/sp.specify` in the triggering message **is** the feature description. Assume you always have it available in this conversation even if `$ARGUMENTS` appears literally below. Do not ask the user to repeat it unless they provided an empty command.
24
+
25
+ Given that feature description, do this:
26
+
27
+ 1. **Generate a concise short name** (2-4 words) for the branch:
28
+ - Analyze the feature description and extract the most meaningful keywords
29
+ - Create a 2-4 word short name that captures the essence of the feature
30
+ - Use action-noun format when possible (e.g., "add-user-auth", "fix-payment-bug")
31
+ - Preserve technical terms and acronyms (OAuth2, API, JWT, etc.)
32
+ - Keep it concise but descriptive enough to understand the feature at a glance
33
+ - Examples:
34
+ - "I want to add user authentication" → "user-auth"
35
+ - "Implement OAuth2 integration for the API" → "oauth2-api-integration"
36
+ - "Create a dashboard for analytics" → "analytics-dashboard"
37
+ - "Fix payment processing timeout bug" → "fix-payment-timeout"
38
+
39
+ 2. **Check for existing branches before creating new one**:
40
+
41
+ a. First, fetch all remote branches to ensure we have the latest information:
42
+
43
+ ```bash
44
+ git fetch --all --prune
45
+ ```
46
+
47
+ b. Find the highest feature number across all sources for the short-name:
48
+ - Remote branches: `git ls-remote --heads origin | grep -E 'refs/heads/[0-9]+-<short-name>$'`
49
+ - Local branches: `git branch | grep -E '^[* ]*[0-9]+-<short-name>$'`
50
+ - Specs directories: Check for directories matching `specs/[0-9]+-<short-name>`
51
+
52
+ c. Determine the next available number:
53
+ - Extract all numbers from all three sources
54
+ - Find the highest number N
55
+ - Use N+1 for the new branch number
56
+
57
+ d. Run the script `.specify/scripts/bash/create-new-feature.sh --json "$ARGUMENTS"` with the calculated number and short-name:
58
+ - Pass `--number N+1` and `--short-name "your-short-name"` along with the feature description
59
+ - Bash example: `.specify/scripts/bash/create-new-feature.sh --json "$ARGUMENTS" --json --number 5 --short-name "user-auth" "Add user authentication"`
60
+ - PowerShell example: `.specify/scripts/bash/create-new-feature.sh --json "$ARGUMENTS" -Json -Number 5 -ShortName "user-auth" "Add user authentication"`
61
+
62
+ **IMPORTANT**:
63
+ - Check all three sources (remote branches, local branches, specs directories) to find the highest number
64
+ - Only match branches/directories with the exact short-name pattern
65
+ - If no existing branches/directories found with this short-name, start with number 1
66
+ - You must only ever run this script once per feature
67
+ - The JSON is provided in the terminal as output - always refer to it to get the actual content you're looking for
68
+ - The JSON output will contain BRANCH_NAME and SPEC_FILE paths
69
+ - For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot")
70
+
71
+ 3. Load `.specify/templates/spec-template.md` to understand required sections.
72
+
73
+ 4. Follow this execution flow:
74
+
75
+ 1. Parse user description from Input
76
+ If empty: ERROR "No feature description provided"
77
+ 2. Extract key concepts from description
78
+ Identify: actors, actions, data, constraints
79
+ 3. For unclear aspects:
80
+ - Make informed guesses based on context and industry standards
81
+ - Only mark with [NEEDS CLARIFICATION: specific question] if:
82
+ - The choice significantly impacts feature scope or user experience
83
+ - Multiple reasonable interpretations exist with different implications
84
+ - No reasonable default exists
85
+ - **LIMIT: Maximum 3 [NEEDS CLARIFICATION] markers total**
86
+ - Prioritize clarifications by impact: scope > security/privacy > user experience > technical details
87
+ 4. Fill User Scenarios & Testing section
88
+ If no clear user flow: ERROR "Cannot determine user scenarios"
89
+ 5. Generate Functional Requirements
90
+ Each requirement must be testable
91
+ Use reasonable defaults for unspecified details (document assumptions in Assumptions section)
92
+ 6. Define Success Criteria
93
+ Create measurable, technology-agnostic outcomes
94
+ Include both quantitative metrics (time, performance, volume) and qualitative measures (user satisfaction, task completion)
95
+ Each criterion must be verifiable without implementation details
96
+ 7. Identify Key Entities (if data involved)
97
+ 8. Return: SUCCESS (spec ready for planning)
98
+
99
+ 5. Write the specification to SPEC_FILE using the template structure, replacing placeholders with concrete details derived from the feature description (arguments) while preserving section order and headings.
100
+
101
+ 6. **Specification Quality Validation**: After writing the initial spec, validate it against quality criteria:
102
+
103
+ a. **Create Spec Quality Checklist**: Generate a checklist file at `FEATURE_DIR/checklists/requirements.md` using the checklist template structure with these validation items:
104
+
105
+ ```markdown
106
+ # Specification Quality Checklist: [FEATURE NAME]
107
+
108
+ **Purpose**: Validate specification completeness and quality before proceeding to planning
109
+ **Created**: [DATE]
110
+ **Feature**: [Link to spec.md]
111
+
112
+ ## Content Quality
113
+
114
+ - [ ] No implementation details (languages, frameworks, APIs)
115
+ - [ ] Focused on user value and business needs
116
+ - [ ] Written for non-technical stakeholders
117
+ - [ ] All mandatory sections completed
118
+
119
+ ## Requirement Completeness
120
+
121
+ - [ ] No [NEEDS CLARIFICATION] markers remain
122
+ - [ ] Requirements are testable and unambiguous
123
+ - [ ] Success criteria are measurable
124
+ - [ ] Success criteria are technology-agnostic (no implementation details)
125
+ - [ ] All acceptance scenarios are defined
126
+ - [ ] Edge cases are identified
127
+ - [ ] Scope is clearly bounded
128
+ - [ ] Dependencies and assumptions identified
129
+
130
+ ## Feature Readiness
131
+
132
+ - [ ] All functional requirements have clear acceptance criteria
133
+ - [ ] User scenarios cover primary flows
134
+ - [ ] Feature meets measurable outcomes defined in Success Criteria
135
+ - [ ] No implementation details leak into specification
136
+
137
+ ## Notes
138
+
139
+ - Items marked incomplete require spec updates before `/sp.clarify` or `/sp.plan`
140
+ ```
141
+
142
+ b. **Run Validation Check**: Review the spec against each checklist item:
143
+ - For each item, determine if it passes or fails
144
+ - Document specific issues found (quote relevant spec sections)
145
+
146
+ c. **Handle Validation Results**:
147
+
148
+ - **If all items pass**: Mark checklist complete and proceed to step 6
149
+
150
+ - **If items fail (excluding [NEEDS CLARIFICATION])**:
151
+ 1. List the failing items and specific issues
152
+ 2. Update the spec to address each issue
153
+ 3. Re-run validation until all items pass (max 3 iterations)
154
+ 4. If still failing after 3 iterations, document remaining issues in checklist notes and warn user
155
+
156
+ - **If [NEEDS CLARIFICATION] markers remain**:
157
+ 1. Extract all [NEEDS CLARIFICATION: ...] markers from the spec
158
+ 2. **LIMIT CHECK**: If more than 3 markers exist, keep only the 3 most critical (by scope/security/UX impact) and make informed guesses for the rest
159
+ 3. For each clarification needed (max 3), present options to user in this format:
160
+
161
+ ```markdown
162
+ ## Question [N]: [Topic]
163
+
164
+ **Context**: [Quote relevant spec section]
165
+
166
+ **What we need to know**: [Specific question from NEEDS CLARIFICATION marker]
167
+
168
+ **Suggested Answers**:
169
+
170
+ | Option | Answer | Implications |
171
+ |--------|--------|--------------|
172
+ | A | [First suggested answer] | [What this means for the feature] |
173
+ | B | [Second suggested answer] | [What this means for the feature] |
174
+ | C | [Third suggested answer] | [What this means for the feature] |
175
+ | Custom | Provide your own answer | [Explain how to provide custom input] |
176
+
177
+ **Your choice**: _[Wait for user response]_
178
+ ```
179
+
180
+ 4. **CRITICAL - Table Formatting**: Ensure markdown tables are properly formatted:
181
+ - Use consistent spacing with pipes aligned
182
+ - Each cell should have spaces around content: `| Content |` not `|Content|`
183
+ - Header separator must have at least 3 dashes: `|--------|`
184
+ - Test that the table renders correctly in markdown preview
185
+ 5. Number questions sequentially (Q1, Q2, Q3 - max 3 total)
186
+ 6. Present all questions together before waiting for responses
187
+ 7. Wait for user to respond with their choices for all questions (e.g., "Q1: A, Q2: Custom - [details], Q3: B")
188
+ 8. Update the spec by replacing each [NEEDS CLARIFICATION] marker with the user's selected or provided answer
189
+ 9. Re-run validation after all clarifications are resolved
190
+
191
+ d. **Update Checklist**: After each validation iteration, update the checklist file with current pass/fail status
192
+
193
+ 7. Report completion with branch name, spec file path, checklist results, and readiness for the next phase (`/sp.clarify` or `/sp.plan`).
194
+
195
+ **NOTE:** The script creates and checks out the new branch and initializes the spec file before writing.
196
+
197
+ ## General Guidelines
198
+
199
+ ## Quick Guidelines
200
+
201
+ - Focus on **WHAT** users need and **WHY**.
202
+ - Avoid HOW to implement (no tech stack, APIs, code structure).
203
+ - Written for business stakeholders, not developers.
204
+ - DO NOT create any checklists that are embedded in the spec. That will be a separate command.
205
+
206
+ ### Section Requirements
207
+
208
+ - **Mandatory sections**: Must be completed for every feature
209
+ - **Optional sections**: Include only when relevant to the feature
210
+ - When a section doesn't apply, remove it entirely (don't leave as "N/A")
211
+
212
+ ### For AI Generation
213
+
214
+ When creating this spec from a user prompt:
215
+
216
+ 1. **Make informed guesses**: Use context, industry standards, and common patterns to fill gaps
217
+ 2. **Document assumptions**: Record reasonable defaults in the Assumptions section
218
+ 3. **Limit clarifications**: Maximum 3 [NEEDS CLARIFICATION] markers - use only for critical decisions that:
219
+ - Significantly impact feature scope or user experience
220
+ - Have multiple reasonable interpretations with different implications
221
+ - Lack any reasonable default
222
+ 4. **Prioritize clarifications**: scope > security/privacy > user experience > technical details
223
+ 5. **Think like a tester**: Every vague requirement should fail the "testable and unambiguous" checklist item
224
+ 6. **Common areas needing clarification** (only if no reasonable default exists):
225
+ - Feature scope and boundaries (include/exclude specific use cases)
226
+ - User types and permissions (if multiple conflicting interpretations possible)
227
+ - Security/compliance requirements (when legally/financially significant)
228
+
229
+ **Examples of reasonable defaults** (don't ask about these):
230
+
231
+ - Data retention: Industry-standard practices for the domain
232
+ - Performance targets: Standard web/mobile app expectations unless specified
233
+ - Error handling: User-friendly messages with appropriate fallbacks
234
+ - Authentication method: Standard session-based or OAuth2 for web apps
235
+ - Integration patterns: RESTful APIs unless specified otherwise
236
+
237
+ ### Success Criteria Guidelines
238
+
239
+ Success criteria must be:
240
+
241
+ 1. **Measurable**: Include specific metrics (time, percentage, count, rate)
242
+ 2. **Technology-agnostic**: No mention of frameworks, languages, databases, or tools
243
+ 3. **User-focused**: Describe outcomes from user/business perspective, not system internals
244
+ 4. **Verifiable**: Can be tested/validated without knowing implementation details
245
+
246
+ **Good examples**:
247
+
248
+ - "Users can complete checkout in under 3 minutes"
249
+ - "System supports 10,000 concurrent users"
250
+ - "95% of searches return results in under 1 second"
251
+ - "Task completion rate improves by 40%"
252
+
253
+ **Bad examples** (implementation-focused):
254
+
255
+ - "API response time is under 200ms" (too technical, use "Users see results instantly")
256
+ - "Database can handle 1000 TPS" (implementation detail, use user-facing metric)
257
+ - "React components render efficiently" (framework-specific)
258
+ - "Redis cache hit rate above 80%" (technology-specific)
259
+
260
+ ---
261
+
262
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
263
+
264
+ 1) Determine Stage
265
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
266
+
267
+ 2) Generate Title and Determine Routing:
268
+ - Generate Title: 3–7 words (slug for filename)
269
+ - Route is automatically determined by stage:
270
+ - `constitution` → `history/prompts/constitution/`
271
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
272
+ - `general` → `history/prompts/general/`
273
+
274
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
275
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
276
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
277
+ - If the script fails:
278
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
279
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
280
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
281
+
282
+ 4) Validate + report
283
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
284
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.tasks.md ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Generate an actionable, dependency-ordered tasks.md for the feature based on available design artifacts.
3
+ handoffs:
4
+ - label: Analyze For Consistency
5
+ agent: sp.analyze
6
+ prompt: Run a project analysis for consistency
7
+ send: true
8
+ - label: Implement Project
9
+ agent: sp.implement
10
+ prompt: Start the implementation in phases
11
+ send: true
12
+ ---
13
+
14
+ ## User Input
15
+
16
+ ```text
17
+ $ARGUMENTS
18
+ ```
19
+
20
+ You **MUST** consider the user input before proceeding (if not empty).
21
+
22
+ ## Outline
23
+
24
+ 1. **Setup**: Run `.specify/scripts/bash/check-prerequisites.sh --json` from repo root and parse FEATURE_DIR and AVAILABLE_DOCS list. All paths must be absolute. For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
25
+
26
+ 2. **Load design documents**: Read from FEATURE_DIR:
27
+ - **Required**: plan.md (tech stack, libraries, structure), spec.md (user stories with priorities)
28
+ - **Optional**: data-model.md (entities), contracts/ (API endpoints), research.md (decisions), quickstart.md (test scenarios)
29
+ - Note: Not all projects have all documents. Generate tasks based on what's available.
30
+
31
+ 3. **Execute task generation workflow**:
32
+ - Load plan.md and extract tech stack, libraries, project structure
33
+ - Load spec.md and extract user stories with their priorities (P1, P2, P3, etc.)
34
+ - If data-model.md exists: Extract entities and map to user stories
35
+ - If contracts/ exists: Map endpoints to user stories
36
+ - If research.md exists: Extract decisions for setup tasks
37
+ - Generate tasks organized by user story (see Task Generation Rules below)
38
+ - Generate dependency graph showing user story completion order
39
+ - Create parallel execution examples per user story
40
+ - Validate task completeness (each user story has all needed tasks, independently testable)
41
+
42
+ 4. **Generate tasks.md**: Use `.specify/templates/tasks-template.md` as structure, fill with:
43
+ - Correct feature name from plan.md
44
+ - Phase 1: Setup tasks (project initialization)
45
+ - Phase 2: Foundational tasks (blocking prerequisites for all user stories)
46
+ - Phase 3+: One phase per user story (in priority order from spec.md)
47
+ - Each phase includes: story goal, independent test criteria, tests (if requested), implementation tasks
48
+ - Final Phase: Polish & cross-cutting concerns
49
+ - All tasks must follow the strict checklist format (see Task Generation Rules below)
50
+ - Clear file paths for each task
51
+ - Dependencies section showing story completion order
52
+ - Parallel execution examples per story
53
+ - Implementation strategy section (MVP first, incremental delivery)
54
+
55
+ 5. **Report**: Output path to generated tasks.md and summary:
56
+ - Total task count
57
+ - Task count per user story
58
+ - Parallel opportunities identified
59
+ - Independent test criteria for each story
60
+ - Suggested MVP scope (typically just User Story 1)
61
+ - Format validation: Confirm ALL tasks follow the checklist format (checkbox, ID, labels, file paths)
62
+
63
+ Context for task generation: $ARGUMENTS
64
+
65
+ The tasks.md should be immediately executable - each task must be specific enough that an LLM can complete it without additional context.
66
+
67
+ ## Task Generation Rules
68
+
69
+ **CRITICAL**: Tasks MUST be organized by user story to enable independent implementation and testing.
70
+
71
+ **Tests are OPTIONAL**: Only generate test tasks if explicitly requested in the feature specification or if user requests TDD approach.
72
+
73
+ ### Checklist Format (REQUIRED)
74
+
75
+ Every task MUST strictly follow this format:
76
+
77
+ ```text
78
+ - [ ] [TaskID] [P?] [Story?] Description with file path
79
+ ```
80
+
81
+ **Format Components**:
82
+
83
+ 1. **Checkbox**: ALWAYS start with `- [ ]` (markdown checkbox)
84
+ 2. **Task ID**: Sequential number (T001, T002, T003...) in execution order
85
+ 3. **[P] marker**: Include ONLY if task is parallelizable (different files, no dependencies on incomplete tasks)
86
+ 4. **[Story] label**: REQUIRED for user story phase tasks only
87
+ - Format: [US1], [US2], [US3], etc. (maps to user stories from spec.md)
88
+ - Setup phase: NO story label
89
+ - Foundational phase: NO story label
90
+ - User Story phases: MUST have story label
91
+ - Polish phase: NO story label
92
+ 5. **Description**: Clear action with exact file path
93
+
94
+ **Examples**:
95
+
96
+ - ✅ CORRECT: `- [ ] T001 Create project structure per implementation plan`
97
+ - ✅ CORRECT: `- [ ] T005 [P] Implement authentication middleware in src/middleware/auth.py`
98
+ - ✅ CORRECT: `- [ ] T012 [P] [US1] Create User model in src/models/user.py`
99
+ - ✅ CORRECT: `- [ ] T014 [US1] Implement UserService in src/services/user_service.py`
100
+ - ❌ WRONG: `- [ ] Create User model` (missing ID and Story label)
101
+ - ❌ WRONG: `T001 [US1] Create model` (missing checkbox)
102
+ - ❌ WRONG: `- [ ] [US1] Create User model` (missing Task ID)
103
+ - ❌ WRONG: `- [ ] T001 [US1] Create model` (missing file path)
104
+
105
+ ### Task Organization
106
+
107
+ 1. **From User Stories (spec.md)** - PRIMARY ORGANIZATION:
108
+ - Each user story (P1, P2, P3...) gets its own phase
109
+ - Map all related components to their story:
110
+ - Models needed for that story
111
+ - Services needed for that story
112
+ - Endpoints/UI needed for that story
113
+ - If tests requested: Tests specific to that story
114
+ - Mark story dependencies (most stories should be independent)
115
+
116
+ 2. **From Contracts**:
117
+ - Map each contract/endpoint → to the user story it serves
118
+ - If tests requested: Each contract → contract test task [P] before implementation in that story's phase
119
+
120
+ 3. **From Data Model**:
121
+ - Map each entity to the user story(ies) that need it
122
+ - If entity serves multiple stories: Put in earliest story or Setup phase
123
+ - Relationships → service layer tasks in appropriate story phase
124
+
125
+ 4. **From Setup/Infrastructure**:
126
+ - Shared infrastructure → Setup phase (Phase 1)
127
+ - Foundational/blocking tasks → Foundational phase (Phase 2)
128
+ - Story-specific setup → within that story's phase
129
+
130
+ ### Phase Structure
131
+
132
+ - **Phase 1**: Setup (project initialization)
133
+ - **Phase 2**: Foundational (blocking prerequisites - MUST complete before user stories)
134
+ - **Phase 3+**: User Stories in priority order (P1, P2, P3...)
135
+ - Within each story: Tests (if requested) → Models → Services → Endpoints → Integration
136
+ - Each phase should be a complete, independently testable increment
137
+ - **Final Phase**: Polish & Cross-Cutting Concerns
138
+
139
+ ---
140
+
141
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
142
+
143
+ 1) Determine Stage
144
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
145
+
146
+ 2) Generate Title and Determine Routing:
147
+ - Generate Title: 3–7 words (slug for filename)
148
+ - Route is automatically determined by stage:
149
+ - `constitution` → `history/prompts/constitution/`
150
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
151
+ - `general` → `history/prompts/general/`
152
+
153
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
154
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
155
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
156
+ - If the script fails:
157
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
158
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
159
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
160
+
161
+ 4) Validate + report
162
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
163
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.claude/commands/sp.taskstoissues.md ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description: Convert existing tasks into actionable, dependency-ordered GitHub issues for the feature based on available design artifacts.
3
+ tools: ['github/github-mcp-server/issue_write']
4
+ ---
5
+
6
+ ## User Input
7
+
8
+ ```text
9
+ $ARGUMENTS
10
+ ```
11
+
12
+ You **MUST** consider the user input before proceeding (if not empty).
13
+
14
+ ## Outline
15
+
16
+ 1. Run `.specify/scripts/bash/check-prerequisites.sh --json --require-tasks --include-tasks` from repo root and parse FEATURE_DIR and AVAILABLE_DOCS list. All paths must be absolute. For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot").
17
+ 1. From the executed script, extract the path to **tasks**.
18
+ 1. Get the Git remote by running:
19
+
20
+ ```bash
21
+ git config --get remote.origin.url
22
+ ```
23
+
24
+ > [!CAUTION]
25
+ > ONLY PROCEED TO NEXT STEPS IF THE REMOTE IS A GITHUB URL
26
+
27
+ 1. For each task in the list, use the GitHub MCP server to create a new issue in the repository that is representative of the Git remote.
28
+
29
+ > [!CAUTION]
30
+ > UNDER NO CIRCUMSTANCES EVER CREATE ISSUES IN REPOSITORIES THAT DO NOT MATCH THE REMOTE URL
31
+
32
+ ---
33
+
34
+ As the main request completes, you MUST create and complete a PHR (Prompt History Record) using agent‑native tools when possible.
35
+
36
+ 1) Determine Stage
37
+ - Stage: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
38
+
39
+ 2) Generate Title and Determine Routing:
40
+ - Generate Title: 3–7 words (slug for filename)
41
+ - Route is automatically determined by stage:
42
+ - `constitution` → `history/prompts/constitution/`
43
+ - Feature stages → `history/prompts/<feature-name>/` (spec, plan, tasks, red, green, refactor, explainer, misc)
44
+ - `general` → `history/prompts/general/`
45
+
46
+ 3) Create and Fill PHR (Shell first; fallback agent‑native)
47
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
48
+ - Open the file and fill remaining placeholders (YAML + body), embedding full PROMPT_TEXT (verbatim) and concise RESPONSE_TEXT.
49
+ - If the script fails:
50
+ - Read `.specify/templates/phr-template.prompt.md` (or `templates/…`)
51
+ - Allocate an ID; compute the output path based on stage from step 2; write the file
52
+ - Fill placeholders and embed full PROMPT_TEXT and concise RESPONSE_TEXT
53
+
54
+ 4) Validate + report
55
+ - No unresolved placeholders; path under `history/prompts/` and matches stage; stage/title/date coherent; print ID + path + stage + title.
56
+ - On failure: warn, don't block. Skip only for `/sp.phr`.
.dockerignore ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ **/.git
2
+ **/.gitignore
3
+ **/node_modules
4
+ **/__pycache__
5
+ **/*.pyc
6
+ **/.pytest_cache
7
+ **/.env
8
+ **/.env.*
9
+ **/venv
10
+ **/venv_linux
11
+ **/.next
12
+ **/uploads/*
13
+ **/*.log
14
+ **/.DS_Store
15
+ phase-2-ai-engine
16
+ phase-3-approval-system
17
+ phase-4-learning-loop
18
+ phase-5-video-engine
19
+ phase-6-saas-core
20
+ docs
21
+ history
22
+ specs
.gitattributes ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ *.woff2 filter=lfs diff=lfs merge=lfs -text
2
+ *.woff filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.pyo
5
+ *.pyd
6
+ .Python
7
+ *.egg-info/
8
+ dist/
9
+ build/
10
+ *.egg
11
+ .pytest_cache/
12
+ .coverage
13
+ htmlcov/
14
+ venv/
15
+ venv_linux/
16
+ env/
17
+ .env
18
+ .env.*
19
+ !.env.example
20
+ # Node.js
21
+ node_modules/
22
+ .next/
23
+ out/
24
+ npm-debug.log*
25
+ yarn-debug.log*
26
+ yarn-error.log*
27
+ *.log
28
+ frontend_output.log
29
+ # IDEs
30
+ .idea/
31
+ .vscode/
32
+ *.swp
33
+ *.swo
34
+ .DS_Store
35
+ # Uploads (user data)
36
+ uploads/
37
+ *.png
38
+ *.jpg
39
+ *.jpeg
40
+ *.mp4
41
+ *.mov
42
+ # Local config
43
+ *.local
44
+ settings.local.json
45
+ # Misc
46
+ mcp-shell.log
47
+ *.bak
48
+ phase-1-core-infra/frontend/.next/standalone
49
+ phase-1-core-infra/frontend/.next/cache
.specify/memory/constitution.md ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [PROJECT_NAME] Constitution
2
+ <!-- Example: Spec Constitution, TaskFlow Constitution, etc. -->
3
+
4
+ ## Core Principles
5
+
6
+ ### [PRINCIPLE_1_NAME]
7
+ <!-- Example: I. Library-First -->
8
+ [PRINCIPLE_1_DESCRIPTION]
9
+ <!-- Example: Every feature starts as a standalone library; Libraries must be self-contained, independently testable, documented; Clear purpose required - no organizational-only libraries -->
10
+
11
+ ### [PRINCIPLE_2_NAME]
12
+ <!-- Example: II. CLI Interface -->
13
+ [PRINCIPLE_2_DESCRIPTION]
14
+ <!-- Example: Every library exposes functionality via CLI; Text in/out protocol: stdin/args → stdout, errors → stderr; Support JSON + human-readable formats -->
15
+
16
+ ### [PRINCIPLE_3_NAME]
17
+ <!-- Example: III. Test-First (NON-NEGOTIABLE) -->
18
+ [PRINCIPLE_3_DESCRIPTION]
19
+ <!-- Example: TDD mandatory: Tests written → User approved → Tests fail → Then implement; Red-Green-Refactor cycle strictly enforced -->
20
+
21
+ ### [PRINCIPLE_4_NAME]
22
+ <!-- Example: IV. Integration Testing -->
23
+ [PRINCIPLE_4_DESCRIPTION]
24
+ <!-- Example: Focus areas requiring integration tests: New library contract tests, Contract changes, Inter-service communication, Shared schemas -->
25
+
26
+ ### [PRINCIPLE_5_NAME]
27
+ <!-- Example: V. Observability, VI. Versioning & Breaking Changes, VII. Simplicity -->
28
+ [PRINCIPLE_5_DESCRIPTION]
29
+ <!-- Example: Text I/O ensures debuggability; Structured logging required; Or: MAJOR.MINOR.BUILD format; Or: Start simple, YAGNI principles -->
30
+
31
+ ### [PRINCIPLE_6_NAME]
32
+
33
+
34
+ [PRINCIPLE__DESCRIPTION]
35
+
36
+ ## [SECTION_2_NAME]
37
+ <!-- Example: Additional Constraints, Security Requirements, Performance Standards, etc. -->
38
+
39
+ [SECTION_2_CONTENT]
40
+ <!-- Example: Technology stack requirements, compliance standards, deployment policies, etc. -->
41
+
42
+ ## [SECTION_3_NAME]
43
+ <!-- Example: Development Workflow, Review Process, Quality Gates, etc. -->
44
+
45
+ [SECTION_3_CONTENT]
46
+ <!-- Example: Code review requirements, testing gates, deployment approval process, etc. -->
47
+
48
+ ## Governance
49
+ <!-- Example: Constitution supersedes all other practices; Amendments require documentation, approval, migration plan -->
50
+
51
+ [GOVERNANCE_RULES]
52
+ <!-- Example: All PRs/reviews must verify compliance; Complexity must be justified; Use [GUIDANCE_FILE] for runtime development guidance -->
53
+
54
+ **Version**: [CONSTITUTION_VERSION] | **Ratified**: [RATIFICATION_DATE] | **Last Amended**: [LAST_AMENDED_DATE]
55
+ <!-- Example: Version: 2.1.1 | Ratified: 2025-06-13 | Last Amended: 2025-07-16 -->
.specify/scripts/bash/check-prerequisites.sh ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ # Consolidated prerequisite checking script
4
+ #
5
+ # This script provides unified prerequisite checking for Spec-Driven Development workflow.
6
+ # It replaces the functionality previously spread across multiple scripts.
7
+ #
8
+ # Usage: ./check-prerequisites.sh [OPTIONS]
9
+ #
10
+ # OPTIONS:
11
+ # --json Output in JSON format
12
+ # --require-tasks Require tasks.md to exist (for implementation phase)
13
+ # --include-tasks Include tasks.md in AVAILABLE_DOCS list
14
+ # --paths-only Only output path variables (no validation)
15
+ # --help, -h Show help message
16
+ #
17
+ # OUTPUTS:
18
+ # JSON mode: {"FEATURE_DIR":"...", "AVAILABLE_DOCS":["..."]}
19
+ # Text mode: FEATURE_DIR:... \n AVAILABLE_DOCS: \n ✓/✗ file.md
20
+ # Paths only: REPO_ROOT: ... \n BRANCH: ... \n FEATURE_DIR: ... etc.
21
+
22
+ set -e
23
+
24
+ # Parse command line arguments
25
+ JSON_MODE=false
26
+ REQUIRE_TASKS=false
27
+ INCLUDE_TASKS=false
28
+ PATHS_ONLY=false
29
+
30
+ for arg in "$@"; do
31
+ case "$arg" in
32
+ --json)
33
+ JSON_MODE=true
34
+ ;;
35
+ --require-tasks)
36
+ REQUIRE_TASKS=true
37
+ ;;
38
+ --include-tasks)
39
+ INCLUDE_TASKS=true
40
+ ;;
41
+ --paths-only)
42
+ PATHS_ONLY=true
43
+ ;;
44
+ --help|-h)
45
+ cat << 'EOF'
46
+ Usage: check-prerequisites.sh [OPTIONS]
47
+
48
+ Consolidated prerequisite checking for Spec-Driven Development workflow.
49
+
50
+ OPTIONS:
51
+ --json Output in JSON format
52
+ --require-tasks Require tasks.md to exist (for implementation phase)
53
+ --include-tasks Include tasks.md in AVAILABLE_DOCS list
54
+ --paths-only Only output path variables (no prerequisite validation)
55
+ --help, -h Show this help message
56
+
57
+ EXAMPLES:
58
+ # Check task prerequisites (plan.md required)
59
+ ./check-prerequisites.sh --json
60
+
61
+ # Check implementation prerequisites (plan.md + tasks.md required)
62
+ ./check-prerequisites.sh --json --require-tasks --include-tasks
63
+
64
+ # Get feature paths only (no validation)
65
+ ./check-prerequisites.sh --paths-only
66
+
67
+ EOF
68
+ exit 0
69
+ ;;
70
+ *)
71
+ echo "ERROR: Unknown option '$arg'. Use --help for usage information." >&2
72
+ exit 1
73
+ ;;
74
+ esac
75
+ done
76
+
77
+ # Source common functions
78
+ SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
79
+ source "$SCRIPT_DIR/common.sh"
80
+
81
+ # Get feature paths and validate branch
82
+ eval $(get_feature_paths)
83
+ check_feature_branch "$CURRENT_BRANCH" "$HAS_GIT" || exit 1
84
+
85
+ # If paths-only mode, output paths and exit (support JSON + paths-only combined)
86
+ if $PATHS_ONLY; then
87
+ if $JSON_MODE; then
88
+ # Minimal JSON paths payload (no validation performed)
89
+ printf '{"REPO_ROOT":"%s","BRANCH":"%s","FEATURE_DIR":"%s","FEATURE_SPEC":"%s","IMPL_PLAN":"%s","TASKS":"%s"}\n' \
90
+ "$REPO_ROOT" "$CURRENT_BRANCH" "$FEATURE_DIR" "$FEATURE_SPEC" "$IMPL_PLAN" "$TASKS"
91
+ else
92
+ echo "REPO_ROOT: $REPO_ROOT"
93
+ echo "BRANCH: $CURRENT_BRANCH"
94
+ echo "FEATURE_DIR: $FEATURE_DIR"
95
+ echo "FEATURE_SPEC: $FEATURE_SPEC"
96
+ echo "IMPL_PLAN: $IMPL_PLAN"
97
+ echo "TASKS: $TASKS"
98
+ fi
99
+ exit 0
100
+ fi
101
+
102
+ # Validate required directories and files
103
+ if [[ ! -d "$FEATURE_DIR" ]]; then
104
+ echo "ERROR: Feature directory not found: $FEATURE_DIR" >&2
105
+ echo "Run /sp.specify first to create the feature structure." >&2
106
+ exit 1
107
+ fi
108
+
109
+ if [[ ! -f "$IMPL_PLAN" ]]; then
110
+ echo "ERROR: plan.md not found in $FEATURE_DIR" >&2
111
+ echo "Run /sp.plan first to create the implementation plan." >&2
112
+ exit 1
113
+ fi
114
+
115
+ # Check for tasks.md if required
116
+ if $REQUIRE_TASKS && [[ ! -f "$TASKS" ]]; then
117
+ echo "ERROR: tasks.md not found in $FEATURE_DIR" >&2
118
+ echo "Run /sp.tasks first to create the task list." >&2
119
+ exit 1
120
+ fi
121
+
122
+ # Build list of available documents
123
+ docs=()
124
+
125
+ # Always check these optional docs
126
+ [[ -f "$RESEARCH" ]] && docs+=("research.md")
127
+ [[ -f "$DATA_MODEL" ]] && docs+=("data-model.md")
128
+
129
+ # Check contracts directory (only if it exists and has files)
130
+ if [[ -d "$CONTRACTS_DIR" ]] && [[ -n "$(ls -A "$CONTRACTS_DIR" 2>/dev/null)" ]]; then
131
+ docs+=("contracts/")
132
+ fi
133
+
134
+ [[ -f "$QUICKSTART" ]] && docs+=("quickstart.md")
135
+
136
+ # Include tasks.md if requested and it exists
137
+ if $INCLUDE_TASKS && [[ -f "$TASKS" ]]; then
138
+ docs+=("tasks.md")
139
+ fi
140
+
141
+ # Output results
142
+ if $JSON_MODE; then
143
+ # Build JSON array of documents
144
+ if [[ ${#docs[@]} -eq 0 ]]; then
145
+ json_docs="[]"
146
+ else
147
+ json_docs=$(printf '"%s",' "${docs[@]}")
148
+ json_docs="[${json_docs%,}]"
149
+ fi
150
+
151
+ printf '{"FEATURE_DIR":"%s","AVAILABLE_DOCS":%s}\n' "$FEATURE_DIR" "$json_docs"
152
+ else
153
+ # Text output
154
+ echo "FEATURE_DIR:$FEATURE_DIR"
155
+ echo "AVAILABLE_DOCS:"
156
+
157
+ # Show status of each potential document
158
+ check_file "$RESEARCH" "research.md"
159
+ check_file "$DATA_MODEL" "data-model.md"
160
+ check_dir "$CONTRACTS_DIR" "contracts/"
161
+ check_file "$QUICKSTART" "quickstart.md"
162
+
163
+ if $INCLUDE_TASKS; then
164
+ check_file "$TASKS" "tasks.md"
165
+ fi
166
+ fi
.specify/scripts/bash/common.sh ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Common functions and variables for all scripts
3
+
4
+ # Get repository root, with fallback for non-git repositories
5
+ get_repo_root() {
6
+ if git rev-parse --show-toplevel >/dev/null 2>&1; then
7
+ git rev-parse --show-toplevel
8
+ else
9
+ # Fall back to script location for non-git repos
10
+ local script_dir="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11
+ (cd "$script_dir/../../.." && pwd)
12
+ fi
13
+ }
14
+
15
+ # Get current branch, with fallback for non-git repositories
16
+ get_current_branch() {
17
+ # First check if SPECIFY_FEATURE environment variable is set
18
+ if [[ -n "${SPECIFY_FEATURE:-}" ]]; then
19
+ echo "$SPECIFY_FEATURE"
20
+ return
21
+ fi
22
+
23
+ # Then check git if available
24
+ if git rev-parse --abbrev-ref HEAD >/dev/null 2>&1; then
25
+ git rev-parse --abbrev-ref HEAD
26
+ return
27
+ fi
28
+
29
+ # For non-git repos, try to find the latest feature directory
30
+ local repo_root=$(get_repo_root)
31
+ local specs_dir="$repo_root/specs"
32
+
33
+ if [[ -d "$specs_dir" ]]; then
34
+ local latest_feature=""
35
+ local highest=0
36
+
37
+ for dir in "$specs_dir"/*; do
38
+ if [[ -d "$dir" ]]; then
39
+ local dirname=$(basename "$dir")
40
+ if [[ "$dirname" =~ ^([0-9]{3})- ]]; then
41
+ local number=${BASH_REMATCH[1]}
42
+ number=$((10#$number))
43
+ if [[ "$number" -gt "$highest" ]]; then
44
+ highest=$number
45
+ latest_feature=$dirname
46
+ fi
47
+ fi
48
+ fi
49
+ done
50
+
51
+ if [[ -n "$latest_feature" ]]; then
52
+ echo "$latest_feature"
53
+ return
54
+ fi
55
+ fi
56
+
57
+ echo "main" # Final fallback
58
+ }
59
+
60
+ # Check if we have git available
61
+ has_git() {
62
+ git rev-parse --show-toplevel >/dev/null 2>&1
63
+ }
64
+
65
+ check_feature_branch() {
66
+ local branch="$1"
67
+ local has_git_repo="$2"
68
+
69
+ # For non-git repos, we can't enforce branch naming but still provide output
70
+ if [[ "$has_git_repo" != "true" ]]; then
71
+ echo "[specify] Warning: Git repository not detected; skipped branch validation" >&2
72
+ return 0
73
+ fi
74
+
75
+ if [[ ! "$branch" =~ ^[0-9]{3}- ]]; then
76
+ echo "ERROR: Not on a feature branch. Current branch: $branch" >&2
77
+ echo "Feature branches should be named like: 001-feature-name" >&2
78
+ return 1
79
+ fi
80
+
81
+ return 0
82
+ }
83
+
84
+ get_feature_dir() { echo "$1/specs/$2"; }
85
+
86
+ # Find feature directory by numeric prefix instead of exact branch match
87
+ # This allows multiple branches to work on the same spec (e.g., 004-fix-bug, 004-add-feature)
88
+ find_feature_dir_by_prefix() {
89
+ local repo_root="$1"
90
+ local branch_name="$2"
91
+ local specs_dir="$repo_root/specs"
92
+
93
+ # Extract numeric prefix from branch (e.g., "004" from "004-whatever")
94
+ if [[ ! "$branch_name" =~ ^([0-9]{3})- ]]; then
95
+ # If branch doesn't have numeric prefix, fall back to exact match
96
+ echo "$specs_dir/$branch_name"
97
+ return
98
+ fi
99
+
100
+ local prefix="${BASH_REMATCH[1]}"
101
+
102
+ # Search for directories in specs/ that start with this prefix
103
+ local matches=()
104
+ if [[ -d "$specs_dir" ]]; then
105
+ for dir in "$specs_dir"/"$prefix"-*; do
106
+ if [[ -d "$dir" ]]; then
107
+ matches+=("$(basename "$dir")")
108
+ fi
109
+ done
110
+ fi
111
+
112
+ # Handle results
113
+ if [[ ${#matches[@]} -eq 0 ]]; then
114
+ # No match found - return the branch name path (will fail later with clear error)
115
+ echo "$specs_dir/$branch_name"
116
+ elif [[ ${#matches[@]} -eq 1 ]]; then
117
+ # Exactly one match - perfect!
118
+ echo "$specs_dir/${matches[0]}"
119
+ else
120
+ # Multiple matches - this shouldn't happen with proper naming convention
121
+ echo "ERROR: Multiple spec directories found with prefix '$prefix': ${matches[*]}" >&2
122
+ echo "Please ensure only one spec directory exists per numeric prefix." >&2
123
+ echo "$specs_dir/$branch_name" # Return something to avoid breaking the script
124
+ fi
125
+ }
126
+
127
+ get_feature_paths() {
128
+ local repo_root=$(get_repo_root)
129
+ local current_branch=$(get_current_branch)
130
+ local has_git_repo="false"
131
+
132
+ if has_git; then
133
+ has_git_repo="true"
134
+ fi
135
+
136
+ # Use prefix-based lookup to support multiple branches per spec
137
+ local feature_dir=$(find_feature_dir_by_prefix "$repo_root" "$current_branch")
138
+
139
+ cat <<EOF
140
+ REPO_ROOT='$repo_root'
141
+ CURRENT_BRANCH='$current_branch'
142
+ HAS_GIT='$has_git_repo'
143
+ FEATURE_DIR='$feature_dir'
144
+ FEATURE_SPEC='$feature_dir/spec.md'
145
+ IMPL_PLAN='$feature_dir/plan.md'
146
+ TASKS='$feature_dir/tasks.md'
147
+ RESEARCH='$feature_dir/research.md'
148
+ DATA_MODEL='$feature_dir/data-model.md'
149
+ QUICKSTART='$feature_dir/quickstart.md'
150
+ CONTRACTS_DIR='$feature_dir/contracts'
151
+ EOF
152
+ }
153
+
154
+ check_file() { [[ -f "$1" ]] && echo " ✓ $2" || echo " ✗ $2"; }
155
+ check_dir() { [[ -d "$1" && -n $(ls -A "$1" 2>/dev/null) ]] && echo " ✓ $2" || echo " ✗ $2"; }
156
+
.specify/scripts/bash/create-adr.sh ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # create-adr.sh - Create a new Architecture Decision Record deterministically
5
+ #
6
+ # This script ONLY:
7
+ # 1. Creates the correct directory structure (history/adr/)
8
+ # 2. Copies the template with {{PLACEHOLDERS}} intact
9
+ # 3. Returns metadata (id, path) for AI to fill in
10
+ #
11
+ # The calling AI agent is responsible for filling {{PLACEHOLDERS}}
12
+ #
13
+ # Usage:
14
+ # scripts/bash/create-adr.sh \
15
+ # --title "Use WebSockets for Real-time Chat" \
16
+ # [--json]
17
+
18
+ JSON=false
19
+ TITLE=""
20
+
21
+ while [[ $# -gt 0 ]]; do
22
+ case "$1" in
23
+ --json) JSON=true; shift ;;
24
+ --title) TITLE=${2:-}; shift 2 ;;
25
+ --help|-h)
26
+ cat <<EOF
27
+ Usage: $0 --title <title> [options]
28
+
29
+ Required:
30
+ --title <text> Title for the ADR (used for filename)
31
+
32
+ Optional:
33
+ --json Output JSON with id and path
34
+
35
+ Output:
36
+ Creates ADR file with template placeholders ({{ID}}, {{TITLE}}, etc.)
37
+ AI agent must fill these placeholders after creation
38
+
39
+ Examples:
40
+ $0 --title "Use WebSockets for Real-time Chat" --json
41
+ $0 --title "Adopt PostgreSQL for Primary Database"
42
+ EOF
43
+ exit 0
44
+ ;;
45
+ *) shift ;;
46
+ esac
47
+ done
48
+
49
+ if [[ -z "$TITLE" ]]; then
50
+ echo "Error: --title is required" >&2
51
+ exit 1
52
+ fi
53
+
54
+ REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
55
+ ADR_DIR="$REPO_ROOT/history/adr"
56
+ mkdir -p "$ADR_DIR"
57
+
58
+ # Check for template (try both locations)
59
+ TPL=""
60
+ if [[ -f "$REPO_ROOT/.specify/templates/adr-template.md" ]]; then
61
+ TPL="$REPO_ROOT/.specify/templates/adr-template.md"
62
+ elif [[ -f "$REPO_ROOT/templates/adr-template.md" ]]; then
63
+ TPL="$REPO_ROOT/templates/adr-template.md"
64
+ else
65
+ echo "Error: ADR template not found at .specify/templates/ or templates/" >&2
66
+ exit 1
67
+ fi
68
+
69
+ # next id
70
+ next_id() {
71
+ local max=0 base num
72
+ shopt -s nullglob
73
+ for f in "$ADR_DIR"/[0-9][0-9][0-9][0-9]-*.md; do
74
+ base=$(basename "$f")
75
+ num=${base%%-*}
76
+ if [[ $num =~ ^[0-9]{4}$ ]]; then
77
+ local n=$((10#$num))
78
+ (( n > max )) && max=$n
79
+ fi
80
+ done
81
+ printf "%04d" $((max+1))
82
+ }
83
+
84
+ slugify() {
85
+ echo "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g; s/-\{2,\}/-/g; s/^-//; s/-$//'
86
+ }
87
+
88
+ ID=$(next_id)
89
+ SLUG=$(slugify "$TITLE")
90
+ OUTFILE="$ADR_DIR/${ID}-${SLUG}.md"
91
+
92
+ # Simply copy the template (AI will fill placeholders)
93
+ cp "$TPL" "$OUTFILE"
94
+
95
+ ABS=$(cd "$(dirname "$OUTFILE")" && pwd)/$(basename "$OUTFILE")
96
+ if $JSON; then
97
+ printf '{"id":"%s","path":"%s","template":"%s"}\n' "$ID" "$ABS" "$(basename "$TPL")"
98
+ else
99
+ echo "✅ ADR template copied → $ABS"
100
+ echo "Note: AI agent should now fill in {{PLACEHOLDERS}}"
101
+ fi
.specify/scripts/bash/create-new-feature.sh ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ JSON_MODE=false
6
+ SHORT_NAME=""
7
+ BRANCH_NUMBER=""
8
+ ARGS=()
9
+ i=1
10
+ while [ $i -le $# ]; do
11
+ arg="${!i}"
12
+ case "$arg" in
13
+ --json)
14
+ JSON_MODE=true
15
+ ;;
16
+ --short-name)
17
+ if [ $((i + 1)) -gt $# ]; then
18
+ echo 'Error: --short-name requires a value' >&2
19
+ exit 1
20
+ fi
21
+ i=$((i + 1))
22
+ next_arg="${!i}"
23
+ # Check if the next argument is another option (starts with --)
24
+ if [[ "$next_arg" == --* ]]; then
25
+ echo 'Error: --short-name requires a value' >&2
26
+ exit 1
27
+ fi
28
+ SHORT_NAME="$next_arg"
29
+ ;;
30
+ --number)
31
+ if [ $((i + 1)) -gt $# ]; then
32
+ echo 'Error: --number requires a value' >&2
33
+ exit 1
34
+ fi
35
+ i=$((i + 1))
36
+ next_arg="${!i}"
37
+ if [[ "$next_arg" == --* ]]; then
38
+ echo 'Error: --number requires a value' >&2
39
+ exit 1
40
+ fi
41
+ BRANCH_NUMBER="$next_arg"
42
+ ;;
43
+ --help|-h)
44
+ echo "Usage: $0 [--json] [--short-name <name>] [--number N] <feature_description>"
45
+ echo ""
46
+ echo "Options:"
47
+ echo " --json Output in JSON format"
48
+ echo " --short-name <name> Provide a custom short name (2-4 words) for the branch"
49
+ echo " --number N Specify branch number manually (overrides auto-detection)"
50
+ echo " --help, -h Show this help message"
51
+ echo ""
52
+ echo "Examples:"
53
+ echo " $0 'Add user authentication system' --short-name 'user-auth'"
54
+ echo " $0 'Implement OAuth2 integration for API' --number 5"
55
+ exit 0
56
+ ;;
57
+ *)
58
+ ARGS+=("$arg")
59
+ ;;
60
+ esac
61
+ i=$((i + 1))
62
+ done
63
+
64
+ FEATURE_DESCRIPTION="${ARGS[*]}"
65
+ if [ -z "$FEATURE_DESCRIPTION" ]; then
66
+ echo "Usage: $0 [--json] [--short-name <name>] [--number N] <feature_description>" >&2
67
+ exit 1
68
+ fi
69
+
70
+ # Function to find the repository root by searching for existing project markers
71
+ find_repo_root() {
72
+ local dir="$1"
73
+ while [ "$dir" != "/" ]; do
74
+ if [ -d "$dir/.git" ] || [ -d "$dir/.specify" ]; then
75
+ echo "$dir"
76
+ return 0
77
+ fi
78
+ dir="$(dirname "$dir")"
79
+ done
80
+ return 1
81
+ }
82
+
83
+ # Function to get highest number from specs directory
84
+ get_highest_from_specs() {
85
+ local specs_dir="$1"
86
+ local highest=0
87
+
88
+ if [ -d "$specs_dir" ]; then
89
+ for dir in "$specs_dir"/*; do
90
+ [ -d "$dir" ] || continue
91
+ dirname=$(basename "$dir")
92
+ number=$(echo "$dirname" | grep -o '^[0-9]\+' || echo "0")
93
+ number=$((10#$number))
94
+ if [ "$number" -gt "$highest" ]; then
95
+ highest=$number
96
+ fi
97
+ done
98
+ fi
99
+
100
+ echo "$highest"
101
+ }
102
+
103
+ # Function to get highest number from git branches
104
+ get_highest_from_branches() {
105
+ local highest=0
106
+
107
+ # Get all branches (local and remote)
108
+ branches=$(git branch -a 2>/dev/null || echo "")
109
+
110
+ if [ -n "$branches" ]; then
111
+ while IFS= read -r branch; do
112
+ # Clean branch name: remove leading markers and remote prefixes
113
+ clean_branch=$(echo "$branch" | sed 's/^[* ]*//; s|^remotes/[^/]*/||')
114
+
115
+ # Extract feature number if branch matches pattern ###-*
116
+ if echo "$clean_branch" | grep -q '^[0-9]\{3\}-'; then
117
+ number=$(echo "$clean_branch" | grep -o '^[0-9]\{3\}' || echo "0")
118
+ number=$((10#$number))
119
+ if [ "$number" -gt "$highest" ]; then
120
+ highest=$number
121
+ fi
122
+ fi
123
+ done <<< "$branches"
124
+ fi
125
+
126
+ echo "$highest"
127
+ }
128
+
129
+ # Function to check existing branches (local and remote) and return next available number
130
+ check_existing_branches() {
131
+ local specs_dir="$1"
132
+
133
+ # Fetch all remotes to get latest branch info (suppress errors if no remotes)
134
+ git fetch --all --prune 2>/dev/null || true
135
+
136
+ # Get highest number from ALL branches (not just matching short name)
137
+ local highest_branch=$(get_highest_from_branches)
138
+
139
+ # Get highest number from ALL specs (not just matching short name)
140
+ local highest_spec=$(get_highest_from_specs "$specs_dir")
141
+
142
+ # Take the maximum of both
143
+ local max_num=$highest_branch
144
+ if [ "$highest_spec" -gt "$max_num" ]; then
145
+ max_num=$highest_spec
146
+ fi
147
+
148
+ # Return next number
149
+ echo $((max_num + 1))
150
+ }
151
+
152
+ # Function to clean and format a branch name
153
+ clean_branch_name() {
154
+ local name="$1"
155
+ echo "$name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//' | sed 's/-$//'
156
+ }
157
+
158
+ # Resolve repository root. Prefer git information when available, but fall back
159
+ # to searching for repository markers so the workflow still functions in repositories that
160
+ # were initialised with --no-git.
161
+ SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
162
+
163
+ if git rev-parse --show-toplevel >/dev/null 2>&1; then
164
+ REPO_ROOT=$(git rev-parse --show-toplevel)
165
+ HAS_GIT=true
166
+ else
167
+ REPO_ROOT="$(find_repo_root "$SCRIPT_DIR")"
168
+ if [ -z "$REPO_ROOT" ]; then
169
+ echo "Error: Could not determine repository root. Please run this script from within the repository." >&2
170
+ exit 1
171
+ fi
172
+ HAS_GIT=false
173
+ fi
174
+
175
+ cd "$REPO_ROOT"
176
+
177
+ SPECS_DIR="$REPO_ROOT/specs"
178
+ mkdir -p "$SPECS_DIR"
179
+
180
+ # Function to generate branch name with stop word filtering and length filtering
181
+ generate_branch_name() {
182
+ local description="$1"
183
+
184
+ # Common stop words to filter out
185
+ local stop_words="^(i|a|an|the|to|for|of|in|on|at|by|with|from|is|are|was|were|be|been|being|have|has|had|do|does|did|will|would|should|could|can|may|might|must|shall|this|that|these|those|my|your|our|their|want|need|add|get|set)$"
186
+
187
+ # Convert to lowercase and split into words
188
+ local clean_name=$(echo "$description" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/ /g')
189
+
190
+ # Filter words: remove stop words and words shorter than 3 chars (unless they're uppercase acronyms in original)
191
+ local meaningful_words=()
192
+ for word in $clean_name; do
193
+ # Skip empty words
194
+ [ -z "$word" ] && continue
195
+
196
+ # Keep words that are NOT stop words AND (length >= 3 OR are potential acronyms)
197
+ if ! echo "$word" | grep -qiE "$stop_words"; then
198
+ if [ ${#word} -ge 3 ]; then
199
+ meaningful_words+=("$word")
200
+ elif echo "$description" | grep -q "\b${word^^}\b"; then
201
+ # Keep short words if they appear as uppercase in original (likely acronyms)
202
+ meaningful_words+=("$word")
203
+ fi
204
+ fi
205
+ done
206
+
207
+ # If we have meaningful words, use first 3-4 of them
208
+ if [ ${#meaningful_words[@]} -gt 0 ]; then
209
+ local max_words=3
210
+ if [ ${#meaningful_words[@]} -eq 4 ]; then max_words=4; fi
211
+
212
+ local result=""
213
+ local count=0
214
+ for word in "${meaningful_words[@]}"; do
215
+ if [ $count -ge $max_words ]; then break; fi
216
+ if [ -n "$result" ]; then result="$result-"; fi
217
+ result="$result$word"
218
+ count=$((count + 1))
219
+ done
220
+ echo "$result"
221
+ else
222
+ # Fallback to original logic if no meaningful words found
223
+ local cleaned=$(clean_branch_name "$description")
224
+ echo "$cleaned" | tr '-' '\n' | grep -v '^$' | head -3 | tr '\n' '-' | sed 's/-$//'
225
+ fi
226
+ }
227
+
228
+ # Generate branch name
229
+ if [ -n "$SHORT_NAME" ]; then
230
+ # Use provided short name, just clean it up
231
+ BRANCH_SUFFIX=$(clean_branch_name "$SHORT_NAME")
232
+ else
233
+ # Generate from description with smart filtering
234
+ BRANCH_SUFFIX=$(generate_branch_name "$FEATURE_DESCRIPTION")
235
+ fi
236
+
237
+ # Determine branch number
238
+ if [ -z "$BRANCH_NUMBER" ]; then
239
+ if [ "$HAS_GIT" = true ]; then
240
+ # Check existing branches on remotes
241
+ BRANCH_NUMBER=$(check_existing_branches "$SPECS_DIR")
242
+ else
243
+ # Fall back to local directory check
244
+ HIGHEST=$(get_highest_from_specs "$SPECS_DIR")
245
+ BRANCH_NUMBER=$((HIGHEST + 1))
246
+ fi
247
+ fi
248
+
249
+ # Force base-10 interpretation to prevent octal conversion (e.g., 010 → 8 in octal, but should be 10 in decimal)
250
+ FEATURE_NUM=$(printf "%03d" "$((10#$BRANCH_NUMBER))")
251
+ BRANCH_NAME="${FEATURE_NUM}-${BRANCH_SUFFIX}"
252
+
253
+ # GitHub enforces a 244-byte limit on branch names
254
+ # Validate and truncate if necessary
255
+ MAX_BRANCH_LENGTH=244
256
+ if [ ${#BRANCH_NAME} -gt $MAX_BRANCH_LENGTH ]; then
257
+ # Calculate how much we need to trim from suffix
258
+ # Account for: feature number (3) + hyphen (1) = 4 chars
259
+ MAX_SUFFIX_LENGTH=$((MAX_BRANCH_LENGTH - 4))
260
+
261
+ # Truncate suffix at word boundary if possible
262
+ TRUNCATED_SUFFIX=$(echo "$BRANCH_SUFFIX" | cut -c1-$MAX_SUFFIX_LENGTH)
263
+ # Remove trailing hyphen if truncation created one
264
+ TRUNCATED_SUFFIX=$(echo "$TRUNCATED_SUFFIX" | sed 's/-$//')
265
+
266
+ ORIGINAL_BRANCH_NAME="$BRANCH_NAME"
267
+ BRANCH_NAME="${FEATURE_NUM}-${TRUNCATED_SUFFIX}"
268
+
269
+ >&2 echo "[specify] Warning: Branch name exceeded GitHub's 244-byte limit"
270
+ >&2 echo "[specify] Original: $ORIGINAL_BRANCH_NAME (${#ORIGINAL_BRANCH_NAME} bytes)"
271
+ >&2 echo "[specify] Truncated to: $BRANCH_NAME (${#BRANCH_NAME} bytes)"
272
+ fi
273
+
274
+ if [ "$HAS_GIT" = true ]; then
275
+ git checkout -b "$BRANCH_NAME"
276
+ else
277
+ >&2 echo "[specify] Warning: Git repository not detected; skipped branch creation for $BRANCH_NAME"
278
+ fi
279
+
280
+ FEATURE_DIR="$SPECS_DIR/$BRANCH_NAME"
281
+ mkdir -p "$FEATURE_DIR"
282
+
283
+ TEMPLATE="$REPO_ROOT/.specify/templates/spec-template.md"
284
+ SPEC_FILE="$FEATURE_DIR/spec.md"
285
+ if [ -f "$TEMPLATE" ]; then cp "$TEMPLATE" "$SPEC_FILE"; else touch "$SPEC_FILE"; fi
286
+
287
+ # Auto-create history/prompts/<branch-name>/ directory (same as specs/<branch-name>/)
288
+ # This keeps naming consistent across branch, specs, and prompts directories
289
+ PROMPTS_DIR="$REPO_ROOT/history/prompts/$BRANCH_NAME"
290
+ mkdir -p "$PROMPTS_DIR"
291
+
292
+ # Set the SPECIFY_FEATURE environment variable for the current session
293
+ export SPECIFY_FEATURE="$BRANCH_NAME"
294
+
295
+ if $JSON_MODE; then
296
+ printf '{"BRANCH_NAME":"%s","SPEC_FILE":"%s","FEATURE_NUM":"%s"}\n' "$BRANCH_NAME" "$SPEC_FILE" "$FEATURE_NUM"
297
+ else
298
+ echo "BRANCH_NAME: $BRANCH_NAME"
299
+ echo "SPEC_FILE: $SPEC_FILE"
300
+ echo "FEATURE_NUM: $FEATURE_NUM"
301
+ echo "SPECIFY_FEATURE environment variable set to: $BRANCH_NAME"
302
+ fi
.specify/scripts/bash/create-phr.sh ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # create-phr.sh - Create Prompt History Record (PHR) - Spec Kit Native
5
+ #
6
+ # Deterministic PHR location strategy:
7
+ # 1. Constitution stage:
8
+ # → history/prompts/constitution/
9
+ # → stage: constitution
10
+ # → naming: 0001-title.constitution.prompt.md
11
+ #
12
+ # 2. Feature stages (spec-specific work):
13
+ # → history/prompts/<spec-name>/
14
+ # → stages: spec, plan, tasks, red, green, refactor, explainer, misc
15
+ # → naming: 0001-title.spec.prompt.md
16
+ #
17
+ # 3. General stage (catch-all):
18
+ # → history/prompts/general/
19
+ # → stage: general
20
+ # → naming: 0001-title.general.prompt.md
21
+ #
22
+ # This script ONLY:
23
+ # 1. Creates the correct directory structure
24
+ # 2. Copies the template with {{PLACEHOLDERS}} intact
25
+ # 3. Returns metadata (id, path, context) for AI to fill in
26
+ #
27
+ # The calling AI agent is responsible for filling {{PLACEHOLDERS}}
28
+ #
29
+ # Usage:
30
+ # scripts/bash/create-phr.sh \
31
+ # --title "Setup authentication" \
32
+ # --stage architect \
33
+ # [--feature 001-auth] \
34
+ # [--json]
35
+
36
+ JSON_MODE=false
37
+ TITLE=""
38
+ STAGE=""
39
+ FEATURE=""
40
+
41
+ # Parse arguments
42
+ while [[ $# -gt 0 ]]; do
43
+ case "$1" in
44
+ --json) JSON_MODE=true; shift ;;
45
+ --title) TITLE=${2:-}; shift 2 ;;
46
+ --stage) STAGE=${2:-}; shift 2 ;;
47
+ --feature) FEATURE=${2:-}; shift 2 ;;
48
+ --help|-h)
49
+ cat <<EOF
50
+ Usage: $0 --title <title> --stage <stage> [options]
51
+
52
+ Required:
53
+ --title <text> Title for the PHR (used for filename)
54
+ --stage <stage> constitution|spec|plan|tasks|red|green|refactor|explainer|misc|general
55
+
56
+ Optional:
57
+ --feature <slug> Feature slug (e.g., 001-auth). Auto-detected from branch if omitted.
58
+ --json Output JSON with id, path, and context
59
+
60
+ Location Rules (all under history/prompts/):
61
+ - constitution → history/prompts/constitution/
62
+ - spec, plan, tasks, red, green, refactor, explainer, misc → history/prompts/<branch-name>/
63
+ - general → history/prompts/general/ (catch-all for non-feature work)
64
+
65
+ Output:
66
+ Creates PHR file with template placeholders ({{ID}}, {{TITLE}}, etc.)
67
+ AI agent must fill these placeholders after creation
68
+
69
+ Examples:
70
+ # Early-phase constitution work (no feature exists)
71
+ $0 --title "Define quality standards" --stage constitution --json
72
+
73
+ # Feature-specific implementation work
74
+ $0 --title "Implement login" --stage green --feature 001-auth --json
75
+ EOF
76
+ exit 0
77
+ ;;
78
+ *) shift ;;
79
+ esac
80
+ done
81
+
82
+ # Validation
83
+ if [[ -z "$TITLE" ]]; then
84
+ echo "Error: --title is required" >&2
85
+ exit 1
86
+ fi
87
+
88
+ if [[ -z "$STAGE" ]]; then
89
+ echo "Error: --stage is required" >&2
90
+ exit 1
91
+ fi
92
+
93
+ # Get repository root
94
+ REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
95
+ SPECS_DIR="$REPO_ROOT/specs"
96
+
97
+ # Check for template (try both locations)
98
+ TEMPLATE_PATH=""
99
+ if [[ -f "$REPO_ROOT/.specify/templates/phr-template.prompt.md" ]]; then
100
+ TEMPLATE_PATH="$REPO_ROOT/.specify/templates/phr-template.prompt.md"
101
+ elif [[ -f "$REPO_ROOT/templates/phr-template.prompt.md" ]]; then
102
+ TEMPLATE_PATH="$REPO_ROOT/templates/phr-template.prompt.md"
103
+ else
104
+ echo "Error: PHR template not found at .specify/templates/ or templates/" >&2
105
+ exit 1
106
+ fi
107
+
108
+ # Deterministic location logic based on STAGE
109
+ # New structure: all prompts go under history/prompts/ with subdirectories:
110
+ # - constitution/ for constitution prompts
111
+ # - <spec-name>/ for spec-specific prompts
112
+ # - general/ for general/catch-all prompts
113
+
114
+ case "$STAGE" in
115
+ constitution)
116
+ # Constitution prompts always go to history/prompts/constitution/
117
+ PROMPTS_DIR="$REPO_ROOT/history/prompts/constitution"
118
+ VALID_STAGES=("constitution")
119
+ CONTEXT="constitution"
120
+ ;;
121
+ spec|plan|tasks|red|green|refactor|explainer|misc)
122
+ # Feature-specific stages: require specs/ directory and feature context
123
+ if [[ ! -d "$SPECS_DIR" ]]; then
124
+ echo "Error: Feature stage '$STAGE' requires specs/ directory and a feature context" >&2
125
+ echo "Run /sp.feature first to create a feature, then try again" >&2
126
+ exit 1
127
+ fi
128
+
129
+ # Auto-detect feature if not specified
130
+ if [[ -z "$FEATURE" ]]; then
131
+ # Try to get from SPECIFY_FEATURE environment variable
132
+ if [[ -n "${SPECIFY_FEATURE:-}" ]]; then
133
+ FEATURE="$SPECIFY_FEATURE"
134
+ # Try to match current branch
135
+ elif git rev-parse --show-toplevel >/dev/null 2>&1; then
136
+ BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
137
+ if [[ -n "$BRANCH" && "$BRANCH" != "main" && "$BRANCH" != "master" ]]; then
138
+ # Check if branch name matches a feature directory
139
+ if [[ -d "$SPECS_DIR/$BRANCH" ]]; then
140
+ FEATURE="$BRANCH"
141
+ fi
142
+ fi
143
+ fi
144
+
145
+ # If still no feature, find the highest numbered feature
146
+ if [[ -z "$FEATURE" ]]; then
147
+ max_num=0
148
+ latest_feature=""
149
+ for dir in "$SPECS_DIR"/*; do
150
+ if [[ -d "$dir" ]]; then
151
+ dirname=$(basename "$dir")
152
+ if [[ "$dirname" =~ ^([0-9]{3})- ]]; then
153
+ num=$((10#${BASH_REMATCH[1]}))
154
+ if (( num > max_num )); then
155
+ max_num=$num
156
+ latest_feature="$dirname"
157
+ fi
158
+ fi
159
+ fi
160
+ done
161
+
162
+ if [[ -n "$latest_feature" ]]; then
163
+ FEATURE="$latest_feature"
164
+ else
165
+ echo "Error: No feature specified and no numbered features found in $SPECS_DIR" >&2
166
+ echo "Please specify --feature or create a feature directory first" >&2
167
+ exit 1
168
+ fi
169
+ fi
170
+ fi
171
+
172
+ # Validate feature exists
173
+ if [[ ! -d "$SPECS_DIR/$FEATURE" ]]; then
174
+ echo "Error: Feature directory not found: $SPECS_DIR/$FEATURE" >&2
175
+ echo "Available features:" >&2
176
+ ls -1 "$SPECS_DIR" 2>/dev/null | head -5 | sed 's/^/ - /' >&2
177
+ exit 1
178
+ fi
179
+
180
+ # Feature prompts go to history/prompts/<branch-name>/ (same as specs/<branch-name>/)
181
+ # This keeps naming consistent across branch, specs, and prompts directories
182
+ PROMPTS_DIR="$REPO_ROOT/history/prompts/$FEATURE"
183
+ VALID_STAGES=("spec" "plan" "tasks" "red" "green" "refactor" "explainer" "misc")
184
+ CONTEXT="feature"
185
+ ;;
186
+ general)
187
+ # General stage: catch-all that goes to history/prompts/general/
188
+ PROMPTS_DIR="$REPO_ROOT/history/prompts/general"
189
+ VALID_STAGES=("general")
190
+ CONTEXT="general"
191
+ ;;
192
+ *)
193
+ echo "Error: Unknown stage '$STAGE'" >&2
194
+ exit 1
195
+ ;;
196
+ esac
197
+
198
+ # Validate stage
199
+ stage_valid=false
200
+ for valid_stage in "${VALID_STAGES[@]}"; do
201
+ if [[ "$STAGE" == "$valid_stage" ]]; then
202
+ stage_valid=true
203
+ break
204
+ fi
205
+ done
206
+
207
+ if [[ "$stage_valid" == "false" ]]; then
208
+ echo "Error: Invalid stage '$STAGE' for $CONTEXT context" >&2
209
+ echo "Valid stages for $CONTEXT: ${VALID_STAGES[*]}" >&2
210
+ exit 1
211
+ fi
212
+
213
+ # Ensure prompts directory exists
214
+ mkdir -p "$PROMPTS_DIR"
215
+
216
+ # Helper: slugify
217
+ slugify() {
218
+ echo "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//' | sed 's/-$//'
219
+ }
220
+
221
+ # Get next ID (local to this directory)
222
+ get_next_id() {
223
+ local max_id=0
224
+ for file in "$PROMPTS_DIR"/[0-9][0-9][0-9][0-9]-*.prompt.md; do
225
+ [[ -e "$file" ]] || continue
226
+ local base=$(basename "$file")
227
+ local num=${base%%-*}
228
+ if [[ "$num" =~ ^[0-9]{4}$ ]]; then
229
+ local value=$((10#$num))
230
+ if (( value > max_id )); then
231
+ max_id=$value
232
+ fi
233
+ fi
234
+ done
235
+ printf '%04d' $((max_id + 1))
236
+ }
237
+
238
+ PHR_ID=$(get_next_id)
239
+ TITLE_SLUG=$(slugify "$TITLE")
240
+ STAGE_SLUG=$(slugify "$STAGE")
241
+
242
+ # Create filename with stage extension
243
+ OUTFILE="$PROMPTS_DIR/${PHR_ID}-${TITLE_SLUG}.${STAGE_SLUG}.prompt.md"
244
+
245
+ # Simply copy the template (AI will fill placeholders)
246
+ cp "$TEMPLATE_PATH" "$OUTFILE"
247
+
248
+ # Output results
249
+ ABS_PATH=$(cd "$(dirname "$OUTFILE")" && pwd)/$(basename "$OUTFILE")
250
+ if $JSON_MODE; then
251
+ printf '{"id":"%s","path":"%s","context":"%s","stage":"%s","feature":"%s","template":"%s"}\n' \
252
+ "$PHR_ID" "$ABS_PATH" "$CONTEXT" "$STAGE" "${FEATURE:-none}" "$(basename "$TEMPLATE_PATH")"
253
+ else
254
+ echo "✅ PHR template copied → $ABS_PATH"
255
+ echo "Note: AI agent should now fill in {{PLACEHOLDERS}}"
256
+ fi
.specify/scripts/bash/setup-plan.sh ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ # Parse command line arguments
6
+ JSON_MODE=false
7
+ ARGS=()
8
+
9
+ for arg in "$@"; do
10
+ case "$arg" in
11
+ --json)
12
+ JSON_MODE=true
13
+ ;;
14
+ --help|-h)
15
+ echo "Usage: $0 [--json]"
16
+ echo " --json Output results in JSON format"
17
+ echo " --help Show this help message"
18
+ exit 0
19
+ ;;
20
+ *)
21
+ ARGS+=("$arg")
22
+ ;;
23
+ esac
24
+ done
25
+
26
+ # Get script directory and load common functions
27
+ SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
28
+ source "$SCRIPT_DIR/common.sh"
29
+
30
+ # Get all paths and variables from common functions
31
+ eval $(get_feature_paths)
32
+
33
+ # Check if we're on a proper feature branch (only for git repos)
34
+ check_feature_branch "$CURRENT_BRANCH" "$HAS_GIT" || exit 1
35
+
36
+ # Ensure the feature directory exists
37
+ mkdir -p "$FEATURE_DIR"
38
+
39
+ # Copy plan template if it exists
40
+ TEMPLATE="$REPO_ROOT/.specify/templates/plan-template.md"
41
+ if [[ -f "$TEMPLATE" ]]; then
42
+ cp "$TEMPLATE" "$IMPL_PLAN"
43
+ echo "Copied plan template to $IMPL_PLAN"
44
+ else
45
+ echo "Warning: Plan template not found at $TEMPLATE"
46
+ # Create a basic plan file if template doesn't exist
47
+ touch "$IMPL_PLAN"
48
+ fi
49
+
50
+ # Output results
51
+ if $JSON_MODE; then
52
+ printf '{"FEATURE_SPEC":"%s","IMPL_PLAN":"%s","SPECS_DIR":"%s","BRANCH":"%s","HAS_GIT":"%s"}\n' \
53
+ "$FEATURE_SPEC" "$IMPL_PLAN" "$FEATURE_DIR" "$CURRENT_BRANCH" "$HAS_GIT"
54
+ else
55
+ echo "FEATURE_SPEC: $FEATURE_SPEC"
56
+ echo "IMPL_PLAN: $IMPL_PLAN"
57
+ echo "SPECS_DIR: $FEATURE_DIR"
58
+ echo "BRANCH: $CURRENT_BRANCH"
59
+ echo "HAS_GIT: $HAS_GIT"
60
+ fi
61
+
.specify/scripts/bash/update-agent-context.sh ADDED
@@ -0,0 +1,799 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ # Update agent context files with information from plan.md
4
+ #
5
+ # This script maintains AI agent context files by parsing feature specifications
6
+ # and updating agent-specific configuration files with project information.
7
+ #
8
+ # MAIN FUNCTIONS:
9
+ # 1. Environment Validation
10
+ # - Verifies git repository structure and branch information
11
+ # - Checks for required plan.md files and templates
12
+ # - Validates file permissions and accessibility
13
+ #
14
+ # 2. Plan Data Extraction
15
+ # - Parses plan.md files to extract project metadata
16
+ # - Identifies language/version, frameworks, databases, and project types
17
+ # - Handles missing or incomplete specification data gracefully
18
+ #
19
+ # 3. Agent File Management
20
+ # - Creates new agent context files from templates when needed
21
+ # - Updates existing agent files with new project information
22
+ # - Preserves manual additions and custom configurations
23
+ # - Supports multiple AI agent formats and directory structures
24
+ #
25
+ # 4. Content Generation
26
+ # - Generates language-specific build/test commands
27
+ # - Creates appropriate project directory structures
28
+ # - Updates technology stacks and recent changes sections
29
+ # - Maintains consistent formatting and timestamps
30
+ #
31
+ # 5. Multi-Agent Support
32
+ # - Handles agent-specific file paths and naming conventions
33
+ # - Supports: Claude, Gemini, Copilot, Cursor, Qwen, opencode, Codex, Windsurf, Kilo Code, Auggie CLI, Roo Code, CodeBuddy CLI, Qoder CLI, Amp, SHAI, or Amazon Q Developer CLI
34
+ # - Can update single agents or all existing agent files
35
+ # - Creates default Claude file if no agent files exist
36
+ #
37
+ # Usage: ./update-agent-context.sh [agent_type]
38
+ # Agent types: claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|shai|q|bob|qoder
39
+ # Leave empty to update all existing agent files
40
+
41
+ set -e
42
+
43
+ # Enable strict error handling
44
+ set -u
45
+ set -o pipefail
46
+
47
+ #==============================================================================
48
+ # Configuration and Global Variables
49
+ #==============================================================================
50
+
51
+ # Get script directory and load common functions
52
+ SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
53
+ source "$SCRIPT_DIR/common.sh"
54
+
55
+ # Get all paths and variables from common functions
56
+ eval $(get_feature_paths)
57
+
58
+ NEW_PLAN="$IMPL_PLAN" # Alias for compatibility with existing code
59
+ AGENT_TYPE="${1:-}"
60
+
61
+ # Agent-specific file paths
62
+ CLAUDE_FILE="$REPO_ROOT/CLAUDE.md"
63
+ GEMINI_FILE="$REPO_ROOT/GEMINI.md"
64
+ COPILOT_FILE="$REPO_ROOT/.github/agents/copilot-instructions.md"
65
+ CURSOR_FILE="$REPO_ROOT/.cursor/rules/specify-rules.mdc"
66
+ QWEN_FILE="$REPO_ROOT/QWEN.md"
67
+ AGENTS_FILE="$REPO_ROOT/AGENTS.md"
68
+ WINDSURF_FILE="$REPO_ROOT/.windsurf/rules/specify-rules.md"
69
+ KILOCODE_FILE="$REPO_ROOT/.kilocode/rules/specify-rules.md"
70
+ AUGGIE_FILE="$REPO_ROOT/.augment/rules/specify-rules.md"
71
+ ROO_FILE="$REPO_ROOT/.roo/rules/specify-rules.md"
72
+ CODEBUDDY_FILE="$REPO_ROOT/CODEBUDDY.md"
73
+ QODER_FILE="$REPO_ROOT/QODER.md"
74
+ AMP_FILE="$REPO_ROOT/AGENTS.md"
75
+ SHAI_FILE="$REPO_ROOT/SHAI.md"
76
+ Q_FILE="$REPO_ROOT/AGENTS.md"
77
+ BOB_FILE="$REPO_ROOT/AGENTS.md"
78
+
79
+ # Template file
80
+ TEMPLATE_FILE="$REPO_ROOT/.specify/templates/agent-file-template.md"
81
+
82
+ # Global variables for parsed plan data
83
+ NEW_LANG=""
84
+ NEW_FRAMEWORK=""
85
+ NEW_DB=""
86
+ NEW_PROJECT_TYPE=""
87
+
88
+ #==============================================================================
89
+ # Utility Functions
90
+ #==============================================================================
91
+
92
+ log_info() {
93
+ echo "INFO: $1"
94
+ }
95
+
96
+ log_success() {
97
+ echo "✓ $1"
98
+ }
99
+
100
+ log_error() {
101
+ echo "ERROR: $1" >&2
102
+ }
103
+
104
+ log_warning() {
105
+ echo "WARNING: $1" >&2
106
+ }
107
+
108
+ # Cleanup function for temporary files
109
+ cleanup() {
110
+ local exit_code=$?
111
+ rm -f /tmp/agent_update_*_$$
112
+ rm -f /tmp/manual_additions_$$
113
+ exit $exit_code
114
+ }
115
+
116
+ # Set up cleanup trap
117
+ trap cleanup EXIT INT TERM
118
+
119
+ #==============================================================================
120
+ # Validation Functions
121
+ #==============================================================================
122
+
123
+ validate_environment() {
124
+ # Check if we have a current branch/feature (git or non-git)
125
+ if [[ -z "$CURRENT_BRANCH" ]]; then
126
+ log_error "Unable to determine current feature"
127
+ if [[ "$HAS_GIT" == "true" ]]; then
128
+ log_info "Make sure you're on a feature branch"
129
+ else
130
+ log_info "Set SPECIFY_FEATURE environment variable or create a feature first"
131
+ fi
132
+ exit 1
133
+ fi
134
+
135
+ # Check if plan.md exists
136
+ if [[ ! -f "$NEW_PLAN" ]]; then
137
+ log_error "No plan.md found at $NEW_PLAN"
138
+ log_info "Make sure you're working on a feature with a corresponding spec directory"
139
+ if [[ "$HAS_GIT" != "true" ]]; then
140
+ log_info "Use: export SPECIFY_FEATURE=your-feature-name or create a new feature first"
141
+ fi
142
+ exit 1
143
+ fi
144
+
145
+ # Check if template exists (needed for new files)
146
+ if [[ ! -f "$TEMPLATE_FILE" ]]; then
147
+ log_warning "Template file not found at $TEMPLATE_FILE"
148
+ log_warning "Creating new agent files will fail"
149
+ fi
150
+ }
151
+
152
+ #==============================================================================
153
+ # Plan Parsing Functions
154
+ #==============================================================================
155
+
156
+ extract_plan_field() {
157
+ local field_pattern="$1"
158
+ local plan_file="$2"
159
+
160
+ grep "^\*\*${field_pattern}\*\*: " "$plan_file" 2>/dev/null | \
161
+ head -1 | \
162
+ sed "s|^\*\*${field_pattern}\*\*: ||" | \
163
+ sed 's/^[ \t]*//;s/[ \t]*$//' | \
164
+ grep -v "NEEDS CLARIFICATION" | \
165
+ grep -v "^N/A$" || echo ""
166
+ }
167
+
168
+ parse_plan_data() {
169
+ local plan_file="$1"
170
+
171
+ if [[ ! -f "$plan_file" ]]; then
172
+ log_error "Plan file not found: $plan_file"
173
+ return 1
174
+ fi
175
+
176
+ if [[ ! -r "$plan_file" ]]; then
177
+ log_error "Plan file is not readable: $plan_file"
178
+ return 1
179
+ fi
180
+
181
+ log_info "Parsing plan data from $plan_file"
182
+
183
+ NEW_LANG=$(extract_plan_field "Language/Version" "$plan_file")
184
+ NEW_FRAMEWORK=$(extract_plan_field "Primary Dependencies" "$plan_file")
185
+ NEW_DB=$(extract_plan_field "Storage" "$plan_file")
186
+ NEW_PROJECT_TYPE=$(extract_plan_field "Project Type" "$plan_file")
187
+
188
+ # Log what we found
189
+ if [[ -n "$NEW_LANG" ]]; then
190
+ log_info "Found language: $NEW_LANG"
191
+ else
192
+ log_warning "No language information found in plan"
193
+ fi
194
+
195
+ if [[ -n "$NEW_FRAMEWORK" ]]; then
196
+ log_info "Found framework: $NEW_FRAMEWORK"
197
+ fi
198
+
199
+ if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]]; then
200
+ log_info "Found database: $NEW_DB"
201
+ fi
202
+
203
+ if [[ -n "$NEW_PROJECT_TYPE" ]]; then
204
+ log_info "Found project type: $NEW_PROJECT_TYPE"
205
+ fi
206
+ }
207
+
208
+ format_technology_stack() {
209
+ local lang="$1"
210
+ local framework="$2"
211
+ local parts=()
212
+
213
+ # Add non-empty parts
214
+ [[ -n "$lang" && "$lang" != "NEEDS CLARIFICATION" ]] && parts+=("$lang")
215
+ [[ -n "$framework" && "$framework" != "NEEDS CLARIFICATION" && "$framework" != "N/A" ]] && parts+=("$framework")
216
+
217
+ # Join with proper formatting
218
+ if [[ ${#parts[@]} -eq 0 ]]; then
219
+ echo ""
220
+ elif [[ ${#parts[@]} -eq 1 ]]; then
221
+ echo "${parts[0]}"
222
+ else
223
+ # Join multiple parts with " + "
224
+ local result="${parts[0]}"
225
+ for ((i=1; i<${#parts[@]}; i++)); do
226
+ result="$result + ${parts[i]}"
227
+ done
228
+ echo "$result"
229
+ fi
230
+ }
231
+
232
+ #==============================================================================
233
+ # Template and Content Generation Functions
234
+ #==============================================================================
235
+
236
+ get_project_structure() {
237
+ local project_type="$1"
238
+
239
+ if [[ "$project_type" == *"web"* ]]; then
240
+ echo "backend/\\nfrontend/\\ntests/"
241
+ else
242
+ echo "src/\\ntests/"
243
+ fi
244
+ }
245
+
246
+ get_commands_for_language() {
247
+ local lang="$1"
248
+
249
+ case "$lang" in
250
+ *"Python"*)
251
+ echo "cd src && pytest && ruff check ."
252
+ ;;
253
+ *"Rust"*)
254
+ echo "cargo test && cargo clippy"
255
+ ;;
256
+ *"JavaScript"*|*"TypeScript"*)
257
+ echo "npm test \\&\\& npm run lint"
258
+ ;;
259
+ *)
260
+ echo "# Add commands for $lang"
261
+ ;;
262
+ esac
263
+ }
264
+
265
+ get_language_conventions() {
266
+ local lang="$1"
267
+ echo "$lang: Follow standard conventions"
268
+ }
269
+
270
+ create_new_agent_file() {
271
+ local target_file="$1"
272
+ local temp_file="$2"
273
+ local project_name="$3"
274
+ local current_date="$4"
275
+
276
+ if [[ ! -f "$TEMPLATE_FILE" ]]; then
277
+ log_error "Template not found at $TEMPLATE_FILE"
278
+ return 1
279
+ fi
280
+
281
+ if [[ ! -r "$TEMPLATE_FILE" ]]; then
282
+ log_error "Template file is not readable: $TEMPLATE_FILE"
283
+ return 1
284
+ fi
285
+
286
+ log_info "Creating new agent context file from template..."
287
+
288
+ if ! cp "$TEMPLATE_FILE" "$temp_file"; then
289
+ log_error "Failed to copy template file"
290
+ return 1
291
+ fi
292
+
293
+ # Replace template placeholders
294
+ local project_structure
295
+ project_structure=$(get_project_structure "$NEW_PROJECT_TYPE")
296
+
297
+ local commands
298
+ commands=$(get_commands_for_language "$NEW_LANG")
299
+
300
+ local language_conventions
301
+ language_conventions=$(get_language_conventions "$NEW_LANG")
302
+
303
+ # Perform substitutions with error checking using safer approach
304
+ # Escape special characters for sed by using a different delimiter or escaping
305
+ local escaped_lang=$(printf '%s\n' "$NEW_LANG" | sed 's/[\[\.*^$()+{}|]/\\&/g')
306
+ local escaped_framework=$(printf '%s\n' "$NEW_FRAMEWORK" | sed 's/[\[\.*^$()+{}|]/\\&/g')
307
+ local escaped_branch=$(printf '%s\n' "$CURRENT_BRANCH" | sed 's/[\[\.*^$()+{}|]/\\&/g')
308
+
309
+ # Build technology stack and recent change strings conditionally
310
+ local tech_stack
311
+ if [[ -n "$escaped_lang" && -n "$escaped_framework" ]]; then
312
+ tech_stack="- $escaped_lang + $escaped_framework ($escaped_branch)"
313
+ elif [[ -n "$escaped_lang" ]]; then
314
+ tech_stack="- $escaped_lang ($escaped_branch)"
315
+ elif [[ -n "$escaped_framework" ]]; then
316
+ tech_stack="- $escaped_framework ($escaped_branch)"
317
+ else
318
+ tech_stack="- ($escaped_branch)"
319
+ fi
320
+
321
+ local recent_change
322
+ if [[ -n "$escaped_lang" && -n "$escaped_framework" ]]; then
323
+ recent_change="- $escaped_branch: Added $escaped_lang + $escaped_framework"
324
+ elif [[ -n "$escaped_lang" ]]; then
325
+ recent_change="- $escaped_branch: Added $escaped_lang"
326
+ elif [[ -n "$escaped_framework" ]]; then
327
+ recent_change="- $escaped_branch: Added $escaped_framework"
328
+ else
329
+ recent_change="- $escaped_branch: Added"
330
+ fi
331
+
332
+ local substitutions=(
333
+ "s|\[PROJECT NAME\]|$project_name|"
334
+ "s|\[DATE\]|$current_date|"
335
+ "s|\[EXTRACTED FROM ALL PLAN.MD FILES\]|$tech_stack|"
336
+ "s|\[ACTUAL STRUCTURE FROM PLANS\]|$project_structure|g"
337
+ "s|\[ONLY COMMANDS FOR ACTIVE TECHNOLOGIES\]|$commands|"
338
+ "s|\[LANGUAGE-SPECIFIC, ONLY FOR LANGUAGES IN USE\]|$language_conventions|"
339
+ "s|\[LAST 3 FEATURES AND WHAT THEY ADDED\]|$recent_change|"
340
+ )
341
+
342
+ for substitution in "${substitutions[@]}"; do
343
+ if ! sed -i.bak -e "$substitution" "$temp_file"; then
344
+ log_error "Failed to perform substitution: $substitution"
345
+ rm -f "$temp_file" "$temp_file.bak"
346
+ return 1
347
+ fi
348
+ done
349
+
350
+ # Convert \n sequences to actual newlines
351
+ newline=$(printf '\n')
352
+ sed -i.bak2 "s/\\\\n/${newline}/g" "$temp_file"
353
+
354
+ # Clean up backup files
355
+ rm -f "$temp_file.bak" "$temp_file.bak2"
356
+
357
+ return 0
358
+ }
359
+
360
+
361
+
362
+
363
+ update_existing_agent_file() {
364
+ local target_file="$1"
365
+ local current_date="$2"
366
+
367
+ log_info "Updating existing agent context file..."
368
+
369
+ # Use a single temporary file for atomic update
370
+ local temp_file
371
+ temp_file=$(mktemp) || {
372
+ log_error "Failed to create temporary file"
373
+ return 1
374
+ }
375
+
376
+ # Process the file in one pass
377
+ local tech_stack=$(format_technology_stack "$NEW_LANG" "$NEW_FRAMEWORK")
378
+ local new_tech_entries=()
379
+ local new_change_entry=""
380
+
381
+ # Prepare new technology entries
382
+ if [[ -n "$tech_stack" ]] && ! grep -q "$tech_stack" "$target_file"; then
383
+ new_tech_entries+=("- $tech_stack ($CURRENT_BRANCH)")
384
+ fi
385
+
386
+ if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]] && [[ "$NEW_DB" != "NEEDS CLARIFICATION" ]] && ! grep -q "$NEW_DB" "$target_file"; then
387
+ new_tech_entries+=("- $NEW_DB ($CURRENT_BRANCH)")
388
+ fi
389
+
390
+ # Prepare new change entry
391
+ if [[ -n "$tech_stack" ]]; then
392
+ new_change_entry="- $CURRENT_BRANCH: Added $tech_stack"
393
+ elif [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]] && [[ "$NEW_DB" != "NEEDS CLARIFICATION" ]]; then
394
+ new_change_entry="- $CURRENT_BRANCH: Added $NEW_DB"
395
+ fi
396
+
397
+ # Check if sections exist in the file
398
+ local has_active_technologies=0
399
+ local has_recent_changes=0
400
+
401
+ if grep -q "^## Active Technologies" "$target_file" 2>/dev/null; then
402
+ has_active_technologies=1
403
+ fi
404
+
405
+ if grep -q "^## Recent Changes" "$target_file" 2>/dev/null; then
406
+ has_recent_changes=1
407
+ fi
408
+
409
+ # Process file line by line
410
+ local in_tech_section=false
411
+ local in_changes_section=false
412
+ local tech_entries_added=false
413
+ local changes_entries_added=false
414
+ local existing_changes_count=0
415
+ local file_ended=false
416
+
417
+ while IFS= read -r line || [[ -n "$line" ]]; do
418
+ # Handle Active Technologies section
419
+ if [[ "$line" == "## Active Technologies" ]]; then
420
+ echo "$line" >> "$temp_file"
421
+ in_tech_section=true
422
+ continue
423
+ elif [[ $in_tech_section == true ]] && [[ "$line" =~ ^##[[:space:]] ]]; then
424
+ # Add new tech entries before closing the section
425
+ if [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
426
+ printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
427
+ tech_entries_added=true
428
+ fi
429
+ echo "$line" >> "$temp_file"
430
+ in_tech_section=false
431
+ continue
432
+ elif [[ $in_tech_section == true ]] && [[ -z "$line" ]]; then
433
+ # Add new tech entries before empty line in tech section
434
+ if [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
435
+ printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
436
+ tech_entries_added=true
437
+ fi
438
+ echo "$line" >> "$temp_file"
439
+ continue
440
+ fi
441
+
442
+ # Handle Recent Changes section
443
+ if [[ "$line" == "## Recent Changes" ]]; then
444
+ echo "$line" >> "$temp_file"
445
+ # Add new change entry right after the heading
446
+ if [[ -n "$new_change_entry" ]]; then
447
+ echo "$new_change_entry" >> "$temp_file"
448
+ fi
449
+ in_changes_section=true
450
+ changes_entries_added=true
451
+ continue
452
+ elif [[ $in_changes_section == true ]] && [[ "$line" =~ ^##[[:space:]] ]]; then
453
+ echo "$line" >> "$temp_file"
454
+ in_changes_section=false
455
+ continue
456
+ elif [[ $in_changes_section == true ]] && [[ "$line" == "- "* ]]; then
457
+ # Keep only first 2 existing changes
458
+ if [[ $existing_changes_count -lt 2 ]]; then
459
+ echo "$line" >> "$temp_file"
460
+ ((existing_changes_count++))
461
+ fi
462
+ continue
463
+ fi
464
+
465
+ # Update timestamp
466
+ if [[ "$line" =~ \*\*Last\ updated\*\*:.*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ]]; then
467
+ echo "$line" | sed "s/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/$current_date/" >> "$temp_file"
468
+ else
469
+ echo "$line" >> "$temp_file"
470
+ fi
471
+ done < "$target_file"
472
+
473
+ # Post-loop check: if we're still in the Active Technologies section and haven't added new entries
474
+ if [[ $in_tech_section == true ]] && [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
475
+ printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
476
+ tech_entries_added=true
477
+ fi
478
+
479
+ # If sections don't exist, add them at the end of the file
480
+ if [[ $has_active_technologies -eq 0 ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
481
+ echo "" >> "$temp_file"
482
+ echo "## Active Technologies" >> "$temp_file"
483
+ printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
484
+ tech_entries_added=true
485
+ fi
486
+
487
+ if [[ $has_recent_changes -eq 0 ]] && [[ -n "$new_change_entry" ]]; then
488
+ echo "" >> "$temp_file"
489
+ echo "## Recent Changes" >> "$temp_file"
490
+ echo "$new_change_entry" >> "$temp_file"
491
+ changes_entries_added=true
492
+ fi
493
+
494
+ # Move temp file to target atomically
495
+ if ! mv "$temp_file" "$target_file"; then
496
+ log_error "Failed to update target file"
497
+ rm -f "$temp_file"
498
+ return 1
499
+ fi
500
+
501
+ return 0
502
+ }
503
+ #==============================================================================
504
+ # Main Agent File Update Function
505
+ #==============================================================================
506
+
507
+ update_agent_file() {
508
+ local target_file="$1"
509
+ local agent_name="$2"
510
+
511
+ if [[ -z "$target_file" ]] || [[ -z "$agent_name" ]]; then
512
+ log_error "update_agent_file requires target_file and agent_name parameters"
513
+ return 1
514
+ fi
515
+
516
+ log_info "Updating $agent_name context file: $target_file"
517
+
518
+ local project_name
519
+ project_name=$(basename "$REPO_ROOT")
520
+ local current_date
521
+ current_date=$(date +%Y-%m-%d)
522
+
523
+ # Create directory if it doesn't exist
524
+ local target_dir
525
+ target_dir=$(dirname "$target_file")
526
+ if [[ ! -d "$target_dir" ]]; then
527
+ if ! mkdir -p "$target_dir"; then
528
+ log_error "Failed to create directory: $target_dir"
529
+ return 1
530
+ fi
531
+ fi
532
+
533
+ if [[ ! -f "$target_file" ]]; then
534
+ # Create new file from template
535
+ local temp_file
536
+ temp_file=$(mktemp) || {
537
+ log_error "Failed to create temporary file"
538
+ return 1
539
+ }
540
+
541
+ if create_new_agent_file "$target_file" "$temp_file" "$project_name" "$current_date"; then
542
+ if mv "$temp_file" "$target_file"; then
543
+ log_success "Created new $agent_name context file"
544
+ else
545
+ log_error "Failed to move temporary file to $target_file"
546
+ rm -f "$temp_file"
547
+ return 1
548
+ fi
549
+ else
550
+ log_error "Failed to create new agent file"
551
+ rm -f "$temp_file"
552
+ return 1
553
+ fi
554
+ else
555
+ # Update existing file
556
+ if [[ ! -r "$target_file" ]]; then
557
+ log_error "Cannot read existing file: $target_file"
558
+ return 1
559
+ fi
560
+
561
+ if [[ ! -w "$target_file" ]]; then
562
+ log_error "Cannot write to existing file: $target_file"
563
+ return 1
564
+ fi
565
+
566
+ if update_existing_agent_file "$target_file" "$current_date"; then
567
+ log_success "Updated existing $agent_name context file"
568
+ else
569
+ log_error "Failed to update existing agent file"
570
+ return 1
571
+ fi
572
+ fi
573
+
574
+ return 0
575
+ }
576
+
577
+ #==============================================================================
578
+ # Agent Selection and Processing
579
+ #==============================================================================
580
+
581
+ update_specific_agent() {
582
+ local agent_type="$1"
583
+
584
+ case "$agent_type" in
585
+ claude)
586
+ update_agent_file "$CLAUDE_FILE" "Claude Code"
587
+ ;;
588
+ gemini)
589
+ update_agent_file "$GEMINI_FILE" "Gemini CLI"
590
+ ;;
591
+ copilot)
592
+ update_agent_file "$COPILOT_FILE" "GitHub Copilot"
593
+ ;;
594
+ cursor-agent)
595
+ update_agent_file "$CURSOR_FILE" "Cursor IDE"
596
+ ;;
597
+ qwen)
598
+ update_agent_file "$QWEN_FILE" "Qwen Code"
599
+ ;;
600
+ opencode)
601
+ update_agent_file "$AGENTS_FILE" "opencode"
602
+ ;;
603
+ codex)
604
+ update_agent_file "$AGENTS_FILE" "Codex CLI"
605
+ ;;
606
+ windsurf)
607
+ update_agent_file "$WINDSURF_FILE" "Windsurf"
608
+ ;;
609
+ kilocode)
610
+ update_agent_file "$KILOCODE_FILE" "Kilo Code"
611
+ ;;
612
+ auggie)
613
+ update_agent_file "$AUGGIE_FILE" "Auggie CLI"
614
+ ;;
615
+ roo)
616
+ update_agent_file "$ROO_FILE" "Roo Code"
617
+ ;;
618
+ codebuddy)
619
+ update_agent_file "$CODEBUDDY_FILE" "CodeBuddy CLI"
620
+ ;;
621
+ qoder)
622
+ update_agent_file "$QODER_FILE" "Qoder CLI"
623
+ ;;
624
+ amp)
625
+ update_agent_file "$AMP_FILE" "Amp"
626
+ ;;
627
+ shai)
628
+ update_agent_file "$SHAI_FILE" "SHAI"
629
+ ;;
630
+ q)
631
+ update_agent_file "$Q_FILE" "Amazon Q Developer CLI"
632
+ ;;
633
+ bob)
634
+ update_agent_file "$BOB_FILE" "IBM Bob"
635
+ ;;
636
+ *)
637
+ log_error "Unknown agent type '$agent_type'"
638
+ log_error "Expected: claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|roo|amp|shai|q|bob|qoder"
639
+ exit 1
640
+ ;;
641
+ esac
642
+ }
643
+
644
+ update_all_existing_agents() {
645
+ local found_agent=false
646
+
647
+ # Check each possible agent file and update if it exists
648
+ if [[ -f "$CLAUDE_FILE" ]]; then
649
+ update_agent_file "$CLAUDE_FILE" "Claude Code"
650
+ found_agent=true
651
+ fi
652
+
653
+ if [[ -f "$GEMINI_FILE" ]]; then
654
+ update_agent_file "$GEMINI_FILE" "Gemini CLI"
655
+ found_agent=true
656
+ fi
657
+
658
+ if [[ -f "$COPILOT_FILE" ]]; then
659
+ update_agent_file "$COPILOT_FILE" "GitHub Copilot"
660
+ found_agent=true
661
+ fi
662
+
663
+ if [[ -f "$CURSOR_FILE" ]]; then
664
+ update_agent_file "$CURSOR_FILE" "Cursor IDE"
665
+ found_agent=true
666
+ fi
667
+
668
+ if [[ -f "$QWEN_FILE" ]]; then
669
+ update_agent_file "$QWEN_FILE" "Qwen Code"
670
+ found_agent=true
671
+ fi
672
+
673
+ if [[ -f "$AGENTS_FILE" ]]; then
674
+ update_agent_file "$AGENTS_FILE" "Codex/opencode"
675
+ found_agent=true
676
+ fi
677
+
678
+ if [[ -f "$WINDSURF_FILE" ]]; then
679
+ update_agent_file "$WINDSURF_FILE" "Windsurf"
680
+ found_agent=true
681
+ fi
682
+
683
+ if [[ -f "$KILOCODE_FILE" ]]; then
684
+ update_agent_file "$KILOCODE_FILE" "Kilo Code"
685
+ found_agent=true
686
+ fi
687
+
688
+ if [[ -f "$AUGGIE_FILE" ]]; then
689
+ update_agent_file "$AUGGIE_FILE" "Auggie CLI"
690
+ found_agent=true
691
+ fi
692
+
693
+ if [[ -f "$ROO_FILE" ]]; then
694
+ update_agent_file "$ROO_FILE" "Roo Code"
695
+ found_agent=true
696
+ fi
697
+
698
+ if [[ -f "$CODEBUDDY_FILE" ]]; then
699
+ update_agent_file "$CODEBUDDY_FILE" "CodeBuddy CLI"
700
+ found_agent=true
701
+ fi
702
+
703
+ if [[ -f "$SHAI_FILE" ]]; then
704
+ update_agent_file "$SHAI_FILE" "SHAI"
705
+ found_agent=true
706
+ fi
707
+
708
+ if [[ -f "$QODER_FILE" ]]; then
709
+ update_agent_file "$QODER_FILE" "Qoder CLI"
710
+ found_agent=true
711
+ fi
712
+
713
+ if [[ -f "$Q_FILE" ]]; then
714
+ update_agent_file "$Q_FILE" "Amazon Q Developer CLI"
715
+ found_agent=true
716
+ fi
717
+
718
+ if [[ -f "$BOB_FILE" ]]; then
719
+ update_agent_file "$BOB_FILE" "IBM Bob"
720
+ found_agent=true
721
+ fi
722
+
723
+ # If no agent files exist, create a default Claude file
724
+ if [[ "$found_agent" == false ]]; then
725
+ log_info "No existing agent files found, creating default Claude file..."
726
+ update_agent_file "$CLAUDE_FILE" "Claude Code"
727
+ fi
728
+ }
729
+ print_summary() {
730
+ echo
731
+ log_info "Summary of changes:"
732
+
733
+ if [[ -n "$NEW_LANG" ]]; then
734
+ echo " - Added language: $NEW_LANG"
735
+ fi
736
+
737
+ if [[ -n "$NEW_FRAMEWORK" ]]; then
738
+ echo " - Added framework: $NEW_FRAMEWORK"
739
+ fi
740
+
741
+ if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]]; then
742
+ echo " - Added database: $NEW_DB"
743
+ fi
744
+
745
+ echo
746
+
747
+ log_info "Usage: $0 [claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|codebuddy|shai|q|bob|qoder]"
748
+ }
749
+
750
+ #==============================================================================
751
+ # Main Execution
752
+ #==============================================================================
753
+
754
+ main() {
755
+ # Validate environment before proceeding
756
+ validate_environment
757
+
758
+ log_info "=== Updating agent context files for feature $CURRENT_BRANCH ==="
759
+
760
+ # Parse the plan file to extract project information
761
+ if ! parse_plan_data "$NEW_PLAN"; then
762
+ log_error "Failed to parse plan data"
763
+ exit 1
764
+ fi
765
+
766
+ # Process based on agent type argument
767
+ local success=true
768
+
769
+ if [[ -z "$AGENT_TYPE" ]]; then
770
+ # No specific agent provided - update all existing agent files
771
+ log_info "No agent specified, updating all existing agent files..."
772
+ if ! update_all_existing_agents; then
773
+ success=false
774
+ fi
775
+ else
776
+ # Specific agent provided - update only that agent
777
+ log_info "Updating specific agent: $AGENT_TYPE"
778
+ if ! update_specific_agent "$AGENT_TYPE"; then
779
+ success=false
780
+ fi
781
+ fi
782
+
783
+ # Print summary
784
+ print_summary
785
+
786
+ if [[ "$success" == true ]]; then
787
+ log_success "Agent context update completed successfully"
788
+ exit 0
789
+ else
790
+ log_error "Agent context update completed with errors"
791
+ exit 1
792
+ fi
793
+ }
794
+
795
+ # Execute main function if script is run directly
796
+ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
797
+ main "$@"
798
+ fi
799
+
.specify/templates/adr-template.md ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ADR-{{ID}}: {{TITLE}}
2
+
3
+ > **Scope**: Document decision clusters, not individual technology choices. Group related decisions that work together (e.g., "Frontend Stack" not separate ADRs for framework, styling, deployment).
4
+
5
+ - **Status:** Proposed | Accepted | Superseded | Rejected
6
+ - **Date:** {{DATE_ISO}}
7
+ - **Feature:** {{FEATURE_NAME}}
8
+ - **Context:** {{CONTEXT}}
9
+
10
+ <!-- Significance checklist (ALL must be true to justify this ADR)
11
+ 1) Impact: Long-term consequence for architecture/platform/security?
12
+ 2) Alternatives: Multiple viable options considered with tradeoffs?
13
+ 3) Scope: Cross-cutting concern (not an isolated detail)?
14
+ If any are false, prefer capturing as a PHR note instead of an ADR. -->
15
+
16
+ ## Decision
17
+
18
+ {{DECISION}}
19
+
20
+ <!-- For technology stacks, list all components:
21
+ - Framework: Next.js 14 (App Router)
22
+ - Styling: Tailwind CSS v3
23
+ - Deployment: Vercel
24
+ - State Management: React Context (start simple)
25
+ -->
26
+
27
+ ## Consequences
28
+
29
+ ### Positive
30
+
31
+ {{POSITIVE_CONSEQUENCES}}
32
+
33
+ <!-- Example: Integrated tooling, excellent DX, fast deploys, strong TypeScript support -->
34
+
35
+ ### Negative
36
+
37
+ {{NEGATIVE_CONSEQUENCES}}
38
+
39
+ <!-- Example: Vendor lock-in to Vercel, framework coupling, learning curve -->
40
+
41
+ ## Alternatives Considered
42
+
43
+ {{ALTERNATIVES}}
44
+
45
+ <!-- Group alternatives by cluster:
46
+ Alternative Stack A: Remix + styled-components + Cloudflare
47
+ Alternative Stack B: Vite + vanilla CSS + AWS Amplify
48
+ Why rejected: Less integrated, more setup complexity
49
+ -->
50
+
51
+ ## References
52
+
53
+ - Feature Spec: {{SPEC_LINK}}
54
+ - Implementation Plan: {{PLAN_LINK}}
55
+ - Related ADRs: {{RELATED_ADRS}}
56
+ - Evaluator Evidence: {{EVAL_NOTES_LINK}} <!-- link to eval notes/PHR showing graders and outcomes -->
.specify/templates/agent-file-template.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [PROJECT NAME] Development Guidelines
2
+
3
+ Auto-generated from all feature plans. Last updated: [DATE]
4
+
5
+ ## Active Technologies
6
+
7
+ [EXTRACTED FROM ALL PLAN.MD FILES]
8
+
9
+ ## Project Structure
10
+
11
+ ```text
12
+ [ACTUAL STRUCTURE FROM PLANS]
13
+ ```
14
+
15
+ ## Commands
16
+
17
+ [ONLY COMMANDS FOR ACTIVE TECHNOLOGIES]
18
+
19
+ ## Code Style
20
+
21
+ [LANGUAGE-SPECIFIC, ONLY FOR LANGUAGES IN USE]
22
+
23
+ ## Recent Changes
24
+
25
+ [LAST 3 FEATURES AND WHAT THEY ADDED]
26
+
27
+ <!-- MANUAL ADDITIONS START -->
28
+ <!-- MANUAL ADDITIONS END -->
.specify/templates/checklist-template.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [CHECKLIST TYPE] Checklist: [FEATURE NAME]
2
+
3
+ **Purpose**: [Brief description of what this checklist covers]
4
+ **Created**: [DATE]
5
+ **Feature**: [Link to spec.md or relevant documentation]
6
+
7
+ **Note**: This checklist is generated by the `/sp.checklist` command based on feature context and requirements.
8
+
9
+ <!--
10
+ ============================================================================
11
+ IMPORTANT: The checklist items below are SAMPLE ITEMS for illustration only.
12
+
13
+ The /sp.checklist command MUST replace these with actual items based on:
14
+ - User's specific checklist request
15
+ - Feature requirements from spec.md
16
+ - Technical context from plan.md
17
+ - Implementation details from tasks.md
18
+
19
+ DO NOT keep these sample items in the generated checklist file.
20
+ ============================================================================
21
+ -->
22
+
23
+ ## [Category 1]
24
+
25
+ - [ ] CHK001 First checklist item with clear action
26
+ - [ ] CHK002 Second checklist item
27
+ - [ ] CHK003 Third checklist item
28
+
29
+ ## [Category 2]
30
+
31
+ - [ ] CHK004 Another category item
32
+ - [ ] CHK005 Item with specific criteria
33
+ - [ ] CHK006 Final item in this category
34
+
35
+ ## Notes
36
+
37
+ - Check items off as completed: `[x]`
38
+ - Add comments or findings inline
39
+ - Link to relevant resources or documentation
40
+ - Items are numbered sequentially for easy reference
.specify/templates/phr-template.prompt.md ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ id: {{ID}}
3
+ title: {{TITLE}}
4
+ stage: {{STAGE}}
5
+ date: {{DATE_ISO}}
6
+ surface: {{SURFACE}}
7
+ model: {{MODEL}}
8
+ feature: {{FEATURE}}
9
+ branch: {{BRANCH}}
10
+ user: {{USER}}
11
+ command: {{COMMAND}}
12
+ labels: [{{LABELS}}]
13
+ links:
14
+ spec: {{LINKS_SPEC}}
15
+ ticket: {{LINKS_TICKET}}
16
+ adr: {{LINKS_ADR}}
17
+ pr: {{LINKS_PR}}
18
+ files:
19
+ {{FILES_YAML}}
20
+ tests:
21
+ {{TESTS_YAML}}
22
+ ---
23
+
24
+ ## Prompt
25
+
26
+ {{PROMPT_TEXT}}
27
+
28
+ ## Response snapshot
29
+
30
+ {{RESPONSE_TEXT}}
31
+
32
+ ## Outcome
33
+
34
+ - ✅ Impact: {{OUTCOME_IMPACT}}
35
+ - 🧪 Tests: {{TESTS_SUMMARY}}
36
+ - 📁 Files: {{FILES_SUMMARY}}
37
+ - 🔁 Next prompts: {{NEXT_PROMPTS}}
38
+ - 🧠 Reflection: {{REFLECTION_NOTE}}
39
+
40
+ ## Evaluation notes (flywheel)
41
+
42
+ - Failure modes observed: {{FAILURE_MODES}}
43
+ - Graders run and results (PASS/FAIL): {{GRADER_RESULTS}}
44
+ - Prompt variant (if applicable): {{PROMPT_VARIANT_ID}}
45
+ - Next experiment (smallest change to try): {{NEXT_EXPERIMENT}}
.specify/templates/plan-template.md ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Implementation Plan: [FEATURE]
2
+
3
+ **Branch**: `[###-feature-name]` | **Date**: [DATE] | **Spec**: [link]
4
+ **Input**: Feature specification from `/specs/[###-feature-name]/spec.md`
5
+
6
+ **Note**: This template is filled in by the `/sp.plan` command. See `.specify/templates/commands/plan.md` for the execution workflow.
7
+
8
+ ## Summary
9
+
10
+ [Extract from feature spec: primary requirement + technical approach from research]
11
+
12
+ ## Technical Context
13
+
14
+ <!--
15
+ ACTION REQUIRED: Replace the content in this section with the technical details
16
+ for the project. The structure here is presented in advisory capacity to guide
17
+ the iteration process.
18
+ -->
19
+
20
+ **Language/Version**: [e.g., Python 3.11, Swift 5.9, Rust 1.75 or NEEDS CLARIFICATION]
21
+ **Primary Dependencies**: [e.g., FastAPI, UIKit, LLVM or NEEDS CLARIFICATION]
22
+ **Storage**: [if applicable, e.g., PostgreSQL, CoreData, files or N/A]
23
+ **Testing**: [e.g., pytest, XCTest, cargo test or NEEDS CLARIFICATION]
24
+ **Target Platform**: [e.g., Linux server, iOS 15+, WASM or NEEDS CLARIFICATION]
25
+ **Project Type**: [single/web/mobile - determines source structure]
26
+ **Performance Goals**: [domain-specific, e.g., 1000 req/s, 10k lines/sec, 60 fps or NEEDS CLARIFICATION]
27
+ **Constraints**: [domain-specific, e.g., <200ms p95, <100MB memory, offline-capable or NEEDS CLARIFICATION]
28
+ **Scale/Scope**: [domain-specific, e.g., 10k users, 1M LOC, 50 screens or NEEDS CLARIFICATION]
29
+
30
+ ## Constitution Check
31
+
32
+ *GATE: Must pass before Phase 0 research. Re-check after Phase 1 design.*
33
+
34
+ [Gates determined based on constitution file]
35
+
36
+ ## Project Structure
37
+
38
+ ### Documentation (this feature)
39
+
40
+ ```text
41
+ specs/[###-feature]/
42
+ ├── plan.md # This file (/sp.plan command output)
43
+ ├── research.md # Phase 0 output (/sp.plan command)
44
+ ├── data-model.md # Phase 1 output (/sp.plan command)
45
+ ├── quickstart.md # Phase 1 output (/sp.plan command)
46
+ ├── contracts/ # Phase 1 output (/sp.plan command)
47
+ └── tasks.md # Phase 2 output (/sp.tasks command - NOT created by /sp.plan)
48
+ ```
49
+
50
+ ### Source Code (repository root)
51
+ <!--
52
+ ACTION REQUIRED: Replace the placeholder tree below with the concrete layout
53
+ for this feature. Delete unused options and expand the chosen structure with
54
+ real paths (e.g., apps/admin, packages/something). The delivered plan must
55
+ not include Option labels.
56
+ -->
57
+
58
+ ```text
59
+ # [REMOVE IF UNUSED] Option 1: Single project (DEFAULT)
60
+ src/
61
+ ├── models/
62
+ ├── services/
63
+ ├── cli/
64
+ └── lib/
65
+
66
+ tests/
67
+ ├── contract/
68
+ ├── integration/
69
+ └── unit/
70
+
71
+ # [REMOVE IF UNUSED] Option 2: Web application (when "frontend" + "backend" detected)
72
+ backend/
73
+ ├── src/
74
+ │ ├── models/
75
+ │ ├── services/
76
+ │ └── api/
77
+ └── tests/
78
+
79
+ frontend/
80
+ ├── src/
81
+ │ ├── components/
82
+ │ ├── pages/
83
+ │ └── services/
84
+ └── tests/
85
+
86
+ # [REMOVE IF UNUSED] Option 3: Mobile + API (when "iOS/Android" detected)
87
+ api/
88
+ └── [same as backend above]
89
+
90
+ ios/ or android/
91
+ └── [platform-specific structure: feature modules, UI flows, platform tests]
92
+ ```
93
+
94
+ **Structure Decision**: [Document the selected structure and reference the real
95
+ directories captured above]
96
+
97
+ ## Complexity Tracking
98
+
99
+ > **Fill ONLY if Constitution Check has violations that must be justified**
100
+
101
+ | Violation | Why Needed | Simpler Alternative Rejected Because |
102
+ |-----------|------------|-------------------------------------|
103
+ | [e.g., 4th project] | [current need] | [why 3 projects insufficient] |
104
+ | [e.g., Repository pattern] | [specific problem] | [why direct DB access insufficient] |
.specify/templates/spec-template.md ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Feature Specification: [FEATURE NAME]
2
+
3
+ **Feature Branch**: `[###-feature-name]`
4
+ **Created**: [DATE]
5
+ **Status**: Draft
6
+ **Input**: User description: "$ARGUMENTS"
7
+
8
+ ## User Scenarios & Testing *(mandatory)*
9
+
10
+ <!--
11
+ IMPORTANT: User stories should be PRIORITIZED as user journeys ordered by importance.
12
+ Each user story/journey must be INDEPENDENTLY TESTABLE - meaning if you implement just ONE of them,
13
+ you should still have a viable MVP (Minimum Viable Product) that delivers value.
14
+
15
+ Assign priorities (P1, P2, P3, etc.) to each story, where P1 is the most critical.
16
+ Think of each story as a standalone slice of functionality that can be:
17
+ - Developed independently
18
+ - Tested independently
19
+ - Deployed independently
20
+ - Demonstrated to users independently
21
+ -->
22
+
23
+ ### User Story 1 - [Brief Title] (Priority: P1)
24
+
25
+ [Describe this user journey in plain language]
26
+
27
+ **Why this priority**: [Explain the value and why it has this priority level]
28
+
29
+ **Independent Test**: [Describe how this can be tested independently - e.g., "Can be fully tested by [specific action] and delivers [specific value]"]
30
+
31
+ **Acceptance Scenarios**:
32
+
33
+ 1. **Given** [initial state], **When** [action], **Then** [expected outcome]
34
+ 2. **Given** [initial state], **When** [action], **Then** [expected outcome]
35
+
36
+ ---
37
+
38
+ ### User Story 2 - [Brief Title] (Priority: P2)
39
+
40
+ [Describe this user journey in plain language]
41
+
42
+ **Why this priority**: [Explain the value and why it has this priority level]
43
+
44
+ **Independent Test**: [Describe how this can be tested independently]
45
+
46
+ **Acceptance Scenarios**:
47
+
48
+ 1. **Given** [initial state], **When** [action], **Then** [expected outcome]
49
+
50
+ ---
51
+
52
+ ### User Story 3 - [Brief Title] (Priority: P3)
53
+
54
+ [Describe this user journey in plain language]
55
+
56
+ **Why this priority**: [Explain the value and why it has this priority level]
57
+
58
+ **Independent Test**: [Describe how this can be tested independently]
59
+
60
+ **Acceptance Scenarios**:
61
+
62
+ 1. **Given** [initial state], **When** [action], **Then** [expected outcome]
63
+
64
+ ---
65
+
66
+ [Add more user stories as needed, each with an assigned priority]
67
+
68
+ ### Edge Cases
69
+
70
+ <!--
71
+ ACTION REQUIRED: The content in this section represents placeholders.
72
+ Fill them out with the right edge cases.
73
+ -->
74
+
75
+ - What happens when [boundary condition]?
76
+ - How does system handle [error scenario]?
77
+
78
+ ## Requirements *(mandatory)*
79
+
80
+ <!--
81
+ ACTION REQUIRED: The content in this section represents placeholders.
82
+ Fill them out with the right functional requirements.
83
+ -->
84
+
85
+ ### Functional Requirements
86
+
87
+ - **FR-001**: System MUST [specific capability, e.g., "allow users to create accounts"]
88
+ - **FR-002**: System MUST [specific capability, e.g., "validate email addresses"]
89
+ - **FR-003**: Users MUST be able to [key interaction, e.g., "reset their password"]
90
+ - **FR-004**: System MUST [data requirement, e.g., "persist user preferences"]
91
+ - **FR-005**: System MUST [behavior, e.g., "log all security events"]
92
+
93
+ *Example of marking unclear requirements:*
94
+
95
+ - **FR-006**: System MUST authenticate users via [NEEDS CLARIFICATION: auth method not specified - email/password, SSO, OAuth?]
96
+ - **FR-007**: System MUST retain user data for [NEEDS CLARIFICATION: retention period not specified]
97
+
98
+ ### Key Entities *(include if feature involves data)*
99
+
100
+ - **[Entity 1]**: [What it represents, key attributes without implementation]
101
+ - **[Entity 2]**: [What it represents, relationships to other entities]
102
+
103
+ ## Success Criteria *(mandatory)*
104
+
105
+ <!--
106
+ ACTION REQUIRED: Define measurable success criteria.
107
+ These must be technology-agnostic and measurable.
108
+ -->
109
+
110
+ ### Measurable Outcomes
111
+
112
+ - **SC-001**: [Measurable metric, e.g., "Users can complete account creation in under 2 minutes"]
113
+ - **SC-002**: [Measurable metric, e.g., "System handles 1000 concurrent users without degradation"]
114
+ - **SC-003**: [User satisfaction metric, e.g., "90% of users successfully complete primary task on first attempt"]
115
+ - **SC-004**: [Business metric, e.g., "Reduce support tickets related to [X] by 50%"]
.specify/templates/tasks-template.md ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+
3
+ description: "Task list template for feature implementation"
4
+ ---
5
+
6
+ # Tasks: [FEATURE NAME]
7
+
8
+ **Input**: Design documents from `/specs/[###-feature-name]/`
9
+ **Prerequisites**: plan.md (required), spec.md (required for user stories), research.md, data-model.md, contracts/
10
+
11
+ **Tests**: The examples below include test tasks. Tests are OPTIONAL - only include them if explicitly requested in the feature specification.
12
+
13
+ **Organization**: Tasks are grouped by user story to enable independent implementation and testing of each story.
14
+
15
+ ## Format: `[ID] [P?] [Story] Description`
16
+
17
+ - **[P]**: Can run in parallel (different files, no dependencies)
18
+ - **[Story]**: Which user story this task belongs to (e.g., US1, US2, US3)
19
+ - Include exact file paths in descriptions
20
+
21
+ ## Path Conventions
22
+
23
+ - **Single project**: `src/`, `tests/` at repository root
24
+ - **Web app**: `backend/src/`, `frontend/src/`
25
+ - **Mobile**: `api/src/`, `ios/src/` or `android/src/`
26
+ - Paths shown below assume single project - adjust based on plan.md structure
27
+
28
+ <!--
29
+ ============================================================================
30
+ IMPORTANT: The tasks below are SAMPLE TASKS for illustration purposes only.
31
+
32
+ The /sp.tasks command MUST replace these with actual tasks based on:
33
+ - User stories from spec.md (with their priorities P1, P2, P3...)
34
+ - Feature requirements from plan.md
35
+ - Entities from data-model.md
36
+ - Endpoints from contracts/
37
+
38
+ Tasks MUST be organized by user story so each story can be:
39
+ - Implemented independently
40
+ - Tested independently
41
+ - Delivered as an MVP increment
42
+
43
+ DO NOT keep these sample tasks in the generated tasks.md file.
44
+ ============================================================================
45
+ -->
46
+
47
+ ## Phase 1: Setup (Shared Infrastructure)
48
+
49
+ **Purpose**: Project initialization and basic structure
50
+
51
+ - [ ] T001 Create project structure per implementation plan
52
+ - [ ] T002 Initialize [language] project with [framework] dependencies
53
+ - [ ] T003 [P] Configure linting and formatting tools
54
+
55
+ ---
56
+
57
+ ## Phase 2: Foundational (Blocking Prerequisites)
58
+
59
+ **Purpose**: Core infrastructure that MUST be complete before ANY user story can be implemented
60
+
61
+ **⚠️ CRITICAL**: No user story work can begin until this phase is complete
62
+
63
+ Examples of foundational tasks (adjust based on your project):
64
+
65
+ - [ ] T004 Setup database schema and migrations framework
66
+ - [ ] T005 [P] Implement authentication/authorization framework
67
+ - [ ] T006 [P] Setup API routing and middleware structure
68
+ - [ ] T007 Create base models/entities that all stories depend on
69
+ - [ ] T008 Configure error handling and logging infrastructure
70
+ - [ ] T009 Setup environment configuration management
71
+
72
+ **Checkpoint**: Foundation ready - user story implementation can now begin in parallel
73
+
74
+ ---
75
+
76
+ ## Phase 3: User Story 1 - [Title] (Priority: P1) 🎯 MVP
77
+
78
+ **Goal**: [Brief description of what this story delivers]
79
+
80
+ **Independent Test**: [How to verify this story works on its own]
81
+
82
+ ### Tests for User Story 1 (OPTIONAL - only if tests requested) ⚠️
83
+
84
+ > **NOTE: Write these tests FIRST, ensure they FAIL before implementation**
85
+
86
+ - [ ] T010 [P] [US1] Contract test for [endpoint] in tests/contract/test_[name].py
87
+ - [ ] T011 [P] [US1] Integration test for [user journey] in tests/integration/test_[name].py
88
+
89
+ ### Implementation for User Story 1
90
+
91
+ - [ ] T012 [P] [US1] Create [Entity1] model in src/models/[entity1].py
92
+ - [ ] T013 [P] [US1] Create [Entity2] model in src/models/[entity2].py
93
+ - [ ] T014 [US1] Implement [Service] in src/services/[service].py (depends on T012, T013)
94
+ - [ ] T015 [US1] Implement [endpoint/feature] in src/[location]/[file].py
95
+ - [ ] T016 [US1] Add validation and error handling
96
+ - [ ] T017 [US1] Add logging for user story 1 operations
97
+
98
+ **Checkpoint**: At this point, User Story 1 should be fully functional and testable independently
99
+
100
+ ---
101
+
102
+ ## Phase 4: User Story 2 - [Title] (Priority: P2)
103
+
104
+ **Goal**: [Brief description of what this story delivers]
105
+
106
+ **Independent Test**: [How to verify this story works on its own]
107
+
108
+ ### Tests for User Story 2 (OPTIONAL - only if tests requested) ⚠️
109
+
110
+ - [ ] T018 [P] [US2] Contract test for [endpoint] in tests/contract/test_[name].py
111
+ - [ ] T019 [P] [US2] Integration test for [user journey] in tests/integration/test_[name].py
112
+
113
+ ### Implementation for User Story 2
114
+
115
+ - [ ] T020 [P] [US2] Create [Entity] model in src/models/[entity].py
116
+ - [ ] T021 [US2] Implement [Service] in src/services/[service].py
117
+ - [ ] T022 [US2] Implement [endpoint/feature] in src/[location]/[file].py
118
+ - [ ] T023 [US2] Integrate with User Story 1 components (if needed)
119
+
120
+ **Checkpoint**: At this point, User Stories 1 AND 2 should both work independently
121
+
122
+ ---
123
+
124
+ ## Phase 5: User Story 3 - [Title] (Priority: P3)
125
+
126
+ **Goal**: [Brief description of what this story delivers]
127
+
128
+ **Independent Test**: [How to verify this story works on its own]
129
+
130
+ ### Tests for User Story 3 (OPTIONAL - only if tests requested) ⚠️
131
+
132
+ - [ ] T024 [P] [US3] Contract test for [endpoint] in tests/contract/test_[name].py
133
+ - [ ] T025 [P] [US3] Integration test for [user journey] in tests/integration/test_[name].py
134
+
135
+ ### Implementation for User Story 3
136
+
137
+ - [ ] T026 [P] [US3] Create [Entity] model in src/models/[entity].py
138
+ - [ ] T027 [US3] Implement [Service] in src/services/[service].py
139
+ - [ ] T028 [US3] Implement [endpoint/feature] in src/[location]/[file].py
140
+
141
+ **Checkpoint**: All user stories should now be independently functional
142
+
143
+ ---
144
+
145
+ [Add more user story phases as needed, following the same pattern]
146
+
147
+ ---
148
+
149
+ ## Phase N: Polish & Cross-Cutting Concerns
150
+
151
+ **Purpose**: Improvements that affect multiple user stories
152
+
153
+ - [ ] TXXX [P] Documentation updates in docs/
154
+ - [ ] TXXX Code cleanup and refactoring
155
+ - [ ] TXXX Performance optimization across all stories
156
+ - [ ] TXXX [P] Additional unit tests (if requested) in tests/unit/
157
+ - [ ] TXXX Security hardening
158
+ - [ ] TXXX Run quickstart.md validation
159
+
160
+ ---
161
+
162
+ ## Dependencies & Execution Order
163
+
164
+ ### Phase Dependencies
165
+
166
+ - **Setup (Phase 1)**: No dependencies - can start immediately
167
+ - **Foundational (Phase 2)**: Depends on Setup completion - BLOCKS all user stories
168
+ - **User Stories (Phase 3+)**: All depend on Foundational phase completion
169
+ - User stories can then proceed in parallel (if staffed)
170
+ - Or sequentially in priority order (P1 → P2 → P3)
171
+ - **Polish (Final Phase)**: Depends on all desired user stories being complete
172
+
173
+ ### User Story Dependencies
174
+
175
+ - **User Story 1 (P1)**: Can start after Foundational (Phase 2) - No dependencies on other stories
176
+ - **User Story 2 (P2)**: Can start after Foundational (Phase 2) - May integrate with US1 but should be independently testable
177
+ - **User Story 3 (P3)**: Can start after Foundational (Phase 2) - May integrate with US1/US2 but should be independently testable
178
+
179
+ ### Within Each User Story
180
+
181
+ - Tests (if included) MUST be written and FAIL before implementation
182
+ - Models before services
183
+ - Services before endpoints
184
+ - Core implementation before integration
185
+ - Story complete before moving to next priority
186
+
187
+ ### Parallel Opportunities
188
+
189
+ - All Setup tasks marked [P] can run in parallel
190
+ - All Foundational tasks marked [P] can run in parallel (within Phase 2)
191
+ - Once Foundational phase completes, all user stories can start in parallel (if team capacity allows)
192
+ - All tests for a user story marked [P] can run in parallel
193
+ - Models within a story marked [P] can run in parallel
194
+ - Different user stories can be worked on in parallel by different team members
195
+
196
+ ---
197
+
198
+ ## Parallel Example: User Story 1
199
+
200
+ ```bash
201
+ # Launch all tests for User Story 1 together (if tests requested):
202
+ Task: "Contract test for [endpoint] in tests/contract/test_[name].py"
203
+ Task: "Integration test for [user journey] in tests/integration/test_[name].py"
204
+
205
+ # Launch all models for User Story 1 together:
206
+ Task: "Create [Entity1] model in src/models/[entity1].py"
207
+ Task: "Create [Entity2] model in src/models/[entity2].py"
208
+ ```
209
+
210
+ ---
211
+
212
+ ## Implementation Strategy
213
+
214
+ ### MVP First (User Story 1 Only)
215
+
216
+ 1. Complete Phase 1: Setup
217
+ 2. Complete Phase 2: Foundational (CRITICAL - blocks all stories)
218
+ 3. Complete Phase 3: User Story 1
219
+ 4. **STOP and VALIDATE**: Test User Story 1 independently
220
+ 5. Deploy/demo if ready
221
+
222
+ ### Incremental Delivery
223
+
224
+ 1. Complete Setup + Foundational → Foundation ready
225
+ 2. Add User Story 1 → Test independently → Deploy/Demo (MVP!)
226
+ 3. Add User Story 2 → Test independently → Deploy/Demo
227
+ 4. Add User Story 3 → Test independently → Deploy/Demo
228
+ 5. Each story adds value without breaking previous stories
229
+
230
+ ### Parallel Team Strategy
231
+
232
+ With multiple developers:
233
+
234
+ 1. Team completes Setup + Foundational together
235
+ 2. Once Foundational is done:
236
+ - Developer A: User Story 1
237
+ - Developer B: User Story 2
238
+ - Developer C: User Story 3
239
+ 3. Stories complete and integrate independently
240
+
241
+ ---
242
+
243
+ ## Notes
244
+
245
+ - [P] tasks = different files, no dependencies
246
+ - [Story] label maps task to specific user story for traceability
247
+ - Each user story should be independently completable and testable
248
+ - Verify tests fail before implementing
249
+ - Commit after each task or logical group
250
+ - Stop at any checkpoint to validate story independently
251
+ - Avoid: vague tasks, same file conflicts, cross-story dependencies that break independence
CLAUDE.md ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Claude Code Rules
2
+
3
+ This file is generated during init for the selected agent.
4
+
5
+ You are an expert AI assistant specializing in Spec-Driven Development (SDD). Your primary goal is to work with the architext to build products.
6
+
7
+ ## Task context
8
+
9
+ **Your Surface:** You operate on a project level, providing guidance to users and executing development tasks via a defined set of tools.
10
+
11
+ **Your Success is Measured By:**
12
+ - All outputs strictly follow the user intent.
13
+ - Prompt History Records (PHRs) are created automatically and accurately for every user prompt.
14
+ - Architectural Decision Record (ADR) suggestions are made intelligently for significant decisions.
15
+ - All changes are small, testable, and reference code precisely.
16
+
17
+ ## Core Guarantees (Product Promise)
18
+
19
+ - Record every user input verbatim in a Prompt History Record (PHR) after every user message. Do not truncate; preserve full multiline input.
20
+ - PHR routing (all under `history/prompts/`):
21
+ - Constitution → `history/prompts/constitution/`
22
+ - Feature-specific → `history/prompts/<feature-name>/`
23
+ - General → `history/prompts/general/`
24
+ - ADR suggestions: when an architecturally significant decision is detected, suggest: "📋 Architectural decision detected: <brief>. Document? Run `/sp.adr <title>`." Never auto‑create ADRs; require user consent.
25
+
26
+ ## Development Guidelines
27
+
28
+ ### 1. Authoritative Source Mandate:
29
+ Agents MUST prioritize and use MCP tools and CLI commands for all information gathering and task execution. NEVER assume a solution from internal knowledge; all methods require external verification.
30
+
31
+ ### 2. Execution Flow:
32
+ Treat MCP servers as first-class tools for discovery, verification, execution, and state capture. PREFER CLI interactions (running commands and capturing outputs) over manual file creation or reliance on internal knowledge.
33
+
34
+ ### 3. Knowledge capture (PHR) for Every User Input.
35
+ After completing requests, you **MUST** create a PHR (Prompt History Record).
36
+
37
+ **When to create PHRs:**
38
+ - Implementation work (code changes, new features)
39
+ - Planning/architecture discussions
40
+ - Debugging sessions
41
+ - Spec/task/plan creation
42
+ - Multi-step workflows
43
+
44
+ **PHR Creation Process:**
45
+
46
+ 1) Detect stage
47
+ - One of: constitution | spec | plan | tasks | red | green | refactor | explainer | misc | general
48
+
49
+ 2) Generate title
50
+ - 3–7 words; create a slug for the filename.
51
+
52
+ 2a) Resolve route (all under history/prompts/)
53
+ - `constitution` → `history/prompts/constitution/`
54
+ - Feature stages (spec, plan, tasks, red, green, refactor, explainer, misc) → `history/prompts/<feature-name>/` (requires feature context)
55
+ - `general` → `history/prompts/general/`
56
+
57
+ 3) Prefer agent‑native flow (no shell)
58
+ - Read the PHR template from one of:
59
+ - `.specify/templates/phr-template.prompt.md`
60
+ - `templates/phr-template.prompt.md`
61
+ - Allocate an ID (increment; on collision, increment again).
62
+ - Compute output path based on stage:
63
+ - Constitution → `history/prompts/constitution/<ID>-<slug>.constitution.prompt.md`
64
+ - Feature → `history/prompts/<feature-name>/<ID>-<slug>.<stage>.prompt.md`
65
+ - General → `history/prompts/general/<ID>-<slug>.general.prompt.md`
66
+ - Fill ALL placeholders in YAML and body:
67
+ - ID, TITLE, STAGE, DATE_ISO (YYYY‑MM‑DD), SURFACE="agent"
68
+ - MODEL (best known), FEATURE (or "none"), BRANCH, USER
69
+ - COMMAND (current command), LABELS (["topic1","topic2",...])
70
+ - LINKS: SPEC/TICKET/ADR/PR (URLs or "null")
71
+ - FILES_YAML: list created/modified files (one per line, " - ")
72
+ - TESTS_YAML: list tests run/added (one per line, " - ")
73
+ - PROMPT_TEXT: full user input (verbatim, not truncated)
74
+ - RESPONSE_TEXT: key assistant output (concise but representative)
75
+ - Any OUTCOME/EVALUATION fields required by the template
76
+ - Write the completed file with agent file tools (WriteFile/Edit).
77
+ - Confirm absolute path in output.
78
+
79
+ 4) Use sp.phr command file if present
80
+ - If `.**/commands/sp.phr.*` exists, follow its structure.
81
+ - If it references shell but Shell is unavailable, still perform step 3 with agent‑native tools.
82
+
83
+ 5) Shell fallback (only if step 3 is unavailable or fails, and Shell is permitted)
84
+ - Run: `.specify/scripts/bash/create-phr.sh --title "<title>" --stage <stage> [--feature <name>] --json`
85
+ - Then open/patch the created file to ensure all placeholders are filled and prompt/response are embedded.
86
+
87
+ 6) Routing (automatic, all under history/prompts/)
88
+ - Constitution → `history/prompts/constitution/`
89
+ - Feature stages → `history/prompts/<feature-name>/` (auto-detected from branch or explicit feature context)
90
+ - General → `history/prompts/general/`
91
+
92
+ 7) Post‑creation validations (must pass)
93
+ - No unresolved placeholders (e.g., `{{THIS}}`, `[THAT]`).
94
+ - Title, stage, and dates match front‑matter.
95
+ - PROMPT_TEXT is complete (not truncated).
96
+ - File exists at the expected path and is readable.
97
+ - Path matches route.
98
+
99
+ 8) Report
100
+ - Print: ID, path, stage, title.
101
+ - On any failure: warn but do not block the main command.
102
+ - Skip PHR only for `/sp.phr` itself.
103
+
104
+ ### 4. Explicit ADR suggestions
105
+ - When significant architectural decisions are made (typically during `/sp.plan` and sometimes `/sp.tasks`), run the three‑part test and suggest documenting with:
106
+ "📋 Architectural decision detected: <brief> — Document reasoning and tradeoffs? Run `/sp.adr <decision-title>`"
107
+ - Wait for user consent; never auto‑create the ADR.
108
+
109
+ ### 5. Human as Tool Strategy
110
+ You are not expected to solve every problem autonomously. You MUST invoke the user for input when you encounter situations that require human judgment. Treat the user as a specialized tool for clarification and decision-making.
111
+
112
+ **Invocation Triggers:**
113
+ 1. **Ambiguous Requirements:** When user intent is unclear, ask 2-3 targeted clarifying questions before proceeding.
114
+ 2. **Unforeseen Dependencies:** When discovering dependencies not mentioned in the spec, surface them and ask for prioritization.
115
+ 3. **Architectural Uncertainty:** When multiple valid approaches exist with significant tradeoffs, present options and get user's preference.
116
+ 4. **Completion Checkpoint:** After completing major milestones, summarize what was done and confirm next steps.
117
+
118
+ ## Default policies (must follow)
119
+ - Clarify and plan first - keep business understanding separate from technical plan and carefully architect and implement.
120
+ - Do not invent APIs, data, or contracts; ask targeted clarifiers if missing.
121
+ - Never hardcode secrets or tokens; use `.env` and docs.
122
+ - Prefer the smallest viable diff; do not refactor unrelated code.
123
+ - Cite existing code with code references (start:end:path); propose new code in fenced blocks.
124
+ - Keep reasoning private; output only decisions, artifacts, and justifications.
125
+
126
+ ### Execution contract for every request
127
+ 1) Confirm surface and success criteria (one sentence).
128
+ 2) List constraints, invariants, non‑goals.
129
+ 3) Produce the artifact with acceptance checks inlined (checkboxes or tests where applicable).
130
+ 4) Add follow‑ups and risks (max 3 bullets).
131
+ 5) Create PHR in appropriate subdirectory under `history/prompts/` (constitution, feature-name, or general).
132
+ 6) If plan/tasks identified decisions that meet significance, surface ADR suggestion text as described above.
133
+
134
+ ### Minimum acceptance criteria
135
+ - Clear, testable acceptance criteria included
136
+ - Explicit error paths and constraints stated
137
+ - Smallest viable change; no unrelated edits
138
+ - Code references to modified/inspected files where relevant
139
+
140
+ ## Architect Guidelines (for planning)
141
+
142
+ Instructions: As an expert architect, generate a detailed architectural plan for [Project Name]. Address each of the following thoroughly.
143
+
144
+ 1. Scope and Dependencies:
145
+ - In Scope: boundaries and key features.
146
+ - Out of Scope: explicitly excluded items.
147
+ - External Dependencies: systems/services/teams and ownership.
148
+
149
+ 2. Key Decisions and Rationale:
150
+ - Options Considered, Trade-offs, Rationale.
151
+ - Principles: measurable, reversible where possible, smallest viable change.
152
+
153
+ 3. Interfaces and API Contracts:
154
+ - Public APIs: Inputs, Outputs, Errors.
155
+ - Versioning Strategy.
156
+ - Idempotency, Timeouts, Retries.
157
+ - Error Taxonomy with status codes.
158
+
159
+ 4. Non-Functional Requirements (NFRs) and Budgets:
160
+ - Performance: p95 latency, throughput, resource caps.
161
+ - Reliability: SLOs, error budgets, degradation strategy.
162
+ - Security: AuthN/AuthZ, data handling, secrets, auditing.
163
+ - Cost: unit economics.
164
+
165
+ 5. Data Management and Migration:
166
+ - Source of Truth, Schema Evolution, Migration and Rollback, Data Retention.
167
+
168
+ 6. Operational Readiness:
169
+ - Observability: logs, metrics, traces.
170
+ - Alerting: thresholds and on-call owners.
171
+ - Runbooks for common tasks.
172
+ - Deployment and Rollback strategies.
173
+ - Feature Flags and compatibility.
174
+
175
+ 7. Risk Analysis and Mitigation:
176
+ - Top 3 Risks, blast radius, kill switches/guardrails.
177
+
178
+ 8. Evaluation and Validation:
179
+ - Definition of Done (tests, scans).
180
+ - Output Validation for format/requirements/safety.
181
+
182
+ 9. Architectural Decision Record (ADR):
183
+ - For each significant decision, create an ADR and link it.
184
+
185
+ ### Architecture Decision Records (ADR) - Intelligent Suggestion
186
+
187
+ After design/architecture work, test for ADR significance:
188
+
189
+ - Impact: long-term consequences? (e.g., framework, data model, API, security, platform)
190
+ - Alternatives: multiple viable options considered?
191
+ - Scope: cross‑cutting and influences system design?
192
+
193
+ If ALL true, suggest:
194
+ 📋 Architectural decision detected: [brief-description]
195
+ Document reasoning and tradeoffs? Run `/sp.adr [decision-title]`
196
+
197
+ Wait for consent; never auto-create ADRs. Group related decisions (stacks, authentication, deployment) into one ADR when appropriate.
198
+
199
+ ## Basic Project Structure
200
+
201
+ - `.specify/memory/constitution.md` — Project principles
202
+ - `specs/<feature>/spec.md` — Feature requirements
203
+ - `specs/<feature>/plan.md` — Architecture decisions
204
+ - `specs/<feature>/tasks.md` — Testable tasks with cases
205
+ - `history/prompts/` — Prompt History Records
206
+ - `history/adr/` — Architecture Decision Records
207
+ - `.specify/` — SpecKit Plus templates and scripts
208
+
209
+ ## Code Standards
210
+ See `.specify/memory/constitution.md` for code quality, testing, performance, security, and architecture principles.
Dockerfile ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ============================================================================
2
+ # HuggingFace Spaces — FastAPI backend (8000) + Next.js standalone (7860)
3
+ # Frontend built inside Docker so source changes always deploy correctly
4
+ # ============================================================================
5
+ FROM python:3.11-slim
6
+
7
+ # Install Node.js 18 + supervisor
8
+ RUN apt-get update && apt-get install -y --no-install-recommends \
9
+ curl ca-certificates xz-utils supervisor \
10
+ && curl -fsSL "https://nodejs.org/dist/v18.20.4/node-v18.20.4-linux-x64.tar.xz" \
11
+ | tar -xJ -C /usr/local --strip-components=1 \
12
+ && rm -rf /var/lib/apt/lists/*
13
+
14
+ WORKDIR /app
15
+
16
+ # ── Python backend ──────────────────────────────────────────────────────────
17
+ COPY phase-1-core-infra/backend/requirements.txt ./
18
+ RUN pip install --no-cache-dir --upgrade pip \
19
+ && pip install --no-cache-dir -r requirements.txt
20
+
21
+ COPY phase-1-core-infra/backend/ ./backend/
22
+
23
+ # ── Next.js frontend — build from source ───────────────────────────────────
24
+ COPY phase-1-core-infra/frontend/package.json phase-1-core-infra/frontend/package-lock.json /build/frontend/
25
+ RUN cd /build/frontend && npm ci --legacy-peer-deps
26
+
27
+ COPY phase-1-core-infra/frontend/ /build/frontend/
28
+ RUN cd /build/frontend \
29
+ && NEXT_PUBLIC_API_URL=http://localhost:8000 \
30
+ NEXT_TELEMETRY_DISABLED=1 \
31
+ npm run build
32
+
33
+ # Copy standalone output into /app/frontend
34
+ RUN cp -r /build/frontend/.next/standalone/. /app/frontend/ \
35
+ && cp -r /build/frontend/.next/static/. /app/frontend/.next/static/ \
36
+ && rm -rf /build
37
+
38
+ # ── Runtime setup ───────────────────────────────────────────────────────────
39
+ RUN mkdir -p /var/log/supervisor /app/backend/uploads
40
+ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
41
+ RUN useradd -m -u 1000 appuser \
42
+ && chown -R appuser:appuser /app /var/log/supervisor
43
+
44
+ EXPOSE 7860
45
+
46
+ ENV PYTHONUNBUFFERED=1 \
47
+ NODE_ENV=production \
48
+ PORT=7860 \
49
+ HOSTNAME=0.0.0.0 \
50
+ NEXT_PUBLIC_API_URL=http://localhost:8000 \
51
+ NEXT_TELEMETRY_DISABLED=1
52
+
53
+ HEALTHCHECK --interval=30s --timeout=15s --start-period=120s --retries=3 \
54
+ CMD curl -f http://localhost:7860 || exit 1
55
+
56
+ CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
README.md ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AI Marketing Automation
3
+ emoji: 🤖
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ sdk_version: latest
8
+ app_port: 7860
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # 🤖 Autonomous AI Marketing Agency
14
+
15
+ **Multi-tenant AI-powered marketing automation platform** with intelligent content generation, video creation, and WhatsApp approval workflows.
16
+
17
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
18
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
19
+ [![Next.js 14](https://img.shields.io/badge/Next.js-14-black)](https://nextjs.org/)
20
+
21
+ ---
22
+
23
+ ## ✨ Features
24
+
25
+ ### 🎨 AI Content Generation
26
+ - Multi-platform content creation using **Claude** and **GPT-4**
27
+ - Platform-specific optimization (LinkedIn, X/Twitter, Instagram, Facebook)
28
+ - Quality-based refinement loop with AI critic agent
29
+ - DALL-E 3 image generation with multiple aspect ratios
30
+ - Voice-to-text with OpenAI Whisper
31
+
32
+ ### 🎥 Video Generation
33
+ - Automated video creation with **Google Veo**
34
+ - Text-to-speech integration
35
+ - Cinematic prompt generation
36
+ - Multiple style and mood options
37
+ - Video library management
38
+
39
+ ### 💬 WhatsApp Approval Workflow
40
+ - Human-in-the-loop content approval via WhatsApp
41
+ - Command-based interface (YES/NO/EDIT)
42
+ - 24-hour approval timeout with reminders
43
+ - Multi-platform publishing after approval
44
+ - Complete audit trail
45
+
46
+ ### 📊 Self-Learning Analytics
47
+ - Real-time engagement tracking across all platforms
48
+ - AI-powered pattern recognition
49
+ - Statistical trend analysis
50
+ - Automatic prompt enhancement based on performance
51
+ - Insights dashboard with interactive charts
52
+
53
+ ### 💳 SaaS Multi-Tenancy
54
+ - Stripe-powered subscription management
55
+ - Tiered pricing (Basic, Pro, Agency)
56
+ - Usage tracking and limit enforcement
57
+ - Row-level security for data isolation
58
+ - Admin dashboard for user management
59
+
60
+ ### 🔐 Security & Authentication
61
+ - **Better Auth** - Self-hosted authentication system
62
+ - Email/password authentication with strong password requirements
63
+ - OAuth social login (Google, GitHub)
64
+ - Password reset and email verification
65
+ - Session management with JWT tokens
66
+ - Admin role management
67
+ - AES-256 encryption for API tokens
68
+ - Rate limiting on all endpoints
69
+ - CSRF protection with state parameters
70
+
71
+ ---
72
+
73
+ ## 🏗️ Architecture
74
+
75
+ ### Technology Stack
76
+
77
+ **Frontend**
78
+ - Next.js 14 (App Router)
79
+ - TypeScript
80
+ - Tailwind CSS
81
+ - React Query
82
+ - Recharts
83
+
84
+ **Backend**
85
+ - FastAPI (Python 3.11+)
86
+ - SQLAlchemy (async)
87
+ - Alembic (migrations)
88
+ - Celery (background jobs)
89
+ - APScheduler (scheduled tasks)
90
+
91
+ **AI & ML**
92
+ - OpenAI GPT-4 & DALL-E 3
93
+ - Anthropic Claude
94
+ - Google Veo (video generation)
95
+ - Google Cloud TTS
96
+
97
+ **Infrastructure**
98
+ - PostgreSQL (Neon/Supabase)
99
+ - Redis (caching & sessions)
100
+ - Docker & Docker Compose
101
+ - HuggingFace Spaces (deployment)
102
+
103
+ ### Phase Architecture
104
+
105
+ The platform is organized into 6 independent phases:
106
+
107
+ 1. **Phase 1: Core Infrastructure** - Authentication, user management, OAuth
108
+ 2. **Phase 2: AI Content Engine** - Multi-agent content generation
109
+ 3. **Phase 3: WhatsApp Approval** - Human-in-the-loop workflow
110
+ 4. **Phase 4: Learning Loop** - Analytics and self-improvement
111
+ 5. **Phase 5: Video Engine** - Automated video generation
112
+ 6. **Phase 6: SaaS Core** - Multi-tenancy and subscriptions
113
+
114
+ ---
115
+
116
+ ## 🚀 Quick Start
117
+
118
+ ### Prerequisites
119
+
120
+ - **Docker & Docker Compose** (recommended) OR
121
+ - **Python 3.11+** and **Node.js 18+** for local development
122
+ - **PostgreSQL Database** (Neon or Supabase recommended)
123
+ - **Redis** (Upstash recommended)
124
+
125
+ ### Option 1: Docker Deployment (Recommended)
126
+
127
+ ```bash
128
+ # 1. Clone repository
129
+ git clone <repository-url>
130
+ cd autonomous-ai-marketing-agency
131
+
132
+ # 2. Configure environment
133
+ cp .env.example .env
134
+ # Edit .env with your API keys and database URLs
135
+
136
+ # 3. Start all services
137
+ docker-compose up -d
138
+
139
+ # 4. Run database migrations
140
+ docker-compose exec backend alembic upgrade head
141
+
142
+ # 5. Access application
143
+ # Frontend: http://localhost:3000
144
+ # Backend API: http://localhost:8000
145
+ # API Docs: http://localhost:8000/docs
146
+ ```
147
+
148
+ ### Option 2: Local Development
149
+
150
+ ```bash
151
+ # 1. Clone repository
152
+ git clone <repository-url>
153
+ cd autonomous-ai-marketing-agency
154
+
155
+ # 2. Configure environment
156
+ cp .env.example .env
157
+ # Edit .env with your configuration
158
+
159
+ # 3. Install backend dependencies
160
+ cd phase-1-core-infra/backend
161
+ pip install -r requirements.txt
162
+
163
+ # 4. Run migrations
164
+ alembic upgrade head
165
+
166
+ # 5. Start backend (Terminal 1)
167
+ uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
168
+
169
+ # 6. Install frontend dependencies (Terminal 2)
170
+ cd ../frontend
171
+ npm install
172
+
173
+ # 7. Start frontend
174
+ npm run dev
175
+
176
+ # 8. Access application
177
+ # Frontend: http://localhost:3000
178
+ # Backend API: http://localhost:8000
179
+ ```
180
+
181
+ ### Required Environment Variables
182
+
183
+ **Minimum Configuration**:
184
+ - `DATABASE_URL` - PostgreSQL connection string (see [Database Setup](docs/database-setup.md))
185
+ - `REDIS_URL` - Redis connection string
186
+ - `BETTER_AUTH_SECRET` - Generate with: `openssl rand -base64 32`
187
+ - `ENCRYPTION_KEY` - Generate with: `python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"`
188
+ - `JWT_SECRET` - Generate with: `openssl rand -hex 32`
189
+ - `OPENAI_API_KEY` - From [OpenAI Platform](https://platform.openai.com)
190
+ - `ANTHROPIC_API_KEY` - From [Anthropic Console](https://console.anthropic.com)
191
+
192
+ **Optional (for full features)**:
193
+ - `STRIPE_SECRET_KEY` - For subscription management
194
+ - `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` - For Google OAuth
195
+ - `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` - For GitHub OAuth
196
+ - `WHATSAPP_API_TOKEN` - For WhatsApp approval workflow
197
+ - `VEO_API_KEY` - For video generation
198
+
199
+ See [Environment Variables Guide](docs/environment-variables.md) for complete configuration.
200
+
201
+ ---
202
+
203
+ ## 📚 Documentation
204
+
205
+ ### Getting Started
206
+ - [Database Setup Guide](docs/database-setup.md) - PostgreSQL setup with Neon/Supabase
207
+ - [Environment Variables Guide](docs/environment-variables.md) - Complete configuration reference
208
+ - [Deployment Guide](docs/deployment.md) - Local, Docker, and HuggingFace Spaces deployment
209
+ - [Architecture Overview](docs/architecture.md) - System design and data flow
210
+
211
+ ### Operations
212
+ - [Troubleshooting Guide](docs/troubleshooting.md) - Common issues and solutions
213
+ - [Migration Guide](docs/database-setup.md#running-migrations) - Database migration instructions
214
+
215
+ ### Phase Documentation
216
+ - [Phase 1: Core Infrastructure](phase-1-core-infra/README.md) - Authentication and user management
217
+ - [Phase 2: AI Engine](phase-2-ai-engine/README.md) - Content generation
218
+ - [Phase 3: Approval System](phase-3-approval-system/README.md) - WhatsApp workflow
219
+ - [Phase 4: Learning Loop](phase-4-learning-loop/README.md) - Analytics and insights
220
+
221
+ ---
222
+
223
+ ## 🎯 Project Status
224
+
225
+ **Overall Completion**: ~58% → Target: 95%
226
+
227
+ - ✅ **Phase 1-3**: Production-ready (100%)
228
+ - ⚠️ **Phase 4**: Functionally complete (78%)
229
+ - 🔄 **Phase 5**: In progress (37% → 90%)
230
+ - 🔄 **Phase 6**: In progress (35% → 90%)
231
+ - ✅ **Better Auth Migration**: Complete (Phases 5-9)
232
+ - OAuth social login (Google, GitHub)
233
+ - Password reset with email
234
+ - Email verification
235
+ - Admin role management
236
+ - Comprehensive documentation
237
+
238
+ See [PROJECT_STATUS_REPORT.md](PROJECT_STATUS_REPORT.md) for detailed status.
239
+
240
+ ---
241
+
242
+ ## 🧪 Testing
243
+
244
+ ```bash
245
+ # Run unit tests
246
+ cd phase-1-core-infra/backend
247
+ pytest
248
+
249
+ # Run with coverage
250
+ pytest --cov=src --cov-report=html
251
+
252
+ # Run E2E tests
253
+ cd ../frontend
254
+ npx playwright test
255
+ ```
256
+
257
+ See [TESTING_GUIDE.md](TESTING_GUIDE.md) for comprehensive testing documentation.
258
+
259
+ ---
260
+
261
+ ## 🔒 Security
262
+
263
+ - **Encryption**: AES-256 for social media tokens, bcrypt for passwords
264
+ - **Authentication**: JWT-based with refresh tokens, OAuth 2.0 support
265
+ - **Authorization**: Role-based access control (RBAC)
266
+ - **Data Isolation**: Row-level security (RLS) for multi-tenancy
267
+ - **Rate Limiting**: 100 requests/minute per user
268
+ - **CSRF Protection**: Enabled on all state-changing operations
269
+ - **Input Validation**: Pydantic schemas for all API inputs
270
+
271
+ ---
272
+
273
+ ## 🤝 Contributing
274
+
275
+ Contributions are welcome! Please read our contributing guidelines before submitting PRs.
276
+
277
+ 1. Fork the repository
278
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
279
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
280
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
281
+ 5. Open a Pull Request
282
+
283
+ ---
284
+
285
+ ## 📄 License
286
+
287
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
288
+
289
+ ---
290
+
291
+ ## 🙏 Acknowledgments
292
+
293
+ - Built with [Claude Code](https://claude.ai/code)
294
+ - Powered by [Anthropic Claude](https://www.anthropic.com/)
295
+ - Deployed on [HuggingFace Spaces](https://huggingface.co/spaces)
296
+
297
+ ---
298
+
299
+ ## 📞 Support
300
+
301
+ - **Documentation**: See `docs/` directory
302
+ - **Issues**: Open an issue on GitHub
303
+ - **Questions**: Check [Troubleshooting Guide](docs/troubleshooting.md)
304
+
305
+ ---
306
+
307
+ **Last Updated**: 2026-02-12 | **Version**: 1.0.0-beta
308
+
309
+ Built with ❤️ using AI-powered development
310
+ # Autonomous_AI_Marketing_Agency
phase-1-core-infra/backend/.env.example ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # Backend Environment Variables
3
+ # =============================================================================
4
+ # Copy this file to .env and fill in your actual values
5
+ # NEVER commit .env to version control
6
+
7
+ # Database (Neon PostgreSQL)
8
+ # Get your connection string from: https://console.neon.tech/
9
+ # Format: postgresql://user:password@ep-xxx.region.aws.neon.tech/dbname?sslmode=require
10
+ DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/phase1_dev?sslmode=require
11
+
12
+ # Encryption
13
+ # Generate with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
14
+ ENCRYPTION_KEY=your-fernet-encryption-key-here
15
+
16
+ # JWT Configuration
17
+ # Generate with: openssl rand -base64 32
18
+ JWT_SECRET=your-jwt-secret-here
19
+ JWT_ALGORITHM=HS256
20
+ JWT_EXPIRATION_DAYS=30
21
+
22
+ # Backend Configuration
23
+ BACKEND_HOST=0.0.0.0
24
+ BACKEND_PORT=8000
25
+ BACKEND_RELOAD=true
26
+
27
+ # Environment
28
+ ENVIRONMENT=development
29
+ DEBUG=true
30
+ LOG_LEVEL=INFO
31
+
32
+ # CORS Configuration
33
+ CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
phase-1-core-infra/backend/.flake8 ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ [flake8]
2
+ max-line-length = 100
3
+ exclude = .git,__pycache__,venv,.venv,alembic/versions
4
+ ignore = E203, W503
phase-1-core-infra/backend/alembic.ini ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # A generic, single database configuration.
2
+
3
+ [alembic]
4
+ # path to migration scripts
5
+ script_location = alembic
6
+
7
+ # template used to generate migration files
8
+ file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d_%%(rev)s_%%(slug)s
9
+
10
+ # sys.path path, will be prepended to sys.path if present.
11
+ prepend_sys_path = .
12
+
13
+ # timezone to use when rendering the date within the migration file
14
+ # as well as the filename.
15
+ # If specified, requires the python-dateutil library that can be
16
+ # installed by adding `alembic[tz]` to the pip requirements
17
+ # string value is passed to dateutil.tz.gettz()
18
+ # leave blank for localtime
19
+ # timezone =
20
+
21
+ # max length of characters to apply to the
22
+ # "slug" field
23
+ # truncate_slug_length = 40
24
+
25
+ # set to 'true' to run the environment during
26
+ # the 'revision' command, regardless of autogenerate
27
+ # revision_environment = false
28
+
29
+ # set to 'true' to allow .pyc and .pyo files without
30
+ # a source .py file to be detected as revisions in the
31
+ # versions/ directory
32
+ # sourceless = false
33
+
34
+ # version location specification; This defaults
35
+ # to alembic/versions. When using multiple version
36
+ # directories, initial revisions must be specified with --version-path.
37
+ # The path separator used here should be the separator specified by "version_path_separator" below.
38
+ # version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
39
+
40
+ # version path separator; As mentioned above, this is the character used to split
41
+ # version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
42
+ # If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
43
+ # Valid values for version_path_separator are:
44
+ #
45
+ # version_path_separator = :
46
+ # version_path_separator = ;
47
+ # version_path_separator = space
48
+ version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
49
+
50
+ # the output encoding used when revision files
51
+ # are written from script.py.mako
52
+ # output_encoding = utf-8
53
+
54
+ # sqlalchemy.url is loaded from environment variables in env.py
55
+ # Do not set it here to avoid conflicts
56
+ # sqlalchemy.url =
57
+ # If you need to set it here, uncomment and use the NeonDB URL:
58
+ # sqlalchemy.url = postgresql://neondb_owner:npg_52aqEhMxcFpS@ep-falling-sky-ai3kxfa4-pooler.c-4.us-east-1.aws.neon.tech/neondb?sslmode=require&channel_binding=require
59
+
60
+
61
+ [post_write_hooks]
62
+ # post_write_hooks defines scripts or Python functions that are run
63
+ # on newly generated revision scripts. See the documentation for further
64
+ # detail and examples
65
+
66
+ # format using "black" - use the console_scripts runner, against the "black" entrypoint
67
+ # hooks = black
68
+ # black.type = console_scripts
69
+ # black.entrypoint = black
70
+ # black.options = -l 79 REVISION_SCRIPT_FILENAME
71
+
72
+ # Logging configuration
73
+ [loggers]
74
+ keys = root,sqlalchemy,alembic
75
+
76
+ [handlers]
77
+ keys = console
78
+
79
+ [formatters]
80
+ keys = generic
81
+
82
+ [logger_root]
83
+ level = WARN
84
+ handlers = console
85
+ qualname =
86
+
87
+ [logger_sqlalchemy]
88
+ level = WARN
89
+ handlers =
90
+ qualname = sqlalchemy.engine
91
+
92
+ [logger_alembic]
93
+ level = INFO
94
+ handlers =
95
+ qualname = alembic
96
+
97
+ [handler_console]
98
+ class = StreamHandler
99
+ args = (sys.stderr,)
100
+ level = NOTSET
101
+ formatter = generic
102
+
103
+ [formatter_generic]
104
+ format = %(levelname)-5.5s [%(name)s] %(message)s
105
+ datefmt = %H:%M:%S
phase-1-core-infra/backend/alembic/env.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from logging.config import fileConfig
3
+ from sqlalchemy import engine_from_config
4
+ from sqlalchemy import pool
5
+ from alembic import context
6
+ import os
7
+ from dotenv import load_dotenv
8
+ from pathlib import Path
9
+
10
+ # Only load environment variables from .env files if DATABASE_URL is not already set
11
+ # This ensures Docker environment variables take precedence over .env files
12
+ if not os.getenv("DATABASE_URL"):
13
+ # Explicitly load environment variables from the backend directory first
14
+ # __file__ is the path to this env.py file in the alembic directory
15
+ # So we need to go up one level to get to the backend directory
16
+ backend_dir = Path(__file__).parent # This is the alembic directory
17
+ backend_dir = backend_dir.parent # This is the backend directory
18
+ backend_env = backend_dir / ".env"
19
+
20
+ # Load the backend .env file explicitly
21
+ if backend_env.exists():
22
+ print(f"Loading environment from: {backend_env}")
23
+ load_dotenv(backend_env, override=True) # Override any existing values
24
+ else:
25
+ print(f"Backend .env file not found at: {backend_env}")
26
+ # Fallback to parent directory (project root)
27
+ root_env = backend_dir.parent / ".env"
28
+ if root_env.exists():
29
+ print(f"Loading environment from: {root_env}")
30
+ load_dotenv(root_env, override=True)
31
+ else:
32
+ print(f"Root .env file not found at: {root_env}")
33
+ else:
34
+ print(f"DATABASE_URL already set, skipping .env file loading")
35
+
36
+ # this is the Alembic Config object
37
+ config = context.config
38
+
39
+ # Interpret the config file for Python logging
40
+ if config.config_file_name is not None:
41
+ fileConfig(config.config_file_name)
42
+
43
+ # Set sqlalchemy.url from environment variable
44
+ database_url = os.getenv("DATABASE_URL")
45
+ print(f"Retrieved DATABASE_URL: {database_url}")
46
+
47
+ if database_url:
48
+ # Convert asyncpg to regular postgresql for migrations
49
+ sync_database_url = database_url.replace("postgresql+asyncpg://", "postgresql://")
50
+ config.set_main_option("sqlalchemy.url", sync_database_url)
51
+ print(f"Using database URL: {sync_database_url}")
52
+ else:
53
+ raise ValueError("DATABASE_URL environment variable not found!")
54
+
55
+ # add your model's MetaData object here for 'autogenerate' support
56
+ from src.database import Base
57
+ target_metadata = Base.metadata
58
+
59
+ def run_migrations_offline() -> None:
60
+ """Run migrations in 'offline' mode."""
61
+ url = config.get_main_option("sqlalchemy.url")
62
+ context.configure(
63
+ url=url,
64
+ target_metadata=target_metadata,
65
+ literal_binds=True,
66
+ dialect_opts={"paramstyle": "named"},
67
+ )
68
+
69
+ with context.begin_transaction():
70
+ context.run_migrations()
71
+
72
+
73
+ def run_migrations_online() -> None:
74
+ """Run migrations in 'online' mode."""
75
+ connectable = engine_from_config(
76
+ config.get_section(config.config_ini_section),
77
+ prefix="sqlalchemy.",
78
+ poolclass=pool.NullPool,
79
+ )
80
+
81
+ with connectable.connect() as connection:
82
+ context.configure(
83
+ connection=connection, target_metadata=target_metadata
84
+ )
85
+
86
+ with context.begin_transaction():
87
+ context.run_migrations()
88
+
89
+
90
+ if context.is_offline_mode():
91
+ run_migrations_offline()
92
+ else:
93
+ run_migrations_online()
phase-1-core-infra/backend/alembic/env_fresh.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from logging.config import fileConfig
2
+ from sqlalchemy import engine_from_config
3
+ from sqlalchemy import pool
4
+ from alembic import context
5
+ import os
6
+ from dotenv import load_dotenv
7
+ from pathlib import Path
8
+
9
+ # Load environment variables from the backend directory
10
+ backend_dir = Path(__file__).parent # alembic directory -> backend directory
11
+ backend_env = backend_dir / ".env"
12
+ if backend_env.exists():
13
+ load_dotenv(backend_env)
14
+ else:
15
+ # Fallback to parent directory
16
+ root_env = backend_dir.parent.parent / ".env"
17
+ if root_env.exists():
18
+ load_dotenv(root_env)
19
+
20
+ # this is the Alembic Config object
21
+ config = context.config
22
+
23
+ # Interpret the config file for Python logging
24
+ if config.config_file_name is not None:
25
+ fileConfig(config.config_file_name)
26
+
27
+ # Set sqlalchemy.url from environment variable
28
+ database_url = os.getenv("DATABASE_URL")
29
+ if database_url:
30
+ # Convert asyncpg to regular postgresql for migrations
31
+ sync_database_url = database_url.replace("postgresql+asyncpg://", "postgresql://")
32
+ config.set_main_option("sqlalchemy.url", sync_database_url)
33
+ print(f"Using database URL: {sync_database_url}")
34
+ else:
35
+ raise ValueError("DATABASE_URL environment variable not found!")
36
+
37
+ # add your model's MetaData object here for 'autogenerate' support
38
+ from src.database import Base
39
+ target_metadata = Base.metadata
40
+
41
+ def run_migrations_offline() -> None:
42
+ """Run migrations in 'offline' mode."""
43
+ url = config.get_main_option("sqlalchemy.url")
44
+ context.configure(
45
+ url=url,
46
+ target_metadata=target_metadata,
47
+ literal_binds=True,
48
+ dialect_opts={"paramstyle": "named"},
49
+ )
50
+
51
+ with context.begin_transaction():
52
+ context.run_migrations()
53
+
54
+
55
+ def run_migrations_online() -> None:
56
+ """Run migrations in 'online' mode."""
57
+ connectable = engine_from_config(
58
+ config.get_section(config.config_ini_section),
59
+ prefix="sqlalchemy.",
60
+ poolclass=pool.NullPool,
61
+ )
62
+
63
+ with connectable.connect() as connection:
64
+ context.configure(
65
+ connection=connection, target_metadata=target_metadata
66
+ )
67
+
68
+ with context.begin_transaction():
69
+ context.run_migrations()
70
+
71
+
72
+ if context.is_offline_mode():
73
+ run_migrations_offline()
74
+ else:
75
+ run_migrations_online()
phase-1-core-infra/backend/alembic/script.py.mako ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """${message}
2
+
3
+ Revision ID: ${up_revision}
4
+ Revises: ${down_revision | comma,n}
5
+ Create Date: ${create_date}
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ ${imports if imports else ""}
11
+
12
+ # revision identifiers, used by Alembic.
13
+ revision = ${repr(up_revision)}
14
+ down_revision = ${repr(down_revision)}
15
+ branch_labels = ${repr(branch_labels)}
16
+ depends_on = ${repr(depends_on)}
17
+
18
+
19
+ def upgrade() -> None:
20
+ ${upgrades if upgrades else "pass"}
21
+
22
+
23
+ def downgrade() -> None:
24
+ ${downgrades if downgrades else "pass"}
phase-1-core-infra/backend/alembic/versions/001_initial_schema.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Initial schema for Phase 1
2
+
3
+ Revision ID: 001_initial_schema
4
+ Revises:
5
+ Create Date: 2026-02-11
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+ # revision identifiers, used by Alembic.
13
+ revision = '001_initial_schema'
14
+ down_revision = None
15
+ branch_labels = None
16
+ depends_on = None
17
+
18
+
19
+ def upgrade() -> None:
20
+ # Enable UUID extension
21
+ op.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
22
+
23
+ # Create users table
24
+ op.create_table(
25
+ 'users',
26
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('uuid_generate_v4()')),
27
+ sa.Column('email', sa.String(255), nullable=False, unique=True),
28
+ sa.Column('hashed_password', sa.String(255), nullable=False),
29
+ sa.Column('name', sa.String(255), nullable=False),
30
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
31
+ sa.Column('last_login_at', sa.DateTime(timezone=True), nullable=True),
32
+ )
33
+ op.create_index('ix_users_email', 'users', ['email'])
34
+ op.create_index('ix_users_created_at', 'users', ['created_at'])
35
+
36
+ # Create social_accounts table
37
+ op.create_table(
38
+ 'social_accounts',
39
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('uuid_generate_v4()')),
40
+ sa.Column('user_id', postgresql.UUID(as_uuid=True), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
41
+ sa.Column('platform', sa.String(50), nullable=False),
42
+ sa.Column('platform_user_id', sa.String(255), nullable=False),
43
+ sa.Column('access_token', sa.Text, nullable=False),
44
+ sa.Column('refresh_token', sa.Text, nullable=True),
45
+ sa.Column('token_expires_at', sa.DateTime(timezone=True), nullable=True),
46
+ sa.Column('connection_status', sa.String(50), nullable=False, server_default='active'),
47
+ sa.Column('last_sync_at', sa.DateTime(timezone=True), nullable=True),
48
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
49
+ )
50
+ op.create_index('ix_social_accounts_user_id', 'social_accounts', ['user_id'])
51
+ op.create_index('ix_social_accounts_connection_status', 'social_accounts', ['connection_status'])
52
+ op.create_unique_constraint('uq_platform_user', 'social_accounts', ['platform', 'platform_user_id'])
53
+
54
+ # Create draft_posts table
55
+ op.create_table(
56
+ 'draft_posts',
57
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('uuid_generate_v4()')),
58
+ sa.Column('user_id', postgresql.UUID(as_uuid=True), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
59
+ sa.Column('social_account_id', postgresql.UUID(as_uuid=True), sa.ForeignKey('social_accounts.id', ondelete='CASCADE'), nullable=False),
60
+ sa.Column('content', sa.Text, nullable=False),
61
+ sa.Column('target_platform', sa.String(50), nullable=False),
62
+ sa.Column('status', sa.String(50), nullable=False, server_default='draft'),
63
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
64
+ sa.Column('scheduled_at', sa.DateTime(timezone=True), nullable=True),
65
+ sa.Column('published_at', sa.DateTime(timezone=True), nullable=True),
66
+ sa.Column('media_urls', postgresql.JSONB, nullable=True),
67
+ )
68
+ op.create_index('ix_draft_posts_user_id', 'draft_posts', ['user_id'])
69
+ op.create_index('ix_draft_posts_social_account_id', 'draft_posts', ['social_account_id'])
70
+ op.create_index('ix_draft_posts_status', 'draft_posts', ['status'])
71
+ op.create_index('ix_draft_posts_scheduled_at', 'draft_posts', ['scheduled_at'])
72
+
73
+
74
+ def downgrade() -> None:
75
+ op.drop_table('draft_posts')
76
+ op.drop_table('social_accounts')
77
+ op.drop_table('users')
78
+ op.execute('DROP EXTENSION IF EXISTS "uuid-ossp"')
phase-1-core-infra/backend/alembic/versions/002_add_encryption_columns.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Add encryption columns to social_accounts table
2
+
3
+ Revision ID: 002_add_encryption
4
+ Revises: 001_initial_schema
5
+ Create Date: 2026-02-12
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+
11
+
12
+ # revision identifiers, used by Alembic.
13
+ revision = '002_add_encryption'
14
+ down_revision = '001_initial_schema'
15
+ branch_labels = None
16
+ depends_on = None
17
+
18
+
19
+ def upgrade() -> None:
20
+ """Add encryption columns to social_accounts table for AES-256 token encryption."""
21
+
22
+ # Add new columns for encrypted tokens
23
+ op.add_column('social_accounts',
24
+ sa.Column('encrypted_access_token', sa.Text(), nullable=True)
25
+ )
26
+ op.add_column('social_accounts',
27
+ sa.Column('encrypted_refresh_token', sa.Text(), nullable=True)
28
+ )
29
+ op.add_column('social_accounts',
30
+ sa.Column('encryption_key_version', sa.Integer(), nullable=False, server_default='1')
31
+ )
32
+
33
+ # Create index for key rotation queries
34
+ op.create_index(
35
+ 'idx_social_accounts_encryption_key_version',
36
+ 'social_accounts',
37
+ ['encryption_key_version']
38
+ )
39
+
40
+ # Note: Migration of existing plaintext tokens to encrypted format
41
+ # should be done via separate script (migrate_encrypt_tokens.py)
42
+ # After migration is verified, drop old plaintext columns:
43
+ # op.drop_column('social_accounts', 'access_token')
44
+ # op.drop_column('social_accounts', 'refresh_token')
45
+
46
+
47
+ def downgrade() -> None:
48
+ """Remove encryption columns from social_accounts table."""
49
+
50
+ op.drop_index('idx_social_accounts_encryption_key_version', table_name='social_accounts')
51
+ op.drop_column('social_accounts', 'encryption_key_version')
52
+ op.drop_column('social_accounts', 'encrypted_refresh_token')
53
+ op.drop_column('social_accounts', 'encrypted_access_token')
phase-1-core-infra/backend/alembic/versions/003_create_rate_limit_rules.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create rate_limit_rules table
2
+
3
+ Revision ID: 003_rate_limit_rules
4
+ Revises: 002_add_encryption
5
+ Create Date: 2026-02-12
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+
13
+ # revision identifiers, used by Alembic.
14
+ revision = '003_rate_limit_rules'
15
+ down_revision = '002_add_encryption'
16
+ branch_labels = None
17
+ depends_on = None
18
+
19
+
20
+ def upgrade() -> None:
21
+ """Create rate_limit_rules table for configuring rate limiting per endpoint."""
22
+
23
+ op.create_table(
24
+ 'rate_limit_rules',
25
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
26
+ sa.Column('endpoint_pattern', sa.String(255), nullable=False, unique=True),
27
+ sa.Column('requests_per_minute', sa.Integer(), nullable=False),
28
+ sa.Column('requests_per_hour', sa.Integer(), nullable=True),
29
+ sa.Column('tier_overrides', postgresql.JSONB(), nullable=False, server_default='{}'),
30
+ sa.Column('enabled', sa.Boolean(), nullable=False, server_default='true'),
31
+ sa.Column('description', sa.Text(), nullable=True),
32
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
33
+ sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
34
+ sa.CheckConstraint('requests_per_minute > 0', name='check_requests_per_minute_positive'),
35
+ sa.CheckConstraint(
36
+ 'requests_per_hour IS NULL OR requests_per_hour >= requests_per_minute * 60',
37
+ name='check_requests_per_hour_valid'
38
+ )
39
+ )
40
+
41
+ # Create indexes for efficient lookups
42
+ op.create_index('idx_rate_limit_rules_endpoint', 'rate_limit_rules', ['endpoint_pattern'])
43
+ op.create_index('idx_rate_limit_rules_enabled', 'rate_limit_rules', ['enabled'])
44
+
45
+
46
+ def downgrade() -> None:
47
+ """Drop rate_limit_rules table."""
48
+
49
+ op.drop_index('idx_rate_limit_rules_enabled', table_name='rate_limit_rules')
50
+ op.drop_index('idx_rate_limit_rules_endpoint', table_name='rate_limit_rules')
51
+ op.drop_table('rate_limit_rules')
phase-1-core-infra/backend/alembic/versions/004_create_rate_limit_violations.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create rate_limit_violations table
2
+
3
+ Revision ID: 004_rate_limit_violations
4
+ Revises: 003_rate_limit_rules
5
+ Create Date: 2026-02-12
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+
13
+ # revision identifiers, used by Alembic.
14
+ revision = '004_rate_limit_violations'
15
+ down_revision = '003_rate_limit_rules'
16
+ branch_labels = None
17
+ depends_on = None
18
+
19
+
20
+ def upgrade() -> None:
21
+ """Create rate_limit_violations table for logging rate limit violations."""
22
+
23
+ op.create_table(
24
+ 'rate_limit_violations',
25
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
26
+ sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
27
+ sa.Column('ip_address', sa.String(45), nullable=False),
28
+ sa.Column('endpoint', sa.String(255), nullable=False),
29
+ sa.Column('requests_made', sa.Integer(), nullable=False),
30
+ sa.Column('limit_allowed', sa.Integer(), nullable=False),
31
+ sa.Column('window_seconds', sa.Integer(), nullable=False),
32
+ sa.Column('user_agent', sa.Text(), nullable=True),
33
+ sa.Column('violated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
34
+ sa.CheckConstraint('requests_made > limit_allowed', name='check_violation_exceeded'),
35
+ sa.CheckConstraint('window_seconds > 0', name='check_window_positive')
36
+ )
37
+
38
+ # Add foreign key to users table (nullable for unauthenticated requests)
39
+ op.create_foreign_key(
40
+ 'fk_rate_limit_violations_user_id',
41
+ 'rate_limit_violations',
42
+ 'users',
43
+ ['user_id'],
44
+ ['id'],
45
+ ondelete='SET NULL'
46
+ )
47
+
48
+ # Create indexes for efficient queries
49
+ op.create_index('idx_rate_limit_violations_user_id', 'rate_limit_violations', ['user_id'])
50
+ op.create_index('idx_rate_limit_violations_ip', 'rate_limit_violations', ['ip_address'])
51
+ op.create_index('idx_rate_limit_violations_violated_at', 'rate_limit_violations', ['violated_at'])
52
+
53
+
54
+ def downgrade() -> None:
55
+ """Drop rate_limit_violations table."""
56
+
57
+ op.drop_index('idx_rate_limit_violations_violated_at', table_name='rate_limit_violations')
58
+ op.drop_index('idx_rate_limit_violations_ip', table_name='rate_limit_violations')
59
+ op.drop_index('idx_rate_limit_violations_user_id', table_name='rate_limit_violations')
60
+ op.drop_constraint('fk_rate_limit_violations_user_id', 'rate_limit_violations', type_='foreignkey')
61
+ op.drop_table('rate_limit_violations')
phase-1-core-infra/backend/alembic/versions/005_create_admin_metrics.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create admin_metrics table
2
+
3
+ Revision ID: 005_admin_metrics
4
+ Revises: 004_rate_limit_violations
5
+ Create Date: 2026-02-12
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+
13
+ # revision identifiers, used by Alembic.
14
+ revision = '005_admin_metrics'
15
+ down_revision = '004_rate_limit_violations'
16
+ branch_labels = None
17
+ depends_on = None
18
+
19
+
20
+ def upgrade() -> None:
21
+ """Create admin_metrics table for pre-computed dashboard metrics."""
22
+
23
+ op.create_table(
24
+ 'admin_metrics',
25
+ sa.Column('metric_date', sa.Date(), primary_key=True),
26
+ sa.Column('total_users', sa.Integer(), nullable=False, server_default='0'),
27
+ sa.Column('active_subscriptions', sa.Integer(), nullable=False, server_default='0'),
28
+ sa.Column('new_users_today', sa.Integer(), nullable=False, server_default='0'),
29
+ sa.Column('mrr_cents', sa.BigInteger(), nullable=False, server_default='0'),
30
+ sa.Column('total_revenue_cents', sa.BigInteger(), nullable=False, server_default='0'),
31
+ sa.Column('openai_tokens_used', sa.BigInteger(), nullable=False, server_default='0'),
32
+ sa.Column('veo_videos_generated', sa.Integer(), nullable=False, server_default='0'),
33
+ sa.Column('ai_cost_cents', sa.BigInteger(), nullable=False, server_default='0'),
34
+ sa.Column('posts_published', sa.Integer(), nullable=False, server_default='0'),
35
+ sa.Column('approval_rate_percent', sa.Numeric(5, 2), nullable=False, server_default='0'),
36
+ sa.Column('avg_response_time_ms', sa.Integer(), nullable=False, server_default='0'),
37
+ sa.Column('error_count', sa.Integer(), nullable=False, server_default='0'),
38
+ sa.Column('computed_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()'))
39
+ )
40
+
41
+ # Create index for time-series queries
42
+ op.create_index('idx_admin_metrics_date', 'admin_metrics', ['metric_date'])
43
+
44
+
45
+ def downgrade() -> None:
46
+ """Drop admin_metrics table."""
47
+
48
+ op.drop_index('idx_admin_metrics_date', table_name='admin_metrics')
49
+ op.drop_table('admin_metrics')
phase-1-core-infra/backend/alembic/versions/007_better_auth_schema.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create Better Auth schema
2
+
3
+ Revision ID: 007_better_auth_schema
4
+ Revises: 005_create_admin_metrics
5
+ Create Date: 2026-02-12
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+ # revision identifiers, used by Alembic.
13
+ revision = '007_better_auth_schema'
14
+ down_revision = '005_admin_metrics'
15
+ branch_labels = None
16
+ depends_on = None
17
+
18
+
19
+ def upgrade() -> None:
20
+ # Alter existing users table to add Better Auth fields
21
+ op.add_column('users', sa.Column('email_verified', sa.Boolean(), nullable=False, server_default='false'))
22
+ op.add_column('users', sa.Column('image', sa.Text(), nullable=True))
23
+ op.add_column('users', sa.Column('is_admin', sa.Boolean(), nullable=False, server_default='false'))
24
+ op.add_column('users', sa.Column('clerk_id', sa.String(255), nullable=True, unique=True))
25
+ op.add_column('users', sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')))
26
+
27
+ # Rename hashed_password to password_hash for Better Auth compatibility
28
+ op.alter_column('users', 'hashed_password', new_column_name='password_hash')
29
+
30
+ # Create index for clerk_id
31
+ op.create_index('idx_users_clerk_id', 'users', ['clerk_id'])
32
+
33
+ # Create sessions table
34
+ op.create_table(
35
+ 'sessions',
36
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
37
+ sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
38
+ sa.Column('token', sa.Text(), nullable=False, unique=True),
39
+ sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
40
+ sa.Column('ip_address', sa.String(45), nullable=True),
41
+ sa.Column('user_agent', sa.Text(), nullable=True),
42
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
43
+ sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
44
+ )
45
+
46
+ # Create indexes for sessions table
47
+ op.create_index('idx_sessions_user_id', 'sessions', ['user_id'])
48
+ op.create_index('idx_sessions_token', 'sessions', ['token'])
49
+ op.create_index('idx_sessions_expires_at', 'sessions', ['expires_at'])
50
+
51
+ # Create accounts table (OAuth providers)
52
+ op.create_table(
53
+ 'accounts',
54
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
55
+ sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
56
+ sa.Column('provider', sa.String(50), nullable=False),
57
+ sa.Column('provider_account_id', sa.String(255), nullable=False),
58
+ sa.Column('access_token', sa.Text(), nullable=True),
59
+ sa.Column('refresh_token', sa.Text(), nullable=True),
60
+ sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
61
+ sa.Column('token_type', sa.String(50), nullable=True),
62
+ sa.Column('scope', sa.Text(), nullable=True),
63
+ sa.Column('id_token', sa.Text(), nullable=True),
64
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
65
+ sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
66
+ sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
67
+ sa.UniqueConstraint('provider', 'provider_account_id', name='uq_provider_account'),
68
+ )
69
+
70
+ # Create indexes for accounts table
71
+ op.create_index('idx_accounts_user_id', 'accounts', ['user_id'])
72
+ op.create_index('idx_accounts_provider', 'accounts', ['provider', 'provider_account_id'])
73
+
74
+ # Create verification_tokens table
75
+ op.create_table(
76
+ 'verification_tokens',
77
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
78
+ sa.Column('identifier', sa.String(255), nullable=False),
79
+ sa.Column('token', sa.Text(), nullable=False, unique=True),
80
+ sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
81
+ sa.Column('type', sa.String(50), nullable=False),
82
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('NOW()')),
83
+ )
84
+
85
+ # Create indexes for verification_tokens table
86
+ op.create_index('idx_verification_tokens_token', 'verification_tokens', ['token'])
87
+ op.create_index('idx_verification_tokens_identifier', 'verification_tokens', ['identifier'])
88
+
89
+
90
+ def downgrade() -> None:
91
+ # Drop new tables
92
+ op.drop_table('verification_tokens')
93
+ op.drop_table('accounts')
94
+ op.drop_table('sessions')
95
+
96
+ # Revert users table changes
97
+ op.drop_index('idx_users_clerk_id', 'users')
98
+ op.alter_column('users', 'password_hash', new_column_name='hashed_password')
99
+ op.drop_column('users', 'updated_at')
100
+ op.drop_column('users', 'clerk_id')
101
+ op.drop_column('users', 'is_admin')
102
+ op.drop_column('users', 'image')
103
+ op.drop_column('users', 'email_verified')
phase-1-core-infra/backend/alembic/versions/008_add_post_logs_and_twitter.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Add post_logs table and Twitter platform support
2
+
3
+ Revision ID: 008_add_post_logs_and_twitter
4
+ Revises: 007_better_auth_schema
5
+ Create Date: 2026-03-01
6
+
7
+ """
8
+ from alembic import op
9
+ import sqlalchemy as sa
10
+ from sqlalchemy.dialects import postgresql
11
+
12
+ # revision identifiers
13
+ revision = '008_add_post_logs_and_twitter'
14
+ down_revision = '007_better_auth_schema'
15
+ branch_labels = None
16
+ depends_on = None
17
+
18
+
19
+ def upgrade() -> None:
20
+ # Make social_account_id nullable in draft_posts (posts can exist without a specific account)
21
+ op.alter_column('draft_posts', 'social_account_id', nullable=True)
22
+
23
+ # Create post_logs table
24
+ # platform and status stored as VARCHAR(50) - consistent with existing schema
25
+ op.create_table(
26
+ 'post_logs',
27
+ sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True),
28
+ sa.Column('post_id', postgresql.UUID(as_uuid=True),
29
+ sa.ForeignKey('draft_posts.id', ondelete='CASCADE'), nullable=False),
30
+ sa.Column('user_id', postgresql.UUID(as_uuid=True),
31
+ sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
32
+ sa.Column('platform', sa.String(50), nullable=False),
33
+ sa.Column('status', sa.String(50), nullable=False, server_default='failed'),
34
+ sa.Column('message', sa.Text(), nullable=True),
35
+ sa.Column('platform_post_id', sa.String(255), nullable=True),
36
+ sa.Column('created_at', sa.DateTime(timezone=True), nullable=False,
37
+ server_default=sa.text('NOW()')),
38
+ )
39
+
40
+ op.create_index('ix_post_logs_post_id', 'post_logs', ['post_id'])
41
+ op.create_index('ix_post_logs_user_id', 'post_logs', ['user_id'])
42
+ op.create_index('ix_post_logs_status', 'post_logs', ['status'])
43
+
44
+
45
+ def downgrade() -> None:
46
+ op.drop_index('ix_post_logs_status', table_name='post_logs')
47
+ op.drop_index('ix_post_logs_user_id', table_name='post_logs')
48
+ op.drop_index('ix_post_logs_post_id', table_name='post_logs')
49
+ op.drop_table('post_logs')
50
+ op.alter_column('draft_posts', 'social_account_id', nullable=False)
phase-1-core-infra/backend/pyproject.toml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.black]
2
+ line-length = 100
3
+ target-version = ['py311']
4
+ include = '\.pyi?$'
5
+
6
+ [tool.isort]
7
+ profile = "black"
8
+ line_length = 100
9
+
10
+ [tool.mypy]
11
+ python_version = "3.11"
12
+ warn_return_any = true
13
+ warn_unused_configs = true
14
+ disallow_untyped_defs = true
15
+ ignore_missing_imports = true
16
+
17
+ [tool.pytest.ini_options]
18
+ testpaths = ["tests"]
19
+ python_files = ["test_*.py"]
20
+ python_classes = ["Test*"]
21
+ python_functions = ["test_*"]
22
+ asyncio_mode = "auto"
phase-1-core-infra/backend/requirements-dev.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Testing
2
+ pytest==7.4.3
3
+ pytest-asyncio==0.21.1
4
+ pytest-cov==4.1.0
5
+ httpx==0.25.2
6
+
7
+ # Linting and formatting
8
+ black==23.12.0
9
+ flake8==6.1.0
10
+ mypy==1.7.1
11
+ isort==5.13.2
12
+
13
+ # Type stubs
14
+ types-passlib==1.7.7.20260211
phase-1-core-infra/backend/requirements.txt ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FastAPI and ASGI server
2
+ fastapi>=0.104.1
3
+ uvicorn[standard]>=0.24.0
4
+ python-multipart>=0.0.6
5
+
6
+ # Database
7
+ sqlalchemy[asyncio]>=2.0.23
8
+ asyncpg>=0.29.0
9
+ psycopg2-binary>=2.9.9
10
+ alembic>=1.13.0
11
+
12
+ # Data validation
13
+ pydantic>=2.5.0
14
+ pydantic-settings>=2.1.0
15
+ email-validator>=2.1.0
16
+
17
+ # Authentication and security
18
+ passlib[bcrypt]>=1.7.4
19
+ pyjwt>=2.8.0
20
+ cryptography>=41.0.7
21
+
22
+ # HTTP client for OAuth
23
+ httpx>=0.25.2
24
+
25
+ # Environment variables
26
+ python-dotenv>=1.0.0
27
+
28
+ # Rate limiting
29
+ slowapi>=0.1.9
30
+
31
+ # Redis client
32
+ redis>=5.0.1
33
+
34
+ # AI Content Generation
35
+ openai>=1.12.0
36
+ google-generativeai>=0.4.0