Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat(doctor): mercury doctor --storage
Salman Qureshi09295abcc102
diff --git a/src/index.ts b/src/index.tsindex ccdebc3e..f9dbc5a9 100644--- a/src/index.ts+++ b/src/index.ts@@ -53,7 +53,9 @@ import { ShortTermMemory, LongTermMemory, EpisodicMemory, migrateLegacyMemory }import { buildConversationHistoryPayload, CloudSessionSynchronizer, SessionRepository } from './sessions/index.js';import { UserMemoryStore } from './memory/user-memory.js';import { BotManager } from './bots/bot-manager.js';-import { isBetterSqlite3Available } from './memory/second-brain-db.js';+import { SECOND_BRAIN_UNAVAILABLE_MESSAGE } from './memory/second-brain-db.js';+import { isSqliteAvailable } from './utils/sqlite-driver.js';+import { runStorageDoctor } from './utils/storage-doctor.js';import { ProviderRegistry } from './providers/registry.js';import { Agent } from './core/agent.js';import { Scheduler } from './core/scheduler.js';@@ -2458,7 +2460,7 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {const episodic = new EpisodicMemory(config);let userMemory: UserMemoryStore | null = null;- if (config.memory.secondBrain?.enabled !== false && isBetterSqlite3Available()) {+ if (config.memory.secondBrain?.enabled !== false && isSqliteAvailable()) {try {userMemory = new UserMemoryStore(config);setWebUserMemory(userMemory);@@ -2471,11 +2473,8 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {logger.warn({ err }, 'Second brain initialization failed, continuing without it');userMemory = null;}- } else if (config.memory.secondBrain?.enabled !== false && !isBetterSqlite3Available()) {- logger.warn(- 'Second brain dependency issue: better-sqlite3 is not available. ' +- 'Memory/brain features require SQLite via better-sqlite3. Install build tools and reinstall dependencies.'- );+ } else if (config.memory.secondBrain?.enabled !== false && !isSqliteAvailable()) {+ logger.warn(`Second brain disabled: ${SECOND_BRAIN_UNAVAILABLE_MESSAGE}`);}const channels = new ChannelRegistry(config);@@ -3867,11 +3866,16 @@ program.command('doctor').description('Reconfigure Mercury setup (name, providers, channels, permissions defaults)').option('--platform', 'Show platform compatibility diagnostics')+ .option('--storage', 'Show SQLite backend, database files and row counts').action(async (opts) => {if (opts.platform) {runPlatformDoctor();return;}+ if (opts.storage) {+ runStorageDoctor();+ return;+ }if (isSetupComplete()) {await configure(loadConfig());} else {diff --git a/src/utils/storage-doctor.test.ts b/src/utils/storage-doctor.test.tsnew file mode 100644index 00000000..0b1d3b7d--- /dev/null+++ b/src/utils/storage-doctor.test.ts@@ -0,0 +1,134 @@+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { collectStorageReport, formatStorageReport, jsonOnlyFixHint } from './storage-doctor.js';+import { SQLITE_BACKEND_ENV, isSqliteAvailable, openSqlite, resetSqliteDriverForTests } from './sqlite-driver.js';+import { SecondBrainDB } from '../memory/second-brain-db.js';+import { SqliteQueueBackend } from '../bots/queue.js';++const savedBackend = process.env[SQLITE_BACKEND_ENV];+let home: string;++beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), 'mercury-storage-doctor-'));+ delete process.env[SQLITE_BACKEND_ENV];+ resetSqliteDriverForTests();+});++afterEach(() => {+ if (savedBackend === undefined) delete process.env[SQLITE_BACKEND_ENV];+ else process.env[SQLITE_BACKEND_ENV] = savedBackend;+ resetSqliteDriverForTests();+ rmSync(home, { recursive: true, force: true });+});++function byId(report: ReturnType<typeof collectStorageReport>, id: string) {+ const sub = report.subsystems.find((s) => s.id === id);+ if (!sub) throw new Error(`missing subsystem ${id}`);+ return sub;+}++describe('storage doctor', () => {+ it.skipIf(!isSqliteAvailable())('reports the active engine and row counts for every SQLite store', () => {+ const brain = new SecondBrainDB(join(home, 'memory', 'second-brain', 'second-brain.db'));+ brain.init();+ const now = Date.now();+ for (const [id, dismissed] of [['m1', 0], ['m2', 0], ['m3', 1]] as const) {+ brain.insert({+ id, user_key: 'user:owner', type: 'preference', categories: '[]', shareable: 0, summary: `memory ${id}`,+ detail: null, scope: 'durable', evidence_kind: 'direct', source: 'conversation', confidence: 0.9,+ importance: 0.5, durability: 0.5, evidence_count: 1, provenance: null, dismissed, superseded_by: null,+ created_at: now, updated_at: now, last_seen_at: now, last_used_at: null, last_used_query: null,+ });+ }+ brain.upsertPerson({ userKey: 'user:owner', name: 'Ada' });+ brain.close();++ const queue = new SqliteQueueBackend(join(home, 'bots'), 10);+ queue.enqueue({ id: 'j1', botId: 'b', trigger: 'chat', prompt: 'p', attempts: 0, createdAt: now, idempotencyKey: 'k1' });+ queue.enqueue({ id: 'j2', botId: 'b', trigger: 'chat', prompt: 'q', attempts: 0, createdAt: now, idempotencyKey: 'k2' });+ queue.claim('j2', 60);+ queue.enqueueMail({ botId: 'b', from: 'c', content: 'hi', createdAt: now });+ queue.close();++ mkdirSync(join(home, 'memory'), { recursive: true });+ const cache = openSqlite(join(home, 'memory', 'pool-search-cache.db'))!;+ cache.exec('CREATE TABLE pool_search_cache (query_hash TEXT PRIMARY KEY, query TEXT, results_json TEXT, fetched_at INTEGER)');+ cache.prepare('INSERT INTO pool_search_cache VALUES (?, ?, ?, ?)').run('h', 'q', '[]', now);+ cache.close();++ const report = collectStorageReport({ home, probeAll: true });+ expect(report.home).toBe(home);+ expect(report.jsonOnly).toBe(false);+ expect(report.backend.backend).not.toBeNull();+ expect(report.backend.candidates.some((c) => c.status === 'active')).toBe(true);++ const brainReport = byId(report, 'second-brain');+ expect(brainReport.mode).toBe('sqlite');+ expect(brainReport.exists).toBe(true);+ expect(brainReport.bytes).toBeGreaterThan(0);+ expect(brainReport.counts).toMatchObject({ memories: 3, 'memories (active)': 2, persons: 1, relationships: 0 });+ expect(brainReport.error).toBeUndefined();++ const queueReport = byId(report, 'bots-queue');+ expect(queueReport.mode).toBe('sqlite');+ expect(queueReport.counts).toMatchObject({ 'jobs (pending)': 1, 'jobs (claimed)': 1, 'dead-letter': 0, mail: 1 });++ const boardsReport = byId(report, 'boards');+ expect(boardsReport.mode).toBe('sqlite');+ expect(boardsReport.exists).toBe(false);+ expect(boardsReport.counts).toEqual({});++ expect(byId(report, 'pool-search-cache').counts).toEqual({ 'cached queries': 1 });++ const text = formatStorageReport(report).join('\n');+ expect(text).toContain('Mercury Storage Doctor');+ expect(text).toContain(report.backend.backend!);+ expect(text).toContain('memories 3');+ expect(text).not.toContain('JSON-only');+ });++ it('flags JSON-only storage, reads the JSON stores and gives a fix hint', () => {+ process.env[SQLITE_BACKEND_ENV] = 'json';+ resetSqliteDriverForTests();+ mkdirSync(join(home, 'bots'), { recursive: true });+ mkdirSync(join(home, 'memory'), { recursive: true });+ writeFileSync(join(home, 'bots', 'queue.json'), JSON.stringify({+ jobs: [{ id: 'a', state: 'pending' }, { id: 'b', state: 'claimed' }, { id: 'c', state: 'pending' }],+ dlq: [{ id: 'd' }],+ mails: [],+ }));+ writeFileSync(join(home, 'memory', 'boards.json'), JSON.stringify({ boards: [{ id: 'x' }, { id: 'y' }] }));+ writeFileSync(join(home, 'memory', 'board-contexts.json'), JSON.stringify({ x: {} }));++ const report = collectStorageReport({ home });+ expect(report.jsonOnly).toBe(true);+ expect(report.backend.backend).toBeNull();+ expect(report.backend.override).toBe('json');+ expect(byId(report, 'second-brain').mode).toBe('disabled');+ expect(byId(report, 'pool-search-cache').mode).toBe('disabled');+ expect(byId(report, 'bots-queue')).toMatchObject({+ mode: 'json',+ counts: { 'jobs (pending)': 2, 'jobs (claimed)': 1, 'dead-letter': 1, mail: 0 },+ });+ expect(byId(report, 'boards')).toMatchObject({ mode: 'json', counts: { boards: 2, contexts: 1 } });++ expect(jsonOnlyFixHint(report)[0]).toContain(SQLITE_BACKEND_ENV);+ const text = formatStorageReport(report).join('\n');+ expect(text).toContain('JSON-only');+ expect(text).toContain('DISABLED');+ });++ it('suggests Node 22.13+ or a rebuild when no engine loads on an older Node', () => {+ const report = collectStorageReport({ home });+ const fake = {+ ...report,+ jsonOnly: true,+ backend: { ...report.backend, backend: null, override: null, runtime: 'node' as const, runtimeVersion: '20.20.2' },+ };+ const hints = jsonOnlyFixHint(fake);+ expect(hints.some((h) => h.includes('Node 22.13+'))).toBe(true);+ expect(hints.some((h) => h.includes('npm rebuild better-sqlite3'))).toBe(true);+ });+});diff --git a/src/utils/storage-doctor.ts b/src/utils/storage-doctor.tsnew file mode 100644index 00000000..d888ecd9--- /dev/null+++ b/src/utils/storage-doctor.ts@@ -0,0 +1,296 @@+/**+ * `mercury doctor --storage` — which SQLite engine is active, where each+ * store lives, how many rows it holds, and a loud warning when storage is+ * JSON-only (Second Brain disabled; bots and boards on JSON files).+ */+import { existsSync, readFileSync, statSync } from 'node:fs';+import { join } from 'node:path';+import chalk from 'chalk';+import { getMercuryHome } from './config.js';+import {+ SQLITE_BACKEND_ENV,+ describeSqliteBackend,+ openSqlite,+ type SqliteBackendDescription,+ type SqliteDatabase,+} from './sqlite-driver.js';++export type StorageMode = 'sqlite' | 'json' | 'disabled';++export interface StorageSubsystemReport {+ id: 'second-brain' | 'bots-queue' | 'boards' | 'pool-search-cache';+ label: string;+ mode: StorageMode;+ /** Files this subsystem reads/writes in the current mode. */+ paths: string[];+ /** True when at least one of `paths` exists on disk. */+ exists: boolean;+ /** Bytes on disk across `paths` (0 when nothing exists). */+ bytes: number;+ /** Row / record counts per table or collection, when readable. */+ counts: Record<string, number>;+ /** Why counts could not be read. */+ error?: string;+}++export interface StorageReport {+ home: string;+ backend: SqliteBackendDescription;+ /** No SQLite engine: Second Brain is off, bots and boards use JSON files. */+ jsonOnly: boolean;+ subsystems: StorageSubsystemReport[];+}++export interface CollectStorageOptions {+ /** Mercury home to inspect (defaults to `getMercuryHome()`). */+ home?: string;+ /** Probe every engine, not just the active one. */+ probeAll?: boolean;+}++// File names mirror the owners: second-brain-db / user-memory, bots/queue,+// core/board-db, cloud/pool-search. Kept as literals here so the doctor never+// has to import (and initialise) those modules.+const SECOND_BRAIN_DB = ['memory', 'second-brain', 'second-brain.db'];+const QUEUE_DB = ['bots', 'queue.db'];+const QUEUE_JSON = ['bots', 'queue.json'];+const BOARDS_DB = ['memory', 'boards.db'];+const BOARDS_JSON = ['memory', 'boards.json'];+const BOARD_CONTEXTS_JSON = ['memory', 'board-contexts.json'];+const POOL_CACHE_DB = ['memory', 'pool-search-cache.db'];++function sizeOf(paths: string[]): { exists: boolean; bytes: number } {+ let exists = false;+ let bytes = 0;+ for (const path of paths) {+ try {+ const stat = statSync(path);+ exists = true;+ bytes += stat.size;+ } catch { /* missing */ }+ }+ return { exists, bytes };+}++function withSqlite<T>(path: string, fn: (db: SqliteDatabase) => T): { value?: T; error?: string } {+ let db: SqliteDatabase | null = null;+ try {+ db = openSqlite(path);+ if (!db) return { error: 'no SQLite engine available' };+ return { value: fn(db) };+ } catch (err) {+ return { error: err instanceof Error ? err.message.split('\n')[0] : String(err) };+ } finally {+ try { db?.close(); } catch { /* ignore */ }+ }+}++/** Count rows per table, skipping tables that do not exist yet. */+function countTables(db: SqliteDatabase, specs: Array<{ table: string; label: string; where?: string }>): Record<string, number> {+ const present = new Set(+ db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table'`).all<{ name: string }>().map((row) => row.name),+ );+ const counts: Record<string, number> = {};+ for (const spec of specs) {+ if (!present.has(spec.table)) continue;+ const row = db.prepare(`SELECT COUNT(*) AS count FROM ${spec.table}${spec.where ? ` WHERE ${spec.where}` : ''}`).get<{ count: number }>();+ counts[spec.label] = Number(row?.count ?? 0);+ }+ return counts;+}++function readJson(path: string): any | undefined {+ try {+ return JSON.parse(readFileSync(path, 'utf-8'));+ } catch {+ return undefined;+ }+}++function secondBrain(home: string, sqlite: boolean): StorageSubsystemReport {+ const path = join(home, ...SECOND_BRAIN_DB);+ const base = { id: 'second-brain' as const, label: 'Second Brain memory', paths: [path], ...sizeOf([path]), counts: {} };+ if (!sqlite) return { ...base, mode: 'disabled' };+ if (!base.exists) return { ...base, mode: 'sqlite' };+ const { value, error } = withSqlite(path, (db) => countTables(db, [+ { table: 'memories', label: 'memories' },+ { table: 'memories', label: 'memories (active)', where: 'dismissed = 0' },+ { table: 'persons', label: 'persons' },+ { table: 'person_relationships', label: 'relationships' },+ ]));+ return { ...base, mode: 'sqlite', counts: value ?? {}, ...(error ? { error } : {}) };+}++function botsQueue(home: string, sqlite: boolean): StorageSubsystemReport {+ if (sqlite) {+ const path = join(home, ...QUEUE_DB);+ const base = { id: 'bots-queue' as const, label: 'Bots job queue', paths: [path], ...sizeOf([path]), counts: {} };+ if (!base.exists) return { ...base, mode: 'sqlite' };+ const { value, error } = withSqlite(path, (db) => countTables(db, [+ { table: 'bot_jobs', label: 'jobs (pending)', where: `state = 'pending'` },+ { table: 'bot_jobs', label: 'jobs (claimed)', where: `state = 'claimed'` },+ { table: 'bot_dlq', label: 'dead-letter' },+ { table: 'bot_mail', label: 'mail' },+ ]));+ return { ...base, mode: 'sqlite', counts: value ?? {}, ...(error ? { error } : {}) };+ }+ const path = join(home, ...QUEUE_JSON);+ const base = { id: 'bots-queue' as const, label: 'Bots job queue', paths: [path], ...sizeOf([path]), counts: {} };+ if (!base.exists) return { ...base, mode: 'json' };+ const data = readJson(path);+ if (!data) return { ...base, mode: 'json', error: 'queue.json is not valid JSON' };+ const jobs: any[] = Array.isArray(data.jobs) ? data.jobs : [];+ return {+ ...base,+ mode: 'json',+ counts: {+ 'jobs (pending)': jobs.filter((j) => j?.state === 'pending').length,+ 'jobs (claimed)': jobs.filter((j) => j?.state === 'claimed').length,+ 'dead-letter': Array.isArray(data.dlq) ? data.dlq.length : 0,+ mail: Array.isArray(data.mails) ? data.mails.length : 0,+ },+ };+}++function boards(home: string, sqlite: boolean): StorageSubsystemReport {+ if (sqlite) {+ const path = join(home, ...BOARDS_DB);+ const base = { id: 'boards' as const, label: 'Boards', paths: [path], ...sizeOf([path]), counts: {} };+ if (!base.exists) return { ...base, mode: 'sqlite' };+ const { value, error } = withSqlite(path, (db) => countTables(db, [+ { table: 'boards', label: 'boards' },+ { table: 'board_contexts', label: 'contexts' },+ ]));+ return { ...base, mode: 'sqlite', counts: value ?? {}, ...(error ? { error } : {}) };+ }+ const boardsPath = join(home, ...BOARDS_JSON);+ const contextsPath = join(home, ...BOARD_CONTEXTS_JSON);+ const paths = [boardsPath, contextsPath];+ const base = { id: 'boards' as const, label: 'Boards', paths, ...sizeOf(paths), counts: {} };+ if (!base.exists) return { ...base, mode: 'json' };+ const boardsData = existsSync(boardsPath) ? readJson(boardsPath) : { boards: [] };+ const contextsData = existsSync(contextsPath) ? readJson(contextsPath) : {};+ return {+ ...base,+ mode: 'json',+ counts: {+ boards: Array.isArray(boardsData?.boards) ? boardsData.boards.length : 0,+ contexts: contextsData && typeof contextsData === 'object' ? Object.keys(contextsData).length : 0,+ },+ };+}++function poolSearchCache(home: string, sqlite: boolean): StorageSubsystemReport {+ const path = join(home, ...POOL_CACHE_DB);+ const base = { id: 'pool-search-cache' as const, label: 'Cloud pool search cache', paths: [path], ...sizeOf([path]), counts: {} };+ if (!sqlite) return { ...base, mode: 'disabled' };+ if (!base.exists) return { ...base, mode: 'sqlite' };+ const { value, error } = withSqlite(path, (db) => countTables(db, [{ table: 'pool_search_cache', label: 'cached queries' }]));+ return { ...base, mode: 'sqlite', counts: value ?? {}, ...(error ? { error } : {}) };+}++export function collectStorageReport(options: CollectStorageOptions = {}): StorageReport {+ const home = options.home ?? getMercuryHome();+ const backend = describeSqliteBackend({ probeAll: options.probeAll });+ const sqlite = backend.backend !== null;+ return {+ home,+ backend,+ jsonOnly: !sqlite,+ subsystems: [+ secondBrain(home, sqlite),+ botsQueue(home, sqlite),+ boards(home, sqlite),+ poolSearchCache(home, sqlite),+ ],+ };+}++function formatBytes(bytes: number): string {+ if (bytes < 1024) return `${bytes} B`;+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;+}++/** Fix hint for a JSON-only install, tailored to the runtime we are on. */+export function jsonOnlyFixHint(report: StorageReport): string[] {+ const { backend } = report;+ if (backend.override) {+ return [`${SQLITE_BACKEND_ENV}=${backend.override} is set — unset it to let Mercury pick an engine.`];+ }+ if (backend.runtime === 'bun') {+ return ['bun:sqlite should always load under Bun; this binary may be corrupted — reinstall with the install script.'];+ }+ const [major = 0, minor = 0] = backend.runtimeVersion.split('.').map((n) => Number.parseInt(n, 10) || 0);+ const hints: string[] = [];+ if (major < 22 || (major === 22 && minor < 13)) {+ hints.push('Upgrade to Node 22.13+ (built-in node:sqlite, no compiler needed), or');+ }+ hints.push('install build tools (make, gcc/g++, python3) and run: npm rebuild better-sqlite3');+ hints.push('or install the standalone binary (ships bun:sqlite): https://mercuryagent.sh');+ return hints;+}++/** Terminal rendering for `mercury doctor --storage`. */+export function formatStorageReport(report: StorageReport): string[] {+ const lines: string[] = [];+ const { backend } = report;+ lines.push('');+ lines.push(chalk.bold.cyan(' Mercury Storage Doctor'));+ lines.push(chalk.dim(' SQLite backend, database files and row counts'));+ lines.push('');+ lines.push(` Runtime: ${chalk.white(`${backend.runtime} ${backend.runtimeVersion}`)}`);+ lines.push(` Home: ${chalk.dim(report.home)}`);+ const active = backend.candidates.find((c) => c.status === 'active');+ lines.push(` SQLite backend: ${backend.backend+ ? chalk.green(backend.backend) + chalk.dim(active?.sqliteVersion ? ` (SQLite ${active.sqliteVersion}${active.providerVersion ? `, ${active.providerVersion}` : ''})` : '')+ : chalk.red('none — storage is JSON-only')}`);+ if (backend.override) {+ lines.push(` Override: ${chalk.yellow(`${SQLITE_BACKEND_ENV}=${backend.override}`)}`);+ }+ for (const candidate of backend.candidates) {+ const status = candidate.status === 'active'+ ? chalk.green('active')+ : candidate.status === 'available'+ ? chalk.white('available')+ : candidate.status === 'unavailable'+ ? chalk.yellow('unavailable')+ : chalk.dim('not probed');+ const reason = candidate.reason ? chalk.dim(` — ${candidate.reason}`) : '';+ lines.push(` • ${candidate.name.padEnd(15)} ${status}${reason}`);+ }+ lines.push('');+ lines.push(chalk.bold.white(' Stores'));+ for (const sub of report.subsystems) {+ const mode = sub.mode === 'sqlite'+ ? chalk.green('sqlite')+ : sub.mode === 'json'+ ? chalk.yellow('json')+ : chalk.red('disabled');+ lines.push(` ${sub.label.padEnd(26)} ${mode}`);+ for (const path of sub.paths) {+ const present = existsSync(path);+ lines.push(` ${chalk.dim(path)}${present ? '' : chalk.dim(' (not created yet)')}`);+ }+ if (sub.exists) lines.push(` size: ${chalk.white(formatBytes(sub.bytes))}`);+ const entries = Object.entries(sub.counts);+ if (entries.length > 0) {+ lines.push(` rows: ${entries.map(([label, count]) => `${label} ${chalk.white(String(count))}`).join(chalk.dim(' · '))}`);+ }+ if (sub.error) lines.push(` ${chalk.yellow(`could not read: ${sub.error}`)}`);+ }+ lines.push('');+ if (report.jsonOnly) {+ lines.push(chalk.yellow(' Warning: storage is JSON-only — no SQLite engine could be loaded.'));+ lines.push(chalk.yellow(' Second Brain memory and the cloud pool-search cache are DISABLED;'));+ lines.push(chalk.yellow(' bots and boards persist to JSON files (slower, no concurrent-writer safety).'));+ for (const hint of jsonOnlyFixHint(report)) lines.push(chalk.dim(` Fix: ${hint}`));+ lines.push('');+ }+ return lines;+}++export function runStorageDoctor(): void {+ const report = collectStorageReport({ probeAll: true });+ for (const line of formatStorageReport(report)) console.log(line);+}