Diagnoses and fixes bugs using a test-first workflow (reproduce, diagnose, fix). Use this skill whenever the user reports a bug, describes unexpected behavior, or asks to investigate or fix a defect.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Fix Bug?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/nowsprinting-fix-bug)
---
name: fix-bug
description: >-
Diagnoses and fixes bugs using a test-first workflow (reproduce, diagnose, fix).
Use this skill whenever the user reports a bug, describes unexpected behavior, or asks to
investigate or fix a defect.
argument-hint: "[incident, failing test name, or issue URLs]"
license: Unlicense
metadata:
author: Koji Hasegawa
---
Guide for diagnosing and fixing bugs.
This skill defines a test-first debugging workflow: reproduce the bug with a failing test, diagnose the root cause, then fix it.
## Task Type Check
If the user's request is to implement a new feature, change a specification, or refactor rather than investigate or fix a bug, stop and guide the user to invoke the `/implement` skill instead.
## Input
Take the bug report from the `incident` argument: the report written directly, the name of a failing test, or the URLs of issues in an issue tracker, in any combination.
For an issue URL, fetch its body and comments (e.g., `gh issue view <URL> --comments` for GitHub Issues): the comments often correct the body.
Combine the argument with the bug report in the conversation, if any; when the argument is omitted, take it from the conversation alone.
## Workflow
**Recording implementation notes:** Notes for this run go in `<scratchpad>/implementation-notes`, where `<scratchpad>` is the scratchpad directory listed in your system prompt (write its absolute path literally): one note per line, in the form `<category>: <note>`.
Immediately before your **first** append in this run — and only then — delete that file if it exists (usually it will not), so this run starts from an empty one: every `/fix-bug` and `/implement` run in the session shares this file, so an earlier run abandoned before its Implementation Notes step would otherwise leak its notes into this one.
Never delete it again afterwards, including when Step 3 sends you back to Step 1 or Step 2; those loops are part of the same run and their notes must survive.
While working through Steps 1-8, whenever one of the following occurs, immediately append a line to that file — do not wait until the end to reconstruct these from memory:
- The confirmed Condition / Expected / Actual differs from what the user first reported, or the documentation conflicted with the report and you resolved which is correct → **Bug report clarifications**
- You established — or later revised — which line(s) or logic are responsible and why they misbehave, including when the fix actually applied differs from the one formulated in Step 5 → **Root cause**
- You investigated a suspected cause, code path, or reproduction approach and eliminated it → **Ruled out**
- You considered alternatives and chose one, and why → **Tradeoffs**
- You modified or deleted test code that was already committed, and why → **Test changes**
- A test design you take test cases from has Issues in its Assessment, or a test case marked unverifiable, undefined, or ambiguous → **Open questions**
Append with `Bash` — the `Write` tool cannot append:
```bash
cat >> "<scratchpad>/implementation-notes" <<'EOF'
Root cause: <one line>
EOF
```
When a step delegates to a subagent or another skill (the test design subagent in Step 6; `/simplify` and `/resolve-diagnostics` in Step 8), append the note yourself from what it returns — they do not write to this file.
### Step 1: Clarify the Bug Report
> Work from the user's report and the documents that state the specification (external specification, design docs) only — do not read code yet.
> Reading the implementation here biases "Expected" toward what the code does rather than what the specification says.
Extract the following from the bug report (see [Input](#input)):
- **Condition**: the setup or scenario that triggers the bug
- **Expected**: the expected behavior
- **Actual**: the observed behavior
If any of the three cannot be determined from the bug report, use `AskUserQuestion` to ask the user before proceeding.
All three must be known before moving to Step 2.
Also determine the **report type**:
- **Existing test failure** — the user reports that an existing test is failing.
The specific failing test method need not be known at this stage; note the scope (class name, scene name, or test assembly) from the bug report.
**Step 2 is skipped** — proceed directly to Step 3.
- **Behavioral bug** — the user describes unexpected runtime behavior with no mention of a failing test.
Proceed normally through Step 2.
Also check the relevant documentation (external specification, design docs) for consistency with the user's bug report.
If the documentation and the report conflict, use `AskUserQuestion` to clarify with the user which is correct.
If the user answers that the specification itself is to change, rather than that the documentation misstates it, the request is a specification change: stop and guide the user to invoke the `/implement` skill instead.
If the docs contain errors or are missing relevant information, note them; Step 9 writes the corrections into the docs.
Note the **test bases** of this bug fix: the issue URLs of the bug report, and the files and URLs of this documentation.
List them in each test file you write or modify in this run (`test-writing-guide`, Test Basis).
### Step 2: Write the Reproduction Test
> **Skip this step** if Step 1 identified this as an **existing test failure** case.
> Proceed directly to Step 3.
Search the project's test code for existing tests closest to the bug scenario.
These serve two purposes:
- Placement anchor — add the reproduction test nearby
- Style reference — follow the same test conventions
**When the bug condition involves on-screen display or a user-facing UI operation**, search for **integration tests with UI operations or visual verification tests** — the reproduction test must be at that test level, so the placement anchor and style reference must be too.
Use Explore agents to locate relevant test files and test cases.
Load the `test-designing-guide` skill — do not rely on automatic skill triggering — and design the reproduction test of the bug report from Step 1 (Condition / Expected / Actual) per its test design resource and its Reproduction tests resource.
Design no regression test cases: Step 6 designs them against the code the fix changes, which is unknown until Step 5.
Skip the Assessment; when the reproduction test case would be unverifiable, undefined, or ambiguous, return to Step 1 and re-clarify the bug report with the user via `AskUserQuestion`.
Then load the `test-writing-guide` and `code-writing-guide` skills — test code is code, so both guides apply; do not rely on automatic skill triggering — and implement the reproduction test.
Place it near the similar tests found above.
If an existing test is testing the wrong behavior (i.e., the test itself is buggy), rewrite that test to correctly reproduce the bug rather than adding a new one.
### Step 3: Verify the Reproduction Test Fails
Run tests using the `/run-tests` skill and verify that the reproduction test **fails**:
- **If a test was added in Step 2**: run that specific test.
- **If Step 2 was skipped (existing test failure)**: narrow down the test to run using the scope identified in Step 1 (e.g., a specific test class or assembly).
If narrowing down is not possible, run all tests.
If multiple tests fail and it is unclear which one corresponds to the reported bug, use `AskUserQuestion` to ask the user which test to focus on.
#### If the test does not fail (Step 2 path only)
Diagnose why the test stays green, and choose the next move from the cause: e.g., the test does not reach the condition, the bug occurs only on the player (see the `run-tests` skill, Run Tests on the Player), it depends on timing or on data the test does not reproduce, or the bug is already fixed on this branch.
If reproduction has been attempted **3 times** without success, return to **Step 1** and use `AskUserQuestion` to re-clarify the bug report with the user.
### Step 4: Confirm Reproduction with User
> **Skip this step** if Step 2 was skipped (existing test failure): the test that failed in Step 3 is the reproduction evidence, and no reproduction test was written to commit.
> Proceed directly to Step 5.
**Present the reproduction evidence to the user** via `AskUserQuestion` before proceeding.
Include:
- Reproduction test: file path and method name
- Test failure message (actual output from the test run)
Once the user confirms the reproduction is as expected:
1. Commit the reproduction test — if test code is modified in a later step, the integrity of Test First is compromised; commit here without fail so the diff remains verifiable
2. Proceed to Step 5
### Step 5: Diagnose & Formulate Fix
With the reproduction confirmed, find the root cause — the line(s) or logic responsible for the bug — and formulate a fix.
Before formulating it:
1. Write a reproduction test at the unit test level that calls the suspected code directly (per `test-writing-guide`; load it and `code-writing-guide` if Step 2 was skipped — do not rely on automatic skill triggering), and run it with `/run-tests` to verify it **fails** — this confirms the suspected cause before changing the production code, and pins the defect at the unit test level where the Step 2 test cannot locate it.
Take its expected result from the specification of the called code — the documentation from Step 1 — not from the code you traced to find the suspect: an expected result derived from the implementation passes whatever the implementation does; nor from its XML documentation comment, as `test-designing-guide`'s test design resource rules for the expected results.
If it passes, the suspect is wrong: delete the test, record it under **Ruled out**, and look for another cause.
Skip this item when the reproduction test from Steps 2-3 is already a unit test that calls the suspected code, or when the cause lies outside code a unit test can call (e.g., scene/prefab wiring or asset settings)
2. Commit the reproduction test of item 1, if it wrote one, for the same reason as Step 4
### Step 6: Regression Test Coverage
Before applying the fix, cover the adjacent behavior the fix could disturb:
1. Run a general-purpose subagent that loads the `test-designing-guide` skill — do not rely on automatic skill triggering — and, per its test design resource, designs the test cases of the specifications of the members the fix formulated in Step 5 changes, e.g., the methods that own the changed lines, and tags the test cases that existing tests implement (Existing Tests).
Give it the test target of each changed member, the member's specifications from the documentation of Step 1, and the user's instructions on the test techniques, if any (e.g., error guessing); have it return the test cases verbatim.
Quote each specification verbatim with its source, e.g., the file path and heading or the URL, together with the passages it depends on, e.g., the definitions of the terms it uses, and instruct the subagent not to read the source documents or follow the links in the quoted passages: a subagent reading a whole document would fill its context with the specifications of parts the fix does not change.
Designing in a subagent keeps the method bodies you read in Step 5 out of the test design: a test case derived from the implementation passes whatever the implementation does.
A changed member without a specification gets no regression test: record it as an unclear specification instead of deriving one from the code
2. Write the test code of the test cases (per `test-writing-guide`) and run them — they must **pass** (they test existing correct behavior, not the bug itself).
### Step 7: Apply Fix & Verify
1. Load the `code-writing-guide` skill — do not rely on automatic skill triggering
2. Apply the fix formulated in Step 5 to the production code
3. Run all affected tests using `/run-tests`
4. Confirm:
- The reproduction tests from Step 2 and Step 5 now **pass** (bug is fixed)
- All regression tests still **pass**
5. Commit production code fix to git (includes regression tests from Step 6 and any unavoidable changes to the reproduction test)
### Step 8: Refactoring
1. Run the Claude Code built-in `/simplify` skill (`Skill({skill: "simplify"})` — not a plugin skill) to apply quality improvements to the modified code
2. Run the test classes that hold the tests added and modified in this iteration with `/run-tests` and confirm **all pass**
3. Run `/resolve-diagnostics` with the files added or modified in this iteration — compute the list **after** `/simplify` finishes, so its edits are covered too
4. Commit all remaining changes to git
### Step 9: Feed Back the Fix to the Specification
When the fix changed what the design docs state, e.g., a method's contract, or Step 1 found errors or missing information in the documents that state the specification, including an expected behavior confirmed with the user that no document states, write the change into those documents, so that they agree with the fixed code and its tests:
- A file → edit the passage it concerns, in the document's own style; place a specification it adds beside the specifications it relates to.
Commit the edited files.
- An issue, or a document you cannot edit, e.g., a web page → list the change in your reply, with the passage it settles: the pull request of this fix carries it, so leave the issue as it is.
Tell the user which documents you changed.
### Step 10: Implementation Notes
1. `Read` `<scratchpad>/implementation-notes` if it exists
2. Report the notes to the user in chat under an `## Implementation Notes` heading — one bold sub-heading per category, each followed by a bullet list.
Always include all six categories in this order: **Bug report clarifications**, **Root cause**, **Ruled out**, **Tradeoffs**, **Test changes**, **Open questions**.
Write `- None` for any category with nothing recorded, and add any last-minute Open questions before finalizing.
If the file was never created, report every category as "None"
3. Delete the temporary notes file if it exists
### Step 11: Preventing Recurrence
Propose to the user, under a `## Preventing Recurrence` heading, measures that keep bugs similar to this one from recurring.
For example, static analysis: when the root cause lies in how the code is written or how a library or framework is used, e.g., `?.` on a destroyed `UnityEngine.Object`, an IDE inspection or a Roslyn analyzer may catch it, and the user can search for one with `/find-and-setup-analysis-rule`.
The proposal ends this run; leave acting on it to the user: the user reviews the report and creates the pull request of this fix first.