feat(practice): store code submissions and practice states apart from attempts

This commit is contained in:
shira committed 2026-10-11 14:58:04 +03:00
1 parent d5e169e71b
commit 51770eea6c
61 files changed
+8584 -135

No files matched your search

+15 -2
View File
@@ -786,13 +786,13 @@ A checker is set per group: `exact`, `tokens`, `numbers` (numeric tokens are equ
Outcomes distinguish `accepted`, `wrong_answer`, `compile_error` (for Python, a syntax check with `compile()` before the tests; for Go, compile and link), `time_limit_exceeded`, `memory_limit_exceeded`, `output_limit_exceeded`, `runtime_error` (including the process limit), and `infrastructure_failure`. Test time is the CPU time of every process of the test, including interpreter startup; memory is the cgroup peak of the test, including nsjail and file cache charged to the test.
Planned and not implemented: deployment of the runner VM with TLS, firewall, and monitoring; the run queue and the practice usecases that call the backend port (the port and its client are implemented, see below); service metrics; running the A16 suite on the runner VM.
Planned and not implemented: deployment of the runner VM with TLS, firewall, and monitoring; the worker of the run queue that calls the backend port, and scoring (the port, its client, and the stored submissions are implemented, see below); service metrics; running the A16 suite on the runner VM.
The A16 hostile-program suite (`make -C runner test-a16`, see [checks](TESTING.md#a16-hostile-program-suite)) is implemented and passes locally. Code practice is enabled only after the suite has passed on the runner VM with the verdict `pass`; any failed case blocks enabling it.
#### Backend port and client
Implemented: the outbound `CodeRunner` port (`backend/internal/port/outbound/code_runner.go`), its client over HTTPS (`backend/internal/adapter/secondary/code_runner/`, built on the generated client in `api/`), and the disabled runner (`code_runner/disabled/`). Bootstrap selects the runner from `CODE_RUNNER_URL` (`bootstrap/code_runner.go`): an unset URL selects the disabled runner, a set one the client, and an unusable setting stops startup instead of falling back. Nothing calls the port yet: the practice usecases, the run queue, and scoring are planned. The environment variables are in [DEPLOY.md](DEPLOY.md#code-runner-client).
Implemented: the outbound `CodeRunner` port (`backend/internal/port/outbound/code_runner.go`), its client over HTTPS (`backend/internal/adapter/secondary/code_runner/`, built on the generated client in `api/`), and the disabled runner (`code_runner/disabled/`). Bootstrap selects the runner from `CODE_RUNNER_URL` (`bootstrap/code_runner.go`): an unset URL selects the disabled runner, a set one the client, and an unusable setting stops startup instead of falling back. Nothing calls the port yet: the submissions it will run are stored (see [Practice submissions](#practice-submissions)), and the worker of the run queue and scoring are planned. The environment variables are in [DEPLOY.md](DEPLOY.md#code-runner-client).
- What leaves the backend. A request holds the language, the source, the technical limits, and the test groups with their checkers; the checker program of a task family is part of a group. It holds no account, attempt, task, or company identifier and no name or contact. `CodeRunRequest.Validate` checks the runner contract rules (ranges, unique IDs, checker parameters, the CPU budget of 300000 ms, the payload size) before anything is sent. A request prints as `[redacted code run request]` in every format and in logs, because the source is candidate work and the hidden tests are task secrets.
- Two calls, no hidden retries. `Submit` is idempotent by run key and reports `running`, `finished`, or `busy`; `Poll` reads the run without sending the payload again. Each makes one runner call and neither retries nor sleeps. `busy` is the runner's 503 `runner_busy`: the runner stored nothing, `RetryAfter` carries its `Retry-After`, and the caller keeps the queue and submits again with the same key. When the runner has lost a run (a restart or the retention time), `Poll` returns `ErrCodeRunNotFound` and the caller submits again.
@@ -803,6 +803,19 @@ Implemented: the outbound `CodeRunner` port (`backend/internal/port/outbound/cod
What the checks cover: the fixture tests run the client against a local TLS 1.3 stand-in that speaks the `api/runner` protocol (success, every outcome, compile error, runner timeout, busy, 401 and 5xx, malformed and contradictory answers, a stopped service, a foreign CA, TLS 1.2, idempotent retry). They do not prove sandbox isolation or behavior on the production runner VM. The run on a real runner is `make -C backend test-code-runner-live` (see DEPLOY) and has not been performed against the runner VM.
#### Practice submissions
Implemented: the storage of practice items, their submissions, and the execution jobs, separate from the attempt snapshot and the result. Code: migration `00052_create_assessment_practice_submissions.sql`, the domain `backend/internal/domain/assessment_practice.go`, the port `outbound.AssessmentPracticeRepository` (`port/outbound/assessment_practice.go`), the repository `adapter/secondary/postgres/assessment_practice.go`, the usecase `backend/internal/usecase/practice/`, the handler `adapter/primary/http/v1/practice.go`, and the wiring `bootstrap/practice.go`.
- Three tables. `assessment_practices` holds one practice item of an attempt, `main` or `additional` (one of each per attempt), with its private task content in `task` (languages, the source length bound, limits, the checker, the public examples, and exactly three hidden groups) and its state: `not_started`, `pending`, `finished`, or `failed` with `sandbox_busy` or `sandbox_error`. Its ID is the item ID the candidate sees and the attempt item of the run key. `assessment_practice_submissions` holds every submission: the mode `public` (a debug run) or `final` (the graded solution), the language, the source with its SHA-256, the candidate's own tests of a debug run, the idempotency scope, and the result, written once. `assessment_practice_jobs` holds the execution job of every submission. Triggers keep the identity and the task of a practice and the request part of a submission immutable: the database refuses to replace the source or the language of a stored submission, to write a second result, and to delete a submission other than by the cascade of its account, attempt, or practice.
- Transactional queue. A submission and its job (`queued`) are inserted in one transaction, so a job exists exactly when its submission committed, and a worker picks it later. The job table is the outbox of the runner: nothing outside PostgreSQL has to learn about a submission before it runs, so no Kafka event is written.
- Debug runs. `CreateMyAssessmentPracticeRun` (`POST /api/v1/me/assessment-attempts/{attempt_id}/practice-runs`) stores a `public` submission and answers 202 with the queued run; `GetMyAssessmentPracticeRun` reads it. A debug run carries the public examples and up to ten tests of the candidate and never a hidden group: the request built for the runner has none, and the response maps only the public groups of a stored result. The task content, the hidden tests, and the source never reach a response. A debug run changes neither the practice state nor the attempt version.
- Final solutions. `practice.Service.SubmitFinal` stores a `final` submission with its job and marks the practice `pending`. The stored solution sent again (the same language and source) stores nothing; another solution is refused while the practice is `pending` or `finished`, so the source of a graded solution never changes; a practice that failed in the sandbox accepts a solution again. No HTTP operation calls it yet: `SubmitMyAssessmentAnswer` will, once the attempt flow serves practice items, and then the attempt version advances in the same transaction.
- Gates. A practice accepts commands only while it is the current item: its attempt is in `practice_pending` (main) or `additional_practice_pending` (additional) and before its deadline, read under a share lock of the attempt. The account must be a verified candidate. Every read is scoped by the owner, so an attempt, practice, or run of another candidate reads as not found. Without `CODE_RUNNER_URL` the operations answer 503 and store nothing.
- Idempotency. A command reserves the generic idempotency record of its scope (a keyed digest of the operation, the key, and the account) in its transaction. A repeat with the same body within 24 hours returns the stored run or the current practice state, and another body answers `idempotency_key_reused`. Refusals are not stored and are evaluated again.
The attempt flow does not serve practice items yet, so no practice exists in production; the assignment (`AssessmentPracticeRepository.Create`) is called by tests only. Retention of stored sources is not defined yet.
## Matching and explanations
Implemented as a deterministic search that never calls ML and reads precomputed vectors (see [Semantic layer and explanations](#semantic-layer-and-explanations)): `POST /api/v1/organizations/{org_id}/matches` (`SearchOrganizationMatches`, [api/README.en.md](../../api/README.en.md#match-search)). Code: the usecase `backend/internal/usecase/matching/` (`search.go` the flow, `filter.go` the filters, `score.go` the integer scoring, `explain.go` the explanation), the card list `candidateview.Service.ListVisibleCards`, the policy section `domain.MatchingPolicy` (`policy_matching.go`), the skill relations `domain.RelateEvidenceSkill` (`evidence_relation.go`), the handler `adapter/primary/http/v1/matches.go`, and the wiring `bootstrap/matches.go`. Visibility has no new query: the search reuses the barrier queries that the card and the evidence already use, so there is one copy of the visibility rule.