feat(assessment): find the attempt behind a leaked task text

This commit is contained in:
shira committed 2026-10-11 13:19:38 +03:00
1 parent bb917bf62e
commit 5a731c25b7
26 files changed
+2500

No files matched your search

@@ -0,0 +1,109 @@
package domain
import (
"errors"
"fmt"
"time"
"github.com/google/uuid"
)
// ErrInvalidAssessmentLeakTrace reports a malformed leak trace record.
var ErrInvalidAssessmentLeakTrace = errors.New("invalid assessment leak trace")
// AssessmentLeakTraceStatus is how a search for the source of a leaked text
// ended. Not found is a normal outcome, and ambiguous means that the data does
// not tell several attempts apart; neither names an attempt.
type AssessmentLeakTraceStatus string
const (
AssessmentLeakTraceFound AssessmentLeakTraceStatus = "found"
AssessmentLeakTraceAmbiguous AssessmentLeakTraceStatus = "ambiguous"
AssessmentLeakTraceNotFound AssessmentLeakTraceStatus = "not_found"
)
// IsKnown reports whether the status is one of the stored values.
func (status AssessmentLeakTraceStatus) IsKnown() bool {
switch status {
case AssessmentLeakTraceFound, AssessmentLeakTraceAmbiguous, AssessmentLeakTraceNotFound:
return true
default:
return false
}
}
// AssessmentLeakTraceMethod names the evidence that decided a search.
type AssessmentLeakTraceMethod string
const (
// AssessmentLeakTraceInvisibleMark is the code that the zero-width mark of the
// statement carries. It is keyed, so it identifies the attempt on its own.
AssessmentLeakTraceInvisibleMark AssessmentLeakTraceMethod = "invisible_mark"
// AssessmentLeakTraceVisibleCode is the code of the faint tile, read from the
// text of a screenshot. The code is keyed, so it identifies the attempt on
// its own; a single wrong character of the recognized text is tolerated.
AssessmentLeakTraceVisibleCode AssessmentLeakTraceMethod = "visible_code"
// AssessmentLeakTraceWording is the choice of interchangeable phrases among
// the attempts that were served the same statement text.
AssessmentLeakTraceWording AssessmentLeakTraceMethod = "wording"
// AssessmentLeakTraceVariant is a statement text that only one attempt was
// ever served. It follows from the seed of the variant, not from a mark.
AssessmentLeakTraceVariant AssessmentLeakTraceMethod = "variant"
)
// IsKnown reports whether the method is one of the stored values.
func (method AssessmentLeakTraceMethod) IsKnown() bool {
switch method {
case AssessmentLeakTraceInvisibleMark, AssessmentLeakTraceVisibleCode,
AssessmentLeakTraceWording, AssessmentLeakTraceVariant:
return true
default:
return false
}
}
// AssessmentLeakTraceRecord is the audit of one search. It holds no leaked
// text, only a keyed digest of it and its length. Whatever the outcome, the
// record exists before a result is shown, and it names an attempt only when the
// search found one. It is not shown to an employer and changes no result,
// category, or quota.
type AssessmentLeakTraceRecord struct {
ID uuid.UUID
Actor AssessmentReviewActor
SearchedAt time.Time
// TextDigest is a keyed digest of the normalized text, so the same text
// searched twice is visible in the audit without keeping it.
TextDigest AssessmentDigest
KeyVersion int16
TextRunes int32
Status AssessmentLeakTraceStatus
Method AssessmentLeakTraceMethod
AttemptID AssessmentAttemptID
// Candidates is the number of attempts that the evidence left in play.
Candidates int32
// EvidenceBits is the strength of the deciding evidence, an upper bound on
// how rarely another attempt would match it by chance (2^-bits).
EvidenceBits int32
}
// Validate checks that the record can be stored.
func (record AssessmentLeakTraceRecord) Validate() error {
const location = "AssessmentLeakTraceRecord.Validate"
switch {
case record.ID == uuid.Nil, record.Actor == "", record.SearchedAt.IsZero():
return fmt.Errorf("%s - identifier, actor, and time are required: %w", location, ErrInvalidAssessmentLeakTrace)
case record.TextDigest.IsZero(), record.KeyVersion < 1, record.TextRunes < 0:
return fmt.Errorf("%s - the text digest, its key version, and a length are required: %w", location, ErrInvalidAssessmentLeakTrace)
case !record.Status.IsKnown():
return fmt.Errorf("%s - status is unknown: %w", location, ErrInvalidAssessmentLeakTrace)
case record.Candidates < 0, record.EvidenceBits < 0:
return fmt.Errorf("%s - counts cannot be negative: %w", location, ErrInvalidAssessmentLeakTrace)
case record.Status == AssessmentLeakTraceFound && (record.AttemptID.IsZero() || !record.Method.IsKnown()):
return fmt.Errorf("%s - a found search names its attempt and its method: %w", location, ErrInvalidAssessmentLeakTrace)
case record.Status != AssessmentLeakTraceFound && !record.AttemptID.IsZero():
return fmt.Errorf("%s - only a found search names an attempt: %w", location, ErrInvalidAssessmentLeakTrace)
case record.Method != "" && !record.Method.IsKnown():
return fmt.Errorf("%s - method is unknown: %w", location, ErrInvalidAssessmentLeakTrace)
}
return nil
}
@@ -0,0 +1,57 @@
package domain
import (
"errors"
"testing"
"time"
"github.com/google/uuid"
)
func validLeakTraceRecord() AssessmentLeakTraceRecord {
return AssessmentLeakTraceRecord{
ID: uuid.New(), Actor: "operator:night", SearchedAt: time.Date(2026, time.October, 11, 9, 0, 0, 0, time.UTC),
TextDigest: AssessmentDigest{1}, KeyVersion: 1, TextRunes: 120,
Status: AssessmentLeakTraceNotFound, Candidates: 0,
}
}
func TestLeakTraceRecordNamesAnAttemptOnlyWhenItFoundOne(t *testing.T) {
record := validLeakTraceRecord()
if err := record.Validate(); err != nil {
t.Fatalf("Validate(not found) error = %v", err)
}
record.Status, record.Candidates = AssessmentLeakTraceAmbiguous, 3
if err := record.Validate(); err != nil {
t.Fatalf("Validate(ambiguous) error = %v", err)
}
found := validLeakTraceRecord()
found.Status, found.Method, found.AttemptID, found.Candidates, found.EvidenceBits = AssessmentLeakTraceFound, AssessmentLeakTraceVisibleCode, AssessmentAttemptID(uuid.New()), 1, 50
if err := found.Validate(); err != nil {
t.Fatalf("Validate(found) error = %v", err)
}
for name, change := range map[string]func(*AssessmentLeakTraceRecord){
"found without an attempt": func(record *AssessmentLeakTraceRecord) { record.AttemptID = AssessmentAttemptID{} },
"found without a method": func(record *AssessmentLeakTraceRecord) { record.Method = "" },
"found with an unknown method": func(record *AssessmentLeakTraceRecord) { record.Method = "psychic" },
"unknown status": func(record *AssessmentLeakTraceRecord) { record.Status = "maybe" },
"no actor": func(record *AssessmentLeakTraceRecord) { record.Actor = "" },
"no digest": func(record *AssessmentLeakTraceRecord) { record.TextDigest = AssessmentDigest{} },
"no key version": func(record *AssessmentLeakTraceRecord) { record.KeyVersion = 0 },
"negative candidates": func(record *AssessmentLeakTraceRecord) { record.Candidates = -1 },
"no time": func(record *AssessmentLeakTraceRecord) { record.SearchedAt = time.Time{} },
} {
t.Run(name, func(t *testing.T) {
record := found
change(&record)
if err := record.Validate(); !errors.Is(err, ErrInvalidAssessmentLeakTrace) {
t.Fatalf("Validate() error = %v, want ErrInvalidAssessmentLeakTrace", err)
}
})
}
notFoundWithAttempt := validLeakTraceRecord()
notFoundWithAttempt.AttemptID = AssessmentAttemptID(uuid.New())
if err := notFoundWithAttempt.Validate(); !errors.Is(err, ErrInvalidAssessmentLeakTrace) {
t.Fatalf("Validate(not found with an attempt) error = %v, want ErrInvalidAssessmentLeakTrace: only a found search names an attempt", err)
}
}