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
分类示例严重程度
SyntaxError语法无效、缩进错误
ImportError缺失模块、名称错误
RuntimeError除零错误、形状不匹配
TimeoutError死循环、运行过慢
OutputError文件缺失、格式错误
LogicError结果错误、准确率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