82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
// policy judges each action against the organisation's plain-language policy.
|
|
// A System One judge reads the rules and the action and answers with a level
|
|
// and the rule it rests on. The guard is advisory, its verdicts recorded and no
|
|
// ceiling set, until the eval result shipped in this package shows the judge
|
|
// model passing its threshold.
|
|
//
|
|
// Settings: policy is the rules, one per line. judge_base_url and judge_model
|
|
// name the System One endpoint and model, and are required. judge_min_confidence
|
|
// is the confidence below which a violation is reported as unclear (default
|
|
// 0.7) 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/policy/internal/judge"
|
|
"github.com/samcharles93/archipelago/sdk/guard"
|
|
"github.com/samcharles93/archipelago/sdk/judgegate"
|
|
)
|
|
|
|
//go:embed eval_result.json
|
|
var recordedEval []byte
|
|
|
|
type policy struct {
|
|
judge *judge.Judge
|
|
// advised says why the guard is advisory, empty when it counts.
|
|
advised string
|
|
}
|
|
|
|
func build(cfg guard.Config) (guard.Guard, error) {
|
|
return newPolicy(cfg, recordedEval, time.Now)
|
|
}
|
|
|
|
func newPolicy(cfg guard.Config, evalJSON []byte, now func() time.Time) (*policy, error) {
|
|
rules := judge.ParseRules(cfg.Settings["policy"])
|
|
if len(rules) == 0 {
|
|
return nil, errors.New("policy: write at least one rule")
|
|
}
|
|
client, ok := judgegate.Client(cfg)
|
|
if !ok {
|
|
return nil, errors.New("judge_base_url and judge_model are required")
|
|
}
|
|
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
|
|
}
|
|
j, err := judge.New(client, rules, minConf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &policy{judge: j, advised: judgegate.AdvisoryReason(evalJSON, client.Model, now(), maxAge)}, nil
|
|
}
|
|
|
|
// Describe is advisory until the recorded eval passes: the whole guard is a
|
|
// model's opinion, so there is nothing else to count.
|
|
func (p *policy) Describe() guard.Description {
|
|
return guard.Description{Name: "policy", Version: "1.0.0", Advisory: p.advised != ""}
|
|
}
|
|
|
|
func (p *policy) Judge(ctx context.Context, a guard.Action) (guard.Verdict, error) {
|
|
out, err := p.judge.Assess(ctx, judgegate.ActionOf(a))
|
|
if err != nil {
|
|
return guard.Verdict{}, err
|
|
}
|
|
v := out.Verdict()
|
|
if p.advised != "" {
|
|
v.BlindSpots = append(v.BlindSpots, "advisory, so it sets no ceiling: "+p.advised)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func main() { guard.Serve(build) }
|