223 lines
10 KiB
Go
223 lines
10 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
assessmentResultSchemaVersion int32 = 1
|
|
assessmentResultRuleBasicPassMark = "demo_basic_pass_mark"
|
|
assessmentResultRuleTrainingOnly = "demonstration_only"
|
|
)
|
|
|
|
// AssessmentAttemptResultPins retain the private immutable source identities
|
|
// behind a candidate-visible result.
|
|
type AssessmentAttemptResultPins struct {
|
|
ProductPolicyVersionID PolicyVersionID `json:"product_policy_version_id"`
|
|
ProductPolicyVersion int32 `json:"product_policy_version"`
|
|
GradingPolicyVersionID PolicyVersionID `json:"grading_policy_version_id"`
|
|
OpeningID uuid.UUID `json:"opening_id"`
|
|
OpeningManifestHash [32]byte `json:"opening_manifest_sha256"`
|
|
TrainingBindingID uuid.UUID `json:"training_binding_id"`
|
|
TrainingManifestHash [32]byte `json:"training_manifest_sha256"`
|
|
BankVersion string `json:"bank_version"`
|
|
AlgorithmVersion string `json:"algorithm_version"`
|
|
}
|
|
|
|
// AssessmentAttemptDecisionRationale records the exact basic rule and policy
|
|
// threshold used by the server. It contains no answer keys or per-item data.
|
|
type AssessmentAttemptDecisionRationale struct {
|
|
Rule string `json:"rule"`
|
|
PassMarkBasisPoints int32 `json:"pass_mark_basis_points"`
|
|
PassPoints int32 `json:"pass_points"`
|
|
ScoredItems int32 `json:"scored_items"`
|
|
}
|
|
|
|
// AssessmentAttemptResultSnapshot is the immutable persisted result for one
|
|
// completed attempt. It is separate from mutable attempt progress and stores
|
|
// only an allowlisted public projection plus private version pins and rationale.
|
|
type AssessmentAttemptResultSnapshot struct {
|
|
SchemaVersion int32 `json:"schema_version"`
|
|
Attempt AssessmentAttemptPublicSnapshot `json:"attempt"`
|
|
Pins AssessmentAttemptResultPins `json:"pins"`
|
|
Outcome AssessmentAttemptOutcome `json:"outcome"`
|
|
NotDeterminedReason string `json:"not_determined_reason,omitempty"`
|
|
BasicScore *AssessmentBasicScore `json:"basic_score,omitempty"`
|
|
Rationale AssessmentAttemptDecisionRationale `json:"rationale"`
|
|
Limitations []string `json:"limitations"`
|
|
Summary string `json:"summary,omitempty"`
|
|
DecidedAt time.Time `json:"decided_at"`
|
|
}
|
|
|
|
// NewAssessmentAttemptResultSnapshot binds a finalized domain decision to the
|
|
// immutable attempt and its pinned product, bank, and grading versions.
|
|
func NewAssessmentAttemptResultSnapshot(
|
|
attempt AssessmentAttempt,
|
|
decision AssessmentAttemptDecision,
|
|
summary string,
|
|
limitations []string,
|
|
) (AssessmentAttemptResultSnapshot, error) {
|
|
const location = "domain.NewAssessmentAttemptResultSnapshot"
|
|
snapshot := attempt.Snapshot()
|
|
if attempt.IsZero() || snapshot.State != AssessmentAttemptStateCompleted || snapshot.EndedAt.IsZero() {
|
|
return AssessmentAttemptResultSnapshot{}, fmt.Errorf("%s - completed attempt is required: %w", location, ErrInvalidAssessmentAttempt)
|
|
}
|
|
result := AssessmentAttemptResultSnapshot{
|
|
SchemaVersion: assessmentResultSchemaVersion, Attempt: attempt.PublicSnapshot(),
|
|
Pins: assessmentResultPins(snapshot.Versions), Outcome: decision.Outcome,
|
|
BasicScore: clonePointer(decision.Score), Rationale: decision.Rationale,
|
|
Limitations: append([]string{}, limitations...), Summary: summary,
|
|
DecidedAt: canonicalAssessmentTime(snapshot.EndedAt),
|
|
}
|
|
if err := ValidateAssessmentAttemptResultSnapshot(result); err != nil {
|
|
return AssessmentAttemptResultSnapshot{}, fmt.Errorf("%s - validate result: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func assessmentResultPins(versions AssessmentAttemptVersions) AssessmentAttemptResultPins {
|
|
return AssessmentAttemptResultPins(versions)
|
|
}
|
|
|
|
// RestoreAssessmentAttemptResultSnapshot detaches slices and validates a
|
|
// stored result before it crosses the use-case boundary.
|
|
func RestoreAssessmentAttemptResultSnapshot(
|
|
result AssessmentAttemptResultSnapshot,
|
|
) (AssessmentAttemptResultSnapshot, error) {
|
|
public, err := RestoreAssessmentAttemptPublicSnapshot(result.Attempt)
|
|
if err != nil {
|
|
return AssessmentAttemptResultSnapshot{}, fmt.Errorf("domain.RestoreAssessmentAttemptResultSnapshot - restore public attempt: %w", err)
|
|
}
|
|
result.Attempt = public
|
|
result.BasicScore = clonePointer(result.BasicScore)
|
|
result.Limitations = append([]string{}, result.Limitations...)
|
|
result.DecidedAt = canonicalAssessmentTime(result.DecidedAt)
|
|
if err := ValidateAssessmentAttemptResultSnapshot(result); err != nil {
|
|
return AssessmentAttemptResultSnapshot{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ValidateAssessmentAttemptResultSnapshot checks stored pins, policy rationale,
|
|
// and the reproducible basic score without loading a current policy.
|
|
func ValidateAssessmentAttemptResultSnapshot(result AssessmentAttemptResultSnapshot) error {
|
|
const location = "domain.ValidateAssessmentAttemptResultSnapshot"
|
|
public, err := RestoreAssessmentAttemptPublicSnapshot(result.Attempt)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - public attempt projection is invalid: %w", location, ErrInvalidAssessmentAttempt)
|
|
}
|
|
if !validAssessmentResultIdentity(result, public) {
|
|
return fmt.Errorf("%s - result identity, versions, or timestamp are invalid: %w", location, ErrInvalidAssessmentAttempt)
|
|
}
|
|
if !validAssessmentResultLimitations(result.Limitations) {
|
|
return fmt.Errorf("%s - result limitations are not a bounded set: %w", location, ErrInvalidAssessmentAttempt)
|
|
}
|
|
if err := validateAssessmentResultKind(result, public.Kind); err != nil {
|
|
return fmt.Errorf("%s - result decision is invalid: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validAssessmentResultIdentity(result AssessmentAttemptResultSnapshot, public AssessmentAttemptPublicSnapshot) bool {
|
|
return result.SchemaVersion == assessmentResultSchemaVersion &&
|
|
public.State == AssessmentAttemptStateCompleted && public.FinishedAt != nil &&
|
|
!result.DecidedAt.IsZero() && public.FinishedAt.Equal(result.DecidedAt) &&
|
|
result.Pins.ProductPolicyVersionID != PolicyVersionID(uuid.Nil) &&
|
|
result.Pins.ProductPolicyVersion == public.PolicyVersion &&
|
|
result.Pins.BankVersion == public.BankVersion && result.Pins.AlgorithmVersion == public.AlgorithmVersion &&
|
|
assessmentVersionLabelPattern.MatchString(result.Pins.BankVersion) &&
|
|
assessmentVersionLabelPattern.MatchString(result.Pins.AlgorithmVersion)
|
|
}
|
|
|
|
func validAssessmentResultLimitations(limitations []string) bool {
|
|
if limitations == nil || len(limitations) > 10 {
|
|
return false
|
|
}
|
|
seen := make(map[string]struct{}, len(limitations))
|
|
for _, limitation := range limitations {
|
|
if limitation == "" || len(limitation) > 64 {
|
|
return false
|
|
}
|
|
if _, exists := seen[limitation]; exists {
|
|
return false
|
|
}
|
|
seen[limitation] = struct{}{}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func validateAssessmentResultKind(result AssessmentAttemptResultSnapshot, kind AssessmentAttemptKind) error {
|
|
switch kind {
|
|
case AssessmentAttemptKindRoot, AssessmentAttemptKindCorrective:
|
|
return validateBasicAssessmentResult(result)
|
|
case AssessmentAttemptKindDemoExpress:
|
|
return validateTrainingAssessmentResult(result)
|
|
default:
|
|
return fmt.Errorf("domain.validateAssessmentResultKind - result kind is invalid: %w", ErrInvalidAssessmentAttempt)
|
|
}
|
|
}
|
|
|
|
func validateBasicAssessmentResult(result AssessmentAttemptResultSnapshot) error {
|
|
if !validBasicResultPins(result.Pins) || !validBasicResultContent(result) {
|
|
return ErrInvalidAssessmentAttempt
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validBasicResultPins(pins AssessmentAttemptResultPins) bool {
|
|
return pins.GradingPolicyVersionID != PolicyVersionID(uuid.Nil) && pins.OpeningID != uuid.Nil &&
|
|
pins.OpeningManifestHash != [32]byte{} && pins.TrainingBindingID == uuid.Nil && pins.TrainingManifestHash == [32]byte{}
|
|
}
|
|
|
|
func validBasicResultContent(result AssessmentAttemptResultSnapshot) bool {
|
|
if result.Attempt.Mode != AssessmentAttemptModeDemoBasic ||
|
|
result.Outcome != AssessmentAttemptOutcomeConfirmed && result.Outcome != AssessmentAttemptOutcomeNotConfirmed ||
|
|
result.BasicScore == nil || result.NotDeterminedReason != "" || result.Summary != "" ||
|
|
result.Rationale.Rule != assessmentResultRuleBasicPassMark ||
|
|
result.Rationale.PassMarkBasisPoints < 1 || result.Rationale.PassMarkBasisPoints > int32(basisPointsPerUnit) {
|
|
return false
|
|
}
|
|
return result.Rationale.ScoredItems >= 1 && result.Rationale.ScoredItems <= result.Attempt.MaxItems &&
|
|
result.Rationale.ScoredItems == result.BasicScore.MaxPoints &&
|
|
result.Rationale.PassPoints == result.BasicScore.PassPoints &&
|
|
validStoredBasicDecision(result.Outcome, *result.BasicScore, result.Rationale)
|
|
}
|
|
|
|
func validateTrainingAssessmentResult(result AssessmentAttemptResultSnapshot) error {
|
|
if result.Attempt.Mode != AssessmentAttemptModeDemoExpress || !validTrainingResultPins(result.Pins) ||
|
|
result.Outcome != AssessmentAttemptOutcomeDemonstration || result.BasicScore != nil ||
|
|
result.NotDeterminedReason != "" || result.Summary == "" ||
|
|
result.Rationale != (AssessmentAttemptDecisionRationale{Rule: assessmentResultRuleTrainingOnly}) {
|
|
return ErrInvalidAssessmentAttempt
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validTrainingResultPins(pins AssessmentAttemptResultPins) bool {
|
|
return pins.GradingPolicyVersionID == PolicyVersionID(uuid.Nil) && pins.OpeningID == uuid.Nil &&
|
|
pins.OpeningManifestHash == [32]byte{} && pins.TrainingBindingID != uuid.Nil && pins.TrainingManifestHash != [32]byte{}
|
|
}
|
|
|
|
func validStoredBasicDecision(
|
|
outcome AssessmentAttemptOutcome,
|
|
score AssessmentBasicScore,
|
|
rationale AssessmentAttemptDecisionRationale,
|
|
) bool {
|
|
if score.MaxPoints < 1 || score.Points < 0 || score.Points > score.MaxPoints ||
|
|
score.PassPoints < 1 || score.PassPoints > score.MaxPoints {
|
|
return false
|
|
}
|
|
passPoints := (int64(rationale.ScoredItems)*int64(rationale.PassMarkBasisPoints) + int64(basisPointsPerUnit) - 1) / int64(basisPointsPerUnit)
|
|
if passPoints != int64(score.PassPoints) || int64(rationale.PassPoints) != passPoints {
|
|
return false
|
|
}
|
|
wantOutcome := AssessmentAttemptOutcomeNotConfirmed
|
|
if score.Points >= score.PassPoints {
|
|
wantOutcome = AssessmentAttemptOutcomeConfirmed
|
|
}
|
|
return outcome == wantOutcome
|
|
}
|