Systematic Debugging
触发条件
通用领域触发矩阵
4阶段根因调试覆盖7大领域,21个子场景。
| 领域 | 场景 | 触发信号 | 示例 |
|---|---|---|---|
| AI/ML | 模型效果异常 | 用户有模型输出不符合预期 | "为什么这个prompt有时返回正确有时乱说" |
| AI/ML | 推理性能下降 | 用户有推理延迟/吞吐异常 | "为什么同样的模型今天慢了3倍" |
| AI/ML | Agent行为异常 | 用户有Agent执行路径不符合预期 | "Agent为什么有时候跳过这个tool调用" |
| Web/后端 | API异常 | 用户有接口返回不符合预期 | "为什么这个API间歇性500" |
| Web/后端 | 数据库慢查询 | 用户有查询性能问题 | "这个查询为什么偶尔超过10秒" |
| Web/后端 | 内存泄漏 | 用户有内存持续增长问题 | "为什么服务跑了两天内存就满了" |
| 前端 | 渲染异常 | 用户有UI渲染不符合预期 | "为什么这个组件有时候白屏" |
| 前端 | 状态异常 | 用户有状态管理问题 | "为什么用户登录后sidebar还是旧状态" |
| 前端 | 性能问题 | 用户有前端性能瓶颈 | "为什么这个页面首次加载要5秒" |
| 数据工程 | 数据不一致 | 用户有数据质量/一致性问题 | "为什么昨天的ETL跑出来的数对不上" |
| 数据工程 | 管道中断 | 用户有数据处理流水线失败 | "为什么这个spark job随机OOM" |
| 数据工程 | 延迟异常 | 用户有数据延迟问题 | "为什么实时数据延迟从3秒变成了3分钟" |
| 基础设施 | 部署失败 | 用户有CI/CD失败 | "为什么这个deploy之前还能跑现在报错" |
| 基础设施 | 网络异常 | 用户有网络连接问题 | "为什么A服务调B服务间歇超时" |
| 基础设施 | 资源异常 | 用户有CPU/内存/磁盘异常 | "为什么k8s node突然pressure" |
| 安全 | 权限异常 | 用户有auth/authz不符合预期 | "为什么这个用户能访问他无权访问的资源" |
| 安全 | 日志异常 | 用户有审计日志问题 | "为什么审计日志漏了这几条操作" |
| 安全 | 证书问题 | 用户有TLS/证书异常 | "为什么这个证书在Chrome正常Firefox报错" |
| 移动 | 崩溃问题 | 用户有App崩溃 | "为什么这个crash只在iOS15上出现" |
| 移动 | 推送问题 | 用户有推送送达异常 | "为什么推送在Android收到iOS收不到" |
| 移动 | 电量异常 | 用户有电池消耗问题 | "为什么后台定位耗电突然翻倍" |
手动触发
- "debug this"
- "为什么会出现这个bug"
- "排查一下"
- "这是什么原因"
- "找根因"
- "systematic debug"
- "root cause"
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)
- Someone 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
Action: Use read_file on the relevant source files. Use search_files to find the error string in the codebase.
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
Action: Use the terminal tool to run the failing test or trigger the bug:
# Run specific failing test
pytest tests/test_module.py::test_name -v
# Run with verbose output
pytest tests/test_module.py -v --tb=long
3. Check Recent Changes
- What changed that could cause this?
- Git diff, recent commits
- New dependencies, config changes
Action:
# Recent commits
git log --oneline -10
# Uncommitted changes
git diff
# Changes in specific file
git log -p --follow src/problematic_file.py | head -100
4. Gather Evidence in Multi-Component Systems
WHEN system has multiple components (API → service → database, CI → build → deploy):
BEFORE proposing fixes, add diagnostic instrumentation:
For EACH component boundary:
- Log what data enters the component
- Log what data exits the component
- Verify environment/config propagation
- Check state at each layer
Run once to gather evidence showing WHERE it breaks. THEN analyze evidence to identify the failing component. THEN investigate that specific component.
5. Trace Data Flow
WHEN error is deep in the call stack:
- Where does the bad value originate?
- What called this function with the bad value?
- Keep tracing upstream until you find the source
- Fix at the source, not at the symptom
Action: Use search_files to trace references:
# Find where the function is called
search_files("function_name(", path="src/", file_glob="*.py")
# Find where the variable is set
search_files("variable_name\\s*=", path="src/", file_glob="*.py")
Phase 1 Completion Checklist
- Error messages fully read and understood
- Issue reproduced consistently
- Recent changes identified and reviewed
- Evidence gathered (logs, state, data flow)
- Problem isolated to specific component/code
- Root cause hypothesis formed
STOP: Do not proceed to Phase 2 until you understand WHY it's happening.
Phase 2: Pattern Analysis
Find the pattern before fixing:
1. Find Working Examples
- Locate similar working code in the same codebase
- What works that's similar to what's broken?
Action: Use search_files to find comparable patterns:
search_files("similar_pattern", path="src/", file_glob="*.py")
2. Compare Against References
- If implementing a pattern, read the 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 a 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 the hypothesis
- One variable at a time
- Don't fix multiple things at once
3. Verify Before Continuing
- Did it work? → 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 the user 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
- MUST have before fixing
- Use the
test-driven-developmentskill
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
# Run the specific regression test
pytest tests/test_module.py::test_regression -v
# Run full suite — no regressions
pytest tests/ -q
4. If Fix Doesn't Work — The Rule of Three
- 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 an architectural problem:
- Each fix reveals new shared state/coupling in a 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 the architecture vs. continue fixing symptoms?
Discuss with the user before attempting more fixes.
This is NOT a failed hypothesis — this is a wrong architecture.
Reference Files
references/bootstrap-modal-debugging.md— Bootstrap 5 Modal Manager lock, DOM-manipulation workaround, browser_click vs .click() discrepancy patterns. Load when debugging multi-modal UI flows.
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
- Browser-testing loop — repeatedly calling
browser_clickon the same button, checking the result, failing, and trying again with a slightly different approach, without ever verifying the code EXISTS. Before the third click attempt, STOP and rungrep -n "functionName" file.htmlor checktypeof functionNameinbrowser_console. If the function literally does not exist in the file (e.g., overwritten by a sibling subagent), no amount of clicking will make it work. This is the single most wasteful debugging pattern in browser-based work. - "One more fix attempt" (when already tried 2+)
- Each fix reveals a new problem in a different place
ALL of these mean: STOP. Return to Phase 1.
If 3+ fixes failed: Question the architecture (Phase 4 step 5).
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 the pattern, don't fix again. |
Quick Reference
| Phase | Key Activities | Success Criteria |
|---|---|---|
| 1. Root Cause | Read errors, reproduce, check changes, gather evidence, trace data flow | Understand WHAT and WHY |
| 2. Pattern | Find working examples, compare, identify differences | Know what's different |
| 3. Hypothesis | Form theory, test minimally, one variable at a time | Confirmed or new hypothesis |
| 4. Implementation | Create regression test, fix root cause, verify | Bug resolved, all tests pass |
Hermes Agent Integration
Investigation Tools
Use these Hermes tools during Phase 1:
search_files— Find error strings, trace function calls, locate patternsread_file— Read source code with line numbers for precise analysisterminal— Run tests, check git history, reproduce bugsweb_search/web_extract— Research error messages, library docs
With delegate_task
For complex multi-component debugging, dispatch investigation subagents:
delegate_task(
goal="Investigate why [specific test/behavior] fails",
context="""
Follow systematic-debugging skill:
1. Read the error message carefully
2. Reproduce the issue
3. Trace the data flow to find root cause
4. Report findings — do NOT fix yet
Error: [paste full error]
File: [path to failing code]
Test command: [exact command]
""",
toolsets=['terminal', 'file']
)
With test-driven-development
When fixing bugs:
- Write a test that reproduces the bug (RED)
- Debug systematically to find root cause
- Fix the root cause (GREEN)
- The test proves the fix and prevents regression
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
No shortcuts. No guessing. Systematic always wins.