Spaces:
Paused
Paused
Upload immunoorg/agents/department.py with huggingface_hub
Browse files- immunoorg/agents/department.py +210 -210
immunoorg/agents/department.py
CHANGED
|
@@ -1,210 +1,210 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Department Agents
|
| 3 |
-
=================
|
| 4 |
-
Siloed department agents with conflicting KPIs that approve/deny/delay
|
| 5 |
-
incident response actions based on their own priorities.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from __future__ import annotations
|
| 9 |
-
|
| 10 |
-
import random
|
| 11 |
-
from typing import Any
|
| 12 |
-
|
| 13 |
-
from immunoorg.models import (
|
| 14 |
-
ApprovalRequest, ApprovalStatus, DepartmentType, OrgNode,
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Department behavioral profiles
|
| 19 |
-
DEPARTMENT_PROFILES: dict[DepartmentType, dict[str, Any]] = {
|
| 20 |
-
DepartmentType.IT_OPS: {
|
| 21 |
-
"personality": "pragmatic",
|
| 22 |
-
"primary_concern": "system_uptime",
|
| 23 |
-
"resistant_to": ["isolate_node", "quarantine_traffic"],
|
| 24 |
-
"eager_for": ["restore_backup", "deploy_patch"],
|
| 25 |
-
"risk_tolerance": 0.5,
|
| 26 |
-
"prompt": (
|
| 27 |
-
"You are the IT Operations Director. Your primary KPI is system uptime (99.9% target). "
|
| 28 |
-
"You resist actions that take systems offline but support quick fixes. "
|
| 29 |
-
"When threat is high, you'll cooperate but push for minimal disruption."
|
| 30 |
-
),
|
| 31 |
-
},
|
| 32 |
-
DepartmentType.SECURITY: {
|
| 33 |
-
"personality": "aggressive",
|
| 34 |
-
"primary_concern": "threat_elimination",
|
| 35 |
-
"resistant_to": [],
|
| 36 |
-
"eager_for": ["block_port", "isolate_node", "quarantine_traffic", "rotate_credentials"],
|
| 37 |
-
"risk_tolerance": 0.2,
|
| 38 |
-
"prompt": (
|
| 39 |
-
"You are the CISO. Threats must be eliminated immediately. You favor aggressive "
|
| 40 |
-
"containment even at the cost of downtime. You approve most security actions quickly "
|
| 41 |
-
"and push for stronger policies."
|
| 42 |
-
),
|
| 43 |
-
},
|
| 44 |
-
DepartmentType.ENGINEERING: {
|
| 45 |
-
"personality": "resistant",
|
| 46 |
-
"primary_concern": "feature_velocity",
|
| 47 |
-
"resistant_to": ["rewrite_policy", "update_approval_protocol", "reduce_bureaucracy"],
|
| 48 |
-
"eager_for": ["deploy_patch"],
|
| 49 |
-
"risk_tolerance": 0.6,
|
| 50 |
-
"prompt": (
|
| 51 |
-
"You are the VP of Engineering. Your team ships features and meeting deadlines is critical. "
|
| 52 |
-
"Security measures that slow deployment are unwelcome. You cooperate only when the threat "
|
| 53 |
-
"is clearly severe and well-documented."
|
| 54 |
-
),
|
| 55 |
-
},
|
| 56 |
-
DepartmentType.DEVOPS: {
|
| 57 |
-
"personality": "pragmatic",
|
| 58 |
-
"primary_concern": "deployment_speed",
|
| 59 |
-
"resistant_to": ["update_approval_protocol"],
|
| 60 |
-
"eager_for": ["deploy_patch", "restore_backup", "enable_ids"],
|
| 61 |
-
"risk_tolerance": 0.4,
|
| 62 |
-
"prompt": (
|
| 63 |
-
"You are the DevOps Lead. You value fast deployments and reliable pipelines. "
|
| 64 |
-
"You support automated fixes but resist adding approval gates that slow things down."
|
| 65 |
-
),
|
| 66 |
-
},
|
| 67 |
-
DepartmentType.MANAGEMENT: {
|
| 68 |
-
"personality": "cautious",
|
| 69 |
-
"primary_concern": "cost_efficiency",
|
| 70 |
-
"resistant_to": ["merge_departments", "add_cross_functional_team"],
|
| 71 |
-
"eager_for": ["reduce_bureaucracy"],
|
| 72 |
-
"risk_tolerance": 0.5,
|
| 73 |
-
"prompt": (
|
| 74 |
-
"You are the CEO. You care about the bottom line and risk management. "
|
| 75 |
-
"Expensive responses need strong justification. You approve structural changes "
|
| 76 |
-
"only when the risk-cost analysis is compelling."
|
| 77 |
-
),
|
| 78 |
-
},
|
| 79 |
-
DepartmentType.LEGAL: {
|
| 80 |
-
"personality": "conservative",
|
| 81 |
-
"primary_concern": "compliance",
|
| 82 |
-
"resistant_to": ["reduce_bureaucracy"],
|
| 83 |
-
"eager_for": ["rewrite_policy", "snapshot_forensics"],
|
| 84 |
-
"risk_tolerance": 0.7,
|
| 85 |
-
"prompt": (
|
| 86 |
-
"You are the General Counsel. Every action must be documented and compliant. "
|
| 87 |
-
"You demand justification before approving and insist on forensic evidence. "
|
| 88 |
-
"You add delay but ensure legal protection."
|
| 89 |
-
),
|
| 90 |
-
},
|
| 91 |
-
DepartmentType.HR: {
|
| 92 |
-
"personality": "protective",
|
| 93 |
-
"primary_concern": "employee_satisfaction",
|
| 94 |
-
"resistant_to": ["merge_departments", "split_department"],
|
| 95 |
-
"eager_for": [],
|
| 96 |
-
"risk_tolerance": 0.5,
|
| 97 |
-
"prompt": (
|
| 98 |
-
"You are the HR Director. Organizational changes affect employee morale. "
|
| 99 |
-
"You resist rapid restructuring and advocate for change management processes. "
|
| 100 |
-
"You cooperate on security but want employee impact minimized."
|
| 101 |
-
),
|
| 102 |
-
},
|
| 103 |
-
DepartmentType.FINANCE: {
|
| 104 |
-
"personality": "analytical",
|
| 105 |
-
"primary_concern": "budget_utilization",
|
| 106 |
-
"resistant_to": ["add_cross_functional_team"],
|
| 107 |
-
"eager_for": ["reduce_bureaucracy"],
|
| 108 |
-
"risk_tolerance": 0.6,
|
| 109 |
-
"prompt": (
|
| 110 |
-
"You are the CFO. Every action has a cost. You support cost-effective responses "
|
| 111 |
-
"but resist expensive measures unless the ROI is clear. You want data before decisions."
|
| 112 |
-
),
|
| 113 |
-
},
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
class DepartmentAgent:
|
| 118 |
-
"""Simulates a department head's decision-making for approval requests."""
|
| 119 |
-
|
| 120 |
-
def __init__(self, org_node: OrgNode, seed: int | None = None):
|
| 121 |
-
self.node = org_node
|
| 122 |
-
self.profile = DEPARTMENT_PROFILES.get(org_node.department_type, {})
|
| 123 |
-
self.rng = random.Random(seed)
|
| 124 |
-
self.approval_history: list[dict[str, Any]] = []
|
| 125 |
-
|
| 126 |
-
def evaluate_request(self, request: ApprovalRequest, threat_level: float) -> ApprovalStatus:
|
| 127 |
-
"""Evaluate an approval request based on department KPIs and personality."""
|
| 128 |
-
score = 0.5 # Base
|
| 129 |
-
|
| 130 |
-
# Threat level influence
|
| 131 |
-
score += threat_level * 0.3
|
| 132 |
-
|
| 133 |
-
# Urgency influence
|
| 134 |
-
score += request.urgency * 0.2
|
| 135 |
-
|
| 136 |
-
# Trust influence
|
| 137 |
-
score += self.node.trust_score * 0.1
|
| 138 |
-
|
| 139 |
-
# Action preference
|
| 140 |
-
if request.action_name in self.profile.get("eager_for", []):
|
| 141 |
-
score += 0.2
|
| 142 |
-
if request.action_name in self.profile.get("resistant_to", []):
|
| 143 |
-
score -= 0.3
|
| 144 |
-
|
| 145 |
-
# Risk tolerance
|
| 146 |
-
risk_tol = self.profile.get("risk_tolerance", 0.5)
|
| 147 |
-
if threat_level > risk_tol:
|
| 148 |
-
score += 0.15 # High threat overrides resistance
|
| 149 |
-
|
| 150 |
-
# Add some noise
|
| 151 |
-
score += self.rng.uniform(-0.05, 0.05)
|
| 152 |
-
|
| 153 |
-
# Decision
|
| 154 |
-
if score >= self.node.cooperation_threshold:
|
| 155 |
-
decision = ApprovalStatus.APPROVED
|
| 156 |
-
elif score >= self.node.cooperation_threshold * 0.6:
|
| 157 |
-
decision = ApprovalStatus.DELAYED
|
| 158 |
-
else:
|
| 159 |
-
decision = ApprovalStatus.DENIED
|
| 160 |
-
|
| 161 |
-
self.approval_history.append({
|
| 162 |
-
"request_id": request.id,
|
| 163 |
-
"action": request.action_name,
|
| 164 |
-
"score": score,
|
| 165 |
-
"decision": decision.value,
|
| 166 |
-
"threat_level": threat_level,
|
| 167 |
-
})
|
| 168 |
-
|
| 169 |
-
return decision
|
| 170 |
-
|
| 171 |
-
def get_prompt(self) -> str:
|
| 172 |
-
"""Get the LLM prompt for this department agent."""
|
| 173 |
-
return self.profile.get("prompt", f"You are the head of {self.node.name}.")
|
| 174 |
-
|
| 175 |
-
def get_cooperation_rate(self) -> float:
|
| 176 |
-
"""Get historical cooperation rate."""
|
| 177 |
-
if not self.approval_history:
|
| 178 |
-
return 0.5
|
| 179 |
-
approved = sum(1 for h in self.approval_history if h["decision"] == "approved")
|
| 180 |
-
return approved / len(self.approval_history)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
class DepartmentAgentPool:
|
| 184 |
-
"""Manages all department agents."""
|
| 185 |
-
|
| 186 |
-
def __init__(self, org_nodes: list[OrgNode], seed: int | None = None):
|
| 187 |
-
self.agents: dict[str, DepartmentAgent] = {}
|
| 188 |
-
for node in org_nodes:
|
| 189 |
-
self.agents[node.id] = DepartmentAgent(node, seed=seed)
|
| 190 |
-
|
| 191 |
-
def get_agent(self, dept_id: str) -> DepartmentAgent | None:
|
| 192 |
-
return self.agents.get(dept_id)
|
| 193 |
-
|
| 194 |
-
def evaluate_all_pending(
|
| 195 |
-
self, requests: list[ApprovalRequest], threat_level: float
|
| 196 |
-
) -> list[tuple[ApprovalRequest, ApprovalStatus]]:
|
| 197 |
-
"""Have all relevant department agents evaluate pending requests."""
|
| 198 |
-
results = []
|
| 199 |
-
for req in requests:
|
| 200 |
-
agent = self.agents.get(req.approver)
|
| 201 |
-
if agent:
|
| 202 |
-
decision = agent.evaluate_request(req, threat_level)
|
| 203 |
-
results.append((req, decision))
|
| 204 |
-
else:
|
| 205 |
-
results.append((req, ApprovalStatus.DENIED))
|
| 206 |
-
return results
|
| 207 |
-
|
| 208 |
-
def get_all_prompts(self) -> dict[str, str]:
|
| 209 |
-
"""Get prompts for all department agents."""
|
| 210 |
-
return {dept_id: agent.get_prompt() for dept_id, agent in self.agents.items()}
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Department Agents
|
| 3 |
+
=================
|
| 4 |
+
Siloed department agents with conflicting KPIs that approve/deny/delay
|
| 5 |
+
incident response actions based on their own priorities.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import random
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
from immunoorg.models import (
|
| 14 |
+
ApprovalRequest, ApprovalStatus, DepartmentType, OrgNode,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Department behavioral profiles
|
| 19 |
+
DEPARTMENT_PROFILES: dict[DepartmentType, dict[str, Any]] = {
|
| 20 |
+
DepartmentType.IT_OPS: {
|
| 21 |
+
"personality": "pragmatic",
|
| 22 |
+
"primary_concern": "system_uptime",
|
| 23 |
+
"resistant_to": ["isolate_node", "quarantine_traffic"],
|
| 24 |
+
"eager_for": ["restore_backup", "deploy_patch"],
|
| 25 |
+
"risk_tolerance": 0.5,
|
| 26 |
+
"prompt": (
|
| 27 |
+
"You are the IT Operations Director. Your primary KPI is system uptime (99.9% target). "
|
| 28 |
+
"You resist actions that take systems offline but support quick fixes. "
|
| 29 |
+
"When threat is high, you'll cooperate but push for minimal disruption."
|
| 30 |
+
),
|
| 31 |
+
},
|
| 32 |
+
DepartmentType.SECURITY: {
|
| 33 |
+
"personality": "aggressive",
|
| 34 |
+
"primary_concern": "threat_elimination",
|
| 35 |
+
"resistant_to": [],
|
| 36 |
+
"eager_for": ["block_port", "isolate_node", "quarantine_traffic", "rotate_credentials"],
|
| 37 |
+
"risk_tolerance": 0.2,
|
| 38 |
+
"prompt": (
|
| 39 |
+
"You are the CISO. Threats must be eliminated immediately. You favor aggressive "
|
| 40 |
+
"containment even at the cost of downtime. You approve most security actions quickly "
|
| 41 |
+
"and push for stronger policies."
|
| 42 |
+
),
|
| 43 |
+
},
|
| 44 |
+
DepartmentType.ENGINEERING: {
|
| 45 |
+
"personality": "resistant",
|
| 46 |
+
"primary_concern": "feature_velocity",
|
| 47 |
+
"resistant_to": ["rewrite_policy", "update_approval_protocol", "reduce_bureaucracy"],
|
| 48 |
+
"eager_for": ["deploy_patch"],
|
| 49 |
+
"risk_tolerance": 0.6,
|
| 50 |
+
"prompt": (
|
| 51 |
+
"You are the VP of Engineering. Your team ships features and meeting deadlines is critical. "
|
| 52 |
+
"Security measures that slow deployment are unwelcome. You cooperate only when the threat "
|
| 53 |
+
"is clearly severe and well-documented."
|
| 54 |
+
),
|
| 55 |
+
},
|
| 56 |
+
DepartmentType.DEVOPS: {
|
| 57 |
+
"personality": "pragmatic",
|
| 58 |
+
"primary_concern": "deployment_speed",
|
| 59 |
+
"resistant_to": ["update_approval_protocol"],
|
| 60 |
+
"eager_for": ["deploy_patch", "restore_backup", "enable_ids"],
|
| 61 |
+
"risk_tolerance": 0.4,
|
| 62 |
+
"prompt": (
|
| 63 |
+
"You are the DevOps Lead. You value fast deployments and reliable pipelines. "
|
| 64 |
+
"You support automated fixes but resist adding approval gates that slow things down."
|
| 65 |
+
),
|
| 66 |
+
},
|
| 67 |
+
DepartmentType.MANAGEMENT: {
|
| 68 |
+
"personality": "cautious",
|
| 69 |
+
"primary_concern": "cost_efficiency",
|
| 70 |
+
"resistant_to": ["merge_departments", "add_cross_functional_team"],
|
| 71 |
+
"eager_for": ["reduce_bureaucracy"],
|
| 72 |
+
"risk_tolerance": 0.5,
|
| 73 |
+
"prompt": (
|
| 74 |
+
"You are the CEO. You care about the bottom line and risk management. "
|
| 75 |
+
"Expensive responses need strong justification. You approve structural changes "
|
| 76 |
+
"only when the risk-cost analysis is compelling."
|
| 77 |
+
),
|
| 78 |
+
},
|
| 79 |
+
DepartmentType.LEGAL: {
|
| 80 |
+
"personality": "conservative",
|
| 81 |
+
"primary_concern": "compliance",
|
| 82 |
+
"resistant_to": ["reduce_bureaucracy"],
|
| 83 |
+
"eager_for": ["rewrite_policy", "snapshot_forensics"],
|
| 84 |
+
"risk_tolerance": 0.7,
|
| 85 |
+
"prompt": (
|
| 86 |
+
"You are the General Counsel. Every action must be documented and compliant. "
|
| 87 |
+
"You demand justification before approving and insist on forensic evidence. "
|
| 88 |
+
"You add delay but ensure legal protection."
|
| 89 |
+
),
|
| 90 |
+
},
|
| 91 |
+
DepartmentType.HR: {
|
| 92 |
+
"personality": "protective",
|
| 93 |
+
"primary_concern": "employee_satisfaction",
|
| 94 |
+
"resistant_to": ["merge_departments", "split_department"],
|
| 95 |
+
"eager_for": [],
|
| 96 |
+
"risk_tolerance": 0.5,
|
| 97 |
+
"prompt": (
|
| 98 |
+
"You are the HR Director. Organizational changes affect employee morale. "
|
| 99 |
+
"You resist rapid restructuring and advocate for change management processes. "
|
| 100 |
+
"You cooperate on security but want employee impact minimized."
|
| 101 |
+
),
|
| 102 |
+
},
|
| 103 |
+
DepartmentType.FINANCE: {
|
| 104 |
+
"personality": "analytical",
|
| 105 |
+
"primary_concern": "budget_utilization",
|
| 106 |
+
"resistant_to": ["add_cross_functional_team"],
|
| 107 |
+
"eager_for": ["reduce_bureaucracy"],
|
| 108 |
+
"risk_tolerance": 0.6,
|
| 109 |
+
"prompt": (
|
| 110 |
+
"You are the CFO. Every action has a cost. You support cost-effective responses "
|
| 111 |
+
"but resist expensive measures unless the ROI is clear. You want data before decisions."
|
| 112 |
+
),
|
| 113 |
+
},
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class DepartmentAgent:
|
| 118 |
+
"""Simulates a department head's decision-making for approval requests."""
|
| 119 |
+
|
| 120 |
+
def __init__(self, org_node: OrgNode, seed: int | None = None):
|
| 121 |
+
self.node = org_node
|
| 122 |
+
self.profile = DEPARTMENT_PROFILES.get(org_node.department_type, {})
|
| 123 |
+
self.rng = random.Random(seed)
|
| 124 |
+
self.approval_history: list[dict[str, Any]] = []
|
| 125 |
+
|
| 126 |
+
def evaluate_request(self, request: ApprovalRequest, threat_level: float) -> ApprovalStatus:
|
| 127 |
+
"""Evaluate an approval request based on department KPIs and personality."""
|
| 128 |
+
score = 0.5 # Base
|
| 129 |
+
|
| 130 |
+
# Threat level influence
|
| 131 |
+
score += threat_level * 0.3
|
| 132 |
+
|
| 133 |
+
# Urgency influence
|
| 134 |
+
score += request.urgency * 0.2
|
| 135 |
+
|
| 136 |
+
# Trust influence
|
| 137 |
+
score += self.node.trust_score * 0.1
|
| 138 |
+
|
| 139 |
+
# Action preference
|
| 140 |
+
if request.action_name in self.profile.get("eager_for", []):
|
| 141 |
+
score += 0.2
|
| 142 |
+
if request.action_name in self.profile.get("resistant_to", []):
|
| 143 |
+
score -= 0.3
|
| 144 |
+
|
| 145 |
+
# Risk tolerance
|
| 146 |
+
risk_tol = self.profile.get("risk_tolerance", 0.5)
|
| 147 |
+
if threat_level > risk_tol:
|
| 148 |
+
score += 0.15 # High threat overrides resistance
|
| 149 |
+
|
| 150 |
+
# Add some noise
|
| 151 |
+
score += self.rng.uniform(-0.05, 0.05)
|
| 152 |
+
|
| 153 |
+
# Decision
|
| 154 |
+
if score >= self.node.cooperation_threshold:
|
| 155 |
+
decision = ApprovalStatus.APPROVED
|
| 156 |
+
elif score >= self.node.cooperation_threshold * 0.6:
|
| 157 |
+
decision = ApprovalStatus.DELAYED
|
| 158 |
+
else:
|
| 159 |
+
decision = ApprovalStatus.DENIED
|
| 160 |
+
|
| 161 |
+
self.approval_history.append({
|
| 162 |
+
"request_id": request.id,
|
| 163 |
+
"action": request.action_name,
|
| 164 |
+
"score": score,
|
| 165 |
+
"decision": decision.value,
|
| 166 |
+
"threat_level": threat_level,
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
return decision
|
| 170 |
+
|
| 171 |
+
def get_prompt(self) -> str:
|
| 172 |
+
"""Get the LLM prompt for this department agent."""
|
| 173 |
+
return self.profile.get("prompt", f"You are the head of {self.node.name}.")
|
| 174 |
+
|
| 175 |
+
def get_cooperation_rate(self) -> float:
|
| 176 |
+
"""Get historical cooperation rate."""
|
| 177 |
+
if not self.approval_history:
|
| 178 |
+
return 0.5
|
| 179 |
+
approved = sum(1 for h in self.approval_history if h["decision"] == "approved")
|
| 180 |
+
return approved / len(self.approval_history)
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
class DepartmentAgentPool:
|
| 184 |
+
"""Manages all department agents."""
|
| 185 |
+
|
| 186 |
+
def __init__(self, org_nodes: list[OrgNode], seed: int | None = None):
|
| 187 |
+
self.agents: dict[str, DepartmentAgent] = {}
|
| 188 |
+
for node in org_nodes:
|
| 189 |
+
self.agents[node.id] = DepartmentAgent(node, seed=seed)
|
| 190 |
+
|
| 191 |
+
def get_agent(self, dept_id: str) -> DepartmentAgent | None:
|
| 192 |
+
return self.agents.get(dept_id)
|
| 193 |
+
|
| 194 |
+
def evaluate_all_pending(
|
| 195 |
+
self, requests: list[ApprovalRequest], threat_level: float
|
| 196 |
+
) -> list[tuple[ApprovalRequest, ApprovalStatus]]:
|
| 197 |
+
"""Have all relevant department agents evaluate pending requests."""
|
| 198 |
+
results = []
|
| 199 |
+
for req in requests:
|
| 200 |
+
agent = self.agents.get(req.approver)
|
| 201 |
+
if agent:
|
| 202 |
+
decision = agent.evaluate_request(req, threat_level)
|
| 203 |
+
results.append((req, decision))
|
| 204 |
+
else:
|
| 205 |
+
results.append((req, ApprovalStatus.DENIED))
|
| 206 |
+
return results
|
| 207 |
+
|
| 208 |
+
def get_all_prompts(self) -> dict[str, str]:
|
| 209 |
+
"""Get prompts for all department agents."""
|
| 210 |
+
return {dept_id: agent.get_prompt() for dept_id, agent in self.agents.items()}
|