Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0054

AI project review: a proposal the instructor approves, charged to the course owner's quota

Trạng thái
accepted; the price is in points since ADR-0057 (60 points, or one free AI use). The price (10 credits), the cap (2 reviews) and the 60k-token input ceiling
Ngày
2026-11-22
Người quyết định
SmartLearn team (#129)

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.reserve with a new AiActionKind.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 (creditsPerReview 10, maxReviewsPerSubmission 2, maxInputTokens 60 000), edited at /admin/billing/plans with an audit row. reserve gains an optional cost, because this price is not in ai.pricing.
  • Instructor-triggered, in the api. POST …/submissions/:id/ai-review after 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 the CodeEvaluationRun it read): status RUNNING → PROPOSED → APPROVED, or FAILED with BUDGET_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 by finalizeGrading.
  • 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 is invalid_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 FAILED with 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: same clientRequestId → the first review; a review RUNNING → that one; cap reached → 409 PROJECT_REVIEW_LIMIT_REACHED. A partial unique index allows one RUNNING row per submission.
  • Approval is per criterion. POST …/ai-review/approve needs a decision for every proposed criterion (ACCEPTED keeps the proposed points, EDITED carries the instructor's), refuses a proposal whose run is no longer the latest, claims the row (PROPOSED → APPROVED), then calls finalizeGrading; 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.