@misterhuydo/cairn-mcp 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cairn-cli.js CHANGED
@@ -537,9 +537,11 @@ if (subcommand === '--version' || subcommand === '-v') {
537
537
  process.exit(0);
538
538
 
539
539
  } else if (subcommand === 'roadmap-hint') {
540
- // Stop hook: print the roadmap tree + cursor only when a cursor is set.
541
- // Silent (exit 0) for projects with no .cairn, no DB, or no cursor — so
542
- // normal chat is unaffected. Activates the moment cairn_todos focus is called.
540
+ // Stop hook: print the roadmap tree only when a cursor is set on the new
541
+ // roadmap_phases table (v1.10.0+). Silent (exit 0) when no .cairn, no DB,
542
+ // or no cursor. On *cursor advance* (cursor changed since last fire),
543
+ // also injects the decision memo's section for the new phase, so the
544
+ // architectural reasoning is in context exactly when needed.
543
545
  const cairnDir = path.join(process.cwd(), '.cairn');
544
546
  const dbPath = path.join(cairnDir, 'index.db');
545
547
  if (!fs.existsSync(dbPath)) process.exit(0);
@@ -550,65 +552,139 @@ if (subcommand === '--version' || subcommand === '-v') {
550
552
  let db;
551
553
  try { db = new DatabaseSync(dbPath, { readonly: true }); } catch { process.exit(0); }
552
554
 
555
+ const slugify = (n) => n.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_|_$/g, '');
556
+
557
+ // Extract `### Phase N: ...` section body from a decision memo file.
558
+ function extractPhaseSection(memoPath, phaseNumber) {
559
+ if (!fs.existsSync(memoPath)) return null;
560
+ const raw = fs.readFileSync(memoPath, 'utf8');
561
+ const lines = raw.split(/\r?\n/);
562
+ let inPhasesSection = false;
563
+ let collecting = false;
564
+ const acc = [];
565
+ for (const line of lines) {
566
+ if (/^##\s+Phases\s*$/i.test(line)) { inPhasesSection = true; continue; }
567
+ if (inPhasesSection && /^##\s+/.test(line) && !/^###/.test(line)) break;
568
+ if (!inPhasesSection) continue;
569
+ const m = /^###\s+Phase\s+(\d+)\s*:\s*(.+?)\s*$/.exec(line);
570
+ if (m) {
571
+ if (collecting) break;
572
+ if (parseInt(m[1], 10) === phaseNumber) {
573
+ acc.push(`### Phase ${m[1]}: ${m[2]}`);
574
+ collecting = true;
575
+ }
576
+ continue;
577
+ }
578
+ if (collecting) acc.push(line);
579
+ }
580
+ return acc.join('\n').trim() || null;
581
+ }
582
+
553
583
  try {
554
- const cursorRow = db.prepare(
555
- "SELECT todo_id FROM roadmap_cursor WHERE singleton = 1"
556
- ).get();
557
- const cursorId = cursorRow?.todo_id ?? null;
584
+ const cursorRow = db.prepare("SELECT phase_id FROM roadmap_cursor WHERE singleton = 1").get();
585
+ const cursorId = cursorRow?.phase_id ?? null;
558
586
  if (cursorId == null) { db.close(); process.exit(0); }
559
587
 
560
588
  const all = db.prepare(
561
- 'SELECT id, parent_id, position, status, text FROM todos ORDER BY IFNULL(parent_id, -1), position, id'
589
+ 'SELECT id, decision_name, phase_number, parent_phase_id, text, status, position FROM roadmap_phases ORDER BY decision_name, phase_number, position, id'
562
590
  ).all();
563
- db.close();
564
591
 
565
- const STATUS_BOX = { open: '[ ]', in_progress: '[~]', done: '[x]', blocked: '[!]' };
566
- const childrenByParent = new Map();
567
- for (const t of all) {
568
- const key = t.parent_id ?? null;
569
- if (!childrenByParent.has(key)) childrenByParent.set(key, []);
570
- childrenByParent.get(key).push(t);
592
+ // Build tree
593
+ const STATUS_BOX = { open: '[ ]', in_progress: '[~]', done: '[x]', blocked: '[!]', stale: '[s]' };
594
+ const byParent = new Map();
595
+ for (const p of all) {
596
+ const k = p.parent_phase_id ?? 'root';
597
+ if (!byParent.has(k)) byParent.set(k, []);
598
+ byParent.get(k).push(p);
599
+ }
600
+ for (const arr of byParent.values()) {
601
+ arr.sort((a, b) => {
602
+ if (a.decision_name && b.decision_name && a.decision_name !== b.decision_name) {
603
+ return a.decision_name.localeCompare(b.decision_name);
604
+ }
605
+ if (a.phase_number != null && b.phase_number != null) return a.phase_number - b.phase_number;
606
+ return a.position - b.position || a.id - b.id;
607
+ });
608
+ }
609
+ const roots = byParent.get('root') || [];
610
+ const decisionRoots = new Map();
611
+ for (const r of roots) {
612
+ if (r.decision_name == null) continue;
613
+ if (!decisionRoots.has(r.decision_name)) decisionRoots.set(r.decision_name, []);
614
+ decisionRoots.get(r.decision_name).push(r);
571
615
  }
572
-
573
616
  const lines = [];
574
- const visit = (parentId, depth) => {
575
- for (const t of childrenByParent.get(parentId) || []) {
576
- const arrow = cursorId === t.id ? '' : '';
577
- lines.push(`${' '.repeat(depth)}${STATUS_BOX[t.status] || '[?]'}${arrow} ${t.text} (id:${t.id})`);
578
- visit(t.id, depth + 1);
617
+ const visitSubtree = (parentId, depth) => {
618
+ for (const p of byParent.get(parentId) || []) {
619
+ if (p.status === 'stale') continue;
620
+ const arrow = cursorId === p.id ? '' : '';
621
+ const label = p.phase_number != null ? `Phase ${p.phase_number}: ${p.text}` : p.text;
622
+ lines.push(`${' '.repeat(depth)}${STATUS_BOX[p.status] || '[?]'}${arrow} ${label}`);
623
+ visitSubtree(p.id, depth + 1);
579
624
  }
580
625
  };
581
- visit(null, 0);
582
-
583
- // Compute "Next" via DFS: first open child of cursor → next open sibling/uncle.
584
- const isOpenish = (s) => s === 'open' || s === 'in_progress';
585
- const findNext = (fromId) => {
586
- const kids = (childrenByParent.get(fromId) || []).filter(t => isOpenish(t.status));
587
- if (kids.length > 0) return kids[0];
588
- let curId = fromId;
589
- while (curId != null) {
590
- const cur = all.find(t => t.id === curId);
591
- if (!cur) return null;
592
- const siblings = (childrenByParent.get(cur.parent_id ?? null) || [])
593
- .filter(t => isOpenish(t.status) && (t.position > cur.position || (t.position === cur.position && t.id > cur.id)));
594
- if (siblings.length > 0) return siblings[0];
595
- curId = cur.parent_id;
626
+ for (const [decisionName, phases] of decisionRoots) {
627
+ const live = phases.filter(p => p.status !== 'stale');
628
+ if (live.length === 0) continue;
629
+ const allDone = live.every(p => p.status === 'done');
630
+ const anyInProg = live.some(p => p.status === 'in_progress');
631
+ const rootStatus = allDone ? 'done' : (anyInProg ? 'in_progress' : 'open');
632
+ lines.push(`${STATUS_BOX[rootStatus]} Decision: ${decisionName}`);
633
+ for (const p of phases) {
634
+ if (p.status === 'stale') continue;
635
+ const arrow = cursorId === p.id ? ' →' : '';
636
+ lines.push(` ${STATUS_BOX[p.status] || '[?]'}${arrow} Phase ${p.phase_number}: ${p.text}`);
637
+ visitSubtree(p.id, 2);
596
638
  }
597
- return null;
598
- };
599
- const cursorNode = all.find(t => t.id === cursorId);
600
- const next = findNext(cursorId);
639
+ }
640
+
641
+ // Find current node + walk up to its root phase to identify decision/phase
642
+ const findById = (id) => all.find(p => p.id === id);
643
+ let cursorNode = findById(cursorId);
644
+ let rootDecision = cursorNode?.decision_name ?? null;
645
+ let rootPhaseNum = cursorNode?.phase_number ?? null;
646
+ let walker = cursorNode;
647
+ while (walker && walker.parent_phase_id != null) {
648
+ walker = findById(walker.parent_phase_id);
649
+ if (!walker) break;
650
+ rootDecision = walker.decision_name ?? rootDecision;
651
+ rootPhaseNum = walker.phase_number ?? rootPhaseNum;
652
+ }
653
+ db.close();
654
+
655
+ // Detect advance via state file
656
+ const stateFile = path.join(cairnDir, '.last-roadmap-cursor');
657
+ let lastCursor = null;
658
+ try { lastCursor = parseInt(fs.readFileSync(stateFile, 'utf8').trim(), 10); } catch {}
659
+ const advanced = lastCursor !== cursorId;
660
+ try { fs.writeFileSync(stateFile, String(cursorId), 'utf8'); } catch {}
601
661
 
602
662
  const out = [
603
- '[cairn] ROADMAP — cursor is active. Tree:',
663
+ '[cairn] ROADMAP — cursor is active.',
604
664
  ...lines.map(l => '[cairn] ' + l),
605
- cursorNode ? `[cairn] Current: (id:${cursorNode.id}) ${cursorNode.text}` : '',
606
- next ? `[cairn] Next: (id:${next.id}) ${next.text}` : '[cairn] Next: (no open todos remaining)',
607
- '[cairn] Use cairn_todos resolve <id> to mark done (auto-advances), focus <id> to move cursor, or next to advance manually.',
608
- ].filter(Boolean);
665
+ ];
666
+ const currentLabel = cursorNode
667
+ ? (cursorNode.phase_number != null
668
+ ? `${cursorNode.decision_name} / Phase ${cursorNode.phase_number} (${cursorNode.text})`
669
+ : `${rootDecision} / Phase ${rootPhaseNum} → sub: ${cursorNode.text}`)
670
+ : '(unknown)';
671
+ out.push(`[cairn] Current: ${currentLabel}`);
672
+
673
+ if (advanced && rootDecision != null && rootPhaseNum != null) {
674
+ const memoPath = path.join(cairnDir, 'memory', `decision_${slugify(rootDecision)}.md`);
675
+ const section = extractPhaseSection(memoPath, rootPhaseNum);
676
+ if (section) {
677
+ out.push('[cairn] ───── Decision memo (current phase) ─────');
678
+ for (const ln of section.split('\n')) out.push('[cairn] ' + ln);
679
+ out.push('[cairn] ───── (re-read after each phase to confirm direction) ─────');
680
+ }
681
+ }
682
+
683
+ out.push('[cairn] Use cairn_roadmap set_status <id> done (auto-advances), focus <id>, or next to move cursor.');
609
684
  process.stdout.write(out.join('\n') + '\n');
610
- } catch {
685
+ } catch (e) {
611
686
  try { db?.close(); } catch {}
687
+ process.stderr.write(`[cairn] roadmap-hint error: ${e.message}\n`);
612
688
  }
613
689
  process.exit(0);
614
690
 
package/index.js CHANGED
@@ -14,6 +14,7 @@ import { resume } from './src/tools/resume.js';
14
14
  import { minify } from './src/tools/minify.js';
15
15
  import { outlineProject } from './src/tools/outline.js';
16
16
  import { todos } from './src/tools/todos.js';
17
+ import { roadmap } from './src/tools/roadmap.js';
17
18
  import { memo } from './src/tools/memo.js';
18
19
  import { switchProject } from './src/tools/switch.js';
19
20
  import { employMemory } from './src/tools/employMemory.js';
@@ -262,23 +263,41 @@ Use to: get a project-wide bird's-eye view, triage code quality, find where to l
262
263
  },
263
264
  {
264
265
  name: 'cairn_todos',
265
- description: 'Manage TODO/FIXME/HACK/XXX items and multi-phase roadmaps. Supports a tree of nested phases with a "current cursor" pointer so multi-phase plans survive context drift. Use action=tree to render the roadmap, focus to set the cursor, next to advance. Stats are included in cairn_maintain and cairn_resume reports.',
266
+ description: 'Long-tail backlog: scanned TODO/FIXME/HACK/XXX comments + manual notes. Stats are included in cairn_maintain and cairn_resume reports. For active multi-phase plans (with cursor + decision-memo references) use cairn_roadmap instead.',
266
267
  inputSchema: {
267
268
  type: 'object',
268
269
  properties: {
269
270
  action: {
270
271
  type: 'string',
271
- enum: ['list', 'tree', 'add', 'resolve', 'focus', 'next', 'set_status', 'scan'],
272
- description: 'list: flat list | tree: hierarchical roadmap with cursor | add: create manual todo (use parent + position for nesting) | resolve: mark done | focus: move cursor to id, mark in_progress, propagate up | next: DFS-advance cursor to next open node | set_status: set status to open|in_progress|done|blocked | scan: re-scan codebase',
272
+ enum: ['list', 'add', 'resolve', 'scan'],
273
+ description: 'list: show todos | add: create a manual todo | resolve: mark done by id | scan: re-scan codebase',
273
274
  },
274
- text: { type: 'string', description: 'Text for the new todo (add action)' },
275
- kind: { type: 'string', enum: ['TODO', 'FIXME', 'HACK', 'XXX', 'NOTE'], description: 'Kind for new todo (default: TODO)' },
276
- id: { type: 'number', description: 'Todo ID (resolve, focus, set_status)' },
277
- status: { type: 'string', enum: ['open', 'in_progress', 'done', 'blocked'], description: 'Filter (list) or new value (set_status)' },
278
- source: { type: 'string', enum: ['scan', 'manual'], description: 'Filter by source (list action)' },
279
- parent: { type: 'number', description: 'Parent todo ID for nesting (add action). Omit for root/phase.' },
280
- position: { type: 'number', description: 'Sibling order within parent (add action). Auto-appends if omitted.' },
281
- compact: { type: 'boolean', description: 'Compact tree render — strips id/kind tags (tree action)' },
275
+ text: { type: 'string', description: 'Text for the new todo (add action)' },
276
+ kind: { type: 'string', enum: ['TODO', 'FIXME', 'HACK', 'XXX', 'NOTE'], description: 'Kind for new todo (default: TODO)' },
277
+ id: { type: 'number', description: 'Todo ID to resolve (resolve action)' },
278
+ status: { type: 'string', enum: ['open', 'done'], description: 'Filter by status (list action)' },
279
+ source: { type: 'string', enum: ['scan', 'manual'], description: 'Filter by source (list action)' },
280
+ },
281
+ required: ['action'],
282
+ },
283
+ },
284
+ {
285
+ name: 'cairn_roadmap',
286
+ description: 'Active project plan: phases with status + decision-memo backing + cursor. Phases are AUTHORED in decision memos via a `## Phases` section (### Phase 1: title, ### Phase 2: ...) — saving a decision memo via cairn_memo auto-seeds them here. Edit the memo to revise the plan; phases re-sync (existing preserved, removed marked stale, added appended). Sub-tasks under a phase are ad-hoc via add --parent. The Stop hook re-prints this tree after every assistant turn while a cursor is set, and injects the current phase memo section on cursor advance.',
287
+ inputSchema: {
288
+ type: 'object',
289
+ properties: {
290
+ action: {
291
+ type: 'string',
292
+ enum: ['tree', 'status', 'focus', 'next', 'set_status', 'add'],
293
+ description: 'tree: render roadmap grouped by decision | status: one-line summary (decisions in flight + current) | focus <id>: move cursor, mark in_progress, propagate up | next: DFS-advance cursor to next actionable phase (skips stale) | set_status <id> <status>: open|in_progress|done|blocked|stale (done auto-advances cursor) | add <parent> <text>: ad-hoc sub-task under an existing phase (inherits decision_ref)',
294
+ },
295
+ id: { type: 'number', description: 'Phase ID (focus, set_status)' },
296
+ status: { type: 'string', enum: ['open', 'in_progress', 'done', 'blocked', 'stale'], description: 'New status (set_status)' },
297
+ parent: { type: 'number', description: 'Parent phase ID (add — required, ad-hoc sub-tasks must hang under an existing phase)' },
298
+ text: { type: 'string', description: 'Sub-task text (add action)' },
299
+ compact: { type: 'boolean', description: 'Compact tree render — strips id tags (tree action)' },
300
+ hide_stale: { type: 'boolean', description: 'Hide stale phases in tree render (tree action)' },
282
301
  },
283
302
  required: ['action'],
284
303
  },
@@ -303,6 +322,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
303
322
  case 'cairn_outline': return outlineProject(db, args);
304
323
  case 'cairn_minify': return minify(db, args);
305
324
  case 'cairn_todos': return await todos(db, args);
325
+ case 'cairn_roadmap': return roadmap(db, args);
306
326
  case 'cairn_memo': return memo(db, args);
307
327
  case 'cairn_switch': return switchProject(db, args);
308
328
  case 'cairn_employ_memory': return employMemory(db, args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@misterhuydo/cairn-mcp",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "MCP server that gives Claude Code persistent memory across sessions. Index your codebase once, search symbols, bundle source, scan for vulnerabilities, and checkpoint/resume work — across Java, TypeScript, Vue, Python, SQL and more.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/graph/db.js CHANGED
@@ -77,6 +77,22 @@ const SCHEMA = `
77
77
  singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
78
78
  todo_id INTEGER REFERENCES todos(id) ON DELETE SET NULL
79
79
  );
80
+
81
+ CREATE TABLE IF NOT EXISTS roadmap_phases (
82
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
83
+ decision_name TEXT,
84
+ phase_number INTEGER,
85
+ parent_phase_id INTEGER REFERENCES roadmap_phases(id) ON DELETE CASCADE,
86
+ text TEXT NOT NULL,
87
+ body TEXT,
88
+ status TEXT NOT NULL DEFAULT 'open',
89
+ position INTEGER NOT NULL DEFAULT 0,
90
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
91
+ resolved_at TEXT
92
+ );
93
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_roadmap_phases_decision_phase
94
+ ON roadmap_phases(decision_name, phase_number)
95
+ WHERE decision_name IS NOT NULL AND phase_number IS NOT NULL;
80
96
  `;
81
97
 
82
98
  // Each sub-DB gets its IDs offset by this multiplier to prevent collisions
@@ -197,13 +213,13 @@ export function openDB() {
197
213
  db.exec(SCHEMA);
198
214
  // Migration: add line column to existing DBs that predate v1.7.0
199
215
  try { db.exec('ALTER TABLE main.symbols ADD COLUMN line INTEGER'); } catch {}
200
- // Migration: roadmap tree columns on todos (idempotentfails silently if present)
216
+ // Migration: roadmap tree columns on todos (v1.9.0kept dormant in v1.10+ for back-compat)
217
+ // Idempotent — fails silently if present. Columns are no longer read by tools as of v1.10.0,
218
+ // but kept to avoid destructive ALTER on user data.
201
219
  let addedRoadmapCols = false;
202
220
  try { db.exec('ALTER TABLE main.todos ADD COLUMN parent_id INTEGER REFERENCES todos(id) ON DELETE SET NULL'); addedRoadmapCols = true; } catch {}
203
221
  try { db.exec('ALTER TABLE main.todos ADD COLUMN position INTEGER NOT NULL DEFAULT 0'); addedRoadmapCols = true; } catch {}
204
222
  if (addedRoadmapCols) {
205
- // Backfill position by id order within each parent group (null parent = root).
206
- // ROW_NUMBER() requires SQLite 3.25+, which node:sqlite ships.
207
223
  db.exec(`
208
224
  UPDATE main.todos AS t
209
225
  SET position = (
@@ -215,6 +231,48 @@ export function openDB() {
215
231
  )
216
232
  `);
217
233
  }
234
+ // Migration: roadmap_cursor.phase_id (v1.10.0 — points at roadmap_phases.id)
235
+ try { db.exec('ALTER TABLE main.roadmap_cursor ADD COLUMN phase_id INTEGER REFERENCES roadmap_phases(id) ON DELETE SET NULL'); } catch {}
236
+ // Migration: copy any v1.9.0 in-flight roadmap (todos with parent_id) into roadmap_phases
237
+ // as a synthetic 'legacy_v1_9' decision. Runs once — guarded by checking if any phase exists
238
+ // under that decision_name.
239
+ try {
240
+ const legacyExists = db.prepare(
241
+ "SELECT 1 FROM main.roadmap_phases WHERE decision_name = 'legacy_v1_9' LIMIT 1"
242
+ ).get();
243
+ if (!legacyExists) {
244
+ const v9Roots = db.prepare(
245
+ "SELECT id, text, status, position FROM main.todos WHERE parent_id IS NULL AND id IN (SELECT DISTINCT parent_id FROM main.todos WHERE parent_id IS NOT NULL) ORDER BY position, id"
246
+ ).all();
247
+ if (v9Roots.length > 0) {
248
+ const insertPhase = db.prepare(
249
+ "INSERT INTO main.roadmap_phases (decision_name, phase_number, parent_phase_id, text, status, position) VALUES (?, ?, ?, ?, ?, ?)"
250
+ );
251
+ const oldToNew = new Map();
252
+ let phaseNum = 1;
253
+ for (const root of v9Roots) {
254
+ const r = insertPhase.run('legacy_v1_9', phaseNum++, null, root.text, root.status, root.position);
255
+ oldToNew.set(root.id, r.lastInsertRowid);
256
+ }
257
+ // Sub-tasks: keep one level (v1.9.0's nesting was usually one deep)
258
+ const v9Children = db.prepare(
259
+ "SELECT id, parent_id, text, status, position FROM main.todos WHERE parent_id IS NOT NULL ORDER BY position, id"
260
+ ).all();
261
+ for (const c of v9Children) {
262
+ const newParent = oldToNew.get(c.parent_id);
263
+ if (newParent == null) continue;
264
+ insertPhase.run(null, null, newParent, c.text, c.status, c.position);
265
+ }
266
+ // If old cursor pointed at a migrated todo, repoint it to the new phase id.
267
+ const oldCursor = db.prepare('SELECT todo_id FROM main.roadmap_cursor WHERE singleton = 1').get();
268
+ if (oldCursor?.todo_id != null && oldToNew.has(oldCursor.todo_id)) {
269
+ db.prepare('UPDATE main.roadmap_cursor SET phase_id = ? WHERE singleton = 1').run(oldToNew.get(oldCursor.todo_id));
270
+ }
271
+ }
272
+ }
273
+ } catch (e) {
274
+ process.stderr.write(`[cairn] v1.9.0→v1.10.0 roadmap migration skipped: ${e.message}\n`);
275
+ }
218
276
  mountParentSubIndexes(db);
219
277
  refreshFederatedViews(db);
220
278
  return db;
package/src/tools/memo.js CHANGED
@@ -5,6 +5,103 @@ import { getMemoryDir } from '../graph/cwd.js';
5
5
  export const TRANSFERABLE_TYPES = ['preference', 'experience'];
6
6
  export const MEMORY_TYPES = ['preference', 'experience', 'decision', 'knowledge'];
7
7
 
8
+ // Parse a decision memo's body for a `## Phases` section. Returns
9
+ // [{ phase_number, text, body }, ...]. Returns [] if no Phases section.
10
+ // Phase headers must look like: ### Phase N: title text
11
+ // Body is lines following the header until next ### Phase or ## heading.
12
+ // Duplicate phase_numbers: first wins, subsequent dropped (caller should warn).
13
+ export function parsePhases(content) {
14
+ if (!content || typeof content !== 'string') return [];
15
+ const lines = content.split(/\r?\n/);
16
+ let inPhasesSection = false;
17
+ let current = null;
18
+ const phases = [];
19
+ const seen = new Set();
20
+
21
+ for (const line of lines) {
22
+ // Section heading detection
23
+ if (/^##\s+Phases\s*$/i.test(line)) { inPhasesSection = true; continue; }
24
+ if (/^##\s+/.test(line) && !/^###/.test(line)) {
25
+ // Any other ## heading ends the Phases section
26
+ if (current) { phases.push(current); current = null; }
27
+ inPhasesSection = false;
28
+ continue;
29
+ }
30
+ if (!inPhasesSection) continue;
31
+
32
+ // Phase header: ### Phase N: title
33
+ const phaseMatch = /^###\s+Phase\s+(\d+)\s*:\s*(.+?)\s*$/.exec(line);
34
+ if (phaseMatch) {
35
+ if (current) phases.push(current);
36
+ const num = parseInt(phaseMatch[1], 10);
37
+ const text = phaseMatch[2];
38
+ if (seen.has(num)) {
39
+ process.stderr.write(`[cairn] memo phase parser: duplicate Phase ${num} ignored\n`);
40
+ current = null;
41
+ continue;
42
+ }
43
+ seen.add(num);
44
+ current = { phase_number: num, text, bodyLines: [] };
45
+ continue;
46
+ }
47
+ // Accumulate body
48
+ if (current) current.bodyLines.push(line);
49
+ }
50
+ if (current) phases.push(current);
51
+
52
+ return phases.map(p => ({
53
+ phase_number: p.phase_number,
54
+ text: p.text,
55
+ body: p.bodyLines.join('\n').trim() || null,
56
+ }));
57
+ }
58
+
59
+ // Sync parsed phases to roadmap_phases for a given decision.
60
+ // Re-edit semantics:
61
+ // - Phase present in memo + DB: update text/body, preserve status/position
62
+ // - Phase added (in memo, not in DB): insert as 'open' with next position
63
+ // - Phase removed (in DB, not in memo): mark status='stale' (preserve work history)
64
+ // Sub-tasks (parent_phase_id NOT NULL, decision_name NULL) are ad-hoc and
65
+ // untouched by this sync — they live and die with the parent phase via FK cascade.
66
+ export function syncPhasesToRoadmap(db, decisionName, phases) {
67
+ const existing = db.prepare(
68
+ 'SELECT id, phase_number, status FROM main.roadmap_phases WHERE decision_name = ? ORDER BY phase_number'
69
+ ).all(decisionName);
70
+ const existingByNum = new Map(existing.map(p => [p.phase_number, p]));
71
+ const seenNums = new Set();
72
+
73
+ const update = db.prepare(
74
+ 'UPDATE main.roadmap_phases SET text = ?, body = ?, status = CASE WHEN status = ? THEN ? ELSE status END WHERE id = ?'
75
+ );
76
+ const insert = db.prepare(
77
+ 'INSERT INTO main.roadmap_phases (decision_name, phase_number, parent_phase_id, text, body, status, position) VALUES (?, ?, NULL, ?, ?, ?, ?)'
78
+ );
79
+ const markStale = db.prepare("UPDATE main.roadmap_phases SET status = 'stale' WHERE id = ?");
80
+
81
+ for (let i = 0; i < phases.length; i++) {
82
+ const p = phases[i];
83
+ seenNums.add(p.phase_number);
84
+ const existingRow = existingByNum.get(p.phase_number);
85
+ if (existingRow) {
86
+ // Re-activate stale phases on re-add (text matched by phase_number, so it's
87
+ // the "same" phase being restored).
88
+ update.run(p.text, p.body, 'stale', 'open', existingRow.id);
89
+ } else {
90
+ insert.run(decisionName, p.phase_number, p.text, p.body, 'open', i);
91
+ }
92
+ }
93
+ for (const [num, row] of existingByNum) {
94
+ if (!seenNums.has(num) && row.status !== 'stale') {
95
+ markStale.run(row.id);
96
+ }
97
+ }
98
+ return {
99
+ updated: phases.filter(p => existingByNum.has(p.phase_number)).length,
100
+ inserted: phases.filter(p => !existingByNum.has(p.phase_number)).length,
101
+ staled: existing.filter(r => !seenNums.has(r.phase_number) && r.status !== 'stale').length,
102
+ };
103
+ }
104
+
8
105
  function slugify(name) {
9
106
  return name.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_|_$/g, '');
10
107
  }
@@ -60,7 +157,7 @@ export function loadPreferenceMemories(memoryDir) {
60
157
  .filter(Boolean);
61
158
  }
62
159
 
63
- export function memo(_db, { action, name, type, content, description = '', origin }) {
160
+ export function memo(db, { action, name, type, content, description = '', origin }) {
64
161
  const memoryDir = getMemoryDir();
65
162
 
66
163
  if (action === 'list') {
@@ -113,6 +210,14 @@ export function memo(_db, { action, name, type, content, description = '', origi
113
210
  const filePath = path.join(memoryDir, removed.file);
114
211
  if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
115
212
  writeIndex(memoryDir, entries);
213
+ // If this was a decision memo, mark all its roadmap phases as stale (preserve history).
214
+ if (removed.type === 'decision' && db) {
215
+ try {
216
+ db.prepare(
217
+ "UPDATE main.roadmap_phases SET status = 'stale' WHERE decision_name = ? AND status != 'stale'"
218
+ ).run(removed.name);
219
+ } catch {}
220
+ }
116
221
  return { content: [{ type: 'text', text: JSON.stringify({ deleted: true, name }, null, 2) }] };
117
222
  }
118
223
 
@@ -153,6 +258,19 @@ export function memo(_db, { action, name, type, content, description = '', origi
153
258
  else entries.push(entry);
154
259
  writeIndex(memoryDir, entries);
155
260
 
261
+ // Decision memos with a `## Phases` section seed/update the project roadmap.
262
+ let phaseSync = null;
263
+ if (type === 'decision' && db) {
264
+ try {
265
+ const phases = parsePhases(content);
266
+ if (phases.length > 0) {
267
+ phaseSync = syncPhasesToRoadmap(db, name, phases);
268
+ }
269
+ } catch (e) {
270
+ process.stderr.write(`[cairn] phase sync failed for ${name}: ${e.message}\n`);
271
+ }
272
+ }
273
+
156
274
  return {
157
275
  content: [{
158
276
  type: 'text',
@@ -162,6 +280,7 @@ export function memo(_db, { action, name, type, content, description = '', origi
162
280
  type,
163
281
  file: filePath,
164
282
  action: isUpdate ? 'updated' : 'created',
283
+ ...(phaseSync ? { phase_sync: phaseSync } : {}),
165
284
  }, null, 2),
166
285
  }],
167
286
  };
@@ -0,0 +1,302 @@
1
+ // cairn_roadmap — active project plan with cursor + decision-memo backed phases.
2
+ // Phases enter the roadmap only via decision memos (see memo.js syncPhasesToRoadmap).
3
+ // Sub-tasks under a phase are ad-hoc, added via `add --parent <phase_id>`.
4
+
5
+ const STATUS_BOX = {
6
+ open: '[ ]',
7
+ in_progress: '[~]',
8
+ done: '[x]',
9
+ blocked: '[!]',
10
+ stale: '[s]',
11
+ };
12
+
13
+ const VALID_STATUS = new Set(['open', 'in_progress', 'done', 'blocked', 'stale']);
14
+ const ACTIONABLE = new Set(['open', 'in_progress']);
15
+
16
+ function getCursor(db) {
17
+ const row = db.prepare('SELECT phase_id FROM main.roadmap_cursor WHERE singleton = 1').get();
18
+ return row?.phase_id ?? null;
19
+ }
20
+
21
+ function setCursor(db, phaseId) {
22
+ db.prepare(
23
+ 'INSERT INTO main.roadmap_cursor (singleton, phase_id) VALUES (1, ?) ON CONFLICT(singleton) DO UPDATE SET phase_id = excluded.phase_id'
24
+ ).run(phaseId);
25
+ }
26
+
27
+ function clearCursor(db) {
28
+ db.prepare('DELETE FROM main.roadmap_cursor WHERE singleton = 1').run();
29
+ }
30
+
31
+ // Mark ancestor phases (parent chain) as in_progress, but only if currently 'open'.
32
+ function propagateInProgressUp(db, phaseId) {
33
+ const stmt = db.prepare('SELECT parent_phase_id, status FROM main.roadmap_phases WHERE id = ?');
34
+ const update = db.prepare("UPDATE main.roadmap_phases SET status = 'in_progress' WHERE id = ? AND status = 'open'");
35
+ let cur = stmt.get(phaseId);
36
+ while (cur && cur.parent_phase_id != null) {
37
+ update.run(cur.parent_phase_id);
38
+ cur = stmt.get(cur.parent_phase_id);
39
+ }
40
+ }
41
+
42
+ // Move cursor to phaseId, promote it to in_progress (if open), propagate up.
43
+ function activatePhase(db, phaseId) {
44
+ setCursor(db, phaseId);
45
+ db.prepare("UPDATE main.roadmap_phases SET status = 'in_progress' WHERE id = ? AND status = 'open'").run(phaseId);
46
+ propagateInProgressUp(db, phaseId);
47
+ }
48
+
49
+ // Compute a synthetic "decision-root" status from its phases.
50
+ // done if all non-stale phases done; in_progress if any in_progress; blocked if any blocked & no in_progress; stale if all stale; else open.
51
+ function decisionRootStatus(phases) {
52
+ const live = phases.filter(p => p.status !== 'stale');
53
+ if (live.length === 0) return 'stale';
54
+ if (live.every(p => p.status === 'done')) return 'done';
55
+ if (live.some(p => p.status === 'in_progress')) return 'in_progress';
56
+ if (live.some(p => p.status === 'blocked') && !live.some(p => p.status === 'in_progress')) return 'blocked';
57
+ return 'open';
58
+ }
59
+
60
+ // DFS-advance the cursor to the next actionable phase, skipping stale.
61
+ // Order within a decision: phase_number ascending; within a phase: sub-tasks first
62
+ // (parent_phase_id = phase_id, ordered by position), then next phase_number.
63
+ // Across decisions: by decision_name (stable alphabetical order — could be improved later).
64
+ function findNextActionable(db, fromPhaseId) {
65
+ const all = db.prepare(
66
+ 'SELECT id, decision_name, phase_number, parent_phase_id, status, position FROM main.roadmap_phases ORDER BY decision_name, phase_number, position, id'
67
+ ).all();
68
+ const isLive = (p) => ACTIONABLE.has(p.status);
69
+
70
+ // Build a flat DFS order: for each top-level phase (parent_phase_id null), visit its sub-tasks
71
+ // (parent_phase_id = phase.id) before the next top-level phase.
72
+ const childrenOf = new Map();
73
+ for (const p of all) {
74
+ const key = p.parent_phase_id ?? `root:${p.decision_name}`;
75
+ if (!childrenOf.has(key)) childrenOf.set(key, []);
76
+ childrenOf.get(key).push(p);
77
+ }
78
+ // Sort each child group
79
+ for (const arr of childrenOf.values()) {
80
+ arr.sort((a, b) => {
81
+ if (a.phase_number != null && b.phase_number != null) return a.phase_number - b.phase_number;
82
+ return a.position - b.position || a.id - b.id;
83
+ });
84
+ }
85
+
86
+ const order = [];
87
+ const decisionsSeen = new Set();
88
+ for (const p of all) {
89
+ if (p.parent_phase_id != null) continue;
90
+ if (decisionsSeen.has(p.decision_name)) continue;
91
+ decisionsSeen.add(p.decision_name);
92
+ const stack = [];
93
+ const phasesInDecision = childrenOf.get(`root:${p.decision_name}`) || [];
94
+ for (let i = phasesInDecision.length - 1; i >= 0; i--) stack.push(phasesInDecision[i]);
95
+ while (stack.length) {
96
+ const node = stack.pop();
97
+ order.push(node);
98
+ const kids = childrenOf.get(node.id) || [];
99
+ for (let i = kids.length - 1; i >= 0; i--) stack.push(kids[i]);
100
+ }
101
+ }
102
+
103
+ if (fromPhaseId == null) {
104
+ return order.find(isLive)?.id ?? null;
105
+ }
106
+ const idx = order.findIndex(p => p.id === fromPhaseId);
107
+ if (idx === -1) return order.find(isLive)?.id ?? null;
108
+ for (let i = idx + 1; i < order.length; i++) {
109
+ if (isLive(order[i])) return order[i].id;
110
+ }
111
+ return null;
112
+ }
113
+
114
+ function renderTree(db, { compact = false, hideStale = false } = {}) {
115
+ const all = db.prepare(
116
+ 'SELECT id, decision_name, phase_number, parent_phase_id, text, status, position FROM main.roadmap_phases ORDER BY decision_name, phase_number, position, id'
117
+ ).all();
118
+ if (all.length === 0) return '(no roadmap — write a decision memo with a `## Phases` section)';
119
+
120
+ const cursor = getCursor(db);
121
+ const byParent = new Map();
122
+ for (const p of all) {
123
+ const key = p.parent_phase_id ?? 'root';
124
+ if (!byParent.has(key)) byParent.set(key, []);
125
+ byParent.get(key).push(p);
126
+ }
127
+ for (const arr of byParent.values()) {
128
+ arr.sort((a, b) => {
129
+ if (a.decision_name && b.decision_name && a.decision_name !== b.decision_name) {
130
+ return a.decision_name.localeCompare(b.decision_name);
131
+ }
132
+ if (a.phase_number != null && b.phase_number != null) return a.phase_number - b.phase_number;
133
+ return a.position - b.position || a.id - b.id;
134
+ });
135
+ }
136
+
137
+ // Group root nodes by decision_name. Roots always have decision_name (they're memo phases).
138
+ const roots = byParent.get('root') || [];
139
+ const decisionRoots = new Map();
140
+ for (const r of roots) {
141
+ if (r.decision_name == null) continue; // safety: shouldn't happen
142
+ if (!decisionRoots.has(r.decision_name)) decisionRoots.set(r.decision_name, []);
143
+ decisionRoots.get(r.decision_name).push(r);
144
+ }
145
+
146
+ const lines = [];
147
+ const visitSubtree = (parentId, depth) => {
148
+ const kids = byParent.get(parentId) || [];
149
+ for (const p of kids) {
150
+ if (hideStale && p.status === 'stale') continue;
151
+ const box = STATUS_BOX[p.status] || '[?]';
152
+ const arrow = cursor === p.id ? ' →' : '';
153
+ const label = p.phase_number != null ? `Phase ${p.phase_number}: ${p.text}` : p.text;
154
+ const tag = compact ? '' : ` (id:${p.id})`;
155
+ lines.push(`${' '.repeat(depth)}${box}${arrow} ${label}${tag}`);
156
+ visitSubtree(p.id, depth + 1);
157
+ }
158
+ };
159
+
160
+ for (const [decisionName, phases] of decisionRoots) {
161
+ if (hideStale && phases.every(p => p.status === 'stale')) continue;
162
+ const rootStatus = decisionRootStatus(phases);
163
+ lines.push(`${STATUS_BOX[rootStatus]} Decision: ${decisionName}`);
164
+ for (const p of phases) {
165
+ if (hideStale && p.status === 'stale') continue;
166
+ const box = STATUS_BOX[p.status] || '[?]';
167
+ const arrow = cursor === p.id ? ' →' : '';
168
+ const label = `Phase ${p.phase_number}: ${p.text}`;
169
+ const tag = compact ? '' : ` (id:${p.id})`;
170
+ lines.push(` ${box}${arrow} ${label}${tag}`);
171
+ visitSubtree(p.id, 2);
172
+ }
173
+ }
174
+ return lines.join('\n');
175
+ }
176
+
177
+ function statusSummary(db) {
178
+ const all = db.prepare(
179
+ "SELECT decision_name, status FROM main.roadmap_phases WHERE parent_phase_id IS NULL"
180
+ ).all();
181
+ const byDecision = new Map();
182
+ for (const p of all) {
183
+ if (!byDecision.has(p.decision_name)) byDecision.set(p.decision_name, []);
184
+ byDecision.get(p.decision_name).push(p);
185
+ }
186
+ let inFlight = 0;
187
+ for (const phases of byDecision.values()) {
188
+ const root = decisionRootStatus(phases);
189
+ if (root === 'in_progress' || root === 'open') inFlight++;
190
+ }
191
+ const cursor = getCursor(db);
192
+ let currentLabel = null;
193
+ if (cursor != null) {
194
+ const cur = db.prepare('SELECT id, decision_name, phase_number, parent_phase_id, text FROM main.roadmap_phases WHERE id = ?').get(cursor);
195
+ if (cur) {
196
+ // Walk up to find the root phase if this is a sub-task
197
+ let rootDecision = cur.decision_name;
198
+ let rootPhaseNum = cur.phase_number;
199
+ let walker = cur;
200
+ while (walker.parent_phase_id != null) {
201
+ walker = db.prepare('SELECT id, decision_name, phase_number, parent_phase_id FROM main.roadmap_phases WHERE id = ?').get(walker.parent_phase_id);
202
+ if (!walker) break;
203
+ rootDecision = walker.decision_name ?? rootDecision;
204
+ rootPhaseNum = walker.phase_number ?? rootPhaseNum;
205
+ }
206
+ currentLabel = cur.phase_number != null
207
+ ? `${cur.decision_name} / Phase ${cur.phase_number} (${cur.text})`
208
+ : `${rootDecision} / Phase ${rootPhaseNum} → sub: ${cur.text}`;
209
+ }
210
+ }
211
+ return {
212
+ decisions_total: byDecision.size,
213
+ decisions_in_flight: inFlight,
214
+ cursor_phase_id: cursor,
215
+ current: currentLabel,
216
+ };
217
+ }
218
+
219
+ export function roadmap(db, args = {}) {
220
+ const action = args.action ?? 'tree';
221
+ const { id, status, parent, text } = args;
222
+
223
+ switch (action) {
224
+ case 'tree': {
225
+ const out = renderTree(db, { compact: args.compact === true, hideStale: args.hide_stale === true });
226
+ const summary = statusSummary(db);
227
+ const lines = [out, '', summary.current
228
+ ? `Current: ${summary.current}`
229
+ : '(no cursor set — call cairn_roadmap focus <id>)'];
230
+ if (summary.decisions_in_flight > 1) {
231
+ lines.push(`In flight: ${summary.decisions_in_flight} decisions`);
232
+ }
233
+ return { content: [{ type: 'text', text: lines.join('\n') }] };
234
+ }
235
+
236
+ case 'status': {
237
+ return { content: [{ type: 'text', text: JSON.stringify(statusSummary(db), null, 2) }] };
238
+ }
239
+
240
+ case 'focus': {
241
+ if (!id) throw new Error('id is required for focus');
242
+ const phase = db.prepare('SELECT * FROM main.roadmap_phases WHERE id = ?').get(id);
243
+ if (!phase) throw new Error(`No phase with id ${id}`);
244
+ if (phase.status === 'stale') throw new Error(`Phase ${id} is stale — restore via decision memo edit`);
245
+ activatePhase(db, id);
246
+ const refreshed = db.prepare('SELECT * FROM main.roadmap_phases WHERE id = ?').get(id);
247
+ return { content: [{ type: 'text', text: JSON.stringify({ focused: refreshed }, null, 2) }] };
248
+ }
249
+
250
+ case 'next': {
251
+ const cursor = getCursor(db);
252
+ const next = findNextActionable(db, cursor);
253
+ if (next == null) {
254
+ clearCursor(db);
255
+ return { content: [{ type: 'text', text: JSON.stringify({ cursor: null, message: 'No actionable phases remaining.' }, null, 2) }] };
256
+ }
257
+ activatePhase(db, next);
258
+ const phase = db.prepare('SELECT * FROM main.roadmap_phases WHERE id = ?').get(next);
259
+ return { content: [{ type: 'text', text: JSON.stringify({ cursor: next, phase }, null, 2) }] };
260
+ }
261
+
262
+ case 'set_status': {
263
+ if (!id) throw new Error('id is required for set_status');
264
+ if (!status) throw new Error('status is required for set_status');
265
+ if (!VALID_STATUS.has(status)) {
266
+ throw new Error(`Invalid status "${status}". Use: ${[...VALID_STATUS].join(', ')}`);
267
+ }
268
+ const resolvedAt = status === 'done' ? "datetime('now')" : 'NULL';
269
+ const updated = db.prepare(
270
+ `UPDATE main.roadmap_phases SET status = ?, resolved_at = ${resolvedAt} WHERE id = ?`
271
+ ).run(status, id);
272
+ if (updated.changes === 0) throw new Error(`No phase with id ${id}`);
273
+ // If we just marked the cursor done, auto-advance and activate the new node.
274
+ let advancedTo = null;
275
+ if (status === 'done' && getCursor(db) === id) {
276
+ const next = findNextActionable(db, id);
277
+ if (next != null) { activatePhase(db, next); advancedTo = next; }
278
+ else clearCursor(db);
279
+ }
280
+ const phase = db.prepare('SELECT * FROM main.roadmap_phases WHERE id = ?').get(id);
281
+ return { content: [{ type: 'text', text: JSON.stringify({ updated: phase, cursor_advanced_to: advancedTo }, null, 2) }] };
282
+ }
283
+
284
+ case 'add': {
285
+ if (!text) throw new Error('text is required for add');
286
+ if (parent == null) throw new Error('parent (phase_id) is required — ad-hoc sub-tasks must hang under an existing phase. For freestanding work use cairn_todos add.');
287
+ const parentRow = db.prepare('SELECT id, decision_name FROM main.roadmap_phases WHERE id = ?').get(parent);
288
+ if (!parentRow) throw new Error(`Parent phase ${parent} not found`);
289
+ const nextPos = db.prepare(
290
+ 'SELECT COALESCE(MAX(position), -1) + 1 AS n FROM main.roadmap_phases WHERE parent_phase_id = ?'
291
+ ).get(parent).n;
292
+ const result = db.prepare(
293
+ 'INSERT INTO main.roadmap_phases (decision_name, phase_number, parent_phase_id, text, status, position) VALUES (NULL, NULL, ?, ?, ?, ?)'
294
+ ).run(parent, text, 'open', nextPos);
295
+ const inserted = db.prepare('SELECT * FROM main.roadmap_phases WHERE id = ?').get(result.lastInsertRowid);
296
+ return { content: [{ type: 'text', text: JSON.stringify({ added: inserted, inherits_decision_from: parentRow.decision_name }, null, 2) }] };
297
+ }
298
+
299
+ default:
300
+ throw new Error(`Unknown action: "${action}". Use tree, status, focus, next, set_status, or add.`);
301
+ }
302
+ }
@@ -1,113 +1,12 @@
1
1
  import { walkRepo } from '../indexer/fileWalker.js';
2
2
  import { scanTodos } from '../indexer/todoScanner.js';
3
3
 
4
- const STATUS_BOX = {
5
- open: '[ ]',
6
- in_progress: '[~]',
7
- done: '[x]',
8
- blocked: '[!]',
9
- };
10
-
11
- const VALID_STATUS = new Set(['open', 'in_progress', 'done', 'blocked']);
12
-
13
- function getCursor(db) {
14
- const row = db.prepare('SELECT todo_id FROM main.roadmap_cursor WHERE singleton = 1').get();
15
- return row?.todo_id ?? null;
16
- }
17
-
18
- function setCursor(db, todoId) {
19
- db.prepare('INSERT OR REPLACE INTO main.roadmap_cursor (singleton, todo_id) VALUES (1, ?)').run(todoId);
20
- }
21
-
22
- function clearCursor(db) {
23
- db.prepare('DELETE FROM main.roadmap_cursor WHERE singleton = 1').run();
24
- }
25
-
26
- // Mark ancestors as in_progress (only if currently 'open' — don't downgrade done/blocked).
27
- function propagateInProgressUp(db, todoId) {
28
- const stmt = db.prepare('SELECT parent_id, status FROM main.todos WHERE id = ?');
29
- const update = db.prepare("UPDATE main.todos SET status = 'in_progress' WHERE id = ? AND status = 'open'");
30
- let cur = stmt.get(todoId);
31
- while (cur && cur.parent_id != null) {
32
- update.run(cur.parent_id);
33
- cur = stmt.get(cur.parent_id);
34
- }
35
- }
36
-
37
- // DFS-find next actionable node starting at/after `fromId`.
38
- // Order: first open child → next open sibling → next open uncle (walk up).
39
- function findNextOpen(db, fromId) {
40
- const childOf = (pid) => db.prepare(
41
- `SELECT id FROM main.todos
42
- WHERE ${pid == null ? 'parent_id IS NULL' : 'parent_id = ?'}
43
- AND status IN ('open', 'in_progress')
44
- ORDER BY position, id`
45
- ).all(...(pid == null ? [] : [pid]));
46
-
47
- if (fromId == null) {
48
- const roots = childOf(null);
49
- return roots[0]?.id ?? null;
50
- }
51
-
52
- const children = childOf(fromId);
53
- if (children.length > 0) return children[0].id;
54
-
55
- let curId = fromId;
56
- while (curId != null) {
57
- const cur = db.prepare('SELECT parent_id, position FROM main.todos WHERE id = ?').get(curId);
58
- if (!cur) return null;
59
- const sibling = db.prepare(
60
- `SELECT id FROM main.todos
61
- WHERE ${cur.parent_id == null ? 'parent_id IS NULL' : 'parent_id = ?'}
62
- AND (position > ? OR (position = ? AND id > ?))
63
- AND status IN ('open', 'in_progress')
64
- ORDER BY position, id LIMIT 1`
65
- ).get(...(cur.parent_id == null ? [cur.position, cur.position, curId] : [cur.parent_id, cur.position, cur.position, curId]));
66
- if (sibling) return sibling.id;
67
- curId = cur.parent_id;
68
- }
69
- return null;
70
- }
71
-
72
- function renderTree(db, { compact = false } = {}) {
73
- const all = db.prepare(
74
- 'SELECT id, parent_id, position, status, kind, text FROM main.todos ORDER BY IFNULL(parent_id, -1), position, id'
75
- ).all();
76
- if (all.length === 0) return '(no todos)';
77
-
78
- const cursor = getCursor(db);
79
- const childrenByParent = new Map();
80
- for (const t of all) {
81
- const key = t.parent_id ?? null;
82
- if (!childrenByParent.has(key)) childrenByParent.set(key, []);
83
- childrenByParent.get(key).push(t);
84
- }
85
-
86
- const lines = [];
87
- const visit = (parentId, depth) => {
88
- const kids = childrenByParent.get(parentId) || [];
89
- for (const t of kids) {
90
- const box = STATUS_BOX[t.status] || '[?]';
91
- const arrow = cursor === t.id ? ' →' : '';
92
- const tag = compact ? '' : ` (id:${t.id}${t.kind !== 'TODO' ? `, ${t.kind}` : ''})`;
93
- lines.push(`${' '.repeat(depth)}${box}${arrow} ${t.text}${tag}`);
94
- visit(t.id, depth + 1);
95
- }
96
- };
97
- visit(null, 0);
98
- return lines.join('\n');
99
- }
100
-
101
- function nextPosition(db, parentId) {
102
- const row = db.prepare(
103
- `SELECT COALESCE(MAX(position), -1) + 1 AS next FROM main.todos
104
- WHERE ${parentId == null ? 'parent_id IS NULL' : 'parent_id = ?'}`
105
- ).get(...(parentId == null ? [] : [parentId]));
106
- return row.next;
107
- }
108
-
4
+ // cairn_todos is the long-tail backlog: scanned TODO/FIXME/HACK comments + manual notes.
5
+ // For active multi-phase plans (with status/cursor/decision-memo), use cairn_roadmap instead.
6
+ // The parent_id/position columns added in v1.9.0 are dormant — kept on the table for back-compat
7
+ // but no longer read by tools.
109
8
  export async function todos(db, args = {}) {
110
- const { text, id, status, source, parent, position } = args;
9
+ const { text, id, status, source } = args;
111
10
  const action = args.action ?? 'list';
112
11
  const kind = args.kind;
113
12
 
@@ -118,9 +17,9 @@ export async function todos(db, args = {}) {
118
17
  if (status) { query += ' AND status = ?'; params.push(status); }
119
18
  if (source) { query += ' AND source = ?'; params.push(source); }
120
19
  if (kind) { query += ' AND kind = ?'; params.push(kind.toUpperCase()); }
121
- query += ' ORDER BY CASE status WHEN \'open\' THEN 0 WHEN \'in_progress\' THEN 0 ELSE 1 END, source, file, line';
20
+ query += ' ORDER BY CASE status WHEN \'open\' THEN 0 ELSE 1 END, source, file, line';
122
21
  const rows = db.prepare(query).all(...params);
123
- const open = rows.filter(r => r.status === 'open' || r.status === 'in_progress').length;
22
+ const open = rows.filter(r => r.status === 'open').length;
124
23
  const done = rows.filter(r => r.status === 'done').length;
125
24
  return {
126
25
  content: [{
@@ -130,34 +29,12 @@ export async function todos(db, args = {}) {
130
29
  };
131
30
  }
132
31
 
133
- case 'tree': {
134
- const compact = args.compact === true;
135
- const tree = renderTree(db, { compact });
136
- const cursor = getCursor(db);
137
- const cursorRow = cursor ? db.prepare('SELECT id, text FROM main.todos WHERE id = ?').get(cursor) : null;
138
- const next = findNextOpen(db, cursor);
139
- const nextRow = next ? db.prepare('SELECT id, text FROM main.todos WHERE id = ?').get(next) : null;
140
- const summary = [
141
- cursorRow ? `Current: (id:${cursorRow.id}) ${cursorRow.text}` : 'Current: (no cursor set)',
142
- nextRow && (!cursorRow || nextRow.id !== cursorRow.id) ? `Next: (id:${nextRow.id}) ${nextRow.text}` : '',
143
- ].filter(Boolean).join('\n');
144
- return {
145
- content: [{ type: 'text', text: `${tree}\n\n${summary}` }],
146
- };
147
- }
148
-
149
32
  case 'add': {
150
33
  if (!text) throw new Error('text is required for add action');
151
34
  const todoKind = (kind || 'TODO').toUpperCase();
152
- const parentId = parent ?? null;
153
- if (parentId != null) {
154
- const exists = db.prepare('SELECT id FROM main.todos WHERE id = ?').get(parentId);
155
- if (!exists) throw new Error(`Parent todo id ${parentId} does not exist`);
156
- }
157
- const pos = position ?? nextPosition(db, parentId);
158
35
  const result = db.prepare(
159
- 'INSERT INTO main.todos (source, kind, text, parent_id, position) VALUES (?, ?, ?, ?, ?)'
160
- ).run('manual', todoKind, text, parentId, pos);
36
+ 'INSERT INTO main.todos (source, kind, text) VALUES (?, ?, ?)'
37
+ ).run('manual', todoKind, text);
161
38
  const todo = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(result.lastInsertRowid);
162
39
  return {
163
40
  content: [{ type: 'text', text: JSON.stringify({ added: todo }, null, 2) }],
@@ -170,63 +47,9 @@ export async function todos(db, args = {}) {
170
47
  'UPDATE main.todos SET status = ?, resolved_at = datetime(\'now\') WHERE id = ?'
171
48
  ).run('done', id);
172
49
  if (updated.changes === 0) throw new Error(`No todo found with id ${id}`);
173
- const cursor = getCursor(db);
174
- let advanced = null;
175
- if (cursor === id) {
176
- const next = findNextOpen(db, id);
177
- if (next) { setCursor(db, next); advanced = next; }
178
- else clearCursor(db);
179
- }
180
- const todo = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(id);
181
- return {
182
- content: [{ type: 'text', text: JSON.stringify({ resolved: todo, cursor_advanced_to: advanced }, null, 2) }],
183
- };
184
- }
185
-
186
- case 'focus': {
187
- if (!id) throw new Error('id is required for focus action');
188
- const todo = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(id);
189
- if (!todo) throw new Error(`No todo found with id ${id}`);
190
- setCursor(db, id);
191
- db.prepare("UPDATE main.todos SET status = 'in_progress' WHERE id = ? AND status = 'open'").run(id);
192
- propagateInProgressUp(db, id);
193
- const refreshed = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(id);
194
- return {
195
- content: [{ type: 'text', text: JSON.stringify({ focused: refreshed }, null, 2) }],
196
- };
197
- }
198
-
199
- case 'next': {
200
- const cursor = getCursor(db);
201
- const next = findNextOpen(db, cursor);
202
- if (next == null) {
203
- clearCursor(db);
204
- return {
205
- content: [{ type: 'text', text: JSON.stringify({ cursor: null, message: 'No open todos remaining.' }, null, 2) }],
206
- };
207
- }
208
- setCursor(db, next);
209
- propagateInProgressUp(db, next);
210
- const todo = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(next);
211
- return {
212
- content: [{ type: 'text', text: JSON.stringify({ cursor: next, todo }, null, 2) }],
213
- };
214
- }
215
-
216
- case 'set_status': {
217
- if (!id) throw new Error('id is required for set_status action');
218
- if (!status) throw new Error('status is required for set_status action');
219
- if (!VALID_STATUS.has(status)) {
220
- throw new Error(`Invalid status "${status}". Use: open, in_progress, done, blocked`);
221
- }
222
- const resolvedAt = status === 'done' ? "datetime('now')" : 'NULL';
223
- const updated = db.prepare(
224
- `UPDATE main.todos SET status = ?, resolved_at = ${resolvedAt} WHERE id = ?`
225
- ).run(status, id);
226
- if (updated.changes === 0) throw new Error(`No todo found with id ${id}`);
227
50
  const todo = db.prepare('SELECT * FROM main.todos WHERE id = ?').get(id);
228
51
  return {
229
- content: [{ type: 'text', text: JSON.stringify({ updated: todo }, null, 2) }],
52
+ content: [{ type: 'text', text: JSON.stringify({ resolved: todo }, null, 2) }],
230
53
  };
231
54
  }
232
55
 
@@ -234,7 +57,7 @@ export async function todos(db, args = {}) {
234
57
  const files = await walkRepo(process.cwd());
235
58
  const found = await scanTodos(db, files);
236
59
  const open = db.prepare(
237
- 'SELECT COUNT(*) AS n FROM main.todos WHERE status IN (\'open\', \'in_progress\')'
60
+ 'SELECT COUNT(*) AS n FROM main.todos WHERE status = \'open\''
238
61
  ).get().n;
239
62
  return {
240
63
  content: [{
@@ -245,6 +68,6 @@ export async function todos(db, args = {}) {
245
68
  }
246
69
 
247
70
  default:
248
- throw new Error(`Unknown action: "${action}". Use list, tree, add, resolve, focus, next, set_status, or scan.`);
71
+ throw new Error(`Unknown action: "${action}". Use list, add, resolve, or scan.`);
249
72
  }
250
73
  }
@@ -0,0 +1,196 @@
1
+ import { strict as assert } from 'node:assert';
2
+ import { test } from 'node:test';
3
+ import { DatabaseSync } from 'node:sqlite';
4
+ import { parsePhases, syncPhasesToRoadmap } from '../src/tools/memo.js';
5
+ import { roadmap } from '../src/tools/roadmap.js';
6
+
7
+ // Build a fresh in-memory DB with the v1.10.0 schema (mirrors db.js SCHEMA, scoped).
8
+ function makeDB() {
9
+ const db = new DatabaseSync(':memory:');
10
+ db.exec(`
11
+ CREATE TABLE todos (
12
+ id INTEGER PRIMARY KEY AUTOINCREMENT, source TEXT, file TEXT, line INTEGER,
13
+ kind TEXT, text TEXT, status TEXT DEFAULT 'open', parent_id INTEGER, position INTEGER DEFAULT 0,
14
+ created_at TEXT DEFAULT (datetime('now')), resolved_at TEXT
15
+ );
16
+ CREATE TABLE roadmap_cursor (
17
+ singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
18
+ todo_id INTEGER, phase_id INTEGER
19
+ );
20
+ CREATE TABLE roadmap_phases (
21
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
22
+ decision_name TEXT, phase_number INTEGER, parent_phase_id INTEGER REFERENCES roadmap_phases(id) ON DELETE CASCADE,
23
+ text TEXT NOT NULL, body TEXT, status TEXT DEFAULT 'open', position INTEGER DEFAULT 0,
24
+ created_at TEXT DEFAULT (datetime('now')), resolved_at TEXT
25
+ );
26
+ CREATE UNIQUE INDEX idx_roadmap_phases_decision_phase
27
+ ON roadmap_phases(decision_name, phase_number)
28
+ WHERE decision_name IS NOT NULL AND phase_number IS NOT NULL;
29
+ `);
30
+ return db;
31
+ }
32
+
33
+ test('parsePhases extracts ### Phase N: text + body until next ## heading', () => {
34
+ const memo = `Why explanation.
35
+
36
+ ## Phases
37
+
38
+ ### Phase 1: First
39
+ Body of first.
40
+ Multiple lines.
41
+
42
+ ### Phase 2: Second
43
+ Just one line.
44
+
45
+ ## Tradeoffs
46
+ Should not be parsed.
47
+ `;
48
+ const phases = parsePhases(memo);
49
+ assert.equal(phases.length, 2);
50
+ assert.equal(phases[0].phase_number, 1);
51
+ assert.equal(phases[0].text, 'First');
52
+ assert.equal(phases[0].body, 'Body of first.\nMultiple lines.');
53
+ assert.equal(phases[1].phase_number, 2);
54
+ assert.equal(phases[1].body, 'Just one line.');
55
+ });
56
+
57
+ test('parsePhases returns [] when no ## Phases section exists', () => {
58
+ assert.deepEqual(parsePhases('No phases here.\n\n## Other heading\nstuff'), []);
59
+ });
60
+
61
+ test('parsePhases ignores duplicate phase numbers (first wins)', () => {
62
+ const memo = `## Phases\n### Phase 1: A\n### Phase 1: B\n### Phase 2: C\n`;
63
+ const phases = parsePhases(memo);
64
+ assert.equal(phases.length, 2);
65
+ assert.equal(phases[0].text, 'A');
66
+ assert.equal(phases[1].phase_number, 2);
67
+ });
68
+
69
+ test('syncPhasesToRoadmap: insert on first, preserve status on update, stale on remove', () => {
70
+ const db = makeDB();
71
+ const phases = [
72
+ { phase_number: 1, text: 'First', body: 'b1' },
73
+ { phase_number: 2, text: 'Second', body: 'b2' },
74
+ { phase_number: 3, text: 'Third', body: 'b3' },
75
+ ];
76
+ let r = syncPhasesToRoadmap(db, 'demo', phases);
77
+ assert.deepEqual(r, { updated: 0, inserted: 3, staled: 0 });
78
+
79
+ // Mark Phase 2 as in_progress, then re-sync without Phase 1
80
+ db.prepare("UPDATE roadmap_phases SET status='in_progress' WHERE phase_number=2 AND decision_name='demo'").run();
81
+ const phases2 = [
82
+ { phase_number: 2, text: 'Second renamed', body: 'b2-new' },
83
+ { phase_number: 3, text: 'Third', body: 'b3' },
84
+ { phase_number: 4, text: 'Fourth', body: 'b4' },
85
+ ];
86
+ r = syncPhasesToRoadmap(db, 'demo', phases2);
87
+ assert.deepEqual(r, { updated: 2, inserted: 1, staled: 1 });
88
+
89
+ const all = JSON.parse(JSON.stringify(
90
+ db.prepare("SELECT phase_number, status, text FROM roadmap_phases WHERE decision_name='demo' ORDER BY phase_number").all()
91
+ ));
92
+ assert.deepEqual(all, [
93
+ { phase_number: 1, status: 'stale', text: 'First' },
94
+ { phase_number: 2, status: 'in_progress', text: 'Second renamed' },
95
+ { phase_number: 3, status: 'open', text: 'Third' },
96
+ { phase_number: 4, status: 'open', text: 'Fourth' },
97
+ ]);
98
+ });
99
+
100
+ test('syncPhasesToRoadmap: re-add of stale phase reactivates it to open', () => {
101
+ const db = makeDB();
102
+ syncPhasesToRoadmap(db, 'demo', [{ phase_number: 1, text: 'A', body: null }]);
103
+ db.prepare("UPDATE roadmap_phases SET status='stale' WHERE phase_number=1").run();
104
+ syncPhasesToRoadmap(db, 'demo', [{ phase_number: 1, text: 'A back', body: null }]);
105
+ const row = db.prepare("SELECT status, text FROM roadmap_phases WHERE phase_number=1").get();
106
+ assert.equal(row.status, 'open');
107
+ assert.equal(row.text, 'A back');
108
+ });
109
+
110
+ test('roadmap focus: marks phase in_progress + propagates up + sets cursor', () => {
111
+ const db = makeDB();
112
+ syncPhasesToRoadmap(db, 'demo', [
113
+ { phase_number: 1, text: 'A', body: null },
114
+ { phase_number: 2, text: 'B', body: null },
115
+ ]);
116
+ const phase2 = db.prepare("SELECT id FROM roadmap_phases WHERE phase_number=2").get().id;
117
+ // Add a sub-task under phase 2
118
+ const sub = JSON.parse(roadmap(db, { action: 'add', parent: phase2, text: 'subtask' }).content[0].text).added.id;
119
+ roadmap(db, { action: 'focus', id: sub });
120
+ const subAfter = db.prepare("SELECT status FROM roadmap_phases WHERE id=?").get(sub);
121
+ const phase2After = db.prepare("SELECT status FROM roadmap_phases WHERE id=?").get(phase2);
122
+ const cursor = db.prepare('SELECT phase_id FROM roadmap_cursor WHERE singleton=1').get().phase_id;
123
+ assert.equal(subAfter.status, 'in_progress');
124
+ assert.equal(phase2After.status, 'in_progress');
125
+ assert.equal(cursor, sub);
126
+ });
127
+
128
+ test('roadmap next: DFS-advances skipping stale, never landing on stale', () => {
129
+ const db = makeDB();
130
+ syncPhasesToRoadmap(db, 'demo', [
131
+ { phase_number: 1, text: 'A', body: null },
132
+ { phase_number: 2, text: 'B', body: null },
133
+ { phase_number: 3, text: 'C', body: null },
134
+ ]);
135
+ db.prepare("UPDATE roadmap_phases SET status='stale' WHERE phase_number=2").run();
136
+ // Cursor null -> next should land on Phase 1 (first actionable)
137
+ let next = JSON.parse(roadmap(db, { action: 'next' }).content[0].text);
138
+ assert.equal(next.phase.phase_number, 1);
139
+ // From Phase 1 -> next should skip stale Phase 2 and land on Phase 3
140
+ next = JSON.parse(roadmap(db, { action: 'next' }).content[0].text);
141
+ assert.equal(next.phase.phase_number, 3);
142
+ });
143
+
144
+ test('roadmap set_status done auto-advances cursor to next actionable', () => {
145
+ const db = makeDB();
146
+ syncPhasesToRoadmap(db, 'demo', [
147
+ { phase_number: 1, text: 'A', body: null },
148
+ { phase_number: 2, text: 'B', body: null },
149
+ ]);
150
+ const p1 = db.prepare("SELECT id FROM roadmap_phases WHERE phase_number=1").get().id;
151
+ roadmap(db, { action: 'focus', id: p1 });
152
+ const r = JSON.parse(roadmap(db, { action: 'set_status', id: p1, status: 'done' }).content[0].text);
153
+ const p2 = db.prepare("SELECT id FROM roadmap_phases WHERE phase_number=2").get().id;
154
+ assert.equal(r.cursor_advanced_to, p2);
155
+ // The new cursor should also be in_progress (activatePhase was called)
156
+ const p2Status = db.prepare("SELECT status FROM roadmap_phases WHERE id=?").get(p2).status;
157
+ assert.equal(p2Status, 'in_progress');
158
+ });
159
+
160
+ test('roadmap add: requires parent phase, inherits decision_name=null on the row', () => {
161
+ const db = makeDB();
162
+ syncPhasesToRoadmap(db, 'demo', [{ phase_number: 1, text: 'A', body: null }]);
163
+ const p1 = db.prepare("SELECT id FROM roadmap_phases WHERE phase_number=1").get().id;
164
+ // No parent should fail
165
+ assert.throws(() => roadmap(db, { action: 'add', text: 'orphan' }), /parent.*required/i);
166
+ // With parent, it inserts with NULL decision_name (sub-task)
167
+ const r = JSON.parse(roadmap(db, { action: 'add', parent: p1, text: 'sub' }).content[0].text);
168
+ assert.equal(r.added.parent_phase_id, p1);
169
+ assert.equal(r.added.decision_name, null);
170
+ assert.equal(r.added.phase_number, null);
171
+ assert.equal(r.inherits_decision_from, 'demo');
172
+ });
173
+
174
+ test('roadmap focus: refuses stale phase', () => {
175
+ const db = makeDB();
176
+ syncPhasesToRoadmap(db, 'demo', [{ phase_number: 1, text: 'A', body: null }]);
177
+ const p1 = db.prepare("SELECT id FROM roadmap_phases WHERE phase_number=1").get().id;
178
+ db.prepare("UPDATE roadmap_phases SET status='stale' WHERE id=?").run(p1);
179
+ assert.throws(() => roadmap(db, { action: 'focus', id: p1 }), /stale/i);
180
+ });
181
+
182
+ test('roadmap status: counts decisions in flight + reports current cursor with sub-task root walk', () => {
183
+ const db = makeDB();
184
+ syncPhasesToRoadmap(db, 'demo_a', [
185
+ { phase_number: 1, text: 'A1', body: null },
186
+ { phase_number: 2, text: 'A2', body: null },
187
+ ]);
188
+ syncPhasesToRoadmap(db, 'demo_b', [{ phase_number: 1, text: 'B1', body: null }]);
189
+ const a2 = db.prepare("SELECT id FROM roadmap_phases WHERE decision_name='demo_a' AND phase_number=2").get().id;
190
+ const sub = JSON.parse(roadmap(db, { action: 'add', parent: a2, text: 'subA2' }).content[0].text).added.id;
191
+ roadmap(db, { action: 'focus', id: sub });
192
+ const status = JSON.parse(roadmap(db, { action: 'status' }).content[0].text);
193
+ assert.equal(status.decisions_total, 2);
194
+ assert.equal(status.decisions_in_flight, 2);
195
+ assert.match(status.current, /demo_a \/ Phase 2 → sub: subA2/);
196
+ });