feat(practice): run practice jobs on the code runner with one stored result per run

This commit is contained in:
shira committed 2026-10-11 19:31:08 +03:00
1 parent 9385b2ddd4
commit 9344228e84
17 files changed
+1010 -35

No files matched your search

+5 -2
View File
@@ -818,11 +818,14 @@ The attempt flow does not serve practice items yet, so no practice exists in pro
#### Practice queue
Implemented: a durable queue with one running and ten queued jobs. Code: migration `00053_create_assessment_practice_queue.sql`, the port `outbound.PracticeJobStore` (`port/outbound/assessment_practice_queue.go`), the store `adapter/secondary/postgres/assessment_practice_queue.go`, the executor `usecase/practice/execute.go`, the worker `backend/internal/worker/practicequeue/`, and the wiring `bootstrap/practice.go`. It is not the scheduled job runtime: a scheduled job runs its handler inside the transaction that completes it, while a practice try waits seconds or minutes for an external run, and no transaction may stay open that long.
Implemented: a durable queue with one running and ten queued jobs. Code: migrations `00053_create_assessment_practice_queue.sql` and `00054_add_practice_job_runner_run.sql`, the port `outbound.PracticeJobStore` (`port/outbound/assessment_practice_queue.go`), the store `adapter/secondary/postgres/assessment_practice_queue.go`, the executor `usecase/practice/execute.go`, the worker `backend/internal/worker/practicequeue/`, and the wiring `bootstrap/practice.go`. It is not the scheduled job runtime: a scheduled job runs its handler inside the transaction that completes it, while a practice try waits seconds or minutes for an external run, and no transaction may stay open that long.
- States. A job is `queued`, `running` (under a lease of one worker), `finished` (its result is stored), or `failed` (the infrastructure gave up; never a verdict). It records the generation of its run key, the counted attempts, when it may be claimed (`available_at`), the lease, the start of the running try, and the class of the last failure.
- Atomic limits. Admission and the claim take the same transaction-scoped advisory lock and count the unfinished jobs. A command is admitted only while fewer than eleven jobs (one running and ten queued) are unfinished; the claim runs a job only while no other running job holds a live lease, so at most one job runs across all backend instances. A debug run beyond the limit answers 503 with `Retry-After` and stores nothing. A final solution beyond it stores nothing either: the practice becomes `failed` with `sandbox_busy`, which consumes nothing and accepts the solution again. The claim prefers a running job whose lease expired, then final solutions before debug runs, and skips rows that another transaction locks, so it never waits for a row lock while it holds the queue lock.
- One try. The executor builds the runner request from the stored task and submission, submits it, and polls the run every `PRACTICE_POLL_INTERVAL`, renewing the lease each time. A finished run stores the result once (`result IS NULL` guard), finishes the job, and, for the latest final submission of a pending practice, finishes the practice, in one transaction guarded by the lease owner and the start of the try. A busy runner puts the job back for the time it asked for (1 s to 1 min) without counting an attempt. A runner that is unreachable, refuses the token, or answers outside its contract is retried with the same run key after a backoff (2 s doubling to 1 min); a run the runner could not evaluate, or a key it holds with another payload, is retried with the next generation, so the next try is a new run; a lost run is submitted again. After `PRACTICE_JOB_MAX_ATTEMPTS` counted attempts, or for a task that cannot form a valid request, the job fails and a final practice becomes `failed` with `sandbox_error`; nothing is graded. A run that stays unfinished for 10 minutes counts as a failed try.
- One try. The executor builds the runner request from the stored task and submission, submits it, and polls the run every `PRACTICE_POLL_INTERVAL`, renewing the lease each time. A finished run stores the result once (`result IS NULL` guard), finishes the job with the opaque run ID of the runner run (`runner_run_id`, unique, set only on a finished job), and, for the latest final submission of a pending practice, finishes the practice, in one transaction guarded by the lease owner and the start of the try. A busy runner puts the job back for the time it asked for (1 s to 1 min) without counting an attempt. A runner that is unreachable, refuses the token, or answers outside its contract is retried with the same run key after a backoff (2 s doubling to 1 min); a run the runner could not evaluate, or a key it holds with another payload, is retried with the next generation, so the next try is a new run; a lost run is submitted again. After `PRACTICE_JOB_MAX_ATTEMPTS` counted attempts, or for a task that cannot form a valid request, the job fails and a final practice becomes `failed` with `sandbox_error`; nothing is graded. A run that stays unfinished for 10 minutes counts as a failed try.
- Runner isolation. The runner request carries the language, the source, the limits, and the test groups, nothing else: a debug run has the public examples and the candidate's own tests, a final solution the hidden groups only. The attempt item, the submission, and the generation reach the runner only as the opaque run ID, an HMAC under `CODE_RUNNER_RUN_KEY`, so the runner cannot link a run to a candidate. The stored `runner_run_id` is that ID: it lets an operator match a stored result to the runner's own log line without the runner learning who the candidate is.
- Repeats. A response lost or cut after the runner accepted the run, a request timeout, and a restart of the worker all end in a retry with the same run key, so the runner returns the run it already holds (200 instead of 202) and starts none. The result is written under the lease of the try and the `result IS NULL` guard, and the database refuses a second result, so a run has at most one result and one `runner_run_id`.
- Infrastructure failure. A runner that is stopped, unreachable, or answers outside its contract never produces a verdict. While tries remain, a debug run reads as `queued`, which tells the client to read it again; after the last try it reads as `failed` with `sandbox_error` and no result, a final practice becomes `failed` with `sandbox_error`, and neither changes the attempt, its result, or the confirmed category of the candidate. A new debug run or final solution is accepted at once.
- Restart. A worker that stops gives its job back at once without counting. A worker that dies leaves the lease to expire; the next claim takes the job over with the same generation and counts one attempt, the runner returns the run it already has, and the result is stored once. A late completion by the dead worker changes nothing, because the job no longer belongs to its try.
- Debug run limits. Besides the queue, a candidate gets at most `PRACTICE_RUNS_PER_MINUTE` and `PRACTICE_RUNS_PER_HOUR` new debug runs (the Redis limiter, keyed by a keyed digest of the account) and one unfinished run per practice; both answer 429 with `Retry-After`. A limiter failure refuses the run as unavailable.
- Off without a runner. Without `CODE_RUNNER_URL` the worker does not start, and the operations answer 503 and store nothing. Several backend instances may run the worker; the claim keeps one job running among them.