The AgentOS Constraint DSL is a domain-specific language designed to define, enforce, and execute runtime safety policies for AI agents.
It acts as a guardrail layer between:
AI decision-making → real-world execution
Instead of hardcoding rules in Python, the DSL allows:
- Declarative safety rules
- Runtime validation
- Dynamic policy updates
- Compile-time optimization
Traditional AI systems embed rules like this:
if risk_score > 0.7:
raise Exception("Unsafe")This approach is:
- Hardcoded
- Not scalable
- Not auditable
- Difficult to modify
DENY action IF risk_score > 0.7
ALLOW action IF compliance_passed == trueNow rules are:
- Human-readable
- Version-controlled
- Runtime configurable
- Easily testable
A constraint defines whether an action is allowed.
ALLOW operation IF latency < 200
DENY operation IF risk_score > 0.8A condition evaluated at runtime.
risk_score > 0.7
user_role == "admin"What the system should do.
| Action | Meaning |
|---|---|
| ALLOW | Proceed |
| DENY | Block execution |
| WARN | Log but allow |
Runtime data passed into evaluation.
{
"risk_score": 0.65,
"user_role": "user",
"latency": 120
}DSL Input
↓
Lexer → Tokens
↓
Parser → AST
↓
Compiler → Executable Rules
↓
Runtime Engine → Evaluation
↓
Decision (ALLOW / DENY / WARN)
constraint_dsl/
├── lexer/
│ └── lexer.py
├── parser/
│ └── parser.py
├── ast/
│ └── nodes.py
├── compiler/
│ └── compiler.py
├── runtime/
│ └── engine.py
├── examples/
│ └── rules.dsl
└── tests/
Convert DSL text → tokens
DENY action IF risk_score > 0.7[
("DENY", "KEYWORD"),
("action", "IDENTIFIER"),
("IF", "KEYWORD"),
("risk_score", "IDENTIFIER"),
(">", "OPERATOR"),
("0.7", "NUMBER")
]class Lexer:
def tokenize(self, text):
tokens = []
words = text.split()
for word in words:
if word in ["ALLOW", "DENY", "IF"]:
tokens.append((word, "KEYWORD"))
elif word.replace('.', '', 1).isdigit():
tokens.append((word, "NUMBER"))
elif word in [">", "<", "=="]:
tokens.append((word, "OPERATOR"))
else:
tokens.append((word, "IDENTIFIER"))
return tokensConvert tokens → Abstract Syntax Tree
ConstraintNode(
action="DENY",
condition=BinaryOp(
left="risk_score",
op=">",
right=0.7
)
)class Parser:
def parse(self, tokens):
return {
"action": tokens[0][0],
"field": tokens[3][0],
"operator": tokens[4][0],
"value": float(tokens[5][0])
}Convert AST → executable function
class Compiler:
def compile(self, ast):
def rule(context):
if ast["operator"] == ">":
return context[ast["field"]] > ast["value"]
return ruleEvaluate rules
class ConstraintEngine:
def __init__(self, rules):
self.rules = rules
def evaluate(self, context):
for rule in self.rules:
if rule(context):
return "DENY"
return "ALLOW"DENY action IF risk_score > 0.7
ALLOW action IF user_role == "admin"lexer = Lexer()
tokens = lexer.tokenize(dsl_text)
parser = Parser()
ast = parser.parse(tokens)
compiler = Compiler()
rule = compiler.compile(ast)
engine = ConstraintEngine([rule])
decision = engine.evaluate({
"risk_score": 0.8,
"user_role": "user"
})
print(decision) # DENYAgent Runtime
↓
Constraint Engine (DSL Runtime)
↓
Strategy Execution
class AgentRuntime:
def __init__(self, constraint_engine):
self.constraint_engine = constraint_engine
def run(self, input_data):
decision = self.constraint_engine.evaluate(input_data)
if decision == "DENY":
raise Exception("Constraint violation")
return "Execution continues"DENY IF risk_score > 0.7
DENY IF fraud_detected == trueDENY IF risk_score > 0.7 AND user_role != "admin"PRIORITY 1 DENY IF risk_score > 0.9
PRIORITY 2 WARN IF risk_score > 0.7Store rules like:
{
"version": "v1.2",
"rules": [...]
}- Update rules without restarting system
- Fetch from DB / config service
DENY IF transaction_amount > 100000 AND risk_score > 0.6DENY IF content_flagged == true
WARN IF toxicity_score > 0.5DENY IF obstacle_distance < 2def test_high_risk():
assert engine.evaluate({"risk_score": 0.9}) == "DENY"- Test with real agent runtime
- Validate end-to-end behavior
- Precompile rules
- Cache AST
- Use vectorized evaluation (future)
- Graph-based constraints
- ML-based adaptive constraints
- UI rule builder
- Policy-as-a-service (SaaS)
The Constraint DSL is not just a feature.
It is:
The safety kernel of AgentOS
It transforms AI systems from:
- Reactive → Proactive
- Unpredictable → Controlled
- Black-box → Auditable