Deterministic impact calculator

Gross at-risk revenue is computed numerically, then scaled by an evidence-grade band into a recoverable range — the hard bounds the LLM cannot exceed.

flowchart LR
    IN["baselineRate · currentRate
affectedSessions · aov · evidenceGrade"] IN --> GROSS["grossAtRisk =
(baseline − current) × sessions × aov"] GROSS --> FACTOR{"evidenceGrade"} FACTOR -->|"weak"| B1["recovery 0–10%"] FACTOR -->|"plausible"| B2["10–25%"] FACTOR -->|"strong"| B3["25–60%"] FACTOR -->|"very_strong"| B4["60–90%"] B1 & B2 & B3 & B4 --> RANGE["RecoverableRevenueRange
low · high · grossAtRisk · basis"] RANGE --> ZOD["Passed to LLM as bounds
Zod rejects values outside [low, high]"] style RANGE fill:#c8e6c9

Constrained LLM diagnosis

Gemini narrates the rank-1 hypothesis. Zod refinement rejects off-whitelist templates or out-of-range revenue; one retry, then a deterministic templated fallback.

flowchart TD
    IN["Cluster + EvidencePack + ranked Hypotheses
+ Template whitelist + Revenue range + DQ report"] IN --> CHECK{"Tracking-only mode?"} CHECK -->|"Yes"| TEMPLATE["templateDataHealthDiagnosis
(deterministic, no LLM)"] CHECK -->|"No"| FORMAT["Format prompt:
ranked hypotheses + whitelist + range"] FORMAT --> INVOKE["Gemini invoke
(structured output)"] INVOKE --> ZOD["Zod refinement"] ZOD --> C1{"templateKeys ⊆ whitelist?"} C1 -->|"No"| RETRY{"Retry < 1?"} RETRY -->|"Yes"| INVOKE RETRY -->|"No"| FB["Deterministic templated fallback"] C1 -->|"Yes"| C2{"revenue ∈ [low, high]?"} C2 -->|"No"| RETRY C2 -->|"Yes"| OUT["Narrative output:
title · primaryCause · diagnosis
alternatesConsidered · recommendations"] TEMPLATE --> PERSIST OUT --> PERSIST FB --> PERSIST[(posthog_diagnoses
+ posthog_recommendations)] style TEMPLATE fill:#fff9c4 style FB fill:#ffccbc style OUT fill:#e8f5e9 style PERSIST fill:#e8f5e9,stroke:#4CAF50