dual-brain 0.2.14 → 0.2.16

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.
@@ -0,0 +1,557 @@
1
+ // Cognitive Loop — the integration layer that makes HEAD a continuous process.
2
+ // Replaces single-shot deliberation with: perceive → plan → predict → dispatch → debrief → replan → ...
3
+
4
+ import { processTurn, loadState, perceive, assessUncertainty, deriveObligations, notice, deliberate, assessDepth, recordDispatchOutcome } from './head.mjs';
5
+ import { planWaves, shouldReplan, replan, estimateWaveCost } from './wave-planner.mjs';
6
+ import { parseDebrief, generateDebriefInstruction, integrateDebrief, summarizeWaveOutcome } from './debrief.mjs';
7
+ import { predictFailureModes, generatePreventions, scoreDispatchReadiness, evolvePatterns, loadSessionPatterns } from './predictive.mjs';
8
+ import { writeDeliberation } from './head-protocol.mjs';
9
+ import { check as checkInbox, generateInboxBrief, purge as purgeInbox } from './inbox.mjs';
10
+ import * as memoryTiers from './memory-tiers.mjs';
11
+ import * as narrative from './narrative.mjs';
12
+ import * as simmer from './simmer.mjs';
13
+ import { build as buildEnvelope } from './envelope.mjs';
14
+ import { acquire as acquireSession, release as releaseSession, isOwner as isSessionOwner } from './session-lock.mjs';
15
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
16
+ import { join } from 'node:path';
17
+
18
+ // ── Pattern cache ──────────────────────────────────────────────────────────
19
+ let _patternsCache = null;
20
+ let _patternsCacheTs = 0;
21
+ function getCachedPatterns() {
22
+ if (!_patternsCache || Date.now() - _patternsCacheTs > 5000) {
23
+ _patternsCache = loadSessionPatterns();
24
+ _patternsCacheTs = Date.now();
25
+ }
26
+ return _patternsCache;
27
+ }
28
+
29
+ const LOOP_STATE_DIR = join(process.cwd(), '.dualbrain');
30
+ const LOOP_STATE_FILE = join(LOOP_STATE_DIR, 'cognitive-loop.json');
31
+
32
+ // ── Loop state persistence ──────────────────────────────────────────────────
33
+
34
+ function loadLoopState() {
35
+ try {
36
+ if (existsSync(LOOP_STATE_FILE)) {
37
+ return JSON.parse(readFileSync(LOOP_STATE_FILE, 'utf8'));
38
+ }
39
+ } catch {}
40
+ return freshLoopState();
41
+ }
42
+
43
+ function freshLoopState() {
44
+ return {
45
+ sessionId: Date.now().toString(36),
46
+ activePlan: null,
47
+ completedWaves: [],
48
+ debriefs: [],
49
+ situationHistory: [],
50
+ replans: 0,
51
+ totalDispatches: 0,
52
+ totalTokensEstimated: 0,
53
+ };
54
+ }
55
+
56
+ function saveLoopState(state) {
57
+ mkdirSync(LOOP_STATE_DIR, { recursive: true });
58
+ writeFileSync(LOOP_STATE_FILE, JSON.stringify(state, null, 2));
59
+ }
60
+
61
+ // ── The cognitive loop ──────────────────────────────────────────────────────
62
+
63
+ /**
64
+ * Entry point: process a user message through the full cognitive loop.
65
+ * Returns a LoopResult that tells the caller exactly what to do next.
66
+ *
67
+ * @param {string} userMessage
68
+ * @param {object} context - { files, recentFiles, priorFailures, uncommittedFiles, etc }
69
+ * @returns {LoopResult}
70
+ */
71
+ export function enter(userMessage, context = {}) {
72
+ // Session lock: one HEAD at a time
73
+ const lock = acquireSession();
74
+ if (!lock.acquired) {
75
+ return {
76
+ phase: 'readonly',
77
+ action: { type: 'respond', mode: 'readonly' },
78
+ rationale: `Another session (${lock.existingSession}) is active. This session is read-only to prevent split-brain.`,
79
+ shouldAskUser: false,
80
+ surfaceNoticings: [],
81
+ plan: null,
82
+ nextDispatch: null,
83
+ };
84
+ }
85
+
86
+ const headState = loadState();
87
+ const loopState = loadLoopState();
88
+
89
+ // Check if we just auto-updated — surface it naturally
90
+ _checkUpdateNotice(context);
91
+
92
+ // Immersion: load memory tiers so HEAD is "in the song"
93
+ const memory = memoryTiers.assemble({
94
+ userMessage,
95
+ files: context.files || [],
96
+ intent: context._detectedIntent || null,
97
+ });
98
+ if (memory.combined) {
99
+ context._immersionContext = memory.combined;
100
+ }
101
+
102
+ // Check inbox for HEAD before processing
103
+ const inboxBrief = generateInboxBrief('head');
104
+ if (inboxBrief) {
105
+ context._inboxBrief = inboxBrief;
106
+ }
107
+
108
+ // Simmer: check for crystallized ideas to surface
109
+ const crystallized = simmer.harvest();
110
+ if (crystallized.length > 0) {
111
+ context._crystallizedIdeas = crystallized.map(i => i.idea);
112
+ }
113
+
114
+ // Phase 1: Full cognitive pipeline
115
+ const turn = processTurn(headState, userMessage, context);
116
+
117
+ // Save situation for history
118
+ loopState.situationHistory.push({
119
+ ts: Date.now(),
120
+ depth: turn.depth,
121
+ action: turn.action.type,
122
+ confidence: turn.result.confidence.score,
123
+ });
124
+
125
+ // Surface update notice as a noticing
126
+ if (context._updateNotice) {
127
+ turn.result.surfaceNoticings = turn.result.surfaceNoticings || [];
128
+ turn.result.surfaceNoticings.unshift(context._updateNotice);
129
+ }
130
+
131
+ // Narrative evolution: update HEAD's running understanding
132
+ _evolveNarrative(turn, userMessage, context);
133
+
134
+ // Simmer: capture any ideas from user message that aren't direct tasks
135
+ _captureSimmerSignals(userMessage, turn);
136
+
137
+ // If HEAD says don't dispatch, or it's a "proceed" with no active plan, respond
138
+ if ((!turn.shouldDispatch && !turn.shouldThink) || (turn.action.type === 'proceed' && !loopState.activePlan)) {
139
+ saveLoopState(loopState);
140
+ return {
141
+ phase: 'respond',
142
+ action: turn.action,
143
+ rationale: turn.rationale,
144
+ shouldAskUser: turn.shouldAskUser,
145
+ surfaceNoticings: turn.result.surfaceNoticings,
146
+ plan: null,
147
+ nextDispatch: null,
148
+ };
149
+ }
150
+
151
+ // Persist deliberation artifact for deliberation-gate hook validation
152
+ try {
153
+ writeDeliberation(userMessage, { situation: turn.situation, result: turn.result });
154
+ } catch (err) {
155
+ // Non-fatal: don't break the loop if deliberation persistence fails
156
+ process.stderr.write(`[cognitive-loop] writeDeliberation failed: ${err?.message?.slice(0, 120)}\n`);
157
+ }
158
+
159
+ // Phase 2: Plan waves
160
+ const plan = planWaves(turn, {
161
+ files: context.files || [],
162
+ priorDebriefs: loopState.debriefs,
163
+ diagnosticPatterns: getCachedPatterns(),
164
+ });
165
+
166
+ loopState.activePlan = plan;
167
+ saveLoopState(loopState);
168
+
169
+ // Phase 3: Prepare first wave for dispatch
170
+ const firstWave = plan.waves[0];
171
+ if (!firstWave) {
172
+ return {
173
+ phase: 'respond',
174
+ action: turn.action,
175
+ rationale: 'Wave planner produced empty plan — falling back to direct response',
176
+ shouldAskUser: true,
177
+ surfaceNoticings: turn.result.surfaceNoticings,
178
+ plan,
179
+ nextDispatch: null,
180
+ };
181
+ }
182
+
183
+ const prepared = prepareWave(firstWave, turn, context, loopState);
184
+
185
+ // Don't dispatch unready work — return as blocked
186
+ if (prepared.blockers.length > 0 && !prepared.allReady) {
187
+ saveLoopState(loopState);
188
+ return {
189
+ phase: 'blocked',
190
+ action: turn.action,
191
+ rationale: turn.rationale,
192
+ shouldAskUser: true,
193
+ surfaceNoticings: turn.result.surfaceNoticings,
194
+ plan,
195
+ nextDispatch: prepared,
196
+ suggestion: prepared.blockers[0],
197
+ };
198
+ }
199
+
200
+ return {
201
+ phase: 'dispatch',
202
+ action: turn.action,
203
+ rationale: turn.rationale,
204
+ shouldAskUser: turn.shouldAskUser,
205
+ surfaceNoticings: turn.result.surfaceNoticings,
206
+ plan,
207
+ nextDispatch: prepared,
208
+ estimatedCost: plan.estimatedCost,
209
+ };
210
+ }
211
+
212
+ /**
213
+ * Called after a wave completes. Processes debriefs and determines next action.
214
+ *
215
+ * @param {string[]} rawResults - raw output from each agent in the completed wave
216
+ * @param {string} completedWaveId - which wave just finished
217
+ * @param {object} context
218
+ * @returns {LoopResult}
219
+ */
220
+ export function advance(rawResults, completedWaveId, context = {}) {
221
+ const loopState = loadLoopState();
222
+ const plan = loopState.activePlan;
223
+
224
+ if (!plan) {
225
+ return { phase: 'done', action: { type: 'respond', mode: 'direct' }, rationale: 'No active plan', plan: null, nextDispatch: null };
226
+ }
227
+
228
+ // Parse debriefs from raw results
229
+ const debriefs = rawResults.map(r => parseDebrief(r));
230
+
231
+ // Track dispatch outcomes in HEAD state for self-awareness
232
+ const headState = loadState();
233
+ for (const d of debriefs) {
234
+ recordDispatchOutcome(headState, { type: d.artifacts?.tier || 'execute', objective: completedWaveId, status: d.status, durationMs: 0 });
235
+ }
236
+
237
+ const waveSummary = summarizeWaveOutcome(debriefs);
238
+ // Bridge field names between debrief (scopeDelta) and wave-planner (scopeChange)
239
+ waveSummary.scopeChange = waveSummary.scopeDelta;
240
+ waveSummary.confidence = waveSummary.aggregateConfidence;
241
+ waveSummary.blockers = waveSummary.allBlockers;
242
+
243
+ // Record
244
+ loopState.debriefs.push(...debriefs);
245
+ loopState.completedWaves.push(completedWaveId);
246
+
247
+ // Evolve prediction accuracy
248
+ const predictions = loopState._lastPredictions || [];
249
+ for (const d of debriefs) {
250
+ evolvePatterns(d, predictions);
251
+ }
252
+
253
+ // Post-wave verbal reflection (Reflexion pattern)
254
+ _postWaveReflection(waveSummary, loopState);
255
+
256
+ // Check if we need to replan
257
+ const needsReplan = shouldReplan(plan, waveSummary);
258
+
259
+ let activePlan = plan;
260
+ if (needsReplan) {
261
+ // Integrate each debrief into situation progressively
262
+ let updatedSituation = loopState.situationHistory[loopState.situationHistory.length - 1] || {};
263
+ for (const d of debriefs) {
264
+ updatedSituation = integrateDebrief(updatedSituation, d);
265
+ }
266
+
267
+ activePlan = replan(plan, waveSummary, { situation: updatedSituation, result: { depth: plan._depth || 'full' } });
268
+ loopState.activePlan = activePlan;
269
+ loopState.replans++;
270
+ }
271
+
272
+ // Find next wave to dispatch
273
+ const nextWave = activePlan.waves.find(w => !loopState.completedWaves.includes(w.id));
274
+
275
+ if (!nextWave) {
276
+ // All waves done — synthesize
277
+ const finalSummary = summarizeWaveOutcome(loopState.debriefs);
278
+
279
+ // Clean expired inbox messages now that all work is complete
280
+ try { purgeInbox(); } catch { /* non-fatal */ }
281
+
282
+ saveLoopState(loopState);
283
+ return {
284
+ phase: 'done',
285
+ action: { type: 'synthesize', mode: 'complete' },
286
+ rationale: `All ${loopState.completedWaves.length} waves complete. ${loopState.replans} replan(s).`,
287
+ plan: activePlan,
288
+ nextDispatch: null,
289
+ waveSummary: finalSummary,
290
+ replanned: needsReplan,
291
+ };
292
+ }
293
+
294
+ // Check gate condition
295
+ if (nextWave.gateCondition) {
296
+ const gateMet = evaluateGate(nextWave.gateCondition, waveSummary, loopState);
297
+ if (!gateMet) {
298
+ saveLoopState(loopState);
299
+ return {
300
+ phase: 'gated',
301
+ action: { type: 'pause', mode: 'gate-unmet' },
302
+ rationale: `Gate condition not met: ${nextWave.gateCondition}`,
303
+ plan: activePlan,
304
+ nextDispatch: null,
305
+ waveSummary,
306
+ gateCondition: nextWave.gateCondition,
307
+ };
308
+ }
309
+ }
310
+
311
+ // Prepare next wave
312
+ const lastDeliberation = { situation: context, result: { depth: activePlan._depth || 'full' } };
313
+ const prepared = prepareWave(nextWave, lastDeliberation, { ...context, priorDebriefs: loopState.debriefs }, loopState);
314
+
315
+ saveLoopState(loopState);
316
+
317
+ return {
318
+ phase: 'dispatch',
319
+ action: { type: 'dispatch', mode: nextWave.phase },
320
+ rationale: `Wave ${loopState.completedWaves.length + 1}/${activePlan.waves.length}: ${nextWave.phase}`,
321
+ plan: activePlan,
322
+ nextDispatch: prepared,
323
+ waveSummary,
324
+ replanned: needsReplan,
325
+ };
326
+ }
327
+
328
+ /**
329
+ * Prepare a wave for dispatch: run predictions, generate preventions, check readiness.
330
+ */
331
+ function prepareWave(wave, deliberation, context, loopState) {
332
+ const agents = wave.agents.map(agentSpec => {
333
+ // Predict failure modes for this agent
334
+ const predictions = predictFailureModes(agentSpec, {
335
+ priorDebriefs: context.priorDebriefs || [],
336
+ diagnosticPatterns: getCachedPatterns(),
337
+ files: context.files || [],
338
+ });
339
+
340
+ // Generate prevention instructions
341
+ const preventions = generatePreventions(predictions);
342
+
343
+ // Generate debrief instruction
344
+ const debriefInstruction = generateDebriefInstruction(agentSpec.tier, {
345
+ objective: agentSpec.objective,
346
+ scope: agentSpec.scope,
347
+ });
348
+
349
+ // Score readiness
350
+ const readiness = scoreDispatchReadiness(agentSpec, { waves: [wave] }, predictions);
351
+
352
+ // Check inbox for messages relevant to this worker tier
353
+ const workerInbox = generateInboxBrief(`worker:${agentSpec.tier}`);
354
+
355
+ return {
356
+ ...agentSpec,
357
+ preventions,
358
+ debriefInstruction,
359
+ readiness,
360
+ predictions,
361
+ prompt: buildAgentPrompt(agentSpec, preventions, debriefInstruction, workerInbox),
362
+ };
363
+ });
364
+
365
+ // Store predictions for later evolution
366
+ loopState._lastPredictions = agents.flatMap(a => a.predictions);
367
+
368
+ return {
369
+ waveId: wave.id,
370
+ phase: wave.phase,
371
+ parallel: wave.parallel,
372
+ agents,
373
+ allReady: agents.every(a => a.readiness.ready),
374
+ blockers: agents.flatMap(a => a.readiness.blockers),
375
+ warnings: agents.flatMap(a => a.readiness.warnings),
376
+ };
377
+ }
378
+
379
+ /**
380
+ * Build the full prompt for an agent using dispatch envelopes.
381
+ * Workers get understanding (prose preamble), not just instructions.
382
+ */
383
+ function buildAgentPrompt(agentSpec, preventions, debriefInstruction, inboxBrief) {
384
+ const envelope = buildEnvelope(agentSpec, {
385
+ preventions,
386
+ debriefInstruction,
387
+ inboxBrief,
388
+ });
389
+ return envelope.full;
390
+ }
391
+
392
+ /**
393
+ * Evaluate a wave gate condition against the current state.
394
+ */
395
+ function evaluateGate(condition, waveSummary, loopState) {
396
+ const lower = condition.toLowerCase();
397
+
398
+ if (lower.includes('confidence') && lower.includes('above')) {
399
+ const threshold = parseFloat(condition.match(/[\d.]+/)?.[0] || '0.5');
400
+ return (waveSummary.aggregateConfidence || 0) >= threshold;
401
+ }
402
+
403
+ if (lower.includes('no blocker')) {
404
+ return (waveSummary.allBlockers || []).length === 0;
405
+ }
406
+
407
+ if (lower.includes('scope confirmed')) {
408
+ return waveSummary.scopeDelta === 'same' || waveSummary.scopeDelta === 'smaller';
409
+ }
410
+
411
+ // Default: gate passes
412
+ return true;
413
+ }
414
+
415
+ // ── Immersion helpers ──────────────────────────────────────────────────────
416
+
417
+ /**
418
+ * Evolve HEAD's running narrative after processing a turn.
419
+ * Captures: what happened, what was decided, what the user cared about.
420
+ */
421
+ function _evolveNarrative(turn, userMessage, context) {
422
+ const parts = [];
423
+
424
+ // What the user said (compressed)
425
+ const userSnippet = userMessage.length > 120 ? userMessage.slice(0, 120) + '...' : userMessage;
426
+ parts.push(`User: "${userSnippet}"`);
427
+
428
+ // What HEAD decided
429
+ const action = turn.action;
430
+ if (action.type === 'dispatch') {
431
+ parts.push(`Decision: dispatch ${action.mode || 'work'} (confidence: ${turn.result.confidence.score})`);
432
+ } else if (action.type === 'respond') {
433
+ parts.push(`Decision: respond directly (${turn.depth} depth)`);
434
+ } else if (action.type === 'clarify') {
435
+ parts.push(`Decision: need clarification`);
436
+ }
437
+
438
+ // Noticings worth remembering
439
+ if (turn.result.surfaceNoticings?.length) {
440
+ parts.push(`Noticed: ${turn.result.surfaceNoticings.join('; ')}`);
441
+ }
442
+
443
+ // Crystallized simmer items surfaced this turn
444
+ if (context._crystallizedIdeas?.length) {
445
+ parts.push(`Crystallized ideas surfaced: ${context._crystallizedIdeas.join('; ')}`);
446
+ }
447
+
448
+ narrative.evolve(parts.join('. '));
449
+ }
450
+
451
+ /**
452
+ * Capture signals from user message that might be simmer-worthy.
453
+ * Looks for: analogies, "what if" ideas, vague suggestions, meta-observations.
454
+ */
455
+ function _captureSimmerSignals(userMessage, turn) {
456
+ const lower = userMessage.toLowerCase();
457
+
458
+ // Skip short/command messages
459
+ if (userMessage.length < 30) return;
460
+
461
+ // Detect idea-like patterns
462
+ const ideaPatterns = [
463
+ /what if (.{10,80})/i,
464
+ /maybe (?:we |it |this )(.{10,80})/i,
465
+ /i feel like (.{10,80})/i,
466
+ /(?:like|similar to) (.{10,80}?)(?: - | — |\.|\?|$)/i,
467
+ /consider (.{10,80})/i,
468
+ ];
469
+
470
+ for (const pattern of ideaPatterns) {
471
+ const match = userMessage.match(pattern);
472
+ if (match) {
473
+ simmer.add(match[0].slice(0, 120), { origin: 'user-message' });
474
+ return; // One simmer per message max
475
+ }
476
+ }
477
+
478
+ // If the message is exploratory (questions about approach) and depth is "full" or "deep",
479
+ // the whole message might be worth simmering
480
+ if (turn.depth === 'deep' && /\?/.test(userMessage) && userMessage.length > 60) {
481
+ const isExploration = /how|should|could|what about|think/i.test(lower);
482
+ if (isExploration) {
483
+ simmer.add(userMessage.slice(0, 150), { origin: 'exploratory-question', initialHeat: 1.5 });
484
+ }
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Post-wave narrative reflection — called after advance() processes debriefs.
490
+ * This is the verbal self-reflection piece (Reflexion pattern).
491
+ */
492
+ function _postWaveReflection(waveSummary, loopState) {
493
+ const parts = [];
494
+
495
+ parts.push(`Wave ${loopState.completedWaves.length} complete.`);
496
+
497
+ if (waveSummary.aggregateConfidence != null) {
498
+ parts.push(`Confidence: ${waveSummary.aggregateConfidence.toFixed(2)}`);
499
+ }
500
+
501
+ if (waveSummary.allBlockers?.length) {
502
+ parts.push(`Blockers emerged: ${waveSummary.allBlockers.join('; ')}`);
503
+ }
504
+
505
+ if (waveSummary.scopeDelta && waveSummary.scopeDelta !== 'same') {
506
+ parts.push(`Scope shifted: ${waveSummary.scopeDelta}`);
507
+ }
508
+
509
+ if (loopState.replans > 0) {
510
+ parts.push(`Replanned ${loopState.replans} time(s) — adapting to reality.`);
511
+ }
512
+
513
+ narrative.evolve(parts.join(' '));
514
+ }
515
+
516
+ /**
517
+ * Check for update notice and surface it to HEAD's awareness.
518
+ */
519
+ function _checkUpdateNotice(context) {
520
+ try {
521
+ const noticeFile = join(LOOP_STATE_DIR, '.update-notice');
522
+ if (!existsSync(noticeFile)) return;
523
+ const notice = JSON.parse(readFileSync(noticeFile, 'utf8'));
524
+ if (Date.now() - notice.ts < 5 * 60 * 1000) {
525
+ context._updateNotice = `Updated dual-brain ${notice.from} → ${notice.to}`;
526
+ }
527
+ // Clear it after reading (one-shot)
528
+ try { writeFileSync(noticeFile, ''); } catch {}
529
+ } catch {}
530
+ }
531
+
532
+ // ── Query functions ─────────────────────────────────────────────────────────
533
+
534
+ export function getActivePlan() {
535
+ return loadLoopState().activePlan;
536
+ }
537
+
538
+ export function getLoopStatus() {
539
+ const state = loadLoopState();
540
+ return {
541
+ hasActivePlan: !!state.activePlan,
542
+ completedWaves: state.completedWaves.length,
543
+ totalWaves: state.activePlan?.waves?.length || 0,
544
+ replans: state.replans,
545
+ totalDispatches: state.totalDispatches,
546
+ debriefCount: state.debriefs.length,
547
+ };
548
+ }
549
+
550
+ export function resetLoop() {
551
+ saveLoopState(freshLoopState());
552
+ releaseSession();
553
+ }
554
+
555
+ export function shutdown() {
556
+ releaseSession();
557
+ }
@@ -32,13 +32,13 @@ import { join } from 'node:path';
32
32
  */
33
33
  export function generateHandoff(sessionState) {
34
34
  return {
35
- version: 1,
35
+ version: 2,
36
36
  timestamp: new Date().toISOString(),
37
37
  task: sessionState.taskDescription || null,
38
38
  progress: {
39
39
  filesChanged: (sessionState.filesChanged || []).slice(0, 20),
40
40
  testsRun: sessionState.testsRun || [],
41
- decisions: (sessionState.decisions || []).slice(0, 5), // most recent routing decisions
41
+ decisions: (sessionState.decisions || []).slice(0, 5),
42
42
  },
43
43
  unresolved: (sessionState.unresolved || []).slice(0, 5),
44
44
  routing: {
@@ -48,19 +48,20 @@ export function generateHandoff(sessionState) {
48
48
  },
49
49
  preferences: sessionState.activePreferences || [],
50
50
  resumeHint: sessionState.resumeHint || null,
51
+ narrative: sessionState.narrative || null,
51
52
  };
52
53
  }
53
54
 
54
55
  // ─── Handoff persistence ──────────────────────────────────────────────────────
55
56
 
56
57
  /**
57
- * Persist a handoff object to .dual-brain/handoffs/.
58
+ * Persist a handoff object to .dualbrain/handoffs/.
58
59
  * @param {object} handoff Result of generateHandoff()
59
60
  * @param {string} [cwd] Project root (defaults to process.cwd())
60
61
  * @returns {string} Absolute path of the written file
61
62
  */
62
63
  export function saveHandoff(handoff, cwd) {
63
- const dir = join(cwd || process.cwd(), '.dual-brain', 'handoffs');
64
+ const dir = join(cwd || process.cwd(), '.dualbrain', 'handoffs');
64
65
  mkdirSync(dir, { recursive: true });
65
66
  const filename = `handoff-${Date.now()}.json`;
66
67
  writeFileSync(join(dir, filename), JSON.stringify(handoff, null, 2));
@@ -68,13 +69,13 @@ export function saveHandoff(handoff, cwd) {
68
69
  }
69
70
 
70
71
  /**
71
- * Load the most recent handoff from .dual-brain/handoffs/.
72
+ * Load the most recent handoff from .dualbrain/handoffs/.
72
73
  * Returns null when no handoffs exist or all are unreadable.
73
74
  * @param {string} [cwd]
74
75
  * @returns {object|null}
75
76
  */
76
77
  export function getLatestHandoff(cwd) {
77
- const dir = join(cwd || process.cwd(), '.dual-brain', 'handoffs');
78
+ const dir = join(cwd || process.cwd(), '.dualbrain', 'handoffs');
78
79
  if (!existsSync(dir)) return null;
79
80
  const files = readdirSync(dir)
80
81
  .filter(f => f.startsWith('handoff-') && f.endsWith('.json'))
@@ -196,7 +197,7 @@ export function buildResumeBrief(cwd) {
196
197
  * @returns {number} Count of files pruned
197
198
  */
198
199
  export function pruneHandoffs(cwd, keep = 10) {
199
- const dir = join(cwd || process.cwd(), '.dual-brain', 'handoffs');
200
+ const dir = join(cwd || process.cwd(), '.dualbrain', 'handoffs');
200
201
  if (!existsSync(dir)) return 0;
201
202
  const files = readdirSync(dir)
202
203
  .filter(f => f.startsWith('handoff-') && f.endsWith('.json'))
@@ -227,7 +228,7 @@ export function pruneHandoffs(cwd, keep = 10) {
227
228
  * }}
228
229
  */
229
230
  export function extractRoutingPatterns(cwd) {
230
- const dir = join(cwd || process.cwd(), '.dual-brain', 'handoffs');
231
+ const dir = join(cwd || process.cwd(), '.dualbrain', 'handoffs');
231
232
  if (!existsSync(dir)) return { patterns: [], confidence: 0, sampleSize: 0 };
232
233
 
233
234
  const files = readdirSync(dir)
@@ -1,4 +1,4 @@
1
- // cost-tracker.mjs — Lightweight cost estimation and efficiency tracking for .dual-brain/costs.jsonl.
1
+ // cost-tracker.mjs — Lightweight cost estimation and efficiency tracking for .dualbrain/costs.jsonl.
2
2
 
3
3
  import { readFileSync, appendFileSync, mkdirSync, existsSync } from 'node:fs';
4
4
  import { join } from 'node:path';
@@ -21,7 +21,7 @@ export function estimateTokenCost(model, tokens) {
21
21
 
22
22
  export function trackCost(action, cwd = process.cwd()) {
23
23
  try {
24
- const dir = join(cwd, '.dual-brain');
24
+ const dir = join(cwd, '.dualbrain');
25
25
  mkdirSync(dir, { recursive: true });
26
26
  const entry = {
27
27
  timestamp: new Date().toISOString(),
@@ -41,7 +41,7 @@ export function trackCost(action, cwd = process.cwd()) {
41
41
  }
42
42
 
43
43
  function readCostLines(cwd) {
44
- const p = join(cwd, '.dual-brain', 'costs.jsonl');
44
+ const p = join(cwd, '.dualbrain', 'costs.jsonl');
45
45
  if (!existsSync(p)) return [];
46
46
  try {
47
47
  return readFileSync(p, 'utf8').trim().split('\n').filter(Boolean).flatMap(line => {