/** * WorkingMemoryBuffer — session-scoped hot cache for retrieval results. * * Holds the top-N most relevant SearchResults the agent has seen in a session, * keyed by result id. Each entry remembers the query tokens that produced it, * so a new query can be matched against past queries via Jaccard — cheap, * deterministic, no embedding round-trip. * * Semantics: * - put(results, query): inserts/updates, refreshes timestamps, prunes by * size after insertion using LRU-by-score (lowest score evicted first). * - prime(results): bulk seed without any associated query — used by session * priming. Entries are still subject to eviction; treat as "warm start". * - get(query, matchThreshold): returns matching entries with score boost. * - invalidate(ids): removes specific entries (e.g., after Git invalidation). * - evict(now): removes entries past TTL. Called opportunistically. * * Not thread-safe — designed for the single-process synapse runtime. */ import type { SearchResult } from "@massa-th0th/shared"; export interface WorkingMemoryBufferConfig { maxSize: number; ttlMs: number; /** Multiplier applied to a result's score when it is served from the buffer. */ hitBoost: number; /** Jaccard threshold over query tokens above which a buffer entry counts as a hit. */ matchThreshold: number; } export interface BufferGetResult { results: SearchResult[]; hitIds: Set; appliedBoost: boolean; } export declare class WorkingMemoryBuffer { readonly config: WorkingMemoryBufferConfig; private entries; constructor(config: WorkingMemoryBufferConfig); size(): number; has(id: string): boolean; /** * Pull entries matching the new query. * * Match decision (IMP-9): * - For query-seeded entries: Jaccard between the new query tokens and * the tokens of the queries that originally produced the entry. * - For primed entries (no associated query): Jaccard between the new * query tokens and the entry's *content* tokens. A primed entry only * surfaces if the query is plausibly looking for it. * * Score returned is always derived from the persisted baseline (IMP-8), * never from a previously boosted snapshot — this prevents compounding * across repeated buffer hits. */ get(query: string, now?: number): BufferGetResult; /** * Insert or refresh results from a normal retrieval. Each entry's queryTokens * is the union of tokens across all queries that have produced it. * * IMP-8: `baselineScore` is the *first* score observed (or the highest if * raw is unavailable). Subsequent puts do NOT compound it. This way, the * hit boost is always applied to a stable starting point regardless of * how many pipeline cycles have re-scored the entry. * * The optional `rawScore` param lets the caller hand in the pre-modulation * score explicitly — preferred whenever it is available. */ put(results: SearchResult[], query: string, now?: number, rawScores?: Map): void; /** * Seed the buffer with results that should be considered always-relevant * for this session — but only matched against new queries via content * tokens, not unconditionally (IMP-9). */ prime(results: SearchResult[], now?: number): void; /** Remove specific entries — e.g., when Git invalidation marks them stale. */ invalidate(ids: Iterable): number; evictExpired(now?: number): number; /** Remove the lowest-baseline-score entries when over maxSize. */ private evictToMaxSize; /** Test hook. */ clear(): void; } export declare const DEFAULT_BUFFER_CONFIG: WorkingMemoryBufferConfig; /** * Serializable snapshot of one buffer entry. Token Sets are regenerable from * `result.content` / the (absent on primed entries) query, so only scalars + * the result are persisted. Mirrors the shape written by the session stores. */ export interface BufferEntrySnapshot { id: string; addedAt: number; lastAccessedAt: number; baselineScore: number; result: SearchResult; } export interface BufferSnapshot { entries: BufferEntrySnapshot[]; config: WorkingMemoryBufferConfig; } /** * Reconstruct a LIVE WorkingMemoryBuffer from a persisted snapshot. * * The snapshot stores per-entry scalars (`addedAt`, `lastAccessedAt`, * `baselineScore`) + the result. We replay each entry so the primed working-set * survives a process restart: queryTokens is rebuilt empty (primed semantics — * matched via contentTokens) and contentTokens is re-tokenized from the result, * which is exactly how `prime()` would have built the entry. This keeps the * restored buffer observationally equivalent to the pre-restart one for the * purpose of `get()` matching and score boosting. * * Returns `undefined` when the snapshot is missing or malformed, so callers can * treat a failed restore as "fresh buffer that refills naturally" (the original * documented behavior). */ export declare function restoreWorkingMemoryBuffer(snapshot: BufferSnapshot | null | undefined): WorkingMemoryBuffer | undefined; /** * Snapshot a live WorkingMemoryBuffer for persistence. Best-effort: scalars * only (token Sets are regenerable from result content on restore). Shared by * the PostgreSQL and PG session stores so the snapshot shape stays consistent. * Returns `null` when the session has no buffer or it has no entries. */ export declare function snapshotWorkingMemoryBuffer(session: { buffer?: WorkingMemoryBuffer; }): BufferSnapshot | null; //# sourceMappingURL=working-memory-buffer.d.ts.map