c6f932988a
* MAESTRO: fix ChromaDB core issues — Python pinning, Windows paths, disable toggle, metadata sanitization, transport errors - Add --python version pinning to uvx args in both local and remote mode (fixes #1196, #1206, #1208) - Convert backslash paths to forward slashes for --data-dir on Windows (fixes #1199) - Add CLAUDE_MEM_CHROMA_ENABLED setting for SQLite-only fallback mode (fixes #707) - Sanitize metadata in addDocuments() to filter null/undefined/empty values (fixes #1183, #1188) - Wrap callTool() in try/catch for transport errors with auto-reconnect (fixes #1162) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix data integrity — content-hash deduplication, project name collision, empty project guard, stuck isProcessing - Add SHA-256 content-hash deduplication to observations INSERT (store.ts, transactions.ts, SessionStore.ts) - Add content_hash column via migration 22 with backfill and index - Fix project name collision: getCurrentProjectName() now returns parent/basename - Guard against empty project string with cwd-derived fallback - Fix stuck isProcessing: hasAnyPendingWork() resets processing messages older than 5 minutes - Add 12 new tests covering all four fixes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix hook lifecycle — stderr suppression, output isolation, conversation pollution prevention - Suppress process.stderr.write in hookCommand() to prevent Claude Code showing diagnostic output as error UI (#1181). Restores stderr in finally block for worker-continues case. - Convert console.error() to logger.warn()/error() in hook-command.ts and handlers/index.ts so all diagnostics route to log file instead of stderr. - Verified all 7 handlers return suppressOutput: true (prevents conversation pollution #598, #784). - Verified session-complete is a recognized event type (fixes #984). - Verified unknown event types return no-op handler with exit 0 (graceful degradation). - Added 10 new tests in tests/hook-lifecycle.test.ts covering event dispatch, adapter defaults, stderr suppression, and standard response constants. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix worker lifecycle — restart loop coordination, stale transport retry, ENOENT shutdown race - Add PID file mtime guard to prevent concurrent restart storms (#1145): isPidFileRecent() + touchPidFile() coordinate across sessions - Add transparent retry in ChromaMcpManager.callTool() on transport error — reconnects and retries once instead of failing (#1131) - Wrap getInstalledPluginVersion() with ENOENT/EBUSY handling (#1042) - Verified ChromaMcpManager.stop() already called on all shutdown paths Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix Windows platform support — uvx.cmd spawn, PowerShell $_ elimination, windowsHide, FTS5 fallback - Route uvx spawn through cmd.exe /c on Windows since MCP SDK lacks shell:true (#1190, #1192, #1199) - Replace all PowerShell Where-Object {$_} pipelines with WQL -Filter server-side filtering (#1024, #1062) - Add windowsHide: true to all exec/spawn calls missing it to prevent console popups (#1048) - Add FTS5 runtime probe with graceful fallback when unavailable on Windows (#791) - Guard FTS5 table creation in migrations, SessionSearch, and SessionStore with try/catch Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix skills/ distribution — build-time verification and regression tests (#1187) Add post-build verification in build-hooks.js that fails if critical distribution files (skills, hooks, plugin manifest) are missing. Add 10 regression tests covering skill file presence, YAML frontmatter, hooks.json integrity, and package.json files field. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix MigrationRunner schema initialization (#979) — version conflict between parallel migration systems Root cause: old DatabaseManager migrations 1-7 shared schema_versions table with MigrationRunner's 4-22, causing version number collisions (5=drop tables vs add column, 6=FTS5 vs prompt tracking, 7=discovery_tokens vs remove UNIQUE). initializeSchema() was gated behind maxApplied===0, so core tables were never created when old versions were present. Fixes: - initializeSchema() always creates core tables via CREATE TABLE IF NOT EXISTS - Migrations 5-7 check actual DB state (columns/constraints) not just version tracking - Crash-safe temp table rebuilds (DROP IF EXISTS _new before CREATE) - Added missing migration 21 (ON UPDATE CASCADE) to MigrationRunner - Added ON UPDATE CASCADE to FK definitions in initializeSchema() - All changes applied to both runner.ts and SessionStore.ts Tests: 13 new tests in migration-runner.test.ts covering fresh DB, idempotency, version conflicts, crash recovery, FK constraints, and data integrity. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix 21 test failures — stale mocks, outdated assertions, missing OpenClaw guards Server tests (12): Added missing workerPath and getAiStatus to ServerOptions mocks after interface expansion. ChromaSync tests (3): Updated to verify transport cleanup in ChromaMcpManager after architecture refactor. OpenClaw (2): Added memory_ tool skipping and response truncation to prevent recursive loops and oversized payloads. MarkdownFormatter (2): Updated assertions to match current output. SettingsDefaultsManager (1): Used correct default key for getBool test. Logger standards (1): Excluded CLI transcript command from background service check. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix Codex CLI compatibility (#744) — session_id fallbacks, unknown platform tolerance, undefined guard Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix Cursor IDE integration (#838, #1049) — adapter field fallbacks, tolerant session-init validation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix /api/logs OOM (#1203) — tail-read replaces full-file readFileSync Replace readFileSync (loads entire file into memory) with readLastLines() that reads only from the end of the file in expanding chunks (64KB → 10MB cap). Prevents OOM on large log files while preserving the same API response shape. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix Settings CORS error (#1029) — explicit methods and allowedHeaders in CORS config Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: add session custom_title for agent attribution (#1213) — migration 23, endpoint + store support Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: prevent CLAUDE.md/AGENTS.md writes inside .git/ directories (#1165) Add .git path guard to all 4 write sites to prevent ref corruption when paths resolve inside .git internals. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix plugin disabled state not respected (#781) — early exit check in all hook entry points Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix UserPromptSubmit context re-injection on every turn (#1079) — contextInjected session flag Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * MAESTRO: fix stale AbortController queue stall (#1099) — lastGeneratorActivity tracking + 30s timeout Three-layer fix: 1. Added lastGeneratorActivity timestamp to ActiveSession, updated by processAgentResponse (all agents), getMessageIterator (queue yields), and startGeneratorWithProvider (generator launch) 2. Added stale generator detection in ensureGeneratorRunning — if no activity for >30s, aborts stale controller, resets state, restarts 3. Added AbortSignal.timeout(30000) in deleteSession to prevent indefinite hang when awaiting a stuck generator promise Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
523 lines
20 KiB
TypeScript
523 lines
20 KiB
TypeScript
import { Database } from 'bun:sqlite';
|
|
import { Migration } from './Database.js';
|
|
|
|
// Re-export MigrationRunner for SessionStore migration extraction
|
|
export { MigrationRunner } from './migrations/runner.js';
|
|
|
|
/**
|
|
* Initial schema migration - creates all core tables
|
|
*/
|
|
export const migration001: Migration = {
|
|
version: 1,
|
|
up: (db: Database) => {
|
|
// Sessions table - core session tracking
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT UNIQUE NOT NULL,
|
|
project TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
source TEXT NOT NULL DEFAULT 'compress',
|
|
archive_path TEXT,
|
|
archive_bytes INTEGER,
|
|
archive_checksum TEXT,
|
|
archived_at TEXT,
|
|
metadata_json TEXT
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sessions_project ON sessions(project);
|
|
CREATE INDEX IF NOT EXISTS idx_sessions_created_at ON sessions(created_at_epoch DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_sessions_project_created ON sessions(project, created_at_epoch DESC);
|
|
`);
|
|
|
|
// Memories table - compressed memory chunks
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS memories (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT NOT NULL,
|
|
text TEXT NOT NULL,
|
|
document_id TEXT UNIQUE,
|
|
keywords TEXT,
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
project TEXT NOT NULL,
|
|
archive_basename TEXT,
|
|
origin TEXT NOT NULL DEFAULT 'transcript',
|
|
FOREIGN KEY (session_id) REFERENCES sessions(session_id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_memories_session ON memories(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_project ON memories(project);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_created_at ON memories(created_at_epoch DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_project_created ON memories(project, created_at_epoch DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_document_id ON memories(document_id);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_origin ON memories(origin);
|
|
`);
|
|
|
|
// Overviews table - session summaries (one per project)
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS overviews (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT NOT NULL,
|
|
content TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
project TEXT NOT NULL,
|
|
origin TEXT NOT NULL DEFAULT 'claude',
|
|
FOREIGN KEY (session_id) REFERENCES sessions(session_id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_overviews_session ON overviews(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_overviews_project ON overviews(project);
|
|
CREATE INDEX IF NOT EXISTS idx_overviews_created_at ON overviews(created_at_epoch DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_overviews_project_created ON overviews(project, created_at_epoch DESC);
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_overviews_project_latest ON overviews(project, created_at_epoch DESC);
|
|
`);
|
|
|
|
// Diagnostics table - system health and debug info
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS diagnostics (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT,
|
|
message TEXT NOT NULL,
|
|
severity TEXT NOT NULL DEFAULT 'info',
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
project TEXT NOT NULL,
|
|
origin TEXT NOT NULL DEFAULT 'system',
|
|
FOREIGN KEY (session_id) REFERENCES sessions(session_id) ON DELETE SET NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_diagnostics_session ON diagnostics(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_diagnostics_project ON diagnostics(project);
|
|
CREATE INDEX IF NOT EXISTS idx_diagnostics_severity ON diagnostics(severity);
|
|
CREATE INDEX IF NOT EXISTS idx_diagnostics_created ON diagnostics(created_at_epoch DESC);
|
|
`);
|
|
|
|
// Transcript events table - raw conversation events
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS transcript_events (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT NOT NULL,
|
|
project TEXT,
|
|
event_index INTEGER NOT NULL,
|
|
event_type TEXT,
|
|
raw_json TEXT NOT NULL,
|
|
captured_at TEXT NOT NULL,
|
|
captured_at_epoch INTEGER NOT NULL,
|
|
FOREIGN KEY (session_id) REFERENCES sessions(session_id) ON DELETE CASCADE,
|
|
UNIQUE(session_id, event_index)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_transcript_events_session ON transcript_events(session_id, event_index);
|
|
CREATE INDEX IF NOT EXISTS idx_transcript_events_project ON transcript_events(project);
|
|
CREATE INDEX IF NOT EXISTS idx_transcript_events_type ON transcript_events(event_type);
|
|
CREATE INDEX IF NOT EXISTS idx_transcript_events_captured ON transcript_events(captured_at_epoch DESC);
|
|
`);
|
|
|
|
console.log('✅ Created all database tables successfully');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
db.run(`
|
|
DROP TABLE IF EXISTS transcript_events;
|
|
DROP TABLE IF EXISTS diagnostics;
|
|
DROP TABLE IF EXISTS overviews;
|
|
DROP TABLE IF EXISTS memories;
|
|
DROP TABLE IF EXISTS sessions;
|
|
`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 002 - Add hierarchical memory fields (v2 format)
|
|
*/
|
|
export const migration002: Migration = {
|
|
version: 2,
|
|
up: (db: Database) => {
|
|
// Add new columns for hierarchical memory structure
|
|
db.run(`
|
|
ALTER TABLE memories ADD COLUMN title TEXT;
|
|
ALTER TABLE memories ADD COLUMN subtitle TEXT;
|
|
ALTER TABLE memories ADD COLUMN facts TEXT;
|
|
ALTER TABLE memories ADD COLUMN concepts TEXT;
|
|
ALTER TABLE memories ADD COLUMN files_touched TEXT;
|
|
`);
|
|
|
|
// Create indexes for the new fields to improve search performance
|
|
db.run(`
|
|
CREATE INDEX IF NOT EXISTS idx_memories_title ON memories(title);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_concepts ON memories(concepts);
|
|
`);
|
|
|
|
console.log('✅ Added hierarchical memory fields to memories table');
|
|
},
|
|
|
|
down: (_db: Database) => {
|
|
// Note: SQLite doesn't support DROP COLUMN in all versions
|
|
// In production, we'd need to recreate the table without these columns
|
|
// For now, we'll just log a warning
|
|
console.log('⚠️ Warning: SQLite ALTER TABLE DROP COLUMN not fully supported');
|
|
console.log('⚠️ To rollback, manually recreate the memories table');
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 003 - Add streaming_sessions table for real-time session tracking
|
|
*/
|
|
export const migration003: Migration = {
|
|
version: 3,
|
|
up: (db: Database) => {
|
|
// Streaming sessions table - tracks active SDK compression sessions
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS streaming_sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
content_session_id TEXT UNIQUE NOT NULL,
|
|
memory_session_id TEXT,
|
|
project TEXT NOT NULL,
|
|
title TEXT,
|
|
subtitle TEXT,
|
|
user_prompt TEXT,
|
|
started_at TEXT NOT NULL,
|
|
started_at_epoch INTEGER NOT NULL,
|
|
updated_at TEXT,
|
|
updated_at_epoch INTEGER,
|
|
completed_at TEXT,
|
|
completed_at_epoch INTEGER,
|
|
status TEXT NOT NULL DEFAULT 'active'
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_streaming_sessions_claude_id ON streaming_sessions(content_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_streaming_sessions_sdk_id ON streaming_sessions(memory_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_streaming_sessions_project ON streaming_sessions(project);
|
|
CREATE INDEX IF NOT EXISTS idx_streaming_sessions_status ON streaming_sessions(status);
|
|
CREATE INDEX IF NOT EXISTS idx_streaming_sessions_started ON streaming_sessions(started_at_epoch DESC);
|
|
`);
|
|
|
|
console.log('✅ Created streaming_sessions table for real-time session tracking');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
db.run(`
|
|
DROP TABLE IF EXISTS streaming_sessions;
|
|
`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 004 - Add SDK agent architecture tables
|
|
* Implements the refactor plan for hook-driven memory with SDK agent synthesis
|
|
*/
|
|
export const migration004: Migration = {
|
|
version: 4,
|
|
up: (db: Database) => {
|
|
// SDK sessions table - tracks SDK streaming sessions
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS sdk_sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
content_session_id TEXT UNIQUE NOT NULL,
|
|
memory_session_id TEXT UNIQUE,
|
|
project TEXT NOT NULL,
|
|
user_prompt TEXT,
|
|
started_at TEXT NOT NULL,
|
|
started_at_epoch INTEGER NOT NULL,
|
|
completed_at TEXT,
|
|
completed_at_epoch INTEGER,
|
|
status TEXT CHECK(status IN ('active', 'completed', 'failed')) NOT NULL DEFAULT 'active'
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_claude_id ON sdk_sessions(content_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_sdk_id ON sdk_sessions(memory_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_project ON sdk_sessions(project);
|
|
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_status ON sdk_sessions(status);
|
|
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_started ON sdk_sessions(started_at_epoch DESC);
|
|
`);
|
|
|
|
// Observation queue table - tracks pending observations for SDK processing
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS observation_queue (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
memory_session_id TEXT NOT NULL,
|
|
tool_name TEXT NOT NULL,
|
|
tool_input TEXT NOT NULL,
|
|
tool_output TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
processed_at_epoch INTEGER,
|
|
FOREIGN KEY(memory_session_id) REFERENCES sdk_sessions(memory_session_id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_observation_queue_sdk_session ON observation_queue(memory_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_observation_queue_processed ON observation_queue(processed_at_epoch);
|
|
CREATE INDEX IF NOT EXISTS idx_observation_queue_pending ON observation_queue(memory_session_id, processed_at_epoch);
|
|
`);
|
|
|
|
// Observations table - stores extracted observations (what SDK decides is important)
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS observations (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
memory_session_id TEXT NOT NULL,
|
|
project TEXT NOT NULL,
|
|
text TEXT NOT NULL,
|
|
type TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
FOREIGN KEY(memory_session_id) REFERENCES sdk_sessions(memory_session_id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_observations_sdk_session ON observations(memory_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_observations_project ON observations(project);
|
|
CREATE INDEX IF NOT EXISTS idx_observations_type ON observations(type);
|
|
CREATE INDEX IF NOT EXISTS idx_observations_created ON observations(created_at_epoch DESC);
|
|
`);
|
|
|
|
// Session summaries table - stores structured session summaries
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS session_summaries (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
memory_session_id TEXT UNIQUE NOT NULL,
|
|
project TEXT NOT NULL,
|
|
request TEXT,
|
|
investigated TEXT,
|
|
learned TEXT,
|
|
completed TEXT,
|
|
next_steps TEXT,
|
|
files_read TEXT,
|
|
files_edited TEXT,
|
|
notes TEXT,
|
|
created_at TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
FOREIGN KEY(memory_session_id) REFERENCES sdk_sessions(memory_session_id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_session_summaries_sdk_session ON session_summaries(memory_session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_session_summaries_project ON session_summaries(project);
|
|
CREATE INDEX IF NOT EXISTS idx_session_summaries_created ON session_summaries(created_at_epoch DESC);
|
|
`);
|
|
|
|
console.log('✅ Created SDK agent architecture tables');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
db.run(`
|
|
DROP TABLE IF EXISTS session_summaries;
|
|
DROP TABLE IF EXISTS observations;
|
|
DROP TABLE IF EXISTS observation_queue;
|
|
DROP TABLE IF EXISTS sdk_sessions;
|
|
`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 005 - Remove orphaned tables
|
|
* Drops streaming_sessions (superseded by sdk_sessions)
|
|
* Drops observation_queue (superseded by Unix socket communication)
|
|
*/
|
|
export const migration005: Migration = {
|
|
version: 5,
|
|
up: (db: Database) => {
|
|
// Drop streaming_sessions - superseded by sdk_sessions in migration004
|
|
// This table was from v2 architecture and is no longer used
|
|
db.run(`DROP TABLE IF EXISTS streaming_sessions`);
|
|
|
|
// Drop observation_queue - superseded by Unix socket communication
|
|
// Worker now uses sockets instead of database polling for observations
|
|
db.run(`DROP TABLE IF EXISTS observation_queue`);
|
|
|
|
console.log('✅ Dropped orphaned tables: streaming_sessions, observation_queue');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
// Recreate tables if needed (though they should never be used)
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS streaming_sessions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
content_session_id TEXT UNIQUE NOT NULL,
|
|
memory_session_id TEXT,
|
|
project TEXT NOT NULL,
|
|
title TEXT,
|
|
subtitle TEXT,
|
|
user_prompt TEXT,
|
|
started_at TEXT NOT NULL,
|
|
started_at_epoch INTEGER NOT NULL,
|
|
updated_at TEXT,
|
|
updated_at_epoch INTEGER,
|
|
completed_at TEXT,
|
|
completed_at_epoch INTEGER,
|
|
status TEXT NOT NULL DEFAULT 'active'
|
|
)
|
|
`);
|
|
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS observation_queue (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
memory_session_id TEXT NOT NULL,
|
|
tool_name TEXT NOT NULL,
|
|
tool_input TEXT NOT NULL,
|
|
tool_output TEXT NOT NULL,
|
|
created_at_epoch INTEGER NOT NULL,
|
|
processed_at_epoch INTEGER,
|
|
FOREIGN KEY(memory_session_id) REFERENCES sdk_sessions(memory_session_id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
|
|
console.log('⚠️ Recreated streaming_sessions and observation_queue (for rollback only)');
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 006 - Add FTS5 full-text search tables
|
|
* Creates virtual tables for fast text search on observations and session_summaries
|
|
*/
|
|
export const migration006: Migration = {
|
|
version: 6,
|
|
up: (db: Database) => {
|
|
// FTS5 may be unavailable on some platforms (e.g., Bun on Windows #791).
|
|
// Probe before creating tables — search falls back to ChromaDB when unavailable.
|
|
try {
|
|
db.run('CREATE VIRTUAL TABLE _fts5_probe USING fts5(test_column)');
|
|
db.run('DROP TABLE _fts5_probe');
|
|
} catch {
|
|
console.log('⚠️ FTS5 not available on this platform — skipping FTS migration (search uses ChromaDB)');
|
|
return;
|
|
}
|
|
|
|
// FTS5 virtual table for observations
|
|
// Note: This assumes the hierarchical fields (title, subtitle, etc.) already exist
|
|
// from the inline migrations in SessionStore constructor
|
|
db.run(`
|
|
CREATE VIRTUAL TABLE IF NOT EXISTS observations_fts USING fts5(
|
|
title,
|
|
subtitle,
|
|
narrative,
|
|
text,
|
|
facts,
|
|
concepts,
|
|
content='observations',
|
|
content_rowid='id'
|
|
);
|
|
`);
|
|
|
|
// Populate FTS table with existing data
|
|
db.run(`
|
|
INSERT INTO observations_fts(rowid, title, subtitle, narrative, text, facts, concepts)
|
|
SELECT id, title, subtitle, narrative, text, facts, concepts
|
|
FROM observations;
|
|
`);
|
|
|
|
// Triggers to keep observations_fts in sync
|
|
db.run(`
|
|
CREATE TRIGGER IF NOT EXISTS observations_ai AFTER INSERT ON observations BEGIN
|
|
INSERT INTO observations_fts(rowid, title, subtitle, narrative, text, facts, concepts)
|
|
VALUES (new.id, new.title, new.subtitle, new.narrative, new.text, new.facts, new.concepts);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS observations_ad AFTER DELETE ON observations BEGIN
|
|
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative, text, facts, concepts)
|
|
VALUES('delete', old.id, old.title, old.subtitle, old.narrative, old.text, old.facts, old.concepts);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS observations_au AFTER UPDATE ON observations BEGIN
|
|
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative, text, facts, concepts)
|
|
VALUES('delete', old.id, old.title, old.subtitle, old.narrative, old.text, old.facts, old.concepts);
|
|
INSERT INTO observations_fts(rowid, title, subtitle, narrative, text, facts, concepts)
|
|
VALUES (new.id, new.title, new.subtitle, new.narrative, new.text, new.facts, new.concepts);
|
|
END;
|
|
`);
|
|
|
|
// FTS5 virtual table for session_summaries
|
|
db.run(`
|
|
CREATE VIRTUAL TABLE IF NOT EXISTS session_summaries_fts USING fts5(
|
|
request,
|
|
investigated,
|
|
learned,
|
|
completed,
|
|
next_steps,
|
|
notes,
|
|
content='session_summaries',
|
|
content_rowid='id'
|
|
);
|
|
`);
|
|
|
|
// Populate FTS table with existing data
|
|
db.run(`
|
|
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
|
|
SELECT id, request, investigated, learned, completed, next_steps, notes
|
|
FROM session_summaries;
|
|
`);
|
|
|
|
// Triggers to keep session_summaries_fts in sync
|
|
db.run(`
|
|
CREATE TRIGGER IF NOT EXISTS session_summaries_ai AFTER INSERT ON session_summaries BEGIN
|
|
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
|
|
VALUES (new.id, new.request, new.investigated, new.learned, new.completed, new.next_steps, new.notes);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS session_summaries_ad AFTER DELETE ON session_summaries BEGIN
|
|
INSERT INTO session_summaries_fts(session_summaries_fts, rowid, request, investigated, learned, completed, next_steps, notes)
|
|
VALUES('delete', old.id, old.request, old.investigated, old.learned, old.completed, old.next_steps, old.notes);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS session_summaries_au AFTER UPDATE ON session_summaries BEGIN
|
|
INSERT INTO session_summaries_fts(session_summaries_fts, rowid, request, investigated, learned, completed, next_steps, notes)
|
|
VALUES('delete', old.id, old.request, old.investigated, old.learned, old.completed, old.next_steps, old.notes);
|
|
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
|
|
VALUES (new.id, new.request, new.investigated, new.learned, new.completed, new.next_steps, new.notes);
|
|
END;
|
|
`);
|
|
|
|
console.log('✅ Created FTS5 virtual tables and triggers for full-text search');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
db.run(`
|
|
DROP TRIGGER IF EXISTS observations_au;
|
|
DROP TRIGGER IF EXISTS observations_ad;
|
|
DROP TRIGGER IF EXISTS observations_ai;
|
|
DROP TABLE IF EXISTS observations_fts;
|
|
|
|
DROP TRIGGER IF EXISTS session_summaries_au;
|
|
DROP TRIGGER IF EXISTS session_summaries_ad;
|
|
DROP TRIGGER IF EXISTS session_summaries_ai;
|
|
DROP TABLE IF EXISTS session_summaries_fts;
|
|
`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Migration 007 - Add discovery_tokens column for ROI metrics
|
|
* Tracks token cost of discovering/creating each observation and summary
|
|
*/
|
|
export const migration007: Migration = {
|
|
version: 7,
|
|
up: (db: Database) => {
|
|
// Add discovery_tokens to observations table
|
|
db.run(`ALTER TABLE observations ADD COLUMN discovery_tokens INTEGER DEFAULT 0`);
|
|
|
|
// Add discovery_tokens to session_summaries table
|
|
db.run(`ALTER TABLE session_summaries ADD COLUMN discovery_tokens INTEGER DEFAULT 0`);
|
|
|
|
console.log('✅ Added discovery_tokens columns for ROI tracking');
|
|
},
|
|
|
|
down: (db: Database) => {
|
|
// Note: SQLite doesn't support DROP COLUMN in all versions
|
|
// In production, would need to recreate tables without these columns
|
|
console.log('⚠️ Warning: SQLite ALTER TABLE DROP COLUMN not fully supported');
|
|
console.log('⚠️ To rollback, manually recreate the observations and session_summaries tables');
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* All migrations in order
|
|
*/
|
|
export const migrations: Migration[] = [
|
|
migration001,
|
|
migration002,
|
|
migration003,
|
|
migration004,
|
|
migration005,
|
|
migration006,
|
|
migration007
|
|
]; |