feat(assessment): record capture hints and keep one window per attempt

This commit is contained in:
shira committed 2026-10-11 13:16:52 +03:00
1 parent c975a0d506
commit bb917bf62e
108 files changed
+6649 -139

No files matched your search

@@ -49,6 +49,7 @@ const (
AssessmentReviewReasonDeviatingAnswers AssessmentReviewReason = "deviating_answers"
AssessmentReviewReasonParallelSessions AssessmentReviewReason = "parallel_sessions"
AssessmentReviewReasonExtraNetworks AssessmentReviewReason = "extra_networks"
AssessmentReviewReasonExtraAgents AssessmentReviewReason = "extra_agents"
AssessmentReviewReasonDuplicateAnswers AssessmentReviewReason = "duplicate_answers"
AssessmentReviewReasonClientSignals AssessmentReviewReason = "client_signals"
)
@@ -57,7 +58,7 @@ const (
func (reason AssessmentReviewReason) IsKnown() bool {
switch reason {
case AssessmentReviewReasonFastAnswers, AssessmentReviewReasonDeviatingAnswers,
AssessmentReviewReasonParallelSessions, AssessmentReviewReasonExtraNetworks,
AssessmentReviewReasonParallelSessions, AssessmentReviewReasonExtraNetworks, AssessmentReviewReasonExtraAgents,
AssessmentReviewReasonDuplicateAnswers, AssessmentReviewReasonClientSignals:
return true
default:
@@ -129,15 +130,17 @@ func (observation AssessmentAnswerObservation) Validate() error {
type AssessmentOriginSource string
const (
AssessmentOriginAnswer AssessmentOriginSource = "answer"
AssessmentOriginSignals AssessmentOriginSource = "signals"
AssessmentOriginAnswer AssessmentOriginSource = "answer"
AssessmentOriginSignals AssessmentOriginSource = "signals"
AssessmentOriginWorkSession AssessmentOriginSource = "work_session"
)
// AssessmentOriginMark records, for one accepted request, keyed digests of the
// authenticated session and of the client network. The digests are bound to
// the attempt, so they cannot be correlated between attempts, and they are
// useless without the server secret. NetworkDigest is absent when the client
// address could not be resolved.
// authenticated session, of the client network, and of the User-Agent. The
// digests are bound to the attempt, so they cannot be correlated between
// attempts, and they are useless without the server secret. NetworkDigest is
// absent when the client address could not be resolved and AgentDigest when
// the request had no User-Agent; the raw User-Agent is never stored.
type AssessmentOriginMark struct {
ID uuid.UUID
AttemptID AssessmentAttemptID
@@ -145,6 +148,7 @@ type AssessmentOriginMark struct {
ObservedAt time.Time
SessionDigest AssessmentDigest
NetworkDigest AssessmentDigest
AgentDigest AssessmentDigest
KeyVersion int16
}
@@ -154,7 +158,7 @@ func (mark AssessmentOriginMark) Validate() error {
if mark.ID == uuid.Nil || mark.AttemptID.IsZero() || mark.ObservedAt.IsZero() || mark.SessionDigest.IsZero() || mark.KeyVersion < 1 {
return fmt.Errorf("%s - identifiers, time, session digest, and key version are required: %w", location, ErrInvalidAssessmentReview)
}
if mark.Source != AssessmentOriginAnswer && mark.Source != AssessmentOriginSignals {
if mark.Source != AssessmentOriginAnswer && mark.Source != AssessmentOriginSignals && mark.Source != AssessmentOriginWorkSession {
return fmt.Errorf("%s - source is unknown: %w", location, ErrInvalidAssessmentReview)
}
return nil
@@ -243,6 +247,11 @@ type AssessmentOriginAnomalies struct {
// ExtraNetworks counts the distinct network digests beyond the first. A
// move to another network and back is one extra network.
ExtraNetworks int32
// ExtraAgents counts the distinct User-Agent digests beyond the first, so
// a switch to another browser or device and back is one extra agent. A
// browser that updates itself during an attempt can add one without any
// change of device.
ExtraAgents int32
}
// CountOriginAnomalies orders the marks by time and then by ID and counts
@@ -258,6 +267,7 @@ func CountOriginAnomalies(marks []AssessmentOriginMark) AssessmentOriginAnomalie
var anomalies AssessmentOriginAnomalies
seenSessions := map[AssessmentDigest]struct{}{}
networks := map[AssessmentDigest]struct{}{}
agents := map[AssessmentDigest]struct{}{}
var previousSession AssessmentDigest
for _, mark := range ordered {
_, seen := seenSessions[mark.SessionDigest]
@@ -269,8 +279,12 @@ func CountOriginAnomalies(marks []AssessmentOriginMark) AssessmentOriginAnomalie
if !mark.NetworkDigest.IsZero() {
networks[mark.NetworkDigest] = struct{}{}
}
if !mark.AgentDigest.IsZero() {
agents[mark.AgentDigest] = struct{}{}
}
}
anomalies.ExtraNetworks = int32(max(len(networks)-1, 0)) //nolint:gosec // The number of marks of an attempt is bounded by its stored list.
anomalies.ExtraAgents = int32(max(len(agents)-1, 0)) //nolint:gosec // The number of marks of an attempt is bounded by its stored list.
return anomalies
}
@@ -283,12 +297,16 @@ type AssessmentReviewMeasures struct {
DeviatingAnswers int32
ParallelAlternations int32
ExtraNetworks int32
ExtraAgents int32
MatchedIntegerItems int32
MatchedSequenceItems int32
DuplicateEvidenceBits int32
ClientFocusLossMS int64
ClientClipboardEvents int32
ClientFullscreenExits int32
// ClientScreenshotKeys and ClientDevtoolsOpens count the weak capture hints.
ClientScreenshotKeys int32
ClientDevtoolsOpens int32
}
// AssessmentReviewOutcome is the computed band with the groups behind it.
@@ -297,12 +315,15 @@ type AssessmentReviewOutcome struct {
Reasons []AssessmentReviewReason
}
// Evaluate computes the band. Server signals decide it. Parallel sessions and
// extra networks are weak and correlated (a phone and a laptop produce both), so
// they are one group: alone it reaches attention at most, and review only
// together with a strong signal (fast answers, deviating answers, or duplicate
// answers) at attention or above. Client signals can only raise a normal band
// to attention, because a browser reports them.
// Evaluate computes the band. Server signals decide it. Parallel sessions,
// extra networks, and extra User-Agents are weak and correlated (a phone and a
// laptop produce all three), so they are one group: alone it reaches attention
// at most, and review only together with a strong signal (fast answers,
// deviating answers, or duplicate answers) at attention or above. Client
// signals, the weak capture hints (screenshot keys and open developer tools)
// among them, can only raise a normal band to attention, because a browser
// reports them and anyone can forge or suppress them. A threshold of the
// optional signals that is zero in the policy leaves that signal out.
func (policy AssessmentSignalPolicy) Evaluate(measures AssessmentReviewMeasures) AssessmentReviewOutcome {
type signal struct {
reason AssessmentReviewReason
@@ -314,6 +335,7 @@ func (policy AssessmentSignalPolicy) Evaluate(measures AssessmentReviewMeasures)
{AssessmentReviewReasonDeviatingAnswers, signalLevel(measures.DeviatingAnswers, policy.DeviatingAnswersAttention, policy.DeviatingAnswersReview), false},
{AssessmentReviewReasonParallelSessions, signalLevel(measures.ParallelAlternations, policy.ParallelAlternationsAttention, policy.ParallelAlternationsReview), true},
{AssessmentReviewReasonExtraNetworks, signalLevel(measures.ExtraNetworks, policy.ExtraNetworksAttention, policy.ExtraNetworksReview), true},
{AssessmentReviewReasonExtraAgents, optionalSignalLevel(measures.ExtraAgents, policy.ExtraAgentsAttention, policy.ExtraAgentsReview), true},
{AssessmentReviewReasonDuplicateAnswers, signalLevel(measures.DuplicateEvidenceBits, policy.DuplicateAttentionBits, policy.DuplicateReviewBits), false},
}
strongRaised := false
@@ -336,9 +358,7 @@ func (policy AssessmentSignalPolicy) Evaluate(measures AssessmentReviewMeasures)
outcome.Band = band
}
}
if measures.ClientFocusLossMS >= int64(policy.ClientFocusLossAttentionMS) ||
measures.ClientClipboardEvents >= policy.ClientClipboardEventsAttention ||
measures.ClientFullscreenExits >= policy.ClientFullscreenExitsAttention {
if policy.clientSignalsRaised(measures) {
outcome.Reasons = append(outcome.Reasons, AssessmentReviewReasonClientSignals)
if outcome.Band == AssessmentReviewBandNormal {
outcome.Band = AssessmentReviewBandAttention
@@ -347,6 +367,37 @@ func (policy AssessmentSignalPolicy) Evaluate(measures AssessmentReviewMeasures)
return outcome
}
// clientSignalsRaised reports whether any browser-reported measure reaches its
// threshold. The capture hints have thresholds only in a policy version that
// opted in to them.
func (policy AssessmentSignalPolicy) clientSignalsRaised(measures AssessmentReviewMeasures) bool {
return measures.ClientFocusLossMS >= int64(policy.ClientFocusLossAttentionMS) ||
measures.ClientClipboardEvents >= policy.ClientClipboardEventsAttention ||
measures.ClientFullscreenExits >= policy.ClientFullscreenExitsAttention ||
optionalSignalReached(measures.ClientScreenshotKeys, policy.ClientScreenshotKeysAttention) ||
optionalSignalReached(measures.ClientDevtoolsOpens, policy.ClientDevtoolsAttention)
}
// optionalSignalReached is false for a threshold of zero, which switches the
// signal off.
func optionalSignalReached(value, threshold int32) bool {
return threshold > 0 && value >= threshold
}
// optionalSignalLevel is signalLevel for a signal that an older policy version
// does not carry: a zero attention threshold switches it off, and a zero review
// threshold leaves it at attention.
func optionalSignalLevel(value, attention, review int32) AssessmentReviewBand {
switch {
case attention == 0 || value < attention:
return AssessmentReviewBandNormal
case review > 0 && value >= review:
return AssessmentReviewBandReview
default:
return AssessmentReviewBandAttention
}
}
func signalLevel(value, attention, review int32) AssessmentReviewBand {
switch {
case value >= review: