| name | python-debugging |
| description | Toolkit for AI agents to debug Python code autonomously without IDE support. Use when agent needs to debug code, insert breakpoints programmatically, analyze execution state, test hypotheses, or automate debugging workflows. Supports pdb, ipdb, debugpy with programmatic breakpoint insertion, conditional breakpoints, AST-based breakpoint suggestions, and automated debugging workflows for agents. |
| metadata | {"short-description":"Agent autonomous Python debugging - programmatic breakpoints, execution analysis"} |
Python Debugging Skill
Toolkit for AI agents to debug Python code autonomously without IDE dependencies. Enables agents to programmatically insert breakpoints, analyze execution state, test hypotheses, and automate debugging workflows.
Agent Debugging Workflow
When debugging code, agents should:
- Analyze code structure - Use AST to identify key locations
- Insert breakpoints programmatically - Add breakpoints to inspect state
- Execute and inspect - Run code, check variables at breakpoints
- Test hypotheses - Evaluate expressions, modify variables
- Clean up - Remove breakpoints after debugging
Quick Debugging Workflow
python scripts/smart_breakpoint_suggester.py script.py --only functions --max 3
python scripts/insert_breakpoint.py script.py --line 10 --method pdb
python script.py > debug_output.txt 2>&1
python scripts/insert_breakpoint.py script.py --remove-all
Core Workflows
1. Debugging New Code
Workflow:
- Get smart suggestions for breakpoint locations
- Insert breakpoints at suggested locations
- Run debugging session
- Clean up breakpoints
Commands:
python scripts/smart_breakpoint_suggester.py script.py --only functions --max 3
python scripts/insert_breakpoint.py script.py --line 10 --method pdb
python scripts/insert_breakpoint.py script.py --line 25 --method pdb
python script.py
python scripts/insert_breakpoint.py script.py --remove-all
2. Hypothesis Testing
Agent tests specific conditions or hypotheses using conditional breakpoints.
Agent Workflow:
- Form hypothesis (e.g., "error occurs when x > 100")
- Insert conditional breakpoint with hypothesis condition
- Run code - only pauses when condition is true
- Inspect state when condition is met
- Validate or refute hypothesis
Example:
python scripts/conditional_breakpoint.py script.py --line 15 --condition "count > 100"
python script.py
3. Automated Code Analysis
Agent uses AST-based analysis to automatically identify debugging points.
Agent Workflow:
- Analyze code structure using AST
- Identify key locations (function entries, loops, conditionals)
- Insert breakpoints at strategic locations
- Run code and collect state information
- Analyze collected data to find issues
Example:
python scripts/smart_breakpoint_suggester.py script.py --only functions loops --max 5
python scripts/auto_debug_assistant.py script.py --focus functions --max-breakpoints 3
Available Tools
scripts/insert_breakpoint.py
Insert, remove, and list breakpoints in Python code.
Usage:
python scripts/insert_breakpoint.py script.py --line 10 --method pdb
python scripts/insert_breakpoint.py script.py --line 10 --remove
python scripts/insert_breakpoint.py script.py --list
python scripts/insert_breakpoint.py script.py --remove-all
Supported debuggers: pdb, ipdb, debugpy
scripts/conditional_breakpoint.py
Insert conditional breakpoints that only pause when conditions are met.
Usage:
python scripts/conditional_breakpoint.py script.py --line 10 --condition "x > 100"
python scripts/conditional_breakpoint.py script.py --list
Examples:
"x > 100" - Only pause when x exceeds 100
"i == 10" - Only pause on 10th iteration
"x > 100 and y < 50" - Complex conditions
scripts/smart_breakpoint_suggester.py
AI analyzes code structure and suggests optimal breakpoint locations.
Usage:
python scripts/smart_breakpoint_suggester.py script.py
python scripts/smart_breakpoint_suggester.py script.py --only functions
python scripts/smart_breakpoint_suggester.py script.py --only functions loops --max 5
Suggestion types:
functions - Function entry points
loops - Loop start positions
conditionals - If/elif/else branches
exceptions - Try/except blocks
returns - Return statements
assignments - Key variable assignments
scripts/auto_debug_assistant.py
Automated debugging assistant that completes entire debugging workflow.
Usage:
python scripts/auto_debug_assistant.py script.py
python scripts/auto_debug_assistant.py script.py --focus functions
python scripts/auto_debug_assistant.py script.py --quick 10
python scripts/auto_debug_assistant.py script.py --max-breakpoints 5
Debugger Commands
Once in debugger (pdb/ipdb), use these commands:
Execution Control
n (next) - Execute next line
s (step) - Step into function
c (continue) - Continue to next breakpoint
q (quit) - Quit debugger
Inspection
p <variable> - Print variable value
pp <variable> - Pretty print variable
l (list) - Show current code
w (where) - Show call stack
u (up) - Move up call stack
d (down) - Move down call stack
Expressions
- Direct variable access - Type variable name to see value
- Python expressions - Execute any Python code
- Modify variables -
x = 100 to change values
- Import modules -
import os; os.getcwd()
Agent Debugging Patterns
Pattern 1: Error Location Debugging
When: Agent encounters error traceback
Agent Steps:
- Identify error line from traceback
- Insert breakpoint before error location
- Run code and inspect variables at breakpoint
- Determine root cause
- Fix code and remove breakpoint
python scripts/insert_breakpoint.py script.py --line 24 --method pdb
python script.py
Pattern 2: Hypothesis Testing
When: Agent suspects specific condition causes issue
Agent Steps:
- Form hypothesis (e.g., "error when count > 100")
- Insert conditional breakpoint with hypothesis
- Run code - only pauses when hypothesis is true
- Inspect state to validate/refute hypothesis
python scripts/conditional_breakpoint.py script.py --line 15 --condition "len(items) > 100"
python script.py
Pattern 3: Systematic Code Analysis
When: Agent needs to understand code flow or find issues
Agent Steps:
- Analyze code structure with AST
- Get suggestions for key locations
- Insert breakpoints at strategic points
- Run and collect state information
- Analyze collected data
python scripts/auto_debug_assistant.py script.py --focus functions --max-breakpoints 5
Pattern 4: Auto Debug Workflow
python scripts/auto_debug_assistant.py script.py
Debugger Selection for Agents
Agents should select debugger based on availability:
- Try
ipdb first - Better experience if available
- Fallback to
pdb - Always available (built-in)
- Use
debugpy - Only if remote debugging needed
Agent Logic:
try:
import ipdb
debugger_method = 'ipdb'
except ImportError:
debugger_method = 'pdb'
Recommendation: Always default to pdb for maximum compatibility.
References
See references/debugging-guide.md for:
- Detailed debugging workflows
- Advanced debugging patterns
- Performance debugging
- Debugging session recording
See references/debugger-commands.md for:
- Complete command reference
- Basic expression examples
- Debugger API details
See references/expression-guide.md for:
- Comprehensive expression usage guide for agents
- Hypothesis testing with expressions
- Agent debugging scenarios
- Advanced expression patterns
Important Notes for Agents
- Breakpoints modify code: Inserted as code statements, so line numbers shift after insertion
- Always clean up: Remove all breakpoints after debugging to restore original code
- Conditional expressions: Must be valid Python expressions that evaluate to boolean
- AST-based analysis: Smart suggestions work on any valid Python code structure
- Debugger availability: Auto assistant detects ipdb, falls back to pdb
- Non-interactive use: For automated workflows, capture debugger output via subprocess
- Error handling: Check if breakpoint insertion succeeded before running code