Create azure_simulator.py
Browse files
infrastructure/azure/azure_simulator.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 agentic_reliability_framework.infrastructure.intents import (
|
| 16 |
+
InfrastructureIntent,
|
| 17 |
+
ProvisionResourceIntent,
|
| 18 |
+
)
|
| 19 |
+
from agentic_reliability_framework.infrastructure.policies import (
|
| 20 |
+
Policy,
|
| 21 |
+
PolicyEvaluator,
|
| 22 |
+
CostThresholdPolicy,
|
| 23 |
+
)
|
| 24 |
+
from agentic_reliability_framework.infrastructure.cost_estimator import CostEstimator
|
| 25 |
+
from agentic_reliability_framework.infrastructure.risk_engine import RiskEngine
|
| 26 |
+
from agentic_reliability_framework.infrastructure.healing_intent import (
|
| 27 |
+
HealingIntent,
|
| 28 |
+
RecommendedAction,
|
| 29 |
+
IntentSource,
|
| 30 |
+
)
|
| 31 |
+
from agentic_reliability_framework.constants import MAX_POLICY_VIOLATIONS
|
| 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:
|
| 94 |
+
recommended_action = RecommendedAction.ESCALATE
|
| 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}.")
|
| 102 |
+
if violations:
|
| 103 |
+
justification_parts.append(f"Policy violations: {'; '.join(violations)}.")
|
| 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,
|
| 114 |
+
"risk_score": risk_score,
|
| 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,
|
| 122 |
+
cost_projection=cost,
|
| 123 |
+
risk_score=risk_score,
|
| 124 |
+
policy_violations=violations,
|
| 125 |
+
recommended_action=recommended_action,
|
| 126 |
+
justification=justification,
|
| 127 |
+
confidence_score=0.9, # could be derived from factor uncertainties
|
| 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()
|