Update infrastructure/azure/azure_simulator.py
Browse files
infrastructure/azure/azure_simulator.py
CHANGED
|
@@ -1,93 +1,54 @@
|
|
| 1 |
-
# agentic_reliability_framework/infrastructure/azure/azure_simulator.py
|
| 2 |
"""
|
| 3 |
Azure Infrastructure Simulator – Main orchestration engine.
|
| 4 |
-
|
| 5 |
-
This module ties together intents, policies, cost estimation, and risk scoring
|
| 6 |
-
to produce a HealingIntent. It is the primary entry point for the OSS advisory layer.
|
| 7 |
-
|
| 8 |
-
The simulator is designed to be deterministic, side-effect-free, and easily
|
| 9 |
-
extendable by the enterprise layer (which will replace simulation with actual
|
| 10 |
-
Azure API calls while preserving the same interface).
|
| 11 |
"""
|
| 12 |
|
| 13 |
from typing import List, Optional, Dict, Any
|
| 14 |
|
| 15 |
-
from
|
| 16 |
InfrastructureIntent,
|
| 17 |
ProvisionResourceIntent,
|
| 18 |
)
|
| 19 |
-
from
|
| 20 |
Policy,
|
| 21 |
PolicyEvaluator,
|
| 22 |
CostThresholdPolicy,
|
| 23 |
)
|
| 24 |
-
from
|
| 25 |
-
from
|
| 26 |
-
from
|
| 27 |
HealingIntent,
|
| 28 |
RecommendedAction,
|
| 29 |
IntentSource,
|
| 30 |
)
|
| 31 |
-
from
|
| 32 |
|
| 33 |
|
| 34 |
class AzureInfrastructureSimulator:
|
| 35 |
-
"""
|
| 36 |
-
Orchestrates the evaluation of an infrastructure intent.
|
| 37 |
-
|
| 38 |
-
The simulator uses:
|
| 39 |
-
- A policy evaluator (with a policy tree)
|
| 40 |
-
- A cost estimator
|
| 41 |
-
- A risk engine
|
| 42 |
-
|
| 43 |
-
It returns a HealingIntent with a recommendation, already marked as OSS advisory.
|
| 44 |
-
"""
|
| 45 |
-
|
| 46 |
def __init__(
|
| 47 |
self,
|
| 48 |
policy: Policy,
|
| 49 |
pricing_file: Optional[str] = None,
|
| 50 |
risk_factors: Optional[List] = None,
|
| 51 |
):
|
| 52 |
-
"""
|
| 53 |
-
Initialize the simulator.
|
| 54 |
-
|
| 55 |
-
Args:
|
| 56 |
-
policy: The root policy (a Policy object, possibly composite).
|
| 57 |
-
pricing_file: Optional path to custom pricing YAML.
|
| 58 |
-
risk_factors: Optional list of custom risk factors.
|
| 59 |
-
"""
|
| 60 |
self._policy_evaluator = PolicyEvaluator(policy)
|
| 61 |
self._cost_estimator = CostEstimator(pricing_file)
|
| 62 |
self._risk_engine = RiskEngine(risk_factors if risk_factors else RiskEngine.DEFAULT_FACTORS)
|
| 63 |
|
| 64 |
def evaluate(self, intent: InfrastructureIntent) -> HealingIntent:
|
| 65 |
-
"""
|
| 66 |
-
Evaluate the intent and produce a HealingIntent.
|
| 67 |
-
|
| 68 |
-
This method is pure and deterministic (same inputs → same output).
|
| 69 |
-
The returned HealingIntent is already marked as OSS advisory.
|
| 70 |
-
"""
|
| 71 |
-
# 1. Estimate cost (if applicable)
|
| 72 |
cost = None
|
| 73 |
if isinstance(intent, ProvisionResourceIntent):
|
| 74 |
cost = self._cost_estimator.estimate_monthly_cost(intent)
|
| 75 |
|
| 76 |
-
# 2. Evaluate policies with context (cost)
|
| 77 |
context = {"cost_estimate": cost} if cost is not None else {}
|
| 78 |
violations = self._policy_evaluator.evaluate(intent, context)
|
| 79 |
|
| 80 |
-
# Enforce OSS limit on policy violations
|
| 81 |
if len(violations) > MAX_POLICY_VIOLATIONS:
|
| 82 |
violations = violations[:MAX_POLICY_VIOLATIONS]
|
| 83 |
|
| 84 |
-
# 3. Compute risk
|
| 85 |
risk_score, explanation, contributions = self._risk_engine.calculate_risk(
|
| 86 |
intent, cost, violations
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# 4. Determine recommended action
|
| 90 |
-
# This is a decision rule; can be made configurable.
|
| 91 |
if risk_score > 0.8 or violations:
|
| 92 |
recommended_action = RecommendedAction.DENY
|
| 93 |
elif risk_score > 0.4:
|
|
@@ -95,7 +56,6 @@ class AzureInfrastructureSimulator:
|
|
| 95 |
else:
|
| 96 |
recommended_action = RecommendedAction.APPROVE
|
| 97 |
|
| 98 |
-
# 5. Build justification
|
| 99 |
justification_parts = [f"Risk score: {risk_score:.2f}."]
|
| 100 |
if cost is not None:
|
| 101 |
justification_parts.append(f"Estimated monthly cost: ${cost:.2f}.")
|
|
@@ -104,10 +64,8 @@ class AzureInfrastructureSimulator:
|
|
| 104 |
justification_parts.append(explanation)
|
| 105 |
justification = " ".join(justification_parts)
|
| 106 |
|
| 107 |
-
# 6. Create summary
|
| 108 |
intent_summary = f"{intent.intent_type} requested by {intent.requester}"
|
| 109 |
|
| 110 |
-
# 7. Package evaluation details
|
| 111 |
details = {
|
| 112 |
"cost_estimate": cost,
|
| 113 |
"violations": violations,
|
|
@@ -115,7 +73,6 @@ class AzureInfrastructureSimulator:
|
|
| 115 |
"factor_contributions": contributions,
|
| 116 |
}
|
| 117 |
|
| 118 |
-
# 8. Create the HealingIntent with proper source and then mark as OSS advisory
|
| 119 |
healing_intent = HealingIntent(
|
| 120 |
intent_id=intent.intent_id,
|
| 121 |
intent_summary=intent_summary,
|
|
@@ -124,10 +81,9 @@ class AzureInfrastructureSimulator:
|
|
| 124 |
policy_violations=violations,
|
| 125 |
recommended_action=recommended_action,
|
| 126 |
justification=justification,
|
| 127 |
-
confidence_score=0.9,
|
| 128 |
evaluation_details=details,
|
| 129 |
source=IntentSource.INFRASTRUCTURE_ANALYSIS,
|
| 130 |
)
|
| 131 |
|
| 132 |
-
# Mark as OSS advisory (sets status=OSS_ADVISORY_ONLY and execution_allowed=False)
|
| 133 |
return healing_intent.mark_as_oss_advisory()
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Azure Infrastructure Simulator – Main orchestration engine.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
from typing import List, Optional, Dict, Any
|
| 6 |
|
| 7 |
+
from ..intents import (
|
| 8 |
InfrastructureIntent,
|
| 9 |
ProvisionResourceIntent,
|
| 10 |
)
|
| 11 |
+
from ..policies import (
|
| 12 |
Policy,
|
| 13 |
PolicyEvaluator,
|
| 14 |
CostThresholdPolicy,
|
| 15 |
)
|
| 16 |
+
from ..cost_estimator import CostEstimator
|
| 17 |
+
from ..risk_engine import RiskEngine
|
| 18 |
+
from ..healing_intent import (
|
| 19 |
HealingIntent,
|
| 20 |
RecommendedAction,
|
| 21 |
IntentSource,
|
| 22 |
)
|
| 23 |
+
from ..constants import MAX_POLICY_VIOLATIONS
|
| 24 |
|
| 25 |
|
| 26 |
class AzureInfrastructureSimulator:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def __init__(
|
| 28 |
self,
|
| 29 |
policy: Policy,
|
| 30 |
pricing_file: Optional[str] = None,
|
| 31 |
risk_factors: Optional[List] = None,
|
| 32 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
self._policy_evaluator = PolicyEvaluator(policy)
|
| 34 |
self._cost_estimator = CostEstimator(pricing_file)
|
| 35 |
self._risk_engine = RiskEngine(risk_factors if risk_factors else RiskEngine.DEFAULT_FACTORS)
|
| 36 |
|
| 37 |
def evaluate(self, intent: InfrastructureIntent) -> HealingIntent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
cost = None
|
| 39 |
if isinstance(intent, ProvisionResourceIntent):
|
| 40 |
cost = self._cost_estimator.estimate_monthly_cost(intent)
|
| 41 |
|
|
|
|
| 42 |
context = {"cost_estimate": cost} if cost is not None else {}
|
| 43 |
violations = self._policy_evaluator.evaluate(intent, context)
|
| 44 |
|
|
|
|
| 45 |
if len(violations) > MAX_POLICY_VIOLATIONS:
|
| 46 |
violations = violations[:MAX_POLICY_VIOLATIONS]
|
| 47 |
|
|
|
|
| 48 |
risk_score, explanation, contributions = self._risk_engine.calculate_risk(
|
| 49 |
intent, cost, violations
|
| 50 |
)
|
| 51 |
|
|
|
|
|
|
|
| 52 |
if risk_score > 0.8 or violations:
|
| 53 |
recommended_action = RecommendedAction.DENY
|
| 54 |
elif risk_score > 0.4:
|
|
|
|
| 56 |
else:
|
| 57 |
recommended_action = RecommendedAction.APPROVE
|
| 58 |
|
|
|
|
| 59 |
justification_parts = [f"Risk score: {risk_score:.2f}."]
|
| 60 |
if cost is not None:
|
| 61 |
justification_parts.append(f"Estimated monthly cost: ${cost:.2f}.")
|
|
|
|
| 64 |
justification_parts.append(explanation)
|
| 65 |
justification = " ".join(justification_parts)
|
| 66 |
|
|
|
|
| 67 |
intent_summary = f"{intent.intent_type} requested by {intent.requester}"
|
| 68 |
|
|
|
|
| 69 |
details = {
|
| 70 |
"cost_estimate": cost,
|
| 71 |
"violations": violations,
|
|
|
|
| 73 |
"factor_contributions": contributions,
|
| 74 |
}
|
| 75 |
|
|
|
|
| 76 |
healing_intent = HealingIntent(
|
| 77 |
intent_id=intent.intent_id,
|
| 78 |
intent_summary=intent_summary,
|
|
|
|
| 81 |
policy_violations=violations,
|
| 82 |
recommended_action=recommended_action,
|
| 83 |
justification=justification,
|
| 84 |
+
confidence_score=0.9,
|
| 85 |
evaluation_details=details,
|
| 86 |
source=IntentSource.INFRASTRUCTURE_ANALYSIS,
|
| 87 |
)
|
| 88 |
|
|
|
|
| 89 |
return healing_intent.mark_as_oss_advisory()
|