---
name: codeflash-optimize
description: >-
  Profiles code, identifies bottlenecks, runs benchmarks, and applies targeted optimizations
  across CPU, async, memory, and codebase structure domains. Use when the user asks to
  "optimize my code", "start an optimization session", "resume optimization", "check
  optimization status", "make this faster", "reduce memory usage", "fix slow functions",
  "run performance experiments", "scan for performance issues", or "diagnose my code".
allowed-tools: "Agent, AskUserQuestion, Read, SendMessage"
argument-hint: "[start|resume|status|scan|review]"
---

Optimization session launcher. Launches the appropriate agent directly.

## For `start` (or no arguments)

**Step 1.** Use AskUserQuestion to ask:

> Before I start optimizing, is there anything I should know? For example: areas to avoid, known constraints, things you've already tried, or specific files to focus on. Or just say 'go' to proceed.

**Step 2.** After the user responds, launch the language router in the foreground:
- **Agent type:** `codeflash-python`
- **run_in_background:** `false`
- **Prompt:** The prompt must contain exactly three parts in this order, and nothing else:

Part 1 — the AUTONOMOUS MODE directive (copy verbatim):
```
AUTONOMOUS MODE: The user has already been asked for context (included below). Do NOT ask the user any questions — work fully autonomously. Make all decisions yourself: generate a run tag from today's date, identify benchmark tiers from available tests, choose optimization targets from profiler output. If something is ambiguous, pick the reasonable default and document your choice in HANDOFF.md.
```

Part 2 — the user's original request (verbatim).

Part 3 — the user's answer from Step 1 (verbatim).

Do not add any other instructions — the router sets up the project, creates the team, launches the optimizer in the background, and coordinates the session. Progress streams directly to the user.

## For `resume`

Launch the language router:
- **Agent type:** `codeflash-python`
- **run_in_background:** `false`
- **Prompt:** The directive below (verbatim), followed by `resume` and the user's request:

```
AUTONOMOUS MODE: Work fully autonomously. Do NOT ask the user any questions. Read session state from .codeflash/ and continue where the last session left off.
```

## For `status`

**If an optimizer agent is currently running** (the session was started or resumed earlier in this conversation): Use `SendMessage(to: "optimizer", summary: "Status request", message: "Report your current status: experiments run, keeps/discards, current target, cumulative improvement.")` and show the response to the user.

**Otherwise** (no active agent in this conversation): Read `.codeflash/results.tsv` and `.codeflash/HANDOFF.md` and show:
- Total experiments run (keeps vs discards)
- Current branch
- Best improvement achieved vs baseline
- What was planned next

## For `scan`

Quick cross-domain diagnosis. Profiles CPU, memory, import time, and async patterns in one pass without making any changes.

Launch the scan agent directly:
- **Agent type:** `codeflash-scan`
- **run_in_background:** `false` (wait for the result — scan is fast)
- **Prompt:** `scan` followed by the user's scope if specified (e.g., a specific test or file), otherwise just `scan`.

Show the scan report to the user. The report includes ranked targets across all domains and recommendations. If the user wants to proceed, they can run `/codeflash-optimize start`.

## For `review`

Launch the review agent directly:
- **Agent type:** `codeflash-review`
- **run_in_background:** `false` (wait for the result)
- **Prompt:** Include the user's request (branch name, PR number, or 'current changes') and any available context:

```
Review the following: <user's request>

## Session Context
<.codeflash/results.tsv contents if it exists>
<.codeflash/HANDOFF.md contents if it exists>
```

Show the verdict and key findings to the user.

## Mid-session steering

The router runs in the foreground coordinating the session. While it's active, its progress output streams directly to the user. If the user needs to interrupt (e.g., to change focus or stop early), they can press **Escape** or **Ctrl+C**. The optimizer (background) may survive the interruption — use `status` to check.

After an interruption, the user can relay feedback to a still-running optimizer:

```
SendMessage(to: "optimizer", summary: "User feedback",
            message: "<user's instruction verbatim>")
```

If no optimizer is currently running, tell the user there's no active session and suggest `/codeflash-optimize resume`.
