feat(guard): a judge record keeps the request, response, path, status and latency of every model call

This commit is contained in:
sam committed 2026-11-16 12:06:05 +11:00
1 parent e909aee155
commit 124d2033fc
7 files changed
+120 -4

No files matched your search

+28
View File
@@ -2,6 +2,8 @@ package connectors
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"fmt"
"time"
@@ -73,6 +75,13 @@ type GuardJudge struct {
Trusted bool
AdvisoryReason string
EvalDate string
// Request and Response are what the judge endpoint was sent and what it
// returned, both kept as the exact bodies for audit.
Request jsontext.Value
Response jsontext.Value
EndpointPath string
HTTPStatus int
LatencyMS int64
}
// Guard is a running guard extension.
@@ -129,11 +138,30 @@ func (g *Guard) Judge(ctx context.Context, r GuardRequest) (GuardAnswer, error)
Model: j.GetModel(), ResultID: j.GetResultId(), Answer: j.GetAnswer(), Confidence: j.GetConfidence(),
Reason: j.GetReason(), Basis: j.GetBasis(), BasisDigest: j.GetBasisDigest(), Answers: j.GetAnswers(),
Trusted: j.GetTrusted(), AdvisoryReason: j.GetAdvisoryReason(), EvalDate: j.GetEvalDate(),
Request: jsonValue(j.GetRequest()), Response: jsonValue(j.GetResponse()),
EndpointPath: j.GetEndpointPath(), HTTPStatus: int(j.GetHttpStatus()), LatencyMS: j.GetLatencyMs(),
})
}
return a, nil
}
// jsonValue keeps a recorded body verbatim when it is JSON, and quotes it when
// it is not, so the stored verdict always carries a valid JSON value.
func jsonValue(b []byte) jsontext.Value {
if len(b) == 0 {
return nil
}
v := jsontext.Value(b)
if v.IsValid() {
return v
}
quoted, err := json.Marshal(string(b))
if err != nil {
return nil
}
return quoted
}
func strengthName(s gpb.Strength) string {
switch s {
case gpb.Strength_STRENGTH_WEAK:
+2
View File
@@ -91,6 +91,8 @@ func (e extensionGuard) Judge(ctx context.Context, a Action) (Verdict, error) {
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
+7
View File
@@ -91,6 +91,13 @@ type JudgeRecord struct {
Trusted bool `json:"trusted,omitzero"`
AdvisoryReason string `json:"advisory_reason,omitzero"`
EvalDate string `json:"eval_date,omitzero"`
// Request and Response are the exact bodies the judge endpoint was sent
// and returned, kept for audit.
Request jsontext.Value `json:"request,omitzero"`
Response jsontext.Value `json:"response,omitzero"`
EndpointPath string `json:"endpoint_path,omitzero"`
HTTPStatus int `json:"http_status,omitzero"`
LatencyMS int64 `json:"latency_ms,omitzero"`
}
// Verdict is one guard's judgement of one action.