contextlift

Building and governing a gold set

You do not need to hand-write 100 SQL queries from scratch to measure context lift. contextlift builds candidate answer keys from existing SQL folders, warehouse query history, dbt verified queries, and production agent failures, then puts a human steward in charge of approving what becomes ground truth.

1. Collect candidate questions

Pick whichever source matches your team's workflow (or combine them):

From a folder of saved SQL queries

contextlift init --db warehouse.duckdb --sql-dir analytics/saved_queries/

init extracts each .sql file into candidates.csv and review/review.csv.

From production agent failures

Turn failed production chats (JSONL logs with question, predicted SQL, and optional error/feedback) into a PII-redacted Steward review sheet. Pass a .csv file path to --out:

contextlift ingest-failures \
  --input prod_failures.jsonl \
  --out review/review.csv \
  --db warehouse.duckdb

Email addresses, SSNs, phone numbers, credit card numbers, and bearer tokens are scrubbed before writing review/review.csv.

From BigQuery query history

Find where real user demand lives before mining:

contextlift log --demand --db bq://BILLING --days 30 --out demand.csv
contextlift log --db bq://BILLING --days 90 --datasets finance,sales \
  --min-tables 2 --require-analytical --max-bytes 1GB --out log.csv
contextlift mine --db bq://BILLING --queries log.csv --datasets finance,sales \
  --out candidates.csv --write-questions

Datasets with more than 50 jobs per account per day are usually scheduled pipelines under personal credentials; exclude those accounts with --exclude-accounts.

2. Business Steward review (no SQL required)

Prepare a review sheet and generate a self-contained offline HTML review pack (steward_review.html) that translates each candidate's SQL into plain English (metric, tables, inclusions, exclusions, grouping, and masked preview rows):

contextlift review --candidates candidates.csv --out review/
contextlift review --sheet review/review.csv --html --db warehouse.duckdb
Business Steward offline HTML review pack showing plain-English contract cards, decided counter, and PII-masked previews

Send review/steward_review.html to a Finance, Ops, or Analytics steward. They open it locally in a browser, approve or reject each question, optionally add a business comment, and export decisions.json. Import their decisions and materialize golds.csv:

contextlift review --sheet review/review.csv --import-decisions decisions.json
contextlift review --sheet review/review.csv --out golds.csv

review --out golds.csv refuses to build an answer key while any row remains undecided. When no human steward is available on day one, review --draft --sheet review/review.csv --model ... can draft accept/reject recommendations (using a different model family than the agent under test); every downstream verdict then prints a visible caveat until a person audits those rows.

3. Audit the answer key (gate and estate-shape)

Run gate before paying for any agent call:

contextlift --workspace .contextlift gate --db warehouse.duckdb --golds golds.csv --timeout 60

gate checks every query against 16 defect classes (including EXEC_FAIL, ZERO_ROWS, WHOLE_ROW, DEGENERATE, NON_SELECT, TIMEOUT, UNSTABLE, LIMIT_NO_ORDER, TEXT_AGGREGATE, and PLACEHOLDER_COMMENT) and 4 warnings. A non-zero exit code means defects were flagged; the clean subset is saved automatically under .contextlift/runs/<gate>/outputs/golds_clean.csv.

Check table balance and question redundancy (free):

contextlift estate-shape .contextlift/runs/<gate>/outputs/golds_clean.csv
contextlift resolvable-lift --n 40

4. Split and lock the holdout

Create a deterministic hash-based split once and keep holdout.csv sealed until the final verdict:

contextlift split \
  --golds .contextlift/runs/<gate>/outputs/golds_clean.csv \
  --out-curate-csv curate.csv \
  --out-holdout-csv holdout.csv \
  --out-curate curate_ids.txt \
  --out-holdout holdout_ids.txt

5. Iterate strictly on the working set (suggest)

After scoring a baseline on curate.csv, use triage, draft-rules, or suggest to propose rule patches strictly from working-set failures:

contextlift suggest \
  --report reports/curate_base.json \
  --spec spec/ \
  --curate-ids curate_ids.txt \
  --holdout-ids holdout_ids.txt \
  --holdout-golds holdout.csv

suggest hard-refuses any report containing a holdout question ID, screens every proposed patch against lint and the leakage gate, and writes candidate drafts for a human to review before installing with context-write. Never curate rules or worked examples from holdout.csv.