Files
DeepTutor/deeptutor/learning/models.py
T
Bingxi Zhao (Frank) bd80a4d28a release: v1.5.17
Partners stop being a deployment-wide admin resource and become
per-user: three nested rights (manage / use / neither) replace the
blanket admin gate on the router, each member can build and own their
own, and every person's conversations with a partner stay private to
their account. Grants keep working and now offer only admin-managed
partners. Channel runtime state moves from one directory per channel to
one per partner — copied, not moved, and only when its owner is
unambiguous — so two partners can finally run the same channel.

Around that: a knowledge base can track GitHub repositories and re-sync
their Markdown (#903); Antigravity CLI joins My Agents now that Gemini
CLI is retired (#828); personal WeChat signs in from a QR code drawn in
the browser instead of the server log (#951); a chat account can be
linked to a DeepTutor account with /link; containers re-apply the extras
they declare instead of losing them at compose down (#762); and
`deeptutor doctor` answers whether a workspace is ready to run (#959).

Reliability, largely from community PRs: embeddings survive a
rate-limited provider through key rotation, a retry budget and a global
throttle (#962); MinerU parsing and code execution work on Windows
(#844, #964); indexing fails bounded instead of hanging (#946); MinerU
page-layout blocks stop being indexed as content (#949); voice uploads
carry a MIME the transcription API accepts (#945); and reasoning effort
can be overridden per conversation (#641).

Release plumbing: notes in assets/releases/ver1-5-17.md, v1.5.13
archived, __version__ 1.5.17, README Releases row and all eleven
translations synced.

Two defects found by the release gate and fixed here:

- Six partner routes — the four channel-onboarding endpoints and both
  WeChat QR endpoints — kept the _auth-only mount they had inherited
  from the blanket admin gate, so any signed-in account could start a
  QR login for someone else's partner and write a bot token into its
  channel config. All six now declare _MANAGEABLE, and a sweep test
  asserts no {partner_id} route is left ungated.
- A configured api_key became `str | list[str]` when key rotation
  landed, but ten call sites pass it somewhere that takes one key; a
  list would have rendered into an Authorization header verbatim.
  keypool.primary_api_key reduces at each boundary.

Gates: pre-commit (all 15 hooks), ruff check + format, pytest
5276 passed, web 605 node tests, docs build.
2026-08-25 02:24:35 +08:00

307 lines
9.2 KiB
Python

from __future__ import annotations
from enum import Enum
import time
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
_KNOWLEDGE_TYPE_LEGACY: dict[str, str] = {
"记忆型": "memory",
"概念型": "concept",
"程序型": "procedure",
"设计型": "design",
}
_ERROR_TYPE_LEGACY: dict[str, str] = {
"知识结构性": "structural",
"理解偏差型": "deviation",
"应用错误": "application",
"元认知型": "metacognitive",
}
class KnowledgeType(str, Enum):
MEMORY = "memory"
CONCEPT = "concept"
PROCEDURE = "procedure"
DESIGN = "design"
@classmethod
def _missing_(cls, value: object) -> KnowledgeType | None:
mapped = _KNOWLEDGE_TYPE_LEGACY.get(str(value))
return cls(mapped) if mapped else None
class ErrorType(str, Enum):
KNOWLEDGE_STRUCTURAL = "structural"
UNDERSTANDING_DEVIATION = "deviation"
APPLICATION_ERROR = "application"
METACOGNITIVE = "metacognitive"
@classmethod
def _missing_(cls, value: object) -> ErrorType | None:
mapped = _ERROR_TYPE_LEGACY.get(str(value))
return cls(mapped) if mapped else None
# Stages removed in the Mastery Path simplification are mapped onto the nearest
# surviving stage so progress persisted by the older engine still deserializes.
_STAGE_LEGACY: dict[str, str] = {
"diagnostic_phase1": "diagnostic",
"diagnostic_phase2": "diagnostic",
"metacognitive_intro": "explain",
"plan": "explain",
"pretest": "explain",
"practice_quiz": "practice",
"module_test": "review",
}
class LearningStage(str, Enum):
"""The Mastery Path loop: diagnose once, then per knowledge point teach and
check understanding, then practice the module, diagnose errors, and schedule
spaced review."""
DIAGNOSTIC = "diagnostic"
EXPLAIN = "explain"
FEYNMAN_CHECK = "feynman_check"
PRACTICE = "practice"
ERROR_DIAGNOSIS = "error_diagnosis"
REVIEW = "review"
COMPLETED = "completed"
@classmethod
def _missing_(cls, value: object) -> LearningStage | None:
mapped = _STAGE_LEGACY.get(str(value))
return cls(mapped) if mapped else None
class KnowledgePoint(BaseModel):
model_config = ConfigDict(extra="ignore")
id: str
name: str
type: KnowledgeType
module_id: str
class LearningModule(BaseModel):
model_config = ConfigDict(extra="ignore")
id: str
name: str
order: int
pass_threshold: float = 0.7
knowledge_points: list[KnowledgePoint] = Field(default_factory=list)
class DiagnosticResult(BaseModel):
model_config = ConfigDict(extra="ignore")
total_questions: int = 0
correct_count: int = 0
module_mastery: dict[str, float] = Field(default_factory=dict)
class QuizAttempt(BaseModel):
model_config = ConfigDict(extra="ignore")
question_id: str
knowledge_point_id: str
module_id: str = ""
is_correct: bool
user_answer: Any = None
error_type: ErrorType | None = None
self_attribution: str = ""
mastery_estimate: float = 0.0
timestamp: float = Field(default_factory=time.time)
class RetryAttempt(BaseModel):
model_config = ConfigDict(extra="ignore")
timestamp: float
is_correct: bool
attempt_number: int
class ErrorRecord(BaseModel):
model_config = ConfigDict(extra="ignore")
id: str
question_id: str
knowledge_point_id: str
module_id: str
error_type: ErrorType
self_attribution: str = ""
ai_confirmation: str = ""
retry_history: list[RetryAttempt] = Field(default_factory=list)
status: Literal["active", "retrying", "review", "graduated"] = "active"
created_at: float = Field(default_factory=time.time)
class RepetitionState(BaseModel):
model_config = ConfigDict(extra="ignore")
interval_index: int = 0
consecutive_correct: int = 0
consecutive_wrong: int = 0
next_review_at: float
class ReviewTask(BaseModel):
model_config = ConfigDict(extra="ignore")
id: str
knowledge_point_id: str
knowledge_type: KnowledgeType
due_at: float
priority: int
state: RepetitionState
class PendingQuestion(BaseModel):
"""A question posed to the learner and awaiting their answer.
Persisted so grading is deterministic across turns: the expected answer
lives here server-side and never round-trips through the model. The tutor
poses a question with ``mastery_quiz`` (storing this), the learner answers
on a later turn, and ``mastery_grade`` scores the stored answer.
"""
model_config = ConfigDict(extra="ignore")
question_id: str
knowledge_point_id: str
module_id: str = ""
prompt: str = ""
question_type: str = "short"
expected_answer: str = ""
options: list[str] = Field(default_factory=list)
# Reference explanation and difficulty, captured when the question is
# posed. Server-side like ``expected_answer`` — ``public_pending_question``
# never projects them, so an explanation cannot leak the answer into the
# card the learner is about to answer. They travel with the attempt into
# the question bank, which is what makes a mastery mistake reviewable
# later instead of a bare right/wrong.
explanation: str = ""
difficulty: str = ""
created_at: float = Field(default_factory=time.time)
class InteractionStatus(str, Enum):
"""Durable lifecycle for one learner-facing mastery interaction.
The chat runtime may disappear at any point; this state is the source of
truth for whether a question still needs an answer or has already been
graded. Terminal interactions are retained for idempotent retries and
audit history.
"""
REGISTERED = "registered"
AWAITING_INPUT = "awaiting_input"
ANSWERED = "answered"
GRADED = "graded"
ABANDONED = "abandoned"
class MasteryInteraction(BaseModel):
"""A persisted question/answer transaction for a mastery path."""
model_config = ConfigDict(extra="ignore")
interaction_id: str
path_id: str
question: PendingQuestion
status: InteractionStatus = InteractionStatus.REGISTERED
session_id: str = ""
turn_id: str = ""
user_answer: str = ""
result: dict[str, Any] = Field(default_factory=dict)
created_at: float = Field(default_factory=time.time)
updated_at: float = Field(default_factory=time.time)
class MasteryEvent(BaseModel):
"""Committed path event consumed by recovery and future live UIs."""
model_config = ConfigDict(extra="ignore")
id: int = 0
path_id: str
revision: int
event_type: str
payload: dict[str, Any] = Field(default_factory=dict)
session_id: str = ""
turn_id: str = ""
created_at: float = Field(default_factory=time.time)
class MasteryPathLease(BaseModel):
"""The one active mutating turn allowed for a mastery path."""
model_config = ConfigDict(extra="ignore")
path_id: str
session_id: str
turn_id: str
acquired_at: float = Field(default_factory=time.time)
class LearningProgress(BaseModel):
model_config = ConfigDict(extra="ignore")
book_id: str
# The learner-facing name of this path. Empty means "never named": the
# display name is then derived (``policy.path_display_name``), which is how
# every path behaved before this field existed — so an aggregate persisted
# without it needs no migration.
name: str = ""
diagnostic: DiagnosticResult | None = None
modules: list[LearningModule] = Field(default_factory=list)
current_module_id: str = ""
current_stage: LearningStage = LearningStage.DIAGNOSTIC
current_kp_index: int = 0
mastery_levels: dict[str, float] = Field(default_factory=dict)
# Qualitative gate for CONCEPT / DESIGN knowledge points: True once the
# tutor judges the learner's explanation sufficient (``mastery_assess``).
# The quantitative ``mastery_levels`` gate covers MEMORY / PROCEDURE.
qualitative_mastery: dict[str, bool] = Field(default_factory=dict)
knowledge_types: dict[str, KnowledgeType] = Field(default_factory=dict)
quiz_attempts: list[QuizAttempt] = Field(default_factory=list)
error_records: list[ErrorRecord] = Field(default_factory=list)
repetition_states: dict[str, RepetitionState] = Field(default_factory=dict)
review_queue: list[ReviewTask] = Field(default_factory=list)
# A single outstanding question; grading reads its expected answer so the
# model never has to recall it across turns.
pending_question: PendingQuestion | None = None
feynman_retries: dict[str, int] = Field(default_factory=dict)
feynman_explanations: dict[str, str] = Field(default_factory=dict)
stage_failure_counts: dict[str, int] = Field(default_factory=dict)
stage_failure_notes: dict[str, str] = Field(default_factory=dict)
version: int = 0
created_at: float = Field(default_factory=time.time)
updated_at: float = Field(default_factory=time.time)
__all__ = [
"KnowledgeType",
"ErrorType",
"LearningStage",
"KnowledgePoint",
"LearningModule",
"DiagnosticResult",
"QuizAttempt",
"RetryAttempt",
"ErrorRecord",
"RepetitionState",
"ReviewTask",
"PendingQuestion",
"InteractionStatus",
"MasteryInteraction",
"MasteryEvent",
"MasteryPathLease",
"LearningProgress",
]