feat(assessment): watermark the task text of real attempts

This commit is contained in:
shira committed 2026-10-11 10:44:30 +03:00
1 parent 24b64d698a
commit c975a0d506
57 files changed
+2429 -92

No files matched your search

@@ -18,8 +18,10 @@ import (
// every candidate acknowledges the new wording. Acknowledgements are stored per
// version, so one given for an earlier version never counts for this one.
//
// Version 2 added the attempt measures to the text; version 1 listed signals only.
const CurrentSignalNoticeVersion int32 = 2
// Version 3 describes the watermark of the task screen and text, which version
// 2 announced as planned; version 2 added the attempt measures to the text;
// version 1 listed signals only.
const CurrentSignalNoticeVersion int32 = 3
// ErrSignalNoticeVersionMismatch reports an acknowledgement for a notice
// version other than the one the server requires.
@@ -0,0 +1,68 @@
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
}
@@ -0,0 +1,95 @@
package assessment
import (
"crypto/sha256"
"strings"
"testing"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
// hashedSeeds stands in for the keyring: a seed that follows the attempt.
type hashedSeeds struct{}
func (hashedSeeds) AssessmentWatermarkSeed(attemptID uuid.UUID) domain.AssessmentWatermarkSeed {
return sha256.Sum256(append([]byte("test-watermark:"), attemptID[:]...))
}
const watermarkTestPrompt = "A cache holds at most 3 entries. A read of a missing key is not a hit. How many reads are hits?"
func presentedSnapshot(kind domain.AssessmentAttemptKind) domain.AssessmentAttemptPublicSnapshot {
return domain.AssessmentAttemptPublicSnapshot{
ID: domain.AssessmentAttemptID(uuid.New()), Kind: kind,
CurrentItem: &domain.AssessmentAttemptPublicItem{
ID: uuid.New(), Sequence: 1, Prompt: watermarkTestPrompt,
Content: domain.AssessmentAttemptPublicItemContent{Format: "number"},
},
}
}
func TestPresentMarksRealAttemptsOnlyAndLeavesTheStoredSnapshotAlone(t *testing.T) {
watermarker, err := NewWatermarker(hashedSeeds{})
if err != nil {
t.Fatalf("NewWatermarker() error = %v", err)
}
for _, kind := range []domain.AssessmentAttemptKind{domain.AssessmentAttemptKindRoot, domain.AssessmentAttemptKindCorrective} {
stored := presentedSnapshot(kind)
presented, err := watermarker.Present(stored)
if err != nil {
t.Fatalf("Present(%s) error = %v", kind, err)
}
if presented.Watermark == nil || len(presented.Watermark.Code) != 11 {
t.Fatalf("Present(%s) watermark = %+v, want the visible code", kind, presented.Watermark)
}
if presented.CurrentItem.Prompt == watermarkTestPrompt || domain.CanonicalAssessmentText(presented.CurrentItem.Prompt) != domain.CanonicalAssessmentText(watermarkTestPrompt) {
t.Fatalf("Present(%s) prompt = %q, want the same statement with a mark", kind, presented.CurrentItem.Prompt)
}
if stored.CurrentItem.Prompt != watermarkTestPrompt || stored.Watermark != nil {
t.Fatalf("Present(%s) changed the stored snapshot", kind)
}
if again, _ := watermarker.Present(stored); again.CurrentItem.Prompt != presented.CurrentItem.Prompt || again.Watermark.Code != presented.Watermark.Code {
t.Fatalf("Present(%s) is not repeatable", kind)
}
}
training := presentedSnapshot(domain.AssessmentAttemptKindDemoExpress)
if got, err := watermarker.Present(training); err != nil || got.Watermark != nil || got.CurrentItem.Prompt != watermarkTestPrompt {
t.Fatalf("Present(training) = %+v, %v, want the unchanged snapshot", got, err)
}
}
func TestTwoAttemptsGetTheSameStatementWithTheirOwnMark(t *testing.T) {
watermarker, err := NewWatermarker(hashedSeeds{})
if err != nil {
t.Fatalf("NewWatermarker() error = %v", err)
}
first, _ := watermarker.Present(presentedSnapshot(domain.AssessmentAttemptKindRoot))
second, _ := watermarker.Present(presentedSnapshot(domain.AssessmentAttemptKindRoot))
if first.Watermark.Code == second.Watermark.Code || first.CurrentItem.Prompt == second.CurrentItem.Prompt {
t.Fatal("two attempts got the same code or the same marked statement")
}
if domain.CanonicalAssessmentText(first.CurrentItem.Prompt) != domain.CanonicalAssessmentText(second.CurrentItem.Prompt) {
t.Fatal("the statements of two attempts differ beyond the mark")
}
// The code on the screen is the code of the invisible mark.
codes := domain.ExtractAssessmentWatermarkCodes(first.CurrentItem.Prompt)
if len(codes) == 0 || codes[0].String() != first.Watermark.Code {
t.Fatalf("invisible mark = %v, want the visible code %s", codes, first.Watermark.Code)
}
if strings.ContainsAny(first.Watermark.Code, "@ ") {
t.Fatalf("visible code %q holds more than a code", first.Watermark.Code)
}
}
func TestAnUnsetWatermarkerLeavesSnapshotsAsTheyAre(t *testing.T) {
var watermarker *Watermarker
stored := presentedSnapshot(domain.AssessmentAttemptKindRoot)
got, err := watermarker.Present(stored)
if err != nil || got.Watermark != nil || got.CurrentItem.Prompt != watermarkTestPrompt {
t.Fatalf("Present() on a nil Watermarker = %+v, %v, want the unchanged snapshot", got, err)
}
if _, err := NewWatermarker(nil); err == nil {
t.Fatal("NewWatermarker(nil) succeeded, want an error")
}
}