feat(assessment): grade choice answers with distractors from error models

This commit is contained in:
shira committed 2026-10-11 03:12:24 +03:00
1 parent efa41d5967
commit f1147b1885
22 files changed
+1346 -47

No files matched your search

@@ -6,7 +6,9 @@ import (
"errors"
"fmt"
"io"
"maps"
"math"
"slices"
"strconv"
"strings"
"time"
@@ -39,6 +41,11 @@ type orderAssessmentAnswer struct {
ElementIDs []string `json:"element_ids"`
}
type choiceAssessmentAnswer struct {
Format string `json:"format"`
OptionIDs []string `json:"option_ids"`
}
// CanonicalAssessmentAnswer validates and canonicalizes a request answer
// before its idempotency fingerprint is created.
func CanonicalAssessmentAnswer(answer json.RawMessage) (json.RawMessage, error) {
@@ -60,6 +67,12 @@ func CanonicalAssessmentAnswer(answer json.RawMessage) (json.RawMessage, error)
return nil, fmt.Errorf("%s - order answer is invalid or exceeds its bound: %w", location, err)
}
return canonical, nil
case AnswerFormatChoice:
canonical, err := canonicalChoiceAssessmentAnswer(answer)
if err != nil {
return nil, fmt.Errorf("%s - choice answer is invalid or exceeds its bound: %w", location, err)
}
return canonical, nil
default:
return nil, fmt.Errorf("%s - answer format is not supported by the current bank: %w", location, ErrInvalidAssessmentAttempt)
}
@@ -112,6 +125,33 @@ func canonicalOrderAssessmentAnswer(answer []byte) ([]byte, error) {
return canonical, nil
}
// canonicalChoiceAssessmentAnswer accepts the selected option IDs as a set:
// two answers that select the same options in any order are the same answer,
// so the canonical form lists the IDs in sorted order.
func canonicalChoiceAssessmentAnswer(answer []byte) ([]byte, error) {
var submitted choiceAssessmentAnswer
if err := decodeStrictAssessmentAnswer(answer, &submitted); err != nil ||
len(submitted.OptionIDs) < 1 || len(submitted.OptionIDs) > maxChoiceOptions {
return nil, ErrInvalidAssessmentAttempt
}
seen := make(map[string]struct{}, len(submitted.OptionIDs))
for _, optionID := range submitted.OptionIDs {
if !assessmentChoiceOptionIDPattern.MatchString(optionID) {
return nil, ErrInvalidAssessmentAttempt
}
if _, exists := seen[optionID]; exists {
return nil, ErrInvalidAssessmentAttempt
}
seen[optionID] = struct{}{}
}
sorted := slices.Sorted(maps.Keys(seen))
canonical, err := json.Marshal(choiceAssessmentAnswer{Format: AnswerFormatChoice, OptionIDs: sorted})
if err != nil || len(canonical) > maxAssessmentAttemptAnswerBytes {
return nil, ErrInvalidAssessmentAttempt
}
return canonical, nil
}
func validateAssessmentOrderElements(elementIDs []string) error {
seen := make(map[string]struct{}, len(elementIDs))
for _, elementID := range elementIDs {
@@ -148,11 +188,36 @@ func CanonicalAssessmentAnswerForVariant(answer json.RawMessage, variant Assessm
return nil, false, fmt.Errorf("%s - compare frozen order key: %w", location, err)
}
return canonical, correct, nil
case AnswerFormatChoice:
correct, err := assessmentChoiceAnswerIsCorrect(canonical, variant)
if err != nil {
return nil, false, fmt.Errorf("%s - compare frozen choice key: %w", location, err)
}
return canonical, correct, nil
default:
return nil, false, fmt.Errorf("%s - frozen answer format is unsupported: %w", location, ErrInvalidAssessmentAttempt)
}
}
// assessmentChoiceAnswerIsCorrect grades a canonical choice answer against the
// frozen options and key of the variant. A variant has one correct option and
// accepts one selected option; an ID that names no option of the variant is an
// invalid answer, not a wrong one.
func assessmentChoiceAnswerIsCorrect(canonical []byte, variant AssessmentVariant) (bool, error) {
var submitted choiceAssessmentAnswer
if err := decodeStrictAssessmentAnswer(canonical, &submitted); err != nil || submitted.Format != AnswerFormatChoice ||
len(submitted.OptionIDs) != 1 || !validFrozenChoiceVariant(variant) {
return false, ErrInvalidAssessmentAttempt
}
correctIndex, _ := choiceCorrectIndex(variant)
for index, option := range variant.Options {
if option.ID == submitted.OptionIDs[0] {
return index == correctIndex, nil
}
}
return false, ErrInvalidAssessmentAttempt
}
func assessmentIntegerAnswerIsCorrect(canonical []byte, referenceAnswer string) (bool, error) {
var submitted numberAssessmentAnswer
if err := decodeStrictAssessmentAnswer(canonical, &submitted); err != nil || submitted.Format != "number" || len(submitted.Number) == 0 {