@ulrichc1/sparn 1.2.2 → 1.4.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.
Files changed (42) hide show
  1. package/PRIVACY.md +1 -1
  2. package/README.md +136 -642
  3. package/SECURITY.md +1 -1
  4. package/dist/cli/dashboard.cjs +3977 -0
  5. package/dist/cli/dashboard.cjs.map +1 -0
  6. package/dist/cli/dashboard.d.cts +17 -0
  7. package/dist/cli/dashboard.d.ts +17 -0
  8. package/dist/cli/dashboard.js +3932 -0
  9. package/dist/cli/dashboard.js.map +1 -0
  10. package/dist/cli/index.cjs +3853 -484
  11. package/dist/cli/index.cjs.map +1 -1
  12. package/dist/cli/index.js +3810 -457
  13. package/dist/cli/index.js.map +1 -1
  14. package/dist/daemon/index.cjs +411 -99
  15. package/dist/daemon/index.cjs.map +1 -1
  16. package/dist/daemon/index.js +423 -103
  17. package/dist/daemon/index.js.map +1 -1
  18. package/dist/hooks/post-tool-result.cjs +115 -266
  19. package/dist/hooks/post-tool-result.cjs.map +1 -1
  20. package/dist/hooks/post-tool-result.js +115 -266
  21. package/dist/hooks/post-tool-result.js.map +1 -1
  22. package/dist/hooks/pre-prompt.cjs +197 -268
  23. package/dist/hooks/pre-prompt.cjs.map +1 -1
  24. package/dist/hooks/pre-prompt.js +182 -268
  25. package/dist/hooks/pre-prompt.js.map +1 -1
  26. package/dist/hooks/stop-docs-refresh.cjs +123 -0
  27. package/dist/hooks/stop-docs-refresh.cjs.map +1 -0
  28. package/dist/hooks/stop-docs-refresh.d.cts +1 -0
  29. package/dist/hooks/stop-docs-refresh.d.ts +1 -0
  30. package/dist/hooks/stop-docs-refresh.js +126 -0
  31. package/dist/hooks/stop-docs-refresh.js.map +1 -0
  32. package/dist/index.cjs +1754 -337
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +539 -40
  35. package/dist/index.d.ts +539 -40
  36. package/dist/index.js +1737 -329
  37. package/dist/index.js.map +1 -1
  38. package/dist/mcp/index.cjs +304 -71
  39. package/dist/mcp/index.cjs.map +1 -1
  40. package/dist/mcp/index.js +308 -71
  41. package/dist/mcp/index.js.map +1 -1
  42. package/package.json +10 -3
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/daemon/index.ts","../../src/core/kv-memory.ts","../../src/daemon/consolidation-scheduler.ts","../../src/core/engram-scorer.ts","../../src/core/sleep-compressor.ts","../../src/cli/commands/consolidate.ts","../../src/core/metrics.ts","../../src/daemon/session-watcher.ts","../../src/utils/context-parser.ts","../../src/utils/hash.ts","../../src/utils/tokenizer.ts","../../src/core/budget-pruner.ts","../../src/core/incremental-optimizer.ts","../../src/core/context-pipeline.ts","../../src/daemon/file-tracker.ts"],"sourcesContent":["/**\n * Daemon Entry Point - Background process main loop\n *\n * This file is executed as a forked child process by daemon-process.ts.\n * It runs the session watcher and handles cleanup on exit.\n */\n\nimport { appendFileSync, existsSync, unlinkSync } from 'node:fs';\nimport { createKVMemory } from '../core/kv-memory.js';\nimport type { SparnConfig } from '../types/config.js';\nimport { createConsolidationScheduler } from './consolidation-scheduler.js';\nimport { createSessionWatcher } from './session-watcher.js';\n\n// Parse config from environment\nconst configJson = process.env['SPARN_CONFIG'];\nconst pidFile = process.env['SPARN_PID_FILE'];\nconst logFile = process.env['SPARN_LOG_FILE'];\n\nif (!configJson || !pidFile || !logFile) {\n console.error('Daemon: Missing required environment variables');\n process.exit(1);\n}\n\nconst config: SparnConfig = JSON.parse(configJson);\n\n// Log helper\nfunction log(message: string): void {\n const timestamp = new Date().toISOString();\n const logMessage = `[${timestamp}] ${message}\\n`;\n\n // logFile is guaranteed to be defined after env check\n if (logFile) {\n try {\n appendFileSync(logFile, logMessage, 'utf-8');\n } catch {\n // Fail silently if can't write log\n }\n }\n}\n\n// Cleanup on exit\nfunction cleanup(): void {\n log('Daemon shutting down');\n\n // Remove PID file (pidFile is guaranteed to be defined after env check)\n if (pidFile && existsSync(pidFile)) {\n try {\n unlinkSync(pidFile);\n } catch {\n // Fail silently\n }\n }\n\n // Stop scheduler\n if (scheduler) {\n scheduler.stop();\n }\n\n // Stop watcher\n watcher.stop();\n\n // Close memory connection\n if (memory) {\n void memory.close();\n }\n\n process.exit(0);\n}\n\n// Signal handlers\nprocess.on('SIGTERM', cleanup);\nprocess.on('SIGINT', cleanup);\nprocess.on('SIGHUP', cleanup);\n\n// Uncaught exception handler\nprocess.on('uncaughtException', (error) => {\n log(`Uncaught exception: ${error.message}`);\n cleanup();\n});\n\nprocess.on('unhandledRejection', (reason) => {\n log(`Unhandled rejection: ${reason}`);\n cleanup();\n});\n\n// Create memory instance for consolidation scheduler\n// Using a default path - could be made configurable in future\nlet memory: Awaited<ReturnType<typeof createKVMemory>> | null = null;\nlet scheduler: ReturnType<typeof createConsolidationScheduler> | null = null;\n\n// Create and start session watcher\nlog('Daemon starting');\n\nconst watcher = createSessionWatcher({\n config,\n onOptimize: (sessionId, stats) => {\n log(\n `Optimized session ${sessionId}: ${stats.optimizedTokens} tokens (${Math.round(stats.reduction * 100)}% reduction)`,\n );\n },\n onError: (error) => {\n log(`Error: ${error.message}`);\n },\n});\n\n// Start watching\nwatcher\n .start()\n .then(async () => {\n log('Daemon ready - watching Claude Code sessions');\n\n // Initialize consolidation scheduler if enabled\n if (\n config.realtime.consolidationInterval !== null &&\n config.realtime.consolidationInterval > 0\n ) {\n try {\n // Create memory instance\n memory = await createKVMemory('.sparn/memory.db');\n\n // Create and start scheduler\n scheduler = createConsolidationScheduler({\n memory,\n config,\n logFile,\n });\n\n scheduler.start();\n log(\n `Consolidation scheduler started (interval: ${config.realtime.consolidationInterval}h)`,\n );\n } catch (error) {\n log(\n `Failed to start consolidation scheduler: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n })\n .catch((error) => {\n log(`Failed to start: ${error.message}`);\n cleanup();\n });\n\n// Keep process alive\nsetInterval(() => {\n // Heartbeat (could be used for health checks)\n}, 60000); // Every minute\n","/**\n * KV Memory Store Module\n * Implements hippocampal key-value storage with dual index/value tables.\n * Maps to: Hippocampal Key-Value — the hippocampus separates what to store from how to retrieve it.\n */\n\nimport { copyFileSync, existsSync } from 'node:fs';\nimport Database from 'better-sqlite3';\nimport type { MemoryEntry, MemoryQueryFilters } from '../types/memory.js';\n\n/**\n * Optimization statistics record.\n */\nexport interface OptimizationStats {\n id: number;\n timestamp: number;\n tokens_before: number;\n tokens_after: number;\n entries_pruned: number;\n duration_ms: number;\n}\n\n/**\n * KV Memory interface.\n */\nexport interface KVMemory {\n /** Store a memory entry */\n put(entry: MemoryEntry): Promise<void>;\n\n /** Retrieve a memory entry by ID */\n get(id: string): Promise<MemoryEntry | null>;\n\n /** Query entries by filters */\n query(filters: MemoryQueryFilters): Promise<MemoryEntry[]>;\n\n /** Delete a memory entry */\n delete(id: string): Promise<void>;\n\n /** List all entry IDs */\n list(): Promise<string[]>;\n\n /** Compact database (remove expired entries) */\n compact(): Promise<number>;\n\n /** Close database connection */\n close(): Promise<void>;\n\n /** Record optimization statistics */\n recordOptimization(stats: Omit<OptimizationStats, 'id'>): Promise<void>;\n\n /** Get all optimization statistics */\n getOptimizationStats(): Promise<OptimizationStats[]>;\n\n /** Clear all optimization statistics */\n clearOptimizationStats(): Promise<void>;\n}\n\n/**\n * Create a timestamped backup of the database\n * @param dbPath - Path to database file\n * @returns Path to backup file\n */\nfunction createBackup(dbPath: string): string {\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\n const backupPath = `${dbPath}.backup-${timestamp}`;\n\n try {\n copyFileSync(dbPath, backupPath);\n console.log(`✓ Database backed up to: ${backupPath}`);\n return backupPath;\n } catch (error) {\n console.error(`Warning: Could not create backup: ${error}`);\n return '';\n }\n}\n\n/**\n * Create KV Memory store with SQLite backend.\n *\n * Initializes database with dual table schema:\n * - entries_index: Fast lookups (id, hash, timestamp, score, ttl, state, accessCount, isBTSP)\n * - entries_value: Content storage (id, content, tags, metadata)\n *\n * @param dbPath - Path to SQLite database file\n * @returns KVMemory instance\n */\nexport async function createKVMemory(dbPath: string): Promise<KVMemory> {\n // Detect database corruption and create backup\n let db: Database.Database;\n try {\n db = new Database(dbPath);\n\n // Quick integrity check\n const integrityCheck = db.pragma('quick_check', { simple: true });\n if (integrityCheck !== 'ok') {\n console.error('⚠ Database corruption detected!');\n\n // Create backup before attempting recovery\n if (existsSync(dbPath)) {\n const backupPath = createBackup(dbPath);\n if (backupPath) {\n console.log(`Backup created at: ${backupPath}`);\n }\n }\n\n // Try to recover\n console.log('Attempting database recovery...');\n db.close();\n db = new Database(dbPath);\n }\n } catch (error) {\n console.error('⚠ Database error detected:', error);\n\n // Create backup if database exists\n if (existsSync(dbPath)) {\n createBackup(dbPath);\n console.log('Creating new database...');\n }\n\n db = new Database(dbPath);\n }\n\n // Enable WAL mode for better concurrency\n db.pragma('journal_mode = WAL');\n\n // Create entries_index table\n db.exec(`\n CREATE TABLE IF NOT EXISTS entries_index (\n id TEXT PRIMARY KEY NOT NULL,\n hash TEXT UNIQUE NOT NULL,\n timestamp INTEGER NOT NULL,\n score REAL NOT NULL DEFAULT 0.0 CHECK(score >= 0.0 AND score <= 1.0),\n ttl INTEGER NOT NULL CHECK(ttl >= 0),\n state TEXT NOT NULL CHECK(state IN ('silent', 'ready', 'active')),\n accessCount INTEGER NOT NULL DEFAULT 0 CHECK(accessCount >= 0),\n isBTSP INTEGER NOT NULL DEFAULT 0 CHECK(isBTSP IN (0, 1)),\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))\n );\n `);\n\n // Create entries_value table\n db.exec(`\n CREATE TABLE IF NOT EXISTS entries_value (\n id TEXT PRIMARY KEY NOT NULL,\n content TEXT NOT NULL,\n tags TEXT,\n metadata TEXT,\n FOREIGN KEY (id) REFERENCES entries_index(id) ON DELETE CASCADE\n );\n `);\n\n // Create optimization_stats table\n db.exec(`\n CREATE TABLE IF NOT EXISTS optimization_stats (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n timestamp INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n tokens_before INTEGER NOT NULL,\n tokens_after INTEGER NOT NULL,\n entries_pruned INTEGER NOT NULL,\n duration_ms INTEGER NOT NULL\n );\n `);\n\n // Create indexes\n db.exec(`\n CREATE INDEX IF NOT EXISTS idx_entries_state ON entries_index(state);\n CREATE INDEX IF NOT EXISTS idx_entries_score ON entries_index(score DESC);\n CREATE INDEX IF NOT EXISTS idx_entries_hash ON entries_index(hash);\n CREATE INDEX IF NOT EXISTS idx_entries_timestamp ON entries_index(timestamp DESC);\n CREATE INDEX IF NOT EXISTS idx_stats_timestamp ON optimization_stats(timestamp DESC);\n `);\n\n // Prepare statements for better performance\n const putIndexStmt = db.prepare(`\n INSERT OR REPLACE INTO entries_index\n (id, hash, timestamp, score, ttl, state, accessCount, isBTSP)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)\n `);\n\n const putValueStmt = db.prepare(`\n INSERT OR REPLACE INTO entries_value\n (id, content, tags, metadata)\n VALUES (?, ?, ?, ?)\n `);\n\n const getStmt = db.prepare(`\n SELECT\n i.id, i.hash, i.timestamp, i.score, i.ttl, i.state, i.accessCount, i.isBTSP,\n v.content, v.tags, v.metadata\n FROM entries_index i\n JOIN entries_value v ON i.id = v.id\n WHERE i.id = ?\n `);\n\n const deleteIndexStmt = db.prepare('DELETE FROM entries_index WHERE id = ?');\n const deleteValueStmt = db.prepare('DELETE FROM entries_value WHERE id = ?');\n\n return {\n async put(entry: MemoryEntry): Promise<void> {\n const transaction = db.transaction(() => {\n putIndexStmt.run(\n entry.id,\n entry.hash,\n entry.timestamp,\n entry.score,\n entry.ttl,\n entry.state,\n entry.accessCount,\n entry.isBTSP ? 1 : 0,\n );\n\n putValueStmt.run(\n entry.id,\n entry.content,\n JSON.stringify(entry.tags),\n JSON.stringify(entry.metadata),\n );\n });\n\n transaction();\n },\n\n async get(id: string): Promise<MemoryEntry | null> {\n const row = getStmt.get(id) as unknown;\n\n if (!row) {\n return null;\n }\n\n const r = row as {\n id: string;\n hash: string;\n timestamp: number;\n score: number;\n ttl: number;\n state: string;\n accessCount: number;\n isBTSP: number;\n content: string;\n tags: string | null;\n metadata: string | null;\n };\n\n return {\n id: r.id,\n content: r.content,\n hash: r.hash,\n timestamp: r.timestamp,\n score: r.score,\n ttl: r.ttl,\n state: r.state as 'silent' | 'ready' | 'active',\n accessCount: r.accessCount,\n tags: r.tags ? JSON.parse(r.tags) : [],\n metadata: r.metadata ? JSON.parse(r.metadata) : {},\n isBTSP: r.isBTSP === 1,\n };\n },\n\n async query(filters: MemoryQueryFilters): Promise<MemoryEntry[]> {\n let sql = `\n SELECT\n i.id, i.hash, i.timestamp, i.score, i.ttl, i.state, i.accessCount, i.isBTSP,\n v.content, v.tags, v.metadata\n FROM entries_index i\n JOIN entries_value v ON i.id = v.id\n WHERE 1=1\n `;\n\n const params: unknown[] = [];\n\n if (filters.state) {\n sql += ' AND i.state = ?';\n params.push(filters.state);\n }\n\n if (filters.minScore !== undefined) {\n sql += ' AND i.score >= ?';\n params.push(filters.minScore);\n }\n\n if (filters.maxScore !== undefined) {\n sql += ' AND i.score <= ?';\n params.push(filters.maxScore);\n }\n\n if (filters.isBTSP !== undefined) {\n sql += ' AND i.isBTSP = ?';\n params.push(filters.isBTSP ? 1 : 0);\n }\n\n sql += ' ORDER BY i.score DESC';\n\n if (filters.limit) {\n sql += ' LIMIT ?';\n params.push(filters.limit);\n }\n\n if (filters.offset) {\n sql += ' OFFSET ?';\n params.push(filters.offset);\n }\n\n const stmt = db.prepare(sql);\n const rows = stmt.all(...params) as unknown[];\n\n return rows.map((row) => {\n const r = row as {\n id: string;\n hash: string;\n timestamp: number;\n score: number;\n ttl: number;\n state: string;\n accessCount: number;\n isBTSP: number;\n content: string;\n tags: string | null;\n metadata: string | null;\n };\n\n return {\n id: r.id,\n content: r.content,\n hash: r.hash,\n timestamp: r.timestamp,\n score: r.score,\n ttl: r.ttl,\n state: r.state as 'silent' | 'ready' | 'active',\n accessCount: r.accessCount,\n tags: r.tags ? JSON.parse(r.tags) : [],\n metadata: r.metadata ? JSON.parse(r.metadata) : {},\n isBTSP: r.isBTSP === 1,\n };\n });\n },\n\n async delete(id: string): Promise<void> {\n const transaction = db.transaction(() => {\n deleteIndexStmt.run(id);\n deleteValueStmt.run(id);\n });\n\n transaction();\n },\n\n async list(): Promise<string[]> {\n const stmt = db.prepare('SELECT id FROM entries_index');\n const rows = stmt.all() as { id: string }[];\n return rows.map((r) => r.id);\n },\n\n async compact(): Promise<number> {\n const before = db.prepare('SELECT COUNT(*) as count FROM entries_index').get() as {\n count: number;\n };\n\n // Remove fully decayed entries (this will be enhanced in sleep-compressor)\n db.exec('DELETE FROM entries_index WHERE ttl <= 0');\n\n db.exec('VACUUM');\n\n const after = db.prepare('SELECT COUNT(*) as count FROM entries_index').get() as {\n count: number;\n };\n\n return before.count - after.count;\n },\n\n async close(): Promise<void> {\n db.close();\n },\n\n async recordOptimization(stats: Omit<OptimizationStats, 'id'>): Promise<void> {\n const stmt = db.prepare(`\n INSERT INTO optimization_stats (timestamp, tokens_before, tokens_after, entries_pruned, duration_ms)\n VALUES (?, ?, ?, ?, ?)\n `);\n\n stmt.run(\n stats.timestamp,\n stats.tokens_before,\n stats.tokens_after,\n stats.entries_pruned,\n stats.duration_ms,\n );\n },\n\n async getOptimizationStats(): Promise<OptimizationStats[]> {\n const stmt = db.prepare(`\n SELECT id, timestamp, tokens_before, tokens_after, entries_pruned, duration_ms\n FROM optimization_stats\n ORDER BY timestamp DESC\n `);\n\n const rows = stmt.all() as OptimizationStats[];\n return rows;\n },\n\n async clearOptimizationStats(): Promise<void> {\n db.exec('DELETE FROM optimization_stats');\n },\n };\n}\n","/**\n * Consolidation Scheduler - Periodic memory consolidation\n *\n * Handles:\n * - Scheduled consolidation using setInterval\n * - Integration with existing consolidate command\n * - Logging to daemon log file\n * - Metrics tracking for consolidation runs\n */\n\nimport { appendFileSync } from 'node:fs';\nimport { consolidateCommand } from '../cli/commands/consolidate.js';\nimport type { KVMemory } from '../core/kv-memory.js';\nimport { getMetrics } from '../core/metrics.js';\nimport type { SparnConfig } from '../types/config.js';\n\nexport interface ConsolidationScheduler {\n /**\n * Start the scheduler\n */\n start(): void;\n\n /**\n * Stop the scheduler\n */\n stop(): void;\n\n /**\n * Get scheduler status\n */\n getStatus(): ConsolidationSchedulerStatus;\n}\n\nexport interface ConsolidationSchedulerStatus {\n /** Is the scheduler running */\n running: boolean;\n /** Interval in hours */\n intervalHours: number | null;\n /** Next run timestamp (null if not running) */\n nextRun: number | null;\n /** Total consolidations run */\n totalRuns: number;\n /** Last run timestamp (null if never run) */\n lastRun: number | null;\n /** Last run result (null if never run) */\n lastResult: ConsolidationResult | null;\n}\n\nexport interface ConsolidationResult {\n /** Timestamp of consolidation */\n timestamp: number;\n /** Entries before consolidation */\n entriesBefore: number;\n /** Entries after consolidation */\n entriesAfter: number;\n /** Decayed entries removed */\n decayedRemoved: number;\n /** Duplicate entries merged */\n duplicatesRemoved: number;\n /** Compression ratio (0.0-1.0) */\n compressionRatio: number;\n /** Duration in milliseconds */\n durationMs: number;\n /** Success flag */\n success: boolean;\n /** Error message if failed */\n error?: string;\n}\n\nexport interface ConsolidationSchedulerOptions {\n /** Memory store instance */\n memory: KVMemory;\n /** Sparn configuration */\n config: SparnConfig;\n /** Log file path (optional, defaults to daemon log) */\n logFile?: string;\n}\n\n/**\n * Create a consolidation scheduler instance\n * @param options - Scheduler options\n * @returns ConsolidationScheduler instance\n */\nexport function createConsolidationScheduler(\n options: ConsolidationSchedulerOptions,\n): ConsolidationScheduler {\n const { memory, config, logFile } = options;\n const intervalHours = config.realtime.consolidationInterval;\n\n let timerId: NodeJS.Timeout | null = null;\n let totalRuns = 0;\n let lastRun: number | null = null;\n let lastResult: ConsolidationResult | null = null;\n let nextRun: number | null = null;\n\n /**\n * Log helper\n */\n function log(message: string): void {\n const timestamp = new Date().toISOString();\n const logMessage = `[${timestamp}] [Consolidation] ${message}\\n`;\n\n // Log to daemon log file if available\n const logPath = logFile || config.realtime.logFile;\n if (logPath) {\n try {\n appendFileSync(logPath, logMessage, 'utf-8');\n } catch {\n // Fail silently if can't write log\n }\n }\n }\n\n /**\n * Run consolidation\n */\n async function runConsolidation(): Promise<void> {\n const startTime = Date.now();\n log('Starting scheduled consolidation');\n\n try {\n const result = await consolidateCommand({ memory });\n\n // Record success\n lastRun = startTime;\n totalRuns++;\n lastResult = {\n timestamp: startTime,\n entriesBefore: result.entriesBefore,\n entriesAfter: result.entriesAfter,\n decayedRemoved: result.decayedRemoved,\n duplicatesRemoved: result.duplicatesRemoved,\n compressionRatio: result.compressionRatio,\n durationMs: result.durationMs,\n success: true,\n };\n\n // Log result\n log(\n `Consolidation completed: ${result.entriesBefore} -> ${result.entriesAfter} entries ` +\n `(${result.decayedRemoved} decayed, ${result.duplicatesRemoved} duplicates, ` +\n `${Math.round(result.compressionRatio * 100)}% compression) in ${result.durationMs}ms`,\n );\n\n // Update metrics\n const metrics = getMetrics();\n metrics.recordOptimization({\n timestamp: startTime,\n duration: result.durationMs,\n tokensBefore: result.entriesBefore * 100, // Rough estimate\n tokensAfter: result.entriesAfter * 100, // Rough estimate\n entriesProcessed: result.entriesBefore,\n entriesKept: result.entriesAfter,\n cacheHitRate: 0, // N/A for consolidation\n memoryUsage: process.memoryUsage().heapUsed,\n });\n } catch (error) {\n // Record failure\n lastRun = startTime;\n totalRuns++;\n lastResult = {\n timestamp: startTime,\n entriesBefore: 0,\n entriesAfter: 0,\n decayedRemoved: 0,\n duplicatesRemoved: 0,\n compressionRatio: 0,\n durationMs: Date.now() - startTime,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n };\n\n log(`Consolidation failed: ${lastResult.error}`);\n }\n\n // Update next run time\n if (intervalHours !== null && intervalHours > 0) {\n nextRun = Date.now() + intervalHours * 60 * 60 * 1000;\n }\n }\n\n function start(): void {\n // Check if interval is configured\n if (intervalHours === null || intervalHours <= 0) {\n log('Consolidation scheduler disabled (consolidationInterval not set)');\n return;\n }\n\n // Check if already running\n if (timerId !== null) {\n log('Consolidation scheduler already running');\n return;\n }\n\n // Calculate interval in milliseconds\n const intervalMs = intervalHours * 60 * 60 * 1000;\n\n // Set up periodic timer\n timerId = setInterval(() => {\n void runConsolidation();\n }, intervalMs);\n\n // Calculate next run\n nextRun = Date.now() + intervalMs;\n\n log(\n `Consolidation scheduler started (interval: ${intervalHours}h, next run: ${new Date(nextRun).toISOString()})`,\n );\n }\n\n function stop(): void {\n if (timerId !== null) {\n clearInterval(timerId);\n timerId = null;\n nextRun = null;\n log('Consolidation scheduler stopped');\n }\n }\n\n function getStatus(): ConsolidationSchedulerStatus {\n return {\n running: timerId !== null,\n intervalHours,\n nextRun,\n totalRuns,\n lastRun,\n lastResult,\n };\n }\n\n return {\n start,\n stop,\n getStatus,\n };\n}\n","/**\n * Engram Scorer - Implements engram theory (memory decay)\n *\n * Neuroscience: Memories fade over time without reinforcement.\n * Application: Apply exponential decay formula to memory scores based on age and access count.\n *\n * Formula: decay = 1 - e^(-age/TTL)\n * Score adjustment: score_new = score_old * (1 - decay) + (accessCount bonus)\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\n\nexport interface EngramScorerConfig {\n /** Default TTL in hours for new entries */\n defaultTTL: number;\n /** Decay threshold (0.0-1.0) above which entries are marked for pruning */\n decayThreshold: number;\n}\n\nexport interface EngramScorer {\n /**\n * Calculate current score for an entry based on decay and access count\n * @param entry - Memory entry to score\n * @param currentTime - Current timestamp in milliseconds (for testing)\n * @returns Updated score (0.0-1.0)\n */\n calculateScore(entry: MemoryEntry, currentTime?: number): number;\n\n /**\n * Refresh TTL to default value\n * @param entry - Entry to refresh\n * @returns Entry with refreshed TTL and timestamp\n */\n refreshTTL(entry: MemoryEntry): MemoryEntry;\n\n /**\n * Calculate decay factor (0.0-1.0) based on age and TTL\n * @param ageInSeconds - Age of entry in seconds\n * @param ttlInSeconds - TTL in seconds\n * @returns Decay factor (0.0 = fresh, 1.0 = fully decayed)\n */\n calculateDecay(ageInSeconds: number, ttlInSeconds: number): number;\n}\n\n/**\n * Create an engram scorer instance\n * @param config - Scorer configuration\n * @returns EngramScorer instance\n */\nexport function createEngramScorer(config: EngramScorerConfig): EngramScorer {\n const { defaultTTL } = config;\n\n function calculateDecay(ageInSeconds: number, ttlInSeconds: number): number {\n if (ttlInSeconds === 0) return 1.0; // Instant decay\n if (ageInSeconds <= 0) return 0.0; // Fresh entry\n\n // Exponential decay: 1 - e^(-age/TTL)\n const ratio = ageInSeconds / ttlInSeconds;\n const decay = 1 - Math.exp(-ratio);\n\n // Clamp to [0.0, 1.0]\n return Math.max(0, Math.min(1, decay));\n }\n\n function calculateScore(entry: MemoryEntry, currentTime: number = Date.now()): number {\n // Calculate age in seconds\n const ageInMilliseconds = currentTime - entry.timestamp;\n const ageInSeconds = Math.max(0, ageInMilliseconds / 1000);\n\n // Calculate decay factor\n const decay = calculateDecay(ageInSeconds, entry.ttl);\n\n // Base score reduced by decay\n let score = entry.score * (1 - decay);\n\n // Access count bonus (diminishing returns via log)\n if (entry.accessCount > 0) {\n const accessBonus = Math.log(entry.accessCount + 1) * 0.1;\n score = Math.min(1.0, score + accessBonus);\n }\n\n // BTSP entries maintain high score\n if (entry.isBTSP) {\n score = Math.max(score, 0.9);\n }\n\n return Math.max(0, Math.min(1, score));\n }\n\n function refreshTTL(entry: MemoryEntry): MemoryEntry {\n return {\n ...entry,\n ttl: defaultTTL * 3600, // Convert hours to seconds\n timestamp: Date.now(),\n };\n }\n\n return {\n calculateScore,\n refreshTTL,\n calculateDecay,\n };\n}\n","/**\n * Sleep Compressor - Implements sleep replay principle\n *\n * Neuroscience: During sleep, the brain consolidates memories by replaying important ones\n * and discarding irrelevant information.\n * Application: Periodic consolidation removes decayed entries and merges duplicates.\n */\n\nimport type { ConsolidateResult, DuplicateGroup } from '../types/consolidate.js';\nimport type { MemoryEntry } from '../types/memory.js';\nimport { createEngramScorer } from './engram-scorer.js';\n\nexport interface SleepCompressor {\n /**\n * Consolidate entries: remove decayed, merge duplicates\n * @param entries - All memory entries\n * @returns Consolidation result\n */\n consolidate(entries: MemoryEntry[]): ConsolidateResult;\n\n /**\n * Find duplicate entries (exact hash or near-duplicate by similarity)\n * @param entries - Memory entries\n * @returns Groups of duplicates\n */\n findDuplicates(entries: MemoryEntry[]): DuplicateGroup[];\n\n /**\n * Merge duplicate entries, keeping highest score\n * @param groups - Duplicate groups\n * @returns Merged entries\n */\n mergeDuplicates(groups: DuplicateGroup[]): MemoryEntry[];\n}\n\n/**\n * Create a sleep compressor instance\n * @returns SleepCompressor instance\n */\nexport function createSleepCompressor(): SleepCompressor {\n const scorer = createEngramScorer({ defaultTTL: 24, decayThreshold: 0.95 });\n\n function consolidate(entries: MemoryEntry[]): ConsolidateResult {\n const startTime = Date.now();\n const originalCount = entries.length;\n\n // Step 1: Remove fully decayed entries (decay ≥ 0.95)\n const now = Date.now();\n const nonDecayed = entries.filter((entry) => {\n const ageInSeconds = (now - entry.timestamp) / 1000;\n const decay = scorer.calculateDecay(ageInSeconds, entry.ttl);\n return decay < 0.95; // Keep entries with decay < 0.95\n });\n\n const decayedRemoved = originalCount - nonDecayed.length;\n\n // Step 2: Find and merge duplicates\n const duplicateGroups = findDuplicates(nonDecayed);\n const merged = mergeDuplicates(duplicateGroups);\n\n // Step 3: Keep non-duplicates\n const duplicateIds = new Set(duplicateGroups.flatMap((g) => g.entries.map((e) => e.id)));\n const nonDuplicates = nonDecayed.filter((e) => !duplicateIds.has(e.id));\n\n // Combine merged duplicates with non-duplicates\n const kept = [...merged, ...nonDuplicates];\n const removed = entries.filter((e) => !kept.some((k) => k.id === e.id));\n\n const duplicatesRemoved = duplicateGroups.reduce((sum, g) => sum + (g.entries.length - 1), 0);\n\n return {\n kept,\n removed,\n entriesBefore: originalCount,\n entriesAfter: kept.length,\n decayedRemoved,\n duplicatesRemoved,\n compressionRatio: originalCount > 0 ? kept.length / originalCount : 0,\n durationMs: Date.now() - startTime,\n };\n }\n\n function findDuplicates(entries: MemoryEntry[]): DuplicateGroup[] {\n const groups: DuplicateGroup[] = [];\n const processed = new Set<string>();\n\n // Find exact hash matches\n for (let i = 0; i < entries.length; i++) {\n const entry = entries[i];\n if (!entry || processed.has(entry.id)) continue;\n\n const duplicates = entries.filter((e, idx) => idx !== i && e.hash === entry.hash);\n\n if (duplicates.length > 0) {\n const group: DuplicateGroup = {\n entries: [entry, ...duplicates],\n similarity: 1.0, // Exact match\n };\n groups.push(group);\n\n // Mark as processed\n processed.add(entry.id);\n for (const dup of duplicates) {\n processed.add(dup.id);\n }\n }\n }\n\n // Find near-duplicates (cosine similarity ≥ 0.85)\n for (let i = 0; i < entries.length; i++) {\n const entryI = entries[i];\n if (!entryI || processed.has(entryI.id)) continue;\n\n for (let j = i + 1; j < entries.length; j++) {\n const entryJ = entries[j];\n if (!entryJ || processed.has(entryJ.id)) continue;\n\n const similarity = cosineSimilarity(entryI.content, entryJ.content);\n\n if (similarity >= 0.85) {\n const group: DuplicateGroup = {\n entries: [entryI, entryJ],\n similarity,\n };\n groups.push(group);\n\n processed.add(entryI.id);\n processed.add(entryJ.id);\n break; // Move to next i\n }\n }\n }\n\n return groups;\n }\n\n function mergeDuplicates(groups: DuplicateGroup[]): MemoryEntry[] {\n const merged: MemoryEntry[] = [];\n\n for (const group of groups) {\n // Keep entry with highest score\n const sorted = [...group.entries].sort((a, b) => b.score - a.score);\n const best = sorted[0];\n if (!best) continue; // Skip empty groups\n\n // Sum access counts\n const totalAccessCount = group.entries.reduce((sum, e) => sum + e.accessCount, 0);\n\n // Merge tags\n const allTags = new Set(group.entries.flatMap((e) => e.tags));\n\n merged.push({\n ...best,\n accessCount: totalAccessCount,\n tags: Array.from(allTags),\n });\n }\n\n return merged;\n }\n\n /**\n * Calculate cosine similarity between two text strings\n * @param text1 - First text\n * @param text2 - Second text\n * @returns Similarity score (0.0-1.0)\n */\n function cosineSimilarity(text1: string, text2: string): number {\n const words1 = tokenize(text1);\n const words2 = tokenize(text2);\n\n // Build vocabulary\n const vocab = new Set([...words1, ...words2]);\n\n // Build word frequency vectors\n const vec1: Record<string, number> = {};\n const vec2: Record<string, number> = {};\n\n for (const word of vocab) {\n vec1[word] = words1.filter((w) => w === word).length;\n vec2[word] = words2.filter((w) => w === word).length;\n }\n\n // Calculate dot product and magnitudes\n let dotProduct = 0;\n let mag1 = 0;\n let mag2 = 0;\n\n for (const word of vocab) {\n const count1 = vec1[word] ?? 0;\n const count2 = vec2[word] ?? 0;\n dotProduct += count1 * count2;\n mag1 += count1 * count1;\n mag2 += count2 * count2;\n }\n\n mag1 = Math.sqrt(mag1);\n mag2 = Math.sqrt(mag2);\n\n if (mag1 === 0 || mag2 === 0) return 0;\n\n return dotProduct / (mag1 * mag2);\n }\n\n function tokenize(text: string): string[] {\n return text\n .toLowerCase()\n .split(/\\s+/)\n .filter((word) => word.length > 0);\n }\n\n return {\n consolidate,\n findDuplicates,\n mergeDuplicates,\n };\n}\n","/**\n * Consolidate Command - Periodic memory consolidation\n */\n\nimport type { KVMemory } from '../../core/kv-memory.js';\nimport { createSleepCompressor } from '../../core/sleep-compressor.js';\n\nexport interface ConsolidateCommandOptions {\n /** Memory store instance */\n memory: KVMemory;\n}\n\nexport interface ConsolidateCommandResult {\n /** Entries before consolidation */\n entriesBefore: number;\n /** Entries after consolidation */\n entriesAfter: number;\n /** Decayed entries removed */\n decayedRemoved: number;\n /** Duplicate entries merged */\n duplicatesRemoved: number;\n /** Compression ratio (0.0-1.0) */\n compressionRatio: number;\n /** Duration in milliseconds */\n durationMs: number;\n /** VACUUM completed */\n vacuumCompleted: boolean;\n}\n\n/**\n * Execute the consolidate command\n * @param options - Command options\n * @returns Consolidation result\n */\nexport async function consolidateCommand(\n options: ConsolidateCommandOptions,\n): Promise<ConsolidateCommandResult> {\n const { memory } = options;\n\n // Get all entries from memory\n const allIds = await memory.list();\n const allEntries = await Promise.all(\n allIds.map(async (id) => {\n const entry = await memory.get(id);\n return entry;\n }),\n );\n\n // Filter out nulls\n const entries = allEntries.filter((e) => e !== null);\n\n // Run consolidation\n const compressor = createSleepCompressor();\n const result = compressor.consolidate(entries);\n\n // Update memory: remove old entries, keep consolidated ones\n for (const removed of result.removed) {\n await memory.delete(removed.id);\n }\n\n for (const kept of result.kept) {\n await memory.put(kept);\n }\n\n // Run VACUUM to reclaim space\n await memory.compact();\n\n return {\n entriesBefore: result.entriesBefore,\n entriesAfter: result.entriesAfter,\n decayedRemoved: result.decayedRemoved,\n duplicatesRemoved: result.duplicatesRemoved,\n compressionRatio: result.compressionRatio,\n durationMs: result.durationMs,\n vacuumCompleted: true,\n };\n}\n","/**\n * Metrics and Telemetry System\n *\n * Tracks performance metrics and optimization statistics:\n * - Optimization duration and throughput\n * - Token savings and reduction rates\n * - Memory usage and cache hit rates\n * - Daemon uptime and session counts\n */\n\nexport interface OptimizationMetric {\n timestamp: number;\n duration: number;\n tokensBefore: number;\n tokensAfter: number;\n entriesProcessed: number;\n entriesKept: number;\n cacheHitRate: number;\n memoryUsage: number;\n}\n\nexport interface DaemonMetric {\n startTime: number;\n sessionsWatched: number;\n totalOptimizations: number;\n totalTokensSaved: number;\n averageLatency: number;\n memoryUsage: number;\n}\n\nexport interface MetricsSnapshot {\n timestamp: number;\n optimization: {\n totalRuns: number;\n totalDuration: number;\n totalTokensSaved: number;\n averageReduction: number;\n p50Latency: number;\n p95Latency: number;\n p99Latency: number;\n };\n cache: {\n hitRate: number;\n totalHits: number;\n totalMisses: number;\n size: number;\n };\n daemon: {\n uptime: number;\n sessionsWatched: number;\n memoryUsage: number;\n };\n}\n\nexport interface MetricsCollector {\n /**\n * Record an optimization metric\n */\n recordOptimization(metric: OptimizationMetric): void;\n\n /**\n * Update daemon metrics\n */\n updateDaemon(metric: Partial<DaemonMetric>): void;\n\n /**\n * Get current metrics snapshot\n */\n getSnapshot(): MetricsSnapshot;\n\n /**\n * Export metrics as JSON\n */\n export(): string;\n\n /**\n * Reset all metrics\n */\n reset(): void;\n}\n\n/**\n * Create a metrics collector instance\n */\nexport function createMetricsCollector(): MetricsCollector {\n const optimizations: OptimizationMetric[] = [];\n let daemonMetrics: DaemonMetric = {\n startTime: Date.now(),\n sessionsWatched: 0,\n totalOptimizations: 0,\n totalTokensSaved: 0,\n averageLatency: 0,\n memoryUsage: 0,\n };\n\n let cacheHits = 0;\n let cacheMisses = 0;\n\n function recordOptimization(metric: OptimizationMetric): void {\n optimizations.push(metric);\n\n // Update daemon totals\n daemonMetrics.totalOptimizations++;\n daemonMetrics.totalTokensSaved += metric.tokensBefore - metric.tokensAfter;\n\n // Update cache stats\n if (metric.cacheHitRate > 0) {\n const hits = Math.round(metric.entriesProcessed * metric.cacheHitRate);\n cacheHits += hits;\n cacheMisses += metric.entriesProcessed - hits;\n }\n\n // Update average latency (moving average)\n daemonMetrics.averageLatency =\n (daemonMetrics.averageLatency * (daemonMetrics.totalOptimizations - 1) + metric.duration) /\n daemonMetrics.totalOptimizations;\n\n // Keep only last 1000 metrics in memory\n if (optimizations.length > 1000) {\n optimizations.shift();\n }\n }\n\n function updateDaemon(metric: Partial<DaemonMetric>): void {\n daemonMetrics = {\n ...daemonMetrics,\n ...metric,\n };\n }\n\n function calculatePercentile(values: number[], percentile: number): number {\n if (values.length === 0) return 0;\n\n const sorted = [...values].sort((a, b) => a - b);\n const index = Math.ceil((percentile / 100) * sorted.length) - 1;\n return sorted[index] || 0;\n }\n\n function getSnapshot(): MetricsSnapshot {\n const totalRuns = optimizations.length;\n const totalDuration = optimizations.reduce((sum, m) => sum + m.duration, 0);\n const totalTokensSaved = optimizations.reduce(\n (sum, m) => sum + (m.tokensBefore - m.tokensAfter),\n 0,\n );\n\n const totalTokensBefore = optimizations.reduce((sum, m) => sum + m.tokensBefore, 0);\n const averageReduction = totalTokensBefore > 0 ? totalTokensSaved / totalTokensBefore : 0;\n\n const durations = optimizations.map((m) => m.duration);\n\n const totalCacheQueries = cacheHits + cacheMisses;\n const hitRate = totalCacheQueries > 0 ? cacheHits / totalCacheQueries : 0;\n\n return {\n timestamp: Date.now(),\n optimization: {\n totalRuns,\n totalDuration,\n totalTokensSaved,\n averageReduction,\n p50Latency: calculatePercentile(durations, 50),\n p95Latency: calculatePercentile(durations, 95),\n p99Latency: calculatePercentile(durations, 99),\n },\n cache: {\n hitRate,\n totalHits: cacheHits,\n totalMisses: cacheMisses,\n size: optimizations.reduce((sum, m) => sum + m.entriesKept, 0),\n },\n daemon: {\n uptime: Date.now() - daemonMetrics.startTime,\n sessionsWatched: daemonMetrics.sessionsWatched,\n memoryUsage: daemonMetrics.memoryUsage,\n },\n };\n }\n\n function exportMetrics(): string {\n return JSON.stringify(getSnapshot(), null, 2);\n }\n\n function reset(): void {\n optimizations.length = 0;\n cacheHits = 0;\n cacheMisses = 0;\n daemonMetrics = {\n startTime: Date.now(),\n sessionsWatched: 0,\n totalOptimizations: 0,\n totalTokensSaved: 0,\n averageLatency: 0,\n memoryUsage: 0,\n };\n }\n\n return {\n recordOptimization,\n updateDaemon,\n getSnapshot,\n export: exportMetrics,\n reset,\n };\n}\n\n// Global metrics instance\nlet globalMetrics: MetricsCollector | null = null;\n\n/**\n * Get or create the global metrics collector\n */\nexport function getMetrics(): MetricsCollector {\n if (!globalMetrics) {\n globalMetrics = createMetricsCollector();\n }\n return globalMetrics;\n}\n","/**\n * Session Watcher - Monitor Claude Code session files for changes\n *\n * Uses Node.js fs.watch to monitor ~/.claude/projects/**\\/*.jsonl files.\n * Debounces events and triggers optimization when token threshold exceeded.\n * Maintains per-session ContextPipeline instances.\n */\n\nimport { type FSWatcher, readdirSync, statSync, watch } from 'node:fs';\nimport { homedir } from 'node:os';\nimport { dirname, join } from 'node:path';\nimport { type ContextPipeline, createContextPipeline } from '../core/context-pipeline.js';\nimport { getMetrics } from '../core/metrics.js';\nimport type { SparnConfig } from '../types/config.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { createFileTracker } from './file-tracker.js';\n\nexport interface SessionWatcherConfig {\n /** Sparn configuration */\n config: SparnConfig;\n /** Callback when optimization triggered */\n onOptimize?: (sessionId: string, stats: SessionStats) => void;\n /** Callback on error */\n onError?: (error: Error) => void;\n}\n\nexport interface SessionStats {\n /** Session ID */\n sessionId: string;\n /** Total tokens ingested */\n totalTokens: number;\n /** Current optimized tokens */\n optimizedTokens: number;\n /** Reduction percentage */\n reduction: number;\n /** Entry count */\n entryCount: number;\n /** Budget utilization */\n budgetUtilization: number;\n}\n\nexport interface SessionWatcher {\n /**\n * Start watching Claude Code session files\n * @returns Promise that resolves when watcher is ready\n */\n start(): Promise<void>;\n\n /**\n * Stop watching and cleanup\n */\n stop(): void;\n\n /**\n * Get statistics for all sessions\n * @returns Array of session stats\n */\n getStats(): SessionStats[];\n\n /**\n * Get statistics for a specific session\n * @param sessionId - Session ID\n * @returns Session stats or null if not found\n */\n getSessionStats(sessionId: string): SessionStats | null;\n\n /**\n * Manually trigger optimization for a session\n * @param sessionId - Session ID\n */\n optimizeSession(sessionId: string): void;\n}\n\n/**\n * Create a session watcher instance\n * @param config - Watcher configuration\n * @returns SessionWatcher instance\n */\nexport function createSessionWatcher(config: SessionWatcherConfig): SessionWatcher {\n const { config: sparnConfig, onOptimize, onError } = config;\n const { realtime, decay, states } = sparnConfig;\n\n // Per-session pipelines and trackers\n const pipelines = new Map<string, ContextPipeline>();\n const fileTracker = createFileTracker();\n\n // File system watchers\n const watchers: FSWatcher[] = [];\n\n // Debounce timers per file\n const debounceTimers = new Map<string, NodeJS.Timeout>();\n\n /**\n * Get Claude Code projects directory\n */\n function getProjectsDir(): string {\n return join(homedir(), '.claude', 'projects');\n }\n\n /**\n * Extract session ID from file path\n * Example: ~/.claude/projects/my-project/abc123.jsonl -> abc123\n */\n function getSessionId(filePath: string): string {\n const filename = filePath.split(/[/\\\\]/).pop() || '';\n return filename.replace(/\\.jsonl$/, '');\n }\n\n /**\n * Get or create pipeline for session\n */\n function getPipeline(sessionId: string): ContextPipeline {\n let pipeline = pipelines.get(sessionId);\n\n if (!pipeline) {\n pipeline = createContextPipeline({\n tokenBudget: realtime.tokenBudget,\n decay,\n states,\n windowSize: realtime.windowSize,\n fullOptimizationInterval: 50, // Full re-optimization every 50 incremental updates\n });\n pipelines.set(sessionId, pipeline);\n }\n\n return pipeline;\n }\n\n /**\n * Handle file change event (debounced)\n */\n function handleFileChange(filePath: string): void {\n // Clear existing timer\n const existingTimer = debounceTimers.get(filePath);\n if (existingTimer) {\n clearTimeout(existingTimer);\n }\n\n // Set new debounced timer\n const timer = setTimeout(() => {\n try {\n // Read new lines from file\n const newLines = fileTracker.readNewLines(filePath);\n\n if (newLines.length === 0) return;\n\n // Parse JSONL content\n const content = newLines.join('\\n');\n const sessionId = getSessionId(filePath);\n const pipeline = getPipeline(sessionId);\n\n // Ingest into pipeline\n pipeline.ingest(content, { sessionId, filePath });\n\n // Check if we should trigger optimization\n const stats = pipeline.getStats();\n if (stats.currentTokens >= realtime.autoOptimizeThreshold) {\n // Update daemon metrics\n getMetrics().updateDaemon({\n sessionsWatched: pipelines.size,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n // Trigger optimization callback\n if (onOptimize) {\n const sessionStats = computeSessionStats(sessionId, pipeline);\n onOptimize(sessionId, sessionStats);\n }\n }\n } catch (error) {\n if (onError) {\n onError(error instanceof Error ? error : new Error(String(error)));\n }\n } finally {\n debounceTimers.delete(filePath);\n }\n }, realtime.debounceMs);\n\n debounceTimers.set(filePath, timer);\n }\n\n /**\n * Recursively find all JSONL files in directory\n */\n function findJsonlFiles(dir: string): string[] {\n const files: string[] = [];\n\n try {\n const entries = readdirSync(dir);\n\n for (const entry of entries) {\n const fullPath = join(dir, entry);\n const stat = statSync(fullPath);\n\n if (stat.isDirectory()) {\n // Recurse into subdirectories\n files.push(...findJsonlFiles(fullPath));\n } else if (entry.endsWith('.jsonl')) {\n // Match pattern\n const matches = realtime.watchPatterns.some((pattern) => {\n // Simple glob matching (supports **/*.jsonl)\n const regex = new RegExp(\n pattern.replace(/\\*\\*/g, '.*').replace(/\\*/g, '[^/\\\\\\\\]*').replace(/\\./g, '\\\\.'),\n );\n return regex.test(fullPath);\n });\n\n if (matches) {\n files.push(fullPath);\n }\n }\n }\n } catch (_error) {\n // Ignore errors (directory might not exist yet)\n }\n\n return files;\n }\n\n /**\n * Compute session statistics\n */\n function computeSessionStats(sessionId: string, pipeline: ContextPipeline): SessionStats {\n const stats = pipeline.getStats();\n const entries = pipeline.getEntries();\n const totalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n return {\n sessionId,\n totalTokens: stats.totalIngested,\n optimizedTokens: stats.currentTokens,\n reduction: totalTokens > 0 ? (totalTokens - stats.currentTokens) / totalTokens : 0,\n entryCount: stats.currentEntries,\n budgetUtilization: stats.budgetUtilization,\n };\n }\n\n async function start(): Promise<void> {\n const projectsDir = getProjectsDir();\n\n // Find all existing JSONL files\n const jsonlFiles = findJsonlFiles(projectsDir);\n\n // Watch each file's parent directory (fs.watch is directory-based)\n const watchedDirs = new Set<string>();\n\n for (const file of jsonlFiles) {\n const dir = dirname(file);\n\n if (!watchedDirs.has(dir)) {\n const watcher = watch(dir, { recursive: false }, (_eventType, filename) => {\n if (filename?.endsWith('.jsonl')) {\n const fullPath = join(dir, filename);\n handleFileChange(fullPath);\n }\n });\n\n watchers.push(watcher);\n watchedDirs.add(dir);\n }\n }\n\n // Also watch projects directory for new subdirectories\n const projectsWatcher = watch(projectsDir, { recursive: true }, (_eventType, filename) => {\n if (filename?.endsWith('.jsonl')) {\n const fullPath = join(projectsDir, filename);\n handleFileChange(fullPath);\n }\n });\n\n watchers.push(projectsWatcher);\n\n // Update daemon metrics\n getMetrics().updateDaemon({\n startTime: Date.now(),\n sessionsWatched: jsonlFiles.length,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n }\n\n function stop(): void {\n // Close all watchers\n for (const watcher of watchers) {\n watcher.close();\n }\n watchers.length = 0;\n\n // Clear all timers\n for (const timer of debounceTimers.values()) {\n clearTimeout(timer);\n }\n debounceTimers.clear();\n\n // Clear pipelines\n pipelines.clear();\n\n // Clear file tracker\n fileTracker.clearAll();\n }\n\n function getStats(): SessionStats[] {\n const stats: SessionStats[] = [];\n\n for (const [sessionId, pipeline] of pipelines.entries()) {\n stats.push(computeSessionStats(sessionId, pipeline));\n }\n\n return stats;\n }\n\n function getSessionStats(sessionId: string): SessionStats | null {\n const pipeline = pipelines.get(sessionId);\n if (!pipeline) return null;\n\n return computeSessionStats(sessionId, pipeline);\n }\n\n function optimizeSession(sessionId: string): void {\n const pipeline = pipelines.get(sessionId);\n if (!pipeline) return;\n\n // Get entries and force full optimization\n const entries = pipeline.getEntries();\n pipeline.clear();\n pipeline.ingest(entries.map((e) => e.content).join('\\n\\n'));\n\n // Trigger callback\n if (onOptimize) {\n const stats = computeSessionStats(sessionId, pipeline);\n onOptimize(sessionId, stats);\n }\n }\n\n return {\n start,\n stop,\n getStats,\n getSessionStats,\n optimizeSession,\n };\n}\n","/**\n * Context Parser - Shared utilities for parsing agent contexts into memory entries\n *\n * Extracted from claude-code adapter to enable reuse across:\n * - Adapters (claude-code, generic)\n * - Real-time pipeline (streaming context)\n * - Hooks (pre-prompt, post-tool-result)\n */\n\nimport { randomUUID } from 'node:crypto';\nimport type { MemoryEntry } from '../types/memory.js';\nimport { hashContent } from './hash.js';\n\n/**\n * Block type classification for Claude Code context\n */\nexport type BlockType = 'conversation' | 'tool' | 'result' | 'other';\n\n/**\n * Parse Claude Code context into memory entries\n * Handles conversation turns, tool uses, and results\n * @param context - Raw context string\n * @returns Array of memory entries\n */\nexport function parseClaudeCodeContext(context: string): MemoryEntry[] {\n const entries: MemoryEntry[] = [];\n const now = Date.now();\n\n // Split by conversation turns and tool boundaries\n const lines = context.split('\\n');\n let currentBlock: string[] = [];\n let blockType: BlockType = 'other';\n\n for (const line of lines) {\n const trimmed = line.trim();\n\n // Detect conversation turns\n if (trimmed.startsWith('User:') || trimmed.startsWith('Assistant:')) {\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'conversation';\n currentBlock.push(line);\n }\n // Detect tool calls\n else if (\n trimmed.includes('<function_calls>') ||\n trimmed.includes('<invoke>') ||\n trimmed.includes('<tool_use>')\n ) {\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'tool';\n currentBlock.push(line);\n }\n // Detect tool results\n else if (trimmed.includes('<function_results>') || trimmed.includes('</function_results>')) {\n if (currentBlock.length > 0 && blockType !== 'result') {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'result';\n currentBlock.push(line);\n }\n // Continue current block\n else if (currentBlock.length > 0) {\n currentBlock.push(line);\n }\n // Start new block if line has content\n else if (trimmed.length > 0) {\n currentBlock.push(line);\n blockType = 'other';\n }\n }\n\n // Add final block\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n }\n\n return entries.filter((e) => e.content.trim().length > 0);\n}\n\n/**\n * Create a memory entry from a content block\n * @param content - Block content\n * @param type - Block type\n * @param baseTime - Base timestamp\n * @returns Memory entry\n */\nexport function createEntry(content: string, type: BlockType, baseTime: number): MemoryEntry {\n const tags: string[] = [type];\n\n // Assign initial score based on type\n let initialScore = 0.5;\n if (type === 'conversation') initialScore = 0.8; // Prioritize conversation\n if (type === 'tool') initialScore = 0.7; // Tool calls are important\n if (type === 'result') initialScore = 0.4; // Results can be verbose\n\n return {\n id: randomUUID(),\n content,\n hash: hashContent(content),\n timestamp: baseTime,\n score: initialScore,\n state: initialScore > 0.7 ? 'active' : initialScore > 0.3 ? 'ready' : 'silent',\n ttl: 24 * 3600, // 24 hours default\n accessCount: 0,\n tags,\n metadata: { type },\n isBTSP: false,\n };\n}\n\n/**\n * Parse generic context (fallback for non-Claude-Code agents)\n * Splits on double newlines, treats as paragraphs\n * @param context - Raw context string\n * @returns Array of memory entries\n */\nexport function parseGenericContext(context: string): MemoryEntry[] {\n const entries: MemoryEntry[] = [];\n const now = Date.now();\n\n // Split on double newlines (paragraph boundaries)\n const blocks = context.split(/\\n\\n+/);\n\n for (const block of blocks) {\n const trimmed = block.trim();\n if (trimmed.length === 0) continue;\n\n entries.push(createEntry(trimmed, 'other', now));\n }\n\n return entries;\n}\n","/**\n * Content hashing utilities.\n * Uses SHA-256 for deduplication.\n */\n\nimport { createHash } from 'node:crypto';\n\n/**\n * Generate SHA-256 hash of content for deduplication.\n *\n * @param content - Content to hash\n * @returns 64-character hex string (SHA-256)\n *\n * @example\n * ```typescript\n * const hash = hashContent('Hello world');\n * console.log(hash.length); // 64\n * ```\n */\nexport function hashContent(content: string): string {\n return createHash('sha256').update(content, 'utf8').digest('hex');\n}\n","/**\n * Token estimation utilities.\n * Uses whitespace heuristic (~90% accuracy vs GPT tokenizer).\n */\n\n/**\n * Estimate token count for text using heuristic.\n *\n * Approximation: 1 token ≈ 4 chars or 0.75 words\n * Provides ~90% accuracy compared to GPT tokenizer, sufficient for optimization heuristics.\n *\n * @param text - Text to count\n * @returns Estimated token count\n *\n * @example\n * ```typescript\n * const tokens = estimateTokens('Hello world');\n * console.log(tokens); // ~2\n * ```\n */\nexport function estimateTokens(text: string): number {\n if (!text || text.length === 0) {\n return 0;\n }\n\n // Split on whitespace to get words\n const words = text.split(/\\s+/).filter((w) => w.length > 0);\n const wordCount = words.length;\n\n // Character-based estimate\n const charCount = text.length;\n const charEstimate = Math.ceil(charCount / 4);\n\n // Word-based estimate\n const wordEstimate = Math.ceil(wordCount * 0.75);\n\n // Return the maximum of both estimates (more conservative)\n return Math.max(wordEstimate, charEstimate);\n}\n","/**\n * Budget-Aware Pruner - Token budget optimization\n *\n * Unlike SparsePruner which keeps top N% entries, BudgetPruner fits entries\n * within a target token budget using priority scoring that combines:\n * - TF-IDF relevance\n * - Engram decay\n * - Confidence state multipliers\n * - BTSP bypass (always included)\n *\n * Target use case: Real-time optimization for Opus model (~50K token budget)\n */\n\nimport type { RealtimeConfig } from '../types/config.js';\nimport type { MemoryEntry } from '../types/memory.js';\nimport type { PruneResult } from '../types/pruner.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { createEngramScorer } from './engram-scorer.js';\n\nexport interface BudgetPrunerConfig {\n /** Target token budget */\n tokenBudget: number;\n /** Decay configuration */\n decay: {\n defaultTTL: number;\n decayThreshold: number;\n };\n /** State multipliers */\n states: {\n activeThreshold: number;\n readyThreshold: number;\n };\n}\n\nexport interface BudgetPruner {\n /**\n * Prune entries to fit within token budget\n * @param entries - Memory entries to prune\n * @param budget - Optional override budget (uses config default if not provided)\n * @returns Result with kept/removed entries and budget utilization\n */\n pruneToFit(entries: MemoryEntry[], budget?: number): PruneResult & { budgetUtilization: number };\n\n /**\n * Calculate priority score for an entry\n * @param entry - Entry to score\n * @param allEntries - All entries for TF-IDF calculation\n * @returns Priority score (higher = more important)\n */\n priorityScore(entry: MemoryEntry, allEntries: MemoryEntry[]): number;\n}\n\n/**\n * Create a budget-aware pruner instance\n * @param config - Pruner configuration\n * @returns BudgetPruner instance\n */\nexport function createBudgetPruner(config: BudgetPrunerConfig): BudgetPruner {\n const { tokenBudget, decay } = config;\n const engramScorer = createEngramScorer(decay);\n\n function tokenize(text: string): string[] {\n return text\n .toLowerCase()\n .split(/\\s+/)\n .filter((word) => word.length > 0);\n }\n\n function calculateTF(term: string, tokens: string[]): number {\n const count = tokens.filter((t) => t === term).length;\n // Sqrt capping to prevent common words from dominating\n return Math.sqrt(count);\n }\n\n function calculateIDF(term: string, allEntries: MemoryEntry[]): number {\n const totalDocs = allEntries.length;\n const docsWithTerm = allEntries.filter((entry) => {\n const tokens = tokenize(entry.content);\n return tokens.includes(term);\n }).length;\n\n if (docsWithTerm === 0) return 0;\n\n return Math.log(totalDocs / docsWithTerm);\n }\n\n function calculateTFIDF(entry: MemoryEntry, allEntries: MemoryEntry[]): number {\n const tokens = tokenize(entry.content);\n if (tokens.length === 0) return 0;\n\n const uniqueTerms = [...new Set(tokens)];\n let totalScore = 0;\n\n for (const term of uniqueTerms) {\n const tf = calculateTF(term, tokens);\n const idf = calculateIDF(term, allEntries);\n totalScore += tf * idf;\n }\n\n // Normalize by entry length\n return totalScore / tokens.length;\n }\n\n function getStateMultiplier(entry: MemoryEntry): number {\n // BTSP entries get max priority (handled separately, but keep high multiplier)\n if (entry.isBTSP) return 2.0;\n\n // State-based multipliers\n switch (entry.state) {\n case 'active':\n return 2.0;\n case 'ready':\n return 1.0;\n case 'silent':\n return 0.5;\n default:\n return 1.0;\n }\n }\n\n function priorityScore(entry: MemoryEntry, allEntries: MemoryEntry[]): number {\n const tfidf = calculateTFIDF(entry, allEntries);\n const currentScore = engramScorer.calculateScore(entry);\n const engramDecay = 1 - currentScore; // Lower decay = higher priority\n const stateMultiplier = getStateMultiplier(entry);\n\n // Priority = TF-IDF * (1 - decay) * state_multiplier\n // This balances relevance, recency, and confidence state\n return tfidf * (1 - engramDecay) * stateMultiplier;\n }\n\n function pruneToFit(\n entries: MemoryEntry[],\n budget: number = tokenBudget,\n ): PruneResult & { budgetUtilization: number } {\n if (entries.length === 0) {\n return {\n kept: [],\n removed: [],\n originalTokens: 0,\n prunedTokens: 0,\n budgetUtilization: 0,\n };\n }\n\n // Calculate original token count\n const originalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Step 1: Separate BTSP entries (always included, bypass budget)\n const btspEntries = entries.filter((e) => e.isBTSP);\n const regularEntries = entries.filter((e) => !e.isBTSP);\n\n const btspTokens = btspEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Step 2: Score regular entries\n const scored = regularEntries.map((entry) => ({\n entry,\n score: priorityScore(entry, entries),\n tokens: estimateTokens(entry.content),\n }));\n\n // Step 3: Sort by priority score descending\n scored.sort((a, b) => b.score - a.score);\n\n // Step 4: Greedy fill until budget exceeded\n const kept: MemoryEntry[] = [...btspEntries];\n const removed: MemoryEntry[] = [];\n let currentTokens = btspTokens;\n\n for (const item of scored) {\n if (currentTokens + item.tokens <= budget) {\n kept.push(item.entry);\n currentTokens += item.tokens;\n } else {\n removed.push(item.entry);\n }\n }\n\n const budgetUtilization = budget > 0 ? currentTokens / budget : 0;\n\n return {\n kept,\n removed,\n originalTokens,\n prunedTokens: currentTokens,\n budgetUtilization,\n };\n }\n\n return {\n pruneToFit,\n priorityScore,\n };\n}\n\n/**\n * Helper to create budget pruner from RealtimeConfig\n * @param realtimeConfig - Realtime configuration\n * @param decayConfig - Decay configuration\n * @param statesConfig - States configuration\n * @returns BudgetPruner instance\n */\nexport function createBudgetPrunerFromConfig(\n realtimeConfig: RealtimeConfig,\n decayConfig: { defaultTTL: number; decayThreshold: number },\n statesConfig: { activeThreshold: number; readyThreshold: number },\n): BudgetPruner {\n return createBudgetPruner({\n tokenBudget: realtimeConfig.tokenBudget,\n decay: decayConfig,\n states: statesConfig,\n });\n}\n","/**\n * Incremental Optimizer - Cache-based delta processing\n *\n * Optimizes performance for real-time scenarios by:\n * - Caching entry scores by content hash\n * - Only recomputing scores for new/changed entries\n * - Pre-computing and caching document frequency tables\n * - Periodically forcing full re-optimization to prevent drift\n *\n * Target: <50ms for incremental updates on 100K token contexts\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\nimport type { PruneResult } from '../types/pruner.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { type BudgetPrunerConfig, createBudgetPruner } from './budget-pruner.js';\nimport { getMetrics } from './metrics.js';\n\nexport interface IncrementalOptimizerConfig extends BudgetPrunerConfig {\n /** Force full re-optimization every N incremental updates */\n fullOptimizationInterval: number;\n}\n\nexport interface IncrementalOptimizerState {\n /** Entry cache keyed by content hash */\n entryCache: Map<string, { entry: MemoryEntry; score: number; timestamp: number }>;\n /** Document frequency table for IDF calculation */\n documentFrequency: Map<string, number>;\n /** Total document count for IDF */\n totalDocuments: number;\n /** Incremental update counter */\n updateCount: number;\n /** Last full optimization timestamp */\n lastFullOptimization: number;\n}\n\nexport interface IncrementalOptimizer {\n /**\n * Optimize incrementally (only process new/changed entries)\n * @param newEntries - New entries to add\n * @param budget - Optional budget override\n * @returns Prune result with budget utilization\n */\n optimizeIncremental(\n newEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number };\n\n /**\n * Optimize fully (recompute all scores)\n * @param allEntries - All entries to optimize\n * @param budget - Optional budget override\n * @returns Prune result with budget utilization\n */\n optimizeFull(\n allEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number };\n\n /**\n * Get current optimizer state (for serialization)\n * @returns Serializable state object\n */\n getState(): IncrementalOptimizerState;\n\n /**\n * Restore optimizer state (from serialization)\n * @param state - State to restore\n */\n restoreState(state: IncrementalOptimizerState): void;\n\n /**\n * Reset optimizer state (clear all caches)\n */\n reset(): void;\n\n /**\n * Get cache statistics\n * @returns Cache stats\n */\n getStats(): {\n cachedEntries: number;\n uniqueTerms: number;\n totalDocuments: number;\n updateCount: number;\n lastFullOptimization: number;\n };\n}\n\n/**\n * Create an incremental optimizer instance\n * @param config - Optimizer configuration\n * @returns IncrementalOptimizer instance\n */\nexport function createIncrementalOptimizer(\n config: IncrementalOptimizerConfig,\n): IncrementalOptimizer {\n const pruner = createBudgetPruner(config);\n const { fullOptimizationInterval } = config;\n\n // Internal state\n let state: IncrementalOptimizerState = {\n entryCache: new Map(),\n documentFrequency: new Map(),\n totalDocuments: 0,\n updateCount: 0,\n lastFullOptimization: Date.now(),\n };\n\n function tokenize(text: string): string[] {\n return text\n .toLowerCase()\n .split(/\\s+/)\n .filter((word) => word.length > 0);\n }\n\n /**\n * Update document frequency table incrementally\n */\n function updateDocumentFrequency(entries: MemoryEntry[], remove = false): void {\n for (const entry of entries) {\n const tokens = tokenize(entry.content);\n const uniqueTerms = [...new Set(tokens)];\n\n for (const term of uniqueTerms) {\n const current = state.documentFrequency.get(term) || 0;\n const updated = remove ? Math.max(0, current - 1) : current + 1;\n\n if (updated === 0) {\n state.documentFrequency.delete(term);\n } else {\n state.documentFrequency.set(term, updated);\n }\n }\n }\n\n state.totalDocuments += remove ? -entries.length : entries.length;\n state.totalDocuments = Math.max(0, state.totalDocuments);\n }\n\n /**\n * Check if entry is cached and still valid\n */\n function getCachedEntry(hash: string): MemoryEntry | null {\n const cached = state.entryCache.get(hash);\n if (!cached) return null;\n\n // Entry is valid if found in cache\n return cached.entry;\n }\n\n /**\n * Cache entry with score\n */\n function cacheEntry(entry: MemoryEntry, score: number): void {\n state.entryCache.set(entry.hash, {\n entry,\n score,\n timestamp: Date.now(),\n });\n }\n\n function optimizeIncremental(\n newEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number } {\n const startTime = Date.now();\n state.updateCount++;\n\n // Force full optimization if interval reached\n if (state.updateCount >= fullOptimizationInterval) {\n // Get all cached entries\n const allEntries = Array.from(state.entryCache.values()).map((c) => c.entry);\n return optimizeFull([...allEntries, ...newEntries], budget);\n }\n\n // Filter out already-cached entries\n const uncachedEntries: MemoryEntry[] = [];\n const cachedEntries: MemoryEntry[] = [];\n\n for (const entry of newEntries) {\n const cached = getCachedEntry(entry.hash);\n if (cached) {\n cachedEntries.push(cached);\n } else {\n uncachedEntries.push(entry);\n }\n }\n\n // Update document frequency for new entries only\n if (uncachedEntries.length > 0) {\n updateDocumentFrequency(uncachedEntries, false);\n }\n\n // Combine with cached entries for scoring context\n const allEntries = [...cachedEntries, ...uncachedEntries];\n\n // Score only uncached entries (reuse cached scores)\n for (const entry of uncachedEntries) {\n const score = pruner.priorityScore(entry, allEntries);\n cacheEntry(entry, score);\n }\n\n // Get all current entries (from cache + new)\n const currentEntries = Array.from(state.entryCache.values()).map((c) => c.entry);\n\n // Calculate tokens before\n const tokensBefore = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Prune to fit budget\n const result = pruner.pruneToFit(currentEntries, budget);\n\n // Calculate tokens after\n const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Update cache: remove pruned entries\n for (const removed of result.removed) {\n state.entryCache.delete(removed.hash);\n }\n\n // Update document frequency to reflect removal\n if (result.removed.length > 0) {\n updateDocumentFrequency(result.removed, true);\n }\n\n // Record metrics\n const duration = Date.now() - startTime;\n const cacheHitRate = newEntries.length > 0 ? cachedEntries.length / newEntries.length : 0;\n\n getMetrics().recordOptimization({\n timestamp: Date.now(),\n duration,\n tokensBefore,\n tokensAfter,\n entriesProcessed: newEntries.length,\n entriesKept: result.kept.length,\n cacheHitRate,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n return result;\n }\n\n function optimizeFull(\n allEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number } {\n const startTime = Date.now();\n\n // Calculate tokens before\n const tokensBefore = allEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Reset state\n state.entryCache.clear();\n state.documentFrequency.clear();\n state.totalDocuments = 0;\n state.updateCount = 0;\n state.lastFullOptimization = Date.now();\n\n // Rebuild document frequency table\n updateDocumentFrequency(allEntries, false);\n\n // Score and cache all entries\n for (const entry of allEntries) {\n const score = pruner.priorityScore(entry, allEntries);\n cacheEntry(entry, score);\n }\n\n // Prune to fit budget\n const result = pruner.pruneToFit(allEntries, budget);\n\n // Calculate tokens after\n const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Update cache: remove pruned entries\n for (const removed of result.removed) {\n state.entryCache.delete(removed.hash);\n }\n\n // Update document frequency to reflect removal\n if (result.removed.length > 0) {\n updateDocumentFrequency(result.removed, true);\n }\n\n // Record metrics\n const duration = Date.now() - startTime;\n\n getMetrics().recordOptimization({\n timestamp: Date.now(),\n duration,\n tokensBefore,\n tokensAfter,\n entriesProcessed: allEntries.length,\n entriesKept: result.kept.length,\n cacheHitRate: 0, // Full optimization has no cache hits\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n return result;\n }\n\n function getState(): IncrementalOptimizerState {\n return {\n entryCache: new Map(state.entryCache),\n documentFrequency: new Map(state.documentFrequency),\n totalDocuments: state.totalDocuments,\n updateCount: state.updateCount,\n lastFullOptimization: state.lastFullOptimization,\n };\n }\n\n function restoreState(restoredState: IncrementalOptimizerState): void {\n state = {\n entryCache: new Map(restoredState.entryCache),\n documentFrequency: new Map(restoredState.documentFrequency),\n totalDocuments: restoredState.totalDocuments,\n updateCount: restoredState.updateCount,\n lastFullOptimization: restoredState.lastFullOptimization,\n };\n }\n\n function reset(): void {\n state = {\n entryCache: new Map(),\n documentFrequency: new Map(),\n totalDocuments: 0,\n updateCount: 0,\n lastFullOptimization: Date.now(),\n };\n }\n\n function getStats() {\n return {\n cachedEntries: state.entryCache.size,\n uniqueTerms: state.documentFrequency.size,\n totalDocuments: state.totalDocuments,\n updateCount: state.updateCount,\n lastFullOptimization: state.lastFullOptimization,\n };\n }\n\n return {\n optimizeIncremental,\n optimizeFull,\n getState,\n restoreState,\n reset,\n getStats,\n };\n}\n","/**\n * Streaming Context Pipeline - Real-time sliding window buffer\n *\n * Maintains an optimized context in real-time by:\n * - Ingesting new content as it arrives\n * - Storing entries by priority internally (for eviction decisions)\n * - Outputting in chronological order (for conversation coherence)\n * - Evicting lowest-priority entries when budget exceeded\n * - Using IncrementalOptimizer for fast delta processing\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\nimport { parseClaudeCodeContext } from '../utils/context-parser.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport {\n createIncrementalOptimizer,\n type IncrementalOptimizerConfig,\n} from './incremental-optimizer.js';\n\nexport interface ContextPipelineConfig extends IncrementalOptimizerConfig {\n /** Sliding window size (max entries to keep) */\n windowSize: number;\n}\n\nexport interface ContextPipelineStats {\n /** Total entries ingested */\n totalIngested: number;\n /** Current entry count */\n currentEntries: number;\n /** Current token count */\n currentTokens: number;\n /** Budget utilization (0.0-1.0) */\n budgetUtilization: number;\n /** Evicted entry count */\n evictedEntries: number;\n /** Optimizer stats */\n optimizer: {\n cachedEntries: number;\n uniqueTerms: number;\n updateCount: number;\n };\n}\n\nexport interface ContextPipeline {\n /**\n * Ingest new content into the pipeline\n * @param content - Raw content string\n * @param metadata - Optional metadata to attach to entries\n * @returns Number of entries ingested\n */\n ingest(content: string, metadata?: Record<string, unknown>): number;\n\n /**\n * Get current optimized context (chronologically ordered)\n * @returns Optimized context string\n */\n getContext(): string;\n\n /**\n * Get current entries (chronologically ordered)\n * @returns Array of memory entries\n */\n getEntries(): MemoryEntry[];\n\n /**\n * Get pipeline statistics\n * @returns Pipeline stats\n */\n getStats(): ContextPipelineStats;\n\n /**\n * Clear all entries and reset state\n */\n clear(): void;\n}\n\n/**\n * Create a context pipeline instance\n * @param config - Pipeline configuration\n * @returns ContextPipeline instance\n */\nexport function createContextPipeline(config: ContextPipelineConfig): ContextPipeline {\n const optimizer = createIncrementalOptimizer(config);\n const { windowSize, tokenBudget } = config;\n\n // Internal state\n let totalIngested = 0;\n let evictedEntries = 0;\n let currentEntries: MemoryEntry[] = [];\n let budgetUtilization = 0;\n\n function ingest(content: string, metadata: Record<string, unknown> = {}): number {\n // Parse content into entries\n const newEntries = parseClaudeCodeContext(content);\n\n if (newEntries.length === 0) return 0;\n\n // Attach metadata to entries\n const entriesWithMetadata = newEntries.map((entry) => ({\n ...entry,\n metadata: { ...entry.metadata, ...metadata },\n }));\n\n // Optimize incrementally\n const result = optimizer.optimizeIncremental(entriesWithMetadata, tokenBudget);\n\n // Update statistics\n totalIngested += newEntries.length;\n evictedEntries += result.removed.length;\n currentEntries = result.kept;\n budgetUtilization = result.budgetUtilization;\n\n // Enforce window size limit (keep most recent if exceeded)\n if (currentEntries.length > windowSize) {\n // Sort by timestamp descending (newest first)\n const sorted = [...currentEntries].sort((a, b) => b.timestamp - a.timestamp);\n const toKeep = sorted.slice(0, windowSize);\n const toRemove = sorted.slice(windowSize);\n\n currentEntries = toKeep;\n evictedEntries += toRemove.length;\n }\n\n return newEntries.length;\n }\n\n function getContext(): string {\n // Sort entries chronologically (oldest first)\n const sorted = [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);\n return sorted.map((e) => e.content).join('\\n\\n');\n }\n\n function getEntries(): MemoryEntry[] {\n // Return entries chronologically (oldest first)\n return [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);\n }\n\n function getStats(): ContextPipelineStats {\n const optimizerStats = optimizer.getStats();\n const currentTokens = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n return {\n totalIngested,\n currentEntries: currentEntries.length,\n currentTokens,\n budgetUtilization,\n evictedEntries,\n optimizer: {\n cachedEntries: optimizerStats.cachedEntries,\n uniqueTerms: optimizerStats.uniqueTerms,\n updateCount: optimizerStats.updateCount,\n },\n };\n }\n\n function clear(): void {\n totalIngested = 0;\n evictedEntries = 0;\n currentEntries = [];\n budgetUtilization = 0;\n optimizer.reset();\n }\n\n return {\n ingest,\n getContext,\n getEntries,\n getStats,\n clear,\n };\n}\n","/**\n * File Tracker - Incremental file reading with byte position tracking\n *\n * Tracks read positions for files to enable efficient incremental reading.\n * Handles JSONL partial line buffering for incomplete writes.\n *\n * Use case: Monitor Claude Code session JSONL files and only read new lines\n * as they're appended, without re-reading the entire file.\n */\n\nimport { readFileSync, statSync } from 'node:fs';\n\nexport interface FilePosition {\n /** File path */\n path: string;\n /** Last read byte position */\n position: number;\n /** Partial line buffer (for JSONL incomplete writes) */\n partialLine: string;\n /** Last modification time */\n lastModified: number;\n /** File size at last read */\n lastSize: number;\n}\n\nexport interface FileTracker {\n /**\n * Read new content from file since last read\n * @param filePath - File to read\n * @returns New content as array of lines (empty if no new content)\n */\n readNewLines(filePath: string): string[];\n\n /**\n * Get current position for a file\n * @param filePath - File path\n * @returns File position or null if not tracked\n */\n getPosition(filePath: string): FilePosition | null;\n\n /**\n * Reset position for a file (start from beginning on next read)\n * @param filePath - File path\n */\n resetPosition(filePath: string): void;\n\n /**\n * Clear all tracked positions\n */\n clearAll(): void;\n\n /**\n * Get all tracked file paths\n * @returns Array of tracked file paths\n */\n getTrackedFiles(): string[];\n}\n\n/**\n * Create a file tracker instance\n * @returns FileTracker instance\n */\nexport function createFileTracker(): FileTracker {\n // Track positions by file path\n const positions = new Map<string, FilePosition>();\n\n function readNewLines(filePath: string): string[] {\n try {\n // Get current file stats\n const stats = statSync(filePath);\n const currentSize = stats.size;\n const currentModified = stats.mtimeMs;\n\n // Get or initialize position\n let pos = positions.get(filePath);\n\n if (!pos) {\n // First read: start from beginning\n pos = {\n path: filePath,\n position: 0,\n partialLine: '',\n lastModified: currentModified,\n lastSize: 0,\n };\n positions.set(filePath, pos);\n }\n\n // Check if file was truncated or is same size\n if (currentSize < pos.lastSize || currentSize === pos.position) {\n // File truncated or no new content\n if (currentSize < pos.lastSize) {\n // Reset position if truncated\n pos.position = 0;\n pos.partialLine = '';\n }\n return [];\n }\n\n // Read new content from last position\n const buffer = Buffer.alloc(currentSize - pos.position);\n const fd = readFileSync(filePath);\n fd.copy(buffer, 0, pos.position, currentSize);\n\n // Convert to string and combine with partial line\n const newContent = (pos.partialLine + buffer.toString('utf-8')).split('\\n');\n\n // Last element might be incomplete (no trailing newline yet)\n const partialLine = newContent.pop() || '';\n\n // Update position\n pos.position = currentSize;\n pos.partialLine = partialLine;\n pos.lastModified = currentModified;\n pos.lastSize = currentSize;\n\n // Return complete lines (filter empty)\n return newContent.filter((line) => line.trim().length > 0);\n } catch (_error) {\n // File doesn't exist or can't be read\n // Return empty array (fail silently for watcher use case)\n return [];\n }\n }\n\n function getPosition(filePath: string): FilePosition | null {\n return positions.get(filePath) || null;\n }\n\n function resetPosition(filePath: string): void {\n positions.delete(filePath);\n }\n\n function clearAll(): void {\n positions.clear();\n }\n\n function getTrackedFiles(): string[] {\n return Array.from(positions.keys());\n }\n\n return {\n readNewLines,\n getPosition,\n resetPosition,\n clearAll,\n getTrackedFiles,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAOA,IAAAA,kBAAuD;;;ACDvD,qBAAyC;AACzC,4BAAqB;AAuDrB,SAAS,aAAa,QAAwB;AAC5C,QAAM,aAAY,oBAAI,KAAK,GAAE,YAAY,EAAE,QAAQ,SAAS,GAAG;AAC/D,QAAM,aAAa,GAAG,MAAM,WAAW,SAAS;AAEhD,MAAI;AACF,qC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CAAC;AAGH,YAAY,MAAM;AAElB,GAAG,GAAK;","names":["import_node_fs","Database","import_node_fs","config","memory","memory","config","logFile","log","import_node_fs","import_node_crypto","config","config","allEntries","config","import_node_fs","config","watcher"]}
1
+ {"version":3,"sources":["../../src/daemon/index.ts","../../src/core/kv-memory.ts","../../src/utils/tokenizer.ts","../../src/daemon/consolidation-scheduler.ts","../../src/utils/tfidf.ts","../../src/core/engram-scorer.ts","../../src/core/sleep-compressor.ts","../../src/cli/commands/consolidate.ts","../../src/core/metrics.ts","../../src/daemon/session-watcher.ts","../../src/utils/context-parser.ts","../../src/utils/hash.ts","../../src/core/budget-pruner.ts","../../src/core/incremental-optimizer.ts","../../src/core/context-pipeline.ts","../../src/daemon/file-tracker.ts"],"sourcesContent":["/**\n * Daemon Entry Point - Background process main loop\n *\n * This file is executed as a forked child process by daemon-process.ts.\n * It runs the session watcher and handles cleanup on exit.\n */\n\nimport { appendFileSync, existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';\nimport { createKVMemory } from '../core/kv-memory.js';\nimport type { SparnConfig } from '../types/config.js';\nimport { setPreciseTokenCounting } from '../utils/tokenizer.js';\nimport { createConsolidationScheduler } from './consolidation-scheduler.js';\nimport { createSessionWatcher } from './session-watcher.js';\n\n// Parse config from environment or config file\nlet configJson = process.env['SPARN_CONFIG'];\nlet pidFile = process.env['SPARN_PID_FILE'];\nlet logFile = process.env['SPARN_LOG_FILE'];\n\n// On Windows, env vars may not survive process detachment - read from config file\nconst configFilePath = process.env['SPARN_CONFIG_FILE'];\nif ((!configJson || !pidFile || !logFile) && configFilePath && existsSync(configFilePath)) {\n const fileConfig = JSON.parse(readFileSync(configFilePath, 'utf-8'));\n configJson = configJson || JSON.stringify(fileConfig.config);\n pidFile = pidFile || fileConfig.pidFile;\n logFile = logFile || fileConfig.logFile;\n}\n\nif (!configJson || !pidFile || !logFile) {\n console.error('Daemon: Missing required environment variables');\n process.exit(1);\n}\n\nconst config: SparnConfig = JSON.parse(configJson);\n\n// Enable precise token counting if configured\nif (config.realtime?.preciseTokenCounting) {\n setPreciseTokenCounting(true);\n}\n\n// Write our own PID so the parent can find us\nwriteFileSync(pidFile, String(process.pid), 'utf-8');\n\n// Log helper\nfunction log(message: string): void {\n const timestamp = new Date().toISOString();\n const logMessage = `[${timestamp}] ${message}\\n`;\n\n // logFile is guaranteed to be defined after env check\n if (logFile) {\n try {\n appendFileSync(logFile, logMessage, 'utf-8');\n } catch {\n // Fail silently if can't write log\n }\n }\n}\n\n// Cleanup on exit\nfunction cleanup(): void {\n log('Daemon shutting down');\n\n // Remove PID file (pidFile is guaranteed to be defined after env check)\n if (pidFile && existsSync(pidFile)) {\n try {\n unlinkSync(pidFile);\n } catch {\n // Fail silently\n }\n }\n\n // Stop scheduler\n if (scheduler) {\n scheduler.stop();\n }\n\n // Stop watcher\n watcher.stop();\n\n // Close memory connection\n if (memory) {\n void memory.close();\n }\n\n process.exit(0);\n}\n\n// Signal handlers\nprocess.on('SIGTERM', cleanup);\nprocess.on('SIGINT', cleanup);\nprocess.on('SIGHUP', cleanup);\n\n// Uncaught exception handler\nprocess.on('uncaughtException', (error) => {\n log(`Uncaught exception: ${error.message}`);\n cleanup();\n});\n\nprocess.on('unhandledRejection', (reason) => {\n log(`Unhandled rejection: ${reason}`);\n cleanup();\n});\n\n// Create memory instance for consolidation scheduler\n// Using a default path - could be made configurable in future\nlet memory: Awaited<ReturnType<typeof createKVMemory>> | null = null;\nlet scheduler: ReturnType<typeof createConsolidationScheduler> | null = null;\n\n// Create and start session watcher\nlog('Daemon starting');\n\nconst watcher = createSessionWatcher({\n config,\n onOptimize: (sessionId, stats) => {\n log(\n `Optimized session ${sessionId}: ${stats.optimizedTokens} tokens (${Math.round(stats.reduction * 100)}% reduction)`,\n );\n },\n onError: (error) => {\n log(`Error: ${error.message}`);\n },\n});\n\n// Start watching\nwatcher\n .start()\n .then(async () => {\n log('Daemon ready - watching Claude Code sessions');\n\n // Initialize consolidation scheduler if enabled\n if (\n config.realtime.consolidationInterval !== null &&\n config.realtime.consolidationInterval > 0\n ) {\n try {\n // Create memory instance\n memory = await createKVMemory('.sparn/memory.db');\n\n // Create and start scheduler\n scheduler = createConsolidationScheduler({\n memory,\n config,\n logFile,\n });\n\n scheduler.start();\n log(\n `Consolidation scheduler started (interval: ${config.realtime.consolidationInterval}h)`,\n );\n } catch (error) {\n log(\n `Failed to start consolidation scheduler: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n })\n .catch((error) => {\n log(`Failed to start: ${error.message}`);\n cleanup();\n });\n\n// Keep process alive - setInterval keeps the event loop active\nsetInterval(() => {\n // Heartbeat - keep event loop active\n}, 30000);\n\n// On Windows, detached processes may need an additional active handle\ntry {\n process.stdin.resume();\n} catch {\n // Ignore if stdin is unavailable (detached mode)\n}\n","/**\n * KV Memory Store Module\n * Key-value storage with dual index/value tables.\n * Separates what to store (content) from how to retrieve it (index with scores and state).\n */\n\nimport { copyFileSync, existsSync } from 'node:fs';\nimport Database from 'better-sqlite3';\nimport type { MemoryEntry, MemoryQueryFilters } from '../types/memory.js';\n\n/**\n * Optimization statistics record.\n */\nexport interface OptimizationStats {\n id: number;\n timestamp: number;\n tokens_before: number;\n tokens_after: number;\n entries_pruned: number;\n duration_ms: number;\n}\n\n/**\n * KV Memory interface.\n */\n/**\n * FTS5 search result with rank score.\n */\nexport interface FTSResult {\n entry: MemoryEntry;\n rank: number;\n}\n\nexport interface KVMemory {\n /** Store a memory entry */\n put(entry: MemoryEntry): Promise<void>;\n\n /** Retrieve a memory entry by ID */\n get(id: string): Promise<MemoryEntry | null>;\n\n /** Query entries by filters */\n query(filters: MemoryQueryFilters): Promise<MemoryEntry[]>;\n\n /** Delete a memory entry */\n delete(id: string): Promise<void>;\n\n /** List all entry IDs */\n list(): Promise<string[]>;\n\n /** Compact database (remove expired entries) */\n compact(): Promise<number>;\n\n /** Close database connection */\n close(): Promise<void>;\n\n /** Record optimization statistics */\n recordOptimization(stats: Omit<OptimizationStats, 'id'>): Promise<void>;\n\n /** Get all optimization statistics */\n getOptimizationStats(): Promise<OptimizationStats[]>;\n\n /** Clear all optimization statistics */\n clearOptimizationStats(): Promise<void>;\n\n /** Full-text search using FTS5 */\n searchFTS(query: string, limit?: number): Promise<FTSResult[]>;\n}\n\n/**\n * Create a timestamped backup of the database\n * @param dbPath - Path to database file\n * @returns Path to backup file\n */\nfunction createBackup(dbPath: string): string {\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\n const backupPath = `${dbPath}.backup-${timestamp}`;\n\n try {\n copyFileSync(dbPath, backupPath);\n console.log(`✓ Database backed up to: ${backupPath}`);\n return backupPath;\n } catch (error) {\n console.error(`Warning: Could not create backup: ${error}`);\n return '';\n }\n}\n\n/**\n * Create KV Memory store with SQLite backend.\n *\n * Initializes database with dual table schema:\n * - entries_index: Fast lookups (id, hash, timestamp, score, ttl, state, accessCount, isBTSP)\n * - entries_value: Content storage (id, content, tags, metadata)\n *\n * @param dbPath - Path to SQLite database file\n * @returns KVMemory instance\n */\nexport async function createKVMemory(dbPath: string): Promise<KVMemory> {\n // Detect database corruption and create backup\n let db: Database.Database;\n try {\n db = new Database(dbPath);\n\n // Quick integrity check\n const integrityCheck = db.pragma('quick_check', { simple: true });\n if (integrityCheck !== 'ok') {\n console.error('⚠ Database corruption detected!');\n\n // Close corrupted db before backup/recovery\n db.close();\n\n // Create backup before attempting recovery\n if (existsSync(dbPath)) {\n const backupPath = createBackup(dbPath);\n if (backupPath) {\n console.log(`Backup created at: ${backupPath}`);\n }\n }\n\n // Try to recover\n console.log('Attempting database recovery...');\n db = new Database(dbPath);\n }\n } catch (error) {\n console.error('⚠ Database error detected:', error);\n\n // Create backup if database exists\n if (existsSync(dbPath)) {\n createBackup(dbPath);\n console.log('Creating new database...');\n }\n\n db = new Database(dbPath);\n }\n\n // Enable WAL mode for better concurrency\n db.pragma('journal_mode = WAL');\n\n // Enable foreign key enforcement (SQLite disables by default)\n db.pragma('foreign_keys = ON');\n\n // Create entries_index table\n db.exec(`\n CREATE TABLE IF NOT EXISTS entries_index (\n id TEXT PRIMARY KEY NOT NULL,\n hash TEXT UNIQUE NOT NULL,\n timestamp INTEGER NOT NULL,\n score REAL NOT NULL DEFAULT 0.0 CHECK(score >= 0.0 AND score <= 1.0),\n ttl INTEGER NOT NULL CHECK(ttl >= 0),\n state TEXT NOT NULL CHECK(state IN ('silent', 'ready', 'active')),\n accessCount INTEGER NOT NULL DEFAULT 0 CHECK(accessCount >= 0),\n isBTSP INTEGER NOT NULL DEFAULT 0 CHECK(isBTSP IN (0, 1)),\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))\n );\n `);\n\n // Create entries_value table\n db.exec(`\n CREATE TABLE IF NOT EXISTS entries_value (\n id TEXT PRIMARY KEY NOT NULL,\n content TEXT NOT NULL,\n tags TEXT,\n metadata TEXT,\n FOREIGN KEY (id) REFERENCES entries_index(id) ON DELETE CASCADE\n );\n `);\n\n // Create optimization_stats table\n db.exec(`\n CREATE TABLE IF NOT EXISTS optimization_stats (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n timestamp INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n tokens_before INTEGER NOT NULL,\n tokens_after INTEGER NOT NULL,\n entries_pruned INTEGER NOT NULL,\n duration_ms INTEGER NOT NULL\n );\n `);\n\n // Create indexes\n db.exec(`\n CREATE INDEX IF NOT EXISTS idx_entries_state ON entries_index(state);\n CREATE INDEX IF NOT EXISTS idx_entries_score ON entries_index(score DESC);\n CREATE INDEX IF NOT EXISTS idx_entries_hash ON entries_index(hash);\n CREATE INDEX IF NOT EXISTS idx_entries_timestamp ON entries_index(timestamp DESC);\n CREATE INDEX IF NOT EXISTS idx_stats_timestamp ON optimization_stats(timestamp DESC);\n `);\n\n // Create FTS5 virtual table for full-text search\n db.exec(`\n CREATE VIRTUAL TABLE IF NOT EXISTS entries_fts USING fts5(id, content, tokenize='porter');\n `);\n\n // Create triggers to keep FTS in sync\n // Note: triggers use INSERT OR REPLACE pattern since FTS5 doesn't support UPSERT\n db.exec(`\n CREATE TRIGGER IF NOT EXISTS entries_fts_insert\n AFTER INSERT ON entries_value\n BEGIN\n INSERT OR REPLACE INTO entries_fts(id, content) VALUES (NEW.id, NEW.content);\n END;\n `);\n\n db.exec(`\n CREATE TRIGGER IF NOT EXISTS entries_fts_delete\n AFTER DELETE ON entries_value\n BEGIN\n DELETE FROM entries_fts WHERE id = OLD.id;\n END;\n `);\n\n db.exec(`\n CREATE TRIGGER IF NOT EXISTS entries_fts_update\n AFTER UPDATE ON entries_value\n BEGIN\n DELETE FROM entries_fts WHERE id = OLD.id;\n INSERT INTO entries_fts(id, content) VALUES (NEW.id, NEW.content);\n END;\n `);\n\n // Backfill existing data into FTS table\n db.exec(`\n INSERT OR IGNORE INTO entries_fts(id, content)\n SELECT id, content FROM entries_value\n WHERE id NOT IN (SELECT id FROM entries_fts);\n `);\n\n // Prepare statements for better performance\n const putIndexStmt = db.prepare(`\n INSERT OR REPLACE INTO entries_index\n (id, hash, timestamp, score, ttl, state, accessCount, isBTSP)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)\n `);\n\n const putValueStmt = db.prepare(`\n INSERT OR REPLACE INTO entries_value\n (id, content, tags, metadata)\n VALUES (?, ?, ?, ?)\n `);\n\n const getStmt = db.prepare(`\n SELECT\n i.id, i.hash, i.timestamp, i.score, i.ttl, i.state, i.accessCount, i.isBTSP,\n v.content, v.tags, v.metadata\n FROM entries_index i\n JOIN entries_value v ON i.id = v.id\n WHERE i.id = ?\n `);\n\n const deleteIndexStmt = db.prepare('DELETE FROM entries_index WHERE id = ?');\n const deleteValueStmt = db.prepare('DELETE FROM entries_value WHERE id = ?');\n\n return {\n async put(entry: MemoryEntry): Promise<void> {\n const transaction = db.transaction(() => {\n putIndexStmt.run(\n entry.id,\n entry.hash,\n entry.timestamp,\n entry.score,\n entry.ttl,\n entry.state,\n entry.accessCount,\n entry.isBTSP ? 1 : 0,\n );\n\n putValueStmt.run(\n entry.id,\n entry.content,\n JSON.stringify(entry.tags),\n JSON.stringify(entry.metadata),\n );\n });\n\n transaction();\n },\n\n async get(id: string): Promise<MemoryEntry | null> {\n const row = getStmt.get(id) as unknown;\n\n if (!row) {\n return null;\n }\n\n const r = row as {\n id: string;\n hash: string;\n timestamp: number;\n score: number;\n ttl: number;\n state: string;\n accessCount: number;\n isBTSP: number;\n content: string;\n tags: string | null;\n metadata: string | null;\n };\n\n return {\n id: r.id,\n content: r.content,\n hash: r.hash,\n timestamp: r.timestamp,\n score: r.score,\n ttl: r.ttl,\n state: r.state as 'silent' | 'ready' | 'active',\n accessCount: r.accessCount,\n tags: r.tags ? JSON.parse(r.tags) : [],\n metadata: r.metadata ? JSON.parse(r.metadata) : {},\n isBTSP: r.isBTSP === 1,\n };\n },\n\n async query(filters: MemoryQueryFilters): Promise<MemoryEntry[]> {\n let sql = `\n SELECT\n i.id, i.hash, i.timestamp, i.score, i.ttl, i.state, i.accessCount, i.isBTSP,\n v.content, v.tags, v.metadata\n FROM entries_index i\n JOIN entries_value v ON i.id = v.id\n WHERE 1=1\n `;\n\n const params: unknown[] = [];\n\n if (filters.state) {\n sql += ' AND i.state = ?';\n params.push(filters.state);\n }\n\n if (filters.minScore !== undefined) {\n sql += ' AND i.score >= ?';\n params.push(filters.minScore);\n }\n\n if (filters.maxScore !== undefined) {\n sql += ' AND i.score <= ?';\n params.push(filters.maxScore);\n }\n\n if (filters.isBTSP !== undefined) {\n sql += ' AND i.isBTSP = ?';\n params.push(filters.isBTSP ? 1 : 0);\n }\n\n if (filters.tags && filters.tags.length > 0) {\n for (const tag of filters.tags) {\n sql += ' AND v.tags LIKE ?';\n params.push(`%\"${tag}\"%`);\n }\n }\n\n sql += ' ORDER BY i.score DESC';\n\n if (filters.limit) {\n sql += ' LIMIT ?';\n params.push(filters.limit);\n\n if (filters.offset) {\n sql += ' OFFSET ?';\n params.push(filters.offset);\n }\n }\n\n const stmt = db.prepare(sql);\n const rows = stmt.all(...params) as unknown[];\n\n return rows.map((row) => {\n const r = row as {\n id: string;\n hash: string;\n timestamp: number;\n score: number;\n ttl: number;\n state: string;\n accessCount: number;\n isBTSP: number;\n content: string;\n tags: string | null;\n metadata: string | null;\n };\n\n return {\n id: r.id,\n content: r.content,\n hash: r.hash,\n timestamp: r.timestamp,\n score: r.score,\n ttl: r.ttl,\n state: r.state as 'silent' | 'ready' | 'active',\n accessCount: r.accessCount,\n tags: r.tags ? JSON.parse(r.tags) : [],\n metadata: r.metadata ? JSON.parse(r.metadata) : {},\n isBTSP: r.isBTSP === 1,\n };\n });\n },\n\n async delete(id: string): Promise<void> {\n const transaction = db.transaction(() => {\n deleteIndexStmt.run(id);\n deleteValueStmt.run(id);\n });\n\n transaction();\n },\n\n async list(): Promise<string[]> {\n const stmt = db.prepare('SELECT id FROM entries_index');\n const rows = stmt.all() as { id: string }[];\n return rows.map((r) => r.id);\n },\n\n async compact(): Promise<number> {\n const before = db.prepare('SELECT COUNT(*) as count FROM entries_index').get() as {\n count: number;\n };\n\n // Remove expired non-BTSP entries: timestamp (ms) + ttl (seconds)*1000 < now\n const now = Date.now();\n db.prepare('DELETE FROM entries_index WHERE isBTSP = 0 AND (timestamp + ttl * 1000) < ?').run(\n now,\n );\n\n // Also remove non-BTSP entries with zero TTL\n db.exec('DELETE FROM entries_index WHERE isBTSP = 0 AND ttl <= 0');\n\n // Decay-based removal: remove non-BTSP entries with decay >= 0.95\n const candidates = db\n .prepare('SELECT id, timestamp, ttl FROM entries_index WHERE isBTSP = 0')\n .all() as Array<{ id: string; timestamp: number; ttl: number }>;\n\n for (const row of candidates) {\n const ageSeconds = Math.max(0, (now - row.timestamp) / 1000);\n const ttlSeconds = row.ttl;\n if (ttlSeconds <= 0) continue;\n const decay = 1 - Math.exp(-ageSeconds / ttlSeconds);\n if (decay >= 0.95) {\n db.prepare('DELETE FROM entries_index WHERE id = ?').run(row.id);\n }\n }\n\n // Clean up orphaned value entries\n db.exec('DELETE FROM entries_value WHERE id NOT IN (SELECT id FROM entries_index)');\n\n db.exec('VACUUM');\n\n const after = db.prepare('SELECT COUNT(*) as count FROM entries_index').get() as {\n count: number;\n };\n\n return before.count - after.count;\n },\n\n async close(): Promise<void> {\n db.close();\n },\n\n async recordOptimization(stats: Omit<OptimizationStats, 'id'>): Promise<void> {\n const stmt = db.prepare(`\n INSERT INTO optimization_stats (timestamp, tokens_before, tokens_after, entries_pruned, duration_ms)\n VALUES (?, ?, ?, ?, ?)\n `);\n\n stmt.run(\n stats.timestamp,\n stats.tokens_before,\n stats.tokens_after,\n stats.entries_pruned,\n stats.duration_ms,\n );\n\n // Retain only last 1000 stats to prevent unbounded growth\n db.prepare(\n 'DELETE FROM optimization_stats WHERE id NOT IN (SELECT id FROM optimization_stats ORDER BY timestamp DESC LIMIT 1000)',\n ).run();\n },\n\n async getOptimizationStats(): Promise<OptimizationStats[]> {\n const stmt = db.prepare(`\n SELECT id, timestamp, tokens_before, tokens_after, entries_pruned, duration_ms\n FROM optimization_stats\n ORDER BY timestamp DESC\n `);\n\n const rows = stmt.all() as OptimizationStats[];\n return rows;\n },\n\n async clearOptimizationStats(): Promise<void> {\n db.exec('DELETE FROM optimization_stats');\n },\n\n async searchFTS(query: string, limit = 10): Promise<FTSResult[]> {\n if (!query || query.trim().length === 0) return [];\n\n // Sanitize query: strip FTS5 special characters to prevent syntax errors\n const sanitized = query.replace(/[{}()[\\]\"':*^~]/g, ' ').trim();\n if (sanitized.length === 0) return [];\n\n const stmt = db.prepare(`\n SELECT\n f.id, f.content, rank,\n i.hash, i.timestamp, i.score, i.ttl, i.state, i.accessCount, i.isBTSP,\n v.tags, v.metadata\n FROM entries_fts f\n JOIN entries_index i ON f.id = i.id\n JOIN entries_value v ON f.id = v.id\n WHERE entries_fts MATCH ?\n ORDER BY rank\n LIMIT ?\n `);\n\n try {\n const rows = stmt.all(sanitized, limit) as Array<{\n id: string;\n content: string;\n rank: number;\n hash: string;\n timestamp: number;\n score: number;\n ttl: number;\n state: string;\n accessCount: number;\n isBTSP: number;\n tags: string | null;\n metadata: string | null;\n }>;\n\n return rows.map((r) => ({\n entry: {\n id: r.id,\n content: r.content,\n hash: r.hash,\n timestamp: r.timestamp,\n score: r.score,\n ttl: r.ttl,\n state: r.state as 'silent' | 'ready' | 'active',\n accessCount: r.accessCount,\n tags: r.tags ? JSON.parse(r.tags) : [],\n metadata: r.metadata ? JSON.parse(r.metadata) : {},\n isBTSP: r.isBTSP === 1,\n },\n rank: r.rank,\n }));\n } catch {\n // FTS query error (bad syntax despite sanitization) — return empty\n return [];\n }\n },\n };\n}\n","/**\n * Token estimation utilities.\n * Supports both heuristic (~90% accuracy) and precise (GPT tokenizer, ~95%+ accuracy) modes.\n */\n\nimport { encode } from 'gpt-tokenizer';\n\n/** Module-level flag for precise token counting */\nlet usePrecise = false;\n\n/**\n * Enable or disable precise token counting using GPT tokenizer.\n * When enabled, estimateTokens() uses gpt-tokenizer for ~95%+ accuracy on code.\n * When disabled (default), uses fast whitespace heuristic.\n *\n * @param enabled - Whether to use precise counting\n */\nexport function setPreciseTokenCounting(enabled: boolean): void {\n usePrecise = enabled;\n}\n\n/**\n * Count tokens precisely using GPT tokenizer.\n *\n * @param text - Text to count\n * @returns Exact token count\n */\nexport function countTokensPrecise(text: string): number {\n if (!text || text.length === 0) {\n return 0;\n }\n return encode(text).length;\n}\n\n/**\n * Estimate token count for text.\n *\n * In default (heuristic) mode: ~90% accuracy, very fast.\n * In precise mode: ~95%+ accuracy using GPT tokenizer.\n *\n * @param text - Text to count\n * @returns Estimated token count\n */\nexport function estimateTokens(text: string): number {\n if (!text || text.length === 0) {\n return 0;\n }\n\n if (usePrecise) {\n return encode(text).length;\n }\n\n // Split on whitespace to get words\n const words = text.split(/\\s+/).filter((w) => w.length > 0);\n const wordCount = words.length;\n\n // Character-based estimate\n const charCount = text.length;\n const charEstimate = Math.ceil(charCount / 4);\n\n // Word-based estimate\n const wordEstimate = Math.ceil(wordCount * 0.75);\n\n // Return the maximum of both estimates (more conservative)\n return Math.max(wordEstimate, charEstimate);\n}\n","/**\n * Consolidation Scheduler - Periodic memory consolidation\n *\n * Handles:\n * - Scheduled consolidation using setInterval\n * - Integration with existing consolidate command\n * - Logging to daemon log file\n * - Metrics tracking for consolidation runs\n */\n\nimport { appendFileSync } from 'node:fs';\nimport { consolidateCommand } from '../cli/commands/consolidate.js';\nimport type { KVMemory } from '../core/kv-memory.js';\nimport { getMetrics } from '../core/metrics.js';\nimport type { SparnConfig } from '../types/config.js';\n\nexport interface ConsolidationScheduler {\n /**\n * Start the scheduler\n */\n start(): void;\n\n /**\n * Stop the scheduler\n */\n stop(): void;\n\n /**\n * Get scheduler status\n */\n getStatus(): ConsolidationSchedulerStatus;\n}\n\nexport interface ConsolidationSchedulerStatus {\n /** Is the scheduler running */\n running: boolean;\n /** Interval in hours */\n intervalHours: number | null;\n /** Next run timestamp (null if not running) */\n nextRun: number | null;\n /** Total consolidations run */\n totalRuns: number;\n /** Last run timestamp (null if never run) */\n lastRun: number | null;\n /** Last run result (null if never run) */\n lastResult: ConsolidationResult | null;\n}\n\nexport interface ConsolidationResult {\n /** Timestamp of consolidation */\n timestamp: number;\n /** Entries before consolidation */\n entriesBefore: number;\n /** Entries after consolidation */\n entriesAfter: number;\n /** Decayed entries removed */\n decayedRemoved: number;\n /** Duplicate entries merged */\n duplicatesRemoved: number;\n /** Compression ratio (0.0-1.0) */\n compressionRatio: number;\n /** Duration in milliseconds */\n durationMs: number;\n /** Success flag */\n success: boolean;\n /** Error message if failed */\n error?: string;\n}\n\nexport interface ConsolidationSchedulerOptions {\n /** Memory store instance */\n memory: KVMemory;\n /** Sparn configuration */\n config: SparnConfig;\n /** Log file path (optional, defaults to daemon log) */\n logFile?: string;\n}\n\n/**\n * Create a consolidation scheduler instance\n * @param options - Scheduler options\n * @returns ConsolidationScheduler instance\n */\nexport function createConsolidationScheduler(\n options: ConsolidationSchedulerOptions,\n): ConsolidationScheduler {\n const { memory, config, logFile } = options;\n const intervalHours = config.realtime.consolidationInterval;\n\n let timerId: NodeJS.Timeout | null = null;\n let totalRuns = 0;\n let lastRun: number | null = null;\n let lastResult: ConsolidationResult | null = null;\n let nextRun: number | null = null;\n let isRunning = false;\n\n /**\n * Log helper\n */\n function log(message: string): void {\n const timestamp = new Date().toISOString();\n const logMessage = `[${timestamp}] [Consolidation] ${message}\\n`;\n\n // Log to daemon log file if available\n const logPath = logFile || config.realtime.logFile;\n if (logPath) {\n try {\n appendFileSync(logPath, logMessage, 'utf-8');\n } catch {\n // Fail silently if can't write log\n }\n }\n }\n\n /**\n * Run consolidation\n */\n async function runConsolidation(): Promise<void> {\n if (isRunning) {\n log('Consolidation already in progress, skipping');\n return;\n }\n\n isRunning = true;\n const startTime = Date.now();\n log('Starting scheduled consolidation');\n\n try {\n const result = await consolidateCommand({ memory });\n\n // Record success\n lastRun = startTime;\n totalRuns++;\n lastResult = {\n timestamp: startTime,\n entriesBefore: result.entriesBefore,\n entriesAfter: result.entriesAfter,\n decayedRemoved: result.decayedRemoved,\n duplicatesRemoved: result.duplicatesRemoved,\n compressionRatio: result.compressionRatio,\n durationMs: result.durationMs,\n success: true,\n };\n\n // Log result\n log(\n `Consolidation completed: ${result.entriesBefore} -> ${result.entriesAfter} entries ` +\n `(${result.decayedRemoved} decayed, ${result.duplicatesRemoved} duplicates, ` +\n `${Math.round(result.compressionRatio * 100)}% compression) in ${result.durationMs}ms`,\n );\n\n // Update metrics\n const metrics = getMetrics();\n metrics.recordOptimization({\n timestamp: startTime,\n duration: result.durationMs,\n tokensBefore: result.entriesBefore * 100, // Rough estimate\n tokensAfter: result.entriesAfter * 100, // Rough estimate\n entriesProcessed: result.entriesBefore,\n entriesKept: result.entriesAfter,\n cacheHitRate: 0, // N/A for consolidation\n memoryUsage: process.memoryUsage().heapUsed,\n });\n } catch (error) {\n // Record failure\n lastRun = startTime;\n totalRuns++;\n lastResult = {\n timestamp: startTime,\n entriesBefore: 0,\n entriesAfter: 0,\n decayedRemoved: 0,\n duplicatesRemoved: 0,\n compressionRatio: 0,\n durationMs: Date.now() - startTime,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n };\n\n log(`Consolidation failed: ${lastResult.error}`);\n }\n\n // Update next run time\n if (intervalHours !== null && intervalHours > 0) {\n nextRun = Date.now() + intervalHours * 60 * 60 * 1000;\n }\n\n isRunning = false;\n }\n\n function start(): void {\n // Check if interval is configured\n if (intervalHours === null || intervalHours <= 0) {\n log('Consolidation scheduler disabled (consolidationInterval not set)');\n return;\n }\n\n // Check if already running\n if (timerId !== null) {\n log('Consolidation scheduler already running');\n return;\n }\n\n // Calculate interval in milliseconds\n const intervalMs = intervalHours * 60 * 60 * 1000;\n\n // Set up periodic timer\n timerId = setInterval(() => {\n void runConsolidation();\n }, intervalMs);\n\n // Calculate next run\n nextRun = Date.now() + intervalMs;\n\n log(\n `Consolidation scheduler started (interval: ${intervalHours}h, next run: ${new Date(nextRun).toISOString()})`,\n );\n }\n\n function stop(): void {\n if (timerId !== null) {\n clearInterval(timerId);\n timerId = null;\n nextRun = null;\n log('Consolidation scheduler stopped');\n }\n }\n\n function getStatus(): ConsolidationSchedulerStatus {\n return {\n running: timerId !== null,\n intervalHours,\n nextRun,\n totalRuns,\n lastRun,\n lastResult,\n };\n }\n\n return {\n start,\n stop,\n getStatus,\n };\n}\n","/**\n * Shared TF-IDF utilities\n *\n * Centralized text tokenization and TF-IDF scoring used by\n * sparse-pruner, budget-pruner, incremental-optimizer, and sleep-compressor.\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\n\n/**\n * Tokenize text into lowercase words\n */\nexport function tokenize(text: string): string[] {\n return text\n .toLowerCase()\n .split(/\\s+/)\n .filter((word) => word.length > 0);\n}\n\n/**\n * Calculate term frequency with sqrt capping\n */\nexport function calculateTF(term: string, tokens: string[]): number {\n const count = tokens.filter((t) => t === term).length;\n return Math.sqrt(count);\n}\n\n/**\n * Calculate inverse document frequency across entries\n */\nexport function calculateIDF(term: string, allEntries: MemoryEntry[]): number {\n const totalDocs = allEntries.length;\n const docsWithTerm = allEntries.filter((entry) => {\n const tokens = tokenize(entry.content);\n return tokens.includes(term);\n }).length;\n\n if (docsWithTerm === 0) return 0;\n\n return Math.log(totalDocs / docsWithTerm);\n}\n\n/**\n * Calculate TF-IDF score for an entry relative to a corpus\n */\nexport function calculateTFIDF(entry: MemoryEntry, allEntries: MemoryEntry[]): number {\n const tokens = tokenize(entry.content);\n if (tokens.length === 0) return 0;\n\n const uniqueTerms = [...new Set(tokens)];\n let totalScore = 0;\n\n for (const term of uniqueTerms) {\n const tf = calculateTF(term, tokens);\n const idf = calculateIDF(term, allEntries);\n totalScore += tf * idf;\n }\n\n // Normalize by entry length\n return totalScore / tokens.length;\n}\n\n/**\n * Pre-computed TF-IDF index for O(1) document frequency lookups.\n * Eliminates O(n^2) bottleneck when scoring multiple entries.\n */\nexport interface TFIDFIndex {\n /** Map of term -> number of documents containing the term */\n documentFrequency: Map<string, number>;\n /** Total number of documents in the corpus */\n totalDocuments: number;\n}\n\n/**\n * Build a pre-computed TF-IDF index from a set of entries.\n * Iterates all entries once to build document frequency map.\n *\n * @param entries - Corpus of memory entries\n * @returns Pre-computed index for use with scoreTFIDF()\n */\nexport function createTFIDFIndex(entries: MemoryEntry[]): TFIDFIndex {\n const documentFrequency = new Map<string, number>();\n\n for (const entry of entries) {\n const tokens = tokenize(entry.content);\n const uniqueTerms = new Set(tokens);\n\n for (const term of uniqueTerms) {\n documentFrequency.set(term, (documentFrequency.get(term) || 0) + 1);\n }\n }\n\n return {\n documentFrequency,\n totalDocuments: entries.length,\n };\n}\n\n/**\n * Score an entry using a pre-computed TF-IDF index.\n * Uses pre-computed document frequencies instead of re-tokenizing all entries.\n *\n * @param entry - Entry to score\n * @param index - Pre-computed TF-IDF index from createTFIDFIndex()\n * @returns TF-IDF score (higher = more distinctive)\n */\nexport function scoreTFIDF(entry: MemoryEntry, index: TFIDFIndex): number {\n const tokens = tokenize(entry.content);\n if (tokens.length === 0) return 0;\n\n const uniqueTerms = new Set(tokens);\n let totalScore = 0;\n\n for (const term of uniqueTerms) {\n const tf = calculateTF(term, tokens);\n const docsWithTerm = index.documentFrequency.get(term) || 0;\n\n if (docsWithTerm === 0) continue;\n\n const idf = Math.log(index.totalDocuments / docsWithTerm);\n totalScore += tf * idf;\n }\n\n // Normalize by entry length\n return totalScore / tokens.length;\n}\n","/**\n * Engram Scorer - Implements time-based memory decay\n *\n * Older entries fade over time unless reinforced by access.\n * Applies exponential decay formula to memory scores based on age and access count.\n *\n * Formula: decay = 1 - e^(-age/TTL)\n * Score adjustment: score_new = score_old * (1 - decay) + (accessCount bonus)\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\n\nexport interface EngramScorerConfig {\n /** Default TTL in hours for new entries */\n defaultTTL: number;\n /** Decay threshold (0.0-1.0) above which entries are marked for pruning */\n decayThreshold: number;\n}\n\nexport interface EngramScorer {\n /**\n * Calculate current score for an entry based on decay and access count\n * @param entry - Memory entry to score\n * @param currentTime - Current timestamp in milliseconds (for testing)\n * @returns Updated score (0.0-1.0)\n */\n calculateScore(entry: MemoryEntry, currentTime?: number): number;\n\n /**\n * Refresh TTL to default value\n * @param entry - Entry to refresh\n * @returns Entry with refreshed TTL and timestamp\n */\n refreshTTL(entry: MemoryEntry): MemoryEntry;\n\n /**\n * Calculate decay factor (0.0-1.0) based on age and TTL\n * @param ageInSeconds - Age of entry in seconds\n * @param ttlInSeconds - TTL in seconds\n * @returns Decay factor (0.0 = fresh, 1.0 = fully decayed)\n */\n calculateDecay(ageInSeconds: number, ttlInSeconds: number): number;\n}\n\n/**\n * Create an engram scorer instance\n * @param config - Scorer configuration\n * @returns EngramScorer instance\n */\nexport function createEngramScorer(\n config: EngramScorerConfig & { recencyBoostMinutes?: number; recencyBoostMultiplier?: number },\n): EngramScorer {\n const { defaultTTL } = config;\n const recencyWindowMs = (config.recencyBoostMinutes ?? 30) * 60 * 1000;\n const recencyMultiplier = config.recencyBoostMultiplier ?? 1.3;\n\n function calculateDecay(ageInSeconds: number, ttlInSeconds: number): number {\n if (ttlInSeconds === 0) return 1.0; // Instant decay\n if (ageInSeconds <= 0) return 0.0; // Fresh entry\n\n // Exponential decay: 1 - e^(-age/TTL)\n const ratio = ageInSeconds / ttlInSeconds;\n const decay = 1 - Math.exp(-ratio);\n\n // Clamp to [0.0, 1.0]\n return Math.max(0, Math.min(1, decay));\n }\n\n function calculateScore(entry: MemoryEntry, currentTime: number = Date.now()): number {\n // Calculate age in seconds\n const ageInMilliseconds = currentTime - entry.timestamp;\n const ageInSeconds = Math.max(0, ageInMilliseconds / 1000);\n\n // Calculate decay factor\n const decay = calculateDecay(ageInSeconds, entry.ttl);\n\n // Base score reduced by decay\n let score = entry.score * (1 - decay);\n\n // Access count bonus (diminishing returns via log)\n if (entry.accessCount > 0) {\n const accessBonus = Math.log(entry.accessCount + 1) * 0.1;\n score = Math.min(1.0, score + accessBonus);\n }\n\n // BTSP entries maintain high score\n if (entry.isBTSP) {\n score = Math.max(score, 0.9);\n }\n\n // Recency boost for non-BTSP entries within the recency window\n if (!entry.isBTSP && recencyWindowMs > 0) {\n const ageMs = currentTime - entry.timestamp;\n if (ageMs >= 0 && ageMs < recencyWindowMs) {\n const boostFactor = 1 + (recencyMultiplier - 1) * (1 - ageMs / recencyWindowMs);\n score = score * boostFactor;\n }\n }\n\n return Math.max(0, Math.min(1, score));\n }\n\n function refreshTTL(entry: MemoryEntry): MemoryEntry {\n return {\n ...entry,\n ttl: defaultTTL * 3600, // Convert hours to seconds\n timestamp: Date.now(),\n };\n }\n\n return {\n calculateScore,\n refreshTTL,\n calculateDecay,\n };\n}\n","/**\n * Sleep Compressor - Periodic consolidation\n *\n * Removes decayed entries and merges duplicates to keep the memory store lean.\n * Runs on demand or on a scheduled interval via the daemon.\n */\n\nimport type { ConsolidateResult, DuplicateGroup } from '../types/consolidate.js';\nimport type { MemoryEntry } from '../types/memory.js';\nimport { tokenize } from '../utils/tfidf.js';\nimport { createEngramScorer } from './engram-scorer.js';\n\nexport interface SleepCompressor {\n /**\n * Consolidate entries: remove decayed, merge duplicates\n * @param entries - All memory entries\n * @returns Consolidation result\n */\n consolidate(entries: MemoryEntry[]): ConsolidateResult;\n\n /**\n * Find duplicate entries (exact hash or near-duplicate by similarity)\n * @param entries - Memory entries\n * @returns Groups of duplicates\n */\n findDuplicates(entries: MemoryEntry[]): DuplicateGroup[];\n\n /**\n * Merge duplicate entries, keeping highest score\n * @param groups - Duplicate groups\n * @returns Merged entries\n */\n mergeDuplicates(groups: DuplicateGroup[]): MemoryEntry[];\n}\n\n/**\n * Create a sleep compressor instance\n * @returns SleepCompressor instance\n */\nexport function createSleepCompressor(): SleepCompressor {\n const scorer = createEngramScorer({ defaultTTL: 24, decayThreshold: 0.95 });\n\n function consolidate(entries: MemoryEntry[]): ConsolidateResult {\n const startTime = Date.now();\n const originalCount = entries.length;\n\n // Step 1: Remove fully decayed entries (decay ≥ 0.95)\n const now = Date.now();\n const nonDecayed = entries.filter((entry) => {\n const ageInSeconds = (now - entry.timestamp) / 1000;\n const decay = scorer.calculateDecay(ageInSeconds, entry.ttl);\n return decay < 0.95; // Keep entries with decay < 0.95\n });\n\n const decayedRemoved = originalCount - nonDecayed.length;\n\n // Step 2: Find and merge duplicates\n const duplicateGroups = findDuplicates(nonDecayed);\n const merged = mergeDuplicates(duplicateGroups);\n\n // Step 3: Keep non-duplicates\n const duplicateIds = new Set(duplicateGroups.flatMap((g) => g.entries.map((e) => e.id)));\n const nonDuplicates = nonDecayed.filter((e) => !duplicateIds.has(e.id));\n\n // Combine merged duplicates with non-duplicates\n const kept = [...merged, ...nonDuplicates];\n const removed = entries.filter((e) => !kept.some((k) => k.id === e.id));\n\n const duplicatesRemoved = duplicateGroups.reduce((sum, g) => sum + (g.entries.length - 1), 0);\n\n return {\n kept,\n removed,\n entriesBefore: originalCount,\n entriesAfter: kept.length,\n decayedRemoved,\n duplicatesRemoved,\n compressionRatio: originalCount > 0 ? kept.length / originalCount : 0,\n durationMs: Date.now() - startTime,\n };\n }\n\n function findDuplicates(entries: MemoryEntry[]): DuplicateGroup[] {\n const groups: DuplicateGroup[] = [];\n const processed = new Set<string>();\n\n // Find exact hash matches\n for (let i = 0; i < entries.length; i++) {\n const entry = entries[i];\n if (!entry || processed.has(entry.id)) continue;\n\n const duplicates = entries.filter((e, idx) => idx !== i && e.hash === entry.hash);\n\n if (duplicates.length > 0) {\n const group: DuplicateGroup = {\n entries: [entry, ...duplicates],\n similarity: 1.0, // Exact match\n };\n groups.push(group);\n\n // Mark as processed\n processed.add(entry.id);\n for (const dup of duplicates) {\n processed.add(dup.id);\n }\n }\n }\n\n // Find near-duplicates (cosine similarity ≥ 0.85)\n for (let i = 0; i < entries.length; i++) {\n const entryI = entries[i];\n if (!entryI || processed.has(entryI.id)) continue;\n\n for (let j = i + 1; j < entries.length; j++) {\n const entryJ = entries[j];\n if (!entryJ || processed.has(entryJ.id)) continue;\n\n const similarity = cosineSimilarity(entryI.content, entryJ.content);\n\n if (similarity >= 0.85) {\n const group: DuplicateGroup = {\n entries: [entryI, entryJ],\n similarity,\n };\n groups.push(group);\n\n processed.add(entryI.id);\n processed.add(entryJ.id);\n break; // Move to next i\n }\n }\n }\n\n return groups;\n }\n\n function mergeDuplicates(groups: DuplicateGroup[]): MemoryEntry[] {\n const merged: MemoryEntry[] = [];\n\n for (const group of groups) {\n // Keep entry with highest score\n const sorted = [...group.entries].sort((a, b) => b.score - a.score);\n const best = sorted[0];\n if (!best) continue; // Skip empty groups\n\n // Sum access counts\n const totalAccessCount = group.entries.reduce((sum, e) => sum + e.accessCount, 0);\n\n // Merge tags\n const allTags = new Set(group.entries.flatMap((e) => e.tags));\n\n merged.push({\n ...best,\n accessCount: totalAccessCount,\n tags: Array.from(allTags),\n });\n }\n\n return merged;\n }\n\n /**\n * Calculate cosine similarity between two text strings\n * @param text1 - First text\n * @param text2 - Second text\n * @returns Similarity score (0.0-1.0)\n */\n function cosineSimilarity(text1: string, text2: string): number {\n const words1 = tokenize(text1);\n const words2 = tokenize(text2);\n\n // Build word frequency vectors in single pass each\n const vec1: Record<string, number> = {};\n const vec2: Record<string, number> = {};\n\n for (const word of words1) {\n vec1[word] = (vec1[word] ?? 0) + 1;\n }\n for (const word of words2) {\n vec2[word] = (vec2[word] ?? 0) + 1;\n }\n\n const vocab = new Set([...words1, ...words2]);\n\n // Calculate dot product and magnitudes\n let dotProduct = 0;\n let mag1 = 0;\n let mag2 = 0;\n\n for (const word of vocab) {\n const count1 = vec1[word] ?? 0;\n const count2 = vec2[word] ?? 0;\n dotProduct += count1 * count2;\n mag1 += count1 * count1;\n mag2 += count2 * count2;\n }\n\n mag1 = Math.sqrt(mag1);\n mag2 = Math.sqrt(mag2);\n\n if (mag1 === 0 || mag2 === 0) return 0;\n\n return dotProduct / (mag1 * mag2);\n }\n\n return {\n consolidate,\n findDuplicates,\n mergeDuplicates,\n };\n}\n","/**\n * Consolidate Command - Periodic memory consolidation\n */\n\nimport type { KVMemory } from '../../core/kv-memory.js';\nimport { createSleepCompressor } from '../../core/sleep-compressor.js';\n\nexport interface ConsolidateCommandOptions {\n /** Memory store instance */\n memory: KVMemory;\n}\n\nexport interface ConsolidateCommandResult {\n /** Entries before consolidation */\n entriesBefore: number;\n /** Entries after consolidation */\n entriesAfter: number;\n /** Decayed entries removed */\n decayedRemoved: number;\n /** Duplicate entries merged */\n duplicatesRemoved: number;\n /** Compression ratio (0.0-1.0) */\n compressionRatio: number;\n /** Duration in milliseconds */\n durationMs: number;\n /** VACUUM completed */\n vacuumCompleted: boolean;\n}\n\n/**\n * Execute the consolidate command\n * @param options - Command options\n * @returns Consolidation result\n */\nexport async function consolidateCommand(\n options: ConsolidateCommandOptions,\n): Promise<ConsolidateCommandResult> {\n const { memory } = options;\n\n // Get all entries from memory\n const allIds = await memory.list();\n const allEntries = await Promise.all(\n allIds.map(async (id) => {\n const entry = await memory.get(id);\n return entry;\n }),\n );\n\n // Filter out nulls\n const entries = allEntries.filter((e) => e !== null);\n\n // Run consolidation\n const compressor = createSleepCompressor();\n const result = compressor.consolidate(entries);\n\n // Update memory: remove old entries, keep consolidated ones\n for (const removed of result.removed) {\n await memory.delete(removed.id);\n }\n\n for (const kept of result.kept) {\n await memory.put(kept);\n }\n\n // Run VACUUM to reclaim space\n await memory.compact();\n\n return {\n entriesBefore: result.entriesBefore,\n entriesAfter: result.entriesAfter,\n decayedRemoved: result.decayedRemoved,\n duplicatesRemoved: result.duplicatesRemoved,\n compressionRatio: result.compressionRatio,\n durationMs: result.durationMs,\n vacuumCompleted: true,\n };\n}\n","/**\n * Metrics and Telemetry System\n *\n * Tracks performance metrics and optimization statistics:\n * - Optimization duration and throughput\n * - Token savings and reduction rates\n * - Memory usage and cache hit rates\n * - Daemon uptime and session counts\n */\n\nexport interface OptimizationMetric {\n timestamp: number;\n duration: number;\n tokensBefore: number;\n tokensAfter: number;\n entriesProcessed: number;\n entriesKept: number;\n cacheHitRate: number;\n memoryUsage: number;\n}\n\nexport interface DaemonMetric {\n startTime: number;\n sessionsWatched: number;\n totalOptimizations: number;\n totalTokensSaved: number;\n averageLatency: number;\n memoryUsage: number;\n}\n\nexport interface MetricsSnapshot {\n timestamp: number;\n optimization: {\n totalRuns: number;\n totalDuration: number;\n totalTokensSaved: number;\n averageReduction: number;\n p50Latency: number;\n p95Latency: number;\n p99Latency: number;\n };\n cache: {\n hitRate: number;\n totalHits: number;\n totalMisses: number;\n size: number;\n };\n daemon: {\n uptime: number;\n sessionsWatched: number;\n memoryUsage: number;\n };\n}\n\nexport interface MetricsCollector {\n /**\n * Record an optimization metric\n */\n recordOptimization(metric: OptimizationMetric): void;\n\n /**\n * Update daemon metrics\n */\n updateDaemon(metric: Partial<DaemonMetric>): void;\n\n /**\n * Get current metrics snapshot\n */\n getSnapshot(): MetricsSnapshot;\n\n /**\n * Export metrics as JSON\n */\n export(): string;\n\n /**\n * Reset all metrics\n */\n reset(): void;\n}\n\n/**\n * Create a metrics collector instance\n */\nexport function createMetricsCollector(): MetricsCollector {\n const optimizations: OptimizationMetric[] = [];\n let daemonMetrics: DaemonMetric = {\n startTime: Date.now(),\n sessionsWatched: 0,\n totalOptimizations: 0,\n totalTokensSaved: 0,\n averageLatency: 0,\n memoryUsage: 0,\n };\n\n let cacheHits = 0;\n let cacheMisses = 0;\n\n function recordOptimization(metric: OptimizationMetric): void {\n optimizations.push(metric);\n\n // Update daemon totals\n daemonMetrics.totalOptimizations++;\n daemonMetrics.totalTokensSaved += metric.tokensBefore - metric.tokensAfter;\n\n // Update cache stats\n if (metric.cacheHitRate > 0) {\n const hits = Math.round(metric.entriesProcessed * metric.cacheHitRate);\n cacheHits += hits;\n cacheMisses += metric.entriesProcessed - hits;\n }\n\n // Update average latency (moving average)\n daemonMetrics.averageLatency =\n (daemonMetrics.averageLatency * (daemonMetrics.totalOptimizations - 1) + metric.duration) /\n daemonMetrics.totalOptimizations;\n\n // Keep only last 1000 metrics in memory\n if (optimizations.length > 1000) {\n optimizations.shift();\n }\n }\n\n function updateDaemon(metric: Partial<DaemonMetric>): void {\n daemonMetrics = {\n ...daemonMetrics,\n ...metric,\n };\n }\n\n function calculatePercentile(sortedValues: number[], percentile: number): number {\n if (sortedValues.length === 0) return 0;\n const index = Math.ceil((percentile / 100) * sortedValues.length) - 1;\n return sortedValues[index] || 0;\n }\n\n function getSnapshot(): MetricsSnapshot {\n const totalRuns = optimizations.length;\n const totalDuration = optimizations.reduce((sum, m) => sum + m.duration, 0);\n const totalTokensSaved = optimizations.reduce(\n (sum, m) => sum + (m.tokensBefore - m.tokensAfter),\n 0,\n );\n\n const totalTokensBefore = optimizations.reduce((sum, m) => sum + m.tokensBefore, 0);\n const averageReduction = totalTokensBefore > 0 ? totalTokensSaved / totalTokensBefore : 0;\n\n // Sort durations once, reuse for all percentile calculations\n const sortedDurations = optimizations.map((m) => m.duration).sort((a, b) => a - b);\n\n const totalCacheQueries = cacheHits + cacheMisses;\n const hitRate = totalCacheQueries > 0 ? cacheHits / totalCacheQueries : 0;\n\n return {\n timestamp: Date.now(),\n optimization: {\n totalRuns,\n totalDuration,\n totalTokensSaved,\n averageReduction,\n p50Latency: calculatePercentile(sortedDurations, 50),\n p95Latency: calculatePercentile(sortedDurations, 95),\n p99Latency: calculatePercentile(sortedDurations, 99),\n },\n cache: {\n hitRate,\n totalHits: cacheHits,\n totalMisses: cacheMisses,\n size: optimizations.reduce((sum, m) => sum + m.entriesKept, 0),\n },\n daemon: {\n uptime: Date.now() - daemonMetrics.startTime,\n sessionsWatched: daemonMetrics.sessionsWatched,\n memoryUsage: daemonMetrics.memoryUsage,\n },\n };\n }\n\n function exportMetrics(): string {\n return JSON.stringify(getSnapshot(), null, 2);\n }\n\n function reset(): void {\n optimizations.length = 0;\n cacheHits = 0;\n cacheMisses = 0;\n daemonMetrics = {\n startTime: Date.now(),\n sessionsWatched: 0,\n totalOptimizations: 0,\n totalTokensSaved: 0,\n averageLatency: 0,\n memoryUsage: 0,\n };\n }\n\n return {\n recordOptimization,\n updateDaemon,\n getSnapshot,\n export: exportMetrics,\n reset,\n };\n}\n\n// Global metrics instance\nlet globalMetrics: MetricsCollector | null = null;\n\n/**\n * Get or create the global metrics collector\n */\nexport function getMetrics(): MetricsCollector {\n if (!globalMetrics) {\n globalMetrics = createMetricsCollector();\n }\n return globalMetrics;\n}\n","/**\n * Session Watcher - Monitor Claude Code session files for changes\n *\n * Uses Node.js fs.watch to monitor ~/.claude/projects/**\\/*.jsonl files.\n * Debounces events and triggers optimization when token threshold exceeded.\n * Maintains per-session ContextPipeline instances.\n */\n\nimport {\n existsSync,\n type FSWatcher,\n mkdirSync,\n readdirSync,\n readFileSync,\n statSync,\n watch,\n writeFileSync,\n} from 'node:fs';\nimport { homedir } from 'node:os';\nimport { dirname, join } from 'node:path';\nimport { type ContextPipeline, createContextPipeline } from '../core/context-pipeline.js';\nimport { getMetrics } from '../core/metrics.js';\nimport type { SparnConfig } from '../types/config.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { createFileTracker } from './file-tracker.js';\n\nexport interface SessionWatcherConfig {\n /** Sparn configuration */\n config: SparnConfig;\n /** Callback when optimization triggered */\n onOptimize?: (sessionId: string, stats: SessionStats) => void;\n /** Callback on error */\n onError?: (error: Error) => void;\n}\n\nexport interface SessionStats {\n /** Session ID */\n sessionId: string;\n /** Total tokens ingested */\n totalTokens: number;\n /** Current optimized tokens */\n optimizedTokens: number;\n /** Reduction percentage */\n reduction: number;\n /** Entry count */\n entryCount: number;\n /** Budget utilization */\n budgetUtilization: number;\n}\n\nexport interface SessionWatcher {\n /**\n * Start watching Claude Code session files\n * @returns Promise that resolves when watcher is ready\n */\n start(): Promise<void>;\n\n /**\n * Stop watching and cleanup\n */\n stop(): void;\n\n /**\n * Get statistics for all sessions\n * @returns Array of session stats\n */\n getStats(): SessionStats[];\n\n /**\n * Get statistics for a specific session\n * @param sessionId - Session ID\n * @returns Session stats or null if not found\n */\n getSessionStats(sessionId: string): SessionStats | null;\n\n /**\n * Manually trigger optimization for a session\n * @param sessionId - Session ID\n */\n optimizeSession(sessionId: string): void;\n}\n\n/**\n * Create a session watcher instance\n * @param config - Watcher configuration\n * @returns SessionWatcher instance\n */\nexport function createSessionWatcher(config: SessionWatcherConfig): SessionWatcher {\n const { config: sparnConfig, onOptimize, onError } = config;\n const { realtime, decay, states } = sparnConfig;\n\n // Per-session pipelines and trackers\n const pipelines = new Map<string, ContextPipeline>();\n const fileTracker = createFileTracker();\n\n // File system watchers\n const watchers: FSWatcher[] = [];\n\n // Debounce timers per file\n const debounceTimers = new Map<string, NodeJS.Timeout>();\n\n /**\n * Get Claude Code projects directory\n */\n function getProjectsDir(): string {\n return join(homedir(), '.claude', 'projects');\n }\n\n /**\n * Extract session ID from file path\n * Example: ~/.claude/projects/my-project/abc123.jsonl -> abc123\n */\n function getSessionId(filePath: string): string {\n const filename = filePath.split(/[/\\\\]/).pop() || '';\n return filename.replace(/\\.jsonl$/, '');\n }\n\n /**\n * Get or create pipeline for session\n */\n function getPipeline(sessionId: string): ContextPipeline {\n let pipeline = pipelines.get(sessionId);\n\n if (!pipeline) {\n pipeline = createContextPipeline({\n tokenBudget: realtime.tokenBudget,\n decay,\n states,\n windowSize: realtime.windowSize,\n fullOptimizationInterval: 50, // Full re-optimization every 50 incremental updates\n });\n // Restore persisted optimizer state if available\n loadState(sessionId, pipeline);\n pipelines.set(sessionId, pipeline);\n }\n\n return pipeline;\n }\n\n /**\n * Handle file change event (debounced)\n */\n function handleFileChange(filePath: string): void {\n // Clear existing timer\n const existingTimer = debounceTimers.get(filePath);\n if (existingTimer) {\n clearTimeout(existingTimer);\n }\n\n // Set new debounced timer\n const timer = setTimeout(() => {\n try {\n // Read new lines from file\n const newLines = fileTracker.readNewLines(filePath);\n\n if (newLines.length === 0) return;\n\n // Parse JSONL content\n const content = newLines.join('\\n');\n const sessionId = getSessionId(filePath);\n const pipeline = getPipeline(sessionId);\n\n // Ingest into pipeline\n pipeline.ingest(content, { sessionId, filePath });\n\n // Check if we should trigger optimization\n const stats = pipeline.getStats();\n if (stats.currentTokens >= realtime.autoOptimizeThreshold) {\n // Update daemon metrics\n getMetrics().updateDaemon({\n sessionsWatched: pipelines.size,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n // Trigger optimization callback\n if (onOptimize) {\n const sessionStats = computeSessionStats(sessionId, pipeline);\n onOptimize(sessionId, sessionStats);\n }\n }\n } catch (error) {\n if (onError) {\n onError(error instanceof Error ? error : new Error(String(error)));\n }\n } finally {\n debounceTimers.delete(filePath);\n }\n }, realtime.debounceMs);\n\n debounceTimers.set(filePath, timer);\n }\n\n /**\n * Recursively find all JSONL files in directory\n */\n function findJsonlFiles(dir: string): string[] {\n const files: string[] = [];\n\n try {\n const entries = readdirSync(dir);\n\n for (const entry of entries) {\n const fullPath = join(dir, entry);\n const stat = statSync(fullPath);\n\n if (stat.isDirectory()) {\n // Recurse into subdirectories\n files.push(...findJsonlFiles(fullPath));\n } else if (entry.endsWith('.jsonl')) {\n // Match pattern\n const matches = realtime.watchPatterns.some((pattern) => {\n // Simple glob matching (supports **/*.jsonl)\n const regex = new RegExp(\n pattern\n .replace(/\\./g, '\\\\.')\n .replace(/\\*\\*/g, '.*')\n .replace(/(?<!\\.)(\\*)/g, '[^/\\\\\\\\]*'),\n );\n return regex.test(fullPath);\n });\n\n if (matches) {\n files.push(fullPath);\n }\n }\n }\n } catch (_error) {\n // Ignore errors (directory might not exist yet)\n }\n\n return files;\n }\n\n /**\n * Compute session statistics\n */\n function computeSessionStats(sessionId: string, pipeline: ContextPipeline): SessionStats {\n const stats = pipeline.getStats();\n const entries = pipeline.getEntries();\n const totalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n return {\n sessionId,\n totalTokens: stats.totalIngested,\n optimizedTokens: stats.currentTokens,\n reduction: totalTokens > 0 ? (totalTokens - stats.currentTokens) / totalTokens : 0,\n entryCount: stats.currentEntries,\n budgetUtilization: stats.budgetUtilization,\n };\n }\n\n async function start(): Promise<void> {\n const projectsDir = getProjectsDir();\n\n // Find all existing JSONL files (for initial metrics count)\n const jsonlFiles = findJsonlFiles(projectsDir);\n\n // Use a single recursive watcher on the projects dir to avoid duplicate events\n // from both per-dir watchers and the recursive watcher firing for the same file\n try {\n const projectsWatcher = watch(projectsDir, { recursive: true }, (_eventType, filename) => {\n if (filename?.endsWith('.jsonl')) {\n const fullPath = join(projectsDir, filename);\n handleFileChange(fullPath);\n }\n });\n\n watchers.push(projectsWatcher);\n } catch {\n // Recursive watch not supported (some Linux kernels) - fall back to per-dir\n const watchedDirs = new Set<string>();\n\n for (const file of jsonlFiles) {\n const dir = dirname(file);\n\n if (!watchedDirs.has(dir)) {\n const watcher = watch(dir, { recursive: false }, (_eventType, filename) => {\n if (filename?.endsWith('.jsonl')) {\n const fullPath = join(dir, filename);\n handleFileChange(fullPath);\n }\n });\n\n watchers.push(watcher);\n watchedDirs.add(dir);\n }\n }\n }\n\n // Update daemon metrics\n getMetrics().updateDaemon({\n startTime: Date.now(),\n sessionsWatched: jsonlFiles.length,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n }\n\n /**\n * State persistence path\n */\n function getStatePath(): string {\n return join(homedir(), '.sparn', 'optimizer-state.json');\n }\n\n /**\n * Save optimizer state for all sessions\n */\n function saveState(): void {\n try {\n const stateMap: Record<string, string> = {};\n for (const [sessionId, pipeline] of pipelines.entries()) {\n stateMap[sessionId] = pipeline.serializeOptimizerState();\n }\n const statePath = getStatePath();\n const dir = dirname(statePath);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n writeFileSync(statePath, JSON.stringify(stateMap), 'utf-8');\n } catch {\n // Best-effort — fail silently\n }\n }\n\n /**\n * Load optimizer state for a session\n */\n function loadState(sessionId: string, pipeline: ReturnType<typeof createContextPipeline>): void {\n try {\n const statePath = getStatePath();\n if (!existsSync(statePath)) return;\n const raw = readFileSync(statePath, 'utf-8');\n const stateMap = JSON.parse(raw) as Record<string, string>;\n const sessionState = stateMap[sessionId];\n if (sessionState) {\n pipeline.deserializeOptimizerState(sessionState);\n }\n } catch {\n // Best-effort — fail silently\n }\n }\n\n function stop(): void {\n // Save optimizer state before stopping\n saveState();\n\n // Close all watchers\n for (const watcher of watchers) {\n watcher.close();\n }\n watchers.length = 0;\n\n // Clear all timers\n for (const timer of debounceTimers.values()) {\n clearTimeout(timer);\n }\n debounceTimers.clear();\n\n // Clear pipelines\n pipelines.clear();\n\n // Clear file tracker\n fileTracker.clearAll();\n }\n\n function getStats(): SessionStats[] {\n const stats: SessionStats[] = [];\n\n for (const [sessionId, pipeline] of pipelines.entries()) {\n stats.push(computeSessionStats(sessionId, pipeline));\n }\n\n return stats;\n }\n\n function getSessionStats(sessionId: string): SessionStats | null {\n const pipeline = pipelines.get(sessionId);\n if (!pipeline) return null;\n\n return computeSessionStats(sessionId, pipeline);\n }\n\n function optimizeSession(sessionId: string): void {\n const pipeline = pipelines.get(sessionId);\n if (!pipeline) return;\n\n // Get entries and force full optimization\n const entries = pipeline.getEntries();\n pipeline.clear();\n pipeline.ingest(entries.map((e) => e.content).join('\\n\\n'));\n\n // Trigger callback\n if (onOptimize) {\n const stats = computeSessionStats(sessionId, pipeline);\n onOptimize(sessionId, stats);\n }\n }\n\n return {\n start,\n stop,\n getStats,\n getSessionStats,\n optimizeSession,\n };\n}\n","/**\n * Context Parser - Shared utilities for parsing agent contexts into memory entries\n *\n * Extracted from claude-code adapter to enable reuse across:\n * - Adapters (claude-code, generic)\n * - Real-time pipeline (streaming context)\n * - Hooks (pre-prompt, post-tool-result)\n */\n\nimport { randomUUID } from 'node:crypto';\nimport type { MemoryEntry } from '../types/memory.js';\nimport { hashContent } from './hash.js';\n\n/**\n * Block type classification for Claude Code context\n */\nexport type BlockType = 'conversation' | 'tool' | 'result' | 'other';\n\n/**\n * Parse Claude Code context into memory entries\n * Handles conversation turns, tool uses, and results\n * @param context - Raw context string\n * @returns Array of memory entries\n */\nexport function parseClaudeCodeContext(context: string): MemoryEntry[] {\n // Auto-detect JSONL format: check if first non-empty line starts with '{'\n const firstNonEmpty = context.split('\\n').find((line) => line.trim().length > 0);\n if (firstNonEmpty?.trim().startsWith('{')) {\n const jsonlEntries = parseJSONLContext(context);\n if (jsonlEntries.length > 0) return jsonlEntries;\n // Fall through to text parser if JSONL parse returned empty\n }\n\n const entries: MemoryEntry[] = [];\n const now = Date.now();\n\n // Split by conversation turns and tool boundaries\n const lines = context.split('\\n');\n let currentBlock: string[] = [];\n let blockType: BlockType = 'other';\n\n for (const line of lines) {\n const trimmed = line.trim();\n\n // Detect conversation turns\n if (trimmed.startsWith('User:') || trimmed.startsWith('Assistant:')) {\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'conversation';\n currentBlock.push(line);\n }\n // Detect tool calls\n else if (\n trimmed.includes('<function_calls>') ||\n trimmed.includes('<invoke>') ||\n trimmed.includes('<tool_use>')\n ) {\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'tool';\n currentBlock.push(line);\n }\n // Detect tool results\n else if (trimmed.includes('<function_results>') || trimmed.includes('</function_results>')) {\n if (currentBlock.length > 0 && blockType !== 'result') {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n currentBlock = [];\n }\n blockType = 'result';\n currentBlock.push(line);\n }\n // Continue current block\n else if (currentBlock.length > 0) {\n currentBlock.push(line);\n }\n // Start new block if line has content\n else if (trimmed.length > 0) {\n currentBlock.push(line);\n blockType = 'other';\n }\n }\n\n // Add final block\n if (currentBlock.length > 0) {\n entries.push(createEntry(currentBlock.join('\\n'), blockType, now));\n }\n\n return entries.filter((e) => e.content.trim().length > 0);\n}\n\n/**\n * Create a memory entry from a content block\n * @param content - Block content\n * @param type - Block type\n * @param baseTime - Base timestamp\n * @returns Memory entry\n */\nexport function createEntry(content: string, type: BlockType, baseTime: number): MemoryEntry {\n const tags: string[] = [type];\n\n // Assign initial score based on type\n let initialScore = 0.5;\n if (type === 'conversation') initialScore = 0.8; // Prioritize conversation\n if (type === 'tool') initialScore = 0.7; // Tool calls are important\n if (type === 'result') initialScore = 0.4; // Results can be verbose\n\n return {\n id: randomUUID(),\n content,\n hash: hashContent(content),\n timestamp: baseTime,\n score: initialScore,\n state: initialScore >= 0.7 ? 'active' : initialScore >= 0.3 ? 'ready' : 'silent',\n ttl: 24 * 3600, // 24 hours default\n accessCount: 0,\n tags,\n metadata: { type },\n isBTSP: false,\n };\n}\n\n/**\n * Parsed JSONL message structure\n */\nexport interface JSONLMessage {\n role?: string;\n content?:\n | string\n | Array<{\n type: string;\n text?: string;\n name?: string;\n input?: unknown;\n content?: string | Array<{ type: string; text?: string }>;\n }>;\n type?: string;\n tool_use?: { name: string; input: unknown };\n tool_result?: { content: string | Array<{ type: string; text?: string }> };\n}\n\n/**\n * Parse a single JSONL line into a message\n * @param line - Single JSON line\n * @returns Parsed message or null on failure\n */\nexport function parseJSONLLine(line: string): JSONLMessage | null {\n const trimmed = line.trim();\n if (trimmed.length === 0) return null;\n\n try {\n return JSON.parse(trimmed) as JSONLMessage;\n } catch {\n return null;\n }\n}\n\n/**\n * Extract text content from a JSONL message content field\n */\nfunction extractContent(content: JSONLMessage['content']): string {\n if (typeof content === 'string') return content;\n if (Array.isArray(content)) {\n return content\n .map((block) => {\n if (block.type === 'text' && block.text) return block.text;\n if (block.type === 'tool_use' && block.name) return `[tool_use: ${block.name}]`;\n if (block.type === 'tool_result') {\n if (typeof block.content === 'string') return block.content;\n if (Array.isArray(block.content)) {\n return block.content\n .filter((c) => c.type === 'text' && c.text)\n .map((c) => c.text)\n .join('\\n');\n }\n }\n return '';\n })\n .filter((s) => s.length > 0)\n .join('\\n');\n }\n return '';\n}\n\n/**\n * Classify a JSONL message into a BlockType\n */\nfunction classifyJSONLMessage(msg: JSONLMessage): BlockType {\n // Check for tool_use blocks in content array\n if (Array.isArray(msg.content)) {\n const hasToolUse = msg.content.some((b) => b.type === 'tool_use');\n const hasToolResult = msg.content.some((b) => b.type === 'tool_result');\n if (hasToolUse) return 'tool';\n if (hasToolResult) return 'result';\n }\n\n if (msg.type === 'tool_use' || msg.tool_use) return 'tool';\n if (msg.type === 'tool_result' || msg.tool_result) return 'result';\n\n if (msg.role === 'user' || msg.role === 'assistant') return 'conversation';\n\n return 'other';\n}\n\n/**\n * Parse JSONL context into memory entries\n * Handles Claude Code transcript format (one JSON object per line)\n *\n * @param context - Raw JSONL context string\n * @returns Array of memory entries, or empty array if parsing fails\n */\nexport function parseJSONLContext(context: string): MemoryEntry[] {\n const entries: MemoryEntry[] = [];\n const now = Date.now();\n const lines = context.split('\\n');\n\n for (const line of lines) {\n const msg = parseJSONLLine(line);\n if (!msg) continue;\n\n const content = extractContent(msg.content);\n if (!content || content.trim().length === 0) continue;\n\n const blockType = classifyJSONLMessage(msg);\n entries.push(createEntry(content, blockType, now));\n }\n\n return entries;\n}\n\n/**\n * Parse generic context (fallback for non-Claude-Code agents)\n * Splits on double newlines, treats as paragraphs\n * @param context - Raw context string\n * @returns Array of memory entries\n */\nexport function parseGenericContext(context: string): MemoryEntry[] {\n const entries: MemoryEntry[] = [];\n const now = Date.now();\n\n // Split on double newlines (paragraph boundaries)\n const blocks = context.split(/\\n\\n+/);\n\n for (const block of blocks) {\n const trimmed = block.trim();\n if (trimmed.length === 0) continue;\n\n entries.push(createEntry(trimmed, 'other', now));\n }\n\n return entries;\n}\n","/**\n * Content hashing utilities.\n * Uses SHA-256 for deduplication.\n */\n\nimport { createHash } from 'node:crypto';\n\n/**\n * Generate SHA-256 hash of content for deduplication.\n *\n * @param content - Content to hash\n * @returns 64-character hex string (SHA-256)\n *\n * @example\n * ```typescript\n * const hash = hashContent('Hello world');\n * console.log(hash.length); // 64\n * ```\n */\nexport function hashContent(content: string): string {\n return createHash('sha256').update(content, 'utf8').digest('hex');\n}\n","/**\n * Budget-Aware Pruner - Token budget optimization\n *\n * Unlike SparsePruner which keeps top N% entries, BudgetPruner fits entries\n * within a target token budget using priority scoring that combines:\n * - TF-IDF relevance\n * - Time-based decay\n * - Confidence state multipliers\n * - BTSP bypass (always included)\n *\n * Target use case: Real-time optimization for Opus model (~50K token budget)\n */\n\nimport type { RealtimeConfig } from '../types/config.js';\nimport type { MemoryEntry } from '../types/memory.js';\nimport type { PruneResult } from '../types/pruner.js';\nimport { createTFIDFIndex, scoreTFIDF, type TFIDFIndex } from '../utils/tfidf.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { createEngramScorer } from './engram-scorer.js';\n\nexport interface BudgetPrunerConfig {\n /** Target token budget */\n tokenBudget: number;\n /** Decay configuration */\n decay: {\n defaultTTL: number;\n decayThreshold: number;\n };\n /** State multipliers */\n states: {\n activeThreshold: number;\n readyThreshold: number;\n };\n}\n\nexport interface BudgetPruner {\n /**\n * Prune entries to fit within token budget\n * @param entries - Memory entries to prune\n * @param budget - Optional override budget (uses config default if not provided)\n * @returns Result with kept/removed entries and budget utilization\n */\n pruneToFit(entries: MemoryEntry[], budget?: number): PruneResult & { budgetUtilization: number };\n\n /**\n * Calculate priority score for an entry\n * @param entry - Entry to score\n * @param allEntries - All entries for TF-IDF calculation\n * @param index - Optional pre-computed TF-IDF index\n * @returns Priority score (higher = more important)\n */\n priorityScore(entry: MemoryEntry, allEntries: MemoryEntry[], index?: TFIDFIndex): number;\n}\n\n/**\n * Create a budget-aware pruner instance\n * @param config - Pruner configuration\n * @returns BudgetPruner instance\n */\nexport function createBudgetPruner(config: BudgetPrunerConfig): BudgetPruner {\n const { tokenBudget, decay } = config;\n const engramScorer = createEngramScorer(decay);\n\n function getStateMultiplier(entry: MemoryEntry): number {\n // BTSP entries get max priority (handled separately, but keep high multiplier)\n if (entry.isBTSP) return 2.0;\n\n // State-based multipliers\n switch (entry.state) {\n case 'active':\n return 2.0;\n case 'ready':\n return 1.0;\n case 'silent':\n return 0.5;\n default:\n return 1.0;\n }\n }\n\n function priorityScore(\n entry: MemoryEntry,\n allEntries: MemoryEntry[],\n index?: TFIDFIndex,\n ): number {\n const tfidf = index\n ? scoreTFIDF(entry, index)\n : scoreTFIDF(entry, createTFIDFIndex(allEntries));\n const currentScore = engramScorer.calculateScore(entry);\n const engramDecay = 1 - currentScore; // Lower decay = higher priority\n const stateMultiplier = getStateMultiplier(entry);\n\n // Priority = TF-IDF * (1 - decay) * state_multiplier\n // This balances relevance, recency, and confidence state\n return tfidf * (1 - engramDecay) * stateMultiplier;\n }\n\n function pruneToFit(\n entries: MemoryEntry[],\n budget: number = tokenBudget,\n ): PruneResult & { budgetUtilization: number } {\n if (entries.length === 0) {\n return {\n kept: [],\n removed: [],\n originalTokens: 0,\n prunedTokens: 0,\n budgetUtilization: 0,\n };\n }\n\n // Calculate original token count\n const originalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Step 1: Separate BTSP entries (high priority but still within budget)\n const btspEntries = entries.filter((e) => e.isBTSP);\n const regularEntries = entries.filter((e) => !e.isBTSP);\n\n // Guard: if BTSP entries alone exceed budget, only include most recent ones\n let includedBtsp: MemoryEntry[] = [];\n let btspTokens = 0;\n const sortedBtsp = [...btspEntries].sort((a, b) => b.timestamp - a.timestamp);\n for (const entry of sortedBtsp) {\n const tokens = estimateTokens(entry.content);\n if (btspTokens + tokens <= budget * 0.8) {\n // Reserve 20% for regular entries\n includedBtsp.push(entry);\n btspTokens += tokens;\n }\n }\n // If no BTSP fits, include at least the most recent one\n if (includedBtsp.length === 0 && sortedBtsp.length > 0) {\n const firstBtsp = sortedBtsp[0];\n if (firstBtsp) {\n includedBtsp = [firstBtsp];\n btspTokens = estimateTokens(firstBtsp.content);\n }\n }\n\n // Track BTSP entries excluded by the guard\n const excludedBtsp = btspEntries.filter((e) => !includedBtsp.includes(e));\n\n // Step 2: Build TF-IDF index once and score regular entries\n const tfidfIndex = createTFIDFIndex(entries);\n const scored = regularEntries.map((entry) => ({\n entry,\n score: priorityScore(entry, entries, tfidfIndex),\n tokens: estimateTokens(entry.content),\n }));\n\n // Step 3: Sort by priority score descending\n scored.sort((a, b) => b.score - a.score);\n\n // Step 4: Greedy fill until budget exceeded\n const kept: MemoryEntry[] = [...includedBtsp];\n const removed: MemoryEntry[] = [...excludedBtsp];\n let currentTokens = btspTokens;\n\n for (const item of scored) {\n if (currentTokens + item.tokens <= budget) {\n kept.push(item.entry);\n currentTokens += item.tokens;\n } else {\n removed.push(item.entry);\n }\n }\n\n const budgetUtilization = budget > 0 ? currentTokens / budget : 0;\n\n return {\n kept,\n removed,\n originalTokens,\n prunedTokens: currentTokens,\n budgetUtilization,\n };\n }\n\n return {\n pruneToFit,\n priorityScore,\n };\n}\n\n/**\n * Helper to create budget pruner from RealtimeConfig\n * @param realtimeConfig - Realtime configuration\n * @param decayConfig - Decay configuration\n * @param statesConfig - States configuration\n * @returns BudgetPruner instance\n */\nexport function createBudgetPrunerFromConfig(\n realtimeConfig: RealtimeConfig,\n decayConfig: { defaultTTL: number; decayThreshold: number },\n statesConfig: { activeThreshold: number; readyThreshold: number },\n): BudgetPruner {\n return createBudgetPruner({\n tokenBudget: realtimeConfig.tokenBudget,\n decay: decayConfig,\n states: statesConfig,\n });\n}\n","/**\n * Incremental Optimizer - Cache-based delta processing\n *\n * Optimizes performance for real-time scenarios by:\n * - Caching entry scores by content hash\n * - Only recomputing scores for new/changed entries\n * - Pre-computing and caching document frequency tables\n * - Periodically forcing full re-optimization to prevent drift\n *\n * Target: <50ms for incremental updates on 100K token contexts\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\nimport type { PruneResult } from '../types/pruner.js';\nimport { tokenize } from '../utils/tfidf.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport { type BudgetPrunerConfig, createBudgetPruner } from './budget-pruner.js';\nimport { getMetrics } from './metrics.js';\n\nexport interface IncrementalOptimizerConfig extends BudgetPrunerConfig {\n /** Force full re-optimization every N incremental updates */\n fullOptimizationInterval: number;\n}\n\nexport interface IncrementalOptimizerState {\n /** Entry cache keyed by content hash */\n entryCache: Map<string, { entry: MemoryEntry; score: number; timestamp: number }>;\n /** Document frequency table for IDF calculation */\n documentFrequency: Map<string, number>;\n /** Total document count for IDF */\n totalDocuments: number;\n /** Incremental update counter */\n updateCount: number;\n /** Last full optimization timestamp */\n lastFullOptimization: number;\n}\n\nexport interface IncrementalOptimizer {\n /**\n * Optimize incrementally (only process new/changed entries)\n * @param newEntries - New entries to add\n * @param budget - Optional budget override\n * @returns Prune result with budget utilization\n */\n optimizeIncremental(\n newEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number };\n\n /**\n * Optimize fully (recompute all scores)\n * @param allEntries - All entries to optimize\n * @param budget - Optional budget override\n * @returns Prune result with budget utilization\n */\n optimizeFull(\n allEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number };\n\n /**\n * Get current optimizer state (for serialization)\n * @returns Serializable state object\n */\n getState(): IncrementalOptimizerState;\n\n /**\n * Restore optimizer state (from serialization)\n * @param state - State to restore\n */\n restoreState(state: IncrementalOptimizerState): void;\n\n /**\n * Reset optimizer state (clear all caches)\n */\n reset(): void;\n\n /**\n * Get cache statistics\n * @returns Cache stats\n */\n getStats(): {\n cachedEntries: number;\n uniqueTerms: number;\n totalDocuments: number;\n updateCount: number;\n lastFullOptimization: number;\n };\n\n /**\n * Serialize optimizer state to JSON string for persistence\n * @returns JSON string of the state\n */\n serializeState(): string;\n\n /**\n * Deserialize optimizer state from JSON string\n * @param json - JSON string to restore from\n * @returns true if successful, false on error\n */\n deserializeState(json: string): boolean;\n}\n\n/**\n * Create an incremental optimizer instance\n * @param config - Optimizer configuration\n * @returns IncrementalOptimizer instance\n */\nexport function createIncrementalOptimizer(\n config: IncrementalOptimizerConfig,\n): IncrementalOptimizer {\n const pruner = createBudgetPruner(config);\n const { fullOptimizationInterval } = config;\n\n // Internal state\n let state: IncrementalOptimizerState = {\n entryCache: new Map(),\n documentFrequency: new Map(),\n totalDocuments: 0,\n updateCount: 0,\n lastFullOptimization: Date.now(),\n };\n\n /**\n * Update document frequency table incrementally\n */\n function updateDocumentFrequency(entries: MemoryEntry[], remove = false): void {\n for (const entry of entries) {\n const tokens = tokenize(entry.content);\n const uniqueTerms = [...new Set(tokens)];\n\n for (const term of uniqueTerms) {\n const current = state.documentFrequency.get(term) || 0;\n const updated = remove ? Math.max(0, current - 1) : current + 1;\n\n if (updated === 0) {\n state.documentFrequency.delete(term);\n } else {\n state.documentFrequency.set(term, updated);\n }\n }\n }\n\n state.totalDocuments += remove ? -entries.length : entries.length;\n state.totalDocuments = Math.max(0, state.totalDocuments);\n }\n\n /**\n * Check if entry is cached and still valid\n */\n function getCachedEntry(hash: string): MemoryEntry | null {\n const cached = state.entryCache.get(hash);\n if (!cached) return null;\n\n // Entry is valid if found in cache\n return cached.entry;\n }\n\n /** Max cache entries to prevent unbounded growth */\n const MAX_CACHE_SIZE = 10000;\n\n /**\n * Cache entry with score\n */\n function cacheEntry(entry: MemoryEntry, score: number): void {\n // Evict oldest entries if cache is full\n if (state.entryCache.size >= MAX_CACHE_SIZE) {\n const entries = Array.from(state.entryCache.entries());\n entries.sort((a, b) => a[1].timestamp - b[1].timestamp);\n // Remove oldest 20%\n const toRemove = Math.floor(MAX_CACHE_SIZE * 0.2);\n for (let i = 0; i < toRemove && i < entries.length; i++) {\n const entry = entries[i];\n if (entry) {\n state.entryCache.delete(entry[0]);\n }\n }\n }\n\n state.entryCache.set(entry.hash, {\n entry,\n score,\n timestamp: Date.now(),\n });\n }\n\n function optimizeIncremental(\n newEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number } {\n const startTime = Date.now();\n state.updateCount++;\n\n // Force full optimization if interval reached\n if (state.updateCount >= fullOptimizationInterval) {\n // Get all cached entries\n const allEntries = Array.from(state.entryCache.values()).map((c) => c.entry);\n return optimizeFull([...allEntries, ...newEntries], budget);\n }\n\n // Filter out already-cached entries\n const uncachedEntries: MemoryEntry[] = [];\n const cachedEntries: MemoryEntry[] = [];\n\n for (const entry of newEntries) {\n const cached = getCachedEntry(entry.hash);\n if (cached) {\n cachedEntries.push(cached);\n } else {\n uncachedEntries.push(entry);\n }\n }\n\n // Update document frequency for new entries only\n if (uncachedEntries.length > 0) {\n updateDocumentFrequency(uncachedEntries, false);\n }\n\n // Combine with cached entries for scoring context\n const allEntries = [...cachedEntries, ...uncachedEntries];\n\n // Score only uncached entries (reuse cached scores)\n for (const entry of uncachedEntries) {\n const score = pruner.priorityScore(entry, allEntries);\n cacheEntry(entry, score);\n }\n\n // Get all current entries (from cache + new)\n const currentEntries = Array.from(state.entryCache.values()).map((c) => c.entry);\n\n // Calculate tokens before\n const tokensBefore = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Prune to fit budget\n const result = pruner.pruneToFit(currentEntries, budget);\n\n // Calculate tokens after\n const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Update cache: remove pruned entries\n for (const removed of result.removed) {\n state.entryCache.delete(removed.hash);\n }\n\n // Update document frequency to reflect removal\n if (result.removed.length > 0) {\n updateDocumentFrequency(result.removed, true);\n }\n\n // Record metrics\n const duration = Date.now() - startTime;\n const cacheHitRate = newEntries.length > 0 ? cachedEntries.length / newEntries.length : 0;\n\n getMetrics().recordOptimization({\n timestamp: Date.now(),\n duration,\n tokensBefore,\n tokensAfter,\n entriesProcessed: newEntries.length,\n entriesKept: result.kept.length,\n cacheHitRate,\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n return result;\n }\n\n function optimizeFull(\n allEntries: MemoryEntry[],\n budget?: number,\n ): PruneResult & { budgetUtilization: number } {\n const startTime = Date.now();\n\n // Calculate tokens before\n const tokensBefore = allEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Reset state\n state.entryCache.clear();\n state.documentFrequency.clear();\n state.totalDocuments = 0;\n state.updateCount = 0;\n state.lastFullOptimization = Date.now();\n\n // Rebuild document frequency table\n updateDocumentFrequency(allEntries, false);\n\n // Score and cache all entries\n for (const entry of allEntries) {\n const score = pruner.priorityScore(entry, allEntries);\n cacheEntry(entry, score);\n }\n\n // Prune to fit budget\n const result = pruner.pruneToFit(allEntries, budget);\n\n // Calculate tokens after\n const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n // Update cache: remove pruned entries\n for (const removed of result.removed) {\n state.entryCache.delete(removed.hash);\n }\n\n // Update document frequency to reflect removal\n if (result.removed.length > 0) {\n updateDocumentFrequency(result.removed, true);\n }\n\n // Record metrics\n const duration = Date.now() - startTime;\n\n getMetrics().recordOptimization({\n timestamp: Date.now(),\n duration,\n tokensBefore,\n tokensAfter,\n entriesProcessed: allEntries.length,\n entriesKept: result.kept.length,\n cacheHitRate: 0, // Full optimization has no cache hits\n memoryUsage: process.memoryUsage().heapUsed,\n });\n\n return result;\n }\n\n function getState(): IncrementalOptimizerState {\n return {\n entryCache: new Map(state.entryCache),\n documentFrequency: new Map(state.documentFrequency),\n totalDocuments: state.totalDocuments,\n updateCount: state.updateCount,\n lastFullOptimization: state.lastFullOptimization,\n };\n }\n\n function restoreState(restoredState: IncrementalOptimizerState): void {\n state = {\n entryCache: new Map(restoredState.entryCache),\n documentFrequency: new Map(restoredState.documentFrequency),\n totalDocuments: restoredState.totalDocuments,\n updateCount: restoredState.updateCount,\n lastFullOptimization: restoredState.lastFullOptimization,\n };\n }\n\n function reset(): void {\n state = {\n entryCache: new Map(),\n documentFrequency: new Map(),\n totalDocuments: 0,\n updateCount: 0,\n lastFullOptimization: Date.now(),\n };\n }\n\n function getStats() {\n return {\n cachedEntries: state.entryCache.size,\n uniqueTerms: state.documentFrequency.size,\n totalDocuments: state.totalDocuments,\n updateCount: state.updateCount,\n lastFullOptimization: state.lastFullOptimization,\n };\n }\n\n function serializeState(): string {\n const s = getState();\n return JSON.stringify({\n entryCache: Array.from(s.entryCache.entries()),\n documentFrequency: Array.from(s.documentFrequency.entries()),\n totalDocuments: s.totalDocuments,\n updateCount: s.updateCount,\n lastFullOptimization: s.lastFullOptimization,\n });\n }\n\n function deserializeState(json: string): boolean {\n try {\n const parsed = JSON.parse(json);\n restoreState({\n entryCache: new Map(parsed.entryCache),\n documentFrequency: new Map(parsed.documentFrequency),\n totalDocuments: parsed.totalDocuments,\n updateCount: parsed.updateCount,\n lastFullOptimization: parsed.lastFullOptimization,\n });\n return true;\n } catch {\n return false;\n }\n }\n\n return {\n optimizeIncremental,\n optimizeFull,\n getState,\n restoreState,\n reset,\n getStats,\n serializeState,\n deserializeState,\n };\n}\n","/**\n * Streaming Context Pipeline - Real-time sliding window buffer\n *\n * Maintains an optimized context in real-time by:\n * - Ingesting new content as it arrives\n * - Storing entries by priority internally (for eviction decisions)\n * - Outputting in chronological order (for conversation coherence)\n * - Evicting lowest-priority entries when budget exceeded\n * - Using IncrementalOptimizer for fast delta processing\n */\n\nimport type { MemoryEntry } from '../types/memory.js';\nimport { parseClaudeCodeContext } from '../utils/context-parser.js';\nimport { estimateTokens } from '../utils/tokenizer.js';\nimport {\n createIncrementalOptimizer,\n type IncrementalOptimizerConfig,\n} from './incremental-optimizer.js';\n\nexport interface ContextPipelineConfig extends IncrementalOptimizerConfig {\n /** Sliding window size (max entries to keep) */\n windowSize: number;\n}\n\nexport interface ContextPipelineStats {\n /** Total entries ingested */\n totalIngested: number;\n /** Current entry count */\n currentEntries: number;\n /** Current token count */\n currentTokens: number;\n /** Budget utilization (0.0-1.0) */\n budgetUtilization: number;\n /** Evicted entry count */\n evictedEntries: number;\n /** Optimizer stats */\n optimizer: {\n cachedEntries: number;\n uniqueTerms: number;\n updateCount: number;\n };\n}\n\nexport interface ContextPipeline {\n /**\n * Serialize optimizer state for persistence\n * @returns JSON string of the optimizer state\n */\n serializeOptimizerState(): string;\n\n /**\n * Restore optimizer state from persistence\n * @param json - JSON string to restore from\n * @returns true if successful\n */\n deserializeOptimizerState(json: string): boolean;\n /**\n * Ingest new content into the pipeline\n * @param content - Raw content string\n * @param metadata - Optional metadata to attach to entries\n * @returns Number of entries ingested\n */\n ingest(content: string, metadata?: Record<string, unknown>): number;\n\n /**\n * Get current optimized context (chronologically ordered)\n * @returns Optimized context string\n */\n getContext(): string;\n\n /**\n * Get current entries (chronologically ordered)\n * @returns Array of memory entries\n */\n getEntries(): MemoryEntry[];\n\n /**\n * Get pipeline statistics\n * @returns Pipeline stats\n */\n getStats(): ContextPipelineStats;\n\n /**\n * Clear all entries and reset state\n */\n clear(): void;\n}\n\n/**\n * Create a context pipeline instance\n * @param config - Pipeline configuration\n * @returns ContextPipeline instance\n */\nexport function createContextPipeline(config: ContextPipelineConfig): ContextPipeline {\n const optimizer = createIncrementalOptimizer(config);\n const { windowSize, tokenBudget } = config;\n\n // Internal state\n let totalIngested = 0;\n let evictedEntries = 0;\n let currentEntries: MemoryEntry[] = [];\n let budgetUtilization = 0;\n\n function ingest(content: string, metadata: Record<string, unknown> = {}): number {\n // Parse content into entries\n const newEntries = parseClaudeCodeContext(content);\n\n if (newEntries.length === 0) return 0;\n\n // Attach metadata to entries\n const entriesWithMetadata = newEntries.map((entry) => ({\n ...entry,\n metadata: { ...entry.metadata, ...metadata },\n }));\n\n // Optimize incrementally\n const result = optimizer.optimizeIncremental(entriesWithMetadata, tokenBudget);\n\n // Update statistics\n totalIngested += newEntries.length;\n evictedEntries += result.removed.length;\n currentEntries = result.kept;\n budgetUtilization = result.budgetUtilization;\n\n // Enforce window size limit using hybrid scoring\n if (currentEntries.length > windowSize) {\n // Calculate hybrid eviction score: ageNormalized * 0.4 + score * 0.6\n const timestamps = currentEntries.map((e) => e.timestamp);\n const minTs = Math.min(...timestamps);\n const maxTs = Math.max(...timestamps);\n const tsRange = maxTs - minTs || 1;\n\n const scored = currentEntries.map((entry) => {\n // BTSP entries always survive\n if (entry.isBTSP) return { entry, hybridScore: 2.0 };\n\n // ageNormalized: 1.0 = newest, 0.0 = oldest\n const ageNormalized = (entry.timestamp - minTs) / tsRange;\n const hybridScore = ageNormalized * 0.4 + entry.score * 0.6;\n return { entry, hybridScore };\n });\n\n // Sort by hybrid score descending (highest = keep)\n scored.sort((a, b) => {\n if (b.hybridScore !== a.hybridScore) return b.hybridScore - a.hybridScore;\n // Tiebreak: newer entries first\n return b.entry.timestamp - a.entry.timestamp;\n });\n\n const toKeep = scored.slice(0, windowSize).map((s) => s.entry);\n const toRemove = scored.slice(windowSize);\n\n currentEntries = toKeep;\n evictedEntries += toRemove.length;\n }\n\n return newEntries.length;\n }\n\n function getContext(): string {\n // Sort entries chronologically (oldest first)\n const sorted = [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);\n return sorted.map((e) => e.content).join('\\n\\n');\n }\n\n function getEntries(): MemoryEntry[] {\n // Return entries chronologically (oldest first)\n return [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);\n }\n\n function getStats(): ContextPipelineStats {\n const optimizerStats = optimizer.getStats();\n const currentTokens = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);\n\n return {\n totalIngested,\n currentEntries: currentEntries.length,\n currentTokens,\n budgetUtilization,\n evictedEntries,\n optimizer: {\n cachedEntries: optimizerStats.cachedEntries,\n uniqueTerms: optimizerStats.uniqueTerms,\n updateCount: optimizerStats.updateCount,\n },\n };\n }\n\n function clear(): void {\n totalIngested = 0;\n evictedEntries = 0;\n currentEntries = [];\n budgetUtilization = 0;\n optimizer.reset();\n }\n\n function serializeOptimizerState(): string {\n return optimizer.serializeState();\n }\n\n function deserializeOptimizerState(json: string): boolean {\n return optimizer.deserializeState(json);\n }\n\n return {\n serializeOptimizerState,\n deserializeOptimizerState,\n ingest,\n getContext,\n getEntries,\n getStats,\n clear,\n };\n}\n","/**\n * File Tracker - Incremental file reading with byte position tracking\n *\n * Tracks read positions for files to enable efficient incremental reading.\n * Handles JSONL partial line buffering for incomplete writes.\n *\n * Use case: Monitor Claude Code session JSONL files and only read new lines\n * as they're appended, without re-reading the entire file.\n */\n\nimport { closeSync, openSync, readSync, statSync } from 'node:fs';\n\nexport interface FilePosition {\n /** File path */\n path: string;\n /** Last read byte position */\n position: number;\n /** Partial line buffer (for JSONL incomplete writes) */\n partialLine: string;\n /** Last modification time */\n lastModified: number;\n /** File size at last read */\n lastSize: number;\n}\n\nexport interface FileTracker {\n /**\n * Read new content from file since last read\n * @param filePath - File to read\n * @returns New content as array of lines (empty if no new content)\n */\n readNewLines(filePath: string): string[];\n\n /**\n * Get current position for a file\n * @param filePath - File path\n * @returns File position or null if not tracked\n */\n getPosition(filePath: string): FilePosition | null;\n\n /**\n * Reset position for a file (start from beginning on next read)\n * @param filePath - File path\n */\n resetPosition(filePath: string): void;\n\n /**\n * Clear all tracked positions\n */\n clearAll(): void;\n\n /**\n * Get all tracked file paths\n * @returns Array of tracked file paths\n */\n getTrackedFiles(): string[];\n}\n\n/**\n * Create a file tracker instance\n * @returns FileTracker instance\n */\nexport function createFileTracker(): FileTracker {\n // Track positions by file path\n const positions = new Map<string, FilePosition>();\n\n function readNewLines(filePath: string): string[] {\n try {\n // Get current file stats\n const stats = statSync(filePath);\n const currentSize = stats.size;\n const currentModified = stats.mtimeMs;\n\n // Get or initialize position\n let pos = positions.get(filePath);\n\n if (!pos) {\n // First read: start from beginning\n pos = {\n path: filePath,\n position: 0,\n partialLine: '',\n lastModified: currentModified,\n lastSize: 0,\n };\n positions.set(filePath, pos);\n }\n\n // Check if file was truncated or is same size\n if (currentSize < pos.lastSize || currentSize === pos.position) {\n // File truncated or no new content\n if (currentSize < pos.lastSize) {\n // Reset position if truncated\n pos.position = 0;\n pos.partialLine = '';\n }\n return [];\n }\n\n // Read only new bytes from last position (delta read)\n const bytesToRead = currentSize - pos.position;\n const buffer = Buffer.alloc(bytesToRead);\n const fd = openSync(filePath, 'r');\n try {\n readSync(fd, buffer, 0, bytesToRead, pos.position);\n } finally {\n closeSync(fd);\n }\n\n // Convert to string and combine with partial line\n const newContent = (pos.partialLine + buffer.toString('utf-8')).split('\\n');\n\n // Last element might be incomplete (no trailing newline yet)\n const partialLine = newContent.pop() || '';\n\n // Update position\n pos.position = currentSize;\n pos.partialLine = partialLine;\n pos.lastModified = currentModified;\n pos.lastSize = currentSize;\n\n // Return complete lines (filter empty)\n return newContent.filter((line) => line.trim().length > 0);\n } catch (_error) {\n // File doesn't exist or can't be read\n // Return empty array (fail silently for watcher use case)\n return [];\n }\n }\n\n function getPosition(filePath: string): FilePosition | null {\n return positions.get(filePath) || null;\n }\n\n function resetPosition(filePath: string): void {\n positions.delete(filePath);\n }\n\n function clearAll(): void {\n positions.clear();\n }\n\n function getTrackedFiles(): string[] {\n return Array.from(positions.keys());\n }\n\n return {\n readNewLines,\n getPosition,\n resetPosition,\n clearAll,\n getTrackedFiles,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAOA,IAAAA,kBAAoF;;;ACDpF,qBAAyC;AACzC,4BAAqB;AAkErB,SAAS,aAAa,QAAwB;AAC5C,QAAM,aAAY,oBAAI,KAAK,GAAE,YAAY,EAAE,QAAQ,SAAS,GAAG;AAC/D,QAAM,aAAa,GAAG,MAAM,WAAW,SAAS;AAEhD,MAAI;AACF,qCAAa,QAAQ,UAAU;AAC/B,YAAQ,IAAI,iCAA4B,UAAU,EAAE;AACpD,WAAO;A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A,QACvC;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAED,gBAAU,MAAM;AAChB;AAAA,QACE,8CAA8C,OAAO,SAAS,qBAAqB;AAAA,MACrF;AAAA,IACF,SAAS,OAAO;AACd;AAAA,QACE,4CAA4C,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,MACpG;AAAA,IACF;AAAA,EACF;AACF,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,MAAI,oBAAoB,MAAM,OAAO,EAAE;AACvC,UAAQ;AACV,CAAC;AAGH,YAAY,MAAM;AAElB,GAAG,GAAK;AAGR,IAAI;AACF,UAAQ,MAAM,OAAO;AACvB,QAAQ;AAER;","names":["import_node_fs","Database","import_node_fs","config","memory","memory","config","logFile","log","import_node_fs","import_node_crypto","config","config","entry","allEntries","config","import_node_fs","config","watcher"]}