From 742d6633e2682dbceaf9459ee8b43ef106332757 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Sun, 4 Oct 2026 15:33:23 +0200 Subject: [PATCH] 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) --- internal/guard/gate.go | 3 +++ internal/guard/judge.go | 19 ++++++++++++++++--- internal/guard/judge_test.go | 16 ++++++++++++++++ internal/guard/output.go | 3 +++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/internal/guard/gate.go b/internal/guard/gate.go index c062e13..20de9fc 100644 --- a/internal/guard/gate.go +++ b/internal/guard/gate.go @@ -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) diff --git a/internal/guard/judge.go b/internal/guard/judge.go index 237b984..290d507 100644 --- a/internal/guard/judge.go +++ b/internal/guard/judge.go @@ -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)) } diff --git a/internal/guard/judge_test.go b/internal/guard/judge_test.go index 119e6dd..2b38125 100644 --- a/internal/guard/judge_test.go +++ b/internal/guard/judge_test.go @@ -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) + } +} diff --git a/internal/guard/output.go b/internal/guard/output.go index 0db7f60..241fb9f 100644 --- a/internal/guard/output.go +++ b/internal/guard/output.go @@ -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()