Files

223 lines
7.2 KiB
Go

package judge
import (
"fmt"
"sort"
"strings"
"github.com/samcharles93/archipelago/guards/integrity/internal/audit"
)
// Apply records a judge's answers in the report and, within the authority it was
// granted, changes the verdict.
//
// The rules, in order of precedence:
//
// 1. A failed judge changes nothing. Its error is recorded and the
// deterministic verdict stands.
// 2. Under advisory authority the answers are recorded and nothing else.
// 3. A signal confirmed at or above the threshold becomes a violation; a signal
// denied at or below its inverse is marked dismissed. Nothing is deleted, so
// the audit keeps the original finding and the judge's probability.
// 4. A proven violation is never dismissed unless authority is "all".
// 5. A judge may raise a verdict the lens missed entirely, by adding a JUDGE-
// finding. This is how the classifier covers the lens's blind spots.
// 6. Everything the judge answered is recorded whether or not it changed the
// outcome, because that record is the raw material for evaluating the model.
func Apply(rep *audit.Report, v Verdict) {
rec := recordOf(v)
rep.Judge = rec
if !v.Ok() || v.Authority == AuthorityAdvisory {
recordDisagreement(rec, rep, v)
return
}
beforeLegitimacy := rep.Legitimacy.Verdict
beforeValidity := rep.Validity.Verdict
applyFindingProbabilities(rep, v)
applyLegitimacyChoice(rep, v)
applyValidityChoice(rep, v)
rep.Recompute()
// Recorded regardless of which answer caused the change: a promoted signal can
// move the legitimacy verdict without any aggregate label being returned.
rec.ChangedLegitimacy = rep.Legitimacy.Verdict != beforeLegitimacy
rec.ChangedValidity = rep.Validity.Verdict != beforeValidity
recordDisagreement(rec, rep, v)
}
// applyFindingProbabilities moves each answered finding according to the
// threshold, promoting confirmed signals and dismissing denied ones.
func applyFindingProbabilities(rep *audit.Report, v Verdict) {
for i := range rep.Findings {
f := &rep.Findings[i]
if !reconsiderable(*f, v.Authority) {
continue
}
probability, asked := v.Signals[f.RuleID]
if !asked {
continue
}
f.JudgeProbability = &probability
switch {
case probability >= v.MinConfidence:
if f.Kind == audit.KindSignal {
f.Kind = audit.KindViolation
f.Confidence = audit.ConfidenceJudge
f.Title += " (judge-confirmed)"
}
case probability <= 1-v.MinConfidence:
f.Dismissed = true
}
}
}
// applyLegitimacyChoice lets the judge raise or lower the legitimacy verdict as a
// whole, which is how a blind-spot case reaches the report.
func applyLegitimacyChoice(rep *audit.Report, v Verdict) {
if v.Legitimacy.Value == "" || v.Legitimacy.Confidence < v.MinConfidence {
return
}
switch v.Legitimacy.Value {
case audit.Nefarious:
if !hasActiveViolation(rep.Findings, audit.AxisLegitimacy) {
rep.Findings = append(rep.Findings, syntheticFinding(audit.JudgeLegitimacyID, audit.AxisLegitimacy, v))
}
case audit.Legitimate:
for i := range rep.Findings {
f := &rep.Findings[i]
if f.Axis == audit.AxisLegitimacy && reconsiderable(*f, v.Authority) {
f.Dismissed = true
}
}
}
}
func applyValidityChoice(rep *audit.Report, v Verdict) {
if v.Validity.Value == "" || v.Validity.Confidence < v.MinConfidence {
return
}
switch v.Validity.Value {
case audit.Invalid:
if !hasActiveViolation(rep.Findings, audit.AxisValidity) {
rep.Findings = append(rep.Findings, syntheticFinding(audit.JudgeValidityID, audit.AxisValidity, v))
}
case audit.Valid:
for i := range rep.Findings {
f := &rep.Findings[i]
if f.Axis == audit.AxisValidity && reconsiderable(*f, v.Authority) {
f.Dismissed = true
}
}
}
}
// recordDisagreement flags a judge that called a call legitimate while the lens
// proved a violation. It changes nothing, but it is exactly the case worth
// counting when the model is evaluated.
func recordDisagreement(rec *audit.JudgeRecord, rep *audit.Report, v Verdict) {
if v.Legitimacy.Value != audit.Legitimate {
return
}
for _, f := range rep.Findings {
if f.Axis == audit.AxisLegitimacy && f.Kind == audit.KindViolation && !f.Dismissed {
rec.ProvenViolationsIgnored = true
return
}
}
}
func hasActiveViolation(findings []audit.Finding, axis string) bool {
for _, f := range findings {
if f.Axis == axis && f.Kind == audit.KindViolation && !f.Dismissed {
return true
}
}
return false
}
// syntheticFinding is a violation the lens did not find and the judge did. Its
// rule id is prefixed JUDGE- so its origin is never mistaken for a lens rule, and
// its severity is P1 rather than P0: a classifier is evidence, not proof.
func syntheticFinding(ruleID, axis string, v Verdict) audit.Finding {
choice := v.Legitimacy
if axis == audit.AxisValidity {
choice = v.Validity
}
title := "Judge classified the output as " + choice.Value
mechanism := fmt.Sprintf(
"A System One decision model (%s) classified this call as %s with confidence %.2f, "+
"on a call where no deterministic rule proved a violation. This is a blind-spot case: "+
"the lens has no pattern for it, so the classification is the only evidence.",
v.Model, choice.Value, choice.Confidence)
return audit.Finding{
RuleID: ruleID,
Title: title,
Axis: axis,
Kind: audit.KindViolation,
Severity: audit.SevP1,
Confidence: audit.ConfidenceJudge,
Mechanism: mechanism,
Remediation: "Treat the output as unsafe pending human review. If the classification is wrong, record it as a case for the judge's eval set rather than weakening the rule.",
Evidence: []audit.Evidence{{
Locator: "judge." + axis,
Quote: describeProbabilities(choice),
Detail: "model " + v.Model + ", authority " + string(v.Authority),
}},
}
}
// describeProbabilities renders a classifier answer as text, which is the closest
// thing to a verbatim quote a label answer has.
func describeProbabilities(choice Choice) string {
if len(choice.Probabilities) == 0 {
return choice.Value
}
labels := make([]string, 0, len(choice.Probabilities))
for label := range choice.Probabilities {
labels = append(labels, label)
}
sort.Strings(labels)
parts := make([]string, 0, len(labels))
for _, label := range labels {
parts = append(parts, fmt.Sprintf("%s=%.2f", label, choice.Probabilities[label]))
}
return strings.Join(parts, " ")
}
func recordOf(v Verdict) *audit.JudgeRecord {
rec := &audit.JudgeRecord{
Provider: v.Provider,
Deployment: v.Deployment,
ResponseID: v.ResponseID,
Model: v.Model,
Authority: string(v.Authority),
MinConfidence: v.MinConfidence,
LatencyMS: v.LatencyMS,
Error: v.Error,
TruncatedInput: v.Truncated,
}
if v.Usage != nil {
rec.InputTokens = v.Usage.Input
rec.OutputTokens = v.Usage.Output
rec.CostUSD = v.Usage.CostUSD
}
if v.Legitimacy.Value != "" {
rec.Legitimacy = v.Legitimacy.Value
rec.Confidence = v.Legitimacy.Confidence
rec.Probabilities = v.Legitimacy.Probabilities
}
if v.Validity.Value != "" {
rec.Validity = v.Validity.Value
rec.ValidityConfidence = v.Validity.Confidence
rec.ValidityProbabilities = v.Validity.Probabilities
}
if len(v.Signals) > 0 {
rec.SignalProbabilities = v.Signals
}
return rec
}