rulegate 0.2.0__tar.gz
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-0.2.0/.gitignore +9 -0
- rulegate-0.2.0/LICENSE +56 -0
- rulegate-0.2.0/PKG-INFO +277 -0
- rulegate-0.2.0/README.md +251 -0
- rulegate-0.2.0/SEMANTICS.md +239 -0
- rulegate-0.2.0/bench_rulegate.py +281 -0
- rulegate-0.2.0/pyproject.toml +63 -0
- rulegate-0.2.0/rulegate/__init__.py +72 -0
- rulegate-0.2.0/rulegate/core.py +205 -0
- rulegate-0.2.0/rulegate/engine.py +397 -0
- rulegate-0.2.0/rulegate/store.py +140 -0
- rulegate-0.2.0/tests/test_rulegate.py +768 -0
rulegate-0.2.0/LICENSE
ADDED
|
@@ -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.
|
rulegate-0.2.0/PKG-INFO
ADDED
|
@@ -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.
|
rulegate-0.2.0/README.md
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# RuleGate
|
|
2
|
+
|
|
3
|
+
Deterministic, pre-execution policy enforcement for semantic actions in agent systems.
|
|
4
|
+
|
|
5
|
+
## Source of Truth
|
|
6
|
+
|
|
7
|
+
The canonical source is [github.com/actiongate-oss/rulegate](https://github.com/actiongate-oss/rulegate). PyPI distribution is a convenience mirror.
|
|
8
|
+
|
|
9
|
+
**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.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from rulegate import Engine, Rule, Ruleset, Context, PolicyViolation
|
|
17
|
+
|
|
18
|
+
engine = Engine()
|
|
19
|
+
|
|
20
|
+
def no_pii(ctx: Context) -> bool:
|
|
21
|
+
return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
|
|
22
|
+
|
|
23
|
+
@engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii,)))
|
|
24
|
+
def search(query: str) -> list[str]:
|
|
25
|
+
return api.search(query)
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
results = search(query="find user")
|
|
29
|
+
except PolicyViolation as e:
|
|
30
|
+
print(f"Blocked: {e.decision.violated_rules}")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Core Concepts
|
|
36
|
+
|
|
37
|
+
### Rule
|
|
38
|
+
|
|
39
|
+
Identifies what's being policy-checked:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
Rule(namespace, action, principal)
|
|
43
|
+
|
|
44
|
+
Rule("api", "search", "user:123") # per-user policy
|
|
45
|
+
Rule("support", "escalate", "agent:42") # per-agent policy
|
|
46
|
+
Rule("billing", "refund", "global") # global policy
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Ruleset
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
Ruleset(
|
|
53
|
+
predicates=(no_pii, business_hours), # all must pass (AND logic)
|
|
54
|
+
mode=Mode.HARD, # HARD raises, SOFT returns decision
|
|
55
|
+
on_store_error=StoreErrorMode.FAIL_CLOSED,
|
|
56
|
+
)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Predicates
|
|
60
|
+
|
|
61
|
+
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`:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
def no_pii(ctx: Context) -> bool:
|
|
65
|
+
return "ssn" not in str(ctx.kwargs.get("query", "")).lower()
|
|
66
|
+
|
|
67
|
+
def business_hours(ctx: Context) -> bool:
|
|
68
|
+
return 9 <= ctx.meta["hour"] < 17
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
For diagnostics, wrap predicates in `NamedPredicate`:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from rulegate import NamedPredicate
|
|
75
|
+
|
|
76
|
+
no_pii_named = NamedPredicate("no_pii", no_pii)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
If a predicate raises an exception, the action is blocked. A predicate that cannot execute cannot assert permission.
|
|
80
|
+
|
|
81
|
+
### Context
|
|
82
|
+
|
|
83
|
+
Every predicate receives the full action context:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
Context(
|
|
87
|
+
rule=Rule("api", "search"), # the rule being evaluated
|
|
88
|
+
args=("hello",), # positional args to guarded function
|
|
89
|
+
kwargs={"query": "find user"}, # keyword args to guarded function
|
|
90
|
+
meta={"role": "admin", "hour": 14},# arbitrary metadata (time, session, etc.)
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Decision
|
|
95
|
+
|
|
96
|
+
Every check returns a Decision:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
decision.allowed # bool
|
|
100
|
+
decision.blocked # bool
|
|
101
|
+
decision.violated_rules # tuple of predicate names that failed
|
|
102
|
+
decision.evaluated_count # number of predicates evaluated
|
|
103
|
+
decision.reason # BlockReason.POLICY_VIOLATION or None
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Two Decorator Styles
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
@engine.guard(rule, ruleset) # returns T, raises PolicyViolation
|
|
110
|
+
@engine.guard_result(rule, ruleset) # returns Result[T], never raises
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Short-Circuit vs. Exhaustive
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
# Production path: stops at first failure
|
|
119
|
+
decision = engine.check(rule, ruleset)
|
|
120
|
+
|
|
121
|
+
# Diagnostic path: evaluates all predicates, reports every violation
|
|
122
|
+
decision = engine.check_all(rule, ruleset)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Determinism Guarantee
|
|
128
|
+
|
|
129
|
+
The allow/deny decision is always deterministic relative to the predicates and context. Specifically:
|
|
130
|
+
|
|
131
|
+
- The store is write-only from the engine's perspective and is never consulted during evaluation. Predicate results are computed independently of store state.
|
|
132
|
+
- 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.
|
|
133
|
+
- Given the same predicates and the same context, the same decision is produced every time.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Scope & Non-Goals
|
|
138
|
+
|
|
139
|
+
**RuleGate does:**
|
|
140
|
+
- Pre-execution policy enforcement (all predicates must pass)
|
|
141
|
+
- Stateless evaluation (decision depends only on predicates and context)
|
|
142
|
+
- Short-circuit and exhaustive evaluation modes
|
|
143
|
+
- Full decision explainability (which predicates failed and why)
|
|
144
|
+
|
|
145
|
+
**RuleGate does not:**
|
|
146
|
+
- Make LLM or model inference calls
|
|
147
|
+
- Perform rate limiting or throttling (use [ActionGate](https://github.com/actiongate-oss/actiongate))
|
|
148
|
+
- Manage costs, budgets, or billing (use [BudgetGate](https://github.com/actiongate-oss/budgetgate))
|
|
149
|
+
- Provide authentication or authorization
|
|
150
|
+
- Evaluate rules based on stored state or historical patterns
|
|
151
|
+
- Make network calls or perform I/O during evaluation
|
|
152
|
+
|
|
153
|
+
See [SEMANTICS.md](SEMANTICS.md) for the formal behavioral contract.
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Observability
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
engine.on_decision(lambda d: logger.info(f"{d.status}: {d.rule} {d.violated_rules}"))
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
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.
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Relation to ActionGate and BudgetGate
|
|
168
|
+
|
|
169
|
+
RuleGate is one of three composable primitives in the agent execution layer:
|
|
170
|
+
|
|
171
|
+
| Primitive | Limits | Use case |
|
|
172
|
+
|-----------|--------|----------|
|
|
173
|
+
| [ActionGate](https://github.com/actiongate-oss/actiongate) | calls/time | Rate limiting |
|
|
174
|
+
| [BudgetGate](https://github.com/actiongate-oss/budgetgate) | cost/time | Spend limiting |
|
|
175
|
+
| RuleGate | policy predicates | Policy enforcement |
|
|
176
|
+
|
|
177
|
+
All three are deterministic, pre-execution, and decorator-friendly. They compose via stacking:
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from decimal import Decimal
|
|
181
|
+
|
|
182
|
+
@actiongate_engine.guard(Gate("api", "search"), Policy(max_calls=100))
|
|
183
|
+
@budgetgate_engine.guard(Ledger("api", "search"), Budget(max_spend=Decimal("1.00")), cost=Decimal("0.01"))
|
|
184
|
+
@rulegate_engine.guard(Rule("api", "search"), Ruleset(predicates=(no_pii, business_hours)))
|
|
185
|
+
def search(query: str) -> list:
|
|
186
|
+
...
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Benchmarks
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
python -m rulegate.bench
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
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:
|
|
198
|
+
|
|
199
|
+
| Scenario | p50 | p95 | p99 |
|
|
200
|
+
|----------|-----|-----|-----|
|
|
201
|
+
| 1 trivial predicate | ~4μs | ~7μs | ~12μs |
|
|
202
|
+
| 5 enterprise predicates | ~4.5μs | ~7μs | ~12μs |
|
|
203
|
+
| 10 predicates (all pass) | ~4.5μs | ~7μs | ~12μs |
|
|
204
|
+
| NullStore (no audit) | ~3μs | ~3μs | ~5μs |
|
|
205
|
+
|
|
206
|
+
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.
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## API Reference
|
|
211
|
+
|
|
212
|
+
| Type | Purpose |
|
|
213
|
+
|------|---------|
|
|
214
|
+
| `Engine` | Core policy evaluation |
|
|
215
|
+
| `Rule` | Action identity tuple |
|
|
216
|
+
| `Ruleset` | Policy configuration (predicates + mode) |
|
|
217
|
+
| `Context` | Immutable context passed to predicates |
|
|
218
|
+
| `Decision` | Evaluation result with full diagnostics |
|
|
219
|
+
| `Result[T]` | Wrapper for `guard_result` |
|
|
220
|
+
| `PolicyViolation` | Exception from `guard` |
|
|
221
|
+
| `NamedPredicate` | Predicate with human-readable name |
|
|
222
|
+
| `MemoryStore` | Single-process audit backend |
|
|
223
|
+
|
|
224
|
+
| Enum | Values |
|
|
225
|
+
|------|--------|
|
|
226
|
+
| `Mode` | `HARD`, `SOFT` |
|
|
227
|
+
| `StoreErrorMode` | `FAIL_CLOSED`, `FAIL_OPEN` |
|
|
228
|
+
| `Status` | `ALLOW`, `BLOCK` |
|
|
229
|
+
| `BlockReason` | `POLICY_VIOLATION`, `STORE_ERROR` |
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## License
|
|
234
|
+
|
|
235
|
+
RuleGate is licensed under the [Business Source License 1.1](LICENSE).
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
Licensor: actiongate-oss
|
|
239
|
+
Licensed Work: RuleGate
|
|
240
|
+
Additional Use Grant: None
|
|
241
|
+
Change Date: 2030-02-25 (four years from initial publication)
|
|
242
|
+
Change License: Mozilla Public License 2.0
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**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.
|
|
246
|
+
|
|
247
|
+
**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.
|
|
248
|
+
|
|
249
|
+
**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.
|
|
250
|
+
|
|
251
|
+
See [LICENSE](LICENSE) for the legally binding text.
|