← Statpro dev log

Ask learned to refuse questions it can't prove

#sports#web#data#product

A glowing amber fingerprint of concentric arcs points at a column of slots; exactly one slot lights up and an empty dashed slot waits at the bottom.

Ask "who has the most Gold Gloves?" (baseball's fielding award) and Statpro's Ask feature gave you a clean leaderboard. Ask "who has the most All-Star selections?" and sometimes you got the Gold Glove leaderboard again. The answer looked right, and nothing in the pipeline could prove it matched the question.

Ask takes a sports question in plain English, works out the intent, pulls the numbers and renders an answer. "Works out the intent" was doing a lot of quiet work. I spent a full day and seven commits on one theme: the pipeline has to prove the answer matches the question before it's allowed to render.

Forty named kinds of question

The core is a typed discriminated union of every kind of question the system actually knows how to answer. A discriminated union is a type whose members each carry a tag, so the compiler always knows which shape it's holding. An intent is no longer a vague string like "a question about awards". It's one of about 40 named kinds: award-winner, coach-win-leaders, team-record-vs-team, threshold-career and so on.

Each parsed intent gets normalized, and then I compute a stable SHA-256 fingerprint over it. Two phrasings of the same question, "most Gold Gloves all-time" and "Gold Glove career leaders", should land on the same fingerprint. The fingerprint is only for correlation, and it's never proof on its own. What certifies an answer is whether the intent matches a kind the system has verified answers for, meaning answers that passed automated checks.

Sort the keys before you hash

A hash only helps if the same logical question always produces the same bytes. Object keys can arrive in any order, so the same intent could hash differently depending on which parser branch touched it last. This recursion sorts the keys at every depth before hashing, which takes key order out of the result:

function stableValue(value: unknown): unknown {
  if (Array.isArray(value)) return value.map(stableValue);
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.entries(value)
        .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
        .map(([k, v]) => [k, stableValue(v)]),
    );
  }
  return value;
}

// Same question, different phrasing: same hash.
const intentFingerprint = (intent: ParsedIntent) =>
  sha256(JSON.stringify(stableValue(normalize(intent))));
ASK PIPELINE every answer has to pass the last box Question in plain English Parsed intent, normalized SHA-256 fingerprint One of ~40 known kinds? match none Render the verified answer Suppress the answer TWO PHRASINGS "most Gold Gloves all-time" "Gold Glove career leaders" one fingerprint
The fingerprint ties phrasings together. Only a match against a known kind lets an answer render.

No match means no answer

The refusal branch is the whole point. Before this change, Ask rendered something for every question. Now, if the intent doesn't match a kind with verified answers behind it, the answer is suppressed. A wrong answer that looks right is worse than no answer, because you can't catch it by reading.

Making every kind pass the gate

Once the gate existed, the rest of the day went to making sure each kind of question actually passed it.

Coach and award questions got their own fidelity paths, which are separate routes that each check the answer against what was asked. So "who won the 2019 Cy Young" (baseball's best-pitcher award) and "which manager has the most wins" stopped borrowing each other's answer shapes. The helper that counts a team's record split by situation was undercounting, which made the verified set look smaller than it really was, so I fixed the counter.

I also shipped a private preview of the full verified catalog, the list of verified questions allowed to become public pages, about 100 pages, so I can eyeball answers in production before they go public. Each league gets team-colored banners, because a leaderboard without team colors reads like a spreadsheet.

The last one was small. The team-leaderboard view rendered both a related-questions block and an example-questions block, and one assertion later it renders one. A double render makes a page feel broken even when the answer on it is correct.

Build the proof layer first

The fidelity union should have existed before the first answer shipped. I built Ask top-down: parse, answer, render. The proof layer came afterwards. In hindsight the proof layer is the product, because nobody trusts a sports reference site that's wrong sometimes.

If I were starting over, I'd write the typed kinds first and answer nothing until each one had a verified test. The render layer would be a view over data that's already certified. The gate is cheap to add late, but the answers you shipped before it are already out there.

This is the build log for Statpro (statpro.io) · All entries · Weekly essays