feat(guards): judge calls record their payload and response, and the OpenAI Decisions API is a judge backend
publish / publish (push) Successful in 1m18s

This commit is contained in:
sam committed 2026-11-16 12:04:37 +11:00
1 parent 3091e4f557
commit eeb00bbcc0
27 files changed
+2230 -99

No files matched your search

+37 -9
View File
@@ -24,6 +24,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
@@ -226,6 +227,13 @@ type Result struct {
Usage *Usage `json:"usage,omitempty"`
StopReason string `json:"stopReason,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
// Request is the exact body sent to the endpoint, and Response the exact
// body it returned. Both are kept even when the call fails, so a failed
// judge call still records what was asked and whatever came back.
Request []byte `json:"-"`
Response []byte `json:"-"`
EndpointPath string `json:"-"`
HTTPStatus int `json:"-"`
// Latency is measured by this client, not reported by the service.
Latency time.Duration `json:"-"`
}
@@ -289,6 +297,12 @@ func (c Client) Endpoint() string {
return strings.TrimRight(c.BaseURL, "/") + "/systemone"
}
// ModelID is the model this client asks.
func (c Client) ModelID() string { return c.Model }
// Deployment names the endpoint in reports.
func (c Client) Deployment() string { return c.Label }
// Classify asks every question about state in one request.
//
// State is any JSON value: the API accepts an object of named fields, and the
@@ -322,11 +336,14 @@ func (c Client) Classify(ctx context.Context, state any, questions map[string]Qu
return Result{}, err
}
endpoint := c.Endpoint()
result := Result{Request: body, EndpointPath: endpointPath(endpoint), Model: c.Model, Provider: c.Label}
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint(), bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return Result{}, fmt.Errorf("systemone: build request: %w", err)
return result, fmt.Errorf("systemone: build request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if c.APIKey != "" {
@@ -340,25 +357,26 @@ func (c Client) Classify(ctx context.Context, state any, questions map[string]Qu
start := time.Now()
resp, err := httpClient.Do(req)
if err != nil {
return Result{}, fmt.Errorf("systemone: %s: %w", c.Label, err)
result.Latency = time.Since(start)
return result, fmt.Errorf("systemone: %s: %w", c.Label, err)
}
defer func() { _ = resp.Body.Close() }()
latency := time.Since(start)
result.Latency = time.Since(start)
result.HTTPStatus = resp.StatusCode
payload, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
result.Response = payload
if err != nil {
return Result{}, fmt.Errorf("systemone: read response: %w", err)
return result, fmt.Errorf("systemone: read response: %w", err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return Result{}, fmt.Errorf("systemone: %s returned HTTP %d%s: %s",
return result, fmt.Errorf("systemone: %s returned HTTP %d%s: %s",
c.Label, resp.StatusCode, statusHint(resp.StatusCode), truncate(string(payload), 300))
}
var result Result
if err := json.Unmarshal(payload, &result); err != nil {
return Result{}, fmt.Errorf("systemone: decode response: %w", err)
return result, fmt.Errorf("systemone: decode response: %w", err)
}
result.Latency = latency
if result.Provider == "" {
result.Provider = c.Label
}
@@ -439,6 +457,16 @@ func statusHint(status int) string {
}
}
// endpointPath is the request path without its host, which is what a stored
// record keeps.
func endpointPath(endpoint string) string {
u, err := url.Parse(endpoint)
if err != nil || u.Path == "" {
return endpoint
}
return u.Path
}
func truncate(s string, n int) string {
s = strings.TrimSpace(s)
if len(s) <= n {