/** * EWC-inspired pattern consolidation. * * Protects high-importance stored patterns from being overwritten by newer, * similar ones — the pattern-store analogue of avoiding catastrophic * forgetting. * * HONEST SCOPE — read before extending this: * This is *not* Elastic Weight Consolidation. Real EWC penalises drift in a * model's parameters using Fisher information estimated from training * gradients: * * L_total = L_new + (lambda/2) * sum_i(F_i * (theta_i - theta_old_i)^2) * * There is no model here, no training loop, and therefore no gradients. What * this file actually computes is a per-dimension importance weight from the * *squared embedding values* of stored patterns, used as a stand-in for the * Fisher diagonal (see computeFisherMatrix — the substitution is noted at the * line that performs it), smoothed with an EMA and applied as a quadratic * penalty when a new pattern would displace an existing one. * * That heuristic is real, deterministic, and useful. The EWC name and the * formula above describe the *inspiration*, not the implementation — do not * cite this as an EWC implementation or reason about it as if `F_i` carried * Fisher semantics. * * What it does: * - Per-dimension importance from squared embedding magnitudes (gradient proxy) * - Online EMA updates as new patterns stream in * - Selective consolidation based on that importance * - Persistent storage in .swarm/ewc-fisher.json * * File-size sweep: types live in ewc-consolidation-types.ts; the class's * method BODIES are grouped into sibling modules — Fisher-matrix methods in * ewc-consolidation-fisher.ts, the consolidate/blend/prune core in * ewc-consolidation-core.ts, stats/accessor methods in * ewc-consolidation-stats.ts, and save/load/clear in * ewc-consolidation-persist.ts — mixed onto EWCConsolidator.prototype below. * The singleton accessor and module-level convenience functions live in * ewc-consolidation-singleton.ts and are re-exported here. Each moved method * stays a real method here (same signature, same visibility) so callers and * TypeScript see no difference; the body is just * `return theSiblingImpl.call(this, ...args)`. Fields those bodies read/write * moved from `private` to `protected` (compile-time only; no runtime change) * — TypeScript allows a standalone function typed `this: EWCConsolidator` to * reach `protected` members but not `private` ones, so `protected` is the * minimum visibility the split needs. * * @module v1/cli/memory/ewc-consolidation */ import type { ConsolidationResult, EWCConfig, EWCStats, FisherEntry, GradientSample, PatternWeights } from './ewc-consolidation-types.js'; export type { ConsolidationResult, EWCConfig, EWCStats, FisherEntry, GradientSample, PatternWeights, }; /** * EWC++ Consolidator * Implements Elastic Weight Consolidation with online updates * for preventing catastrophic forgetting in continual learning */ export declare class EWCConsolidator { protected config: EWCConfig; protected patterns: Map; protected gradientHistory: GradientSample[]; protected globalFisher: number[]; protected consolidationHistory: { timestamp: number; penalty: number; patterns: number; }[]; protected initialized: boolean; /** Dirty flag: true when in-memory state diverges from disk */ protected dirty: boolean; /** Pending debounced write timer */ protected saveTimer: ReturnType | null; /** Debounce window for disk writes (ms) */ private static readonly SAVE_DEBOUNCE_MS; constructor(config?: Partial); /** * Initialize the consolidator by loading persisted state */ initialize(): Promise; computeFisherMatrix(patterns: { id: string; embedding: number[]; success: boolean; }[]): number[]; getPenalty(oldWeights: number[], newWeights: number[], fisher?: number[]): number; resetFisher(): void; updateFisherFromConfidences(confidenceChanges: { id: string; embedding: number[]; oldConf: number; newConf: number; }[]): void; computeConfidencePenalty(oldConfidence: number, newConfidence: number): number; consolidate(newPatterns: { id: string; embedding: number[]; type: string; description?: string; }[], oldPatterns?: PatternWeights[]): ConsolidationResult; protected blendWeights(oldWeights: number[], newWeights: number[], blendFactor: number, fisher: number[]): number[]; protected pruneOldPatterns(): void; getConsolidationStats(): EWCStats; recordGradient(patternId: string, gradients: number[], success: boolean): void; getPatternWeights(id: string): PatternWeights | undefined; getAllPatterns(): PatternWeights[]; setLambda(lambda: number): void; getLambda(): number; protected calculateImportance(pattern: PatternWeights): number; clear(): void; private saveToDisk; private loadFromDisk; /** * Schedule a debounced disk flush. * Multiple calls within SAVE_DEBOUNCE_MS coalesce into one write, * preventing blocking I/O on every consolidation or gradient event. */ protected scheduleSave(): void; } export { consolidatePatterns, getEWCConsolidator, getEWCStats, recordPatternOutcome, resetEWCConsolidator, } from './ewc-consolidation-singleton.js'; import { consolidatePatterns, getEWCConsolidator, getEWCStats, recordPatternOutcome, resetEWCConsolidator } from './ewc-consolidation-singleton.js'; declare const _default: { EWCConsolidator: typeof EWCConsolidator; getEWCConsolidator: typeof getEWCConsolidator; resetEWCConsolidator: typeof resetEWCConsolidator; consolidatePatterns: typeof consolidatePatterns; recordPatternOutcome: typeof recordPatternOutcome; getEWCStats: typeof getEWCStats; }; export default _default; //# sourceMappingURL=ewc-consolidation.d.ts.map