DeepTutor icon indicating copy to clipboard operation
DeepTutor copied to clipboard

[Bug]: "Mark as seen" on the KB-drift banner can never clear — refresh-fingerprints bumps book.revision before rejecting, so the force retry always 409s

Open TecFancy opened this issue 3 weeks ago • 2 comments

Do you need to file an issue?

  • [x] I have searched the existing issues and this bug is not already filed.
  • [x] I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

On a book whose knowledge bases changed after compilation, the reader shows the KB-drift banner ("自本书生成后,你的知识库发生了变化 / N compiled pages may be out of date") with Mark as seen (标记为已查看). That button can never succeed: every click ends in 409 Conflict, and each failed click makes the next attempt more likely to fail, because the rejected call still advances book.revision — the value the client holds in expected_revision.

Because the failure is not side-effect free, reloading does not converge either: the failed attempt bumps the revision, the reloaded page shows the new one, the next attempt bumps it again, forever.

Version: 1.7.1 (PyPI, self-hosted). The same code path is on dev today (line references below), so this is not fixed yet.

Steps to reproduce

  1. Have a book with at least one knowledge base whose fingerprints were recorded at compile time.
  2. Change a document in one of those KBs (e.g. re-upload a doc), so GET /api/books/{id}/health reports has_drift: true and N stale pages.
  3. Open a chapter page — the drift banner appears.
  4. Click Mark as seen (force=false). Server answers 409 ("Cannot mark KB drift as seen before these pages are recompiled").
  5. The banner now offers the force override; click it. Server answers 409 book_revision_conflict — "This book changed since your last action".
  6. Reload the page and repeat. The revision moved by one during the failed attempt, so the next click 409s again.

One real attempt sequence from the server log (nothing was edited between these calls):

POST /api/books/bk_…/refresh-fingerprints?force=false&expected_revision=573 → 409 Conflict
POST /api/books/bk_…/refresh-fingerprints?force=true &expected_revision=573 → 409 Conflict
POST /api/books/bk_…/refresh-fingerprints?force=false&expected_revision=574 → 409 Conflict
POST /api/books/bk_…/refresh-fingerprints?force=true &expected_revision=574 → 409
# meanwhile book.revision: 573 → 574 → 575, with no content written

Expected Behavior

  • Mark as seen (with force=true) succeeds — dismissing drift only rewrites the fingerprint baseline, it cannot overwrite anyone's prose; and/or
  • a rejected attempt leaves book.revision untouched, so the client's expected_revision stays valid and the force retry works.

What actually happens: a failed request mutates canonical state, and the client's retry is guaranteed to lose the revision race.

Root cause

Backend — deeptutor/api/routers/book.py, refresh_fingerprints (~L1316–1343 on dev):

resolved = _resolve_book_or_404(book_id, edit=True)
engine = resolved.engine
revision = _claim_content_mutation(          # ← bumps and persists book.revision (~L358)
    resolved,
    book_id,
    expected_revision,
    "refresh_fingerprints",
    extra={"force": force},
)
try:
    result = engine.refresh_kb_fingerprints(book_id, force=force)
except ValueError as exc:                     # ← "still stale" refusal, kb_health.py (~L358)
    raise HTTPException(status_code=409, detail=str(exc)) from exc

The revision claim happens before refresh_book_fingerprints checks whether the flagged pages have been recompiled (deeptutor/book/kb_health.py, ~L324–360). So the intentionally safe 409 is not side-effect free.

Frontend — web/app/(workspace)/learning/books/components/BookHealthBanner.tsx, acknowledge (~L161–186): it sends the expectedRevision prop and, on failure, offers the force retry without refreshing it:

} catch (err) {
  setAcknowledgeError(...);
  if (!force) setCanForce(true);  // retry resends the same, now-stale expectedRevision
}

Suggested fix

Any one of these breaks the loop; (1) is what I'd send as a PR:

  1. Check before claiming (or roll back on failure): run engine.refresh_kb_fingerprints(...) / validate stale_page_ids first, and only then claim the revision; or restore book.revision when the engine raises.
  2. Skip the strict content-mutation guard for dismissal: _claim_content_mutation(..., strict=False). Dismissing drift is not a content mutation, which is the same reasoning already used for generation commands (compile-page / pause / resume) in that function's docstring.
  3. Frontend: on 409, read detail.current_revision and call onRevisionChange(...) (or refetch the book) before offering the retry.

I'm currently running (2) as a local patch, which is the only way I've been able to clear the banner. Happy to open a PR against dev with (1) + a regression test for "failed refresh does not bump book.revision" if that's useful.

Related Module

Book

Configuration Used

Self-hosted. deeptutor 1.7.1 from PyPI (deeptutor[full]), file-backed book storage (data/user/workspace/book/book_<id>/), 6 KBs attached to the book, backend deeptutor serve on 127.0.0.1:8001, frontend from the packaged standalone Next.js build.

Logs and screenshots

The four 409 lines under Steps to reproduce are the whole story. I can attach deeper logs (uvicorn, log.md entries, manifest dumps) if that helps.

Additional Information

  • DeepTutor Version: 1.7.1 (PyPI); same logic present on dev as of 2026-11-09
  • Operating System: Ubuntu 24.04
  • Python Version: 3.12.3
  • Node.js Version: 24.19.0
  • Browser (if applicable): Chrome
  • Related Issues: searched "mark as seen" / "refresh-fingerprints" / "stale pages" — no existing report

TecFancy avatar Nov 09 '26 15:11 TecFancy

Independently reproduced on freshly fetched dev@60fb3bdf, using the real HTTP router, BookEngine, file-backed book storage and a temporary KB. I changed a real source file after taking its fingerprint, then let the normal drift detector flag its compiled page; no model call or mocked refusal was needed.

Sequence, with the client holding revision 573 dev #2578 at b04ac1bbc
force=false while the page is stale 409; persisted revision 574 409; revision remains 573
force=true, same expected_revision=573 409 book_revision_conflict; stale page remains 200; revision 574, stale list cleared, fingerprint updated

Two controls also pass on both versions: an actually stale expected_revision=572 is rejected without changing the manifest, and a page recompiled after drift allows ordinary non-forced refresh with one revision increment. The PR's submitted regression fails on dev (revision 2 != 1) and passes on its head; the refresh/drift/empty-source/shared-access test files pass together (18 passed).

The distinction in suggested fix (1) matters: run a read-only refusal check before claiming, as #2578 does. Calling the actual refresh_kb_fingerprints() before checking the client's revision would already persist the new fingerprints and clear stale markers, even if the revision check subsequently rejects the request. The stale-revision control above is worth keeping.

This verifies the sequential API/persistence failure and proposed repair; I did not exercise browser interaction or concurrent drift arriving between preflight and refresh. #2578 currently targets main; please retarget it to dev per the contribution guide.

Taz33m avatar Nov 10 '26 20:11 Taz33m

Verified fixed on the current local dev branch (a768f739a208), with regression checks.

will be fixed in the next releases!

pancacake avatar Nov 14 '26 16:11 pancacake