Runs, events and replay
An answer is a run: a row in executions that one process claims and executes. This page explains how runs are started and limited, how they survive a restart, and how their events drive the live view and replay. The code is in app/execution/.
A run's life
stateDiagram-v2 [*] --> accepted: message posted accepted --> running: the pool starts it running --> waiting: pause (sign-in, code) waiting --> running: the student answers running --> completed running --> failed running --> cancelled: Stop running --> timed_out: past the time limit running --> accepted: process died, resume running --> interrupted: died, no resumes left waiting --> timed_out: no answer in 24 h
Figure 1. Run states. completed, failed, cancelled, timed_out and interrupted are final.
POST /api/threads/{id}/messagesstores the user message and an execution with statusaccepted. The request carries anIdempotency-Keyheader, so a resent request returns the same run instead of starting a second one.- The response gives the run id and its events address,
/api/runs/{id}/events. - The run pool (
pool.py) runs at mostRUN_CONCURRENCYruns at once. The others wait in arrival order and get arun.queuedevent with their position. - The runner (
runner.py) loads the chat's history, the Space's sources and settings and the student's memory, builds the graph input, and runs the graph. It writes a heartbeat every 5 seconds. - When the graph finishes, the runner saves the assistant message with the answer, citations, confidence and mind map, links any code cells to it, and marks the run
completed. - After that, it may propose memory items. This happens after the answer is saved on purpose: a slow or failed proposal never delays or loses the answer.
Durable runs
LangGraph's Postgres checkpointer saves the graph state after every node, keyed by the run id.
- The API looks for runs whose heartbeat stopped for
STALE_EXECUTION_SECONDS(30 s), at start-up and every 15 seconds after. - Such a run goes back to
acceptedand resumes from its last finished node. A resume counts againstMAX_RUN_RESUMES(2). - Past that, the run ends as
interruptedand the student is asked to send the message again.
So a restart of the API mid-answer costs at most the step that was running.
Cancelling
Stop sets cancel_requested and the run's in-memory cancel switch. The graph wrapper checks the switch before every node, and closing the run's HTTP clients aborts a model call or page fetch in flight. The run ends as cancelled. The switch lives in the API process's memory, which is why the API runs as one process.
Events
Every run writes events to execution_events, numbered per run. The main kinds:
| Event | When |
|---|---|
execution.started, .completed, .failed, .cancelled, .timed_out, .interrupted, .resuming, .resumed | Status changes |
run.queued | Waiting for a free slot, with the position |
run.waiting, run.resumed | A pause for the student, and its end |
node.started, node.completed, node.failed, node.waiting | Each graph node, with time and summary |
retrieval.candidates | Everything retrieval found, with scores and coverage |
evidence.packed | Which passages became evidence, and why others didn't |
token.delta | Batched tokens of the draft and the reasoning, as they stream |
verify.claim | Each claim's verdict |
llm.call | Each model call: step, tokens, time, time waited for the gate |
web.provider_switched | Tavily failed, SearXNG took over |
code.ran, math.checked | Code cells and equation checks |
Events never hold cookies, keys or passwords.
Live view over SSE
GET /api/runs/{id}/events is a server-sent events stream. It sends every event after the one the browser last saw (Last-Event-ID, or a query parameter after a token refresh), then waits for more.
The stream does not poll the database every moment. When any session in the API process commits an event, events.py wakes the streams watching that run. A slow timer still re-reads, which covers events written by another process and keeps proxies from closing the stream. A reconnect never re-runs anything: it only reads the log.
Replay
The browser builds its run view with one reducer (runStore.ts) that takes events one at a time. Live view and replay feed it the same events, so a replayed run looks exactly as it did live. The replay slider in the Flow tab picks how many events to apply.
Events are kept for EVENT_RETENTION_HOURS (30 days). A purge_events job removes older ones.
The model gate
Model calls from every run, the worker and study tools go through one priority gate per process (see Backend and API). An llm.call event records how long a call waited for it, which the Steps tab shows.