- Enforce BEGIN IMMEDIATE write-serialization boundary prior to response snapshot assembly. - Atomically commit final submission, COMPLETED status, and publication intent in Path A. - Version export projections in publication intent and validate reconstructed hashes. - Decouple Phase 1 read-only extraction from Phase 2 post-verification writable WAL checkpointing. - Mandate explicit closure of Phase 1 connection before opening Phase 2 cleanup. - Enforce dual advisory locking (per-attempt and destination) and atomic no-replace publication. - Decouple operational exit codes: separate EXPORT_IO_ERROR from CLEANUP_PENDING and conflicts.
9.4 KiB
ADR-0007: Atomic Versioned JSON Export Contract
- Status: Accepted
- Date: 2026-11-10
Context
When live-quiz concludes, it must hand off the assessment evidence to caller workflows (agents, evaluators, or future learning platforms). We needed a durable, forward-compatible integration boundary that does not expose internal SQLite database implementation details.
Decision
Establish attempt.json as a standardized, versioned JSON contract published via an atomic file-replacement pattern:
-
Ten-Step Atomic Publication Pipeline & Directory Durability: Publication follows a strict 10-step sequence protected by dual advisory locks (per-attempt lock
attempt-<attempt-id>.lockand destination lockdest-<sha256_dest>.lock):- Acquire exclusive advisory locks (never unlinked while held).
- Establish write-serialization boundary via
BEGIN IMMEDIATE:- Path A (Final Submission →
COMPLETED): The response mutation toSUBMITTED, transition toCOMPLETED, complete response snapshot, andpublication_intent(target_schema_version,export_projection_version: "1",canonicalization_version: "1",destination_path,expected_sha256) are committed together in one atomic transaction. - Path B (Timeout / Early Close): The transaction is established before reading the final response snapshot and committing terminal status (
TIMED_OUT/CLOSED_EARLY) andpublication_intent.
- Path A (Final Submission →
- Validate structurally and serialize canonical payload conforming to RFC 8785 Canonical JSON, computing
expected_sha256prior toCOMMIT;. - Destination conflict check: under destination lock, verify destination does not hold a different
attempt_id(prohibiting overwriting another attempt's artifact). - Write canonical bytes to attempt-specific temporary file
.attempt.<attempt-id>.tmpin destination directory, flush, andfsyncfile descriptor. - Atomically link/rename
.attempt.<attempt-id>.tmptoattempt.json(using atomic no-replace semantics e.g.renameat2(..., RENAME_NOREPLACE)). fsyncthe parent directory descriptor to guarantee rename durability across power failure.- Verified WAL checkpoint through open writable connection (
PRAGMA wal_checkpoint(TRUNCATE);). - Clean close & ordered journal cleanup: close handles cleanly → unlink
-wal→ unlink-shm→ unlink primary.sqlite3last. - Emit machine-readable outcome on stdout and exit 0 (
SUCCESS_FINALIZED).
-
Reattempt vs. Recovery Semantics & Idempotency Verification:
- Reattempt (
live-quiz start): Launches a fresh execution against the same immutable assessment definition, generating a distinct newattempt_id. - Nonterminal Recovery (
live-quiz recover): In-place transactional migration; resumes interrupted or paused live assessment under the existingattempt_id. - Terminal Export Recovery (
live-quiz recover):- Phase 1 (Read-Only): Opens SQLite with read-only connection (
mode=ro), reading existing WAL frames without mutating tables or schema; reconstructs export usingexport_projection_versionand validates reconstructed hash against persistedexpected_sha256(failing closed with exit 3 on mismatch). Legacy journals lackingpublication_intentare explicitly rejected as unsupported (UNSUPPORTED_SCHEMA_VERSION, exit 5). Phase 1 read-only connection is closed cleanly before Phase 2. - Idempotency Verification: If target exists and matches
attempt_idandexpected_sha256, advances directly to Phase 2 cleanup. If differentattempt_id, exits code 2; if hash mismatch, exits code 3. - Phase 2 (Writable Cleanup): Opened only after Phase 1 connection is closed and publication durability is established; executes verified WAL checkpoint without schema modifications, closes handles, and unlinks companions then
.sqlite3last.
- Phase 1 (Read-Only): Opens SQLite with read-only connection (
- Reattempt (
-
Contract Attributes & Canonical Objective Traceability: Every exported attempt contains explicit provenance:
schema_versionattempt_idassessment_id&assessment_version(withquestionnaire_hash)workspacelocatorstatus(COMPLETED,CLOSED_EARLY,TIMED_OUT)timing(active duration seconds, wall-clock duration seconds, pause intervals)responsesarray preserving mappings to associated canonical objective references carried in the questionnaire.
Structural vs. Semantic Validation Invariant: The exported payload must conform structurally to the versioned attempt schema contract (
attempt.schema.json). However, the engine maintains zero semantic awareness of external registries: it does not validate, resolve, or query objective identifiers against external catalogs, preserving them verbatim for downstream evaluation. The exact schema layout for preserving question-to-objective response traceability remains open until the JSON schema contracts are formally authored. -
Response State Triad & Attainment Decoupling: Each item in
responses[]represents its interaction state independently of the assessment lifecycle status (COMPLETED,CLOSED_EARLY,TIMED_OUT):SUBMITTED: Explicitly confirmed by the learner. Includes explicit blank submissions where the learner intentionally submitted an empty response.DRAFT: Autosaved input acknowledged by SQLite commit-before-ack but unsubmitted when session terminated. Represents an acknowledged learner-generated modification (initial empty fields remainUNANSWERED). The latest acknowledged draft payload and server-side receipt timestamp are preserved verbatim. (Per-question active duration is designated provisional pending formal measurement definitions).UNANSWERED: Zero acknowledged learner input recorded (payload isnull).
Valid Terminal/Response State Constraints:
COMPLETED: Strictly requires every questionnaire item to haveresponse_status: SUBMITTED(explicit blank submissions qualify).CLOSED_EARLY: At least one response must beDRAFTorUNANSWERED, following an explicit learner termination action.TIMED_OUT: At least one response must beDRAFTorUNANSWERED, following authoritative timer expiration.
Generalized Server-Side Serialization & Absorbing Terminal States: All response mutations (draft autosaves, explicit submissions) and terminal lifecycle transitions (
COMPLETED,CLOSED_EARLY,TIMED_OUT) are serialized server-side within SQLite transactions. The final required response submission and transition toCOMPLETEDmust be committed atomically. Once any terminal lifecycle transition commits, its status is strictly immutable and absorbing: all subsequent response mutations and competing terminal transition attempts are rejected.Attainment vs. Submission Independence: Submission status describes the user interaction, not the correctness or cognitive quality of the content.
quiz-evaluatorenforces evidence-first principles across the response triad:- Explicit Blank
SUBMITTED: Evaluated as an intentional empty response against rubric criteria. DRAFT: Evaluated as available evidence of learning without automatic penalty for draft status, disclosing draft provenance and reporting demonstrated vs. unestablished criteria.UNANSWERED: Reports that no response evidence exists; must not infer misconceptions or conclude that the learner lacks underlying knowledge solely due to absence of recorded input.
-
Structured Process Outcomes & Exit Codes: Operational failures are strictly decoupled from assessment lifecycle outcomes. Standard output emits a single-line JSON result packet (
{"outcome": "...", "attempt_id": "...", ...}), while standard error carries human-readable logs:Exit Code Symbolic Name Meaning Journal Action 0SUCCESS_FINALIZEDExport verified, directory synced, journal unlinked. Deleted 0SUCCESS_SUSPENDEDPause grace period expired cleanly; resume available. Preserved 1EXPORT_IO_ERRORTransient disk/write/sync failure during publication. Preserved 2CONFLICT_DIFFERENT_ATTEMPTDestination occupied by a different attempt_id.Preserved 3CONFLICT_INTEGRITY_MISMATCHDestination has matching ID but differing SHA-256 hash. Preserved 4RECOVERY_CONCURRENCY_LOCKEDAnother process holds the advisory recovery lock. Preserved 5UNSUPPORTED_SCHEMA_VERSIONJournal or export schema version unsupported. Preserved 6CLEANUP_PENDINGExport durable and verified, but journal removal failed. Preserved for retry
Alternatives Considered
- Direct SQLite Database Querying by Evaluators / Future LMS: Rejected. Exposing internal database tables couples external consumers to
live-quizschema migrations. - Direct In-Place File Overwriting: Rejected. If a process crash occurs during file write, the destination file is left partially written and corrupted.
Consequences
- Positive: Complete data durability; forward compatibility with any future LMS or evaluator; safe atomic handoff to agent callers.
- Negative / Trade-offs: Requires an atomic rename step and validation pass prior to session journal cleanup.