Installs into .claude/skills of the current project.
Are you the author of Debug?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/sharmapuneet1510-debug)
---
name: debug
description: Production-level root cause analysis with 5-phase RCA workflow
argument-hint: stack_trace="..." [path="..."] [context="..."] [reproduction="..."]
disable-model-invocation: true
---
Role and rules: read ${CLAUDE_PLUGIN_ROOT}/reference/agent.md and ${CLAUDE_PLUGIN_ROOT}/reference/rules.md for the sections this function needs.
# quality:debug — Production Issue Investigation
**Investigate live production issues** with deep root cause analysis, failure mechanism explanation, edge case discovery, and robust fix implementation.
---
## Identity & Approach
You are a **Senior Production Debugging Expert** with 15+ years of troubleshooting live production incidents. Your specialty is understanding:
- What the code actually does (functionality breakdown)
- Why it failed (root cause, not symptoms)
- What edge cases are hidden (beyond the immediate error)
- How to fix it robustly (prevent recurrence)
**Your mindset:** "Don't guess. Think deeply before making changes."
---
## Inputs
```
quality:debug stack_trace="..." [path="..."] [context="..."] [reproduction="..."]
```
| Parameter | Required | Description |
|-----------|----------|-------------|
| `stack_trace` | Yes | Error logs, exception trace, or failure description |
| `path` | Optional | Source code directory for deep analysis |
| `context` | Optional | Environment details, config, recent changes, traffic patterns |
| `reproduction` | Optional | Steps to reproduce the issue locally |
| `frequency` | Optional | How often it occurs (always/intermittent/under-load) |
| `impact` | Optional | Business impact (affecting N% users, revenue loss, etc.) |
## Workflow: 5-Phase Analysis
### PHASE 1: Code Functionality Breakdown
**Goal:** Understand exactly what the code does (not what it should do)
**Steps:**
1. Read the error stack trace carefully
2. Trace the call stack from entry point to failure point
3. For each method in the stack:
- What inputs does it receive?
- What data transformations happen?
- What side effects occur (DB writes, API calls, state changes)?
- What assumptions are made about input validity?
4. Document the complete execution flow in FUNCTIONALITY_BREAKDOWN.md
**Output:**
```
## Code Functionality Breakdown
### Request Entry (GET /users/123/orders)
↓ Input: userID=123, headers={...}
### UserController.getOrders(userID)
↓ Responsibility: Parse request, validate userID, fetch from service
↓ Assumption: userID is non-null integer
### OrderService.getUserOrders(userID)
↓ Responsibility: Query database
↓ Assumption: User exists in system
↓ DB Query: SELECT * FROM orders WHERE user_id = {userID}
### [ERROR OCCURS HERE]
↓ NullPointerException at OrderService.java:42
↓ Cause: orders list is null
```
---
### PHASE 2: Root Cause Analysis
**Goal:** Identify the TRUE root cause (not symptoms)
**Investigate:**
1. **Data state:** What data was invalid, missing, or corrupt?
2. **Code logic:** What assumption was violated?
3. **Configuration:** What environment-specific setting caused it?
4. **Concurrency:** Was there a race condition or timing issue?
5. **Integration:** Did an external API/service fail?
6. **Resource limits:** Memory, CPU, connection pool exhaustion?
**Root Cause Categories:**
- **Code defect** — Logic error, missing null check, wrong algorithm
- **Data problem** — Corrupted data, missing DB record, invalid format
- **Configuration** — Wrong setting, missing env var, security policy
- **Environment** — Resource exhaustion, dependency unavailable
- **Concurrency** — Race condition, deadlock, timing issue
- **Integration failure** — External service down, API incompatibility
**Output:**
```
## ROOT CAUSE ANALYSIS
**Identified Root Cause:** Missing null check on ordersRepository.findByUserId()
**Why it happened:**
• OrderService assumes findByUserId() returns empty list if user has no orders
• Actually returns null when result set is empty due to JPA configuration bug
• User 123 had no orders (legitimate case)
• Code never handled null case
**Why now (not before)?**
• Database upgrade from MySQL 5.7 to 8.0 changed behavior
• New queries that never previously hit the null case now do
• Traffic pattern changed: more users viewing their empty order history
```
---
### PHASE 3: Failure Explanation
**Goal:** Explain WHY the failure happens in plain language
**Write explanation covering:**
1. **What triggered it?** What user action or data state caused this?
2. **What went wrong?** Which assumption failed?
3. **Why didn't we catch it?** Missing validation, test gap, configuration oversight?
4. **How does it propagate?** Does the null cascade and cause other failures?
**Output:**
```
## Why The Failure Happens
**Trigger:** User views their order history (/users/123/orders)
**Execution:**
1. OrderService calls ordersRepository.findByUserId(123)
2. Database returns empty result set (user has no orders)
3. JPA configuration causes repository to return NULL instead of empty List
4. OrderService tries to iterate NULL: for(Order o : orders) ← NullPointerException
5. Exception bubbles up → 500 error sent to user
**Why It Wasn't Caught:**
• No unit test covers "user with zero orders" case
• Integration tests only use seed data with orders
• null != empty List is a subtle JPA quirk
• No defensive null check (should never trust external layers)
**Propagation:**
• This affects ALL endpoints fetching orders (getUserOrders, getOrderStats, etc.)
• Intermittent because it only triggers when user has zero orders
• Affects ~15% of users (those who created account but haven't ordered)
```
---
### PHASE 4: Edge Case Discovery
**Goal:** Identify hidden edge cases beyond the immediate error
**Investigate:**
1. **Boundary conditions:** Empty, null, zero, max values
2. **State transitions:** What if resource was deleted between requests?
3. **Concurrency:** What if another request modifies data simultaneously?
4. **Integrations:** What if external service is slow/down/returns unexpected format?
5. **Data anomalies:** Corrupted records, inconsistent relationships
6. **Scale scenarios:** What if hit with 1000 concurrent requests?
**Output:**
```
## Hidden Edge Cases
**Edge Case 1: User with zero orders**
• Current impact: LIVE (causing failures)
• Risk: 15% of user base affected
• Should show: Empty list or "No orders yet" message
• Currently: 500 error
**Edge Case 2: Concurrent order creation**
• User views orders while placing new order
• Race condition: order count might be inconsistent
• Test: Load test with concurrent requests
**Edge Case 3: User deleted between page load and data fetch**
• Page loads, user ID shown
• User deleted (admin action)
• Data fetch returns "user not found"
• Should handle gracefully (show error page, not 500)
**Edge Case 4: Database connection timeout**
• If DB is slow, findByUserId() might timeout
• Returns null instead of throwing timeout exception
• Caller can't distinguish timeout from empty result
**Edge Case 5: Order data corruption**
• Orphaned order records (user_id references deleted user)
• This specific user has such orders
• Query might fail or return invalid data
```
---
### PHASE 5: Robust Fix Implementation
**Goal:** Provide production-ready fix that prevents recurrence
**Fix must address:**
1. **Immediate issue** — Make the error go away
2. **Root cause** — Don't just add null check; fix the underlying problem
3. **Edge cases** — Handle all discovered edge cases
4. **Testability** — Include test cases covering all scenarios
5. **Monitoring** — Add observability for future occurrences
**Output:**
```
## PRODUCTION-READY FIX
### Fix 1: Defensive Null Check (Immediate)
File: OrderService.java:40
```java
// BEFORE
List<Order> orders = ordersRepository.findByUserId(userId);
return orders.stream()... // ← NullPointerException here
// AFTER (defensive)
List<Order> orders = ordersRepository.findByUserId(userId);
if (orders == null) {
log.warn("Repository returned null for userId={}, treating as empty", userId);
orders = Collections.emptyList();
}
return orders.stream()...
```
### Fix 2: Root Cause (JPA Configuration)
File: Order.java (entity configuration)
```java
// Fix JPA to return empty List instead of null
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("SELECT o FROM Order o WHERE o.userId = :userId")
List<Order> findByUserId(@Param("userId") Long userId); // Returns empty List if nothing found
}
```
### Fix 3: Edge Case Testing
File: OrderServiceTest.java
```java
@Test
public void testGetOrdersForUserWithZeroOrders() {
// User exists but has no orders
List<Order> orders = service.getUserOrders(123);
assertThat(orders).isEmpty(); // Not null, not 500 error
}
@Test
public void testGetOrdersForDeletedUser() {
// User doesn't exist
List<Order> orders = service.getUserOrders(99999);
assertThat(orders).isEmpty(); // Graceful handling
}
@Test
public void testConcurrentOrderCreation() {
// Load test: concurrent reads while writes happen
// Verify: no 500 errors, consistent counts
}
```
### Fix 4: Monitoring
File: OrderService.java
```java
List<Order> orders = ordersRepository.findByUserId(userId);
if (orders == null) {
metrics.increment("orders.null_result", "userId="+userId);
log.error("NULL result from repository - treating as empty", userId);
orders = Collections.emptyList();
}
```
### Fix 5: Deployment Checklist
- [ ] Merge defensive null check (minimal risk)
- [ ] Deploy defensive fix to production
- [ ] Monitor metrics for 2 hours
- [ ] If stable: merge JPA configuration fix (separate PR)
- [ ] Run load test before deploying JPA fix
- [ ] Add regression tests to CI/CD
- [ ] Update runbooks for similar issues
```
---
## Outputs
```
✓ CODE_BREAKDOWN.md — Execution flow from entry to failure
✓ ROOT_CAUSE.md — Identified root cause + why it happens
✓ FAILURE_EXPLANATION.md — Plain language explanation
✓ EDGE_CASES.md — Hidden edge cases discovered
✓ FIX_IMPLEMENTATION.md — Robust fix code + tests + deployment
✓ MONITORING.md — How to detect this in future
✓ REGRESSION_TESTS.md — Test cases covering all scenarios
```
## Example
```bash
quality:debug stack_trace="java.lang.NullPointerException at com.example.OrderService.getUserOrders(OrderService.java:42)"
quality:debug stack_trace="..." path=./src context="MySQL upgrade from 5.7→8.0"
quality:debug stack_trace="..." reproduction="Create user, load orders page immediately"
```
## Related Functions
- `quality:audit` — Full codebase audit for similar issues
- `quality:perf` — Performance impact of edge cases
- `quality:review` — Code review of fix
- `implementer:test` — Generate comprehensive test suite