dual-brain 0.2.30 → 0.3.1

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 (312) hide show
  1. package/.dual-brain/docs/claude-code-extension-points.md +32 -0
  2. package/.dual-brain/docs/data-tools-capabilities.md +181 -0
  3. package/.dual-brain/docs/ecosystem-tools.md +91 -0
  4. package/.dual-brain/docs/panel-handoff.md +124 -0
  5. package/.dual-brain/docs/ruflo-analysis.md +48 -0
  6. package/bin/dual-brain.mjs +56 -56
  7. package/dist/mcp-server/index.d.ts +27 -0
  8. package/dist/mcp-server/index.js +359 -0
  9. package/dist/mcp-server/index.js.map +1 -0
  10. package/dist/src/agent-protocol.d.ts +163 -0
  11. package/dist/src/agent-protocol.js +368 -0
  12. package/dist/src/agent-protocol.js.map +1 -0
  13. package/dist/src/agents/registry.d.ts +52 -0
  14. package/dist/src/agents/registry.js +393 -0
  15. package/dist/src/agents/registry.js.map +1 -0
  16. package/dist/src/awareness.d.ts +93 -0
  17. package/dist/src/awareness.js +406 -0
  18. package/dist/src/awareness.js.map +1 -0
  19. package/dist/src/brief.d.ts +48 -0
  20. package/dist/src/brief.js +179 -0
  21. package/dist/src/brief.js.map +1 -0
  22. package/dist/src/calibration.d.ts +32 -0
  23. package/dist/src/calibration.js +133 -0
  24. package/dist/src/calibration.js.map +1 -0
  25. package/dist/src/checkpoint.d.ts +33 -0
  26. package/dist/src/checkpoint.js +99 -0
  27. package/dist/src/checkpoint.js.map +1 -0
  28. package/dist/src/ci-triage.d.ts +33 -0
  29. package/dist/src/ci-triage.js +193 -0
  30. package/dist/src/ci-triage.js.map +1 -0
  31. package/dist/src/cognitive-loop.d.ts +56 -0
  32. package/dist/src/cognitive-loop.js +495 -0
  33. package/dist/src/cognitive-loop.js.map +1 -0
  34. package/dist/src/collaboration.d.ts +147 -0
  35. package/dist/src/collaboration.js +438 -0
  36. package/dist/src/collaboration.js.map +1 -0
  37. package/dist/src/context-intel.d.ts +47 -0
  38. package/dist/src/context-intel.js +156 -0
  39. package/dist/src/context-intel.js.map +1 -0
  40. package/dist/src/context.d.ts +53 -0
  41. package/dist/src/context.js +332 -0
  42. package/dist/src/context.js.map +1 -0
  43. package/dist/src/continuity.d.ts +89 -0
  44. package/dist/src/continuity.js +230 -0
  45. package/dist/src/continuity.js.map +1 -0
  46. package/dist/src/cost-tracker.d.ts +47 -0
  47. package/dist/src/cost-tracker.js +170 -0
  48. package/dist/src/cost-tracker.js.map +1 -0
  49. package/dist/src/debrief.d.ts +53 -0
  50. package/dist/src/debrief.js +222 -0
  51. package/dist/src/debrief.js.map +1 -0
  52. package/dist/src/decide.d.ts +96 -0
  53. package/dist/src/decide.js +744 -0
  54. package/dist/src/decide.js.map +1 -0
  55. package/dist/src/decompose.d.ts +39 -0
  56. package/dist/src/decompose.js +218 -0
  57. package/dist/src/decompose.js.map +1 -0
  58. package/dist/src/detect.d.ts +91 -0
  59. package/dist/src/detect.js +544 -0
  60. package/dist/src/detect.js.map +1 -0
  61. package/dist/src/dispatch.d.ts +154 -0
  62. package/dist/src/dispatch.js +1306 -0
  63. package/dist/src/dispatch.js.map +1 -0
  64. package/dist/src/doctor.d.ts +421 -0
  65. package/dist/src/doctor.js +1689 -0
  66. package/dist/src/doctor.js.map +1 -0
  67. package/dist/src/engine.d.ts +70 -0
  68. package/dist/src/engine.js +155 -0
  69. package/dist/src/engine.js.map +1 -0
  70. package/dist/src/envelope.d.ts +36 -0
  71. package/dist/src/envelope.js +80 -0
  72. package/dist/src/envelope.js.map +1 -0
  73. package/dist/src/failure-memory.d.ts +55 -0
  74. package/dist/src/failure-memory.js +175 -0
  75. package/dist/src/failure-memory.js.map +1 -0
  76. package/dist/src/fx.d.ts +87 -0
  77. package/dist/src/fx.js +272 -0
  78. package/dist/src/fx.js.map +1 -0
  79. package/dist/src/governance.d.ts +93 -0
  80. package/dist/src/governance.js +261 -0
  81. package/dist/src/governance.js.map +1 -0
  82. package/dist/src/handoff.d.ts +11 -0
  83. package/dist/src/handoff.js +90 -0
  84. package/dist/src/handoff.js.map +1 -0
  85. package/dist/src/head-protocol.d.ts +76 -0
  86. package/dist/src/head-protocol.js +109 -0
  87. package/dist/src/head-protocol.js.map +1 -0
  88. package/dist/src/head.d.ts +222 -0
  89. package/dist/src/head.js +765 -0
  90. package/dist/src/head.js.map +1 -0
  91. package/dist/src/health.d.ts +132 -0
  92. package/dist/src/health.js +435 -0
  93. package/dist/src/health.js.map +1 -0
  94. package/dist/src/inbox.d.ts +70 -0
  95. package/dist/src/inbox.js +218 -0
  96. package/dist/src/inbox.js.map +1 -0
  97. package/dist/src/index.d.ts +33 -0
  98. package/dist/src/index.js +38 -0
  99. package/dist/src/index.js.map +1 -0
  100. package/dist/src/install-hooks.d.ts +13 -0
  101. package/dist/src/install-hooks.js +88 -0
  102. package/dist/src/install-hooks.js.map +1 -0
  103. package/dist/src/integrity.d.ts +59 -0
  104. package/dist/src/integrity.js +206 -0
  105. package/dist/src/integrity.js.map +1 -0
  106. package/dist/src/intelligence.d.ts +104 -0
  107. package/dist/src/intelligence.js +391 -0
  108. package/dist/src/intelligence.js.map +1 -0
  109. package/dist/src/ledger.d.ts +54 -0
  110. package/dist/src/ledger.js +179 -0
  111. package/dist/src/ledger.js.map +1 -0
  112. package/dist/src/living-docs.d.ts +14 -0
  113. package/dist/src/living-docs.js +197 -0
  114. package/dist/src/living-docs.js.map +1 -0
  115. package/dist/src/memory-tiers.d.ts +37 -0
  116. package/dist/src/memory-tiers.js +160 -0
  117. package/dist/src/memory-tiers.js.map +1 -0
  118. package/dist/src/model-profiles.d.ts +65 -0
  119. package/dist/src/model-profiles.js +568 -0
  120. package/dist/src/model-profiles.js.map +1 -0
  121. package/dist/src/models.d.ts +58 -0
  122. package/dist/src/models.js +327 -0
  123. package/dist/src/models.js.map +1 -0
  124. package/dist/src/narrative.d.ts +54 -0
  125. package/dist/src/narrative.js +163 -0
  126. package/dist/src/narrative.js.map +1 -0
  127. package/dist/src/nextstep.d.ts +16 -0
  128. package/dist/src/nextstep.js +103 -0
  129. package/dist/src/nextstep.js.map +1 -0
  130. package/dist/src/observer.d.ts +18 -0
  131. package/dist/src/observer.js +251 -0
  132. package/dist/src/observer.js.map +1 -0
  133. package/dist/src/outcome.d.ts +110 -0
  134. package/dist/src/outcome.js +377 -0
  135. package/dist/src/outcome.js.map +1 -0
  136. package/dist/src/pipeline.d.ts +167 -0
  137. package/dist/src/pipeline.js +1503 -0
  138. package/dist/src/pipeline.js.map +1 -0
  139. package/dist/src/playbook.d.ts +59 -0
  140. package/dist/src/playbook.js +238 -0
  141. package/dist/src/playbook.js.map +1 -0
  142. package/dist/src/pr-agent.d.ts +97 -0
  143. package/dist/src/pr-agent.js +195 -0
  144. package/dist/src/pr-agent.js.map +1 -0
  145. package/dist/src/predictive.d.ts +57 -0
  146. package/dist/src/predictive.js +230 -0
  147. package/dist/src/predictive.js.map +1 -0
  148. package/dist/src/profile.d.ts +294 -0
  149. package/dist/src/profile.js +1347 -0
  150. package/dist/src/profile.js.map +1 -0
  151. package/dist/src/prompt-audit.d.ts +22 -0
  152. package/dist/src/prompt-audit.js +194 -0
  153. package/dist/src/prompt-audit.js.map +1 -0
  154. package/dist/src/prompt-intel.d.ts +12 -0
  155. package/dist/src/prompt-intel.js +321 -0
  156. package/dist/src/prompt-intel.js.map +1 -0
  157. package/dist/src/provider-context.d.ts +121 -0
  158. package/dist/src/provider-context.js +222 -0
  159. package/dist/src/provider-context.js.map +1 -0
  160. package/dist/src/provider-manager.d.ts +92 -0
  161. package/dist/src/provider-manager.js +428 -0
  162. package/dist/src/provider-manager.js.map +1 -0
  163. package/dist/src/receipt.d.ts +87 -0
  164. package/dist/src/receipt.js +326 -0
  165. package/dist/src/receipt.js.map +1 -0
  166. package/dist/src/recommendations.d.ts +13 -0
  167. package/dist/src/recommendations.js +291 -0
  168. package/dist/src/recommendations.js.map +1 -0
  169. package/dist/src/redact.d.ts +15 -0
  170. package/dist/src/redact.js +129 -0
  171. package/dist/src/redact.js.map +1 -0
  172. package/dist/src/replit.d.ts +397 -0
  173. package/dist/src/replit.js +1160 -0
  174. package/dist/src/replit.js.map +1 -0
  175. package/dist/src/repo.d.ts +149 -0
  176. package/dist/src/repo.js +416 -0
  177. package/dist/src/repo.js.map +1 -0
  178. package/dist/src/revert.d.ts +30 -0
  179. package/dist/src/revert.js +166 -0
  180. package/dist/src/revert.js.map +1 -0
  181. package/dist/src/room.d.ts +102 -0
  182. package/dist/src/room.js +212 -0
  183. package/dist/src/room.js.map +1 -0
  184. package/dist/src/routing-advisor.d.ts +57 -0
  185. package/dist/src/routing-advisor.js +221 -0
  186. package/dist/src/routing-advisor.js.map +1 -0
  187. package/dist/src/self-correct.d.ts +40 -0
  188. package/dist/src/self-correct.js +137 -0
  189. package/dist/src/self-correct.js.map +1 -0
  190. package/dist/src/session-lock.d.ts +35 -0
  191. package/dist/src/session-lock.js +134 -0
  192. package/dist/src/session-lock.js.map +1 -0
  193. package/dist/src/session.d.ts +267 -0
  194. package/dist/src/session.js +1660 -0
  195. package/dist/src/session.js.map +1 -0
  196. package/dist/src/settings-tui.d.ts +5 -0
  197. package/dist/src/settings-tui.js +422 -0
  198. package/dist/src/settings-tui.js.map +1 -0
  199. package/dist/src/setup-flow.d.ts +63 -0
  200. package/dist/src/setup-flow.js +233 -0
  201. package/dist/src/setup-flow.js.map +1 -0
  202. package/dist/src/signal.d.ts +19 -0
  203. package/dist/src/signal.js +122 -0
  204. package/dist/src/signal.js.map +1 -0
  205. package/dist/src/simmer.d.ts +85 -0
  206. package/dist/src/simmer.js +224 -0
  207. package/dist/src/simmer.js.map +1 -0
  208. package/dist/src/state-export.d.ts +129 -0
  209. package/dist/src/state-export.js +233 -0
  210. package/dist/src/state-export.js.map +1 -0
  211. package/dist/src/strategy.d.ts +54 -0
  212. package/dist/src/strategy.js +95 -0
  213. package/dist/src/strategy.js.map +1 -0
  214. package/dist/src/subscription.d.ts +40 -0
  215. package/dist/src/subscription.js +189 -0
  216. package/dist/src/subscription.js.map +1 -0
  217. package/dist/src/templates.d.ts +208 -0
  218. package/dist/src/templates.js +238 -0
  219. package/dist/src/templates.js.map +1 -0
  220. package/dist/src/test.d.ts +9 -0
  221. package/dist/src/test.js +1173 -0
  222. package/dist/src/test.js.map +1 -0
  223. package/dist/src/think-engine.d.ts +67 -0
  224. package/dist/src/think-engine.js +412 -0
  225. package/dist/src/think-engine.js.map +1 -0
  226. package/dist/src/tui.d.ts +71 -0
  227. package/dist/src/tui.js +242 -0
  228. package/dist/src/tui.js.map +1 -0
  229. package/dist/src/types.d.ts +177 -0
  230. package/dist/src/types.js +6 -0
  231. package/dist/src/types.js.map +1 -0
  232. package/dist/src/update-check.d.ts +7 -0
  233. package/dist/src/update-check.js +36 -0
  234. package/dist/src/update-check.js.map +1 -0
  235. package/dist/src/wave-planner.d.ts +30 -0
  236. package/dist/src/wave-planner.js +281 -0
  237. package/dist/src/wave-planner.js.map +1 -0
  238. package/hooks/head-guard.sh +41 -0
  239. package/hooks/precompact.mjs +3 -3
  240. package/hooks/session-end.mjs +3 -3
  241. package/hooks/task-classifier.mjs +328 -0
  242. package/hooks/vibe-router.mjs +387 -0
  243. package/install.mjs +2 -2
  244. package/package.json +29 -153
  245. package/src/agents/registry.mjs +0 -405
  246. package/src/awareness.mjs +0 -425
  247. package/src/brief.mjs +0 -266
  248. package/src/calibration.mjs +0 -148
  249. package/src/checkpoint.mjs +0 -109
  250. package/src/ci-triage.mjs +0 -191
  251. package/src/cognitive-loop.mjs +0 -562
  252. package/src/collaboration.mjs +0 -545
  253. package/src/context-intel.mjs +0 -158
  254. package/src/context.mjs +0 -389
  255. package/src/continuity.mjs +0 -298
  256. package/src/cost-tracker.mjs +0 -184
  257. package/src/debrief.mjs +0 -228
  258. package/src/decide.mjs +0 -1099
  259. package/src/decompose.mjs +0 -331
  260. package/src/detect.mjs +0 -702
  261. package/src/dispatch.mjs +0 -1447
  262. package/src/doctor.mjs +0 -1607
  263. package/src/envelope.mjs +0 -139
  264. package/src/failure-memory.mjs +0 -178
  265. package/src/fx.mjs +0 -276
  266. package/src/governance.mjs +0 -279
  267. package/src/handoff.mjs +0 -87
  268. package/src/head-protocol.mjs +0 -128
  269. package/src/head.mjs +0 -952
  270. package/src/health.mjs +0 -528
  271. package/src/inbox.mjs +0 -195
  272. package/src/index.mjs +0 -44
  273. package/src/install-hooks.mjs +0 -100
  274. package/src/integrity.mjs +0 -245
  275. package/src/intelligence.mjs +0 -447
  276. package/src/ledger.mjs +0 -196
  277. package/src/living-docs.mjs +0 -210
  278. package/src/memory-tiers.mjs +0 -193
  279. package/src/models.mjs +0 -363
  280. package/src/narrative.mjs +0 -169
  281. package/src/nextstep.mjs +0 -100
  282. package/src/observer.mjs +0 -241
  283. package/src/outcome.mjs +0 -400
  284. package/src/pipeline.mjs +0 -1711
  285. package/src/playbook.mjs +0 -257
  286. package/src/pr-agent.mjs +0 -214
  287. package/src/predictive.mjs +0 -250
  288. package/src/profile.mjs +0 -1411
  289. package/src/prompt-audit.mjs +0 -231
  290. package/src/prompt-intel.mjs +0 -325
  291. package/src/provider-context.mjs +0 -257
  292. package/src/receipt.mjs +0 -344
  293. package/src/recommendations.mjs +0 -296
  294. package/src/redact.mjs +0 -192
  295. package/src/replit.mjs +0 -1210
  296. package/src/repo.mjs +0 -445
  297. package/src/revert.mjs +0 -149
  298. package/src/routing-advisor.mjs +0 -204
  299. package/src/self-correct.mjs +0 -147
  300. package/src/session-lock.mjs +0 -160
  301. package/src/session.mjs +0 -1655
  302. package/src/settings-tui.mjs +0 -373
  303. package/src/setup-flow.mjs +0 -223
  304. package/src/signal.mjs +0 -115
  305. package/src/simmer.mjs +0 -241
  306. package/src/strategy.mjs +0 -235
  307. package/src/subscription.mjs +0 -212
  308. package/src/templates.mjs +0 -260
  309. package/src/think-engine.mjs +0 -428
  310. package/src/tui.mjs +0 -276
  311. package/src/update-check.mjs +0 -35
  312. package/src/wave-planner.mjs +0 -294
@@ -1,447 +0,0 @@
1
- /**
2
- * intelligence.mjs — Situational awareness for every pipeline run.
3
- * Reads project reality fresh, derives task context, and detects contradictions
4
- * between what an agent plans to do and what is actually true.
5
- */
6
-
7
- import { existsSync, readFileSync, readdirSync } from 'node:fs';
8
- import { execSync } from 'node:child_process';
9
- import { join } from 'node:path';
10
-
11
- const PROTECTED_PATHS = [
12
- 'src/pipeline.mjs',
13
- 'src/dispatch.mjs',
14
- 'src/decide.mjs',
15
- '.claude/hooks/head-guard.mjs',
16
- ];
17
-
18
- // ─── Git helpers ──────────────────────────────────────────────────────────────
19
-
20
- function safeExec(cmd, cwd) {
21
- try {
22
- return execSync(cmd, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
23
- } catch {
24
- return '';
25
- }
26
- }
27
-
28
- function getDirtyFiles(cwd) {
29
- const raw = safeExec('git status --porcelain', cwd);
30
- if (!raw) return [];
31
- return raw
32
- .split('\n')
33
- .filter(l => l.trim())
34
- .map(l => l.slice(3).trim().replace(/^"(.*)"$/, '$1'))
35
- .filter(Boolean);
36
- }
37
-
38
- function getRecentCommits(cwd, n = 5) {
39
- const raw = safeExec(`git log -${n} --pretty=format:%s`, cwd);
40
- if (!raw) return [];
41
- return raw.split('\n').filter(Boolean);
42
- }
43
-
44
- function getAheadCount(cwd) {
45
- const raw = safeExec('git rev-list --count @{u}..HEAD', cwd);
46
- const n = parseInt(raw, 10);
47
- return isNaN(n) ? 0 : n;
48
- }
49
-
50
- function getCurrentBranch(cwd) {
51
- return safeExec('git branch --show-current', cwd) || 'unknown';
52
- }
53
-
54
- // ─── Failure reader ───────────────────────────────────────────────────────────
55
-
56
- function readRecentFailures(cwd, limit = 10) {
57
- const path = join(cwd, '.dualbrain', 'failures.jsonl');
58
- if (!existsSync(path)) return [];
59
- try {
60
- const lines = readFileSync(path, 'utf8').split('\n').filter(Boolean);
61
- return lines
62
- .slice(-limit)
63
- .reverse()
64
- .map(line => {
65
- try { return JSON.parse(line); } catch { return null; }
66
- })
67
- .filter(r => r && !r.resolved)
68
- .map(r => ({
69
- prompt: r.prompt ?? '',
70
- error: r.error ?? '',
71
- approach: r.tier ? `${r.tier}/${r.model ?? 'unknown'}` : (r.model ?? 'unknown'),
72
- timestamp: r.timestamp ?? 0,
73
- }));
74
- } catch {
75
- return [];
76
- }
77
- }
78
-
79
- // ─── Outcome reader ───────────────────────────────────────────────────────────
80
-
81
- function readRecentOutcomes(cwd, limit = 10) {
82
- const dir = join(cwd, '.dualbrain', 'outcomes');
83
- if (!existsSync(dir)) return [];
84
- try {
85
- const files = readdirSync(dir)
86
- .filter(f => f.endsWith('.jsonl'))
87
- .sort()
88
- .reverse()
89
- .slice(0, 3);
90
-
91
- const records = [];
92
- for (const file of files) {
93
- try {
94
- const lines = readFileSync(join(dir, file), 'utf8').split('\n').filter(Boolean);
95
- for (const line of lines) {
96
- try {
97
- const r = JSON.parse(line);
98
- records.push({
99
- task: r.prompt ?? '',
100
- success: r.result?.success ?? false,
101
- timestamp: r.timestamp ?? 0,
102
- });
103
- } catch { /* skip bad line */ }
104
- }
105
- } catch { /* skip unreadable file */ }
106
- }
107
-
108
- return records
109
- .sort((a, b) => b.timestamp - a.timestamp)
110
- .slice(0, limit);
111
- } catch {
112
- return [];
113
- }
114
- }
115
-
116
- // ─── Package.json reader ──────────────────────────────────────────────────────
117
-
118
- function readPackageJson(cwd) {
119
- try {
120
- return JSON.parse(readFileSync(join(cwd, 'package.json'), 'utf8'));
121
- } catch {
122
- return {};
123
- }
124
- }
125
-
126
- // ─── Exports ──────────────────────────────────────────────────────────────────
127
-
128
- /**
129
- * Read project reality fresh. No cache. Returns a ProjectBrief.
130
- */
131
- export function deriveProjectState(cwd = process.cwd()) {
132
- const pkg = readPackageJson(cwd);
133
-
134
- const version = pkg.version ?? '0.0.0';
135
- const versionMajor = parseInt(version.split('.')[0], 10) || 0;
136
-
137
- const dirtyFiles = getDirtyFiles(cwd);
138
- const recentCommits = getRecentCommits(cwd, 5);
139
- const branch = getCurrentBranch(cwd);
140
- const aheadOfRemote = getAheadCount(cwd);
141
-
142
- const binField = pkg.bin ?? {};
143
- const binValues = Object.values(binField);
144
- const entryPoint = binValues[0] ?? (pkg.main ?? '');
145
-
146
- const testCommand = pkg.scripts?.test ?? null;
147
-
148
- const recentFailures = readRecentFailures(cwd, 10);
149
- const recentOutcomes = readRecentOutcomes(cwd, 10);
150
-
151
- return {
152
- packageName: pkg.name ?? 'unknown',
153
- version,
154
- versionMajor,
155
- description: pkg.description ?? '',
156
-
157
- branch,
158
- dirty: dirtyFiles.length > 0,
159
- dirtyFiles,
160
- recentCommits,
161
- aheadOfRemote,
162
-
163
- brandName: 'dual-brain',
164
- cliCommand: 'dual-brain',
165
-
166
- moduleType: pkg.type === 'module' ? 'esm' : 'cjs',
167
- entryPoint,
168
- testCommand,
169
-
170
- protectedPaths: PROTECTED_PATHS,
171
-
172
- recentFailures,
173
- recentOutcomes,
174
-
175
- derivedAt: Date.now(),
176
- };
177
- }
178
-
179
- /**
180
- * Derive task-scoped context from the current prompt and optional session events.
181
- */
182
- export function deriveTaskContext(task = '', recentEvents = []) {
183
- const priorAttempts = [];
184
- const filesOutOfScope = [];
185
- const filesInScopeSet = new Set();
186
-
187
- const FILE_RE = /(?:^|\s)((?:src|hooks|bin|scripts|\.claude)\/[\w./\-]+\.\w+)/g;
188
- let m;
189
-
190
- FILE_RE.lastIndex = 0;
191
- while ((m = FILE_RE.exec(task)) !== null) filesInScopeSet.add(m[1]);
192
-
193
- for (const ev of (recentEvents ?? [])) {
194
- if (!ev) continue;
195
-
196
- if (ev.type === 'failure' || ev.failed) {
197
- priorAttempts.push({
198
- approach: ev.approach ?? ev.tier ?? 'unknown',
199
- failed: true,
200
- reason: ev.error ?? ev.reason ?? '',
201
- });
202
- for (const f of (ev.files ?? ev.filesChanged ?? [])) {
203
- filesOutOfScope.push(f);
204
- }
205
- }
206
-
207
- FILE_RE.lastIndex = 0;
208
- const evText = JSON.stringify(ev);
209
- while ((m = FILE_RE.exec(evText)) !== null) filesInScopeSet.add(m[1]);
210
- }
211
-
212
- const failureCount = priorAttempts.filter(a => a.failed).length;
213
- const escalationLevel =
214
- failureCount >= 3 ? 'critical' :
215
- failureCount >= 1 ? 'elevated' :
216
- 'normal';
217
-
218
- const constraintKeywords = [];
219
- const CONSTRAINT_RE = /\b(must|never|always|do not|don't|only|no\s+\w+)\b[^.!?]{0,80}/gi;
220
- let cm;
221
- CONSTRAINT_RE.lastIndex = 0;
222
- while ((cm = CONSTRAINT_RE.exec(task)) !== null) {
223
- constraintKeywords.push(cm[0].trim());
224
- }
225
-
226
- return {
227
- task,
228
- priorAttempts,
229
- activeConstraints: constraintKeywords,
230
- filesInScope: [...filesInScopeSet],
231
- filesOutOfScope: [...new Set(filesOutOfScope)],
232
- escalationLevel,
233
- };
234
- }
235
-
236
- /**
237
- * Detect contradictions between project reality, task context, and a proposed plan.
238
- * Returns an array of contradiction objects.
239
- */
240
- export function detectContradictions(projectBrief, taskBrief, plan = {}) {
241
- const contradictions = [];
242
-
243
- const planDesc = plan.description ?? '';
244
- const planAssumptions = plan.assumptions ?? {};
245
- const targetFiles = Array.isArray(plan.targetFiles) ? plan.targetFiles : [];
246
-
247
- // 1. version_mismatch
248
- const assumedVersion = typeof planAssumptions === 'string'
249
- ? planAssumptions
250
- : (planAssumptions.version ?? planAssumptions.packageVersion ?? '');
251
-
252
- if (assumedVersion) {
253
- const assumedMajor = parseInt(String(assumedVersion).split('.')[0], 10);
254
- if (!isNaN(assumedMajor) && assumedMajor !== projectBrief.versionMajor) {
255
- contradictions.push({
256
- type: 'version_mismatch',
257
- severity: 'block',
258
- message: `Plan assumes major version ${assumedMajor} but package is v${projectBrief.versionMajor} (${projectBrief.version})`,
259
- evidence: { expected: projectBrief.version, actual: assumedVersion },
260
- });
261
- }
262
- }
263
-
264
- // version reference in description
265
- const versionInDesc = planDesc.match(/\bv?(\d+)\.\d+\.\d+\b/);
266
- if (versionInDesc) {
267
- const descMajor = parseInt(versionInDesc[1], 10);
268
- if (!isNaN(descMajor) && descMajor !== projectBrief.versionMajor) {
269
- contradictions.push({
270
- type: 'version_mismatch',
271
- severity: 'warn',
272
- message: `Plan description references v${versionInDesc[0]} but package is v${projectBrief.version}`,
273
- evidence: { expected: projectBrief.version, actual: versionInDesc[0] },
274
- });
275
- }
276
- }
277
-
278
- // 2. branding_error
279
- const WRONG_NAMES = ['data-tools', 'orchestrator', 'dual_brain', 'dualbrain', 'brain-dual'];
280
- const searchText = [planDesc, JSON.stringify(planAssumptions)].join(' ').toLowerCase();
281
- for (const wrongName of WRONG_NAMES) {
282
- if (searchText.includes(wrongName) && !searchText.includes('dual-brain')) {
283
- contradictions.push({
284
- type: 'branding_error',
285
- severity: 'block',
286
- message: `Plan references "${wrongName}" but correct package name is "${projectBrief.brandName}"`,
287
- evidence: { expected: projectBrief.brandName, actual: wrongName },
288
- });
289
- break;
290
- }
291
- }
292
-
293
- // check explicit packageName assumption
294
- const assumedName = typeof planAssumptions === 'object' ? planAssumptions.packageName : null;
295
- if (assumedName && assumedName !== projectBrief.packageName) {
296
- contradictions.push({
297
- type: 'name_mismatch',
298
- severity: 'block',
299
- message: `Plan assumes packageName "${assumedName}" but actual package is "${projectBrief.packageName}"`,
300
- evidence: { expected: projectBrief.packageName, actual: assumedName },
301
- });
302
- }
303
-
304
- // 3. repeated_failure
305
- const planWords = new Set(
306
- planDesc.toLowerCase().split(/\W+/).filter(w => w.length > 3)
307
- );
308
- for (const failure of (projectBrief.recentFailures ?? [])) {
309
- const failureWords = (failure.prompt ?? '').toLowerCase().split(/\W+/).filter(w => w.length > 3);
310
- const overlap = failureWords.filter(w => planWords.has(w)).length;
311
- const similarity = overlap / Math.max(planWords.size, failureWords.length, 1);
312
- if (similarity >= 0.4) {
313
- contradictions.push({
314
- type: 'repeated_failure',
315
- severity: 'warn',
316
- message: `Plan resembles a recent failed attempt: "${failure.prompt.slice(0, 80)}"`,
317
- evidence: { expected: 'novel approach', actual: failure.prompt.slice(0, 80) },
318
- });
319
- break;
320
- }
321
- }
322
-
323
- // 4. scope_violation + 5. protected_file
324
- const taskFiles = new Set(taskBrief?.filesInScope ?? []);
325
- const protectedSet = new Set(projectBrief.protectedPaths ?? []);
326
-
327
- for (const f of targetFiles) {
328
- const isProtected = protectedSet.has(f) || [...protectedSet].some(p => f.endsWith(p));
329
- const inScope = taskFiles.has(f) || taskFiles.size === 0;
330
-
331
- if (isProtected && !inScope) {
332
- contradictions.push({
333
- type: 'protected_file',
334
- severity: 'block',
335
- message: `Plan targets protected file "${f}" without explicit scope justification`,
336
- evidence: { expected: 'file not in plan', actual: f },
337
- });
338
- } else if (!inScope && isProtected) {
339
- contradictions.push({
340
- type: 'scope_violation',
341
- severity: 'warn',
342
- message: `Plan targets "${f}" which is protected and not mentioned in task scope`,
343
- evidence: { expected: [...taskFiles].join(', ') || 'none', actual: f },
344
- });
345
- } else if (!inScope && taskFiles.size > 0) {
346
- contradictions.push({
347
- type: 'scope_violation',
348
- severity: 'warn',
349
- message: `Plan targets "${f}" which is outside the task's stated file scope`,
350
- evidence: { expected: [...taskFiles].join(', '), actual: f },
351
- });
352
- }
353
- }
354
-
355
- return contradictions;
356
- }
357
-
358
- /**
359
- * Format a compact situational awareness summary (max 15 lines) for agent prompts.
360
- * @param {object} projectBrief
361
- * @param {object} taskBrief
362
- * @param {object|null} [sessionContext] Optional: { relatedSessions, riskSignals, priorAttempts, relevantFiles }
363
- */
364
- export function formatBrief(projectBrief, taskBrief, sessionContext = null) {
365
- const lines = [];
366
-
367
- const dirtyLabel = projectBrief.dirty ? 'dirty' : 'clean';
368
- lines.push(
369
- `PROJECT: ${projectBrief.packageName} v${projectBrief.version} (${projectBrief.moduleType})`
370
- );
371
-
372
- lines.push(
373
- `BRANCH: ${projectBrief.branch} (${dirtyLabel}) | ${projectBrief.aheadOfRemote} ahead`
374
- );
375
-
376
- if (projectBrief.recentCommits?.length > 0) {
377
- const preview = projectBrief.recentCommits
378
- .slice(0, 2)
379
- .map(c => `"${c.slice(0, 50)}"`)
380
- .join(' · ');
381
- lines.push(`RECENT: ${preview}`);
382
- }
383
-
384
- const failureCount = (projectBrief.recentFailures ?? []).length;
385
- if (failureCount > 0) {
386
- const dayMs = 24 * 60 * 60 * 1000;
387
- const cutoff = Date.now() - dayMs;
388
- const recent24 = projectBrief.recentFailures.filter(f => f.timestamp >= cutoff).length;
389
- const categories = [...new Set(
390
- projectBrief.recentFailures.slice(0, 5).map(f => f.error?.split(':')[0]?.trim()).filter(Boolean)
391
- )].slice(0, 2).join(', ');
392
- lines.push(
393
- `FAILURES: ${recent24} in last 24h${categories ? ` (${categories})` : ''}`
394
- );
395
- } else {
396
- lines.push('FAILURES: none');
397
- }
398
-
399
- const protectedNames = (projectBrief.protectedPaths ?? [])
400
- .map(p => p.split('/').pop())
401
- .join(', ');
402
- if (protectedNames) lines.push(`PROTECTED: ${protectedNames}`);
403
-
404
- if (taskBrief) {
405
- const taskPreview = (taskBrief.task ?? '').slice(0, 80);
406
- if (taskPreview) lines.push(`TASK: "${taskPreview}"`);
407
-
408
- const failedAttempts = (taskBrief.priorAttempts ?? []).filter(a => a.failed);
409
- if (failedAttempts.length > 0) {
410
- const lastReason = failedAttempts[0].reason
411
- ? ` (${failedAttempts[0].reason.slice(0, 40)})`
412
- : '';
413
- lines.push(`PRIOR ATTEMPTS: ${failedAttempts.length} failed${lastReason}`);
414
- }
415
-
416
- if (taskBrief.escalationLevel && taskBrief.escalationLevel !== 'normal') {
417
- lines.push(`ESCALATION: ${taskBrief.escalationLevel}`);
418
- }
419
-
420
- if (taskBrief.filesInScope?.length > 0) {
421
- lines.push(`IN SCOPE: ${taskBrief.filesInScope.slice(0, 4).join(', ')}`);
422
- }
423
- }
424
-
425
- // Session context: include 1-2 lines about prior cross-session work when available
426
- if (sessionContext) {
427
- const priorAttempts = Array.isArray(sessionContext.priorAttempts) ? sessionContext.priorAttempts : [];
428
- const failures = priorAttempts.filter(a => a && (a.failed || a.status === 'failed'));
429
- const successes = priorAttempts.filter(a => a && !a.failed && a.status !== 'failed');
430
-
431
- if (failures.length > 0) {
432
- const lastFail = failures[failures.length - 1];
433
- const age = lastFail.daysAgo != null ? `${lastFail.daysAgo}d ago` : (lastFail.when ?? 'recently');
434
- const reason = lastFail.error ?? lastFail.reason ?? '';
435
- const reasonClip = reason ? ` (${reason.slice(0, 40)})` : '';
436
- lines.push(`PRIOR: similar task failed ${age}${reasonClip}`);
437
- } else if (successes.length > 0) {
438
- const lastSuccess = successes[successes.length - 1];
439
- const age = lastSuccess.daysAgo != null ? `${lastSuccess.daysAgo}d ago` : (lastSuccess.when ?? 'recently');
440
- lines.push(`PRIOR: related work completed successfully ${age}`);
441
- } else if (Array.isArray(sessionContext.relatedSessions) && sessionContext.relatedSessions.length > 0) {
442
- lines.push(`PRIOR: ${sessionContext.relatedSessions.length} related session(s) found`);
443
- }
444
- }
445
-
446
- return lines.slice(0, 15).join('\n');
447
- }
package/src/ledger.mjs DELETED
@@ -1,196 +0,0 @@
1
- // Task ledger: append-only accountability store for HEAD's promises. Every tracked task has a full snapshot per state change.
2
-
3
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
4
- import { join } from 'path';
5
-
6
- const LEDGER_PATH = '.dualbrain/ledger.jsonl';
7
-
8
- function ledgerPath(cwd) {
9
- return join(cwd || process.cwd(), LEDGER_PATH);
10
- }
11
-
12
- function readAllEntries(cwd) {
13
- const p = ledgerPath(cwd);
14
- if (!existsSync(p)) return [];
15
- const raw = readFileSync(p, 'utf8').trim();
16
- if (!raw) return [];
17
- return raw.split('\n').map(line => JSON.parse(line));
18
- }
19
-
20
- function appendEntry(entry, cwd) {
21
- const p = ledgerPath(cwd);
22
- mkdirSync(join(cwd || process.cwd(), '.dualbrain'), { recursive: true });
23
- writeFileSync(p, JSON.stringify(entry) + '\n', { flag: 'a' });
24
- }
25
-
26
- function getCurrentTasks(cwd) {
27
- const entries = readAllEntries(cwd);
28
- const map = new Map();
29
- for (const entry of entries) {
30
- map.set(entry.id, entry);
31
- }
32
- return Array.from(map.values());
33
- }
34
-
35
- export function createTask(task, cwd) {
36
- const entry = {
37
- id: `task_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`,
38
- created: new Date().toISOString(),
39
- updated: new Date().toISOString(),
40
- intent: task.intent || '',
41
- status: 'open',
42
- owner: task.owner || 'head',
43
- priority: task.priority || 'medium',
44
- blockers: task.blockers || [],
45
- proof: task.proof || null,
46
- subtasks: task.subtasks || [],
47
- parentTask: task.parentTask || null,
48
- files: task.files || [],
49
- result: task.result || null,
50
- cost: task.cost || null,
51
- };
52
- appendEntry(entry, cwd);
53
- return entry;
54
- }
55
-
56
- export function updateTask(taskId, updates, cwd) {
57
- const current = getTask(taskId, cwd);
58
- if (!current) throw new Error(`Task not found: ${taskId}`);
59
-
60
- if (updates.status === 'done') {
61
- const proof = updates.proof ?? current.proof;
62
- const result = updates.result ?? current.result;
63
- if (!proof) throw new Error(`Cannot mark task done without proof: ${taskId}`);
64
- if (!result) throw new Error(`Cannot mark task done without result: ${taskId}`);
65
- }
66
-
67
- const updated = {
68
- ...current,
69
- ...updates,
70
- id: taskId,
71
- updated: new Date().toISOString(),
72
- };
73
- appendEntry(updated, cwd);
74
- return updated;
75
- }
76
-
77
- export function failTask(taskId, reason, cwd) {
78
- const current = getTask(taskId, cwd);
79
- if (!current) throw new Error(`Task not found: ${taskId}`);
80
- const updated = {
81
- ...current,
82
- status: 'failed',
83
- result: reason || 'failed',
84
- updated: new Date().toISOString(),
85
- };
86
- appendEntry(updated, cwd);
87
- return updated;
88
- }
89
-
90
- export function blockTask(taskId, blocker, cwd) {
91
- const current = getTask(taskId, cwd);
92
- if (!current) throw new Error(`Task not found: ${taskId}`);
93
- const updated = {
94
- ...current,
95
- status: 'blocked',
96
- blockers: [...(current.blockers || []), blocker],
97
- updated: new Date().toISOString(),
98
- };
99
- appendEntry(updated, cwd);
100
- return updated;
101
- }
102
-
103
- export function getTask(taskId, cwd) {
104
- const entries = readAllEntries(cwd);
105
- let latest = null;
106
- for (const entry of entries) {
107
- if (entry.id === taskId) latest = entry;
108
- }
109
- return latest;
110
- }
111
-
112
- export function getOpenTasks(cwd) {
113
- return getCurrentTasks(cwd).filter(t =>
114
- t.status === 'open' || t.status === 'in_progress' || t.status === 'blocked'
115
- );
116
- }
117
-
118
- export function getTaskHistory(taskId, cwd) {
119
- return readAllEntries(cwd).filter(e => e.id === taskId);
120
- }
121
-
122
- export function getTaskSummary(cwd) {
123
- const tasks = getCurrentTasks(cwd);
124
- const summary = { open: 0, inProgress: 0, blocked: 0, done: 0, failed: 0, total: tasks.length };
125
- for (const t of tasks) {
126
- if (t.status === 'open') summary.open++;
127
- else if (t.status === 'in_progress') summary.inProgress++;
128
- else if (t.status === 'blocked') summary.blocked++;
129
- else if (t.status === 'done') summary.done++;
130
- else if (t.status === 'failed') summary.failed++;
131
- }
132
- return summary;
133
- }
134
-
135
- export function formatTaskList(tasks) {
136
- const all = tasks;
137
- const open = all.filter(t => t.status === 'open' || t.status === 'in_progress').length;
138
- const blocked = all.filter(t => t.status === 'blocked').length;
139
- const done = all.filter(t => t.status === 'done').length;
140
-
141
- const lines = [`TASKS (${open} open, ${blocked} blocked, ${done} done)`];
142
-
143
- for (const t of all) {
144
- const pri = t.priority === 'critical' ? 'crit' : t.priority === 'high' ? 'high' : t.priority === 'low' ? 'low' : 'med';
145
- const label = t.intent.length > 48 ? t.intent.slice(0, 45) + '...' : t.intent;
146
-
147
- if (t.status === 'done') {
148
- lines.push(` ✓ [${pri}] ${label} (done)`);
149
- } else if (t.status === 'failed') {
150
- lines.push(` ✗ [${pri}] ${label} (failed)`);
151
- } else if (t.status === 'blocked') {
152
- const blockerNote = t.blockers && t.blockers.length ? `: ${t.blockers[t.blockers.length - 1]}` : '';
153
- lines.push(` ◌ [${pri}] ${label} (blocked${blockerNote})`);
154
- } else {
155
- lines.push(` ● [${pri}] ${label} (${t.status})`);
156
- }
157
- }
158
-
159
- return lines.join('\n');
160
- }
161
-
162
- export function reconcile(cwd) {
163
- const tasks = getCurrentTasks(cwd);
164
- const cutoff = Date.now() - 24 * 60 * 60 * 1000;
165
- return tasks.filter(t =>
166
- (t.status === 'open' || t.status === 'in_progress') &&
167
- new Date(t.updated).getTime() < cutoff
168
- );
169
- }
170
-
171
- export function decompose(taskId, subtasks, cwd) {
172
- const parent = getTask(taskId, cwd);
173
- if (!parent) throw new Error(`Task not found: ${taskId}`);
174
-
175
- const created = subtasks.map(sub =>
176
- createTask(
177
- {
178
- ...sub,
179
- parentTask: taskId,
180
- owner: sub.owner || parent.owner,
181
- priority: sub.priority || parent.priority,
182
- },
183
- cwd
184
- )
185
- );
186
-
187
- const subtaskIds = created.map(s => s.id);
188
- const updatedParent = {
189
- ...parent,
190
- subtasks: [...(parent.subtasks || []), ...subtaskIds],
191
- updated: new Date().toISOString(),
192
- };
193
- appendEntry(updatedParent, cwd);
194
-
195
- return { parent: updatedParent, subtasks: created };
196
- }