Debugging Workflows
Your API endpoint returns a 500 error, but only for certain users. You paste the error into Cursor Agent mode and say “fix this.” It changes three files, the error goes away for your test case, but now a different set of users gets a 403 error. You tell Agent to fix that too. Two more file changes. Now the original 500 is back. You have been going back and forth for 20 minutes and you are further from a fix than when you started.
The problem is not Cursor. The problem is treating debugging as “tell the AI to fix it and hope.” Effective AI-assisted debugging is a structured process: investigate first, understand the root cause, then apply a targeted fix.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A systematic debugging workflow using Ask, Debug, and Agent modes in sequence
- Copy-paste prompts for each stage of the debugging process
- Techniques for using logs, error traces, and runtime data with AI
- Patterns for preventing the “fix-break-fix” loop
The Three-Phase Debugging Workflow
Section titled “The Three-Phase Debugging Workflow”Phase 1: Investigate (Ask Mode)
Section titled “Phase 1: Investigate (Ask Mode)”Before touching any code, understand what is happening:
This gives you a mental model of the problem before you start changing things.
Phase 2: Gather Evidence (Debug Mode or Manual Logging)
Section titled “Phase 2: Gather Evidence (Debug Mode or Manual Logging)”Once you have hypotheses, gather runtime evidence to confirm or rule them out.
Option A: Use Debug Mode
Switch to Debug mode and describe the bug. The agent will:
- Generate hypotheses about root causes
- Add strategic log statements to the code
- Ask you to reproduce the bug
- Analyze the captured logs
- Identify the root cause based on evidence
Option B: Manual Log-and-Analyze
If Debug mode is not available or you prefer manual control:
After reproducing the bug, paste the logs back into Cursor:
Phase 3: Targeted Fix (Agent Mode)
Section titled “Phase 3: Targeted Fix (Agent Mode)”Only after you understand the root cause, switch to Agent mode for the fix:
The Fix-Then-Verify Pattern
Section titled “The Fix-Then-Verify Pattern”After every fix, verify it actually works by adding a test that would have caught the bug:
Add a regression test for the bug we just fixed.
The test should:1. Set up the exact conditions that caused the failure (user with [specific condition])2. Call the endpoint with the same request that was failing3. Assert that it succeeds with the correct response4. Also test the edge case where [related condition]
Put the test in @src/routes/__tests__/orders.test.ts following existing patterns.Run the test to confirm it passes.If the test fails, you know the fix was incomplete. If it passes, you have a regression test that prevents this bug from returning.
Common Debugging Patterns
Section titled “Common Debugging Patterns”Pattern: Type Error Investigation
Section titled “Pattern: Type Error Investigation”TypeScript reports this error at build time:
[PASTE TSC ERROR]
Trace the types involved. Show me:1. Where the type is defined2. How it flows through the code to this point3. Why the types are incompatible4. The minimal fix that maintains type safety
Do not use `any` or type assertions to fix this. Find the real type issue.Pattern: Race Condition Analysis
Section titled “Pattern: Race Condition Analysis”This code has a race condition -- when two requests arrive simultaneouslyfor the same user, the second request overwrites the first one's data.
Relevant code: @src/services/user-service.ts
Analyze the concurrent execution paths and identify:1. Which operation is not atomic2. Where the race window exists3. The best fix (optimistic locking, database transaction, or mutex)
Show me the fix with before/after code comparison.Pattern: Build Error Cascade
Section titled “Pattern: Build Error Cascade”When you have many build errors, do not fix them one by one:
This is the TDD-style workflow that Steve Sewell at Builder.io popularized: let the agent run the build, see errors, fix them, and iterate until everything passes — all automatically when auto-run is enabled.
When This Breaks
Section titled “When This Breaks”The “fix-break-fix” loop. You are skipping Phase 1 (investigation). Go back to Ask mode, understand the system, then make one targeted change.
Agent “fixes” the test instead of the code. Explicitly say: “The test is correct. The implementation is wrong. Fix the implementation to make the test pass.”
Debug mode logs do not capture the issue. The bug may be in a different part of the call chain than expected. Ask the agent to add logging more broadly, or reproduce the bug multiple times to capture intermittent behavior.
Too many errors to fix at once. Triage: fix compile errors first (they block everything else), then runtime errors, then logic errors. Use the build error cascade pattern above.
What’s Next
Section titled “What’s Next”- Testing Patterns — Writing tests that prevent bugs from recurring
- Refactoring Strategies — Cleaning up code after bug fixes
- Review Workflows — Catching bugs before they reach production