Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /tests /test_autonomous_agent.py
"""Tests for faultray.remediation.autonomous_agent.
Covers the full detect -> plan -> simulate -> execute -> verify cycle,
ratchet permission enforcement, persistence, and edge cases.
"""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from faultray.model.components import (
AutoScalingConfig,
Capacity,
Component,
ComponentType,
Dependency,
FailoverConfig,
HealthStatus,
ResourceMetrics,
SecurityProfile,
)
from faultray.model.graph import InfraGraph
from faultray.remediation.autonomous_agent import (
AutonomousRemediationAgent,
RemediationCycle,
StepResult,
_PlanStep,
_SimResult,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _comp(
cid: str,
name: str,
ctype: ComponentType = ComponentType.APP_SERVER,
replicas: int = 1,
cpu: float = 0.0,
memory: float = 0.0,
health: HealthStatus = HealthStatus.HEALTHY,
failover: bool = False,
autoscaling: bool = False,
backup: bool = False,
encryption: bool = False,
) -> Component:
c = Component(id=cid, name=name, type=ctype, replicas=replicas)
c.metrics = ResourceMetrics(cpu_percent=cpu, memory_percent=memory)
c.capacity = Capacity()
c.health = health
if failover:
c.failover = FailoverConfig(enabled=True)
if autoscaling:
c.autoscaling = AutoScalingConfig(enabled=True)
c.security = SecurityProfile(
backup_enabled=backup, encryption_at_rest=encryption
)
return c
def _graph(*comps: Component) -> InfraGraph:
g = InfraGraph()
for c in comps:
g.add_component(c)
return g
def _healthy_graph() -> InfraGraph:
"""A well-configured graph that produces no remediation issues."""
return _graph(
_comp(
"web",
"Web Server",
ComponentType.WEB_SERVER,
replicas=3,
failover=True,
autoscaling=True,
backup=True,
encryption=True,
),
_comp(
"db",
"Database",
ComponentType.DATABASE,
replicas=3,
failover=True,
backup=True,
encryption=True,
),
)
def _unhealthy_graph() -> InfraGraph:
"""A graph with many issues for remediation."""
g = _graph(
_comp("web", "Web Server", ComponentType.WEB_SERVER, replicas=1),
_comp("app", "App Server", ComponentType.APP_SERVER, replicas=1, cpu=95.0),
_comp("db", "Database", ComponentType.DATABASE, replicas=1),
_comp("cache", "Cache", ComponentType.CACHE, replicas=1),
)
g.add_dependency(Dependency(source_id="web", target_id="app"))
g.add_dependency(Dependency(source_id="app", target_id="db"))
g.add_dependency(Dependency(source_id="app", target_id="cache"))
return g
def _make_agent(
tmp_path: Path,
graph: InfraGraph | None = None,
auto_approve: bool = False,
dry_run: bool = True,
max_risk: str = "medium",
ratchet_enabled: bool = True,
) -> AutonomousRemediationAgent:
"""Create an agent with a saved model in tmp_path."""
model_path = tmp_path / "model.json"
g = graph or _unhealthy_graph()
g.save(model_path)
return AutonomousRemediationAgent(
model_path=str(model_path),
auto_approve=auto_approve,
max_risk_level=max_risk,
ratchet_enabled=ratchet_enabled,
dry_run=dry_run,
output_dir=str(tmp_path / "remediation"),
)
# ===========================================================================
# 1. Full dry-run cycle (detect -> plan -> simulate -> complete)
# ===========================================================================
class TestFullDryRunCycle:
def test_dry_run_cycle_completes(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
assert cycle.status in ("completed", "failed", "awaiting_approval")
assert cycle.id
assert cycle.started_at
def test_dry_run_cycle_has_initial_score(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
# Score is computed from the graph
assert isinstance(cycle.initial_score, float)
def test_dry_run_execution_log_shows_dry_run(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.execution_log:
statuses = {e["status"] for e in cycle.execution_log}
# In dry-run mode, steps are "dry_run" or "blocked"
assert statuses <= {"dry_run", "blocked"}
# ===========================================================================
# 2. No issues found
# ===========================================================================
class TestNoIssuesFound:
def test_healthy_graph_no_issues(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, graph=_healthy_graph(), auto_approve=True)
cycle = agent.run_cycle()
# Healthy graph might still have some issues detected by the engine
# but the cycle should complete
assert cycle.status in ("completed", "failed")
def test_empty_graph_completes(self, tmp_path: Path) -> None:
g = InfraGraph()
agent = _make_agent(tmp_path, graph=g, auto_approve=True)
cycle = agent.run_cycle()
assert cycle.status == "completed"
assert len(cycle.issues_found) == 0
# ===========================================================================
# 3. Issue extraction from simulation report
# ===========================================================================
class TestIssueExtraction:
def test_unhealthy_graph_finds_issues(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
assert len(cycle.issues_found) > 0
def test_issues_have_expected_fields(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.issues_found:
issue = cycle.issues_found[0]
assert "plan_id" in issue
assert "description" in issue
assert "priority" in issue
assert "affected_components" in issue
# ===========================================================================
# 4. Plan generation produces valid IaC
# ===========================================================================
class TestPlanGeneration:
def test_plan_is_generated(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
# If issues were found, a plan should be generated
if cycle.issues_found:
assert cycle.remediation_plan is not None
def test_plan_has_cost_estimate(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.remediation_plan:
assert cycle.estimated_cost != "$0.00"
# ===========================================================================
# 5. Ratchet blocks unauthorized operations
# ===========================================================================
class TestRatchetBlocking:
def test_ratchet_narrows_after_high_risk(self, tmp_path: Path) -> None:
"""After executing a high-risk step, ratchet should narrow permissions."""
agent = _make_agent(tmp_path, auto_approve=True, ratchet_enabled=True)
cycle = agent.run_cycle()
if cycle.ratchet_state:
perms = set(cycle.ratchet_state.get("remaining_permissions", []))
# After high-risk access, write/send perms should be gone
# (depends on what steps were actually executed)
assert isinstance(perms, set)
def test_ratchet_disabled_preserves_all_perms(self, tmp_path: Path) -> None:
agent = _make_agent(
tmp_path, auto_approve=True, ratchet_enabled=False
)
cycle = agent.run_cycle()
if cycle.ratchet_state:
perms = set(cycle.ratchet_state.get("remaining_permissions", []))
# Without ratchet, permissions should include more scopes
assert "write:internal" in perms or len(perms) > 0
def test_blocked_steps_logged(self, tmp_path: Path) -> None:
"""If ratchet blocks a step, it should appear in execution log."""
agent = _make_agent(tmp_path, auto_approve=True, ratchet_enabled=True)
cycle = agent.run_cycle()
blocked = [
e for e in cycle.execution_log if e.get("status") == "blocked"
]
# Some steps may be blocked after high-risk operations narrow permissions
# This is expected behavior — just verify they're logged correctly
for entry in blocked:
assert "reason" in entry
# ===========================================================================
# 6. Ratchet narrows permissions after production access
# ===========================================================================
class TestRatchetNarrowing:
def test_ratchet_state_is_recorded(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.execution_log:
assert "ratchet_state" in cycle.to_dict()
def test_ratchet_high_water_mark(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True, ratchet_enabled=True)
cycle = agent.run_cycle()
if cycle.ratchet_state:
level = cycle.ratchet_state.get("final_level")
assert level is not None
# ===========================================================================
# 7. Auto-approve vs manual approve flow
# ===========================================================================
class TestApprovalFlow:
def test_manual_approve_pauses_at_approval(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=False)
cycle = agent.run_cycle()
# If issues found, should pause at awaiting_approval
if cycle.issues_found and cycle.simulation_passed:
assert cycle.status == "awaiting_approval"
# Execution log should be empty (not yet executed)
assert len(cycle.execution_log) == 0
def test_auto_approve_skips_approval(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
# Should NOT be awaiting_approval
assert cycle.status != "awaiting_approval"
# ===========================================================================
# 8. Verification detects improvement
# ===========================================================================
class TestVerification:
def test_completed_cycle_has_final_score(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status == "completed" and cycle.issues_found:
assert cycle.final_score is not None
def test_improvement_is_calculated(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status == "completed" and cycle.issues_found:
assert cycle.improvement_achieved is not None
# ===========================================================================
# 9. Report generation
# ===========================================================================
class TestReportGeneration:
def test_report_summary_is_set(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
assert cycle.report_summary
def test_markdown_report_saved(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status in ("completed", "failed", "rolled_back"):
report = agent.get_latest_report()
if report:
assert "FaultRay" in report
def test_json_report_saved(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status in ("completed", "failed", "rolled_back"):
data = agent.get_latest_report_json()
if data:
assert "id" in data
assert "status" in data
# ===========================================================================
# 10. Cycle persistence (save/load)
# ===========================================================================
class TestCyclePersistence:
def test_save_and_load_cycle(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=False)
cycle = agent.run_cycle()
# The cycle should be saved
loaded = agent._load_cycle(cycle.id)
assert loaded.id == cycle.id
assert loaded.status == cycle.status
assert loaded.initial_score == cycle.initial_score
def test_load_nonexistent_raises(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
with pytest.raises(FileNotFoundError):
agent._load_cycle("nonexistent-id")
def test_list_history(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
agent.run_cycle()
history = agent.list_history()
assert len(history) >= 1
def test_list_pending(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=False)
cycle = agent.run_cycle()
pending = agent.list_pending()
if cycle.status == "awaiting_approval":
assert any(p.id == cycle.id for p in pending)
# ===========================================================================
# 11. Simulation failure
# ===========================================================================
class TestSimulationFailure:
def _regressing_plan(self):
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
return RemediationPlan(
expected_score_before=80.0,
expected_score_after=70.0,
files=[
RemediationFile(
path="bad.tf", content="resource {}", description="Bad fix",
phase=3, impact_score_delta=-10.0, monthly_cost=0.0, category="dr",
),
],
)
def test_measured_regression_fails_simulation(self, tmp_path: Path) -> None:
"""A fix whose INDEPENDENTLY MEASURED effect lowers the score fails."""
agent = _make_agent(tmp_path)
# Force the measured effect to be a regression (90 -> 50).
agent._measure_fix_effect = lambda g, p=None: (90.0, 50.0) # type: ignore[method-assign]
result = agent._simulate_with_fix(_unhealthy_graph(), self._regressing_plan())
assert not result.passed
assert result.new_score == 50.0 # measured score is authoritative
assert any("Measured score would decrease" in s for s in result.side_effects)
def test_projection_regression_fails_when_unmeasurable(self, tmp_path: Path) -> None:
"""When independent measurement is unavailable, fall back to the
projection — a projected regression still fails (unverified)."""
agent = _make_agent(tmp_path)
agent._measure_fix_effect = lambda g, p=None: (None, None) # type: ignore[method-assign]
result = agent._simulate_with_fix(_unhealthy_graph(), self._regressing_plan())
assert not result.passed
assert any("decrease" in s for s in result.side_effects)
def test_empty_plan_passes_simulation(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
from faultray.remediation.iac_generator import RemediationPlan
plan = RemediationPlan()
result = agent._simulate_with_fix(_unhealthy_graph(), plan)
assert result.passed
# ===========================================================================
# 12. _PlanStep and permission helpers
# ===========================================================================
class TestHelpers:
def test_plan_to_steps_terraform(self, tmp_path: Path) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
plan = RemediationPlan(
files=[
RemediationFile(
path="phase1/test.tf",
content="resource {}",
description="Test",
phase=1,
impact_score_delta=5.0,
monthly_cost=100.0,
category="redundancy",
),
]
)
steps = AutonomousRemediationAgent._plan_to_steps(plan)
assert len(steps) == 1
assert steps[0].execution_type == "terraform"
assert steps[0].risk_level == "high"
def test_plan_to_steps_kubernetes(self, tmp_path: Path) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
plan = RemediationPlan(
files=[
RemediationFile(
path="phase1/test.yaml",
content="apiVersion: v1",
description="K8s",
phase=2,
impact_score_delta=3.0,
monthly_cost=0.0,
category="security",
),
]
)
steps = AutonomousRemediationAgent._plan_to_steps(plan)
assert steps[0].execution_type == "kubernetes"
assert steps[0].risk_level == "medium"
def test_step_permissions_terraform(self) -> None:
step = _PlanStep(
description="test",
risk_level="high",
file_path="test.tf",
content="",
execution_type="terraform",
)
perms = AutonomousRemediationAgent._step_permissions(step)
assert "execute:tool" in perms
assert "write:internal" in perms
def test_step_permissions_kubernetes(self) -> None:
step = _PlanStep(
description="test",
risk_level="low",
file_path="test.yaml",
content="",
execution_type="kubernetes",
)
perms = AutonomousRemediationAgent._step_permissions(step)
assert "write:internal" in perms
assert "execute:tool" not in perms
# ===========================================================================
# 13. Cycle serialization
# ===========================================================================
class TestCycleSerialization:
def test_to_dict_round_trip(self) -> None:
cycle = RemediationCycle(
id="test-123",
started_at="2026-01-01T00:00:00Z",
status="completed",
initial_score=60.0,
issues_found=[{"description": "test", "priority": "urgent"}],
estimated_cost="$100.00/mo",
)
d = cycle.to_dict()
assert d["id"] == "test-123"
assert d["status"] == "completed"
assert d["initial_score"] == 60.0
restored = AutonomousRemediationAgent._dict_to_cycle(d)
assert restored.id == cycle.id
assert restored.status == cycle.status
assert restored.initial_score == cycle.initial_score
def test_dict_to_cycle_missing_fields(self) -> None:
"""_dict_to_cycle should handle missing optional fields gracefully."""
data = {
"id": "minimal",
"started_at": "2026-01-01T00:00:00Z",
}
cycle = AutonomousRemediationAgent._dict_to_cycle(data)
assert cycle.id == "minimal"
assert cycle.status == "unknown"
assert cycle.issues_found == []
# ===========================================================================
# 14. StepResult dataclass
# ===========================================================================
class TestStepResult:
def test_step_result_default(self) -> None:
r = StepResult(status="success")
assert r.status == "success"
assert r.output == ""
def test_step_result_with_output(self) -> None:
r = StepResult(status="failed", output="something broke")
assert r.status == "failed"
assert "broke" in r.output
# ===========================================================================
# 15. _SimResult dataclass
# ===========================================================================
class TestSimResult:
def test_sim_result_passed(self) -> None:
r = _SimResult(passed=True, new_score=95.0)
assert r.passed
assert r.side_effects == []
def test_sim_result_failed(self) -> None:
r = _SimResult(passed=False, new_score=50.0, side_effects=["bad"])
assert not r.passed
assert len(r.side_effects) == 1
# ===========================================================================
# 16. Model file not found
# ===========================================================================
class TestModelNotFound:
def test_missing_model_raises(self, tmp_path: Path) -> None:
agent = AutonomousRemediationAgent(
model_path=str(tmp_path / "nonexistent.json"),
output_dir=str(tmp_path / "remediation"),
)
with pytest.raises(FileNotFoundError):
agent.run_cycle()
# ===========================================================================
# 17. Approve non-pending cycle
# ===========================================================================
class TestApproveErrors:
def test_approve_nonexistent_cycle_raises(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
with pytest.raises(FileNotFoundError):
agent.approve_and_execute("does-not-exist")
def test_approve_completed_cycle_raises(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status != "awaiting_approval":
with pytest.raises(ValueError, match="not awaiting approval"):
agent.approve_and_execute(cycle.id)
# ===========================================================================
# 18. Latest report when no reports exist
# ===========================================================================
class TestNoReports:
def test_no_latest_report(self, tmp_path: Path) -> None:
agent = AutonomousRemediationAgent(
model_path="fake.json",
output_dir=str(tmp_path / "empty-remediation"),
)
assert agent.get_latest_report() is None
def test_no_latest_report_json(self, tmp_path: Path) -> None:
agent = AutonomousRemediationAgent(
model_path="fake.json",
output_dir=str(tmp_path / "empty-remediation"),
)
assert agent.get_latest_report_json() is None
# ===========================================================================
# 19. Max risk level filtering
# ===========================================================================
class TestMaxRiskLevel:
def test_low_risk_blocks_high_risk_steps(self, tmp_path: Path) -> None:
"""With max_risk=low, high-risk file phases should produce side effects."""
agent = _make_agent(tmp_path, auto_approve=True, max_risk="low")
cycle = agent.run_cycle()
# The cycle may fail or have side_effects reported
# depending on whether the plan has high-risk files
assert cycle.status in ("completed", "failed")
# ===========================================================================
# 20. Phase-to-risk mapping
# ===========================================================================
class TestPhaseRiskMapping:
def test_phase1_is_high(self) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
plan = RemediationPlan(
files=[
RemediationFile(
path="p1/test.tf",
content="",
description="",
phase=1,
impact_score_delta=0.0,
monthly_cost=0.0,
category="redundancy",
),
]
)
steps = AutonomousRemediationAgent._plan_to_steps(plan)
assert steps[0].risk_level == "high"
def test_phase2_is_medium(self) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
plan = RemediationPlan(
files=[
RemediationFile(
path="p2/test.tf",
content="",
description="",
phase=2,
impact_score_delta=0.0,
monthly_cost=0.0,
category="security",
),
]
)
steps = AutonomousRemediationAgent._plan_to_steps(plan)
assert steps[0].risk_level == "medium"
def test_phase3_is_low(self) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
plan = RemediationPlan(
files=[
RemediationFile(
path="p3/test.tf",
content="",
description="",
phase=3,
impact_score_delta=0.0,
monthly_cost=0.0,
category="dr",
),
]
)
steps = AutonomousRemediationAgent._plan_to_steps(plan)
assert steps[0].risk_level == "low"
# ===========================================================================
# 21. Execute step (subprocess mocking)
# ===========================================================================
class TestExecuteStep:
def _tf_step(self) -> _PlanStep:
return _PlanStep(
description="test tf", risk_level="low", file_path="test.tf",
content='resource "null" "test" {}', execution_type="terraform",
)
def _k8s_step(self) -> _PlanStep:
return _PlanStep(
description="test k8s", risk_level="low", file_path="test.yaml",
content="apiVersion: v1\nkind: ConfigMap", execution_type="kubernetes",
)
def test_execute_terraform_blocked_without_full_optin(self, tmp_path: Path) -> None:
# dry_run=False alone is NOT enough: the boundary guard requires the
# full triple opt-in, so a direct call must not shell out to real infra.
agent = _make_agent(tmp_path, dry_run=False, auto_approve=False)
result = agent._execute_terraform(self._tf_step())
assert result.status == "dry_run"
def test_execute_kubernetes_blocked_without_full_optin(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, dry_run=False, auto_approve=False)
result = agent._execute_kubernetes(self._k8s_step())
assert result.status == "dry_run"
def test_execute_terraform_blocked_writes_no_file(self, tmp_path: Path) -> None:
# The opt-in is checked BEFORE any write, so a blocked call leaves no
# unapproved .tf in the workspace for a later `terraform plan/apply`.
agent = _make_agent(tmp_path, dry_run=False, auto_approve=False)
result = agent._execute_terraform(self._tf_step())
assert result.status == "dry_run"
assert not (tmp_path / "remediation" / "terraform" / "test.tf").exists()
def test_execute_kubernetes_blocked_writes_no_file(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, dry_run=False, auto_approve=False)
result = agent._execute_kubernetes(self._k8s_step())
assert result.status == "dry_run"
assert not (tmp_path / "remediation" / "kubernetes" / "test.yaml").exists()
def test_execute_terraform_attempts_with_full_optin(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Full opt-in: dry_run=False AND auto_approve=True AND env flag.
monkeypatch.setenv("FAULTRAY_ALLOW_AUTO_APPLY", "1")
agent = _make_agent(tmp_path, dry_run=False, auto_approve=True)
result = agent._execute_terraform(self._tf_step())
# terraform CLI is absent in the test env, so it actually attempts and
# fails; a CLI-present runner may instead apply-fail (cluster/init
# errors) -> "apply_failed". Never "dry_run" (it was fully opted in).
assert result.status in ("failed", "timeout", "apply_failed")
def test_execute_kubernetes_attempts_with_full_optin(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("FAULTRAY_ALLOW_AUTO_APPLY", "1")
agent = _make_agent(tmp_path, dry_run=False, auto_approve=True)
result = agent._execute_kubernetes(self._k8s_step())
# CLI-absent runner -> "failed" (FileNotFoundError); CLI-present runner
# reaches kubectl apply and errors against no cluster -> "apply_failed".
assert result.status in ("failed", "timeout", "apply_failed")
def test_ratchet_stops_after_apply_failed(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# A live apply that errors after starting ("apply_failed") is terminal:
# the agent must NOT keep applying later steps onto a partial/failed
# apply. Stub a 2-step plan whose first step apply-fails.
monkeypatch.setenv("FAULTRAY_ALLOW_AUTO_APPLY", "1")
agent = _make_agent(tmp_path, dry_run=False, auto_approve=True)
steps = [
_PlanStep(description="s1", risk_level="low", file_path="a.tf",
content="", execution_type="terraform"),
_PlanStep(description="s2", risk_level="low", file_path="b.tf",
content="", execution_type="terraform"),
]
agent._plan_to_steps = lambda plan: steps # type: ignore[assignment]
agent._execute_step = ( # type: ignore[assignment]
lambda step: StepResult(status="apply_failed", output="boom")
)
cycle = RemediationCycle(id="t", started_at="2026-01-01T00:00:00Z")
agent._execute_with_ratchet(cycle, object())
# Only the first (apply_failed) step ran; the second was never attempted.
assert [e["status"] for e in cycle.execution_log] == ["apply_failed"]
assert cycle.status == "failed"
def test_execute_step_unknown_type(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, dry_run=False)
step = _PlanStep(
description="custom",
risk_level="low",
file_path="script.sh",
content="echo hello",
execution_type="script",
)
result = agent._execute_step(step)
assert result.status == "success"
# ===========================================================================
# 22. Markdown report rendering
# ===========================================================================
class TestMarkdownReport:
def test_render_contains_expected_sections(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = RemediationCycle(
id="rpt-test",
started_at="2026-01-01T00:00:00Z",
completed_at="2026-01-01T00:01:00Z",
status="completed",
initial_score=50.0,
simulated_score=80.0,
final_score=75.0,
improvement_achieved=25.0,
issues_found=[
{"description": "DB no replica", "priority": "urgent", "affected_components": ["db"]},
],
execution_log=[
{"step": "Add replica", "status": "dry_run"},
],
ratchet_state={"final_level": "RESTRICTED", "remaining_permissions": ["read:internal"]},
)
md = agent._render_markdown_report(cycle)
assert "FaultRay Remediation Report" in md
assert "Score" in md
assert "Execution Log" in md
assert "Ratchet State" in md
# ===========================================================================
# 23. Cycle completed_at timestamp
# ===========================================================================
class TestTimestamps:
def test_completed_cycle_has_completed_at(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=True)
cycle = agent.run_cycle()
if cycle.status in ("completed", "failed", "rolled_back"):
assert cycle.completed_at is not None
def test_pending_cycle_no_completed_at(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path, auto_approve=False)
cycle = agent.run_cycle()
if cycle.status == "awaiting_approval":
assert cycle.completed_at is None
# ===========================================================================
# 24. Execution failure sets status to failed
# ===========================================================================
class TestExecutionFailure:
def test_failed_step_sets_cycle_failed(self, tmp_path: Path) -> None:
"""If _execute_step returns failed, cycle status should be failed."""
agent = _make_agent(tmp_path, auto_approve=True, dry_run=False)
# This will likely fail because terraform/kubectl aren't installed
cycle = agent.run_cycle()
# The cycle could be completed (no execution steps) or failed
assert cycle.status in ("completed", "failed", "rolled_back")
# ===========================================================================
# 25. Cost / step hard stops in simulation (fail-safe, not advisory)
# ===========================================================================
def _one_file_plan(cost: float, *, files: int = 1):
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
return RemediationPlan(
total_monthly_cost=cost,
expected_score_before=50.0,
expected_score_after=80.0,
files=[
RemediationFile(
path=f"fix-{i}.tf",
content="resource {}",
description="Fix",
phase=3,
impact_score_delta=30.0,
monthly_cost=cost / max(files, 1),
category="dr",
)
for i in range(files)
],
)
class TestCostAndStepHardStops:
def test_cost_under_cap_passes_without_warning(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path) # default cap $100k/mo
result = agent._simulate_with_fix(_unhealthy_graph(), _one_file_plan(15000.0))
assert result.passed
assert not any("cost" in s.lower() for s in result.side_effects)
def test_cost_over_cap_fails_closed(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
# A runaway plan must NOT pass simulation (previously the cost carve-out
# let any cost through with only an advisory string).
result = agent._simulate_with_fix(_unhealthy_graph(), _one_file_plan(1_000_000.0))
assert result.passed is False
assert any("safety cap" in s for s in result.side_effects)
def test_configurable_cost_cap(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
agent.max_monthly_cost = 5000.0
result = agent._simulate_with_fix(_unhealthy_graph(), _one_file_plan(8000.0))
assert result.passed is False
def test_too_many_steps_fails_closed(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
agent.max_steps = 10
result = agent._simulate_with_fix(_unhealthy_graph(), _one_file_plan(100.0, files=11))
assert result.passed is False
assert any("steps" in s for s in result.side_effects)
# ===========================================================================
# Independent SIMULATE measurement (Wave 1a)
# ===========================================================================
class TestIndependentSimulation:
def test_every_remediation_rule_is_mapped_to_a_mutation(self) -> None:
"""Drift guard: a new IaC rule without a graph mutation would silently
make the independent simulation under-measure."""
from faultray.remediation.iac_generator import REMEDIATION_RULES
from faultray.remediation.autonomous_agent import _RULE_MUTATIONS
unmapped = {r.key for r in REMEDIATION_RULES} - set(_RULE_MUTATIONS)
assert not unmapped, f"rules missing a structural mutation: {unmapped}"
def test_apply_intent_improves_and_does_not_mutate_original(self) -> None:
import copy
from faultray.remediation.autonomous_agent import _apply_remediation_intent
g = _unhealthy_graph()
before = g.resilience_score()
patched = copy.deepcopy(g)
applied = _apply_remediation_intent(patched)
assert applied > 0
assert patched.resilience_score() >= before
assert g.resilience_score() == before # original untouched
def test_simulate_uses_measured_score_not_projection(self, tmp_path: Path) -> None:
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
agent = _make_agent(tmp_path)
plan = RemediationPlan(
expected_score_before=0.0,
expected_score_after=999.0, # absurd projection must be ignored
files=[RemediationFile(path="x.tf", content="r {}", description="d",
phase=3, impact_score_delta=999.0, monthly_cost=1.0,
category="dr")],
)
result = agent._simulate_with_fix(_unhealthy_graph(), plan)
assert result.new_score != 999.0
def test_unmeasurable_fix_fails_closed_even_when_projection_improves(
self, tmp_path: Path
) -> None:
# The independent measurement IS the safety gate. When it is unavailable
# (e.g. the scorer raised), the simulation must FAIL CLOSED — never
# approve a plan on the generator's optimistic projection alone, even
# when that projection claims a big improvement.
from faultray.remediation.iac_generator import RemediationFile, RemediationPlan
agent = _make_agent(tmp_path)
agent._measure_fix_effect = lambda g, p=None: (None, None) # type: ignore[method-assign]
plan = RemediationPlan(
expected_score_before=10.0,
expected_score_after=90.0, # optimistic improvement, NOT a regression
files=[RemediationFile(path="x.tf", content="r {}", description="d",
phase=3, impact_score_delta=80.0, monthly_cost=1.0,
category="dr")],
)
result = agent._simulate_with_fix(_unhealthy_graph(), plan)
assert result.passed is False
assert any(
"UNVERIFIED" in s or "could not be independently measured" in s
for s in result.side_effects
)
# ===========================================================================
# Real rollback vs honest status (Wave 1c)
# ===========================================================================
class TestVerifyRollback:
def _regressing_cycle(self, **kw) -> RemediationCycle:
# initial_score far above any real graph score forces improvement < 0.
cycle = RemediationCycle(id="t", started_at="now")
cycle.initial_score = 1000.0
for k, v in kw.items():
setattr(cycle, k, v)
return cycle
def test_dry_run_regression_reported_not_rolled_back(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
cycle = self._regressing_cycle(
execution_log=[{"step": "x", "status": "dry_run"}]
)
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "regressed"
assert "nothing to roll back" in cycle.report_summary
def test_applied_regression_triggers_real_rollback(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
agent._rollback_step = lambda et, fp: True # type: ignore[method-assign]
cycle = self._regressing_cycle(execution_log=[
{"step": "tf", "status": "success",
"execution_type": "terraform", "file_path": "a.tf"},
])
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "rolled_back"
def test_timeout_regression_triggers_real_rollback(self, tmp_path: Path) -> None:
# A timed-out live apply may have partially applied; a regression after
# it must attempt rollback, not be dismissed as "nothing applied".
agent = _make_agent(tmp_path)
agent._rollback_step = lambda et, fp: True # type: ignore[method-assign]
cycle = self._regressing_cycle(execution_log=[
{"step": "tf", "status": "timeout",
"execution_type": "terraform", "file_path": "a.tf"},
])
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "rolled_back"
def test_apply_failed_regression_triggers_real_rollback(self, tmp_path: Path) -> None:
# An apply that exited non-zero after starting may have partially
# applied (Terraform records created resources, no auto-rollback), so a
# regression afterward must attempt rollback, not be dismissed.
agent = _make_agent(tmp_path)
agent._rollback_step = lambda et, fp: True # type: ignore[method-assign]
cycle = self._regressing_cycle(execution_log=[
{"step": "tf", "status": "apply_failed",
"execution_type": "terraform", "file_path": "a.tf"},
])
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "rolled_back"
def test_verify_preserves_failed_apply_no_regression_rolls_back(
self, tmp_path: Path
) -> None:
# A live apply that failed/timed out but did NOT lower the score must
# still end as a failure (never "completed"): _verify resets the status
# to "verifying" and would otherwise mark it "completed" on a
# non-regressing score. Partial changes are rolled back and the failure
# is preserved in status + report.
agent = _make_agent(tmp_path)
agent._load_or_discover = lambda: _unhealthy_graph() # type: ignore[method-assign]
agent._rollback_step = lambda et, fp: True # type: ignore[method-assign]
cycle = RemediationCycle(id="t", started_at="now")
cycle.initial_score = 0.0 # any real score >= 0 -> no regression
cycle.status = "failed" # set terminally by the execution step loop
cycle.execution_log = [
{"step": "tf", "status": "apply_failed",
"execution_type": "terraform", "file_path": "a.tf"},
]
agent._verify(cycle, _unhealthy_graph())
assert cycle.improvement_achieved >= 0 # not a regression
assert cycle.status == "failed" # NOT "completed"
assert "rolled back" in cycle.report_summary.lower()
def test_verify_failed_apply_rollback_failure_remains_applied(
self, tmp_path: Path
) -> None:
# If the partial changes from a failed apply cannot be undone, surface
# rollback_failed with the "REMAIN APPLIED" warning even without a score
# regression.
agent = _make_agent(tmp_path)
agent._load_or_discover = lambda: _unhealthy_graph() # type: ignore[method-assign]
agent._rollback_step = lambda et, fp: False # type: ignore[method-assign]
cycle = RemediationCycle(id="t", started_at="now")
cycle.initial_score = 0.0
cycle.status = "failed"
cycle.execution_log = [
{"step": "tf", "status": "apply_failed",
"execution_type": "terraform", "file_path": "a.tf"},
]
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "rollback_failed"
assert "REMAIN APPLIED" in cycle.report_summary
def test_verify_failed_plan_phase_no_applied_steps_stays_failed(
self, tmp_path: Path
) -> None:
# A plan-phase failure (step status "failed", excluded from the applied
# set) with no real changes must report "failed", never "completed".
agent = _make_agent(tmp_path)
agent._load_or_discover = lambda: _unhealthy_graph() # type: ignore[method-assign]
cycle = RemediationCycle(id="t", started_at="now")
cycle.initial_score = 0.0
cycle.status = "failed"
cycle.execution_log = [
{"step": "tf", "status": "failed",
"execution_type": "terraform", "file_path": "a.tf"},
]
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "failed"
assert "no changes recorded as applied" in cycle.report_summary
def test_failed_rollback_is_surfaced(self, tmp_path: Path) -> None:
agent = _make_agent(tmp_path)
agent._rollback_step = lambda et, fp: False # type: ignore[method-assign]
cycle = self._regressing_cycle(execution_log=[
{"step": "tf", "status": "success",
"execution_type": "terraform", "file_path": "a.tf"},
])
agent._verify(cycle, _unhealthy_graph())
assert cycle.status == "rollback_failed"
assert "REMAIN APPLIED" in cycle.report_summary
def test_rollback_step_blocked_without_optin(self, tmp_path: Path) -> None:
# The undo is itself gated: no live-apply opt-in => cannot run destroy.
agent = _make_agent(tmp_path, dry_run=True)
assert agent._rollback_step("terraform", "a.tf") is False
class TestTargetedRollback:
def test_terraform_targets_extracted_from_file(self, tmp_path: Path) -> None:
from faultray.remediation.autonomous_agent import AutonomousRemediationAgent
p = tmp_path / "x.tf"
p.write_text(
'resource "aws_db_instance" "replica" {}\n'
'resource "aws_s3_bucket" "b" {}\n'
)
assert AutonomousRemediationAgent._terraform_targets(p) == [
"aws_db_instance.replica",
"aws_s3_bucket.b",
]
def test_rollback_refuses_untargeted_destroy(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Full opt-in so the gate is open, but no .tf file exists -> no targets
# -> rollback must REFUSE (never run an untargeted destroy).
monkeypatch.setenv("FAULTRAY_ALLOW_AUTO_APPLY", "1")
agent = _make_agent(tmp_path, dry_run=False, auto_approve=True)
assert agent._rollback_step("terraform", "missing.tf") is False
"""Tests for faultray.remediation.autonomous_agent.
Covers the full detect -> plan -> simulate -> execute -> verify cycle,
ratchet permission enforcement, persistence, and edge cases.
"""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from faultray.model.components import (
AutoScalingConfig,
Capacity,
Component,
ComponentType,
Dependency,
FailoverConfig,
HealthStatus,
ResourceMetrics,
SecurityProfile,
)
from faultray.model.graph import InfraGraph
from faultray.remediation.autonomous_agent import (
AutonomousRemediationAgent,
RemediationCycle,
StepResult,
_PlanStep,
_SimResult,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _comp(
cid: str,
name: str,
ctype: ComponentType = ComponentType.APP_SERVER,
replicas: int = 1,
cpu: float = 0.0,
memory: float = 0.0,
health: HealthStatus = HealthStatus.HEALTHY,
failover: bool = False,
autoscaling: bool = False,
backup: bool = False,
encryption: bool = False,
) -> Component:
c = Component(id=cid, name=name, type=ctype, replicas=replicas)
c.metrics = ResourceMetrics(cpu_percent=cpu, memory_percent=memory)
c.capacity = Capacity()
c.health = health
if failover:
c.failover = FailoverConfig(enabled=True)
if autoscaling:
c.autoscaling = AutoScalingConfig(enabled=True)
c.security = SecurityProfile(
backup_enabled=backup, encryption_at_rest=encryption
)
return c
def _graph(*comps: Component) -> InfraGraph:
g = InfraGraph()
for c in comps:
g.add_component(c)
return g
def _healthy_graph() -> InfraGraph:
"""A well-configured graph that produces no remediation issues."""
return _graph(
_comp(
"web",
"Web Server",
ComponentType.WEB_SERVER,
replicas=3,
failover=True,
autoscaling=True,
backup=True,
encryption=True,
),
_comp(
"db",
"Database",
ComponentType.DATABASE,
replicas=3,
failover=True,
backup=True,
encryption=True,
),
)
def _unhealthy_graph() -> InfraGraph:
Xet Storage Details
- Size:
- 47.2 kB
- Xet hash:
- d238b698cbd56ab24f77a0ac2b240305cc86f4d42efe8c14f8f52fb3d400525a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.