/** * Memory-health DEEP metrics (FEAT-444) — the heavy half of the memory-health * score, deliberately kept OUT of memory-health.ts's cheap/full tiers so * they can never accidentally get wired into `GET /health` or the default * `doctor` pass (design review, FEAT-444 section: "duplication-rate is a * particular risk ... running anything resembling [consolidate.ts's O(n^2) * pairwise scan] inline on a hot health-check path ... would be far worse * than the bug [migration 019] just fixed"). Callers: the CLI's * `astramem-local memory health --deep` flag only. * * Both estimates below are bounded samples, not exhaustive scans — the * bound is returned alongside the result so a report never silently implies * more coverage than it actually has. */ import type { DB } from '../../storage/db.js'; import type { LLMProvider } from '../../contracts/llm.js'; export interface NearDuplicateBound { maxGroups: number; maxPerGroup: number; /** Same cosine threshold as consolidate.ts's default `proposeThreshold` — * a near-duplicate estimate should agree with what consolidation itself * would flag, not use an unrelated cutoff. */ threshold: number; } export interface NearDuplicateEstimate { /** (repo, type) groups actually scanned (capped at bound.maxGroups, the * largest groups first — those are where near-duplicates concentrate). */ scannedGroups: number; /** Atoms actually compared (each group capped at bound.maxPerGroup). */ sampledAtoms: number; sampledPairs: number; nearDuplicatePairs: number; /** nearDuplicatePairs / sampledPairs; null when sampledPairs === 0. */ rate: number | null; bound: NearDuplicateBound; } /** * Bounded near-duplicate estimate: groups active, embedded atoms by (repo, * type) exactly like consolidate.ts does, takes the `maxGroups` LARGEST * groups (most likely to contain near-duplicates), samples up to * `maxPerGroup` atoms per group (most-recently-created first), and runs the * same pairwise cosine consolidate.ts uses. This is O(maxGroups * * maxPerGroup^2) by construction, independent of total corpus size — a * deliberate bound, not a full O(n^2) scan of the whole store. Document the * bound in the printed report (AC intent: "reuse consolidation similarity on * a bounded sample, document the bound"). */ export declare function estimateNearDuplicates(db: DB, opts?: { maxGroups?: number; maxPerGroup?: number; threshold?: number; }): NearDuplicateEstimate; export interface JunkHeuristicBound { sampleBound: number; ultraShortChars: number; lowImportanceThreshold: number; } export interface JunkHeuristicEstimate { sampledCount: number; ultraShortCount: number; lowImportanceCount: number; neverRecalledCount: number; /** Union of the three flags (an atom counted once even if it trips more * than one heuristic). */ junkCount: number; /** junkCount / sampledCount; null when sampledCount === 0. */ rate: number | null; bound: JunkHeuristicBound; } /** * Samples up to `sampleBound` active atoms (most-recently-created first) and * flags: ultra-short text (< ultraShortChars, normalized-length), low * importance (< lowImportanceThreshold), and never-recalled (no * recall_served/recall_used usefulness event ever recorded for that atom). * The never-recalled set is looked up via a single IN-list query against * the sampled ids — bounded by the sample size, not the whole * memory_events table. */ export declare function estimateJunkHeuristic(db: DB, opts?: { sampleBound?: number; ultraShortChars?: number; lowImportanceThreshold?: number; }): JunkHeuristicEstimate; export interface LlmJudgedJunkResult { enabled: boolean; sampledCount: number; judgedJunkCount: number; rate: number | null; } /** * AC-5: when `opts.enabled` is false (the config default, * `memoryHealth.llmJudgedJunkSample.enabled`), this returns immediately with * zeros and makes NO provider call whatsoever — the only way the judged * sample activates is an explicit config opt-in that the CLI checks before * ever constructing a provider or calling this function with enabled=true. * A best-effort, never-throws parse: any response line that doesn't parse * cleanly is simply not counted (informational metric, must never fail the * CLI report over an LLM formatting slip). */ export declare function estimateLlmJudgedJunk(db: DB, opts: { enabled: boolean; provider?: LLMProvider; sampleBound?: number; }): Promise;