PolicyWatcher that monitors your YAML policy file and automatically reloads it when changes are detected. This allows you to update policy rules, risk thresholds, and default behaviors in production without restarting your application.
How It Works
ThePolicyWatcher runs a background loop that periodically computes the SHA-256 hash of the policy file. When the hash changes, the watcher parses the new file, validates it, and swaps the active policy configuration. If the new file is invalid, the watcher keeps the previous valid configuration and calls an error callback.
Basic Usage
- Python
- TypeScript
Change Detection
The watcher uses SHA-256 hashing to detect file changes. On each tick it reads the file, computes the hash, and compares it to the last known hash. This approach avoids false reloads caused by file metadata changes (modified timestamps, permissions) that do not affect the file content.Error Handling
Provide anon_error callback to handle cases where the updated policy file is invalid or unreadable. The watcher will continue running and retain the last valid configuration.
- Python
- TypeScript
Integration with Memproof
To wire the watcher into a running Memproof instance, pass the instance’sreload_policy method as the on_reload callback. This ensures the orchestrator, risk engine, and policy engine all pick up the new configuration atomically.
- Python
- TypeScript
The policy swap is atomic. In-flight operations that already passed policy evaluation will complete under the old policy. New operations will immediately use the reloaded policy.