MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / rankEpisodes

Function rankEpisodes

src/context/episodic-memory.ts:107–142  ·  view source on GitHub ↗
(query: string, episodes: Episode[], opts: RankOptions = {})

Source from the content-addressed store, hash-verified

105 * episodes, so the K injected are relevant AND distinct. 0 = legacy (pure relevance).
106 * Default 0.3 (gentle — relevance still dominates).
107 */
108 diversity?: number;
109 /**
110 * Optional file-existence predicate (injected; keeps this function PURE). An episode whose
111 * recorded `filesChanged` mostly no longer exist is stale guidance — its relevance is
112 * scaled down (not dropped: the *approach* may still help). Episodes with no files are
113 * unaffected. Mirrors the codebase-fit grounding used in skill judgment.
114 */
115 fileExists?: (file: string) => boolean;
116}
117
118/** Fraction of an episode's touched files that still exist (1 when none recorded — nothing to judge). */
119export function fileFreshness(files: string[], exists: (f: string) => boolean): number {
120 if (files.length === 0) return 1;
121 return files.filter(exists).length / files.length;
122}
123
124/**
125 * Rank episodes against a query by lexical similarity (prompt + summary), then select a
126 * RELEVANT-and-DIVERSE top-K. PURE.
127 *
128 * - Excludes near-identical re-runs of the exact same prompt (score ≥ 0.98) so "similar
129 * past work" doesn't just echo the current task back.
130 * - File grounding: episodes pointing at files that no longer exist are scaled down
131 * (`fileExists`), so stale episodes lose to fresh ones.
132 * - MMR diversity (`diversity`): greedily pick highest relevance, penalising each remaining
133 * candidate by its similarity to what's already chosen — kills redundant injection.
134 * - Recency tie-break: episodes arrive oldest→newest, so a later index breaks near-ties
135 * toward the more recent solution (clock-free, stays pure).
136 */
137/**
138 * Light FA→EN gloss for recall only (not skill-dedup). Lets «لاگ» overlap `log`
139 * without a translation model. PURE.
140 */
141const RECALL_GLOSS: [RegExp, string][] = [
142 [/لاگ/g, ' log '],
143 [/فرمت/g, ' format '],
144 [/هلپر/g, ' helper '],
145 [/تست(?:‌ها)?/g, ' test '],

Callers 2

loadEpisodeBlockFunction · 0.85

Calls 5

termFreqFunction · 0.85
tokenizeFunction · 0.85
fileFreshnessFunction · 0.85
cosineSimFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected