TanStack Oracles: how the TanStack team teaches AI agents to hunt bugs using formal behavior lawsNote · digest for December 5
European comic-style illustration: a robot detective with a magnifying glass and a book of strict rules spots hidden red cracks in a seemingly green machine
Note3 sections≈ 4 min

TanStack Oracles: how the TanStack team teaches AI agents to hunt bugs using formal behavior laws

It started while polishing the TanStack DB client store for 1.0 RC1. Collections send insert, update, and delete events to subscribers, and a test subscriber handled both insert and update with Map.set. A buggy update on a deleted key silently recreated the row, so final state looked fine.

Once they tightened the check (only insert new keys, only update or delete existing ones), 26 of 378 old tests failed right away without adding a single new case. Those runs had been hitting a delete-and-sync race bug for months; the test just hadn't noticed.

Out of that lesson came TanStack Oracles by Kyle Mathews and the TanStack team (@tanstack/oracles, preview 0.0.1, MIT). It gives AI agents formal rules to hunt down reproducible bugs.

Why unit tests and agent reviews fall short

Agents made writing code cheap, so verification is now the bottleneck. Asking another agent to 'look for bugs' is a weak bet: without a clear standard, it misses state races or nitpicks style.

Unit tests only check scenarios you thought of ahead of time. When caches, live queries, optimistic edits, and sync collide, there are too many combinations. In three months, TanStack DB grew 211 oracles across 15 packages and roughly 420,000 lines of tests, all driven by contracts.

How an oracle works and how the agent runs it

A test oracle is an independent judge that gives the right answer for any sequence of actions. The same history runs through fast production code and a simple reference model. If their outputs differ, you've caught a bug.

The framework ties together property-based testing via fast-check (thousands of histories shrunk to minimal failures), model checks, fuzzing, and mutation testing. For 'top 10 open tasks by priority and ID', the reference model is tiny:

// Reference model: no indexes, recompute from scratch
function expectedTopTasks(tasks: Iterable<Task>) {
  return [...tasks]
    .filter((t) => !t.completed)
    .sort((a, b) =>
      b.priority - a.priority
        || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
    )
    .slice(0, 10)
    .map((t) => t.id)
}

In eight lines of plain sorting, bugs have nowhere to hide. Next, oracleProperty connects the model, history generator, driver, and law:

// Law is a plain TS object, no custom DSL needed:
// statement (rule), authority (source), limits (scope)
oracleProperty({
  id: 'top-open-tasks',
  law: topTasksLaw,
  histories: taskHistories,
  model: expectedTaskResults,
  driver: driveTaskQuery,
  check: checkTaskResults,
})

No custom DSL is needed: topTasksLaw is a plain object holding the rule, authority, and scope. If an agent breaks sorting during an optimization, the oracle returns a short failing trace and the broken law.

You can start the loop with the oracles-explore/SKILL.md skill from TanStack/oracles:

  1. The agent installs @tanstack/oracles and probes the code.
  2. It proposes a list of laws for a human to approve.
  3. It builds the model, runs oracleProperty, and reports failures.
  4. It runs oracle-gaps, a mutation CLI that breaks copies of the code to find gaps in the oracle.

Mathews ran this loop on 100+ repos including node-lru-cache, immer, and react-headroom, finding bugs in every one. In react-headroom, a single law ('wrapper height matches header height') caught the header overlapping page text.

Preview limits and where it pays off

Version 0.0.1 is early and the API will change. For now, oracle-gaps and integrations only support JS/TS on Node.js 20+, Vitest 4.1+, and fast-check 4.

The biggest trap is model independence. If an agent pulls production helpers into the reference model, the model copies the same bug. You guard against this with @tanstack/eslint-plugin-oracles, calibration checks (deliberate wrong answers), and a critic sub-agent review.

The second filter is cost. TanStack DB took three months and billions of tokens, and oracle-gaps hits your CPU hard. It's overkill for a simple CRUD form. In caches, parsers, sync engines, and pagination, though, catching one subtle bug saves days of debugging.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 5