feat(assessment): add table families that pick a variant row by seed

This commit is contained in:
shira committed 2026-10-11 08:19:17 +03:00
1 parent f1147b1885
commit 5e1db37746
16 files changed
+1023 -56

No files matched your search

+97 -25
View File
@@ -239,7 +239,7 @@ func ParseAssessmentLifecycle(value string) (AssessmentLifecycle, error) {
type AssessmentAnswerKey struct {
// AnswerFormat is the format of the reference answer: "integer",
// "integer_sequence", or "choice". It must match the algorithm of the
// generator.
// generator; a table generator takes "integer" or "choice".
AnswerFormat string `json:"answer_format"`
// ReferenceSolution describes the reference approach in prose.
ReferenceSolution string `json:"reference_solution"`
@@ -289,9 +289,11 @@ func (rubric AssessmentRubric) validate(location string) error {
return nil
}
func (key AssessmentAnswerKey) validate(location string, algorithm assessmentAlgorithm) error {
if key.AnswerFormat != algorithm.answerFormat {
return invalidAssessmentFamily(location, "answer format does not match the answer format of the generator algorithm")
// validate checks the answer key against the answer format that the
// generator produces.
func (key AssessmentAnswerKey) validate(location, generatorFormat string) error {
if key.AnswerFormat != generatorFormat {
return invalidAssessmentFamily(location, "answer format does not match the answer format of the generator")
}
return checkAssessmentText(location, "reference solution", key.ReferenceSolution)
}
@@ -379,16 +381,33 @@ func (core AssessmentFamilyCore) validate(location string, probe bool) error {
if err := core.validateReview(location); err != nil {
return err
}
algorithm, err := core.Generator.validate(location, probe)
generatorFormat, err := core.validateGenerator(location, probe)
if err != nil {
return err
}
if err := core.AnswerKey.validate(location, algorithm); err != nil {
if err := core.AnswerKey.validate(location, generatorFormat); err != nil {
return err
}
return core.Rubric.validate(location)
}
// validateGenerator validates the generator of either kind and returns the
// answer format that it produces. A table generator produces the format its
// answer key declares, after the table has been checked against it.
func (core AssessmentFamilyCore) validateGenerator(location string, probe bool) (string, error) {
if core.Generator.Table != nil {
if err := core.Generator.validateTable(location, core.AnswerKey.AnswerFormat, probe); err != nil {
return "", err
}
return core.AnswerKey.AnswerFormat, nil
}
algorithm, err := core.Generator.validate(location, probe)
if err != nil {
return "", err
}
return algorithm.answerFormat, nil
}
func (core AssessmentFamilyCore) validateReview(location string) error {
if _, err := ParseAssessmentReviewStatus(string(core.ReviewStatus)); err != nil {
return fmt.Errorf("%s - validate review status: %w", location, err)
@@ -408,25 +427,58 @@ func (core AssessmentFamilyCore) validateReview(location string) error {
return nil
}
// currentAssessmentHashScheme is the scheme that new versions are stored
// with. A stored row keeps its own scheme in the hash_scheme column, and
// RestoreAssessmentFamily verifies it with that scheme's function, so a later
// scheme verifies new rows without invalidating the old ones. A change of the
// canonical layout, of a field, or of the encoding adds a scheme here and to
// the assessment_families_hash_scheme_known CHECK; it never edits the
// function of an existing scheme.
const currentAssessmentHashScheme int16 = 1
// The hash schemes of family content. A stored row keeps its own scheme in
// the hash_scheme column, and RestoreAssessmentFamily verifies it with that
// scheme's function, so a later scheme verifies new rows without invalidating
// the old ones. A change of the canonical layout, of a field, or of the
// encoding adds a scheme here and to the assessment_families_hash_scheme_known
// CHECK; it never edits the function of an existing scheme.
//
// Scheme 1 hashes the layout of an algorithmic generator. Scheme 2 is the
// layout of a table generator, whose canonical content holds the table instead
// of an algorithm and its parameters. A new version gets the scheme of its
// generator kind, so the content of an algorithmic family keeps the hash it
// had before table generators existed, and importing it again is still
// recognized as the same version.
const (
assessmentHashSchemeAlgorithmic int16 = 1
assessmentHashSchemeTable int16 = 2
)
// hashScheme is the scheme that a new version of this content is stored with.
func (core AssessmentFamilyCore) hashScheme() int16 {
if core.Generator.Table != nil {
return assessmentHashSchemeTable
}
return assessmentHashSchemeAlgorithmic
}
// assessmentHashSchemes maps a scheme number to its hash function. It is a
// variable only so that a test can register a scheme.
var assessmentHashSchemes = map[int16]func(AssessmentFamilyCore) (AssessmentContentHash, error){
1: contentHashV1,
assessmentHashSchemeAlgorithmic: contentHashV1,
assessmentHashSchemeTable: contentHashV2,
}
// assessmentCanonicalContentV1 is the form that scheme 1 hashes. The bundle
// name, the ID, and the lifecycle are not part of it: they are provenance and
// state, not content.
type assessmentCanonicalContentV1 struct {
// assessmentCanonicalGeneratorV1 is the generator layout of scheme 1. It is a
// type of its own, so that a later field of AssessmentSeedGenerator cannot
// change what scheme 1 hashes.
type assessmentCanonicalGeneratorV1 struct {
Algorithm string `json:"algorithm"`
Parameters map[string]AssessmentParameterRange `json:"parameters"`
StatementTemplate string `json:"statement_template"`
}
// assessmentCanonicalGeneratorV2 is the generator layout of scheme 2.
type assessmentCanonicalGeneratorV2 struct {
StatementTemplate string `json:"statement_template"`
Table *AssessmentVariantTable `json:"table"`
}
// assessmentCanonicalContent is the form that the schemes hash, with the
// generator layout of the scheme. The bundle name, the ID, and the lifecycle
// are not part of it: they are provenance and state, not content.
type assessmentCanonicalContent[Generator any] struct {
Key AssessmentFamilyKey `json:"family_key"`
Version int32 `json:"version"`
Category AssessmentCategory `json:"category"`
@@ -435,7 +487,7 @@ type assessmentCanonicalContentV1 struct {
Author AssessmentAuthor `json:"author"`
Reviewer AssessmentOperator `json:"reviewer,omitempty"`
ReviewStatus AssessmentReviewStatus `json:"review_status"`
Generator AssessmentSeedGenerator `json:"seed_generator"`
Generator Generator `json:"seed_generator"`
AnswerKey AssessmentAnswerKey `json:"answer_key"`
Rubric AssessmentRubric `json:"rubric"`
}
@@ -454,13 +506,32 @@ func (core AssessmentFamilyCore) contentHash(scheme int16) (AssessmentContentHas
}
func contentHashV1(core AssessmentFamilyCore) (AssessmentContentHash, error) {
encoded, err := json.Marshal(assessmentCanonicalContentV1{
if core.Generator.Table != nil {
return AssessmentContentHash{}, invalidAssessmentFamily("domain.contentHashV1", "scheme 1 does not cover a table generator")
}
return hashCanonicalContent("domain.contentHashV1", core, assessmentCanonicalGeneratorV1{
Algorithm: core.Generator.Algorithm, Parameters: core.Generator.Parameters,
StatementTemplate: core.Generator.StatementTemplate,
})
}
func contentHashV2(core AssessmentFamilyCore) (AssessmentContentHash, error) {
if core.Generator.Table == nil || core.Generator.Algorithm != "" || len(core.Generator.Parameters) != 0 {
return AssessmentContentHash{}, invalidAssessmentFamily("domain.contentHashV2", "scheme 2 covers only a table generator")
}
return hashCanonicalContent("domain.contentHashV2", core, assessmentCanonicalGeneratorV2{
StatementTemplate: core.Generator.StatementTemplate, Table: core.Generator.Table,
})
}
func hashCanonicalContent[Generator any](location string, core AssessmentFamilyCore, generator Generator) (AssessmentContentHash, error) {
encoded, err := json.Marshal(assessmentCanonicalContent[Generator]{
Key: core.Key, Version: core.Version, Category: core.Category, Specialization: core.Specialization,
Title: core.Title, Author: core.Author, Reviewer: core.Reviewer, ReviewStatus: core.ReviewStatus,
Generator: core.Generator, AnswerKey: core.AnswerKey, Rubric: core.Rubric,
Generator: generator, AnswerKey: core.AnswerKey, Rubric: core.Rubric,
})
if err != nil {
return AssessmentContentHash{}, fmt.Errorf("domain.contentHashV1 - encode canonical content: %w", err)
return AssessmentContentHash{}, fmt.Errorf("%s - encode canonical content: %w", location, err)
}
return sha256.Sum256(encoded), nil
}
@@ -483,7 +554,8 @@ func NewAssessmentFamilyDraft(input NewAssessmentFamilyDraftInput) (AssessmentFa
if err := core.validate(location, true); err != nil {
return AssessmentFamilyDraft{}, err
}
hash, err := core.contentHash(currentAssessmentHashScheme)
scheme := core.hashScheme()
hash, err := core.contentHash(scheme)
if err != nil {
return AssessmentFamilyDraft{}, err
}
@@ -491,7 +563,7 @@ func NewAssessmentFamilyDraft(input NewAssessmentFamilyDraftInput) (AssessmentFa
if err != nil {
return AssessmentFamilyDraft{}, fmt.Errorf("%s - assign ID: %w", location, err)
}
return AssessmentFamilyDraft{id: id, core: core, hash: hash, scheme: currentAssessmentHashScheme}, nil
return AssessmentFamilyDraft{id: id, core: core, hash: hash, scheme: scheme}, nil
}
func (draft AssessmentFamilyDraft) ID() AssessmentFamilyID { return draft.id }