GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(judge): harden cache and provider implementations · majorcontext/keep@f7043f6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • judge
    • anthropic
      • provider.go
    • cache.go
    • openai
      • provider.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎judge/anthropic/provider.go‎

Copy file name to clipboardExpand all lines: judge/anthropic/provider.go
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11+
"strings"
1112

1213
"github.com/majorcontext/keep/judge"
1314
)
@@ -74,7 +75,8 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict,
7475
"description": "Brief explanation for the decision.",
7576
},
7677
},
77-
"required": []string{"decision", "reason"},
78+
"required": []string{"decision", "reason"},
79+
"additionalProperties": false,
7880
},
7981
}},
8082
"tool_choice": map[string]any{
@@ -150,7 +152,7 @@ func parseResponse(body []byte) (judge.Verdict, error) {
150152
}
151153

152154
var d judge.Decision
153-
switch verdict.Decision {
155+
switch strings.ToLower(verdict.Decision) {
154156
case "allow":
155157
d = judge.Allow
156158
case "deny":
Collapse file

‎judge/cache.go‎

Copy file name to clipboardExpand all lines: judge/cache.go
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
const defaultMaxSize = 10_000
1010

11+
var _ Provider = (*Cache)(nil)
12+
1113
// Cache wraps a Provider with an in-memory verdict cache.
1214
// Identical requests (same model, prompt, content) return cached verdicts
1315
// without calling the underlying provider. Errors are not cached.
@@ -24,8 +26,14 @@ type CacheOption func(*Cache)
2426

2527
// WithMaxSize sets the maximum number of cached verdicts.
2628
// When exceeded, the oldest entries are evicted. Default: 10,000.
29+
// Values less than 1 are clamped to 1.
2730
func WithMaxSize(n int) CacheOption {
28-
return func(c *Cache) { c.maxSize = n }
31+
return func(c *Cache) {
32+
if n < 1 {
33+
n = 1
34+
}
35+
c.maxSize = n
36+
}
2937
}
3038

3139
// NewCache creates a caching wrapper around a Provider.
Collapse file

‎judge/openai/provider.go‎

Copy file name to clipboardExpand all lines: judge/openai/provider.go
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"strings"
1011

1112
"github.com/majorcontext/keep/judge"
1213
)
@@ -47,8 +48,8 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict,
4748
model := ResolveModel(req.Model)
4849

4950
body := map[string]any{
50-
"model": model,
51-
"max_tokens": 256,
51+
"model": model,
52+
"max_completion_tokens": 256,
5253
"messages": []map[string]any{{
5354
"role": "user",
5455
"content": fmt.Sprintf(
@@ -114,8 +115,10 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict,
114115
func parseResponse(body []byte) (judge.Verdict, error) {
115116
var apiResp struct {
116117
Choices []struct {
117-
Message struct {
118+
FinishReason string `json:"finish_reason"`
119+
Message struct {
118120
Content string `json:"content"`
121+
Refusal string `json:"refusal"`
119122
} `json:"message"`
120123
} `json:"choices"`
121124
Usage struct {
@@ -131,16 +134,24 @@ func parseResponse(body []byte) (judge.Verdict, error) {
131134
return judge.Verdict{}, fmt.Errorf("empty response from judge")
132135
}
133136

137+
choice := apiResp.Choices[0]
138+
if choice.FinishReason == "length" {
139+
return judge.Verdict{}, fmt.Errorf("judge response truncated (finish_reason=length), increase max_completion_tokens")
140+
}
141+
if choice.Message.Refusal != "" {
142+
return judge.Verdict{}, fmt.Errorf("judge model refused: %s", choice.Message.Refusal)
143+
}
144+
134145
var verdict struct {
135146
Decision string `json:"decision"`
136147
Reason string `json:"reason"`
137148
}
138-
if err := json.Unmarshal([]byte(apiResp.Choices[0].Message.Content), &verdict); err != nil {
149+
if err := json.Unmarshal([]byte(choice.Message.Content), &verdict); err != nil {
139150
return judge.Verdict{}, fmt.Errorf("parse verdict JSON: %w", err)
140151
}
141152

142153
var d judge.Decision
143-
switch verdict.Decision {
154+
switch strings.ToLower(verdict.Decision) {
144155
case "allow":
145156
d = judge.Allow
146157
case "deny":

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.