Overview
This example demonstrates a production-ready financial compliance agent that enforces regulatory rules in real-time. It showcases:- Runtime compliance rule engine using Rhai
- Hot-reloading of regulatory rules without restart
- File watching for automatic rule updates
- Multi-layered compliance checking
- Audit trail and decision tracking
What You’ll Learn
- Building compliance agents for financial services
- Implementing hot-reloadable business rules
- File watching with the
notifycrate - Rhai scripting for rule engines
- Handling regulatory updates dynamically
Prerequisites
- Rust 1.75 or higher
- Understanding of compliance requirements
- Basic knowledge of Rhai syntax
Use Case
Financial institutions must enforce ever-changing compliance rules. This agent:- Checks content against forbidden products/keywords
- Validates interest rate disclosures
- Prevents misleading guarantee promises
- Updates rules dynamically when regulations change
Architecture
Source Code
Running the Example
let sensitive_products = [
"high-risk", "illegal fundraising",
"cryptocurrency", "pyramid scheme",
"forex margin" // Add new restriction
];
Expected Output
Compliance Rules
The example implements three key compliance rules:FIN-001: Sensitive Products
FIN-001: Sensitive Products
Rule: Forbid mentioning sensitive financial productsKeywords:
- High-risk
- Illegal fundraising
- Cryptocurrency
- Pyramid scheme
- Forex margin (can be added)
FIN-002: Interest Rate Disclosure
FIN-002: Interest Rate Disclosure
Rule: Interest rates must specify period (annual/monthly)Triggers: Content mentions “rate”, “return”, etc.Requires: Must include “annual” or “monthly”Example Violation: “10% return” (missing period)
FIN-003: Guarantee Promises
FIN-003: Guarantee Promises
Rule: Cannot promise principal or return guaranteesForbidden Words:
- Guaranteed
- Principal protection
- Risk-free
Key Features
Hot-Reload Capability
- Update rules without service restart
- Immediate regulatory compliance
- Zero downtime for rule changes
- Audit trail of rule versions
Rule Engine Design
Audit Trail
Advanced Patterns
Multi-Jurisdiction Rules
Risk Scoring
Contextual Rules
Real-World Integration
API Endpoint
Kafka Integration
Common Use Cases
Financial Services
Banking, investment, insurance compliance
Healthcare
HIPAA, patient data privacy rules
E-Commerce
Product listing, pricing regulations
Advertising
Marketing claims, disclosure requirements
Troubleshooting
Rule Not Reloading
Rule Not Reloading
Problem: File changes not detectedSolution: Check file watcher:
False Positives
False Positives
Problem: Valid content flagged as non-compliantSolution: Refine rules with context:
Performance Issues
Performance Issues
Problem: Compliance checks are slowSolution: Cache compiled rules:
Best Practices
Version Control: Track all rule changes in git
Testing: Write comprehensive tests for each rule
Audit Logs: Record all compliance decisions
Gradual Rollout: Test new rules in staging first
Next Steps
Medical Diagnosis
Similar pattern for healthcare
Rhai Hot Reload
Deep dive into hot-reload
Compliance Guide
Build compliance systems
Rule Engine
Advanced rule engine patterns