Skip to content

Job lifecycle

voxix edited this page Nov 14, 2026 · 12 revisions

Job lifecycle

The flow is async and one-shot: the student submits, leaves, and is notified in the app when the result is ready. They accept it or send feedback. Feedback starts a new round of the same job.

received ──dispatch──▶ processing ──result saved──▶ awaiting_review ──accept──▶ accepted
                          │   ▲                        │      │
              error after │   └──── feedback ──────────┘      │ 48 h, no reply
                  1 retry ▼                                    ▼
                        failed                              expired ──feedback──▶ processing
State Agent actor Notes
received none Upload saved, job row written
processing running One round is in progress
awaiting_review hibernated Snapshot kept until the review deadline
accepted gone Final; snapshot deleted
expired gone Result kept, snapshot deleted. Late feedback starts a fresh actor. No sweeper sets this state yet
failed gone Final, after one retry in round 1. A later round that fails returns to the last answer. The student is notified

Accept and feedback carry the round the student saw (the count above, not the row number), and the API answers 409 if it is not the one now shown, so a stale tab can't accept an answer it never showed.

Transitions are defined once in shared.models.TRANSITIONS; the API refuses anything else. Load the job with SELECT ... FOR UPDATE before changing its status, so two requests (for example feedback and the expiry sweeper) can't both win.

How a round runs

POST /jobs and feedback commit the job and answer at once, with status queued or running. The dispatcher then works in the background:

  1. Move the job to processing.
  2. Create the actor job-<id> from the template, unless an earlier round already did.
  3. POST /run through the router, which wakes a suspended actor. An actor fills its worker, so when all workers are taken the router answers 503 and the job waits for one, up to 60 s. The reply streams a newline every 5 s until the answer; 60 s without a byte is an error. Errors other than the 503 get one retry.
  4. Suspend the actor, or delete it if there is no answer: a crashed actor can't be resumed, so the next try needs a fresh one.
  5. Save the result. An answer moves the job to awaiting_review. No answer in round 1 moves it to failed. No answer to feedback marks that round failed_at, keeps its row and artifacts, and moves the job back to awaiting_review on the last answer: failed is final, and a failed revision must not cost the student the answer they have. The Jobs API shows the newest round that did not fail, and reports the failed round's feedback as failedFeedback, so the web app says the revision failed and puts the text back in the box, even after a reload. The row numbers (job_rounds.number) can have gaps: the next feedback is the newest row plus one, and they stay internal. The student counts the rounds that did not fail (1, 2, 3), so there is no gap for them, and the retry of a failed revision is the same "round 2" again.

Step 4 comes before step 5 on purpose. The student can only reply once the result is saved, so a reply can't reach an actor that is still being suspended. A clean-up is tried three times, and one that still fails is logged and costs an idle actor, never the answer. Accepting deletes the actor in the background.

Each round runs as a task inside an API process, which first claims it in the database: a lease (lease_id, lease_until) on the job_rounds row, taken in the transaction that holds the job lock, using the database clock. The lease lasts 60 s and the run renews it every 20 s. A sweep at start-up and every 30 s picks up the newest round of every received or processing job whose lease has expired, so a crashed or restarted instance's rounds are run again, and two instances do not run the same round. A round can be claimed 3 times (claims); after that it stays processing and is logged, which bounds paid retries. Finishing a round needs the process's own lease_id, so one that lost its lease changes nothing. The actor is reused.

POST /run on the agent is idempotent per job, round and lease (lease_id, which the dispatcher sends): a second call under the same lease joins the run in progress (or returns the finished answer) instead of starting another, and a caller hanging up does not cancel the work. A run that raised is dropped, so a retry runs again. A round claimed again under a new lease gets a run of its own, because the old run holds a Context token for the old lease, whose writes the API refuses.

If Postgres can't be reached, a round keeps retrying its database steps every 5 s, and so does recovery at start-up, so a short outage doesn't cost a finished answer. Other errors are not retried: the job stays in processing until the next restart.

Rounds

  • Round 1 answers the request. Each feedback adds a round to the same job.
  • A round's answer is its final artifact (at most one per round).
  • Feedback within the snapshot's lifetime restores the snapshot and appends the feedback as a new message. After that, a fresh actor rebuilds the conversation from the transcript in Postgres. Today the model path keeps the conversation in the actor's memory, and a fresh actor rebuilds the job from the task and its feedback through the Context API, without the earlier answers. The fake agent keeps no conversation.

Timing

  • The actor hibernates just before the result is saved; there is no idle wait.
  • Review window: 48 h. A storage lifecycle rule deletes snapshots older than 3 days as a safety net.
  • Notifications are in-app polling (about every 5 s). The poll endpoint repeats a few seconds before updated_since and the client de-duplicates by id, because commit time and updated_at can differ. Email and push come later.

Clone this wiki locally