Skip to main content

DriftCatch

Learns what your AI agent always does — then tells you the moment it stops.

python tests license status

A passing replay, then a regression caught and its cause named.

0.1 pre-release. The engine, CLI, pytest plugin, local UI and MCP server are implemented and tested against the real OpenAI and Anthropic SDKs — 407 tests, mypy --strict clean. Not yet on PyPI, and not yet used by anyone but its author. If you try it and something is wrong, an issue is more useful to me than a star.


The problem

You tweak one word in a prompt. Your agent quietly stops querying the database and starts web-searching. The answer still looks right, so every eval passes. You find out from a customer.

Or worse — the trajectory is identical, every tool fires in the right order, and the refund amount is wrong by a factor of a hundred.

Agents don't fail loudly. They drift.

30 seconds

import driftcatch as drift

@drift.tool
def lookup_order(order_id: int) -> dict: ...

def test_refund():
    with drift.take("refund_1182") as take:      # records once, replays forever
        result = refund_agent("refund order 1182")

    take.assert_tools().called("lookup_order").never("web_search")
    take.assert_invariants().hold()              # rules you never wrote
pytest            # run 1: records a PENDING recording
drift review      # a human approves it -> golden
pytest            # run 2: replays. zero API calls, milliseconds.

The 60-second demo

assets/driftcatch-launch.mp4 — or render it yourself from demo/, where the script and the Remotion source live.

Every command in it exists, and the drift bisect output is copied from a real run against the sandbox in demo/seed_demo.py. An earlier cut showed a composite nobody could reproduce; that seemed like the wrong corner to cut for a tool about honest failure reporting.

What makes this different

Record/replay for agents is not a differentiator. vcrpy, pytest-agentreplay, agentreplay, graphtrace, trajence, agent-trajectory-diff and LangChain's own agentevals all record runs and diff tool-call sequences. If that is all you need, use one of those — several are good.

DriftCatch is built on a different idea:

Every other tool treats a recording as a mock. DriftCatch treats a corpus of approved recordings as a specification you never had to write.

1. It learns your agent's rules ⭐

Six approved recordings. Nobody wrote a single assertion:

$ drift learn

  Mined 15 invariant(s) from 6 approved recording(s):

  ALWAYS lookup_order always runs before issue_refund                6/6 runs  conf 0.98
  ALWAYS notify_customer is always the last tool                     6/6 runs  conf 0.98
  ALWAYS issue_refund.args.amount always equals lookup_order.result.total  6/6  conf 0.98
  BOUND  a run never makes more than 3 tool calls                    6/6 runs  conf 0.98
  NEVER  web_search is offered but never called                      6/6 runs  conf 0.98

  15 not yet enforced. Mined rules are evidence, not truth -
  review them, then: drift learn --approve

That third rule is a business rule your agent follows that nobody ever wrote down. Then a new run arrives:

trajectory:  lookup_order -> issue_refund -> notify_customer     ← looks perfect
x issue_refund.args.amount always equals lookup_order.result.total
    5000 != 50
    (held in 6/6 previous runs)

A decimal slipped. Every trajectory-diffing tool passes this run. Caught by a rule nobody wrote, in a scenario nobody tested, for free.

This is specification mining — 25 years proven in conventional software (Daikon, Perracotta), never before pointed at agent trajectories. It needs a corpus of runs you know are correct, which is why the human approval gate is load-bearing rather than ceremony.

Four filters stop it generating confident nonsense:

Filter Why
support ≥ 5 two observations is a coincidence
confidence ≥ 0.95 (1 − 0.5ⁿ) Daikon's statistical gate
the value must vary across runs two fields both 40 every time is a constant, not a relationship
redundancy removal transitive ordering and equality collapsed, so 19 rules become 15 distinct facts

Daikon's own evaluation found ~⅓ of mined invariants were wrong or irrelevant — which is exactly why nothing is enforced until you approve it.

2. Behaviour coverage

$ drift coverage

  BEHAVIOUR COVERAGE    25.0%  #####...............

  TOOL                      CALLED  CALLS   ERROR PATH
  escalate_to_human             NO      0           NO
  issue_refund                 yes      1           NO
  web_search                    NO      0           NO

Code coverage tells you which lines ran. This tells you which behaviours were ever tested — and the denominator comes free from the tool schemas in your own recorded request. Each tool counts twice: success path and failure path.

--fail-under 80 gates CI (exit code 4). --badge prints the badge.

3. Root-cause attribution

Other tools say "it differs." DriftCatch says why:

$ drift bisect refund_1182 --previous

  ROOT CAUSE: the system prompt.
    - Use available tools to find
    + Search for

Certain when exactly one input moved; it declines to guess when several did.

4. Drift detection

Four inputs — system prompt, model id, tool schemas, matcher config — hashed separately, so a change names itself:

⚠ recording "refund_1182" is STALE: prompt_hash changed since it was approved

Detected offline during a normal replay run, before anything is sent. Comparing inputs never requires the model.

5. What-if, without re-recording

with drift.take("refund_1182").what_if(tool="send_reply", raises="TimeoutError") as take:
    refund_agent("refund order 1182")
take.assert_tools().call_count("send_reply", 2)      # the retry finally runs

Coverage finds the gap; what-if fills it. The file on disk is never modified.

6. Determinism scoring

result = drift.run_live("refund_1182", lambda: agent("..."), runs=20)
result.assert_matches_golden(pass_k=(17, 20))
20 live runs · 3 distinct trajectories · DETERMINISM 0.85
recommended gate: pass_k=(17, 20)

Assertions

take.assert_tools().called_in_order("lookup_order", "issue_refund")
take.assert_trajectory().matches_golden(tolerance="exact")
take.assert_output().is_json().json_has("decision", "amount")
take.assert_output().excludes("internal_customer_id")
take.assert_cost().under(tokens=2_000)
take.assert_latency().under(total_ms=5_000)
take.assert_invariants().hold()

Latency assertions skip during replay, with a warning, rather than comparing a recorded duration against itself and passing forever. Replay reads bytes from disk; there is no call to time. See assertions.

Scenario suites

One body, many cases, one shared policy — so the fortieth scenario gets the assertion somebody added to the third.

refunds = drift.Suite(
    "refund",
    cases={"small": "refund order 1182", "large": "refund order 9999"},
    check=lambda take: take.assert_invariants().hold(),
)

@refunds.parametrize
def test_refund(take, case):
    run_agent(case)

Each case gets its own recording, so a diff still points at one scenario.

Built for coding agents

Cursor, Claude Code and Codex are first-class users, not an afterthought.

drift init writes an AGENTS.md your agent reads automatically — including the rules it would otherwise get wrong ("do not re-record to make a failing test pass").

Every read command takes --json with a stable {ok, command, ...} envelope, clean stdout, no ANSI, and errors as JSON with a hint field.

Documented exit codes, so an agent branches before parsing:

0 ok · 1 error · 2 usage · 3 regression · 4 gate_failed
5 secrets_found · 6 not_found · 7 not_approved

drift explain --json is a self-describing manifest — one call and the agent knows the commands, workflows, cautions and what this tool is not for.

An MCP server (drift mcp) exposing 10 tools. See docs/MCP.md.

What the agent deliberately cannot do: approve, reject, record, restore and live runs are not exposed. An agent's reflex on a red test is to make it green — and in DriftCatch the green button is approve, which means "a human confirmed this behaviour is correct". pending_approvals lets the agent surface the decision without making it.


Honest limits

Two tiers, and we are explicit about which catches what:

Tier Cost Catches Cannot catch
1 — replay (every commit) $0, ms changes to your prompt, model id, tool schemas, code; broken learned invariants what the model would decide today — it isn't running
2 — live check (deliberate) real spend model-behaviour change, provider-side change nothing deterministically; thresholded with pass_k

Any tool claiming replay alone catches model-behaviour change is wrong about the physics.

Also stated plainly:

  • Not a production cache. It errors on any request it hasn't recorded. Using it to serve users would be dangerous.
  • Not an evaluation tool. It never scores answer quality. Answer text may change freely; behaviour must not.
  • No universal tool capture. @drift.tool is one line per tool; there is no generic hook.
  • Invariant mining needs a corpus — at least 5 approved recordings before it says anything.
  • Mined rules are likely invariants, not proofs. Review them.

Security

Redaction runs at record time, before anything touches disk, and fails closed — if a credential-shaped value survives, the recording is not written at all. Headers are never recorded. drift scan audits existing recordings (exit 5 on findings) for a pre-commit hook. The UI binds loopback only and refuses anything else.


Install

pip install driftcatch                 # not yet published
pip install "driftcatch[ui]"           # local web UI
pip install "driftcatch[mcp]"          # MCP server for coding agents

CLI — 24 commands

drift init                          # set up, write AGENTS.md, print a starter test
drift explain [--json]              # self-describing manifest for agents
drift status [--json]               # recordings: status, behaviour, drift
drift status --require-approved     # exit 7 if anything is still pending (CI)
drift show <name>                   # one recording in full
drift review [name] [--yes]         # walk the pending queue  (the human gate)
drift approve <name> --by you       # approve one, non-interactively
drift reject <name>                 # mark one wrong, so it never becomes golden
drift learn [--approve] [--json]    # mine the rules your agent follows
drift invariants [--json]           # learned rules and whether enforced
drift contracts [--json]            # declared tool schemas vs how they are used
drift coverage [--fail-under N]     # behaviour coverage (+ --badge, --json)
drift suggest                       # scenarios for uncovered paths
drift diff <name> --against <other> # behaviour diff
drift bisect <name>                 # attribute a behaviour change
drift bisect <name> --previous      # ...against the version a re-record replaced
drift restore <name>                # undo a re-record
drift what-if <name> --fail-tool X
drift audit <name>                  # how each request keys
drift scan [--json]                 # secret / PII audit
drift prune                         # drop raw bodies, keep behaviour
drift record / drift check [--live] # run your tests in the right mode
drift ui                            # local web UI
drift mcp                           # MCP server for coding agents

Documented exit codes, so CI and agents can branch without parsing output: 0 ok · 1 error · 2 usage · 3 behaviour regression · 4 gate failed · 5 secrets found · 6 not found · 7 not approved.

pytest

pytest --drift-mode=replay        # auto | record | replay | live
pytest --drift-update             # re-record (returns to pending; old version archived)
pytest --drift-policy=fail        # treat stale recordings as failures

Extending

Three match strategies (field, positional, tool_call) plus a chain(...) combinator, five normalizers, and a registry where your own are first-class:

@drift.matcher("match_on_question_only")
def _(request, candidates, state, ctx): ...

Development

python -m venv .venv
.venv\Scripts\python.exe -m pip install -e ".[dev]"
.venv\Scripts\python.exe -m pytest -n 4
.venv\Scripts\python.exe -m ruff check driftcatch tests examples scripts
.venv\Scripts\python.exe -m mypy driftcatch

Try the demos:

.venv\Scripts\python.exe scripts\demo_invariants.py
.venv\Scripts\python.exe -m pytest examples/refund_agent -q

Documentation

Full docs: docs/index.md

Getting started Install to caught regression, five minutes
Core concepts Two tiers, the approval gate, fingerprints
Recording and replay Modes, capture, streaming, pruning
Assertions Tools, trajectory, output, cost, latency
Scenario suites One body, many cases, one policy
Matching Matchers, normalizers, chaining
Learned invariants Rules mined from the corpus
Tool contracts Declared schema vs real use
CLI reference Every command and exit code
CI integration Pipelines, JUnit, what to gate on
Coding agents and MCP Cursor, Claude Code, the MCP server
Configuration Every setting and its default
Security Secrets, redaction, the MCP boundary
Troubleshooting Specific errors and fixes
FAQ Including what this deliberately does not do

Design background:

License

MIT. See LICENSE.

Metadata

Release files for driftcatch 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for driftcatch 0.1.0
File Size Uploaded Provenance Actions
driftcatch-0.1.0.tar.gz 218.5 kB Without provenance Details for driftcatch-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for driftcatch 0.1.0
File Interpreter ABI Platform Actions
driftcatch-0.1.0-py3-none-any.whl Python 3 none any Details for driftcatch-0.1.0-py3-none-any.whl

Total release size: 334.8 kB

Release files / driftcatch-0.1.0.tar.gz

Download URL driftcatch-0.1.0.tar.gz
Size 218.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9d5823f5f705f05e8178c9203831692a4bd6205baaaf38f73785876ba9f14765
BLAKE2b-256 checksum
How to use checksums
5a4a46e532d7bc2a0474d0db654b4afcf15ad024d8a0b6b90199e0b1b0b8a010
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / driftcatch-0.1.0-py3-none-any.whl

Download URL driftcatch-0.1.0-py3-none-any.whl
Size 116.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70e01ba56772b187d0511bc823e5382a8e7f1159e7a5d5dc1397e57824e638fc
BLAKE2b-256 checksum
How to use checksums
4d69a89e490866e0e6618cd04ae0e87249715e965b104f0fce5e708828faf42c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for driftcatch 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page