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.
+12
View File
@@ -642,6 +642,18 @@ On the runner host the suite runs from a bundle: `make -C runner a16-bundle` (`A
What the tests do not prove: isolation in the production VM until the suite has passed there; protection against kernel, nsjail, or cgroup vulnerabilities; the absence of side channels (timing, CPU caches); or behavior under load.
## Code practice tests
`TestAssessmentPracticeSubmissionStorage` in `backend/integration_test/assessment_practice_http_test.go` uses `bootstrap(t, postgresService, redisService)`, an isolated migrated database, the real router, the practice usecase, and the PostgreSQL repository. No flow serves practice items yet, so the test moves a real attempt to `practice_pending` and creates its main and additional practices directly; the synthetic task marks every hidden test text. The sequential cases are:
- A debug run answers 202 with `Cache-Control: no-store`, is stored with exactly one queued job, and a repeat with the same key and body returns the same run without a new row; the same key with another body answers 422. Another candidate, another attempt, and an unknown run read as 404.
- Refusals store nothing: a practice of another candidate (404), the additional practice while the attempt is at the main one (409), and a source over the bound of the task (400).
- A stored result that also holds a hidden group is shown with the public tests only; the response has only the contract fields and no hidden text, source, or test text.
- A final solution marks the practice `pending` and is stored with one job; a replay returns it, the same solution under a new key stores nothing, another solution conflicts, and a reused key with another body is refused. The database refuses to replace the source or the language of the stored solution, to delete it, and to write a second result. After a sandbox failure the practice accepts another solution, and the first one stays as it was.
- With the runner disabled a run is refused as unavailable and nothing is stored. At the end every stored submission has exactly one job.
`backend/internal/usecase/practice/request_test.go` checks that the runner request of a debug run holds only the public examples and the candidate's tests, and that of a final solution exactly the three hidden groups. The tests do not run code: the worker of the run queue is planned.
## Semantic matching tests
- ML service (`make -C ml test`, no model, no network): `tests/test_embeddings.py` checks the `POST /v1/embeddings` contract on the deterministic hash encoder: response order, normalization, equal vectors for two equal requests, micro-batches by length, 503 `service_unavailable` before readiness and ready only after the warm-up, 400 for too many texts, a length, and unknown fields, 413 for a body over the limit (with Content-Length and streamed), and no request text in the logs; `tests/test_model_files.py` checks the lock file (only safetensors and configs, a full revision, no code or pickle) and the size and SHA-256 verification.