Skip to content

Memory & Review ​

Memory is not "store the chat log": after each session the Memory Curator distills evidence-backed structured memory, the learner model updates mastery BKT-style, and a review queue schedules spaced repetition. Every number is a hard constant, recomputable.

Four memory kinds ​

KindTableSemantics
corememory_blocksuser/role core blocks; retrieval hits score a fixed 0.75
factfactsfacts with entity, a subject key and a validity window (valid_from / valid_to); retrieval only takes valid_to IS NULL
episodeepisodessession summaries; retrieval takes the latest 40
reflectionreflectionsreview reflections; curator output tagged curator, weight grows with use

Retrieval scoring (scoreText):

text
score = 0.55 × text relevance + 0.25 × importance + 0.2 × recency
recency = exp(−ageDays / halfLife)

Half-life depends on the kind: episode 45 days, fact 180, reflection 90. A two-week-old episode about one session should fade; "the candidate prefers backend" should not fade with it.

Text relevance takes the better of two legs: literal matching and an FTS5 index (memory_fts), segmented the same way as the knowledge base (CJK characters + bigrams). Without the second leg, "how do you handle cache breakdown" is one token and almost never matches. The index backfills itself on first search.

A reflection's weight is earned: a new one starts at 0.45 and gains 0.1 each time it is actually injected (cap 0.95), recorded by markReflectionsUsed(). Otherwise one written reflection stays authoritative forever, including a wrong one.

Two facts about one subject: side by side, never overwritten ​

facts.subject is a normalised topic key (the curator must emit it; a normalised text fallback covers omissions). When two facts share a subject and disagree, the reader gets both, newest first, and decides — overwriting at write time loses the older statement's detail. valid_to stays for manual invalidation only.

Memory Curator: post-session only, best-effort ​

  • Single trigger: the finish flow after the report is persisted; a curator outage emits curator_failed but the session still reaches ended — memory consolidation must never block the main flow.
  • Proposals go through memory.propose (zod: kind ∈ fact|episode|reflection + optional evidence); the engine applies them:
    • episode → episodes, importance default 0.6;
    • fact → facts, confidence default 0.7, source = sessionId;
    • reflection → reflections, tags ['curator'].
  • The same step also runs per-question updateLearnerModel, consolidateSession (with JD gaps top-2) and a snapshotLearner snapshot.

Forgetting must be explainable ​

Memory rows only disappear when a session is deleted, and that leaves a trace: purgeSession writes each episode / reflection / fact into memory_evictions (migration 17) before deleting, and GET /memory/evictions answers "which session, when, what".

Without that trail, "why don't I remember this" has three indistinguishable answers: never recorded, deleted with its session, or superseded.

Maintenance and quality ​

  • reviewCoreBlocks(): a core block (always in the prompt) with no valid supporting fact is marked stale with a reason. Support is matched on title tokens — the body is too loose, because CJK bigrams make two unrelated sentences sharing "candidate" look like support.
  • promotionCandidates(): facts at importance ≥ 0.8 that no core block covers.
  • Both are read-only: retiring always-on memory is a user decision, not a background job's.
  • GET /memory/quality: counts per kind, reflections actually used, mean fact importance, conflicting subjects, stale core blocks.

Learner model: BKT-style mastery ​

learner_kp starts at importance 0.6 / mastery 0.5 / confidence 0.3. Each answer updates:

text
pCorrect  = prior × (1 − slip) + (1 − prior) × guess      slip 0.1 · guess 0.2
posterior = Bayesian posterior (observed ≥ 0.5 counts as correct)
mastery   = clamp(posterior × (1 − learn) + observed × learn, 0.05, 0.98)   learn 0.12
confidence = min(0.95, confidence + 0.08)

The session's weakest topic additionally loses 0.03 mastery (floor 0.05) — a weak spot is not erased by one high score.

Review queue: spaced repetition ​

Two enqueue sources:

SourceDueStabilityDifficultyDedup
session's weakest question+1 day1.26skip if the kp already has a future due (rescores don't flood)
JD gaps top-2 (JD · <gap>)+2 days15skip if any row exists for the kp

Completing a review (completeReview, rating again / hard / good / easy):

text
stability  = max(0.4, stability × {again 0.3, hard 1.2, good 2.2, easy 3.2})
difficulty = clamp(difficulty + {again +0.6, hard +0.2, good 0, easy −0.4}, 1, 10)
interval   = again → 0.2 day; otherwise max(1, round(stability)) days

Each review writes review_log (rating / stability / difficulty / due) and runs another BKT-style mastery update with observed (again 0.15 / hard 0.45 / good 0.8 / easy 0.95), confidence +0.05.

Heatmap and wrong-book ​

  • Heatmap (GET /api/v1/heatmap): learner_kp grouped by theme, mastery/confidence weighted by importance (practised only), plus a kp_snapshots session timeline — progress is visible.
  • Wrong-book (GET /api/v1/review/wrong-book): threshold default 70 (query-tunable, clamped 1–100); questions below it enter the wrong-book for review.

Structure at a glance ​

Figure 1 · Three destinations for memory, and the loop back

Local-first · inference egresses on demand · fully traceable