Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat(memory): add recall — what the learner touched lately, across surfaces

Bingxi Zhao (Frank)da6dfa002bc4
diff --git a/deeptutor/services/memory/recall.py b/deeptutor/services/memory/recall.py
new file mode 100644
index 000000000..062f2a840
--- /dev/null
+++ b/deeptutor/services/memory/recall.py
@@ -0,0 +1,237 @@
+"""Reading memory back — what the learner has been doing lately.
+
+L1 keeps two kinds of record, and answering "what happened lately" needs both:
+
+* the **snapshot** knows what *exists* — every chat session, notebook record,
+ quiz attempt, document, book and knowledge base, each with a label and a
+ timestamp. :func:`recent` reads that.
+* the **trace** knows what was *done* — the events surfaces emit as they run.
+ :func:`recent_queries` reads the one kind that records the learner asking
+ something in their own words.
+
+Both run on stamps: no message body, no document text, no answer is read. That
+is what makes them safe to call from an interactive path rather than only from
+consolidation, which is the whole reason this module exists separately from
+:mod:`deeptutor.services.memory.store`.
+
+Every hit carries ``days_ago`` rather than leaving an ISO timestamp for the
+caller to subtract. A model asked to do date arithmetic on
+``2026-08-16T11:03:00+00:00`` will sometimes get it wrong, and "how long ago"
+is the entire basis on which anything decides whether an item is still worth
+raising.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from datetime import datetime, timedelta, timezone
+import logging
+from typing import Any, Iterable
+
+from deeptutor.services.memory.paths import SURFACES, Surface
+
+logger = logging.getLogger(__name__)
+
+_DEFAULT_LIMIT = 20
+_MAX_LIMIT = 100
+# A week: long enough to survive a couple of quiet days, short enough that what
+# comes back is still recognisably "lately".
+_DEFAULT_DAYS = 7
+# A label is a title, a question or a filename. Anything longer is a body that
+# leaked into one, and carrying it would defeat the point of stamps-only reads.
+_MAX_LABEL_CHARS = 200
+
+
+@dataclass(frozen=True, slots=True)
+class RecallHit:
+ """One thing the learner touched, shaped for a caller to act on."""
+
+ surface: str
+ label: str
+ ts: str
+ days_ago: int | None
+
+ def to_dict(self) -> dict[str, Any]:
+ return {
+ "surface": self.surface,
+ "label": self.label,
+ "ts": self.ts,
+ "days_ago": self.days_ago,
+ }
+
+
+# ── Time ─────────────────────────────────────────────────────────────────
+
+
+def _now() -> datetime:
+ return datetime.now(tz=timezone.utc)
+
+
+def _parse_ts(ts: str) -> datetime | None:
+ if not ts:
+ return None
+ try:
+ parsed = datetime.fromisoformat(str(ts).replace("Z", "+00:00"))
+ except (TypeError, ValueError):
+ return None
+ return parsed if parsed.tzinfo else parsed.replace(tzinfo=timezone.utc)
+
+
+def days_ago(ts: str, *, now: datetime | None = None) -> int | None:
+ """Whole days between ``ts`` and now, or ``None`` for an unusable stamp."""
+ parsed = _parse_ts(ts)
+ if parsed is None:
+ return None
+ delta = (now or _now()) - parsed
+ # A future stamp (clock skew on an imported record) reads as today rather
+ # than as a negative age.
+ return max(0, delta.days)
+
+
+def _cutoff(days: int | None, now: datetime) -> datetime | None:
+ if days is None or days < 0:
+ return None
+ return now - timedelta(days=days)
+
+
+# ── Shaping ──────────────────────────────────────────────────────────────
+
+
+def _clean_label(raw: str) -> str:
+ """One line, bounded. Empty when there is nothing worth showing."""
+ text = " ".join(str(raw or "").split())
+ return text[:_MAX_LABEL_CHARS]
+
+
+def _resolve_surfaces(surfaces: Iterable[str] | None) -> tuple[Surface, ...]:
+ if not surfaces:
+ return SURFACES
+ wanted = {str(surface).strip().lower() for surface in surfaces}
+ resolved = tuple(surface for surface in SURFACES if surface in wanted)
+ return resolved or SURFACES
+
+
+def _clamp_limit(limit: int | None) -> int:
+ if not limit or limit < 1:
+ return _DEFAULT_LIMIT
+ return min(int(limit), _MAX_LIMIT)
+
+
+def _newest_first(hits: list[RecallHit]) -> list[RecallHit]:
+ """Sort newest first, dropping repeats of the same label on the same surface.
+
+ A knowledge base queried five times, or a session reopened all afternoon,
+ is one thing the learner is working on — not five. Keeping the duplicates
+ would let a single topic crowd out everything else downstream.
+ """
+ hits.sort(key=lambda hit: hit.ts, reverse=True)
+ seen: set[tuple[str, str]] = set()
+ deduped: list[RecallHit] = []
+ for hit in hits:
+ key = (hit.surface, hit.label.casefold())
+ if key in seen:
+ continue
+ seen.add(key)
+ deduped.append(hit)
+ return deduped
+
+
+# ── What exists (snapshot) ───────────────────────────────────────────────
+
+
+def recent(
+ *,
+ days: int | None = _DEFAULT_DAYS,
+ limit: int | None = _DEFAULT_LIMIT,
+ surfaces: Iterable[str] | None = None,
+) -> list[RecallHit]:
+ """The latest workspace activity across surfaces, newest first.
+
+ Stamps only — no content is read, so this stays cheap enough to call on a
+ page load.
+
+ Items without a usable timestamp are dropped rather than kept: placing an
+ item of unknown age inside "the last seven days" asserts something the data
+ does not support. This notably excludes knowledge bases, whose snapshot
+ timestamp is their *earliest* index time — a KB in daily use would look
+ stale here. What the learner has actually been asking a KB lives in the
+ trace instead; see :func:`recent_queries`.
+ """
+ from deeptutor.services.memory.snapshot import adapters
+
+ now = _now()
+ cutoff = _cutoff(days, now)
+ hits: list[RecallHit] = []
+ for surface in _resolve_surfaces(surfaces):
+ try:
+ stamps = adapters.read_stamps(surface)
+ except Exception:
+ logger.warning("recall.recent: stamps failed surface=%s", surface, exc_info=True)
+ continue
+ for stamp in stamps:
+ parsed = _parse_ts(stamp.ts)
+ if parsed is None or (cutoff is not None and parsed < cutoff):
+ continue
+ label = _clean_label(stamp.label)
+ if not label:
+ continue
+ hits.append(
+ RecallHit(
+ surface=surface,
+ label=label,
+ ts=stamp.ts,
+ days_ago=days_ago(stamp.ts, now=now),
+ )
+ )
+ return _newest_first(hits)[: _clamp_limit(limit)]
+
+
+# ── What was asked (trace) ───────────────────────────────────────────────
+
+# The trace kinds that carry a learner-authored question. Knowledge-base
+# queries are the only ones today: the other emitters record system-side facts
+# (a stated preference, a partner turn), not something the learner typed as a
+# question.
+_QUERY_KINDS: dict[Surface, str] = {"kb": "query"}
+
+
+def recent_queries(
+ *,
+ days: int | None = _DEFAULT_DAYS,
+ limit: int | None = _DEFAULT_LIMIT,
+) -> list[RecallHit]:
+ """Searches the learner ran lately, in their own words, newest first.
+
+ Complements :func:`recent`: the snapshot can say a knowledge base exists,
+ but only the trace records that someone asked it about eigenvalues on
+ Tuesday.
+ """
+ from deeptutor.services.memory import trace
+
+ now = _now()
+ cutoff = _cutoff(days, now)
+ hits: list[RecallHit] = []
+ for surface, kind in _QUERY_KINDS.items():
+ try:
+ events = trace.iter_since(surface, cutoff)
+ except Exception:
+ logger.warning("recall.recent_queries: failed surface=%s", surface, exc_info=True)
+ continue
+ for event in events:
+ if event.kind != kind:
+ continue
+ label = _clean_label(str((event.payload or {}).get("query") or ""))
+ if not label:
+ continue
+ hits.append(
+ RecallHit(
+ surface=surface,
+ label=label,
+ ts=event.ts,
+ days_ago=days_ago(event.ts, now=now),
+ )
+ )
+ return _newest_first(hits)[: _clamp_limit(limit)]
+
+
+__all__ = ["RecallHit", "days_ago", "recent", "recent_queries"]
diff --git a/tests/services/memory/test_recall.py b/tests/services/memory/test_recall.py
new file mode 100644
index 000000000..4cdd33886
--- /dev/null
+++ b/tests/services/memory/test_recall.py
@@ -0,0 +1,243 @@
+"""Recall — "what happened lately", across surfaces, on stamps alone."""
+
+from __future__ import annotations
+
+from datetime import datetime, timedelta, timezone
+import json
+from pathlib import Path
+
+import pytest
+
+from deeptutor.services.memory import recall
+from deeptutor.services.memory.snapshot.entity import EntityStamp
+
+
+def _iso(days: float) -> str:
+ """An ISO stamp *days* in the past."""
+ return (datetime.now(tz=timezone.utc) - timedelta(days=days)).isoformat()
+
+
+def _stub_stamps(
+ monkeypatch: pytest.MonkeyPatch, by_surface: dict[str, list[EntityStamp]]
+) -> None:
+ from deeptutor.services.memory.snapshot import adapters
+
+ monkeypatch.setattr(adapters, "read_stamps", lambda surface: by_surface.get(surface, []))
+
+
+def _stamp(label: str, ts: str, ident: str = "") -> EntityStamp:
+ return EntityStamp(id=ident or label, label=label, fingerprint="fp", ts=ts)
+
+
+# ── recent ───────────────────────────────────────────────────────────────
+
+
+def test_recent_merges_surfaces_newest_first(monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [_stamp("Chain rule", _iso(2))],
+ "quiz": [_stamp("What is an eigenvalue?", _iso(0.5))],
+ "book": [_stamp("Calculus notes", _iso(4))],
+ },
+ )
+
+ hits = recall.recent()
+
+ assert [(h.surface, h.label) for h in hits] == [
+ ("quiz", "What is an eigenvalue?"),
+ ("chat", "Chain rule"),
+ ("book", "Calculus notes"),
+ ]
+ assert hits[0].days_ago == 0
+ assert hits[1].days_ago == 2
+
+
+def test_recent_honours_the_lookback_window(monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [_stamp("Yesterday", _iso(1)), _stamp("Last month", _iso(30))],
+ },
+ )
+
+ assert [h.label for h in recall.recent(days=7)] == ["Yesterday"]
+ assert len(recall.recent(days=None)) == 2
+
+
+def test_recent_drops_undated_items(monkeypatch: pytest.MonkeyPatch) -> None:
+ """An item of unknown age must not be asserted into "the last seven days".
+
+ This is what keeps knowledge bases out: their snapshot timestamp is the
+ earliest index time, so a KB in daily use would still look ancient.
+ """
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [_stamp("Dated", _iso(1))],
+ "kb": [_stamp("Physics KB", "")],
+ },
+ )
+
+ assert [h.label for h in recall.recent()] == ["Dated"]
+
+
+def test_recent_keeps_only_the_newest_of_a_repeated_label(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [
+ _stamp("Chain rule", _iso(3), ident="s1"),
+ _stamp("chain RULE", _iso(1), ident="s2"),
+ ],
+ },
+ )
+
+ hits = recall.recent()
+
+ assert len(hits) == 1
+ assert hits[0].days_ago == 1
+
+
+def test_recent_same_label_on_different_surfaces_is_kept(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [_stamp("Eigenvalues", _iso(1))],
+ "book": [_stamp("Eigenvalues", _iso(2))],
+ },
+ )
+
+ assert {h.surface for h in recall.recent()} == {"chat", "book"}
+
+
+def test_recent_respects_limit_and_surface_filter(monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {
+ "chat": [_stamp(f"S{i}", _iso(i)) for i in range(5)],
+ "quiz": [_stamp("Q1", _iso(0.1))],
+ },
+ )
+
+ assert len(recall.recent(limit=2)) == 2
+ assert {h.surface for h in recall.recent(surfaces=["chat"])} == {"chat"}
+ # An unrecognised surface name falls back to all rather than to nothing.
+ assert {h.surface for h in recall.recent(surfaces=["nope"])} == {"chat", "quiz"}
+
+
+def test_recent_survives_one_broken_surface(monkeypatch: pytest.MonkeyPatch) -> None:
+ from deeptutor.services.memory.snapshot import adapters
+
+ def _read(surface: str) -> list[EntityStamp]:
+ if surface == "chat":
+ raise RuntimeError("db locked")
+ return [_stamp("Calculus notes", _iso(1))] if surface == "book" else []
+
+ monkeypatch.setattr(adapters, "read_stamps", _read)
+
+ assert [h.label for h in recall.recent()] == ["Calculus notes"]
+
+
+def test_recent_collapses_whitespace_and_bounds_labels(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _stub_stamps(
+ monkeypatch,
+ {"chat": [_stamp("a ragged\n\ntitle", _iso(1)), _stamp("x" * 400, _iso(2))]},
+ )
+
+ labels = [h.label for h in recall.recent()]
+
+ assert labels[0] == "a ragged title"
+ assert len(labels[1]) == 200
+
+
+# ── recent_queries ───────────────────────────────────────────────────────
+
+
+class _FakePathService:
+ def __init__(self, root: Path) -> None:
+ self._root = root
+
+ def get_memory_dir(self) -> Path:
+ return self._root
+
+
+def memory_root(tmp_path: Path):
+ from deeptutor.services.memory.paths import memory_path_service_override
+
+ with memory_path_service_override(_FakePathService(tmp_path)):
+ yield tmp_path
+
+
+def _write_trace(root: Path, surface: str, events: list[dict]) -> None:
+ directory = root / "trace" / surface
+ directory.mkdir(parents=True, exist_ok=True)
+ by_day: dict[str, list[str]] = {}
+ for event in events:
+ day = event["ts"][:10]
+ by_day.setdefault(day, []).append(json.dumps(event, ensure_ascii=False))
+ for day, lines in by_day.items():
+ (directory / f"{day}.jsonl").write_text("\n".join(lines) + "\n", encoding="utf-8")
+
+
+def _query_event(query: str, ts: str) -> dict:
+ return {
+ "id": f"kb:{query}",
+ "ts": ts,
+ "surface": "kb",
+ "kind": "query",
+ "payload": {"query": query, "kb_name": "Physics", "answer_chars": 120},
+ "session_id": None,
+ "turn_id": None,
+ }
+
+
+def test_recent_queries_reads_the_learners_own_words(memory_root: Path) -> None:
+ _write_trace(
+ memory_root,
+ "kb",
+ [
+ _query_event("what is an eigenvalue", _iso(1)),
+ _query_event("how does backprop work", _iso(0.2)),
+ ],
+ )
+
+ hits = recall.recent_queries()
+
+ assert [h.label for h in hits] == ["how does backprop work", "what is an eigenvalue"]
+ assert {h.surface for h in hits} == {"kb"}
+
+
+def test_recent_queries_ignores_other_event_kinds(memory_root: Path) -> None:
+ other = _query_event("indexed something", _iso(1))
+ other["kind"] = "index"
+ _write_trace(memory_root, "kb", [other, _query_event("real question", _iso(1))])
+
+ assert [h.label for h in recall.recent_queries()] == ["real question"]
+
+
+def test_recent_queries_dedupes_a_repeated_search(memory_root: Path) -> None:
+ _write_trace(
+ memory_root,
+ "kb",
+ [
+ _query_event("eigenvalues", _iso(3)),
+ _query_event("eigenvalues", _iso(1)),
+ ],
+ )
+
+ hits = recall.recent_queries()
+
+ assert len(hits) == 1
+ assert hits[0].days_ago == 1
+
+
+def test_recent_queries_is_empty_without_a_trace(memory_root: Path) -> None:
+ assert recall.recent_queries() == []