@polymorphism-tech/morph-spec 4.18.0 → 4.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +2 -2
  2. package/bin/morph-spec.js +11 -0
  3. package/bin/task-manager.js +47 -10
  4. package/claude-plugin.json +1 -1
  5. package/docs/CHEATSHEET.md +1 -1
  6. package/docs/QUICKSTART.md +1 -1
  7. package/framework/agents/architecture/architecture-expert.md +98 -0
  8. package/framework/agents/architecture/morph-spec-architect.md +66 -0
  9. package/framework/agents/architecture/packages-validator.md +89 -0
  10. package/framework/agents/architecture/security-expert.md +79 -0
  11. package/framework/agents/backend/ms-agent-expert.md +1 -1
  12. package/framework/agents/frontend/design-system-validator.md +100 -0
  13. package/framework/agents/frontend/nextjs-component-validator.md +97 -0
  14. package/framework/agents/frontend/{ui-ux-designer.md → ui-designer.md} +31 -0
  15. package/framework/agents.json +1 -1
  16. package/framework/hooks/claude-code/post-tool-use/dispatch.js +56 -0
  17. package/framework/hooks/claude-code/post-tool-use/handle-tool-failure.js +78 -44
  18. package/framework/hooks/claude-code/post-tool-use/loop-detect.js +103 -0
  19. package/framework/hooks/claude-code/post-tool-use/validator-feedback.js +60 -11
  20. package/framework/hooks/claude-code/session-start/inject-morph-context.js +21 -2
  21. package/framework/hooks/claude-code/stop/validate-completion.js +48 -1
  22. package/framework/hooks/shared/activity-logger.js +56 -1
  23. package/framework/hooks/shared/build-check.js +79 -0
  24. package/framework/hooks/shared/payload-utils.js +39 -0
  25. package/framework/hooks/shared/session-snapshot.js +105 -0
  26. package/framework/phases.json +1 -1
  27. package/framework/rubrics/backend.json +36 -0
  28. package/framework/rubrics/default.json +36 -0
  29. package/framework/rubrics/frontend.json +36 -0
  30. package/framework/skills/level-0-meta/mcp-registry.json +34 -1
  31. package/framework/skills/level-0-meta/morph-frontend-review/SKILL.md +1 -1
  32. package/framework/skills/level-1-workflows/morph-phase-codebase-analysis/SKILL.md +1 -1
  33. package/framework/skills/level-1-workflows/morph-phase-design/SKILL.md +11 -1
  34. package/framework/skills/level-1-workflows/morph-phase-implement/SKILL.md +12 -1
  35. package/framework/skills/level-1-workflows/morph-phase-plan/SKILL.md +1 -1
  36. package/framework/skills/level-1-workflows/morph-phase-tasks/SKILL.md +1 -1
  37. package/framework/skills/level-1-workflows/morph-phase-uiux/SKILL.md +119 -11
  38. package/framework/skills/level-1-workflows/morph-scope-escalation/SKILL.md +1 -1
  39. package/framework/standards/ai-agents/production.md +5 -2
  40. package/framework/standards/ai-agents/setup.md +13 -14
  41. package/framework/standards/ai-agents/workflows.md +8 -6
  42. package/framework/standards/backend/database/vector-search-rag.md +1 -1
  43. package/framework/templates/code/dotnet/jobs/agent.cs +0 -1
  44. package/package.json +1 -1
  45. package/src/commands/dashboard/dashboard.js +53 -0
  46. package/src/commands/mcp/mcp-setup.js +21 -0
  47. package/src/commands/project/init.js +11 -0
  48. package/src/dashboard/app/App.jsx +31 -0
  49. package/src/dashboard/app/components/DetailPanel.jsx +11 -0
  50. package/src/dashboard/app/components/FlowGraph.jsx +17 -0
  51. package/src/dashboard/app/components/SearchFilter.jsx +22 -0
  52. package/src/dashboard/app/components/Sidebar.jsx +45 -0
  53. package/src/dashboard/app/hooks/useDetailPanel.js +15 -0
  54. package/src/dashboard/app/main.jsx +13 -0
  55. package/src/dashboard/app/pages/AgentMap.jsx +371 -0
  56. package/src/dashboard/app/pages/HookInspector.jsx +418 -0
  57. package/src/dashboard/app/pages/LiveState.jsx +239 -0
  58. package/src/dashboard/app/pages/PhasePipeline.jsx +410 -0
  59. package/src/dashboard/app/pages/RulesMap.jsx +273 -0
  60. package/src/dashboard/app/pages/SkillsLibrary.jsx +367 -0
  61. package/src/dashboard/app/pages/Standards.jsx +332 -0
  62. package/src/dashboard/app/pages/Workflows.jsx +346 -0
  63. package/src/dashboard/app/styles/index.css +47 -0
  64. package/src/dashboard/index.html +12 -0
  65. package/src/dashboard/package.json +24 -0
  66. package/src/dashboard/scanner/agents-scanner.js +35 -0
  67. package/src/dashboard/scanner/hooks-scanner.js +41 -0
  68. package/src/dashboard/scanner/index.js +21 -0
  69. package/src/dashboard/scanner/phases-scanner.js +15 -0
  70. package/src/dashboard/scanner/rubrics-scanner.js +12 -0
  71. package/src/dashboard/scanner/rules-scanner.js +37 -0
  72. package/src/dashboard/scanner/skills-scanner.js +43 -0
  73. package/src/dashboard/scanner/squads-scanner.js +13 -0
  74. package/src/dashboard/scanner/standards-scanner.js +26 -0
  75. package/src/dashboard/scanner/state-scanner.js +8 -0
  76. package/src/dashboard/scanner/workflows-scanner.js +18 -0
  77. package/src/dashboard/vite-plugin-morph-data.js +17 -0
  78. package/src/dashboard/vite.config.js +24 -0
  79. package/src/lib/checkpoints/checkpoint-hooks.js +2 -1
  80. package/src/lib/contracts/sprint-contract-generator.js +137 -0
  81. package/src/lib/generators/recap-generator.js +15 -0
  82. package/src/lib/installers/mcp-installer.js +25 -0
  83. package/src/lib/validators/score-aggregator.js +244 -0
  84. package/src/lib/validators/validation-runner.js +44 -0
  85. package/src/scripts/setup-infra.js +8 -0
  86. package/src/utils/hooks-installer.js +12 -1
@@ -0,0 +1,410 @@
1
+ import { useCallback, useMemo } from 'react';
2
+ import { Handle, Position } from '@xyflow/react';
3
+ import morphData from 'virtual:morph-data';
4
+ import FlowGraph from '../components/FlowGraph.jsx';
5
+ import DetailPanel from '../components/DetailPanel.jsx';
6
+ import { useDetailPanel } from '../hooks/useDetailPanel.js';
7
+
8
+ // Phase color palette by phase id
9
+ const PHASE_COLORS = {
10
+ proposal: { bg: '#166534', border: '#22c55e', text: '#bbf7d0' },
11
+ uiux: { bg: '#854d0e', border: '#f59e0b', text: '#fef3c7' },
12
+ design: { bg: '#1e40af', border: '#60a5fa', text: '#dbeafe' },
13
+ plan: { bg: '#7c2d12', border: '#fb923c', text: '#fed7aa' },
14
+ tasks: { bg: '#6d28d9', border: '#a78bfa', text: '#ede9fe' },
15
+ implement: { bg: '#0e7490', border: '#22d3ee', text: '#cffafe' },
16
+ sync: { bg: '#374151', border: '#9ca3af', text: '#f3f4f6' },
17
+ };
18
+
19
+ const GATED_PHASES = new Set(['uiux', 'design', 'plan', 'tasks']);
20
+
21
+ // Gate node: small gold square with "G" label
22
+ function GateNode({ data }) {
23
+ return (
24
+ <div
25
+ title={data.label}
26
+ className="flex items-center justify-center rounded-sm select-none cursor-default"
27
+ style={{
28
+ width: 32,
29
+ height: 32,
30
+ background: '#78350f',
31
+ border: '2px solid #f59e0b',
32
+ boxShadow: '0 0 8px #f59e0b44',
33
+ color: '#fef3c7',
34
+ fontWeight: 700,
35
+ fontSize: 12,
36
+ }}
37
+ >
38
+ <Handle type="target" position={Position.Left} style={{ background: '#f59e0b', width: 7, height: 7 }} />
39
+ G
40
+ <Handle type="source" position={Position.Right} style={{ background: '#f59e0b', width: 7, height: 7 }} />
41
+ </div>
42
+ );
43
+ }
44
+
45
+ // Phase node: colored block with name and optional/index badge
46
+ function PhaseNode({ data }) {
47
+ const c = PHASE_COLORS[data.phaseId] || PHASE_COLORS.sync;
48
+ const isOptional = data.optional;
49
+
50
+ return (
51
+ <div
52
+ className="rounded-lg px-4 py-3 min-w-[140px] select-none cursor-pointer"
53
+ style={{
54
+ background: c.bg,
55
+ border: `2px ${isOptional ? 'dashed' : 'solid'} ${c.border}`,
56
+ boxShadow: `0 0 10px ${c.border}33`,
57
+ }}
58
+ >
59
+ <Handle type="target" position={Position.Left} style={{ background: c.border, width: 8, height: 8 }} />
60
+
61
+ <div className="text-white text-sm font-bold leading-tight">{data.label}</div>
62
+ <div className="mt-1 text-[10px] font-medium" style={{ color: c.text }}>
63
+ {isOptional ? 'optional' : `index: ${data.index}`}
64
+ </div>
65
+
66
+ <Handle type="source" position={Position.Right} style={{ background: c.border, width: 8, height: 8 }} />
67
+ </div>
68
+ );
69
+ }
70
+
71
+ const nodeTypes = { phaseNode: PhaseNode, gateNode: GateNode };
72
+
73
+ // Build nodes + edges with manual horizontal layout
74
+ function buildPipelineGraph(phases) {
75
+ const nodes = [];
76
+ const edges = [];
77
+
78
+ // Layout constants
79
+ const PHASE_W = 150;
80
+ const PHASE_H = 68;
81
+ const GATE_W = 32;
82
+ const GATE_H = 32;
83
+ const GAP = 60; // gap between a phase and the next gate / phase
84
+ const Y_CENTER = 0;
85
+
86
+ // Pre-compute x positions for each phase and its preceding gate (if gated)
87
+ // Walk phases left to right, tracking current x cursor
88
+ let cursorX = 0;
89
+ const phaseLayout = []; // { phase, x, gateX? }
90
+
91
+ for (const phase of phases) {
92
+ if (GATED_PHASES.has(phase.id)) {
93
+ // Insert gate before this phase
94
+ const gateX = cursorX;
95
+ cursorX += GATE_W + GAP;
96
+ phaseLayout.push({ phase, x: cursorX, gateX });
97
+ } else {
98
+ phaseLayout.push({ phase, x: cursorX, gateX: null });
99
+ }
100
+ cursorX += PHASE_W + GAP;
101
+ }
102
+
103
+ // Build nodes
104
+ for (const { phase, x, gateX } of phaseLayout) {
105
+ const c = PHASE_COLORS[phase.id] || PHASE_COLORS.sync;
106
+
107
+ // Gate node (if any)
108
+ if (gateX !== null) {
109
+ nodes.push({
110
+ id: `gate-${phase.id}`,
111
+ type: 'gateNode',
112
+ data: { label: `Gate: ${phase.displayName}` },
113
+ position: { x: gateX, y: Y_CENTER + (PHASE_H - GATE_H) / 2 },
114
+ });
115
+ }
116
+
117
+ // Phase node
118
+ nodes.push({
119
+ id: phase.id,
120
+ type: 'phaseNode',
121
+ data: {
122
+ label: phase.displayName,
123
+ phaseId: phase.id,
124
+ optional: phase.optional,
125
+ index: phase.index,
126
+ color: c.border,
127
+ phaseRef: phase,
128
+ },
129
+ position: { x, y: Y_CENTER },
130
+ });
131
+ }
132
+
133
+ // Build edges: chain gates and phases in sequence
134
+ // sequence: ph0 → gate-ph1 → ph1 → gate-ph2 → ph2 → ...
135
+ for (let i = 0; i < phaseLayout.length; i++) {
136
+ const curr = phaseLayout[i];
137
+ const next = phaseLayout[i + 1];
138
+
139
+ if (!next) continue;
140
+
141
+ const sourceId = curr.phase.id;
142
+
143
+ if (next.gateX !== null) {
144
+ // phase → gate → next-phase (two edges)
145
+ edges.push({
146
+ id: `edge-${sourceId}-gate-${next.phase.id}`,
147
+ source: sourceId,
148
+ target: `gate-${next.phase.id}`,
149
+ type: 'smoothstep',
150
+ style: { stroke: '#475569', strokeWidth: 1.8 },
151
+ });
152
+ edges.push({
153
+ id: `edge-gate-${next.phase.id}-${next.phase.id}`,
154
+ source: `gate-${next.phase.id}`,
155
+ target: next.phase.id,
156
+ type: 'smoothstep',
157
+ style: { stroke: '#475569', strokeWidth: 1.8 },
158
+ });
159
+ } else {
160
+ // phase → phase (no gate)
161
+ edges.push({
162
+ id: `edge-${sourceId}-${next.phase.id}`,
163
+ source: sourceId,
164
+ target: next.phase.id,
165
+ type: 'smoothstep',
166
+ style: { stroke: '#475569', strokeWidth: 1.8 },
167
+ });
168
+ }
169
+ }
170
+
171
+ return { nodes, edges };
172
+ }
173
+
174
+ // Detail panel content for a phase
175
+ function PhaseDetail({ phase, agents }) {
176
+ const c = PHASE_COLORS[phase.id] || PHASE_COLORS.sync;
177
+ const isGated = GATED_PHASES.has(phase.id);
178
+
179
+ // Agents active in this phase
180
+ const activeAgents = agents.filter(
181
+ (a) => a.activePhases?.includes(phase.id) || a.activePhases?.includes('*')
182
+ );
183
+
184
+ return (
185
+ <div className="text-sm text-gray-200 space-y-5">
186
+ {/* Phase badge + optional/gated indicators */}
187
+ <div className="flex flex-wrap items-center gap-2">
188
+ <span
189
+ className="px-2 py-0.5 rounded text-xs font-bold"
190
+ style={{ background: c.bg, color: c.text, border: `1px solid ${c.border}` }}
191
+ >
192
+ Phase {phase.index}
193
+ </span>
194
+ {phase.optional && (
195
+ <span className="px-2 py-0.5 rounded text-xs bg-gray-800 border border-dashed border-gray-600 text-gray-400">
196
+ optional
197
+ </span>
198
+ )}
199
+ {isGated && (
200
+ <span className="px-2 py-0.5 rounded text-xs bg-yellow-900/50 border border-yellow-600 text-yellow-300">
201
+ approval gate
202
+ </span>
203
+ )}
204
+ {phase.folder && (
205
+ <span className="px-2 py-0.5 rounded text-xs font-mono bg-gray-800 border border-gray-700 text-gray-400">
206
+ {phase.folder}
207
+ </span>
208
+ )}
209
+ </div>
210
+
211
+ {/* Goals */}
212
+ {phase.goals?.length > 0 && (
213
+ <div>
214
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Goals</p>
215
+ <ul className="space-y-1.5">
216
+ {phase.goals.map((g, i) => (
217
+ <li key={i} className="flex gap-2 text-xs text-gray-300">
218
+ <span className="text-gray-600 flex-shrink-0">•</span>
219
+ {g}
220
+ </li>
221
+ ))}
222
+ </ul>
223
+ </div>
224
+ )}
225
+
226
+ {/* Required outputs */}
227
+ {phase.requiredOutputs?.length > 0 && (
228
+ <div>
229
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Required Outputs</p>
230
+ <div className="flex flex-wrap gap-1">
231
+ {phase.requiredOutputs.map((o) => (
232
+ <span key={o} className="px-2 py-0.5 rounded text-xs bg-green-900/50 border border-green-700 text-green-300 font-mono">
233
+ {o}
234
+ </span>
235
+ ))}
236
+ </div>
237
+ </div>
238
+ )}
239
+
240
+ {/* Optional outputs */}
241
+ {phase.optionalOutputs?.length > 0 && (
242
+ <div>
243
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Optional Outputs</p>
244
+ <div className="flex flex-wrap gap-1">
245
+ {phase.optionalOutputs.map((o) => (
246
+ <span key={o} className="px-2 py-0.5 rounded text-xs bg-gray-800 border border-gray-700 text-gray-400 font-mono">
247
+ {o}
248
+ </span>
249
+ ))}
250
+ </div>
251
+ </div>
252
+ )}
253
+
254
+ {/* Active agents */}
255
+ {activeAgents.length > 0 && (
256
+ <div>
257
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Active Agents ({activeAgents.length})</p>
258
+ <div className="space-y-1">
259
+ {activeAgents.map((a) => (
260
+ <div key={a.id} className="flex items-center justify-between text-xs px-2 py-1 bg-gray-800 rounded">
261
+ <span className="text-gray-200 font-medium truncate">{a.title}</span>
262
+ <span className="text-gray-500 ml-2 flex-shrink-0">T{a.tier}</span>
263
+ </div>
264
+ ))}
265
+ </div>
266
+ </div>
267
+ )}
268
+
269
+ {/* Required skills */}
270
+ {phase.requiredSkills?.length > 0 && (
271
+ <div>
272
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Required Skills</p>
273
+ <div className="space-y-1">
274
+ {phase.requiredSkills.map((s, i) => (
275
+ <div key={i} className="flex items-start gap-2 text-xs">
276
+ <span className="px-1.5 py-0.5 rounded bg-pink-900/50 border border-pink-700 text-pink-300 font-mono flex-shrink-0">
277
+ {s.trigger}
278
+ </span>
279
+ <span className="text-gray-300 font-mono">{s.skill}</span>
280
+ </div>
281
+ ))}
282
+ </div>
283
+ </div>
284
+ )}
285
+
286
+ {/* Recommended MCPs */}
287
+ {phase.recommendedMCPs?.length > 0 && (
288
+ <div>
289
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Recommended MCPs</p>
290
+ <div className="flex flex-wrap gap-1">
291
+ {phase.recommendedMCPs.map((m) => (
292
+ <span key={m} className="px-2 py-0.5 rounded text-xs bg-blue-900/50 border border-blue-700 text-blue-300 font-mono">
293
+ {m}
294
+ </span>
295
+ ))}
296
+ </div>
297
+ </div>
298
+ )}
299
+
300
+ {/* Pause points */}
301
+ {phase.pausePoints?.length > 0 && (
302
+ <div>
303
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-2">Pause Points</p>
304
+ <div className="space-y-1">
305
+ {phase.pausePoints.map((pp) => (
306
+ <div key={pp.id} className="flex items-center gap-2 text-xs px-2 py-1.5 bg-yellow-900/20 border border-yellow-700/50 rounded">
307
+ <span className="text-yellow-400 flex-shrink-0">&#x1F512;</span>
308
+ <span className="text-yellow-200">{pp.label}</span>
309
+ </div>
310
+ ))}
311
+ </div>
312
+ </div>
313
+ )}
314
+ </div>
315
+ );
316
+ }
317
+
318
+ export default function PhasePipeline() {
319
+ const { phases, agents } = morphData;
320
+ const { panel, openPanel, closePanel } = useDetailPanel();
321
+
322
+ const { nodes, edges } = useMemo(() => buildPipelineGraph(phases.list), [phases.list]);
323
+
324
+ const handleNodeClick = useCallback(
325
+ (_event, node) => {
326
+ if (node.type === 'gateNode') {
327
+ // Extract phase id from gate id ("gate-design" → "design")
328
+ const phaseId = node.id.replace('gate-', '');
329
+ const phase = phases.list.find((p) => p.id === phaseId);
330
+ if (phase) {
331
+ openPanel(`Gate: ${phase.displayName}`, (
332
+ <div className="text-sm text-gray-300 space-y-3">
333
+ <p className="text-yellow-300 text-xs">This gate requires explicit approval before the phase can begin.</p>
334
+ <p className="text-gray-400 text-xs">Gate ID: <span className="font-mono text-gray-300">{phaseId}</span></p>
335
+ </div>
336
+ ));
337
+ }
338
+ return;
339
+ }
340
+
341
+ if (node.type === 'phaseNode') {
342
+ const phase = node.data.phaseRef;
343
+ if (phase) {
344
+ openPanel(phase.displayName, (
345
+ <PhaseDetail phase={phase} agents={agents.list} />
346
+ ));
347
+ }
348
+ }
349
+ },
350
+ [openPanel, phases.list, agents.list]
351
+ );
352
+
353
+ return (
354
+ <div className="flex flex-col h-full">
355
+ {/* Header */}
356
+ <div className="px-6 py-4 border-b border-gray-800 flex-shrink-0">
357
+ <h1 className="text-white text-xl font-bold">Phase Pipeline</h1>
358
+ <p className="text-gray-400 text-sm mt-0.5">
359
+ Horizontal workflow of the 7 morph-spec phases. Gold gates mark required approval points. Dashed borders indicate optional phases.
360
+ </p>
361
+ </div>
362
+
363
+ {/* Flow graph */}
364
+ <div className="flex-1 relative">
365
+ <FlowGraph
366
+ nodes={nodes}
367
+ edges={edges}
368
+ nodeTypes={nodeTypes}
369
+ onNodeClick={handleNodeClick}
370
+ fitView
371
+ />
372
+
373
+ {/* Legend */}
374
+ <div className="absolute top-3 left-3 bg-gray-900/90 border border-gray-700 rounded-lg p-3 text-xs space-y-2 z-10">
375
+ <p className="text-gray-400 font-semibold uppercase tracking-wide text-[10px] mb-2">Legend</p>
376
+ <div className="flex items-center gap-2">
377
+ <span className="inline-block w-10 h-4 rounded-sm" style={{ background: '#166534', border: '2px solid #22c55e' }} />
378
+ <span className="text-gray-300">Phase (solid = required)</span>
379
+ </div>
380
+ <div className="flex items-center gap-2">
381
+ <span className="inline-block w-10 h-4 rounded-sm" style={{ background: '#854d0e', border: '2px dashed #f59e0b' }} />
382
+ <span className="text-gray-300">Phase (dashed = optional)</span>
383
+ </div>
384
+ <div className="flex items-center gap-2">
385
+ <span
386
+ className="inline-flex items-center justify-center rounded-sm text-[10px] font-bold flex-shrink-0"
387
+ style={{ width: 20, height: 20, background: '#78350f', border: '2px solid #f59e0b', color: '#fef3c7' }}
388
+ >
389
+ G
390
+ </span>
391
+ <span className="text-gray-300">Approval Gate</span>
392
+ </div>
393
+ <div className="flex items-center gap-2">
394
+ <span className="inline-block w-6 border-t-2 border-gray-500" />
395
+ <span className="text-gray-400">Sequence</span>
396
+ </div>
397
+ </div>
398
+
399
+ {/* Phase count badge */}
400
+ <div className="absolute top-3 right-3 bg-gray-900/90 border border-gray-700 rounded-lg px-3 py-1.5 text-xs text-gray-400 z-10">
401
+ {phases.list.length} phases · {phases.list.filter((p) => GATED_PHASES.has(p.id)).length} gates
402
+ </div>
403
+ </div>
404
+
405
+ <DetailPanel open={panel.open} title={panel.title} onClose={closePanel}>
406
+ {panel.content}
407
+ </DetailPanel>
408
+ </div>
409
+ );
410
+ }
@@ -0,0 +1,273 @@
1
+ import { useState, useMemo, useCallback } from 'react';
2
+ import { Handle, Position } from '@xyflow/react';
3
+ import morphData from 'virtual:morph-data';
4
+ import FlowGraph from '../components/FlowGraph.jsx';
5
+ import DetailPanel from '../components/DetailPanel.jsx';
6
+ import SearchFilter from '../components/SearchFilter.jsx';
7
+ import { useDetailPanel } from '../hooks/useDetailPanel.js';
8
+
9
+ // ─── Node Components ──────────────────────────────────────────────────────────
10
+
11
+ function RuleNode({ data }) {
12
+ return (
13
+ <div
14
+ className="rounded-lg px-3 py-2 min-w-[150px] max-w-[180px] cursor-pointer select-none"
15
+ style={{
16
+ background: '#451a03',
17
+ border: `1.5px solid ${data.dimmed ? '#78350f' : '#f59e0b'}`,
18
+ boxShadow: data.dimmed ? 'none' : '0 0 8px #f59e0b33',
19
+ opacity: data.dimmed ? 0.3 : 1,
20
+ }}
21
+ >
22
+ <Handle type="source" position={Position.Right} style={{ background: '#f59e0b', width: 7, height: 7 }} />
23
+ <div className="text-amber-100 text-xs font-semibold leading-tight truncate" title={data.label}>
24
+ {data.label}
25
+ </div>
26
+ <div className="text-amber-400 text-[10px] mt-0.5">
27
+ {data.pathCount} glob{data.pathCount !== 1 ? 's' : ''}
28
+ {data.stacks?.length > 0 ? ` · ${data.stacks.join(', ')}` : ''}
29
+ </div>
30
+ </div>
31
+ );
32
+ }
33
+
34
+ function StandardRefNode({ data }) {
35
+ return (
36
+ <div
37
+ className="rounded-lg px-3 py-2 min-w-[140px] max-w-[170px] select-none cursor-default"
38
+ style={{
39
+ background: '#1e3a5f',
40
+ border: `1.5px solid ${data.dimmed ? '#1e40af' : '#3b82f6'}`,
41
+ boxShadow: data.dimmed ? 'none' : '0 0 8px #3b82f633',
42
+ opacity: data.dimmed ? 0.3 : 1,
43
+ }}
44
+ >
45
+ <Handle type="target" position={Position.Left} style={{ background: '#3b82f6', width: 7, height: 7 }} />
46
+ <div className="text-blue-100 text-xs font-semibold leading-tight truncate" title={data.label}>
47
+ {data.label}
48
+ </div>
49
+ <div className="text-blue-400 text-[10px] mt-0.5">standard ref</div>
50
+ </div>
51
+ );
52
+ }
53
+
54
+ const nodeTypes = { ruleNode: RuleNode, standardRefNode: StandardRefNode };
55
+
56
+ // ─── Graph Builder ────────────────────────────────────────────────────────────
57
+
58
+ function buildGraph(rules, search, stackFilter, dimmed) {
59
+ const nodes = [];
60
+ const edges = [];
61
+
62
+ // Collect unique standard refs
63
+ const allStdRefs = [...new Set(rules.flatMap((r) => r.standardRefs))];
64
+
65
+ // Left column: rules at x=0
66
+ const RULE_X = 0;
67
+ const STD_X = 320;
68
+ const ROW_H = 70;
69
+
70
+ // Position rules
71
+ const filteredRuleIds = new Set(
72
+ rules
73
+ .filter((r) => {
74
+ const q = search.toLowerCase();
75
+ const matchSearch = !q || r.name.toLowerCase().includes(q) || r.stacks.some((s) => s.toLowerCase().includes(q));
76
+ const matchStack = !stackFilter || r.stacks.includes(stackFilter);
77
+ return matchSearch && matchStack;
78
+ })
79
+ .map((r) => r.name)
80
+ );
81
+
82
+ const hasFilter = search || stackFilter;
83
+
84
+ rules.forEach((rule, i) => {
85
+ const isDimmed = hasFilter && !filteredRuleIds.has(rule.name);
86
+ nodes.push({
87
+ id: `rule-${rule.name}`,
88
+ type: 'ruleNode',
89
+ position: { x: RULE_X, y: i * ROW_H },
90
+ data: {
91
+ label: rule.name,
92
+ pathCount: rule.paths.length,
93
+ stacks: rule.stacks,
94
+ ruleRef: rule,
95
+ dimmed: isDimmed,
96
+ },
97
+ });
98
+ });
99
+
100
+ // Position standard refs in right column
101
+ allStdRefs.forEach((ref, i) => {
102
+ const label = ref.replace(/\.md$/, '').split('/').pop();
103
+ nodes.push({
104
+ id: `std-${ref}`,
105
+ type: 'standardRefNode',
106
+ position: { x: STD_X, y: i * ROW_H + (rules.length * ROW_H - allStdRefs.length * ROW_H) / 2 },
107
+ data: { label, fullPath: ref, dimmed: false },
108
+ });
109
+ });
110
+
111
+ // Edges: rule → its standard refs
112
+ for (const rule of rules) {
113
+ const isDimmed = hasFilter && !filteredRuleIds.has(rule.name);
114
+ for (const ref of rule.standardRefs) {
115
+ edges.push({
116
+ id: `edge-${rule.name}-${ref}`,
117
+ source: `rule-${rule.name}`,
118
+ target: `std-${ref}`,
119
+ type: 'smoothstep',
120
+ style: {
121
+ stroke: isDimmed ? '#374151' : '#f59e0b',
122
+ strokeWidth: 1.2,
123
+ opacity: isDimmed ? 0.2 : 0.6,
124
+ },
125
+ });
126
+ }
127
+ }
128
+
129
+ return { nodes, edges };
130
+ }
131
+
132
+ // ─── Detail Content ───────────────────────────────────────────────────────────
133
+
134
+ function RuleDetail({ rule }) {
135
+ const [showContent, setShowContent] = useState(false);
136
+ return (
137
+ <div className="text-sm text-gray-200 space-y-4">
138
+ <div>
139
+ <p className="text-gray-400 text-xs">File: <span className="font-mono text-gray-300">{rule.filename}</span></p>
140
+ </div>
141
+
142
+ {rule.stacks?.length > 0 && (
143
+ <div>
144
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-1.5">Stacks</p>
145
+ <div className="flex flex-wrap gap-1">
146
+ {rule.stacks.map((s) => (
147
+ <span key={s} className="px-2 py-0.5 bg-green-900/50 border border-green-700 rounded text-xs text-green-300">{s}</span>
148
+ ))}
149
+ </div>
150
+ </div>
151
+ )}
152
+
153
+ {rule.paths?.length > 0 && (
154
+ <div>
155
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-1.5">Path Globs</p>
156
+ <div className="space-y-1">
157
+ {rule.paths.map((p) => (
158
+ <code key={p} className="block px-2 py-1 bg-gray-950 border border-gray-800 rounded text-[10px] font-mono text-amber-300">{p}</code>
159
+ ))}
160
+ </div>
161
+ </div>
162
+ )}
163
+
164
+ {rule.standardRefs?.length > 0 && (
165
+ <div>
166
+ <p className="text-gray-500 text-xs uppercase tracking-wide mb-1.5">Standard References</p>
167
+ <ul className="space-y-1">
168
+ {rule.standardRefs.map((ref) => (
169
+ <li key={ref} className="flex items-center gap-2 text-xs">
170
+ <span className="w-1.5 h-1.5 rounded-full bg-blue-400 flex-shrink-0" />
171
+ <span className="font-mono text-blue-300">{ref}</span>
172
+ </li>
173
+ ))}
174
+ </ul>
175
+ </div>
176
+ )}
177
+
178
+ <div>
179
+ <button
180
+ onClick={() => setShowContent((v) => !v)}
181
+ className="w-full text-left px-3 py-1.5 bg-gray-800 hover:bg-gray-700 border border-gray-700 rounded text-xs text-gray-300 transition-colors"
182
+ >
183
+ {showContent ? 'Hide Content' : 'Show Full Content'}
184
+ </button>
185
+ {showContent && (
186
+ <pre className="mt-2 p-3 bg-gray-950 border border-gray-800 rounded text-[9px] text-gray-400 overflow-x-auto whitespace-pre-wrap font-mono leading-relaxed max-h-64 overflow-y-auto">
187
+ {rule.content}
188
+ </pre>
189
+ )}
190
+ </div>
191
+ </div>
192
+ );
193
+ }
194
+
195
+ // ─── Main Page ────────────────────────────────────────────────────────────────
196
+
197
+ export default function RulesMap() {
198
+ const { rules } = morphData;
199
+ const [search, setSearch] = useState('');
200
+ const [stackFilter, setStackFilter] = useState('');
201
+ const { panel, openPanel, closePanel } = useDetailPanel();
202
+
203
+ const allStacks = useMemo(
204
+ () => [...new Set(rules.flatMap((r) => r.stacks))].sort(),
205
+ [rules]
206
+ );
207
+
208
+ const { nodes, edges } = useMemo(
209
+ () => buildGraph(rules, search, stackFilter),
210
+ [rules, search, stackFilter]
211
+ );
212
+
213
+ const handleNodeClick = useCallback(
214
+ (_event, node) => {
215
+ if (node.type === 'ruleNode') {
216
+ const rule = node.data.ruleRef;
217
+ if (rule) openPanel(rule.name, <RuleDetail rule={rule} />);
218
+ }
219
+ },
220
+ [openPanel]
221
+ );
222
+
223
+ const filterConfig = [
224
+ { name: 'stack', label: 'Stack', options: allStacks, value: stackFilter },
225
+ ];
226
+
227
+ return (
228
+ <div className="flex flex-col h-full">
229
+ <SearchFilter
230
+ placeholder="Search rules by name or stack..."
231
+ filters={filterConfig}
232
+ onSearchChange={setSearch}
233
+ onFilterChange={(name, value) => {
234
+ if (name === 'stack') setStackFilter(value);
235
+ }}
236
+ />
237
+
238
+ <div className="flex-1 relative">
239
+ <FlowGraph
240
+ nodes={nodes}
241
+ edges={edges}
242
+ nodeTypes={nodeTypes}
243
+ onNodeClick={handleNodeClick}
244
+ fitView
245
+ />
246
+
247
+ <div className="absolute top-3 left-3 bg-gray-900/90 border border-gray-700 rounded-lg p-3 text-xs space-y-2 z-10">
248
+ <p className="text-gray-400 font-semibold uppercase tracking-wide text-[10px] mb-2">Legend</p>
249
+ <div className="flex items-center gap-2">
250
+ <span className="inline-block w-4 h-4 rounded-sm" style={{ background: '#451a03', border: '1.5px solid #f59e0b' }} />
251
+ <span className="text-gray-300">Rule file</span>
252
+ </div>
253
+ <div className="flex items-center gap-2">
254
+ <span className="inline-block w-4 h-4 rounded-sm" style={{ background: '#1e3a5f', border: '1.5px solid #3b82f6' }} />
255
+ <span className="text-gray-300">Standard reference</span>
256
+ </div>
257
+ <div className="flex items-center gap-2">
258
+ <span className="inline-block w-6 border-t border-amber-400 opacity-60" />
259
+ <span className="text-gray-400">references</span>
260
+ </div>
261
+ </div>
262
+
263
+ <div className="absolute top-3 right-3 bg-gray-900/90 border border-gray-700 rounded-lg px-3 py-1.5 text-xs text-gray-400 z-10">
264
+ {rules.length} rules
265
+ </div>
266
+ </div>
267
+
268
+ <DetailPanel open={panel.open} title={panel.title} onClose={closePanel}>
269
+ {panel.content}
270
+ </DetailPanel>
271
+ </div>
272
+ );
273
+ }