Files
DeepTutor/deeptutor/book/progress.py
Bingxi Zhao (Frank) 96d6df9eb1 release: v1.5.13
Book is the bulk of this one. Compilation outlives the request that
started it, so a bus owned by that request dropped every background event
and "watch your book being written" degraded into a frozen page; the
stream is now owned per book by the process and survives a refresh or a
reconnect. Reading progress is finally recorded — `visited_page_ids` and
`bookmarked_page_ids` had been declared but never written for the life of
the feature — and rolls up into a completion score and weak chapters. A
book can also leave the app now, as one self-contained Markdown document,
with non-ASCII titles surviving the download.

Two scheduling defects that were invisible in production are fixed: the
same page compiled twice concurrently (billed twice, one result
discarded), and a book grinding on through a provider outage until every
chapter was half-generated. The source sweep is bounded and allocated
round-robin rather than spent on the first few knowledge bases, and
retrieval now tells indexed bases from connected pointers, so an Obsidian
vault or a remote IMA library no longer contributes zero while looking
like a source with nothing relevant. Confirming a spine shows a live
per-chapter estimate derived from the Section Architect's own templates.

Also here: three home starter suggestions built from memory recall,
mastery-path handoff moved into one module that changes lease, session
preference, and live turn together, and account-level Tencent IMA
credentials with the per-KB pair still winning.

Notes: assets/releases/ver1-5-13.md

This commit also carries the README prose sync, the eleven translated
READMEs, the release-notes archive sweep, and the version bump. Two gate
fixes ride along: the `Stamped` protocol is declared read-only so the
frozen `EntityStamp` satisfies it, and six GraphRAG adapter tests that
reached a deep `graphrag` import without guarding it now skip like their
four neighbours instead of failing where the extra is not installed.
2026-08-17 11:22:26 +08:00

137 lines
4.4 KiB
Python

"""
Reading progress
================
Where the reader is in a book, what they have bookmarked, and how they did on
its quizzes.
Pure functions over a :class:`Progress` value — no storage, no engine, no IO.
Two reasons:
- The engine stays a scheduler. Progress is a different concern with different
rules, and mixing them is what left ``visited_page_ids`` and
``bookmarked_page_ids`` declared but never written for the whole life of the
feature.
- Immersive reading tracks the same three things (position, bookmarks,
comprehension checks) in its own silo. When those merge, this is the layer
that generalises, and it can be tested without a filesystem.
Derived, not accumulated
------------------------
``score`` and ``weak_chapters`` are recomputed from the **latest** attempt per
question rather than incremented as attempts arrive. Accumulating meant
re-answering a question you already knew inflated your score, and a chapter
stayed "weak" forever once you had missed anything in it — a progress display
that only ever ratchets one way tells the reader nothing.
"""
from __future__ import annotations
from .models import Progress, QuizAttempt
def _question_key(attempt: QuizAttempt) -> tuple[str, str]:
return (attempt.block_id, attempt.question_id)
def latest_attempts(progress: Progress) -> dict[tuple[str, str], QuizAttempt]:
"""The most recent attempt per question, keyed by (block, question)."""
latest: dict[tuple[str, str], QuizAttempt] = {}
for attempt in progress.quiz_attempts:
key = _question_key(attempt)
current = latest.get(key)
if current is None or attempt.timestamp >= current.timestamp:
latest[key] = attempt
return latest
def recompute_score(progress: Progress) -> int:
"""Number of distinct questions currently answered correctly."""
return sum(1 for a in latest_attempts(progress).values() if a.is_correct is True)
def recompute_weak_chapters(progress: Progress, page_to_chapter: dict[str, str]) -> list[str]:
"""Chapters holding at least one question whose latest answer was wrong.
A chapter drops off this list as soon as the reader gets its questions
right — being wrong once should not brand a chapter permanently.
"""
weak: set[str] = set()
for attempt in latest_attempts(progress).values():
if attempt.is_correct is not False:
continue
chapter_id = page_to_chapter.get(attempt.page_id, "")
if chapter_id:
weak.add(chapter_id)
return sorted(weak)
def record_attempt(
progress: Progress,
*,
page_id: str,
block_id: str,
question_id: str,
user_answer: str,
is_correct: bool | None,
page_to_chapter: dict[str, str],
) -> Progress:
"""Append an attempt and refresh everything derived from the set."""
progress.quiz_attempts.append(
QuizAttempt(
block_id=block_id,
page_id=page_id,
question_id=question_id,
user_answer=user_answer,
is_correct=is_correct,
)
)
progress.score = recompute_score(progress)
progress.weak_chapters = recompute_weak_chapters(progress, page_to_chapter)
return progress
def mark_visited(progress: Progress, page_id: str) -> bool:
"""Record that the reader opened *page_id*. True when anything changed.
The caller uses the return value to skip a disk write on the common case:
re-reading a page you have already seen.
"""
changed = False
if progress.current_page_id != page_id:
progress.current_page_id = page_id
changed = True
if page_id not in progress.visited_page_ids:
progress.visited_page_ids.append(page_id)
changed = True
return changed
def toggle_bookmark(progress: Progress, page_id: str) -> bool:
"""Flip *page_id*'s bookmark. Returns whether it is bookmarked now."""
if page_id in progress.bookmarked_page_ids:
progress.bookmarked_page_ids.remove(page_id)
return False
progress.bookmarked_page_ids.append(page_id)
return True
def completion_ratio(progress: Progress, total_pages: int) -> float:
"""Fraction of the book visited, clamped to [0, 1]."""
if total_pages <= 0:
return 0.0
seen = len({p for p in progress.visited_page_ids})
return min(1.0, seen / total_pages)
__all__ = [
"latest_attempts",
"recompute_score",
"recompute_weak_chapters",
"record_attempt",
"mark_visited",
"toggle_bookmark",
"completion_ratio",
]