@polymorphism-tech/morph-spec 8.32.0 → 8.34.2

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 (176) hide show
  1. package/CLAUDE.md +4 -0
  2. package/README.md +2 -2
  3. package/bin/morph-spec.js +26 -2
  4. package/bin/validate.js +379 -379
  5. package/claude-plugin.json +1 -1
  6. package/docs/CHEATSHEET.md +1 -1
  7. package/docs/QUICKSTART.md +1 -1
  8. package/framework/CLAUDE.md +2 -2
  9. package/framework/MORPH.md +4 -5
  10. package/framework/agents.json +1 -0
  11. package/framework/docs/CLI.md +71 -68
  12. package/framework/hooks/README.md +11 -1
  13. package/framework/hooks/claude-code/core/block-destructive-worktree-removal.js +5 -8
  14. package/framework/hooks/claude-code/core/block-raw-worktree-add.js +79 -0
  15. package/framework/hooks/claude-code/core/chain.js +27 -7
  16. package/framework/hooks/claude-code/core/worktree-context.js +83 -14
  17. package/framework/hooks/claude-code/core/worktree-create.js +206 -0
  18. package/framework/hooks/claude-code/core/worktree-env-inject.js +87 -0
  19. package/framework/hooks/claude-code/core/worktree-remove.js +81 -0
  20. package/framework/hooks/claude-code/statusline.sh +11 -11
  21. package/framework/hooks/git/commit-msg/conventional-commits.sh +33 -33
  22. package/framework/hooks/git/pre-commit/agents.sh +25 -25
  23. package/framework/hooks/git/pre-commit/orchestrator.sh +64 -64
  24. package/framework/hooks/git/pre-commit/specs.sh +50 -50
  25. package/framework/hooks/git/pre-push/run-tests.sh +44 -44
  26. package/framework/hooks/shared/command-tokens.js +23 -0
  27. package/framework/hooks/shared/e2e-check.js +0 -0
  28. package/framework/hooks/shared/git-paths.js +25 -0
  29. package/framework/hooks/shared/hook-response.js +17 -0
  30. package/framework/hooks/shared/morph-cli.js +134 -0
  31. package/framework/hooks/shared/stdin-reader.js +26 -26
  32. package/framework/rules/infrastructure-standards.md +3 -1
  33. package/framework/skills/core/create-umbraco-lp/SKILL.md +1 -1
  34. package/framework/skills/core/migrate-lp-to-umbraco/SKILL.md +1 -1
  35. package/framework/skills/core/morph-apply/SKILL.md +3 -3
  36. package/framework/skills/core/morph-brainstorming/SKILL.md +1 -1
  37. package/framework/skills/core/morph-eval/SKILL.md +1 -1
  38. package/framework/skills/core/morph-hotfix/SKILL.md +1 -1
  39. package/framework/skills/core/morph-implement/SKILL.md +3 -1
  40. package/framework/skills/core/morph-plan/SKILL.md +6 -3
  41. package/framework/skills/core/morph-proposal/SKILL.md +1 -1
  42. package/framework/skills/core/morph-review/SKILL.md +1 -1
  43. package/framework/skills/core/morph-stack-scan/SKILL.md +1 -1
  44. package/framework/skills/core/morph-standards/SKILL.md +1 -1
  45. package/framework/skills/core/morph-uiux/SKILL.md +1 -1
  46. package/framework/standards/STANDARDS.json +30 -0
  47. package/framework/standards/ai-agents/agent-session.md +160 -160
  48. package/framework/standards/ai-agents/batching.md +193 -193
  49. package/framework/standards/ai-agents/context-providers.md +188 -188
  50. package/framework/standards/ai-agents/mcp-server.md +250 -250
  51. package/framework/standards/ai-agents/mcp-tools.md +197 -197
  52. package/framework/standards/ai-agents/middleware-patterns.md +207 -207
  53. package/framework/standards/ai-agents/modalities-image-gen.md +173 -173
  54. package/framework/standards/ai-agents/modalities-speech-to-text.md +171 -171
  55. package/framework/standards/ai-agents/modalities-text-to-speech.md +181 -181
  56. package/framework/standards/ai-agents/modalities-vision.md +201 -201
  57. package/framework/standards/ai-agents/multi-agent-patterns.md +155 -155
  58. package/framework/standards/ai-agents/production.md +231 -231
  59. package/framework/standards/ai-agents/prompts-in-db.md +214 -214
  60. package/framework/standards/ai-agents/providers/model-registry.md +158 -158
  61. package/framework/standards/ai-agents/service-tier-flex.md +135 -135
  62. package/framework/standards/ai-agents/structured-output.md +249 -249
  63. package/framework/standards/ai-agents/sweet-spot.md +109 -109
  64. package/framework/standards/infrastructure/docker/local-compose-isolation.md +88 -0
  65. package/framework/templates/IDE-SNIPPETS.md +262 -262
  66. package/framework/templates/REGISTRY.json +1 -1
  67. package/framework/templates/code/dotnet/abstractions/api-endpoint.cs +7 -7
  68. package/framework/templates/code/dotnet/ai-agents/AgentBootstrap.cs.template +39 -39
  69. package/framework/templates/code/dotnet/ai-agents/EmbeddingService.cs.template +68 -68
  70. package/framework/templates/code/dotnet/ai-agents/ModelRegistry.cs.template +117 -117
  71. package/framework/templates/code/dotnet/ai-agents/PromptRepository.cs.template +95 -95
  72. package/framework/templates/code/dotnet/ai-agents/ResilientToolMiddleware.cs.template +61 -61
  73. package/framework/templates/code/dotnet/ai-agents/VectorSearchTool.cs.template +107 -107
  74. package/framework/templates/code/dotnet/ai-agents/model-registry.json.template +58 -58
  75. package/framework/templates/code/dotnet/contracts/contracts-vsa.cs +281 -281
  76. package/framework/templates/code/dotnet/database/migration.cs +83 -83
  77. package/framework/templates/code/dotnet/feature/endpoint.cs.hbs +21 -21
  78. package/framework/templates/code/dotnet/feature/request.cs.hbs +4 -4
  79. package/framework/templates/code/dotnet/feature/response.cs.hbs +4 -4
  80. package/framework/templates/code/dotnet/feature/validator.cs.hbs +15 -15
  81. package/framework/templates/code/sql/ai-agents/agent-prompts-migration.sql.template +58 -58
  82. package/framework/templates/code/sql/ai-agents/pgvector-embeddings-migration.sql.template +91 -91
  83. package/framework/templates/code/typescript/contracts.ts +167 -167
  84. package/framework/templates/docs/clarifications.md +253 -253
  85. package/framework/templates/docs/schema-analysis.md +119 -119
  86. package/framework/templates/docs/ui-components.md +52 -52
  87. package/framework/templates/docs/ui-design-system.md +76 -76
  88. package/framework/templates/docs/ui-flows.md +167 -167
  89. package/framework/templates/docs/ui-mockups.md +98 -98
  90. package/framework/templates/feature/decisions.md +122 -122
  91. package/framework/templates/frontend/nextjs/Dockerfile.nextjs.hbs +43 -43
  92. package/framework/templates/frontend/nextjs/client-component.tsx.hbs +26 -26
  93. package/framework/templates/frontend/nextjs/env.mjs.hbs +32 -32
  94. package/framework/templates/frontend/nextjs/feature-form.tsx.hbs +56 -56
  95. package/framework/templates/frontend/nextjs/page.tsx.hbs +22 -22
  96. package/framework/templates/frontend/nextjs/tsconfig.json.hbs +26 -26
  97. package/framework/templates/frontend/nextjs/use-feature.ts.hbs +54 -54
  98. package/framework/templates/infrastructure/docker/Dockerfile.template +58 -58
  99. package/framework/templates/infrastructure/docker/docker-compose.template.yml +24 -14
  100. package/framework/templates/infrastructure/docker/dockerfile-api.dockerfile +38 -38
  101. package/framework/templates/infrastructure/docker/dockerfile-web.dockerfile +48 -48
  102. package/framework/templates/infrastructure/github/README.md +56 -56
  103. package/framework/templates/infrastructure/github/actions/docker-build-push/action.yml.hbs +45 -45
  104. package/framework/templates/infrastructure/github/actions/health-check/action.yml.hbs +27 -27
  105. package/framework/templates/infrastructure/github/workflows/docker-build-push.yml.hbs +59 -59
  106. package/framework/templates/infrastructure/github/workflows/dotnet-build.yml.hbs +39 -39
  107. package/framework/templates/integrations/asaas-client.cs +387 -387
  108. package/framework/templates/integrations/asaas-webhook.cs +351 -351
  109. package/framework/templates/meta-prompts/fusion/fusion-agent.md +76 -76
  110. package/framework/templates/meta-prompts/fusion/fusion-aggregator.md +100 -100
  111. package/framework/templates/meta-prompts/validators/pre-commit-validator.md +95 -95
  112. package/framework/templates/project/validate.js +120 -120
  113. package/framework/templates/state.template.json +17 -17
  114. package/framework/templates/ui/design-system.css +226 -226
  115. package/package.json +1 -1
  116. package/src/commands/dashboard/dashboard.js +53 -53
  117. package/src/commands/e2e/e2e.js +77 -39
  118. package/src/commands/index.js +13 -13
  119. package/src/commands/project/checkpoint.js +209 -209
  120. package/src/commands/project/doctor.js +8 -6
  121. package/src/commands/project/env.js +95 -0
  122. package/src/commands/project/worktree.js +266 -52
  123. package/src/commands/state/delete.js +8 -0
  124. package/src/commands/state/finish.js +29 -34
  125. package/src/core/state/phase-state-machine.js +197 -197
  126. package/src/core/templates/template-renderer.js +477 -477
  127. package/src/dashboard/app/components/DetailPanel.jsx +11 -11
  128. package/src/dashboard/app/components/FlowGraph.jsx +17 -17
  129. package/src/dashboard/app/components/SearchFilter.jsx +22 -22
  130. package/src/dashboard/app/hooks/useDetailPanel.js +15 -15
  131. package/src/dashboard/app/main.jsx +13 -13
  132. package/src/dashboard/app/pages/AgentMap.jsx +370 -370
  133. package/src/dashboard/app/pages/HookInspector.jsx +430 -418
  134. package/src/dashboard/app/pages/PhasePipeline.jsx +408 -408
  135. package/src/dashboard/app/pages/RulesMap.jsx +273 -273
  136. package/src/dashboard/app/pages/SkillsLibrary.jsx +366 -366
  137. package/src/dashboard/app/pages/Standards.jsx +332 -332
  138. package/src/dashboard/app/styles/index.css +47 -47
  139. package/src/dashboard/index.html +12 -12
  140. package/src/dashboard/package.json +24 -24
  141. package/src/dashboard/scanner/hooks-scanner.js +5 -1
  142. package/src/dashboard/scanner/phases-scanner.js +15 -15
  143. package/src/dashboard/scanner/rubrics-scanner.js +12 -12
  144. package/src/dashboard/scanner/rules-scanner.js +37 -37
  145. package/src/dashboard/scanner/skills-scanner.js +43 -43
  146. package/src/dashboard/scanner/squads-scanner.js +13 -13
  147. package/src/dashboard/vite-plugin-morph-data.js +17 -17
  148. package/src/dashboard/vite.config.js +24 -24
  149. package/src/lib/detectors/design-system-detector.js +188 -188
  150. package/src/lib/detectors/index.js +217 -217
  151. package/src/lib/e2e/compose-env.js +6 -11
  152. package/src/lib/e2e/stack-teardown.js +115 -0
  153. package/src/lib/generators/architecture-scanner.js +2 -1
  154. package/src/lib/generators/design-system-generator.js +508 -508
  155. package/src/lib/generators/settings-generator.js +209 -209
  156. package/src/lib/graph/graphifyignore.js +2 -1
  157. package/src/lib/hooks/hook-executor.js +167 -167
  158. package/src/lib/installers/mcp-installer.js +719 -719
  159. package/src/lib/tasks/task-classifier.js +30 -30
  160. package/src/lib/tasks/task-parser.js +209 -209
  161. package/src/lib/tasks/test-runner.js +218 -39
  162. package/src/lib/validators/checkpoint-persistence.js +39 -39
  163. package/src/lib/validators/css/css-validator.js +340 -340
  164. package/src/lib/validators/shared/emit-validator-dispatch.js +14 -14
  165. package/src/lib/validators/shared/issue-counter.js +14 -14
  166. package/src/lib/validators/verify-runner.js +37 -4
  167. package/src/lib/worktree/dirs.js +32 -0
  168. package/src/lib/worktree/env-allocator.js +45 -11
  169. package/src/lib/worktree/worktree-audit.js +40 -2
  170. package/src/utils/agent-id-mapper.js +11 -11
  171. package/src/utils/claude-settings-manager.js +300 -300
  172. package/src/utils/file-copier.js +18 -13
  173. package/src/utils/hooks-installer.js +46 -3
  174. package/src/utils/skills-installer.js +187 -187
  175. package/framework/templates/.idea/morph-templates.xml +0 -92
  176. package/framework/templates/.vscode/morph-templates.code-snippets +0 -186
@@ -89,6 +89,59 @@ function featureFromBranch(dir) {
89
89
  }
90
90
  }
91
91
 
92
+ /**
93
+ * Task-isolation worktree (`morph-task/{name}`): the tree a `WorktreeCreate`
94
+ * hook provisions for a parallel sub-agent batch (morph-apply §3b-w) or for a
95
+ * `claude -w` session with no feature behind it. Not a feature — no
96
+ * feature.json, no gates — but it owns a port block and the same junctions.
97
+ */
98
+ function taskFromBranch(dir) {
99
+ try {
100
+ const branch = gitRevParse('--abbrev-ref HEAD', dir);
101
+ const match = branch.match(/^morph-task\/(.+)$/);
102
+ return match ? match[1] : null;
103
+ } catch {
104
+ return null;
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Is this secondary worktree inside the primary root's `worktrees/` (or the
110
+ * legacy `.worktrees/`) container? A tree kept anywhere else — `.claude/
111
+ * worktrees/` from a native Claude Code run before the hook existed, a
112
+ * sibling `../repo-x`, superpowers' `~/.config/...` — was created without
113
+ * morph: no junctions, no ports, invisible to `fleet`. Fail-open: any error
114
+ * reads as "in container" so a healthy tree is never nagged by mistake.
115
+ */
116
+ function isOutsideContainer(dir) {
117
+ try {
118
+ const common = resolve(dir, gitRevParse('--git-common-dir', dir)).replace(/\\/g, '/');
119
+ const root = common.replace(/\/\.git$/, '');
120
+ const norm = resolve(dir).replace(/\\/g, '/');
121
+ return !['worktrees', '.worktrees'].some((d) => norm.startsWith(`${root}/${d}/`));
122
+ } catch {
123
+ return false;
124
+ }
125
+ }
126
+
127
+ function buildOutOfContainerWarning(dir) {
128
+ return '⚠ Este worktree está FORA do container do morph (worktrees/): foi criado sem o morph-spec — ' +
129
+ 'sem junctions de infra, sem bloco de portas, invisível ao `fleet`. ' +
130
+ 'Integre a branch e remova com `morph-spec worktree remove --target "<path>"`; ' +
131
+ 'crie worktrees por `morph-spec worktree setup <f>` ou `worktree provision <n>` (o hook WorktreeCreate faz isso sozinho). ' +
132
+ 'Diagnóstico: `morph-spec doctor --worktrees`.';
133
+ }
134
+
135
+ function buildTaskContext(dir, task) {
136
+ let text = `🧩 Worktree de isolamento de task: ${task} (branch morph-task/${task}). ` +
137
+ 'Não é uma feature: trabalhe só na task despachada, commite na branch como último ato e NUNCA faça merge — ' +
138
+ 'o orquestrador integra e remove este worktree.';
139
+ const envLine = buildEnvLine(dir);
140
+ if (envLine) text += `\n${envLine}`;
141
+ text += buildMissingInfraWarning(dir);
142
+ return text;
143
+ }
144
+
92
145
  /** Fallback: if exactly one feature directory exists, assume that's the one. */
93
146
  function featureFromSingleDir(dir) {
94
147
  try {
@@ -114,8 +167,11 @@ function buildEnvLine(dir) {
114
167
  const port = content.match(/^PORT=(\d+)$/m)?.[1];
115
168
  if (!port) return null;
116
169
  const range = content.match(/^MORPH_PORT_RANGE=([\d-]+)$/m)?.[1];
117
- return `🔌 Ambiente desta worktree: PORT=${port}${range ? ` (bloco ${range})` : ''}. ` +
118
- `Suba dev servers SEMPRE nessas portas (fonte: .morph/worktree.env) — as portas padrão pertencem à raiz.`;
170
+ const pg = content.match(/^POSTGRES_PORT=(\d+)$/m)?.[1];
171
+ const project = content.match(/^COMPOSE_PROJECT_NAME=(\S+)$/m)?.[1];
172
+ const extras = [pg ? `POSTGRES_PORT=${pg}` : null, project ? `COMPOSE_PROJECT_NAME=${project}` : null].filter(Boolean);
173
+ return `🔌 Ambiente desta worktree: PORT=${port}${range ? ` (bloco ${range})` : ''}${extras.length ? `, ${extras.join(', ')}` : ''}. ` +
174
+ 'Suba dev servers SEMPRE nessas portas (fonte: .morph/worktree.env; o hook PreToolUse injeta em npm run dev/dotnet run/docker compose) — as portas padrão pertencem à raiz.';
119
175
  } catch {
120
176
  return null;
121
177
  }
@@ -148,20 +204,25 @@ function buildWorktreeContext(dir, feature) {
148
204
  const handoffLine = buildHandoffLine(dir, feature);
149
205
  if (handoffLine) text += `\n${handoffLine}`;
150
206
 
151
- // `.morph/framework` is checked alongside the other two because the hook
152
- // commands resolve to `$CLAUDE_PROJECT_DIR/.morph/framework/hooks/...`: if
153
- // that junction is missing, `node` finds no file and EVERY guardrail in this
154
- // worktree is silently dark — the worst failure mode of the lot, and the one
155
- // the old two-item check could not see.
207
+ text += buildMissingInfraWarning(dir);
208
+ return text;
209
+ }
210
+
211
+ /**
212
+ * `.morph/framework` is checked alongside the other two because the hook
213
+ * commands resolve to `$CLAUDE_PROJECT_DIR/.morph/framework/hooks/...`: if
214
+ * that junction is missing, `node` finds no file and EVERY guardrail in this
215
+ * worktree is silently dark — the worst failure mode of the lot, and the one
216
+ * the old two-item check could not see.
217
+ */
218
+ function buildMissingInfraWarning(dir) {
156
219
  const missing = ['node_modules', '.claude', join('.morph', 'framework')]
157
220
  .filter((rel) => !existsSync(join(dir, rel)));
158
- if (missing.length) {
159
- text += `\n⚠ Infra compartilhada ausente (${missing.join(', ').replace(/\\/g, '/')}). Rode: morph-spec worktree link --all`;
160
- if (missing.includes(join('.morph', 'framework'))) {
161
- text += `\n Sem .morph/framework os hooks do morph-spec NÃO executam neste worktree (nenhum guardrail ativo).`;
162
- }
221
+ if (!missing.length) return '';
222
+ let text = `\n⚠ Infra compartilhada ausente (${missing.join(', ').replace(/\\/g, '/')}). Rode: morph-spec worktree link --all`;
223
+ if (missing.includes(join('.morph', 'framework'))) {
224
+ text += `\n Sem .morph/framework os hooks do morph-spec NÃO executam neste worktree (nenhum guardrail ativo).`;
163
225
  }
164
-
165
226
  return text;
166
227
  }
167
228
 
@@ -234,9 +295,17 @@ export async function run(payload) {
234
295
  try { rotateEventsLogIfLarge(dir); } catch { /* non-blocking */ }
235
296
 
236
297
  if (isSecondaryWorktree(dir)) {
298
+ const task = taskFromBranch(dir);
299
+ if (task) {
300
+ return contextResult(buildTaskContext(dir, task), 'SessionStart');
301
+ }
237
302
  const feature = featureFromBranch(dir) || featureFromSingleDir(dir);
238
303
  if (!feature) {
239
- return null;
304
+ // A tree with no morph identity at all: worth a warning ONLY when it
305
+ // also lives outside the container — that is the signature of a
306
+ // worktree created behind morph's back (native tool, raw git, another
307
+ // skill). Inside the container with no identity → stay silent.
308
+ return isOutsideContainer(dir) ? contextResult(buildOutOfContainerWarning(dir), 'SessionStart') : null;
240
309
  }
241
310
  return contextResult(buildWorktreeContext(dir, feature) + claimBlock(dir, feature, sessionId), 'SessionStart');
242
311
  }
@@ -0,0 +1,206 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * worktree-create.js — WorktreeCreate hook
4
+ *
5
+ * Event: WorktreeCreate (fires for `claude -w`, the EnterWorktree tool, and
6
+ * `Agent isolation:"worktree"` — measured, not assumed).
7
+ *
8
+ * Claude Code's native worktree is a bare `git worktree add` somewhere under
9
+ * `.claude/worktrees/`: no infra junctions (so no hooks run in there), no port
10
+ * block, no identity, invisible to the fleet. When this hook is registered it
11
+ * REPLACES that logic entirely: it must print the path of the tree it created
12
+ * as the last non-empty stdout line and exit 0; any non-zero exit fails the
13
+ * creation. It cannot return JSON — the standalone tail below is therefore
14
+ * NOT `runStandalone` (which prints a JSON verdict). Diagnostics go to stderr.
15
+ *
16
+ * What it does:
17
+ * 1. root = the PRIMARY root of the repo `payload.cwd` is in (never a
18
+ * secondary worktree — a tree under a tree is the fleet-invisible shape).
19
+ * 2. name → kebab slug (`Bold_Oak` → `bold-oak`, `agent-a1b2` stays).
20
+ * 3. feature or task? `<root>/.morph/features/<n>/feature.json` exists, or
21
+ * branch `morph/<n>` exists → `morph-spec worktree setup <n>`; otherwise
22
+ * `morph-spec worktree provision <n> [--base <ref>]`.
23
+ * 4. `worktree.baseRef` from the merged settings (`~/.claude/settings.json`
24
+ * < `<root>/.claude/settings.json` < `<root>/.claude/settings.local.json`):
25
+ * `"head"` → the current branch (or sha) of `payload.cwd`; anything else
26
+ * → the repo's default branch (the CLI's own default).
27
+ * 5. CLI exit 0 or 2 (already exists) → its `path`. Exit 1/3 → error.
28
+ * CLI unavailable → in-hook git fallback that still lands the tree under
29
+ * `<root>/worktrees/<n>` on `morph-task/<n>` (degraded: no junctions, no
30
+ * ports — but never outside the governed directory).
31
+ *
32
+ * `stateExists(root)` gate: outside a morph project (no `.morph/state.json`
33
+ * and no feature.json) there is nothing to govern — the same in-hook git
34
+ * fallback runs, so a non-morph repo with this hook installed still gets a
35
+ * worktree instead of a failed creation.
36
+ */
37
+
38
+ import { existsSync, mkdirSync, readFileSync } from 'fs';
39
+ import { execFileSync } from 'child_process';
40
+ import { dirname, join, resolve } from 'path';
41
+ import { homedir } from 'os';
42
+ import { stateExists } from '../../shared/state-reader.js';
43
+ import { resolveGitCommonDir } from '../../shared/git-paths.js';
44
+ import { runMorphSpec } from '../../shared/morph-cli.js';
45
+
46
+ const WORKTREES_DIR = 'worktrees';
47
+
48
+ const fwd = (p) => String(p).replace(/\\/g, '/');
49
+
50
+ function git(args, cwd) {
51
+ return execFileSync('git', args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim();
52
+ }
53
+
54
+ /**
55
+ * The primary root of the repo containing `startDir`. `git worktree list`'s
56
+ * first entry is the primary checkout, from any worktree; the pure-filesystem
57
+ * common-dir resolver is the no-subprocess fallback. Null outside a repo.
58
+ */
59
+ export function resolvePrimaryRoot(startDir) {
60
+ try {
61
+ const first = git(['worktree', 'list', '--porcelain'], startDir).split('\n').find((l) => l.startsWith('worktree '));
62
+ if (first) return fwd(first.slice('worktree '.length).trim());
63
+ } catch { /* fall through */ }
64
+ const common = resolveGitCommonDir(startDir);
65
+ return common ? fwd(dirname(common)) : null;
66
+ }
67
+
68
+ /** `Bold_Oak` → `bold-oak`; empty → a timestamped slug. */
69
+ export function slugify(name) {
70
+ const slug = String(name || '')
71
+ .toLowerCase()
72
+ .replace(/[^a-z0-9-]+/g, '-')
73
+ .replace(/-{2,}/g, '-')
74
+ .replace(/^-+|-+$/g, '');
75
+ return slug || `wt-${Date.now().toString(36)}`;
76
+ }
77
+
78
+ function readJsonSafe(path) {
79
+ try { return JSON.parse(readFileSync(path, 'utf8').replace(/^/, '')); } catch { return null; }
80
+ }
81
+
82
+ /**
83
+ * `worktree.baseRef` across the three settings layers, last one wins.
84
+ * Fail-open per file. Returns `'fresh'` (the documented default) when unset.
85
+ */
86
+ export function readBaseRefSetting(root, home = homedir()) {
87
+ let value = 'fresh';
88
+ for (const p of [
89
+ join(home, '.claude', 'settings.json'),
90
+ join(root, '.claude', 'settings.json'),
91
+ join(root, '.claude', 'settings.local.json'),
92
+ ]) {
93
+ const s = readJsonSafe(p);
94
+ const v = s?.worktree?.baseRef;
95
+ if (typeof v === 'string' && v) value = v;
96
+ }
97
+ return value;
98
+ }
99
+
100
+ /** The ref `baseRef:"head"` means: the branch checked out in `cwd`, or its sha. */
101
+ function headRef(cwd) {
102
+ try {
103
+ const name = git(['rev-parse', '--abbrev-ref', 'HEAD'], cwd);
104
+ if (name && name !== 'HEAD') return name;
105
+ return git(['rev-parse', 'HEAD'], cwd) || null;
106
+ } catch {
107
+ return null;
108
+ }
109
+ }
110
+
111
+ function isKnownFeature(root, name) {
112
+ if (existsSync(join(root, '.morph', 'features', name, 'feature.json'))) return true;
113
+ try {
114
+ git(['show-ref', '--verify', '--quiet', `refs/heads/morph/${name}`], root);
115
+ return true;
116
+ } catch {
117
+ return false;
118
+ }
119
+ }
120
+
121
+ function defaultBranch(root) {
122
+ try {
123
+ const m = git(['symbolic-ref', '--quiet', 'refs/remotes/origin/HEAD'], root).match(/^refs\/remotes\/origin\/(.+)$/);
124
+ if (m) return m[1];
125
+ } catch { /* no origin/HEAD */ }
126
+ for (const c of ['main', 'master']) {
127
+ try { git(['show-ref', '--verify', '--quiet', `refs/heads/${c}`], root); return c; } catch { /* next */ }
128
+ }
129
+ return null;
130
+ }
131
+
132
+ /**
133
+ * Degraded path — the CLI is not reachable, but the tree still has to land
134
+ * in the governed directory on the governed branch namespace.
135
+ */
136
+ function gitFallback(root, name, base) {
137
+ const branch = `morph-task/${name}`;
138
+ const path = `${root}/${WORKTREES_DIR}/${name}`;
139
+ if (existsSync(path)) return { path };
140
+ try { git(['show-ref', '--verify', '--quiet', `refs/heads/${branch}`], root); }
141
+ catch {
142
+ const from = base || defaultBranch(root);
143
+ git(from ? ['branch', branch, from] : ['branch', branch], root);
144
+ }
145
+ mkdirSync(join(root, WORKTREES_DIR), { recursive: true });
146
+ git(['worktree', 'add', path, branch], root);
147
+ return { path };
148
+ }
149
+
150
+ /**
151
+ * @param {object} payload - parsed WorktreeCreate stdin payload
152
+ * ({ session_id, cwd, hook_event_name, name })
153
+ * @returns {Promise<{ path: string }|{ error: string }>}
154
+ */
155
+ export async function run(payload) {
156
+ const start = payload?.cwd || process.env.CLAUDE_PROJECT_DIR || process.cwd();
157
+ const root = resolvePrimaryRoot(start) || (process.env.CLAUDE_PROJECT_DIR ? fwd(resolve(process.env.CLAUDE_PROJECT_DIR)) : null);
158
+ if (!root) return { error: `worktree-create: "${start}" is not inside a git repository` };
159
+
160
+ const name = slugify(payload?.name);
161
+ const baseSetting = readBaseRefSetting(root);
162
+ const base = baseSetting === 'head' ? headRef(start) : null;
163
+
164
+ // Outside a morph project there is nothing to govern beyond WHERE the tree
165
+ // lands — plain git, under <root>/worktrees/.
166
+ if (!stateExists(root)) {
167
+ try { return gitFallback(root, name, base); }
168
+ catch (err) { return { error: `worktree-create: ${err?.message || err}` }; }
169
+ }
170
+
171
+ const args = isKnownFeature(root, name)
172
+ ? ['worktree', 'setup', name, '--json']
173
+ : ['worktree', 'provision', name, '--json', ...(base ? ['--base', base] : [])];
174
+
175
+ const r = runMorphSpec(args, { cwd: root });
176
+ if (r.unavailable) {
177
+ process.stderr.write('worktree-create: morph-spec CLI not found — creating the tree with plain git under worktrees/ (no infra links, no port block). Run `morph-spec worktree link --all` inside it.\n');
178
+ try { return gitFallback(root, name, base); }
179
+ catch (err) { return { error: `worktree-create: ${err?.message || err}` }; }
180
+ }
181
+ if (r.stderr?.trim()) process.stderr.write(r.stderr);
182
+
183
+ const ok = r.code === 0 || r.code === 2;
184
+ const path = r.json?.path;
185
+ if (ok && path && !r.json?.error) return { path: fwd(path) };
186
+ const why = r.json?.error || r.json?.hint || r.stderr?.trim() || `exit ${r.code}`;
187
+ return { error: `worktree-create: morph-spec ${args.slice(0, 2).join(' ')} ${name} failed — ${why}` };
188
+ }
189
+
190
+ // Standalone tail — NOT runStandalone(): this event's contract is "path on
191
+ // stdout, exit 0" / "non-zero exit", never a JSON verdict.
192
+ if (!process.env.MORPH_HOOK_CHAINED) {
193
+ const { readStdin } = await import('../../shared/stdin-reader.js');
194
+ let result;
195
+ try {
196
+ result = await run(await readStdin());
197
+ } catch (err) {
198
+ result = { error: `worktree-create: ${err?.message || err}` };
199
+ }
200
+ if (result?.path) {
201
+ process.stdout.write(`${result.path}\n`);
202
+ process.exit(0);
203
+ }
204
+ process.stderr.write(`${result?.error || 'worktree-create: no path produced'}\n`);
205
+ process.exit(1);
206
+ }
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * worktree-env-inject.js — PreToolUse hook (Bash matcher), last link of the
4
+ * Bash chain.
5
+ *
6
+ * `.morph/worktree.env` used to be a manifest nobody executed: SessionStart
7
+ * printed the port block and the model was expected to pass `PORT` by hand
8
+ * every time it started a dev server. Two worktrees whose sessions forgot
9
+ * collided on 3000 — a runtime clash dressed as a broken app. This hook makes
10
+ * the block reach the process deterministically: when the command is a
11
+ * dev-server / compose / spec launcher and the cwd is a worktree that owns a
12
+ * manifest, the command is rewritten to `export K='V' …; <command>` via
13
+ * `updatedInput`, and the rewrite is named in `additionalContext`.
14
+ *
15
+ * Scope is deliberately narrow: only launcher-shaped commands, only when the
16
+ * command does not already set a layout key itself (`PORT=…`, `--port`,
17
+ * `compose -p`), only for the Bash tool. Everything else passes untouched.
18
+ *
19
+ * Fail-open: no state, no manifest, unreadable manifest → null (pass).
20
+ */
21
+
22
+ import { existsSync } from 'fs';
23
+ import { join } from 'path';
24
+ import { stateExists } from '../../shared/state-reader.js';
25
+ import { updatedInputResult, runStandalone } from '../../shared/hook-response.js';
26
+ import { loadEnvFile } from '../../shared/e2e-check.js';
27
+
28
+ /** Commands that start a server or a stack and therefore need the block. */
29
+ const LAUNCHER_RE = /\b(?:(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start)\b|next\s+(?:dev|start)\b|vite\b|dotnet\s+(?:run|watch)\b|docker(?:\s+|-)compose\b|playwright\s+test\b)/;
30
+
31
+ /** The command already chose its own port/project — respect it. */
32
+ const SELF_CONFIGURED_RE = /(?:^|[\s;&|])(?:PORT|API_PORT|POSTGRES_PORT|COMPOSE_PROJECT_NAME|ASPNETCORE_URLS)=|--port\b|(?:^|\s)-p\s+\d|compose\b[^|;&]*\s-p\s/;
33
+
34
+ /** Keys the layout owns; anything else in the manifest is left alone. */
35
+ const LAYOUT_KEYS = ['COMPOSE_PROJECT_NAME', 'MORPH_PORT_BASE', 'MORPH_PORT_RANGE', 'PORT', 'API_PORT', 'POSTGRES_PORT', 'REDIS_PORT', 'ASPNETCORE_URLS', 'PLAYWRIGHT_BASE_URL'];
36
+
37
+ /** POSIX single-quote, safe for Git Bash on Windows too. */
38
+ function shq(v) {
39
+ return `'${String(v).replace(/'/g, "'\\''")}'`;
40
+ }
41
+
42
+ /**
43
+ * Pure decision: the rewritten command, or null when nothing should change.
44
+ * @param {string} command
45
+ * @param {Record<string,string>} env - parsed manifest
46
+ * @returns {{ command: string, injected: string[] }|null}
47
+ */
48
+ export function rewriteCommand(command, env) {
49
+ if (!command || !LAUNCHER_RE.test(command)) return null;
50
+ if (SELF_CONFIGURED_RE.test(command)) return null;
51
+ const pairs = LAYOUT_KEYS.filter((k) => env[k]).map((k) => [k, env[k]]);
52
+ if (!pairs.some(([k]) => k === 'PORT')) return null;
53
+ // The web tier's URL is what the spec suite needs; derive it once here.
54
+ if (!env.PLAYWRIGHT_BASE_URL) pairs.push(['PLAYWRIGHT_BASE_URL', `http://localhost:${env.PORT}`]);
55
+ const prefix = `export ${pairs.map(([k, v]) => `${k}=${shq(v)}`).join(' ')}; `;
56
+ return { command: prefix + command, injected: pairs.map(([k, v]) => `${k}=${v}`) };
57
+ }
58
+
59
+ /**
60
+ * @param {object} payload - parsed PreToolUse stdin payload (Bash matcher)
61
+ * @returns {Promise<object|null>} an updatedInput result, or null (pass).
62
+ */
63
+ export async function run(payload) {
64
+ try {
65
+ if (payload?.tool_name && payload.tool_name !== 'Bash') return null;
66
+ if (!stateExists()) return null;
67
+ const command = payload?.tool_input?.command || '';
68
+ if (!command) return null;
69
+
70
+ const dir = payload?.cwd || process.env.CLAUDE_PROJECT_DIR || process.cwd();
71
+ const manifest = join(dir, '.morph', 'worktree.env');
72
+ if (!existsSync(manifest)) return null;
73
+
74
+ const rewritten = rewriteCommand(command, loadEnvFile(manifest));
75
+ if (!rewritten) return null;
76
+
77
+ return updatedInputResult(
78
+ { ...payload.tool_input, command: rewritten.command },
79
+ 'PreToolUse',
80
+ `🔌 worktree env injetado (.morph/worktree.env): ${rewritten.injected.join(' ')}`,
81
+ );
82
+ } catch {
83
+ return null;
84
+ }
85
+ }
86
+
87
+ await runStandalone(run);
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * worktree-remove.js — WorktreeRemove hook
4
+ *
5
+ * Event: WorktreeRemove (Claude Code's own cleanup of a tree it created via
6
+ * `claude -w` / EnterWorktree / Agent isolation). Input carries
7
+ * `worktree_path` (absolute). No decision control, output ignored — this hook
8
+ * ALWAYS exits 0.
9
+ *
10
+ * Why it exists: Claude Code's native removal is a raw `git worktree remove`,
11
+ * which FOLLOWS the infra junctions the WorktreeCreate hook put in that tree
12
+ * and empties the primary root's `.claude/` and `.morph/framework/`. This
13
+ * hook routes the removal through `morph-spec worktree remove --target
14
+ * --if-clean` instead: junctions dropped first, PROVE-or-DECLINE, and a tree
15
+ * holding uncommitted work is left alone (Claude Code's notion of "clean" is
16
+ * not the user's).
17
+ *
18
+ * Secondary net: in headless runs the event was observed NOT to fire; the
19
+ * PreToolUse guard (block-destructive-worktree-removal) stays the primary
20
+ * defense. Fail-open everywhere — diagnostics to stderr, never a throw, never
21
+ * a non-zero exit.
22
+ */
23
+
24
+ import { existsSync } from 'fs';
25
+ import { execFileSync } from 'child_process';
26
+ import { dirname, resolve } from 'path';
27
+ import { stateExists } from '../../shared/state-reader.js';
28
+ import { resolveGitCommonDir } from '../../shared/git-paths.js';
29
+ import { runMorphSpec } from '../../shared/morph-cli.js';
30
+
31
+ const fwd = (p) => String(p).replace(/\\/g, '/');
32
+
33
+ function primaryRoot(startDir) {
34
+ try {
35
+ const out = execFileSync('git', ['worktree', 'list', '--porcelain'], { cwd: startDir, stdio: ['ignore', 'pipe', 'pipe'] }).toString();
36
+ const first = out.split('\n').find((l) => l.startsWith('worktree '));
37
+ if (first) return fwd(first.slice('worktree '.length).trim());
38
+ } catch { /* fall through */ }
39
+ const common = resolveGitCommonDir(startDir);
40
+ return common ? fwd(dirname(common)) : null;
41
+ }
42
+
43
+ /**
44
+ * @param {object} payload - parsed WorktreeRemove stdin payload ({ cwd, worktree_path })
45
+ * @returns {Promise<null>} always null — the event ignores output
46
+ */
47
+ export async function run(payload) {
48
+ try {
49
+ const target = payload?.worktree_path;
50
+ if (!target || !existsSync(target)) return null;
51
+
52
+ const start = payload?.cwd || process.env.CLAUDE_PROJECT_DIR || dirname(resolve(target));
53
+ const root = primaryRoot(start) || primaryRoot(dirname(resolve(target)));
54
+ if (!root) return null;
55
+ // Nothing to govern outside a morph project — and never a raw removal
56
+ // from here either: the tree is simply left for git's own cleanup.
57
+ if (!stateExists(root)) return null;
58
+
59
+ const r = runMorphSpec(['worktree', 'remove', '--target', fwd(resolve(target)), '--if-clean', '--json'], { cwd: root });
60
+ if (r.unavailable) {
61
+ process.stderr.write(`worktree-remove: morph-spec CLI not found — "${target}" left in place (remove it with \`morph-spec worktree remove --target\`, never with raw git: it follows the infra junctions out).\n`);
62
+ return null;
63
+ }
64
+ if (!r.ok) {
65
+ const why = r.json?.error || r.json?.hint || r.stderr?.trim() || `exit ${r.code}`;
66
+ process.stderr.write(`worktree-remove: "${target}" NOT removed — ${why}\n`);
67
+ }
68
+ return null;
69
+ } catch (err) {
70
+ process.stderr.write(`worktree-remove: ${err?.message || err}\n`);
71
+ return null;
72
+ }
73
+ }
74
+
75
+ // Standalone tail: output is ignored by this event, so nothing is printed;
76
+ // the exit code is always 0.
77
+ if (!process.env.MORPH_HOOK_CHAINED) {
78
+ const { readStdin } = await import('../../shared/stdin-reader.js');
79
+ try { await run(await readStdin()); } catch { /* fail-open */ }
80
+ process.exit(0);
81
+ }
@@ -1,11 +1,11 @@
1
- #!/usr/bin/env bash
2
- # morph-spec statusline — installed globally to ~/.claude/statusline.sh
3
- # Claude Code invokes this with JSON via stdin after each response.
4
- # Requires: Python 3 available as `python3` or `python` on PATH.
5
-
6
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7
- if command -v python3 &>/dev/null; then
8
- python3 "${SCRIPT_DIR}/statusline.py" 2>/dev/null || true
9
- elif command -v python &>/dev/null; then
10
- python "${SCRIPT_DIR}/statusline.py" 2>/dev/null || true
11
- fi
1
+ #!/usr/bin/env bash
2
+ # morph-spec statusline — installed globally to ~/.claude/statusline.sh
3
+ # Claude Code invokes this with JSON via stdin after each response.
4
+ # Requires: Python 3 available as `python3` or `python` on PATH.
5
+
6
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7
+ if command -v python3 &>/dev/null; then
8
+ python3 "${SCRIPT_DIR}/statusline.py" 2>/dev/null || true
9
+ elif command -v python &>/dev/null; then
10
+ python "${SCRIPT_DIR}/statusline.py" 2>/dev/null || true
11
+ fi
@@ -1,33 +1,33 @@
1
- #!/usr/bin/env bash
2
- # Scope: universal | Validates commit message format (Conventional Commits)
3
- # MORPH-SPEC Commit Message Hook
4
- # Enforces Conventional Commits specification
5
-
6
- COMMIT_MSG_FILE="$1"
7
- COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
8
-
9
- # Conventional Commits pattern: type(scope): description
10
- # Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert
11
- PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert)(\(.+\))?: .{1,100}"
12
-
13
- if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
14
- echo "❌ Invalid commit message format!"
15
- echo ""
16
- echo "Required format: <type>(<scope>): <description>"
17
- echo ""
18
- echo "Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert"
19
- echo ""
20
- echo "Examples:"
21
- echo " feat(auth): add JWT refresh token rotation"
22
- echo " fix(api): resolve null reference in UserService"
23
- echo " docs(readme): update installation instructions"
24
- echo ""
25
- echo "Your message:"
26
- echo " $COMMIT_MSG"
27
- echo ""
28
- echo "Override with: git commit --no-verify"
29
- exit 1
30
- fi
31
-
32
- echo "✅ Commit message format valid"
33
- exit 0
1
+ #!/usr/bin/env bash
2
+ # Scope: universal | Validates commit message format (Conventional Commits)
3
+ # MORPH-SPEC Commit Message Hook
4
+ # Enforces Conventional Commits specification
5
+
6
+ COMMIT_MSG_FILE="$1"
7
+ COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
8
+
9
+ # Conventional Commits pattern: type(scope): description
10
+ # Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert
11
+ PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert)(\(.+\))?: .{1,100}"
12
+
13
+ if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
14
+ echo "❌ Invalid commit message format!"
15
+ echo ""
16
+ echo "Required format: <type>(<scope>): <description>"
17
+ echo ""
18
+ echo "Types: feat, fix, docs, style, refactor, perf, test, chore, build, ci, revert"
19
+ echo ""
20
+ echo "Examples:"
21
+ echo " feat(auth): add JWT refresh token rotation"
22
+ echo " fix(api): resolve null reference in UserService"
23
+ echo " docs(readme): update installation instructions"
24
+ echo ""
25
+ echo "Your message:"
26
+ echo " $COMMIT_MSG"
27
+ echo ""
28
+ echo "Override with: git commit --no-verify"
29
+ exit 1
30
+ fi
31
+
32
+ echo "✅ Commit message format valid"
33
+ exit 0
@@ -1,25 +1,25 @@
1
- #!/bin/bash
2
- # Scope: universal | Validates agents.json Schema
3
- # MORPH-SPEC Pre-Commit Hook: Agent Configuration Validation
4
- # Uses validate-agents.js to check agents.json
5
-
6
- echo "🤖 Validating agent configuration..."
7
-
8
- # Check if agents.json is being modified
9
- if git diff --cached --name-only | grep -q 'agents\.json$'; then
10
- echo "Detected changes to agents.json"
11
-
12
- # Run validator
13
- if ! morph-spec validate-agents-skills; then
14
- echo ""
15
- echo "❌ COMMIT BLOCKED: agents.json validation failed"
16
- echo " Fix errors above before committing"
17
- exit 1
18
- fi
19
-
20
- echo "✓ agents.json is valid"
21
- else
22
- echo "✓ No changes to agents.json"
23
- fi
24
-
25
- exit 0
1
+ #!/bin/bash
2
+ # Scope: universal | Validates agents.json Schema
3
+ # MORPH-SPEC Pre-Commit Hook: Agent Configuration Validation
4
+ # Uses validate-agents.js to check agents.json
5
+
6
+ echo "🤖 Validating agent configuration..."
7
+
8
+ # Check if agents.json is being modified
9
+ if git diff --cached --name-only | grep -q 'agents\.json$'; then
10
+ echo "Detected changes to agents.json"
11
+
12
+ # Run validator
13
+ if ! morph-spec validate-agents-skills; then
14
+ echo ""
15
+ echo "❌ COMMIT BLOCKED: agents.json validation failed"
16
+ echo " Fix errors above before committing"
17
+ exit 1
18
+ fi
19
+
20
+ echo "✓ agents.json is valid"
21
+ else
22
+ echo "✓ No changes to agents.json"
23
+ fi
24
+
25
+ exit 0