100 lines
3.0 KiB
Go
100 lines
3.0 KiB
Go
package judgement
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
)
|
|
|
|
// GuardExtensions lists the guard extensions running now.
|
|
type GuardExtensions interface {
|
|
Guards() []*connectors.Guard
|
|
}
|
|
|
|
// identified is a guard that says which package answered.
|
|
type identified interface {
|
|
Version() string
|
|
Digest() string
|
|
}
|
|
|
|
// RunContext is the run an action belongs to, passed to guards that want it.
|
|
type RunContext struct {
|
|
RunID uuid.UUID
|
|
Key string
|
|
Workflow string
|
|
// Diagnosis and Evidence are what the proposing agent concluded and read
|
|
// before proposing the action.
|
|
Diagnosis string
|
|
Evidence []Evidence
|
|
// Overlay is policy text judged as if published, for a dry run.
|
|
Overlay *PolicyOverlay
|
|
}
|
|
|
|
// Evidence is one tool result the agent read, as the tool returned it.
|
|
type Evidence struct {
|
|
Source string `json:"source"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
type runKey struct{}
|
|
|
|
func withRun(ctx context.Context, r RunContext) context.Context {
|
|
return context.WithValue(ctx, runKey{}, r)
|
|
}
|
|
|
|
func runFrom(ctx context.Context) RunContext {
|
|
r, _ := ctx.Value(runKey{}).(RunContext)
|
|
return r
|
|
}
|
|
|
|
// extensionGuard adapts a guard extension to Guard.
|
|
type extensionGuard struct {
|
|
g *connectors.Guard
|
|
}
|
|
|
|
func (e extensionGuard) Name() string { return e.g.Name() }
|
|
func (e extensionGuard) Advisory() bool { return e.g.Advisory() }
|
|
func (e extensionGuard) Version() string { return e.g.Version() }
|
|
func (e extensionGuard) Digest() string { return e.g.Digest() }
|
|
|
|
// Judge puts the action to the extension. The caller's context carries the
|
|
// deadline the host enforces. A ceiling outside the scale is left for judge to
|
|
// hold.
|
|
func (e extensionGuard) Judge(ctx context.Context, a Action) (Verdict, error) {
|
|
run := runFrom(ctx)
|
|
req := connectors.GuardRequest{
|
|
Connector: a.Connector, Name: a.Name, Input: a.Input, Digest: a.Digest,
|
|
Target: a.Target, Effect: a.Effect,
|
|
RunID: run.RunID.String(), DecisionKey: run.Key,
|
|
At: time.Now(),
|
|
}
|
|
req.Diagnosis = run.Diagnosis
|
|
for _, e := range run.Evidence {
|
|
req.Evidence = append(req.Evidence, connectors.GuardEvidence{Source: e.Source, Text: e.Text})
|
|
}
|
|
if d, ok := ctx.Deadline(); ok {
|
|
req.Deadline = time.Until(d)
|
|
}
|
|
ans, err := e.g.Judge(ctx, req)
|
|
if err != nil {
|
|
return Verdict{}, err
|
|
}
|
|
v := Verdict{Ceiling: Level(ans.Ceiling), BlindSpots: ans.BlindSpots}
|
|
for _, f := range ans.Findings {
|
|
v.Findings = append(v.Findings, Finding{Text: f.Text, Evidence: f.Evidence, Strength: f.Strength})
|
|
}
|
|
for _, j := range ans.Judges {
|
|
v.Judges = append(v.Judges, JudgeRecord{
|
|
Model: j.Model, ResultID: j.ResultID, Answer: j.Answer, Confidence: j.Confidence, Reason: j.Reason,
|
|
Basis: j.Basis, BasisDigest: j.BasisDigest, Answers: j.Answers,
|
|
Trusted: j.Trusted, AdvisoryReason: j.AdvisoryReason, EvalDate: j.EvalDate,
|
|
Request: j.Request, Response: j.Response,
|
|
EndpointPath: j.EndpointPath, HTTPStatus: j.HTTPStatus, LatencyMS: j.LatencyMS,
|
|
})
|
|
}
|
|
return v, nil
|
|
}
|