選項
首頁首頁 Skill 開發者工具 systematic-debugging

systematic-debugging

obra/superpowers obra/superpowers

在提出任何修正方案之前,請先找出錯誤、測試失敗或意外行為的根本原因。

...展開全部
21
更新時間 2026-09-03

系統化除錯

概述

隨機修復不僅浪費時間,還會產生新的錯誤。倉促的修補措施僅能掩蓋潛在問題。

核心原則:在嘗試修復之前,務必先找出根本原因。僅修復症狀是失敗的做法。

違反此流程的字面規定,即是違背了除錯的精神。

鐵律

未先調查根本原因,絕不進行修復

若尚未完成第一階段,則不得提出修正方案。

適用時機

適用於任何技術問題:

  • 測試失敗
  • 生產環境中的錯誤
  • 預期之外的行為
  • 效能問題
  • 建置失敗
  • 整合問題

特別是在以下情況下請務必使用此方法:

  • 面臨時間壓力時(緊急情況下容易讓人想靠猜測解決)
  • 「只要快速修補一下」似乎是顯而易見的解決方案
  • 您已經嘗試過多種解決方案
  • 先前嘗試的解決方案未見成效
  • 您尚未完全理解問題所在

請勿跳過以下情況:

  • 問題看似簡單時(即使是簡單的錯誤,也有其根本原因)
  • 您時間緊迫(倉促行事必然導致返工)
  • 主管要求「立刻」解決(系統性處理比胡亂試錯更快)

四個階段

您必須完成每個階段,才能進入下一階段。

第一階段:根本原因調查

在嘗試任何修復措施之前:

  1. 仔細閱讀錯誤訊息

    • 請勿跳過任何錯誤或警告訊息
    • 這些訊息通常包含確切的解決方案
    • 請完整閱讀堆疊追蹤記錄
    • 記錄行號、檔案路徑及錯誤代碼
  2. 確保能一致地重現錯誤

    • 你能可靠地觸發此問題嗎?
    • 具體步驟是什麼?
    • 每次都會發生嗎?
    • 若無法重現 → 請蒐集更多資料,切勿憑空推測
  3. 檢查最近的變更

    • 有哪些變更可能導致此問題?
    • Git diff、最近的提交
    • 新增的依賴項、設定變更
    • 環境差異
  4. 在多組件系統中蒐集證據

    當系統包含多個組件時(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"
    

    這顯示:哪一層級失敗(機密 → 工作流程 ✓,工作流程 → 建置 ✗)

  5. 追蹤資料流

    當錯誤深埋於呼叫堆疊中時:

    請參閱本目錄中的root-cause-tracing.md,了解完整的逆向追蹤技術。

    簡要說明:

    • 錯誤值源自何處?
    • 是哪個函式傳入錯誤值調用此函式的?
    • 持續向上追溯,直到找到來源
    • 在源頭解決問題,而非僅處理症狀

第二階段:模式分析

在修正之前先找出模式:

  1. 尋找可正常運作的範例

    • 在同一程式碼庫中找出類似且運作正常的程式碼
    • 哪些運作正常的程式碼與出錯的程式碼相似?
  2. 與參考實作進行比對

    • 若要實作該模式,請完整閱讀參考實作
    • 切勿草草瀏覽——請逐行細讀
    • 在應用之前,務必徹底理解該設計模式
  3. 找出差異

    • 正常運作與錯誤運作之間有何差異?
    • 列出所有差異,無論多麼微小
    • 切勿假設「那不重要」
  4. 理解依賴關係

    • 這需要哪些其他元件?
    • 需要哪些設定、配置和環境?
    • 它基於哪些假設?

第三階段:假設與測試

科學方法:

  1. 提出單一假說

    • 明確陳述:「我認為 X 是根本原因,因為 Y」
    • 將其寫下來
    • 要具體,不要含糊
  2. 進行最小限度測試

    • 為驗證假設,盡可能做出最微小的變更
    • 每次只變更一個變數
    • 不要一次修正多項內容
  3. 繼續之前先驗證

    • 有效嗎?是 → 第 4 階段
    • 沒成功嗎?提出新的假設
    • 切勿在現有解決方案上疊加更多修正
  4. 當你不知道時

    • 請說:「我不明白 X」
    • 不要假裝知道
    • 尋求協助
    • 進行更多研究

第四階段:執行

解決根本原因,而非症狀:

  1. 建立會失敗的測試案例

    • 盡可能簡單的重現步驟
    • 若可行,應採用自動化測試
    • 若無測試框架,則編寫單次使用的測試腳本
    • 在修復前必須具備
    • 運用「測試驅動開發」這項超能力,撰寫正確的失敗測試
  2. 實作單一修正

    • 解決已識別的根本原因
    • 每次只進行一項變更
    • 切勿進行「既然已經在做」式的改進
    • 不進行捆綁式重構
  3. 驗證修正結果

    • 測試現在通過了嗎?
    • 其他測試是否仍能正常執行?
    • 問題真的解決了嗎?
  4. 若修復無效

    • 停止
    • 計數:您嘗試過多少種修復方法?
    • 若 < 3:返回第 1 階段,根據新資訊重新分析
    • 若 ≥ 3:停止並檢視架構(參見下方第 5 步)
    • 在未進行架構討論前,請勿嘗試解決方案 #4
  5. 若 3 項以上修復方案均失敗:檢討架構

    顯示架構問題的模式:

    • 每次修正都會在不同處揭露新的共享狀態/耦合/問題
    • 實施修正方案需進行「大規模重構」
    • 每項修正都會在其他地方引發新的症狀

    請暫停並重新審視基礎:

    • 這種模式在根本上是否合理?
    • 我們是否只是「出於慣性而堅持下去」?
    • 我們應該重構架構,還是繼續修補症狀?

    在嘗試更多修復措施之前,請先與你的合作夥伴討論

    這並非「失敗的假設」——而是「錯誤的架構」。

警示訊號——立即停止並遵循流程

若你發現自己有以下想法:

  • 「先快速修補,之後再調查」
  • 「先試著修改 X,看看是否有效」
  • 「一次加入多項變更,然後執行測試」
  • 「跳過測試,我來手動驗證」
  • 「大概是 X 的問題,讓我來修一下」
  • 「我雖然不太理解,但這樣做或許可行」
  • 「模式雖說要這樣做,但我會另作調整」
  • 「主要問題如下:[未經調查便列出修正方案]」
  • 在追蹤資料流之前就提出解決方案
  • 「再試一次修復」(當已經嘗試過 2 次以上時)
  • 每次修正都會在不同處揭露新問題

以上所有情況都意味著:停止。回到第 1 階段。

若 3 次以上修復失敗:質疑架構(參見第 4.5 階段)

你的合作夥伴發出的「你做錯了」訊號

請留意以下這些引導性提問:

  • 「那不是沒發生嗎?」——你未經核實便擅自假設
  • 「這會顯示給我們看嗎⋯⋯?」——你本應補充蒐集證據的步驟
  • 「別再猜了」——你在未理解問題本質的情況下就提出解決方案
  • 「深入思考一下」——要質疑根本原因,而非僅止於表象
  • 「我們卡住了?」(沮喪)——你的方法行不通

當你看到這些時:停下來。回到第一階段。

常見的自我合理化

藉口 現實
「問題很簡單,不需要流程」 簡單的問題也有其根本原因。針對簡單的錯誤,流程反而能更快解決。
「緊急情況,沒時間走流程」 系統性的除錯比「猜測與驗證」的盲目嘗試來得更快。
「先試試這個,再調查」 首次修復會確立處理模式。從一開始就該做對。
「等確認修復有效後,我再寫測試」 未經測試的修復方案無法持久。先測試才能驗證其有效性。
「一次修復多個問題可以節省時間」 無法釐清哪些部分有效,反而會引發新的錯誤。
「參考資料太長,我會調整模式」 片面理解必定會導致錯誤。請完整閱讀。
「我看到問題了,讓我來修復」 看到症狀 ≠ 理解根本原因。
「再試一次修復」(在失敗兩次以上之後) 3 次以上失敗 = 架構問題。質疑設計模式,不要再修補了。

快速參考

階段 關鍵活動 成功標準
1. 根本原因 閱讀錯誤、重現問題、檢查變更、蒐集證據 釐清「是什麼」與「為什麼」
2. 模式 尋找正常運作的範例,進行比較 辨識差異
3. 假設 建立理論,進行最小限度測試 確認或提出新假設
4. 實作 建立測試、修正、驗證 錯誤已解決,測試通過

當流程揭示「無根本原因」時

若經系統性調查後發現問題確實屬於環境因素、時間依賴性或外部因素所致:

  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%
  • 引入新錯誤:接近零 對比 常見
在 GitHub 上查看
---
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

所有檔案

0 個檔案

安裝 systematic-debugging

請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/obra/superpowers/tree/main/skills/systematic-debugging # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/ Claude 會自動偵測並使用該技能
儲存庫 obra/superpowers

相關技能

algorithmic-art
更新時間 2026-08-27
receiving-code-review
更新時間 2026-09-03
tech-debt-tracker
更新時間 2026-08-29
senior-backend
更新時間 2026-08-30
OR