---
name: codeflash-optimize
description: >-
  Profiles code, identifies bottlenecks, runs benchmarks, and applies targeted optimizations
  across CPU, concurrency, memory, and codebase structure domains for Go projects. Use when
  the user asks to "optimize my code", "start an optimization session", "resume optimization",
  "check optimization status", "make this faster", "reduce allocations", "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 packages to focus on. Or just say 'go' to proceed.

**Step 2.** After the user responds, launch the deep agent directly:
- **Agent name:** `optimizer`
- **Agent type:** `codeflash-deep`
- **run_in_background:** `true`
- **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 agent has its own workflow.

## For `resume`

Launch the deep agent directly:
- **Agent name:** `optimizer`
- **Agent type:** `codeflash-deep`
- **run_in_background:** `true`
- **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, allocations, concurrency, and build time 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 package), 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

When the user wants to give feedback to a running optimizer (e.g., "tell it to skip function X", "focus on package Y", "stop after the next experiment"), use SendMessage to relay:

```
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`.
