/** * ═══════════════════════════════════════════════════════════════════════════════ * PROMPTSPEAK SQLITE DATABASE * ═══════════════════════════════════════════════════════════════════════════════ * * SQLite backend for PromptSpeak symbol storage. * Provides ACID transactions, concurrent safety, and efficient queries. * * Benefits over JSON files: * - Atomic commits (no partial writes) * - WAL mode for concurrent read/write * - Indexed queries (O(log n) vs O(n)) * - Built-in integrity constraints * - Transaction support for multi-operation safety * * ═══════════════════════════════════════════════════════════════════════════════ */ import type { Statement } from 'better-sqlite3'; import type { DirectiveSymbol, SymbolCategory, SymbolRegistryEntry } from './types.js'; export interface SymbolRow { id: number; symbol_id: string; category: SymbolCategory; subcategory: string | null; version: number; content_hash: string; commanders_intent: string; data: string; created_at: string; updated_at: string | null; created_by: string | null; tags: string | null; parent_symbol: string | null; } export interface AuditRow { id: number; timestamp: string; event_type: string; symbol_id: string | null; risk_score: number | null; details: string | null; violations: string | null; } export interface OpaqueRow { id: number; token: string; plaintext: string; symbol_id: string | null; field_name: string | null; created_at: string; expires_at: string | null; access_count: number; last_accessed: string | null; } export interface DatabaseStats { totalSymbols: number; byCategory: Record; totalAuditEvents: number; databaseSizeBytes: number; } export declare class SymbolDatabase { private db; private dbPath; private stmtInsertSymbol; private stmtGetSymbol; private stmtUpdateSymbol; private stmtDeleteSymbol; private stmtListSymbols; private stmtCountByCategory; private stmtInsertAudit; private stmtSearchSymbols; private stmtInsertOpaque; private stmtGetOpaque; private stmtGetOpaqueMultiple; private stmtUpdateOpaqueAccess; private stmtDeleteExpiredOpaque; private stmtGetMaxOpaqueId; constructor(dbPath: string); private prepareStatements; /** * Insert a new symbol */ insertSymbol(symbol: DirectiveSymbol): { success: boolean; error?: string; }; /** * Get a symbol by ID */ getSymbol(symbolId: string): DirectiveSymbol | null; /** * Update an existing symbol */ updateSymbol(symbol: DirectiveSymbol): { success: boolean; error?: string; }; /** * Delete a symbol */ deleteSymbol(symbolId: string): { success: boolean; deleted: boolean; }; /** * Check if a symbol exists */ symbolExists(symbolId: string): boolean; /** * List symbols with filtering */ listSymbols(options: { category?: SymbolCategory; search?: string; createdAfter?: string; createdBefore?: string; limit?: number; offset?: number; }): { symbols: Array; total: number; hasMore: boolean; }; /** * Get all symbols (for export) */ getAllSymbols(): DirectiveSymbol[]; /** * Search symbols using full-text search * Uses the symbols_fts FTS5 table to find symbols matching the query */ search(query: string): SymbolRow[]; /** * Insert an audit entry */ insertAuditEntry(entry: { eventType: string; symbolId?: string; riskScore?: number; details?: Record; violations?: unknown[]; }): void; /** * Get recent audit entries */ getRecentAuditEntries(limit?: number): AuditRow[]; /** * Get injection attempts */ getInjectionAttempts(): AuditRow[]; /** * Get audit entries for a symbol */ getAuditForSymbol(symbolId: string): AuditRow[]; /** * Get the current maximum token ID (for counter initialization) */ getMaxOpaqueId(): number; /** * Insert a new opaque token mapping */ insertOpaqueToken(params: { token: string; plaintext: string; symbolId?: string; fieldName?: string; expiresAt?: string; }): { success: boolean; error?: string; }; /** * Get a single opaque token mapping */ getOpaqueToken(token: string, updateAccess?: boolean): OpaqueRow | null; /** * Resolve multiple opaque tokens at once */ resolveOpaqueTokens(tokens: string[], updateAccess?: boolean): Record; /** * Get all opaque tokens for a symbol */ getOpaqueTokensForSymbol(symbolId: string): OpaqueRow[]; /** * Delete expired opaque tokens */ cleanupExpiredOpaqueTokens(): number; /** * Delete all opaque tokens for a symbol */ deleteOpaqueTokensForSymbol(symbolId: string): number; /** * Get opacity statistics */ getOpaqueStats(): { totalTokens: number; totalAccesses: number; tokensWithSymbol: number; expiredTokens: number; }; /** * Insert a verification event for a symbol */ insertVerificationEvent(params: { symbolId: string; eventType: string; oldStatus?: string; newStatus?: string; oldConfidence?: number; newConfidence?: number; reviewer: string; evidenceAdded?: string[]; notes?: string; }): { success: boolean; error?: string; }; /** * Get verification history for a symbol */ getVerificationHistory(symbolId: string): Array<{ id: number; symbolId: string; timestamp: string; eventType: string; oldStatus: string | null; newStatus: string | null; oldConfidence: number | null; newConfidence: number | null; reviewer: string; evidenceAdded: string[] | null; notes: string | null; }>; /** * List symbols that require human review based on their epistemic metadata. * Joins symbols table with their JSON data to filter by epistemic fields. */ listSymbolsNeedingReview(options: { claimType?: string; minConfidence?: number; maxConfidence?: number; limit?: number; offset?: number; }): { symbols: SymbolRow[]; total: number; }; /** * Get database statistics */ getStats(): DatabaseStats; /** * Run multiple operations in a transaction */ transaction(fn: () => T): T; /** * Optimize the database (run periodically) */ optimize(): void; /** * Close the database connection */ close(): void; /** * Check database integrity */ checkIntegrity(): { ok: boolean; errors: string[]; }; /** * Execute raw SQL (for DDL statements, migrations, etc.) */ exec(sql: string): void; /** * Prepare a statement for execution */ prepare(sql: string): Statement; } export declare function initializeDatabase(dbPath: string): SymbolDatabase; export declare function getDatabase(): SymbolDatabase; export declare function closeDatabase(): void; //# sourceMappingURL=database.d.ts.map