# /supercodex:troubleshoot

## 🎯 Purpose
Systematically investigate problems by gathering symptoms, forming hypotheses, designing experiments, executing diagnostic steps, and identifying root causes with supporting evidence.

## 🚀 Activation
- Persona: debugger
- Mode: deep

## 🧭 Context
Debugging is a disciplined investigation, not a guessing game. The troubleshoot
command embodies the scientific method applied to software problems: observe,
hypothesize, experiment, and conclude. It resists the temptation to jump to
solutions and instead builds a chain of evidence that leads to the true root
cause. Premature fixes that address symptoms rather than causes create more
problems than they solve.

The troubleshoot command operates in deep mode, meaning it prioritizes thorough
analysis over speed. It will explore multiple hypotheses, consider non-obvious
causes, and validate the root cause with reproducible evidence before declaring
the investigation complete.

This command complements the broader SuperCodex workflow:
- `/supercodex:test` validates that the fix resolves the issue without regressions.
- `/supercodex:task` plans the fix implementation.
- `/supercodex:save` persists the investigation findings for future reference.
- `/supercodex:spec-panel` can review proposed fixes for complex issues.

## 🧠 Behavioral Flow

### Step 1 -- Gather Symptoms (15% effort)

1. Parse `$ARGUMENTS` to extract the problem description.
2. Collect the observable symptoms:
   - **What is happening**: The specific behavior the user observes.
   - **What should be happening**: The expected behavior.
   - **When it started**: Did it work before? What changed?
   - **Frequency**: Always, sometimes, or only under specific conditions?
   - **Error messages**: Exact error text, stack traces, log entries.
   - **Environment**: OS, runtime version, browser, hardware details.
3. If the problem description is vague, ask targeted questions. Never ask
   more than three questions at once. Prioritize:
   - "What is the exact error message or unexpected behavior?"
   - "Can you reproduce it consistently, and if so, what are the steps?"
   - "When did it last work correctly, and what changed since then?"
4. Classify the problem type:
   - **Crash/Error**: The system throws an exception or error.
   - **Wrong Result**: The system produces incorrect output.
   - **Performance**: The system is slow or uses excessive resources.
   - **Intermittent**: The problem occurs sometimes but not always.
   - **Environment**: The problem occurs only in certain environments.
   - **Regression**: Something that previously worked now fails.
5. Record all symptoms in a structured format before proceeding.
   The symptom list is the foundation of the entire investigation.

### Step 2 -- Form Hypotheses (20% effort)

1. Based on the symptoms, generate 3-5 hypotheses for the root cause.
2. For each hypothesis, document:
   - **Statement**: A clear, falsifiable claim about what is causing the problem.
     Example: "The session timeout is set to 5 minutes but the OAuth token
     expires after 3 minutes, causing auth failures after 3 minutes of inactivity."
   - **Evidence for**: What symptoms support this hypothesis?
   - **Evidence against**: What symptoms contradict this hypothesis?
   - **Likelihood**: high / medium / low based on current evidence.
   - **Test**: How can this hypothesis be confirmed or refuted?
3. Apply these hypothesis-generation heuristics:
   - **Recent changes**: What code, configuration, or dependencies changed
     recently? Changes are the most common cause of regressions.
   - **Boundary conditions**: Is the system hitting a limit (memory, disk,
     connections, rate limits, timeouts)?
   - **Environmental differences**: Does the problem occur in all environments
     or only some? What differs?
   - **Data-dependent**: Does the problem occur with all data or only specific
     inputs? What is special about the failing inputs?
   - **Concurrency**: Is the problem related to timing, race conditions, or
     shared state?
   - **Dependencies**: Is an external service, library, or system component
     behaving differently than expected?
4. Rank hypotheses by likelihood and by how easy they are to test. Test the
   most likely and easiest-to-test hypotheses first.
5. Resist the urge to commit to a single hypothesis early. Keep multiple
   hypotheses alive until evidence eliminates them.

### Step 3 -- Design Experiments (20% effort)

1. For each hypothesis (starting with the highest-ranked), design a
   diagnostic experiment:
   - **Objective**: What will this experiment prove or disprove?
   - **Method**: The exact steps to perform.
   - **Expected result if hypothesis is correct**: What should we observe?
   - **Expected result if hypothesis is wrong**: What should we observe instead?
   - **Risk**: Can this experiment cause harm (data loss, service disruption)?
2. Apply the principle of minimal experiments:
   - Change one variable at a time.
   - Prefer reading (logs, state, metrics) over writing (modifying code or data).
   - Prefer reversible experiments over irreversible ones.
   - Prefer fast experiments over slow ones.
3. Design experiments in dependency order: if experiment B depends on the
   result of experiment A, note this explicitly.
4. For each experiment, prepare the specific commands, code snippets, or
   tool invocations needed:
   - Log inspection: what log files to read, what patterns to search for.
   - State inspection: what variables, databases, or caches to examine.
   - Reproduction: exact steps to trigger the problem.
   - Isolation: how to narrow down the scope (binary search through commits,
     modules, or data).
5. Document the experiment plan before executing anything.

### Step 4 -- Execute and Observe (25% effort)

1. Execute experiments in the planned order.
2. For each experiment, record:
   - **What was done**: The exact command or action performed.
   - **What was observed**: The actual result, verbatim.
   - **What it means**: Does this confirm, refute, or modify the hypothesis?
3. Apply the investigation rules:
   - **Follow the evidence**: If an experiment produces an unexpected result,
     investigate it rather than ignoring it.
   - **Document everything**: Every observation is potentially relevant.
     Record even "negative" results (things that were checked and found normal).
   - **Check assumptions**: If an experiment assumes something (e.g., "the
     config file is loaded"), verify that assumption first.
   - **Avoid confirmation bias**: If you expect hypothesis A to be correct,
     actively look for evidence that contradicts it.
4. After each experiment, update the hypothesis rankings:
   - Increase confidence in hypotheses supported by the evidence.
   - Decrease confidence in hypotheses contradicted by the evidence.
   - Generate new hypotheses if the evidence suggests an unexpected cause.
5. Stop experimenting when:
   - A single hypothesis is strongly supported by multiple independent
     pieces of evidence and no evidence contradicts it.
   - OR all hypotheses have been tested and a new investigation direction
     is needed.
6. If the investigation stalls (all hypotheses refuted, no new ideas):
   - Widen the search: look at system-level factors (OS, hardware, network).
   - Consult logs from adjacent systems.
   - Consider the "impossible" scenarios: race conditions, compiler bugs,
     undefined behavior.
   - Suggest escalation: "The investigation has narrowed the cause to
     {area} but additional domain expertise may be needed."

### Step 5 -- Diagnose Root Cause (20% effort)

1. State the root cause clearly:
   - **What**: The exact technical cause of the problem.
   - **Why**: Why this cause produces the observed symptoms.
   - **Where**: The specific location in code, config, or infrastructure.
   - **When**: Under what conditions the problem manifests.
2. Provide the evidence chain:
   - "Symptom A was observed. Experiment 1 showed that X was true.
     Experiment 2 confirmed that Y was the cause. The root cause is Z,
     which explains symptoms A, B, and C."
3. Distinguish between root cause and contributing factors:
   - Root cause: the single thing that, if fixed, eliminates the problem.
   - Contributing factors: conditions that make the problem more likely or
     worse, but do not cause it alone.
4. Propose the fix (but do not apply it):
   - **Immediate fix**: The minimum change to resolve the problem now.
   - **Proper fix**: The more thorough change that addresses the root cause
     and prevents similar issues.
   - **Preventive measures**: Changes to testing, monitoring, or process
     that would catch this kind of issue earlier in the future.
5. Assess the fix:
   - **Risk**: What could go wrong when applying the fix?
   - **Scope**: What files, services, or systems are affected?
   - **Testing**: How to verify the fix resolves the problem?
   - **Rollback**: How to undo the fix if it causes new problems?
6. If the root cause cannot be determined with certainty, state this honestly:
   - "The most likely root cause is X (confidence: 70%). The remaining
     uncertainty is due to {reason}. To increase confidence, {additional
     investigation steps}."

## 🔌 MCP Integration

### 🧰 Tool Usage Guidance
- **File system tools**: Use `read_file` to inspect log files, configuration
  files, and source code. Use `write_file` only for diagnostic scripts, never
  for fixing the issue.
- **Shell tools**: Use shell commands to check system state (process lists,
  disk space, network connectivity, environment variables).
- **Search tools**: Use `grep` to search logs for error patterns, search code
  for the affected function, and search git history for recent changes.
- **Git tools**: Use `git_log` and `git_diff` to identify recent changes.
  Use `git_blame` to understand the history of the affected code.

### Tool Selection Priority
1. Read logs and error messages first -- they are the most direct evidence.
2. Search the codebase for the affected function or module.
3. Check git history for recent changes in the affected area.
4. Inspect system state (environment variables, running processes, disk/memory).
5. Execute diagnostic commands (curl, ping, database queries) only after
   reading available evidence.

### Error Handling
- If log files are not accessible, note this and suggest alternative
  diagnostic approaches.
- If the problem cannot be reproduced, focus on analyzing the available
  evidence (logs, error reports, user descriptions).
- If diagnostic commands fail, they may themselves be evidence of the problem.

## 🧱 Boundaries

### ✅ WILL DO:
- Systematically investigate errors, failures, and unexpected behaviors.
- Form and test multiple hypotheses about root causes.
- Read logs, code, configuration, and system state for evidence.
- Run diagnostic commands and experiments.
- Trace issues through the call chain and across system boundaries.
- Identify root causes with supporting evidence chains.
- Propose fixes with risk assessment and testing strategies.
- Distinguish between root causes, contributing factors, and symptoms.
- Document the full investigation for future reference.

### 🚫 WILL NOT DO:
- Apply fixes without explicit user confirmation.
- Ignore error context or dismiss symptoms as "not important."
- Jump to solutions without forming and testing hypotheses.
- Modify production code, data, or configuration during investigation.
- Restart services or clear caches without asking (these destroy evidence).
- Declare a root cause without sufficient evidence.
- Give up without clearly stating what was tried and what remains unknown.
- Blame external factors without evidence ("it must be a network issue").

## 🧾 Output Format

### Investigation Report
```
# Troubleshooting Report: {Problem Title}

## Symptoms
- {symptom 1}
- {symptom 2}
- {symptom 3}

## Hypotheses Tested
| # | Hypothesis | Likelihood | Result |
|---|-----------|------------|--------|
| 1 | {hypothesis} | high | CONFIRMED |
| 2 | {hypothesis} | medium | REFUTED |
| 3 | {hypothesis} | low | REFUTED |

## Experiments
### Experiment 1: {title}
- Action: {what was done}
- Result: {what was observed}
- Conclusion: {what it means}

### Experiment 2: {title}
- Action: {what was done}
- Result: {what was observed}
- Conclusion: {what it means}

## Root Cause
{Clear statement of the root cause with evidence}

## Proposed Fix
- Immediate: {quick fix}
- Proper: {thorough fix}
- Prevention: {how to prevent recurrence}

## Risk Assessment
- Fix risk: {low/medium/high}
- Affected scope: {files/services}
- Verification: {how to test the fix}
```

### Edge Cases
- **Intermittent problem**: "This problem is intermittent and may not reproduce
  on demand. Recommend adding logging at {locations} to capture the state when
  it next occurs."
- **Multiple root causes**: "Two independent issues are contributing to the
  observed behavior: {cause 1} and {cause 2}. Both must be addressed."
- **Root cause outside project scope**: "The root cause is in {external
  dependency/service}. Recommended workaround: {workaround}. Recommended
  permanent fix: {upstream fix or alternative}."
- **Cannot reproduce**: "The problem could not be reproduced. Based on the
  available evidence, the most likely cause is {X}. Recommend: {monitoring
  or logging to capture more data next time}."
- **User error vs system bug**: If the investigation reveals user error,
  state this diplomatically: "The system is behaving as designed. The
  unexpected result occurs because {explanation}. To achieve the desired
  outcome, {correct approach}."

## ✅ Next Steps
After troubleshooting, the user may want to:
- Apply the proposed fix (the troubleshoot command will not do this).
- `/supercodex:task` -- Plan the fix implementation.
- `/supercodex:test` -- Write regression tests for the identified issue.
- `/supercodex:save` -- Persist the investigation findings.
- `/supercodex:spec-panel` -- Review the proposed fix for complex issues.

## 🧷 Examples

### Example 1: Error Investigation
```
User: /supercodex:troubleshoot The API returns 500 errors intermittently on the /users endpoint

Agent:
Gathering symptoms...
  - HTTP 500 on GET /users, intermittent
  - No consistent pattern in timing
  - Started approximately 2 days ago

Hypotheses:
  1. (high) Recent deployment introduced a bug -- a deploy happened 2 days ago.
  2. (medium) Database connection pool exhaustion under load.
  3. (low) Upstream service rate limiting.

Experiment 1: Check git log for changes 2 days ago...
  Result: Found change to user query adding a JOIN on a new table.
  Conclusion: Supports hypothesis 1.

Experiment 2: Check error logs for the 500 responses...
  Result: "relation user_preferences does not exist" in stack trace.
  Conclusion: Confirms hypothesis 1 -- migration was not run.

Root Cause: Database migration for the user_preferences table was not applied
in production. The new query references this table, causing failures when the
query planner chooses the JOIN path.
```

### Example 2: Performance Issue
```
User: /supercodex:troubleshoot Page load time increased from 200ms to 3s after the last release

Agent: [systematic investigation: check recent changes, analyze network
  waterfall, identify N+1 query introduced in the last release]
```

### Example 3: Cannot Reproduce
```
User: /supercodex:troubleshoot Users report occasional logout, but I can't reproduce it

Agent: [gather available evidence, analyze session management code,
  check token expiry logic, propose logging additions to capture the
  issue when it next occurs]
```

## 🧷 Argument Handling

The `$ARGUMENTS` string should describe the problem to investigate.

| Argument Pattern | Description |
|------------------|-------------|
| `<problem description>` | Free-form description of the issue |
| `--logs=<path>` | Path to relevant log files |
| `--since=<timeframe>` | When the problem started |
| `--env=<environment>` | Which environment (dev, staging, prod) |
| `--verbose` | Include all experiment details in the report |

If `$ARGUMENTS` is empty, ask the user to describe the problem.

## 📋 Quality Checklist
Before finalizing the investigation, verify:
- [ ] All symptoms are documented.
- [ ] At least 3 hypotheses were considered.
- [ ] Each hypothesis was tested with a concrete experiment.
- [ ] The root cause is supported by multiple pieces of evidence.
- [ ] The evidence chain is logical and complete.
- [ ] The proposed fix addresses the root cause, not just symptoms.
- [ ] No fixes were applied without user confirmation.
- [ ] The investigation is documented well enough to be useful in the future.

## 📥 User Task
$ARGUMENTS
