Commit b747d38
feat: optional embedding-based dedup on reflection memory writes
Adds two new AppSettings — reflectionSemanticDedupEnabled (boolean,
default false) and reflectionSemanticDedupThreshold (number, default
0.88) — that layer cosine-similarity dedup on top of the existing
text-equality cross-run dedup in writeReflectionMemories.
Motivation: the existing cross-run dedup catches reflections only when
the LLM produces literally-identical normalized text. In practice the
classifier rederives the same insight in different words across runs
("Always verify before acting" vs "Confirm state first"); text-equality
misses these, and 95% of memory rows end up as routine reflection/*.
Embedding-based dedup catches the near-duplicates.
How it works:
- In writeReflectionMemories, after the existing text-equality cross-run
dedup loop, if reflectionSemanticDedupEnabled is set: pull recent
reflection memories' embeddings (new memoryDb.recentReflectionEmbeddings
method), embed each candidate note with the configured embedder, and
skip notes whose cosine similarity to any recent reflection exceeds
the threshold.
- Falls back gracefully when embeddings aren't configured (getEmbedding
returns null), when recent memories have no stored embeddings, or on
any error — never blocks the write.
- writeReflectionMemories becomes async (sole caller already in an async
fn, so the change is contained).
Why opt-in: each candidate note costs one embedder call (~10-20 ms local
nomic-embed-text, more for remote providers). For deployments with the
local Ollama setup running already, the cost is negligible; for OpenAI
embedding users it adds tokens. Default false leaves behavior unchanged.
Files:
- src/types/app-settings.ts: 2 new optional fields
- src/lib/server/memory/memory-db.ts: recentReflectionEmbeddings method
- src/lib/server/autonomy/supervisor-reflection.ts: semantic dedup pass
in writeReflectionMemories (now async; call site awaits)1 parent 4d03800 commit b747d38
3 files changed
Lines changed: 74 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
747 | | - | |
| 747 | + | |
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
| |||
761 | 761 | | |
762 | 762 | | |
763 | 763 | | |
764 | | - | |
| 764 | + | |
765 | 765 | | |
766 | 766 | | |
767 | 767 | | |
| |||
809 | 809 | | |
810 | 810 | | |
811 | 811 | | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
812 | 853 | | |
813 | 854 | | |
814 | 855 | | |
815 | 856 | | |
816 | 857 | | |
| 858 | + | |
817 | 859 | | |
818 | 860 | | |
819 | 861 | | |
| |||
1085 | 1127 | | |
1086 | 1128 | | |
1087 | 1129 | | |
1088 | | - | |
1089 | | - | |
1090 | | - | |
1091 | | - | |
1092 | 1130 | | |
1093 | 1131 | | |
1094 | 1132 | | |
| |||
1100 | 1138 | | |
1101 | 1139 | | |
1102 | 1140 | | |
1103 | | - | |
| 1141 | + | |
1104 | 1142 | | |
1105 | 1143 | | |
1106 | 1144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1270 | 1270 | | |
1271 | 1271 | | |
1272 | 1272 | | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
1273 | 1297 | | |
1274 | 1298 | | |
1275 | 1299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
| 114 | + | |
122 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
123 | 120 | | |
124 | 121 | | |
125 | 122 | | |
| |||
0 commit comments