-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeep.go
More file actions
431 lines (368 loc) · 12.7 KB
/
keep.go
File metadata and controls
431 lines (368 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Package keep is an API-level policy engine for AI agents.
package keep
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
keepcel "github.com/majorcontext/keep/internal/cel"
"github.com/majorcontext/keep/internal/config"
"github.com/majorcontext/keep/internal/engine"
"github.com/majorcontext/keep/internal/rate"
"github.com/majorcontext/keep/internal/redact"
"github.com/majorcontext/keep/internal/secrets"
"github.com/majorcontext/keep/judge"
)
// Type aliases re-exported from internal packages.
type Call = engine.Call
type CallContext = engine.CallContext
type EvalResult = engine.EvalResult
type Decision = engine.Decision
type AuditEntry = engine.AuditEntry
type RuleResult = engine.RuleResult
type RedactedField = engine.RedactedField
type Mutation = redact.Mutation
type JudgeAudit = engine.JudgeAudit
type JudgeUsage = engine.JudgeUsage
// JudgeFunc is the function signature for judge evaluation at the keep API level.
type JudgeFunc func(ctx context.Context, req judge.Request) (judge.Verdict, error)
// Decision constants re-exported from the engine package.
const (
Allow = engine.Allow
Deny = engine.Deny
Redact = engine.Redact
)
// Engine holds compiled evaluators for each policy scope.
type Engine struct {
mu sync.RWMutex
evaluators map[string]*engine.Evaluator
rateStore *rate.Store
secrets *secrets.Detector
cfg engineConfig
}
type engineConfig struct {
rulesDir string
profilesDir string
packsDir string
modeOverride config.Mode
auditHook func(AuditEntry)
judgeFunc JudgeFunc
}
// Option configures Load behavior.
type Option func(*engineConfig)
// WithProfilesDir sets the directory to load profile YAML files from.
func WithProfilesDir(dir string) Option { return func(c *engineConfig) { c.profilesDir = dir } }
// WithPacksDir sets the directory to load starter pack YAML files from.
func WithPacksDir(dir string) Option { return func(c *engineConfig) { c.packsDir = dir } }
// WithMode overrides the mode for all scopes. Valid values are "enforce"
// and "audit_only". Returns an error from Load/LoadFromBytes if invalid.
func WithMode(mode string) Option {
return func(c *engineConfig) { c.modeOverride = config.Mode(mode) }
}
// WithAuditHook registers a callback invoked synchronously after every
// Evaluate call. The hook receives the AuditEntry from the evaluation
// result. It is not called when Evaluate returns an error (e.g. unknown scope).
func WithAuditHook(hook func(AuditEntry)) Option {
return func(c *engineConfig) { c.auditHook = hook }
}
// WithJudge registers a judge function for LLM-as-judge evaluation.
func WithJudge(fn JudgeFunc) Option { return func(c *engineConfig) { c.judgeFunc = fn } }
// WithForceEnforce overrides every scope's mode to "enforce".
// Deprecated: Use WithMode("enforce") instead.
func WithForceEnforce() Option { return WithMode("enforce") }
// LoadFromBytes creates an Engine from raw YAML bytes representing a single
// rule file. The YAML must contain a valid Keep rule file with a scope field.
// Pack references are not supported — all rules must be inline.
//
// The returned Engine is safe for concurrent use. Call Close when done.
//
// This constructor is intended for embedding Keep in other programs (e.g. Moat)
// where the caller controls configuration and does not use the filesystem.
func LoadFromBytes(data []byte, opts ...Option) (*Engine, error) {
var cfg engineConfig
for _, opt := range opts {
opt(&cfg)
}
if err := cfg.validate(); err != nil {
return nil, err
}
rf, err := config.ParseRuleFile(data)
if err != nil {
return nil, fmt.Errorf("keep: %w", err)
}
lr := &config.LoadResult{
Scopes: map[string]*config.RuleFile{rf.Scope: rf},
ResolvedRules: map[string][]config.Rule{rf.Scope: rf.Rules},
Profiles: map[string]*config.Profile{},
}
return buildEngine(lr, cfg)
}
// Load reads rule files from rulesDir, compiles all CEL expressions and
// redact patterns, and returns a ready-to-use Engine.
func Load(rulesDir string, opts ...Option) (*Engine, error) {
cfg := engineConfig{rulesDir: rulesDir}
for _, opt := range opts {
opt(&cfg)
}
if err := cfg.validate(); err != nil {
return nil, err
}
loadResult, err := config.LoadAll(rulesDir, cfg.profilesDir, cfg.packsDir)
if err != nil {
return nil, fmt.Errorf("keep: load config: %w", err)
}
return buildEngine(loadResult, cfg)
}
// Close stops the rate counter GC goroutine. Call this when the engine
// is no longer needed to prevent goroutine leaks.
func (e *Engine) Close() {
if e.rateStore != nil {
e.rateStore.StopGC()
}
}
// Evaluate runs all rules in the given scope against the call and returns
// the policy decision.
func (e *Engine) Evaluate(ctx context.Context, call Call, scope string) (EvalResult, error) {
e.mu.RLock()
ev, ok := e.evaluators[scope]
e.mu.RUnlock()
if !ok {
return EvalResult{}, fmt.Errorf("keep: scope %q not found (available: %s)", scope, strings.Join(e.Scopes(), ", "))
}
result := ev.Evaluate(ctx, call)
if e.cfg.auditHook != nil {
e.cfg.auditHook(result.Audit)
}
return result, nil
}
// Scopes returns the sorted list of loaded scope names.
func (e *Engine) Scopes() []string {
e.mu.RLock()
defer e.mu.RUnlock()
scopes := make([]string, 0, len(e.evaluators))
for name := range e.evaluators {
scopes = append(scopes, name)
}
sort.Strings(scopes)
return scopes
}
// Reload re-reads all configuration from disk and recompiles evaluators.
// The rate store is preserved across reloads.
func (e *Engine) Reload() error {
loadResult, err := config.LoadAll(e.cfg.rulesDir, e.cfg.profilesDir, e.cfg.packsDir)
if err != nil {
return fmt.Errorf("keep: reload: %w", err)
}
celEnv, err := keepcel.NewEnv(keepcel.WithRateStore(e.rateStore), keepcel.WithSecretDetector(e.secrets))
if err != nil {
return fmt.Errorf("keep: reload cel env: %w", err)
}
evaluators, err := buildEvaluators(loadResult, celEnv, e.cfg, e.secrets)
if err != nil {
return fmt.Errorf("keep: reload: %w", err)
}
e.mu.Lock()
e.evaluators = evaluators
e.mu.Unlock()
return nil
}
// ApplyMutations returns a new params map with the given mutations applied.
// The original map is not modified.
func ApplyMutations(params map[string]any, mutations []Mutation) map[string]any {
return redact.ApplyMutations(params, mutations)
}
// LintWarning is a non-fatal issue found during linting.
type LintWarning = config.LintWarning
// LintRules loads rule files from the given directory and returns lint warnings
// without building a full engine. This is used by the validate command.
func LintRules(rulesDir string, profilesDir string, packsDir string) ([]LintWarning, error) {
lr, err := config.LoadAll(rulesDir, profilesDir, packsDir)
if err != nil {
return nil, err
}
return config.LintAll(lr), nil
}
// ValidateRuleBytes parses and validates a Keep rule file from raw YAML bytes
// without compiling an engine. Use this to catch invalid rules early (e.g. at
// deploy time) before the engine is needed at runtime.
func ValidateRuleBytes(data []byte) error {
rf, err := config.ParseRuleFile(data)
if err != nil {
return fmt.Errorf("keep: %w", err)
}
// Also verify CEL expressions compile, since ParseRuleFile only checks
// YAML structure and field validation.
celEnv, err := keepcel.NewEnv()
if err != nil {
return fmt.Errorf("keep: create cel env: %w", err)
}
for _, rule := range rf.Rules {
if rule.Match.When != "" {
if _, err := celEnv.Compile(rule.Match.When); err != nil {
return fmt.Errorf("keep: rule %q: invalid CEL expression: %w", rule.Name, err)
}
}
}
return nil
}
// RuleSet is a programmatic builder for constructing policy rules without
// generating YAML. It produces the same internal representation as LoadFromBytes.
type RuleSet struct {
scope string
mode string
allow []string
deny []string
}
// NewRuleSet creates a new rule builder for the given scope.
// Mode should be "enforce" or "audit_only".
func NewRuleSet(scope, mode string) *RuleSet {
return &RuleSet{scope: scope, mode: mode}
}
// Allow adds operations to the allowlist. When an allowlist is present,
// operations not in the list are denied.
func (rs *RuleSet) Allow(ops ...string) {
rs.allow = append(rs.allow, ops...)
}
// Deny adds operations to the denylist. Deny takes precedence over Allow
// for overlapping entries.
func (rs *RuleSet) Deny(ops ...string) {
rs.deny = append(rs.deny, ops...)
}
// Compile builds an Engine from the rule set. Options (WithMode, WithAuditHook)
// are applied the same as with LoadFromBytes.
func (rs *RuleSet) Compile(opts ...Option) (*Engine, error) {
var cfg engineConfig
for _, opt := range opts {
opt(&cfg)
}
if err := cfg.validate(); err != nil {
return nil, err
}
rules := rs.buildRules()
rf := &config.RuleFile{
Version: config.SupportedVersion,
Scope: rs.scope,
Mode: config.Mode(rs.mode),
Rules: rules,
}
lr := &config.LoadResult{
Scopes: map[string]*config.RuleFile{rs.scope: rf},
ResolvedRules: map[string][]config.Rule{rs.scope: rules},
Profiles: map[string]*config.Profile{},
}
return buildEngine(lr, cfg)
}
func (rs *RuleSet) buildRules() []config.Rule {
var rules []config.Rule
// Deny rules come first — exact match, short-circuits on hit.
for _, op := range rs.deny {
rules = append(rules, config.Rule{
Name: "deny-" + op,
Match: config.Match{Operation: op},
Action: config.ActionDeny,
Message: fmt.Sprintf("%s is not allowed", op),
})
}
// If there's an allowlist, add a catch-all deny that skips allowed
// operations via a CEL when clause on context.operation.
if len(rs.allow) > 0 {
// Build CEL expression: !(context.operation in ['op1', 'op2'])
quoted := make([]string, len(rs.allow))
for i, op := range rs.allow {
quoted[i] = fmt.Sprintf("'%s'", op)
}
when := fmt.Sprintf("!(context.operation in [%s])", strings.Join(quoted, ", "))
rules = append(rules, config.Rule{
Name: "deny-unlisted",
Match: config.Match{Operation: "*", When: when},
Action: config.ActionDeny,
Message: "operation not in allowlist",
})
}
return rules
}
func (c *engineConfig) validate() error {
if c.modeOverride != "" && c.modeOverride != config.ModeEnforce && c.modeOverride != config.ModeAuditOnly {
return fmt.Errorf("keep: invalid mode %q (must be %q or %q)", c.modeOverride, config.ModeEnforce, config.ModeAuditOnly)
}
return nil
}
// buildEngine creates a ready-to-use Engine from a LoadResult and config.
func buildEngine(lr *config.LoadResult, cfg engineConfig) (*Engine, error) {
store := rate.NewStore()
detector, err := secrets.NewDetector()
if err != nil {
return nil, fmt.Errorf("keep: init secrets detector: %w", err)
}
celEnv, err := keepcel.NewEnv(keepcel.WithRateStore(store), keepcel.WithSecretDetector(detector))
if err != nil {
return nil, fmt.Errorf("keep: create cel env: %w", err)
}
evaluators, err := buildEvaluators(lr, celEnv, cfg, detector)
if err != nil {
return nil, err
}
store.StartGC(60*time.Second, 24*time.Hour)
return &Engine{
evaluators: evaluators,
rateStore: store,
secrets: detector,
cfg: cfg,
}, nil
}
// buildEvaluators creates compiled evaluators for every scope in the load result.
func buildEvaluators(lr *config.LoadResult, celEnv *keepcel.Env, cfg engineConfig, detector *secrets.Detector) (map[string]*engine.Evaluator, error) {
evaluators := make(map[string]*engine.Evaluator, len(lr.Scopes))
for scopeName, rf := range lr.Scopes {
rules := lr.ResolvedRules[scopeName]
// Get profile aliases if scope has a profile.
var aliases map[string]string
if rf.Profile != "" {
if p, ok := lr.Profiles[rf.Profile]; ok {
aliases = p.Aliases
}
}
mode := rf.Mode
if cfg.modeOverride != "" {
mode = cfg.modeOverride
}
if mode == "" {
mode = config.ModeAuditOnly // default
}
onError := rf.OnError
if onError == "" {
onError = config.ErrorModeClosed // default
}
ev, err := engine.NewEvaluator(celEnv, scopeName, mode, onError, rules, aliases, rf.Defs, detector, rf.CaseSensitive)
if err != nil {
return nil, fmt.Errorf("keep: compile scope %q: %w", scopeName, err)
}
if cfg.judgeFunc != nil {
ev.SetJudgeFunc(judgeAdapter(cfg.judgeFunc))
}
evaluators[scopeName] = ev
}
return evaluators, nil
}
// judgeAdapter converts a keep-level JudgeFunc to an engine-level JudgeHandler.
func judgeAdapter(fn JudgeFunc) engine.JudgeHandler {
return func(ctx context.Context, model, prompt, content string) (engine.JudgeResult, error) {
v, err := fn(ctx, judge.Request{
Prompt: prompt,
Content: content,
Model: model,
})
if err != nil {
return engine.JudgeResult{}, err
}
return engine.JudgeResult{
Decision: string(v.Decision),
Reason: v.Reason,
InputTokens: v.Usage.InputTokens,
OutputTokens: v.Usage.OutputTokens,
Cached: v.Cached,
}, nil
}
}