code-debugging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Debugging
代码调试
Systematically debug experiment code with structured error categorization and fix strategies.
通过结构化错误分类和修复策略,系统性地调试实验代码。
Input
输入
- — Error message, stderr output, or code file with issues
$0 - — Optional: the code that produced the error
$1
- — 错误信息、stderr输出或存在问题的代码文件
$0 - — 可选:产生错误的对应代码
$1
References
参考资料
- Debug patterns and state machine:
~/.claude/skills/code-debugging/references/debug-patterns.md
- 调试模式与状态机:
~/.claude/skills/code-debugging/references/debug-patterns.md
Workflow
工作流程
Step 1: Categorize the Error
步骤1:错误分类
| Category | Examples | Severity |
|---|---|---|
| SyntaxError | Invalid syntax, indentation | Low |
| ImportError | Missing module, wrong name | Low |
| RuntimeError | Division by zero, shape mismatch | Medium |
| TimeoutError | Infinite loop, too slow | Medium |
| OutputError | Missing files, wrong format | Medium |
| LogicError | Wrong results, 0% accuracy | High |
| 分类 | 示例 | 严重程度 |
|---|---|---|
| SyntaxError | 语法无效、缩进错误 | 低 |
| ImportError | 缺失模块、名称错误 | 低 |
| RuntimeError | 除零错误、形状不匹配 | 中 |
| TimeoutError | 死循环、运行过慢 | 中 |
| OutputError | 文件缺失、格式错误 | 中 |
| LogicError | 结果错误、准确率0% | 高 |
Step 2: Analyze Root Cause
步骤2:分析根本原因
- Read the error traceback (last 1500 chars if truncated)
- Identify the exact line and variable causing the error
- Check for common patterns:
- Device mismatch (CPU vs GPU tensors)
- Shape mismatch in matrix operations
- Missing data normalization
- Off-by-one errors in indexing
- Incorrect loss function for task type
- 读取错误回溯信息(若被截断则取最后1500个字符)
- 定位导致错误的精确代码行和变量
- 检查常见错误模式:
- 设备不匹配(CPU 与 GPU 张量)
- 矩阵运算中的形状不匹配
- 缺失数据归一化
- 索引差一错误
- 任务类型对应的损失函数使用错误
Step 3: Apply Fix Strategy
步骤3:应用修复策略
For syntax/import errors: Direct fix, single attempt
For runtime errors: Fix and rerun, up to 4 retries
For logic errors: Reflect on approach, consider alternative methods
For timeout: Reduce dataset size, optimize bottleneck, add early stopping
语法/导入错误:直接修复,单次尝试
运行时错误:修复后重新运行,最多重试4次
逻辑错误:复盘实现思路,考虑替代方案
超时问题:减小数据集规模、优化性能瓶颈、添加早停机制
Step 4: Reflect and Prevent
步骤4:复盘与预防
After fixing:
- Explain why the error occurred
- Identify which lines caused it
- Describe the fix line-by-line
- Note patterns to avoid in future code
修复完成后:
- 解释错误发生的原因
- 定位导致错误的代码行
- 逐行说明修复内容
- 记录后续代码需要规避的模式
Fix Strategy State Machine
修复策略状态机
Stage 0 (first attempt) → repost code as fresh
Stage 1 (second attempt) → repost or leave depending on severity
Stage 2 (third attempt) → regenerate from scratch if still failingStage 0 (first attempt) → repost code as fresh
Stage 1 (second attempt) → repost or leave depending on severity
Stage 2 (third attempt) → regenerate from scratch if still failingRules
规则
- Prefer minimal targeted edits over full rewrites
- Maximum 4-5 fix attempts before changing approach
- Always truncate long error outputs to last 1500 characters
- After fixing, verify the fix doesn't introduce new errors
- Keep error history to avoid repeating the same mistakes
- If 0% accuracy: check accuracy calculation first, then check data pipeline
- 优先采用最小范围的针对性修改,而非全量重写
- 调整方案前最多进行4-5次修复尝试
- 长错误输出始终截断至最后1500个字符
- 修复完成后验证不会引入新错误
- 留存错误历史,避免重复犯相同错误
- 若出现准确率0%的情况:先检查准确率计算逻辑,再检查数据管道
Related Skills
相关技能
- Upstream: experiment-code
- See also: paper-to-code, data-analysis
- 上游:experiment-code
- 另见:paper-to-code、data-analysis