rulegate 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rulegate/__init__.py ADDED
@@ -0,0 +1,72 @@
1
+ # Copyright 2026 actiongate-oss
2
+ # Licensed under the Business Source License 1.1 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License in the LICENSE file at the
5
+ # root of this repository.
6
+
7
+ """RuleGate: Deterministic policy enforcement for agent systems.
8
+
9
+ Evaluates callable predicates against action context before execution.
10
+ All rules must pass for the action to proceed. Pairs with ActionGate
11
+ and BudgetGate as composable primitives in the agent execution layer.
12
+
13
+ Example:
14
+ from rulegate import Engine, Rule, Ruleset, Context, PolicyViolation
15
+
16
+ engine = Engine()
17
+
18
+ def no_pii(ctx: Context) -> bool:
19
+ return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
20
+
21
+ @engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii,)))
22
+ def search(query: str) -> list[str]:
23
+ return api.search(query)
24
+
25
+ try:
26
+ results = search(query="find user")
27
+ except PolicyViolation as e:
28
+ print(f"Blocked: {e.decision.violated_rules}")
29
+ """
30
+
31
+ from .core import (
32
+ MISSING,
33
+ BlockReason,
34
+ Context,
35
+ Decision,
36
+ Mode,
37
+ NamedPredicate,
38
+ Predicate,
39
+ Result,
40
+ Rule,
41
+ Ruleset,
42
+ Status,
43
+ StoreErrorMode,
44
+ )
45
+ from .engine import Engine, PolicyViolation
46
+ from .store import EvalRecord, MemoryStore, Store
47
+
48
+ __all__ = [
49
+ # Core types
50
+ "Rule",
51
+ "Ruleset",
52
+ "Context",
53
+ "Decision",
54
+ "Result",
55
+ "MISSING",
56
+ "Predicate",
57
+ "NamedPredicate",
58
+ # Enums
59
+ "Mode",
60
+ "Status",
61
+ "BlockReason",
62
+ "StoreErrorMode",
63
+ # Engine
64
+ "Engine",
65
+ "PolicyViolation",
66
+ # Store
67
+ "Store",
68
+ "MemoryStore",
69
+ "EvalRecord",
70
+ ]
71
+
72
+ __version__ = "0.2.0"
rulegate/core.py ADDED
@@ -0,0 +1,205 @@
1
+ # Copyright 2026 actiongate-oss
2
+ # Licensed under the Business Source License 1.1 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License in the LICENSE file at the
5
+ # root of this repository.
6
+
7
+ """Core types for RuleGate."""
8
+
9
+ from __future__ import annotations
10
+
11
+ from dataclasses import dataclass, field
12
+ from enum import Enum, auto
13
+ from typing import Any, Callable
14
+
15
+
16
+ class Mode(Enum):
17
+ """Enforcement mode for blocked actions."""
18
+ HARD = auto() # Raise exception on block
19
+ SOFT = auto() # Return blocked decision (caller handles fallback)
20
+
21
+
22
+ class StoreErrorMode(Enum):
23
+ """Behavior when store backend fails."""
24
+ FAIL_CLOSED = auto() # Block action (safe default)
25
+ FAIL_OPEN = auto() # Allow action (availability over safety)
26
+
27
+
28
+ class Status(Enum):
29
+ """Decision outcome."""
30
+ ALLOW = auto()
31
+ BLOCK = auto()
32
+
33
+
34
+ class BlockReason(Enum):
35
+ """Why an action was blocked."""
36
+ POLICY_VIOLATION = auto() # One or more rules failed
37
+ STORE_ERROR = auto() # Backend failure (behavior depends on ruleset)
38
+
39
+
40
+ @dataclass(frozen=True, slots=True)
41
+ class Rule:
42
+ """Identifies a policy-checked action stream.
43
+
44
+ Examples:
45
+ Rule("api", "search", "user:123") # per-user policy
46
+ Rule("support", "escalate", "agent:42") # per-agent policy
47
+ Rule("billing", "refund", "global") # global policy
48
+ """
49
+ namespace: str
50
+ action: str
51
+ principal: str = "global"
52
+
53
+ def __str__(self) -> str:
54
+ return f"{self.namespace}:{self.action}@{self.principal}"
55
+
56
+ @property
57
+ def key(self) -> str:
58
+ """Redis-friendly key string."""
59
+ return f"rg:{self.namespace}:{self.action}:{self.principal}"
60
+
61
+
62
+ @dataclass(frozen=True, slots=True)
63
+ class Context:
64
+ """Immutable context passed to rule predicates.
65
+
66
+ Carries the arguments and metadata of the action being evaluated.
67
+ Rules inspect this to decide allow/deny without side effects.
68
+
69
+ Args:
70
+ rule: The rule being evaluated.
71
+ args: Positional arguments to the guarded function.
72
+ kwargs: Keyword arguments to the guarded function.
73
+ meta: Arbitrary metadata (e.g., headers, session info).
74
+ """
75
+ rule: Rule
76
+ args: tuple[Any, ...] = ()
77
+ kwargs: dict[str, Any] = field(default_factory=dict)
78
+ meta: dict[str, Any] = field(default_factory=dict)
79
+
80
+
81
+ # Type alias for rule predicates.
82
+ # A predicate receives a Context and returns True (allow) or False (deny).
83
+ Predicate = Callable[[Context], bool]
84
+
85
+
86
+ @dataclass(frozen=True, slots=True)
87
+ class NamedPredicate:
88
+ """A predicate with a human-readable name for diagnostics.
89
+
90
+ When a predicate fails, the name appears in Decision.violated_rules
91
+ so operators can identify which rule blocked the action.
92
+ """
93
+ name: str
94
+ fn: Predicate
95
+
96
+ def __call__(self, ctx: Context) -> bool:
97
+ return self.fn(ctx)
98
+
99
+
100
+ @dataclass(frozen=True, slots=True)
101
+ class Ruleset:
102
+ """Policy rules for a gated action.
103
+
104
+ A ruleset contains one or more predicates. ALL must pass for the action
105
+ to be allowed (conjunction / AND logic). Any failure blocks the action.
106
+
107
+ Predicates can be plain callables or NamedPredicate instances.
108
+ Plain callables use their __name__ or __qualname__ for diagnostics.
109
+
110
+ Args:
111
+ predicates: One or more rule predicates (all must pass).
112
+ mode: HARD raises on block, SOFT returns decision.
113
+ on_store_error: FAIL_CLOSED blocks, FAIL_OPEN allows.
114
+
115
+ Example:
116
+ def no_pii(ctx: Context) -> bool:
117
+ return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
118
+
119
+ Ruleset(
120
+ predicates=[no_pii],
121
+ mode=Mode.HARD,
122
+ )
123
+ """
124
+ predicates: tuple[Predicate | NamedPredicate, ...] = ()
125
+ mode: Mode = Mode.HARD
126
+ on_store_error: StoreErrorMode = StoreErrorMode.FAIL_CLOSED
127
+
128
+ def __post_init__(self) -> None:
129
+ if not self.predicates:
130
+ raise ValueError("predicates must not be empty")
131
+ for p in self.predicates:
132
+ if not callable(p):
133
+ raise TypeError(f"predicate must be callable, got {type(p).__name__}")
134
+
135
+
136
+ @dataclass(frozen=True, slots=True)
137
+ class Decision:
138
+ """Result of evaluating an action against its ruleset."""
139
+ status: Status
140
+ rule: Rule
141
+ ruleset: Ruleset
142
+ reason: BlockReason | None = None
143
+ message: str | None = None
144
+ violated_rules: tuple[str, ...] = ()
145
+ evaluated_count: int = 0
146
+
147
+ @property
148
+ def allowed(self) -> bool:
149
+ return self.status == Status.ALLOW
150
+
151
+ @property
152
+ def blocked(self) -> bool:
153
+ return self.status == Status.BLOCK
154
+
155
+ def __bool__(self) -> bool:
156
+ """Truthy = allowed."""
157
+ return self.allowed
158
+
159
+
160
+ class _Missing:
161
+ """Sentinel for distinguishing None from missing value."""
162
+ __slots__ = ()
163
+ def __repr__(self) -> str:
164
+ return "<MISSING>"
165
+
166
+ MISSING = _Missing()
167
+
168
+
169
+ @dataclass(frozen=True, slots=True)
170
+ class Result[T]:
171
+ """Wrapper for guarded function results.
172
+
173
+ Uses a sentinel to distinguish between:
174
+ - Function returned None (legitimate value)
175
+ - Function was blocked (no value)
176
+ """
177
+ decision: Decision
178
+ _value: T | _Missing = MISSING
179
+
180
+ @property
181
+ def ok(self) -> bool:
182
+ return self.decision.allowed
183
+
184
+ @property
185
+ def has_value(self) -> bool:
186
+ return not isinstance(self._value, _Missing)
187
+
188
+ @property
189
+ def value(self) -> T | None:
190
+ """Get value or None if blocked/missing."""
191
+ if isinstance(self._value, _Missing):
192
+ return None
193
+ return self._value
194
+
195
+ def unwrap(self) -> T:
196
+ """Get value or raise if blocked."""
197
+ if isinstance(self._value, _Missing):
198
+ raise ValueError(f"No value: {self.decision.message or 'blocked'}")
199
+ return self._value
200
+
201
+ def unwrap_or(self, default: T) -> T:
202
+ """Get value or return default if blocked."""
203
+ if isinstance(self._value, _Missing):
204
+ return default
205
+ return self._value
rulegate/engine.py ADDED
@@ -0,0 +1,397 @@
1
+ # Copyright 2026 actiongate-oss
2
+ # Licensed under the Business Source License 1.1 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License in the LICENSE file at the
5
+ # root of this repository.
6
+
7
+ """Core engine for RuleGate."""
8
+
9
+ from __future__ import annotations
10
+
11
+ import time
12
+ from functools import wraps
13
+ from typing import Any, Callable, ParamSpec, TypeVar
14
+
15
+ from .core import (
16
+ BlockReason,
17
+ Context,
18
+ Decision,
19
+ Mode,
20
+ NamedPredicate,
21
+ Predicate,
22
+ Result,
23
+ Rule,
24
+ Ruleset,
25
+ Status,
26
+ )
27
+ from .store import MemoryStore, Store
28
+
29
+ P = ParamSpec("P")
30
+ T = TypeVar("T")
31
+
32
+
33
+ class PolicyViolation(RuntimeError):
34
+ """Raised when action violates policy in HARD mode."""
35
+
36
+ def __init__(self, decision: Decision) -> None:
37
+ super().__init__(decision.message or f"Policy violation: {decision.reason}")
38
+ self.decision = decision
39
+
40
+
41
+ def _predicate_name(predicate: Predicate | NamedPredicate) -> str:
42
+ """Extract a human-readable name from a predicate."""
43
+ if isinstance(predicate, NamedPredicate):
44
+ return predicate.name
45
+ name = getattr(predicate, "__qualname__", None) or getattr(predicate, "__name__", None)
46
+ return name or repr(predicate)
47
+
48
+
49
+ class Engine:
50
+ """RuleGate engine for policy enforcement on agent actions.
51
+
52
+ RuleGate evaluates callable predicates against action context
53
+ before execution. All predicates in a ruleset must pass for
54
+ the action to be allowed (conjunction / AND logic).
55
+
56
+ Rules are stateless: evaluation depends only on the predicates
57
+ and the context at call time, never on stored state. The store
58
+ is used only for audit logging of evaluation outcomes.
59
+
60
+ Example:
61
+ engine = Engine()
62
+
63
+ def no_pii(ctx: Context) -> bool:
64
+ return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
65
+
66
+ def business_hours(ctx: Context) -> bool:
67
+ return 9 <= ctx.meta["hour"] < 17
68
+
69
+ @engine.guard(
70
+ Rule("api", "search"),
71
+ Ruleset(predicates=(no_pii, business_hours)),
72
+ )
73
+ def search(query: str) -> list[str]:
74
+ return db.search(query)
75
+
76
+ try:
77
+ results = search(query="find user")
78
+ except PolicyViolation as e:
79
+ print(f"Blocked: {e.decision.violated_rules}")
80
+
81
+ # Or use guard_result for no-exception handling:
82
+ @engine.guard_result(
83
+ Rule("api", "fetch"),
84
+ Ruleset(predicates=(no_pii,), mode=Mode.SOFT),
85
+ )
86
+ def fetch(url: str) -> dict:
87
+ return requests.get(url).json()
88
+
89
+ result = fetch(url="https://api.example.com")
90
+ data = result.unwrap_or({"error": "policy violation"})
91
+ """
92
+
93
+ __slots__ = ("_store", "_clock", "_rulesets", "_listeners", "_errors")
94
+
95
+ def __init__(
96
+ self,
97
+ store: Store | None = None,
98
+ clock: Callable[[], float] | None = None,
99
+ ) -> None:
100
+ self._store: Store = store or MemoryStore()
101
+ self._clock = clock or time.monotonic
102
+ self._rulesets: dict[Rule, Ruleset] = {}
103
+ self._listeners: list[Callable[[Decision], None]] = []
104
+ self._errors = 0
105
+
106
+ # ─────────────────────────────────────────────────────────────
107
+ # Configuration
108
+ # ─────────────────────────────────────────────────────────────
109
+
110
+ def register(self, rule: Rule, ruleset: Ruleset) -> None:
111
+ """Register a ruleset for a rule."""
112
+ self._rulesets[rule] = ruleset
113
+
114
+ def ruleset_for(self, rule: Rule) -> Ruleset | None:
115
+ """Get ruleset for rule (None if not registered)."""
116
+ return self._rulesets.get(rule)
117
+
118
+ def on_decision(self, listener: Callable[[Decision], None]) -> None:
119
+ """Add a listener for decisions (for logging/metrics)."""
120
+ self._listeners.append(listener)
121
+
122
+ @property
123
+ def listener_errors(self) -> int:
124
+ """Count of listener exceptions (never block execution)."""
125
+ return self._errors
126
+
127
+ # ─────────────────────────────────────────────────────────────
128
+ # Core API
129
+ # ─────────────────────────────────────────────────────────────
130
+
131
+ def check(
132
+ self,
133
+ rule: Rule,
134
+ ruleset: Ruleset | None = None,
135
+ *,
136
+ args: tuple[Any, ...] = (),
137
+ kwargs: dict[str, Any] | None = None,
138
+ meta: dict[str, Any] | None = None,
139
+ ) -> Decision:
140
+ """Evaluate all predicates in the ruleset against the action context.
141
+
142
+ All predicates must return True for the action to be allowed.
143
+ Evaluation short-circuits on the first failure.
144
+
145
+ Args:
146
+ rule: The rule identity to evaluate.
147
+ ruleset: Ruleset to use (overrides registered). Required if not registered.
148
+ args: Positional arguments to the guarded function.
149
+ kwargs: Keyword arguments to the guarded function.
150
+ meta: Arbitrary metadata for predicates to inspect.
151
+
152
+ Returns:
153
+ Decision with status ALLOW or BLOCK.
154
+
155
+ Raises:
156
+ ValueError: If no ruleset is registered or provided.
157
+ """
158
+ return self._evaluate(
159
+ rule, ruleset, args=args, kwargs=kwargs, meta=meta, short_circuit=True,
160
+ )
161
+
162
+ def check_all(
163
+ self,
164
+ rule: Rule,
165
+ ruleset: Ruleset | None = None,
166
+ *,
167
+ args: tuple[Any, ...] = (),
168
+ kwargs: dict[str, Any] | None = None,
169
+ meta: dict[str, Any] | None = None,
170
+ ) -> Decision:
171
+ """Evaluate ALL predicates without short-circuiting.
172
+
173
+ Unlike check(), this evaluates every predicate even after a failure.
174
+ Useful for diagnostics: shows all violated rules, not just the first.
175
+
176
+ Same signature and return type as check().
177
+ """
178
+ return self._evaluate(
179
+ rule, ruleset, args=args, kwargs=kwargs, meta=meta, short_circuit=False,
180
+ )
181
+
182
+ def enforce(self, decision: Decision) -> None:
183
+ """Raise PolicyViolation if decision is blocked in HARD mode."""
184
+ if decision.blocked and decision.ruleset.mode == Mode.HARD:
185
+ raise PolicyViolation(decision)
186
+
187
+ def clear(self, rule: Rule) -> None:
188
+ """Clear evaluation records for a rule."""
189
+ self._store.clear(rule)
190
+
191
+ def clear_all(self) -> None:
192
+ """Clear all evaluation records."""
193
+ self._store.clear_all()
194
+
195
+ # ─────────────────────────────────────────────────────────────
196
+ # Decorator API
197
+ # ─────────────────────────────────────────────────────────────
198
+
199
+ def guard(
200
+ self,
201
+ rule: Rule,
202
+ ruleset: Ruleset | None = None,
203
+ *,
204
+ meta: dict[str, Any] | None = None,
205
+ ) -> Callable[[Callable[P, T]], Callable[P, T]]:
206
+ """Decorator that returns T directly.
207
+
208
+ Evaluates all predicates before executing the guarded function.
209
+ The function's args and kwargs are passed to predicates via Context.
210
+
211
+ Raises PolicyViolation on block regardless of mode.
212
+ Use guard_result for no-exception handling.
213
+
214
+ Args:
215
+ rule: The rule identity.
216
+ ruleset: Ruleset to register (optional if already registered).
217
+ meta: Static metadata passed to every evaluation.
218
+
219
+ Example:
220
+ @engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii,)))
221
+ def search(query: str) -> list[str]:
222
+ return db.search(query)
223
+
224
+ results = search("hello") # Returns list[str] or raises PolicyViolation
225
+ """
226
+ if ruleset is not None:
227
+ self.register(rule, ruleset)
228
+
229
+ def decorator(fn: Callable[P, T]) -> Callable[P, T]:
230
+ @wraps(fn)
231
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
232
+ decision = self.check(
233
+ rule,
234
+ args=args,
235
+ kwargs=dict(kwargs),
236
+ meta=meta or {},
237
+ )
238
+ if decision.blocked:
239
+ raise PolicyViolation(decision)
240
+ return fn(*args, **kwargs)
241
+
242
+ return wrapper
243
+
244
+ return decorator
245
+
246
+ def guard_result(
247
+ self,
248
+ rule: Rule,
249
+ ruleset: Ruleset | None = None,
250
+ *,
251
+ meta: dict[str, Any] | None = None,
252
+ ) -> Callable[[Callable[P, T]], Callable[P, Result[T]]]:
253
+ """Decorator that returns Result[T] (never raises).
254
+
255
+ Use this when you want to handle policy violations gracefully
256
+ without exceptions.
257
+
258
+ Example:
259
+ @engine.guard_result(
260
+ Rule("api", "fetch"),
261
+ Ruleset(predicates=(no_pii,), mode=Mode.SOFT),
262
+ )
263
+ def fetch(url: str) -> dict:
264
+ return requests.get(url).json()
265
+
266
+ result = fetch(url="https://api.example.com")
267
+ data = result.unwrap_or({"error": "policy violation"})
268
+ """
269
+ if ruleset is not None:
270
+ self.register(rule, ruleset)
271
+
272
+ def decorator(fn: Callable[P, T]) -> Callable[P, Result[T]]:
273
+ @wraps(fn)
274
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> Result[T]:
275
+ decision = self.check(
276
+ rule,
277
+ args=args,
278
+ kwargs=dict(kwargs),
279
+ meta=meta or {},
280
+ )
281
+
282
+ if decision.blocked:
283
+ return Result(decision=decision)
284
+
285
+ value = fn(*args, **kwargs)
286
+ return Result(decision=decision, _value=value)
287
+
288
+ return wrapper
289
+
290
+ return decorator
291
+
292
+ # ─────────────────────────────────────────────────────────────
293
+ # Internal
294
+ # ─────────────────────────────────────────────────────────────
295
+
296
+ def _evaluate(
297
+ self,
298
+ rule: Rule,
299
+ ruleset: Ruleset | None,
300
+ *,
301
+ args: tuple[Any, ...],
302
+ kwargs: dict[str, Any] | None,
303
+ meta: dict[str, Any] | None,
304
+ short_circuit: bool,
305
+ ) -> Decision:
306
+ """Shared evaluation logic for check() and check_all()."""
307
+ now = self._clock()
308
+ ruleset = ruleset or self.ruleset_for(rule)
309
+
310
+ if ruleset is None:
311
+ method = "check" if short_circuit else "check_all"
312
+ raise ValueError(
313
+ f"No ruleset registered for {rule}. "
314
+ f"Call engine.register() or pass ruleset to {method}()."
315
+ )
316
+
317
+ ctx = Context(
318
+ rule=rule,
319
+ args=args,
320
+ kwargs=kwargs or {},
321
+ meta=meta or {},
322
+ )
323
+
324
+ violated: list[str] = []
325
+ evaluated = 0
326
+
327
+ for predicate in ruleset.predicates:
328
+ evaluated += 1
329
+ try:
330
+ result = predicate(ctx)
331
+ except Exception as e:
332
+ name = _predicate_name(predicate)
333
+ violated.append(f"{name} (raised: {type(e).__name__}: {e})")
334
+ if short_circuit:
335
+ break
336
+ continue
337
+
338
+ if not result:
339
+ violated.append(_predicate_name(predicate))
340
+ if short_circuit:
341
+ break
342
+
343
+ if violated:
344
+ decision = self._decide(
345
+ rule,
346
+ ruleset,
347
+ status=Status.BLOCK,
348
+ reason=BlockReason.POLICY_VIOLATION,
349
+ message=f"Policy violation: {', '.join(violated)}",
350
+ violated_rules=tuple(violated),
351
+ evaluated_count=evaluated,
352
+ )
353
+ else:
354
+ decision = self._decide(
355
+ rule,
356
+ ruleset,
357
+ status=Status.ALLOW,
358
+ evaluated_count=evaluated,
359
+ )
360
+
361
+ # Audit log (fire-and-forget, never affects decision)
362
+ try:
363
+ self._store.record(rule, now, decision)
364
+ except Exception:
365
+ self._errors += 1
366
+
367
+ return decision
368
+
369
+ def _decide(
370
+ self,
371
+ rule: Rule,
372
+ ruleset: Ruleset,
373
+ *,
374
+ status: Status,
375
+ reason: BlockReason | None = None,
376
+ message: str | None = None,
377
+ violated_rules: tuple[str, ...] = (),
378
+ evaluated_count: int = 0,
379
+ ) -> Decision:
380
+ decision = Decision(
381
+ status=status,
382
+ rule=rule,
383
+ ruleset=ruleset,
384
+ reason=reason,
385
+ message=message,
386
+ violated_rules=violated_rules,
387
+ evaluated_count=evaluated_count,
388
+ )
389
+ self._emit(decision)
390
+ return decision
391
+
392
+ def _emit(self, decision: Decision) -> None:
393
+ for listener in self._listeners:
394
+ try:
395
+ listener(decision)
396
+ except Exception:
397
+ self._errors += 1
rulegate/store.py ADDED
@@ -0,0 +1,140 @@
1
+ # Copyright 2026 actiongate-oss
2
+ # Licensed under the Business Source License 1.1 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License in the LICENSE file at the
5
+ # root of this repository.
6
+
7
+ """Storage backends for RuleGate.
8
+
9
+ RuleGate rules are stateless predicates — they don't accumulate events
10
+ like ActionGate or BudgetGate. The store records evaluation outcomes
11
+ for observability and audit purposes.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import threading
17
+ from dataclasses import dataclass
18
+ from typing import Protocol
19
+
20
+ from .core import Decision, Rule
21
+
22
+
23
+ @dataclass(slots=True)
24
+ class EvalRecord:
25
+ """A recorded rule evaluation."""
26
+ ts: float
27
+ decision: Decision
28
+
29
+
30
+ class Store(Protocol):
31
+ """Storage backend protocol for evaluation records.
32
+
33
+ Implementations provide audit logging of rule evaluation outcomes.
34
+ Unlike ActionGate/BudgetGate stores, the RuleGate store is NOT
35
+ in the critical path — evaluation does not depend on store state.
36
+ Store failures never affect the allow/deny decision.
37
+ """
38
+
39
+ def record(self, rule: Rule, now: float, decision: Decision) -> None:
40
+ """Record an evaluation outcome.
41
+
42
+ This is fire-and-forget. Failures are silently ignored
43
+ (they do not affect the decision).
44
+ """
45
+ ...
46
+
47
+ def get_records(
48
+ self,
49
+ rule: Rule,
50
+ now: float,
51
+ window: float | None,
52
+ ) -> list[EvalRecord]:
53
+ """Get evaluation records for a rule within a time window.
54
+
55
+ Args:
56
+ rule: The rule to query.
57
+ now: Current timestamp.
58
+ window: Rolling window in seconds (None = all records).
59
+
60
+ Returns:
61
+ List of evaluation records, oldest first.
62
+ """
63
+ ...
64
+
65
+ def clear(self, rule: Rule) -> None:
66
+ """Clear evaluation records for a specific rule."""
67
+ ...
68
+
69
+ def clear_all(self) -> None:
70
+ """Clear all evaluation records."""
71
+ ...
72
+
73
+
74
+ class MemoryStore:
75
+ """Thread-safe in-memory store for evaluation records.
76
+
77
+ Lock ordering (must always acquire in this order to prevent deadlock):
78
+ 1. _global_lock
79
+ 2. rule-specific lock from _locks
80
+
81
+ Suitable for single-process deployments and testing.
82
+ """
83
+
84
+ __slots__ = ("_records", "_locks", "_global_lock")
85
+
86
+ def __init__(self) -> None:
87
+ self._records: dict[Rule, list[EvalRecord]] = {}
88
+ self._locks: dict[Rule, threading.Lock] = {}
89
+ self._global_lock = threading.Lock()
90
+
91
+ def _get_lock(self, rule: Rule) -> threading.Lock:
92
+ """Get or create lock for rule. Must hold _global_lock when calling."""
93
+ if rule not in self._locks:
94
+ self._locks[rule] = threading.Lock()
95
+ return self._locks[rule]
96
+
97
+ def _prune(
98
+ self,
99
+ records: list[EvalRecord],
100
+ now: float,
101
+ window: float | None,
102
+ ) -> list[EvalRecord]:
103
+ """Remove records outside the window."""
104
+ if window is None:
105
+ return list(records)
106
+ cutoff = now - window
107
+ return [r for r in records if r.ts >= cutoff]
108
+
109
+ def record(self, rule: Rule, now: float, decision: Decision) -> None:
110
+ with self._global_lock:
111
+ lock = self._get_lock(rule)
112
+ with lock:
113
+ records = self._records.get(rule, [])
114
+ records.append(EvalRecord(ts=now, decision=decision))
115
+ self._records[rule] = records
116
+
117
+ def get_records(
118
+ self,
119
+ rule: Rule,
120
+ now: float,
121
+ window: float | None,
122
+ ) -> list[EvalRecord]:
123
+ with self._global_lock:
124
+ lock = self._get_lock(rule)
125
+ with lock:
126
+ records = self._records.get(rule, [])
127
+ pruned = self._prune(records, now, window)
128
+ self._records[rule] = pruned
129
+ return list(pruned)
130
+
131
+ def clear(self, rule: Rule) -> None:
132
+ with self._global_lock:
133
+ lock = self._get_lock(rule)
134
+ with lock:
135
+ self._records.pop(rule, None)
136
+
137
+ def clear_all(self) -> None:
138
+ with self._global_lock:
139
+ self._records.clear()
140
+ self._locks.clear()
@@ -0,0 +1,277 @@
1
+ Metadata-Version: 2.4
2
+ Name: rulegate
3
+ Version: 0.2.0
4
+ Summary: Deterministic, pre-execution policy enforcement for semantic actions in agent systems.
5
+ Project-URL: Homepage, https://github.com/actiongate-oss/rulegate
6
+ Project-URL: Documentation, https://github.com/actiongate-oss/rulegate#readme
7
+ Project-URL: Repository, https://github.com/actiongate-oss/rulegate
8
+ Author-email: ActionGate OSS <actiongate-oss@users.noreply.github.com>
9
+ License-Expression: BUSL-1.1
10
+ License-File: LICENSE
11
+ Keywords: agent-framework,ai-agents,llm,policy-enforcement,rules-engine
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: Other/Proprietary License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: Typing :: Typed
19
+ Requires-Python: >=3.12
20
+ Provides-Extra: dev
21
+ Requires-Dist: mypy>=1.0; extra == 'dev'
22
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
23
+ Requires-Dist: pytest>=8.0; extra == 'dev'
24
+ Requires-Dist: ruff>=0.1; extra == 'dev'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # RuleGate
28
+
29
+ Deterministic, pre-execution policy enforcement for semantic actions in agent systems.
30
+
31
+ ## Source of Truth
32
+
33
+ The canonical source is [github.com/actiongate-oss/rulegate](https://github.com/actiongate-oss/rulegate). PyPI distribution is a convenience mirror.
34
+
35
+ **Vendoring and forking are permitted** under the terms of the [BSL 1.1 license](LICENSE). If you vendor RuleGate, you must preserve the LICENSE file, preserve copyright headers in source files, and not remove or modify the BSL terms. The production use restriction applies to vendored copies. See [SEMANTICS.md](SEMANTICS.md) for the behavioral contract if you reimplement.
36
+
37
+ ---
38
+
39
+ ## Quick Start
40
+
41
+ ```python
42
+ from rulegate import Engine, Rule, Ruleset, Context, PolicyViolation
43
+
44
+ engine = Engine()
45
+
46
+ def no_pii(ctx: Context) -> bool:
47
+ return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
48
+
49
+ @engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii,)))
50
+ def search(query: str) -> list[str]:
51
+ return api.search(query)
52
+
53
+ try:
54
+ results = search(query="find user")
55
+ except PolicyViolation as e:
56
+ print(f"Blocked: {e.decision.violated_rules}")
57
+ ```
58
+
59
+ ---
60
+
61
+ ## Core Concepts
62
+
63
+ ### Rule
64
+
65
+ Identifies what's being policy-checked:
66
+
67
+ ```python
68
+ Rule(namespace, action, principal)
69
+
70
+ Rule("api", "search", "user:123") # per-user policy
71
+ Rule("support", "escalate", "agent:42") # per-agent policy
72
+ Rule("billing", "refund", "global") # global policy
73
+ ```
74
+
75
+ ### Ruleset
76
+
77
+ ```python
78
+ Ruleset(
79
+ predicates=(no_pii, business_hours), # all must pass (AND logic)
80
+ mode=Mode.HARD, # HARD raises, SOFT returns decision
81
+ on_store_error=StoreErrorMode.FAIL_CLOSED,
82
+ )
83
+ ```
84
+
85
+ ### Predicates
86
+
87
+ A predicate is a callable that receives a `Context` and returns `True` (allow) or `False` (deny). Predicates must be pure functions — no I/O, no side effects, no mutations. All external state (time, configuration, session data) should be passed via `meta`:
88
+
89
+ ```python
90
+ def no_pii(ctx: Context) -> bool:
91
+ return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
92
+
93
+ def business_hours(ctx: Context) -> bool:
94
+ return 9 <= ctx.meta["hour"] < 17
95
+ ```
96
+
97
+ For diagnostics, wrap predicates in `NamedPredicate`:
98
+
99
+ ```python
100
+ from rulegate import NamedPredicate
101
+
102
+ no_pii_named = NamedPredicate("no_pii", no_pii)
103
+ ```
104
+
105
+ If a predicate raises an exception, the action is blocked. A predicate that cannot execute cannot assert permission.
106
+
107
+ ### Context
108
+
109
+ Every predicate receives the full action context:
110
+
111
+ ```python
112
+ Context(
113
+ rule=Rule("api", "search"), # the rule being evaluated
114
+ args=("hello",), # positional args to guarded function
115
+ kwargs={"query": "find user"}, # keyword args to guarded function
116
+ meta={"role": "admin", "hour": 14},# arbitrary metadata (time, session, etc.)
117
+ )
118
+ ```
119
+
120
+ ### Decision
121
+
122
+ Every check returns a Decision:
123
+
124
+ ```python
125
+ decision.allowed # bool
126
+ decision.blocked # bool
127
+ decision.violated_rules # tuple of predicate names that failed
128
+ decision.evaluated_count # number of predicates evaluated
129
+ decision.reason # BlockReason.POLICY_VIOLATION or None
130
+ ```
131
+
132
+ ### Two Decorator Styles
133
+
134
+ ```python
135
+ @engine.guard(rule, ruleset) # returns T, raises PolicyViolation
136
+ @engine.guard_result(rule, ruleset) # returns Result[T], never raises
137
+ ```
138
+
139
+ ---
140
+
141
+ ## Short-Circuit vs. Exhaustive
142
+
143
+ ```python
144
+ # Production path: stops at first failure
145
+ decision = engine.check(rule, ruleset)
146
+
147
+ # Diagnostic path: evaluates all predicates, reports every violation
148
+ decision = engine.check_all(rule, ruleset)
149
+ ```
150
+
151
+ ---
152
+
153
+ ## Determinism Guarantee
154
+
155
+ The allow/deny decision is always deterministic relative to the predicates and context. Specifically:
156
+
157
+ - The store is write-only from the engine's perspective and is never consulted during evaluation. Predicate results are computed independently of store state.
158
+ - Store audit write failures are counted and silently ignored. The `on_store_error` field on `Ruleset` is accepted for forward compatibility but is not consulted in v0.2 — the store is not in the decision path.
159
+ - Given the same predicates and the same context, the same decision is produced every time.
160
+
161
+ ---
162
+
163
+ ## Scope & Non-Goals
164
+
165
+ **RuleGate does:**
166
+ - Pre-execution policy enforcement (all predicates must pass)
167
+ - Stateless evaluation (decision depends only on predicates and context)
168
+ - Short-circuit and exhaustive evaluation modes
169
+ - Full decision explainability (which predicates failed and why)
170
+
171
+ **RuleGate does not:**
172
+ - Make LLM or model inference calls
173
+ - Perform rate limiting or throttling (use [ActionGate](https://github.com/actiongate-oss/actiongate))
174
+ - Manage costs, budgets, or billing (use [BudgetGate](https://github.com/actiongate-oss/budgetgate))
175
+ - Provide authentication or authorization
176
+ - Evaluate rules based on stored state or historical patterns
177
+ - Make network calls or perform I/O during evaluation
178
+
179
+ See [SEMANTICS.md](SEMANTICS.md) for the formal behavioral contract.
180
+
181
+ ---
182
+
183
+ ## Observability
184
+
185
+ ```python
186
+ engine.on_decision(lambda d: logger.info(f"{d.status}: {d.rule} {d.violated_rules}"))
187
+ ```
188
+
189
+ Every decision includes: status, rule, ruleset, reason, violated_rules, evaluated_count. The store records evaluation outcomes for audit purposes but is never in the decision path.
190
+
191
+ ---
192
+
193
+ ## Relation to ActionGate and BudgetGate
194
+
195
+ RuleGate is one of three composable primitives in the agent execution layer:
196
+
197
+ | Primitive | Limits | Use case |
198
+ |-----------|--------|----------|
199
+ | [ActionGate](https://github.com/actiongate-oss/actiongate) | calls/time | Rate limiting |
200
+ | [BudgetGate](https://github.com/actiongate-oss/budgetgate) | cost/time | Spend limiting |
201
+ | RuleGate | policy predicates | Policy enforcement |
202
+
203
+ All three are deterministic, pre-execution, and decorator-friendly. They compose via stacking:
204
+
205
+ ```python
206
+ from decimal import Decimal
207
+
208
+ @actiongate_engine.guard(Gate("api", "search"), Policy(max_calls=100))
209
+ @budgetgate_engine.guard(Ledger("api", "search"), Budget(max_spend=Decimal("1.00")), cost=Decimal("0.01"))
210
+ @rulegate_engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii, business_hours)))
211
+ def search(query: str) -> list:
212
+ ...
213
+ ```
214
+
215
+ ---
216
+
217
+ ## Benchmarks
218
+
219
+ ```bash
220
+ python -m rulegate.bench
221
+ ```
222
+
223
+ Single-thread latency, CPython 3.12, default GC, no `PYTHONOPTIMIZE`. Measured on Linux (container, 2 vCPU). Run `bench_rulegate.py` on your target hardware — Docker, VM, and bare metal will produce different tail profiles:
224
+
225
+ | Scenario | p50 | p95 | p99 |
226
+ |----------|-----|-----|-----|
227
+ | 1 trivial predicate | ~4μs | ~7μs | ~12μs |
228
+ | 5 enterprise predicates | ~4.5μs | ~7μs | ~12μs |
229
+ | 10 predicates (all pass) | ~4.5μs | ~7μs | ~12μs |
230
+ | NullStore (no audit) | ~3μs | ~3μs | ~5μs |
231
+
232
+ Predicate count adds ~30–50ns per predicate. The MemoryStore audit write is the dominant fixed cost (~1.5μs). Decision logic is bounded at 3–6μs regardless of composition.
233
+
234
+ ---
235
+
236
+ ## API Reference
237
+
238
+ | Type | Purpose |
239
+ |------|---------|
240
+ | `Engine` | Core policy evaluation |
241
+ | `Rule` | Action identity tuple |
242
+ | `Ruleset` | Policy configuration (predicates + mode) |
243
+ | `Context` | Immutable context passed to predicates |
244
+ | `Decision` | Evaluation result with full diagnostics |
245
+ | `Result[T]` | Wrapper for `guard_result` |
246
+ | `PolicyViolation` | Exception from `guard` |
247
+ | `NamedPredicate` | Predicate with human-readable name |
248
+ | `MemoryStore` | Single-process audit backend |
249
+
250
+ | Enum | Values |
251
+ |------|--------|
252
+ | `Mode` | `HARD`, `SOFT` |
253
+ | `StoreErrorMode` | `FAIL_CLOSED`, `FAIL_OPEN` |
254
+ | `Status` | `ALLOW`, `BLOCK` |
255
+ | `BlockReason` | `POLICY_VIOLATION`, `STORE_ERROR` |
256
+
257
+ ---
258
+
259
+ ## License
260
+
261
+ RuleGate is licensed under the [Business Source License 1.1](LICENSE).
262
+
263
+ ```
264
+ Licensor: actiongate-oss
265
+ Licensed Work: RuleGate
266
+ Additional Use Grant: None
267
+ Change Date: 2030-02-25 (four years from initial publication)
268
+ Change License: Mozilla Public License 2.0
269
+ ```
270
+
271
+ **What this means:** You may copy, modify, create derivative works, redistribute, and make non-production use of RuleGate. The Additional Use Grant is "None", which means any use in a live environment that provides value to end users or internal business operations — including SaaS, internal enterprise deployment, and paid betas — requires a commercial license from the licensor. On the Change Date, RuleGate becomes available under [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/) and the production restriction terminates. Each version has its own Change Date calculated from its publication.
272
+
273
+ **If you vendor RuleGate:** Preserve the LICENSE file and copyright headers. Do not remove or modify the BSL terms. The production restriction applies to all copies, vendored or otherwise.
274
+
275
+ **Licensing difference from siblings:** [ActionGate](https://github.com/actiongate-oss/actiongate) and [BudgetGate](https://github.com/actiongate-oss/budgetgate) are Apache 2.0. RuleGate is BSL 1.1. If composing all three, ensure your use complies with both license terms.
276
+
277
+ See [LICENSE](LICENSE) for the legally binding text.
@@ -0,0 +1,8 @@
1
+ rulegate/__init__.py,sha256=HXYpkX8BL4YBnIJeAcmPZ3cwxJzf0A1ANw7qtCr1Mn0,1700
2
+ rulegate/core.py,sha256=5llNGJ39scdd66AA9y0fmh4LGvQo7uFfK5LYx5G526E,6001
3
+ rulegate/engine.py,sha256=DXqQasVS0_g6hzIEOEGxOvp6JdO4TncNGMk5hhmbu1I,13617
4
+ rulegate/store.py,sha256=0U8QURZuYaPZkQRJufOIkfXgciozFCreos78QQkRKX8,4205
5
+ rulegate-0.2.0.dist-info/METADATA,sha256=UTVIp-z9kk-nRfX0enonHHi5pW2Kr3Py1EdORwXegDk,10145
6
+ rulegate-0.2.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
7
+ rulegate-0.2.0.dist-info/licenses/LICENSE,sha256=3k0hOCMFgjQev2SkD6FI15ckvohsOKzzlKpfq0s95x8,4152
8
+ rulegate-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.29.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,56 @@
1
+ Business Source License 1.1
2
+
3
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
4
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
5
+
6
+ -----------------------------------------------------------------------------
7
+
8
+ Parameters
9
+
10
+ Licensor: actiongate-oss
11
+ Licensed Work: RuleGate
12
+ Additional Use Grant: None
13
+ Change Date: Four years from the date the Licensed Work is published.
14
+ Change License: Mozilla Public License 2.0
15
+
16
+ -----------------------------------------------------------------------------
17
+
18
+ Terms
19
+
20
+ The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
21
+
22
+ Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate.
23
+
24
+ If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
25
+
26
+ All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
27
+
28
+ You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work.
29
+
30
+ Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
31
+
32
+ This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License).
33
+
34
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
35
+
36
+ MariaDB hereby grants you permission to use this License's text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below.
37
+
38
+ -----------------------------------------------------------------------------
39
+
40
+ Covenants of Licensor
41
+
42
+ In consideration of the right to use this License's text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor:
43
+
44
+ 1. To specify as the Change License the GPL Version 2.0 or any later version, or a license that is compatible with GPL Version 2.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 2.0 or a later version. Licensor may specify additional Change Licenses without limitation.
45
+
46
+ 2. To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None".
47
+
48
+ 3. To specify a Change Date.
49
+
50
+ 4. Not to modify this License in any other way.
51
+
52
+ -----------------------------------------------------------------------------
53
+
54
+ Notice
55
+
56
+ The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License.