Skip to content

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

fix(home): three reasons the starter lines never appeared

Bingxi Zhao (Frank)8226352274f4
diff --git a/deeptutor/services/suggestions.py b/deeptutor/services/suggestions.py
index 347707391..1aca1c332 100644
--- a/deeptutor/services/suggestions.py
+++ b/deeptutor/services/suggestions.py
@@ -37,9 +37,11 @@ when the *content* should change, the second is so the same three lines do not
greet someone all week. A manual refresh bypasses both and generates
synchronously, because there a human is deliberately waiting.
-Empty is a valid answer. A learner with no history gets no generated lines and
-no LLM call; the frontend shows a fixed set of product starters instead, which
-is honest about knowing nothing yet.
+Empty is a valid answer. A learner with no usable history gets no lines and no
+LLM call, and the frontend renders nothing at all — generic copy in that slot
+would teach people to stop reading it. But empty is only *cached* when it is
+the right answer: an empty result produced despite having material is a
+failure, and writing it would pin that failure in place for a whole TTL.
"""
from __future__ import annotations
@@ -65,12 +67,37 @@ _TTL_SECONDS = 6 * 3600
# meaningfully change that fast.
_PROBE_INTERVAL_SECONDS = 60.0
_LLM_TIMEOUT = 25.0
-# How much history shapes the lines.
-_LOOKBACK_DAYS = 7
+# How much history shapes the lines. A month rather than a week: labels are
+# deduplicated by name and capped per surface, so a long window does not mean a
+# long list — it means a learner who spent this week on one thing still has
+# something to be offered. A week left an active user with a single usable
+# trace once repeats were collapsed.
+_LOOKBACK_DAYS = 30
_MAX_TOPICS = 8
-# Per surface, so three chats cannot crowd out the quiz and the KB. The whole
-# point of reading every surface is that the three differ in kind.
-_MAX_PER_SURFACE = 2
+# Per surface, so chat cannot crowd out the quiz and the KB. The whole point of
+# reading every surface is that the three differ in kind.
+_MAX_PER_SURFACE = 3
+# Below this a label carries no subject to propose anything about — a knowledge
+# base named "q", a search for "q2", an untitled draft. They are real activity
+# and recall is right to return them; they just cannot ground a suggestion, and
+# left in they crowd out traces that can.
+_MIN_TOPIC_CHARS = 3
+# Titles a surface assigns before the learner (or the model) names the thing.
+# They are the *newest* rows by definition — a conversation is created before
+# it is titled — so without this they take the per-surface budget and leave the
+# named work outside it. Mirrors ``DEFAULT_SESSION_TITLE`` in
+# ``web/lib/session-title.ts``; drifting means one placeholder slips through,
+# which costs a suggestion rather than breaking anything.
+_PLACEHOLDER_LABELS = frozenset(
+ {
+ "new conversation",
+ "new chat",
+ "新对话",
+ "untitled",
+ "untitled draft",
+ "无标题",
+ }
+)
# The line the learner reads, and the message behind it. Over-long output means
# the model ignored the brief; the item is dropped rather than truncated,
# because half a sentence is worse than one fewer starting point. The label
@@ -220,6 +247,9 @@ def _collect_topics() -> list[_Topic]:
by_surface: dict[str, list[_Topic]] = {}
def _add(surface: str, label: str, age: int | None) -> None:
+ stripped = label.strip()
+ if len(stripped) < _MIN_TOPIC_CHARS or stripped.casefold() in _PLACEHOLDER_LABELS:
+ return
bucket = by_surface.setdefault(surface, [])
if len(bucket) >= _MAX_PER_SURFACE:
return
@@ -280,6 +310,7 @@ Other good shapes: "Why the chain rule underlies backpropagation" / "Where self-
Rules:
- Reply with ONLY a JSON array of exactly 3 such objects. No prose, no markdown fence.
- Every proposal must be traceable to the material below. Going one step beyond what it literally says is the point — but never introduce a subject with no root in it.
+- The material is raw activity and some of it is noise: a scratch file, a one-word search, a conversation that went nowhere. Skip those and build on the traces that carry a real subject. Three proposals from two good traces beat three that include one about "hello".
- Make the three differ: different subjects from the material, and different kinds of question (a distinction, a mechanism, a why, a boundary case).
- No greetings, no emoji, no quotes around the fields' text."""
@@ -302,6 +333,7 @@ _SYSTEM_ZH = """你要提出三个"接下来值得探索什么"。每一个都
规则:
- 只回复一个 JSON 数组,正好 3 个这样的对象。不要有任何解释文字,不要 markdown 代码块。
- 每一个都必须能追溯到下面的素材。比素材字面上说的**多走一步**正是要点——但绝不能引入素材里毫无根据的话题。
+- 素材是原始活动记录,其中有噪音:随手建的文件、一个词的检索、没聊起来的对话。跳过它们,只在真正有内容的痕迹上做文章。宁可三个都从两条好素材里长出来,也不要有一个是关于 "hello" 的。
- 三个之间要有区别:取素材里不同的内容,也问不同类型的问题(一个区别、一个机制、一个为什么、一个边界情况)。
- 不要问候语、不要 emoji、字段文本里不要加引号。"""
@@ -357,9 +389,13 @@ def _sanitize(raw: str) -> tuple[Suggestion, ...]:
return tuple(items[:_COUNT]) if len(items) >= _COUNT else ()
-async def _generate(language: str) -> SuggestionSet:
- """Build a fresh set. Always returns one — empty on any failure."""
- topics = _collect_topics()
+async def _generate(language: str, topics: list[_Topic]) -> SuggestionSet:
+ """Build a fresh set from *topics*. Always returns one — empty on failure.
+
+ Takes the material rather than collecting it, so one pass reads the
+ surfaces once and every downstream decision — the fingerprint, whether an
+ empty result is meaningful — is made about the same snapshot.
+ """
fingerprint = _fingerprint(topics, language)
empty = SuggestionSet(
suggestions=(),
@@ -420,11 +456,26 @@ def _is_fresh(cached: SuggestionSet | None, language: str, *, now: float) -> boo
)
+async def _generate_and_cache(language: str, topics: list[_Topic]) -> SuggestionSet:
+ """Generate from *topics* and cache the result — unless it is a failure.
+
+ An empty result is only written when it is the *right* answer: there was no
+ material to work from, and saying so costs nothing to repeat. An empty
+ result produced *despite* having material means the call failed or the
+ model ignored the brief, and writing that would pin the failure in place
+ for a full TTL — the fingerprint would keep matching, so the background
+ pass would see nothing due and never retry. Those are dropped instead, and
+ the next visit tries again.
+ """
+ value = await _generate(language, topics)
+ if value.suggestions or not topics:
+ _save(value)
+ return value
+
+
async def refresh_suggestions(language: str = "en") -> SuggestionSet:
"""Generate a new set now and cache it. For the manual reroll."""
- value = await _generate(language)
- _save(value)
- return value
+ return await _generate_and_cache(language, _collect_topics())
async def _regenerate_if_due(language: str) -> None:
@@ -440,16 +491,24 @@ async def _regenerate_if_due(language: str) -> None:
cached = _load()
if _is_fresh(cached, language, now=time.time()) and cached.fingerprint == fingerprint:
return
- await refresh_suggestions(language)
+ await _generate_and_cache(language, topics)
def _schedule_probe(language: str) -> None:
- """Check (and maybe regenerate) in the background, throttled and deduped."""
+ """Check (and maybe regenerate) in the background, throttled and deduped.
+
+ The throttle is keyed by scope *and* language. A cached set in one language
+ is not an answer for another, so a learner whose UI is Chinese must not be
+ held behind a throttle stamped by an English probe — that combination
+ leaves them looking at an empty slot until the interval expires, with a
+ perfectly fresh cache sitting there in the wrong language.
+ """
try:
loop = asyncio.get_running_loop()
except RuntimeError:
return
- key = _scope_key()
+ scope = _scope_key()
+ key = f"{scope}\0{language}"
now = time.monotonic()
pending = _inflight.get(key)
if pending is not None and not pending.done():
diff --git a/tests/services/test_suggestions.py b/tests/services/test_suggestions.py
index 4ccbdb901..e183d8b67 100644
--- a/tests/services/test_suggestions.py
+++ b/tests/services/test_suggestions.py
@@ -323,6 +323,30 @@ async def test_probe_is_throttled_across_a_burst_of_loads(
assert len(scheduled) == 1
+async def test_the_throttle_does_not_swallow_a_language_switch(
+ monkeypatch: pytest.MonkeyPatch, no_material
+) -> None:
+ """A cached set in one language is not an answer for another.
+
+ The UI can ask in English before i18n settles and in Chinese immediately
+ after. If the second ask is throttled behind the first, the Chinese user
+ stares at an empty slot while a perfectly fresh English set sits in the
+ cache, unusable — for the whole throttle interval.
+ """
+ scheduled: list[str] = []
+ monkeypatch.setattr(
+ suggestions, "_regenerate_if_due", lambda language: _noop(scheduled, language)
+ )
+
+ await suggestions.get_suggestions("en")
+ await suggestions.get_suggestions("zh")
+ await suggestions.get_suggestions("zh") # this one IS throttled
+ await asyncio.gather(*suggestions._inflight.values())
+
+ assert scheduled == ["en", "zh"]
+
+
async def _noop(sink: list[str], language: str) -> None:
sink.append(language)
@@ -375,6 +399,45 @@ async def test_a_failing_model_leaves_an_empty_set_not_an_exception(
assert result.suggestions == ()
+async def test_a_failed_generation_is_not_cached_over_the_material(
+ monkeypatch: pytest.MonkeyPatch, isolated_scope: Path
+) -> None:
+ """Caching a failure would pin it in place for a whole TTL.
+
+ The fingerprint of a failed run matches the material it failed on, so the
+ background pass would then see "fresh, unchanged" and never retry — the
+ learner gets an empty slot for six hours because one call timed out.
+ """
+ import deeptutor.services.llm as llm
+
+ _stub_material(monkeypatch, [_hit("chat", "Chain rule")])
+
+ async def _boom(**_):
+ raise RuntimeError("provider timed out")
+
+ monkeypatch.setattr(llm, "complete", _boom)
+ await suggestions.refresh_suggestions("en")
+
+ assert not (isolated_scope / "suggestions" / "starters.json").exists()
+
+ # And the retry lands, rather than being skipped as "nothing due".
+ calls = _stub_llm(monkeypatch, _THREE)
+ await suggestions._regenerate_if_due("en")
+ assert len(calls) == 1
+ assert len((await suggestions.get_suggestions("en"))["suggestions"]) == 3
+
+
+async def test_an_empty_result_with_no_material_is_cached(
+ monkeypatch: pytest.MonkeyPatch, isolated_scope: Path, no_material
+) -> None:
+ """Empty is the right answer here, and repeating it should stay free."""
+ await suggestions.refresh_suggestions("en")
+
+ assert (isolated_scope / "suggestions" / "starters.json").exists()
+
+
@pytest.mark.asyncio
async def test_unchanged_material_inside_the_ttl_skips_the_model(
monkeypatch: pytest.MonkeyPatch,
diff --git a/web/components/chat/home/StarterSuggestions.tsx b/web/components/chat/home/StarterSuggestions.tsx
index 8510f25ba..54adb4dd5 100644
--- a/web/components/chat/home/StarterSuggestions.tsx
+++ b/web/components/chat/home/StarterSuggestions.tsx
@@ -19,13 +19,17 @@ interface SuggestionPayload {
}
/**
- * How long to wait before re-reading a set the backend said was stale.
+ * How long to wait before re-reading a set the backend said was stale, and how
+ * many times.
*
- * The backend answers instantly with the previous set and regenerates behind
- * the request, so a fresh one exists a moment later. One delayed re-read picks
- * it up without polling.
+ * The backend answers instantly and regenerates behind the request, so the
+ * fresh set exists a moment later — but "a moment" is a model call, which on a
+ * cold provider is comfortably longer than one interval. A few spaced re-reads
+ * cover that without turning into a poll; they stop as soon as something
+ * arrives, and the first visit of a session is the only time they all run.
*/
const RESETTLE_MS = 3500;
+const RESETTLE_ATTEMPTS = 4;
/**
* The three things worth exploring next, under the home composer.
@@ -56,7 +60,10 @@ export default function StarterSuggestions({
onPick: (prompt: string) => void;
disabled?: boolean;
}) {
- const { t, i18n } = useTranslation();
+ const { t, i18n, ready } = useTranslation();
+ // Asking before i18n settles resolves to "en" for everyone, and the backend
+ // caches per language — so one early request makes an English set that a
+ // Chinese UI can then never use. Wait for the real answer.
const language = i18n.language?.toLowerCase().startsWith("zh") ? "zh" : "en";
// Everything rendered here is generated, already in the learner's language,
// and never goes through the i18n table — a label that happened to match a
@@ -82,6 +89,7 @@ export default function StarterSuggestions({
);
useEffect(() => {
+ if (!ready) return;
const controller = new AbortController();
void (async () => {
const payload = await load(controller.signal);
@@ -90,18 +98,27 @@ export default function StarterSuggestions({
if (!payload.stale) return;
// A fresher set is being generated — which is also the ordinary first
// visit, where memory has material but nothing has been generated from
- // it yet. Collect it once rather than poll.
- timerRef.current = setTimeout(() => {
+ // it yet. Collect it as it lands rather than poll: the first look is
+ // early enough for a cache hit, the later ones cover a slow model.
+ let attempt = 0;
+ const collect = () => {
void load(controller.signal).then((next) => {
- if (next?.suggestions.length) setItems(next.suggestions);
+ if (next?.suggestions.length) {
+ setItems(next.suggestions);
+ return;
+ }
+ if (++attempt < RESETTLE_ATTEMPTS && !controller.signal.aborted) {
+ timerRef.current = setTimeout(collect, RESETTLE_MS);
+ }
});
- }, RESETTLE_MS);
+ };
+ timerRef.current = setTimeout(collect, RESETTLE_MS);
})();
return () => {
controller.abort();
if (timerRef.current) clearTimeout(timerRef.current);
};
- }, [load]);
+ }, [load, ready]);
const reroll = useCallback(async () => {
setRefreshing(true);