Skip to content

Commit 66c9653

Browse files
authored
Merge pull request #40 from codeflash-ai/python-demo
Simplify Plugin
2 parents c75845f + 555b95f commit 66c9653

9 files changed

Lines changed: 367 additions & 566 deletions

File tree

.claude-plugin/marketplace.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
},
77
"metadata": {
88
"description": "Codeflash plugin for Claude Code — optimize code for performance (Python, Java, JavaScript, TypeScript)",
9-
"version": "0.2.0"
9+
"version": "0.3.0"
1010
},
1111
"plugins": [
1212
{
1313
"name": "codeflash",
1414
"description": "Run codeflash as a background agent to optimize code for performance (Python, Java, JavaScript, TypeScript)",
15-
"version": "0.2.0",
15+
"version": "0.3.0",
1616
"author": {
1717
"name": "Codeflash",
1818
"email": "support@codeflash.ai"

.claude-plugin/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codeflash",
33
"description": "Run codeflash as a background agent to optimize code for performance (Python, Java, JavaScript, TypeScript)",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"author": {
66
"name": "Codeflash",
77
"url": "https://codeflash.ai"

agents/optimizer.md

Lines changed: 0 additions & 380 deletions
This file was deleted.

commands/setup.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/suggest-optimize.sh

Lines changed: 110 additions & 95 deletions
Large diffs are not rendered by default.

skills/optimize/SKILL.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
name: optimize
33
description: Optimize Python, Java, JavaScript, or TypeScript code for performance using Codeflash
44
user-invocable: true
5-
argument-hint: "[--file] [--function] [--subagent]"
6-
context: fork
7-
agent: codeflash:optimizer
8-
allowed-tools: Task, Read, Grep, Glob, Bash(*codeflash*), Bash(git *)
5+
argument-hint: "[--file path] [--function name]"
6+
allowed-tools: ["Bash"]
97
---
108

11-
Optimize code using Codeflash.
9+
Run the `codeflash` CLI to optimize code for performance.
1210

13-
Pass the following to the optimizer agent:
11+
## Build the command
1412

15-
```
16-
Optimize code using the workflow in your system prompt.
13+
Start with: `codeflash --subagent`
1714

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

20-
If no arguments were provided, run codeflash without --file — it detects changed files itself.
21-
If a file path was provided without a function name, optimize all functions in that file.
22-
If both file and function were provided, optimize that specific function.
23-
Add the --subagent flag to the codeflash command.
24-
```
20+
## Execute
21+
22+
Run the command as a **non-blocking background** Bash call (`run_in_background: true`) with a **10-minute timeout** (`timeout: 600000`).
23+
24+
If the command fails (not installed, not authenticated, or missing config), invoke the `codeflash:setup` skill to resolve the issue, then retry.

skills/setup/SKILL.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
name: setup
3+
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\"."
4+
color: cyan
5+
tools: ["Read", "Glob", "Grep", "Bash", "Write", "Task"]
6+
---
7+
8+
# Codeflash Setup
9+
10+
Set up codeflash when it is missing, unauthenticated, or unconfigured. This skill is typically invoked as a fallback when running codeflash fails.
11+
12+
## Workflow
13+
14+
Run the following diagnostic checks and fix only the ones that fail.
15+
16+
### Check 1: Installation
17+
18+
```bash
19+
which codeflash
20+
```
21+
22+
If this fails, codeflash is not installed. Detect the project's package manager and install accordingly:
23+
24+
- If a `uv.lock` file exists or `pyproject.toml` uses `[tool.uv]`: run `uv add --dev codeflash`
25+
- Otherwise: run `pip install codeflash`
26+
27+
**Never** use `uv tool install` to install codeflash.
28+
29+
### Check 2: Authentication
30+
31+
```bash
32+
codeflash auth status
33+
```
34+
35+
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.
36+
37+
### Check 3: Project Configuration (Python)
38+
39+
```bash
40+
grep -rq '\[tool\.codeflash\]' $(git rev-parse --show-toplevel)/pyproject.toml 2>/dev/null
41+
```
42+
43+
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`.
44+
45+
- If a `pyproject.toml` exists but lacks `[tool.codeflash]`, run **Configuration Discovery (Python)** below and append the section.
46+
- If no `pyproject.toml` exists, run **Configuration Discovery (Python)** and create one at the git repository root.
47+
48+
#### Configuration Discovery (Python)
49+
50+
Perform the following discovery steps relative to the directory containing the target `pyproject.toml`:
51+
52+
**Discover module root:**
53+
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.
54+
55+
**Discover tests folder:**
56+
Find the relative path to the tests directory. Look for:
57+
1. Existing directories named `tests` or `test`
58+
2. Folders containing files matching `test_*.py`
59+
If no tests directory exists, default to `tests`.
60+
61+
**Write the configuration:**
62+
Append the `[tool.codeflash]` section to the target `pyproject.toml`. Use exactly this format:
63+
64+
```toml
65+
[tool.codeflash]
66+
# All paths are relative to this pyproject.toml's directory.
67+
module-root = "<discovered module root>"
68+
tests-root = "<discovered tests folder>"
69+
ignore-paths = ["dist", "**/node_modules", "**/__tests__"]
70+
formatter-cmds = ["disabled"]
71+
```
72+
73+
After writing, confirm the configuration with the user before proceeding.
74+
75+
### Check 3: Project Configuration (Javascript/Typescript)
76+
77+
```bash
78+
grep -rq 'codeflash' $(git rev-parse --show-toplevel)/package.json 2>/dev/null
79+
```
80+
81+
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`.
82+
83+
- If a `package.json` exists but lacks the `codeflash` key, run **Configuration Discovery (Javascript/Typescript)** below and append the section.
84+
- If no `package.json` exists, run **Configuration Discovery (Javascript/Typescript)** and create one at the git repository root.
85+
86+
#### Configuration Discovery (Javascript/Typescript)
87+
88+
Perform the following discovery steps relative to the directory containing the target `package.json`:
89+
90+
**Discover module root:**
91+
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.
92+
93+
**Discover tests folder:**
94+
Find the relative path to the tests directory. Look for:
95+
1. Existing directories named `tests`, `test`, `__tests__`, or `spec`
96+
2. Folders containing files matching `*.test.ts`, `*.test.js`, `*.spec.ts`, `*.spec.js`
97+
3. Test runner configuration in `package.json` (e.g., `jest.testMatch`, `jest.roots`) or config files (`jest.config.*`, `vitest.config.*`)
98+
If no tests directory exists, default to `tests`.
99+
100+
**Write the configuration:**
101+
Add a `codeflash` key to the target `package.json`. Use exactly this format:
102+
103+
```json
104+
{
105+
"codeflash": {
106+
"moduleRoot": "<discovered module root>",
107+
"testsRoot": "<discovered tests folder>",
108+
"ignorePaths": [],
109+
"formatterCmds": ["disabled"]
110+
}
111+
}
112+
```
113+
114+
Merge this into the existing `package.json` object — do not overwrite other fields. After writing, confirm the configuration with the user before proceeding.
115+
116+
## Permissions Setup
117+
118+
1. Check if `.claude/settings.json` exists in the project root (use `git rev-parse --show-toplevel` to find it).
119+
120+
2. If the file exists, read it and check if `Bash(*codeflash*)` is already in `permissions.allow`.
121+
122+
3. If already configured, tell the user: "Codeflash is already configured to run automatically. No changes needed."
123+
124+
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.
125+
126+
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."
127+
128+
## After Setup
129+
130+
Once all checks pass, inform the user that codeflash is ready, and they can retry their optimization.

tests/helpers/setup.bash

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,23 @@ run_hook() {
240240
env -u VIRTUAL_ENV "$@" bash "$SUGGEST_OPTIMIZE" < "$input_file"
241241
}
242242

243+
# Run hook with a custom transcript path (for multi-session tests).
244+
# Usage: run_hook_with_transcript <transcript_path> <stop_active> [ENV_VAR=value ...]
245+
run_hook_with_transcript() {
246+
local transcript="$1"
247+
local stop_active="${2:-false}"
248+
shift 2 || true
249+
250+
local input_file="$BATS_TEST_TMPDIR/hook_input.json"
251+
jq -nc \
252+
--arg tp "$transcript" \
253+
--argjson sa "$stop_active" \
254+
'{transcript_path: $tp, stop_hook_active: $sa}' > "$input_file"
255+
256+
cd "$REPO"
257+
env -u VIRTUAL_ENV "$@" bash "$SUGGEST_OPTIMIZE" < "$input_file"
258+
}
259+
243260
# ---------------------------------------------------------------------------
244261
# Assertions
245262
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)