155 lines
4.7 KiB
Go
155 lines
4.7 KiB
Go
package judge
|
|
|
|
import "github.com/samcharles93/archipelago/guards/integrity/internal/audit"
|
|
|
|
// buildState assembles what the judge is allowed to see.
|
|
//
|
|
// Two rules apply to everything here. Every string is redacted, because a judge
|
|
// is a third party and a leaked credential must not be re-leaked to it. Every
|
|
// long string is truncated, and the judge is told the input was cut, so it does
|
|
// not reason confidently about text it never received.
|
|
func buildState(b *audit.Bundle, rep *audit.Report, opts Options) (map[string]any, bool) {
|
|
truncated := false
|
|
text := func(s string) string {
|
|
redacted := audit.RedactSecrets(s)
|
|
out := audit.Truncate(redacted, opts.MaxFieldChars)
|
|
if out != redacted {
|
|
truncated = true
|
|
}
|
|
return out
|
|
}
|
|
redacted := func(v any) any { return audit.RedactValues(v) }
|
|
|
|
state := map[string]any{}
|
|
if b.ID != "" {
|
|
state["call_id"] = b.ID
|
|
}
|
|
if b.Subject.Actor != "" {
|
|
state["actor"] = b.Subject.Actor
|
|
}
|
|
if b.Subject.Purpose != "" {
|
|
state["purpose"] = text(b.Subject.Purpose)
|
|
}
|
|
if len(b.Request.Messages) > 0 {
|
|
messages := make([]map[string]any, 0, len(b.Request.Messages))
|
|
for _, m := range b.Request.Messages {
|
|
messages = append(messages, map[string]any{"role": m.Role, "content": text(m.Content)})
|
|
}
|
|
state["request_messages"] = messages
|
|
}
|
|
if b.Response.Content != "" {
|
|
state["output_text"] = text(b.Response.Content)
|
|
}
|
|
if b.Response.Output != nil {
|
|
state["output_structured"] = redacted(b.Response.Output)
|
|
}
|
|
if len(b.Response.ToolCalls) > 0 {
|
|
calls := make([]map[string]any, 0, len(b.Response.ToolCalls))
|
|
for _, c := range b.Response.ToolCalls {
|
|
calls = append(calls, map[string]any{"name": c.Name, "arguments": redacted(c.Arguments)})
|
|
}
|
|
state["tool_calls_requested"] = calls
|
|
}
|
|
if len(b.Request.Tools) > 0 {
|
|
names := make([]string, 0, len(b.Request.Tools))
|
|
for _, t := range b.Request.Tools {
|
|
names = append(names, t.Name)
|
|
}
|
|
state["tools_declared"] = names
|
|
}
|
|
if b.Response.FinishReason != "" {
|
|
state["finish_reason"] = b.Response.FinishReason
|
|
}
|
|
if b.Response.Refusal {
|
|
state["refusal"] = true
|
|
}
|
|
if len(b.Sources) > 0 {
|
|
sources := make([]map[string]any, 0, len(b.Sources))
|
|
for _, s := range b.Sources {
|
|
sources = append(sources, map[string]any{"id": s.ID, "uri": s.URI, "text": text(s.Text)})
|
|
}
|
|
state["sources"] = sources
|
|
}
|
|
if b.Contract != nil {
|
|
state["contract"] = contractState(b.Contract, text, redacted)
|
|
}
|
|
|
|
state["deterministic_findings"] = findingsState(rep, text)
|
|
state["lens_coverage"] = map[string]any{
|
|
"contract_provided": rep.Coverage.ContractProvided,
|
|
"rules_evaluated": rep.Coverage.RulesEvaluated,
|
|
"skipped": skippedState(rep),
|
|
"not_audited": rep.Coverage.NotAudited,
|
|
}
|
|
return state, truncated
|
|
}
|
|
|
|
func contractState(c *audit.Contract, text func(string) string, redacted func(any) any) map[string]any {
|
|
out := map[string]any{}
|
|
if c.Task != "" {
|
|
out["task"] = text(c.Task)
|
|
}
|
|
if c.OutputSchema != nil {
|
|
out["output_schema"] = redacted(c.OutputSchema)
|
|
}
|
|
if len(c.RequiredTools) > 0 {
|
|
out["required_tools"] = c.RequiredTools
|
|
}
|
|
if len(c.MustInclude) > 0 {
|
|
out["must_include"] = c.MustInclude
|
|
}
|
|
if len(c.MustNotInclude) > 0 {
|
|
out["must_not_include"] = c.MustNotInclude
|
|
}
|
|
if len(c.ExpectedFinishReasons) > 0 {
|
|
out["expected_finish_reasons"] = c.ExpectedFinishReasons
|
|
}
|
|
if c.RequireGrounding {
|
|
out["require_grounding"] = true
|
|
}
|
|
if c.ExpectedRefusal != nil {
|
|
out["expected_refusal"] = *c.ExpectedRefusal
|
|
}
|
|
return out
|
|
}
|
|
|
|
// findingsState shows the judge what the lens already found, with its own
|
|
// rationales, so the judge can confirm or dismiss rather than re-derive.
|
|
func findingsState(rep *audit.Report, text func(string) string) []map[string]any {
|
|
out := make([]map[string]any, 0, len(rep.Findings))
|
|
for _, f := range rep.Findings {
|
|
evidence := make([]map[string]any, 0, len(f.Evidence))
|
|
for _, ev := range f.Evidence {
|
|
item := map[string]any{"locator": ev.Locator}
|
|
if ev.Quote != "" {
|
|
item["quote"] = text(ev.Quote)
|
|
}
|
|
if ev.Detail != "" {
|
|
item["detail"] = text(ev.Detail)
|
|
}
|
|
evidence = append(evidence, item)
|
|
}
|
|
out = append(out, map[string]any{
|
|
"rule": f.RuleID,
|
|
"title": f.Title,
|
|
"axis": f.Axis,
|
|
"kind": f.Kind,
|
|
"severity": f.Severity,
|
|
"confidence": f.Confidence,
|
|
"mechanism": f.Mechanism,
|
|
"evidence": evidence,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// skippedState tells the judge which checks could not run, so it spends its
|
|
// attention where the lens was blind rather than where it was already decisive.
|
|
func skippedState(rep *audit.Report) []map[string]string {
|
|
out := make([]map[string]string, 0, len(rep.Coverage.Skipped))
|
|
for _, s := range rep.Coverage.Skipped {
|
|
out = append(out, map[string]string{"rule": s.RuleID, "reason": s.Reason})
|
|
}
|
|
return out
|
|
}
|