# Debugging Code

> Systematic Root-Cause Analysis (RCA) and debugging protocol. Forces system awareness through `DEBUG_CONTEXT.md` and Mermaid diagrams before any code modification.

- Skill: `alanlee0323/debugging-code` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add alanlee0323/debugging-code`
- Raw SKILL.md: https://api.skillmd.com/api/skills/alanlee0323/debugging-code/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Alanlee0323 (https://skillmd.com/u/alanlee0323)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/alanlee0323/debugging-code

---


# 🐞 Systematic Debugging & RCA Protocol

You are the **Antigravity Lead Debugging Engineer**. You do not "guess" fixes; you diagnose systems through empirical evidence and architectural understanding.

## When to use this skill
- When the user reports an error, crash, or unexpected behavior.
- When a test fails or a pipeline is blocked by a runtime error.
- **Mandatory** for any task involving "fixing" or "investigating" bugs.
- Trigger words: "debug code", "fix crash", "exception", "runtime error", "邏輯錯誤".

## 🛠️ The "System Awareness" Workflow (MANDATORY)

### Phase 1: Contextual Discovery
Before proposing any fix, you **MUST** map the system:
1.  **Trace Upstream**: Search for all callers of the affected module.
2.  **Trace Downstream**: Identify all functions, APIs, or UI components that consume the output of the affected module.
3.  **Identify State**: Check environment variables, database schemas, and global states involved.

### Phase 2: Generate `DEBUG_CONTEXT.md`
You are **FORBIDDEN** from modifying source code until this file is generated and confirmed by the user. Create it at the project root:

````markdown
# 🐞 Bug Analysis Report: [Short Title]

## 1. Problem Summary
- **Observed**: [What is happening?]
- **Expected**: [What should happen?]

## 2. System Context
- **⬆️ Upstream (Callers)**: Who triggers this? What are the inputs?
- **⬇️ Downstream (Impact)**: Who consumes this? What breaks if this changes?
- **🌍 Environment**: Relevant configs, OS, or hardware state (e.g., CUDA).

## 3. Logic Flow (Mermaid)
```mermaid
graph TD
  %% Visualize the data flow and the point of failure
```

## 4. Hypothesis & Binary Search
- **Hypothesis**: [Your best technical guess]
- **Elimination**: [What have you ruled out?]
````

### Phase 3: Pause & Confirm
After generating the file, stop and ask:
> "I have completed the System Awareness analysis in `DEBUG_CONTEXT.md`. Does this accurately reflect the logic? May I proceed with the fix?"

### Phase 4: Implementation & Regression Check
1.  **Atomic Fix**: Apply the minimal change required.
2.  **Verify**: Run the specific test case that failed.
3.  **Downstream Check**: Verify that the fix didn't break the components identified in Phase 2.

### Phase 5: Knowledge Feedback
Once fixed, trigger `conducting-postmortem` to update relevant `using-*` skills.

## 🛡️ Debugging Mindset
- **Assume Nothing**: Verify paths, types, and connections manually.
- **Binary Search**: Isolate the problem to Front-end vs Back-end, or Logic vs Data.
- **Observability**: Suggest adding telemetry/logging if the root cause is still opaque.

## 🧰 Tools & References
- grep / file content search: To find callers and references.
- read / view files: To analyze logic.
- write / edit files: To generate `DEBUG_CONTEXT.md`.
- execute shell commands: To run tests and check system logs.

### Detailed References
- [Debugging Principles](references/debugging_principles.md) — Mindsets and core strategies.
- [Mermaid Templates](references/mermaid_templates.md) — How to visualize failure points.

