ADR-144 — Documents provided: a course library of PDF and Markdown files, read by the students of an evaluation¶
Status¶
Accepted (2026-11-24, product owner in conversation for the product rules; the orchestrator of the feature for the technical ones, after a spec challenge and an architect's plan). Delivered in three pull requests: the library (PR1), a viewer and the staff preview (PR2), the documents an evaluation provides (PR3). Check the code for what has shipped.
Scope: the library module (apps/api/src/modules/library/), the table
course_documents and the column courses.documents_quota_bytes (migration
0159_course_documents), the store ASSETS_DIR/documents/, the course
page's Documents tab; from PR2 the viewer; from PR3 an evaluation's provided
documents (evaluation_provided_documents, migration 0160) and the
player's panel. F-ORG-18, F-ADMIN-10, N-SEC-24, F-EVAL-37, F-LIVE-15.
Relations: amends ADR-003 §1 and
ADR-110 §1–2 (scoped
amendments below); extends the provided tools of an evaluation, after the
calculator (ADR-069) and the notepad
(ADR-090); reads ADR-088 (the integrity
journal), ADR-068 (the roles of a course) and ADR-117 (an account is
pseudonymized, never deleted). PR3 amends invariant 4 of CLAUDE.md.
Context¶
Under Safe Exam Browser or on a kiosk station a student opens nothing but
Quiz. A teacher who wants a datasheet, an extract of a standard or a
formula sheet on the desk of an exam has paper as the only way. Quiz already
holds files: the images of the pools (assets, readable by any teacher of
the instance through /app/api/assets/:id), the scans of paper copies
(ASSETS_DIR/paper/, ADR-125 §15) and the journal's assets. None of them is
a course's material, and the first is deliberately readable instance-wide.
The word document is taken in the code: apps/api/src/documents/,
EvaluationDocumentRecord and /evaluations/:id/document.* are the exports
of ADR-121. The module is therefore named
library, its types CourseDocument and CourseLibrary.
Decision¶
1. One library per course (product owner)¶
A course has one library, shown as a Documents tab of its page. Not
several vaults, not one per teacher. Any member of the course staff uploads
and renames. A document is deleted by its uploader or by an owner of the
course; anyone else gets 403 owner_required (ADR-068). There is no
"replace": upload the new file, delete the old one. The student's own
material (old labs, notes) is out of scope.
2. Quota per course (product owner)¶
A course's documents share a quota, 100 MB by default
(courses.documents_quota_bytes), raised by an administrator only, up to
10 GB, from the course's Documents tab: an administrator who reaches the
course (a seat or Super Powers) types it in; there is no administration list
of courses. The same file in two courses is stored once on disk and counts
in full in each course's quota. The check runs inside a transaction that
locks the course row FOR UPDATE and sums bytes: over the quota is
409 quota_exceeded { usedBytes, quotaBytes }. 413 stays the per-file
cap. Every size is in decimal units, as the interface shows them: 1 MB is
10^6 bytes.
3. Kinds and sizes, by the bytes (N-SEC-24)¶
A document is a PDF or a Markdown file, decided by its bytes, never by the
declared type: %PDF- is a PDF; otherwise valid UTF-8 without NUL, named
.md or .markdown and at most 500 kB, is Markdown; anything else 415
document_unsupported. A file is at most DOCUMENT_MAX_BYTES (default
50 MB, at most 100 MB); a Markdown file past 500 kB is 413 with that
limit in the body. No text is extracted from a PDF.
A Markdown document is rendered at upload by @quiz/docrender's
renderStandalone (renderPage with no page and no asset beside it): raw
HTML shown as text, a relative image becomes its alt text and a warning, an
external one is dropped with a warning; a document carries no image in v1.
The HTML, the table of contents, the warnings and the RENDERER_VERSION
are stored; a row rendered by an older version is rendered again, from
its file, by the library's daily task (library.blob_sweep), never by a
read. The staff list shows the warnings.
4. Its own store, streamed in, never unlinked by a request¶
The library never reuses the pool's assets table nor
/app/api/assets/:id. Its files live at
ASSETS_DIR/documents/<sha256[0:2]>/<sha256>, rebuilt from the hash,
through the file primitives of modules/pool/assets.ts. An upload is
streamed to a temporary file of the same file system while it is hashed,
then renamed into place: a 50 MB upload never sits whole in the memory of
the process that serves live exams. The same bytes uploaded twice in one
course answer 200 with the existing document; the reply never depends on
whether another course holds the same bytes, so an upload learns nothing
of other courses.
A request never unlinks a file. A delete removes the row and sets the
file's time to the server's; the daily task library.blob_sweep removes the
files under documents/ that no row names and whose time is older than
48 hours. The grace covers a delete racing an upload of the same bytes and
a database restored from an older dump. The delete sets the time before it
removes the row, and the sweep re-checks each file under a transaction-level
advisory lock on its hash, the lock an upload takes before it places the
file and inserts its row: a file an upload is about to name is never
removed under it.
A course's deletion takes its rows by the foreign key's cascade and does not touch the files: they leave at the next sweep, without the 48-hour grace (their time is their upload's). Accepted: a course's deletion is rare, confirmed, and refused while its students have work (ADR-141).
5. Inert files, staff routes (N-SEC-24)¶
Every route of PR1 is a staff route: teacherGuard, then the course
through accessibleCourse (invariant 6), a document of another course
being the 404 of a missing one. The file is streamed from disk with one
Range (206, 416 past the end, a multi-range or malformed header
answered 200 whole), always with accept-ranges, content-length,
content-disposition: attachment, x-content-type-options: nosniff,
cache-control: no-store and content-security-policy: default-src
'none'; sandbox (sendRanged, modules/http.ts). The audit log records
course.document_upload, course.document_rename,
course.document_delete and course.documents_quota.
6. The viewer (PR2)¶
PR2 adds a viewer for both kinds (apps/web/src/documents/) and a staff
preview: a document's title opens it in the app's expand layer. The viewer
takes a URL or HTML and knows nothing of courses, so PR3 mounts it with the
attempt's routes. A Markdown document's HTML is the one stored at upload,
served by GET …/documents/:documentId/html (DocumentHtml, the library's
guards; a PDF has none, the 404 of a missing document).
The PDF viewer is pdf.js, pinned, hardened:
- Legacy build. The modern one needs Safari 18; Safe Exam Browser on
macOS runs the system's WebKit. The legacy build polyfills what it uses
but
Promise.withResolversandAbortSignal.any(Safari 17.4): the viewer installs those two, in the page before pdf.js and in the worker before pdf.js's own code, which reaches the app's Safari 16.4. - No script. No
eval(pdf.js 6 has none left; the CSP refuses one), no document script (no scripting manager), no XFA, forms and annotation editing off; external links inert, internal links and the outline navigate. - Our origin only. The worker is a build asset of Quiz's origin (a
module worker, never
blob:); the character maps, standard fonts, ICC profiles and decoders are copied under/pdfjs/<pdf.js version>/, and the viewer asks for the folder of the version it loaded.useSystemFontsis off: a font a PDF does not embed comes from those standard fonts, so a page reads the same on every machine. Scoped amendment of ADR-110 §1–2:pdfjs/is kept across deploys and cached likeassets/(its files never change under their version), so a tab opened before an upgrade of pdf.js keeps its worker and its own data. -
Ranges.
disableAutoFetch,disableStream, chunks of 1 MiB: only what the pages on screen or a search need. pdf.js merges adjacent chunks into one request, and reads a file of 2 MiB or less in one request. -
Links navigate nothing. No link of a document, PDF or Markdown, keeps an
hrefor atarget: a click, a middle click, the context menu or a drag opens nothing, and a web address shows itself as a tooltip. A link within the document (a heading, a page, an outline entry) stays a keyboard link and moves the viewer. - A copy is the page's. The viewer is never an
iframe, and a copy made in it is one of the page's own copies (ADR-088): the viewer takes the copy from pdf.js's text layer, so the clipboard gets the selection as the journal reads it and the event reaches the journal's listeners, and pdf.js's "copy all" (a hidden element and a document-wide listener that writes the whole PDF's text after Ctrl+A) is taken out of the page. - Ids are the viewer's. A Markdown document's ids are prefixed per viewer, so a heading never collides with an id of the page, nor with one of another document mounted beside it.
7. Documents provided by an evaluation (PR3, product owner)¶
- An exam or an exercise may provide documents of its course's library,
at most 10 (
MAX_PROVIDED_DOCUMENTS), in the order ticked in its advanced options; a poll is refused422 documents_not_allowed(an empty list passes); a document of another course is422 document_unknown, as an unknown id. The selection is the join tableevaluation_provided_documents(migration0160), written by theevaluationmodule alone (providedDocuments.ts), the patch fielddocuments, replaced whole. - The selection is frozen like every setting once an attempt exists or the
evaluation runs (
configLock: the field is writable under no lock). - The evaluation's conditions (ADR-079)
gain an imposed line of kind provided, after the notepad's:
{ key: "documents", count }, a count and never the titles.imposedConditionstakesdocumentCountfrom every caller; an exam's blank practice counts none. - A document is not deleted while a template of the course or an
evaluation that is not over (
isEvaluationOver: closed, grading, released) provides it:409 document_in_use { evaluations }, decided under the document's row lock; a selection being written reads the documentFOR SHARE. Once only finished evaluations provide it, the delete goes and their rows go by cascade, their ids in the audit row's payload. The library lists where each document is used (usedBy). - A duplicate within the course, Save as template and an instance copy the selection; a copy into another course drops it (no screen copies across courses yet: the API's duplicate does, and drops). A change of a template's selection moves its revision; a pull leaves the instance's documents alone, like its settings.
- The student's exit (invariant 4): the student view of the
librarymodule,library/studentView.ts, the one reader of a document toward a student. It applies one pure rule,providedDocumentsReadable(@quiz/domain), built on the write gate of an answer (attemptClosedReason, moved there fromlive/gates.ts): never a poll, never during a pause; on an exam the official attempt in progress while the exam isrunning— not the lobby — and the server's clock before the attempt's deadline plus the grace; never a blank practice after the release (ADR-142); on an exercise an open attempt, its practice after the due date included (ADR-122). "Not in the lobby, not during a pause, not to a blank practice after the release" is the orchestrator's reading of the owner's "nothing afterwards", stated to the product owner on 2026-11-24 and not objected to. Two sitting routes (SITTING):GET /app/api/attempts/:id/documents/:documentId/file(a PDF,sendRanged, named after its title) and…/html(a Markdown document'sDocumentHtml,{ html, toc }: the caller holds the title); every refusal is the 404 of a missing document, save the IP rule's403 ip_not_allowedon the caller's own attempt, which does not depend on the document. Nothing leaves beyond the title, the kind and the bytes or the HTML. An impersonation of the attempt's holder is served, read-only, like every student payload (ADR-034; the orchestrator's decision, 2026-11-24).AttemptView.documentslists them while the rule holds,[]otherwise; the conditions state their number only, outside the rule. - The teacher's stateless preview (ADR-018) has no attempt: it lists the
selection, and reads the files through the course's staff routes. The
preview's builder loads the selection for it alone (
withDocuments): the hand-out a student prints (ADR-122 §9) and the paper copies never load it. A teacher on a staff seat has a real attempt and reads through the student's routes. - No audit row and no attempt event for a reading. A copy made from the in-page viewer is one of the page's own copies for the integrity journal (ADR-088), as the notepad's is (ADR-090 §6). A paper copy (ADR-125) gets nothing.
- The player's panel (PR3, second half;
student/DocumentsPanel.tsx, DESIGN.md "The documents panel"), as the validated mockup: a Documents dock above the calculator's and the notepad's (it closes neither); a split view from 900 px with a separator (360 px and 420 px kept, 46 % by default), an overlay under it; full screen a CSS layer on the same node with the clock, the save state and "Back to the questions"; one tab per document, a menu of them once the strip overflows; Ctrl/⌘+F to the search of the document on screen; Escape from the search to the document to full screen (or the overlay). What the student set is kept per attempt insessionStorage(memory only for the preview and an impersonation), each document's state read back only by the viewer'sreadViewState, and gone at every end of the attempt, at sign-out and when a kiosk station loads. The viewers are mounted when first shown and unmounted when the panel closes or hides: the panel follows the attempt view's list and the evaluation's pause, and a 404 from either route hides it and reads the attempt view again. The preview reads the course's staff routes. A 404 from a document's route marks that document "not available" in its tab and reads the attempt view again: a list without it drops the tab, an empty one hides the panel, a list that still holds it (a missing file, as on staging) keeps the mark beside the other documents. The resume reads the attempt view at once. What the device keeps of an attempt goes through one module (attempt/localState.ts). A refit of the PDF viewer (a full screen, a resize) keeps the view on the kept spot against pdf.js's own late scrolls until the reader moves (PdfView.#refit): a wheel, a pointer, a key or a toolbar step ends it. - The notepad's blocked clipboard (ADR-090 §6) stays the rule:
provided_no_clipboardrefuses a paste or a drop INTO the notepad, a document's text included, and nothing is journaled for it (the text is one of the page's own copies). A copy FROM a document is free, as from a question; the answer fields keep their clipboard. Nothing to build: the notepad's own handlers already refuse it, tested with the real viewer and journal. - Safe Exam Browser and the kiosk. The
.sebfile's URL filter admits Quiz's host whole (URLFilterRules, host patterns), so/pdfjs/<version>/and the worker under/assets/are admitted, as every route of the app; pdf.js fetches the file from the page (not the worker), so the requests carry SEB's Config Key header. The kiosk extension restricts no URL; the station's allowed URLs are ChromeOS policy, outside the repository. Both stay owed in a real client (§11).
8. Licensing¶
The licence of a standard or a datasheet is the teacher's and the school's responsibility. The platform reminds it at upload, in one line under the library, and checks nothing.
9. Backups, staging and retention¶
Scoped amendment of ADR-003 §1. The
stateful scope is PostgreSQL and the content-addressed file store under
ASSETS_DIR, which has been true since the pool's images and the paper
scans. The store is covered by the off-site borg copy and the VM backup, not
by the on-VM database dumps: that is why a document is a file, not a
bytea row that would swell every dump.
scripts/staging-export.sh leaves documents/ out of the staging copy to
save disk, the staging copy living on the same VM; it hides nothing:
staging keeps the rows, so a Markdown document's rendered HTML is there
like any other course content, and each document reads "File missing".
A deleted document leaves the backups on their own schedule (N-DATA-03), as a deleted paper scan does (open question 64).
10. Out of v1¶
Images in Markdown, replace, text extraction at upload (a warning for a scanned PDF, a search across documents), an MCP tool, a line on the System status page. Searching inside one PDF is in v1: the viewer searches its text layer.
11. Owed before production¶
- A real Safe Exam Browser, on Windows and on macOS, with
SEB_CONFIG_KEY_ENFORCE=1: the worker and the/pdfjs/<version>/files admitted by the URL filter; the range requests passing the Config Key check; no download prompt; a copy from a document pasted into an answer and into the code editor raising no incident (Windows' isolated clipboard included); Ctrl/Cmd+F reaching the page; no "left the page" incident from the panel; the pause and the deadline. - A kiosk station: the station's policy admitting those files; range requests across a re-attestation; no state of the previous student left.
- A load test on production-like PostgreSQL (the local one, on PGlite, is an indication only: 30 clients reading a 29 MB PDF whole while one autosaves, autosave p50 11 → 31 ms).
Consequences¶
- A course carries its exam material, and an exam under SEB can hand it out without paper.
- The file store grows by up to the sum of the quotas, minus what courses
share. The borg copy and the VM backup must keep covering
ASSETS_DIR. - The upload path holds no file in memory; the sweep is the only code that unlinks a document's file.
- Staging holds production's document rows, rendered Markdown included, but not their files: a PDF cannot be opened there.
- PR3 adds one more student exit to invariant 4, with its own leak test.
Alternatives considered¶
- The pool's
assetstable and route: any teacher of the instance can read any asset there; a course's licensed standard must not be. - Files as
bytearows: every on-VM dump would carry them, and the dumps are kept for months. - A library per teacher, or several vaults per course: refused by the product owner; the course is the unit its staff shares.
- Unlinking the file on delete: races an upload of the same bytes into another course, and a restored dump would name files already gone.
- Extracting the text of a PDF for search: out of v1; pdf.js searches the text layer in the browser.