๐ง AI agent governanceJune 13, 2026โ
Tests passing
Policy Enforcer
A lightweight Python library for defining and enforcing governance policies for AI agent actions. Developers can define rulesets (e.g., allowable actions, thresholds, or resource usage limits) and dynamically apply them to monitor and restrict agent behavior in production.
What It Does
- Define custom policy rules for agent behavior.
- Cross-check agent actions against defined policies.
- Event-based triggers for violations, such as alerts or action rollbacks.
Installation
Install the library using pip:
pip install -r requirements.txtUsage
Example
from policy_enforcer import PolicyEnforcer
rules = [
{
"name": "Limit action type",
"condition": {"type": {"allowed": ["read", "write"]}},
"message": "Action type not allowed"
}
]
action = {"type": "delete"}
enforcer = PolicyEnforcer(rules)
result = enforcer.check(action)
print(result)CLI Usage
python policy_enforcer.py <rules.json> <action.json>Source Code
import json
from typing import Dict, Any, List
from pydantic import BaseModel, ValidationError
from cerberus import Validator
class PolicyRule(BaseModel):
name: str
condition: Dict[str, Any]
message: str
class PolicyEnforcer:
def __init__(self, rules: List[Dict[str, Any]]):
"""
Initialize the PolicyEnforcer with a list of rules.
:param rules: List of dictionaries defining policy rules.
"""
self.rules = []
for rule in rules:
try:
validated_rule = PolicyRule(**rule)
self.rules.append(validated_rule)
except ValidationError as e:
raise ValueError(f"Invalid rule definition: {e}")
def check(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""
Check an agent action against the defined rules.
:param action: Dictionary defining the agent action.
:return: Compliance report with violations if any.
"""
validator = Validator()
violations = []
for rule in self.rules:
validator.schema = rule.condition
if not validator.validate(action):
violations.append({
"rule": rule.name,
"message": rule.message,
"errors": validator.errors
})
return {
"compliant": len(violations) == 0,
"violations": violations
}
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Policy Enforcer CLI")
parser.add_argument("rules", type=str, help="Path to JSON file containing policy rules")
parser.add_argument("action", type=str, help="Path to JSON file containing agent action")
args = parser.parse_args()
try:
with open(args.rules, "r") as rules_file:
rules = json.load(rules_file)
with open(args.action, "r") as action_file:
action = json.load(action_file)
enforcer = PolicyEnforcer(rules)
result = enforcer.check(action)
print(json.dumps(result, indent=4))
except FileNotFoundError as e:
print(f"Error: {e}")
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON format - {e}")
except ValueError as e:
print(f"Error: {e}")
Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- policy_enforcer
- Category
- AI agent governance
- Generated
- June 13, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-06-13/policy_enforcer cd generated_tools/2026-06-13/policy_enforcer pip install -r requirements.txt 2>/dev/null || true python policy_enforcer.py