Skip to content

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

fix(book): rescue PLANNING pages too; contain storage I/O in the worker loop

Bingxi Zhao (Frank)294849d1e57e
diff --git a/deeptutor/book/engine.py b/deeptutor/book/engine.py
index 44853c1a2..ebab3c44f 100644
--- a/deeptutor/book/engine.py
+++ b/deeptutor/book/engine.py
@@ -801,17 +801,18 @@ class BookEngine:
return runtime
def _mark_page_error(self, page: Page | None, exc: Exception, *, prefix: str) -> None:
- """Flip a page stranded at ``GENERATING`` to ``ERROR``.
+ """Flip a page stranded in an in-flight state to ``ERROR``.
- The compiler sets ``page.status = GENERATING`` before running block
- generators; when one of them throws, nothing resets the status, so
- without this the page spins forever and is neither retried on resume
- nor force-regenerable in the UI. Saving is best-effort — this runs
+ The compiler persists ``page.status = PLANNING`` before the LLM
+ planning call and ``GENERATING`` before running block generators;
+ when either throws, nothing resets the status, so without this the
+ page spins forever and is neither retried on resume nor
+ force-regenerable in the UI. Saving is best-effort — this runs
inside exception handlers (including the background worker loop,
which must survive), so a failing save must not mask the original
error or kill the worker.
"""
- if page is None or page.status != PageStatus.GENERATING:
+ if page is None or page.status not in (PageStatus.GENERATING, PageStatus.PLANNING):
return
page.status = PageStatus.ERROR
page.error = f"{prefix}: {exc}"
@@ -875,17 +876,25 @@ class BookEngine:
f"Background compilation failed for {book_id}/{page_id}: {exc}",
exc_info=True,
)
- self.storage.append_log(
- book_id,
- f"background compile failed for page {page_id}: {exc}",
- op="compile_error",
- )
# Reset page status so it can be retried on next resume.
self._mark_page_error(page, exc, prefix="Background compile failed")
+ try:
+ self.storage.append_log(
+ book_id,
+ f"background compile failed for page {page_id}: {exc}",
+ op="compile_error",
+ )
+ except Exception:
+ logger.warning(
+ f"Failed to append compile-error log for {book_id}", exc_info=True
+ )
finally:
runtime.queued.discard(page_id)
- await self._maybe_finalize_book(book_id)
+ try:
+ await self._maybe_finalize_book(book_id)
+ except Exception:
+ logger.warning(f"finalize check failed for {book_id}", exc_info=True)
async def _maybe_finalize_book(self, book_id: str) -> None:
book = self.storage.load_book(book_id)
diff --git a/tests/book/test_engine_controls.py b/tests/book/test_engine_controls.py
index 629ee65a3..b1cc6996f 100644
--- a/tests/book/test_engine_controls.py
+++ b/tests/book/test_engine_controls.py
@@ -61,6 +61,18 @@ def test_mark_page_error_resets_generating_page() -> None:
assert storage.saved == [page]
+def test_mark_page_error_resets_planning_page() -> None:
+ storage = _RecordingStorage()
+ engine = _engine_with_storage(storage)
+ page = Page(status=PageStatus.PLANNING)
+
+ engine._mark_page_error(page, RuntimeError("planner crashed"), prefix="Compilation failed")
+
+ assert page.status == PageStatus.ERROR
+ assert "planner crashed" in page.error
+ assert storage.saved == [page]
+
+
def test_mark_page_error_ignores_missing_or_settled_pages() -> None:
storage = _RecordingStorage()
engine = _engine_with_storage(storage)