Files
lincodex/backend/internal/domain/assessment_signal_policy.go
T

191 lines
9.3 KiB
Go

package domain
// AssessmentSignalPolicy holds the versioned thresholds that turn the server
// observations of an attempt into a review band. The band is a hint for a
// manual check: it never changes a result, a category, or a quota. The section
// is optional; a policy version without it makes no band, and an absent
// section never acquires defaults.
//
// Counts are numbers of items or events, durations carry their unit in the
// field name, and the duplicate thresholds are evidence bits: a matched answer
// adds the number of bits that the policy assumes for its format, and a pair of
// candidates that coincides by chance reaches N bits with a probability of
// about 2^-N when every matched answer is independent.
type AssessmentSignalPolicy struct {
// FastFloorIntegerMS and FastFloorSequenceMS are the shortest server time
// from the previous answer or session start to the answer that is still
// plausible for the answer format. A shorter time is a fast answer.
FastFloorIntegerMS int32
FastFloorSequenceMS int32
// FastAnswersAttention and FastAnswersReview are the numbers of fast
// answers that raise the band.
FastAnswersAttention int32
FastAnswersReview int32
// DeviationRatio is the factor by which an answer time may differ from the
// median answer time of the same attempt, in both directions, before it is
// an anomalous deviation. 50000 means five times. It applies only to an
// attempt with at least DeviationMinAnswers answers.
DeviationRatio BasisPoints
DeviationMinAnswers int32
DeviatingAnswersAttention int32
DeviatingAnswersReview int32
ParallelAlternationsAttention int32
ParallelAlternationsReview int32
// ExtraNetworks counts the distinct client networks of the attempt beyond
// the first. Going from one network to another and back adds one, however
// often it happens.
ExtraNetworksAttention int32
ExtraNetworksReview int32
// A matched answer carries -log2 of the share of attempts that hold the
// same fingerprint for the family version, so a popular value, such as the
// correct answer of a variant, carries little. DuplicateIntegerCapBits and
// DuplicateSequenceCapBits cap what one answer of the format can carry; an
// integer answer has a small answer space and is capped lower. The bits of
// the matched answers of one pair of attempts add up, and the highest pair
// decides: an attempt is compared with every other one, not with the whole
// population at once.
DuplicateIntegerCapBits int32
DuplicateSequenceCapBits int32
DuplicateAttentionBits int32
DuplicateReviewBits int32
// The client thresholds can raise a normal band to attention only, because
// a browser reports them and anyone can forge or suppress them.
ClientFocusLossAttentionMS int32
ClientClipboardEventsAttention int32
ClientFullscreenExitsAttention int32
// The signals below were added after the policy section was first
// published, so a stored version does not carry them. A zero threshold
// leaves the signal out of the band, and a version never acquires a
// default: the facts are stored and counted for the operator either way.
//
// ClientScreenshotKeysAttention and ClientDevtoolsAttention are client
// thresholds, so they raise a normal band to attention only.
ClientScreenshotKeysAttention int32
ClientDevtoolsAttention int32
// ExtraAgents counts the distinct User-Agent digests of the attempt beyond
// the first. It joins the weak group of parallel sessions and extra
// networks, so alone it reaches attention at most.
ExtraAgentsAttention int32
ExtraAgentsReview int32
}
// Sanity bounds of the signal section. They reject a value in the wrong unit;
// the demo values themselves are recorded in DemoAssessmentSignalPolicy.
const (
minSignalFloorMS = 1000
maxSignalFloorMS = 10 * 60 * 1000
maxSignalCount = 1000
minSignalDeviationRatio = BasisPoints(20000)
maxSignalDeviationRatio = BasisPoints(1000000)
minSignalDeviationAnswers = 3
maxSignalDeviationAnswers = maxAssessmentAttemptItems
maxSignalEvidenceBits = 64
maxSignalThresholdBits = 4096
minSignalClientFocusLossMS = 1000
maxSignalClientFocusLossMS = 24 * 60 * 60 * 1000
)
// DemoAssessmentSignalPolicy returns the initial thresholds. They are agreed
// starting values for verifying the flow, not measured results: no study of
// real candidates supports them, and a calibrated policy must replace them.
func DemoAssessmentSignalPolicy() AssessmentSignalPolicy {
return AssessmentSignalPolicy{
FastFloorIntegerMS: 5000,
FastFloorSequenceMS: 8000,
FastAnswersAttention: 3,
FastAnswersReview: 6,
DeviationRatio: 50000,
DeviationMinAnswers: 5,
DeviatingAnswersAttention: 3,
DeviatingAnswersReview: 6,
ParallelAlternationsAttention: 2,
ParallelAlternationsReview: 6,
ExtraNetworksAttention: 2, ExtraNetworksReview: 5,
DuplicateIntegerCapBits: 8, DuplicateSequenceCapBits: 12,
DuplicateAttentionBits: 48,
DuplicateReviewBits: 64,
ClientFocusLossAttentionMS: 120000,
ClientClipboardEventsAttention: 3,
ClientFullscreenExitsAttention: 3,
}
}
// DemoAssessmentSignalPolicyWithCaptureHints is the demonstration policy with
// the optional signals switched on: three screenshot keys, one detection of
// open developer tools, and two extra User-Agents reach attention. Like the
// other demo values they are agreed starting points that no study supports.
// DemoAssessmentSignalPolicy itself stays as it was published, because a stored
// version never changes.
func DemoAssessmentSignalPolicyWithCaptureHints() AssessmentSignalPolicy {
policy := DemoAssessmentSignalPolicy()
policy.ClientScreenshotKeysAttention = 3
policy.ClientDevtoolsAttention = 1
policy.ExtraAgentsAttention, policy.ExtraAgentsReview = 2, 4
return policy
}
// Validate rejects a zero, out-of-range, or inconsistent section.
func (policy AssessmentSignalPolicy) Validate() error {
const location = "AssessmentSignalPolicy.Validate"
if err := firstError(
checkRange(location, "assessment.signals.fast_floor_integer_ms", policy.FastFloorIntegerMS, minSignalFloorMS, maxSignalFloorMS),
checkRange(location, "assessment.signals.fast_floor_sequence_ms", policy.FastFloorSequenceMS, minSignalFloorMS, maxSignalFloorMS),
checkRange(location, "assessment.signals.deviation_ratio_bp", policy.DeviationRatio, minSignalDeviationRatio, maxSignalDeviationRatio),
checkRange(location, "assessment.signals.deviation_min_answers", policy.DeviationMinAnswers, minSignalDeviationAnswers, maxSignalDeviationAnswers),
checkRange(location, "assessment.signals.duplicate_integer_cap_bits", policy.DuplicateIntegerCapBits, 1, maxSignalEvidenceBits),
checkRange(location, "assessment.signals.duplicate_sequence_cap_bits", policy.DuplicateSequenceCapBits, 1, maxSignalEvidenceBits),
checkRange(location, "assessment.signals.client_focus_loss_attention_ms", policy.ClientFocusLossAttentionMS, minSignalClientFocusLossMS, maxSignalClientFocusLossMS),
checkRange(location, "assessment.signals.client_clipboard_events_attention", policy.ClientClipboardEventsAttention, 1, maxSignalCount),
checkRange(location, "assessment.signals.client_fullscreen_exits_attention", policy.ClientFullscreenExitsAttention, 1, maxSignalCount),
); err != nil {
return err
}
if err := firstError(
checkOptionalRange(location, "assessment.signals.client_screenshot_keys_attention", policy.ClientScreenshotKeysAttention, maxSignalCount),
checkOptionalRange(location, "assessment.signals.client_devtools_attention", policy.ClientDevtoolsAttention, maxSignalCount),
checkOptionalRange(location, "assessment.signals.extra_agents_attention", policy.ExtraAgentsAttention, maxSignalCount),
); err != nil {
return err
}
if policy.ExtraAgentsAttention == 0 && policy.ExtraAgentsReview != 0 {
return invalidPolicy(location, "assessment.signals.extra_agents_review needs extra_agents_attention")
}
if policy.ExtraAgentsAttention != 0 {
if err := checkRange(location, "assessment.signals.extra_agents_review", policy.ExtraAgentsReview, policy.ExtraAgentsAttention, maxSignalCount); err != nil {
return err
}
}
for _, pair := range []struct {
field string
attention, review int32
maximum int32
}{
{"fast_answers", policy.FastAnswersAttention, policy.FastAnswersReview, maxSignalCount},
{"deviating_answers", policy.DeviatingAnswersAttention, policy.DeviatingAnswersReview, maxSignalCount},
{"parallel_alternations", policy.ParallelAlternationsAttention, policy.ParallelAlternationsReview, maxSignalCount},
{"extra_networks", policy.ExtraNetworksAttention, policy.ExtraNetworksReview, maxSignalCount},
{"duplicate_bits", policy.DuplicateAttentionBits, policy.DuplicateReviewBits, maxSignalThresholdBits},
} {
if err := checkRange(location, "assessment.signals."+pair.field+"_attention", pair.attention, 1, pair.maximum); err != nil {
return err
}
if err := checkRange(location, "assessment.signals."+pair.field+"_review", pair.review, pair.attention, pair.maximum); err != nil {
return err
}
}
return nil
}
// checkOptionalRange accepts zero, which leaves an optional signal out, or a
// value from 1 to maximum.
func checkOptionalRange(location, field string, value, maximum int32) error {
if value == 0 {
return nil
}
return checkRange(location, field, value, 1, maximum)
}