Context
The worker already asks the model for free-text feedback on every graded project (GRADING_FEEDBACK, not charged). #129 turns that into a score: one proposal per rubric criterion, with a reason that cites the code, which the instructor approves or edits before the student sees a grade. ADR-0049 priced it at 10 credits "charged to the course budget, never to the student". There is no per-course credit budget in the code: the only per-course AI pool is the tutor allowance of a points course (ADR-0052), which counts student answers, not instructor work.
Decision
- Who pays: the course owner's AI quota, through
AiQuotaService.reservewith a newAiActionKind.PROJECT_REVIEW(free month first, then purchased credits; an ADMIN owner is exempt). The course is the budget's scope, the owner is its holder, so a course budget needs no new table and the instructor's wallet already shows it. The student is never charged. - Price and limits are the AppSetting
ai.project-review(creditsPerReview10,maxReviewsPerSubmission2,maxInputTokens60 000), edited at/admin/billing/planswith an audit row.reservegains an optionalcost, because this price is not inai.pricing. - Instructor-triggered, in the api.
POST …/submissions/:id/ai-reviewafter the worker's run has finished. The worker's run stays the grade; the review is an extra, paid step the instructor asks for, so nothing is spent on submissions nobody reviews. It runs in the api because the charge, the refund and the lock are the api's (AiQuotaService); the student's online path never triggers it (AGENTS.md rule 5). - Stored as a proposal.
ProjectReviewProposal(one row per review, linked to theCodeEvaluationRunit read): statusRUNNING → PROPOSED → APPROVED, orFAILEDwithBUDGET_EXHAUSTED | AI_UNAVAILABLE | INVALID_OUTPUT | INTERRUPTED. The charge is written on the row before the call, so a review cut off by a restart is closed and refunded on the next trigger. No student route reads the table; the grade the student sees is still only written byfinalizeGrading. - Only human-graded criteria (
MANUAL_INSTRUCTOR,AI_REVIEW) get a proposal; automated ones keep the runner's score (ADR-0038 teacher-first). - What the model reads is an allow-list: the stem, those criteria (title, description, scoring levels, maximum), the files the worker graded with numbered lines, and the count of public tests passed. Hidden test inputs, every expected output, the sample answer, the reference solution and expected SQL are collected and redacted from the prompt and from the reasons, as are credential-looking values. The prompt's SHA-256 is kept, the prompt is not.
- Validated output. A Zod schema keyed by the criteria asked for (
packages/shared); a missing or unknown criterion isinvalid_output. Points are clamped to[0, max](and the clamp shown), rounded to 0.25; a citation of a file or line that was not sent is dropped. - Failure falls back, never blocks. Budget refused, provider down, daily budget spent, AI switched off or malformed output: the proposal is
FAILEDwith a Vietnamese message, the charge is refunded (refund:<generationId>), and the deterministic result and manual review stay as they were. A refunded review does not count toward the cap. - Charged once. The trigger row-locks the
CodeSubmission, then: sameclientRequestId→ the first review; a reviewRUNNING→ that one; cap reached → 409PROJECT_REVIEW_LIMIT_REACHED. A partial unique index allows oneRUNNINGrow per submission. - Approval is per criterion.
POST …/ai-review/approveneeds a decision for every proposed criterion (ACCEPTEDkeeps the proposed points,EDITEDcarries the instructor's), refuses a proposal whose run is no longer the latest, claims the row (PROPOSED → APPROVED), then callsfinalizeGrading; if that fails the claim is undone.
Alternatives considered
- A per-course credit pool. A new table, a top-up flow and a way to fund it; nothing pays into it today. The owner's quota is the same money with no new moving parts.
- Propose automatically in the worker on every submission. Spends the owner's credits on submissions nobody opens, and the worker cannot refund through the api's quota service.
- Write the proposal into
CodeEvaluationRun.results. That JSON is what the attempt answer copies and student views read; a proposal there is one refactor away from leaking.
Consequences
Proven on Postgres by apps/api/test/integration/project-review.int-spec.ts: proposal stored and hidden from the student, approve and edit, provider failure refunded, the cap of 2, concurrent double triggers charging once. One review is one synchronous call of up to AI_TIMEOUT_MS with retries; the instructor waits for it. An instructor with no credits left gets the manual flow, not an error page.