The gate: judge requests and answers by the policies in force, buffered or a sentence at a time

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 12:31:55 +02:00
1 parent ccdc07fe0c
commit b4bdd250af
7 files changed
+1411 -12

No files matched your search

+24 -11
View File
@@ -31,7 +31,7 @@ func (p *parsedRequest) guardSubject(model string) guard.Subject {
}
func setGuardHeaders(h http.Header, v guard.Verdict) {
if v.Action == guard.Allow {
if v.Action == guard.Allow || v.Shadow {
return
}
h.Set(guardHeader, string(v.Action))
@@ -59,7 +59,14 @@ func (r *Router) guardInput(ctx context.Context, w http.ResponseWriter, req *par
return false
}
req.guardIn = v
if v.Shadow {
// Recorded, not done: the client sees nothing of it.
return true
}
setGuardHeaders(w.Header(), v)
if v.Action != guard.Block && v.Body != nil {
r.useGuardBody(req, v.Body)
}
switch v.Action {
case guard.Block:
msg := v.Message
@@ -68,20 +75,23 @@ func (r *Router) guardInput(ctx context.Context, w http.ResponseWriter, req *par
}
r.refuseGuarded(w, req, v, http.StatusBadRequest, "invalid_request_error", policyViolation, msg, start)
return false
case guard.Redact:
if v.Body != nil {
req.UpstreamBody = v.Body
// Routed by what is sent, so the prefix tree never holds what
// was taken out.
var b inferenceBody
if json.Unmarshal(v.Body, &b) == nil {
req.PromptPrefix = promptPrefix(&b)
}
}
}
return true
}
// useGuardBody sends the body a guard rewrote, and routes by it, so the
// prefix tree never holds what was taken out.
func (r *Router) useGuardBody(req *parsedRequest, body []byte) {
req.UpstreamBody = body
var b inferenceBody
if json.Unmarshal(body, &b) == nil {
req.PromptPrefix = promptPrefix(&b)
if n := max(b.MaxTokens, b.MaxCompletionTokens); n > 0 {
req.MaxTokens = n
}
}
}
// refuseGuarded answers a request a guard stopped. Nothing is charged —
// nothing was generated — but the row is kept, with the verdict, so a team
// running into a policy shows as one.
@@ -136,5 +146,8 @@ func guardFields(u *store.Usage, in, out guard.Verdict) {
v = out
}
u.GuardAction, u.GuardPolicy, u.GuardCategory = string(v.Action), v.Policy, v.Category
if v.Shadow && v.Action != guard.Allow {
u.GuardAction = "shadow-" + u.GuardAction
}
u.GuardMs = (in.Took + out.Took).Milliseconds()
}