Files
lincodex/backend/internal/port/outbound/assessment_signal.go
T

135 lines
6.4 KiB
Go

package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
ErrAssessmentSignalOutcomeNotFound = errors.New("assessment signal outcome not found")
ErrAssessmentReviewNotFound = errors.New("assessment attempt review not found")
)
// AssessmentReviewInputs are the stored facts a review is computed from, apart
// from the comparison with other attempts. They hold no answer, address, or
// session identifier.
type AssessmentReviewInputs struct {
Answers []domain.AssessmentResponseSample
Marks []domain.AssessmentOriginMark
ClientFocusLossMS int64
ClientClipboardCount int32
ClientFullscreenExit int32
ClientScreenshotKeys int32
ClientDevtoolsOpens int32
}
// AssessmentDuplicateQuery selects the pairs of attempts that share answers.
// The caps come from the policy of the attempt being evaluated.
type AssessmentDuplicateQuery struct {
IntegerCapBits int32
SequenceCapBits int32
// MinBits drops pairs with less evidence.
MinBits int32
// EndedOnly keeps only attempts that are over at Now: terminal, or past
// their deadline.
EndedOnly bool
Now time.Time
Limit int32
}
// AssessmentDuplicatePair is one other attempt that shares answers with the
// evaluated one. It is used inside the usecase only: nothing that identifies
// the other attempt or its candidate leaves it.
type AssessmentDuplicatePair struct {
AttemptID domain.AssessmentAttemptID
PolicyVersionID domain.PolicyVersionID
EvidenceBits int32
IntegerItems int32
SequenceItems int32
}
type AssessmentSignalRepositories interface {
AssessmentSignals() AssessmentSignalRepository
AssessmentWorkLeases() AssessmentWorkLeaseRepository
AssessmentLeakTraces() AssessmentLeakTraceRepository
}
// AssessmentTraceAttempt is a real attempt as the leak trace reads it: its
// identifier and the time span in which a tile could have been on its screen.
type AssessmentTraceAttempt struct {
ID domain.AssessmentAttemptID
CreatedAt time.Time
ExpiresAt time.Time
}
// AssessmentTraceStatement is the stored, unmarked statement of one item of a
// real attempt.
type AssessmentTraceStatement struct {
AttemptID domain.AssessmentAttemptID
Statement string
}
// AssessmentLeakTraceRepository serves the operator command that finds the
// attempt behind a leaked text. It reads statements and attempt identifiers
// only, never an answer, and writes only the audit of the search.
type AssessmentLeakTraceRepository interface {
// ListRealAttempts returns up to limit real attempts with an identifier above
// after, in ascending order, so that a caller can walk all of them.
ListRealAttempts(ctx context.Context, after domain.AssessmentAttemptID, limit int32) ([]AssessmentTraceAttempt, error)
// FindStatementsContaining returns up to limit statements of real attempts
// whose text, with runs of white space collapsed, contains the fragment.
FindStatementsContaining(ctx context.Context, fragment string, limit int32) ([]AssessmentTraceStatement, error)
// RecordSearch stores the audit of a search; the record is immutable.
RecordSearch(ctx context.Context, record domain.AssessmentLeakTraceRecord) error
}
// AssessmentWorkLeaseRepository keeps the hold of the single active work
// session of a real attempt. The caller holds the row lock of the attempt, so
// the lease of an attempt is read and written by one transaction at a time.
type AssessmentWorkLeaseRepository interface {
// Get returns the lease; found is false when the attempt never had one.
Get(ctx context.Context, attemptID domain.AssessmentAttemptID) (lease domain.AssessmentWorkLease, found bool, err error)
// Save stores the lease, replacing the previous one of the attempt.
Save(ctx context.Context, lease domain.AssessmentWorkLease) error
}
// AssessmentSignalRepository persists event metadata and the immutable
// candidate receipt in the current unit of work.
type AssessmentSignalRepository interface {
CountRecentOutcomes(ctx context.Context, attemptID domain.AssessmentAttemptID, since time.Time) (int64, time.Time, error)
CountAttemptEvents(ctx context.Context, attemptID domain.AssessmentAttemptID) (int64, error)
SaveBatch(ctx context.Context, batch domain.AssessmentSignalBatch) error
SaveOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope, attemptID domain.AssessmentAttemptID, receipt domain.AssessmentSignalReceipt) error
DeleteExpiredOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope) error
GetOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope) (domain.AssessmentSignalReceipt, error)
// SaveAnswerObservation and SaveOriginMark store server-side facts of an
// accepted request in the current unit of work. The caller holds the row
// lock of the attempt; no other lock is taken.
SaveAnswerObservation(ctx context.Context, observation domain.AssessmentAnswerObservation) error
SaveOriginMark(ctx context.Context, mark domain.AssessmentOriginMark) error
LastOriginMark(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.AssessmentOriginMark, bool, error)
ReviewInputs(ctx context.Context, attemptID domain.AssessmentAttemptID) (AssessmentReviewInputs, error)
// DuplicatePairs lists the other attempts that share answers with the given
// one, strongest first and bounded by the query.
DuplicatePairs(ctx context.Context, attemptID domain.AssessmentAttemptID, query AssessmentDuplicateQuery) ([]AssessmentDuplicatePair, error)
// ReviewSubject returns the product policy version a real attempt is pinned
// to; found is false for an unknown or training attempt.
ReviewSubject(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.PolicyVersionID, bool, error)
// LockReview takes the transaction-scoped lock of one attempt's review. A
// job locks every review it will write, in ascending attempt order, before
// it reads any fact, so concurrent evaluations of one attempt run one after
// another and the later one reads what the earlier one committed.
LockReview(ctx context.Context, attemptID domain.AssessmentAttemptID) error
// SaveReview stores the review; the times come from the database clock.
SaveReview(ctx context.Context, review domain.AssessmentAttemptReview) error
GetReview(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.AssessmentAttemptReview, error)
RecordReviewRead(ctx context.Context, id uuid.UUID, attemptID domain.AssessmentAttemptID, actor domain.AssessmentReviewActor, found bool, at time.Time) error
}