Files

175 lines
5.8 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_api selects systemone (the default) or openai-decisions.
// judge_base_url and judge_model name the endpoint and model; systemone
// requires both and runs without a judge when either is empty, while
// openai-decisions defaults them to the OpenAI API and gpt-6-luna.
// 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"
"errors"
"fmt"
"time"
"github.com/samcharles93/archipelago/guards/integrity/internal/audit"
"github.com/samcharles93/archipelago/guards/integrity/internal/judge"
"github.com/samcharles93/archipelago/sdk/guard"
"github.com/samcharles93/archipelago/sdk/judgegate"
)
//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
// standing is the judge's recorded eval standing.
standing judgegate.Standing
}
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}}
client, ok, err := judgegate.NewClient(cfg)
if err != nil {
return nil, err
}
if !ok {
return g, nil
}
minConf, err := judgegate.PositiveNumber(cfg.Settings["judge_min_confidence"], judge.DefaultMinConfidence)
if err != nil {
return nil, fmt.Errorf("judge_min_confidence: %w", err)
}
maxAge, err := judgegate.MaxAge(cfg)
if err != nil {
return nil, err
}
kind := cfg.Settings["judge_api"]
if kind == "" {
kind = judgegate.APISystemOne
}
g.judge = judge.SystemOne{Client: client, Kind: kind}
g.opts.MinConfidence = minConf
g.standing = judgegate.StandingOf(evalJSON, client.ModelID(), g.now(), maxAge)
g.advised = g.standing.Reason
if g.advised == "" {
g.opts.Authority = judge.AuthoritySignals
}
return g, 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)
v.Judges = append(v.Judges, judgeRecord(answer, g.standing))
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
}
// judgeRecord keeps the judge's whole answer, a failed call included.
func judgeRecord(a judge.Verdict, st judgegate.Standing) guard.JudgeRecord {
call := judgegate.Call{
Model: a.Model, ResultID: a.ResponseID, Answers: a.Answers,
Request: a.RequestJSON, Response: a.Response,
EndpointPath: a.EndpointPath, HTTPStatus: a.HTTPStatus, LatencyMS: int64(a.LatencyMS),
}
if !a.Ok() {
return call.Failed(errors.New(a.Error), st)
}
reason := "legitimacy " + a.Legitimacy.Value
if a.Validity.Value != "" {
reason += "; validity " + a.Validity.Value
}
return call.Record(a.Legitimacy.Value, a.Legitimacy.Confidence, reason, st)
}
// 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) }