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 | from __future__ import annotations |
| 3 | |
| 4 | from agent_guard.models import Decision, Finding, Policy, PolicyMode, RiskLevel |
| 5 | from agent_guard.policies import apply_mode_adjustment, apply_policy, most_restrictive |
| 6 | |
| 7 | |
| 8 | def test_precedence() -> None: |
| 9 | assert most_restrictive(Decision.ALLOW, Decision.WARN, Decision.BLOCK) == Decision.BLOCK |
| 10 | assert most_restrictive(Decision.ALLOW, Decision.WARN) == Decision.WARN |
| 11 | |
| 12 | |
| 13 | def test_strict_elevates_warn() -> None: |
| 14 | d = apply_mode_adjustment(Decision.WARN, RiskLevel.HIGH, PolicyMode.STRICT) |
| 15 | assert d == Decision.BLOCK |
| 16 | |
| 17 | |
| 18 | def test_permissive_demotes_non_critical_block() -> None: |
| 19 | d = apply_mode_adjustment(Decision.BLOCK, RiskLevel.HIGH, PolicyMode.PERMISSIVE) |
| 20 | assert d == Decision.WARN |
| 21 | |
| 22 | |
| 23 | def test_permissive_keeps_critical() -> None: |
| 24 | d = apply_mode_adjustment(Decision.BLOCK, RiskLevel.CRITICAL, PolicyMode.PERMISSIVE) |
| 25 | assert d == Decision.BLOCK |
| 26 | |
| 27 | |
| 28 | def test_apply_policy() -> None: |
| 29 | policy = Policy( |
| 30 | name="default", |
| 31 | mode=PolicyMode.DEFAULT, |
| 32 | rules={"destructive_git": Decision.WARN}, |
| 33 | ) |
| 34 | findings = [ |
| 35 | Finding( |
| 36 | rule_id="git.reset_hard", |
| 37 | category="destructive_git", |
| 38 | risk=RiskLevel.HIGH, |
| 39 | decision=Decision.WARN, |
| 40 | reason="x", |
| 41 | score_delta=55, |
| 42 | ) |
| 43 | ] |
| 44 | assert apply_policy(findings, policy, RiskLevel.HIGH) == Decision.WARN |