ChurnIntel
Text-to-SQL copilot with a read-only SQL sandbox and SHAP churn drivers
Schema-grounded text-to-SQL copilot over a churn database: live schema introspection, Gemini / OpenAI-compatible generation with a rule-based fallback, and a three-layer read-only SQL sandbox. A LightGBM churn model writes per-customer SHAP risk drivers back into the database for the copilot to query.
- SQL attack queries blocked, 8/8 legitimate queries allowed
- 22/22
- churn-predictive-agent/benchmarks/results.json
- churn ROC-AUC against a 0.8513 oracle ceiling (synthetic)
- 0.8271
- churn-predictive-agent/benchmarks/results.json
The problem
LLM-written SQL can destroy data or answer the wrong question, and retention teams need to know who will churn and why.
The approach
Live schema introspection, LLM or rule-based SQL generation with bound parameters, a three-layer read-only sandbox, and a LightGBM churn model whose SHAP drivers the copilot can query.
Architecture
- 01
Synthetic churn database
6,000 subscribers with 12 months of usage, support tickets and payments up to a 2025-12-31 snapshot; churn is cancellation in the following 90 days.
- 02
Schema grounding
Tables, keys, row counts, data-dictionary descriptions and distinct categorical values are introspected live and given to the LLM; the offline planner builds its vocabulary from the same catalogue.
- 03
Generation
Gemini or any OpenAI-compatible endpoint returns JSON {sql, params}; if it is rejected or fails, a rule-based planner builds a small query IR and renders parameterised SQL.
- 04
Read-only sandbox
A sqlparse validator (one SELECT, no DML/DDL/ATTACH/PRAGMA), the SQLite authorizer (allow-listed tables and functions) and a read-only connection with a timeout and row cap.
- 05
Churn model
LightGBM selected by 5-fold CV against a logistic baseline; cross-fitted scores, risk tiers and top-3 TreeSHAP drivers per customer are written to the database.
Key decisions
- Treats LLM output as untrusted: it passes the same sandbox as everything else and falls back to the planner when rejected.
- Binds every literal from the question as a parameter instead of splicing it into SQL.
- Layers the authorizer and a read-only connection under the static check, so a validator bypass still cannot write.
- Adds a held-out question set written before the planner was run on it, and reports its one silent wrong answer.
Results
- 22/22 attack queries and 22/22 malicious-LLM outputs blocked, 8/8 legitimate controls allowed, 5/5 write requests refused, database unchanged.
- Churn LightGBM test ROC-AUC 0.8271 vs 0.8187 for logistic regression; the High tier (10% of customers) holds 40% of churners.
| Metric | Value | Source |
|---|---|---|
| SQL attacks blocked / legitimate controls allowed | 22/22 · 8/8 | churn-predictive-agent/benchmarks/results.json |
| Text-to-SQL execution accuracy (offline planner) | 15/15 dev · 9/10 held-out | churn-predictive-agent/benchmarks/results.json |
| Churn ROC-AUC, test split (oracle ceiling 0.8513) | 0.8271 | churn-predictive-agent/benchmarks/results.json |
How it is tested
- 61 pytest tests: attack and control queries, authorizer and read-only bypass checks, timeouts, mocked Gemini and OpenAI-compatible responses, malicious-LLM fallback, and SHAP checked against LightGBM's own TreeSHAP.
- Evaluation script rebuilds the database, trains the model, runs 25 questions and the safety suite, and confirms the tables are unchanged.
Limits, stated plainly
- Synthetic data, and the offline planner is narrow: it answered the held-out question 'Which region has the highest churn rate?' with the overall rate.
- No live-LLM evaluation yet; the browser demo is still a client-side simulation.