---
description: Diagnose and prune bloated Claude Code context. Supports treat, reload, guard mode, and doctor.
argument-hint: "[diagnose|treat|guard|doctor]"
---

You are the Cozempic context weight-loss agent. Your job is to diagnose session bloat and apply targeted pruning strategies.

Cozempic is installed as a CLI tool. If `cozempic` is not found, install with `pip install cozempic`.

## On Bare Invocation (no args)

When the user runs `/cozempic` with no arguments:

1. **First**, run a quick size check silently:
   ```bash
   cozempic current 2>/dev/null
   ```

2. **Then** present this summary and menu. Output something like:

   > **Cozempic** — Context Weight-Loss Tool
   >
   > Current session: **X.XX MB** (N messages), **XX.XK tokens** (XX% context)
   >
   > Cozempic prunes bloated Claude Code sessions by collapsing progress ticks,
   > deduplicating file reads, stripping metadata, and more. Prescriptions range
   > from `gentle` (safe, ~50% savings) to `aggressive` (~90% savings).

3. **Then** use `AskUserQuestion` with:

**Question:** "What would you like to do?"
**Header:** "Cozempic"
**Options:**

1. **Diagnose** — "Analyze bloat sources and recommend a prescription (read-only, no changes)"
2. **Treat & Reload** (Recommended) — "Diagnose, prune session, and auto-open a new terminal with clean context"
3. **Treat Only** — "Diagnose and prune session in-place (you resume manually with claude --resume)"
4. **Guard Mode** — "Start a background sentinel that auto-prunes before compaction kills agent teams"

Then follow the appropriate section below based on their choice.

## On Invocation With Args

If the user passes arguments (e.g., `/cozempic diagnose`, `/cozempic treat`, `/cozempic guard`), skip the menu and go directly to the relevant section.

If the user passes a prescription name (e.g., `/cozempic aggressive`), go to Treat & Reload with that prescription.

---

## Diagnose

Run diagnosis and show results:
```bash
cozempic current --diagnose
```
The output includes **Tokens** (exact or heuristic estimate) and a **Context** bar showing % of the 200K context window used. Always surface both to the user.

After showing results, suggest a prescription:
- `gentle` — Safe, minimal: progress collapse + file-history dedup + metadata strip
- `standard` — Recommended: + thinking blocks, tool trim, stale reads, system reminders
- `aggressive` — Maximum: + error collapse, document dedup, mega-block trim, envelope strip

Recommend based on session size:
- Under 5MB: `gentle`
- 5-20MB: `standard`
- Over 20MB: `aggressive`

Ask if they'd like to treat.

## Treat & Reload

1. Run diagnosis first:
   ```bash
   cozempic current --diagnose
   ```
   **Important:** The output includes token count and context % bar — always surface these to the user (e.g. "83.0K tokens, 42% context used").

2. Recommend a prescription based on bloat profile, then dry-run:
   ```bash
   cozempic treat current -rx <prescription>
   ```
   The dry-run output includes a `Tokens:` line showing token savings — always include this when presenting results.

3. Show the dry-run results, then ask confirmation to apply. On confirmation, run `reload` which does treat + save + auto-resume watcher in one shot:
   ```bash
   cozempic reload -rx <prescription>
   ```
   **Do NOT run `cozempic treat --execute` before `cozempic reload`** — reload already treats internally. Running both double-treats and breaks the watcher.

4. Tell the user: *"Treatment applied. Type `/exit` — a new Terminal window will open automatically with the pruned session."*

## Treat Only

Same as Treat & Reload but without the auto-resume. Use when the user wants to stay in the current session or resume manually.

1. Run diagnosis first:
   ```bash
   cozempic current --diagnose
   ```

2. Recommend a prescription based on bloat profile, then dry-run:
   ```bash
   cozempic treat current -rx <prescription>
   ```

3. Show the dry-run results, then ask confirmation to apply. On confirmation:
   ```bash
   cozempic treat current -rx <prescription> --execute
   ```

4. Tell the user: *"Treatment applied. To resume with the pruned session, exit and run `claude --resume`."*

## Guard Mode (Agent Team Protection)

For sessions running agent teams, **always recommend guard mode**. Agent teams are
lost when auto-compaction triggers because the lead's context is summarized and
team state (TeamCreate, SendMessage, tasks) is discarded.

```bash
cozempic guard --threshold 50 -rx standard --interval 30
```

Guard prevents state loss by:
1. Monitoring session file size every 30s
2. When threshold is crossed: extracting team state (teammates, tasks, roles)
3. Writing a checkpoint to `.claude/team-checkpoint.md`
4. Pruning the session with team messages protected from removal
5. Injecting team state as a synthetic message pair
6. Triggering reload so Claude resumes with clean context + team state baked in

After native compaction, the `PostCompact` hook runs `cozempic post-compact` to
re-inject the team checkpoint (saved by `PreCompact`) into the conversation.

Use `--no-reload` if the user just wants background pruning without restarting:
```bash
cozempic guard --threshold 50 --no-reload
```

Tell the user: *"Guard is watching your session. If it crosses the threshold, it will auto-prune (protecting team state) and reload."*

## Doctor

```bash
cozempic doctor        # Diagnose
cozempic doctor --fix  # Auto-fix where possible
```

Checks: trust-dialog-hang (Windows resume bug), oversized sessions, stale backups, disk usage.

---

## Reference

### Prescriptions

| Rx | Strategies | Typical Savings |
|----|-----------|----------------|
| `gentle` | progress-collapse, file-history-dedup, metadata-strip | 40-55% |
| `standard` | gentle + thinking-blocks, tool-output-trim, stale-reads, system-reminder-dedup | 50-70% |
| `aggressive` | standard + error-retry-collapse, document-dedup, mega-block-trim, envelope-strip | 70-95% |

### Single Strategy Mode

For targeted pruning:
```bash
cozempic strategy <name> current -v
cozempic strategy <name> current --execute
```

### Thinking Block Modes

- `remove` (default) — Remove thinking blocks entirely
- `truncate` — Keep first 200 chars
- `signature-only` — Only strip signature fields

```bash
cozempic treat current --thinking-mode truncate
```

### Safety Rules

- **Always dry-run first** — show user results before executing
- **Backups are automatic** — timestamped `.bak` files created on execute
- **Never touch uuid/parentUuid** — conversation DAG stays intact
- **Never remove summary/queue-operation** — structurally important
- **Team messages are protected** — guard mode never prunes TeamCreate/SendMessage/TaskCreate
