Skip to content

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

fix(learning): preserve latest assessment projections on retries (#1542)

Bingxi Zhao (Frank)924f8cd2274d
diff --git a/deeptutor/api/routers/book.py b/deeptutor/api/routers/book.py
index 0deca5eb4..da3bd8de8 100644
--- a/deeptutor/api/routers/book.py
+++ b/deeptutor/api/routers/book.py
@@ -184,15 +184,24 @@ def _focus_check_question(
def _verified_choice_grade(question: dict[str, Any], user_answer: str) -> bool | None:
"""A linked Book answer is trusted only when the stored key can grade it."""
- if str(question.get("question_type") or "").strip().lower() != "choice":
+ question_type = str(question.get("question_type") or "").strip().lower().replace(" ", "_")
+ if question_type not in {"choice", "multiple_choice", "multiple-choice", "mcq"}:
return None
options = question.get("options")
if not isinstance(options, dict):
return None
answer = user_answer.strip().upper()
- correct = str(question.get("correct_answer") or "").strip().upper()
- keys = {str(key).strip().upper() for key in options}
- if answer not in keys or correct not in keys:
+ keys = {str(key).strip().upper(): str(label).strip() for key, label in options.items()}
+ correct_text = str(question.get("correct_answer") or "").strip()
+ correct = correct_text.upper()
+ if correct not in keys:
+ matching = [
+ key for key, label in keys.items() if label.casefold() == correct_text.casefold()
+ ]
+ if len(matching) != 1:
+ return None
+ correct = matching[0]
+ if answer not in keys:
return None
return answer == correct
diff --git a/deeptutor/api/routers/reading_extensions.py b/deeptutor/api/routers/reading_extensions.py
index c178ae3da..1f84072c9 100644
--- a/deeptutor/api/routers/reading_extensions.py
+++ b/deeptutor/api/routers/reading_extensions.py
@@ -318,7 +318,8 @@ async def submit_quiz_answers(material_id: str, payload: QuizAnswersPayload) ->
correct_text = str(choices[correct_index]) if 0 <= correct_index < len(choices) else ""
submission_id = payload.submission_id.strip()
attempt_id = (
- f"reading:{material_id}:{payload.locator}:{item.question_id.strip()}:{submission_id}"
+ f"reading:{origin_type}:{origin_ref}:{turn_id}:"
+ f"{item.question_id.strip()}:{submission_id}"
if submission_id
else ""
)
diff --git a/deeptutor/learning/assessment.py b/deeptutor/learning/assessment.py
index ad11210e1..96725691b 100644
--- a/deeptutor/learning/assessment.py
+++ b/deeptutor/learning/assessment.py
@@ -432,6 +432,11 @@ async def record_assessment(record: AssessmentRecord) -> AssessmentOutcome:
store = get_sqlite_session_store()
existing_attempt = await store.get_assessment_attempt(attempt_id)
+ if existing_attempt is not None:
+ # An earlier submission may have had an untrusted mapping removed.
+ # Its immutable event owns the linkage on every retry.
+ item["mastery_path_id"] = str(existing_attempt.get("mastery_path_id") or "")
+ item["knowledge_point_id"] = str(existing_attempt.get("knowledge_point_id") or "")
if (
existing_attempt is None
and record.source != "mastery_path"
diff --git a/deeptutor/learning/tests/test_assessment.py b/deeptutor/learning/tests/test_assessment.py
index f562429b4..4d266dc12 100644
--- a/deeptutor/learning/tests/test_assessment.py
+++ b/deeptutor/learning/tests/test_assessment.py
@@ -249,6 +249,31 @@ def test_old_submission_retry_cannot_overwrite_newer_answer(store: SQLiteSession
assert len(attempts) == 2
+def test_late_arriving_older_attempt_does_not_rewind_question_card(
+ store: SQLiteSessionStore,
+) -> None:
+ asyncio.run(store.create_session(title="S", session_id="session-1"))
+ newer = _mastery_record(
+ attempt_id="newer",
+ user_answer="4",
+ result="correct",
+ is_correct=True,
+ created_at=200.0,
+ )
+ older = _mastery_record(
+ attempt_id="older", user_answer="3", result="incorrect", created_at=100.0
+ )
+ asyncio.run(record_assessment(newer))
+ outcome = asyncio.run(record_assessment(older))
+
+ latest = asyncio.run(store.find_notebook_entry("session-1", "q-1", turn_id="turn-1"))
+ attempts = asyncio.run(store.list_assessment_attempts("session-1", question_id="q-1"))
+ assert outcome.attempt_recorded is True
+ assert outcome.upserted is False
+ assert latest["result"] == "correct"
+ assert [item["attempt_id"] for item in attempts] == ["older", "newer"]
+
+
def test_submission_id_cannot_be_reused_for_different_answer(store: SQLiteSessionStore) -> None:
asyncio.run(store.create_session(title="S", session_id="session-1"))
asyncio.run(record_assessment(_mastery_record(attempt_id="same", user_answer="3")))
@@ -281,9 +306,11 @@ def test_unknown_cross_surface_objective_keeps_unlinked_attempt(store: SQLiteSes
attempt_id="unlinked",
)
outcome = asyncio.run(record_assessment(record))
+ retry = asyncio.run(record_assessment(record))
latest = asyncio.run(store.find_notebook_entry("session-1", "q-1", turn_id="turn-1"))
attempts = asyncio.run(store.list_assessment_attempts("session-1", question_id="q-1"))
assert "dropped_invalid_mastery_linkage" in outcome.diagnostics
+ assert retry.attempt_recorded is False
assert latest["mastery_path_id"] == ""
assert attempts[0]["mastery_path_id"] == ""
diff --git a/deeptutor/services/session/sqlite_store.py b/deeptutor/services/session/sqlite_store.py
index 655c29dd8..5cda472f7 100644
--- a/deeptutor/services/session/sqlite_store.py
+++ b/deeptutor/services/session/sqlite_store.py
@@ -3266,6 +3266,17 @@ class SQLiteSessionStore:
count = int(row["count"]) + 1
item = {**item, "attempt_count": count}
attempt = {**attempt, "attempt_count": count}
+ latest = conn.execute(
+ "SELECT occurred_at, attempt_id FROM assessment_attempts "
+ "WHERE origin_type = ? AND origin_ref = ? AND turn_id = ? AND question_id = ? "
+ "ORDER BY occurred_at DESC, attempt_id DESC LIMIT 1",
+ (origin_type, origin_ref, turn_id, question_id),
+ ).fetchone()
+ occurred_at = float(attempt.get("occurred_at") or time.time())
+ is_latest = latest is None or (occurred_at, attempt_id) >= (
+ float(latest["occurred_at"]),
+ str(latest["attempt_id"]),
+ )
conn.execute(
"""
INSERT INTO assessment_attempts (
@@ -3287,11 +3298,13 @@ class SQLiteSessionStore:
str(attempt.get("result") or "ungraded"),
str(attempt.get("mastery_path_id") or ""),
str(attempt.get("knowledge_point_id") or ""),
- float(attempt.get("occurred_at") or time.time()),
+ occurred_at,
_json_dumps(attempt),
),
)
- upserted = self._upsert_notebook_entries_in_conn(conn, session_id, [item])
+ upserted = (
+ self._upsert_notebook_entries_in_conn(conn, session_id, [item]) if is_latest else 0
+ )
row = conn.execute(
"SELECT id FROM notebook_entries WHERE origin_type = ? "
"AND origin_ref = ? AND turn_id = ? AND question_id = ?",
diff --git a/tests/api/test_book_quiz_attempt_notebook.py b/tests/api/test_book_quiz_attempt_notebook.py
index 95b5b8562..086156e80 100644
--- a/tests/api/test_book_quiz_attempt_notebook.py
+++ b/tests/api/test_book_quiz_attempt_notebook.py
@@ -4,6 +4,7 @@ import asyncio
from types import SimpleNamespace
from fastapi import FastAPI
+import pytest
from starlette.testclient import TestClient
from deeptutor.api.routers import book as book_router
@@ -61,6 +62,31 @@ class _FakeResolvedBook:
return self.learning.load_progress(book_id)
+ ("question_type", "correct_answer", "submitted", "expected"),
+ [
+ ("choice", "B", "B", True),
+ ("multiple_choice", "Two", "B", True),
+ ("mcq", "Two", "A", False),
+ ("written", "Two", "B", None),
+ ],
+)
+def test_book_linked_choice_grade_matches_supported_ui_answers(
+ question_type: str, correct_answer: str, submitted: str, expected: bool | None
+) -> None:
+ assert (
+ book_router._verified_choice_grade(
+ {
+ "question_type": question_type,
+ "options": {"A": "One", "B": "Two"},
+ "correct_answer": correct_answer,
+ },
+ submitted,
+ )
+ is expected
+ )
+
+
def test_quiz_attempt_syncs_focus_check_to_question_bank(tmp_path, monkeypatch) -> None:
store = SQLiteSessionStore(db_path=tmp_path / "sessions.db")
asyncio.run(store.create_session(session_id="page-chat-1", title="Page 1 chat"))
diff --git a/tests/api/test_reading_quiz_answers.py b/tests/api/test_reading_quiz_answers.py
index 1b8be8383..b07139329 100644
--- a/tests/api/test_reading_quiz_answers.py
+++ b/tests/api/test_reading_quiz_answers.py
@@ -226,6 +226,25 @@ def test_reading_submission_id_distinguishes_retry_from_new_attempt(
assert [item["attempt_count"] for item in attempts] == [1, 2]
+def test_reading_submission_id_is_scoped_to_session(store: SQLiteSessionStore) -> None:
+ for session_id in ("reader-a", "reader-b"):
+ asyncio.run(store.create_session(title=session_id, session_id=session_id))
+ asyncio.run(store.put_reading_quiz_pending(MATERIAL_ID, LOCATOR, _quiz_questions()))
+ with TestClient(_build_app(store)) as client:
+ for session_id in ("reader-a", "reader-b"):
+ response = client.post(
+ f"/api/reading/materials/{MATERIAL_ID}/extensions/quiz/answers",
+ json=_payload(
+ session_id=session_id,
+ answers=_answers(("q_1", 1)),
+ submission_id="same-browser-id",
+ ),
+ )
+ assert response.status_code == 200
+ assert len(asyncio.run(store.list_assessment_attempts("reader-a"))) == 1
+ assert len(asyncio.run(store.list_assessment_attempts("reader-b"))) == 1
+
+
@pytest.mark.parametrize("selected_index", [-1, 2])
def test_invalid_selection_does_not_save_any_part_of_batch(store, selected_index):
asyncio.run(store.put_reading_quiz_pending(MATERIAL_ID, LOCATOR, _quiz_questions()))
diff --git a/web/app/(workspace)/learning/books/BooksRoute.tsx b/web/app/(workspace)/learning/books/BooksRoute.tsx
index e3e1815a2..062d69178 100644
--- a/web/app/(workspace)/learning/books/BooksRoute.tsx
+++ b/web/app/(workspace)/learning/books/BooksRoute.tsx
@@ -881,10 +881,12 @@ function BookPageInner() {
is_correct: args.isCorrect,
submission_id: submissionId,
})
- pendingQuizSubmissions.current.delete(key)
- setDetail(current =>
- current && current.book.id === bookId ? { ...current, progress } : current
- )
+ if (pendingQuizSubmissions.current.get(key)?.id === submissionId) {
+ pendingQuizSubmissions.current.delete(key)
+ setDetail(current =>
+ current && current.book.id === bookId ? { ...current, progress } : current
+ )
+ }
})
/**
diff --git a/web/tests/reading-quiz-persistence.spec.tsx b/web/tests/reading-quiz-persistence.spec.tsx
index dd3dc7869..5e386fccc 100644
--- a/web/tests/reading-quiz-persistence.spec.tsx
+++ b/web/tests/reading-quiz-persistence.spec.tsx
@@ -61,6 +61,7 @@ it("waits for saving, uses the server verdict, and preserves the original locato
expect(submitReadingQuizAnswers).toHaveBeenCalledWith("material-1", {
locator: 4,
session_id: "",
+ submission_id: expect.any(String),
answers: [{ question_id: "quiz-1", selected_index: 1 }],
});
await act(async () => {
@@ -81,4 +82,13 @@ it("keeps a failed answer retryable without showing a successful grade", async (
expect(screen.queryByText("Correct")).not.toBeInTheDocument();
expect(answer).toHaveAttribute("aria-pressed", "false");
expect(answer).not.toBeDisabled();
+ const firstSubmission = vi.mocked(submitReadingQuizAnswers).mock.calls[0][1].submission_id;
+ vi.mocked(submitReadingQuizAnswers).mockResolvedValueOnce([
+ { question_id: "quiz-1", is_correct: true, result: "correct" },
+ ]);
+ fireEvent.click(answer);
+ await waitFor(() => expect(submitReadingQuizAnswers).toHaveBeenCalledTimes(2));
+ expect(vi.mocked(submitReadingQuizAnswers).mock.calls[1][1].submission_id).toBe(
+ firstSubmission,
+ );
});