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 ground truth.

1. Collect candidate questions

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. Re-running on an existing review.csv preserves already-reviewed rows by id unless --force is passed:

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

Email addresses, SSNs, phone numbers, Luhn-valid card numbers, JWTs, and bearer/API tokens are scrubbed before writing review/review.csv.

From BigQuery query history

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, OR/AND-accurate inclusions and exclusions, grouping, complex-SQL warnings for UNION/QUALIFY/windows/EXISTS, and PII-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, and export decisions.json (which binds every approval to that row's sql_sha256 so post-approval SQL tampering is refused on import):

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

review --out golds.csv refuses while any row remains undecided. When no human steward is available on day one, review --draft --sheet review/review.csv --model ... drafts accept/reject recommendations from 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)

contextlift --workspace .contextlift gate --db warehouse.duckdb --golds golds.csv --timeout 60 --clean-out golds_clean.csv
contextlift estate-shape golds_clean.csv
contextlift resolvable-lift --n 40

gate checks every query against 16 defect classes (EXEC_FAIL, ZERO_ROWS, WHOLE_ROW, DEGENERATE, NON_SELECT, TIMEOUT, UNSTABLE, LIMIT_NO_ORDER, TEXT_AGGREGATE, PLACEHOLDER_COMMENT, etc.) and 4 warnings. A non-zero exit code means defects were flagged; golds_clean.csv holds the clean subset.

4. Split and lock the holdout

contextlift split \
  --golds 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 (failing closed if an explicit --holdout-ids or --curate-ids file is missing), screens every proposed patch against lint and the leakage gate, and writes candidate drafts for human review before context-write.