Skip to content

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.

  1. POST /api/threads/{id}/messages stores the user message and an execution with status accepted. The request carries an Idempotency-Key header, so a resent request returns the same run instead of starting a second one.
  2. The response gives the run id and its events address, /api/runs/{id}/events.
  3. The run pool (pool.py) runs at most RUN_CONCURRENCY runs at once. The others wait in arrival order and get a run.queued event with their position.
  4. 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.
  5. 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.
  6. 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 accepted and resumes from its last finished node. A resume counts against MAX_RUN_RESUMES (2).
  • Past that, the run ends as interrupted and 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:

EventWhen
execution.started, .completed, .failed, .cancelled, .timed_out, .interrupted, .resuming, .resumedStatus changes
run.queuedWaiting for a free slot, with the position
run.waiting, run.resumedA pause for the student, and its end
node.started, node.completed, node.failed, node.waitingEach graph node, with time and summary
retrieval.candidatesEverything retrieval found, with scores and coverage
evidence.packedWhich passages became evidence, and why others didn't
token.deltaBatched tokens of the draft and the reasoning, as they stream
verify.claimEach claim's verdict
llm.callEach model call: step, tokens, time, time waited for the gate
web.provider_switchedTavily failed, SearXNG took over
code.ran, math.checkedCode 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.