fix(memory): rank the whole history and keep relevant recent records

- Search scored only the newest 5,000 matching records, so an old record
  behind enough newer chatter was never found. Both passes (word rarity counts
  and the score) now run in SQLite over every record in scope and only the
  best rows come back.
- context() excluded the recent window from search and then cut that window
  for the budget, so a relevant recent record could vanish. Search now covers
  the recent window, matches keep their half of the budget, and a matched
  record is removed from the recent block so it appears once.
- Matching used SQLite's ASCII-only lower() against Python's lowercasing.
  Stored text gets a folded column (NFKC + casefold) and query words are
  folded the same way, so case is ignored in every script.
- Prompt label "Records related to this question" (matches may now be recent);
  eval re-run 3x on qwen2.5:7b, all pass. Spec, memory.md and roadmap updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 12:50:37 +09:00
parent 54a465948a
commit 7a288129de
6 changed files with 125 additions and 66 deletions

View File

@@ -198,3 +198,30 @@ def test_search_over_a_large_history_stays_fast(store):
elapsed = time.perf_counter() - t
assert got and got[0].text == "희귀한 단어 오로라"
assert elapsed < 1.0
def test_search_covers_the_whole_history_not_just_the_newest_records(store):
store.add_voice(VC, "a", "Alice", "오늘 고양이 병원 예약은 목요일", T0)
for i in range(6000):
store.add_voice(VC, "a", "Alice", f"오늘 점심 이야기 {i}", T0 + 10 + i)
got = store.search("a", "오늘 고양이 병원 예약 언제", scope=Scope(), limit=3)
assert got and got[0].text == "오늘 고양이 병원 예약은 목요일"
def test_a_relevant_recent_record_survives_the_budget(store):
now = T0 + 1000
store.add_voice(VC, "a", "Alice", "고양이 병원 예약은 목요일", now - 600)
for i in range(19):
store.add_voice(VC, "a", "Alice", f"잡담 {i} " + "가" * 400, now - 500 + i)
ctx = store.context("a", "고양이 병원 예약 언제", now=now, scope=Scope())
lines = ctx.recent + ctx.matches
assert sum(1 for x in lines if "고양이 병원 예약은 목요일" in x) == 1
total = sum(len(x.encode("utf-8")) for x in lines) / 3
assert total <= 4000
def test_search_folds_case_the_same_way_for_every_script(store):
store.add_voice(VC, "a", "Alice", "Поездка в МОСКВА завтра", T0)
store.add_voice(VC, "a", "Alice", "Straße frei", T0 + 1)
assert texts(store.search("a", "москва", scope=Scope())) == ["Поездка в МОСКВА завтра"]
assert texts(store.search("a", "STRASSE", scope=Scope())) == ["Straße frei"]