@polymorphism-tech/morph-spec 4.9.0 → 4.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +2 -2
  2. package/bin/morph-spec.js +30 -0
  3. package/bin/task-manager.js +34 -22
  4. package/claude-plugin.json +1 -1
  5. package/docs/CHEATSHEET.md +1 -1
  6. package/docs/QUICKSTART.md +1 -1
  7. package/framework/CLAUDE.md +35 -98
  8. package/framework/agents/backend/api-designer.md +3 -0
  9. package/framework/agents/backend/dotnet-senior.md +3 -0
  10. package/framework/agents/backend/ef-modeler.md +2 -0
  11. package/framework/agents/backend/hangfire-orchestrator.md +2 -0
  12. package/framework/agents/backend/ms-agent-expert.md +2 -0
  13. package/framework/agents/frontend/blazor-builder.md +2 -0
  14. package/framework/agents/frontend/nextjs-expert.md +2 -0
  15. package/framework/agents/infrastructure/azure-architect.md +2 -0
  16. package/framework/agents/infrastructure/azure-deploy-specialist.md +2 -0
  17. package/framework/agents/infrastructure/bicep-architect.md +2 -0
  18. package/framework/agents/infrastructure/container-specialist.md +2 -0
  19. package/framework/agents/infrastructure/devops-engineer.md +3 -0
  20. package/framework/agents/infrastructure/infra-architect.md +3 -0
  21. package/framework/agents/integrations/asaas-financial.md +2 -0
  22. package/framework/agents/integrations/azure-identity.md +2 -0
  23. package/framework/agents/integrations/clerk-auth.md +3 -0
  24. package/framework/agents/integrations/hangfire-integration.md +2 -0
  25. package/framework/agents/integrations/resend-email.md +2 -0
  26. package/framework/agents.json +37 -7
  27. package/framework/commands/commit.md +166 -0
  28. package/framework/commands/morph-apply.md +156 -155
  29. package/framework/commands/morph-archive.md +33 -27
  30. package/framework/commands/morph-infra.md +83 -77
  31. package/framework/commands/morph-preflight.md +97 -55
  32. package/framework/commands/morph-proposal.md +131 -58
  33. package/framework/commands/morph-status.md +36 -30
  34. package/framework/commands/morph-troubleshoot.md +68 -59
  35. package/framework/hooks/claude-code/notification/approval-reminder.js +3 -2
  36. package/framework/hooks/claude-code/post-tool-use/dispatch.js +154 -31
  37. package/framework/hooks/claude-code/post-tool-use/skill-reminder.js +7 -84
  38. package/framework/hooks/claude-code/post-tool-use/validator-feedback.js +8 -17
  39. package/framework/hooks/claude-code/pre-compact/save-morph-context.js +16 -3
  40. package/framework/hooks/claude-code/pre-tool-use/enforce-phase-writes.js +4 -3
  41. package/framework/hooks/claude-code/pre-tool-use/protect-spec-files.js +3 -2
  42. package/framework/hooks/claude-code/pre-tool-use/task-tracking-guard.js +60 -0
  43. package/framework/hooks/claude-code/session-start/inject-morph-context.js +55 -2
  44. package/framework/hooks/claude-code/session-start/post-compact-restore.js +41 -0
  45. package/framework/hooks/claude-code/stop/validate-completion.js +2 -15
  46. package/framework/hooks/claude-code/user-prompt/enrich-prompt.js +23 -5
  47. package/framework/hooks/shared/compact-restore.js +100 -0
  48. package/framework/hooks/shared/dispatch-helpers.js +116 -0
  49. package/framework/hooks/shared/phase-utils.js +9 -5
  50. package/framework/hooks/shared/state-reader.js +27 -3
  51. package/framework/phases.json +30 -7
  52. package/framework/rules/csharp-standards.md +3 -0
  53. package/framework/rules/frontend-standards.md +2 -0
  54. package/framework/rules/infrastructure-standards.md +3 -0
  55. package/framework/rules/morph-workflow.md +143 -86
  56. package/framework/rules/nextjs-standards.md +2 -0
  57. package/framework/rules/testing-standards.md +3 -0
  58. package/framework/skills/level-0-meta/mcp-registry.json +86 -51
  59. package/framework/skills/level-0-meta/morph-brainstorming/SKILL.md +139 -0
  60. package/framework/skills/level-0-meta/morph-checklist/SKILL.md +42 -19
  61. package/framework/skills/level-0-meta/{code-review → morph-code-review}/SKILL.md +8 -5
  62. package/framework/skills/level-0-meta/{code-review-nextjs → morph-code-review-nextjs}/SKILL.md +8 -6
  63. package/framework/skills/level-0-meta/morph-frontend-review/SKILL.md +362 -0
  64. package/framework/skills/level-0-meta/morph-init/SKILL.md +114 -20
  65. package/framework/skills/level-0-meta/morph-post-implementation/SKILL.md +362 -0
  66. package/framework/skills/level-0-meta/morph-replicate/SKILL.md +95 -87
  67. package/framework/skills/level-0-meta/{simulation-checklist → morph-simulation-checklist}/SKILL.md +24 -0
  68. package/framework/skills/level-0-meta/{tool-usage-guide → morph-tool-usage-guide}/SKILL.md +43 -43
  69. package/framework/skills/level-0-meta/{tool-usage-guide → morph-tool-usage-guide}/references/tools-per-phase.md +1 -2
  70. package/framework/skills/level-0-meta/{verification-before-completion → morph-verification-before-completion}/SKILL.md +23 -12
  71. package/framework/skills/level-0-meta/{verification-before-completion → morph-verification-before-completion}/scripts/check-phase-outputs.mjs +2 -2
  72. package/framework/skills/level-1-workflows/morph-phase-clarify/SKILL.md +247 -0
  73. package/framework/skills/level-1-workflows/morph-phase-codebase-analysis/SKILL.md +270 -0
  74. package/framework/skills/level-1-workflows/morph-phase-design/SKILL.md +499 -0
  75. package/framework/skills/level-1-workflows/morph-phase-implement/.morph/logs/activity.json +38 -0
  76. package/framework/skills/level-1-workflows/morph-phase-implement/SKILL.md +472 -0
  77. package/framework/skills/level-1-workflows/morph-phase-implement/prompts/code-quality-reviewer-prompt.md +50 -0
  78. package/framework/skills/level-1-workflows/morph-phase-implement/prompts/implementer-prompt.md +45 -0
  79. package/framework/skills/level-1-workflows/morph-phase-implement/prompts/spec-reviewer-prompt.md +47 -0
  80. package/framework/skills/level-1-workflows/morph-phase-plan/SKILL.md +246 -0
  81. package/framework/skills/level-1-workflows/morph-phase-setup/SKILL.md +238 -0
  82. package/framework/skills/level-1-workflows/morph-phase-tasks/.morph/logs/activity.json +14 -0
  83. package/framework/skills/level-1-workflows/morph-phase-tasks/SKILL.md +312 -0
  84. package/framework/skills/level-1-workflows/{phase-tasks → morph-phase-tasks}/scripts/validate-tasks.mjs +3 -3
  85. package/framework/skills/level-1-workflows/morph-phase-uiux/SKILL.md +324 -0
  86. package/framework/skills/level-1-workflows/morph-scope-escalation/SKILL.md +146 -0
  87. package/framework/standards/integration/mcp/mcp-tools.md +25 -7
  88. package/framework/templates/docs/onboarding.md +2 -2
  89. package/package.json +3 -4
  90. package/src/commands/agents/dispatch-agents.js +50 -3
  91. package/src/commands/mcp/mcp-setup.js +39 -2
  92. package/src/commands/phase/phase-reset.js +74 -0
  93. package/src/commands/project/doctor.js +26 -7
  94. package/src/commands/project/update.js +4 -4
  95. package/src/commands/scope/escalate.js +215 -0
  96. package/src/commands/state/advance-phase.js +27 -53
  97. package/src/commands/state/state.js +1 -1
  98. package/src/commands/task/expand.js +100 -0
  99. package/src/core/paths/output-schema.js +4 -3
  100. package/src/core/state/phase-state-machine.js +7 -4
  101. package/src/core/state/state-manager.js +4 -3
  102. package/src/lib/detectors/claude-config-detector.js +93 -347
  103. package/src/lib/detectors/design-system-detector.js +189 -189
  104. package/src/lib/detectors/index.js +155 -57
  105. package/src/lib/generators/context-generator.js +2 -2
  106. package/src/lib/installers/mcp-installer.js +37 -5
  107. package/src/lib/phase-chain/phase-validator.js +22 -16
  108. package/src/lib/scope/impact-analyzer.js +106 -0
  109. package/src/lib/stack-filter.js +58 -0
  110. package/src/lib/tasks/task-parser.js +1 -1
  111. package/src/lib/validators/shared/emit-validator-dispatch.js +64 -0
  112. package/src/scripts/setup-infra.js +68 -18
  113. package/src/utils/agents-installer.js +51 -17
  114. package/src/utils/claude-md-injector.js +90 -0
  115. package/src/utils/file-copier.js +0 -1
  116. package/src/utils/hooks-installer.js +16 -5
  117. package/src/utils/skills-installer.js +67 -7
  118. package/CLAUDE.md +0 -98
  119. package/framework/memory/patterns-learned.md +0 -766
  120. package/framework/skills/level-0-meta/brainstorming/SKILL.md +0 -137
  121. package/framework/skills/level-0-meta/frontend-review/SKILL.md +0 -359
  122. package/framework/skills/level-0-meta/post-implementation/SKILL.md +0 -362
  123. package/framework/skills/level-0-meta/terminal-title/SKILL.md +0 -61
  124. package/framework/skills/level-0-meta/terminal-title/scripts/set_title.sh +0 -65
  125. package/framework/skills/level-1-workflows/phase-clarify/SKILL.md +0 -216
  126. package/framework/skills/level-1-workflows/phase-codebase-analysis/SKILL.md +0 -252
  127. package/framework/skills/level-1-workflows/phase-design/SKILL.md +0 -383
  128. package/framework/skills/level-1-workflows/phase-implement/SKILL.md +0 -492
  129. package/framework/skills/level-1-workflows/phase-setup/SKILL.md +0 -195
  130. package/framework/skills/level-1-workflows/phase-tasks/SKILL.md +0 -271
  131. package/framework/skills/level-1-workflows/phase-uiux/SKILL.md +0 -286
  132. package/src/commands/project/index.js +0 -8
  133. package/src/core/index.js +0 -10
  134. package/src/core/state/index.js +0 -8
  135. package/src/core/templates/index.js +0 -9
  136. package/src/core/templates/template-data-sources.js +0 -325
  137. package/src/core/workflows/index.js +0 -7
  138. package/src/lib/detectors/config-detector.js +0 -223
  139. package/src/lib/detectors/standards-generator.js +0 -335
  140. package/src/lib/detectors/structure-detector.js +0 -275
  141. package/src/lib/monitor/agent-resolver.js +0 -144
  142. package/src/lib/monitor/renderer.js +0 -230
  143. package/src/lib/orchestration/index.js +0 -7
  144. package/src/lib/orchestration/team-orchestrator.js +0 -404
  145. package/src/sanitizer/context-sanitizer.js +0 -221
  146. package/src/sanitizer/patterns.js +0 -163
  147. package/src/writer/file-writer.js +0 -86
  148. /package/framework/skills/level-0-meta/{brainstorming → morph-brainstorming}/references/proposal-example.md +0 -0
  149. /package/framework/skills/level-0-meta/{code-review → morph-code-review}/references/review-example.md +0 -0
  150. /package/framework/skills/level-0-meta/{code-review → morph-code-review}/references/review-guidelines.md +0 -0
  151. /package/framework/skills/level-0-meta/{code-review → morph-code-review}/scripts/scan-csharp.mjs +0 -0
  152. /package/framework/skills/level-0-meta/{code-review-nextjs → morph-code-review-nextjs}/references/review-example-nextjs.md +0 -0
  153. /package/framework/skills/level-0-meta/{code-review-nextjs → morph-code-review-nextjs}/scripts/scan-nextjs.mjs +0 -0
  154. /package/framework/skills/level-0-meta/{frontend-review → morph-frontend-review}/scripts/scan-accessibility.mjs +0 -0
  155. /package/framework/skills/level-0-meta/{post-implementation → morph-post-implementation}/scripts/detect-dev-server.mjs +0 -0
  156. /package/framework/skills/level-0-meta/{post-implementation → morph-post-implementation}/scripts/detect-stack.mjs +0 -0
  157. /package/framework/skills/level-1-workflows/{phase-clarify → morph-phase-clarify}/references/clarifications-example.md +0 -0
  158. /package/framework/skills/level-1-workflows/{phase-design → morph-phase-design}/references/architecture-analysis-guide.md +0 -0
  159. /package/framework/skills/level-1-workflows/{phase-design → morph-phase-design}/references/spec-authoring-guide.md +0 -0
  160. /package/framework/skills/level-1-workflows/{phase-design → morph-phase-design}/references/spec-example.md +0 -0
  161. /package/framework/skills/level-1-workflows/{phase-implement → morph-phase-implement}/references/recap-example.md +0 -0
  162. /package/framework/skills/level-1-workflows/{phase-implement → morph-phase-implement}/references/vsa-implementation-guide.md +0 -0
  163. /package/framework/skills/level-1-workflows/{phase-tasks → morph-phase-tasks}/references/task-planning-patterns.md +0 -0
  164. /package/framework/skills/level-1-workflows/{phase-tasks → morph-phase-tasks}/references/tasks-example.md +0 -0
@@ -0,0 +1,247 @@
1
+ ---
2
+ name: morph:phase-clarify
3
+ description: MORPH-SPEC Phase 3 (Clarify). Iterative clarification loop driven by a satisfaction score (0-100). Generates targeted questions per round, always presents "End Clarify" as exit option, and stops when score >= 85 or user exits. Use after design approval to eliminate spec ambiguities before task breakdown begins.
4
+ argument-hint: "[feature-name]"
5
+ user-invocable: false
6
+ allowed-tools: Read, Write, Edit, Bash, Glob, Grep, AskUserQuestion
7
+ cliVersion: "4.10.1"
8
+ ---
9
+
10
+ # MORPH Clarify — Phase 3
11
+
12
+ > INTERNAL: Workflow skill used by /morph-proposal during automated phase orchestration. Not a user command.
13
+
14
+ Eliminate spec ambiguities through an iterative question loop until the spec has sufficient quality to generate tasks.
15
+
16
+ ## Prerequisites
17
+
18
+ - [ ] Phase 2 (Design) completed
19
+ - [ ] `spec.md` approved by user
20
+ - [ ] `contracts.cs` (or `contracts.ts`) defined
21
+
22
+ ## Recommended Tools
23
+
24
+ > **Ref:** `framework/skills/level-0-meta/morph-tool-usage-guide/SKILL.md` for complete guide.
25
+ > **Example:** `references/clarifications-example.md` — filled-in clarifications.md showing expected Q&A format.
26
+
27
+ | Action | Tool | Alternative |
28
+ |--------|------|-------------|
29
+ | Read spec + contracts | **Read** all design outputs | — |
30
+ | Verify technical feasibility | **Context7 MCP** `query_docs()` | **WebSearch** |
31
+ | Check external issues/limitations | **GitHub MCP** `search_issues()` | — |
32
+ | Ask clarifying questions | **AskUserQuestion** (never plain text) | — |
33
+ | Update spec with clarifications | **Edit** spec.md | — |
34
+ | Create clarifications.md | **Write** | — |
35
+ | Update state | **Bash** `npx morph-spec state mark-output $ARGUMENTS clarifications` | — |
36
+
37
+ ---
38
+
39
+ ## Satisfaction Scoring System
40
+
41
+ Before each round, calculate the **Satisfaction Score (0-100)** by evaluating the current spec:
42
+
43
+ | Dimension | Pts | Maximum Score Criteria |
44
+ |-----------|-----|----------------------|
45
+ | **Functional completeness** | 25 | All requirements have measurable acceptance criteria (no vague "should", "may", "generally") |
46
+ | **Failure coverage** | 20 | All error paths have defined behavior (invalid input, not found, timeout, unauthorized) |
47
+ | **Edge cases** | 20 | Boundaries, concurrent states, empty/extreme inputs documented |
48
+ | **Definitional clarity** | 20 | No critical term undefined, no "TBD", no "to be defined" |
49
+ | **Integration contracts** | 15 | External dependencies have payload format, errors, and retry policy defined |
50
+
51
+ **Thresholds:**
52
+
53
+ | Score | Action |
54
+ |-------|--------|
55
+ | >= 85 | Satisfied. Present results and offer "End Clarify" as the primary option |
56
+ | 70-84 | Continue. Focus questions on the lowest-scoring dimensions |
57
+ | < 70 | Continue. Broad questions covering the largest gaps |
58
+
59
+ > The score rises as answers are incorporated. There's no fixed maximum rounds — the loop ends when score >= 85 **or** the user chooses to end.
60
+
61
+ ---
62
+
63
+ ## Workflow
64
+
65
+ ### Initialization
66
+
67
+ **Step 0 (BEFORE any writing) — Ensure clarify phase:**
68
+
69
+ ```bash
70
+ npx morph-spec state get $ARGUMENTS
71
+ ```
72
+
73
+ Check the `"phase"` field in the output:
74
+
75
+ **If `"phase": "clarify"`** → correct phase, proceed.
76
+
77
+ **If `"phase": "design"`** → run in sequence:
78
+ 1. `npx morph-spec approve $ARGUMENTS design`
79
+ 2. `npx morph-spec phase advance $ARGUMENTS` (→ clarify)
80
+
81
+ **Any other value** → STOP — inconsistent state, report to user.
82
+
83
+ > **Rule:** Never write `clarifications.md` or update `spec.md` until the phase is `clarify`.
84
+
85
+ ---
86
+
87
+ Read the base files in PARALLEL:
88
+
89
+ ```
90
+ Read: .morph/features/$ARGUMENTS/1-design/spec.md
91
+ + Read: .morph/features/$ARGUMENTS/1-design/contracts.cs (or contracts.ts for TypeScript)
92
+ + Read: .morph/features/$ARGUMENTS/1-design/schema-analysis.md (if exists)
93
+ ```
94
+
95
+ Calculate the initial score. Track accumulated answers from prior rounds to avoid repeating already-answered questions.
96
+
97
+ ---
98
+
99
+ ### Main Loop
100
+
101
+ **Repeat until: score >= 85 OR user chooses to end.**
102
+
103
+ #### A. Calculate Current Round Score
104
+
105
+ Score each dimension based on the spec + accumulated answers. Note which dimensions are weakest — they guide this round's questions.
106
+
107
+ #### B. Identify New Questions
108
+
109
+ Analyze only what **hasn't been answered yet**. Categorize by dimension:
110
+
111
+ | Category | Example Questions |
112
+ |----------|-------------------|
113
+ | **Validation** | Field limits, accepted formats, business rules |
114
+ | **Behavior** | What happens when X fails? Empty state? Loading state? |
115
+ | **Priority** | Must-have vs nice-to-have? MVP vs v2? |
116
+ | **Integration** | Webhook vs polling? Retry policy? Timeout? |
117
+ | **Performance** | Expected volume? Response SLA? |
118
+ | **UX** | Error message shown to user? Visual feedback? |
119
+
120
+ #### C. Present Questions via `AskUserQuestion`
121
+
122
+ **Why this matters:** AskUserQuestion gives structured options the user can click — far better UX than walls of text. It also captures answers programmatically for the clarifications.md output.
123
+
124
+ **Rules:**
125
+
126
+ 1. Use `AskUserQuestion` — **NEVER list questions as plain text**
127
+ 2. Maximum **4 questions per call** (tool limit)
128
+ 3. If there are more than 3 new questions, make sequential calls — the **exit option always appears in the last call of each round**
129
+ 4. **Always include the following question as the last in the final batch of each round:**
130
+
131
+ ```json
132
+ {
133
+ "header": "End Clarify?",
134
+ "question": "End the Clarify phase now or continue refining the spec?",
135
+ "multiSelect": false,
136
+ "options": [
137
+ {
138
+ "label": "Continue refining",
139
+ "description": "There are still open questions identified in this round"
140
+ },
141
+ {
142
+ "label": "End Clarify",
143
+ "description": "Proceed to Plan phase with the spec in its current state"
144
+ }
145
+ ]
146
+ }
147
+ ```
148
+
149
+ > If score >= 85, adjust the first option label to: `"Continue (optional)"` with description: `"Score ${score}/100 — spec is satisfactory, but you can refine further"`
150
+
151
+ #### D. Wait and Incorporate Answers
152
+
153
+ **Do not proceed before receiving all responses.**
154
+
155
+ After receiving answers:
156
+ 1. Check if the answer is consistent with `contracts.cs` and `schema-analysis.md`
157
+ 2. If any answer is ambiguous or contradictory → ask for clarification before updating
158
+ 3. Update `spec.md` with:
159
+ - Section `## Clarifications (Phase 3)` with structured Q&A
160
+ - Updates to affected functional sections
161
+ - New edge cases in `## Edge Cases`
162
+
163
+ #### E. Check Exit Condition
164
+
165
+ - User chose **"End Clarify"** → exit loop
166
+ - Score >= 85 AND no new questions identified → exit loop
167
+ - Otherwise → return to A
168
+
169
+ ---
170
+
171
+ ### Post-Loop: Generate Outputs
172
+
173
+ **Step 1: Create `clarifications.md`**
174
+
175
+ Write to `.morph/features/$ARGUMENTS/1-design/clarifications.md`:
176
+
177
+ ```markdown
178
+ # Clarifications — {Feature Name}
179
+
180
+ **Phase:** Clarify (Phase 3)
181
+ **Rounds completed:** {N}
182
+ **Final score:** {score}/100
183
+ **Status:** Resolved — incorporated into spec.md
184
+
185
+ ---
186
+
187
+ ## Questions & Answers
188
+
189
+ ### Q{N}: {Title}
190
+ **Question:** {question}
191
+ **Answer:** {user's answer}
192
+ **Spec Updates:** {which sections were updated}
193
+ **Priority:** High/Medium/Low
194
+ **Resolved:** Yes
195
+
196
+ ---
197
+
198
+ ## Edge Cases
199
+
200
+ ### EC{N}: {Name}
201
+ **Scenario:** {when it happens}
202
+ **Expected Behavior:** {how the system reacts}
203
+ **Implementation Notes:** {tips}
204
+
205
+ ---
206
+
207
+ ## Summary
208
+
209
+ | | Count |
210
+ |---|---|
211
+ | Questions Answered | {N} |
212
+ | Edge Cases Documented | {N} |
213
+ | Spec Sections Updated | {N} |
214
+ | Final Score | {score}/100 |
215
+ ```
216
+
217
+ **Step 2: Update State**
218
+
219
+ ```bash
220
+ npx morph-spec state mark-output $ARGUMENTS clarifications
221
+ ```
222
+
223
+ ---
224
+
225
+ ## Phase Outputs
226
+
227
+ <!-- morph:outputs:clarify -->
228
+ | Output | Path |
229
+ |--------|------|
230
+ | `clarifications` | `.morph/features/{feature}/1-design/clarifications.md` |
231
+ <!-- /morph:outputs -->
232
+
233
+ ## Advancement Criteria
234
+
235
+ - [x] Satisfaction score >= 85 OR user opted to end
236
+ - [x] All answers are consistent with `contracts.cs`
237
+ - [x] `spec.md` updated with clarifications and edge cases
238
+ - [x] `clarifications.md` created with Q&A and final score
239
+ - [x] State updated (`mark-output clarifications`)
240
+
241
+ ---
242
+
243
+ ```bash
244
+ npx morph-spec phase advance $ARGUMENTS
245
+ ```
246
+
247
+ Continue automatically to Phase 4 (Plan) after clarifications.md is generated.
@@ -0,0 +1,270 @@
1
+ ---
2
+ name: morph:phase-codebase-analysis
3
+ description: MORPH-SPEC Design sub-phase that analyzes existing codebase and database schema, producing schema-analysis.md with real column names, types, relationships, and field mismatches. Use at the start of Design phase before generating contracts.cs to prevent incorrect field names or types.
4
+ user-invocable: false
5
+ allowed-tools: Read, Write, Edit, Bash, Glob, Grep, AskUserQuestion
6
+ cliVersion: "4.10.1"
7
+ ---
8
+
9
+ # MORPH Codebase Analysis - Design Sub-phase
10
+
11
+ > INTERNAL: Automation skill for schema analysis step within Phase 2 (Design).
12
+ > Called by `phase-design.md` Step 2. Not a standalone user command.
13
+
14
+ Automates existing code analysis to generate `schema-analysis.md` with real database/code data.
15
+
16
+ ## Prerequisites
17
+
18
+ - [ ] Phase 1 (Setup) completed
19
+ - [ ] Feature has approved `proposal.md`
20
+ - [ ] Project context loaded (stack, agents)
21
+
22
+ ## Recommended Tools
23
+
24
+ > **Ref:** `framework/skills/level-0-meta/morph-tool-usage-guide/SKILL.md` for complete guide.
25
+ > **Ref:** `framework/standards/integration/mcp/mcp-tools.md` for MCP reference.
26
+
27
+ | Action | Tool | Alternative |
28
+ |------|------------|-------------|
29
+ | List database tables | **Supabase MCP** `list_tables()` | **Grep** `.from(` in code |
30
+ | Get table schema | **Supabase MCP** `get_table_schema()` | **Read** type definitions |
31
+ | Get FK relationships | **Supabase MCP** `get_relationships()` | **Grep** JOIN/FK patterns |
32
+ | Get RLS policies | **Supabase MCP** `query()` | **Read** policy files |
33
+ | Find queries in code | **Grep** `\.from\(` in `*.ts,*.tsx,*.js,*.cs` | — |
34
+ | Find type definitions | **Glob** `src/**/types/**/*.ts` or `**/Entities/**/*.cs` | — |
35
+ | Read found files | **Read** each file | — |
36
+ | Complex multi-file analysis | **Task** (subagent Explore) | Read individual |
37
+ | Generate schema-analysis.md | **Write** using template | **Bash** template render |
38
+
39
+ **MCPs for this phase:** Supabase (schema — **PRIORITY**), Generic database MCPs.
40
+
41
+ ---
42
+
43
+ ## Automated Workflow
44
+
45
+ ### Step 1: Detect Analysis Method
46
+
47
+ **IMPORTANT:** Do not assume the MCP is available and accessible.
48
+ Verify first with a test call before attempting to use it.
49
+
50
+ ```
51
+ 1. IF Supabase MCP configured in settings.json:
52
+ → Try mcp__supabase__list_tables() with implicit timeout
53
+ → IF returns valid data (array of tables with content):
54
+ → Use Method A (direct MCP)
55
+ → IF returns error, timeout, empty array, or inaccessible data:
56
+ → Log: "Supabase MCP configured but inaccessible (CloudSQL? Auth? Permissions?)"
57
+ → Use Method B (static code analysis)
58
+
59
+ 2. IF Database MCP (other) configured:
60
+ → Test connectivity with trivial query before using
61
+ → IF OK: Use adapted Method A
62
+ → IF fails: Use Method B
63
+
64
+ 3. IF no MCP available:
65
+ → Use Method B (static code analysis)
66
+ ```
67
+
68
+ **Signs of inaccessible MCP:**
69
+ - Empty response or `[]` from `list_tables()`
70
+ - Authentication error / permission denied
71
+ - Returned schema doesn't match code (e.g., tables exist in code but not in MCP)
72
+ - Project uses database other than Supabase (CloudSQL, RDS, Azure SQL, etc.)
73
+
74
+ ### Step 2A: MCP Method (Preferred)
75
+
76
+ **Tools:** Supabase MCP
77
+
78
+ ```javascript
79
+ // 1. List all tables
80
+ const tables = await mcp__supabase__list_tables();
81
+
82
+ // 2. For each feature-relevant table:
83
+ for (const table of relevantTables) {
84
+ // 2a. Complete schema
85
+ const schema = await mcp__supabase__get_table_schema({ table: table.name });
86
+ // → column_name, data_type, is_nullable, column_default
87
+
88
+ // 2b. Relationships
89
+ const rels = await mcp__supabase__get_relationships({ table: table.name });
90
+ // → foreign_table, foreign_column, constraint_type
91
+
92
+ // 2c. Indexes
93
+ const indexes = await mcp__supabase__query({
94
+ query: `SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '${table.name}'`
95
+ });
96
+
97
+ // 2d. RLS policies (security)
98
+ const policies = await mcp__supabase__query({
99
+ query: `SELECT policyname, cmd, qual FROM pg_policies WHERE tablename = '${table.name}'`
100
+ });
101
+ }
102
+ ```
103
+
104
+ ### Step 2B: Static Analysis Method (Fallback)
105
+
106
+ **Tools:** Grep, Glob, Read, Task (subagent for large projects)
107
+
108
+ **Phase B1: Find Queries**
109
+
110
+ ```
111
+ Grep: "\.from\(|\.select\(|SELECT |supabase\.|context\.|dbContext\.|DbSet<"
112
+ Type: ts,tsx,js,jsx,cs
113
+ Output: files_with_matches
114
+ ```
115
+
116
+ **Phase B2: Read Query Files**
117
+
118
+ For each found file, use **Read** to extract:
119
+ - Table names: `from('leads')`, `DbSet<Lead>`, `FROM leads`
120
+ - Column names: `.select('fullname, phonenumber')`, `l.FullName`
121
+ - Data types: TypeScript interfaces, C# DTOs
122
+ - Relationships: JOIN clauses, navigation properties
123
+
124
+ **Phase B3: Find Type Definitions**
125
+
126
+ ```
127
+ # TypeScript/JavaScript:
128
+ Glob: "src/**/types/**/*.ts"
129
+ Glob: "src/**/*.d.ts"
130
+ Glob: "src/**/interfaces/*.ts"
131
+
132
+ # .NET:
133
+ Glob: "**/*Dto.cs"
134
+ Glob: "**/Entities/**/*.cs"
135
+ Glob: "**/Models/**/*.cs"
136
+ ```
137
+
138
+ **Phase B4: When to use Task (subagent)**
139
+
140
+ Use Task subagent **ONLY** when:
141
+ - Project has 20+ query files
142
+ - Multiple data access patterns (Supabase + EF Core + raw SQL)
143
+ - Precise cross-context analysis across many files
144
+
145
+ Do not use Task subagent when:
146
+ - Project has < 10 query files (direct Read is faster)
147
+ - Data access pattern is uniform (only Supabase OR only EF Core)
148
+
149
+ ### Step 2B-Format: Standardized Format for Static Analysis
150
+
151
+ **IMPORTANT:** When using Method B (static analysis), **always use the official template**.
152
+ This ensures `contracts.cs` can be generated mechanically, without manual interpretation.
153
+
154
+ The template is at `framework/templates/docs/schema-analysis.md`. Use it via:
155
+
156
+ ```bash
157
+ npx morph-spec template render docs/schema-analysis \
158
+ .morph/features/{feature}/1-design/schema-analysis.md \
159
+ '{
160
+ "FEATURE_NAME": "{feature}",
161
+ "FEATURE_NAME_TITLE": "{Feature Display Name}",
162
+ "DATE": "YYYY-MM-DD",
163
+ "AUTHOR": "Claude Code (Sonnet 4.6)",
164
+ "method": "Static Code Analysis",
165
+ "mcpUsed": "No",
166
+ "tableCount": N,
167
+ "fileCount": N,
168
+ "tables": [
169
+ {
170
+ "name": "table_name",
171
+ "codeSource": "path/to/file.ts",
172
+ "columns": [
173
+ { "name": "column_name", "type": "string", "nullable": false, "notes": "from SELECT query" }
174
+ ],
175
+ "relationships": [{ "description": "belongs to users via user_id" }],
176
+ "indexes": [{ "description": "idx_leads_created_at (created_at DESC)" }]
177
+ }
178
+ ],
179
+ "fieldMismatches": [
180
+ { "description": "Code uses user.name but DB column is users.fullname" }
181
+ ],
182
+ "typeMismatches": [],
183
+ "relationshipIssues": [],
184
+ "dtoRecommendations": [
185
+ {
186
+ "tableName": "leads",
187
+ "dtoName": "LeadDto",
188
+ "fields": [
189
+ { "type": "Guid", "name": "Id", "nullable": false, "comment": "PK" },
190
+ { "type": "string", "name": "FullName", "nullable": false, "comment": "leads.fullname" }
191
+ ]
192
+ }
193
+ ]
194
+ }'
195
+ ```
196
+
197
+ **If template render is not available**, use **Write** tool to create the file directly
198
+ following the structure above. The table and mismatch format is MANDATORY — without standard format
199
+ it's not possible to generate contracts.cs mechanically.
200
+
201
+ ### Step 3: Map Inconsistencies
202
+
203
+ Create a map of:
204
+
205
+ | Frontend/Code | Database | Type | Problem |
206
+ |----------------|----------------|------|----------|
207
+ | user.name | users.fullname | string | MISMATCH |
208
+ | lead.phone | leads.phonenumber | string | MISMATCH |
209
+ | order.metadata | orders.metadata | JSONB | Type complex |
210
+
211
+ ### Step 4: Generate `schema-analysis.md`
212
+
213
+ Use the template at `framework/templates/docs/schema-analysis.md`:
214
+
215
+ ```bash
216
+ npx morph-spec template render docs/schema-analysis \
217
+ .morph/features/{feature-name}/1-design/schema-analysis.md \
218
+ '{ "FEATURE_NAME": "{feature-name}", "DATE": "..." }'
219
+ ```
220
+
221
+ OR use **Write** tool to create directly with collected data.
222
+
223
+ ### Step 5: Update State
224
+
225
+ ```bash
226
+ npx morph-spec state mark-output {feature-name} schema-analysis
227
+ ```
228
+
229
+ ---
230
+
231
+ ## Outputs
232
+
233
+ - `.morph/features/{feature}/1-design/schema-analysis.md`
234
+ - State updated with `schemaAnalysis` output
235
+
236
+ ## CHECKPOINT
237
+
238
+ Before proceeding to contracts.cs, present results and ask for confirmation using `AskUserQuestion`:
239
+
240
+ ```json
241
+ {
242
+ "questions": [
243
+ {
244
+ "header": "Schema Review",
245
+ "question": "Schema analysis complete: {N} tables, {N} field mismatches, {N} type mismatches, {N} relationships. Is the analysis correct?",
246
+ "multiSelect": false,
247
+ "options": [
248
+ { "label": "Correct", "description": "Schema analysis matches the real database" },
249
+ { "label": "Has issues", "description": "I see problems — let me explain" }
250
+ ]
251
+ },
252
+ {
253
+ "header": "Contracts",
254
+ "question": "May I generate contracts.cs based on this real schema?",
255
+ "multiSelect": false,
256
+ "options": [
257
+ { "label": "Generate contracts", "description": "Proceed to contracts.cs using the analyzed schema" },
258
+ { "label": "Wait", "description": "I want to review schema-analysis.md first" }
259
+ ]
260
+ }
261
+ ]
262
+ }
263
+ ```
264
+
265
+ **If "Has issues"** → ask what's wrong, fix schema-analysis.md, re-present checkpoint.
266
+ **If "Correct" + "Generate contracts"** → proceed to contracts generation.
267
+
268
+ ---
269
+
270
+ *MORPH-SPEC by Polymorphism Tech*