diff --git a/docs/architecture.md b/docs/architecture.md index 0be7233..a158f36 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -124,8 +124,10 @@ The system explicitly separates: ### 5.2 Ephemeral SQLite Session Journal During active assessment, `live-quiz` maintains state in a temporary SQLite database (`~/.local/state/live-quiz/sessions/.sqlite3`): - Server commits acknowledged draft updates, answer submissions, and timing events atomically to SQLite. -- Pausing commits state and **allows the server process to exit cleanly**, preventing idle background processes. -- Unexpected kills (`SIGKILL`, power failure) leave the journal intact. +- **Durable Pause Checkpoint & In-Browser Resume:** Clicking **Pause** commits state, freezes active time, records `status = PAUSED` in SQLite, and displays an in-browser Resume control. +- **Configurable Pause Grace Period (Default: 2 Hours):** The server remains alive and accessible in the browser during a 2-hour window (`--pause-grace-period 2h`). If resumed within this window, the assessment continues instantly in the browser. +- **Grace-Period Expiration:** If the 2-hour grace period expires without resumption, the server process terminates cleanly to prevent zombie processes, retaining the journal with status `PAUSED` and emitting `outcome: SUSPENDED` on stdout. +- Unexpected kills (`SIGKILL`, power failure) leave the journal intact for recovery. ### 5.3 Atomic Publication & Journal Deletion Upon reaching a terminal state (`COMPLETED`, `CLOSED_EARLY`, `TIMED_OUT`): diff --git a/docs/decisions/0002-harness-agnostic-process-lifecycle.md b/docs/decisions/0002-harness-agnostic-process-lifecycle.md index 3cab707..7f91348 100644 --- a/docs/decisions/0002-harness-agnostic-process-lifecycle.md +++ b/docs/decisions/0002-harness-agnostic-process-lifecycle.md @@ -4,15 +4,53 @@ - Date: 2026-11-10 ## Context -When an AI agent or skill invokes `live-quiz`, the execution must not block agent reasoning indefinitely, yet the agent must be notified when the assessment concludes. We needed a process coordination mechanism that works cleanly without tight coupling to proprietary agent harnesses. +When an AI agent or skill invokes `live-quiz`, the execution must not block agent reasoning indefinitely, yet the agent must be notified when the assessment concludes or suspends. We needed a process coordination mechanism that works cleanly without tight coupling to proprietary agent harnesses, while correctly distinguishing between process exit and assessment completion. ## Decision -Design `live-quiz` as a standard foreground UNIX process whose termination signals session conclusion. In agent environments that support managed asynchronous tasks (such as Antigravity background tasks), the agent launches `live-quiz start`, yields the URL to the user, and terminates its turn. When `live-quiz` reaches a terminal state, its process exits with code 0, naturally triggering reactive wakeup of the agent harness. +Design `live-quiz` as a standard foreground UNIX process whose termination signals that an execution cycle has ended, but **not necessarily that the assessment has concluded**: + +1. **Independent Lifecycles:** + $$\text{Process State (Running / Exited)} \neq \text{Assessment State (RUNNING / PAUSED / COMPLETED)}$$ + A process exit indicates only that the current HTTP execution cycle has ended. +2. **Structured Lifecycle Outcome:** + Upon process termination, `live-quiz` emits a machine-readable JSON outcome on stdout: + - **`FINALIZED`:** The assessment reached a terminal state (`COMPLETED`, `CLOSED_EARLY`, `TIMED_OUT`). `attempt.json` was durably exported. Caller proceeds to evaluation. + - **`SUSPENDED`:** The assessment was paused and the 2-hour grace period expired (or process terminated). The SQLite journal is retained. Caller does **not** invoke evaluation. + - **`ERROR`:** An operational error occurred. The SQLite journal is retained for recovery. +3. **Sequence Diagram:** + +```mermaid +sequenceDiagram + autonumber + actor Learner + participant Browser + participant Server as live-quiz Server + participant SQLite as SQLite Journal + participant Caller as Agent / Harness + + Learner->>Browser: Click Pause + Browser->>Server: POST /api/session/pause + Server->>SQLite: Commit drafts & transition status to PAUSED + SQLite-->>Server: Transaction committed + Server-->>Browser: 200 OK (Suspension confirmed) + Browser->>Learner: Display "Assessment Paused" + Resume button + + alt Resumed within 2-hour grace period + Learner->>Browser: Click Resume + Browser->>Server: POST /api/session/resume + Server->>SQLite: Commit status RUNNING & unfreeze clock + Server-->>Browser: 200 OK + else Grace period (2h) expires without resume + Server->>Caller: Process exits (stdout: outcome=SUSPENDED) + Caller->>Caller: Inspect outcome: SUSPENDED (Do NOT invoke Evaluator) + end +``` ## Alternatives Considered -- **Antigravity Sidecars and `agentapi`:** Rejected. Testing revealed that `agentapi` relies on Antigravity internal language server environment variables (`ANTIGRAVITY_LS_ADDRESS`, `ANTIGRAVITY_CSRF_TOKEN`), breaking execution when run outside the agent environment and introducing unnecessary daemon infrastructure. -- **Indefinite Synchronous Blocking Command:** Rejected. Fails over human time scales; if a learner pauses or steps away, long-running agent tool calls timeout or hang. -- **Polling Loop:** Rejected. Wastes tokens and agent execution turns polling local status endpoints. +- **Treating Every Process Exit as Assessment Completion:** Rejected. Conflating process termination with assessment finalization leads to evaluating incomplete paused attempts. +- **Immediate Process Exit on Pause:** Rejected. Turning an ordinary short interruption (e.g. 2-minute phone call) into an unrecoverable dead browser tab destroys UX. +- **Antigravity Sidecars and `agentapi`:** Rejected. Coupled to internal language server environment variables. + ## Consequences - **Positive:** Complete decoupling from harness internals; standard OS process semantics (`exit 0`); portable across Antigravity, Claude Code, terminal shells, and CI pipelines. diff --git a/docs/decisions/0006-ephemeral-sqlite-session-journal.md b/docs/decisions/0006-ephemeral-sqlite-session-journal.md index cecc63f..663de42 100644 --- a/docs/decisions/0006-ephemeral-sqlite-session-journal.md +++ b/docs/decisions/0006-ephemeral-sqlite-session-journal.md @@ -9,17 +9,40 @@ During an assessment, the application must withstand interruptions (unexpected p ## Decision Use a temporary, local SQLite database (`~/.local/state/live-quiz/sessions/.sqlite3`) strictly as an ACID-compliant **write-ahead session journal** during active execution: 1. **Three Separated Lifecycles:** - - *Server Process:* Ephemeral (runs on demand, exits on terminal state or explicit pause). + - *Server Process:* Ephemeral (runs on demand, exits on finalization or pause grace-period expiration). - *Assessment Instance:* Persistent concept identified by stable IDs and immutable questionnaire version hashes. - *Assessment Attempt:* Persistent run representing a single pass against an immutable assessment version. -2. **Transactional State Journaling:** +2. **Transactional State Journaling & Durable Checkpoints:** - Every acknowledged answer submission, draft update, and timing event is committed immediately to SQLite. - - Pausing commits state and **terminates the server process cleanly**, eliminating zombie processes. -3. **Journal Destruction Invariant:** + - Clicking **Pause** creates a durable checkpoint: pending drafts are saved, active time is frozen, and `status = PAUSED` is committed before browser acknowledgment. + - **Configurable Pause Grace Period (Default: 2 Hours):** The local server stays running during a 2-hour window (`--pause-grace-period 2h`). Resuming within this window is instant in the browser without CLI/agent interaction. + - If the 2-hour window expires without resumption, the server terminates cleanly to prevent zombie processes, retaining the journal with status `PAUSED`. +3. **Assessment State Machine:** + +```mermaid +stateDiagram-v2 + [*] --> RUNNING: Attempt started + + RUNNING --> PAUSED: Learner clicks Pause\n(Durable commit in SQLite) + PAUSED --> RUNNING: Learner clicks Resume\n(In-browser within 2h grace period) + + RUNNING --> COMPLETED: Final question submitted + RUNNING --> CLOSED_EARLY: Learner explicitly closes early + RUNNING --> TIMED_OUT: Active duration budget exhausted + + note right of PAUSED + Server stays alive for up to 2 hours. + If grace period expires, server exits cleanly, + retaining journal with status PAUSED. + Resume via: live-quiz recover --from-path + end note +``` + +4. **Journal Destruction Invariant:** - Upon reaching a terminal state (`COMPLETED`, `CLOSED_EARLY`, `TIMED_OUT`), the server publishes `attempt.json`. - The temporary SQLite journal is deleted **only after durable export publication succeeds**. -4. **Explicit Recovery Command:** - - If a process is killed unexpectedly or paused, the journal remains on disk. +5. **Explicit Recovery Command:** + - If a process is killed unexpectedly (`SIGKILL`) or grace period expires while paused, the journal remains on disk. - Running `live-quiz recover --from-path ` validates the journal, restores the authoritative clock, reloads drafts, and resumes the server under the exact same `attempt_id`. - *Recovery Idempotency:* If recovery is invoked on a journal whose `attempt.json` was already successfully published, it verifies the export, cleans up the journal, and exits cleanly.