Commit 686fb6f
feat(memory): persisted go-vector episode index + featurization quality (#12)
* feat(memory): persisted go-vector episode index + featurization quality
Fixes the two remaining memory weaknesses using only the existing go-vector
library — no new embedding dependency.
**#4 — per-turn LLM call eliminated.**
FormatEpisodeContext previously called episodes.Search → NewLLMRanker →
one llm.SimpleCall per turn. The RP fallback was no better at scale:
NewRPRanker re-instantiated RandomProjections and re-fit + re-embedded every
episode on every Search call (no caching).
New episodeVectorIndex (mirroring session/vector_index.go) persists a
go-vector Store + RandomProjections embedder to gob files. FormatEpisodeContext
now calls episodes.recallByVector → sharedEpisodeIndex.search → embed query +
N cached cosines. Zero LLM calls on the per-turn path.
Design: dirty-flag + full rebuild. RP must be Fit on the full corpus to produce
a valid vocabulary — incremental embedding after a stale Fit yields degenerate
vectors. Episodes are written at session-end (infrequent); rebuild is triggered
on the next search after any write, then cached for all subsequent turns until
the next write. One O(n) rebuild per session-end, then µs cosine per turn.
Process-wide singleton per memory directory (sharedEpisodeIndex, mirroring
factsDirLock) prevents concurrent serve.go per-connection managers from racing
on the gob files.
SearchEpisodes (explicit memory tool) now fetches candidates from the vector
index and LLM-reranks only those candidates (bounded), fixing the O(n)·LLM
cost in the explicit search path too. llm_search now gates explicit search
reranking only — per-turn recall always uses RP.
**#3 — featurization quality.**
New featurize.go: normalizeForEmbedding (lowercase + alphanumeric tokens,
strips punctuation so "Postgres," == "postgres") + featurizeForEmbedding
(normalise + bigrams "w1_w2" for light local word order). Applied at the
go-vector boundary in both the episode index (full ~1KB on-disk summaries,
up from 120-char truncated; 256 dims, up from 64) and MergeDetector.Fit/
Classify/AppendEntry/ReplaceEntry. Raw strings are preserved in m.corpus;
only the RP boundary uses featurized text.
Verified: FormatEpisodeContext fires zero LLM calls; postgres vs mysql episodes
rank distinctly; postgres vs "database is postgres" ranks similar; persistence
round-trips; dirty rebuild picks up new episodes; provenance filter holds;
concurrent safety under -race; all existing tests green.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* fix(memory): close verification findings on episode vector index (D-04/D-05/D-06)
Remediates the AI Verification Protocol findings on PR #12.
D-05 (OOV zero-score bypass): when a query has no vocabulary overlap with the
episode corpus, go-vector Embed returns a zero vector and Store.Search returns k
results all with cosine similarity=0. recallByVector was returning those as
non-empty candidates, so SearchEpisodes skipped the LLM fallback with noise.
Fix: filter zero-score results in recallByVector before returning; an all-OOV
query now returns nil, correctly triggering the fallback path in SearchEpisodes.
D-06 (SearchEpisodes at 52.9% coverage): four branches untested.
Added: llm_search=false (no LLM), nil LLM client, limit truncation, rankFn
error fallback, and OOV query triggering the nil-then-fallback path.
Coverage now 76.5%.
D-04 (multi-process caveat undocumented): the in-process singleton serializes
concurrent MemoryManagers within one process, but two separate odek processes
sharing ~/.odek/memory are not serialized. Added explicit documentation to the
singleton var block explaining the limitation and its bounded impact.
D-01/D-02/D-03/D-07/D-08/D-09 confirmed held (no fix needed): double-checked
locking is correct; per-TempDir tests prevent singleton bleed; corrupted emb gob
falls back to rebuild; featurization is symmetric across all Fit/Embed paths.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 0f9f586 commit 686fb6f
8 files changed
Lines changed: 880 additions & 21 deletions
File tree
- docs
- internal/memory
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
| 220 | + | |
221 | 221 | | |
222 | 222 | | |
223 | | - | |
224 | | - | |
| 223 | + | |
| 224 | + | |
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
0 commit comments