# Understanding Oracles

Oracles brings powerful and sophisticated testing and specification ideas into the everyday work of developing software with coding agents.

## Start with one rule

> The header must never cover the page content.

- Start with Headroom: change the header’s content and size, then check that its wrapper reserves enough space.
- Show the historical bug: the header grew while its wrapper kept the old height.
- Some laws need only a simple constraint. Others need a reference calculation or a small simulation. Start with the simplest model that captures the behavior you want to check.

![Before and after a Headroom fix: the wrapper grows with the header so the header does not cover the page content.](https://raw.githubusercontent.com/KyleAMathews/tanstack-oracles-talk-assets/main/assets/02-header-height-2db4612fefea.gif)

_A historical Headroom bug, reproduced against an earlier version._

## Decide what you want, then check it

- The design loop works out what the software should do: behavior, priorities, performance budgets and tradeoffs.
- The enforcement loop checks whether the implementation does that, and whether the oracle notices when it does not.
- Agents help explore alternatives and their consequences. You make the design decisions, such as how tasks with equal priority should be ordered.

## From examples to promises

- An example test records an answer for one case.
- A property states a rule over a domain; generated inputs explore it.
- A test oracle supplies a judgment about correct behavior.
- Introduce the running example: maintaining the highest-priority open tasks as records change.

## Simulate the behavior simply

- Keep tasks in a plain collection, apply each batch of edits, then filter, sort and take the top ten.
- Production may use indexes, caches and incremental updates. The independent model can recompute the result after each batch.
- Compare exactly the promised result: an empty answer can satisfy filtering, ordering and size constraints without satisfying the query.
- Explain why an independent model can still be mistaken and needs scrutiny.

## Observe behavior where the promise applies

- Law, history, model, driver, observation and check, connected through one example.
- Include intermediate publications or messages when final state alone can hide a defect.
- A disagreement can reveal a bug, a model mistake or an unresolved design question.

## Challenge the oracle too

- Constructed histories ask whether execution reached the important situation. Deliberately create a tie at the tenth slot: add nine higher-priority tasks, then equal-priority B followed by A. Check that A takes the slot, completing A admits B and reopening A restores A.
- Calibrations ask whether the check would recognize a failure. In a separate history where both tied tasks fit, replace `[A, B]` with `[B, A]` and require the ordering check to reject it. A check that compares only length would miss the swap.
- Gap investigations change copies of production code and check whether the oracle notices. An unnoticed fault starts an investigation: improve the histories, model, observations or comparison and run again. Enforcement gaps are not waived.
- Generated exploration, shrinking and replay; what finite testing does and does not establish.

## Make the next change easier

- Follow an optimization that accidentally changes the order of equal-priority tasks. Show the failure and the design decision it points back to.
- Explain how a later human or agent can use that decision when it becomes relevant to their work.
- When a bug slips through, improve the part of the oracle that missed it. More random inputs will not repair a comparison that ignores the relevant result.
- Use the subscriber-message story from Kyle’s DB post: checking message semantics exposed bugs that comparing final maps had hidden.

## Where these ideas come from

- Property-based testing: [QuickCheck](https://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/quick.pdf) and generators.
- Model-based testing: [fast-check’s conceptual overview](https://fast-check.dev/docs/advanced/model-based-testing/) and independent state/behavior judgments.
- Mutation testing: [Stryker’s introduction](https://stryker-mutator.io/docs/) and testing whether tests notice changes.
- Formal methods: [TLA+](https://lamport.azurewebsites.net/tla/tla.html) and specifying designs as models with properties to verify.
- These ideas apply across languages. The current JavaScript/TypeScript tooling is one integration of the method.

## Connect formal verification to production code

- Formal verification establishes properties of a model; an implementation oracle checks those same properties against production code across generated histories.
- Explain how Lean proofs and TLA+/TLC model checking can strengthen the models used to judge production behavior.
- Show the shared model connecting the two: **formal verification ↔ model ↔ production code**.

## Why coding agents change the economics

- Agents make demanding model, generator and investigation work more practical.
- Better checks make it practical to attempt more ambitious algorithms, richer behavior and larger changes.
- Preserve what each investigation teaches, so later work starts with better feedback and a clearer design.
- Read [Kyle’s DB/Oracles post](https://tanstack.com/blog/tanstack-db-1.0-rc1-going-all-in-on-oracles) and the existing [introduction](../intro.md).

<!-- Sources: intro, glossary, model styles, Kyle’s DB post and release-blog draft, and the conceptual sources inspected in the survey. -->
