| name | bisect-aware-instrumentation |
| description | Instrument code to support efficient git bisect by producing deterministic pass/fail signals and concise runtime summaries for each tested commit. Use when debugging regressions with git bisect, automating bisect workflows, creating bisect test scripts, handling flaky tests during bisection, or needing clear exit codes and logging for automated bisect runs. Helps identify the exact commit that introduced a bug through automated testing. |
Bisect-Aware Instrumentation
Overview
Instrument code to support efficient git bisect operations by producing deterministic pass/fail signals and concise runtime summaries. This skill helps create robust test scripts that work reliably with git bisect run, handling edge cases like flaky tests, build failures, and non-deterministic behavior.
Core Workflow
1. Understand the Regression
Before instrumenting, clarify:
- What behavior changed? (bug introduced, performance regression, test failure)
- What is the "good" commit? (known working state)
- What is the "bad" commit? (known broken state)
- How to reproduce the issue? (test command, manual steps)
2. Create Bisect Test Script
Generate a test script that returns proper exit codes for git bisect:
Exit Code Convention:
0: Good commit (test passes)
1-124, 126-127: Bad commit (test fails)
125: Skip commit (cannot test - build failure, missing dependencies)
Template:
#!/bin/bash
set -e
if ! make build 2>/dev/null; then
echo "SKIP: Build failed"
exit 125
fi
timeout 30s ./run_test || TEST_RESULT=$?
if [ $TEST_RESULT -eq 0 ]; then
echo "GOOD: Test passed"
exit 0
elif [ $TEST_RESULT -eq 124 ]; then
echo "SKIP: Test timeout"
exit 125
else
echo "BAD: Test failed with code $TEST_RESULT"
exit 1
fi
3. Add Determinism Safeguards
Handle non-deterministic behavior:
Retry Logic for Flaky Tests:
PASS_COUNT=0
for i in {1..3}; do
if ./run_test; then
((PASS_COUNT++))
fi
done
if [ $PASS_COUNT -eq 3 ]; then
echo "GOOD: All 3 runs passed"
exit 0
elif [ $PASS_COUNT -eq 0 ]; then
echo "BAD: All 3 runs failed"
exit 1
else
echo "SKIP: Flaky test ($PASS_COUNT/3 passed)"
exit 125
fi
Environment Isolation:
rm -rf /tmp/test_cache
export RANDOM_SEED=42
export TZ=UTC
4. Add Logging and Summaries
Generate concise output for each commit:
#!/bin/bash
COMMIT=$(git rev-parse --short HEAD)
LOG_FILE="bisect_log_${COMMIT}.txt"
echo "Testing commit: $COMMIT" | tee $LOG_FILE
echo "Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" | tee -a $LOG_FILE
if ./run_test > test_output.txt 2>&1; then
echo "RESULT: GOOD" | tee -a $LOG_FILE
exit 0
else
echo "RESULT: BAD" | tee -a $LOG_FILE
echo "Error output:" | tee -a $LOG_FILE
tail -20 test_output.txt | tee -a $LOG_FILE
exit 1
fi
5. Run Git Bisect
Execute the bisect workflow:
git bisect start
git bisect bad HEAD
git bisect good v1.2.0
chmod +x bisect_test.sh
git bisect run ./bisect_test.sh
git bisect log
Instrumentation Patterns
Pattern 1: Performance Regression Detection
#!/bin/bash
THRESHOLD=1000
DURATION=$(./benchmark | grep "Duration:" | awk '{print $2}')
if [ -z "$DURATION" ]; then
echo "SKIP: Benchmark failed to run"
exit 125
fi
if [ $DURATION -lt $THRESHOLD ]; then
echo "GOOD: Performance $DURATION ms (< $THRESHOLD ms)"
exit 0
else
echo "BAD: Performance $DURATION ms (>= $THRESHOLD ms)"
exit 1
fi
Pattern 2: Test Suite Bisection
#!/bin/bash
TEST_NAME="test_user_authentication"
if pytest tests/${TEST_NAME}.py -v; then
echo "GOOD: $TEST_NAME passed"
exit 0
else
echo "BAD: $TEST_NAME failed"
exit 1
fi
Pattern 3: Build Failure Detection
#!/bin/bash
if make clean && make all; then
echo "GOOD: Build succeeded"
exit 0
else
echo "BAD: Build failed"
exit 1
fi
Pattern 4: Output Validation
#!/bin/bash
EXPECTED_OUTPUT="Success: 42"
ACTUAL_OUTPUT=$(./program 2>&1)
if [ "$ACTUAL_OUTPUT" = "$EXPECTED_OUTPUT" ]; then
echo "GOOD: Output matches expected"
exit 0
else
echo "BAD: Output mismatch"
echo " Expected: $EXPECTED_OUTPUT"
echo " Actual: $ACTUAL_OUTPUT"
exit 1
fi
Advanced Techniques
Handling Complex Build Systems
#!/bin/bash
if ! command -v node &> /dev/null; then
echo "SKIP: Node.js not available in this commit"
exit 125
fi
if [ -f package.json ]; then
npm ci --silent || {
echo "SKIP: Dependency installation failed"
exit 125
}
fi
npm test
Parallel Test Execution
#!/bin/bash
parallel --halt soon,fail=1 ::: \
"pytest tests/unit/" \
"pytest tests/integration/" \
"npm run lint"
if [ $? -eq 0 ]; then
echo "GOOD: All tests passed"
exit 0
else
echo "BAD: At least one test failed"
exit 1
fi
State Preservation
#!/bin/bash
STATE_DIR=".bisect_state"
mkdir -p $STATE_DIR
git rev-parse HEAD > $STATE_DIR/current_commit
./run_test
RESULT=$?
echo "$(git rev-parse --short HEAD): $RESULT" >> $STATE_DIR/results.log
exit $RESULT
Troubleshooting
Issue: Bisect Marks Wrong Commit
Cause: Test script has incorrect exit codes or flaky behavior
Solution: Add verbose logging and retry logic
set -x
Issue: Too Many Commits Skipped
Cause: Build failures or missing dependencies across history
Solution: Use broader skip conditions
if git log -1 --format=%s | grep -q "WIP\|broken"; then
echo "SKIP: Known broken commit"
exit 125
fi
Issue: Bisect Takes Too Long
Cause: Slow test execution
Solution: Optimize test or use binary search hints
timeout 10s ./run_test || exit 125
git bisect skip $(git rev-list --grep="refactor" HEAD~50..HEAD)
Best Practices
- Make tests deterministic: Fix random seeds, timestamps, and external dependencies
- Use timeouts: Prevent hanging tests from blocking bisect
- Log everything: Save detailed logs for each tested commit
- Handle build failures gracefully: Use exit code 125 to skip untestable commits
- Test the test script: Verify it works on known good and bad commits before bisecting
- Keep it fast: Optimize tests to run quickly (bisect tests O(log n) commits)
Quick Reference
Start bisect:
git bisect start
git bisect bad <bad-commit>
git bisect good <good-commit>
Run automated bisect:
git bisect run ./bisect_test.sh
Manual bisect:
git bisect good
git bisect bad
git bisect skip
End bisect:
git bisect reset
Resources