UNPKG

open-memex

Version:

Local-first memory layer and protocol for AI coding agents. Markdown source of truth, SQLite FTS5 index, zero cloud.

96 lines (95 loc) • 3.88 kB
/** * D79 (first slice): reuse-signal ranking. * * D77-L2 already records `retrieved_count` (exposure) and the useful split * (explicit confirmations only). This module turns that telemetry into a * deliberately small ranking nudge: inside a near-tied BM25 band, memories * with a high `reuse_rate` move ahead of equally relevant memories that were * only ever retrieved. * * Guardrails (D77, non-negotiable): * - the score itself is never modified — feedback only reorders a band, so * `search --explain` still shows the raw BM25 number; * - only memories with `retrieved_count >= FEEDBACK_MIN_RETRIEVED` participate; * new/rare memories keep their exact BM25 slot (no cold-start penalty), * and an eligible run never jumps over one of them; * - `reuse_rate` is signal strength, not a probability (it may exceed 1.0); * it is never used as a hard prune threshold; * - band membership is anchored to the band head's score, so a large score gap * cannot be crossed. Review tiers are reordered separately by search.ts — * feedback never lifts a draft over reviewed knowledge. * * Pure by construction: smoke-pure.ts tests it without opening SQLite. */ /** Minimum exposure before a memory may be reordered by feedback. */ export const FEEDBACK_MIN_RETRIEVED = 3; /** * A hit is in the same band as the band head while its score is at least * this fraction of the head's. 0.75 = within 25%, i.e. a near-tie, not a * relevance gap. Measured on the D79 fixture (see docs/design/decisions.md). */ export const DEFAULT_FEEDBACK_BAND_RATIO = 0.75; /** * Reorder one review tier. Callers pass a tier's hits in BM25 order. Within * each near-tied band, only contiguous runs of eligible hits are re-sorted by * reuse_rate (descending, stable); ineligible hits keep their exact slot. So a * memory with too little telemetry is never pushed down by this mechanism. */ export function applyFeedbackRanking(hits, usage, opts = {}) { const minRetrieved = opts.minRetrieved ?? FEEDBACK_MIN_RETRIEVED; const bandRatio = opts.bandRatio ?? DEFAULT_FEEDBACK_BAND_RATIO; const info = new Map(); for (const h of hits) { const u = usage.get(h.id); const retrieved = Math.max(0, Number(u?.retrieved) || 0); const useful = Math.max(0, Number(u?.useful) || 0); const rate = retrieved > 0 ? useful / retrieved : null; info.set(h.id, { retrieved, useful, rate, eligible: retrieved >= minRetrieved, }); } const ordered = []; let i = 0; while (i < hits.length) { const head = hits[i]; const threshold = head.score > 0 ? head.score * bandRatio : head.score; let j = i + 1; while (j < hits.length && hits[j].score >= threshold) j++; const band = hits.slice(i, j); ordered.push(...reorderBand(band, info)); i = j; } return { ordered, info }; } function reorderBand(band, info) { if (band.length < 2) return [...band]; const out = [...band]; let i = 0; while (i < band.length) { if (!info.get(band[i].id).eligible) { i++; continue; } let j = i + 1; while (j < band.length && info.get(band[j].id).eligible) j++; if (j - i > 1) { // Stable sort of the run only. Ineligible hits are barriers, so a run // can never jump over a memory with too little telemetry. const run = band.slice(i, j).map((h, k) => ({ h, k })); run.sort((a, b) => { const ra = info.get(a.h.id).rate ?? 0; const rb = info.get(b.h.id).rate ?? 0; return ra !== rb ? rb - ra : a.k - b.k; }); run.forEach((x, k) => (out[i + k] = x.h)); } i = j; } return out; }