Skip to main content
BeginnerDevelopmentEst. Time: 20-30 min

AI-Assisted Bug Fix

Isolate verified root causes, write reproducing regression tests, and fix defects systematically without guesswork.

The Problem

When facing bugs, AI agents frequently guess symptom patches (adding optional chaining `?.`, suppressive try-catch blocks) rather than diagnosing root causes, introducing silent technical debt.

Who It Is For

Developers debugging user-reported bugs, Sentry exceptions, or failing CI pipelines.

Prerequisites & Tools

Prerequisites Checklist:
  • Reproducible bug report or failing test scenario
  • Target repository with test suite configured
Tool / CommandTypeRole in Tutorial
start_taskmcpRegister bugfix contract with reproduction steps
mark_bug_fixedmcpRecord root cause analysis and evidence before code changes
run_testsmcpVerify regression test failure, then verify green

Example Task Contract

Exact start_task payload representing this workflow in DevWorkflow local-first governance.

AI Tool Call: start_task payload
{
  "description": "Prevent session crash and redirect to login when JWT expires during checkout",
  "type": "bugfix",
  "plan": [
    "Add unit test in tests/authInterceptor.test.js",
    "Update handleExpiredSession to emit reauth event and save cart draft",
    "Verify all authentication tests pass"
  ],
  "definitionOfDone": [
    "Reproduction test verified failing before implementation",
    "Failing test passes cleanly after fix",
    "No unhandled Promise rejections on expired tokens"
  ]
}

Step-by-Step Tutorial Workflow

Follow these sequential actions with validated copyable commands and expected outputs.

1

Register Bugfix Task

Pass `type: 'bugfix'` with reproduction steps, expected behavior, and observed failure.

Code / Tool Payload
Execution Payload
{
  "description": "Fix null pointer crash when user session expires during checkout",
  "type": "bugfix",
  "plan": [
    "Write reproducing test simulating expired auth token on checkout API",
    "Identify root cause in token refresh interceptor",
    "Implement graceful redirect to login with cart preservation",
    "Run paratest / jest suite to confirm regression is fixed"
  ],
  "definitionOfDone": [
    "Reproduction test fails before fix and passes after fix",
    "Cart state remains preserved in storage on 401 response",
    "All tests pass with zero regressions"
  ]
}
2

Reproduce with a Failing Test First

Never touch production code before reproducing the defect with a failing automated test.

Copyable Command
Terminal Command
npm test -- -t 'handles expired session'
Expected Terminal Output
FAIL tests/checkout.test.js
  ✕ handles expired session (TypeError: Cannot read properties of null)
3

Apply Minimal Root-Cause Fix

Fix the underlying bug at the source. Re-run tests to confirm green status.

Copyable Command
Terminal Command
npm test -- -t 'handles expired session'
Expected Terminal Output
PASS tests/checkout.test.js
  ✓ handles expired session (12.4ms)

Verification Steps

Verify that your work genuinely satisfies the task contract before closing.

Verify git diff touches only the root cause file and new regression test
Run full test suite to guarantee zero regression

Limitations & Troubleshooting

Issue: AI agent wants to fix by adding try/catch without writing test
Resolution: Enforce the TDD discipline rule: reject the proposal until a reproduction test exists.