跳到正文

第 11 课 · Agent Mode

Debug Mode in Cursor

Use Cursor Debug mode to collect runtime evidence, test hypotheses with logs, fix the root cause of hard bugs and remove the added logging afterward.

Some bugs are very hard to solve by reading code. They depend on timing, data or the order of events. A guess may "fix" something that was never broken. Debug mode takes another route: it forms hypotheses, adds logging, uses what happens at runtime to find the cause, and then fixes it.

When to use it

The documentation recommends it for:

  • Bugs you can reproduce but cannot explain from the code.
  • Race conditions and timing problems.
  • Performance problems and memory leaks.
  • Regressions, where something that worked has stopped working.

For an obvious error with a clear traceback, ordinary Agent mode may be enough.

The documented workflow

Evidence first

Hypotheses Several possible causes
Add logs
Reproduce
Read the data
1 / 2

According to the documentation, the added log statements send data to a local debug server run by the product's extension, and the agent gives you steps to reproduce the problem. Switch to Debug mode with the mode picker or Shift+Tab.

Give it a useful bug report

TEXT
Bug: sometimes the cart total is wrong after changing a quantity.
Steps: add two items, then change the first quantity twice, quickly.
Expected: the total matches the quantities shown.
Actual: the total matches the earlier quantity, about 1 try in 5.
Started: after we added autosave last week.
Error text: nothing in the console.

Notice "about 1 try in 5". Frequency is a strong clue for a timing problem. Follow the reproduction steps you are given exactly, and reproduce several times for intermittent bugs.

Watch the method work

Here is a real, deterministic Python example of a stale response bug. Two save requests finish in the opposite order from how they started. The log shows the cause.

Python Use logs to find a stale response bug

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The hypothesis "the math is wrong" is now unlikely: each value is correct, but the responses arrive out of order. A targeted fix is to ignore any response older than the newest one already applied.

Python The targeted fix, verified

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Testing every arrival order is much stronger than running the program a few times and hoping that the bad order appears. Remember that this fixes what the screen shows. In a real system, also check what the server saved.

Log safely

Logs can leak data. Before you keep or share them:

  • Do not log passwords, tokens, payment data or personal details.
  • Prefer IDs over names and email addresses.
  • If you add your own temporary lines, give them a tag, such as DEBUG-1234, so you can find them.

The documented workflow removes the instrumentation after verification. Check that it did, because leftover logging is easy to miss. Read git diff, and search for your tag:

Terminal
git diff
git grep -n "DEBUG-1234"

Judge the fix

Ask these questions:

  1. Does the explanation match the evidence in the logs?
  2. Does the fix address the cause, or only hide the symptom? Adding a delay, or catching and ignoring an exception, often only hides it.
  3. Is there a test that failed before and passes now?
  4. Were all temporary logging lines and debug files removed?

For debugging techniques in code, see Python debugging.

Think in hypotheses

You can use the same method yourself, with or without a special mode. For each possible cause, write down what you would expect to see if it were true:

Hypothesis Evidence that supports it Evidence against it
The total is calculated wrongly Wrong value even for one request Correct values in every log line
Responses arrive out of order The older response is logged last Wrong total with only one request
A cached value is shown No new request is logged Logs show fresh data on screen

A good log line answers a question from this table. A line that only says "here" rarely does. Include an ID that links related events, the value you care about and the order of events.

When you cannot reproduce it

Sometimes the bug appears only on a user's machine. Do not let anyone, human or AI, guess a fix and call it solved. Instead, collect facts: versions, time, steps and error messages with private data removed. Add safe, minimal logging through your normal release process, and wait for the next occurrence. If you ship a speculative fix, label it as speculative and define how you will know whether it worked.

Common mistakes

  • Saying only "it does not work". Give steps, expected and actual results.
  • Accepting a fix without having reproduced the original bug.
  • Debugging directly on a live production system. Reproduce in a safe environment with test data.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What makes Debug mode different from asking for a quick fix?

    It forms hypotheses, logs and reads runtime data to locate the cause.
  2. 2. Which change is likely only hiding a race condition?

    A delay changes timing but does not guarantee order.
  3. 3. What should you check after the bug is fixed?

    Instrumentation should not stay in the code; check the diff.

Summary

  • Debug mode uses hypotheses, logs and reproduction to find root causes.
  • A good report has steps, expected and actual results, and frequency.
  • Confirm the fix with a test that covers the failing case.
  • Keep secrets out of logs and remove instrumentation.

Next: Worktrees and Cloud Agents

你的学习进度保存在此浏览器中,无需账号。