fix(policy): support explicit policy_id registration#31
Conversation
- Update PolicyEngine.register to use policy.policy_id - Deprecate brittle name-to-id string conversion - Ensure policies like GDPR are correctly loaded
📝 WalkthroughWalkthroughPolicy registration and identification mechanism updated to use policy hashing based on normalized code and canonical configuration instead of source-based hashing. PolicyDescriptor now stores policy_hash. PolicyEngine.register uses policy_id for config lookup with name-based fallback, and compute_policy_hash performs SHA-256 hashing of normalized source and canonical config. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
backend/app/services/policy/engine.py (1)
208-213: Reuse the cached descriptor hash during evaluation.
policy_set()already computes and freezes eachpolicy_hash. Recomputing it here repeats the expensive source normalization on every batch and can drift from the cachedPolicySetsnapshot if the engine ever outlives a code reload.♻️ Proposed refactor
- for policy in self._policies: - policy_hash = self.compute_policy_hash(policy) - + for policy, descriptor in zip(self._policies, ps.policies): try: violations = policy.evaluate( events=events, policy_version=ps.version, - policy_hash=policy_hash, + policy_hash=descriptor.policy_hash, )Also applies to: 279-289
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@backend/app/services/policy/engine.py` around lines 208 - 213, The code recomputes policy hashes during evaluation (calling self.compute_policy_hash(p)) even though policy_set() already computed and froze each policy_hash; replace those recomputations with the cached value (use the Policy object's policy_hash field or the frozen PolicyDescriptor) in the block constructing PolicyDescriptor (and the similar block at 279-289) so you read p.policy_hash (or the existing descriptor.policy_hash) instead of calling compute_policy_hash; ensure you remove the expensive normalization call and rely on the precomputed hash to avoid drift across reloads.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/app/services/policy/engine.py`:
- Around line 181-196: Ensure Policy declares a mandatory policy_id and validate
it once during registration: in Policy (class/dataclass) add policy_id: str,
then in PolicyEngine.register (the block that previously accessed
policy.policy_id/name) first check that policy.policy_id exists and is a
non-empty string, look up that exact key in self._config["policies"] and if
missing raise an explicit error; store the resolved policy_id and its
policy_config in local variables and use those for deciding enabled and
appending to self._policies (instead of redoing a name-based fallback that
dereferences policy.policy_id earlier). Finally, update compute_policy_hash to
use the same resolved policy_id and the stored/validated policy_config (from the
same config lookup) when computing the hash so the recorded hash matches the
config actually used.
---
Nitpick comments:
In `@backend/app/services/policy/engine.py`:
- Around line 208-213: The code recomputes policy hashes during evaluation
(calling self.compute_policy_hash(p)) even though policy_set() already computed
and froze each policy_hash; replace those recomputations with the cached value
(use the Policy object's policy_hash field or the frozen PolicyDescriptor) in
the block constructing PolicyDescriptor (and the similar block at 279-289) so
you read p.policy_hash (or the existing descriptor.policy_hash) instead of
calling compute_policy_hash; ensure you remove the expensive normalization call
and rely on the precomputed hash to avoid drift across reloads.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 12851568-565b-4eac-9402-9b1ae28e9d62
📒 Files selected for processing (1)
backend/app/services/policy/engine.py
support explicit policy_id registration
Summary by CodeRabbit
Bug Fixes
Improvements