Fix the bisect over an agent's commit stack

Review the AI's codeMedium15 min

Your task

Overnight an AI agent landed 6 commits in your Go monorepo, and now an integration test fails about 30% of the time on CI. Every commit passed its own check, because one run of a flaky test proves little. You bisect the agent's stack, judging each commit by repeated runs.

An assistant wrote first_failing_commit(run_logs, runs_per_step), the bisect. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • run_logs: one string per commit, oldest first. Index 0 is the parent commit from before the agent's stack, and the last index is the tip. Each character is one test run: F is a failure and any other character is a pass.
  • runs_per_step: how many runs to look at for each commit.
  • Return the index of the first bad commit, or -1.

Rules the tests check:

  • A commit is bad if any of its first runs_per_step runs is F (uppercase only). If it has fewer runs, look at all of them. A commit with no runs is good.
  • Return -1 if there are no commits, if runs_per_step is 0 or less, if the parent (index 0) is bad, or if the tip (the last index) is good.
  • Otherwise run a binary search. Start with low = 0 and high = the last index. While high minus low is more than 1, judge the commit at mid = (low + high) / 2, rounded down. If it is bad, set high = mid; if not, set low = mid. Then return high.
  • Judge only the commits the search visits. With flaky logs the answer is the commit this search lands on.

Examples

first_failing_commit(run_logs: list[str], runs_per_step: int) -> int
Example 1
Inputfirst_failing_commit(["PPPP", "PPPP", "PPPP", "FPPP", "FFPP"], 4)
Output3
Example 2
Inputfirst_failing_commit(["PPPP", "PPPP", "PPPP"], 4)
Output-1