---
name: svelte-store-debugger
description: "Finds the cause of a misbehaving Svelte store and returns the corrected store and component code with a reason for every change, in a ticket-ready markdown report. Use when a Svelte store is not updating, a derived store shows a stale value, or a subscription fires at the wrong time and you need the cause and the fix. Do not use for writing a new store from scratch, documenting a working store, or Svelte 5 rune-only state that never touches a store. Related topics: coding, svelte."
---

# Svelte Store Debugger

Finds the cause of a misbehaving Svelte store and returns the corrected store and component code with a reason for every change, in a ticket-ready markdown report.

## When to use

Use this skill when a Svelte store is not updating, a derived store shows a stale value, or a subscription fires at the wrong time and you need the cause and the fix. Do not use it for writing a new store from scratch, documenting a working store, or Svelte 5 rune-only state that never touches a store.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Trace every read of the store in the supplied code and classify it as auto-subscription, manual subscribe, or one-off get, so each read's update behavior is known.
3. Trace every write and mark any direct mutation or same-reference set that never notifies subscribers; the list of faulty writes is complete.
4. For derived and custom stores, confirm the inputs, the return or set on every path, and the subscribe contract; each gap is recorded as an issue.
5. Rank the candidate causes by how well they explain the observed behavior and pick one as primary, with the check that would confirm it.
6. Write the corrected store and component code with imports, and note any Svelte version difference that changes the answer.
7. Assemble the five-section markdown report and remove anything that is not needed to reproduce or fix the problem.

## Inputs

- **Store name** (free text, e.g. "cartStore")
- **Store type** (choose one: writable, readable, derived, custom)
- **Store code and the component that uses it** (longer free text)
- **Expected behavior** (longer free text)
- **Observed behavior** (longer free text)
- **Svelte version** (choose one: Svelte 5 with runes, Svelte 4, Svelte 3, Not sure)
- **Debugging depth** (choose one: Quick check, Standard, Deep dive with runtime tracing)

## Output

A markdown document with Overview, Summary Table, Current State (code block with faulty lines marked), Identified Issues (bullets), and Recommendations (numbered fixes with code blocks), and nothing else.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Svelte Store Debugger

Diagnose the store below and return a markdown report I can paste into a ticket.

- Store name: {{Store name}}
- Store type: {{Store type}}
- Store code and the component that uses it: {{Store code and the component that uses it}}
- Expected behavior: {{Expected behavior}}
- Observed behavior: {{Observed behavior}}
- Svelte version: {{Svelte version}}
- Debugging depth: {{Debugging depth}}

Work from the symptom back to the cause. First trace how the store is read: a `$store` auto-subscription, a manual `subscribe`, or a one-off `get`, since a value read once at mount never updates. Then check how it is written: direct mutation of an object or array inside the store never notifies subscribers, so look for `.push`, property assignment, or `set` with the same reference. For derived stores, confirm every input is listed and the callback returns or calls `set` on every path. For custom stores, confirm the returned object exposes `subscribe` and that async work resolves after the component has unmounted. Flag manual subscriptions with no unsubscribe in `onDestroy`.

Rank the causes by how well each one explains the observed behavior, name the one you believe, and say what would confirm it (a console.log in a specific spot, or a specific test). Then give the corrected store and component code with the imports they need, and note anything that changes between Svelte versions, such as runes replacing stores for local state in Svelte 5.

### Output
Return only a markdown document with these sections: Overview (two sentences), Summary Table (symptom, likely cause, confidence), Current State (the store code with the faulty lines marked), Identified Issues (one bullet per issue with why it produces the symptom), Recommendations (numbered fixes with code blocks and imports). No commentary outside the document.

## Pitfalls

- Blaming reactivity when the store is read once with get() at mount, which never updates by design.
- Suggesting set(obj) with the same object reference, which still skips the notification Svelte relies on.
- Fixing the store while leaving a manual subscribe without onDestroy cleanup, which leaks and fires after unmount.
- Giving Svelte 4 store advice to a Svelte 5 runes codebase where $state would replace the store entirely.
