69 lines
2.5 KiB
Go
69 lines
2.5 KiB
Go
package assessment
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
// WatermarkSeeds derives the secret stream that marks the task text of one
|
|
// attempt. The seed is computed on the server from the attempt identifier and a
|
|
// server key and never leaves it.
|
|
type WatermarkSeeds interface {
|
|
AssessmentWatermarkSeed(attemptID uuid.UUID) domain.AssessmentWatermarkSeed
|
|
}
|
|
|
|
// Watermarker puts the attempt watermark on the task text of a response. The
|
|
// visible code is returned next to the text and the invisible mark is written
|
|
// into it, both from the seed of the attempt. The stored attempt, the
|
|
// idempotent responses, the reference answer, and the grading are not touched:
|
|
// the mark exists only in what is presented.
|
|
type Watermarker struct {
|
|
seeds WatermarkSeeds
|
|
}
|
|
|
|
// NewWatermarker fails at startup when the seed source is missing.
|
|
func NewWatermarker(seeds WatermarkSeeds) (*Watermarker, error) {
|
|
if seeds == nil {
|
|
return nil, errors.New("assessment.NewWatermarker - seeds must be set")
|
|
}
|
|
return &Watermarker{seeds: seeds}, nil
|
|
}
|
|
|
|
// Watermark returns the code and the marked statement for one attempt.
|
|
func (watermarker *Watermarker) Watermark(attemptID domain.AssessmentAttemptID) (domain.AssessmentWatermark, error) {
|
|
const location = "Watermarker.Watermark"
|
|
watermark, err := domain.NewAssessmentWatermark(watermarker.seeds.AssessmentWatermarkSeed(attemptID.UUID()))
|
|
if err != nil {
|
|
return domain.AssessmentWatermark{}, fmt.Errorf("%s - build watermark: %w", location, err)
|
|
}
|
|
return watermark, nil
|
|
}
|
|
|
|
// Present returns the snapshot as the candidate receives it. A real attempt,
|
|
// root or corrective, gets the visible code and the marked statement of its
|
|
// current item; a training run is returned unchanged. A nil Watermarker leaves
|
|
// every snapshot unchanged, so a deployment without it still serves attempts.
|
|
func (watermarker *Watermarker) Present(
|
|
snapshot domain.AssessmentAttemptPublicSnapshot,
|
|
) (domain.AssessmentAttemptPublicSnapshot, error) {
|
|
const location = "Watermarker.Present"
|
|
if watermarker == nil || !isRealAttemptKind(snapshot.Kind) {
|
|
return snapshot, nil
|
|
}
|
|
watermark, err := watermarker.Watermark(snapshot.ID)
|
|
if err != nil {
|
|
return domain.AssessmentAttemptPublicSnapshot{}, fmt.Errorf("%s - watermark attempt: %w", location, err)
|
|
}
|
|
snapshot.Watermark = &domain.AssessmentAttemptPublicWatermark{Code: watermark.Code().String()}
|
|
if snapshot.CurrentItem != nil {
|
|
item := *snapshot.CurrentItem
|
|
item.Prompt = watermark.Mark(item.Prompt)
|
|
snapshot.CurrentItem = &item
|
|
}
|
|
return snapshot, nil
|
|
}
|