AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | """Policy resolution and mode adjustments.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | from agent_guard.models import Decision, Finding, Policy, PolicyMode, RiskLevel |
| 7 | |
| 8 | DECISION_RANK = { |
| 9 | Decision.ALLOW: 0, |
| 10 | Decision.WARN: 1, |
| 11 | Decision.BLOCK: 2, |
| 12 | } |
| 13 | |
| 14 | |
| 15 | def most_restrictive(*decisions: Decision) -> Decision: |
| 16 | if not decisions: |
| 17 | return Decision.ALLOW |
| 18 | return max(decisions, key=lambda d: DECISION_RANK[d]) |
| 19 | |
| 20 | |
| 21 | def apply_mode_adjustment( |
| 22 | decision: Decision, |
| 23 | risk: RiskLevel, |
| 24 | mode: PolicyMode, |
| 25 | ) -> Decision: |
| 26 | """ |
| 27 | Adjust decision by policy mode. |
| 28 | |
| 29 | - strict: elevate WARN→BLOCK for HIGH/CRITICAL; MEDIUM WARN stays WARN; |
| 30 | LOW ALLOW may become WARN for HIGH signals already handled by rules |
| 31 | - permissive: demote BLOCK→WARN for non-CRITICAL; WARN→ALLOW for MEDIUM/LOW |
| 32 | - default: no change |
| 33 | """ |
| 34 | if mode == PolicyMode.DEFAULT: |
| 35 | return decision |
| 36 | |
| 37 | if mode == PolicyMode.STRICT: |
| 38 | if decision == Decision.WARN and risk.rank >= RiskLevel.HIGH.rank: |
| 39 | return Decision.BLOCK |
| 40 | if decision == Decision.ALLOW and risk.rank >= RiskLevel.HIGH.rank: |
| 41 | return Decision.WARN |
| 42 | return decision |
| 43 | |
| 44 | if mode == PolicyMode.PERMISSIVE: |
| 45 | # Never auto-allow CRITICAL; never silently drop CRITICAL blocks |
| 46 | if risk == RiskLevel.CRITICAL: |
| 47 | return decision if decision != Decision.ALLOW else Decision.BLOCK |
| 48 | if decision == Decision.BLOCK and risk.rank < RiskLevel.CRITICAL.rank: |
| 49 | return Decision.WARN |
| 50 | if decision == Decision.WARN and risk.rank <= RiskLevel.MEDIUM.rank: |
| 51 | return Decision.ALLOW |
| 52 | return decision |
| 53 | |
| 54 | return decision |
| 55 | |
| 56 | |
| 57 | def resolve_policy_decision( |
| 58 | findings: list[Finding], |
| 59 | policy: Policy, |
| 60 | ) -> Decision: |
| 61 | """ |
| 62 | Apply per-category policy overrides then take the most restrictive decision. |
| 63 | |
| 64 | Informational ALLOW findings (e.g. regenerable build dirs) are not elevated |
| 65 | by category policy — only WARN/BLOCK findings participate in overrides. |
| 66 | """ |
| 67 | if not findings: |
| 68 | return Decision.ALLOW |
| 69 | |
| 70 | decisions: list[Decision] = [] |
| 71 | for finding in findings: |
| 72 | if finding.decision == Decision.ALLOW: |
| 73 | decisions.append(Decision.ALLOW) |
| 74 | continue |
| 75 | key = finding.category |
| 76 | base = finding.decision |
| 77 | overridden = policy.decision_for(key, base) |
| 78 | decisions.append(overridden) |
| 79 | |
| 80 | return most_restrictive(*decisions) |
| 81 | |
| 82 | |
| 83 | def apply_policy( |
| 84 | findings: list[Finding], |
| 85 | policy: Policy, |
| 86 | risk: RiskLevel, |
| 87 | ) -> Decision: |
| 88 | decision = resolve_policy_decision(findings, policy) |
| 89 | return apply_mode_adjustment(decision, risk, policy.mode) |