Read a judge's verdict after the answer llama.cpp goes on from, and log a verdict that cannot be read

Found on the PoC: llama.cpp takes a conversation ending on the assistant's
turn as a reply to continue, so Llama Guard answered "Rome.safe" and every
answer judged was served unjudged.

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 15:33:23 +02:00
1 parent c69e6f151c
commit 742d6633e2
4 files changed
+38 -3

No files matched your search

+3
View File
@@ -361,6 +361,9 @@ func (g *Gate) Input(ctx context.Context, s Subject, body []byte) (Verdict, erro
// The judge last: what a pattern already stopped need not wait for it.
if jp := p.Judge; jp != nil && jp.Input != "" && user != "" {
res, err := g.judger.ask(ctx, jp, user, "")
if err != nil {
g.log.Warn("the judge gave no verdict on a request", "judge", jp.Model, "err", err)
}
if err != nil && p.Fail == "closed" && !v.Shadow {
v.Took = g.now().Sub(start)
return v, fmt.Errorf("the judge could not be asked: %w", err)
+16 -3
View File
@@ -159,7 +159,7 @@ func (j *judge) ask(ctx context.Context, jp *JudgePolicy, user, answer string) (
if err != nil {
return judgment{}, err
}
res, err = parseJudgment(format, names, out)
res, err = parseJudgment(format, names, out, answer)
if err != nil {
return judgment{}, err
}
@@ -231,7 +231,11 @@ func judgeRequest(model, format string, categories []string, user, answer string
}
// parseJudgment reads a judge model's answer.
func parseJudgment(format string, names []string, body []byte) (judgment, error) {
//
// answer is the text that was judged, when it was an answer: llama.cpp
// takes a conversation ending on the assistant's turn as a reply to go on
// with, and its answer then begins with that turn — "Rome.safe".
func parseJudgment(format string, names []string, body []byte, answer string) (judgment, error) {
var resp struct {
Choices []struct {
Message struct {
@@ -243,6 +247,9 @@ func parseJudgment(format string, names []string, body []byte) (judgment, error)
return judgment{}, fmt.Errorf("%w: not a chat completion", errJudgeFormat)
}
text := strings.TrimSpace(resp.Choices[0].Message.Content)
if a := strings.TrimSpace(answer); a != "" {
text = strings.TrimSpace(strings.TrimPrefix(text, a))
}
switch format {
case "llamaguard3":
lines := strings.Split(text, "\n")
@@ -287,5 +294,11 @@ func parseJudgment(format string, names []string, body []byte) (judgment, error)
}
}
}
return judgment{}, fmt.Errorf("%w: not in the form %s has", errJudgeFormat, format)
// The judge's own answer, cut short, so an operator can see what form it
// came in: a verdict, not the text judged.
seen := []rune(text)
if len(seen) > 40 {
seen = seen[:40]
}
return judgment{}, fmt.Errorf("%w: not in the form %s has: %q", errJudgeFormat, format, string(seen))
}
+16
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"slices"
"strings"
"sync/atomic"
"testing"
@@ -153,3 +154,18 @@ func TestTheGateCountsJudgeFailuresAndRedactions(t *testing.T) {
t.Error("a judge that timed out under fail open was not counted")
}
}
func TestAJudgeThatGoesOnFromTheAnswerIsStillRead(t *testing.T) {
body := func(s string) []byte {
b, _ := json.Marshal(map[string]any{"choices": []any{map[string]any{"message": map[string]any{"content": s}}}})
return b
}
// Found on the PoC with llama.cpp: the reply to judge, then the verdict.
if j, err := parseJudgment("llamaguard3", nil, body("The capital of Italy is Rome.safe"), "The capital of Italy is Rome."); err != nil || j.unsafe {
t.Errorf("%+v %v", j, err)
}
if j, err := parseJudgment("llamaguard3", []string{"Violent Crimes"}, body("Step one.\n\nunsafe\nS1"), "Step one."); err != nil || !j.unsafe ||
!slices.Contains(j.categories, "Violent Crimes") {
t.Errorf("%+v %v", j, err)
}
}
+3
View File
@@ -192,6 +192,9 @@ func (f *outFilter) askJudge(text string, final bool) finding {
start := time.Now()
res, err := f.g.judger.ask(f.ctx, jp, f.user, f.sofar.String())
f.took += time.Since(start)
if err != nil {
f.g.log.Warn("the judge gave no verdict on an answer", "judge", jp.Model, "err", err)
}
jf := judgeFinding(f.p, jp.Output, res, err)
if jf.failOpen {
f.g.m.failOpen.Inc()