Debug a runtime failure with an agent and the debugger

Runtime failures such as crashes, unexpected exceptions, race conditions, and corrupted state often depend on live inputs, environment settings, or call order that static code reading misses. Pairing the Chat view with the built-in Visual Studio Code debugger lets you collect real runtime evidence while using an agent to form hypotheses, locate root causes, and verify fixes.

In this guide, you investigate a reproducible runtime failure in your own project, use the debugger to test the agent's hypotheses against live state, lock in a failing regression test, and verify a minimal fix.

If you want a guided practice tutorial with a sample project first, complete Fix an API bug with an agent.

Prerequisites

Project: Your repository with a reproducible runtime failure. Harness: Copilot. Interface: Chat view.

1. Capture runtime evidence before asking for a fix

Start by collecting concrete output from the failing run instead of describing the symptom from memory. In Copilot harness sessions, share runtime evidence in one of these ways:

Evidence source How to share it in the Chat view
Stack trace, terminal output, or Debug Console output Copy and paste the relevant output directly into the chat prompt. Pasted blocks attach cleanly as text context.
Saved log file Select Add Context (+) > Files and select the log file, even if it sits outside your workspace.
Web application runtime errors Open the app in the integrated browser and select Add to Chat > Add Console Logs to Chat.
Failing command or test script Ask the agent to run the reproduction command with Manual permissions enabled so you approve the command and the agent inspects stdout and stderr directly.
Caution

Review stack traces and log files before sharing them in chat. Redact API keys, access tokens, connection strings, and sensitive customer data.

2. Form testable hypotheses before editing code

Ask the agent to diagnose the failure in read-only mode first. A confident explanation from static code is a hypothesis, not proof of the root cause.

  1. Open the Chat view (⌃⌘I (Windows, Linux Ctrl+Alt+I)), start a new chat, and confirm Copilot, Agent, and Manual permissions are selected.

  2. Paste the stack trace or attach the log file, describe how to reproduce the failure, and ask for ranked hypotheses plus where to inspect runtime state:

    Investigate this runtime failure without editing any files.
    Here are the reproduction steps and error output:
    <paste stack trace or log excerpt>
    
    List the top 2 or 3 root-cause hypotheses with exact files and line numbers.
    For each hypothesis, suggest specific breakpoints, conditional breakpoints,
    or logpoints to confirm or rule it out in the debugger.
    
  3. Review the referenced files in the editor and check whether the suggested breakpoint locations match the failing code path.

3. Inspect live state with the debugger beside chat

Use the VS Code debugger to test the agent's leading hypothesis against actual runtime values.

  1. If the project does not have a debug configuration yet, ask the agent to generate a launch.json configuration. Verify the generated command, arguments, and environment variables before running it.

  2. Set the suggested breakpoints, conditional breakpoints, or logpoints in the editor margin (F9). Prefer logpoints for high-frequency loops or timing-sensitive code where pausing changes the behavior.

  3. Start debugging (F5) and trigger the failure.

  4. When execution pauses, inspect the VARIABLES and CALL STACK panels in the Run and Debug view and evaluate expressions in the Debug Console (⇧⌘Y (Windows, Linux Ctrl+Shift+Y)). Use Step Over (F10) to watch state change across lines.

  5. Share the concrete debugger observations back in the Chat view. For example:

    I paused at <file>:<line> right before the crash.
    The variable <name> was <actual value> instead of <expected value>,
    and the call stack entered from <caller>.
    Confirm the root cause and explain the smallest safe fix without editing files yet.
    

Repeat this check until runtime observations confirm the exact root cause.

4. Add a failing regression test and apply a focused fix

Once live evidence confirms the root cause, encode the failing condition in a test before changing production code.

  1. Ask the agent to write a minimal regression test or reproduction script that triggers the confirmed root cause:

    Write a focused regression test that reproduces this failure for <confirmed root cause>.
    Run the test and confirm it fails with the same error before editing the implementation.
    
  2. Verify the test fails for the expected reason.

  3. Ask the agent to implement the smallest fix that resolves the root cause without changing unrelated behavior, then re-run the regression test and relevant existing tests:

    Implement the minimal fix for the confirmed root cause.
    Run the new regression test and the existing tests for this area,
    and report what passed or failed.
    
  4. Re-run the original scenario under the debugger (F5) to confirm the runtime failure no longer reproduces.

5. Review the diff and clean up debug artifacts

Runtime debugging often leaves temporary artifacts behind. Check every modified file before committing:

  1. Open the Source Control view and inspect every changed and untracked file in the diff editor.

  2. Remove any temporary print statements, diagnostic logging, disabled checks, or unwanted edits to .vscode/launch.json, and remove temporary breakpoints or logpoints from the BREAKPOINTS section.

  3. Run your project's full relevant test suite yourself to verify the final clean state, then commit the fix and regression test.

Next steps