dual-brain 0.1.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 (68) hide show
  1. package/AGENTS.md +97 -0
  2. package/CLAUDE.md +147 -0
  3. package/LICENSE +21 -0
  4. package/README.md +197 -0
  5. package/agents/implementer.md +22 -0
  6. package/agents/researcher.md +25 -0
  7. package/agents/verifier.md +30 -0
  8. package/bin/dual-brain.mjs +2868 -0
  9. package/hooks/auto-update-wrapper.mjs +102 -0
  10. package/hooks/auto-update.sh +67 -0
  11. package/hooks/budget-balancer.mjs +679 -0
  12. package/hooks/control-panel.mjs +1195 -0
  13. package/hooks/cost-logger.mjs +286 -0
  14. package/hooks/cost-report.mjs +351 -0
  15. package/hooks/decision-ledger.mjs +299 -0
  16. package/hooks/dual-brain-review.mjs +404 -0
  17. package/hooks/dual-brain-think.mjs +393 -0
  18. package/hooks/enforce-tier.mjs +469 -0
  19. package/hooks/failure-detector.mjs +138 -0
  20. package/hooks/gpt-work-dispatcher.mjs +512 -0
  21. package/hooks/head-guard.mjs +105 -0
  22. package/hooks/health-check.mjs +444 -0
  23. package/hooks/install-git-hooks.mjs +106 -0
  24. package/hooks/model-registry.mjs +859 -0
  25. package/hooks/plan-generator.mjs +544 -0
  26. package/hooks/profiles.mjs +254 -0
  27. package/hooks/quality-gate.mjs +355 -0
  28. package/hooks/risk-classifier.mjs +41 -0
  29. package/hooks/session-report.mjs +514 -0
  30. package/hooks/setup-wizard.mjs +130 -0
  31. package/hooks/summary-checkpoint.mjs +432 -0
  32. package/hooks/task-classifier.mjs +328 -0
  33. package/hooks/test-orchestrator.mjs +1077 -0
  34. package/hooks/vibe-memory.mjs +463 -0
  35. package/hooks/vibe-router.mjs +387 -0
  36. package/hooks/wave-orchestrator.mjs +1397 -0
  37. package/install.mjs +1541 -0
  38. package/mcp-server/README.md +81 -0
  39. package/mcp-server/index.mjs +388 -0
  40. package/orchestrator.json +215 -0
  41. package/package.json +108 -0
  42. package/playbooks/debug.json +49 -0
  43. package/playbooks/refactor.json +57 -0
  44. package/playbooks/security-audit.json +57 -0
  45. package/playbooks/security.json +38 -0
  46. package/playbooks/test-gen.json +48 -0
  47. package/plugin.json +22 -0
  48. package/review-rules.md +17 -0
  49. package/shell-hook.sh +26 -0
  50. package/skills/go.md +22 -0
  51. package/skills/review.md +19 -0
  52. package/skills/status.md +13 -0
  53. package/skills/think.md +22 -0
  54. package/src/brief.mjs +266 -0
  55. package/src/decide.mjs +635 -0
  56. package/src/decompose.mjs +331 -0
  57. package/src/detect.mjs +345 -0
  58. package/src/dispatch.mjs +942 -0
  59. package/src/health.mjs +253 -0
  60. package/src/index.mjs +44 -0
  61. package/src/install-hooks.mjs +100 -0
  62. package/src/playbook.mjs +257 -0
  63. package/src/profile.mjs +990 -0
  64. package/src/redact.mjs +192 -0
  65. package/src/repo.mjs +292 -0
  66. package/src/session.mjs +1036 -0
  67. package/src/tui.mjs +197 -0
  68. package/src/update-check.mjs +35 -0
@@ -0,0 +1,331 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * decompose.mjs — Task graph decomposition for the Dual-Brain Orchestrator HEAD.
4
+ *
5
+ * Splits complex prompts into a dependency-aware task graph using heuristic
6
+ * analysis. Pure data — returns a plan, does NOT spawn agents.
7
+ *
8
+ * Exports: decompose, isSimpleTask, taskGraphToWaves
9
+ */
10
+
11
+ import { detectTask, classifyIntent, extractPaths } from './detect.mjs';
12
+ import { loadPlaybook } from './playbook.mjs';
13
+
14
+ // ─── Role inference ───────────────────────────────────────────────────────────
15
+
16
+ /**
17
+ * Map a tier + intent to the appropriate agent role.
18
+ * @param {string} tier
19
+ * @param {string} intent
20
+ * @returns {'researcher'|'implementer'|'reviewer'|'verifier'}
21
+ */
22
+ function inferRole(tier, intent) {
23
+ if (tier === 'search') return 'researcher';
24
+ if (tier === 'think') {
25
+ if (intent === 'review') return 'reviewer';
26
+ return 'researcher'; // architect/planner stays researcher (read-only)
27
+ }
28
+ // execute tier
29
+ if (intent === 'test') return 'verifier';
30
+ if (intent === 'review') return 'reviewer';
31
+ return 'implementer';
32
+ }
33
+
34
+ // ─── Conjunction splitter ─────────────────────────────────────────────────────
35
+
36
+ const CONJUNCTIONS = /\s+(?:and(?:\s+also)?|then|also|plus|after\s+that|additionally|as\s+well\s+as)\s+/i;
37
+
38
+ /**
39
+ * Split a prompt on natural-language conjunctions into clauses.
40
+ * @param {string} prompt
41
+ * @returns {string[]}
42
+ */
43
+ function splitClauses(prompt) {
44
+ const parts = prompt.split(CONJUNCTIONS).map(s => s.trim()).filter(Boolean);
45
+ return parts.length > 1 ? parts : [prompt];
46
+ }
47
+
48
+ // ─── File ownership inference ─────────────────────────────────────────────────
49
+
50
+ /**
51
+ * Extract file globs from a clause, falling back to intent-based heuristics.
52
+ * @param {string} clause
53
+ * @param {string} intent
54
+ * @param {string[]} contextFiles — file paths provided in decompose context
55
+ * @returns {string[]}
56
+ */
57
+ function inferOwnership(clause, intent, contextFiles = []) {
58
+ const readOnly = ['search', 'explain', 'compare', 'review'];
59
+ if (readOnly.includes(intent)) return []; // read-only roles own nothing
60
+
61
+ // Paths mentioned explicitly in the clause
62
+ const mentioned = extractPaths(clause);
63
+ if (mentioned.length > 0) return mentioned;
64
+
65
+ // Paths from context that relate to this clause (simple keyword match)
66
+ const clauseLower = clause.toLowerCase();
67
+ const related = contextFiles.filter(f => {
68
+ const base = f.split('/').pop() ?? '';
69
+ return clauseLower.includes(base.replace(/\.[^.]+$/, ''));
70
+ });
71
+ if (related.length > 0) return related;
72
+
73
+ // Structural heuristics by intent
74
+ if (intent === 'test') return ['**/*.test.*', '**/*.spec.*'];
75
+ if (intent === 'document') return ['**/*.md'];
76
+ if (intent === 'format') return ['**/*'];
77
+
78
+ return [];
79
+ }
80
+
81
+ // ─── Dependency ordering ──────────────────────────────────────────────────────
82
+
83
+ const ROLE_ORDER = { researcher: 0, reviewer: 1, implementer: 2, verifier: 3 };
84
+
85
+ /**
86
+ * Build dependency graph: earlier-role tasks must complete before later-role tasks.
87
+ * @param {{ id: string, role: string }[]} tasks
88
+ * @returns {{ [taskId: string]: string[] }} task id → list of dependent task ids
89
+ */
90
+ function buildDependencies(tasks) {
91
+ const deps = {};
92
+ for (const task of tasks) {
93
+ deps[task.id] = [];
94
+ }
95
+
96
+ // Each task depends on all tasks with a strictly lower role rank
97
+ for (let i = 0; i < tasks.length; i++) {
98
+ for (let j = 0; j < i; j++) {
99
+ if (ROLE_ORDER[tasks[j].role] < ROLE_ORDER[tasks[i].role]) {
100
+ deps[tasks[i].id].push(tasks[j].id);
101
+ }
102
+ }
103
+ }
104
+
105
+ return deps;
106
+ }
107
+
108
+ // ─── Exported: taskGraphToWaves ───────────────────────────────────────────────
109
+
110
+ /**
111
+ * Topological sort: group tasks into dependency layers (waves).
112
+ * Tasks with no unmet dependencies form the earliest wave.
113
+ * @param {{ id: string, dependsOn: string[] }[]} tasks
114
+ * @returns {string[][]} each element is an array of task IDs in that wave
115
+ */
116
+ export function taskGraphToWaves(tasks) {
117
+ const remaining = new Set(tasks.map(t => t.id));
118
+ const satisfied = new Set();
119
+ const waves = [];
120
+
121
+ while (remaining.size > 0) {
122
+ const wave = [];
123
+ for (const task of tasks) {
124
+ if (!remaining.has(task.id)) continue;
125
+ const allDepsSatisfied = task.dependsOn.every(dep => satisfied.has(dep));
126
+ if (allDepsSatisfied) wave.push(task.id);
127
+ }
128
+
129
+ if (wave.length === 0) {
130
+ // Cycle or unresolvable — dump remaining tasks into one last wave
131
+ waves.push([...remaining]);
132
+ break;
133
+ }
134
+
135
+ waves.push(wave);
136
+ for (const id of wave) {
137
+ remaining.delete(id);
138
+ satisfied.add(id);
139
+ }
140
+ }
141
+
142
+ return waves;
143
+ }
144
+
145
+ // ─── Exported: isSimpleTask ───────────────────────────────────────────────────
146
+
147
+ /**
148
+ * Returns true if the detection indicates a trivial or simple task that does
149
+ * not need decomposition.
150
+ * @param {{ complexity: string }} detection
151
+ * @returns {boolean}
152
+ */
153
+ export function isSimpleTask(detection) {
154
+ return detection.complexity === 'trivial' || detection.complexity === 'simple';
155
+ }
156
+
157
+ // ─── Playbook → task graph ────────────────────────────────────────────────────
158
+
159
+ /**
160
+ * Convert a loaded playbook's steps into a task graph.
161
+ * @param {object} playbook
162
+ * @param {string[]} contextFiles
163
+ * @returns {{ id, title, goal, tier, role, owns, dependsOn, consensus, risk }[]}
164
+ */
165
+ function playbookToTasks(playbook, contextFiles = []) {
166
+ const steps = playbook.steps ?? [];
167
+ const tasks = steps.map(step => {
168
+ const tier = step.tier ?? 'execute';
169
+ const intent = tier === 'think' ? 'architecture' : tier === 'search' ? 'search' : 'edit';
170
+ const role = inferRole(tier, intent);
171
+ return {
172
+ id: step.id,
173
+ title: step.title,
174
+ goal: step.goal,
175
+ tier,
176
+ role,
177
+ owns: inferOwnership(step.goal, intent, contextFiles),
178
+ dependsOn: [], // will be filled below
179
+ consensus: step.consensus === true,
180
+ risk: 'medium',
181
+ };
182
+ });
183
+
184
+ // Each task depends on the immediately prior task (sequential playbook flow)
185
+ for (let i = 1; i < tasks.length; i++) {
186
+ tasks[i].dependsOn = [tasks[i - 1].id];
187
+ }
188
+
189
+ return tasks;
190
+ }
191
+
192
+ // ─── Heuristic task graph from clause splitting ───────────────────────────────
193
+
194
+ /**
195
+ * Build a task graph from clause-split sub-prompts via independent detection.
196
+ * @param {string[]} clauses
197
+ * @param {string[]} files
198
+ * @returns {{ id, title, goal, tier, role, owns, dependsOn, consensus, risk }[]}
199
+ */
200
+ function clausesToTasks(clauses, files = []) {
201
+ const raw = clauses.map((clause, idx) => {
202
+ const detection = detectTask({ prompt: clause, files });
203
+ const { intent, tier, risk } = detection;
204
+ const role = inferRole(tier, intent);
205
+ return {
206
+ id: `task-${idx + 1}`,
207
+ title: clause.length > 60 ? clause.slice(0, 57) + '...' : clause,
208
+ goal: clause,
209
+ tier,
210
+ role,
211
+ owns: inferOwnership(clause, intent, files),
212
+ dependsOn: [], // placeholder
213
+ consensus: risk === 'critical' || (risk === 'high' && tier === 'think'),
214
+ risk,
215
+ };
216
+ });
217
+
218
+ // Build dependency graph and fill in dependsOn
219
+ const deps = buildDependencies(raw);
220
+ for (const task of raw) {
221
+ task.dependsOn = deps[task.id];
222
+ }
223
+
224
+ return raw;
225
+ }
226
+
227
+ // ─── Exported: decompose ──────────────────────────────────────────────────────
228
+
229
+ /**
230
+ * Decompose a prompt into a dependency-aware task graph.
231
+ * Pure data — returns a plan, does NOT spawn agents.
232
+ *
233
+ * @param {string} prompt
234
+ * @param {{ files?: string[], cwd?: string, repo?: object, profile?: object }} [context]
235
+ * @returns {{
236
+ * tasks: object[],
237
+ * waves: string[][],
238
+ * confidence: 'high'|'medium'|'low',
239
+ * needsResearch: boolean,
240
+ * parallelizable: boolean,
241
+ * }}
242
+ */
243
+ export function decompose(prompt, context = {}) {
244
+ const { files = [], cwd } = context;
245
+
246
+ // 1. Classify the full prompt
247
+ const detection = detectTask({ prompt, files });
248
+
249
+ // 2. Trivial/simple → single-task graph
250
+ if (isSimpleTask(detection)) {
251
+ const { intent, tier, risk } = detection;
252
+ const role = inferRole(tier, intent);
253
+ const task = {
254
+ id: 'task-1',
255
+ title: prompt.length > 60 ? prompt.slice(0, 57) + '...' : prompt,
256
+ goal: prompt,
257
+ tier,
258
+ role,
259
+ owns: inferOwnership(prompt, intent, files),
260
+ dependsOn: [],
261
+ consensus: false,
262
+ risk,
263
+ };
264
+ return {
265
+ tasks: [task],
266
+ waves: [['task-1']],
267
+ confidence: 'high',
268
+ needsResearch: false,
269
+ parallelizable: false,
270
+ };
271
+ }
272
+
273
+ // 3. Try to match a playbook
274
+ const playbook = loadPlaybook(detection.intent, cwd);
275
+ if (playbook) {
276
+ const tasks = playbookToTasks(playbook, files);
277
+ const waves = taskGraphToWaves(tasks);
278
+ return {
279
+ tasks,
280
+ waves,
281
+ confidence: 'high',
282
+ needsResearch: false,
283
+ parallelizable: waves.some(w => w.length > 1),
284
+ };
285
+ }
286
+
287
+ // 4. Heuristic clause splitting
288
+ const clauses = splitClauses(prompt);
289
+ const hasResearch = files.length === 0 && detection.complexity !== 'trivial';
290
+
291
+ // If splitting didn't help (single clause), add a research task prefix for complex tasks
292
+ let allClauses = clauses;
293
+ if (clauses.length === 1 && hasResearch) {
294
+ allClauses = [`Research: find all files and context relevant to: ${prompt}`, ...clauses];
295
+ }
296
+
297
+ const tasks = clausesToTasks(allClauses, files);
298
+ const waves = taskGraphToWaves(tasks);
299
+
300
+ // Confidence: medium if we split cleanly (>1 clause), low if ambiguous
301
+ const confidence = clauses.length > 1 ? 'medium' : 'low';
302
+
303
+ return {
304
+ tasks,
305
+ waves,
306
+ confidence,
307
+ needsResearch: hasResearch,
308
+ parallelizable: waves.some(w => w.length > 1),
309
+ };
310
+ }
311
+
312
+ // ─── CLI ──────────────────────────────────────────────────────────────────────
313
+
314
+ if (process.argv[1] && new URL(import.meta.url).pathname === process.argv[1]) {
315
+ const args = process.argv.slice(2);
316
+ const prompt = args.find(a => !a.startsWith('--')) || '';
317
+
318
+ if (!prompt) {
319
+ console.error('Usage: node src/decompose.mjs "task description" [--files a,b]');
320
+ process.exit(1);
321
+ }
322
+
323
+ const filesFlag = args.find(a => a.startsWith('--files=')) ||
324
+ (args.includes('--files') ? args[args.indexOf('--files') + 1] : null);
325
+ const files = filesFlag
326
+ ? filesFlag.replace(/^--files=/, '').split(',').map(f => f.trim()).filter(Boolean)
327
+ : [];
328
+
329
+ const result = decompose(prompt, { files });
330
+ console.log(JSON.stringify(result, null, 2));
331
+ }
package/src/detect.mjs ADDED
@@ -0,0 +1,345 @@
1
+ #!/usr/bin/env node
2
+ // detect.mjs — Task detection for dual-brain. Self-contained, no internal imports.
3
+ // Exports: detectTask, classifyIntent, classifyRisk, estimateComplexity, inferTier, extractPaths, classifySpecialist
4
+
5
+ import { readFileSync } from 'fs';
6
+ import { resolve, dirname } from 'path';
7
+ import { fileURLToPath } from 'url';
8
+
9
+ const __dirname = dirname(fileURLToPath(import.meta.url));
10
+
11
+ // ─── Intent definitions ────────────────────────────────────────────────────────
12
+
13
+ const INTENTS = {
14
+ search: /\b(grep|find|locate|where is|where are|list|explore|read|look up|look for|check|what is|show me|display)\b/i,
15
+ explain: /\b(explain|walk me through|what does|how does|describe|summarize|understand|clarify)\b/i,
16
+ compare: /\b(compare|contrast|difference|versus|vs\.?|trade.?off|which is better|pros and cons|benchmark|performance)\b/i,
17
+ document: /\b(document|docs?|readme|jsdoc|typedoc|api docs|write docs|add docs|update docs)\b/i,
18
+ format: /\b(format|lint|prettier|style|indent|whitespace|typo|typos|comment[s]?|reformat)\b/i,
19
+ planning: /\b(plan|roadmap|strategy|prioritize|break down|decompose|prioritise)\b/i,
20
+ architecture: /\b(design|architect|architecture|propose|how should we|system design|system architecture)\b/i,
21
+ security: /\b(auth(?:enticat\w*)?|credential|secret|token|password|encrypt|permission[s]?|vulnerability|vulnerabilities|CVE|oauth|jwt|api.?key)\b/i,
22
+ review: /\b(review|audit|check for issues|evaluate|assess|inspect code|code review)\b/i,
23
+ debug: /\b(debug|investigate|why (is|does|isn't|doesn't)|trace|diagnose|figure out|broken|not working|failing|regression)\b/i,
24
+ test: /\b(test[s]?|spec[s]?|add test|fix test|test coverage|unit test|e2e|integration test|jest|vitest|mocha)\b/i,
25
+ refactor: /\b(refactor|restructure|reorganize|reorganise|extract|split|consolidate|clean up|cleanup|dedupe|dedup)\b/i,
26
+ edit: /\b(fix|add|update|modify|change|rename|move|replace|write|implement|create|remove|delete|insert)\b/i,
27
+ };
28
+
29
+ const INTENT_PRIORITY = [
30
+ 'security', 'architecture', 'planning', 'compare', 'review',
31
+ 'debug', 'refactor', 'test', 'explain', 'document', 'format', 'search', 'edit',
32
+ ];
33
+
34
+ // ─── Risk patterns (file path based) ──────────────────────────────────────────
35
+
36
+ const RISK_PATTERNS = [
37
+ { level: 'critical', regex: /\b(auth|credential|secret|\.env|key[s]?|token[s]?|password|encrypt|certificate|cert[s]?|\.pem|\.key)\b/i, label: 'security-sensitive' },
38
+ { level: 'high', regex: /\b(billing|payment|migration|deploy|ci[-/]cd|\.github\/workflows|security|permission|policy|schema\.prisma|schema\.sql|api[-_]?contract|openapi|swagger)\b/i, label: 'high-impact infrastructure' },
39
+ { level: 'medium', regex: /\b(test|spec|\.test\.|\.spec\.|shared|util[s]?|lib\/|public[-_]?api|integrat|config|\.config\.)\b/i, label: 'shared/tested code' },
40
+ { level: 'low', regex: /\b(readme|\.md$|docs?\/|comment|format|lint|\.prettierrc|local[-_]?script|internal[-_]?only|changelog)\b/i, label: 'docs/formatting' },
41
+ ];
42
+
43
+ // ─── Description-level risk keywords ──────────────────────────────────────────
44
+
45
+ const RISK_KEYWORDS = [
46
+ { level: 'critical', regex: /\b(auth|secret|credential|token|password|encrypt|certificate|oauth|jwt|api.?key|vulnerability|CVE)\b/i },
47
+ { level: 'high', regex: /\b(billing|payment|migration|deploy|ci.?cd|security|permission|policy|schema|openapi|swagger|production|prod)\b/i },
48
+ { level: 'medium', regex: /\b(test|spec|config|shared|util|lib|integration|public.?api)\b/i },
49
+ { level: 'low', regex: /\b(readme|docs?|comment|format|lint|changelog|typo|whitespace)\b/i },
50
+ ];
51
+
52
+ const DESIGN_IMPACT_PATTERNS = [
53
+ /\bbin\/dual-brain\.mjs\b/,
54
+ /\bsrc\/(?:tui|profile|detect|decide|dispatch|session|health|index)\.mjs\b/,
55
+ /\bhooks\/(?:head-guard|enforce-tier|budget-balancer|dual-brain-think|dual-brain-review|wave-orchestrator)\.mjs\b/,
56
+ /\bVISION\.md\b/,
57
+ ];
58
+
59
+ const LEVEL_ORDER = { critical: 3, high: 2, medium: 1, low: 0 };
60
+
61
+ // ─── Helpers / Exported functions ─────────────────────────────────────────────
62
+
63
+ function higherRisk(a, b) { return LEVEL_ORDER[a] >= LEVEL_ORDER[b] ? a : b; }
64
+
65
+ /** Extract file paths from free-form text. */
66
+ function extractPaths(text) {
67
+ if (!text) return [];
68
+ const matches = text.match(/(?:^|\s|["'`])([./~]?(?:[\w@.-]+\/)+[\w@.*-]+(?:\.\w+)?)/g);
69
+ if (!matches) return [];
70
+ return matches.map(m => m.trim().replace(/^["'`]/, ''));
71
+ }
72
+
73
+ /** Classify risk from an array of file paths. Returns { level, riskyFiles }. */
74
+ function classifyRisk(paths) {
75
+ if (!paths || paths.length === 0) {
76
+ return { level: 'low', riskyFiles: [] };
77
+ }
78
+
79
+ let highestLevel = 'low';
80
+ const riskyFiles = [];
81
+
82
+ for (const p of paths) {
83
+ for (const pattern of RISK_PATTERNS) {
84
+ if (pattern.regex.test(p)) {
85
+ if (LEVEL_ORDER[pattern.level] > LEVEL_ORDER['low']) {
86
+ riskyFiles.push({ path: p, risk: pattern.level, reason: pattern.label });
87
+ }
88
+ if (LEVEL_ORDER[pattern.level] > LEVEL_ORDER[highestLevel]) {
89
+ highestLevel = pattern.level;
90
+ if (highestLevel === 'critical') break;
91
+ }
92
+ break; // use highest-priority match for this path
93
+ }
94
+ }
95
+ if (highestLevel === 'critical') break;
96
+ }
97
+
98
+ return { level: highestLevel, riskyFiles };
99
+ }
100
+
101
+ /** Extract the dominant intent from a task description. */
102
+ function classifyIntent(prompt) {
103
+ for (const key of INTENT_PRIORITY) {
104
+ if (INTENTS[key].test(prompt)) return key;
105
+ }
106
+ return 'edit';
107
+ }
108
+
109
+ /** Determine complexity from description, file count, risk, intent, prior failures. */
110
+ function estimateComplexity({ prompt, fileCount = 0, risk = 'low', intent = 'edit', priorFailures = 0 }) {
111
+ const isAmbiguous = prompt.length > 120 || /\b(and also|as well as|plus|additionally|also)\b/i.test(prompt);
112
+
113
+ if (priorFailures >= 2 || intent === 'architecture' || risk === 'critical' || fileCount >= 6) {
114
+ return 'complex';
115
+ }
116
+ if (fileCount >= 3 || intent === 'refactor' || intent === 'debug' || risk === 'high' || isAmbiguous) {
117
+ return 'moderate';
118
+ }
119
+ if (fileCount <= 2 && (risk === 'low' || risk === 'medium')) {
120
+ if (intent === 'format' || (fileCount <= 1 && risk === 'low')) return 'trivial';
121
+ return 'simple';
122
+ }
123
+ return 'moderate';
124
+ }
125
+
126
+ /** Map intent + risk + complexity → tier (think / search / execute). */
127
+ function inferTier({ intent, risk, complexity, effort, specialistTierBias }) {
128
+ const thinkIntents = ['architecture', 'security', 'planning', 'compare', 'review'];
129
+ if (thinkIntents.includes(intent) || risk === 'critical') return 'think';
130
+
131
+ // Specialist tier_bias can elevate to think before general tier logic
132
+ if (specialistTierBias === 'think') return 'think';
133
+
134
+ const searchIntents = ['search', 'explain', 'format'];
135
+ if (searchIntents.includes(intent) && effort === 'low') return 'search';
136
+
137
+ return 'execute';
138
+ }
139
+
140
+ /** Whether this task likely requires writing/editing files. */
141
+ function requiresWrite(intent) {
142
+ const readOnly = ['search', 'explain', 'compare', 'review'];
143
+ return !readOnly.includes(intent);
144
+ }
145
+
146
+ /** Build a one-sentence explanation of the classification. */
147
+ function buildExplanation({ intent, risk, complexity, fileCount, priorFailures }) {
148
+ const parts = [];
149
+
150
+ const complexityWord = { trivial: 'Trivial', simple: 'Simple', moderate: 'Moderate', complex: 'Complex' }[complexity];
151
+ const riskWord = risk === 'low' ? 'low-risk' : `${risk}-risk`;
152
+ parts.push(`${complexityWord} ${riskWord} ${intent}`);
153
+
154
+ if (fileCount > 0) parts.push(`touching ${fileCount} file${fileCount === 1 ? '' : 's'}`);
155
+ if (priorFailures > 0) parts.push(`with ${priorFailures} prior failure${priorFailures === 1 ? '' : 's'} — elevated effort`);
156
+
157
+ return parts.join(' ') + '.';
158
+ }
159
+
160
+ /** Main detection function. Input: { prompt, files?, priorFailures? } */
161
+ function detectTask(input) {
162
+ const { prompt = '', files = [], priorFailures = 0 } = input;
163
+
164
+ // 1. Intent
165
+ const intent = classifyIntent(prompt);
166
+
167
+ // 2. Paths and risk
168
+ const extractedPaths = extractPaths(prompt);
169
+ const allPaths = [...files, ...extractedPaths];
170
+ const { level: pathRiskLevel, riskyFiles } = classifyRisk(allPaths);
171
+ const designImpact = allPaths.some(p => DESIGN_IMPACT_PATTERNS.some(re => re.test(p)));
172
+
173
+ // 3. Keyword risk from description
174
+ let keywordRisk = 'low';
175
+ for (const { level, regex } of RISK_KEYWORDS) {
176
+ if (regex.test(prompt)) { keywordRisk = level; break; }
177
+ }
178
+
179
+ const risk = higherRisk(pathRiskLevel, keywordRisk);
180
+ const fileCount = files.length;
181
+
182
+ // 4. Complexity
183
+ const complexity = estimateComplexity({ prompt, fileCount, risk, intent, priorFailures });
184
+
185
+ // 5. Effort
186
+ const effortOrder = ['low', 'medium', 'high', 'xhigh'];
187
+ function bumpEffort(e, n = 1) {
188
+ return effortOrder[Math.min(effortOrder.indexOf(e) + n, effortOrder.length - 1)];
189
+ }
190
+
191
+ let effort = { trivial: 'low', simple: 'medium', moderate: 'high', complex: 'high' }[complexity];
192
+ if (risk === 'critical' && LEVEL_ORDER[effort] < LEVEL_ORDER['high']) effort = 'high';
193
+ if (priorFailures >= 2) {
194
+ effort = 'xhigh';
195
+ } else if (priorFailures === 1) {
196
+ effort = bumpEffort(effort, 1);
197
+ }
198
+ if (intent === 'format' || intent === 'search') {
199
+ if (LEVEL_ORDER[effort] > LEVEL_ORDER['medium']) effort = 'medium';
200
+ }
201
+ if ((intent === 'architecture' || intent === 'security') && LEVEL_ORDER[effort] < LEVEL_ORDER['high']) {
202
+ effort = 'high';
203
+ }
204
+
205
+ // 6. Specialist
206
+ const specialistResult = classifySpecialist(prompt, files);
207
+ const specialistDef = SPECIALIST_DEFS[specialistResult.specialist] || null;
208
+ const specialistTierBias = specialistDef?.tier_bias || null;
209
+
210
+ // 7. Tier
211
+ const tier = inferTier({ intent, risk, complexity, effort, specialistTierBias });
212
+
213
+ // 8. Explanation
214
+ const explanation = buildExplanation({ intent, risk, complexity, fileCount, priorFailures });
215
+
216
+ return {
217
+ intent,
218
+ risk,
219
+ complexity,
220
+ effort,
221
+ tier,
222
+ fileCount,
223
+ riskyFiles,
224
+ designImpact,
225
+ requiresWrite: requiresWrite(intent),
226
+ explanation,
227
+ specialist: specialistResult,
228
+ };
229
+ }
230
+
231
+ // ─── Specialist registry ──────────────────────────────────────────────────────
232
+
233
+ const SPECIALIST_REGISTRY_PATH = resolve(__dirname, '../agents/specialists/registry.json');
234
+
235
+ const DEFAULT_SPECIALISTS = {
236
+ python: { triggers: { extensions: ['.py', '.pyx', '.pyi'], keywords: ['python', 'pip', 'pytest', 'django', 'flask', 'asyncio'] } },
237
+ typescript: { triggers: { extensions: ['.ts', '.tsx', '.mts'], keywords: ['typescript', 'tsc', 'generics', 'react', 'next', 'node'] } },
238
+ html: { triggers: { extensions: ['.html', '.css', '.scss', '.svg'], keywords: ['html', 'css', 'accessibility', 'a11y', 'aria', 'responsive', 'tailwind'] } },
239
+ linux: { triggers: { extensions: ['.sh', '.bash', '.conf', '.service', '.dockerfile'], keywords: ['linux', 'bash', 'shell', 'systemd', 'nginx', 'docker', 'ssh', 'deploy'] } },
240
+ security: { triggers: { extensions: [], keywords: ['auth', 'oauth', 'jwt', 'credential', 'secret', 'encrypt', 'vulnerability', 'vulnerabilities', 'audit', 'owasp', 'xss', 'csrf'] }, tier_bias: 'think' },
241
+ };
242
+
243
+ function loadSpecialistRegistry() {
244
+ try {
245
+ const raw = readFileSync(SPECIALIST_REGISTRY_PATH, 'utf8');
246
+ const parsed = JSON.parse(raw);
247
+ return parsed.specialists || DEFAULT_SPECIALISTS;
248
+ } catch {
249
+ return DEFAULT_SPECIALISTS;
250
+ }
251
+ }
252
+
253
+ const SPECIALIST_DEFS = loadSpecialistRegistry();
254
+
255
+ /**
256
+ * Classify which specialist domain best matches the prompt and file list.
257
+ * Returns { specialist, confidence, triggers }.
258
+ */
259
+ function classifySpecialist(prompt = '', files = []) {
260
+ const promptLower = prompt.toLowerCase();
261
+ const scores = {};
262
+ const matchedTriggers = {};
263
+
264
+ for (const [name, def] of Object.entries(SPECIALIST_DEFS)) {
265
+ const { extensions = [], keywords = [] } = def.triggers || {};
266
+ let score = 0;
267
+ const hits = [];
268
+
269
+ // +2 per matching file extension
270
+ for (const file of files) {
271
+ for (const ext of extensions) {
272
+ if (file.endsWith(ext)) {
273
+ score += 2;
274
+ hits.push(ext);
275
+ break; // count each file once per specialist
276
+ }
277
+ }
278
+ }
279
+
280
+ // +1 per matching keyword in prompt
281
+ for (const kw of keywords) {
282
+ // Use word-boundary-aware match where possible
283
+ const re = new RegExp(`\\b${kw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i');
284
+ if (re.test(promptLower)) {
285
+ score += 1;
286
+ hits.push(kw);
287
+ }
288
+ }
289
+
290
+ scores[name] = score;
291
+ matchedTriggers[name] = hits;
292
+ }
293
+
294
+ // Find highest score
295
+ let best = null;
296
+ let bestScore = 0;
297
+ let bestExtCount = 0;
298
+
299
+ for (const [name, score] of Object.entries(scores)) {
300
+ if (score < 2) continue;
301
+ const extCount = matchedTriggers[name].filter(t => t.startsWith('.')).length;
302
+ if (
303
+ score > bestScore ||
304
+ (score === bestScore && extCount > bestExtCount)
305
+ ) {
306
+ best = name;
307
+ bestScore = score;
308
+ bestExtCount = extCount;
309
+ }
310
+ }
311
+
312
+ if (!best) {
313
+ return { specialist: 'generic', confidence: 'low', triggers: [] };
314
+ }
315
+
316
+ const confidence = bestScore >= 4 ? 'high' : 'medium';
317
+ return { specialist: best, confidence, triggers: matchedTriggers[best] };
318
+ }
319
+
320
+ // ─── CLI ──────────────────────────────────────────────────────────────────────
321
+
322
+ if (process.argv[1] && new URL(import.meta.url).pathname === process.argv[1]) {
323
+ const args = process.argv.slice(2);
324
+ const prompt = args.find(a => !a.startsWith('--')) || '';
325
+
326
+ if (!prompt) {
327
+ console.error('Usage: node src/detect.mjs "task description" [--files a,b]');
328
+ process.exit(1);
329
+ }
330
+
331
+ const filesFlag = args.find(a => a.startsWith('--files=')) ||
332
+ (args.includes('--files') ? args[args.indexOf('--files') + 1] : null);
333
+ const files = filesFlag
334
+ ? filesFlag.replace(/^--files=/, '').split(',').map(f => f.trim()).filter(Boolean)
335
+ : [];
336
+
337
+ const failuresFlag = args.find(a => a.startsWith('--failures=')) ||
338
+ (args.includes('--failures') ? args[args.indexOf('--failures') + 1] : null);
339
+ const priorFailures = failuresFlag ? parseInt(failuresFlag.replace(/^--failures=/, ''), 10) : 0;
340
+
341
+ const result = detectTask({ prompt, files, priorFailures });
342
+ console.log(JSON.stringify(result, null, 2));
343
+ }
344
+
345
+ export { detectTask, classifyIntent, classifyRisk, estimateComplexity, inferTier, extractPaths, classifySpecialist };