/** * @fileoverview PatternStore helper classes and utility functions * @module @skillsmith/core/learning/PatternStore.helpers * * Contains FisherInformationMatrix implementation, SQL schema, and utility functions. */ import type { PatternRecommendationContext, StoredPattern, PatternOutcome, PatternRow, EWCConfig, ConsolidationState } from './PatternStore.types.js'; import type { Database } from '../db/database-interface.js'; /** * SQLite schema for pattern storage */ export declare const PATTERN_STORE_SCHEMA = "\n-- Patterns table: stores recommendation patterns with outcomes\nCREATE TABLE IF NOT EXISTS patterns (\n pattern_id TEXT PRIMARY KEY,\n context_embedding BLOB NOT NULL,\n skill_id TEXT NOT NULL,\n skill_features TEXT NOT NULL,\n context_data TEXT NOT NULL,\n outcome_type TEXT NOT NULL,\n outcome_reward REAL NOT NULL,\n importance REAL NOT NULL DEFAULT 0.1,\n original_score REAL NOT NULL,\n source TEXT NOT NULL,\n access_count INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL DEFAULT (unixepoch()),\n last_accessed_at INTEGER NOT NULL DEFAULT (unixepoch())\n);\n\n-- Indexes for efficient queries\nCREATE INDEX IF NOT EXISTS idx_patterns_skill_id ON patterns(skill_id);\nCREATE INDEX IF NOT EXISTS idx_patterns_outcome_type ON patterns(outcome_type);\nCREATE INDEX IF NOT EXISTS idx_patterns_importance ON patterns(importance DESC);\nCREATE INDEX IF NOT EXISTS idx_patterns_created_at ON patterns(created_at DESC);\n\n-- Fisher Information matrix state\nCREATE TABLE IF NOT EXISTS fisher_info (\n id INTEGER PRIMARY KEY DEFAULT 1,\n matrix_data BLOB NOT NULL,\n update_count INTEGER NOT NULL DEFAULT 0,\n last_decay_at INTEGER NOT NULL DEFAULT (unixepoch()),\n updated_at INTEGER NOT NULL DEFAULT (unixepoch())\n);\n\n-- Consolidation history for monitoring\nCREATE TABLE IF NOT EXISTS consolidation_history (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n timestamp INTEGER NOT NULL DEFAULT (unixepoch()),\n patterns_processed INTEGER NOT NULL,\n patterns_preserved INTEGER NOT NULL,\n patterns_pruned INTEGER NOT NULL,\n preservation_rate REAL NOT NULL,\n duration_ms INTEGER NOT NULL,\n average_importance REAL NOT NULL\n);\n"; /** * Fisher Information Matrix interface */ export interface IFisherInformationMatrix { getImportance(dimensionIndex: number): number; update(gradient: Float32Array): void; decay(decayFactor: number): void; getImportanceVector(): Float32Array; getAverageImportance(): number; serialize(): Buffer; deserialize(buffer: Buffer): void; reset(): void; getUpdateCount(): number; } /** * Fisher Information Matrix implementation for EWC++ * * Stores diagonal approximation of Fisher Information, * indicating which "weights" (pattern dimensions) are important. * * In the context of pattern storage: * - Each dimension of the context embedding has an importance value * - High importance = changing this dimension would harm prediction * - Low importance = safe to overwrite with new patterns */ export declare class FisherInformationMatrix implements IFisherInformationMatrix { private dimensions; /** Diagonal of Fisher Information (importance per dimension) */ private importance; /** Running sum for online updates */ private runningSum; /** Number of updates performed */ private updateCount; constructor(dimensions: number); getImportance(dimensionIndex: number): number; update(gradient: Float32Array): void; decay(decayFactor: number): void; getImportanceVector(): Float32Array; getAverageImportance(): number; serialize(): Buffer; deserialize(buffer: Buffer): void; reset(): void; getUpdateCount(): number; } /** * Convert pattern context to text for embedding */ export declare function contextToText(context: PatternRecommendationContext): string; /** * Compute gradient between two embeddings */ export declare function computeGradient(a: Float32Array, b: Float32Array): Float32Array; /** * Deserialize embedding from buffer */ export declare function deserializeEmbedding(buffer: Buffer, dimensions: number): Float32Array; /** * Calculate cosine similarity between two embeddings */ export declare function cosineSimilarity(a: Float32Array, b: Float32Array): number; /** * Calculate importance-weighted similarity */ export declare function importanceWeightedSimilarity(a: Float32Array, b: Float32Array, importance: Float32Array): number; /** * Calculate pattern importance based on outcome and access patterns */ export declare function calculatePatternImportance(pattern: StoredPattern, outcome: PatternOutcome): number; /** * Calculate dimension-based importance using Fisher Information */ export declare function calculateDimensionImportance(pattern: StoredPattern, importanceVector: Float32Array, dimensions: number, lambda: number): number; /** * Convert database row to StoredPattern */ export declare function rowToStoredPattern(row: PatternRow, dimensions: number): StoredPattern; /** * Compute the average embedding across stored patterns * * @param db - Database instance * @param limit - Max patterns to sample * @param dimensions - Embedding dimensions * @returns Average embedding vector */ export declare function computeAverageEmbedding(db: Database, limit: number, dimensions: number): Promise; /** * Determine whether the store should run consolidation * * @param state - Current consolidation state * @param ewcConfig - EWC configuration * @returns True if consolidation should run */ export declare function shouldConsolidate(state: ConsolidationState, ewcConfig: EWCConfig): boolean; //# sourceMappingURL=PatternStore.helpers.d.ts.map