139 lines
4.4 KiB
TypeScript
139 lines
4.4 KiB
TypeScript
/**
|
|
* RvfLearningStore - Persistent storage for SONA learning artifacts
|
|
*
|
|
* Stores patterns, LoRA adapters, EWC state, and trajectories in a
|
|
* binary-header JSON-lines file format for fast append and rebuild.
|
|
*
|
|
* File format:
|
|
* 4-byte magic "RVLS" + newline
|
|
* One JSON record per line: {"type":"pattern"|"lora"|"ewc"|"trajectory","data":{...}}
|
|
*
|
|
* @module @claude-flow/memory/rvf-learning-store
|
|
*/
|
|
export interface RvfLearningStoreConfig {
|
|
/** Path to the persistence file */
|
|
storePath: string;
|
|
/** SONA embedding dimension (default: 64) */
|
|
dimensions?: number;
|
|
/** Auto-persist interval in ms (default: 30000) */
|
|
autoPersistInterval?: number;
|
|
/** Enable verbose logging (default: false) */
|
|
verbose?: boolean;
|
|
}
|
|
export interface PatternRecord {
|
|
id: string;
|
|
type: string;
|
|
embedding: number[];
|
|
successRate: number;
|
|
useCount: number;
|
|
lastUsed: string;
|
|
}
|
|
export interface LoraRecord {
|
|
id: string;
|
|
config: Record<string, unknown>;
|
|
weights: string;
|
|
frozen: boolean;
|
|
numParameters: number;
|
|
}
|
|
export interface EwcRecord {
|
|
tasksLearned: number;
|
|
protectionStrength: number;
|
|
forgettingRate: number;
|
|
taskWeights: Record<string, number[]>;
|
|
}
|
|
export interface TrajectoryRecord {
|
|
id: string;
|
|
steps: Array<{
|
|
type: string;
|
|
input: string;
|
|
output: string;
|
|
durationMs: number;
|
|
confidence: number;
|
|
}>;
|
|
outcome: string;
|
|
durationMs: number;
|
|
timestamp: string;
|
|
}
|
|
/**
|
|
* Persistent store for SONA learning artifacts.
|
|
*
|
|
* Maintains in-memory maps for fast reads and flushes to a JSON-lines
|
|
* file with a binary header on persist(). On initialize(), the file is
|
|
* read line-by-line to rebuild state.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const store = new RvfLearningStore({ storePath: './data/learning.rvls' });
|
|
* await store.initialize();
|
|
*
|
|
* await store.savePatterns([{ id: 'p1', type: 'query_response', ... }]);
|
|
* await store.persist();
|
|
* await store.close();
|
|
* ```
|
|
*/
|
|
export declare class RvfLearningStore {
|
|
private config;
|
|
private patterns;
|
|
private loraAdapters;
|
|
private ewcState;
|
|
private trajectories;
|
|
private dirty;
|
|
private initialized;
|
|
private autoPersistTimer;
|
|
constructor(config: RvfLearningStoreConfig);
|
|
/**
|
|
* Initialize the store by loading any existing data from disk.
|
|
* Creates the parent directory if it does not exist.
|
|
*/
|
|
initialize(): Promise<void>;
|
|
/**
|
|
* Save or update patterns. Existing patterns with matching IDs are
|
|
* overwritten; new patterns are added.
|
|
*
|
|
* @returns The number of patterns stored
|
|
*/
|
|
savePatterns(patterns: PatternRecord[]): Promise<number>;
|
|
/** Load all patterns currently held in memory */
|
|
loadPatterns(): Promise<PatternRecord[]>;
|
|
/** Return the number of stored patterns */
|
|
getPatternCount(): Promise<number>;
|
|
/** Save or update a LoRA adapter record */
|
|
saveLoraAdapter(record: LoraRecord): Promise<void>;
|
|
/** Load all LoRA adapter records */
|
|
loadLoraAdapters(): Promise<LoraRecord[]>;
|
|
/** Delete a LoRA adapter by ID */
|
|
deleteLoraAdapter(id: string): Promise<boolean>;
|
|
/** Save EWC state (replaces any existing state) */
|
|
saveEwcState(record: EwcRecord): Promise<void>;
|
|
/** Load the EWC state, or null if none has been stored */
|
|
loadEwcState(): Promise<EwcRecord | null>;
|
|
/** Append a trajectory record (append-only, never overwritten) */
|
|
appendTrajectory(record: TrajectoryRecord): Promise<void>;
|
|
/**
|
|
* Return stored trajectories, newest first.
|
|
* @param limit Maximum number to return (default: all)
|
|
*/
|
|
getTrajectories(limit?: number): Promise<TrajectoryRecord[]>;
|
|
/** Return the number of stored trajectories */
|
|
getTrajectoryCount(): Promise<number>;
|
|
/**
|
|
* Flush all in-memory state to disk. The entire file is rewritten
|
|
* to ensure consistency (patterns may have been updated in-place).
|
|
*/
|
|
persist(): Promise<void>;
|
|
/** Persist and release resources */
|
|
close(): Promise<void>;
|
|
/** Return summary statistics about the store */
|
|
getStats(): Promise<{
|
|
patterns: number;
|
|
loraAdapters: number;
|
|
trajectories: number;
|
|
hasEwcState: boolean;
|
|
fileSizeBytes: number;
|
|
}>;
|
|
private loadFromDisk;
|
|
private applyRecord;
|
|
private ensureInitialized;
|
|
private log;
|
|
}
|
|
//# sourceMappingURL=rvf-learning-store.d.ts.map
|