GeezerVison is showing github.com — fix(judge): harden cache and provider implementations · majorcontext/keep@f7043f6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: judge/anthropic/provider.go +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "fmt" | 9 | 9 | "io" | 10 | 10 | "net/http" | | 11 | + "strings" | 11 | 12 |
| 12 | 13 | "github.com/majorcontext/keep/judge" | 13 | 14 | ) | @@ -74,7 +75,8 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict, | 74 | 75 | "description": "Brief explanation for the decision.", | 75 | 76 | }, | 76 | 77 | }, | 77 | | - "required": []string{"decision", "reason"}, | | 78 | + "required": []string{"decision", "reason"}, | | 79 | + "additionalProperties": false, | 78 | 80 | }, | 79 | 81 | }}, | 80 | 82 | "tool_choice": map[string]any{ | @@ -150,7 +152,7 @@ func parseResponse(body []byte) (judge.Verdict, error) { | 150 | 152 | } | 151 | 153 |
| 152 | 154 | var d judge.Decision | 153 | | - switch verdict.Decision { | | 155 | + switch strings.ToLower(verdict.Decision) { | 154 | 156 | case "allow": | 155 | 157 | d = judge.Allow | 156 | 158 | case "deny": | |
Collapse file Copy file name to clipboardExpand all lines: judge/cache.go +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | const defaultMaxSize = 10_000 | 10 | 10 |
| | 11 | +var _ Provider = (*Cache)(nil) | | 12 | + | 11 | 13 | // Cache wraps a Provider with an in-memory verdict cache. | 12 | 14 | // Identical requests (same model, prompt, content) return cached verdicts | 13 | 15 | // without calling the underlying provider. Errors are not cached. | @@ -24,8 +26,14 @@ type CacheOption func(*Cache) | 24 | 26 |
| 25 | 27 | // WithMaxSize sets the maximum number of cached verdicts. | 26 | 28 | // When exceeded, the oldest entries are evicted. Default: 10,000. | | 29 | +// Values less than 1 are clamped to 1. | 27 | 30 | 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 | + } | 29 | 37 | } | 30 | 38 |
| 31 | 39 | // NewCache creates a caching wrapper around a Provider. | |
Collapse file Copy file name to clipboardExpand all lines: judge/openai/provider.go +16-5Lines changed: 16 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | "fmt" | 8 | 8 | "io" | 9 | 9 | "net/http" | | 10 | + "strings" | 10 | 11 |
| 11 | 12 | "github.com/majorcontext/keep/judge" | 12 | 13 | ) | @@ -47,8 +48,8 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict, | 47 | 48 | model := ResolveModel(req.Model) | 48 | 49 |
| 49 | 50 | body := map[string]any{ | 50 | | - "model": model, | 51 | | - "max_tokens": 256, | | 51 | + "model": model, | | 52 | + "max_completion_tokens": 256, | 52 | 53 | "messages": []map[string]any{{ | 53 | 54 | "role": "user", | 54 | 55 | "content": fmt.Sprintf( | @@ -114,8 +115,10 @@ func (p *Provider) Judge(ctx context.Context, req judge.Request) (judge.Verdict, | 114 | 115 | func parseResponse(body []byte) (judge.Verdict, error) { | 115 | 116 | var apiResp struct { | 116 | 117 | Choices []struct { | 117 | | - Message struct { | | 118 | + FinishReason string `json:"finish_reason"` | | 119 | + Message struct { | 118 | 120 | Content string `json:"content"` | | 121 | + Refusal string `json:"refusal"` | 119 | 122 | } `json:"message"` | 120 | 123 | } `json:"choices"` | 121 | 124 | Usage struct { | @@ -131,16 +134,24 @@ func parseResponse(body []byte) (judge.Verdict, error) { | 131 | 134 | return judge.Verdict{}, fmt.Errorf("empty response from judge") | 132 | 135 | } | 133 | 136 |
| | 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 | + | 134 | 145 | var verdict struct { | 135 | 146 | Decision string `json:"decision"` | 136 | 147 | Reason string `json:"reason"` | 137 | 148 | } | 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 { | 139 | 150 | return judge.Verdict{}, fmt.Errorf("parse verdict JSON: %w", err) | 140 | 151 | } | 141 | 152 |
| 142 | 153 | var d judge.Decision | 143 | | - switch verdict.Decision { | | 154 | + switch strings.ToLower(verdict.Decision) { | 144 | 155 | case "allow": | 145 | 156 | d = judge.Allow | 146 | 157 | case "deny": | |
|