contextlift

How it works

contextlift treats business context like application code. A change to a rule, dbt metric, semantic view, or worked example does not ship on intuition; it runs in a paired experiment against real SQL execution on a held-out question set.

Two arms, one difference

Every spec/ repository compiles into two strict arms:

ArmNameContents
ControlArm 0dspec/schema/ only: the physical table and column descriptions the estate already had.
TreatmentArm 1spec/schema/ plus authored context: rules/, authored/, vocabulary/, examples/, and relationships.yaml.

Because Arm 0d is a strict subset of Arm 1, any accuracy delta belongs exclusively to the authored context change. Giving Arm 0d zero schema descriptions would credit authored rules for baseline metadata the warehouse already carried.

Execution accuracy as ground truth

contextlift never grades SQL by string similarity or by an uncalibrated LLM judge alone. Every predicted query and every gold query runs read-only against your real warehouse:

The sealed holdout firewall

A gold set is split deterministically by hash (split) into two disjoint halves:

  1. Working set (curate.csv): used for failure triage (triage), rule drafting (draft-rules), patch proposals (suggest), worked-example pooling (curate-examples), and lint coverage checks.
  2. Held-out set (holdout.csv): never fed into draft-rules, suggest, curate-examples, or pre-round lint coverage. round evaluates both arms on the held-out set and records every scored holdout read in .contextlift/holdout_ledger.json. Reading the same holdout a second time triggers an interactive confirmation gate so teams cannot hill-climb on the exam.

Paired statistics: McNemar and Tango

Every question is answered by both arms, creating paired binary outcomes:

contextlift computes two exact paired statistics:

  1. Exact two-sided McNemar p-value over discordant pairs (b, c) using the binomial distribution.
  2. 95% paired Tango score interval (metrics.paired_difference_interval, Tango 1998 with continuity correction), which maintains nominal coverage even on small samples (n = 20 to 100) where unadjusted Wald intervals under-cover.

Verdict words

Every completed round produces exactly one verdict word:

VerdictMeaning
REAL GAINStatistically significant positive lift (p < 0.05 and lift > 0) on held-out questions, with all validity gates passing.
REAL LOSSStatistically significant negative lift (p < 0.05 and lift < 0). The context actively harmed accuracy.
NOT PROVENHeld-out set had enough statistical power to detect the target lift, but the observed difference is not significant (p >= 0.05).
UNDERPOWEREDHeld-out set is too small to resolve --target-lift cleanly; neither a gain nor a null can be claimed.
INVALIDA pre-flight or post-run validity gate failed (leakage, template twins, unwaived lint refusals, copied gold answers, or insufficient power).

Safety badges

Positive average lift can hide regressions. Every verdict therefore carries an independent safety badge alongside the lift:

Safety badgeCondition
SAFE0 held-out questions broke (b == 0). Every movement was an improvement or unchanged.
MIXEDPositive net lift, but at least one question that worked without context broke with it (c > b > 0). Broken question IDs are printed beside the badge.
UNSAFEContext broke at least as many questions as it fixed (b >= c with b > 0).

Pre-flight refusal gates (fail closed at $0)

Before making the first paid model call, round runs six free checks and stops if any fail:

  1. Table coverage: confirms the arm exposes the tables named by the golds.
  2. Project match: catches cross-project BigQuery references before billing.
  3. Resolvable lift: refuses question sets too small to prove --target-lift (6 unanimous flips is the smallest exact McNemar result at p < 0.05).
  4. Leakage & template twins: refuses any context containing a held-out question, held-out SQL, or structurally identical worked-example template twin.
  5. Context lint: runs lint (including live --db enum literal checks when --db is set) and refuses on hard defects unless explicitly waived with --allow-lint-refusals (recorded in the manifest).
  6. Budget and confirmation: requires an explicit --budget ceiling on paid calls and interactive /dev/tty confirmation (or signed CI approval token).

Rule attribution and bisection

When a round fixes or breaks questions, attribute parses the SQL ASTs of baseline and treatment queries to score which specific rule lines or terms explain each flip. Suspect rules are leads rather than final proof; bisect confirms causality by binary-searching the rule set to isolate the exact offending rule.

For the full statistical proofs and historical derivation, see docs/research/METHODOLOGY.md and docs/research/ATTRIBUTION.md.