code-debugging

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Debugging

代码调试

Systematically debug experiment code with structured error categorization and fix strategies.
通过结构化的错误分类和修复策略,系统性地调试实验代码。

Input

输入

  • $0
    — Error message, stderr output, or code file with issues
  • $1
    — Optional: the code that produced the error
  • $0
    — 错误信息、stderr输出或存在问题的代码文件
  • $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:错误分类

CategoryExamplesSeverity
SyntaxErrorInvalid syntax, indentationLow
ImportErrorMissing module, wrong nameLow
RuntimeErrorDivision by zero, shape mismatchMedium
TimeoutErrorInfinite loop, too slowMedium
OutputErrorMissing files, wrong formatMedium
LogicErrorWrong results, 0% accuracyHigh
类别示例严重程度
语法错误无效语法、缩进错误
导入错误缺失模块、名称错误
运行时错误除零错误、形状不匹配
超时错误无限循环、运行过慢
输出错误文件缺失、格式错误
逻辑错误结果错误、准确率为0%

Step 2: Analyze Root Cause

步骤2:分析根本原因

  1. Read the error traceback (last 1500 chars if truncated)
  2. Identify the exact line and variable causing the error
  3. 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
  1. 阅读错误回溯信息(如果被截断则取最后1500个字符)
  2. 定位导致错误的具体代码行和变量
  3. 检查常见问题模式:
    • 设备不匹配(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:
  1. Explain why the error occurred
  2. Identify which lines caused it
  3. Describe the fix line-by-line
  4. Note patterns to avoid in future code
修复完成后:
  1. 解释错误发生的原因
  2. 定位导致错误的代码行
  3. 逐行描述修复内容
  4. 记录未来代码编写中需要避免的问题模式

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 failing
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 failing

Rules

规则

  • 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-codedata-analysis