dual-brain 7.1.21 → 7.1.22
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/bin/dual-brain.mjs +2580 -717
- package/hooks/budget-balancer.mjs +104 -266
- package/hooks/wave-orchestrator.mjs +29 -26
- package/package.json +13 -3
- package/scripts/verify-publish.mjs +26 -0
- package/src/context.mjs +389 -0
- package/src/decide.mjs +283 -60
- package/src/detect.mjs +133 -1
- package/src/dispatch.mjs +175 -30
- package/src/doctor.mjs +577 -0
- package/src/failure-memory.mjs +178 -0
- package/src/nextstep.mjs +100 -0
- package/src/observer.mjs +241 -0
- package/src/outcome.mjs +256 -0
- package/src/pipeline.mjs +759 -0
- package/src/profile.mjs +357 -485
- package/src/receipt.mjs +131 -0
- package/src/session.mjs +358 -10
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* failure-memory.mjs — Track task failures and enable automatic escalation.
|
|
4
|
+
*
|
|
5
|
+
* Exports: recordFailure, checkFailureHistory, formatEscalation,
|
|
6
|
+
* clearFailures, getFailureStats
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { readFileSync, appendFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
10
|
+
import { join } from 'path';
|
|
11
|
+
import { randomUUID } from 'crypto';
|
|
12
|
+
|
|
13
|
+
const STOP_WORDS = new Set(['the','a','an','is','in','on','at','to','for','of','and','or','with','this','that','it','be','was','are','were','has','have','had','do','does','did','not','from','by','as','if','but','we','i','you']);
|
|
14
|
+
const WINDOW_48H = 48 * 60 * 60 * 1000;
|
|
15
|
+
|
|
16
|
+
const DEPTH_ORDER = ['low', 'medium', 'high', 'ultra'];
|
|
17
|
+
const MODEL_ORDER = ['haiku', 'sonnet', 'opus'];
|
|
18
|
+
|
|
19
|
+
function failuresPath(cwd) {
|
|
20
|
+
const dir = join(cwd, '.dualbrain');
|
|
21
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
22
|
+
return join(dir, 'failures.jsonl');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function categorizeError(error = '') {
|
|
26
|
+
const e = error.toLowerCase();
|
|
27
|
+
if (/test|assert|expect/.test(e)) return 'test-failure';
|
|
28
|
+
if (/timeout|timed out/.test(e)) return 'timeout';
|
|
29
|
+
if (/syntax|parse|unexpected token/.test(e)) return 'syntax-error';
|
|
30
|
+
if (/permission|eacces/.test(e)) return 'permission-error';
|
|
31
|
+
if (/not found|enoent/.test(e)) return 'not-found';
|
|
32
|
+
return 'unknown';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function tokenize(text = '') {
|
|
36
|
+
return text.toLowerCase().split(/\W+/).filter(w => w.length > 2 && !STOP_WORDS.has(w));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function similarity(promptA, promptB, filesA = [], filesB = []) {
|
|
40
|
+
const wordsA = new Set(tokenize(promptA));
|
|
41
|
+
const wordsB = new Set(tokenize(promptB));
|
|
42
|
+
if (!wordsA.size && !wordsB.size) return 0;
|
|
43
|
+
const shared = [...wordsA].filter(w => wordsB.has(w)).length;
|
|
44
|
+
const wordScore = shared / Math.max(wordsA.size, wordsB.size);
|
|
45
|
+
const sharedFiles = filesA.some(f => filesB.includes(f));
|
|
46
|
+
return sharedFiles ? Math.max(wordScore, 0.5) : wordScore;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function readFailures(cwd) {
|
|
50
|
+
const path = failuresPath(cwd);
|
|
51
|
+
if (!existsSync(path)) return [];
|
|
52
|
+
return readFileSync(path, 'utf8')
|
|
53
|
+
.split('\n')
|
|
54
|
+
.filter(Boolean)
|
|
55
|
+
.map(line => { try { return JSON.parse(line); } catch { return null; } })
|
|
56
|
+
.filter(Boolean);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function writeAll(cwd, records) {
|
|
60
|
+
writeFileSync(failuresPath(cwd), records.map(r => JSON.stringify(r)).join('\n') + '\n');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function bumpDepth(depth) {
|
|
64
|
+
const idx = DEPTH_ORDER.indexOf(depth);
|
|
65
|
+
return idx === -1 || idx >= DEPTH_ORDER.length - 1 ? 'ultra' : DEPTH_ORDER[idx + 1];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function bumpModel(model = '') {
|
|
69
|
+
const m = model.toLowerCase();
|
|
70
|
+
const match = MODEL_ORDER.find(k => m.includes(k)) ?? 'sonnet';
|
|
71
|
+
const idx = MODEL_ORDER.indexOf(match);
|
|
72
|
+
return idx >= MODEL_ORDER.length - 1 ? `claude-opus-4-5` : `claude-${MODEL_ORDER[idx + 1]}-4-5`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ─── Exports ──────────────────────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
export async function recordFailure(prompt, plan = {}, error = '', cwd = process.cwd()) {
|
|
78
|
+
const record = {
|
|
79
|
+
id: randomUUID(),
|
|
80
|
+
timestamp: Date.now(),
|
|
81
|
+
prompt,
|
|
82
|
+
promptWords: tokenize(prompt),
|
|
83
|
+
model: plan.model ?? null,
|
|
84
|
+
reasoningDepth: plan.reasoningDepth ?? null,
|
|
85
|
+
tier: plan.tier ?? null,
|
|
86
|
+
error: String(error),
|
|
87
|
+
errorCategory: categorizeError(error),
|
|
88
|
+
files: plan.files ?? [],
|
|
89
|
+
escalatedFrom: plan.escalatedFrom ?? null,
|
|
90
|
+
resolved: false,
|
|
91
|
+
};
|
|
92
|
+
appendFileSync(failuresPath(cwd), JSON.stringify(record) + '\n');
|
|
93
|
+
return record;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function checkFailureHistory(prompt, files = [], cwd = process.cwd()) {
|
|
97
|
+
const cutoff = Date.now() - WINDOW_48H;
|
|
98
|
+
const all = readFailures(cwd);
|
|
99
|
+
const recent = all.filter(r => !r.resolved && r.timestamp >= cutoff);
|
|
100
|
+
const matches = recent
|
|
101
|
+
.map(r => ({ r, score: similarity(prompt, r.prompt, files, r.files ?? []) }))
|
|
102
|
+
.filter(({ score }) => score >= 0.4)
|
|
103
|
+
.sort((a, b) => b.r.timestamp - a.r.timestamp);
|
|
104
|
+
|
|
105
|
+
const count = matches.length;
|
|
106
|
+
const last = matches[0]?.r ?? null;
|
|
107
|
+
|
|
108
|
+
const escalation = { recommended: false, fromModel: null, toModel: null, fromDepth: null, toDepth: null, useChallenger: false, reason: '' };
|
|
109
|
+
|
|
110
|
+
if (count >= 1) {
|
|
111
|
+
escalation.recommended = true;
|
|
112
|
+
escalation.fromModel = last.model;
|
|
113
|
+
escalation.fromDepth = last.reasoningDepth;
|
|
114
|
+
|
|
115
|
+
if (count === 1) {
|
|
116
|
+
escalation.toDepth = bumpDepth(last.reasoningDepth ?? 'medium');
|
|
117
|
+
escalation.toModel = last.model;
|
|
118
|
+
escalation.useChallenger = false;
|
|
119
|
+
escalation.reason = `1 prior failure on similar task, bumping depth to ${escalation.toDepth}`;
|
|
120
|
+
} else if (count === 2) {
|
|
121
|
+
escalation.toDepth = 'ultra';
|
|
122
|
+
escalation.toModel = last.model?.includes('opus') ? last.model : bumpModel(last.model);
|
|
123
|
+
escalation.useChallenger = false;
|
|
124
|
+
escalation.reason = `2 prior failures on similar task, escalating to Opus + ultrathink`;
|
|
125
|
+
} else {
|
|
126
|
+
escalation.toDepth = 'ultra';
|
|
127
|
+
escalation.toModel = last.model?.includes('opus') ? last.model : bumpModel(last.model);
|
|
128
|
+
escalation.useChallenger = true;
|
|
129
|
+
escalation.reason = `${count} prior failures on similar task, forcing dual-brain`;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { hasPriorFailures: count > 0, failureCount: count, lastFailure: last, escalation };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function formatEscalation(escalation) {
|
|
137
|
+
if (!escalation?.recommended) return '';
|
|
138
|
+
const prev = [escalation.fromModel, escalation.fromDepth].filter(Boolean).join(', ') || 'unknown';
|
|
139
|
+
const next = [escalation.toModel, escalation.toDepth, escalation.useChallenger ? 'GPT challenger' : null].filter(Boolean).join(' + ');
|
|
140
|
+
return `⚡ Strategy changed\n Previous: failed (${prev})\n Escalated: ${next}\n Reason: ${escalation.reason}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function clearFailures(prompt, cwd = process.cwd()) {
|
|
144
|
+
const all = readFailures(cwd);
|
|
145
|
+
const promptWords = tokenize(prompt);
|
|
146
|
+
const fakePrompt = promptWords.join(' ');
|
|
147
|
+
let changed = false;
|
|
148
|
+
const updated = all.map(r => {
|
|
149
|
+
if (!r.resolved && similarity(fakePrompt, r.prompt) >= 0.4) {
|
|
150
|
+
changed = true;
|
|
151
|
+
return { ...r, resolved: true };
|
|
152
|
+
}
|
|
153
|
+
return r;
|
|
154
|
+
});
|
|
155
|
+
if (changed) writeAll(cwd, updated);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function getFailureStats(cwd = process.cwd()) {
|
|
159
|
+
const all = readFailures(cwd);
|
|
160
|
+
const byCategory = {};
|
|
161
|
+
let resolved = 0;
|
|
162
|
+
let escalationSum = 0;
|
|
163
|
+
let escalationCount = 0;
|
|
164
|
+
|
|
165
|
+
for (const r of all) {
|
|
166
|
+
if (r.resolved) resolved++;
|
|
167
|
+
byCategory[r.errorCategory] = (byCategory[r.errorCategory] ?? 0) + 1;
|
|
168
|
+
if (r.escalatedFrom) { escalationSum++; escalationCount++; }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
total: all.length,
|
|
173
|
+
resolved,
|
|
174
|
+
unresolved: all.length - resolved,
|
|
175
|
+
byCategory,
|
|
176
|
+
avgEscalationsToResolve: escalationCount ? +(escalationSum / escalationCount).toFixed(2) : 0,
|
|
177
|
+
};
|
|
178
|
+
}
|
package/src/nextstep.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
const AUTH_PAT = /\b(auth|credential|secret|token|password|encrypt|permission|oauth|jwt|api.?key)\b/i;
|
|
6
|
+
const TEST_PAT = /\b(test|spec|\.test\.|\.spec\.)\b/i;
|
|
7
|
+
|
|
8
|
+
function gitBranch(cwd) {
|
|
9
|
+
try { return execSync('git rev-parse --abbrev-ref HEAD', { cwd, stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim(); }
|
|
10
|
+
catch { return null; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function packageVersionChanged(cwd, files) {
|
|
14
|
+
if (!files.some(f => f.includes('package.json'))) return false;
|
|
15
|
+
try { return execSync('git diff HEAD~1 HEAD -- package.json', { cwd, stdio: ['ignore', 'pipe', 'ignore'] }).toString().includes('"version"'); }
|
|
16
|
+
catch { return false; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function changelogExists(cwd) {
|
|
20
|
+
return ['CHANGELOG.md', 'CHANGELOG', 'changelog.md'].some(f => existsSync(join(cwd, f)));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function step(priority, type, message, command, reason) {
|
|
24
|
+
return { priority, type, message, command, reason };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function dedup(steps) {
|
|
28
|
+
const seen = new Set();
|
|
29
|
+
return steps.filter(s => { if (seen.has(s.type)) return false; seen.add(s.type); return true; });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function suggestNextSteps(completedTask = {}, outcome = {}, cwd = process.cwd()) {
|
|
33
|
+
try {
|
|
34
|
+
const { prompt = '', files = [], trigger } = completedTask;
|
|
35
|
+
const { success = false, filesChanged = [], error = '' } = outcome;
|
|
36
|
+
const steps = [];
|
|
37
|
+
const branch = gitBranch(cwd);
|
|
38
|
+
const onMain = !branch || branch === 'main' || branch === 'master';
|
|
39
|
+
const allFiles = [...files, ...filesChanged];
|
|
40
|
+
const hasAuth = allFiles.some(f => AUTH_PAT.test(f));
|
|
41
|
+
const hasTests = allFiles.some(f => TEST_PAT.test(f));
|
|
42
|
+
const n = filesChanged.length;
|
|
43
|
+
const fs = (count) => `${count} file${count !== 1 ? 's' : ''}`;
|
|
44
|
+
|
|
45
|
+
if (trigger === 'auto-commit') {
|
|
46
|
+
steps.push(!onMain && branch
|
|
47
|
+
? step(4, 'pr', `Open a pull request for branch "${branch}"`, `gh pr create --head ${branch}`, `On feature branch — changes need review before merging`)
|
|
48
|
+
: step(3, 'deploy', 'Deploy or tag a release', null, 'Committed to main — ready to ship or version'));
|
|
49
|
+
if (packageVersionChanged(cwd, filesChanged))
|
|
50
|
+
steps.push(step(4, 'publish', 'Publish the new package version to npm', 'npm publish', 'package.json version changed in this commit'));
|
|
51
|
+
if (changelogExists(cwd) && !filesChanged.some(f => /changelog/i.test(f)))
|
|
52
|
+
steps.push(step(2, 'changelog', 'Update CHANGELOG with this change', null, 'CHANGELOG exists but was not updated'));
|
|
53
|
+
|
|
54
|
+
} else if (trigger === 'review' || trigger === 'think') {
|
|
55
|
+
const issues = error || /issue|problem|fail|error|warn/i.test(prompt);
|
|
56
|
+
steps.push(issues
|
|
57
|
+
? step(5, 'fix', 'Fix the issues identified in the review', `dual-brain go "fix issues identified in review"`, 'Review found problems that need resolution')
|
|
58
|
+
: step(3, 'continue', 'Ship it — the review looks good', null, 'Review completed without critical findings'));
|
|
59
|
+
|
|
60
|
+
} else if (!success) {
|
|
61
|
+
steps.push(step(5, 'fix', 'Retry with higher reasoning depth', `dual-brain go --tier think "${prompt}"`, 'Task failed — escalating tier may resolve it'));
|
|
62
|
+
if (error && /test/i.test(error))
|
|
63
|
+
steps.push(step(4, 'test', 'Look at test output for clues', null, 'Error references tests — check output to understand the failure'));
|
|
64
|
+
steps.push(step(3, 'review', 'Try a different approach — dual-brain think', `node .claude/hooks/dual-brain-think.mjs --question "${prompt}"`, 'GPT perspective may surface a different solution'));
|
|
65
|
+
|
|
66
|
+
} else if (success && n > 0) {
|
|
67
|
+
if (!hasTests)
|
|
68
|
+
steps.push(step(5, 'test', `Run tests to verify the ${fs(n)} changed`, 'npm test', `${fs(n)} changed without test verification`));
|
|
69
|
+
if (hasAuth)
|
|
70
|
+
steps.push(step(5, 'review', 'Run a security review on auth/credential changes', `node .claude/hooks/dual-brain-think.mjs --question "Security review: ${prompt}"`, 'Auth or security-sensitive files were modified'));
|
|
71
|
+
if (n > 3)
|
|
72
|
+
steps.push(step(4, 'review', `Review the ${n}-file diff before committing`, 'git diff', `${n} files changed — quick diff review before committing`));
|
|
73
|
+
if (!onMain && branch)
|
|
74
|
+
steps.push(step(4, 'pr', `Open a pull request for branch "${branch}"`, `gh pr create --head ${branch}`, `Changes are on feature branch "${branch}" — ready for PR`));
|
|
75
|
+
if (hasTests)
|
|
76
|
+
steps.push(step(3, 'commit', 'Commit changes', 'git add -p && git commit', 'Tests passed — safe to commit'));
|
|
77
|
+
steps.push(step(2, 'continue', 'Check for edge cases in the changed code', null, 'Edge cases are often missed during implementation'));
|
|
78
|
+
if (changelogExists(cwd) && !filesChanged.some(f => /changelog/i.test(f)))
|
|
79
|
+
steps.push(step(2, 'changelog', 'Update CHANGELOG with this change', null, 'CHANGELOG exists but was not updated in this batch'));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const sorted = dedup(steps.sort((a, b) => b.priority - a.priority));
|
|
83
|
+
return {
|
|
84
|
+
steps: sorted,
|
|
85
|
+
topSuggestion: sorted.length > 0 ? `→ ${sorted[0].message}` : '→ Nothing urgent — task complete',
|
|
86
|
+
};
|
|
87
|
+
} catch {
|
|
88
|
+
return { steps: [], topSuggestion: '→ Task complete' };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function formatNextSteps(steps, limit = 3) {
|
|
93
|
+
if (!steps?.length) return '';
|
|
94
|
+
return `📋 Next steps\n${steps.slice(0, limit).map((s, i) => ` ${i + 1}. ${s.message}`).join('\n')}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function getTopSuggestion(steps) {
|
|
98
|
+
if (!steps?.length) return '→ Task complete';
|
|
99
|
+
return `→ ${steps[0].message}`;
|
|
100
|
+
}
|
package/src/observer.mjs
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
const SEC_PATTERNS = /auth|login|password|token|secret|credential|session|jwt|oauth|permission|role|middleware/i;
|
|
6
|
+
const SOURCE_EXT = /\.(mjs|js|ts|py)$/;
|
|
7
|
+
|
|
8
|
+
function exec(cmd, cwd, timeout = 5000) {
|
|
9
|
+
try {
|
|
10
|
+
return execSync(cmd, { cwd, encoding: 'utf8', timeout, stdio: ['pipe', 'pipe', 'pipe'] }).trim();
|
|
11
|
+
} catch {
|
|
12
|
+
return '';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function changedFiles(cwd) {
|
|
17
|
+
const output = exec('git diff --name-only HEAD 2>/dev/null || git diff --name-only', cwd);
|
|
18
|
+
return output ? output.split('\n').filter(Boolean) : [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function checkSecurity(files) {
|
|
22
|
+
const hits = files.filter(f => SEC_PATTERNS.test(f));
|
|
23
|
+
if (!hits.length) return null;
|
|
24
|
+
return {
|
|
25
|
+
type: 'security-review',
|
|
26
|
+
priority: 'high',
|
|
27
|
+
message: 'Auth-related files changed — want a security review?',
|
|
28
|
+
action: 'dual-brain review',
|
|
29
|
+
files: hits,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function checkNoTests(files, cwd) {
|
|
34
|
+
const sources = files.filter(f => SOURCE_EXT.test(f));
|
|
35
|
+
if (!sources.length) return null;
|
|
36
|
+
|
|
37
|
+
const untested = sources.filter(f => {
|
|
38
|
+
const base = f.replace(SOURCE_EXT, '');
|
|
39
|
+
const dir = join(cwd, f.split('/').slice(0, -1).join('/'));
|
|
40
|
+
const name = f.split('/').pop().replace(SOURCE_EXT, '');
|
|
41
|
+
const candidates = ['test','spec'].flatMap(k =>
|
|
42
|
+
['mjs','js','ts'].flatMap(e => [
|
|
43
|
+
join(cwd, `${base}.${k}.${e}`),
|
|
44
|
+
join(dir, '__tests__', `${name}.${e}`),
|
|
45
|
+
])
|
|
46
|
+
);
|
|
47
|
+
return !candidates.some(c => existsSync(c));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!untested.length) return null;
|
|
51
|
+
return {
|
|
52
|
+
type: 'no-tests',
|
|
53
|
+
priority: 'medium',
|
|
54
|
+
message: `${untested.length} changed file${untested.length > 1 ? 's' : ''} have no tests`,
|
|
55
|
+
action: "dual-brain go 'add tests for changed files'",
|
|
56
|
+
files: untested,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function checkLargeDiff(cwd) {
|
|
61
|
+
const stat = exec('git diff --stat', cwd);
|
|
62
|
+
if (!stat) return null;
|
|
63
|
+
const match = stat.match(/(\d+) insertion|(\d+) deletion/g);
|
|
64
|
+
if (!match) return null;
|
|
65
|
+
const total = match.reduce((sum, m) => sum + parseInt(m), 0);
|
|
66
|
+
if (total <= 500) return null;
|
|
67
|
+
return {
|
|
68
|
+
type: 'large-diff',
|
|
69
|
+
priority: 'medium',
|
|
70
|
+
message: `Large uncommitted changes (${total} lines) — consider committing`,
|
|
71
|
+
action: "dual-brain go 'commit current changes'",
|
|
72
|
+
files: [],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function checkStaleBranch(cwd, files) {
|
|
77
|
+
if (!files.length) return null;
|
|
78
|
+
const ts = exec('git log -1 --format=%ct', cwd);
|
|
79
|
+
if (!ts) return null;
|
|
80
|
+
const age = Date.now() / 1000 - parseInt(ts);
|
|
81
|
+
if (age < 86400) return null;
|
|
82
|
+
const hours = Math.round(age / 3600);
|
|
83
|
+
return {
|
|
84
|
+
type: 'stale-branch',
|
|
85
|
+
priority: 'low',
|
|
86
|
+
message: `Last commit was ${hours}h ago with uncommitted work`,
|
|
87
|
+
action: "dual-brain go 'commit current changes'",
|
|
88
|
+
files: [],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function checkConflicts(cwd) {
|
|
93
|
+
const conflicted = exec('git diff --name-only --diff-filter=U', cwd);
|
|
94
|
+
if (!conflicted) return null;
|
|
95
|
+
const files = conflicted.split('\n').filter(Boolean);
|
|
96
|
+
if (!files.length) return null;
|
|
97
|
+
return {
|
|
98
|
+
type: 'conflict',
|
|
99
|
+
priority: 'high',
|
|
100
|
+
message: `${files.length} file${files.length > 1 ? 's' : ''} have merge conflicts`,
|
|
101
|
+
action: "dual-brain go 'resolve merge conflicts'",
|
|
102
|
+
files,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function checkUnfinishedWork(cwd) {
|
|
107
|
+
const outcomesDir = join(cwd, '.dualbrain', 'outcomes');
|
|
108
|
+
if (!existsSync(outcomesDir)) return null;
|
|
109
|
+
|
|
110
|
+
const cutoff = Date.now() - 86_400_000;
|
|
111
|
+
let failed = null;
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const files = readdirSync(outcomesDir).filter(f => f.endsWith('.jsonl')).sort().reverse();
|
|
115
|
+
for (const file of files) {
|
|
116
|
+
const lines = readFileSync(join(outcomesDir, file), 'utf8')
|
|
117
|
+
.split('\n').filter(Boolean);
|
|
118
|
+
for (const line of lines.reverse()) {
|
|
119
|
+
try {
|
|
120
|
+
const rec = JSON.parse(line);
|
|
121
|
+
if (rec.timestamp && rec.timestamp < cutoff) break;
|
|
122
|
+
if (rec.result && rec.result.success === false && rec.prompt) {
|
|
123
|
+
failed = rec;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
} catch { /* skip */ }
|
|
127
|
+
}
|
|
128
|
+
if (failed) break;
|
|
129
|
+
}
|
|
130
|
+
} catch { return null; }
|
|
131
|
+
|
|
132
|
+
if (!failed) return null;
|
|
133
|
+
const prompt = failed.prompt.slice(0, 60);
|
|
134
|
+
return {
|
|
135
|
+
type: 'unfinished-work',
|
|
136
|
+
priority: 'medium',
|
|
137
|
+
message: `Last session had a failed task: '${prompt}' — resume?`,
|
|
138
|
+
action: `dual-brain go '${failed.prompt}'`,
|
|
139
|
+
files: [],
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function checkFailingTests(cwd) {
|
|
144
|
+
const pkgPath = join(cwd, 'package.json');
|
|
145
|
+
if (!existsSync(pkgPath)) return null;
|
|
146
|
+
try {
|
|
147
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
148
|
+
if (!pkg.scripts?.test) return null;
|
|
149
|
+
} catch { return null; }
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
execSync('npm test --silent 2>&1', { cwd, encoding: 'utf8', timeout: 30000, stdio: 'pipe' });
|
|
153
|
+
return null;
|
|
154
|
+
} catch {
|
|
155
|
+
return {
|
|
156
|
+
type: 'failing-tests',
|
|
157
|
+
priority: 'high',
|
|
158
|
+
message: 'Tests are failing — want me to investigate?',
|
|
159
|
+
action: "dual-brain go 'fix failing tests'",
|
|
160
|
+
files: [],
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function buildSummary(files, observations) {
|
|
166
|
+
const conflicts = observations.filter(o => o.type === 'conflict').length;
|
|
167
|
+
const hi = observations.filter(o => o.priority === 'high').length;
|
|
168
|
+
const parts = [];
|
|
169
|
+
if (files.length) parts.push(`${files.length} file${files.length > 1 ? 's' : ''} changed`);
|
|
170
|
+
else parts.push('no uncommitted changes');
|
|
171
|
+
if (conflicts) parts.push(`${conflicts} conflict${conflicts > 1 ? 's' : ''}`);
|
|
172
|
+
if (hi) parts.push(`${hi} high-priority suggestion${hi > 1 ? 's' : ''}`);
|
|
173
|
+
return parts.join(', ');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function observe(cwd, options = {}) {
|
|
177
|
+
const observations = [];
|
|
178
|
+
try {
|
|
179
|
+
const files = changedFiles(cwd);
|
|
180
|
+
|
|
181
|
+
const sec = checkSecurity(files);
|
|
182
|
+
if (sec) observations.push(sec);
|
|
183
|
+
|
|
184
|
+
const conflicts = checkConflicts(cwd);
|
|
185
|
+
if (conflicts) observations.push(conflicts);
|
|
186
|
+
|
|
187
|
+
const noTests = checkNoTests(files, cwd);
|
|
188
|
+
if (noTests) observations.push(noTests);
|
|
189
|
+
|
|
190
|
+
const largeDiff = checkLargeDiff(cwd);
|
|
191
|
+
if (largeDiff) observations.push(largeDiff);
|
|
192
|
+
|
|
193
|
+
const stale = checkStaleBranch(cwd, files);
|
|
194
|
+
if (stale) observations.push(stale);
|
|
195
|
+
|
|
196
|
+
const unfinished = checkUnfinishedWork(cwd);
|
|
197
|
+
if (unfinished) observations.push(unfinished);
|
|
198
|
+
|
|
199
|
+
if (options.runTests) {
|
|
200
|
+
const failing = await checkFailingTests(cwd);
|
|
201
|
+
if (failing) observations.push(failing);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { observations, summary: buildSummary(files, observations) };
|
|
205
|
+
} catch {
|
|
206
|
+
return { observations: [], summary: 'unable to observe repo state' };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function formatObservations(observations) {
|
|
211
|
+
if (!observations.length) return '💡 Suggestions\n (none)';
|
|
212
|
+
const icon = { high: '🔴', medium: '🟡', low: '🟢' };
|
|
213
|
+
const lines = observations.map(o => ` ${icon[o.priority] || '⚪'} ${o.message}`);
|
|
214
|
+
return `💡 Suggestions\n${lines.join('\n')}`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function getQuickState(cwd) {
|
|
218
|
+
try {
|
|
219
|
+
const files = changedFiles(cwd);
|
|
220
|
+
const observations = [];
|
|
221
|
+
|
|
222
|
+
const sec = checkSecurity(files);
|
|
223
|
+
if (sec) observations.push(sec);
|
|
224
|
+
|
|
225
|
+
const conflicts = checkConflicts(cwd);
|
|
226
|
+
if (conflicts) observations.push(conflicts);
|
|
227
|
+
|
|
228
|
+
const noTests = checkNoTests(files, cwd);
|
|
229
|
+
if (noTests) observations.push(noTests);
|
|
230
|
+
|
|
231
|
+
const largeDiff = checkLargeDiff(cwd);
|
|
232
|
+
if (largeDiff) observations.push(largeDiff);
|
|
233
|
+
|
|
234
|
+
const stale = checkStaleBranch(cwd, files);
|
|
235
|
+
if (stale) observations.push(stale);
|
|
236
|
+
|
|
237
|
+
return { observations, summary: buildSummary(files, observations) };
|
|
238
|
+
} catch {
|
|
239
|
+
return { observations: [], summary: 'unable to observe repo state' };
|
|
240
|
+
}
|
|
241
|
+
}
|