Aller au contenu

Leçon 19 · Reviewing and Testing AI Code

Reviewing AI-Written Changes

A practical method to review Cursor agent diffs: check scope, behavior, security, tests and dependencies, spot common AI mistakes and undo changes safely.

An agent can produce a large change very quickly. Review then becomes the slow part, and it is the part that protects your users. The people who submit and merge code stay responsible for it, no matter who typed it.

Review while it works

You can watch the agent and stop it as soon as it goes the wrong way. Note what the security documentation says: the agent can modify files in your workspace without approval (configuration files are an exception and need approval), and changes are saved to disk at once. If your tools reload automatically, a change can run before you read it. So use Git and a safe development environment.

Review in passes

One read for everything at once misses things. Go in passes:

Pass Question
1. Scope Which files changed, and should they have?
2. Behavior Does it do what was asked, including edge cases?
3. Tests Do they check real behavior, and were any weakened?
4. Security Is input validated? Any secrets, injection or wider permissions?
5. Dependencies Anything new in package or configuration files?
6. Quality Is it readable and consistent with the project?

Start with the file list:

Terminal
git status
git diff --stat
git diff
git diff -- '*test*'

git diff does not show untracked files, so check them from the git status list. The diff lesson explains more options. The documentation also describes clicking Review and then Find Issues after a task. It is a helpful extra reader, not a replacement for you.

Mistakes to look for

  • Calls to functions or options that do not exist.
  • Code that handles only the happy path.
  • Errors swallowed by a broad except.
  • A weakened test, or a special case added only to pass a test.
  • A copy of a helper that already exists.
  • A new package that is not needed.
  • Leftover debug prints, unused code or unexpected files.

Find the red flags

This real script scans a diff for patterns that deserve a closer look. It cannot decide whether code is right; it only tells you where to look first.

Python Flag risky lines in a diff

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.

All of these are warning signs: a payment error silently ignored, a query open to SQL injection, and a precise test replaced by a weak one.

Fix the SQL, then prove it

The safe version uses parameters. You can demonstrate the difference with an in-memory database:

Python String-built SQL versus parameters

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 first query returns every row. The second treats the whole input as one value. Parameters do not replace authorization: you must still check that this user may see this order.

Ask for a skeptical second look

You can use the chat as another reviewer, preferably in a new chat, so the earlier reasoning is not carried over. Attach the diff with an @ Git mention:

TEXT
Review the attached diff as a skeptical reviewer. List bugs and
risks with file and line. For each one, describe an input that
triggers it. Do not praise the code, and do not change files.

"Describe an input" pushes toward claims that you can test. Verify each finding; reviewers, human or AI, can be wrong.

Keep diffs small

Small, focused changes are easier to review well, and huge ones tend to get skimmed. If a diff is too big, ask for the work to be split into separate commits or pull requests. A built-in /split-to-prs command exists for splitting large changes.

Accept, change or reject

  • Accept when you understand the change and the checks pass.
  • Change by editing it yourself or asking for a precise correction.
  • Reject when the approach is wrong. Restore a checkpoint or use Git, then improve your request.

Before you run git restore or git reset, check git status. These commands can also discard your own unsaved work.

On Enterprise plans, an attribution feature that extends git blame can show which lines came from Tab, an agent or a person. It helps with history, but it does not replace review.

Read beyond the diff

A diff shows what changed, not what should have changed. Ask what is missing:

  • If a function gained a parameter, were all callers updated?
  • If a field was added, what about old records, exports and documentation?
  • If a new endpoint exists, where is its permission check?
  • If a feature can fail, what does the user see?

Search for the old names and look at neighboring code. Then run the program and use the feature as a user would. Some problems, such as a button that does nothing, are easier to see in the running application than in text.

Finally, try to explain the change to someone else in a few sentences. Where your explanation becomes vague, you have found the part you have not yet understood. Go back to that part before you approve.

Common mistakes

  • Reading only the agent's summary instead of the diff.
  • Approving because the code "looks professional".
  • Skipping lock files and configuration changes.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Which review pass comes first in this method?

    Checking scope first reveals unexpected changes early.
  2. 2. A diff replaces assertEqual(total, 95) with assertTrue(total). What is the concern?

    Almost any nonzero total would now pass.
  3. 3. Why ask for a review in a new chat?

    A fresh context is less tied to earlier choices.

Summary

  • Review in passes: scope, behavior, tests, security, dependencies, quality.
  • Watch for invented APIs, swallowed errors and weakened tests.
  • Use SQL parameters and test your claims.
  • Keep changes small, and reject wrong approaches.

Next: Testing AI-Written Code

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.