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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Psychological Persuasion Layer
|
| 3 |
+
Implements: Loss aversion, social proof, scarcity, authority
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import random
|
| 7 |
+
from typing import Dict, List
|
| 8 |
+
|
| 9 |
+
class PsychologyEngine:
|
| 10 |
+
"""Applies psychological principles to demo presentation"""
|
| 11 |
+
|
| 12 |
+
def __init__(self):
|
| 13 |
+
# Loss aversion scenarios
|
| 14 |
+
self.loss_scenarios = {
|
| 15 |
+
"high": [
|
| 16 |
+
"Data breach ($3.9M average cost)",
|
| 17 |
+
"Service disruption ($300k/hour)",
|
| 18 |
+
"Compliance fines (up to $20M)"
|
| 19 |
+
],
|
| 20 |
+
"medium": [
|
| 21 |
+
"Data corruption (24h recovery time)",
|
| 22 |
+
"Performance degradation (50% slower)",
|
| 23 |
+
"Security vulnerability exposure"
|
| 24 |
+
],
|
| 25 |
+
"low": [
|
| 26 |
+
"Minor configuration drift",
|
| 27 |
+
"Increased operational overhead",
|
| 28 |
+
"Manual review delays"
|
| 29 |
+
]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Social proof statements
|
| 33 |
+
self.social_proofs = {
|
| 34 |
+
"oss": [
|
| 35 |
+
"92% of Enterprise users report reduced incidents",
|
| 36 |
+
"Fortune 500 companies save $2.3M annually with mechanical gates",
|
| 37 |
+
"Developers report 15 minutes saved per decision"
|
| 38 |
+
],
|
| 39 |
+
"trial": [
|
| 40 |
+
"Join 1,000+ developers using ARF",
|
| 41 |
+
"50+ companies started with trial and upgraded",
|
| 42 |
+
"Average user prevents 3 high-risk actions weekly"
|
| 43 |
+
],
|
| 44 |
+
"professional": [
|
| 45 |
+
"Trusted by 200+ scale-ups",
|
| 46 |
+
"Teams report 92% faster incident response",
|
| 47 |
+
"40% reduction in on-call alerts"
|
| 48 |
+
],
|
| 49 |
+
"enterprise": [
|
| 50 |
+
"Deployed at 50+ Fortune 500 companies",
|
| 51 |
+
"99.9% reliability across 1M+ decisions",
|
| 52 |
+
"SOC 2 certified with zero findings"
|
| 53 |
+
]
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Authority signals
|
| 57 |
+
self.authority_signals = [
|
| 58 |
+
"SOC 2 Type II Certified",
|
| 59 |
+
"GDPR & CCPA Compliant",
|
| 60 |
+
"ISO 27001 Certified",
|
| 61 |
+
"99.9% SLA Guarantee",
|
| 62 |
+
"24/7 Dedicated Support",
|
| 63 |
+
"On-prem Deployment Available"
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# Scarcity messages
|
| 67 |
+
self.scarcity_messages = {
|
| 68 |
+
"trial": [
|
| 69 |
+
"⏳ Limited time: {days} days remaining in trial",
|
| 70 |
+
"🎁 Free trial ends soon - upgrade to keep mechanical gates",
|
| 71 |
+
"⚠️ Trial license expires in {days} days"
|
| 72 |
+
],
|
| 73 |
+
"starter": [
|
| 74 |
+
"💰 Special pricing: First 3 months at 50% off",
|
| 75 |
+
"👥 Limited seats available at current price",
|
| 76 |
+
"⏰ Offer ends this quarter"
|
| 77 |
+
]
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
def generate_loss_aversion_message(self, risk_score: float) -> Dict:
|
| 81 |
+
"""Generate loss aversion framing based on risk"""
|
| 82 |
+
if risk_score > 0.7:
|
| 83 |
+
category = "high"
|
| 84 |
+
elif risk_score > 0.4:
|
| 85 |
+
category = "medium"
|
| 86 |
+
else:
|
| 87 |
+
category = "low"
|
| 88 |
+
|
| 89 |
+
scenarios = self.loss_scenarios[category]
|
| 90 |
+
selected = random.sample(scenarios, min(3, len(scenarios)))
|
| 91 |
+
|
| 92 |
+
return {
|
| 93 |
+
"title": f"🚨 Without Enterprise, you risk:",
|
| 94 |
+
"points": selected,
|
| 95 |
+
"category": category,
|
| 96 |
+
"risk_score": risk_score
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
def generate_social_proof(self, license_tier: str) -> str:
|
| 100 |
+
"""Generate tier-specific social proof"""
|
| 101 |
+
proofs = self.social_proofs.get(license_tier, self.social_proofs["oss"])
|
| 102 |
+
return random.choice(proofs)
|
| 103 |
+
|
| 104 |
+
def generate_scarcity_message(self, license_tier: str, days_remaining: int = 14) -> str:
|
| 105 |
+
"""Generate scarcity messaging"""
|
| 106 |
+
if license_tier in self.scarcity_messages:
|
| 107 |
+
messages = self.scarcity_messages[license_tier]
|
| 108 |
+
message = random.choice(messages)
|
| 109 |
+
return message.format(days=days_remaining)
|
| 110 |
+
return ""
|
| 111 |
+
|
| 112 |
+
def generate_authority_signals(self, count: int = 3) -> List[str]:
|
| 113 |
+
"""Generate authority signals"""
|
| 114 |
+
return random.sample(self.authority_signals, min(count, len(self.authority_signals)))
|
| 115 |
+
|
| 116 |
+
def apply_prospect_theory(self, risk_score: float) -> float:
|
| 117 |
+
"""
|
| 118 |
+
Apply Kahneman & Tversky's Prospect Theory:
|
| 119 |
+
- Losses loom larger than gains (λ ≈ 2.25)
|
| 120 |
+
- Value function is concave for gains, convex for losses
|
| 121 |
+
"""
|
| 122 |
+
# For losses (risk > 0), apply convex weighting
|
| 123 |
+
if risk_score > 0:
|
| 124 |
+
# Diminishing sensitivity for losses
|
| 125 |
+
perceived_risk = risk_score ** 0.88
|
| 126 |
+
# Loss aversion coefficient (losses feel 2.25x worse)
|
| 127 |
+
perceived_risk *= 2.25
|
| 128 |
+
else:
|
| 129 |
+
# For gains, apply concave weighting
|
| 130 |
+
perceived_risk = -((-risk_score) ** 0.88)
|
| 131 |
+
|
| 132 |
+
return min(1.0, max(0.0, perceived_risk))
|
| 133 |
+
|
| 134 |
+
def generate_psychological_insights(self, risk_score: float, recommendation: str, license_tier: str) -> Dict:
|
| 135 |
+
"""Generate comprehensive psychological insights"""
|
| 136 |
+
return {
|
| 137 |
+
"loss_aversion": self.generate_loss_aversion_message(risk_score),
|
| 138 |
+
"social_proof": self.generate_social_proof(license_tier),
|
| 139 |
+
"scarcity": self.generate_scarcity_message(license_tier),
|
| 140 |
+
"authority": self.generate_authority_signals(2),
|
| 141 |
+
"perceived_risk": self.apply_prospect_theory(risk_score),
|
| 142 |
+
"recommendation_impact": self._assess_recommendation_impact(recommendation),
|
| 143 |
+
"tier_motivation": self._generate_tier_motivation(license_tier, risk_score)
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
def _assess_recommendation_impact(self, recommendation: str) -> str:
|
| 147 |
+
"""Assess psychological impact of recommendation"""
|
| 148 |
+
if "BLOCKED" in recommendation or "HIGH RISK" in recommendation:
|
| 149 |
+
return "high_anxiety"
|
| 150 |
+
elif "REQUIRES APPROVAL" in recommendation or "MODERATE RISK" in recommendation:
|
| 151 |
+
return "moderate_concern"
|
| 152 |
+
else:
|
| 153 |
+
return "low_concern"
|
| 154 |
+
|
| 155 |
+
def _generate_tier_motivation(self, current_tier: str, risk_score: float) -> Dict:
|
| 156 |
+
"""Generate motivation to upgrade from current tier"""
|
| 157 |
+
if current_tier == "oss":
|
| 158 |
+
return {
|
| 159 |
+
"target_tier": "trial",
|
| 160 |
+
"value_prop": "Get mechanical gates for free",
|
| 161 |
+
"motivation": "fear_of_loss",
|
| 162 |
+
"urgency": "high" if risk_score > 0.5 else "medium"
|
| 163 |
+
}
|
| 164 |
+
elif current_tier == "trial":
|
| 165 |
+
return {
|
| 166 |
+
"target_tier": "starter",
|
| 167 |
+
"value_prop": "Keep mechanical gates after trial",
|
| 168 |
+
"motivation": "fear_of_loss",
|
| 169 |
+
"urgency": "high"
|
| 170 |
+
}
|
| 171 |
+
elif current_tier == "starter":
|
| 172 |
+
return {
|
| 173 |
+
"target_tier": "professional",
|
| 174 |
+
"value_prop": "Get 24/7 support and advanced gates",
|
| 175 |
+
"motivation": "aspiration",
|
| 176 |
+
"urgency": "medium"
|
| 177 |
+
}
|
| 178 |
+
elif current_tier == "professional":
|
| 179 |
+
return {
|
| 180 |
+
"target_tier": "enterprise",
|
| 181 |
+
"value_prop": "Enterprise features and dedicated support",
|
| 182 |
+
"motivation": "authority",
|
| 183 |
+
"urgency": "low"
|
| 184 |
+
}
|
| 185 |
+
else:
|
| 186 |
+
return {
|
| 187 |
+
"target_tier": None,
|
| 188 |
+
"value_prop": "You have the highest tier",
|
| 189 |
+
"motivation": None,
|
| 190 |
+
"urgency": None
|
| 191 |
+
}
|