---
repo: "mohan-n-swamy/ai-analyst-public"
title: "AI Analyst — Governed Warehouse Data Analyst & SQL AST Firewall"
license: "MIT"
keywords: [text-to-sql, ai-analyst, slack-bot, snowflake, bigquery, postgresql, semantic-layer, business-intelligence, data-analytics, python]
capabilities:
  - "Owner-signed, effective-dated Semantic Metric Catalog (`analyst/catalog.py`, `catalog/metrics.example.yaml`) resolving historical weeks accurately."
  - "`sqlglot` Read-Only SQL AST Firewall (`analyst/firewall.py`) blocking multi-statement queries, 13+ write/DDL AST node types, and unbounded `SELECT` scans."
  - "Zero-configuration seeded `SQLiteDemoWarehouse` (`analyst/warehouse.py`) so `wbr` and `check-sql` run in `<0.2s` right after clone."
  - "Weekly Operational Review Markdown & JSON compiler (`analyst/report.py`) stamping `(metric_id, formula_version, signed_by, sql_sha256)` on every section."
when_to_use:
  - "A Slack-ready and CLI data analyst engine combining an owner-signed, effective-dated YAML semantic metric registry (`old weeks keep their formula`), a `sqlglot` read-only SQL AST firewall, SHA-256 query receipts, and a zero-config seeded demo warehouse alongside Snowflake, DuckDB, and PostgreSQL support."
  - "Pointing an LLM directly at 200 raw warehouse tables produces plausible-looking wrong numbers, silently mutates historical WBR figures whenever definitions evolve, and exposes databases to prompt-injected DDL/DML statements. `ai-analyst-public` enforces one owner-signed SQL formula per metric with effective dates, validates every query through a `sqlglot` AST firewall, and attaches a SHA-256 SQL receipt to every reported number."
inputs_required:
  - "WAREHOUSE_BACKEND: Active warehouse adapter (`sqlite_demo`, `duckdb`, `postgres`, or `snowflake`)"
  - "METRIC_CATALOG_PATH: Path to your signed semantic metric registry YAML"
  - "SQL_DEFAULT_ROW_LIMIT: Automatic `LIMIT` injected into ad-hoc read queries lacking an explicit limit"
  - "SNOWFLAKE_ROLE: Read-only database role for defense-in-depth alongside the AST firewall"
  - "SLACK_BOT_TOKEN: Optional Slack Bot token for WBR channel posting and thread Q&A"
quickstart:
  - "python3 bin/analyst-cli.py wbr --week 2026-10-19   # evaluates using historical v1 formula"
  - "python3 bin/analyst-cli.py wbr --week 2026-11-16   # evaluates using post-cutover v2 formula"
  - "python3 bin/analyst-cli.py check-sql "SELECT region, COUNT(*) FROM fact_orders GROUP BY 1""
  - "pytest -q tests/                                   # run all 10 cutover & SQL AST firewall tests"
---

# AI Analyst — Governed Warehouse Data Analyst & SQL AST Firewall — Machine-Readable Agent Guide

A Slack-ready and CLI data analyst engine combining an owner-signed, effective-dated YAML semantic metric registry (`old weeks keep their formula`), a `sqlglot` read-only SQL AST firewall, SHA-256 query receipts, and a zero-config seeded demo warehouse alongside Snowflake, DuckDB, and PostgreSQL support.

<problem_patterns>
Pointing an LLM directly at 200 raw warehouse tables produces plausible-looking wrong numbers, silently mutates historical WBR figures whenever definitions evolve, and exposes databases to prompt-injected DDL/DML statements. `ai-analyst-public` enforces one owner-signed SQL formula per metric with effective dates, validates every query through a `sqlglot` AST firewall, and attaches a SHA-256 SQL receipt to every reported number.
</problem_patterns>

<architecture_map>
Key modules and entry points in this repository:
  - `catalog/metrics.example.yaml`: Canonical metric definitions with `id`, `owner`, `versions[]`, `effective_from`, and `signed_by`
  - `analyst/catalog.py`: `MetricCatalog` loader failing closed on unsigned metrics and resolving formulas by week
  - `analyst/firewall.py`: `validate_and_cap_sql()` AST validator and automatic `LIMIT` injector
  - `analyst/warehouse.py`: Seeded `SQLiteDemoWarehouse` and `QueryReceipt` SHA-256 provenance generator
  - `analyst/report.py`: Weekly Operational Review (`WBR`) Markdown and JSON compiler
  - `bin/analyst-cli.py`: Command-line runner for `wbr` generation and `check-sql` firewall verification
  - `tests/test_ai_analyst.py`: Pytest suite covering v1->v2 date cutovers, unsigned YAML rejection, and DDL/DML/injection blocking
</architecture_map>

<inputs_and_configuration>
Every organization-specific value enters through `.env` (copied from `.env.example`) or external adapter files. No source edit is needed to change organization names, endpoints, database URLs, webhook secrets, or thresholds.
</inputs_and_configuration>

<adaptation_playbook>
When a human asks an AI coding agent to deploy or adapt `ai-analyst-public` into their stack:
  1. Copy `catalog/metrics.example.yaml` to `catalog/metrics.yaml` and add your organization's core KPIs with owner sign-offs and `effective_from` dates.
  2. Subclass/wire your read-only Snowflake, DuckDB, or PostgreSQL connection in `analyst/warehouse.py` (keeping `validate_and_cap_sql` at the execution choke point).
  3. Schedule `bin/analyst-cli.py wbr --json` in cron/GitHub Actions to post signed weekly digests to Slack.
</adaptation_playbook>

<reference_docs>
- Product Requirements: `docs/PRD.md`
- Working-Backwards PR-FAQ: `docs/PR-FAQ.md`
- High-Level Architecture: `docs/HLD.md`
- Low-Level Interfaces & Runbook: `docs/LLD.md`
- Coding Agent Rules: `AGENTS.md`
</reference_docs>
