Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
},
"metadata": {
"description": "Codeflash plugin for Claude Code — optimize code for performance (Python, Java, JavaScript, TypeScript)",
"version": "0.2.0"
"version": "0.3.0"
},
"plugins": [
{
"name": "codeflash",
"description": "Run codeflash as a background agent to optimize code for performance (Python, Java, JavaScript, TypeScript)",
"version": "0.2.0",
"version": "0.3.0",
"author": {
"name": "Codeflash",
"email": "support@codeflash.ai"
Expand Down
2 changes: 1 addition & 1 deletion .claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codeflash",
"description": "Run codeflash as a background agent to optimize code for performance (Python, Java, JavaScript, TypeScript)",
"version": "0.2.0",
"version": "0.3.0",
"author": {
"name": "Codeflash",
"url": "https://codeflash.ai"
Expand Down
380 changes: 0 additions & 380 deletions agents/optimizer.md

This file was deleted.

19 changes: 0 additions & 19 deletions commands/setup.md

This file was deleted.

205 changes: 110 additions & 95 deletions scripts/suggest-optimize.sh

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions skills/optimize/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
name: optimize
description: Optimize Python, Java, JavaScript, or TypeScript code for performance using Codeflash
user-invocable: true
argument-hint: "[--file] [--function] [--subagent]"
context: fork
agent: codeflash:optimizer
allowed-tools: Task, Read, Grep, Glob, Bash(*codeflash*), Bash(git *)
argument-hint: "[--file path] [--function name]"
allowed-tools: ["Bash"]
---

Optimize code using Codeflash.
Run the `codeflash` CLI to optimize code for performance.

Pass the following to the optimizer agent:
## Build the command

```
Optimize code using the workflow in your system prompt.
Start with: `codeflash --subagent`

Arguments: $ARGUMENTS
Then add flags based on `$ARGUMENTS`:
- If a `--file` path was provided: add `--file <path>`
- If a `--function` name was also provided: add `--function <name>`
- If no arguments were provided, run `codeflash --subagent` as-is (it detects changed files automatically)

If no arguments were provided, run codeflash without --file — it detects changed files itself.
If a file path was provided without a function name, optimize all functions in that file.
If both file and function were provided, optimize that specific function.
Add the --subagent flag to the codeflash command.
```
## Execute

Run the command as a **non-blocking background** Bash call (`run_in_background: true`) with a **10-minute timeout** (`timeout: 600000`).

If the command fails (not installed, not authenticated, or missing config), invoke the `codeflash:setup` skill to resolve the issue, then retry.
130 changes: 130 additions & 0 deletions skills/setup/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
name: setup
description: "This skill should be used when codeflash fails to run due to missing installation, authentication issues, or missing project configuration. It handles installing codeflash (via pip or uv), authenticating, and configuring pyproject.toml. Trigger phrases: \"setup codeflash\", \"configure codeflash\", \"codeflash is not installed\", \"codeflash auth failed\", \"fix codeflash setup\"."
color: cyan
tools: ["Read", "Glob", "Grep", "Bash", "Write", "Task"]
---

# Codeflash Setup

Set up codeflash when it is missing, unauthenticated, or unconfigured. This skill is typically invoked as a fallback when running codeflash fails.

## Workflow

Run the following diagnostic checks and fix only the ones that fail.

### Check 1: Installation

```bash
which codeflash
```

If this fails, codeflash is not installed. Detect the project's package manager and install accordingly:

- If a `uv.lock` file exists or `pyproject.toml` uses `[tool.uv]`: run `uv add --dev codeflash`
- Otherwise: run `pip install codeflash`

**Never** use `uv tool install` to install codeflash.

### Check 2: Authentication

```bash
codeflash auth status
```

If this fails, the user is not authenticated. Run `codeflash auth login` interactively. This requires user interaction, so let them know the login flow is starting.

### Check 3: Project Configuration (Python)

```bash
grep -rq '\[tool\.codeflash\]' $(git rev-parse --show-toplevel)/pyproject.toml 2>/dev/null
```

If this fails, the project configuration is missing. Walk upward from the current working directory to the git repository root, looking for a `pyproject.toml`.

- If a `pyproject.toml` exists but lacks `[tool.codeflash]`, run **Configuration Discovery (Python)** below and append the section.
- If no `pyproject.toml` exists, run **Configuration Discovery (Python)** and create one at the git repository root.

#### Configuration Discovery (Python)

Perform the following discovery steps relative to the directory containing the target `pyproject.toml`:

**Discover module root:**
Find the relative path to the root of the Python module. The module root is where tests import from. For example, if the module root is `abc/` then tests would import code as `from abc import xyz`. Look for directories containing `__init__.py` files at the top level. Common patterns: `src/package_name/`, `package_name/`, or the project root itself.

**Discover tests folder:**
Find the relative path to the tests directory. Look for:
1. Existing directories named `tests` or `test`
2. Folders containing files matching `test_*.py`
If no tests directory exists, default to `tests`.

**Write the configuration:**
Append the `[tool.codeflash]` section to the target `pyproject.toml`. Use exactly this format:

```toml
[tool.codeflash]
# All paths are relative to this pyproject.toml's directory.
module-root = "<discovered module root>"
tests-root = "<discovered tests folder>"
ignore-paths = ["dist", "**/node_modules", "**/__tests__"]
formatter-cmds = ["disabled"]
```

After writing, confirm the configuration with the user before proceeding.

### Check 3: Project Configuration (Javascript/Typescript)

```bash
grep -rq 'codeflash' $(git rev-parse --show-toplevel)/package.json 2>/dev/null
```

If this fails, the project configuration is missing. Walk upward from the current working directory to the git repository root, looking for a `package.json`.

- If a `package.json` exists but lacks the `codeflash` key, run **Configuration Discovery (Javascript/Typescript)** below and append the section.
- If no `package.json` exists, run **Configuration Discovery (Javascript/Typescript)** and create one at the git repository root.

#### Configuration Discovery (Javascript/Typescript)

Perform the following discovery steps relative to the directory containing the target `package.json`:

**Discover module root:**
Find the relative path to the root of the source code. The module root is where the main application or library code lives. Look for the `main`, `module`, or `exports` fields in `package.json` for hints. Common patterns: `src/`, `lib/`, `dist/` (for compiled output — prefer the source directory), or the project root itself. If a `tsconfig.json` exists, check its `rootDir` or `include` fields for guidance.

**Discover tests folder:**
Find the relative path to the tests directory. Look for:
1. Existing directories named `tests`, `test`, `__tests__`, or `spec`
2. Folders containing files matching `*.test.ts`, `*.test.js`, `*.spec.ts`, `*.spec.js`
3. Test runner configuration in `package.json` (e.g., `jest.testMatch`, `jest.roots`) or config files (`jest.config.*`, `vitest.config.*`)
If no tests directory exists, default to `tests`.

**Write the configuration:**
Add a `codeflash` key to the target `package.json`. Use exactly this format:

```json
{
"codeflash": {
"moduleRoot": "<discovered module root>",
"testsRoot": "<discovered tests folder>",
"ignorePaths": [],
"formatterCmds": ["disabled"]
}
}
```

Merge this into the existing `package.json` object — do not overwrite other fields. After writing, confirm the configuration with the user before proceeding.

## Permissions Setup

1. Check if `.claude/settings.json` exists in the project root (use `git rev-parse --show-toplevel` to find it).

2. If the file exists, read it and check if `Bash(*codeflash*)` is already in `permissions.allow`.

3. If already configured, tell the user: "Codeflash is already configured to run automatically. No changes needed."

4. If not configured, add `Bash(*codeflash*)` to the `permissions.allow` array in `.claude/settings.json`. Create the file and any necessary parent directories if they don't exist. Preserve any existing settings.

5. Confirm to the user what was added and explain: "Codeflash will now run automatically in the background after commits that change code files, without prompting for permission each time."

## After Setup

Once all checks pass, inform the user that codeflash is ready, and they can retry their optimization.
17 changes: 17 additions & 0 deletions tests/helpers/setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ run_hook() {
env -u VIRTUAL_ENV "$@" bash "$SUGGEST_OPTIMIZE" < "$input_file"
}

# Run hook with a custom transcript path (for multi-session tests).
# Usage: run_hook_with_transcript <transcript_path> <stop_active> [ENV_VAR=value ...]
run_hook_with_transcript() {
local transcript="$1"
local stop_active="${2:-false}"
shift 2 || true

local input_file="$BATS_TEST_TMPDIR/hook_input.json"
jq -nc \
--arg tp "$transcript" \
--argjson sa "$stop_active" \
'{transcript_path: $tp, stop_hook_active: $sa}' > "$input_file"

cd "$REPO"
env -u VIRTUAL_ENV "$@" bash "$SUGGEST_OPTIMIZE" < "$input_file"
}

# ---------------------------------------------------------------------------
# Assertions
# ---------------------------------------------------------------------------
Expand Down
Loading
Loading