systematic-debugging
obra/superpowers
在提出任何修复方案之前,先找出缺陷、测试失败或意外行为的根本原因。
...展开全部系统化调试
概述
随机修复既浪费时间,又会产生新的缺陷。临时补丁只能掩盖根本问题。
核心原则:在尝试修复之前,务必先找出根本原因。仅修复症状是行不通的。
违反此流程的字面规定,就是违背了调试的精神。
铁律
未先调查根本原因,不得进行任何修复
若未完成第一阶段,则不得提出修复方案。
适用场景
适用于任何技术问题:
- 测试失败
- 生产环境中的缺陷
- 意外行为
- 性能问题
- 构建失败
- 集成问题
特别是在以下情况下请使用此功能:
- 时间紧迫时(紧急情况容易让人想靠猜测解决)
- “只是一个快速修复”似乎显而易见
- 你已经尝试过多种修复方案
- 之前的修复方法未奏效
- 您尚未完全理解问题所在
在以下情况下切勿跳过:
- 问题看似简单(简单的 bug 也有其根本原因)
- 您时间紧迫(仓促行事必然导致返工)
- 经理要求“立刻”修复(系统性解决比盲目折腾更快)
四个阶段
你必须完成每个阶段后才能进入下一阶段。
第一阶段:根本原因调查
在尝试任何修复措施之前:
仔细阅读错误信息
- 不要跳过错误或警告信息
- 它们通常包含确切的解决方案
- 请完整阅读堆栈跟踪信息
- 记录行号、文件路径和错误代码
始终如一地重现问题
- 你能可靠地触发该问题吗?
- 具体步骤是什么?
- 每次都会发生吗?
- 如果无法重现 → 收集更多数据,不要凭空猜测
检查最近的更改
- 有哪些可能导致此问题的变更?
- Git diff、最近的提交
- 新增依赖项、配置变更
- 环境差异
在多组件系统中收集证据
当系统包含多个组件时(CI → 构建 → 签名,API → 服务 → 数据库):
在提出修复方案之前,先添加诊断工具:
针对每个组件边界: - 记录进入组件的数据 - 记录离开组件的数据 - 验证环境/配置的传递 - 检查各层的状态 运行一次以收集证据,显示故障发生的位置 然后分析证据以识别故障组件 接着调查该特定组件示例(多层系统):
# 第 1 层:工作流 echo "=== 工作流中可用的密钥: ===" echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}" # 第 2 层:构建脚本 echo "=== 构建脚本中的环境变量: ===" env | grep IDENTITY || echo "环境中不存在 IDENTITY" # 第 3 层:签名脚本 echo "=== 密钥链状态: ===" security list-keychains security find-identity -v # 第 4 层:实际签名 codesign --sign "$IDENTITY" --verbose=4 "$APP"这揭示了:哪个层级失败(密钥 → 工作流 ✓,工作流 → 构建 ✗)
追踪数据流
当错误深埋在调用栈中时:
请参阅本目录中的
root-cause-tracing.md,了解完整的逆向追踪技术。简要版本:
- 错误值源自何处?
- 是哪个函数带着错误值调用了此函数?
- 不断向上追溯,直到找到源头
- 在源头修复,而非治标
第二阶段:模式分析
在修复之前找出规律:
查找可正常运行的示例
- 在同一代码库中查找类似的正常运行代码
- 有哪些与故障代码相似且能正常运行的代码?
与参考实现进行对比
- 如果要实现该模式,请完整阅读参考实现
- 不要走马观花——逐行阅读
- 在应用之前要完全理解该设计模式
找出差异
- 正常运行的实现与出错的实现有何不同?
- 列出所有差异,无论多么细微
- 不要认为“那不重要”
理解依赖关系
- 这还需要哪些其他组件?
- 需要哪些设置、配置和环境?
- 它基于哪些假设?
第三阶段:假设与测试
科学方法:
提出单一假设
- 明确表述:“我认为X是根本原因,因为Y”
- 将其写下来
- 表述要具体,不要含糊
进行最小限度测试
- 对假设进行尽可能微小的改变以进行验证
- 每次只改变一个变量
- 不要同时修改多项内容
继续之前先验证
- 有效吗?是 → 第4阶段
- 没成功?提出新假设
- 切勿在此基础上叠加更多修复方案
当你不知道时
- 说“我不明白X”
- 不要装作懂
- 寻求帮助
- 进一步研究
第四阶段:实施
解决根本原因,而非症状:
创建会失败的测试用例
- 尽可能简单的重现步骤
- 如有可能,采用自动化测试
- 若无测试框架,则编写一次性测试脚本
- 在修复之前必须具备
- 运用
“测试驱动开发”这一超能力来编写正确的失败测试
实施单一修复
- 解决已查明的根本原因
- 每次只进行一项更改
- 不要进行“既然已经在这里了”式的改进
- 不进行捆绑式重构
验证修复结果
- 测试现在通过了吗?
- 其他测试是否仍能通过?
- 问题真的解决了?
如果修复无效
- 停止
- 计数:你尝试了多少种修复方法?
- 如果 < 3:返回第 1 阶段,根据新信息重新分析
- 如果 ≥ 3:停止,并质疑架构(见下文第 5 步)
- 在未进行架构讨论前,切勿尝试修复方案 #4
若3种及以上修复方案均失败:质疑架构
表明存在架构问题的模式:
- 每次修复都会在不同位置揭示新的共享状态/耦合/问题
- 实施修复方案需要进行“大规模重构”
- 每次修复都会在其他地方引发新的症状
请停下来,重新审视基础:
- 这种模式在根本上是否合理?
- 我们是否只是“出于惯性而固守”?
- 我们应该重构架构,还是继续修补症状?
在尝试更多修复之前,先与你的同事讨论
这并非一个失败的假设——而是架构本身有问题。
危险信号——立即停止并遵循流程
如果你发现自己在想:
- “先快速修复,以后再调查”
- “先试着改改X,看看能不能行”
- “一次性添加多项修改,然后运行测试”
- “跳过测试,我手动验证一下”
- “大概是X的问题,我来修一下”
- “我虽然没完全搞懂,但这方法可能行得通”
- “模式规定是X,但我会按其他方式调整”
- “主要问题如下:[未进行调查就列出修复方案]”
- 在追踪数据流之前就提出解决方案
- “再试一次修复”(当已经尝试过2次以上时)
- 每次修复都会在不同位置暴露新问题
以上所有情况均意味着:停止。返回第1阶段。
如果3次及以上修复均失败:质疑架构(参见第4.5阶段)
你的合作伙伴发出的“你做错了”信号
注意以下这些话语转向:
- “那不是没发生吗?”——你未经核实就做了假设
- “这会显示出来吗……?”——你本应补充收集证据的步骤
- “别再猜了”——你在不理解问题的情况下就提出解决方案
- “深入思考一下”——要质疑根本原因,而非仅仅关注表象
- “我们卡住了?”(沮丧)——你的方法行不通
当你看到这些情况时:停下来。回到第一阶段。
常见的自我辩解
| 借口 | 现实 |
|---|---|
| “问题很简单,不需要流程” | 简单的问题也有其根本原因。对于简单的错误,遵循流程反而能更快解决。 |
| “紧急情况,没时间走流程” | 系统化的调试比盲目试错要快得多。 |
| “先试这个,再调查” | 首次修复就奠定了基调。从一开始就要做对。 |
| “等确认修复有效后再写测试用例” | 未经测试的修复无法持久。先测试才能验证其有效性。 |
| “一次修复多个问题可以节省时间” | 无法确定是哪部分起作用了,还会引发新的 bug。 |
| “引用太长,我来调整一下模式” | 片面理解必然导致错误。请通读全文。 |
| “我发现了问题,让我来修复它” | 看到症状 ≠ 理解根本原因。 |
| “再试一次修复”(在失败2次以上后) | 3次以上失败 = 架构问题。质疑设计模式,不要再修补了。 |
快速参考
| 阶段 | 关键活动 | 成功标准 |
|---|---|---|
| 1. 根本原因 | 分析错误、复现、检查变更、收集证据 | 弄清“是什么”和“为什么” |
| 2. 模式 | 查找正常运行的示例,进行对比 | 识别差异 |
| 3. 假设 | 构建理论,进行最小验证 | 验证或提出新假设 |
| 4. 实现 | 编写测试、修复、验证 | 缺陷已修复,测试通过 |
当流程显示“无根本原因”时
如果系统性调查表明问题确实是环境因素、时间依赖性或外部因素造成的:
- 则该流程已完成
- 记录调查内容
- 实施适当的处理措施(重试、超时、错误信息)
- 为今后的调查添加监控/日志记录
但是:95%的“找不到根本原因”案例,其实是调查不彻底所致。
辅助技术
这些技术是系统化调试的一部分,可在以下目录中找到:
root-cause-tracing.md- 通过调用堆栈向后追踪错误,以找出最初的触发点defense-in-depth.md- 找到根本原因后,在多层级添加验证机制condition-based-waiting.md- 用条件轮询取代任意的超时机制
相关技能:
- superpowers:test-driven-development- 用于创建失败的测试用例(第 4 阶段,第 1 步)
- superpowers:verification-before-completion- 在宣布修复成功前先验证修复是否有效
实际影响
来自调试会:
- 系统化方法:15-30分钟修复
- 随机修复方法:2-3小时的盲目摸索
- 首次修复率:95% 对比 40%
- 引入新缺陷:接近零 vs 常见
---
name: systematic-debugging
description: Find root causes of bugs, test failures, or unexpected behavior before proposing any fixes.
---
# Systematic Debugging
## Overview
Random fixes waste time and create new bugs. Quick patches mask underlying issues.
**Core principle:** ALWAYS find root cause before attempting fixes. Symptom fixes are failure.
**Violating the letter of this process is violating the spirit of debugging.**
## The Iron Law
```
NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
```
If you haven't completed Phase 1, you cannot propose fixes.
## When to Use
Use for ANY technical issue:
- Test failures
- Bugs in production
- Unexpected behavior
- Performance problems
- Build failures
- Integration issues
**Use this ESPECIALLY when:**
- Under time pressure (emergencies make guessing tempting)
- "Just one quick fix" seems obvious
- You've already tried multiple fixes
- Previous fix didn't work
- You don't fully understand the issue
**Don't skip when:**
- Issue seems simple (simple bugs have root causes too)
- You're in a hurry (rushing guarantees rework)
- Manager wants it fixed NOW (systematic is faster than thrashing)
## The Four Phases
You MUST complete each phase before proceeding to the next.
### Phase 1: Root Cause Investigation
**BEFORE attempting ANY fix:**
1. **Read Error Messages Carefully**
- Don't skip past errors or warnings
- They often contain the exact solution
- Read stack traces completely
- Note line numbers, file paths, error codes
2. **Reproduce Consistently**
- Can you trigger it reliably?
- What are the exact steps?
- Does it happen every time?
- If not reproducible → gather more data, don't guess
3. **Check Recent Changes**
- What changed that could cause this?
- Git diff, recent commits
- New dependencies, config changes
- Environmental differences
4. **Gather Evidence in Multi-Component Systems**
**WHEN system has multiple components (CI → build → signing, API → service → database):**
**BEFORE proposing fixes, add diagnostic instrumentation:**
```
For EACH component boundary:
- Log what data enters component
- Log what data exits component
- Verify environment/config propagation
- Check state at each layer
Run once to gather evidence showing WHERE it breaks
THEN analyze evidence to identify failing component
THEN investigate that specific component
```
**Example (multi-layer system):**
```bash
# Layer 1: Workflow
echo "=== Secrets available in workflow: ==="
echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"
# Layer 2: Build script
echo "=== Env vars in build script: ==="
env | grep IDENTITY || echo "IDENTITY not in environment"
# Layer 3: Signing script
echo "=== Keychain state: ==="
security list-keychains
security find-identity -v
# Layer 4: Actual signing
codesign --sign "$IDENTITY" --verbose=4 "$APP"
```
**This reveals:** Which layer fails (secrets → workflow ✓, workflow → build ✗)
5. **Trace Data Flow**
**WHEN error is deep in call stack:**
See `root-cause-tracing.md` in this directory for the complete backward tracing technique.
**Quick version:**
- Where does bad value originate?
- What called this with bad value?
- Keep tracing up until you find the source
- Fix at source, not at symptom
### Phase 2: Pattern Analysis
**Find the pattern before fixing:**
1. **Find Working Examples**
- Locate similar working code in same codebase
- What works that's similar to what's broken?
2. **Compare Against References**
- If implementing pattern, read reference implementation COMPLETELY
- Don't skim - read every line
- Understand the pattern fully before applying
3. **Identify Differences**
- What's different between working and broken?
- List every difference, however small
- Don't assume "that can't matter"
4. **Understand Dependencies**
- What other components does this need?
- What settings, config, environment?
- What assumptions does it make?
### Phase 3: Hypothesis and Testing
**Scientific method:**
1. **Form Single Hypothesis**
- State clearly: "I think X is the root cause because Y"
- Write it down
- Be specific, not vague
2. **Test Minimally**
- Make the SMALLEST possible change to test hypothesis
- One variable at a time
- Don't fix multiple things at once
3. **Verify Before Continuing**
- Did it work? Yes → Phase 4
- Didn't work? Form NEW hypothesis
- DON'T add more fixes on top
4. **When You Don't Know**
- Say "I don't understand X"
- Don't pretend to know
- Ask for help
- Research more
### Phase 4: Implementation
**Fix the root cause, not the symptom:**
1. **Create Failing Test Case**
- Simplest possible reproduction
- Automated test if possible
- One-off test script if no framework
- MUST have before fixing
- Use the `superpowers:test-driven-development` skill for writing proper failing tests
2. **Implement Single Fix**
- Address the root cause identified
- ONE change at a time
- No "while I'm here" improvements
- No bundled refactoring
3. **Verify Fix**
- Test passes now?
- No other tests broken?
- Issue actually resolved?
4. **If Fix Doesn't Work**
- STOP
- Count: How many fixes have you tried?
- If < 3: Return to Phase 1, re-analyze with new information
- **If ≥ 3: STOP and question the architecture (step 5 below)**
- DON'T attempt Fix #4 without architectural discussion
5. **If 3+ Fixes Failed: Question Architecture**
**Pattern indicating architectural problem:**
- Each fix reveals new shared state/coupling/problem in different place
- Fixes require "massive refactoring" to implement
- Each fix creates new symptoms elsewhere
**STOP and question fundamentals:**
- Is this pattern fundamentally sound?
- Are we "sticking with it through sheer inertia"?
- Should we refactor architecture vs. continue fixing symptoms?
**Discuss with your human partner before attempting more fixes**
This is NOT a failed hypothesis - this is a wrong architecture.
## Red Flags - STOP and Follow Process
If you catch yourself thinking:
- "Quick fix for now, investigate later"
- "Just try changing X and see if it works"
- "Add multiple changes, run tests"
- "Skip the test, I'll manually verify"
- "It's probably X, let me fix that"
- "I don't fully understand but this might work"
- "Pattern says X but I'll adapt it differently"
- "Here are the main problems: [lists fixes without investigation]"
- Proposing solutions before tracing data flow
- **"One more fix attempt" (when already tried 2+)**
- **Each fix reveals new problem in different place**
**ALL of these mean: STOP. Return to Phase 1.**
**If 3+ fixes failed:** Question the architecture (see Phase 4.5)
## your human partner's Signals You're Doing It Wrong
**Watch for these redirections:**
- "Is that not happening?" - You assumed without verifying
- "Will it show us...?" - You should have added evidence gathering
- "Stop guessing" - You're proposing fixes without understanding
- "Ultra-think this" - Question fundamentals, not just symptoms
- "We're stuck?" (frustrated) - Your approach isn't working
**When you see these:** STOP. Return to Phase 1.
## Common Rationalizations
| Excuse | Reality |
|--------|---------|
| "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |
| "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |
| "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |
| "I'll write test after confirming fix works" | Untested fixes don't stick. Test first proves it. |
| "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |
| "Reference too long, I'll adapt the pattern" | Partial understanding guarantees bugs. Read it completely. |
| "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. |
| "One more fix attempt" (after 2+ failures) | 3+ failures = architectural problem. Question pattern, don't fix again. |
## Quick Reference
| Phase | Key Activities | Success Criteria |
|-------|---------------|------------------|
| **1. Root Cause** | Read errors, reproduce, check changes, gather evidence | Understand WHAT and WHY |
| **2. Pattern** | Find working examples, compare | Identify differences |
| **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |
| **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |
## When Process Reveals "No Root Cause"
If systematic investigation reveals issue is truly environmental, timing-dependent, or external:
1. You've completed the process
2. Document what you investigated
3. Implement appropriate handling (retry, timeout, error message)
4. Add monitoring/logging for future investigation
**But:** 95% of "no root cause" cases are incomplete investigation.
## Supporting Techniques
These techniques are part of systematic debugging and available in this directory:
- **`root-cause-tracing.md`** - Trace bugs backward through call stack to find original trigger
- **`defense-in-depth.md`** - Add validation at multiple layers after finding root cause
- **`condition-based-waiting.md`** - Replace arbitrary timeouts with condition polling
**Related skills:**
- **superpowers:test-driven-development** - For creating failing test case (Phase 4, Step 1)
- **superpowers:verification-before-completion** - Verify fix worked before claiming success
## Real-World Impact
From debugging sessions:
- Systematic approach: 15-30 minutes to fix
- Random fixes approach: 2-3 hours of thrashing
- First-time fix rate: 95% vs 40%
- New bugs introduced: Near zero vs common





首页
