Skip to content
mentoor

Topic 4 of 5 · 4 min read

Bisecting regressions with an agent

Goal: Set up a bisect run that an agent can drive with a scripted check, and read which commit it blames.

When something used to work, stop reading code and let git binary search the history: give it a script that says good or bad, and it finds the first bad commit. An agent can write that script and drive the search, but you check the result.

Check yourself · 1 of 3

What does git bisect run ./check.sh do?

Choose one answer

Why it matters

A regression has a cause in a specific commit. Reading a diff of hundreds of changes is slow, and an agent reading it will guess. Bisect halves the range each step, so even a long history takes only a handful of checks. The git documentation describes this binary search, and the Pro Git book walks through it.

How to do it

  1. Find a good commit and a bad one. The bad one is usually HEAD. The good one is a tag or a date when it worked.
  2. Write a check script. It exits 0 when the behaviour is fine, and 1 to 127 (except 125) when it is broken. Exit 125 means "cannot test this commit", and git skips it.
  3. Start and run:
git bisect start
git bisect bad HEAD
git bisect good v2.3.0
git bisect run ./check.sh
  1. Read the result. Git prints "<hash> is the first bad commit" with its message.
  2. Run git bisect reset to return to your branch.

Ask the agent to write check.sh from your reproduction, to run it on a known good and a known bad commit by hand, and only then to start the bisect.

A script that works

#!/bin/sh
npm ci --silent || exit 125
npx vitest run tests/invoice.test.ts --reporter=dot

The install step exits 125 when it fails, so a broken build is skipped rather than blamed.

What goes wrong

  • A flaky check. If it passes and fails at random, bisect blames the wrong commit. Run it several times first.
  • Wrong good commit. If it was already broken there, the result is meaningless.
  • Build failures counted as bad. Use 125 for them.
  • Trusting the blame blindly. The first bad commit shows where the behaviour changed. Read its diff to understand why, and write a fix.
  • Forgetting to reset, and leaving the repo in a detached state.

Examples

Letting the agent drive

Write check.sh that runs tests/invoice.test.ts and exits non-zero on failure.
Run it on v2.3.0 (expect pass) and HEAD (expect fail) and show both outputs.
Then run git bisect with it and report the first bad commit.

The agent proves the script works at both ends before it relies on it.

Reading the answer

7c1e9a4 is the first bad commit
    refactor: switch invoice rounding to Math.round

The message names the change. Open the diff, confirm that it explains the symptom, and fix it with the reproduction as the check.

Key points

  • Bisect needs one known good commit, one known bad commit and a script that decides.
  • The script exits 0 for good, 1 for bad, and 125 to skip a commit that cannot be tested.
  • Check the script on both ends by hand before running the search.
  • A flaky check gives a wrong answer, so make it deterministic first.
  • Read the blamed commit's diff, then fix; and run git bisect reset when done.

Common mistakes

  • Starting bisect without testing the script on a good and a bad commit.
  • Using a flaky or slow test as the check.
  • Treating a failed build as a bad commit instead of skipping it.
  • Never running git bisect reset.

Check yourself · 2 of 3

In a bisect script, which exit code tells git to skip a commit it cannot test?

Choose one answer

Check yourself · 3 of 3

Bisect keeps blaming an unrelated commit, and the check passes sometimes and fails sometimes. Likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here