166 lines
5.5 KiB
Go
166 lines
5.5 KiB
Go
// integrity is the auditor as a guard. A deterministic lens of rules decides
|
|
// the ceiling: proven violations escalate, signals hold at propose and wait.
|
|
// A System One judge is asked about the same action. Its answer is advisory,
|
|
// recorded as a finding that sets no ceiling, until the eval result shipped in
|
|
// this package shows the judge model passing its threshold. When the judge is
|
|
// unreachable or not configured the lens decides alone and the verdict says so.
|
|
//
|
|
// Settings: judge_base_url and judge_model name the System One endpoint and
|
|
// model (no judge when either is empty), judge_min_confidence is the
|
|
// confidence a trusted judge needs to promote a signal (default 0.9) and
|
|
// eval_max_age_days is how long a passing eval stays trusted (default 90).
|
|
// Secret: judge_api_key, optional.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archipelago/guards/integrity/internal/audit"
|
|
"github.com/samcharles93/archipelago/guards/integrity/internal/eval"
|
|
"github.com/samcharles93/archipelago/guards/integrity/internal/judge"
|
|
"github.com/samcharles93/archipelago/guards/integrity/internal/systemone"
|
|
"github.com/samcharles93/archipelago/sdk/guard"
|
|
)
|
|
|
|
//go:embed eval_result.json
|
|
var recordedEval []byte
|
|
|
|
type integrity struct {
|
|
auditor *audit.Auditor
|
|
// judge is nil when no judge is configured.
|
|
judge judge.Judge
|
|
opts judge.Options
|
|
now func() time.Time
|
|
advised string
|
|
}
|
|
|
|
func build(cfg guard.Config) (guard.Guard, error) {
|
|
return newIntegrity(cfg, recordedEval, time.Now)
|
|
}
|
|
|
|
func newIntegrity(cfg guard.Config, evalJSON []byte, now func() time.Time) (*integrity, error) {
|
|
rules, err := audit.DefaultRules()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("lens: %w", err)
|
|
}
|
|
a, err := audit.NewAuditor(rules)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("lens: %w", err)
|
|
}
|
|
g := &integrity{auditor: a, now: now, opts: judge.Options{Authority: judge.AuthorityAdvisory}}
|
|
base, model := cfg.Settings["judge_base_url"], cfg.Settings["judge_model"]
|
|
if base == "" || model == "" {
|
|
return g, nil
|
|
}
|
|
minConf, err := number(cfg.Settings["judge_min_confidence"], judge.DefaultMinConfidence)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("judge_min_confidence: %w", err)
|
|
}
|
|
days, err := number(cfg.Settings["eval_max_age_days"], 90)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("eval_max_age_days: %w", err)
|
|
}
|
|
g.judge = judge.SystemOne{Client: systemone.Client{BaseURL: base, Model: model, APIKey: cfg.Secrets["judge_api_key"], Label: "judge"}}
|
|
g.opts.MinConfidence = minConf
|
|
g.advised = advisoryReason(evalJSON, model, g.now(), time.Duration(days*24*float64(time.Hour)))
|
|
if g.advised == "" {
|
|
g.opts.Authority = judge.AuthoritySignals
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// advisoryReason is empty only when the recorded eval shows this model passing.
|
|
func advisoryReason(evalJSON []byte, model string, now time.Time, maxAge time.Duration) string {
|
|
rec, err := eval.ParseRecord(evalJSON)
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
return rec.AdvisoryReason(model, now, maxAge)
|
|
}
|
|
|
|
func number(s string, fallback float64) (float64, error) {
|
|
if s == "" {
|
|
return fallback, nil
|
|
}
|
|
n, err := strconv.ParseFloat(s, 64)
|
|
if err != nil || n <= 0 {
|
|
return 0, fmt.Errorf("%q is not a positive number", s)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// Describe is not advisory: the lens always decides. Only the judge's answer
|
|
// can be advisory, and that is handled inside Judge.
|
|
func (*integrity) Describe() guard.Description {
|
|
return guard.Description{Name: "integrity", Version: "1.0.0"}
|
|
}
|
|
|
|
func (g *integrity) Judge(ctx context.Context, a guard.Action) (guard.Verdict, error) {
|
|
b := bundleFrom(a)
|
|
rep := g.auditor.Audit(b)
|
|
v := guard.Verdict{BlindSpots: append([]string(nil), rep.Coverage.NotAudited...)}
|
|
|
|
if g.judge == nil {
|
|
v.BlindSpots = append(v.BlindSpots, "no judge is configured, so the lens decided alone")
|
|
} else {
|
|
answer := g.judge.Judge(ctx, b, rep, g.opts)
|
|
judge.Apply(rep, answer)
|
|
switch {
|
|
case !answer.Ok():
|
|
v.BlindSpots = append(v.BlindSpots, "the judge was unreachable, so the lens decided alone: "+answer.Error)
|
|
case g.opts.Authority == judge.AuthorityAdvisory:
|
|
v.Findings = append(v.Findings, advisoryFindings(rep.Judge, g.advised, g.opts.MinConfidence)...)
|
|
}
|
|
}
|
|
|
|
v.Ceiling = guard.ActQuietly
|
|
for _, f := range rep.Findings {
|
|
if f.Dismissed || f.Axis == audit.AxisRecord {
|
|
continue
|
|
}
|
|
v.Findings = append(v.Findings, finding(f))
|
|
v.Ceiling = max(v.Ceiling, ceilingOf(f))
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// ceilingOf holds a proven violation for someone with authority and a signal
|
|
// at propose and wait.
|
|
func ceilingOf(f audit.Finding) guard.Ceiling {
|
|
if f.Kind == audit.KindViolation {
|
|
return guard.Escalate
|
|
}
|
|
return guard.ProposeAndWait
|
|
}
|
|
|
|
func finding(f audit.Finding) guard.Finding {
|
|
out := guard.Finding{Text: f.RuleID + ": " + f.Title, Strength: guard.Moderate}
|
|
if f.Kind == audit.KindViolation {
|
|
out.Strength = guard.Strong
|
|
}
|
|
if len(f.Evidence) > 0 {
|
|
out.Evidence = f.Evidence[0].Locator + ": " + f.Evidence[0].Quote
|
|
}
|
|
return out
|
|
}
|
|
|
|
// advisoryFindings reports what an untrusted judge thinks. They are findings
|
|
// only: the ceiling is read from the lens's report, which an advisory judge
|
|
// leaves unchanged.
|
|
func advisoryFindings(rec *audit.JudgeRecord, why string, minConf float64) []guard.Finding {
|
|
if rec == nil || rec.Legitimacy != audit.Nefarious || rec.Confidence < minConf {
|
|
return nil
|
|
}
|
|
return []guard.Finding{{
|
|
Text: fmt.Sprintf("the judge rates this action nefarious at confidence %.2f, advisory: %s", rec.Confidence, why),
|
|
Evidence: "judge model " + rec.Model,
|
|
Strength: guard.Weak,
|
|
}}
|
|
}
|
|
|
|
func main() { guard.Serve(build) }
|