feat(practice): add the code runner port and its TLS client

This commit is contained in:
shira committed 2026-10-11 09:48:44 +03:00
1 parent 53a125f2b5
commit facc1789b1
24 files changed
+3374 -6

No files matched your search

+17 -2
View File
@@ -573,7 +573,7 @@ backend/
usecase/files/ shared private file layer: policies, opaque keys, stream limits, checksums
usecase/notification/ mail requests and the consumer that stores them as deliveries, the quota gate, the service mail enqueue (`ServiceMailer`), the senders of service mail (`ServiceMailSender`) and of product notifications (`NewNotificationSender`), and their rights checks
usecase/notification/mailtemplate/ pure rendering of versioned service emails; the delivery consumer renders notifications with it
port/outbound/ readiness, cache, object storage, transaction, outbox, outbox relay, event publishing, event consumption, identity, consent ledger, organization and membership, policy, password hashing, and mail boundaries
port/outbound/ readiness, cache, object storage, transaction, outbox, outbox relay, event publishing, event consumption, identity, consent ledger, organization and membership, policy, password hashing, mail, and code runner boundaries
adapter/primary/http/
api/ generated ogen handler
middleware/ request log and request metrics by route template
@@ -588,7 +588,9 @@ backend/
mail/resend/ mail transport that sends one request to the Resend HTTP API per message
mail/resend/resendfixture/ local stand-in for the Resend API, for tests only; production never imports it
ml/api/ generated client from backend/codegen/ogen-ml.yml
code_runner/ client of the code runner: TLS 1.3 transport pinned to the configured CA, opaque idempotent run IDs, request and response validation
code_runner/api/ generated client from backend/codegen/ogen-code-runner.yml
code_runner/disabled/ code runner that runs nothing and reports itself unavailable
postgres/ pool, migrator, transactor, outbox writer and relay store, event consumer store, identity, consent ledger, organization, membership, and policy repositories, health check
postgres/sqlc/ generated SQL adapter
redis/ cache adapter and Redis check
@@ -746,10 +748,23 @@ 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; calls from the backend and the run queue; 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 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.
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).
- 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.
- Opaque idempotent run ID. The run ID is HMAC-SHA256 over the attempt item ID, the submission ID, and a generation counter, keyed with `CODE_RUNNER_RUN_KEY` and domain-separated, formatted as a UUID. A retry of the same submission therefore reuses the run and the runner starts it once, while the runner cannot link an ID to an attempt or a submission and nobody can guess the ID of another submission. The key differs from the service token, which the runner knows. After a run the runner could not evaluate, the caller raises the generation: the runner keeps the stored failure under the old ID. The same key with another payload is refused (`ErrCodeRunKeyReused`).
- Infrastructure failure is never a verdict. `finished` always holds a validated result with the outcomes `accepted`, `wrong_answer`, `compile_error`, `time_limit_exceeded`, `memory_limit_exceeded`, `output_limit_exceeded`, and `runtime_error`, per group and test. Everything else is an error: `ErrCodeRunnerUnavailable` is the umbrella for a disabled runner, an unreachable one, a timeout, a failed TLS check, a rejected token (`ErrCodeRunnerUnauthorized`, HTTP 401), a server error, an answer outside the contract (`ErrCodeRunnerBadResponse`), and a run that finished with `infrastructure_failure` (`ErrCodeRunnerInfrastructureFailure`). With such an error the returned status is empty. A caller must report the practice as unavailable and keep the submission ungraded; it never scores, fails, or accepts it. A request the runner rejects as invalid (400, 413) is `ErrInvalidCodeRunRequest`, a bug of the task content, not of the candidate.
- Response validation. The generated client already checks the schema. The adapter adds the rules the schema cannot express: the run ID equals the one derived, a `running` run has no result, a 202 answer is `running`, the groups and tests of the result are the requested ones in order with the requested counts, a group stops at its first failed test, `passed_tests` equals the accepted tests, and the group, compile, and run outcomes agree with each other (the run outcome is `compile_error` or the outcome of the first failed group). A hidden test never passes on a diagnostic, even if a faulty runner sends one. A body above 4 MiB fails.
- Transport. TLS 1.3 only, the runner certificate is verified against the configured CA alone (the system roots are not trusted; there is no switch that skips verification), the optional `CODE_RUNNER_SERVER_NAME` changes only the name checked, redirects and environment proxies are not followed, one request is limited by `CODE_RUNNER_REQUEST_TIMEOUT`, and the token is sent only in the `Authorization` header and appears in no error or log. Error texts name the failure class, never the URL or the response body.
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.
## Matching and explanations
Implemented as a deterministic search without ML: `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`. There is no new table, migration, or query file: the search reuses the barrier queries that the card and the evidence already use, so there is one copy of the visibility rule.