@polymorphism-tech/morph-spec 8.1.2 → 8.2.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.
- package/README.md +2 -2
- package/bin/morph-spec.js +15 -0
- package/claude-plugin.json +1 -1
- package/docs/CHEATSHEET.md +6 -4
- package/docs/QUICKSTART.md +1 -1
- package/framework/CLAUDE.md +6 -3
- package/framework/MORPH.md +8 -2
- package/framework/hooks/claude-code/core/worktree-context.js +43 -0
- package/framework/skills/core/create-umbraco-lp/SKILL.md +1 -1
- package/framework/skills/core/migrate-lp-to-umbraco/SKILL.md +1 -1
- package/framework/skills/core/morph-apply/SKILL.md +2 -2
- package/framework/skills/core/morph-brainstorming/SKILL.md +1 -1
- package/framework/skills/core/morph-eval/SKILL.md +1 -1
- package/framework/skills/core/morph-hotfix/SKILL.md +1 -1
- package/framework/skills/core/morph-implement/SKILL.md +1 -1
- package/framework/skills/core/morph-plan/SKILL.md +1 -1
- package/framework/skills/core/morph-proposal/SKILL.md +1 -1
- package/framework/skills/core/morph-review/SKILL.md +1 -1
- package/framework/skills/core/morph-stack-scan/SKILL.md +1 -1
- package/framework/skills/core/morph-standards/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/commands/project/fleet.js +54 -0
- package/src/commands/project/worktree.js +56 -7
- package/src/commands/state/advance.js +8 -0
- package/src/commands/state/create.js +6 -0
- package/src/commands/state/handoff.js +49 -0
- package/src/lib/generators/handoff-generator.js +267 -0
- package/src/lib/generators/index.js +1 -3
- package/src/lib/worktree/env-allocator.js +149 -0
- package/src/lib/worktree/fleet-scanner.js +156 -0
- package/src/lib/context/context-builder.js +0 -289
- package/src/lib/generators/context-generator.js +0 -519
- package/src/lib/generators/mandate-generator.js +0 -468
- package/src/lib/generators/recap-generator.js +0 -285
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Context-Aware Dispatch — Agent Context Builder
|
|
3
|
-
*
|
|
4
|
-
* Assembles curated context payloads per agent, based on domain persona,
|
|
5
|
-
* phase, and active task. Agents get only what they need — not everything.
|
|
6
|
-
*
|
|
7
|
-
* @module context-builder
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { readFileSync, existsSync } from 'fs';
|
|
11
|
-
import { join, dirname } from 'path';
|
|
12
|
-
import { fileURLToPath } from 'url';
|
|
13
|
-
import { getAbsoluteOutputPath } from '../../core/paths/output-schema.js';
|
|
14
|
-
|
|
15
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
-
const AGENTS_JSON_PATH = join(__dirname, '../../../framework/agents.json');
|
|
17
|
-
const STANDARDS_JSON_PATH = join(__dirname, '../../../framework/standards/STANDARDS.json');
|
|
18
|
-
|
|
19
|
-
let _agentsCache = null;
|
|
20
|
-
let _standardsCache = null;
|
|
21
|
-
|
|
22
|
-
function loadAgents() {
|
|
23
|
-
if (_agentsCache) return _agentsCache;
|
|
24
|
-
if (!existsSync(AGENTS_JSON_PATH)) return { agents: {} };
|
|
25
|
-
try {
|
|
26
|
-
_agentsCache = JSON.parse(readFileSync(AGENTS_JSON_PATH, 'utf8'));
|
|
27
|
-
return _agentsCache;
|
|
28
|
-
} catch { return { agents: {} }; }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function loadStandards() {
|
|
32
|
-
if (_standardsCache) return _standardsCache;
|
|
33
|
-
if (!existsSync(STANDARDS_JSON_PATH)) return { standards: [] };
|
|
34
|
-
try {
|
|
35
|
-
_standardsCache = JSON.parse(readFileSync(STANDARDS_JSON_PATH, 'utf8'));
|
|
36
|
-
return _standardsCache;
|
|
37
|
-
} catch { return { standards: [] }; }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Max chars for spec content (flat — v5 has no tier hierarchy) */
|
|
41
|
-
const SPEC_BUDGET = 8000;
|
|
42
|
-
|
|
43
|
-
/** Max number of standard digests */
|
|
44
|
-
const STANDARDS_BUDGET = 10;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Context assembly rules by phase.
|
|
48
|
-
* Controls which context sections are included.
|
|
49
|
-
*/
|
|
50
|
-
const PHASE_CONTEXT_PROFILES = {
|
|
51
|
-
proposal: {
|
|
52
|
-
includeSpec: false,
|
|
53
|
-
includeStandards: false,
|
|
54
|
-
includeTask: false,
|
|
55
|
-
includeCodePaths: false,
|
|
56
|
-
includePhaseArtifacts: false,
|
|
57
|
-
},
|
|
58
|
-
uiux: {
|
|
59
|
-
includeSpec: false,
|
|
60
|
-
includeStandards: true,
|
|
61
|
-
includeTask: false,
|
|
62
|
-
includeCodePaths: false,
|
|
63
|
-
includePhaseArtifacts: false,
|
|
64
|
-
},
|
|
65
|
-
plan: {
|
|
66
|
-
includeSpec: true,
|
|
67
|
-
includeStandards: true,
|
|
68
|
-
includeTask: false,
|
|
69
|
-
includeCodePaths: false,
|
|
70
|
-
includePhaseArtifacts: true,
|
|
71
|
-
},
|
|
72
|
-
implement: {
|
|
73
|
-
includeSpec: true,
|
|
74
|
-
includeStandards: true,
|
|
75
|
-
includeTask: true,
|
|
76
|
-
includeCodePaths: true,
|
|
77
|
-
includePhaseArtifacts: false,
|
|
78
|
-
},
|
|
79
|
-
review: {
|
|
80
|
-
includeSpec: true,
|
|
81
|
-
includeStandards: true,
|
|
82
|
-
includeTask: false,
|
|
83
|
-
includeCodePaths: true,
|
|
84
|
-
includePhaseArtifacts: true,
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Extract only the FR sections referenced by a task.
|
|
90
|
-
*
|
|
91
|
-
* @param {string} specContent - Full spec.md content
|
|
92
|
-
* @param {string[]} frIds - FR IDs (e.g., ['FR001', 'FR003'])
|
|
93
|
-
* @returns {string} Scoped spec with only referenced FRs
|
|
94
|
-
*/
|
|
95
|
-
export function scopeSpecToFRs(specContent, frIds) {
|
|
96
|
-
if (!frIds || frIds.length === 0 || !specContent) return specContent;
|
|
97
|
-
|
|
98
|
-
const sections = [];
|
|
99
|
-
|
|
100
|
-
// Always include overview/context sections
|
|
101
|
-
const overviewMatch = specContent.match(/^(#[^#][\s\S]*?)(?=\n##\s+FR\d)/m);
|
|
102
|
-
if (overviewMatch) {
|
|
103
|
-
sections.push(overviewMatch[1].trim());
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Extract each referenced FR section
|
|
107
|
-
for (const frId of frIds) {
|
|
108
|
-
const pattern = new RegExp(
|
|
109
|
-
`(##\\s+${frId}[:\\s—–\\-][\\s\\S]*?)(?=\\n##\\s+FR\\d|\\n##\\s+[^F]|$)`,
|
|
110
|
-
'm'
|
|
111
|
-
);
|
|
112
|
-
const match = pattern.exec(specContent);
|
|
113
|
-
if (match) {
|
|
114
|
-
sections.push(match[1].trim());
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return sections.length > 0 ? sections.join('\n\n---\n\n') : specContent;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Build standard digests for an agent based on their standards[] array.
|
|
123
|
-
*
|
|
124
|
-
* @param {string} agentId - Agent ID from agents.json
|
|
125
|
-
* @returns {string[]} Array of digest paragraphs
|
|
126
|
-
*/
|
|
127
|
-
export function buildAgentDigests(agentId) {
|
|
128
|
-
const agents = loadAgents();
|
|
129
|
-
const agentData = agents.agents?.[agentId];
|
|
130
|
-
if (!agentData?.standards || agentData.standards.length === 0) return [];
|
|
131
|
-
|
|
132
|
-
const standards = loadStandards();
|
|
133
|
-
const standardsList = standards.standards || [];
|
|
134
|
-
|
|
135
|
-
const digests = [];
|
|
136
|
-
for (const stdId of agentData.standards) {
|
|
137
|
-
const std = standardsList.find(s => s.id === stdId);
|
|
138
|
-
if (std?.digest) {
|
|
139
|
-
digests.push(`[${std.id}] ${std.name}: ${std.digest}`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return digests;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Extract FR IDs referenced by a task. Reads `[FR001, FR003]` markers from the
|
|
148
|
-
* task title and any `FR...` references in the task description/body.
|
|
149
|
-
*
|
|
150
|
-
* @param {string} taskTitle - Task title that may contain [FR...] references
|
|
151
|
-
* @param {string} [taskBlock] - Task description/body for further references
|
|
152
|
-
* @returns {string[]}
|
|
153
|
-
*/
|
|
154
|
-
export function extractFRIds(taskTitle, taskBlock) {
|
|
155
|
-
const frIds = new Set();
|
|
156
|
-
|
|
157
|
-
// From title: [FR001, FR003]
|
|
158
|
-
const titleMatch = (taskTitle || '').match(/\[([^\]]*FR\d+[^\]]*)\]/);
|
|
159
|
-
if (titleMatch) {
|
|
160
|
-
const matches = titleMatch[1].match(/FR\d+/g);
|
|
161
|
-
if (matches) matches.forEach(id => frIds.add(id));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// From body: **FRs:** FR001, FR002
|
|
165
|
-
if (taskBlock) {
|
|
166
|
-
const bodyMatch = taskBlock.match(/\*\*FRs?:\*\*\s*(.+)/i);
|
|
167
|
-
if (bodyMatch) {
|
|
168
|
-
const matches = bodyMatch[1].match(/FR\d+/g);
|
|
169
|
-
if (matches) matches.forEach(id => frIds.add(id));
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return [...frIds];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Build a curated context payload for a specific agent.
|
|
178
|
-
*
|
|
179
|
-
* @param {string} agentId - Agent ID from agents.json
|
|
180
|
-
* @param {Object} options
|
|
181
|
-
* @param {string} options.feature - Feature name
|
|
182
|
-
* @param {string} options.phase - Current phase
|
|
183
|
-
* @param {string} [options.taskId] - Active task ID (implement phase)
|
|
184
|
-
* @param {string} [options.projectPath='.'] - Project root
|
|
185
|
-
* @returns {AgentContext}
|
|
186
|
-
*
|
|
187
|
-
* @typedef {Object} AgentContext
|
|
188
|
-
* @property {string|null} spec - Full spec or task-scoped section
|
|
189
|
-
* @property {string[]} standards - Digest paragraphs for this agent's standards
|
|
190
|
-
* @property {string|null} task - Mandate task section or null
|
|
191
|
-
* @property {string[]} codeContext - Relevant file paths
|
|
192
|
-
* @property {string|null} phaseArtifacts - Previous phase outputs
|
|
193
|
-
* @property {Object} budget - Context budget hints
|
|
194
|
-
*/
|
|
195
|
-
export async function buildAgentContext(agentId, { feature, phase, taskId, projectPath = '.' }) {
|
|
196
|
-
const agents = loadAgents();
|
|
197
|
-
const profile = PHASE_CONTEXT_PROFILES[phase] || PHASE_CONTEXT_PROFILES.implement;
|
|
198
|
-
const maxSpecChars = SPEC_BUDGET;
|
|
199
|
-
const maxStandards = STANDARDS_BUDGET;
|
|
200
|
-
|
|
201
|
-
const result = {
|
|
202
|
-
spec: null,
|
|
203
|
-
standards: [],
|
|
204
|
-
task: null,
|
|
205
|
-
codeContext: [],
|
|
206
|
-
phaseArtifacts: null,
|
|
207
|
-
budget: { maxSpecChars, maxStandards },
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
// Standards digests
|
|
211
|
-
if (profile.includeStandards) {
|
|
212
|
-
result.standards = buildAgentDigests(agentId).slice(0, maxStandards);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Spec content (scoped by task FRs when possible)
|
|
216
|
-
if (profile.includeSpec) {
|
|
217
|
-
try {
|
|
218
|
-
const specPath = getAbsoluteOutputPath(projectPath, feature, 'spec');
|
|
219
|
-
if (existsSync(specPath)) {
|
|
220
|
-
let specContent = readFileSync(specPath, 'utf8');
|
|
221
|
-
|
|
222
|
-
// If task-scoped, extract mandate section for this task
|
|
223
|
-
if (taskId) {
|
|
224
|
-
const mandatePath = getAbsoluteOutputPath(projectPath, feature, 'mandate');
|
|
225
|
-
if (existsSync(mandatePath)) {
|
|
226
|
-
try {
|
|
227
|
-
const { extractTaskSection } = await import('../../lib/generators/mandate-generator.js');
|
|
228
|
-
const section = extractTaskSection(readFileSync(mandatePath, 'utf8'), taskId);
|
|
229
|
-
if (section) {
|
|
230
|
-
result.spec = section.slice(0, maxSpecChars);
|
|
231
|
-
result.task = section;
|
|
232
|
-
}
|
|
233
|
-
} catch { /* fall through to raw spec */ }
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (!result.spec) {
|
|
238
|
-
result.spec = specContent.slice(0, maxSpecChars);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
} catch { /* spec not available */ }
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Task mandate section
|
|
245
|
-
if (profile.includeTask && taskId && !result.task) {
|
|
246
|
-
try {
|
|
247
|
-
const mandatePath = getAbsoluteOutputPath(projectPath, feature, 'mandate');
|
|
248
|
-
if (existsSync(mandatePath)) {
|
|
249
|
-
const { extractTaskSection } = await import('../../lib/generators/mandate-generator.js');
|
|
250
|
-
const section = extractTaskSection(readFileSync(mandatePath, 'utf8'), taskId);
|
|
251
|
-
if (section) result.task = section;
|
|
252
|
-
}
|
|
253
|
-
} catch { /* mandate not available */ }
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Code paths from task
|
|
257
|
-
if (profile.includeCodePaths && taskId) {
|
|
258
|
-
try {
|
|
259
|
-
const planPath = getAbsoluteOutputPath(projectPath, feature, 'plan');
|
|
260
|
-
if (existsSync(planPath)) {
|
|
261
|
-
const planContent = readFileSync(planPath, 'utf8');
|
|
262
|
-
const backtickPaths = planContent.match(/`([^`]+\.[a-z]{1,4})`/g);
|
|
263
|
-
if (backtickPaths) {
|
|
264
|
-
result.codeContext = [...new Set(backtickPaths.map(p => p.replace(/`/g, '')))].slice(0, 20);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
} catch { /* plan not available */ }
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Phase artifacts (decisions, previous outputs)
|
|
271
|
-
if (profile.includePhaseArtifacts) {
|
|
272
|
-
try {
|
|
273
|
-
const decisionsPath = getAbsoluteOutputPath(projectPath, feature, 'decisions');
|
|
274
|
-
if (existsSync(decisionsPath)) {
|
|
275
|
-
result.phaseArtifacts = readFileSync(decisionsPath, 'utf8').slice(0, 3000);
|
|
276
|
-
}
|
|
277
|
-
} catch { /* decisions not available */ }
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return result;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Invalidate module-level caches. Used in tests.
|
|
285
|
-
*/
|
|
286
|
-
export function clearCache() {
|
|
287
|
-
_agentsCache = null;
|
|
288
|
-
_standardsCache = null;
|
|
289
|
-
}
|