fix(backend): resolve CORS policy, dashboard stats column mapping, and swarm simulation calibrations

This commit is contained in:
adithiyan 2026-10-26 23:45:18 +05:30
parent 4341b056fe
commit 04674f4f49
10 changed files with 223 additions and 80 deletions

View file

@ -138,7 +138,7 @@ def login(
"refresh_token", "refresh_token",
raw_refresh, raw_refresh,
httponly=True, httponly=True,
secure=True, secure=False if settings.ENVIRONMENT == "development" else True,
samesite="lax" samesite="lax"
) )
response.headers["X-User-Email"] = user.email response.headers["X-User-Email"] = user.email

View file

@ -129,9 +129,9 @@ def get_dashboard_stats(
if len(top_products_list) < 25: if len(top_products_list) < 25:
existing_names = {p["name"] for p in top_products_list} existing_names = {p["name"] for p in top_products_list}
catalog_products = db.execute(text(""" catalog_products = db.execute(text("""
SELECT title, base_price FROM products SELECT name FROM products
WHERE status = 'active' WHERE status = 'active'
ORDER BY base_price DESC ORDER BY created_at DESC
LIMIT 60 LIMIT 60
""")).fetchall() """)).fetchall()
for p_row in catalog_products: for p_row in catalog_products:

View file

@ -2,13 +2,14 @@ import os
import sys import sys
from pathlib import Path from pathlib import Path
# Ensure backend root and workspace root are in sys.path for swarm package discovery # Ensure backend root has highest priority in sys.path
_backend_root = str(Path(__file__).resolve().parents[1])
if _backend_root not in sys.path:
sys.path.insert(0, _backend_root)
_workspace_root = str(Path(__file__).resolve().parents[2]) _workspace_root = str(Path(__file__).resolve().parents[2])
if _workspace_root not in sys.path: if _workspace_root not in sys.path:
sys.path.insert(0, _workspace_root) sys.path.append(_workspace_root)
_backend_root = str(Path(__file__).resolve().parents[1])
if _backend_root in sys.path:
sys.path.remove(_backend_root)
sys.path.insert(0, _backend_root)
import hashlib import hashlib
from fastapi import FastAPI, Request, Response, status from fastapi import FastAPI, Request, Response, status
@ -110,6 +111,7 @@ app.add_middleware(
"http://127.0.0.1:3022", "http://127.0.0.1:3022",
"http://127.0.0.1:8000", "http://127.0.0.1:8000",
], ],
allow_origin_regex=r"^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$",
allow_credentials=True, allow_credentials=True,
allow_methods=["*"], allow_methods=["*"],
allow_headers=["*"], allow_headers=["*"],
@ -238,6 +240,20 @@ def validation_exception_handler(request: Request, exc: RequestValidationError):
response.headers["Access-Control-Allow-Credentials"] = "true" response.headers["Access-Control-Allow-Credentials"] = "true"
return response return response
@app.exception_handler(Exception)
def generic_exception_handler(request: Request, exc: Exception):
import traceback
traceback.print_exc()
response = JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": "Internal server error occurred", "error": str(exc)}
)
origin = request.headers.get("origin")
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
return response
# 4. Register Router Endpoints # 4. Register Router Endpoints
app.include_router(storefront_legacy_router) app.include_router(storefront_legacy_router)
app.include_router(auth_router) app.include_router(auth_router)

View file

@ -3,8 +3,16 @@
@purpose Central configuration for Swarm Intelligence Engine. @purpose Central configuration for Swarm Intelligence Engine.
""" """
import os import os
from pathlib import Path
from dotenv import load_dotenv
from pydantic import BaseModel from pydantic import BaseModel
_backend_env = Path(__file__).resolve().parent.parent / ".env"
if _backend_env.exists():
load_dotenv(_backend_env)
else:
load_dotenv()
class SwarmSettings(BaseModel): class SwarmSettings(BaseModel):
# Execution & Concurrency # Execution & Concurrency
@ -14,7 +22,7 @@ class SwarmSettings(BaseModel):
ZOMBIE_REAP_MINUTES: int = 3 ZOMBIE_REAP_MINUTES: int = 3
# Default Provider: "mock", "gemini", or "openai" # Default Provider: "mock", "gemini", or "openai"
DEFAULT_PROVIDER: str = os.getenv("SWARM_LLM_PROVIDER", "mock") DEFAULT_PROVIDER: str = os.getenv("SWARM_LLM_PROVIDER", "gemini" if os.getenv("GEMINI_API_KEY") else "mock")
# API Keys # API Keys
GEMINI_API_KEY: str = os.getenv("GEMINI_API_KEY", "") GEMINI_API_KEY: str = os.getenv("GEMINI_API_KEY", "")
@ -22,11 +30,12 @@ class SwarmSettings(BaseModel):
OPENAI_BASE_URL: str = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1") OPENAI_BASE_URL: str = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
# Models # Models
GEMINI_MODEL: str = os.getenv("GEMINI_MODEL", "gemini-2.0-flash") GEMINI_MODEL: str = os.getenv("GEMINI_MODEL", "gemini-3.1-flash-lite")
OPENAI_MODEL: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini") OPENAI_MODEL: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
# Token Pricing Matrices (USD per 1,000,000 tokens) # Token Pricing Matrices (USD per 1,000,000 tokens)
PRICING_MATRIX: dict = { PRICING_MATRIX: dict = {
"gemini-3.1-flash-lite": {"input_per_million": 0.075, "output_per_million": 0.30},
"gemini-2.0-flash": {"input_per_million": 0.10, "output_per_million": 0.40}, "gemini-2.0-flash": {"input_per_million": 0.10, "output_per_million": 0.40},
"gpt-4o-mini": {"input_per_million": 0.15, "output_per_million": 0.60}, "gpt-4o-mini": {"input_per_million": 0.15, "output_per_million": 0.60},
"mock": {"input_per_million": 0.0, "output_per_million": 0.0}, "mock": {"input_per_million": 0.0, "output_per_million": 0.0},

View file

@ -20,6 +20,31 @@ class HistoricalBaseline(BaseModel):
data_source: str data_source: str
observed_period: str observed_period: str
# Frontend compatibility fields
historical_sample_size: Optional[int] = None
historical_mean: Optional[float] = None
historical_std: Optional[float] = None
empirical_95_ci_lower: Optional[float] = None
empirical_95_ci_upper: Optional[float] = None
baseline_source: Optional[str] = None
sampling_notice: Optional[str] = None
def model_post_init(self, __context: Any) -> None:
if self.historical_sample_size is None:
self.historical_sample_size = self.sample_size
if self.historical_mean is None:
self.historical_mean = self.empirical_mean
if self.historical_std is None:
self.historical_std = self.empirical_std
if self.empirical_95_ci_lower is None and self.empirical_ci_95:
self.empirical_95_ci_lower = self.empirical_ci_95[0]
if self.empirical_95_ci_upper is None and len(self.empirical_ci_95) > 1:
self.empirical_95_ci_upper = self.empirical_ci_95[1]
if self.baseline_source is None:
self.baseline_source = self.data_source
if self.sampling_notice is None:
self.sampling_notice = f"Calibrated against {self.sample_size} historical store records ({self.observed_period})."
class SimulationDispersion(BaseModel): class SimulationDispersion(BaseModel):
agent_sample_count: int agent_sample_count: int
@ -37,6 +62,24 @@ class CalibratedProjection(BaseModel):
calibrated_range_95: List[float] # [lower, upper] calibrated_range_95: List[float] # [lower, upper]
disclaimer: str disclaimer: str
# Frontend compatibility fields
point_estimate: Optional[float] = None
credible_range_lower: Optional[float] = None
credible_range_upper: Optional[float] = None
historical_weight: float = 0.60
simulation_weight: float = 0.40
epistemic_notice: Optional[str] = None
def model_post_init(self, __context: Any) -> None:
if self.point_estimate is None:
self.point_estimate = self.calibrated_projected_value
if self.credible_range_lower is None and self.calibrated_range_95:
self.credible_range_lower = self.calibrated_range_95[0]
if self.credible_range_upper is None and len(self.calibrated_range_95) > 1:
self.credible_range_upper = self.calibrated_range_95[1]
if self.epistemic_notice is None:
self.epistemic_notice = self.disclaimer
def fetch_historical_baseline(scenario_type: str, store_id: int = 1) -> HistoricalBaseline: def fetch_historical_baseline(scenario_type: str, store_id: int = 1) -> HistoricalBaseline:
""" """

View file

@ -1,6 +1,7 @@
""" """
Swarm Providers Factory Swarm Providers Factory
""" """
import os
from typing import Optional from typing import Optional
from swarm.config import swarm_settings from swarm.config import swarm_settings
from swarm.providers.base import BaseLLMProvider, TokenUsage from swarm.providers.base import BaseLLMProvider, TokenUsage
@ -10,12 +11,14 @@ from swarm.providers.openai_provider import OpenAIProvider
def get_llm_provider(name: Optional[str] = None) -> BaseLLMProvider: def get_llm_provider(name: Optional[str] = None) -> BaseLLMProvider:
provider_name = (name or swarm_settings.DEFAULT_PROVIDER).lower().strip() provider_name = (name or os.getenv("SWARM_LLM_PROVIDER") or swarm_settings.DEFAULT_PROVIDER).lower().strip()
if provider_name == "gemini": if provider_name == "gemini" or (swarm_settings.GEMINI_API_KEY and provider_name != "openai" and provider_name != "mock"):
return GeminiProvider() return GeminiProvider()
elif provider_name in ("openai", "qwen", "deepseek"): elif provider_name in ("openai", "qwen", "deepseek"):
return OpenAIProvider() return OpenAIProvider()
elif swarm_settings.GEMINI_API_KEY:
return GeminiProvider()
else: else:
return MockProvider() return MockProvider()

View file

@ -65,9 +65,15 @@ class FleetQuoteScenario(BaseScenario):
sla_penalty = max(0.0, (turnaround_days - 2) * 0.08) sla_penalty = max(0.0, (turnaround_days - 2) * 0.08)
accepted_deals = 0 accepted_deals = 0
total_deal_val = device_count * quoted_price_per_unit
for p in personas: for p in personas:
win_score = (price_competitiveness * 0.50) + warranty_bonus - sla_penalty # Individual enterprise sensitivity and budget realism
win_prob = min(0.95, max(0.05, baseline.empirical_mean + (win_score * 0.40))) budget_ratio = min(1.25, max(0.65, p.budget_inr / max(1.0, total_deal_val)))
sens_drag = (p.price_sensitivity - 0.50) * 0.30
trust_boost = (p.trust_threshold - 0.50) * 0.15
win_score = (price_competitiveness * 0.45 * budget_ratio) + warranty_bonus - sla_penalty - sens_drag + trust_boost
win_prob = min(0.95, max(0.05, baseline.empirical_mean + (win_score * 0.35)))
agent_outcomes.append(win_prob) agent_outcomes.append(win_prob)
is_accepted = win_prob >= 0.50 is_accepted = win_prob >= 0.50
@ -116,12 +122,18 @@ class FleetQuoteScenario(BaseScenario):
comp_vote = "Approved" if deliberation.compliance_vote else "Concerns (Warranty terms)" comp_vote = "Approved" if deliberation.compliance_vote else "Concerns (Warranty terms)"
primary_objection = deliberation.primary_objection primary_objection = deliberation.primary_objection
recommended_adjustment = deliberation.recommended_adjustment recommended_adjustment = deliberation.recommended_adjustment
cfo_score = 88 if deliberation.cfo_vote else 36
it_score = 92 if deliberation.it_ops_vote else 42
comp_score = 86 if deliberation.compliance_vote else 40
except Exception: except Exception:
cfo_vote = "Approved" if quoted_price_per_unit <= 4000 else "Vetoed" cfo_vote = "Approved" if quoted_price_per_unit <= 4000 else "Vetoed"
it_vote = "Approved" if turnaround_days <= 3 else "Needs 48h turnaround" it_vote = "Approved" if turnaround_days <= 3 else "Needs 48h turnaround"
comp_vote = "Approved" if warranty_months >= 6 else "Demands 12-month warranty" comp_vote = "Approved" if warranty_months >= 6 else "Demands 12-month warranty"
primary_objection = "Quoted price is 10% above standard bulk fleet maintenance benchmarks." primary_objection = "Quoted price is 10% above standard bulk fleet maintenance benchmarks."
recommended_adjustment = "Offer Rs 3,950/unit if batch size increases to 75 units." recommended_adjustment = "Offer Rs 3,950/unit if batch size increases to 75 units."
cfo_score = 80 if quoted_price_per_unit <= 4000 else 35
it_score = 85 if turnaround_days <= 3 else 45
comp_score = 82 if warranty_months >= 6 else 40
archetype_breakdown = [ archetype_breakdown = [
{"name": "CFO / Finance Committee", "share": "40%", "rate": cfo_vote, "friction": primary_objection}, {"name": "CFO / Finance Committee", "share": "40%", "rate": cfo_vote, "friction": primary_objection},
@ -141,7 +153,12 @@ class FleetQuoteScenario(BaseScenario):
"baseline_win_rate_pct": round(baseline.empirical_mean * 100.0, 1), "baseline_win_rate_pct": round(baseline.empirical_mean * 100.0, 1),
"projected_win_rate_pct": round(projection.calibrated_projected_value * 100.0, 1), "projected_win_rate_pct": round(projection.calibrated_projected_value * 100.0, 1),
"win_probability_category": "STRONG WIN (>= 60%)" if projection.calibrated_projected_value >= 0.60 else ("COMPETITIVE (40-59%)" if projection.calibrated_projected_value >= 0.40 else "AT RISK (< 40%)"), "win_probability_category": "STRONG WIN (>= 60%)" if projection.calibrated_projected_value >= 0.60 else ("COMPETITIVE (40-59%)" if projection.calibrated_projected_value >= 0.40 else "AT RISK (< 40%)"),
"recommended_bid_adjustment": recommended_adjustment "recommended_bid_adjustment": recommended_adjustment,
"committee_votes": {
"cfo": {"score": cfo_score, "verdict": cfo_vote},
"it_admin": {"score": it_score, "verdict": it_vote},
"legal_procurement": {"score": comp_score, "verdict": comp_vote}
}
} }
# 6. Render Report # 6. Render Report

View file

@ -2,6 +2,7 @@
@scenario PricingElasticityScenario (swarm/scenarios/pricing_elasticity.py) @scenario PricingElasticityScenario (swarm/scenarios/pricing_elasticity.py)
@purpose Simulates customer price sensitivity, sales volume change, and margin cannibalization. @purpose Simulates customer price sensitivity, sales volume change, and margin cannibalization.
""" """
import asyncio
import time import time
import json import json
from typing import Dict, Any, Callable, Optional, List from typing import Dict, Any, Callable, Optional, List
@ -96,14 +97,11 @@ class PricingElasticityScenario(BaseScenario):
total_completion_tokens = 0 total_completion_tokens = 0
total_cost = 0.0 total_cost = 0.0
for arch_id, p_list in arch_groups.items(): async def _eval_archetype(arch_id: str, p_list: List[AgentPersona]):
if cancellation_check and cancellation_check():
raise InterruptedError("Simulation cancelled by user.")
avg_prob = sum( avg_prob = sum(
next(log["simulated_purchase_prob"] for log in agent_logs if log["agent_id"] == p.agent_id) next(log["simulated_purchase_prob"] for log in agent_logs if log["agent_id"] == p.agent_id)
for p in p_list for p in p_list
) / len(p_list) ) / max(1, len(p_list))
prompt = ( prompt = (
f"You are evaluating customer behavior for archetype '{p_list[0].archetype_name}' in iFixKart.\n" f"You are evaluating customer behavior for archetype '{p_list[0].archetype_name}' in iFixKart.\n"
@ -111,32 +109,48 @@ class PricingElasticityScenario(BaseScenario):
f"Simulated average purchase rate: {avg_prob:.2%}.\n" f"Simulated average purchase rate: {avg_prob:.2%}.\n"
f"Generate the primary friction point and a realistic 1-sentence quote explaining why this persona bought or abandoned." f"Generate the primary friction point and a realistic 1-sentence quote explaining why this persona bought or abandoned."
) )
try: try:
reasoning, usage = await provider.generate_structured( reasoning, usage = await provider.generate_structured(
prompt=prompt, prompt=prompt,
schema=ArchetypeReasoningSchema, schema=ArchetypeReasoningSchema,
system_prompt="You are a behavioral economics analyst specializing in electronics repair commerce." system_prompt="You are a behavioral economics analyst specializing in electronics repair commerce."
) )
total_prompt_tokens += usage.prompt_tokens return {
total_completion_tokens += usage.completion_tokens "breakdown": {
total_cost += usage.estimated_cost_usd "name": p_list[0].archetype_name,
primary_friction = reasoning.primary_friction "share": f"{len(p_list)/len(personas):.0%}",
quote = reasoning.interview_quote "rate": f"{avg_prob:.1%}",
"friction": reasoning.primary_friction
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": reasoning.interview_quote
},
"usage": usage
}
except Exception: except Exception:
primary_friction = "Balancing cost savings against OEM screen durability." return {
quote = f"At Rs {discounted_price:.0f}, the deal is tempting, but I want to be sure it has a 6-month warranty." "breakdown": {
"name": p_list[0].archetype_name,
"share": f"{len(p_list)/len(personas):.0%}",
"rate": f"{avg_prob:.1%}",
"friction": "Balancing cost savings against OEM screen durability."
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": f"At Rs {discounted_price:.0f}, the deal is tempting, but I want to be sure it has a 6-month warranty."
},
"usage": None
}
archetype_breakdown.append({ results = await asyncio.gather(*(_eval_archetype(a_id, p_l) for a_id, p_l in arch_groups.items()))
"name": p_list[0].archetype_name, for r in results:
"share": f"{len(p_list)/len(personas):.0%}", archetype_breakdown.append(r["breakdown"])
"rate": f"{avg_prob:.1%}", interview_samples.append(r["sample"])
"friction": primary_friction if r["usage"]:
}) total_prompt_tokens += r["usage"].prompt_tokens
interview_samples.append({ total_completion_tokens += r["usage"].completion_tokens
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})", total_cost += r["usage"].estimated_cost_usd
"quote": quote
})
# 7. Financial Elasticity Metrics # 7. Financial Elasticity Metrics
baseline_margin_per_unit = current_price - cost_price baseline_margin_per_unit = current_price - cost_price

View file

@ -3,6 +3,7 @@
@purpose Simulates customer reception, click-through rates, and phone number block/spam risk @purpose Simulates customer reception, click-through rates, and phone number block/spam risk
for proposed WhatsApp campaigns before broadcasting via OpenWA. for proposed WhatsApp campaigns before broadcasting via OpenWA.
""" """
import asyncio
import time import time
import json import json
from typing import Dict, Any, Callable, Optional, List from typing import Dict, Any, Callable, Optional, List
@ -108,6 +109,7 @@ class WhatsAppPreflightScenario(BaseScenario):
if cancellation_check and cancellation_check(): if cancellation_check and cancellation_check():
raise InterruptedError("Simulation cancelled by user.") raise InterruptedError("Simulation cancelled by user.")
async def _eval_wa_archetype(arch_id: str, p_list: List[AgentPersona]):
sub_logs = [log for log in agent_logs if log["agent_id"] in {p.agent_id for p in p_list}] sub_logs = [log for log in agent_logs if log["agent_id"] in {p.agent_id for p in p_list}]
sub_clicks = sum(1 for log in sub_logs if log["decision"] == "CLICK") sub_clicks = sum(1 for log in sub_logs if log["decision"] == "CLICK")
sub_blocks = sum(1 for log in sub_logs if log["decision"] == "BLOCK") sub_blocks = sum(1 for log in sub_logs if log["decision"] == "BLOCK")
@ -119,32 +121,48 @@ class WhatsAppPreflightScenario(BaseScenario):
f"Archetype: {p_list[0].archetype_name} (Total: {len(p_list)}, Clicks: {sub_clicks}, Blocks: {sub_blocks}).\n" f"Archetype: {p_list[0].archetype_name} (Total: {len(p_list)}, Clicks: {sub_clicks}, Blocks: {sub_blocks}).\n"
f"Provide a 1-sentence reaction and a simulated customer quote about this broadcast." f"Provide a 1-sentence reaction and a simulated customer quote about this broadcast."
) )
try: try:
reasoning, usage = await provider.generate_structured( reasoning, usage = await provider.generate_structured(
prompt=prompt, prompt=prompt,
schema=WhatsAppArchetypeReasoning, schema=WhatsAppArchetypeReasoning,
system_prompt="You are a messaging deliverability & customer sentiment analyst." system_prompt="You are a messaging deliverability & customer sentiment analyst."
) )
total_prompt_tokens += usage.prompt_tokens return {
total_completion_tokens += usage.completion_tokens "breakdown": {
total_cost += usage.estimated_cost_usd "name": p_list[0].archetype_name,
spam_reaction = reasoning.spam_reaction "share": f"{len(p_list)/len(personas):.0%}",
quote = reasoning.interview_quote "rate": f"{sub_clicks/max(1, len(p_list)):.1%}",
"friction": reasoning.spam_reaction
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": reasoning.interview_quote
},
"usage": usage
}
except Exception: except Exception:
spam_reaction = "Tolerates targeted repair offers if infrequent." return {
quote = "I don't mind a useful discount message once a month, but if you text weekly I'll block the number." "breakdown": {
"name": p_list[0].archetype_name,
"share": f"{len(p_list)/len(personas):.0%}",
"rate": f"{sub_clicks/max(1, len(p_list)):.1%}",
"friction": "Tolerates targeted repair offers if infrequent."
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": "I don't mind a useful discount message once a month, but if you text weekly I'll block the number."
},
"usage": None
}
archetype_breakdown.append({ results = await asyncio.gather(*(_eval_wa_archetype(a_id, p_l) for a_id, p_l in arch_groups.items()))
"name": p_list[0].archetype_name, for r in results:
"share": f"{len(p_list)/len(personas):.0%}", archetype_breakdown.append(r["breakdown"])
"rate": f"{sub_clicks/len(p_list):.1%}", interview_samples.append(r["sample"])
"friction": spam_reaction if r["usage"]:
}) total_prompt_tokens += r["usage"].prompt_tokens
interview_samples.append({ total_completion_tokens += r["usage"].completion_tokens
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})", total_cost += r["usage"].estimated_cost_usd
"quote": quote
})
# 6. Block Risk Score (0 - 100) # 6. Block Risk Score (0 - 100)
simulated_block_rate = spam_block_count / max(1, len(personas)) simulated_block_rate = spam_block_count / max(1, len(personas))
@ -163,9 +181,13 @@ class WhatsAppPreflightScenario(BaseScenario):
"target_segment": target_segment, "target_segment": target_segment,
"baseline_ctr_pct": round(baseline.empirical_mean * 100.0, 2), "baseline_ctr_pct": round(baseline.empirical_mean * 100.0, 2),
"projected_ctr_pct": round(projection.calibrated_projected_value * 100.0, 2), "projected_ctr_pct": round(projection.calibrated_projected_value * 100.0, 2),
"baseline_open_rate_pct": round(baseline.empirical_mean * 100.0 * 2.5, 2),
"projected_open_rate_pct": round(projection.calibrated_projected_value * 100.0 * 2.5, 2),
"simulated_block_rate_pct": round(simulated_block_rate * 100.0, 2), "simulated_block_rate_pct": round(simulated_block_rate * 100.0, 2),
"whatsapp_block_risk_score": round(block_risk_score, 1), "whatsapp_block_risk_score": round(block_risk_score, 1),
"risk_verdict": risk_verdict "spam_block_risk_score": round(block_risk_score, 1),
"risk_verdict": risk_verdict,
"safety_verdict": risk_verdict
} }
# 7. Render Report # 7. Render Report

View file

@ -3,6 +3,7 @@
@purpose Simulates repair ticket intake surges and spare parts lead-time delays @purpose Simulates repair ticket intake surges and spare parts lead-time delays
to forecast workshop turnaround bottlenecks and technician backlog. to forecast workshop turnaround bottlenecks and technician backlog.
""" """
import asyncio
import time import time
import json import json
from typing import Dict, Any, Callable, Optional, List from typing import Dict, Any, Callable, Optional, List
@ -93,42 +94,55 @@ class WorkshopSurgeScenario(BaseScenario):
for p in personas: for p in personas:
arch_groups.setdefault(p.archetype_id, []).append(p) arch_groups.setdefault(p.archetype_id, []).append(p)
for arch_id, p_list in arch_groups.items(): async def _eval_workshop_archetype(arch_id: str, p_list: List[AgentPersona]):
if cancellation_check and cancellation_check():
raise InterruptedError("Simulation cancelled by user.")
prompt = ( prompt = (
f"You are simulating repair customers during a workshop backlog at iFixKart.\n" f"You are simulating repair customers during a workshop backlog at iFixKart.\n"
f"Current turnaround delay: {delay_hours:.1f} hours ({parts_delay_days} days parts delay).\n" f"Current turnaround delay: {delay_hours:.1f} hours ({parts_delay_days} days parts delay).\n"
f"Archetype: {p_list[0].archetype_name}.\n" f"Archetype: {p_list[0].archetype_name}.\n"
f"Provide a 1-sentence assessment of their tolerance and a customer quote about this turnaround delay." f"Provide a 1-sentence assessment of their tolerance and a customer quote about this turnaround delay."
) )
try: try:
reasoning, usage = await provider.generate_structured( reasoning, usage = await provider.generate_structured(
prompt=prompt, prompt=prompt,
schema=WorkshopReasoningSchema, schema=WorkshopReasoningSchema,
system_prompt="You are a customer experience (CX) and workshop operations auditor." system_prompt="You are a customer experience (CX) and workshop operations auditor."
) )
total_prompt_tokens += usage.prompt_tokens return {
total_completion_tokens += usage.completion_tokens "breakdown": {
total_cost += usage.estimated_cost_usd "name": p_list[0].archetype_name,
delay_tolerance = reasoning.delay_tolerance "share": f"{len(p_list)/len(personas):.0%}",
quote = reasoning.interview_quote "rate": f"{delay_hours:.1f} hrs",
"friction": reasoning.delay_tolerance
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": reasoning.interview_quote
},
"usage": usage
}
except Exception: except Exception:
delay_tolerance = "Urgent device users will request job cancellation if delay exceeds 24h." return {
quote = "I need my phone for office work every day. Waiting 3 days for a screen is unacceptable." "breakdown": {
"name": p_list[0].archetype_name,
"share": f"{len(p_list)/len(personas):.0%}",
"rate": f"{delay_hours:.1f} hrs",
"friction": "Urgent device users will request job cancellation if delay exceeds 24h."
},
"sample": {
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})",
"quote": "I need my phone for office work every day. Waiting 3 days for a screen is unacceptable."
},
"usage": None
}
archetype_breakdown.append({ results = await asyncio.gather(*(_eval_workshop_archetype(a_id, p_l) for a_id, p_l in arch_groups.items()))
"name": p_list[0].archetype_name, for r in results:
"share": f"{len(p_list)/len(personas):.0%}", archetype_breakdown.append(r["breakdown"])
"rate": f"{delay_hours:.1f} hrs", interview_samples.append(r["sample"])
"friction": delay_tolerance if r["usage"]:
}) total_prompt_tokens += r["usage"].prompt_tokens
interview_samples.append({ total_completion_tokens += r["usage"].completion_tokens
"persona": f"{p_list[0].archetype_name} ({p_list[0].name})", total_cost += r["usage"].estimated_cost_usd
"quote": quote
})
summary_metrics = { summary_metrics = {
"surge_multiplier": surge_multiplier, "surge_multiplier": surge_multiplier,
@ -136,8 +150,13 @@ class WorkshopSurgeScenario(BaseScenario):
"active_technicians": active_technicians, "active_technicians": active_technicians,
"baseline_tat_hours": baseline.empirical_mean, "baseline_tat_hours": baseline.empirical_mean,
"projected_tat_hours": round(projection.calibrated_projected_value, 1), "projected_tat_hours": round(projection.calibrated_projected_value, 1),
"projected_turnaround_hours": round(projection.calibrated_projected_value, 1),
"baseline_daily_intake": round(15.0, 1),
"projected_queue_tickets": round(15.0 * surge_multiplier, 1),
"sla_threshold_hours": 48.0,
"dispute_risk_rate_pct": round((disgruntled_count / max(1, len(personas))) * 100.0, 1), "dispute_risk_rate_pct": round((disgruntled_count / max(1, len(personas))) * 100.0, 1),
"bottleneck_severity": "CRITICAL" if delay_hours > 36.0 else ("ELEVATED" if delay_hours > 12.0 else "NORMAL") "bottleneck_severity": "CRITICAL" if delay_hours > 36.0 else ("ELEVATED" if delay_hours > 12.0 else "NORMAL"),
"safety_verdict": "CRITICAL CAPACITY BOTTLENECK" if delay_hours > 36.0 else ("ELEVATED QUEUE RISK" if delay_hours > 12.0 else "CAPACITY STABLE")
} }
# 6. Render Report # 6. Render Report